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 unsigned OperandNo = Use.getOperandNo();
6253 // Check if this use is insensitive to the sign of zero
6254 switch (User->getOpcode()) {
6255 case ISD::SETCC:
6256 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6257 case ISD::FABS:
6258 // fabs always produces +0.0.
6259 return true;
6260 case ISD::FCOPYSIGN:
6261 // copysign overwrites the sign bit of the first operand.
6262 return OperandNo == 0;
6263 case ISD::FADD:
6264 case ISD::FSUB: {
6265 // Arithmetic with non-zero constants fixes the uncertainty around the
6266 // sign bit.
6267 SDValue Other = User->getOperand(1 - OperandNo);
6269 }
6270 case ISD::FP_TO_SINT:
6271 case ISD::FP_TO_UINT:
6272 // fp-to-int conversions normalize signed zeros.
6273 return true;
6274 default:
6275 return false;
6276 }
6277}
6278
6280 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6281 // regression. Ideally, this should be implemented as a demanded-bits
6282 // optimization that stems from the users.
6283 if (Op->use_size() > 2)
6284 return false;
6285 return all_of(Op->uses(),
6286 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6287}
6288
6290 // Check the obvious case.
6291 if (A == B) return true;
6292
6293 // For negative and positive zero.
6296 if (CA->isZero() && CB->isZero()) return true;
6297
6298 // Otherwise they may not be equal.
6299 return false;
6300}
6301
6302// Only bits set in Mask must be negated, other bits may be arbitrary.
6304 if (isBitwiseNot(V, AllowUndefs))
6305 return V.getOperand(0);
6306
6307 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6308 // bits in the non-extended part.
6309 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6310 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6311 return SDValue();
6312 SDValue ExtArg = V.getOperand(0);
6313 if (ExtArg.getScalarValueSizeInBits() >=
6314 MaskC->getAPIntValue().getActiveBits() &&
6315 isBitwiseNot(ExtArg, AllowUndefs) &&
6316 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6317 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6318 return ExtArg.getOperand(0).getOperand(0);
6319 return SDValue();
6320}
6321
6323 // Match masked merge pattern (X & ~M) op (Y & M)
6324 // Including degenerate case (X & ~M) op M
6325 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6326 SDValue Other) {
6327 if (SDValue NotOperand =
6328 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6329 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6330 NotOperand->getOpcode() == ISD::TRUNCATE)
6331 NotOperand = NotOperand->getOperand(0);
6332
6333 if (Other == NotOperand)
6334 return true;
6335 if (Other->getOpcode() == ISD::AND)
6336 return NotOperand == Other->getOperand(0) ||
6337 NotOperand == Other->getOperand(1);
6338 }
6339 return false;
6340 };
6341
6342 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6343 A = A->getOperand(0);
6344
6345 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6346 B = B->getOperand(0);
6347
6348 if (A->getOpcode() == ISD::AND)
6349 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6350 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6351 return false;
6352}
6353
6354// FIXME: unify with llvm::haveNoCommonBitsSet.
6356 assert(A.getValueType() == B.getValueType() &&
6357 "Values must have the same type");
6360 return true;
6363}
6364
6365static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6366 SelectionDAG &DAG) {
6367 if (cast<ConstantSDNode>(Step)->isZero())
6368 return DAG.getConstant(0, DL, VT);
6369
6370 return SDValue();
6371}
6372
6375 SelectionDAG &DAG) {
6376 int NumOps = Ops.size();
6377 assert(NumOps != 0 && "Can't build an empty vector!");
6378 assert(!VT.isScalableVector() &&
6379 "BUILD_VECTOR cannot be used with scalable types");
6380 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6381 "Incorrect element count in BUILD_VECTOR!");
6382
6383 // BUILD_VECTOR of UNDEFs is UNDEF.
6384 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6385 return DAG.getUNDEF(VT);
6386
6387 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6388 SDValue IdentitySrc;
6389 bool IsIdentity = true;
6390 for (int i = 0; i != NumOps; ++i) {
6392 Ops[i].getOperand(0).getValueType() != VT ||
6393 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6394 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6395 Ops[i].getConstantOperandAPInt(1) != i) {
6396 IsIdentity = false;
6397 break;
6398 }
6399 IdentitySrc = Ops[i].getOperand(0);
6400 }
6401 if (IsIdentity)
6402 return IdentitySrc;
6403
6404 return SDValue();
6405}
6406
6407/// Try to simplify vector concatenation to an input value, undef, or build
6408/// vector.
6411 SelectionDAG &DAG) {
6412 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6414 [Ops](SDValue Op) {
6415 return Ops[0].getValueType() == Op.getValueType();
6416 }) &&
6417 "Concatenation of vectors with inconsistent value types!");
6418 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6419 VT.getVectorElementCount() &&
6420 "Incorrect element count in vector concatenation!");
6421
6422 if (Ops.size() == 1)
6423 return Ops[0];
6424
6425 // Concat of UNDEFs is UNDEF.
6426 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6427 return DAG.getUNDEF(VT);
6428
6429 // Scan the operands and look for extract operations from a single source
6430 // that correspond to insertion at the same location via this concatenation:
6431 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6432 SDValue IdentitySrc;
6433 bool IsIdentity = true;
6434 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6435 SDValue Op = Ops[i];
6436 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6437 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6438 Op.getOperand(0).getValueType() != VT ||
6439 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6440 Op.getConstantOperandVal(1) != IdentityIndex) {
6441 IsIdentity = false;
6442 break;
6443 }
6444 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6445 "Unexpected identity source vector for concat of extracts");
6446 IdentitySrc = Op.getOperand(0);
6447 }
6448 if (IsIdentity) {
6449 assert(IdentitySrc && "Failed to set source vector of extracts");
6450 return IdentitySrc;
6451 }
6452
6453 // The code below this point is only designed to work for fixed width
6454 // vectors, so we bail out for now.
6455 if (VT.isScalableVector())
6456 return SDValue();
6457
6458 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6459 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6460 // BUILD_VECTOR.
6461 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6462 EVT SVT = VT.getScalarType();
6464 for (SDValue Op : Ops) {
6465 EVT OpVT = Op.getValueType();
6466 if (Op.isUndef())
6467 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6468 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6469 Elts.append(Op->op_begin(), Op->op_end());
6470 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6471 OpVT.getVectorNumElements() == 1 &&
6472 isNullConstant(Op.getOperand(2)))
6473 Elts.push_back(Op.getOperand(1));
6474 else
6475 return SDValue();
6476 }
6477
6478 // BUILD_VECTOR requires all inputs to be of the same type, find the
6479 // maximum type and extend them all.
6480 for (SDValue Op : Elts)
6481 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6482
6483 if (SVT.bitsGT(VT.getScalarType())) {
6484 for (SDValue &Op : Elts) {
6485 if (Op.isUndef())
6486 Op = DAG.getUNDEF(SVT);
6487 else
6488 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6489 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6490 : DAG.getSExtOrTrunc(Op, DL, SVT);
6491 }
6492 }
6493
6494 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6495 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6496 return V;
6497}
6498
6499/// Gets or creates the specified node.
6500SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6501 SDVTList VTs = getVTList(VT);
6503 AddNodeIDNode(ID, Opcode, VTs, {});
6504 void *IP = nullptr;
6505 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6506 return SDValue(E, 0);
6507
6508 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6509 CSEMap.InsertNode(N, IP);
6510
6511 InsertNode(N);
6512 SDValue V = SDValue(N, 0);
6513 NewSDValueDbgMsg(V, "Creating new node: ", this);
6514 return V;
6515}
6516
6517SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6518 SDValue N1) {
6519 SDNodeFlags Flags;
6520 if (Inserter)
6521 Flags = Inserter->getFlags();
6522 return getNode(Opcode, DL, VT, N1, Flags);
6523}
6524
6525SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6526 SDValue N1, const SDNodeFlags Flags) {
6527 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6528
6529 // Constant fold unary operations with a vector integer or float operand.
6530 switch (Opcode) {
6531 default:
6532 // FIXME: Entirely reasonable to perform folding of other unary
6533 // operations here as the need arises.
6534 break;
6535 case ISD::FNEG:
6536 case ISD::FABS:
6537 case ISD::FCEIL:
6538 case ISD::FTRUNC:
6539 case ISD::FFLOOR:
6540 case ISD::FP_EXTEND:
6541 case ISD::FP_TO_SINT:
6542 case ISD::FP_TO_UINT:
6543 case ISD::FP_TO_FP16:
6544 case ISD::FP_TO_BF16:
6545 case ISD::TRUNCATE:
6546 case ISD::ANY_EXTEND:
6547 case ISD::ZERO_EXTEND:
6548 case ISD::SIGN_EXTEND:
6549 case ISD::UINT_TO_FP:
6550 case ISD::SINT_TO_FP:
6551 case ISD::FP16_TO_FP:
6552 case ISD::BF16_TO_FP:
6553 case ISD::BITCAST:
6554 case ISD::ABS:
6555 case ISD::BITREVERSE:
6556 case ISD::BSWAP:
6557 case ISD::CTLZ:
6559 case ISD::CTTZ:
6561 case ISD::CTPOP:
6562 case ISD::CTLS:
6563 case ISD::STEP_VECTOR: {
6564 SDValue Ops = {N1};
6565 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6566 return Fold;
6567 }
6568 }
6569
6570 unsigned OpOpcode = N1.getNode()->getOpcode();
6571 switch (Opcode) {
6572 case ISD::STEP_VECTOR:
6573 assert(VT.isScalableVector() &&
6574 "STEP_VECTOR can only be used with scalable types");
6575 assert(OpOpcode == ISD::TargetConstant &&
6576 VT.getVectorElementType() == N1.getValueType() &&
6577 "Unexpected step operand");
6578 break;
6579 case ISD::FREEZE:
6580 assert(VT == N1.getValueType() && "Unexpected VT!");
6581 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6582 return N1;
6583 break;
6584 case ISD::TokenFactor:
6585 case ISD::MERGE_VALUES:
6587 return N1; // Factor, merge or concat of one node? No need.
6588 case ISD::BUILD_VECTOR: {
6589 // Attempt to simplify BUILD_VECTOR.
6590 SDValue Ops[] = {N1};
6591 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6592 return V;
6593 break;
6594 }
6595 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6596 case ISD::FP_EXTEND:
6598 "Invalid FP cast!");
6599 if (N1.getValueType() == VT) return N1; // noop conversion.
6600 assert((!VT.isVector() || VT.getVectorElementCount() ==
6602 "Vector element count mismatch!");
6603 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6604 if (N1.isUndef())
6605 return getUNDEF(VT);
6606 break;
6607 case ISD::FP_TO_SINT:
6608 case ISD::FP_TO_UINT:
6609 if (N1.isUndef())
6610 return getUNDEF(VT);
6611 break;
6612 case ISD::SINT_TO_FP:
6613 case ISD::UINT_TO_FP:
6614 // [us]itofp(undef) = 0, because the result value is bounded.
6615 if (N1.isUndef())
6616 return getConstantFP(0.0, DL, VT);
6617 break;
6618 case ISD::SIGN_EXTEND:
6619 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6620 "Invalid SIGN_EXTEND!");
6621 assert(VT.isVector() == N1.getValueType().isVector() &&
6622 "SIGN_EXTEND result type type should be vector iff the operand "
6623 "type is vector!");
6624 if (N1.getValueType() == VT) return N1; // noop extension
6625 assert((!VT.isVector() || VT.getVectorElementCount() ==
6627 "Vector element count mismatch!");
6628 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6629 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6630 SDNodeFlags Flags;
6631 if (OpOpcode == ISD::ZERO_EXTEND)
6632 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6633 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6634 transferDbgValues(N1, NewVal);
6635 return NewVal;
6636 }
6637
6638 if (OpOpcode == ISD::POISON)
6639 return getPOISON(VT);
6640
6641 if (N1.isUndef())
6642 // sext(undef) = 0, because the top bits will all be the same.
6643 return getConstant(0, DL, VT);
6644
6645 // Skip unnecessary sext_inreg pattern:
6646 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6647 if (OpOpcode == ISD::TRUNCATE) {
6648 SDValue OpOp = N1.getOperand(0);
6649 if (OpOp.getValueType() == VT) {
6650 unsigned NumSignExtBits =
6652 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6653 transferDbgValues(N1, OpOp);
6654 return OpOp;
6655 }
6656 }
6657 }
6658 break;
6659 case ISD::ZERO_EXTEND:
6660 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6661 "Invalid ZERO_EXTEND!");
6662 assert(VT.isVector() == N1.getValueType().isVector() &&
6663 "ZERO_EXTEND result type type should be vector iff the operand "
6664 "type is vector!");
6665 if (N1.getValueType() == VT) return N1; // noop extension
6666 assert((!VT.isVector() || VT.getVectorElementCount() ==
6668 "Vector element count mismatch!");
6669 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6670 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6671 SDNodeFlags Flags;
6672 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6673 SDValue NewVal =
6674 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6675 transferDbgValues(N1, NewVal);
6676 return NewVal;
6677 }
6678
6679 if (OpOpcode == ISD::POISON)
6680 return getPOISON(VT);
6681
6682 if (N1.isUndef())
6683 // zext(undef) = 0, because the top bits will be zero.
6684 return getConstant(0, DL, VT);
6685
6686 // Skip unnecessary zext_inreg pattern:
6687 // (zext (trunc x)) -> x iff the upper bits are known zero.
6688 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6689 // use to recognise zext_inreg patterns.
6690 if (OpOpcode == ISD::TRUNCATE) {
6691 SDValue OpOp = N1.getOperand(0);
6692 if (OpOp.getValueType() == VT) {
6693 if (OpOp.getOpcode() != ISD::AND) {
6696 if (MaskedValueIsZero(OpOp, HiBits)) {
6697 transferDbgValues(N1, OpOp);
6698 return OpOp;
6699 }
6700 }
6701 }
6702 }
6703 break;
6704 case ISD::ANY_EXTEND:
6705 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6706 "Invalid ANY_EXTEND!");
6707 assert(VT.isVector() == N1.getValueType().isVector() &&
6708 "ANY_EXTEND result type type should be vector iff the operand "
6709 "type is vector!");
6710 if (N1.getValueType() == VT) return N1; // noop extension
6711 assert((!VT.isVector() || VT.getVectorElementCount() ==
6713 "Vector element count mismatch!");
6714 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6715
6716 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6717 OpOpcode == ISD::ANY_EXTEND) {
6718 SDNodeFlags Flags;
6719 if (OpOpcode == ISD::ZERO_EXTEND)
6720 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6721 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6722 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6723 }
6724 if (N1.isUndef())
6725 return getUNDEF(VT);
6726
6727 // (ext (trunc x)) -> x
6728 if (OpOpcode == ISD::TRUNCATE) {
6729 SDValue OpOp = N1.getOperand(0);
6730 if (OpOp.getValueType() == VT) {
6731 transferDbgValues(N1, OpOp);
6732 return OpOp;
6733 }
6734 }
6735 break;
6736 case ISD::TRUNCATE:
6737 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6738 "Invalid TRUNCATE!");
6739 assert(VT.isVector() == N1.getValueType().isVector() &&
6740 "TRUNCATE result type type should be vector iff the operand "
6741 "type is vector!");
6742 if (N1.getValueType() == VT) return N1; // noop truncate
6743 assert((!VT.isVector() || VT.getVectorElementCount() ==
6745 "Vector element count mismatch!");
6746 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6747 if (OpOpcode == ISD::TRUNCATE)
6748 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6749 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6750 OpOpcode == ISD::ANY_EXTEND) {
6751 // If the source is smaller than the dest, we still need an extend.
6753 VT.getScalarType())) {
6754 SDNodeFlags Flags;
6755 if (OpOpcode == ISD::ZERO_EXTEND)
6756 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6757 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6758 }
6759 if (N1.getOperand(0).getValueType().bitsGT(VT))
6760 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6761 return N1.getOperand(0);
6762 }
6763 if (N1.isUndef())
6764 return getUNDEF(VT);
6765 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6766 return getVScale(DL, VT,
6768 break;
6772 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6773 assert(N1.getValueType().bitsLE(VT) &&
6774 "The input must be the same size or smaller than the result.");
6777 "The destination vector type must have fewer lanes than the input.");
6778 break;
6779 case ISD::ABS:
6780 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6781 if (N1.isUndef())
6782 return getConstant(0, DL, VT);
6783 break;
6784 case ISD::BSWAP:
6785 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6786 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6787 "BSWAP types must be a multiple of 16 bits!");
6788 if (N1.isUndef())
6789 return getUNDEF(VT);
6790 // bswap(bswap(X)) -> X.
6791 if (OpOpcode == ISD::BSWAP)
6792 return N1.getOperand(0);
6793 break;
6794 case ISD::BITREVERSE:
6795 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6796 if (N1.isUndef())
6797 return getUNDEF(VT);
6798 break;
6799 case ISD::BITCAST:
6801 "Cannot BITCAST between types of different sizes!");
6802 if (VT == N1.getValueType()) return N1; // noop conversion.
6803 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6804 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6805 if (N1.isUndef())
6806 return getUNDEF(VT);
6807 break;
6809 assert(VT.isVector() && !N1.getValueType().isVector() &&
6810 (VT.getVectorElementType() == N1.getValueType() ||
6812 N1.getValueType().isInteger() &&
6814 "Illegal SCALAR_TO_VECTOR node!");
6815 if (N1.isUndef())
6816 return getUNDEF(VT);
6817 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6818 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6820 N1.getConstantOperandVal(1) == 0 &&
6821 N1.getOperand(0).getValueType() == VT)
6822 return N1.getOperand(0);
6823 break;
6824 case ISD::FNEG:
6825 // Negation of an unknown bag of bits is still completely undefined.
6826 if (N1.isUndef())
6827 return getUNDEF(VT);
6828
6829 if (OpOpcode == ISD::FNEG) // --X -> X
6830 return N1.getOperand(0);
6831 break;
6832 case ISD::FABS:
6833 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6834 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6835 break;
6836 case ISD::VSCALE:
6837 assert(VT == N1.getValueType() && "Unexpected VT!");
6838 break;
6839 case ISD::CTPOP:
6840 if (N1.getValueType().getScalarType() == MVT::i1)
6841 return N1;
6842 break;
6843 case ISD::CTLZ:
6844 case ISD::CTTZ:
6845 if (N1.getValueType().getScalarType() == MVT::i1)
6846 return getNOT(DL, N1, N1.getValueType());
6847 break;
6848 case ISD::CTLS:
6849 if (N1.getValueType().getScalarType() == MVT::i1)
6850 return getConstant(0, DL, VT);
6851 break;
6852 case ISD::VECREDUCE_ADD:
6853 if (N1.getValueType().getScalarType() == MVT::i1)
6854 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6855 break;
6858 if (N1.getValueType().getScalarType() == MVT::i1)
6859 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6860 break;
6863 if (N1.getValueType().getScalarType() == MVT::i1)
6864 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6865 break;
6866 case ISD::SPLAT_VECTOR:
6867 assert(VT.isVector() && "Wrong return type!");
6868 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6869 // that for now.
6871 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6873 N1.getValueType().isInteger() &&
6875 "Wrong operand type!");
6876 break;
6877 }
6878
6879 SDNode *N;
6880 SDVTList VTs = getVTList(VT);
6881 SDValue Ops[] = {N1};
6882 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6884 AddNodeIDNode(ID, Opcode, VTs, Ops);
6885 void *IP = nullptr;
6886 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6887 E->intersectFlagsWith(Flags);
6888 return SDValue(E, 0);
6889 }
6890
6891 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6892 N->setFlags(Flags);
6893 createOperands(N, Ops);
6894 CSEMap.InsertNode(N, IP);
6895 } else {
6896 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6897 createOperands(N, Ops);
6898 }
6899
6900 InsertNode(N);
6901 SDValue V = SDValue(N, 0);
6902 NewSDValueDbgMsg(V, "Creating new node: ", this);
6903 return V;
6904}
6905
6906static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6907 const APInt &C2) {
6908 switch (Opcode) {
6909 case ISD::ADD: return C1 + C2;
6910 case ISD::SUB: return C1 - C2;
6911 case ISD::MUL: return C1 * C2;
6912 case ISD::AND: return C1 & C2;
6913 case ISD::OR: return C1 | C2;
6914 case ISD::XOR: return C1 ^ C2;
6915 case ISD::SHL: return C1 << C2;
6916 case ISD::SRL: return C1.lshr(C2);
6917 case ISD::SRA: return C1.ashr(C2);
6918 case ISD::ROTL: return C1.rotl(C2);
6919 case ISD::ROTR: return C1.rotr(C2);
6920 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6921 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6922 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6923 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6924 case ISD::SADDSAT: return C1.sadd_sat(C2);
6925 case ISD::UADDSAT: return C1.uadd_sat(C2);
6926 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6927 case ISD::USUBSAT: return C1.usub_sat(C2);
6928 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6929 case ISD::USHLSAT: return C1.ushl_sat(C2);
6930 case ISD::UDIV:
6931 if (!C2.getBoolValue())
6932 break;
6933 return C1.udiv(C2);
6934 case ISD::UREM:
6935 if (!C2.getBoolValue())
6936 break;
6937 return C1.urem(C2);
6938 case ISD::SDIV:
6939 if (!C2.getBoolValue())
6940 break;
6941 return C1.sdiv(C2);
6942 case ISD::SREM:
6943 if (!C2.getBoolValue())
6944 break;
6945 return C1.srem(C2);
6946 case ISD::AVGFLOORS:
6947 return APIntOps::avgFloorS(C1, C2);
6948 case ISD::AVGFLOORU:
6949 return APIntOps::avgFloorU(C1, C2);
6950 case ISD::AVGCEILS:
6951 return APIntOps::avgCeilS(C1, C2);
6952 case ISD::AVGCEILU:
6953 return APIntOps::avgCeilU(C1, C2);
6954 case ISD::ABDS:
6955 return APIntOps::abds(C1, C2);
6956 case ISD::ABDU:
6957 return APIntOps::abdu(C1, C2);
6958 case ISD::MULHS:
6959 return APIntOps::mulhs(C1, C2);
6960 case ISD::MULHU:
6961 return APIntOps::mulhu(C1, C2);
6962 case ISD::CLMUL:
6963 return APIntOps::clmul(C1, C2);
6964 case ISD::CLMULR:
6965 return APIntOps::clmulr(C1, C2);
6966 case ISD::CLMULH:
6967 return APIntOps::clmulh(C1, C2);
6968 }
6969 return std::nullopt;
6970}
6971// Handle constant folding with UNDEF.
6972// TODO: Handle more cases.
6973static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6974 bool IsUndef1, const APInt &C2,
6975 bool IsUndef2) {
6976 if (!(IsUndef1 || IsUndef2))
6977 return FoldValue(Opcode, C1, C2);
6978
6979 // Fold and(x, undef) -> 0
6980 // Fold mul(x, undef) -> 0
6981 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6982 return APInt::getZero(C1.getBitWidth());
6983
6984 return std::nullopt;
6985}
6986
6988 const GlobalAddressSDNode *GA,
6989 const SDNode *N2) {
6990 if (GA->getOpcode() != ISD::GlobalAddress)
6991 return SDValue();
6992 if (!TLI->isOffsetFoldingLegal(GA))
6993 return SDValue();
6994 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6995 if (!C2)
6996 return SDValue();
6997 int64_t Offset = C2->getSExtValue();
6998 switch (Opcode) {
6999 case ISD::ADD:
7000 case ISD::PTRADD:
7001 break;
7002 case ISD::SUB: Offset = -uint64_t(Offset); break;
7003 default: return SDValue();
7004 }
7005 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
7006 GA->getOffset() + uint64_t(Offset));
7007}
7008
7010 switch (Opcode) {
7011 case ISD::SDIV:
7012 case ISD::UDIV:
7013 case ISD::SREM:
7014 case ISD::UREM: {
7015 // If a divisor is zero/undef or any element of a divisor vector is
7016 // zero/undef, the whole op is undef.
7017 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
7018 SDValue Divisor = Ops[1];
7019 if (Divisor.isUndef() || isNullConstant(Divisor))
7020 return true;
7021
7022 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
7023 llvm::any_of(Divisor->op_values(),
7024 [](SDValue V) { return V.isUndef() ||
7025 isNullConstant(V); });
7026 // TODO: Handle signed overflow.
7027 }
7028 // TODO: Handle oversized shifts.
7029 default:
7030 return false;
7031 }
7032}
7033
7036 SDNodeFlags Flags) {
7037 // If the opcode is a target-specific ISD node, there's nothing we can
7038 // do here and the operand rules may not line up with the below, so
7039 // bail early.
7040 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
7041 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
7042 // foldCONCAT_VECTORS in getNode before this is called.
7043 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
7044 return SDValue();
7045
7046 unsigned NumOps = Ops.size();
7047 if (NumOps == 0)
7048 return SDValue();
7049
7050 if (isUndef(Opcode, Ops))
7051 return getUNDEF(VT);
7052
7053 // Handle unary special cases.
7054 if (NumOps == 1) {
7055 SDValue N1 = Ops[0];
7056
7057 // Constant fold unary operations with an integer constant operand. Even
7058 // opaque constant will be folded, because the folding of unary operations
7059 // doesn't create new constants with different values. Nevertheless, the
7060 // opaque flag is preserved during folding to prevent future folding with
7061 // other constants.
7062 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
7063 const APInt &Val = C->getAPIntValue();
7064 switch (Opcode) {
7065 case ISD::SIGN_EXTEND:
7066 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7067 C->isTargetOpcode(), C->isOpaque());
7068 case ISD::TRUNCATE:
7069 if (C->isOpaque())
7070 break;
7071 [[fallthrough]];
7072 case ISD::ZERO_EXTEND:
7073 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7074 C->isTargetOpcode(), C->isOpaque());
7075 case ISD::ANY_EXTEND:
7076 // Some targets like RISCV prefer to sign extend some types.
7077 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7078 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7079 C->isTargetOpcode(), C->isOpaque());
7080 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7081 C->isTargetOpcode(), C->isOpaque());
7082 case ISD::ABS:
7083 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7084 C->isOpaque());
7085 case ISD::BITREVERSE:
7086 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7087 C->isOpaque());
7088 case ISD::BSWAP:
7089 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7090 C->isOpaque());
7091 case ISD::CTPOP:
7092 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7093 C->isOpaque());
7094 case ISD::CTLZ:
7096 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7097 C->isOpaque());
7098 case ISD::CTTZ:
7100 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7101 C->isOpaque());
7102 case ISD::CTLS:
7103 // CTLS returns the number of extra sign bits so subtract one.
7104 return getConstant(Val.getNumSignBits() - 1, DL, VT,
7105 C->isTargetOpcode(), C->isOpaque());
7106 case ISD::UINT_TO_FP:
7107 case ISD::SINT_TO_FP: {
7109 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7111 return getConstantFP(FPV, DL, VT);
7112 }
7113 case ISD::FP16_TO_FP:
7114 case ISD::BF16_TO_FP: {
7115 bool Ignored;
7116 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7117 : APFloat::BFloat(),
7118 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7119
7120 // This can return overflow, underflow, or inexact; we don't care.
7121 // FIXME need to be more flexible about rounding mode.
7123 &Ignored);
7124 return getConstantFP(FPV, DL, VT);
7125 }
7126 case ISD::STEP_VECTOR:
7127 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7128 return V;
7129 break;
7130 case ISD::BITCAST:
7131 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7132 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7133 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7134 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7135 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7136 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7137 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7138 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7139 break;
7140 }
7141 }
7142
7143 // Constant fold unary operations with a floating point constant operand.
7144 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7145 APFloat V = C->getValueAPF(); // make copy
7146 switch (Opcode) {
7147 case ISD::FNEG:
7148 V.changeSign();
7149 return getConstantFP(V, DL, VT);
7150 case ISD::FABS:
7151 V.clearSign();
7152 return getConstantFP(V, DL, VT);
7153 case ISD::FCEIL: {
7154 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7156 return getConstantFP(V, DL, VT);
7157 return SDValue();
7158 }
7159 case ISD::FTRUNC: {
7160 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7162 return getConstantFP(V, DL, VT);
7163 return SDValue();
7164 }
7165 case ISD::FFLOOR: {
7166 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7168 return getConstantFP(V, DL, VT);
7169 return SDValue();
7170 }
7171 case ISD::FP_EXTEND: {
7172 bool ignored;
7173 // This can return overflow, underflow, or inexact; we don't care.
7174 // FIXME need to be more flexible about rounding mode.
7175 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7176 &ignored);
7177 return getConstantFP(V, DL, VT);
7178 }
7179 case ISD::FP_TO_SINT:
7180 case ISD::FP_TO_UINT: {
7181 bool ignored;
7182 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7183 // FIXME need to be more flexible about rounding mode.
7185 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7186 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7187 break;
7188 return getConstant(IntVal, DL, VT);
7189 }
7190 case ISD::FP_TO_FP16:
7191 case ISD::FP_TO_BF16: {
7192 bool Ignored;
7193 // This can return overflow, underflow, or inexact; we don't care.
7194 // FIXME need to be more flexible about rounding mode.
7195 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7196 : APFloat::BFloat(),
7198 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7199 }
7200 case ISD::BITCAST:
7201 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7202 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7203 VT);
7204 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7205 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7206 VT);
7207 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7208 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7209 VT);
7210 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7211 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7212 break;
7213 }
7214 }
7215
7216 // Early-out if we failed to constant fold a bitcast.
7217 if (Opcode == ISD::BITCAST)
7218 return SDValue();
7219 }
7220
7221 // Handle binops special cases.
7222 if (NumOps == 2) {
7223 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7224 return CFP;
7225
7226 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7227 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7228 if (C1->isOpaque() || C2->isOpaque())
7229 return SDValue();
7230
7231 std::optional<APInt> FoldAttempt =
7232 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7233 if (!FoldAttempt)
7234 return SDValue();
7235
7236 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7237 assert((!Folded || !VT.isVector()) &&
7238 "Can't fold vectors ops with scalar operands");
7239 return Folded;
7240 }
7241 }
7242
7243 // fold (add Sym, c) -> Sym+c
7245 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7246 if (TLI->isCommutativeBinOp(Opcode))
7248 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7249
7250 // fold (sext_in_reg c1) -> c2
7251 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7252 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7253
7254 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7255 unsigned FromBits = EVT.getScalarSizeInBits();
7256 Val <<= Val.getBitWidth() - FromBits;
7257 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7258 return getConstant(Val, DL, ConstantVT);
7259 };
7260
7261 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7262 const APInt &Val = C1->getAPIntValue();
7263 return SignExtendInReg(Val, VT);
7264 }
7265
7267 SmallVector<SDValue, 8> ScalarOps;
7268 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7269 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7270 SDValue Op = Ops[0].getOperand(I);
7271 if (Op.isUndef()) {
7272 ScalarOps.push_back(getUNDEF(OpVT));
7273 continue;
7274 }
7275 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7276 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7277 }
7278 return getBuildVector(VT, DL, ScalarOps);
7279 }
7280
7281 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7282 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7283 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7284 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7285 Ops[0].getOperand(0).getValueType()));
7286 }
7287 }
7288
7289 // Handle fshl/fshr special cases.
7290 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7291 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7292 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7293 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7294
7295 if (C1 && C2 && C3) {
7296 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7297 return SDValue();
7298 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7299 &V3 = C3->getAPIntValue();
7300
7301 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7302 : APIntOps::fshr(V1, V2, V3);
7303 return getConstant(FoldedVal, DL, VT);
7304 }
7305 }
7306
7307 // Handle fma/fmad special cases.
7308 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7309 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7310 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7311 Ops[2].getValueType() == VT && "FMA types must match!");
7315 if (C1 && C2 && C3) {
7316 APFloat V1 = C1->getValueAPF();
7317 const APFloat &V2 = C2->getValueAPF();
7318 const APFloat &V3 = C3->getValueAPF();
7319 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7322 } else
7324 return getConstantFP(V1, DL, VT);
7325 }
7326 }
7327
7328 // This is for vector folding only from here on.
7329 if (!VT.isVector())
7330 return SDValue();
7331
7332 ElementCount NumElts = VT.getVectorElementCount();
7333
7334 // See if we can fold through any bitcasted integer ops.
7335 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7336 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7337 (Ops[0].getOpcode() == ISD::BITCAST ||
7338 Ops[1].getOpcode() == ISD::BITCAST)) {
7341 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7342 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7343 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7344 N2.getValueType().isInteger()) {
7345 bool IsLE = getDataLayout().isLittleEndian();
7346 unsigned EltBits = VT.getScalarSizeInBits();
7347 SmallVector<APInt> RawBits1, RawBits2;
7348 BitVector UndefElts1, UndefElts2;
7349 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7350 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7351 SmallVector<APInt> RawBits;
7352 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7353 std::optional<APInt> Fold = FoldValueWithUndef(
7354 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7355 if (!Fold)
7356 break;
7357 RawBits.push_back(*Fold);
7358 }
7359 if (RawBits.size() == NumElts.getFixedValue()) {
7360 // We have constant folded, but we might need to cast this again back
7361 // to the original (possibly legalized) type.
7362 EVT BVVT, BVEltVT;
7363 if (N1.getValueType() == VT) {
7364 BVVT = N1.getValueType();
7365 BVEltVT = BV1->getOperand(0).getValueType();
7366 } else {
7367 BVVT = N2.getValueType();
7368 BVEltVT = BV2->getOperand(0).getValueType();
7369 }
7370 unsigned BVEltBits = BVEltVT.getSizeInBits();
7371 SmallVector<APInt> DstBits;
7372 BitVector DstUndefs;
7374 DstBits, RawBits, DstUndefs,
7375 BitVector(RawBits.size(), false));
7376 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7377 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7378 if (DstUndefs[I])
7379 continue;
7380 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7381 }
7382 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7383 }
7384 }
7385 }
7386 }
7387
7388 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7389 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7390 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7391 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7392 APInt RHSVal;
7393 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7394 APInt NewStep = Opcode == ISD::MUL
7395 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7396 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7397 return getStepVector(DL, VT, NewStep);
7398 }
7399 }
7400
7401 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7402 return !Op.getValueType().isVector() ||
7403 Op.getValueType().getVectorElementCount() == NumElts;
7404 };
7405
7406 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7407 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7408 Op.getOpcode() == ISD::BUILD_VECTOR ||
7409 Op.getOpcode() == ISD::SPLAT_VECTOR;
7410 };
7411
7412 // All operands must be vector types with the same number of elements as
7413 // the result type and must be either UNDEF or a build/splat vector
7414 // or UNDEF scalars.
7415 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7416 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7417 return SDValue();
7418
7419 // If we are comparing vectors, then the result needs to be a i1 boolean that
7420 // is then extended back to the legal result type depending on how booleans
7421 // are represented.
7422 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7423 ISD::NodeType ExtendCode =
7424 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7425 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7427
7428 // Find legal integer scalar type for constant promotion and
7429 // ensure that its scalar size is at least as large as source.
7430 EVT LegalSVT = VT.getScalarType();
7431 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7432 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7433 if (LegalSVT.bitsLT(VT.getScalarType()))
7434 return SDValue();
7435 }
7436
7437 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7438 // only have one operand to check. For fixed-length vector types we may have
7439 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7440 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7441
7442 // Constant fold each scalar lane separately.
7443 SmallVector<SDValue, 4> ScalarResults;
7444 for (unsigned I = 0; I != NumVectorElts; I++) {
7445 SmallVector<SDValue, 4> ScalarOps;
7446 for (SDValue Op : Ops) {
7447 EVT InSVT = Op.getValueType().getScalarType();
7448 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7449 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7450 if (Op.isUndef())
7451 ScalarOps.push_back(getUNDEF(InSVT));
7452 else
7453 ScalarOps.push_back(Op);
7454 continue;
7455 }
7456
7457 SDValue ScalarOp =
7458 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7459 EVT ScalarVT = ScalarOp.getValueType();
7460
7461 // Build vector (integer) scalar operands may need implicit
7462 // truncation - do this before constant folding.
7463 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7464 // Don't create illegally-typed nodes unless they're constants or undef
7465 // - if we fail to constant fold we can't guarantee the (dead) nodes
7466 // we're creating will be cleaned up before being visited for
7467 // legalization.
7468 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7469 !isa<ConstantSDNode>(ScalarOp) &&
7470 TLI->getTypeAction(*getContext(), InSVT) !=
7472 return SDValue();
7473 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7474 }
7475
7476 ScalarOps.push_back(ScalarOp);
7477 }
7478
7479 // Constant fold the scalar operands.
7480 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7481
7482 // Scalar folding only succeeded if the result is a constant or UNDEF.
7483 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7484 ScalarResult.getOpcode() != ISD::ConstantFP)
7485 return SDValue();
7486
7487 // Legalize the (integer) scalar constant if necessary. We only do
7488 // this once we know the folding succeeded, since otherwise we would
7489 // get a node with illegal type which has a user.
7490 if (LegalSVT != SVT)
7491 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7492
7493 ScalarResults.push_back(ScalarResult);
7494 }
7495
7496 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7497 : getBuildVector(VT, DL, ScalarResults);
7498 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7499 return V;
7500}
7501
7504 // TODO: Add support for unary/ternary fp opcodes.
7505 if (Ops.size() != 2)
7506 return SDValue();
7507
7508 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7509 // should. That will require dealing with a potentially non-default
7510 // rounding mode, checking the "opStatus" return value from the APFloat
7511 // math calculations, and possibly other variations.
7512 SDValue N1 = Ops[0];
7513 SDValue N2 = Ops[1];
7514 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7515 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7516 if (N1CFP && N2CFP) {
7517 APFloat C1 = N1CFP->getValueAPF(); // make copy
7518 const APFloat &C2 = N2CFP->getValueAPF();
7519 switch (Opcode) {
7520 case ISD::FADD:
7522 return getConstantFP(C1, DL, VT);
7523 case ISD::FSUB:
7525 return getConstantFP(C1, DL, VT);
7526 case ISD::FMUL:
7528 return getConstantFP(C1, DL, VT);
7529 case ISD::FDIV:
7531 return getConstantFP(C1, DL, VT);
7532 case ISD::FREM:
7533 C1.mod(C2);
7534 return getConstantFP(C1, DL, VT);
7535 case ISD::FCOPYSIGN:
7536 C1.copySign(C2);
7537 return getConstantFP(C1, DL, VT);
7538 case ISD::FMINNUM:
7539 if (C1.isSignaling() || C2.isSignaling())
7540 return SDValue();
7541 return getConstantFP(minnum(C1, C2), DL, VT);
7542 case ISD::FMAXNUM:
7543 if (C1.isSignaling() || C2.isSignaling())
7544 return SDValue();
7545 return getConstantFP(maxnum(C1, C2), DL, VT);
7546 case ISD::FMINIMUM:
7547 return getConstantFP(minimum(C1, C2), DL, VT);
7548 case ISD::FMAXIMUM:
7549 return getConstantFP(maximum(C1, C2), DL, VT);
7550 case ISD::FMINIMUMNUM:
7551 return getConstantFP(minimumnum(C1, C2), DL, VT);
7552 case ISD::FMAXIMUMNUM:
7553 return getConstantFP(maximumnum(C1, C2), DL, VT);
7554 default: break;
7555 }
7556 }
7557 if (N1CFP && Opcode == ISD::FP_ROUND) {
7558 APFloat C1 = N1CFP->getValueAPF(); // make copy
7559 bool Unused;
7560 // This can return overflow, underflow, or inexact; we don't care.
7561 // FIXME need to be more flexible about rounding mode.
7563 &Unused);
7564 return getConstantFP(C1, DL, VT);
7565 }
7566
7567 switch (Opcode) {
7568 case ISD::FSUB:
7569 // -0.0 - undef --> undef (consistent with "fneg undef")
7570 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7571 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7572 return getUNDEF(VT);
7573 [[fallthrough]];
7574
7575 case ISD::FADD:
7576 case ISD::FMUL:
7577 case ISD::FDIV:
7578 case ISD::FREM:
7579 // If both operands are undef, the result is undef. If 1 operand is undef,
7580 // the result is NaN. This should match the behavior of the IR optimizer.
7581 if (N1.isUndef() && N2.isUndef())
7582 return getUNDEF(VT);
7583 if (N1.isUndef() || N2.isUndef())
7585 }
7586 return SDValue();
7587}
7588
7590 const SDLoc &DL, EVT DstEltVT) {
7591 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7592
7593 // If this is already the right type, we're done.
7594 if (SrcEltVT == DstEltVT)
7595 return SDValue(BV, 0);
7596
7597 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7598 unsigned DstBitSize = DstEltVT.getSizeInBits();
7599
7600 // If this is a conversion of N elements of one type to N elements of another
7601 // type, convert each element. This handles FP<->INT cases.
7602 if (SrcBitSize == DstBitSize) {
7604 for (SDValue Op : BV->op_values()) {
7605 // If the vector element type is not legal, the BUILD_VECTOR operands
7606 // are promoted and implicitly truncated. Make that explicit here.
7607 if (Op.getValueType() != SrcEltVT)
7608 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7609 Ops.push_back(getBitcast(DstEltVT, Op));
7610 }
7611 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7613 return getBuildVector(VT, DL, Ops);
7614 }
7615
7616 // Otherwise, we're growing or shrinking the elements. To avoid having to
7617 // handle annoying details of growing/shrinking FP values, we convert them to
7618 // int first.
7619 if (SrcEltVT.isFloatingPoint()) {
7620 // Convert the input float vector to a int vector where the elements are the
7621 // same sizes.
7622 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7623 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7625 DstEltVT);
7626 return SDValue();
7627 }
7628
7629 // Now we know the input is an integer vector. If the output is a FP type,
7630 // convert to integer first, then to FP of the right size.
7631 if (DstEltVT.isFloatingPoint()) {
7632 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7633 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7635 DstEltVT);
7636 return SDValue();
7637 }
7638
7639 // Okay, we know the src/dst types are both integers of differing types.
7640 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7641
7642 // Extract the constant raw bit data.
7643 BitVector UndefElements;
7644 SmallVector<APInt> RawBits;
7645 bool IsLE = getDataLayout().isLittleEndian();
7646 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7647 return SDValue();
7648
7650 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7651 if (UndefElements[I])
7652 Ops.push_back(getUNDEF(DstEltVT));
7653 else
7654 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7655 }
7656
7657 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7658 return getBuildVector(VT, DL, Ops);
7659}
7660
7662 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7663
7664 // There's no need to assert on a byte-aligned pointer. All pointers are at
7665 // least byte aligned.
7666 if (A == Align(1))
7667 return Val;
7668
7669 SDVTList VTs = getVTList(Val.getValueType());
7671 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7672 ID.AddInteger(A.value());
7673
7674 void *IP = nullptr;
7675 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7676 return SDValue(E, 0);
7677
7678 auto *N =
7679 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7680 createOperands(N, {Val});
7681
7682 CSEMap.InsertNode(N, IP);
7683 InsertNode(N);
7684
7685 SDValue V(N, 0);
7686 NewSDValueDbgMsg(V, "Creating new node: ", this);
7687 return V;
7688}
7689
7690SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7691 SDValue N1, SDValue N2) {
7692 SDNodeFlags Flags;
7693 if (Inserter)
7694 Flags = Inserter->getFlags();
7695 return getNode(Opcode, DL, VT, N1, N2, Flags);
7696}
7697
7699 SDValue &N2) const {
7700 if (!TLI->isCommutativeBinOp(Opcode))
7701 return;
7702
7703 // Canonicalize:
7704 // binop(const, nonconst) -> binop(nonconst, const)
7707 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7708 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7709 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7710 std::swap(N1, N2);
7711
7712 // Canonicalize:
7713 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7714 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7716 std::swap(N1, N2);
7717}
7718
7719SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7720 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7722 N2.getOpcode() != ISD::DELETED_NODE &&
7723 "Operand is DELETED_NODE!");
7724
7725 canonicalizeCommutativeBinop(Opcode, N1, N2);
7726
7727 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7728 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7729
7730 // Don't allow undefs in vector splats - we might be returning N2 when folding
7731 // to zero etc.
7732 ConstantSDNode *N2CV =
7733 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7734
7735 switch (Opcode) {
7736 default: break;
7737 case ISD::TokenFactor:
7738 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7739 N2.getValueType() == MVT::Other && "Invalid token factor!");
7740 // Fold trivial token factors.
7741 if (N1.getOpcode() == ISD::EntryToken) return N2;
7742 if (N2.getOpcode() == ISD::EntryToken) return N1;
7743 if (N1 == N2) return N1;
7744 break;
7745 case ISD::BUILD_VECTOR: {
7746 // Attempt to simplify BUILD_VECTOR.
7747 SDValue Ops[] = {N1, N2};
7748 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7749 return V;
7750 break;
7751 }
7752 case ISD::CONCAT_VECTORS: {
7753 SDValue Ops[] = {N1, N2};
7754 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7755 return V;
7756 break;
7757 }
7758 case ISD::AND:
7759 assert(VT.isInteger() && "This operator does not apply to FP types!");
7760 assert(N1.getValueType() == N2.getValueType() &&
7761 N1.getValueType() == VT && "Binary operator types must match!");
7762 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7763 // worth handling here.
7764 if (N2CV && N2CV->isZero())
7765 return N2;
7766 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7767 return N1;
7768 break;
7769 case ISD::OR:
7770 case ISD::XOR:
7771 case ISD::ADD:
7772 case ISD::PTRADD:
7773 case ISD::SUB:
7774 assert(VT.isInteger() && "This operator does not apply to FP types!");
7775 assert(N1.getValueType() == N2.getValueType() &&
7776 N1.getValueType() == VT && "Binary operator types must match!");
7777 // The equal operand types requirement is unnecessarily strong for PTRADD.
7778 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7779 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7780 // logic everywhere where PTRADDs may be folded or combined to properly
7781 // support them. If/when we introduce pointer types to the SDAG, we will
7782 // need to relax this constraint.
7783
7784 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7785 // it's worth handling here.
7786 if (N2CV && N2CV->isZero())
7787 return N1;
7788 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7789 VT.getScalarType() == MVT::i1)
7790 return getNode(ISD::XOR, DL, VT, N1, N2);
7791 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7792 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7793 N2.getOpcode() == ISD::VSCALE) {
7794 const APInt &C1 = N1->getConstantOperandAPInt(0);
7795 const APInt &C2 = N2->getConstantOperandAPInt(0);
7796 return getVScale(DL, VT, C1 + C2);
7797 }
7798 break;
7799 case ISD::MUL:
7800 assert(VT.isInteger() && "This operator does not apply to FP types!");
7801 assert(N1.getValueType() == N2.getValueType() &&
7802 N1.getValueType() == VT && "Binary operator types must match!");
7803 if (VT.getScalarType() == MVT::i1)
7804 return getNode(ISD::AND, DL, VT, N1, N2);
7805 if (N2CV && N2CV->isZero())
7806 return N2;
7807 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7808 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7809 const APInt &N2CImm = N2C->getAPIntValue();
7810 return getVScale(DL, VT, MulImm * N2CImm);
7811 }
7812 break;
7813 case ISD::UDIV:
7814 case ISD::UREM:
7815 case ISD::MULHU:
7816 case ISD::MULHS:
7817 case ISD::SDIV:
7818 case ISD::SREM:
7819 case ISD::SADDSAT:
7820 case ISD::SSUBSAT:
7821 case ISD::UADDSAT:
7822 case ISD::USUBSAT:
7823 assert(VT.isInteger() && "This operator does not apply to FP types!");
7824 assert(N1.getValueType() == N2.getValueType() &&
7825 N1.getValueType() == VT && "Binary operator types must match!");
7826 if (VT.getScalarType() == MVT::i1) {
7827 // fold (add_sat x, y) -> (or x, y) for bool types.
7828 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7829 return getNode(ISD::OR, DL, VT, N1, N2);
7830 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7831 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7832 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7833 }
7834 break;
7835 case ISD::SCMP:
7836 case ISD::UCMP:
7837 assert(N1.getValueType() == N2.getValueType() &&
7838 "Types of operands of UCMP/SCMP must match");
7839 assert(N1.getValueType().isVector() == VT.isVector() &&
7840 "Operands and return type of must both be scalars or vectors");
7841 if (VT.isVector())
7844 "Result and operands must have the same number of elements");
7845 break;
7846 case ISD::AVGFLOORS:
7847 case ISD::AVGFLOORU:
7848 case ISD::AVGCEILS:
7849 case ISD::AVGCEILU:
7850 assert(VT.isInteger() && "This operator does not apply to FP types!");
7851 assert(N1.getValueType() == N2.getValueType() &&
7852 N1.getValueType() == VT && "Binary operator types must match!");
7853 break;
7854 case ISD::ABDS:
7855 case ISD::ABDU:
7856 assert(VT.isInteger() && "This operator does not apply to FP types!");
7857 assert(N1.getValueType() == N2.getValueType() &&
7858 N1.getValueType() == VT && "Binary operator types must match!");
7859 if (VT.getScalarType() == MVT::i1)
7860 return getNode(ISD::XOR, DL, VT, N1, N2);
7861 break;
7862 case ISD::SMIN:
7863 case ISD::UMAX:
7864 assert(VT.isInteger() && "This operator does not apply to FP types!");
7865 assert(N1.getValueType() == N2.getValueType() &&
7866 N1.getValueType() == VT && "Binary operator types must match!");
7867 if (VT.getScalarType() == MVT::i1)
7868 return getNode(ISD::OR, DL, VT, N1, N2);
7869 break;
7870 case ISD::SMAX:
7871 case ISD::UMIN:
7872 assert(VT.isInteger() && "This operator does not apply to FP types!");
7873 assert(N1.getValueType() == N2.getValueType() &&
7874 N1.getValueType() == VT && "Binary operator types must match!");
7875 if (VT.getScalarType() == MVT::i1)
7876 return getNode(ISD::AND, DL, VT, N1, N2);
7877 break;
7878 case ISD::FADD:
7879 case ISD::FSUB:
7880 case ISD::FMUL:
7881 case ISD::FDIV:
7882 case ISD::FREM:
7883 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7884 assert(N1.getValueType() == N2.getValueType() &&
7885 N1.getValueType() == VT && "Binary operator types must match!");
7886 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7887 return V;
7888 break;
7889 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7890 assert(N1.getValueType() == VT &&
7893 "Invalid FCOPYSIGN!");
7894 break;
7895 case ISD::SHL:
7896 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7897 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7898 const APInt &ShiftImm = N2C->getAPIntValue();
7899 return getVScale(DL, VT, MulImm << ShiftImm);
7900 }
7901 [[fallthrough]];
7902 case ISD::SRA:
7903 case ISD::SRL:
7904 if (SDValue V = simplifyShift(N1, N2))
7905 return V;
7906 [[fallthrough]];
7907 case ISD::ROTL:
7908 case ISD::ROTR:
7909 case ISD::SSHLSAT:
7910 case ISD::USHLSAT:
7911 assert(VT == N1.getValueType() &&
7912 "Shift operators return type must be the same as their first arg");
7913 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7914 "Shifts only work on integers");
7915 assert((!VT.isVector() || VT == N2.getValueType()) &&
7916 "Vector shift amounts must be in the same as their first arg");
7917 // Verify that the shift amount VT is big enough to hold valid shift
7918 // amounts. This catches things like trying to shift an i1024 value by an
7919 // i8, which is easy to fall into in generic code that uses
7920 // TLI.getShiftAmount().
7923 "Invalid use of small shift amount with oversized value!");
7924
7925 // Always fold shifts of i1 values so the code generator doesn't need to
7926 // handle them. Since we know the size of the shift has to be less than the
7927 // size of the value, the shift/rotate count is guaranteed to be zero.
7928 if (VT == MVT::i1)
7929 return N1;
7930 if (N2CV && N2CV->isZero())
7931 return N1;
7932 break;
7933 case ISD::FP_ROUND:
7935 VT.bitsLE(N1.getValueType()) && N2C &&
7936 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7937 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7938 if (N1.getValueType() == VT) return N1; // noop conversion.
7939 break;
7940 case ISD::AssertNoFPClass: {
7942 "AssertNoFPClass is used for a non-floating type");
7943 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7944 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7945 assert(llvm::to_underlying(NoFPClass) <=
7947 "FPClassTest value too large");
7948 (void)NoFPClass;
7949 break;
7950 }
7951 case ISD::AssertSext:
7952 case ISD::AssertZext: {
7953 EVT EVT = cast<VTSDNode>(N2)->getVT();
7954 assert(VT == N1.getValueType() && "Not an inreg extend!");
7955 assert(VT.isInteger() && EVT.isInteger() &&
7956 "Cannot *_EXTEND_INREG FP types");
7957 assert(!EVT.isVector() &&
7958 "AssertSExt/AssertZExt type should be the vector element type "
7959 "rather than the vector type!");
7960 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7961 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7962 break;
7963 }
7965 EVT EVT = cast<VTSDNode>(N2)->getVT();
7966 assert(VT == N1.getValueType() && "Not an inreg extend!");
7967 assert(VT.isInteger() && EVT.isInteger() &&
7968 "Cannot *_EXTEND_INREG FP types");
7969 assert(EVT.isVector() == VT.isVector() &&
7970 "SIGN_EXTEND_INREG type should be vector iff the operand "
7971 "type is vector!");
7972 assert((!EVT.isVector() ||
7974 "Vector element counts must match in SIGN_EXTEND_INREG");
7975 assert(EVT.getScalarType().bitsLE(VT.getScalarType()) && "Not extending!");
7976 if (EVT == VT) return N1; // Not actually extending
7977 break;
7978 }
7980 case ISD::FP_TO_UINT_SAT: {
7981 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7982 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7983 assert(N1.getValueType().isVector() == VT.isVector() &&
7984 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7985 "vector!");
7986 assert((!VT.isVector() || VT.getVectorElementCount() ==
7988 "Vector element counts must match in FP_TO_*INT_SAT");
7989 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7990 "Type to saturate to must be a scalar.");
7991 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7992 "Not extending!");
7993 break;
7994 }
7997 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7998 element type of the vector.");
7999
8000 // Extract from an undefined value or using an undefined index is undefined.
8001 if (N1.isUndef() || N2.isUndef())
8002 return getUNDEF(VT);
8003
8004 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
8005 // vectors. For scalable vectors we will provide appropriate support for
8006 // dealing with arbitrary indices.
8007 if (N2C && N1.getValueType().isFixedLengthVector() &&
8008 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
8009 return getUNDEF(VT);
8010
8011 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
8012 // expanding copies of large vectors from registers. This only works for
8013 // fixed length vectors, since we need to know the exact number of
8014 // elements.
8015 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
8017 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
8018 return getExtractVectorElt(DL, VT,
8019 N1.getOperand(N2C->getZExtValue() / Factor),
8020 N2C->getZExtValue() % Factor);
8021 }
8022
8023 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
8024 // lowering is expanding large vector constants.
8025 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
8026 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
8029 "BUILD_VECTOR used for scalable vectors");
8030 unsigned Index =
8031 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
8032 SDValue Elt = N1.getOperand(Index);
8033
8034 if (VT != Elt.getValueType())
8035 // If the vector element type is not legal, the BUILD_VECTOR operands
8036 // are promoted and implicitly truncated, and the result implicitly
8037 // extended. Make that explicit here.
8038 Elt = getAnyExtOrTrunc(Elt, DL, VT);
8039
8040 return Elt;
8041 }
8042
8043 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
8044 // operations are lowered to scalars.
8045 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
8046 // If the indices are the same, return the inserted element else
8047 // if the indices are known different, extract the element from
8048 // the original vector.
8049 SDValue N1Op2 = N1.getOperand(2);
8051
8052 if (N1Op2C && N2C) {
8053 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
8054 if (VT == N1.getOperand(1).getValueType())
8055 return N1.getOperand(1);
8056 if (VT.isFloatingPoint()) {
8058 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
8059 }
8060 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
8061 }
8062 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
8063 }
8064 }
8065
8066 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
8067 // when vector types are scalarized and v1iX is legal.
8068 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
8069 // Here we are completely ignoring the extract element index (N2),
8070 // which is fine for fixed width vectors, since any index other than 0
8071 // is undefined anyway. However, this cannot be ignored for scalable
8072 // vectors - in theory we could support this, but we don't want to do this
8073 // without a profitability check.
8074 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8076 N1.getValueType().getVectorNumElements() == 1) {
8077 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8078 N1.getOperand(1));
8079 }
8080 break;
8082 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8083 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8084 (N1.getValueType().isInteger() == VT.isInteger()) &&
8085 N1.getValueType() != VT &&
8086 "Wrong types for EXTRACT_ELEMENT!");
8087
8088 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8089 // 64-bit integers into 32-bit parts. Instead of building the extract of
8090 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8091 if (N1.getOpcode() == ISD::BUILD_PAIR)
8092 return N1.getOperand(N2C->getZExtValue());
8093
8094 // EXTRACT_ELEMENT of a constant int is also very common.
8095 if (N1C) {
8096 unsigned ElementSize = VT.getSizeInBits();
8097 unsigned Shift = ElementSize * N2C->getZExtValue();
8098 const APInt &Val = N1C->getAPIntValue();
8099 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8100 }
8101 break;
8103 EVT N1VT = N1.getValueType();
8104 assert(VT.isVector() && N1VT.isVector() &&
8105 "Extract subvector VTs must be vectors!");
8107 "Extract subvector VTs must have the same element type!");
8108 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8109 "Cannot extract a scalable vector from a fixed length vector!");
8110 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8112 "Extract subvector must be from larger vector to smaller vector!");
8113 assert(N2C && "Extract subvector index must be a constant");
8114 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8115 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8116 N1VT.getVectorMinNumElements()) &&
8117 "Extract subvector overflow!");
8118 assert(N2C->getAPIntValue().getBitWidth() ==
8119 TLI->getVectorIdxWidth(getDataLayout()) &&
8120 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8121 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8122 "Extract index is not a multiple of the output vector length");
8123
8124 // Trivial extraction.
8125 if (VT == N1VT)
8126 return N1;
8127
8128 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8129 if (N1.isUndef())
8130 return getUNDEF(VT);
8131
8132 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8133 // the concat have the same type as the extract.
8134 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8135 VT == N1.getOperand(0).getValueType()) {
8136 unsigned Factor = VT.getVectorMinNumElements();
8137 return N1.getOperand(N2C->getZExtValue() / Factor);
8138 }
8139
8140 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8141 // during shuffle legalization.
8142 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8143 VT == N1.getOperand(1).getValueType())
8144 return N1.getOperand(1);
8145 break;
8146 }
8147 }
8148
8149 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8150 switch (Opcode) {
8151 case ISD::XOR:
8152 case ISD::ADD:
8153 case ISD::PTRADD:
8154 case ISD::SUB:
8156 case ISD::UDIV:
8157 case ISD::SDIV:
8158 case ISD::UREM:
8159 case ISD::SREM:
8160 case ISD::MUL:
8161 case ISD::AND:
8162 case ISD::SSUBSAT:
8163 case ISD::USUBSAT:
8164 case ISD::UMIN:
8165 case ISD::OR:
8166 case ISD::SADDSAT:
8167 case ISD::UADDSAT:
8168 case ISD::UMAX:
8169 case ISD::SMAX:
8170 case ISD::SMIN:
8171 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8172 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8173 }
8174 }
8175
8176 // Canonicalize an UNDEF to the RHS, even over a constant.
8177 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8178 if (TLI->isCommutativeBinOp(Opcode)) {
8179 std::swap(N1, N2);
8180 } else {
8181 switch (Opcode) {
8182 case ISD::PTRADD:
8183 case ISD::SUB:
8184 // fold op(undef, non_undef_arg2) -> undef.
8185 return N1;
8187 case ISD::UDIV:
8188 case ISD::SDIV:
8189 case ISD::UREM:
8190 case ISD::SREM:
8191 case ISD::SSUBSAT:
8192 case ISD::USUBSAT:
8193 // fold op(undef, non_undef_arg2) -> 0.
8194 return getConstant(0, DL, VT);
8195 }
8196 }
8197 }
8198
8199 // Fold a bunch of operators when the RHS is undef.
8200 if (N2.getOpcode() == ISD::UNDEF) {
8201 switch (Opcode) {
8202 case ISD::XOR:
8203 if (N1.getOpcode() == ISD::UNDEF)
8204 // Handle undef ^ undef -> 0 special case. This is a common
8205 // idiom (misuse).
8206 return getConstant(0, DL, VT);
8207 [[fallthrough]];
8208 case ISD::ADD:
8209 case ISD::PTRADD:
8210 case ISD::SUB:
8211 // fold op(arg1, undef) -> undef.
8212 return N2;
8213 case ISD::UDIV:
8214 case ISD::SDIV:
8215 case ISD::UREM:
8216 case ISD::SREM:
8217 // fold op(arg1, undef) -> poison.
8218 return getPOISON(VT);
8219 case ISD::MUL:
8220 case ISD::AND:
8221 case ISD::SSUBSAT:
8222 case ISD::USUBSAT:
8223 case ISD::UMIN:
8224 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8225 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8226 case ISD::OR:
8227 case ISD::SADDSAT:
8228 case ISD::UADDSAT:
8229 case ISD::UMAX:
8230 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8231 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8232 case ISD::SMAX:
8233 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8234 return N1.getOpcode() == ISD::UNDEF
8235 ? N2
8236 : getConstant(
8238 VT);
8239 case ISD::SMIN:
8240 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8241 return N1.getOpcode() == ISD::UNDEF
8242 ? N2
8243 : getConstant(
8245 VT);
8246 }
8247 }
8248
8249 // Perform trivial constant folding.
8250 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8251 return SV;
8252
8253 // Memoize this node if possible.
8254 SDNode *N;
8255 SDVTList VTs = getVTList(VT);
8256 SDValue Ops[] = {N1, N2};
8257 if (VT != MVT::Glue) {
8259 AddNodeIDNode(ID, Opcode, VTs, Ops);
8260 void *IP = nullptr;
8261 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8262 E->intersectFlagsWith(Flags);
8263 return SDValue(E, 0);
8264 }
8265
8266 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8267 N->setFlags(Flags);
8268 createOperands(N, Ops);
8269 CSEMap.InsertNode(N, IP);
8270 } else {
8271 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8272 createOperands(N, Ops);
8273 }
8274
8275 InsertNode(N);
8276 SDValue V = SDValue(N, 0);
8277 NewSDValueDbgMsg(V, "Creating new node: ", this);
8278 return V;
8279}
8280
8281SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8282 SDValue N1, SDValue N2, SDValue N3) {
8283 SDNodeFlags Flags;
8284 if (Inserter)
8285 Flags = Inserter->getFlags();
8286 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8287}
8288
8289SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8290 SDValue N1, SDValue N2, SDValue N3,
8291 const SDNodeFlags Flags) {
8293 N2.getOpcode() != ISD::DELETED_NODE &&
8294 N3.getOpcode() != ISD::DELETED_NODE &&
8295 "Operand is DELETED_NODE!");
8296 // Perform various simplifications.
8297 switch (Opcode) {
8298 case ISD::BUILD_VECTOR: {
8299 // Attempt to simplify BUILD_VECTOR.
8300 SDValue Ops[] = {N1, N2, N3};
8301 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8302 return V;
8303 break;
8304 }
8305 case ISD::CONCAT_VECTORS: {
8306 SDValue Ops[] = {N1, N2, N3};
8307 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8308 return V;
8309 break;
8310 }
8311 case ISD::SETCC: {
8312 assert(VT.isInteger() && "SETCC result type must be an integer!");
8313 assert(N1.getValueType() == N2.getValueType() &&
8314 "SETCC operands must have the same type!");
8315 assert(VT.isVector() == N1.getValueType().isVector() &&
8316 "SETCC type should be vector iff the operand type is vector!");
8317 assert((!VT.isVector() || VT.getVectorElementCount() ==
8319 "SETCC vector element counts must match!");
8320 // Use FoldSetCC to simplify SETCC's.
8321 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8322 return V;
8323 break;
8324 }
8325 case ISD::SELECT:
8326 case ISD::VSELECT:
8327 if (SDValue V = simplifySelect(N1, N2, N3))
8328 return V;
8329 break;
8331 llvm_unreachable("should use getVectorShuffle constructor!");
8333 if (isNullConstant(N3))
8334 return N1;
8335 break;
8337 if (isNullConstant(N3))
8338 return N2;
8339 break;
8341 assert(VT.isVector() && VT == N1.getValueType() &&
8342 "INSERT_VECTOR_ELT vector type mismatch");
8344 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8345 assert((!VT.isFloatingPoint() ||
8346 VT.getVectorElementType() == N2.getValueType()) &&
8347 "INSERT_VECTOR_ELT fp scalar type mismatch");
8348 assert((!VT.isInteger() ||
8350 "INSERT_VECTOR_ELT int scalar size mismatch");
8351
8352 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8353 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8354 // for scalable vectors where we will generate appropriate code to
8355 // deal with out-of-bounds cases correctly.
8356 if (N3C && VT.isFixedLengthVector() &&
8357 N3C->getZExtValue() >= VT.getVectorNumElements())
8358 return getUNDEF(VT);
8359
8360 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8361 if (N3.isUndef())
8362 return getUNDEF(VT);
8363
8364 // If inserting poison, just use the input vector.
8365 if (N2.getOpcode() == ISD::POISON)
8366 return N1;
8367
8368 // Inserting undef into undef/poison is still undef.
8369 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8370 return getUNDEF(VT);
8371
8372 // If the inserted element is an UNDEF, just use the input vector.
8373 // But not if skipping the insert could make the result more poisonous.
8374 if (N2.isUndef()) {
8375 if (N3C && VT.isFixedLengthVector()) {
8376 APInt EltMask =
8377 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8378 if (isGuaranteedNotToBePoison(N1, EltMask))
8379 return N1;
8380 } else if (isGuaranteedNotToBePoison(N1))
8381 return N1;
8382 }
8383 break;
8384 }
8385 case ISD::INSERT_SUBVECTOR: {
8386 // If inserting poison, just use the input vector,
8387 if (N2.getOpcode() == ISD::POISON)
8388 return N1;
8389
8390 // Inserting undef into undef/poison is still undef.
8391 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8392 return getUNDEF(VT);
8393
8394 EVT N2VT = N2.getValueType();
8395 assert(VT == N1.getValueType() &&
8396 "Dest and insert subvector source types must match!");
8397 assert(VT.isVector() && N2VT.isVector() &&
8398 "Insert subvector VTs must be vectors!");
8400 "Insert subvector VTs must have the same element type!");
8401 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8402 "Cannot insert a scalable vector into a fixed length vector!");
8403 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8405 "Insert subvector must be from smaller vector to larger vector!");
8407 "Insert subvector index must be constant");
8408 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8409 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8411 "Insert subvector overflow!");
8413 TLI->getVectorIdxWidth(getDataLayout()) &&
8414 "Constant index for INSERT_SUBVECTOR has an invalid size");
8415
8416 // Trivial insertion.
8417 if (VT == N2VT)
8418 return N2;
8419
8420 // If this is an insert of an extracted vector into an undef/poison vector,
8421 // we can just use the input to the extract. But not if skipping the
8422 // extract+insert could make the result more poisonous.
8423 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8424 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8425 if (N1.getOpcode() == ISD::POISON)
8426 return N2.getOperand(0);
8427 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8428 unsigned LoBit = N3->getAsZExtVal();
8429 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8430 APInt EltMask =
8431 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8432 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8433 return N2.getOperand(0);
8434 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8435 return N2.getOperand(0);
8436 }
8437
8438 // If the inserted subvector is UNDEF, just use the input vector.
8439 // But not if skipping the insert could make the result more poisonous.
8440 if (N2.isUndef()) {
8441 if (VT.isFixedLengthVector()) {
8442 unsigned LoBit = N3->getAsZExtVal();
8443 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8444 APInt EltMask =
8445 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8446 if (isGuaranteedNotToBePoison(N1, EltMask))
8447 return N1;
8448 } else if (isGuaranteedNotToBePoison(N1))
8449 return N1;
8450 }
8451 break;
8452 }
8453 case ISD::BITCAST:
8454 // Fold bit_convert nodes from a type to themselves.
8455 if (N1.getValueType() == VT)
8456 return N1;
8457 break;
8458 case ISD::VP_TRUNCATE:
8459 case ISD::VP_SIGN_EXTEND:
8460 case ISD::VP_ZERO_EXTEND:
8461 // Don't create noop casts.
8462 if (N1.getValueType() == VT)
8463 return N1;
8464 break;
8465 case ISD::VECTOR_COMPRESS: {
8466 [[maybe_unused]] EVT VecVT = N1.getValueType();
8467 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8468 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8469 assert(VT == VecVT && "Vector and result type don't match.");
8470 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8471 "All inputs must be vectors.");
8472 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8474 "Vector and mask must have same number of elements.");
8475
8476 if (N1.isUndef() || N2.isUndef())
8477 return N3;
8478
8479 break;
8480 }
8485 [[maybe_unused]] EVT AccVT = N1.getValueType();
8486 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8487 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8488 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8489 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8490 "node to have the same type!");
8491 assert(VT.isVector() && VT == AccVT &&
8492 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8493 "the same type as its result!");
8495 AccVT.getVectorElementCount()) &&
8496 "Expected the element count of the second and third operands of the "
8497 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8498 "element count of the first operand and the result!");
8500 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8501 "node to have an element type which is the same as or smaller than "
8502 "the element type of the first operand and result!");
8503 break;
8504 }
8505 }
8506
8507 // Perform trivial constant folding for arithmetic operators.
8508 switch (Opcode) {
8509 case ISD::FMA:
8510 case ISD::FMAD:
8511 case ISD::SETCC:
8512 case ISD::FSHL:
8513 case ISD::FSHR:
8514 if (SDValue SV =
8515 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8516 return SV;
8517 break;
8518 }
8519
8520 // Memoize node if it doesn't produce a glue result.
8521 SDNode *N;
8522 SDVTList VTs = getVTList(VT);
8523 SDValue Ops[] = {N1, N2, N3};
8524 if (VT != MVT::Glue) {
8526 AddNodeIDNode(ID, Opcode, VTs, Ops);
8527 void *IP = nullptr;
8528 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8529 E->intersectFlagsWith(Flags);
8530 return SDValue(E, 0);
8531 }
8532
8533 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8534 N->setFlags(Flags);
8535 createOperands(N, Ops);
8536 CSEMap.InsertNode(N, IP);
8537 } else {
8538 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8539 createOperands(N, Ops);
8540 }
8541
8542 InsertNode(N);
8543 SDValue V = SDValue(N, 0);
8544 NewSDValueDbgMsg(V, "Creating new node: ", this);
8545 return V;
8546}
8547
8548SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8549 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8550 const SDNodeFlags Flags) {
8551 SDValue Ops[] = { N1, N2, N3, N4 };
8552 return getNode(Opcode, DL, VT, Ops, Flags);
8553}
8554
8555SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8556 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8557 SDNodeFlags Flags;
8558 if (Inserter)
8559 Flags = Inserter->getFlags();
8560 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8561}
8562
8563SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8564 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8565 SDValue N5, const SDNodeFlags Flags) {
8566 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8567 return getNode(Opcode, DL, VT, Ops, Flags);
8568}
8569
8570SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8571 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8572 SDValue N5) {
8573 SDNodeFlags Flags;
8574 if (Inserter)
8575 Flags = Inserter->getFlags();
8576 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8577}
8578
8579/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8580/// the incoming stack arguments to be loaded from the stack.
8582 SmallVector<SDValue, 8> ArgChains;
8583
8584 // Include the original chain at the beginning of the list. When this is
8585 // used by target LowerCall hooks, this helps legalize find the
8586 // CALLSEQ_BEGIN node.
8587 ArgChains.push_back(Chain);
8588
8589 // Add a chain value for each stack argument.
8590 for (SDNode *U : getEntryNode().getNode()->users())
8591 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8592 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8593 if (FI->getIndex() < 0)
8594 ArgChains.push_back(SDValue(L, 1));
8595
8596 // Build a tokenfactor for all the chains.
8597 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8598}
8599
8600/// getMemsetValue - Vectorized representation of the memset value
8601/// operand.
8603 const SDLoc &dl) {
8604 assert(!Value.isUndef());
8605
8606 unsigned NumBits = VT.getScalarSizeInBits();
8608 assert(C->getAPIntValue().getBitWidth() == 8);
8609 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8610 if (VT.isInteger()) {
8611 bool IsOpaque = VT.getSizeInBits() > 64 ||
8612 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8613 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8614 }
8615 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8616 }
8617
8618 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8619 EVT IntVT = VT.getScalarType();
8620 if (!IntVT.isInteger())
8621 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8622
8623 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8624 if (NumBits > 8) {
8625 // Use a multiplication with 0x010101... to extend the input to the
8626 // required length.
8627 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8628 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8629 DAG.getConstant(Magic, dl, IntVT));
8630 }
8631
8632 if (VT != Value.getValueType() && !VT.isInteger())
8633 Value = DAG.getBitcast(VT.getScalarType(), Value);
8634 if (VT != Value.getValueType())
8635 Value = DAG.getSplatBuildVector(VT, dl, Value);
8636
8637 return Value;
8638}
8639
8640/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8641/// used when a memcpy is turned into a memset when the source is a constant
8642/// string ptr.
8644 const TargetLowering &TLI,
8645 const ConstantDataArraySlice &Slice) {
8646 // Handle vector with all elements zero.
8647 if (Slice.Array == nullptr) {
8648 if (VT.isInteger())
8649 return DAG.getConstant(0, dl, VT);
8650 return DAG.getNode(ISD::BITCAST, dl, VT,
8651 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8652 }
8653
8654 assert(!VT.isVector() && "Can't handle vector type here!");
8655 unsigned NumVTBits = VT.getSizeInBits();
8656 unsigned NumVTBytes = NumVTBits / 8;
8657 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8658
8659 APInt Val(NumVTBits, 0);
8660 if (DAG.getDataLayout().isLittleEndian()) {
8661 for (unsigned i = 0; i != NumBytes; ++i)
8662 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8663 } else {
8664 for (unsigned i = 0; i != NumBytes; ++i)
8665 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8666 }
8667
8668 // If the "cost" of materializing the integer immediate is less than the cost
8669 // of a load, then it is cost effective to turn the load into the immediate.
8670 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8671 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8672 return DAG.getConstant(Val, dl, VT);
8673 return SDValue();
8674}
8675
8677 const SDLoc &DL,
8678 const SDNodeFlags Flags) {
8679 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
8680 return getMemBasePlusOffset(Base, Index, DL, Flags);
8681}
8682
8684 const SDLoc &DL,
8685 const SDNodeFlags Flags) {
8686 assert(Offset.getValueType().isInteger());
8687 EVT BasePtrVT = Ptr.getValueType();
8688 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8689 BasePtrVT))
8690 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8691 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8692 SDNodeFlags AddFlags = Flags;
8693 AddFlags.setInBounds(false);
8694 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8695}
8696
8697/// Returns true if memcpy source is constant data.
8699 uint64_t SrcDelta = 0;
8700 GlobalAddressSDNode *G = nullptr;
8701 if (Src.getOpcode() == ISD::GlobalAddress)
8703 else if (Src->isAnyAdd() &&
8704 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8705 Src.getOperand(1).getOpcode() == ISD::Constant) {
8706 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8707 SrcDelta = Src.getConstantOperandVal(1);
8708 }
8709 if (!G)
8710 return false;
8711
8712 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8713 SrcDelta + G->getOffset());
8714}
8715
8717 SelectionDAG &DAG) {
8718 // On Darwin, -Os means optimize for size without hurting performance, so
8719 // only really optimize for size when -Oz (MinSize) is used.
8721 return MF.getFunction().hasMinSize();
8722 return DAG.shouldOptForSize();
8723}
8724
8726 SmallVector<SDValue, 32> &OutChains, unsigned From,
8727 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8728 SmallVector<SDValue, 16> &OutStoreChains) {
8729 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8730 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8731 SmallVector<SDValue, 16> GluedLoadChains;
8732 for (unsigned i = From; i < To; ++i) {
8733 OutChains.push_back(OutLoadChains[i]);
8734 GluedLoadChains.push_back(OutLoadChains[i]);
8735 }
8736
8737 // Chain for all loads.
8738 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8739 GluedLoadChains);
8740
8741 for (unsigned i = From; i < To; ++i) {
8742 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8743 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8744 ST->getBasePtr(), ST->getMemoryVT(),
8745 ST->getMemOperand());
8746 OutChains.push_back(NewStore);
8747 }
8748}
8749
8751 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8752 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8753 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8754 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8755 // Turn a memcpy of undef to nop.
8756 // FIXME: We need to honor volatile even is Src is undef.
8757 if (Src.isUndef())
8758 return Chain;
8759
8760 // Expand memcpy to a series of load and store ops if the size operand falls
8761 // below a certain threshold.
8762 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8763 // rather than maybe a humongous number of loads and stores.
8764 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8765 const DataLayout &DL = DAG.getDataLayout();
8766 LLVMContext &C = *DAG.getContext();
8767 std::vector<EVT> MemOps;
8768 bool DstAlignCanChange = false;
8770 MachineFrameInfo &MFI = MF.getFrameInfo();
8771 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8773 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8774 DstAlignCanChange = true;
8775 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8776 if (!SrcAlign || Alignment > *SrcAlign)
8777 SrcAlign = Alignment;
8778 assert(SrcAlign && "SrcAlign must be set");
8780 // If marked as volatile, perform a copy even when marked as constant.
8781 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8782 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8783 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8784 const MemOp Op = isZeroConstant
8785 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8786 /*IsZeroMemset*/ true, isVol)
8787 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8788 *SrcAlign, isVol, CopyFromConstant);
8789 if (!TLI.findOptimalMemOpLowering(
8790 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8791 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes(), nullptr))
8792 return SDValue();
8793
8794 if (DstAlignCanChange) {
8795 Type *Ty = MemOps[0].getTypeForEVT(C);
8796 Align NewAlign = DL.getABITypeAlign(Ty);
8797
8798 // Don't promote to an alignment that would require dynamic stack
8799 // realignment which may conflict with optimizations such as tail call
8800 // optimization.
8802 if (!TRI->hasStackRealignment(MF))
8803 if (MaybeAlign StackAlign = DL.getStackAlignment())
8804 NewAlign = std::min(NewAlign, *StackAlign);
8805
8806 if (NewAlign > Alignment) {
8807 // Give the stack frame object a larger alignment if needed.
8808 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8809 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8810 Alignment = NewAlign;
8811 }
8812 }
8813
8814 // Prepare AAInfo for loads/stores after lowering this memcpy.
8815 AAMDNodes NewAAInfo = AAInfo;
8816 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8817
8818 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8819 bool isConstant =
8820 BatchAA && SrcVal &&
8821 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8822
8823 MachineMemOperand::Flags MMOFlags =
8825 SmallVector<SDValue, 16> OutLoadChains;
8826 SmallVector<SDValue, 16> OutStoreChains;
8827 SmallVector<SDValue, 32> OutChains;
8828 unsigned NumMemOps = MemOps.size();
8829 uint64_t SrcOff = 0, DstOff = 0;
8830 for (unsigned i = 0; i != NumMemOps; ++i) {
8831 EVT VT = MemOps[i];
8832 unsigned VTSize = VT.getSizeInBits() / 8;
8833 SDValue Value, Store;
8834
8835 if (VTSize > Size) {
8836 // Issuing an unaligned load / store pair that overlaps with the previous
8837 // pair. Adjust the offset accordingly.
8838 assert(i == NumMemOps-1 && i != 0);
8839 SrcOff -= VTSize - Size;
8840 DstOff -= VTSize - Size;
8841 }
8842
8843 if (CopyFromConstant &&
8844 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8845 // It's unlikely a store of a vector immediate can be done in a single
8846 // instruction. It would require a load from a constantpool first.
8847 // We only handle zero vectors here.
8848 // FIXME: Handle other cases where store of vector immediate is done in
8849 // a single instruction.
8850 ConstantDataArraySlice SubSlice;
8851 if (SrcOff < Slice.Length) {
8852 SubSlice = Slice;
8853 SubSlice.move(SrcOff);
8854 } else {
8855 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8856 SubSlice.Array = nullptr;
8857 SubSlice.Offset = 0;
8858 SubSlice.Length = VTSize;
8859 }
8860 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8861 if (Value.getNode()) {
8862 Store = DAG.getStore(
8863 Chain, dl, Value,
8864 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8865 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8866 OutChains.push_back(Store);
8867 }
8868 }
8869
8870 if (!Store.getNode()) {
8871 // The type might not be legal for the target. This should only happen
8872 // if the type is smaller than a legal type, as on PPC, so the right
8873 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8874 // to Load/Store if NVT==VT.
8875 // FIXME does the case above also need this?
8876 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8877 assert(NVT.bitsGE(VT));
8878
8879 bool isDereferenceable =
8880 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8881 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8882 if (isDereferenceable)
8884 if (isConstant)
8885 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8886
8887 Value = DAG.getExtLoad(
8888 ISD::EXTLOAD, dl, NVT, Chain,
8889 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8890 SrcPtrInfo.getWithOffset(SrcOff), VT,
8891 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8892 OutLoadChains.push_back(Value.getValue(1));
8893
8894 Store = DAG.getTruncStore(
8895 Chain, dl, Value,
8896 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8897 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8898 OutStoreChains.push_back(Store);
8899 }
8900 SrcOff += VTSize;
8901 DstOff += VTSize;
8902 Size -= VTSize;
8903 }
8904
8905 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8907 unsigned NumLdStInMemcpy = OutStoreChains.size();
8908
8909 if (NumLdStInMemcpy) {
8910 // It may be that memcpy might be converted to memset if it's memcpy
8911 // of constants. In such a case, we won't have loads and stores, but
8912 // just stores. In the absence of loads, there is nothing to gang up.
8913 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8914 // If target does not care, just leave as it.
8915 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8916 OutChains.push_back(OutLoadChains[i]);
8917 OutChains.push_back(OutStoreChains[i]);
8918 }
8919 } else {
8920 // Ld/St less than/equal limit set by target.
8921 if (NumLdStInMemcpy <= GluedLdStLimit) {
8922 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8923 NumLdStInMemcpy, OutLoadChains,
8924 OutStoreChains);
8925 } else {
8926 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8927 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8928 unsigned GlueIter = 0;
8929
8930 // Residual ld/st.
8931 if (RemainingLdStInMemcpy) {
8933 DAG, dl, OutChains, NumLdStInMemcpy - RemainingLdStInMemcpy,
8934 NumLdStInMemcpy, OutLoadChains, OutStoreChains);
8935 }
8936
8937 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8938 unsigned IndexFrom = NumLdStInMemcpy - RemainingLdStInMemcpy -
8939 GlueIter - GluedLdStLimit;
8940 unsigned IndexTo = NumLdStInMemcpy - RemainingLdStInMemcpy - GlueIter;
8941 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8942 OutLoadChains, OutStoreChains);
8943 GlueIter += GluedLdStLimit;
8944 }
8945 }
8946 }
8947 }
8948 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8949}
8950
8952 SDValue Chain, SDValue Dst, SDValue Src,
8953 uint64_t Size, Align Alignment,
8954 bool isVol, bool AlwaysInline,
8955 MachinePointerInfo DstPtrInfo,
8956 MachinePointerInfo SrcPtrInfo,
8957 const AAMDNodes &AAInfo) {
8958 // Turn a memmove of undef to nop.
8959 // FIXME: We need to honor volatile even is Src is undef.
8960 if (Src.isUndef())
8961 return Chain;
8962
8963 // Expand memmove to a series of load and store ops if the size operand falls
8964 // below a certain threshold.
8965 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8966 const DataLayout &DL = DAG.getDataLayout();
8967 LLVMContext &C = *DAG.getContext();
8968 std::vector<EVT> MemOps;
8969 bool DstAlignCanChange = false;
8971 MachineFrameInfo &MFI = MF.getFrameInfo();
8972 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8974 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8975 DstAlignCanChange = true;
8976 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8977 if (!SrcAlign || Alignment > *SrcAlign)
8978 SrcAlign = Alignment;
8979 assert(SrcAlign && "SrcAlign must be set");
8980 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8981 if (!TLI.findOptimalMemOpLowering(
8982 C, MemOps, Limit,
8983 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8984 /*IsVolatile*/ true),
8985 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8986 MF.getFunction().getAttributes(), nullptr))
8987 return SDValue();
8988
8989 if (DstAlignCanChange) {
8990 Type *Ty = MemOps[0].getTypeForEVT(C);
8991 Align NewAlign = DL.getABITypeAlign(Ty);
8992
8993 // Don't promote to an alignment that would require dynamic stack
8994 // realignment which may conflict with optimizations such as tail call
8995 // optimization.
8997 if (!TRI->hasStackRealignment(MF))
8998 if (MaybeAlign StackAlign = DL.getStackAlignment())
8999 NewAlign = std::min(NewAlign, *StackAlign);
9000
9001 if (NewAlign > Alignment) {
9002 // Give the stack frame object a larger alignment if needed.
9003 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9004 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9005 Alignment = NewAlign;
9006 }
9007 }
9008
9009 // Prepare AAInfo for loads/stores after lowering this memmove.
9010 AAMDNodes NewAAInfo = AAInfo;
9011 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9012
9013 MachineMemOperand::Flags MMOFlags =
9015 uint64_t SrcOff = 0, DstOff = 0;
9016 SmallVector<SDValue, 8> LoadValues;
9017 SmallVector<SDValue, 8> LoadChains;
9018 SmallVector<SDValue, 8> OutChains;
9019 unsigned NumMemOps = MemOps.size();
9020 for (unsigned i = 0; i < NumMemOps; i++) {
9021 EVT VT = MemOps[i];
9022 unsigned VTSize = VT.getSizeInBits() / 8;
9023 SDValue Value;
9024
9025 bool isDereferenceable =
9026 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9027 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9028 if (isDereferenceable)
9030
9031 Value = DAG.getLoad(
9032 VT, dl, Chain,
9033 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9034 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
9035 LoadValues.push_back(Value);
9036 LoadChains.push_back(Value.getValue(1));
9037 SrcOff += VTSize;
9038 }
9039 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
9040 OutChains.clear();
9041 for (unsigned i = 0; i < NumMemOps; i++) {
9042 EVT VT = MemOps[i];
9043 unsigned VTSize = VT.getSizeInBits() / 8;
9044 SDValue Store;
9045
9046 Store = DAG.getStore(
9047 Chain, dl, LoadValues[i],
9048 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9049 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
9050 OutChains.push_back(Store);
9051 DstOff += VTSize;
9052 }
9053
9054 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9055}
9056
9057/// Lower the call to 'memset' intrinsic function into a series of store
9058/// operations.
9059///
9060/// \param DAG Selection DAG where lowered code is placed.
9061/// \param dl Link to corresponding IR location.
9062/// \param Chain Control flow dependency.
9063/// \param Dst Pointer to destination memory location.
9064/// \param Src Value of byte to write into the memory.
9065/// \param Size Number of bytes to write.
9066/// \param Alignment Alignment of the destination in bytes.
9067/// \param isVol True if destination is volatile.
9068/// \param AlwaysInline Makes sure no function call is generated.
9069/// \param DstPtrInfo IR information on the memory pointer.
9070/// \returns New head in the control flow, if lowering was successful, empty
9071/// SDValue otherwise.
9072///
9073/// The function tries to replace 'llvm.memset' intrinsic with several store
9074/// operations and value calculation code. This is usually profitable for small
9075/// memory size or when the semantic requires inlining.
9077 SDValue Chain, SDValue Dst, SDValue Src,
9078 uint64_t Size, Align Alignment, bool isVol,
9079 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9080 const AAMDNodes &AAInfo) {
9081 // Turn a memset of undef to nop.
9082 // FIXME: We need to honor volatile even is Src is undef.
9083 if (Src.isUndef())
9084 return Chain;
9085
9086 // Expand memset to a series of load/store ops if the size operand
9087 // falls below a certain threshold.
9088 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9089 std::vector<EVT> MemOps;
9090 bool DstAlignCanChange = false;
9091 LLVMContext &C = *DAG.getContext();
9093 MachineFrameInfo &MFI = MF.getFrameInfo();
9094 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9096 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9097 DstAlignCanChange = true;
9098 bool IsZeroVal = isNullConstant(Src);
9099 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9100
9101 EVT LargestVT;
9102 if (!TLI.findOptimalMemOpLowering(
9103 C, MemOps, Limit,
9104 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9105 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes(),
9106 &LargestVT))
9107 return SDValue();
9108
9109 if (DstAlignCanChange) {
9110 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9111 const DataLayout &DL = DAG.getDataLayout();
9112 Align NewAlign = DL.getABITypeAlign(Ty);
9113
9114 // Don't promote to an alignment that would require dynamic stack
9115 // realignment which may conflict with optimizations such as tail call
9116 // optimization.
9118 if (!TRI->hasStackRealignment(MF))
9119 if (MaybeAlign StackAlign = DL.getStackAlignment())
9120 NewAlign = std::min(NewAlign, *StackAlign);
9121
9122 if (NewAlign > Alignment) {
9123 // Give the stack frame object a larger alignment if needed.
9124 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9125 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9126 Alignment = NewAlign;
9127 }
9128 }
9129
9130 SmallVector<SDValue, 8> OutChains;
9131 uint64_t DstOff = 0;
9132 unsigned NumMemOps = MemOps.size();
9133
9134 // Find the largest store and generate the bit pattern for it.
9135 // If target didn't set LargestVT, compute it from MemOps.
9136 if (!LargestVT.isSimple()) {
9137 LargestVT = MemOps[0];
9138 for (unsigned i = 1; i < NumMemOps; i++)
9139 if (MemOps[i].bitsGT(LargestVT))
9140 LargestVT = MemOps[i];
9141 }
9142 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9143
9144 // Prepare AAInfo for loads/stores after lowering this memset.
9145 AAMDNodes NewAAInfo = AAInfo;
9146 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9147
9148 for (unsigned i = 0; i < NumMemOps; i++) {
9149 EVT VT = MemOps[i];
9150 unsigned VTSize = VT.getSizeInBits() / 8;
9151 // The target should specify store types that exactly cover the memset size
9152 // (with the last store potentially being oversized for overlapping stores).
9153 assert(Size > 0 && "Target specified more stores than needed in "
9154 "findOptimalMemOpLowering");
9155 if (VTSize > Size) {
9156 // Issuing an unaligned load / store pair that overlaps with the previous
9157 // pair. Adjust the offset accordingly.
9158 assert(i == NumMemOps-1 && i != 0);
9159 DstOff -= VTSize - Size;
9160 }
9161
9162 // If this store is smaller than the largest store see whether we can get
9163 // the smaller value for free with a truncate or extract vector element and
9164 // then store.
9165 SDValue Value = MemSetValue;
9166 if (VT.bitsLT(LargestVT)) {
9167 unsigned Index;
9168 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9169 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9170 if (!LargestVT.isVector() && !VT.isVector() &&
9171 TLI.isTruncateFree(LargestVT, VT))
9172 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9173 else if (LargestVT.isVector() && !VT.isVector() &&
9175 LargestVT.getTypeForEVT(*DAG.getContext()),
9176 VT.getSizeInBits(), Index) &&
9177 TLI.isTypeLegal(SVT) &&
9178 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9179 // Target which can combine store(extractelement VectorTy, Idx) can get
9180 // the smaller value for free.
9181 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9182 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9183 } else
9184 Value = getMemsetValue(Src, VT, DAG, dl);
9185 }
9186 assert(Value.getValueType() == VT && "Value with wrong type.");
9187 SDValue Store = DAG.getStore(
9188 Chain, dl, Value,
9189 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9190 DstPtrInfo.getWithOffset(DstOff), Alignment,
9192 NewAAInfo);
9193 OutChains.push_back(Store);
9194 DstOff += VT.getSizeInBits() / 8;
9195 // For oversized overlapping stores, only subtract the remaining bytes.
9196 // For normal stores, subtract the full store size.
9197 if (VTSize > Size) {
9198 Size = 0;
9199 } else {
9200 Size -= VTSize;
9201 }
9202 }
9203
9204 // After processing all stores, Size should be exactly 0. Any remaining bytes
9205 // indicate a bug in the target's findOptimalMemOpLowering implementation.
9206 assert(Size == 0 && "Target's findOptimalMemOpLowering did not specify "
9207 "stores that exactly cover the memset size");
9208
9209 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9210}
9211
9213 unsigned AS) {
9214 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9215 // pointer operands can be losslessly bitcasted to pointers of address space 0
9216 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9217 report_fatal_error("cannot lower memory intrinsic in address space " +
9218 Twine(AS));
9219 }
9220}
9221
9223 const SelectionDAG *SelDAG,
9224 bool AllowReturnsFirstArg) {
9225 if (!CI || !CI->isTailCall())
9226 return false;
9227 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9228 // helper symbol we lower to.
9229 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9230 AllowReturnsFirstArg &&
9232}
9233
9234static std::pair<SDValue, SDValue>
9237 const CallInst *CI, RTLIB::Libcall Call,
9238 SelectionDAG *DAG, const TargetLowering *TLI) {
9239 RTLIB::LibcallImpl LCImpl = DAG->getLibcalls().getLibcallImpl(Call);
9240
9241 if (LCImpl == RTLIB::Unsupported)
9242 return {};
9243
9245 bool IsTailCall =
9246 isInTailCallPositionWrapper(CI, DAG, /*AllowReturnsFirstArg=*/true);
9247 SDValue Callee =
9248 DAG->getExternalSymbol(LCImpl, TLI->getPointerTy(DAG->getDataLayout()));
9249
9250 CLI.setDebugLoc(dl)
9251 .setChain(Chain)
9253 CI->getType(), Callee, std::move(Args))
9254 .setTailCall(IsTailCall);
9255
9256 return TLI->LowerCallTo(CLI);
9257}
9258
9259std::pair<SDValue, SDValue> SelectionDAG::getStrstr(SDValue Chain,
9260 const SDLoc &dl, SDValue S1,
9261 SDValue S2,
9262 const CallInst *CI) {
9264 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9265 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9266 RTLIB::STRSTR, this, TLI);
9267}
9268
9269std::pair<SDValue, SDValue>
9271 SDValue Mem1, SDValue Size, const CallInst *CI) {
9272 RTLIB::LibcallImpl MemcmpImpl = Libcalls->getLibcallImpl(RTLIB::MEMCMP);
9273 if (MemcmpImpl == RTLIB::Unsupported)
9274 return {};
9275
9278 {Mem0, PT},
9279 {Mem1, PT},
9281
9283 bool IsTailCall =
9284 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9285
9286 CLI.setDebugLoc(dl)
9287 .setChain(Chain)
9288 .setLibCallee(
9289 Libcalls->getLibcallImplCallingConv(MemcmpImpl),
9291 getExternalSymbol(MemcmpImpl, TLI->getPointerTy(getDataLayout())),
9292 std::move(Args))
9293 .setTailCall(IsTailCall);
9294
9295 return TLI->LowerCallTo(CLI);
9296}
9297
9298std::pair<SDValue, SDValue> SelectionDAG::getStrcpy(SDValue Chain,
9299 const SDLoc &dl,
9300 SDValue Dst, SDValue Src,
9301 const CallInst *CI) {
9302 RTLIB::LibcallImpl LCImpl = Libcalls->getLibcallImpl(RTLIB::STRCPY);
9303 if (LCImpl == RTLIB::Unsupported)
9304 return {};
9305
9307 TargetLowering::ArgListTy Args = {{Dst, PT}, {Src, PT}};
9308
9310 bool IsTailCall =
9311 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg=*/true);
9312
9313 CLI.setDebugLoc(dl)
9314 .setChain(Chain)
9315 .setLibCallee(
9316 Libcalls->getLibcallImplCallingConv(LCImpl), CI->getType(),
9317 getExternalSymbol(LCImpl, TLI->getPointerTy(getDataLayout())),
9318 std::move(Args))
9319 .setTailCall(IsTailCall);
9320
9321 return TLI->LowerCallTo(CLI);
9322}
9323
9324std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9325 const SDLoc &dl,
9326 SDValue Src,
9327 const CallInst *CI) {
9328 RTLIB::LibcallImpl StrlenImpl = Libcalls->getLibcallImpl(RTLIB::STRLEN);
9329 if (StrlenImpl == RTLIB::Unsupported)
9330 return {};
9331
9332 // Emit a library call.
9335
9337 bool IsTailCall =
9338 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9339
9340 CLI.setDebugLoc(dl)
9341 .setChain(Chain)
9342 .setLibCallee(Libcalls->getLibcallImplCallingConv(StrlenImpl),
9343 CI->getType(),
9345 StrlenImpl, TLI->getProgramPointerTy(getDataLayout())),
9346 std::move(Args))
9347 .setTailCall(IsTailCall);
9348
9349 return TLI->LowerCallTo(CLI);
9350}
9351
9353 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9354 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9355 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9356 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9357 BatchAAResults *BatchAA) {
9358 // Check to see if we should lower the memcpy to loads and stores first.
9359 // For cases within the target-specified limits, this is the best choice.
9361 if (ConstantSize) {
9362 // Memcpy with size zero? Just return the original chain.
9363 if (ConstantSize->isZero())
9364 return Chain;
9365
9367 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9368 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9369 if (Result.getNode())
9370 return Result;
9371 }
9372
9373 // Then check to see if we should lower the memcpy with target-specific
9374 // code. If the target chooses to do this, this is the next best.
9375 if (TSI) {
9376 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9377 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9378 DstPtrInfo, SrcPtrInfo);
9379 if (Result.getNode())
9380 return Result;
9381 }
9382
9383 // If we really need inline code and the target declined to provide it,
9384 // use a (potentially long) sequence of loads and stores.
9385 if (AlwaysInline) {
9386 assert(ConstantSize && "AlwaysInline requires a constant size!");
9388 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9389 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9390 }
9391
9394
9395 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9396 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9397 // respect volatile, so they may do things like read or write memory
9398 // beyond the given memory regions. But fixing this isn't easy, and most
9399 // people don't care.
9400
9401 // Emit a library call.
9404 Args.emplace_back(Dst, PtrTy);
9405 Args.emplace_back(Src, PtrTy);
9406 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9407 // FIXME: pass in SDLoc
9409 bool IsTailCall = false;
9410 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9411
9412 if (OverrideTailCall.has_value()) {
9413 IsTailCall = *OverrideTailCall;
9414 } else {
9415 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9416 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9417 }
9418
9419 CLI.setDebugLoc(dl)
9420 .setChain(Chain)
9421 .setLibCallee(
9422 Libcalls->getLibcallImplCallingConv(MemCpyImpl),
9423 Dst.getValueType().getTypeForEVT(*getContext()),
9424 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
9425 std::move(Args))
9427 .setTailCall(IsTailCall);
9428
9429 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9430 return CallResult.second;
9431}
9432
9434 SDValue Dst, SDValue Src, SDValue Size,
9435 Type *SizeTy, unsigned ElemSz,
9436 bool isTailCall,
9437 MachinePointerInfo DstPtrInfo,
9438 MachinePointerInfo SrcPtrInfo) {
9439 // Emit a library call.
9442 Args.emplace_back(Dst, ArgTy);
9443 Args.emplace_back(Src, ArgTy);
9444 Args.emplace_back(Size, SizeTy);
9445
9446 RTLIB::Libcall LibraryCall =
9448 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9449 if (LibcallImpl == RTLIB::Unsupported)
9450 report_fatal_error("Unsupported element size");
9451
9453 CLI.setDebugLoc(dl)
9454 .setChain(Chain)
9455 .setLibCallee(
9456 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9458 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9459 std::move(Args))
9461 .setTailCall(isTailCall);
9462
9463 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9464 return CallResult.second;
9465}
9466
9468 SDValue Src, SDValue Size, Align Alignment,
9469 bool isVol, const CallInst *CI,
9470 std::optional<bool> OverrideTailCall,
9471 MachinePointerInfo DstPtrInfo,
9472 MachinePointerInfo SrcPtrInfo,
9473 const AAMDNodes &AAInfo,
9474 BatchAAResults *BatchAA) {
9475 // Check to see if we should lower the memmove to loads and stores first.
9476 // For cases within the target-specified limits, this is the best choice.
9478 if (ConstantSize) {
9479 // Memmove with size zero? Just return the original chain.
9480 if (ConstantSize->isZero())
9481 return Chain;
9482
9484 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9485 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9486 if (Result.getNode())
9487 return Result;
9488 }
9489
9490 // Then check to see if we should lower the memmove with target-specific
9491 // code. If the target chooses to do this, this is the next best.
9492 if (TSI) {
9493 SDValue Result =
9494 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9495 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9496 if (Result.getNode())
9497 return Result;
9498 }
9499
9502
9503 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9504 // not be safe. See memcpy above for more details.
9505
9506 // Emit a library call.
9509 Args.emplace_back(Dst, PtrTy);
9510 Args.emplace_back(Src, PtrTy);
9511 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9512 // FIXME: pass in SDLoc
9514
9515 RTLIB::LibcallImpl MemmoveImpl = Libcalls->getLibcallImpl(RTLIB::MEMMOVE);
9516
9517 bool IsTailCall = false;
9518 if (OverrideTailCall.has_value()) {
9519 IsTailCall = *OverrideTailCall;
9520 } else {
9521 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9522 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9523 }
9524
9525 CLI.setDebugLoc(dl)
9526 .setChain(Chain)
9527 .setLibCallee(
9528 Libcalls->getLibcallImplCallingConv(MemmoveImpl),
9529 Dst.getValueType().getTypeForEVT(*getContext()),
9530 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
9531 std::move(Args))
9533 .setTailCall(IsTailCall);
9534
9535 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9536 return CallResult.second;
9537}
9538
9540 SDValue Dst, SDValue Src, SDValue Size,
9541 Type *SizeTy, unsigned ElemSz,
9542 bool isTailCall,
9543 MachinePointerInfo DstPtrInfo,
9544 MachinePointerInfo SrcPtrInfo) {
9545 // Emit a library call.
9547 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9548 Args.emplace_back(Dst, IntPtrTy);
9549 Args.emplace_back(Src, IntPtrTy);
9550 Args.emplace_back(Size, SizeTy);
9551
9552 RTLIB::Libcall LibraryCall =
9554 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9555 if (LibcallImpl == RTLIB::Unsupported)
9556 report_fatal_error("Unsupported element size");
9557
9559 CLI.setDebugLoc(dl)
9560 .setChain(Chain)
9561 .setLibCallee(
9562 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9564 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9565 std::move(Args))
9567 .setTailCall(isTailCall);
9568
9569 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9570 return CallResult.second;
9571}
9572
9574 SDValue Src, SDValue Size, Align Alignment,
9575 bool isVol, bool AlwaysInline,
9576 const CallInst *CI,
9577 MachinePointerInfo DstPtrInfo,
9578 const AAMDNodes &AAInfo) {
9579 // Check to see if we should lower the memset to stores first.
9580 // For cases within the target-specified limits, this is the best choice.
9582 if (ConstantSize) {
9583 // Memset with size zero? Just return the original chain.
9584 if (ConstantSize->isZero())
9585 return Chain;
9586
9587 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9588 ConstantSize->getZExtValue(), Alignment,
9589 isVol, false, DstPtrInfo, AAInfo);
9590
9591 if (Result.getNode())
9592 return Result;
9593 }
9594
9595 // Then check to see if we should lower the memset with target-specific
9596 // code. If the target chooses to do this, this is the next best.
9597 if (TSI) {
9598 SDValue Result = TSI->EmitTargetCodeForMemset(
9599 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9600 if (Result.getNode())
9601 return Result;
9602 }
9603
9604 // If we really need inline code and the target declined to provide it,
9605 // use a (potentially long) sequence of loads and stores.
9606 if (AlwaysInline) {
9607 assert(ConstantSize && "AlwaysInline requires a constant size!");
9608 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9609 ConstantSize->getZExtValue(), Alignment,
9610 isVol, true, DstPtrInfo, AAInfo);
9611 assert(Result &&
9612 "getMemsetStores must return a valid sequence when AlwaysInline");
9613 return Result;
9614 }
9615
9617
9618 // Emit a library call.
9619 auto &Ctx = *getContext();
9620 const auto& DL = getDataLayout();
9621
9623 // FIXME: pass in SDLoc
9624 CLI.setDebugLoc(dl).setChain(Chain);
9625
9626 RTLIB::LibcallImpl BzeroImpl = Libcalls->getLibcallImpl(RTLIB::BZERO);
9627 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
9628
9629 // If zeroing out and bzero is present, use it.
9630 if (UseBZero) {
9632 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9633 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9634 CLI.setLibCallee(
9635 Libcalls->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
9636 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
9637 } else {
9638 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
9639
9641 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9642 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9643 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9644 CLI.setLibCallee(Libcalls->getLibcallImplCallingConv(MemsetImpl),
9645 Dst.getValueType().getTypeForEVT(Ctx),
9646 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
9647 std::move(Args));
9648 }
9649
9650 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
9651 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9652
9653 // If we're going to use bzero, make sure not to tail call unless the
9654 // subsequent return doesn't need a value, as bzero doesn't return the first
9655 // arg unlike memset.
9656 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9657 bool IsTailCall =
9658 CI && CI->isTailCall() &&
9659 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9660 CLI.setDiscardResult().setTailCall(IsTailCall);
9661
9662 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9663 return CallResult.second;
9664}
9665
9668 Type *SizeTy, unsigned ElemSz,
9669 bool isTailCall,
9670 MachinePointerInfo DstPtrInfo) {
9671 // Emit a library call.
9673 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9674 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9675 Args.emplace_back(Size, SizeTy);
9676
9677 RTLIB::Libcall LibraryCall =
9679 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9680 if (LibcallImpl == RTLIB::Unsupported)
9681 report_fatal_error("Unsupported element size");
9682
9684 CLI.setDebugLoc(dl)
9685 .setChain(Chain)
9686 .setLibCallee(
9687 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9689 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9690 std::move(Args))
9692 .setTailCall(isTailCall);
9693
9694 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9695 return CallResult.second;
9696}
9697
9698SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9700 MachineMemOperand *MMO,
9701 ISD::LoadExtType ExtType) {
9703 AddNodeIDNode(ID, Opcode, VTList, Ops);
9704 ID.AddInteger(MemVT.getRawBits());
9705 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9706 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9707 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9708 ID.AddInteger(MMO->getFlags());
9709 void* IP = nullptr;
9710 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9711 E->refineAlignment(MMO);
9712 E->refineRanges(MMO);
9713 return SDValue(E, 0);
9714 }
9715
9716 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9717 VTList, MemVT, MMO, ExtType);
9718 createOperands(N, Ops);
9719
9720 CSEMap.InsertNode(N, IP);
9721 InsertNode(N);
9722 SDValue V(N, 0);
9723 NewSDValueDbgMsg(V, "Creating new node: ", this);
9724 return V;
9725}
9726
9728 EVT MemVT, SDVTList VTs, SDValue Chain,
9729 SDValue Ptr, SDValue Cmp, SDValue Swp,
9730 MachineMemOperand *MMO) {
9731 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9733 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9734
9735 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9736 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9737}
9738
9739SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9740 SDValue Chain, SDValue Ptr, SDValue Val,
9741 MachineMemOperand *MMO) {
9742 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9743 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9744 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9745 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9746 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9747 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9748 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9749 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9750 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9751 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9752 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9753 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9754 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9755 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9756 Opcode == ISD::ATOMIC_STORE) &&
9757 "Invalid Atomic Op");
9758
9759 EVT VT = Val.getValueType();
9760
9761 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9762 getVTList(VT, MVT::Other);
9763 SDValue Ops[] = {Chain, Ptr, Val};
9764 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9765}
9766
9768 EVT MemVT, EVT VT, SDValue Chain,
9769 SDValue Ptr, MachineMemOperand *MMO) {
9770 SDVTList VTs = getVTList(VT, MVT::Other);
9771 SDValue Ops[] = {Chain, Ptr};
9772 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9773}
9774
9775/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9777 if (Ops.size() == 1)
9778 return Ops[0];
9779
9781 VTs.reserve(Ops.size());
9782 for (const SDValue &Op : Ops)
9783 VTs.push_back(Op.getValueType());
9784 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9785}
9786
9788 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9789 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9791 const AAMDNodes &AAInfo) {
9792 if (Size.hasValue() && !Size.getValue())
9794
9796 MachineMemOperand *MMO =
9797 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9798
9799 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9800}
9801
9803 SDVTList VTList,
9804 ArrayRef<SDValue> Ops, EVT MemVT,
9805 MachineMemOperand *MMO) {
9806 assert(
9807 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9808 Opcode == ISD::PREFETCH ||
9809 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9810 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9811 "Opcode is not a memory-accessing opcode!");
9812
9813 // Memoize the node unless it returns a glue result.
9815 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9817 AddNodeIDNode(ID, Opcode, VTList, Ops);
9818 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9819 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9820 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9821 ID.AddInteger(MMO->getFlags());
9822 ID.AddInteger(MemVT.getRawBits());
9823 void *IP = nullptr;
9824 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9825 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9826 return SDValue(E, 0);
9827 }
9828
9829 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9830 VTList, MemVT, MMO);
9831 createOperands(N, Ops);
9832
9833 CSEMap.InsertNode(N, IP);
9834 } else {
9835 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9836 VTList, MemVT, MMO);
9837 createOperands(N, Ops);
9838 }
9839 InsertNode(N);
9840 SDValue V(N, 0);
9841 NewSDValueDbgMsg(V, "Creating new node: ", this);
9842 return V;
9843}
9844
9846 SDValue Chain, int FrameIndex) {
9847 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9848 const auto VTs = getVTList(MVT::Other);
9849 SDValue Ops[2] = {
9850 Chain,
9851 getFrameIndex(FrameIndex,
9852 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9853 true)};
9854
9856 AddNodeIDNode(ID, Opcode, VTs, Ops);
9857 ID.AddInteger(FrameIndex);
9858 void *IP = nullptr;
9859 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9860 return SDValue(E, 0);
9861
9862 LifetimeSDNode *N =
9863 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9864 createOperands(N, Ops);
9865 CSEMap.InsertNode(N, IP);
9866 InsertNode(N);
9867 SDValue V(N, 0);
9868 NewSDValueDbgMsg(V, "Creating new node: ", this);
9869 return V;
9870}
9871
9873 uint64_t Guid, uint64_t Index,
9874 uint32_t Attr) {
9875 const unsigned Opcode = ISD::PSEUDO_PROBE;
9876 const auto VTs = getVTList(MVT::Other);
9877 SDValue Ops[] = {Chain};
9879 AddNodeIDNode(ID, Opcode, VTs, Ops);
9880 ID.AddInteger(Guid);
9881 ID.AddInteger(Index);
9882 void *IP = nullptr;
9883 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9884 return SDValue(E, 0);
9885
9886 auto *N = newSDNode<PseudoProbeSDNode>(
9887 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9888 createOperands(N, Ops);
9889 CSEMap.InsertNode(N, IP);
9890 InsertNode(N);
9891 SDValue V(N, 0);
9892 NewSDValueDbgMsg(V, "Creating new node: ", this);
9893 return V;
9894}
9895
9896/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9897/// MachinePointerInfo record from it. This is particularly useful because the
9898/// code generator has many cases where it doesn't bother passing in a
9899/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9901 SelectionDAG &DAG, SDValue Ptr,
9902 int64_t Offset = 0) {
9903 // If this is FI+Offset, we can model it.
9904 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9906 FI->getIndex(), Offset);
9907
9908 // If this is (FI+Offset1)+Offset2, we can model it.
9909 if (Ptr.getOpcode() != ISD::ADD ||
9912 return Info;
9913
9914 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9916 DAG.getMachineFunction(), FI,
9917 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9918}
9919
9920/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9921/// MachinePointerInfo record from it. This is particularly useful because the
9922/// code generator has many cases where it doesn't bother passing in a
9923/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9925 SelectionDAG &DAG, SDValue Ptr,
9926 SDValue OffsetOp) {
9927 // If the 'Offset' value isn't a constant, we can't handle this.
9929 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9930 if (OffsetOp.isUndef())
9931 return InferPointerInfo(Info, DAG, Ptr);
9932 return Info;
9933}
9934
9936 EVT VT, const SDLoc &dl, SDValue Chain,
9937 SDValue Ptr, SDValue Offset,
9938 MachinePointerInfo PtrInfo, EVT MemVT,
9939 Align Alignment,
9940 MachineMemOperand::Flags MMOFlags,
9941 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9942 assert(Chain.getValueType() == MVT::Other &&
9943 "Invalid chain type");
9944
9945 MMOFlags |= MachineMemOperand::MOLoad;
9946 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9947 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9948 // clients.
9949 if (PtrInfo.V.isNull())
9950 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9951
9952 TypeSize Size = MemVT.getStoreSize();
9954 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9955 Alignment, AAInfo, Ranges);
9956 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9957}
9958
9960 EVT VT, const SDLoc &dl, SDValue Chain,
9961 SDValue Ptr, SDValue Offset, EVT MemVT,
9962 MachineMemOperand *MMO) {
9963 if (VT == MemVT) {
9964 ExtType = ISD::NON_EXTLOAD;
9965 } else if (ExtType == ISD::NON_EXTLOAD) {
9966 assert(VT == MemVT && "Non-extending load from different memory type!");
9967 } else {
9968 // Extending load.
9969 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9970 "Should only be an extending load, not truncating!");
9971 assert(VT.isInteger() == MemVT.isInteger() &&
9972 "Cannot convert from FP to Int or Int -> FP!");
9973 assert(VT.isVector() == MemVT.isVector() &&
9974 "Cannot use an ext load to convert to or from a vector!");
9975 assert((!VT.isVector() ||
9977 "Cannot use an ext load to change the number of vector elements!");
9978 }
9979
9980 assert((!MMO->getRanges() ||
9982 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9983 MemVT.isInteger())) &&
9984 "Range metadata and load type must match!");
9985
9986 bool Indexed = AM != ISD::UNINDEXED;
9987 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9988
9989 SDVTList VTs = Indexed ?
9990 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9991 SDValue Ops[] = { Chain, Ptr, Offset };
9993 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9994 ID.AddInteger(MemVT.getRawBits());
9995 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9996 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9997 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9998 ID.AddInteger(MMO->getFlags());
9999 void *IP = nullptr;
10000 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10001 E->refineAlignment(MMO);
10002 E->refineRanges(MMO);
10003 return SDValue(E, 0);
10004 }
10005 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10006 ExtType, MemVT, MMO);
10007 createOperands(N, Ops);
10008
10009 CSEMap.InsertNode(N, IP);
10010 InsertNode(N);
10011 SDValue V(N, 0);
10012 NewSDValueDbgMsg(V, "Creating new node: ", this);
10013 return V;
10014}
10015
10017 SDValue Ptr, MachinePointerInfo PtrInfo,
10018 MaybeAlign Alignment,
10019 MachineMemOperand::Flags MMOFlags,
10020 const AAMDNodes &AAInfo, const MDNode *Ranges) {
10022 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10023 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
10024}
10025
10027 SDValue Ptr, MachineMemOperand *MMO) {
10029 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10030 VT, MMO);
10031}
10032
10034 EVT VT, SDValue Chain, SDValue Ptr,
10035 MachinePointerInfo PtrInfo, EVT MemVT,
10036 MaybeAlign Alignment,
10037 MachineMemOperand::Flags MMOFlags,
10038 const AAMDNodes &AAInfo) {
10040 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
10041 MemVT, Alignment, MMOFlags, AAInfo);
10042}
10043
10045 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
10046 MachineMemOperand *MMO) {
10048 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
10049 MemVT, MMO);
10050}
10051
10055 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
10056 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10057 // Don't propagate the invariant or dereferenceable flags.
10058 auto MMOFlags =
10059 LD->getMemOperand()->getFlags() &
10061 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10062 LD->getChain(), Base, Offset, LD->getPointerInfo(),
10063 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
10064}
10065
10067 SDValue Ptr, MachinePointerInfo PtrInfo,
10068 Align Alignment,
10069 MachineMemOperand::Flags MMOFlags,
10070 const AAMDNodes &AAInfo) {
10071 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10072
10073 MMOFlags |= MachineMemOperand::MOStore;
10074 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10075
10076 if (PtrInfo.V.isNull())
10077 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10078
10081 MachineMemOperand *MMO =
10082 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
10083 return getStore(Chain, dl, Val, Ptr, MMO);
10084}
10085
10087 SDValue Ptr, MachineMemOperand *MMO) {
10089 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
10091}
10092
10094 SDValue Ptr, SDValue Offset, EVT SVT,
10096 bool IsTruncating) {
10097 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10098 EVT VT = Val.getValueType();
10099 if (VT == SVT) {
10100 IsTruncating = false;
10101 } else if (!IsTruncating) {
10102 assert(VT == SVT && "No-truncating store from different memory type!");
10103 } else {
10105 "Should only be a truncating store, not extending!");
10106 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10107 assert(VT.isVector() == SVT.isVector() &&
10108 "Cannot use trunc store to convert to or from a vector!");
10109 assert((!VT.isVector() ||
10111 "Cannot use trunc store to change the number of vector elements!");
10112 }
10113
10114 bool Indexed = AM != ISD::UNINDEXED;
10115 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
10116 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10117 : getVTList(MVT::Other);
10118 SDValue Ops[] = {Chain, Val, Ptr, Offset};
10121 ID.AddInteger(SVT.getRawBits());
10122 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
10123 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
10124 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10125 ID.AddInteger(MMO->getFlags());
10126 void *IP = nullptr;
10127 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10128 cast<StoreSDNode>(E)->refineAlignment(MMO);
10129 return SDValue(E, 0);
10130 }
10131 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10132 IsTruncating, SVT, MMO);
10133 createOperands(N, Ops);
10134
10135 CSEMap.InsertNode(N, IP);
10136 InsertNode(N);
10137 SDValue V(N, 0);
10138 NewSDValueDbgMsg(V, "Creating new node: ", this);
10139 return V;
10140}
10141
10143 SDValue Ptr, MachinePointerInfo PtrInfo,
10144 EVT SVT, Align Alignment,
10145 MachineMemOperand::Flags MMOFlags,
10146 const AAMDNodes &AAInfo) {
10147 assert(Chain.getValueType() == MVT::Other &&
10148 "Invalid chain type");
10149
10150 MMOFlags |= MachineMemOperand::MOStore;
10151 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10152
10153 if (PtrInfo.V.isNull())
10154 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10155
10157 MachineMemOperand *MMO = MF.getMachineMemOperand(
10158 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10159 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10160}
10161
10163 SDValue Ptr, EVT SVT,
10164 MachineMemOperand *MMO) {
10166 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10167}
10168
10172 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10173 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10174 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10175 ST->getMemoryVT(), ST->getMemOperand(), AM,
10176 ST->isTruncatingStore());
10177}
10178
10180 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10181 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10182 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10183 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10184 const MDNode *Ranges, bool IsExpanding) {
10185 MMOFlags |= MachineMemOperand::MOLoad;
10186 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10187 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10188 // clients.
10189 if (PtrInfo.V.isNull())
10190 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10191
10192 TypeSize Size = MemVT.getStoreSize();
10194 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10195 Alignment, AAInfo, Ranges);
10196 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10197 MMO, IsExpanding);
10198}
10199
10201 ISD::LoadExtType ExtType, EVT VT,
10202 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10203 SDValue Offset, SDValue Mask, SDValue EVL,
10204 EVT MemVT, MachineMemOperand *MMO,
10205 bool IsExpanding) {
10206 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10207 assert(Mask.getValueType().getVectorElementCount() ==
10208 VT.getVectorElementCount() &&
10209 "Vector width mismatch between mask and data");
10210
10211 bool Indexed = AM != ISD::UNINDEXED;
10212 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10213
10214 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10215 : getVTList(VT, MVT::Other);
10216 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10218 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10219 ID.AddInteger(MemVT.getRawBits());
10220 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10221 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10222 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10223 ID.AddInteger(MMO->getFlags());
10224 void *IP = nullptr;
10225 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10226 E->refineAlignment(MMO);
10227 E->refineRanges(MMO);
10228 return SDValue(E, 0);
10229 }
10230 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10231 ExtType, IsExpanding, MemVT, MMO);
10232 createOperands(N, Ops);
10233
10234 CSEMap.InsertNode(N, IP);
10235 InsertNode(N);
10236 SDValue V(N, 0);
10237 NewSDValueDbgMsg(V, "Creating new node: ", this);
10238 return V;
10239}
10240
10242 SDValue Ptr, SDValue Mask, SDValue EVL,
10243 MachinePointerInfo PtrInfo,
10244 MaybeAlign Alignment,
10245 MachineMemOperand::Flags MMOFlags,
10246 const AAMDNodes &AAInfo, const MDNode *Ranges,
10247 bool IsExpanding) {
10249 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10250 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10251 IsExpanding);
10252}
10253
10255 SDValue Ptr, SDValue Mask, SDValue EVL,
10256 MachineMemOperand *MMO, bool IsExpanding) {
10258 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10259 Mask, EVL, VT, MMO, IsExpanding);
10260}
10261
10263 EVT VT, SDValue Chain, SDValue Ptr,
10264 SDValue Mask, SDValue EVL,
10265 MachinePointerInfo PtrInfo, EVT MemVT,
10266 MaybeAlign Alignment,
10267 MachineMemOperand::Flags MMOFlags,
10268 const AAMDNodes &AAInfo, bool IsExpanding) {
10270 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10271 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10272 IsExpanding);
10273}
10274
10276 EVT VT, SDValue Chain, SDValue Ptr,
10277 SDValue Mask, SDValue EVL, EVT MemVT,
10278 MachineMemOperand *MMO, bool IsExpanding) {
10280 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10281 EVL, MemVT, MMO, IsExpanding);
10282}
10283
10287 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10288 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10289 // Don't propagate the invariant or dereferenceable flags.
10290 auto MMOFlags =
10291 LD->getMemOperand()->getFlags() &
10293 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10294 LD->getChain(), Base, Offset, LD->getMask(),
10295 LD->getVectorLength(), LD->getPointerInfo(),
10296 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10297 nullptr, LD->isExpandingLoad());
10298}
10299
10301 SDValue Ptr, SDValue Offset, SDValue Mask,
10302 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10303 ISD::MemIndexedMode AM, bool IsTruncating,
10304 bool IsCompressing) {
10305 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10306 assert(Mask.getValueType().getVectorElementCount() ==
10308 "Vector width mismatch between mask and data");
10309
10310 bool Indexed = AM != ISD::UNINDEXED;
10311 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10312 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10313 : getVTList(MVT::Other);
10314 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10316 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10317 ID.AddInteger(MemVT.getRawBits());
10318 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10319 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10320 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10321 ID.AddInteger(MMO->getFlags());
10322 void *IP = nullptr;
10323 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10324 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10325 return SDValue(E, 0);
10326 }
10327 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10328 IsTruncating, IsCompressing, MemVT, MMO);
10329 createOperands(N, Ops);
10330
10331 CSEMap.InsertNode(N, IP);
10332 InsertNode(N);
10333 SDValue V(N, 0);
10334 NewSDValueDbgMsg(V, "Creating new node: ", this);
10335 return V;
10336}
10337
10339 SDValue Val, SDValue Ptr, SDValue Mask,
10340 SDValue EVL, MachinePointerInfo PtrInfo,
10341 EVT SVT, Align Alignment,
10342 MachineMemOperand::Flags MMOFlags,
10343 const AAMDNodes &AAInfo,
10344 bool IsCompressing) {
10345 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10346
10347 MMOFlags |= MachineMemOperand::MOStore;
10348 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10349
10350 if (PtrInfo.V.isNull())
10351 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10352
10354 MachineMemOperand *MMO = MF.getMachineMemOperand(
10355 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10356 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10357 IsCompressing);
10358}
10359
10361 SDValue Val, SDValue Ptr, SDValue Mask,
10362 SDValue EVL, EVT SVT,
10363 MachineMemOperand *MMO,
10364 bool IsCompressing) {
10365 EVT VT = Val.getValueType();
10366
10367 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10368 if (VT == SVT)
10369 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10370 EVL, VT, MMO, ISD::UNINDEXED,
10371 /*IsTruncating*/ false, IsCompressing);
10372
10374 "Should only be a truncating store, not extending!");
10375 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10376 assert(VT.isVector() == SVT.isVector() &&
10377 "Cannot use trunc store to convert to or from a vector!");
10378 assert((!VT.isVector() ||
10380 "Cannot use trunc store to change the number of vector elements!");
10381
10382 SDVTList VTs = getVTList(MVT::Other);
10384 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10386 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10387 ID.AddInteger(SVT.getRawBits());
10388 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10389 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10390 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10391 ID.AddInteger(MMO->getFlags());
10392 void *IP = nullptr;
10393 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10394 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10395 return SDValue(E, 0);
10396 }
10397 auto *N =
10398 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10399 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10400 createOperands(N, Ops);
10401
10402 CSEMap.InsertNode(N, IP);
10403 InsertNode(N);
10404 SDValue V(N, 0);
10405 NewSDValueDbgMsg(V, "Creating new node: ", this);
10406 return V;
10407}
10408
10412 auto *ST = cast<VPStoreSDNode>(OrigStore);
10413 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10414 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10415 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10416 Offset, ST->getMask(), ST->getVectorLength()};
10418 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10419 ID.AddInteger(ST->getMemoryVT().getRawBits());
10420 ID.AddInteger(ST->getRawSubclassData());
10421 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10422 ID.AddInteger(ST->getMemOperand()->getFlags());
10423 void *IP = nullptr;
10424 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10425 return SDValue(E, 0);
10426
10427 auto *N = newSDNode<VPStoreSDNode>(
10428 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10429 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10430 createOperands(N, Ops);
10431
10432 CSEMap.InsertNode(N, IP);
10433 InsertNode(N);
10434 SDValue V(N, 0);
10435 NewSDValueDbgMsg(V, "Creating new node: ", this);
10436 return V;
10437}
10438
10440 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10441 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10442 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10443 bool Indexed = AM != ISD::UNINDEXED;
10444 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10445
10446 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10447 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10448 : getVTList(VT, MVT::Other);
10450 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10451 ID.AddInteger(VT.getRawBits());
10452 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10453 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10454 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10455
10456 void *IP = nullptr;
10457 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10458 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10459 return SDValue(E, 0);
10460 }
10461
10462 auto *N =
10463 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10464 ExtType, IsExpanding, MemVT, MMO);
10465 createOperands(N, Ops);
10466 CSEMap.InsertNode(N, IP);
10467 InsertNode(N);
10468 SDValue V(N, 0);
10469 NewSDValueDbgMsg(V, "Creating new node: ", this);
10470 return V;
10471}
10472
10474 SDValue Ptr, SDValue Stride,
10475 SDValue Mask, SDValue EVL,
10476 MachineMemOperand *MMO,
10477 bool IsExpanding) {
10479 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10480 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10481}
10482
10484 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10485 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10486 MachineMemOperand *MMO, bool IsExpanding) {
10488 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10489 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10490}
10491
10493 SDValue Val, SDValue Ptr,
10494 SDValue Offset, SDValue Stride,
10495 SDValue Mask, SDValue EVL, EVT MemVT,
10496 MachineMemOperand *MMO,
10498 bool IsTruncating, bool IsCompressing) {
10499 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10500 bool Indexed = AM != ISD::UNINDEXED;
10501 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10502 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10503 : getVTList(MVT::Other);
10504 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10506 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10507 ID.AddInteger(MemVT.getRawBits());
10508 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10509 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10510 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10511 void *IP = nullptr;
10512 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10513 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10514 return SDValue(E, 0);
10515 }
10516 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10517 VTs, AM, IsTruncating,
10518 IsCompressing, MemVT, MMO);
10519 createOperands(N, Ops);
10520
10521 CSEMap.InsertNode(N, IP);
10522 InsertNode(N);
10523 SDValue V(N, 0);
10524 NewSDValueDbgMsg(V, "Creating new node: ", this);
10525 return V;
10526}
10527
10529 SDValue Val, SDValue Ptr,
10530 SDValue Stride, SDValue Mask,
10531 SDValue EVL, EVT SVT,
10532 MachineMemOperand *MMO,
10533 bool IsCompressing) {
10534 EVT VT = Val.getValueType();
10535
10536 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10537 if (VT == SVT)
10538 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10539 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10540 /*IsTruncating*/ false, IsCompressing);
10541
10543 "Should only be a truncating store, not extending!");
10544 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10545 assert(VT.isVector() == SVT.isVector() &&
10546 "Cannot use trunc store to convert to or from a vector!");
10547 assert((!VT.isVector() ||
10549 "Cannot use trunc store to change the number of vector elements!");
10550
10551 SDVTList VTs = getVTList(MVT::Other);
10553 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10555 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10556 ID.AddInteger(SVT.getRawBits());
10557 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10558 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10559 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10560 void *IP = nullptr;
10561 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10562 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10563 return SDValue(E, 0);
10564 }
10565 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10566 VTs, ISD::UNINDEXED, true,
10567 IsCompressing, SVT, MMO);
10568 createOperands(N, Ops);
10569
10570 CSEMap.InsertNode(N, IP);
10571 InsertNode(N);
10572 SDValue V(N, 0);
10573 NewSDValueDbgMsg(V, "Creating new node: ", this);
10574 return V;
10575}
10576
10579 ISD::MemIndexType IndexType) {
10580 assert(Ops.size() == 6 && "Incompatible number of operands");
10581
10583 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10584 ID.AddInteger(VT.getRawBits());
10585 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10586 dl.getIROrder(), VTs, VT, MMO, IndexType));
10587 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10588 ID.AddInteger(MMO->getFlags());
10589 void *IP = nullptr;
10590 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10591 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10592 return SDValue(E, 0);
10593 }
10594
10595 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10596 VT, MMO, IndexType);
10597 createOperands(N, Ops);
10598
10599 assert(N->getMask().getValueType().getVectorElementCount() ==
10600 N->getValueType(0).getVectorElementCount() &&
10601 "Vector width mismatch between mask and data");
10602 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10603 N->getValueType(0).getVectorElementCount().isScalable() &&
10604 "Scalable flags of index and data do not match");
10606 N->getIndex().getValueType().getVectorElementCount(),
10607 N->getValueType(0).getVectorElementCount()) &&
10608 "Vector width mismatch between index and data");
10609 assert(isa<ConstantSDNode>(N->getScale()) &&
10610 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10611 "Scale should be a constant power of 2");
10612
10613 CSEMap.InsertNode(N, IP);
10614 InsertNode(N);
10615 SDValue V(N, 0);
10616 NewSDValueDbgMsg(V, "Creating new node: ", this);
10617 return V;
10618}
10619
10622 MachineMemOperand *MMO,
10623 ISD::MemIndexType IndexType) {
10624 assert(Ops.size() == 7 && "Incompatible number of operands");
10625
10627 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10628 ID.AddInteger(VT.getRawBits());
10629 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10630 dl.getIROrder(), VTs, VT, MMO, IndexType));
10631 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10632 ID.AddInteger(MMO->getFlags());
10633 void *IP = nullptr;
10634 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10635 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10636 return SDValue(E, 0);
10637 }
10638 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10639 VT, MMO, IndexType);
10640 createOperands(N, Ops);
10641
10642 assert(N->getMask().getValueType().getVectorElementCount() ==
10643 N->getValue().getValueType().getVectorElementCount() &&
10644 "Vector width mismatch between mask and data");
10645 assert(
10646 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10647 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10648 "Scalable flags of index and data do not match");
10650 N->getIndex().getValueType().getVectorElementCount(),
10651 N->getValue().getValueType().getVectorElementCount()) &&
10652 "Vector width mismatch between index and data");
10653 assert(isa<ConstantSDNode>(N->getScale()) &&
10654 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10655 "Scale should be a constant power of 2");
10656
10657 CSEMap.InsertNode(N, IP);
10658 InsertNode(N);
10659 SDValue V(N, 0);
10660 NewSDValueDbgMsg(V, "Creating new node: ", this);
10661 return V;
10662}
10663
10666 SDValue PassThru, EVT MemVT,
10667 MachineMemOperand *MMO,
10669 ISD::LoadExtType ExtTy, bool isExpanding) {
10670 bool Indexed = AM != ISD::UNINDEXED;
10671 assert((Indexed || Offset.isUndef()) &&
10672 "Unindexed masked load with an offset!");
10673 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10674 : getVTList(VT, MVT::Other);
10675 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10678 ID.AddInteger(MemVT.getRawBits());
10679 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10680 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10681 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10682 ID.AddInteger(MMO->getFlags());
10683 void *IP = nullptr;
10684 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10685 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10686 return SDValue(E, 0);
10687 }
10688 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10689 AM, ExtTy, isExpanding, MemVT, MMO);
10690 createOperands(N, Ops);
10691
10692 CSEMap.InsertNode(N, IP);
10693 InsertNode(N);
10694 SDValue V(N, 0);
10695 NewSDValueDbgMsg(V, "Creating new node: ", this);
10696 return V;
10697}
10698
10703 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10704 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10705 Offset, LD->getMask(), LD->getPassThru(),
10706 LD->getMemoryVT(), LD->getMemOperand(), AM,
10707 LD->getExtensionType(), LD->isExpandingLoad());
10708}
10709
10712 SDValue Mask, EVT MemVT,
10713 MachineMemOperand *MMO,
10714 ISD::MemIndexedMode AM, bool IsTruncating,
10715 bool IsCompressing) {
10716 assert(Chain.getValueType() == MVT::Other &&
10717 "Invalid chain type");
10718 bool Indexed = AM != ISD::UNINDEXED;
10719 assert((Indexed || Offset.isUndef()) &&
10720 "Unindexed masked store with an offset!");
10721 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10722 : getVTList(MVT::Other);
10723 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10726 ID.AddInteger(MemVT.getRawBits());
10727 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10728 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10729 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10730 ID.AddInteger(MMO->getFlags());
10731 void *IP = nullptr;
10732 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10733 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10734 return SDValue(E, 0);
10735 }
10736 auto *N =
10737 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10738 IsTruncating, IsCompressing, MemVT, MMO);
10739 createOperands(N, Ops);
10740
10741 CSEMap.InsertNode(N, IP);
10742 InsertNode(N);
10743 SDValue V(N, 0);
10744 NewSDValueDbgMsg(V, "Creating new node: ", this);
10745 return V;
10746}
10747
10752 assert(ST->getOffset().isUndef() &&
10753 "Masked store is already a indexed store!");
10754 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10755 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10756 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10757}
10758
10761 MachineMemOperand *MMO,
10762 ISD::MemIndexType IndexType,
10763 ISD::LoadExtType ExtTy) {
10764 assert(Ops.size() == 6 && "Incompatible number of operands");
10765
10768 ID.AddInteger(MemVT.getRawBits());
10769 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10770 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10771 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10772 ID.AddInteger(MMO->getFlags());
10773 void *IP = nullptr;
10774 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10775 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10776 return SDValue(E, 0);
10777 }
10778
10779 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10780 VTs, MemVT, MMO, IndexType, ExtTy);
10781 createOperands(N, Ops);
10782
10783 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10784 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10785 assert(N->getMask().getValueType().getVectorElementCount() ==
10786 N->getValueType(0).getVectorElementCount() &&
10787 "Vector width mismatch between mask and data");
10788 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10789 N->getValueType(0).getVectorElementCount().isScalable() &&
10790 "Scalable flags of index and data do not match");
10792 N->getIndex().getValueType().getVectorElementCount(),
10793 N->getValueType(0).getVectorElementCount()) &&
10794 "Vector width mismatch between index and data");
10795 assert(isa<ConstantSDNode>(N->getScale()) &&
10796 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10797 "Scale should be a constant power of 2");
10798
10799 CSEMap.InsertNode(N, IP);
10800 InsertNode(N);
10801 SDValue V(N, 0);
10802 NewSDValueDbgMsg(V, "Creating new node: ", this);
10803 return V;
10804}
10805
10808 MachineMemOperand *MMO,
10809 ISD::MemIndexType IndexType,
10810 bool IsTrunc) {
10811 assert(Ops.size() == 6 && "Incompatible number of operands");
10812
10815 ID.AddInteger(MemVT.getRawBits());
10816 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10817 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10818 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10819 ID.AddInteger(MMO->getFlags());
10820 void *IP = nullptr;
10821 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10822 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10823 return SDValue(E, 0);
10824 }
10825
10826 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10827 VTs, MemVT, MMO, IndexType, IsTrunc);
10828 createOperands(N, Ops);
10829
10830 assert(N->getMask().getValueType().getVectorElementCount() ==
10831 N->getValue().getValueType().getVectorElementCount() &&
10832 "Vector width mismatch between mask and data");
10833 assert(
10834 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10835 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10836 "Scalable flags of index and data do not match");
10838 N->getIndex().getValueType().getVectorElementCount(),
10839 N->getValue().getValueType().getVectorElementCount()) &&
10840 "Vector width mismatch between index and data");
10841 assert(isa<ConstantSDNode>(N->getScale()) &&
10842 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10843 "Scale should be a constant power of 2");
10844
10845 CSEMap.InsertNode(N, IP);
10846 InsertNode(N);
10847 SDValue V(N, 0);
10848 NewSDValueDbgMsg(V, "Creating new node: ", this);
10849 return V;
10850}
10851
10853 const SDLoc &dl, ArrayRef<SDValue> Ops,
10854 MachineMemOperand *MMO,
10855 ISD::MemIndexType IndexType) {
10856 assert(Ops.size() == 7 && "Incompatible number of operands");
10857
10860 ID.AddInteger(MemVT.getRawBits());
10861 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10862 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10863 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10864 ID.AddInteger(MMO->getFlags());
10865 void *IP = nullptr;
10866 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10867 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10868 return SDValue(E, 0);
10869 }
10870
10871 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10872 VTs, MemVT, MMO, IndexType);
10873 createOperands(N, Ops);
10874
10875 assert(N->getMask().getValueType().getVectorElementCount() ==
10876 N->getIndex().getValueType().getVectorElementCount() &&
10877 "Vector width mismatch between mask and data");
10878 assert(isa<ConstantSDNode>(N->getScale()) &&
10879 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10880 "Scale should be a constant power of 2");
10881 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10882
10883 CSEMap.InsertNode(N, IP);
10884 InsertNode(N);
10885 SDValue V(N, 0);
10886 NewSDValueDbgMsg(V, "Creating new node: ", this);
10887 return V;
10888}
10889
10891 SDValue Ptr, SDValue Mask, SDValue EVL,
10892 MachineMemOperand *MMO) {
10893 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10894 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10896 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10897 ID.AddInteger(VT.getRawBits());
10898 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10899 VTs, VT, MMO));
10900 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10901 ID.AddInteger(MMO->getFlags());
10902 void *IP = nullptr;
10903 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10904 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10905 return SDValue(E, 0);
10906 }
10907 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10908 VT, MMO);
10909 createOperands(N, Ops);
10910
10911 CSEMap.InsertNode(N, IP);
10912 InsertNode(N);
10913 SDValue V(N, 0);
10914 NewSDValueDbgMsg(V, "Creating new node: ", this);
10915 return V;
10916}
10917
10919 EVT MemVT, MachineMemOperand *MMO) {
10920 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10921 SDVTList VTs = getVTList(MVT::Other);
10922 SDValue Ops[] = {Chain, Ptr};
10925 ID.AddInteger(MemVT.getRawBits());
10926 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10927 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10928 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10929 ID.AddInteger(MMO->getFlags());
10930 void *IP = nullptr;
10931 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10932 return SDValue(E, 0);
10933
10934 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10935 dl.getDebugLoc(), VTs, MemVT, MMO);
10936 createOperands(N, Ops);
10937
10938 CSEMap.InsertNode(N, IP);
10939 InsertNode(N);
10940 SDValue V(N, 0);
10941 NewSDValueDbgMsg(V, "Creating new node: ", this);
10942 return V;
10943}
10944
10946 EVT MemVT, MachineMemOperand *MMO) {
10947 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10948 SDVTList VTs = getVTList(MVT::Other);
10949 SDValue Ops[] = {Chain, Ptr};
10952 ID.AddInteger(MemVT.getRawBits());
10953 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10954 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10955 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10956 ID.AddInteger(MMO->getFlags());
10957 void *IP = nullptr;
10958 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10959 return SDValue(E, 0);
10960
10961 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10962 dl.getDebugLoc(), VTs, MemVT, MMO);
10963 createOperands(N, Ops);
10964
10965 CSEMap.InsertNode(N, IP);
10966 InsertNode(N);
10967 SDValue V(N, 0);
10968 NewSDValueDbgMsg(V, "Creating new node: ", this);
10969 return V;
10970}
10971
10973 // select undef, T, F --> T (if T is a constant), otherwise F
10974 // select, ?, undef, F --> F
10975 // select, ?, T, undef --> T
10976 if (Cond.isUndef())
10977 return isConstantValueOfAnyType(T) ? T : F;
10978 if (T.isUndef())
10980 if (F.isUndef())
10982
10983 // select true, T, F --> T
10984 // select false, T, F --> F
10985 if (auto C = isBoolConstant(Cond))
10986 return *C ? T : F;
10987
10988 // select ?, T, T --> T
10989 if (T == F)
10990 return T;
10991
10992 return SDValue();
10993}
10994
10996 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10997 if (X.isUndef())
10998 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10999 // shift X, undef --> undef (because it may shift by the bitwidth)
11000 if (Y.isUndef())
11001 return getUNDEF(X.getValueType());
11002
11003 // shift 0, Y --> 0
11004 // shift X, 0 --> X
11006 return X;
11007
11008 // shift X, C >= bitwidth(X) --> undef
11009 // All vector elements must be too big (or undef) to avoid partial undefs.
11010 auto isShiftTooBig = [X](ConstantSDNode *Val) {
11011 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
11012 };
11013 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
11014 return getUNDEF(X.getValueType());
11015
11016 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
11017 if (X.getValueType().getScalarType() == MVT::i1)
11018 return X;
11019
11020 return SDValue();
11021}
11022
11024 SDNodeFlags Flags) {
11025 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
11026 // (an undef operand can be chosen to be Nan/Inf), then the result of this
11027 // operation is poison. That result can be relaxed to undef.
11028 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
11029 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
11030 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
11031 (YC && YC->getValueAPF().isNaN());
11032 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
11033 (YC && YC->getValueAPF().isInfinity());
11034
11035 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
11036 return getUNDEF(X.getValueType());
11037
11038 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
11039 return getUNDEF(X.getValueType());
11040
11041 if (!YC)
11042 return SDValue();
11043
11044 // X + -0.0 --> X
11045 if (Opcode == ISD::FADD)
11046 if (YC->getValueAPF().isNegZero())
11047 return X;
11048
11049 // X - +0.0 --> X
11050 if (Opcode == ISD::FSUB)
11051 if (YC->getValueAPF().isPosZero())
11052 return X;
11053
11054 // X * 1.0 --> X
11055 // X / 1.0 --> X
11056 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
11057 if (YC->getValueAPF().isExactlyValue(1.0))
11058 return X;
11059
11060 // X * 0.0 --> 0.0
11061 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
11062 if (YC->getValueAPF().isZero())
11063 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
11064
11065 return SDValue();
11066}
11067
11069 SDValue Ptr, SDValue SV, unsigned Align) {
11070 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
11071 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
11072}
11073
11074SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11076 switch (Ops.size()) {
11077 case 0: return getNode(Opcode, DL, VT);
11078 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
11079 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
11080 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
11081 default: break;
11082 }
11083
11084 // Copy from an SDUse array into an SDValue array for use with
11085 // the regular getNode logic.
11087 return getNode(Opcode, DL, VT, NewOps);
11088}
11089
11090SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11092 SDNodeFlags Flags;
11093 if (Inserter)
11094 Flags = Inserter->getFlags();
11095 return getNode(Opcode, DL, VT, Ops, Flags);
11096}
11097
11098SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11099 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11100 unsigned NumOps = Ops.size();
11101 switch (NumOps) {
11102 case 0: return getNode(Opcode, DL, VT);
11103 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
11104 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
11105 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
11106 default: break;
11107 }
11108
11109#ifndef NDEBUG
11110 for (const auto &Op : Ops)
11111 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11112 "Operand is DELETED_NODE!");
11113#endif
11114
11115 switch (Opcode) {
11116 default: break;
11117 case ISD::BUILD_VECTOR:
11118 // Attempt to simplify BUILD_VECTOR.
11119 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
11120 return V;
11121 break;
11123 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
11124 return V;
11125 break;
11126 case ISD::SELECT_CC:
11127 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
11128 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
11129 "LHS and RHS of condition must have same type!");
11130 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11131 "True and False arms of SelectCC must have same type!");
11132 assert(Ops[2].getValueType() == VT &&
11133 "select_cc node must be of same type as true and false value!");
11134 assert((!Ops[0].getValueType().isVector() ||
11135 Ops[0].getValueType().getVectorElementCount() ==
11136 VT.getVectorElementCount()) &&
11137 "Expected select_cc with vector result to have the same sized "
11138 "comparison type!");
11139 break;
11140 case ISD::BR_CC:
11141 assert(NumOps == 5 && "BR_CC takes 5 operands!");
11142 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11143 "LHS/RHS of comparison should match types!");
11144 break;
11145 case ISD::VP_ADD:
11146 case ISD::VP_SUB:
11147 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
11148 if (VT.getScalarType() == MVT::i1)
11149 Opcode = ISD::VP_XOR;
11150 break;
11151 case ISD::VP_MUL:
11152 // If it is VP_MUL mask operation then turn it to VP_AND
11153 if (VT.getScalarType() == MVT::i1)
11154 Opcode = ISD::VP_AND;
11155 break;
11156 case ISD::VP_REDUCE_MUL:
11157 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11158 if (VT == MVT::i1)
11159 Opcode = ISD::VP_REDUCE_AND;
11160 break;
11161 case ISD::VP_REDUCE_ADD:
11162 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11163 if (VT == MVT::i1)
11164 Opcode = ISD::VP_REDUCE_XOR;
11165 break;
11166 case ISD::VP_REDUCE_SMAX:
11167 case ISD::VP_REDUCE_UMIN:
11168 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11169 // VP_REDUCE_AND.
11170 if (VT == MVT::i1)
11171 Opcode = ISD::VP_REDUCE_AND;
11172 break;
11173 case ISD::VP_REDUCE_SMIN:
11174 case ISD::VP_REDUCE_UMAX:
11175 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11176 // VP_REDUCE_OR.
11177 if (VT == MVT::i1)
11178 Opcode = ISD::VP_REDUCE_OR;
11179 break;
11180 }
11181
11182 // Memoize nodes.
11183 SDNode *N;
11184 SDVTList VTs = getVTList(VT);
11185
11186 if (VT != MVT::Glue) {
11188 AddNodeIDNode(ID, Opcode, VTs, Ops);
11189 void *IP = nullptr;
11190
11191 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11192 E->intersectFlagsWith(Flags);
11193 return SDValue(E, 0);
11194 }
11195
11196 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11197 createOperands(N, Ops);
11198
11199 CSEMap.InsertNode(N, IP);
11200 } else {
11201 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11202 createOperands(N, Ops);
11203 }
11204
11205 N->setFlags(Flags);
11206 InsertNode(N);
11207 SDValue V(N, 0);
11208 NewSDValueDbgMsg(V, "Creating new node: ", this);
11209 return V;
11210}
11211
11212SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11213 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11214 SDNodeFlags Flags;
11215 if (Inserter)
11216 Flags = Inserter->getFlags();
11217 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11218}
11219
11220SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11222 const SDNodeFlags Flags) {
11223 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11224}
11225
11226SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11228 SDNodeFlags Flags;
11229 if (Inserter)
11230 Flags = Inserter->getFlags();
11231 return getNode(Opcode, DL, VTList, Ops, Flags);
11232}
11233
11234SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11235 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11236 if (VTList.NumVTs == 1)
11237 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11238
11239#ifndef NDEBUG
11240 for (const auto &Op : Ops)
11241 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11242 "Operand is DELETED_NODE!");
11243#endif
11244
11245 switch (Opcode) {
11246 case ISD::SADDO:
11247 case ISD::UADDO:
11248 case ISD::SSUBO:
11249 case ISD::USUBO: {
11250 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11251 "Invalid add/sub overflow op!");
11252 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11253 Ops[0].getValueType() == Ops[1].getValueType() &&
11254 Ops[0].getValueType() == VTList.VTs[0] &&
11255 "Binary operator types must match!");
11256 SDValue N1 = Ops[0], N2 = Ops[1];
11257 canonicalizeCommutativeBinop(Opcode, N1, N2);
11258
11259 // (X +- 0) -> X with zero-overflow.
11260 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11261 /*AllowTruncation*/ true);
11262 if (N2CV && N2CV->isZero()) {
11263 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11264 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11265 }
11266
11267 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11268 VTList.VTs[1].getScalarType() == MVT::i1) {
11269 SDValue F1 = getFreeze(N1);
11270 SDValue F2 = getFreeze(N2);
11271 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11272 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11273 return getNode(ISD::MERGE_VALUES, DL, VTList,
11274 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11275 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11276 Flags);
11277 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11278 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11279 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11280 return getNode(ISD::MERGE_VALUES, DL, VTList,
11281 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11282 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11283 Flags);
11284 }
11285 }
11286 break;
11287 }
11288 case ISD::SADDO_CARRY:
11289 case ISD::UADDO_CARRY:
11290 case ISD::SSUBO_CARRY:
11291 case ISD::USUBO_CARRY:
11292 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11293 "Invalid add/sub overflow op!");
11294 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11295 Ops[0].getValueType() == Ops[1].getValueType() &&
11296 Ops[0].getValueType() == VTList.VTs[0] &&
11297 Ops[2].getValueType() == VTList.VTs[1] &&
11298 "Binary operator types must match!");
11299 break;
11300 case ISD::SMUL_LOHI:
11301 case ISD::UMUL_LOHI: {
11302 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11303 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11304 VTList.VTs[0] == Ops[0].getValueType() &&
11305 VTList.VTs[0] == Ops[1].getValueType() &&
11306 "Binary operator types must match!");
11307 // Constant fold.
11310 if (LHS && RHS) {
11311 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11312 unsigned OutWidth = Width * 2;
11313 APInt Val = LHS->getAPIntValue();
11314 APInt Mul = RHS->getAPIntValue();
11315 if (Opcode == ISD::SMUL_LOHI) {
11316 Val = Val.sext(OutWidth);
11317 Mul = Mul.sext(OutWidth);
11318 } else {
11319 Val = Val.zext(OutWidth);
11320 Mul = Mul.zext(OutWidth);
11321 }
11322 Val *= Mul;
11323
11324 SDValue Hi =
11325 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11326 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11327 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11328 }
11329 break;
11330 }
11331 case ISD::FFREXP: {
11332 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11333 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11334 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11335
11337 int FrexpExp;
11338 APFloat FrexpMant =
11339 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11340 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11341 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11342 DL, VTList.VTs[1]);
11343 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11344 }
11345
11346 break;
11347 }
11349 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11350 "Invalid STRICT_FP_EXTEND!");
11351 assert(VTList.VTs[0].isFloatingPoint() &&
11352 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11353 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11354 "STRICT_FP_EXTEND result type should be vector iff the operand "
11355 "type is vector!");
11356 assert((!VTList.VTs[0].isVector() ||
11357 VTList.VTs[0].getVectorElementCount() ==
11358 Ops[1].getValueType().getVectorElementCount()) &&
11359 "Vector element count mismatch!");
11360 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11361 "Invalid fpext node, dst <= src!");
11362 break;
11364 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11365 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11366 "STRICT_FP_ROUND result type should be vector iff the operand "
11367 "type is vector!");
11368 assert((!VTList.VTs[0].isVector() ||
11369 VTList.VTs[0].getVectorElementCount() ==
11370 Ops[1].getValueType().getVectorElementCount()) &&
11371 "Vector element count mismatch!");
11372 assert(VTList.VTs[0].isFloatingPoint() &&
11373 Ops[1].getValueType().isFloatingPoint() &&
11374 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11375 Ops[2].getOpcode() == ISD::TargetConstant &&
11376 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11377 "Invalid STRICT_FP_ROUND!");
11378 break;
11379 }
11380
11381 // Memoize the node unless it returns a glue result.
11382 SDNode *N;
11383 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11385 AddNodeIDNode(ID, Opcode, VTList, Ops);
11386 void *IP = nullptr;
11387 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11388 E->intersectFlagsWith(Flags);
11389 return SDValue(E, 0);
11390 }
11391
11392 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11393 createOperands(N, Ops);
11394 CSEMap.InsertNode(N, IP);
11395 } else {
11396 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11397 createOperands(N, Ops);
11398 }
11399
11400 N->setFlags(Flags);
11401 InsertNode(N);
11402 SDValue V(N, 0);
11403 NewSDValueDbgMsg(V, "Creating new node: ", this);
11404 return V;
11405}
11406
11407SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11408 SDVTList VTList) {
11409 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11410}
11411
11412SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11413 SDValue N1) {
11414 SDValue Ops[] = { N1 };
11415 return getNode(Opcode, DL, VTList, Ops);
11416}
11417
11418SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11419 SDValue N1, SDValue N2) {
11420 SDValue Ops[] = { N1, N2 };
11421 return getNode(Opcode, DL, VTList, Ops);
11422}
11423
11424SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11425 SDValue N1, SDValue N2, SDValue N3) {
11426 SDValue Ops[] = { N1, N2, N3 };
11427 return getNode(Opcode, DL, VTList, Ops);
11428}
11429
11430SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11431 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11432 SDValue Ops[] = { N1, N2, N3, N4 };
11433 return getNode(Opcode, DL, VTList, Ops);
11434}
11435
11436SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11437 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11438 SDValue N5) {
11439 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11440 return getNode(Opcode, DL, VTList, Ops);
11441}
11442
11444 if (!VT.isExtended())
11445 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11446
11447 return makeVTList(&(*EVTs.insert(VT).first), 1);
11448}
11449
11452 ID.AddInteger(2U);
11453 ID.AddInteger(VT1.getRawBits());
11454 ID.AddInteger(VT2.getRawBits());
11455
11456 void *IP = nullptr;
11457 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11458 if (!Result) {
11459 EVT *Array = Allocator.Allocate<EVT>(2);
11460 Array[0] = VT1;
11461 Array[1] = VT2;
11462 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11463 VTListMap.InsertNode(Result, IP);
11464 }
11465 return Result->getSDVTList();
11466}
11467
11470 ID.AddInteger(3U);
11471 ID.AddInteger(VT1.getRawBits());
11472 ID.AddInteger(VT2.getRawBits());
11473 ID.AddInteger(VT3.getRawBits());
11474
11475 void *IP = nullptr;
11476 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11477 if (!Result) {
11478 EVT *Array = Allocator.Allocate<EVT>(3);
11479 Array[0] = VT1;
11480 Array[1] = VT2;
11481 Array[2] = VT3;
11482 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11483 VTListMap.InsertNode(Result, IP);
11484 }
11485 return Result->getSDVTList();
11486}
11487
11490 ID.AddInteger(4U);
11491 ID.AddInteger(VT1.getRawBits());
11492 ID.AddInteger(VT2.getRawBits());
11493 ID.AddInteger(VT3.getRawBits());
11494 ID.AddInteger(VT4.getRawBits());
11495
11496 void *IP = nullptr;
11497 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11498 if (!Result) {
11499 EVT *Array = Allocator.Allocate<EVT>(4);
11500 Array[0] = VT1;
11501 Array[1] = VT2;
11502 Array[2] = VT3;
11503 Array[3] = VT4;
11504 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11505 VTListMap.InsertNode(Result, IP);
11506 }
11507 return Result->getSDVTList();
11508}
11509
11511 unsigned NumVTs = VTs.size();
11513 ID.AddInteger(NumVTs);
11514 for (unsigned index = 0; index < NumVTs; index++) {
11515 ID.AddInteger(VTs[index].getRawBits());
11516 }
11517
11518 void *IP = nullptr;
11519 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11520 if (!Result) {
11521 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11522 llvm::copy(VTs, Array);
11523 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11524 VTListMap.InsertNode(Result, IP);
11525 }
11526 return Result->getSDVTList();
11527}
11528
11529
11530/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11531/// specified operands. If the resultant node already exists in the DAG,
11532/// this does not modify the specified node, instead it returns the node that
11533/// already exists. If the resultant node does not exist in the DAG, the
11534/// input node is returned. As a degenerate case, if you specify the same
11535/// input operands as the node already has, the input node is returned.
11537 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11538
11539 // Check to see if there is no change.
11540 if (Op == N->getOperand(0)) return N;
11541
11542 // See if the modified node already exists.
11543 void *InsertPos = nullptr;
11544 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11545 return Existing;
11546
11547 // Nope it doesn't. Remove the node from its current place in the maps.
11548 if (InsertPos)
11549 if (!RemoveNodeFromCSEMaps(N))
11550 InsertPos = nullptr;
11551
11552 // Now we update the operands.
11553 N->OperandList[0].set(Op);
11554
11556 // If this gets put into a CSE map, add it.
11557 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11558 return N;
11559}
11560
11562 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11563
11564 // Check to see if there is no change.
11565 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11566 return N; // No operands changed, just return the input node.
11567
11568 // See if the modified node already exists.
11569 void *InsertPos = nullptr;
11570 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11571 return Existing;
11572
11573 // Nope it doesn't. Remove the node from its current place in the maps.
11574 if (InsertPos)
11575 if (!RemoveNodeFromCSEMaps(N))
11576 InsertPos = nullptr;
11577
11578 // Now we update the operands.
11579 if (N->OperandList[0] != Op1)
11580 N->OperandList[0].set(Op1);
11581 if (N->OperandList[1] != Op2)
11582 N->OperandList[1].set(Op2);
11583
11585 // If this gets put into a CSE map, add it.
11586 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11587 return N;
11588}
11589
11592 SDValue Ops[] = { Op1, Op2, Op3 };
11593 return UpdateNodeOperands(N, Ops);
11594}
11595
11598 SDValue Op3, SDValue Op4) {
11599 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11600 return UpdateNodeOperands(N, Ops);
11601}
11602
11605 SDValue Op3, SDValue Op4, SDValue Op5) {
11606 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11607 return UpdateNodeOperands(N, Ops);
11608}
11609
11612 unsigned NumOps = Ops.size();
11613 assert(N->getNumOperands() == NumOps &&
11614 "Update with wrong number of operands");
11615
11616 // If no operands changed just return the input node.
11617 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11618 return N;
11619
11620 // See if the modified node already exists.
11621 void *InsertPos = nullptr;
11622 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11623 return Existing;
11624
11625 // Nope it doesn't. Remove the node from its current place in the maps.
11626 if (InsertPos)
11627 if (!RemoveNodeFromCSEMaps(N))
11628 InsertPos = nullptr;
11629
11630 // Now we update the operands.
11631 for (unsigned i = 0; i != NumOps; ++i)
11632 if (N->OperandList[i] != Ops[i])
11633 N->OperandList[i].set(Ops[i]);
11634
11636 // If this gets put into a CSE map, add it.
11637 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11638 return N;
11639}
11640
11641/// DropOperands - Release the operands and set this node to have
11642/// zero operands.
11644 // Unlike the code in MorphNodeTo that does this, we don't need to
11645 // watch for dead nodes here.
11646 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11647 SDUse &Use = *I++;
11648 Use.set(SDValue());
11649 }
11650}
11651
11653 ArrayRef<MachineMemOperand *> NewMemRefs) {
11654 if (NewMemRefs.empty()) {
11655 N->clearMemRefs();
11656 return;
11657 }
11658
11659 // Check if we can avoid allocating by storing a single reference directly.
11660 if (NewMemRefs.size() == 1) {
11661 N->MemRefs = NewMemRefs[0];
11662 N->NumMemRefs = 1;
11663 return;
11664 }
11665
11666 MachineMemOperand **MemRefsBuffer =
11667 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11668 llvm::copy(NewMemRefs, MemRefsBuffer);
11669 N->MemRefs = MemRefsBuffer;
11670 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11671}
11672
11673/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11674/// machine opcode.
11675///
11677 EVT VT) {
11678 SDVTList VTs = getVTList(VT);
11679 return SelectNodeTo(N, MachineOpc, VTs, {});
11680}
11681
11683 EVT VT, SDValue Op1) {
11684 SDVTList VTs = getVTList(VT);
11685 SDValue Ops[] = { Op1 };
11686 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11687}
11688
11690 EVT VT, SDValue Op1,
11691 SDValue Op2) {
11692 SDVTList VTs = getVTList(VT);
11693 SDValue Ops[] = { Op1, Op2 };
11694 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11695}
11696
11698 EVT VT, SDValue Op1,
11699 SDValue Op2, SDValue Op3) {
11700 SDVTList VTs = getVTList(VT);
11701 SDValue Ops[] = { Op1, Op2, Op3 };
11702 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11703}
11704
11707 SDVTList VTs = getVTList(VT);
11708 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11709}
11710
11712 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11713 SDVTList VTs = getVTList(VT1, VT2);
11714 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11715}
11716
11718 EVT VT1, EVT VT2) {
11719 SDVTList VTs = getVTList(VT1, VT2);
11720 return SelectNodeTo(N, MachineOpc, VTs, {});
11721}
11722
11724 EVT VT1, EVT VT2, EVT VT3,
11726 SDVTList VTs = getVTList(VT1, VT2, VT3);
11727 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11728}
11729
11731 EVT VT1, EVT VT2,
11732 SDValue Op1, SDValue Op2) {
11733 SDVTList VTs = getVTList(VT1, VT2);
11734 SDValue Ops[] = { Op1, Op2 };
11735 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11736}
11737
11740 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11741 // Reset the NodeID to -1.
11742 New->setNodeId(-1);
11743 if (New != N) {
11744 ReplaceAllUsesWith(N, New);
11746 }
11747 return New;
11748}
11749
11750/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11751/// the line number information on the merged node since it is not possible to
11752/// preserve the information that operation is associated with multiple lines.
11753/// This will make the debugger working better at -O0, were there is a higher
11754/// probability having other instructions associated with that line.
11755///
11756/// For IROrder, we keep the smaller of the two
11757SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11758 DebugLoc NLoc = N->getDebugLoc();
11759 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11760 N->setDebugLoc(DebugLoc());
11761 }
11762 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11763 N->setIROrder(Order);
11764 return N;
11765}
11766
11767/// MorphNodeTo - This *mutates* the specified node to have the specified
11768/// return type, opcode, and operands.
11769///
11770/// Note that MorphNodeTo returns the resultant node. If there is already a
11771/// node of the specified opcode and operands, it returns that node instead of
11772/// the current one. Note that the SDLoc need not be the same.
11773///
11774/// Using MorphNodeTo is faster than creating a new node and swapping it in
11775/// with ReplaceAllUsesWith both because it often avoids allocating a new
11776/// node, and because it doesn't require CSE recalculation for any of
11777/// the node's users.
11778///
11779/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11780/// As a consequence it isn't appropriate to use from within the DAG combiner or
11781/// the legalizer which maintain worklists that would need to be updated when
11782/// deleting things.
11785 // If an identical node already exists, use it.
11786 void *IP = nullptr;
11787 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11789 AddNodeIDNode(ID, Opc, VTs, Ops);
11790 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11791 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11792 }
11793
11794 if (!RemoveNodeFromCSEMaps(N))
11795 IP = nullptr;
11796
11797 // Start the morphing.
11798 N->NodeType = Opc;
11799 N->ValueList = VTs.VTs;
11800 N->NumValues = VTs.NumVTs;
11801
11802 // Clear the operands list, updating used nodes to remove this from their
11803 // use list. Keep track of any operands that become dead as a result.
11804 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11805 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11806 SDUse &Use = *I++;
11807 SDNode *Used = Use.getNode();
11808 Use.set(SDValue());
11809 if (Used->use_empty())
11810 DeadNodeSet.insert(Used);
11811 }
11812
11813 // For MachineNode, initialize the memory references information.
11815 MN->clearMemRefs();
11816
11817 // Swap for an appropriately sized array from the recycler.
11818 removeOperands(N);
11819 createOperands(N, Ops);
11820
11821 // Delete any nodes that are still dead after adding the uses for the
11822 // new operands.
11823 if (!DeadNodeSet.empty()) {
11824 SmallVector<SDNode *, 16> DeadNodes;
11825 for (SDNode *N : DeadNodeSet)
11826 if (N->use_empty())
11827 DeadNodes.push_back(N);
11828 RemoveDeadNodes(DeadNodes);
11829 }
11830
11831 if (IP)
11832 CSEMap.InsertNode(N, IP); // Memoize the new node.
11833 return N;
11834}
11835
11837 unsigned OrigOpc = Node->getOpcode();
11838 unsigned NewOpc;
11839 switch (OrigOpc) {
11840 default:
11841 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11842#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11843 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11844#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11845 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11846#include "llvm/IR/ConstrainedOps.def"
11847 }
11848
11849 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11850
11851 // We're taking this node out of the chain, so we need to re-link things.
11852 SDValue InputChain = Node->getOperand(0);
11853 SDValue OutputChain = SDValue(Node, 1);
11854 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11855
11857 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11858 Ops.push_back(Node->getOperand(i));
11859
11860 SDVTList VTs = getVTList(Node->getValueType(0));
11861 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11862
11863 // MorphNodeTo can operate in two ways: if an existing node with the
11864 // specified operands exists, it can just return it. Otherwise, it
11865 // updates the node in place to have the requested operands.
11866 if (Res == Node) {
11867 // If we updated the node in place, reset the node ID. To the isel,
11868 // this should be just like a newly allocated machine node.
11869 Res->setNodeId(-1);
11870 } else {
11873 }
11874
11875 return Res;
11876}
11877
11878/// getMachineNode - These are used for target selectors to create a new node
11879/// with specified return type(s), MachineInstr opcode, and operands.
11880///
11881/// Note that getMachineNode returns the resultant node. If there is already a
11882/// node of the specified opcode and operands, it returns that node instead of
11883/// the current one.
11885 EVT VT) {
11886 SDVTList VTs = getVTList(VT);
11887 return getMachineNode(Opcode, dl, VTs, {});
11888}
11889
11891 EVT VT, SDValue Op1) {
11892 SDVTList VTs = getVTList(VT);
11893 SDValue Ops[] = { Op1 };
11894 return getMachineNode(Opcode, dl, VTs, Ops);
11895}
11896
11898 EVT VT, SDValue Op1, SDValue Op2) {
11899 SDVTList VTs = getVTList(VT);
11900 SDValue Ops[] = { Op1, Op2 };
11901 return getMachineNode(Opcode, dl, VTs, Ops);
11902}
11903
11905 EVT VT, SDValue Op1, SDValue Op2,
11906 SDValue Op3) {
11907 SDVTList VTs = getVTList(VT);
11908 SDValue Ops[] = { Op1, Op2, Op3 };
11909 return getMachineNode(Opcode, dl, VTs, Ops);
11910}
11911
11914 SDVTList VTs = getVTList(VT);
11915 return getMachineNode(Opcode, dl, VTs, Ops);
11916}
11917
11919 EVT VT1, EVT VT2, SDValue Op1,
11920 SDValue Op2) {
11921 SDVTList VTs = getVTList(VT1, VT2);
11922 SDValue Ops[] = { Op1, Op2 };
11923 return getMachineNode(Opcode, dl, VTs, Ops);
11924}
11925
11927 EVT VT1, EVT VT2, SDValue Op1,
11928 SDValue Op2, SDValue Op3) {
11929 SDVTList VTs = getVTList(VT1, VT2);
11930 SDValue Ops[] = { Op1, Op2, Op3 };
11931 return getMachineNode(Opcode, dl, VTs, Ops);
11932}
11933
11935 EVT VT1, EVT VT2,
11937 SDVTList VTs = getVTList(VT1, VT2);
11938 return getMachineNode(Opcode, dl, VTs, Ops);
11939}
11940
11942 EVT VT1, EVT VT2, EVT VT3,
11943 SDValue Op1, SDValue Op2) {
11944 SDVTList VTs = getVTList(VT1, VT2, VT3);
11945 SDValue Ops[] = { Op1, Op2 };
11946 return getMachineNode(Opcode, dl, VTs, Ops);
11947}
11948
11950 EVT VT1, EVT VT2, EVT VT3,
11951 SDValue Op1, SDValue Op2,
11952 SDValue Op3) {
11953 SDVTList VTs = getVTList(VT1, VT2, VT3);
11954 SDValue Ops[] = { Op1, Op2, Op3 };
11955 return getMachineNode(Opcode, dl, VTs, Ops);
11956}
11957
11959 EVT VT1, EVT VT2, EVT VT3,
11961 SDVTList VTs = getVTList(VT1, VT2, VT3);
11962 return getMachineNode(Opcode, dl, VTs, Ops);
11963}
11964
11966 ArrayRef<EVT> ResultTys,
11968 SDVTList VTs = getVTList(ResultTys);
11969 return getMachineNode(Opcode, dl, VTs, Ops);
11970}
11971
11973 SDVTList VTs,
11975 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11977 void *IP = nullptr;
11978
11979 if (DoCSE) {
11981 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11982 IP = nullptr;
11983 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11984 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11985 }
11986 }
11987
11988 // Allocate a new MachineSDNode.
11989 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11990 createOperands(N, Ops);
11991
11992 if (DoCSE)
11993 CSEMap.InsertNode(N, IP);
11994
11995 InsertNode(N);
11996 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11997 return N;
11998}
11999
12000/// getTargetExtractSubreg - A convenience function for creating
12001/// TargetOpcode::EXTRACT_SUBREG nodes.
12003 SDValue Operand) {
12004 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12005 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
12006 VT, Operand, SRIdxVal);
12007 return SDValue(Subreg, 0);
12008}
12009
12010/// getTargetInsertSubreg - A convenience function for creating
12011/// TargetOpcode::INSERT_SUBREG nodes.
12013 SDValue Operand, SDValue Subreg) {
12014 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12015 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
12016 VT, Operand, Subreg, SRIdxVal);
12017 return SDValue(Result, 0);
12018}
12019
12020/// getNodeIfExists - Get the specified node if it's already available, or
12021/// else return NULL.
12024 bool AllowCommute) {
12025 SDNodeFlags Flags;
12026 if (Inserter)
12027 Flags = Inserter->getFlags();
12028 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
12029}
12030
12033 const SDNodeFlags Flags,
12034 bool AllowCommute) {
12035 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
12036 return nullptr;
12037
12038 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
12040 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
12041 void *IP = nullptr;
12042 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
12043 E->intersectFlagsWith(Flags);
12044 return E;
12045 }
12046 return nullptr;
12047 };
12048
12049 if (SDNode *Existing = Lookup(Ops))
12050 return Existing;
12051
12052 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
12053 return Lookup({Ops[1], Ops[0]});
12054
12055 return nullptr;
12056}
12057
12058/// doesNodeExist - Check if a node exists without modifying its flags.
12059bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
12061 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
12063 AddNodeIDNode(ID, Opcode, VTList, Ops);
12064 void *IP = nullptr;
12065 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
12066 return true;
12067 }
12068 return false;
12069}
12070
12071/// getDbgValue - Creates a SDDbgValue node.
12072///
12073/// SDNode
12075 SDNode *N, unsigned R, bool IsIndirect,
12076 const DebugLoc &DL, unsigned O) {
12077 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12078 "Expected inlined-at fields to agree");
12079 return new (DbgInfo->getAlloc())
12080 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
12081 {}, IsIndirect, DL, O,
12082 /*IsVariadic=*/false);
12083}
12084
12085/// Constant
12087 DIExpression *Expr,
12088 const Value *C,
12089 const DebugLoc &DL, unsigned O) {
12090 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12091 "Expected inlined-at fields to agree");
12092 return new (DbgInfo->getAlloc())
12093 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
12094 /*IsIndirect=*/false, DL, O,
12095 /*IsVariadic=*/false);
12096}
12097
12098/// FrameIndex
12100 DIExpression *Expr, unsigned FI,
12101 bool IsIndirect,
12102 const DebugLoc &DL,
12103 unsigned O) {
12104 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12105 "Expected inlined-at fields to agree");
12106 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
12107}
12108
12109/// FrameIndex with dependencies
12111 DIExpression *Expr, unsigned FI,
12112 ArrayRef<SDNode *> Dependencies,
12113 bool IsIndirect,
12114 const DebugLoc &DL,
12115 unsigned O) {
12116 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12117 "Expected inlined-at fields to agree");
12118 return new (DbgInfo->getAlloc())
12119 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
12120 Dependencies, IsIndirect, DL, O,
12121 /*IsVariadic=*/false);
12122}
12123
12124/// VReg
12126 Register VReg, bool IsIndirect,
12127 const DebugLoc &DL, unsigned O) {
12128 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12129 "Expected inlined-at fields to agree");
12130 return new (DbgInfo->getAlloc())
12131 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
12132 {}, IsIndirect, DL, O,
12133 /*IsVariadic=*/false);
12134}
12135
12138 ArrayRef<SDNode *> Dependencies,
12139 bool IsIndirect, const DebugLoc &DL,
12140 unsigned O, bool IsVariadic) {
12141 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12142 "Expected inlined-at fields to agree");
12143 return new (DbgInfo->getAlloc())
12144 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
12145 DL, O, IsVariadic);
12146}
12147
12149 unsigned OffsetInBits, unsigned SizeInBits,
12150 bool InvalidateDbg) {
12151 SDNode *FromNode = From.getNode();
12152 SDNode *ToNode = To.getNode();
12153 assert(FromNode && ToNode && "Can't modify dbg values");
12154
12155 // PR35338
12156 // TODO: assert(From != To && "Redundant dbg value transfer");
12157 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12158 if (From == To || FromNode == ToNode)
12159 return;
12160
12161 if (!FromNode->getHasDebugValue())
12162 return;
12163
12164 SDDbgOperand FromLocOp =
12165 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12167
12169 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12170 if (Dbg->isInvalidated())
12171 continue;
12172
12173 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12174
12175 // Create a new location ops vector that is equal to the old vector, but
12176 // with each instance of FromLocOp replaced with ToLocOp.
12177 bool Changed = false;
12178 auto NewLocOps = Dbg->copyLocationOps();
12179 std::replace_if(
12180 NewLocOps.begin(), NewLocOps.end(),
12181 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12182 bool Match = Op == FromLocOp;
12183 Changed |= Match;
12184 return Match;
12185 },
12186 ToLocOp);
12187 // Ignore this SDDbgValue if we didn't find a matching location.
12188 if (!Changed)
12189 continue;
12190
12191 DIVariable *Var = Dbg->getVariable();
12192 auto *Expr = Dbg->getExpression();
12193 // If a fragment is requested, update the expression.
12194 if (SizeInBits) {
12195 // When splitting a larger (e.g., sign-extended) value whose
12196 // lower bits are described with an SDDbgValue, do not attempt
12197 // to transfer the SDDbgValue to the upper bits.
12198 if (auto FI = Expr->getFragmentInfo())
12199 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12200 continue;
12201 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12202 SizeInBits);
12203 if (!Fragment)
12204 continue;
12205 Expr = *Fragment;
12206 }
12207
12208 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12209 // Clone the SDDbgValue and move it to To.
12210 SDDbgValue *Clone = getDbgValueList(
12211 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12212 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12213 Dbg->isVariadic());
12214 ClonedDVs.push_back(Clone);
12215
12216 if (InvalidateDbg) {
12217 // Invalidate value and indicate the SDDbgValue should not be emitted.
12218 Dbg->setIsInvalidated();
12219 Dbg->setIsEmitted();
12220 }
12221 }
12222
12223 for (SDDbgValue *Dbg : ClonedDVs) {
12224 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12225 "Transferred DbgValues should depend on the new SDNode");
12226 AddDbgValue(Dbg, false);
12227 }
12228}
12229
12231 if (!N.getHasDebugValue())
12232 return;
12233
12234 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12235 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12236 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12237 return SDDbgOperand::fromNode(Node, ResNo);
12238 };
12239
12241 for (auto *DV : GetDbgValues(&N)) {
12242 if (DV->isInvalidated())
12243 continue;
12244 switch (N.getOpcode()) {
12245 default:
12246 break;
12247 case ISD::ADD: {
12248 SDValue N0 = N.getOperand(0);
12249 SDValue N1 = N.getOperand(1);
12250 if (!isa<ConstantSDNode>(N0)) {
12251 bool RHSConstant = isa<ConstantSDNode>(N1);
12253 if (RHSConstant)
12254 Offset = N.getConstantOperandVal(1);
12255 // We are not allowed to turn indirect debug values variadic, so
12256 // don't salvage those.
12257 if (!RHSConstant && DV->isIndirect())
12258 continue;
12259
12260 // Rewrite an ADD constant node into a DIExpression. Since we are
12261 // performing arithmetic to compute the variable's *value* in the
12262 // DIExpression, we need to mark the expression with a
12263 // DW_OP_stack_value.
12264 auto *DIExpr = DV->getExpression();
12265 auto NewLocOps = DV->copyLocationOps();
12266 bool Changed = false;
12267 size_t OrigLocOpsSize = NewLocOps.size();
12268 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12269 // We're not given a ResNo to compare against because the whole
12270 // node is going away. We know that any ISD::ADD only has one
12271 // result, so we can assume any node match is using the result.
12272 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12273 NewLocOps[i].getSDNode() != &N)
12274 continue;
12275 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12276 if (RHSConstant) {
12279 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12280 } else {
12281 // Convert to a variadic expression (if not already).
12282 // convertToVariadicExpression() returns a const pointer, so we use
12283 // a temporary const variable here.
12284 const auto *TmpDIExpr =
12288 ExprOps.push_back(NewLocOps.size());
12289 ExprOps.push_back(dwarf::DW_OP_plus);
12290 SDDbgOperand RHS =
12292 NewLocOps.push_back(RHS);
12293 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12294 }
12295 Changed = true;
12296 }
12297 (void)Changed;
12298 assert(Changed && "Salvage target doesn't use N");
12299
12300 bool IsVariadic =
12301 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12302
12303 auto AdditionalDependencies = DV->getAdditionalDependencies();
12304 SDDbgValue *Clone = getDbgValueList(
12305 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12306 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12307 ClonedDVs.push_back(Clone);
12308 DV->setIsInvalidated();
12309 DV->setIsEmitted();
12310 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12311 N0.getNode()->dumprFull(this);
12312 dbgs() << " into " << *DIExpr << '\n');
12313 }
12314 break;
12315 }
12316 case ISD::TRUNCATE: {
12317 SDValue N0 = N.getOperand(0);
12318 TypeSize FromSize = N0.getValueSizeInBits();
12319 TypeSize ToSize = N.getValueSizeInBits(0);
12320
12321 DIExpression *DbgExpression = DV->getExpression();
12322 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12323 auto NewLocOps = DV->copyLocationOps();
12324 bool Changed = false;
12325 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12326 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12327 NewLocOps[i].getSDNode() != &N)
12328 continue;
12329
12330 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12331 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12332 Changed = true;
12333 }
12334 assert(Changed && "Salvage target doesn't use N");
12335 (void)Changed;
12336
12337 SDDbgValue *Clone =
12338 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12339 DV->getAdditionalDependencies(), DV->isIndirect(),
12340 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12341
12342 ClonedDVs.push_back(Clone);
12343 DV->setIsInvalidated();
12344 DV->setIsEmitted();
12345 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12346 dbgs() << " into " << *DbgExpression << '\n');
12347 break;
12348 }
12349 }
12350 }
12351
12352 for (SDDbgValue *Dbg : ClonedDVs) {
12353 assert((!Dbg->getSDNodes().empty() ||
12354 llvm::any_of(Dbg->getLocationOps(),
12355 [&](const SDDbgOperand &Op) {
12356 return Op.getKind() == SDDbgOperand::FRAMEIX;
12357 })) &&
12358 "Salvaged DbgValue should depend on a new SDNode");
12359 AddDbgValue(Dbg, false);
12360 }
12361}
12362
12363/// Creates a SDDbgLabel node.
12365 const DebugLoc &DL, unsigned O) {
12366 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12367 "Expected inlined-at fields to agree");
12368 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12369}
12370
12371namespace {
12372
12373/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12374/// pointed to by a use iterator is deleted, increment the use iterator
12375/// so that it doesn't dangle.
12376///
12377class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12380
12381 void NodeDeleted(SDNode *N, SDNode *E) override {
12382 // Increment the iterator as needed.
12383 while (UI != UE && N == UI->getUser())
12384 ++UI;
12385 }
12386
12387public:
12388 RAUWUpdateListener(SelectionDAG &d,
12391 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12392};
12393
12394} // end anonymous namespace
12395
12396/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12397/// This can cause recursive merging of nodes in the DAG.
12398///
12399/// This version assumes From has a single result value.
12400///
12402 SDNode *From = FromN.getNode();
12403 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12404 "Cannot replace with this method!");
12405 assert(From != To.getNode() && "Cannot replace uses of with self");
12406
12407 // Preserve Debug Values
12408 transferDbgValues(FromN, To);
12409 // Preserve extra info.
12410 copyExtraInfo(From, To.getNode());
12411
12412 // Iterate over all the existing uses of From. New uses will be added
12413 // to the beginning of the use list, which we avoid visiting.
12414 // This specifically avoids visiting uses of From that arise while the
12415 // replacement is happening, because any such uses would be the result
12416 // of CSE: If an existing node looks like From after one of its operands
12417 // is replaced by To, we don't want to replace of all its users with To
12418 // too. See PR3018 for more info.
12419 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12420 RAUWUpdateListener Listener(*this, UI, UE);
12421 while (UI != UE) {
12422 SDNode *User = UI->getUser();
12423
12424 // This node is about to morph, remove its old self from the CSE maps.
12425 RemoveNodeFromCSEMaps(User);
12426
12427 // A user can appear in a use list multiple times, and when this
12428 // happens the uses are usually next to each other in the list.
12429 // To help reduce the number of CSE recomputations, process all
12430 // the uses of this user that we can find this way.
12431 do {
12432 SDUse &Use = *UI;
12433 ++UI;
12434 Use.set(To);
12435 if (To->isDivergent() != From->isDivergent())
12437 } while (UI != UE && UI->getUser() == User);
12438 // Now that we have modified User, add it back to the CSE maps. If it
12439 // already exists there, recursively merge the results together.
12440 AddModifiedNodeToCSEMaps(User);
12441 }
12442
12443 // If we just RAUW'd the root, take note.
12444 if (FromN == getRoot())
12445 setRoot(To);
12446}
12447
12448/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12449/// This can cause recursive merging of nodes in the DAG.
12450///
12451/// This version assumes that for each value of From, there is a
12452/// corresponding value in To in the same position with the same type.
12453///
12455#ifndef NDEBUG
12456 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12457 assert((!From->hasAnyUseOfValue(i) ||
12458 From->getValueType(i) == To->getValueType(i)) &&
12459 "Cannot use this version of ReplaceAllUsesWith!");
12460#endif
12461
12462 // Handle the trivial case.
12463 if (From == To)
12464 return;
12465
12466 // Preserve Debug Info. Only do this if there's a use.
12467 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12468 if (From->hasAnyUseOfValue(i)) {
12469 assert((i < To->getNumValues()) && "Invalid To location");
12470 transferDbgValues(SDValue(From, i), SDValue(To, i));
12471 }
12472 // Preserve extra info.
12473 copyExtraInfo(From, To);
12474
12475 // Iterate over just the existing users of From. See the comments in
12476 // the ReplaceAllUsesWith above.
12477 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12478 RAUWUpdateListener Listener(*this, UI, UE);
12479 while (UI != UE) {
12480 SDNode *User = UI->getUser();
12481
12482 // This node is about to morph, remove its old self from the CSE maps.
12483 RemoveNodeFromCSEMaps(User);
12484
12485 // A user can appear in a use list multiple times, and when this
12486 // happens the uses are usually next to each other in the list.
12487 // To help reduce the number of CSE recomputations, process all
12488 // the uses of this user that we can find this way.
12489 do {
12490 SDUse &Use = *UI;
12491 ++UI;
12492 Use.setNode(To);
12493 if (To->isDivergent() != From->isDivergent())
12495 } while (UI != UE && UI->getUser() == User);
12496
12497 // Now that we have modified User, add it back to the CSE maps. If it
12498 // already exists there, recursively merge the results together.
12499 AddModifiedNodeToCSEMaps(User);
12500 }
12501
12502 // If we just RAUW'd the root, take note.
12503 if (From == getRoot().getNode())
12504 setRoot(SDValue(To, getRoot().getResNo()));
12505}
12506
12507/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12508/// This can cause recursive merging of nodes in the DAG.
12509///
12510/// This version can replace From with any result values. To must match the
12511/// number and types of values returned by From.
12513 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12514 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12515
12516 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12517 // Preserve Debug Info.
12518 transferDbgValues(SDValue(From, i), To[i]);
12519 // Preserve extra info.
12520 copyExtraInfo(From, To[i].getNode());
12521 }
12522
12523 // Iterate over just the existing users of From. See the comments in
12524 // the ReplaceAllUsesWith above.
12525 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12526 RAUWUpdateListener Listener(*this, UI, UE);
12527 while (UI != UE) {
12528 SDNode *User = UI->getUser();
12529
12530 // This node is about to morph, remove its old self from the CSE maps.
12531 RemoveNodeFromCSEMaps(User);
12532
12533 // A user can appear in a use list multiple times, and when this happens the
12534 // uses are usually next to each other in the list. To help reduce the
12535 // number of CSE and divergence recomputations, process all the uses of this
12536 // user that we can find this way.
12537 bool To_IsDivergent = false;
12538 do {
12539 SDUse &Use = *UI;
12540 const SDValue &ToOp = To[Use.getResNo()];
12541 ++UI;
12542 Use.set(ToOp);
12543 if (ToOp.getValueType() != MVT::Other)
12544 To_IsDivergent |= ToOp->isDivergent();
12545 } while (UI != UE && UI->getUser() == User);
12546
12547 if (To_IsDivergent != From->isDivergent())
12549
12550 // Now that we have modified User, add it back to the CSE maps. If it
12551 // already exists there, recursively merge the results together.
12552 AddModifiedNodeToCSEMaps(User);
12553 }
12554
12555 // If we just RAUW'd the root, take note.
12556 if (From == getRoot().getNode())
12557 setRoot(SDValue(To[getRoot().getResNo()]));
12558}
12559
12560/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12561/// uses of other values produced by From.getNode() alone. The Deleted
12562/// vector is handled the same way as for ReplaceAllUsesWith.
12564 // Handle the really simple, really trivial case efficiently.
12565 if (From == To) return;
12566
12567 // Handle the simple, trivial, case efficiently.
12568 if (From.getNode()->getNumValues() == 1) {
12569 ReplaceAllUsesWith(From, To);
12570 return;
12571 }
12572
12573 // Preserve Debug Info.
12574 transferDbgValues(From, To);
12575 copyExtraInfo(From.getNode(), To.getNode());
12576
12577 // Iterate over just the existing users of From. See the comments in
12578 // the ReplaceAllUsesWith above.
12579 SDNode::use_iterator UI = From.getNode()->use_begin(),
12580 UE = From.getNode()->use_end();
12581 RAUWUpdateListener Listener(*this, UI, UE);
12582 while (UI != UE) {
12583 SDNode *User = UI->getUser();
12584 bool UserRemovedFromCSEMaps = false;
12585
12586 // A user can appear in a use list multiple times, and when this
12587 // happens the uses are usually next to each other in the list.
12588 // To help reduce the number of CSE recomputations, process all
12589 // the uses of this user that we can find this way.
12590 do {
12591 SDUse &Use = *UI;
12592
12593 // Skip uses of different values from the same node.
12594 if (Use.getResNo() != From.getResNo()) {
12595 ++UI;
12596 continue;
12597 }
12598
12599 // If this node hasn't been modified yet, it's still in the CSE maps,
12600 // so remove its old self from the CSE maps.
12601 if (!UserRemovedFromCSEMaps) {
12602 RemoveNodeFromCSEMaps(User);
12603 UserRemovedFromCSEMaps = true;
12604 }
12605
12606 ++UI;
12607 Use.set(To);
12608 if (To->isDivergent() != From->isDivergent())
12610 } while (UI != UE && UI->getUser() == User);
12611 // We are iterating over all uses of the From node, so if a use
12612 // doesn't use the specific value, no changes are made.
12613 if (!UserRemovedFromCSEMaps)
12614 continue;
12615
12616 // Now that we have modified User, add it back to the CSE maps. If it
12617 // already exists there, recursively merge the results together.
12618 AddModifiedNodeToCSEMaps(User);
12619 }
12620
12621 // If we just RAUW'd the root, take note.
12622 if (From == getRoot())
12623 setRoot(To);
12624}
12625
12626namespace {
12627
12628/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12629/// to record information about a use.
12630struct UseMemo {
12631 SDNode *User;
12632 unsigned Index;
12633 SDUse *Use;
12634};
12635
12636/// operator< - Sort Memos by User.
12637bool operator<(const UseMemo &L, const UseMemo &R) {
12638 return (intptr_t)L.User < (intptr_t)R.User;
12639}
12640
12641/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12642/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12643/// the node already has been taken care of recursively.
12644class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12645 SmallVectorImpl<UseMemo> &Uses;
12646
12647 void NodeDeleted(SDNode *N, SDNode *E) override {
12648 for (UseMemo &Memo : Uses)
12649 if (Memo.User == N)
12650 Memo.User = nullptr;
12651 }
12652
12653public:
12654 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12655 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12656};
12657
12658} // end anonymous namespace
12659
12660/// Return true if a glue output should propagate divergence information.
12662 switch (Node->getOpcode()) {
12663 case ISD::CopyFromReg:
12664 case ISD::CopyToReg:
12665 return false;
12666 default:
12667 return true;
12668 }
12669
12670 llvm_unreachable("covered opcode switch");
12671}
12672
12674 if (TLI->isSDNodeAlwaysUniform(N)) {
12675 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12676 "Conflicting divergence information!");
12677 return false;
12678 }
12679 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12680 return true;
12681 for (const auto &Op : N->ops()) {
12682 EVT VT = Op.getValueType();
12683
12684 // Skip Chain. It does not carry divergence.
12685 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12686 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12687 return true;
12688 }
12689 return false;
12690}
12691
12693 SmallVector<SDNode *, 16> Worklist(1, N);
12694 do {
12695 N = Worklist.pop_back_val();
12696 bool IsDivergent = calculateDivergence(N);
12697 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12698 N->SDNodeBits.IsDivergent = IsDivergent;
12699 llvm::append_range(Worklist, N->users());
12700 }
12701 } while (!Worklist.empty());
12702}
12703
12704void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12706 Order.reserve(AllNodes.size());
12707 for (auto &N : allnodes()) {
12708 unsigned NOps = N.getNumOperands();
12709 Degree[&N] = NOps;
12710 if (0 == NOps)
12711 Order.push_back(&N);
12712 }
12713 for (size_t I = 0; I != Order.size(); ++I) {
12714 SDNode *N = Order[I];
12715 for (auto *U : N->users()) {
12716 unsigned &UnsortedOps = Degree[U];
12717 if (0 == --UnsortedOps)
12718 Order.push_back(U);
12719 }
12720 }
12721}
12722
12723#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12724void SelectionDAG::VerifyDAGDivergence() {
12725 std::vector<SDNode *> TopoOrder;
12726 CreateTopologicalOrder(TopoOrder);
12727 for (auto *N : TopoOrder) {
12728 assert(calculateDivergence(N) == N->isDivergent() &&
12729 "Divergence bit inconsistency detected");
12730 }
12731}
12732#endif
12733
12734/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12735/// uses of other values produced by From.getNode() alone. The same value
12736/// may appear in both the From and To list. The Deleted vector is
12737/// handled the same way as for ReplaceAllUsesWith.
12739 const SDValue *To,
12740 unsigned Num){
12741 // Handle the simple, trivial case efficiently.
12742 if (Num == 1)
12743 return ReplaceAllUsesOfValueWith(*From, *To);
12744
12745 transferDbgValues(*From, *To);
12746 copyExtraInfo(From->getNode(), To->getNode());
12747
12748 // Read up all the uses and make records of them. This helps
12749 // processing new uses that are introduced during the
12750 // replacement process.
12752 for (unsigned i = 0; i != Num; ++i) {
12753 unsigned FromResNo = From[i].getResNo();
12754 SDNode *FromNode = From[i].getNode();
12755 for (SDUse &Use : FromNode->uses()) {
12756 if (Use.getResNo() == FromResNo) {
12757 UseMemo Memo = {Use.getUser(), i, &Use};
12758 Uses.push_back(Memo);
12759 }
12760 }
12761 }
12762
12763 // Sort the uses, so that all the uses from a given User are together.
12765 RAUOVWUpdateListener Listener(*this, Uses);
12766
12767 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12768 UseIndex != UseIndexEnd; ) {
12769 // We know that this user uses some value of From. If it is the right
12770 // value, update it.
12771 SDNode *User = Uses[UseIndex].User;
12772 // If the node has been deleted by recursive CSE updates when updating
12773 // another node, then just skip this entry.
12774 if (User == nullptr) {
12775 ++UseIndex;
12776 continue;
12777 }
12778
12779 // This node is about to morph, remove its old self from the CSE maps.
12780 RemoveNodeFromCSEMaps(User);
12781
12782 // The Uses array is sorted, so all the uses for a given User
12783 // are next to each other in the list.
12784 // To help reduce the number of CSE recomputations, process all
12785 // the uses of this user that we can find this way.
12786 do {
12787 unsigned i = Uses[UseIndex].Index;
12788 SDUse &Use = *Uses[UseIndex].Use;
12789 ++UseIndex;
12790
12791 Use.set(To[i]);
12792 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12793
12794 // Now that we have modified User, add it back to the CSE maps. If it
12795 // already exists there, recursively merge the results together.
12796 AddModifiedNodeToCSEMaps(User);
12797 }
12798}
12799
12800/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12801/// based on their topological order. It returns the maximum id and a vector
12802/// of the SDNodes* in assigned order by reference.
12804 unsigned DAGSize = 0;
12805
12806 // SortedPos tracks the progress of the algorithm. Nodes before it are
12807 // sorted, nodes after it are unsorted. When the algorithm completes
12808 // it is at the end of the list.
12809 allnodes_iterator SortedPos = allnodes_begin();
12810
12811 // Visit all the nodes. Move nodes with no operands to the front of
12812 // the list immediately. Annotate nodes that do have operands with their
12813 // operand count. Before we do this, the Node Id fields of the nodes
12814 // may contain arbitrary values. After, the Node Id fields for nodes
12815 // before SortedPos will contain the topological sort index, and the
12816 // Node Id fields for nodes At SortedPos and after will contain the
12817 // count of outstanding operands.
12819 checkForCycles(&N, this);
12820 unsigned Degree = N.getNumOperands();
12821 if (Degree == 0) {
12822 // A node with no uses, add it to the result array immediately.
12823 N.setNodeId(DAGSize++);
12824 allnodes_iterator Q(&N);
12825 if (Q != SortedPos)
12826 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12827 assert(SortedPos != AllNodes.end() && "Overran node list");
12828 ++SortedPos;
12829 } else {
12830 // Temporarily use the Node Id as scratch space for the degree count.
12831 N.setNodeId(Degree);
12832 }
12833 }
12834
12835 // Visit all the nodes. As we iterate, move nodes into sorted order,
12836 // such that by the time the end is reached all nodes will be sorted.
12837 for (SDNode &Node : allnodes()) {
12838 SDNode *N = &Node;
12839 checkForCycles(N, this);
12840 // N is in sorted position, so all its uses have one less operand
12841 // that needs to be sorted.
12842 for (SDNode *P : N->users()) {
12843 unsigned Degree = P->getNodeId();
12844 assert(Degree != 0 && "Invalid node degree");
12845 --Degree;
12846 if (Degree == 0) {
12847 // All of P's operands are sorted, so P may sorted now.
12848 P->setNodeId(DAGSize++);
12849 if (P->getIterator() != SortedPos)
12850 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12851 assert(SortedPos != AllNodes.end() && "Overran node list");
12852 ++SortedPos;
12853 } else {
12854 // Update P's outstanding operand count.
12855 P->setNodeId(Degree);
12856 }
12857 }
12858 if (Node.getIterator() == SortedPos) {
12859#ifndef NDEBUG
12861 SDNode *S = &*++I;
12862 dbgs() << "Overran sorted position:\n";
12863 S->dumprFull(this); dbgs() << "\n";
12864 dbgs() << "Checking if this is due to cycles\n";
12865 checkForCycles(this, true);
12866#endif
12867 llvm_unreachable(nullptr);
12868 }
12869 }
12870
12871 assert(SortedPos == AllNodes.end() &&
12872 "Topological sort incomplete!");
12873 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12874 "First node in topological sort is not the entry token!");
12875 assert(AllNodes.front().getNodeId() == 0 &&
12876 "First node in topological sort has non-zero id!");
12877 assert(AllNodes.front().getNumOperands() == 0 &&
12878 "First node in topological sort has operands!");
12879 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12880 "Last node in topologic sort has unexpected id!");
12881 assert(AllNodes.back().use_empty() &&
12882 "Last node in topologic sort has users!");
12883 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12884 return DAGSize;
12885}
12886
12888 SmallVectorImpl<const SDNode *> &SortedNodes) const {
12889 SortedNodes.clear();
12890 // Node -> remaining number of outstanding operands.
12891 DenseMap<const SDNode *, unsigned> RemainingOperands;
12892
12893 // Put nodes without any operands into SortedNodes first.
12894 for (const SDNode &N : allnodes()) {
12895 checkForCycles(&N, this);
12896 unsigned NumOperands = N.getNumOperands();
12897 if (NumOperands == 0)
12898 SortedNodes.push_back(&N);
12899 else
12900 // Record their total number of outstanding operands.
12901 RemainingOperands[&N] = NumOperands;
12902 }
12903
12904 // A node is pushed into SortedNodes when all of its operands (predecessors in
12905 // the graph) are also in SortedNodes.
12906 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
12907 const SDNode *N = SortedNodes[i];
12908 for (const SDNode *U : N->users()) {
12909 // HandleSDNode is never part of a DAG and therefore has no entry in
12910 // RemainingOperands.
12911 if (U->getOpcode() == ISD::HANDLENODE)
12912 continue;
12913 unsigned &NumRemOperands = RemainingOperands[U];
12914 assert(NumRemOperands && "Invalid number of remaining operands");
12915 --NumRemOperands;
12916 if (!NumRemOperands)
12917 SortedNodes.push_back(U);
12918 }
12919 }
12920
12921 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
12922 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
12923 "First node in topological sort is not the entry token");
12924 assert(SortedNodes.front()->getNumOperands() == 0 &&
12925 "First node in topological sort has operands");
12926}
12927
12928/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12929/// value is produced by SD.
12930void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12931 for (SDNode *SD : DB->getSDNodes()) {
12932 if (!SD)
12933 continue;
12934 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12935 SD->setHasDebugValue(true);
12936 }
12937 DbgInfo->add(DB, isParameter);
12938}
12939
12940void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12941
12943 SDValue NewMemOpChain) {
12944 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12945 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12946 // The new memory operation must have the same position as the old load in
12947 // terms of memory dependency. Create a TokenFactor for the old load and new
12948 // memory operation and update uses of the old load's output chain to use that
12949 // TokenFactor.
12950 if (OldChain == NewMemOpChain || OldChain.use_empty())
12951 return NewMemOpChain;
12952
12953 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12954 OldChain, NewMemOpChain);
12955 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12956 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12957 return TokenFactor;
12958}
12959
12961 SDValue NewMemOp) {
12962 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12963 SDValue OldChain = SDValue(OldLoad, 1);
12964 SDValue NewMemOpChain = NewMemOp.getValue(1);
12965 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12966}
12967
12969 Function **OutFunction) {
12970 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12971
12972 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12973 auto *Module = MF->getFunction().getParent();
12974 auto *Function = Module->getFunction(Symbol);
12975
12976 if (OutFunction != nullptr)
12977 *OutFunction = Function;
12978
12979 if (Function != nullptr) {
12980 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12981 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12982 }
12983
12984 std::string ErrorStr;
12985 raw_string_ostream ErrorFormatter(ErrorStr);
12986 ErrorFormatter << "Undefined external symbol ";
12987 ErrorFormatter << '"' << Symbol << '"';
12988 report_fatal_error(Twine(ErrorStr));
12989}
12990
12991//===----------------------------------------------------------------------===//
12992// SDNode Class
12993//===----------------------------------------------------------------------===//
12994
12997 return Const != nullptr && Const->isZero();
12998}
12999
13001 return V.isUndef() || isNullConstant(V);
13002}
13003
13006 return Const != nullptr && Const->isZero() && !Const->isNegative();
13007}
13008
13011 return Const != nullptr && Const->isAllOnes();
13012}
13013
13016 return Const != nullptr && Const->isOne();
13017}
13018
13021 return Const != nullptr && Const->isMinSignedValue();
13022}
13023
13024bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
13025 unsigned OperandNo) {
13026 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
13027 // TODO: Target-specific opcodes could be added.
13028 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
13029 /*AllowTruncation*/ true)) {
13030 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
13031 switch (Opcode) {
13032 case ISD::ADD:
13033 case ISD::OR:
13034 case ISD::XOR:
13035 case ISD::UMAX:
13036 return Const.isZero();
13037 case ISD::MUL:
13038 return Const.isOne();
13039 case ISD::AND:
13040 case ISD::UMIN:
13041 return Const.isAllOnes();
13042 case ISD::SMAX:
13043 return Const.isMinSignedValue();
13044 case ISD::SMIN:
13045 return Const.isMaxSignedValue();
13046 case ISD::SUB:
13047 case ISD::SHL:
13048 case ISD::SRA:
13049 case ISD::SRL:
13050 return OperandNo == 1 && Const.isZero();
13051 case ISD::UDIV:
13052 case ISD::SDIV:
13053 return OperandNo == 1 && Const.isOne();
13054 }
13055 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
13056 switch (Opcode) {
13057 case ISD::FADD:
13058 return ConstFP->isZero() &&
13059 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
13060 case ISD::FSUB:
13061 return OperandNo == 1 && ConstFP->isZero() &&
13062 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
13063 case ISD::FMUL:
13064 return ConstFP->isExactlyValue(1.0);
13065 case ISD::FDIV:
13066 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
13067 case ISD::FMINNUM:
13068 case ISD::FMAXNUM: {
13069 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13070 EVT VT = V.getValueType();
13071 const fltSemantics &Semantics = VT.getFltSemantics();
13072 APFloat NeutralAF = !Flags.hasNoNaNs()
13073 ? APFloat::getQNaN(Semantics)
13074 : !Flags.hasNoInfs()
13075 ? APFloat::getInf(Semantics)
13076 : APFloat::getLargest(Semantics);
13077 if (Opcode == ISD::FMAXNUM)
13078 NeutralAF.changeSign();
13079
13080 return ConstFP->isExactlyValue(NeutralAF);
13081 }
13082 }
13083 }
13084 return false;
13085}
13086
13088 while (V.getOpcode() == ISD::BITCAST)
13089 V = V.getOperand(0);
13090 return V;
13091}
13092
13094 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
13095 V = V.getOperand(0);
13096 return V;
13097}
13098
13100 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
13101 V = V.getOperand(0);
13102 return V;
13103}
13104
13106 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13107 SDValue InVec = V.getOperand(0);
13108 SDValue EltNo = V.getOperand(2);
13109 EVT VT = InVec.getValueType();
13110 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
13111 if (IndexC && VT.isFixedLengthVector() &&
13112 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
13113 !DemandedElts[IndexC->getZExtValue()]) {
13114 V = InVec;
13115 continue;
13116 }
13117 break;
13118 }
13119 return V;
13120}
13121
13123 while (V.getOpcode() == ISD::TRUNCATE)
13124 V = V.getOperand(0);
13125 return V;
13126}
13127
13128bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
13129 if (V.getOpcode() != ISD::XOR)
13130 return false;
13131 V = peekThroughBitcasts(V.getOperand(1));
13132 unsigned NumBits = V.getScalarValueSizeInBits();
13133 ConstantSDNode *C =
13134 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
13135 return C && (C->getAPIntValue().countr_one() >= NumBits);
13136}
13137
13139 bool AllowTruncation) {
13140 EVT VT = N.getValueType();
13141 APInt DemandedElts = VT.isFixedLengthVector()
13143 : APInt(1, 1);
13144 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
13145}
13146
13148 bool AllowUndefs,
13149 bool AllowTruncation) {
13151 return CN;
13152
13153 // SplatVectors can truncate their operands. Ignore that case here unless
13154 // AllowTruncation is set.
13155 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
13156 EVT VecEltVT = N->getValueType(0).getVectorElementType();
13157 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
13158 EVT CVT = CN->getValueType(0);
13159 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13160 if (AllowTruncation || CVT == VecEltVT)
13161 return CN;
13162 }
13163 }
13164
13166 BitVector UndefElements;
13167 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13168
13169 // BuildVectors can truncate their operands. Ignore that case here unless
13170 // AllowTruncation is set.
13171 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13172 if (CN && (UndefElements.none() || AllowUndefs)) {
13173 EVT CVT = CN->getValueType(0);
13174 EVT NSVT = N.getValueType().getScalarType();
13175 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13176 if (AllowTruncation || (CVT == NSVT))
13177 return CN;
13178 }
13179 }
13180
13181 return nullptr;
13182}
13183
13185 EVT VT = N.getValueType();
13186 APInt DemandedElts = VT.isFixedLengthVector()
13188 : APInt(1, 1);
13189 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13190}
13191
13193 const APInt &DemandedElts,
13194 bool AllowUndefs) {
13196 return CN;
13197
13199 BitVector UndefElements;
13200 ConstantFPSDNode *CN =
13201 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13202 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13203 if (CN && (UndefElements.none() || AllowUndefs))
13204 return CN;
13205 }
13206
13207 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13208 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13209 return CN;
13210
13211 return nullptr;
13212}
13213
13214bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13215 // TODO: may want to use peekThroughBitcast() here.
13216 ConstantSDNode *C =
13217 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13218 return C && C->isZero();
13219}
13220
13221bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13222 ConstantSDNode *C =
13223 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13224 return C && C->isOne();
13225}
13226
13227bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13228 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13229 return C && C->isExactlyValue(1.0);
13230}
13231
13232bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13234 unsigned BitWidth = N.getScalarValueSizeInBits();
13235 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13236 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13237}
13238
13239bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13240 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13241 return C && APInt::isSameValue(C->getAPIntValue(),
13242 APInt(C->getAPIntValue().getBitWidth(), 1));
13243}
13244
13245bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13247 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13248 return C && C->isZero();
13249}
13250
13251bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13252 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13253 return C && C->isZero();
13254}
13255
13259
13260MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
13261 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
13262 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
13263 MemSDNodeBits.IsVolatile = MMO->isVolatile();
13264 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
13265 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
13266 MemSDNodeBits.IsInvariant = MMO->isInvariant();
13267
13268 // We check here that the size of the memory operand fits within the size of
13269 // the MMO. This is because the MMO might indicate only a possible address
13270 // range instead of specifying the affected memory addresses precisely.
13271 assert(
13272 (!MMO->getType().isValid() ||
13273 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
13274 "Size mismatch!");
13275}
13276
13277/// Profile - Gather unique data for the node.
13278///
13280 AddNodeIDNode(ID, this);
13281}
13282
13283namespace {
13284
13285 struct EVTArray {
13286 std::vector<EVT> VTs;
13287
13288 EVTArray() {
13289 VTs.reserve(MVT::VALUETYPE_SIZE);
13290 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13291 VTs.push_back(MVT((MVT::SimpleValueType)i));
13292 }
13293 };
13294
13295} // end anonymous namespace
13296
13297/// getValueTypeList - Return a pointer to the specified value type.
13298///
13299const EVT *SDNode::getValueTypeList(MVT VT) {
13300 static EVTArray SimpleVTArray;
13301
13302 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13303 return &SimpleVTArray.VTs[VT.SimpleTy];
13304}
13305
13306/// hasAnyUseOfValue - Return true if there are any use of the indicated
13307/// value. This method ignores uses of other values defined by this operation.
13308bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13309 assert(Value < getNumValues() && "Bad value!");
13310
13311 for (SDUse &U : uses())
13312 if (U.getResNo() == Value)
13313 return true;
13314
13315 return false;
13316}
13317
13318/// isOnlyUserOf - Return true if this node is the only use of N.
13319bool SDNode::isOnlyUserOf(const SDNode *N) const {
13320 bool Seen = false;
13321 for (const SDNode *User : N->users()) {
13322 if (User == this)
13323 Seen = true;
13324 else
13325 return false;
13326 }
13327
13328 return Seen;
13329}
13330
13331/// Return true if the only users of N are contained in Nodes.
13333 bool Seen = false;
13334 for (const SDNode *User : N->users()) {
13335 if (llvm::is_contained(Nodes, User))
13336 Seen = true;
13337 else
13338 return false;
13339 }
13340
13341 return Seen;
13342}
13343
13344/// Return true if the referenced return value is an operand of N.
13345bool SDValue::isOperandOf(const SDNode *N) const {
13346 return is_contained(N->op_values(), *this);
13347}
13348
13349bool SDNode::isOperandOf(const SDNode *N) const {
13350 return any_of(N->op_values(),
13351 [this](SDValue Op) { return this == Op.getNode(); });
13352}
13353
13354/// reachesChainWithoutSideEffects - Return true if this operand (which must
13355/// be a chain) reaches the specified operand without crossing any
13356/// side-effecting instructions on any chain path. In practice, this looks
13357/// through token factors and non-volatile loads. In order to remain efficient,
13358/// this only looks a couple of nodes in, it does not do an exhaustive search.
13359///
13360/// Note that we only need to examine chains when we're searching for
13361/// side-effects; SelectionDAG requires that all side-effects are represented
13362/// by chains, even if another operand would force a specific ordering. This
13363/// constraint is necessary to allow transformations like splitting loads.
13365 unsigned Depth) const {
13366 if (*this == Dest) return true;
13367
13368 // Don't search too deeply, we just want to be able to see through
13369 // TokenFactor's etc.
13370 if (Depth == 0) return false;
13371
13372 // If this is a token factor, all inputs to the TF happen in parallel.
13373 if (getOpcode() == ISD::TokenFactor) {
13374 // First, try a shallow search.
13375 if (is_contained((*this)->ops(), Dest)) {
13376 // We found the chain we want as an operand of this TokenFactor.
13377 // Essentially, we reach the chain without side-effects if we could
13378 // serialize the TokenFactor into a simple chain of operations with
13379 // Dest as the last operation. This is automatically true if the
13380 // chain has one use: there are no other ordering constraints.
13381 // If the chain has more than one use, we give up: some other
13382 // use of Dest might force a side-effect between Dest and the current
13383 // node.
13384 if (Dest.hasOneUse())
13385 return true;
13386 }
13387 // Next, try a deep search: check whether every operand of the TokenFactor
13388 // reaches Dest.
13389 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13390 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13391 });
13392 }
13393
13394 // Loads don't have side effects, look through them.
13395 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13396 if (Ld->isUnordered())
13397 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13398 }
13399 return false;
13400}
13401
13402bool SDNode::hasPredecessor(const SDNode *N) const {
13405 Worklist.push_back(this);
13406 return hasPredecessorHelper(N, Visited, Worklist);
13407}
13408
13410 this->Flags &= Flags;
13411}
13412
13413SDValue
13415 ArrayRef<ISD::NodeType> CandidateBinOps,
13416 bool AllowPartials) {
13417 // The pattern must end in an extract from index 0.
13418 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13419 !isNullConstant(Extract->getOperand(1)))
13420 return SDValue();
13421
13422 // Match against one of the candidate binary ops.
13423 SDValue Op = Extract->getOperand(0);
13424 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13425 return Op.getOpcode() == unsigned(BinOp);
13426 }))
13427 return SDValue();
13428
13429 // Floating-point reductions may require relaxed constraints on the final step
13430 // of the reduction because they may reorder intermediate operations.
13431 unsigned CandidateBinOp = Op.getOpcode();
13432 if (Op.getValueType().isFloatingPoint()) {
13433 SDNodeFlags Flags = Op->getFlags();
13434 switch (CandidateBinOp) {
13435 case ISD::FADD:
13436 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13437 return SDValue();
13438 break;
13439 default:
13440 llvm_unreachable("Unhandled FP opcode for binop reduction");
13441 }
13442 }
13443
13444 // Matching failed - attempt to see if we did enough stages that a partial
13445 // reduction from a subvector is possible.
13446 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13447 if (!AllowPartials || !Op)
13448 return SDValue();
13449 EVT OpVT = Op.getValueType();
13450 EVT OpSVT = OpVT.getScalarType();
13451 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13452 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13453 return SDValue();
13454 BinOp = (ISD::NodeType)CandidateBinOp;
13455 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13456 };
13457
13458 // At each stage, we're looking for something that looks like:
13459 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13460 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13461 // i32 undef, i32 undef, i32 undef, i32 undef>
13462 // %a = binop <8 x i32> %op, %s
13463 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13464 // we expect something like:
13465 // <4,5,6,7,u,u,u,u>
13466 // <2,3,u,u,u,u,u,u>
13467 // <1,u,u,u,u,u,u,u>
13468 // While a partial reduction match would be:
13469 // <2,3,u,u,u,u,u,u>
13470 // <1,u,u,u,u,u,u,u>
13471 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13472 SDValue PrevOp;
13473 for (unsigned i = 0; i < Stages; ++i) {
13474 unsigned MaskEnd = (1 << i);
13475
13476 if (Op.getOpcode() != CandidateBinOp)
13477 return PartialReduction(PrevOp, MaskEnd);
13478
13479 SDValue Op0 = Op.getOperand(0);
13480 SDValue Op1 = Op.getOperand(1);
13481
13483 if (Shuffle) {
13484 Op = Op1;
13485 } else {
13486 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13487 Op = Op0;
13488 }
13489
13490 // The first operand of the shuffle should be the same as the other operand
13491 // of the binop.
13492 if (!Shuffle || Shuffle->getOperand(0) != Op)
13493 return PartialReduction(PrevOp, MaskEnd);
13494
13495 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13496 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13497 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13498 return PartialReduction(PrevOp, MaskEnd);
13499
13500 PrevOp = Op;
13501 }
13502
13503 // Handle subvector reductions, which tend to appear after the shuffle
13504 // reduction stages.
13505 while (Op.getOpcode() == CandidateBinOp) {
13506 unsigned NumElts = Op.getValueType().getVectorNumElements();
13507 SDValue Op0 = Op.getOperand(0);
13508 SDValue Op1 = Op.getOperand(1);
13509 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13511 Op0.getOperand(0) != Op1.getOperand(0))
13512 break;
13513 SDValue Src = Op0.getOperand(0);
13514 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13515 if (NumSrcElts != (2 * NumElts))
13516 break;
13517 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13518 Op1.getConstantOperandAPInt(1) == NumElts) &&
13519 !(Op1.getConstantOperandAPInt(1) == 0 &&
13520 Op0.getConstantOperandAPInt(1) == NumElts))
13521 break;
13522 Op = Src;
13523 }
13524
13525 BinOp = (ISD::NodeType)CandidateBinOp;
13526 return Op;
13527}
13528
13530 EVT VT = N->getValueType(0);
13531 EVT EltVT = VT.getVectorElementType();
13532 unsigned NE = VT.getVectorNumElements();
13533
13534 SDLoc dl(N);
13535
13536 // If ResNE is 0, fully unroll the vector op.
13537 if (ResNE == 0)
13538 ResNE = NE;
13539 else if (NE > ResNE)
13540 NE = ResNE;
13541
13542 if (N->getNumValues() == 2) {
13543 SmallVector<SDValue, 8> Scalars0, Scalars1;
13544 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13545 EVT VT1 = N->getValueType(1);
13546 EVT EltVT1 = VT1.getVectorElementType();
13547
13548 unsigned i;
13549 for (i = 0; i != NE; ++i) {
13550 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13551 SDValue Operand = N->getOperand(j);
13552 EVT OperandVT = Operand.getValueType();
13553
13554 // A vector operand; extract a single element.
13555 EVT OperandEltVT = OperandVT.getVectorElementType();
13556 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13557 }
13558
13559 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13560 Scalars0.push_back(EltOp);
13561 Scalars1.push_back(EltOp.getValue(1));
13562 }
13563
13564 for (; i < ResNE; ++i) {
13565 Scalars0.push_back(getUNDEF(EltVT));
13566 Scalars1.push_back(getUNDEF(EltVT1));
13567 }
13568
13569 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13570 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13571 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13572 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13573 return getMergeValues({Vec0, Vec1}, dl);
13574 }
13575
13576 assert(N->getNumValues() == 1 &&
13577 "Can't unroll a vector with multiple results!");
13578
13580 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13581
13582 unsigned i;
13583 for (i= 0; i != NE; ++i) {
13584 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13585 SDValue Operand = N->getOperand(j);
13586 EVT OperandVT = Operand.getValueType();
13587 if (OperandVT.isVector()) {
13588 // A vector operand; extract a single element.
13589 EVT OperandEltVT = OperandVT.getVectorElementType();
13590 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13591 } else {
13592 // A scalar operand; just use it as is.
13593 Operands[j] = Operand;
13594 }
13595 }
13596
13597 switch (N->getOpcode()) {
13598 default: {
13599 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13600 N->getFlags()));
13601 break;
13602 }
13603 case ISD::VSELECT:
13604 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13605 break;
13606 case ISD::SHL:
13607 case ISD::SRA:
13608 case ISD::SRL:
13609 case ISD::ROTL:
13610 case ISD::ROTR:
13611 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13612 getShiftAmountOperand(Operands[0].getValueType(),
13613 Operands[1])));
13614 break;
13616 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13617 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13618 Operands[0],
13619 getValueType(ExtVT)));
13620 break;
13621 }
13622 case ISD::ADDRSPACECAST: {
13623 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13624 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13625 ASC->getSrcAddressSpace(),
13626 ASC->getDestAddressSpace()));
13627 break;
13628 }
13629 }
13630 }
13631
13632 for (; i < ResNE; ++i)
13633 Scalars.push_back(getUNDEF(EltVT));
13634
13635 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13636 return getBuildVector(VecVT, dl, Scalars);
13637}
13638
13639std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13640 SDNode *N, unsigned ResNE) {
13641 unsigned Opcode = N->getOpcode();
13642 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13643 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13644 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13645 "Expected an overflow opcode");
13646
13647 EVT ResVT = N->getValueType(0);
13648 EVT OvVT = N->getValueType(1);
13649 EVT ResEltVT = ResVT.getVectorElementType();
13650 EVT OvEltVT = OvVT.getVectorElementType();
13651 SDLoc dl(N);
13652
13653 // If ResNE is 0, fully unroll the vector op.
13654 unsigned NE = ResVT.getVectorNumElements();
13655 if (ResNE == 0)
13656 ResNE = NE;
13657 else if (NE > ResNE)
13658 NE = ResNE;
13659
13660 SmallVector<SDValue, 8> LHSScalars;
13661 SmallVector<SDValue, 8> RHSScalars;
13662 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13663 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13664
13665 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13666 SDVTList VTs = getVTList(ResEltVT, SVT);
13667 SmallVector<SDValue, 8> ResScalars;
13668 SmallVector<SDValue, 8> OvScalars;
13669 for (unsigned i = 0; i < NE; ++i) {
13670 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13671 SDValue Ov =
13672 getSelect(dl, OvEltVT, Res.getValue(1),
13673 getBoolConstant(true, dl, OvEltVT, ResVT),
13674 getConstant(0, dl, OvEltVT));
13675
13676 ResScalars.push_back(Res);
13677 OvScalars.push_back(Ov);
13678 }
13679
13680 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13681 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13682
13683 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13684 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13685 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13686 getBuildVector(NewOvVT, dl, OvScalars));
13687}
13688
13691 unsigned Bytes,
13692 int Dist) const {
13693 if (LD->isVolatile() || Base->isVolatile())
13694 return false;
13695 // TODO: probably too restrictive for atomics, revisit
13696 if (!LD->isSimple())
13697 return false;
13698 if (LD->isIndexed() || Base->isIndexed())
13699 return false;
13700 if (LD->getChain() != Base->getChain())
13701 return false;
13702 EVT VT = LD->getMemoryVT();
13703 if (VT.getSizeInBits() / 8 != Bytes)
13704 return false;
13705
13706 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13707 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13708
13709 int64_t Offset = 0;
13710 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13711 return (Dist * (int64_t)Bytes == Offset);
13712 return false;
13713}
13714
13715/// InferPtrAlignment - Infer alignment of a load / store address. Return
13716/// std::nullopt if it cannot be inferred.
13718 // If this is a GlobalAddress + cst, return the alignment.
13719 const GlobalValue *GV = nullptr;
13720 int64_t GVOffset = 0;
13721 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13722 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13723 KnownBits Known(PtrWidth);
13725 unsigned AlignBits = Known.countMinTrailingZeros();
13726 if (AlignBits)
13727 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13728 }
13729
13730 // If this is a direct reference to a stack slot, use information about the
13731 // stack slot's alignment.
13732 int FrameIdx = INT_MIN;
13733 int64_t FrameOffset = 0;
13735 FrameIdx = FI->getIndex();
13736 } else if (isBaseWithConstantOffset(Ptr) &&
13738 // Handle FI+Cst
13739 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13740 FrameOffset = Ptr.getConstantOperandVal(1);
13741 }
13742
13743 if (FrameIdx != INT_MIN) {
13745 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13746 }
13747
13748 return std::nullopt;
13749}
13750
13751/// Split the scalar node with EXTRACT_ELEMENT using the provided
13752/// VTs and return the low/high part.
13753std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13754 const SDLoc &DL,
13755 const EVT &LoVT,
13756 const EVT &HiVT) {
13757 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13758 "Split node must be a scalar type");
13759 SDValue Lo =
13761 SDValue Hi =
13763 return std::make_pair(Lo, Hi);
13764}
13765
13766/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13767/// which is split (or expanded) into two not necessarily identical pieces.
13768std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13769 // Currently all types are split in half.
13770 EVT LoVT, HiVT;
13771 if (!VT.isVector())
13772 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13773 else
13774 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13775
13776 return std::make_pair(LoVT, HiVT);
13777}
13778
13779/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13780/// type, dependent on an enveloping VT that has been split into two identical
13781/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13782std::pair<EVT, EVT>
13784 bool *HiIsEmpty) const {
13785 EVT EltTp = VT.getVectorElementType();
13786 // Examples:
13787 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13788 // custom VL=9 with enveloping VL=8/8 yields 8/1
13789 // custom VL=10 with enveloping VL=8/8 yields 8/2
13790 // etc.
13791 ElementCount VTNumElts = VT.getVectorElementCount();
13792 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13793 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13794 "Mixing fixed width and scalable vectors when enveloping a type");
13795 EVT LoVT, HiVT;
13796 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13797 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13798 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13799 *HiIsEmpty = false;
13800 } else {
13801 // Flag that hi type has zero storage size, but return split envelop type
13802 // (this would be easier if vector types with zero elements were allowed).
13803 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13804 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13805 *HiIsEmpty = true;
13806 }
13807 return std::make_pair(LoVT, HiVT);
13808}
13809
13810/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13811/// low/high part.
13812std::pair<SDValue, SDValue>
13813SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13814 const EVT &HiVT) {
13815 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13816 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13817 "Splitting vector with an invalid mixture of fixed and scalable "
13818 "vector types");
13820 N.getValueType().getVectorMinNumElements() &&
13821 "More vector elements requested than available!");
13822 SDValue Lo, Hi;
13823 Lo = getExtractSubvector(DL, LoVT, N, 0);
13824 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13825 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13826 // IDX with the runtime scaling factor of the result vector type. For
13827 // fixed-width result vectors, that runtime scaling factor is 1.
13830 return std::make_pair(Lo, Hi);
13831}
13832
13833std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13834 const SDLoc &DL) {
13835 // Split the vector length parameter.
13836 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13837 EVT VT = N.getValueType();
13839 "Expecting the mask to be an evenly-sized vector");
13840 SDValue HalfNumElts = getElementCount(
13842 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13843 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13844 return std::make_pair(Lo, Hi);
13845}
13846
13847/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13849 EVT VT = N.getValueType();
13852 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13853}
13854
13857 unsigned Start, unsigned Count,
13858 EVT EltVT) {
13859 EVT VT = Op.getValueType();
13860 if (Count == 0)
13862 if (EltVT == EVT())
13863 EltVT = VT.getVectorElementType();
13864 SDLoc SL(Op);
13865 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13866 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13867 }
13868}
13869
13870// getAddressSpace - Return the address space this GlobalAddress belongs to.
13872 return getGlobal()->getType()->getAddressSpace();
13873}
13874
13877 return Val.MachineCPVal->getType();
13878 return Val.ConstVal->getType();
13879}
13880
13881bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13882 unsigned &SplatBitSize,
13883 bool &HasAnyUndefs,
13884 unsigned MinSplatBits,
13885 bool IsBigEndian) const {
13886 EVT VT = getValueType(0);
13887 assert(VT.isVector() && "Expected a vector type");
13888 unsigned VecWidth = VT.getSizeInBits();
13889 if (MinSplatBits > VecWidth)
13890 return false;
13891
13892 // FIXME: The widths are based on this node's type, but build vectors can
13893 // truncate their operands.
13894 SplatValue = APInt(VecWidth, 0);
13895 SplatUndef = APInt(VecWidth, 0);
13896
13897 // Get the bits. Bits with undefined values (when the corresponding element
13898 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13899 // in SplatValue. If any of the values are not constant, give up and return
13900 // false.
13901 unsigned int NumOps = getNumOperands();
13902 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13903 unsigned EltWidth = VT.getScalarSizeInBits();
13904
13905 for (unsigned j = 0; j < NumOps; ++j) {
13906 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13907 SDValue OpVal = getOperand(i);
13908 unsigned BitPos = j * EltWidth;
13909
13910 if (OpVal.isUndef())
13911 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13912 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13913 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13914 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13915 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13916 else
13917 return false;
13918 }
13919
13920 // The build_vector is all constants or undefs. Find the smallest element
13921 // size that splats the vector.
13922 HasAnyUndefs = (SplatUndef != 0);
13923
13924 // FIXME: This does not work for vectors with elements less than 8 bits.
13925 while (VecWidth > 8) {
13926 // If we can't split in half, stop here.
13927 if (VecWidth & 1)
13928 break;
13929
13930 unsigned HalfSize = VecWidth / 2;
13931 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13932 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13933 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13934 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13935
13936 // If the two halves do not match (ignoring undef bits), stop here.
13937 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13938 MinSplatBits > HalfSize)
13939 break;
13940
13941 SplatValue = HighValue | LowValue;
13942 SplatUndef = HighUndef & LowUndef;
13943
13944 VecWidth = HalfSize;
13945 }
13946
13947 // FIXME: The loop above only tries to split in halves. But if the input
13948 // vector for example is <3 x i16> it wouldn't be able to detect a
13949 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13950 // optimizations. I guess that back in the days when this helper was created
13951 // vectors normally was power-of-2 sized.
13952
13953 SplatBitSize = VecWidth;
13954 return true;
13955}
13956
13958 BitVector *UndefElements) const {
13959 unsigned NumOps = getNumOperands();
13960 if (UndefElements) {
13961 UndefElements->clear();
13962 UndefElements->resize(NumOps);
13963 }
13964 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13965 if (!DemandedElts)
13966 return SDValue();
13967 SDValue Splatted;
13968 for (unsigned i = 0; i != NumOps; ++i) {
13969 if (!DemandedElts[i])
13970 continue;
13971 SDValue Op = getOperand(i);
13972 if (Op.isUndef()) {
13973 if (UndefElements)
13974 (*UndefElements)[i] = true;
13975 } else if (!Splatted) {
13976 Splatted = Op;
13977 } else if (Splatted != Op) {
13978 return SDValue();
13979 }
13980 }
13981
13982 if (!Splatted) {
13983 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13984 assert(getOperand(FirstDemandedIdx).isUndef() &&
13985 "Can only have a splat without a constant for all undefs.");
13986 return getOperand(FirstDemandedIdx);
13987 }
13988
13989 return Splatted;
13990}
13991
13993 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13994 return getSplatValue(DemandedElts, UndefElements);
13995}
13996
13998 SmallVectorImpl<SDValue> &Sequence,
13999 BitVector *UndefElements) const {
14000 unsigned NumOps = getNumOperands();
14001 Sequence.clear();
14002 if (UndefElements) {
14003 UndefElements->clear();
14004 UndefElements->resize(NumOps);
14005 }
14006 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
14007 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
14008 return false;
14009
14010 // Set the undefs even if we don't find a sequence (like getSplatValue).
14011 if (UndefElements)
14012 for (unsigned I = 0; I != NumOps; ++I)
14013 if (DemandedElts[I] && getOperand(I).isUndef())
14014 (*UndefElements)[I] = true;
14015
14016 // Iteratively widen the sequence length looking for repetitions.
14017 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
14018 Sequence.append(SeqLen, SDValue());
14019 for (unsigned I = 0; I != NumOps; ++I) {
14020 if (!DemandedElts[I])
14021 continue;
14022 SDValue &SeqOp = Sequence[I % SeqLen];
14024 if (Op.isUndef()) {
14025 if (!SeqOp)
14026 SeqOp = Op;
14027 continue;
14028 }
14029 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
14030 Sequence.clear();
14031 break;
14032 }
14033 SeqOp = Op;
14034 }
14035 if (!Sequence.empty())
14036 return true;
14037 }
14038
14039 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
14040 return false;
14041}
14042
14044 BitVector *UndefElements) const {
14045 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14046 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
14047}
14048
14051 BitVector *UndefElements) const {
14053 getSplatValue(DemandedElts, UndefElements));
14054}
14055
14058 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
14059}
14060
14063 BitVector *UndefElements) const {
14065 getSplatValue(DemandedElts, UndefElements));
14066}
14067
14072
14073int32_t
14075 uint32_t BitWidth) const {
14076 if (ConstantFPSDNode *CN =
14078 bool IsExact;
14079 APSInt IntVal(BitWidth);
14080 const APFloat &APF = CN->getValueAPF();
14081 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
14082 APFloat::opOK ||
14083 !IsExact)
14084 return -1;
14085
14086 return IntVal.exactLogBase2();
14087 }
14088 return -1;
14089}
14090
14092 bool IsLittleEndian, unsigned DstEltSizeInBits,
14093 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
14094 // Early-out if this contains anything but Undef/Constant/ConstantFP.
14095 if (!isConstant())
14096 return false;
14097
14098 unsigned NumSrcOps = getNumOperands();
14099 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
14100 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14101 "Invalid bitcast scale");
14102
14103 // Extract raw src bits.
14104 SmallVector<APInt> SrcBitElements(NumSrcOps,
14105 APInt::getZero(SrcEltSizeInBits));
14106 BitVector SrcUndeElements(NumSrcOps, false);
14107
14108 for (unsigned I = 0; I != NumSrcOps; ++I) {
14110 if (Op.isUndef()) {
14111 SrcUndeElements.set(I);
14112 continue;
14113 }
14114 auto *CInt = dyn_cast<ConstantSDNode>(Op);
14115 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
14116 assert((CInt || CFP) && "Unknown constant");
14117 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
14118 : CFP->getValueAPF().bitcastToAPInt();
14119 }
14120
14121 // Recast to dst width.
14122 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
14123 SrcBitElements, UndefElements, SrcUndeElements);
14124 return true;
14125}
14126
14127void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
14128 unsigned DstEltSizeInBits,
14129 SmallVectorImpl<APInt> &DstBitElements,
14130 ArrayRef<APInt> SrcBitElements,
14131 BitVector &DstUndefElements,
14132 const BitVector &SrcUndefElements) {
14133 unsigned NumSrcOps = SrcBitElements.size();
14134 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
14135 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14136 "Invalid bitcast scale");
14137 assert(NumSrcOps == SrcUndefElements.size() &&
14138 "Vector size mismatch");
14139
14140 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
14141 DstUndefElements.clear();
14142 DstUndefElements.resize(NumDstOps, false);
14143 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
14144
14145 // Concatenate src elements constant bits together into dst element.
14146 if (SrcEltSizeInBits <= DstEltSizeInBits) {
14147 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
14148 for (unsigned I = 0; I != NumDstOps; ++I) {
14149 DstUndefElements.set(I);
14150 APInt &DstBits = DstBitElements[I];
14151 for (unsigned J = 0; J != Scale; ++J) {
14152 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14153 if (SrcUndefElements[Idx])
14154 continue;
14155 DstUndefElements.reset(I);
14156 const APInt &SrcBits = SrcBitElements[Idx];
14157 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
14158 "Illegal constant bitwidths");
14159 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
14160 }
14161 }
14162 return;
14163 }
14164
14165 // Split src element constant bits into dst elements.
14166 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14167 for (unsigned I = 0; I != NumSrcOps; ++I) {
14168 if (SrcUndefElements[I]) {
14169 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14170 continue;
14171 }
14172 const APInt &SrcBits = SrcBitElements[I];
14173 for (unsigned J = 0; J != Scale; ++J) {
14174 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14175 APInt &DstBits = DstBitElements[Idx];
14176 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14177 }
14178 }
14179}
14180
14182 for (const SDValue &Op : op_values()) {
14183 unsigned Opc = Op.getOpcode();
14184 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14185 return false;
14186 }
14187 return true;
14188}
14189
14190std::optional<std::pair<APInt, APInt>>
14192 unsigned NumOps = getNumOperands();
14193 if (NumOps < 2)
14194 return std::nullopt;
14195
14196 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14197 APInt Start, Stride;
14198 int FirstIdx = -1, SecondIdx = -1;
14199
14200 // Find the first two non-undef constant elements to determine Start and
14201 // Stride, then verify all remaining elements match the sequence.
14202 for (unsigned I = 0; I < NumOps; ++I) {
14204 if (Op->isUndef())
14205 continue;
14206 if (!isa<ConstantSDNode>(Op))
14207 return std::nullopt;
14208
14209 APInt Val = getConstantOperandAPInt(I).trunc(EltSize);
14210 if (FirstIdx < 0) {
14211 FirstIdx = I;
14212 Start = Val;
14213 } else if (SecondIdx < 0) {
14214 SecondIdx = I;
14215 // Compute stride using modular arithmetic. Simple division would handle
14216 // common strides (1, 2, -1, etc.), but modular inverse maximizes matches.
14217 // Example: <0, poison, poison, 0xFF> has stride 0x55 since 3*0x55 = 0xFF
14218 // Note that modular arithmetic is agnostic to signed/unsigned.
14219 unsigned IdxDiff = I - FirstIdx;
14220 APInt ValDiff = Val - Start;
14221
14222 // Step 1: Factor out common powers of 2 from IdxDiff and ValDiff.
14223 unsigned CommonPow2Bits = llvm::countr_zero(IdxDiff);
14224 if (ValDiff.countr_zero() < CommonPow2Bits)
14225 return std::nullopt; // ValDiff not divisible by 2^CommonPow2Bits
14226 IdxDiff >>= CommonPow2Bits;
14227 ValDiff.lshrInPlace(CommonPow2Bits);
14228
14229 // Step 2: IdxDiff is now odd, so its inverse mod 2^EltSize exists.
14230 // TODO: There are 2^CommonPow2Bits valid strides; currently we only try
14231 // one, but we could try all candidates to handle more cases.
14232 Stride = ValDiff * APInt(EltSize, IdxDiff).multiplicativeInverse();
14233 if (Stride.isZero())
14234 return std::nullopt;
14235
14236 // Step 3: Adjust Start based on the first defined element's index.
14237 Start -= Stride * FirstIdx;
14238 } else {
14239 // Verify this element matches the sequence.
14240 if (Val != Start + Stride * I)
14241 return std::nullopt;
14242 }
14243 }
14244
14245 // Need at least two defined elements.
14246 if (SecondIdx < 0)
14247 return std::nullopt;
14248
14249 return std::make_pair(Start, Stride);
14250}
14251
14253 // Find the first non-undef value in the shuffle mask.
14254 unsigned i, e;
14255 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14256 /* search */;
14257
14258 // If all elements are undefined, this shuffle can be considered a splat
14259 // (although it should eventually get simplified away completely).
14260 if (i == e)
14261 return true;
14262
14263 // Make sure all remaining elements are either undef or the same as the first
14264 // non-undef value.
14265 for (int Idx = Mask[i]; i != e; ++i)
14266 if (Mask[i] >= 0 && Mask[i] != Idx)
14267 return false;
14268 return true;
14269}
14270
14271// Returns true if it is a constant integer BuildVector or constant integer,
14272// possibly hidden by a bitcast.
14274 SDValue N, bool AllowOpaques) const {
14276
14277 if (auto *C = dyn_cast<ConstantSDNode>(N))
14278 return AllowOpaques || !C->isOpaque();
14279
14281 return true;
14282
14283 // Treat a GlobalAddress supporting constant offset folding as a
14284 // constant integer.
14285 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14286 if (GA->getOpcode() == ISD::GlobalAddress &&
14287 TLI->isOffsetFoldingLegal(GA))
14288 return true;
14289
14290 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14291 isa<ConstantSDNode>(N.getOperand(0)))
14292 return true;
14293 return false;
14294}
14295
14296// Returns true if it is a constant float BuildVector or constant float.
14299 return true;
14300
14302 return true;
14303
14304 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14305 isa<ConstantFPSDNode>(N.getOperand(0)))
14306 return true;
14307
14308 return false;
14309}
14310
14311std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14312 ConstantSDNode *Const =
14313 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14314 if (!Const)
14315 return std::nullopt;
14316
14317 EVT VT = N->getValueType(0);
14318 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14319 switch (TLI->getBooleanContents(N.getValueType())) {
14321 if (CVal.isOne())
14322 return true;
14323 if (CVal.isZero())
14324 return false;
14325 return std::nullopt;
14327 if (CVal.isAllOnes())
14328 return true;
14329 if (CVal.isZero())
14330 return false;
14331 return std::nullopt;
14333 return CVal[0];
14334 }
14335 llvm_unreachable("Unknown BooleanContent enum");
14336}
14337
14338void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14339 assert(!Node->OperandList && "Node already has operands");
14341 "too many operands to fit into SDNode");
14342 SDUse *Ops = OperandRecycler.allocate(
14343 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14344
14345 bool IsDivergent = false;
14346 for (unsigned I = 0; I != Vals.size(); ++I) {
14347 Ops[I].setUser(Node);
14348 Ops[I].setInitial(Vals[I]);
14349 EVT VT = Ops[I].getValueType();
14350
14351 // Skip Chain. It does not carry divergence.
14352 if (VT != MVT::Other &&
14353 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14354 Ops[I].getNode()->isDivergent()) {
14355 IsDivergent = true;
14356 }
14357 }
14358 Node->NumOperands = Vals.size();
14359 Node->OperandList = Ops;
14360 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14361 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14362 Node->SDNodeBits.IsDivergent = IsDivergent;
14363 }
14364 checkForCycles(Node);
14365}
14366
14369 size_t Limit = SDNode::getMaxNumOperands();
14370 while (Vals.size() > Limit) {
14371 unsigned SliceIdx = Vals.size() - Limit;
14372 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14373 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14374 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14375 Vals.emplace_back(NewTF);
14376 }
14377 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14378}
14379
14381 EVT VT, SDNodeFlags Flags) {
14382 switch (Opcode) {
14383 default:
14384 return SDValue();
14385 case ISD::ADD:
14386 case ISD::OR:
14387 case ISD::XOR:
14388 case ISD::UMAX:
14389 return getConstant(0, DL, VT);
14390 case ISD::MUL:
14391 return getConstant(1, DL, VT);
14392 case ISD::AND:
14393 case ISD::UMIN:
14394 return getAllOnesConstant(DL, VT);
14395 case ISD::SMAX:
14397 case ISD::SMIN:
14399 case ISD::FADD:
14400 // If flags allow, prefer positive zero since it's generally cheaper
14401 // to materialize on most targets.
14402 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14403 case ISD::FMUL:
14404 return getConstantFP(1.0, DL, VT);
14405 case ISD::FMINNUM:
14406 case ISD::FMAXNUM: {
14407 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14408 const fltSemantics &Semantics = VT.getFltSemantics();
14409 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14410 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14411 APFloat::getLargest(Semantics);
14412 if (Opcode == ISD::FMAXNUM)
14413 NeutralAF.changeSign();
14414
14415 return getConstantFP(NeutralAF, DL, VT);
14416 }
14417 case ISD::FMINIMUM:
14418 case ISD::FMAXIMUM: {
14419 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14420 const fltSemantics &Semantics = VT.getFltSemantics();
14421 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14422 : APFloat::getLargest(Semantics);
14423 if (Opcode == ISD::FMAXIMUM)
14424 NeutralAF.changeSign();
14425
14426 return getConstantFP(NeutralAF, DL, VT);
14427 }
14428
14429 }
14430}
14431
14432/// Helper used to make a call to a library function that has one argument of
14433/// pointer type.
14434///
14435/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14436/// used to get or set floating-point state. They have one argument of pointer
14437/// type, which points to the memory region containing bits of the
14438/// floating-point state. The value returned by such function is ignored in the
14439/// created call.
14440///
14441/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14442/// \param Ptr Pointer used to save/load state.
14443/// \param InChain Ingoing token chain.
14444/// \returns Outgoing chain token.
14446 SDValue InChain,
14447 const SDLoc &DLoc) {
14448 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14450 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14451 RTLIB::LibcallImpl LibcallImpl =
14452 Libcalls->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
14453 if (LibcallImpl == RTLIB::Unsupported)
14454 reportFatalUsageError("emitting call to unsupported libcall");
14455
14456 SDValue Callee =
14457 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
14459 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14460 Libcalls->getLibcallImplCallingConv(LibcallImpl),
14461 Type::getVoidTy(*getContext()), Callee, std::move(Args));
14462 return TLI->LowerCallTo(CLI).second;
14463}
14464
14466 assert(From && To && "Invalid SDNode; empty source SDValue?");
14467 auto I = SDEI.find(From);
14468 if (I == SDEI.end())
14469 return;
14470
14471 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14472 // the iterator, hence the need to make a copy to prevent a use-after-free.
14473 NodeExtraInfo NEI = I->second;
14474 if (LLVM_LIKELY(!NEI.PCSections)) {
14475 // No deep copy required for the types of extra info set.
14476 //
14477 // FIXME: Investigate if other types of extra info also need deep copy. This
14478 // depends on the types of nodes they can be attached to: if some extra info
14479 // is only ever attached to nodes where a replacement To node is always the
14480 // node where later use and propagation of the extra info has the intended
14481 // semantics, no deep copy is required.
14482 SDEI[To] = std::move(NEI);
14483 return;
14484 }
14485
14486 const SDNode *EntrySDN = getEntryNode().getNode();
14487
14488 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14489 // through the replacement of From with To. Otherwise, replacements of a node
14490 // (From) with more complex nodes (To and its operands) may result in lost
14491 // extra info where the root node (To) is insignificant in further propagating
14492 // and using extra info when further lowering to MIR.
14493 //
14494 // In the first step pre-populate the visited set with the nodes reachable
14495 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14496 // DAG that is not new and should be left untouched.
14497 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14498 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14499 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14500 if (MaxDepth == 0) {
14501 // Remember this node in case we need to increase MaxDepth and continue
14502 // populating FromReach from this node.
14503 Leafs.emplace_back(N);
14504 return;
14505 }
14506 if (!FromReach.insert(N).second)
14507 return;
14508 for (const SDValue &Op : N->op_values())
14509 Self(Self, Op.getNode(), MaxDepth - 1);
14510 };
14511
14512 // Copy extra info to To and all its transitive operands (that are new).
14514 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14515 if (FromReach.contains(N))
14516 return true;
14517 if (!Visited.insert(N).second)
14518 return true;
14519 if (EntrySDN == N)
14520 return false;
14521 for (const SDValue &Op : N->op_values()) {
14522 if (N == To && Op.getNode() == EntrySDN) {
14523 // Special case: New node's operand is the entry node; just need to
14524 // copy extra info to new node.
14525 break;
14526 }
14527 if (!Self(Self, Op.getNode()))
14528 return false;
14529 }
14530 // Copy only if entry node was not reached.
14531 SDEI[N] = std::move(NEI);
14532 return true;
14533 };
14534
14535 // We first try with a lower MaxDepth, assuming that the path to common
14536 // operands between From and To is relatively short. This significantly
14537 // improves performance in the common case. The initial MaxDepth is big
14538 // enough to avoid retry in the common case; the last MaxDepth is large
14539 // enough to avoid having to use the fallback below (and protects from
14540 // potential stack exhaustion from recursion).
14541 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14542 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14543 // StartFrom is the previous (or initial) set of leafs reachable at the
14544 // previous maximum depth.
14546 std::swap(StartFrom, Leafs);
14547 for (const SDNode *N : StartFrom)
14548 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14549 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14550 return;
14551 // This should happen very rarely (reached the entry node).
14552 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14553 assert(!Leafs.empty());
14554 }
14555
14556 // This should not happen - but if it did, that means the subgraph reachable
14557 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14558 // could not visit all reachable common operands. Consequently, we were able
14559 // to reach the entry node.
14560 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14561 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14562 // Best-effort fallback if assertions disabled.
14563 SDEI[To] = std::move(NEI);
14564}
14565
14566#ifndef NDEBUG
14567static void checkForCyclesHelper(const SDNode *N,
14570 const llvm::SelectionDAG *DAG) {
14571 // If this node has already been checked, don't check it again.
14572 if (Checked.count(N))
14573 return;
14574
14575 // If a node has already been visited on this depth-first walk, reject it as
14576 // a cycle.
14577 if (!Visited.insert(N).second) {
14578 errs() << "Detected cycle in SelectionDAG\n";
14579 dbgs() << "Offending node:\n";
14580 N->dumprFull(DAG); dbgs() << "\n";
14581 abort();
14582 }
14583
14584 for (const SDValue &Op : N->op_values())
14585 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14586
14587 Checked.insert(N);
14588 Visited.erase(N);
14589}
14590#endif
14591
14593 const llvm::SelectionDAG *DAG,
14594 bool force) {
14595#ifndef NDEBUG
14596 bool check = force;
14597#ifdef EXPENSIVE_CHECKS
14598 check = true;
14599#endif // EXPENSIVE_CHECKS
14600 if (check) {
14601 assert(N && "Checking nonexistent SDNode");
14604 checkForCyclesHelper(N, visited, checked, DAG);
14605 }
14606#endif // !NDEBUG
14607}
14608
14609void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14610 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14611}
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:1415
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1023
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1549
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1400
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1679
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1394
LLVM_ABI APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition APInt.cpp:639
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1044
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1521
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:936
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1339
APInt abs() const
Get the absolute value.
Definition APInt.h:1804
LLVM_ABI APInt sadd_sat(const APInt &RHS) const
Definition APInt.cpp:2037
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1183
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:259
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1677
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1497
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1112
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1655
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1405
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition APInt.cpp:1165
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition APInt.h:835
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1167
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1648
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1637
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1607
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition APInt.cpp:651
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sshl_sat(const APInt &RHS) const
Definition APInt.cpp:2097
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
Definition APInt.cpp:2111
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1052
LLVM_ABI APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition APInt.cpp:1152
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition APInt.cpp:397
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition APInt.h:1444
unsigned logBase2() const
Definition APInt.h:1770
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition APInt.cpp:2047
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:828
LLVM_ABI APInt 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:1151
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:996
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1376
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:874
LLVM_ABI APInt byteSwap() const
Definition APInt.cpp:746
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1258
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
static bool isSameValue(const APInt &I1, const APInt &I2)
Determine if two APInts have the same value, after zero-extending one of them (if needed!...
Definition APInt.h:554
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition APInt.h:1426
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1397
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:482
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1238
bool isOne() const
Determine if this is a value of 1.
Definition APInt.h:390
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition APInt.h:859
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:852
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1222
LLVM_ABI APInt ssub_sat(const APInt &RHS) const
Definition APInt.cpp:2056
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
unsigned getSrcAddressSpace() const
unsigned getDestAddressSpace() const
static Capacity get(size_t N)
Get the capacity of an array that can hold at least N elements.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
This is an SDNode representing atomic operations.
static LLVM_ABI BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
BitVector & reset()
Definition BitVector.h:411
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition BitVector.h:360
void clear()
clear - Removes all bits from the bitvector.
Definition BitVector.h:354
BitVector & set()
Definition BitVector.h:370
bool none() const
none - Returns true if none of the bits are set.
Definition BitVector.h:207
size_type size() const
size - Returns the number of bits in this bitvector.
Definition BitVector.h:178
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition Constants.h:904
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
LLVM_ABI bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &RawBitElements, BitVector &UndefElements) const
Extract the raw bit data from a build vector of Undef, Constant or ConstantFP node elements.
static LLVM_ABI void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &DstBitElements, ArrayRef< APInt > SrcBitElements, BitVector &DstUndefElements, const BitVector &SrcUndefElements)
Recast bit data SrcBitElements to DstEltSizeInBits wide elements.
LLVM_ABI bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
LLVM_ABI ConstantFPSDNode * getConstantFPSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant FP or null if this is not a constant FP splat.
LLVM_ABI std::optional< std::pair< APInt, APInt > > isConstantSequence() const
If this BuildVector is constant and represents 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:2277
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
Definition APInt.cpp:3184
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3020
LLVM_ABI APInt clmulh(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, and return high-bits.
Definition APInt.cpp:3217
APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition APInt.h:2282
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition APInt.cpp:3114
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition APInt.cpp:3124
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
ISD namespace - This namespace contains an enum which represents all of the SelectionDAG node types a...
Definition ISDOpcodes.h:24
LLVM_ABI CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical AND between different comparisons of identical values: ((X op1 Y) & (X...
LLVM_ABI bool isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are ~0 ...
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h: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)