LLVM 23.0.0git
SelectionDAG.cpp
Go to the documentation of this file.
1//===- SelectionDAG.cpp - Implement the SelectionDAG data structures ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements the SelectionDAG class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/Twine.h"
51#include "llvm/IR/Constant.h"
52#include "llvm/IR/Constants.h"
53#include "llvm/IR/DataLayout.h"
55#include "llvm/IR/DebugLoc.h"
57#include "llvm/IR/Function.h"
58#include "llvm/IR/GlobalValue.h"
59#include "llvm/IR/Metadata.h"
60#include "llvm/IR/Type.h"
64#include "llvm/Support/Debug.h"
73#include <algorithm>
74#include <cassert>
75#include <cstdint>
76#include <cstdlib>
77#include <limits>
78#include <optional>
79#include <string>
80#include <utility>
81#include <vector>
82
83using namespace llvm;
84using namespace llvm::SDPatternMatch;
85
86/// makeVTList - Return an instance of the SDVTList struct initialized with the
87/// specified members.
88static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
89 SDVTList Res = {VTs, NumVTs};
90 return Res;
91}
92
93// Default null implementations of the callbacks.
97
98void SelectionDAG::DAGNodeDeletedListener::anchor() {}
99void SelectionDAG::DAGNodeInsertedListener::anchor() {}
100
101#define DEBUG_TYPE "selectiondag"
102
103static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
104 cl::Hidden, cl::init(true),
105 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
106
107static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
108 cl::desc("Number limit for gluing ld/st of memcpy."),
109 cl::Hidden, cl::init(0));
110
112 MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192),
113 cl::desc("DAG combiner limit number of steps when searching DAG "
114 "for predecessor nodes"));
115
117 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
118}
119
121
122//===----------------------------------------------------------------------===//
123// ConstantFPSDNode Class
124//===----------------------------------------------------------------------===//
125
126/// isExactlyValue - We don't rely on operator== working on double values, as
127/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
128/// As such, this method can be used to do an exact bit-for-bit comparison of
129/// two floating point values.
131 return getValueAPF().bitwiseIsEqual(V);
132}
133
135 const APFloat& Val) {
136 assert(VT.isFloatingPoint() && "Can only convert between FP types");
137
138 // convert modifies in place, so make a copy.
139 APFloat Val2 = APFloat(Val);
140 bool losesInfo;
142 &losesInfo);
143 return !losesInfo;
144}
145
146//===----------------------------------------------------------------------===//
147// ISD Namespace
148//===----------------------------------------------------------------------===//
149
150bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
151 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
152 if (auto OptAPInt = N->getOperand(0)->bitcastToAPInt()) {
153 unsigned EltSize =
154 N->getValueType(0).getVectorElementType().getSizeInBits();
155 SplatVal = OptAPInt->trunc(EltSize);
156 return true;
157 }
158 }
159
160 auto *BV = dyn_cast<BuildVectorSDNode>(N);
161 if (!BV)
162 return false;
163
164 APInt SplatUndef;
165 unsigned SplatBitSize;
166 bool HasUndefs;
167 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
168 // Endianness does not matter here. We are checking for a splat given the
169 // element size of the vector, and if we find such a splat for little endian
170 // layout, then that should be valid also for big endian (as the full vector
171 // size is known to be a multiple of the element size).
172 const bool IsBigEndian = false;
173 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
174 EltSize, IsBigEndian) &&
175 EltSize == SplatBitSize;
176}
177
178// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
179// specializations of the more general isConstantSplatVector()?
180
181bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
182 // Look through a bit convert.
183 while (N->getOpcode() == ISD::BITCAST)
184 N = N->getOperand(0).getNode();
185
186 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
187 APInt SplatVal;
188 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
189 }
190
191 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
192
193 unsigned i = 0, e = N->getNumOperands();
194
195 // Skip over all of the undef values.
196 while (i != e && N->getOperand(i).isUndef())
197 ++i;
198
199 // Do not accept an all-undef vector.
200 if (i == e) return false;
201
202 // Do not accept build_vectors that aren't all constants or which have non-~0
203 // elements. We have to be a bit careful here, as the type of the constant
204 // may not be the same as the type of the vector elements due to type
205 // legalization (the elements are promoted to a legal type for the target and
206 // a vector of a type may be legal when the base element type is not).
207 // We only want to check enough bits to cover the vector elements, because
208 // we care if the resultant vector is all ones, not whether the individual
209 // constants are.
210 SDValue NotZero = N->getOperand(i);
211 if (auto OptAPInt = NotZero->bitcastToAPInt()) {
212 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
213 if (OptAPInt->countr_one() < EltSize)
214 return false;
215 } else
216 return false;
217
218 // Okay, we have at least one ~0 value, check to see if the rest match or are
219 // undefs. Even with the above element type twiddling, this should be OK, as
220 // the same type legalization should have applied to all the elements.
221 for (++i; i != e; ++i)
222 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
223 return false;
224 return true;
225}
226
227bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
228 // Look through a bit convert.
229 while (N->getOpcode() == ISD::BITCAST)
230 N = N->getOperand(0).getNode();
231
232 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
233 APInt SplatVal;
234 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
235 }
236
237 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
238
239 bool IsAllUndef = true;
240 for (const SDValue &Op : N->op_values()) {
241 if (Op.isUndef())
242 continue;
243 IsAllUndef = false;
244 // Do not accept build_vectors that aren't all constants or which have non-0
245 // elements. We have to be a bit careful here, as the type of the constant
246 // may not be the same as the type of the vector elements due to type
247 // legalization (the elements are promoted to a legal type for the target
248 // and a vector of a type may be legal when the base element type is not).
249 // We only want to check enough bits to cover the vector elements, because
250 // we care if the resultant vector is all zeros, not whether the individual
251 // constants are.
252 if (auto OptAPInt = Op->bitcastToAPInt()) {
253 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
254 if (OptAPInt->countr_zero() < EltSize)
255 return false;
256 } else
257 return false;
258 }
259
260 // Do not accept an all-undef vector.
261 if (IsAllUndef)
262 return false;
263 return true;
264}
265
267 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
268}
269
271 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
272}
273
275 if (N->getOpcode() != ISD::BUILD_VECTOR)
276 return false;
277
278 for (const SDValue &Op : N->op_values()) {
279 if (Op.isUndef())
280 continue;
282 return false;
283 }
284 return true;
285}
286
288 if (N->getOpcode() != ISD::BUILD_VECTOR)
289 return false;
290
291 for (const SDValue &Op : N->op_values()) {
292 if (Op.isUndef())
293 continue;
295 return false;
296 }
297 return true;
298}
299
300bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
301 bool Signed) {
302 assert(N->getValueType(0).isVector() && "Expected a vector!");
303
304 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
305 if (EltSize <= NewEltSize)
306 return false;
307
308 if (N->getOpcode() == ISD::ZERO_EXTEND) {
309 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
310 NewEltSize) &&
311 !Signed;
312 }
313 if (N->getOpcode() == ISD::SIGN_EXTEND) {
314 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
315 NewEltSize) &&
316 Signed;
317 }
318 if (N->getOpcode() != ISD::BUILD_VECTOR)
319 return false;
320
321 for (const SDValue &Op : N->op_values()) {
322 if (Op.isUndef())
323 continue;
325 return false;
326
327 APInt C = Op->getAsAPIntVal().trunc(EltSize);
328 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
329 return false;
330 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
331 return false;
332 }
333
334 return true;
335}
336
338 // Return false if the node has no operands.
339 // This is "logically inconsistent" with the definition of "all" but
340 // is probably the desired behavior.
341 if (N->getNumOperands() == 0)
342 return false;
343 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
344}
345
347 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
348}
349
350template <typename ConstNodeType>
352 std::function<bool(ConstNodeType *)> Match,
353 bool AllowUndefs, bool AllowTruncation) {
354 // FIXME: Add support for scalar UNDEF cases?
355 if (auto *C = dyn_cast<ConstNodeType>(Op))
356 return Match(C);
357
358 // FIXME: Add support for vector UNDEF cases?
359 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
360 ISD::SPLAT_VECTOR != Op.getOpcode())
361 return false;
362
363 EVT SVT = Op.getValueType().getScalarType();
364 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
365 if (AllowUndefs && Op.getOperand(i).isUndef()) {
366 if (!Match(nullptr))
367 return false;
368 continue;
369 }
370
371 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
372 if (!Cst || (!AllowTruncation && Cst->getValueType(0) != SVT) ||
373 !Match(Cst))
374 return false;
375 }
376 return true;
377}
378// Build used template types.
380 SDValue, std::function<bool(ConstantSDNode *)>, bool, bool);
382 SDValue, std::function<bool(ConstantFPSDNode *)>, bool, bool);
383
385 SDValue LHS, SDValue RHS,
386 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
387 bool AllowUndefs, bool AllowTypeMismatch) {
388 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
389 return false;
390
391 // TODO: Add support for scalar UNDEF cases?
392 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
393 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
394 return Match(LHSCst, RHSCst);
395
396 // TODO: Add support for vector UNDEF cases?
397 if (LHS.getOpcode() != RHS.getOpcode() ||
398 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
399 LHS.getOpcode() != ISD::SPLAT_VECTOR))
400 return false;
401
402 EVT SVT = LHS.getValueType().getScalarType();
403 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
404 SDValue LHSOp = LHS.getOperand(i);
405 SDValue RHSOp = RHS.getOperand(i);
406 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
407 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
408 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
409 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
410 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
411 return false;
412 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
413 LHSOp.getValueType() != RHSOp.getValueType()))
414 return false;
415 if (!Match(LHSCst, RHSCst))
416 return false;
417 }
418 return true;
419}
420
422 switch (MinMaxOpc) {
423 default:
424 llvm_unreachable("unrecognized opcode");
425 case ISD::UMIN:
426 return ISD::UMAX;
427 case ISD::UMAX:
428 return ISD::UMIN;
429 case ISD::SMIN:
430 return ISD::SMAX;
431 case ISD::SMAX:
432 return ISD::SMIN;
433 }
434}
435
437 switch (MinMaxOpc) {
438 default:
439 llvm_unreachable("unrecognized min/max opcode");
440 case ISD::SMIN:
441 return ISD::UMIN;
442 case ISD::SMAX:
443 return ISD::UMAX;
444 case ISD::UMIN:
445 return ISD::SMIN;
446 case ISD::UMAX:
447 return ISD::SMAX;
448 }
449}
450
452 switch (VecReduceOpcode) {
453 default:
454 llvm_unreachable("Expected VECREDUCE opcode");
457 case ISD::VP_REDUCE_FADD:
458 case ISD::VP_REDUCE_SEQ_FADD:
459 return ISD::FADD;
462 case ISD::VP_REDUCE_FMUL:
463 case ISD::VP_REDUCE_SEQ_FMUL:
464 return ISD::FMUL;
466 case ISD::VP_REDUCE_ADD:
467 return ISD::ADD;
469 case ISD::VP_REDUCE_MUL:
470 return ISD::MUL;
472 case ISD::VP_REDUCE_AND:
473 return ISD::AND;
475 case ISD::VP_REDUCE_OR:
476 return ISD::OR;
478 case ISD::VP_REDUCE_XOR:
479 return ISD::XOR;
481 case ISD::VP_REDUCE_SMAX:
482 return ISD::SMAX;
484 case ISD::VP_REDUCE_SMIN:
485 return ISD::SMIN;
487 case ISD::VP_REDUCE_UMAX:
488 return ISD::UMAX;
490 case ISD::VP_REDUCE_UMIN:
491 return ISD::UMIN;
493 case ISD::VP_REDUCE_FMAX:
494 return ISD::FMAXNUM;
496 case ISD::VP_REDUCE_FMIN:
497 return ISD::FMINNUM;
499 case ISD::VP_REDUCE_FMAXIMUM:
500 return ISD::FMAXIMUM;
502 case ISD::VP_REDUCE_FMINIMUM:
503 return ISD::FMINIMUM;
504 }
505}
506
507bool ISD::isVPOpcode(unsigned Opcode) {
508 switch (Opcode) {
509 default:
510 return false;
511#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
512 case ISD::VPSD: \
513 return true;
514#include "llvm/IR/VPIntrinsics.def"
515 }
516}
517
518bool ISD::isVPBinaryOp(unsigned Opcode) {
519 switch (Opcode) {
520 default:
521 break;
522#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
523#define VP_PROPERTY_BINARYOP return true;
524#define END_REGISTER_VP_SDNODE(VPSD) break;
525#include "llvm/IR/VPIntrinsics.def"
526 }
527 return false;
528}
529
530bool ISD::isVPReduction(unsigned Opcode) {
531 switch (Opcode) {
532 default:
533 return false;
534 case ISD::VP_REDUCE_ADD:
535 case ISD::VP_REDUCE_MUL:
536 case ISD::VP_REDUCE_AND:
537 case ISD::VP_REDUCE_OR:
538 case ISD::VP_REDUCE_XOR:
539 case ISD::VP_REDUCE_SMAX:
540 case ISD::VP_REDUCE_SMIN:
541 case ISD::VP_REDUCE_UMAX:
542 case ISD::VP_REDUCE_UMIN:
543 case ISD::VP_REDUCE_FMAX:
544 case ISD::VP_REDUCE_FMIN:
545 case ISD::VP_REDUCE_FMAXIMUM:
546 case ISD::VP_REDUCE_FMINIMUM:
547 case ISD::VP_REDUCE_FADD:
548 case ISD::VP_REDUCE_FMUL:
549 case ISD::VP_REDUCE_SEQ_FADD:
550 case ISD::VP_REDUCE_SEQ_FMUL:
551 return true;
552 }
553}
554
555/// The operand position of the vector mask.
556std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
557 switch (Opcode) {
558 default:
559 return std::nullopt;
560#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
561 case ISD::VPSD: \
562 return MASKPOS;
563#include "llvm/IR/VPIntrinsics.def"
564 }
565}
566
567/// The operand position of the explicit vector length parameter.
568std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
569 switch (Opcode) {
570 default:
571 return std::nullopt;
572#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
573 case ISD::VPSD: \
574 return EVLPOS;
575#include "llvm/IR/VPIntrinsics.def"
576 }
577}
578
579std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
580 bool hasFPExcept) {
581 // FIXME: Return strict opcodes in case of fp exceptions.
582 switch (VPOpcode) {
583 default:
584 return std::nullopt;
585#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
586#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
587#define END_REGISTER_VP_SDNODE(VPOPC) break;
588#include "llvm/IR/VPIntrinsics.def"
589 }
590 return std::nullopt;
591}
592
593std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
594 switch (Opcode) {
595 default:
596 return std::nullopt;
597#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
598#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
599#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
600#include "llvm/IR/VPIntrinsics.def"
601 }
602}
603
605 switch (ExtType) {
606 case ISD::EXTLOAD:
607 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
608 case ISD::SEXTLOAD:
609 return ISD::SIGN_EXTEND;
610 case ISD::ZEXTLOAD:
611 return ISD::ZERO_EXTEND;
612 default:
613 break;
614 }
615
616 llvm_unreachable("Invalid LoadExtType");
617}
618
620 // To perform this operation, we just need to swap the L and G bits of the
621 // operation.
622 unsigned OldL = (Operation >> 2) & 1;
623 unsigned OldG = (Operation >> 1) & 1;
624 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
625 (OldL << 1) | // New G bit
626 (OldG << 2)); // New L bit.
627}
628
630 unsigned Operation = Op;
631 if (isIntegerLike)
632 Operation ^= 7; // Flip L, G, E bits, but not U.
633 else
634 Operation ^= 15; // Flip all of the condition bits.
635
637 Operation &= ~8; // Don't let N and U bits get set.
638
639 return ISD::CondCode(Operation);
640}
641
645
647 bool isIntegerLike) {
648 return getSetCCInverseImpl(Op, isIntegerLike);
649}
650
651/// For an integer comparison, return 1 if the comparison is a signed operation
652/// and 2 if the result is an unsigned comparison. Return zero if the operation
653/// does not depend on the sign of the input (setne and seteq).
654static int isSignedOp(ISD::CondCode Opcode) {
655 switch (Opcode) {
656 default: llvm_unreachable("Illegal integer setcc operation!");
657 case ISD::SETEQ:
658 case ISD::SETNE: return 0;
659 case ISD::SETLT:
660 case ISD::SETLE:
661 case ISD::SETGT:
662 case ISD::SETGE: return 1;
663 case ISD::SETULT:
664 case ISD::SETULE:
665 case ISD::SETUGT:
666 case ISD::SETUGE: return 2;
667 }
668}
669
671 EVT Type) {
672 bool IsInteger = Type.isInteger();
673 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
674 // Cannot fold a signed integer setcc with an unsigned integer setcc.
675 return ISD::SETCC_INVALID;
676
677 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
678
679 // If the N and U bits get set, then the resultant comparison DOES suddenly
680 // care about orderedness, and it is true when ordered.
681 if (Op > ISD::SETTRUE2)
682 Op &= ~16; // Clear the U bit if the N bit is set.
683
684 // Canonicalize illegal integer setcc's.
685 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
686 Op = ISD::SETNE;
687
688 return ISD::CondCode(Op);
689}
690
692 EVT Type) {
693 bool IsInteger = Type.isInteger();
694 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
695 // Cannot fold a signed setcc with an unsigned setcc.
696 return ISD::SETCC_INVALID;
697
698 // Combine all of the condition bits.
699 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
700
701 // Canonicalize illegal integer setcc's.
702 if (IsInteger) {
703 switch (Result) {
704 default: break;
705 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
706 case ISD::SETOEQ: // SETEQ & SETU[LG]E
707 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
708 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
709 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
710 }
711 }
712
713 return Result;
714}
715
716//===----------------------------------------------------------------------===//
717// SDNode Profile Support
718//===----------------------------------------------------------------------===//
719
720/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
721static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
722 ID.AddInteger(OpC);
723}
724
725/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
726/// solely with their pointer.
728 ID.AddPointer(VTList.VTs);
729}
730
731/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
734 for (const auto &Op : Ops) {
735 ID.AddPointer(Op.getNode());
736 ID.AddInteger(Op.getResNo());
737 }
738}
739
740/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
743 for (const auto &Op : Ops) {
744 ID.AddPointer(Op.getNode());
745 ID.AddInteger(Op.getResNo());
746 }
747}
748
749static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
750 SDVTList VTList, ArrayRef<SDValue> OpList) {
751 AddNodeIDOpcode(ID, OpC);
752 AddNodeIDValueTypes(ID, VTList);
753 AddNodeIDOperands(ID, OpList);
754}
755
756/// If this is an SDNode with special info, add this info to the NodeID data.
758 switch (N->getOpcode()) {
761 case ISD::MCSymbol:
762 llvm_unreachable("Should only be used on nodes with operands");
763 default: break; // Normal nodes don't need extra info.
765 case ISD::Constant: {
767 ID.AddPointer(C->getConstantIntValue());
768 ID.AddBoolean(C->isOpaque());
769 break;
770 }
772 case ISD::ConstantFP:
773 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
774 break;
780 ID.AddPointer(GA->getGlobal());
781 ID.AddInteger(GA->getOffset());
782 ID.AddInteger(GA->getTargetFlags());
783 break;
784 }
785 case ISD::BasicBlock:
787 break;
788 case ISD::Register:
789 ID.AddInteger(cast<RegisterSDNode>(N)->getReg().id());
790 break;
792 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
793 break;
794 case ISD::SRCVALUE:
795 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
796 break;
797 case ISD::FrameIndex:
799 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
800 break;
802 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
803 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
804 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
805 break;
806 case ISD::JumpTable:
808 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
809 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
810 break;
814 ID.AddInteger(CP->getAlign().value());
815 ID.AddInteger(CP->getOffset());
818 else
819 ID.AddPointer(CP->getConstVal());
820 ID.AddInteger(CP->getTargetFlags());
821 break;
822 }
823 case ISD::TargetIndex: {
825 ID.AddInteger(TI->getIndex());
826 ID.AddInteger(TI->getOffset());
827 ID.AddInteger(TI->getTargetFlags());
828 break;
829 }
830 case ISD::LOAD: {
831 const LoadSDNode *LD = cast<LoadSDNode>(N);
832 ID.AddInteger(LD->getMemoryVT().getRawBits());
833 ID.AddInteger(LD->getRawSubclassData());
834 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
835 ID.AddInteger(LD->getMemOperand()->getFlags());
836 break;
837 }
838 case ISD::STORE: {
839 const StoreSDNode *ST = cast<StoreSDNode>(N);
840 ID.AddInteger(ST->getMemoryVT().getRawBits());
841 ID.AddInteger(ST->getRawSubclassData());
842 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
843 ID.AddInteger(ST->getMemOperand()->getFlags());
844 break;
845 }
846 case ISD::VP_LOAD: {
847 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
848 ID.AddInteger(ELD->getMemoryVT().getRawBits());
849 ID.AddInteger(ELD->getRawSubclassData());
850 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
851 ID.AddInteger(ELD->getMemOperand()->getFlags());
852 break;
853 }
854 case ISD::VP_LOAD_FF: {
855 const auto *LD = cast<VPLoadFFSDNode>(N);
856 ID.AddInteger(LD->getMemoryVT().getRawBits());
857 ID.AddInteger(LD->getRawSubclassData());
858 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
859 ID.AddInteger(LD->getMemOperand()->getFlags());
860 break;
861 }
862 case ISD::VP_STORE: {
863 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
864 ID.AddInteger(EST->getMemoryVT().getRawBits());
865 ID.AddInteger(EST->getRawSubclassData());
866 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
867 ID.AddInteger(EST->getMemOperand()->getFlags());
868 break;
869 }
870 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
872 ID.AddInteger(SLD->getMemoryVT().getRawBits());
873 ID.AddInteger(SLD->getRawSubclassData());
874 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
875 break;
876 }
877 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
879 ID.AddInteger(SST->getMemoryVT().getRawBits());
880 ID.AddInteger(SST->getRawSubclassData());
881 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
882 break;
883 }
884 case ISD::VP_GATHER: {
886 ID.AddInteger(EG->getMemoryVT().getRawBits());
887 ID.AddInteger(EG->getRawSubclassData());
888 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
889 ID.AddInteger(EG->getMemOperand()->getFlags());
890 break;
891 }
892 case ISD::VP_SCATTER: {
894 ID.AddInteger(ES->getMemoryVT().getRawBits());
895 ID.AddInteger(ES->getRawSubclassData());
896 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
897 ID.AddInteger(ES->getMemOperand()->getFlags());
898 break;
899 }
900 case ISD::MLOAD: {
902 ID.AddInteger(MLD->getMemoryVT().getRawBits());
903 ID.AddInteger(MLD->getRawSubclassData());
904 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
905 ID.AddInteger(MLD->getMemOperand()->getFlags());
906 break;
907 }
908 case ISD::MSTORE: {
910 ID.AddInteger(MST->getMemoryVT().getRawBits());
911 ID.AddInteger(MST->getRawSubclassData());
912 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
913 ID.AddInteger(MST->getMemOperand()->getFlags());
914 break;
915 }
916 case ISD::MGATHER: {
918 ID.AddInteger(MG->getMemoryVT().getRawBits());
919 ID.AddInteger(MG->getRawSubclassData());
920 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
921 ID.AddInteger(MG->getMemOperand()->getFlags());
922 break;
923 }
924 case ISD::MSCATTER: {
926 ID.AddInteger(MS->getMemoryVT().getRawBits());
927 ID.AddInteger(MS->getRawSubclassData());
928 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
929 ID.AddInteger(MS->getMemOperand()->getFlags());
930 break;
931 }
934 case ISD::ATOMIC_SWAP:
946 case ISD::ATOMIC_LOAD:
947 case ISD::ATOMIC_STORE: {
948 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
949 ID.AddInteger(AT->getMemoryVT().getRawBits());
950 ID.AddInteger(AT->getRawSubclassData());
951 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
952 ID.AddInteger(AT->getMemOperand()->getFlags());
953 break;
954 }
955 case ISD::VECTOR_SHUFFLE: {
956 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
957 for (int M : Mask)
958 ID.AddInteger(M);
959 break;
960 }
961 case ISD::ADDRSPACECAST: {
963 ID.AddInteger(ASC->getSrcAddressSpace());
964 ID.AddInteger(ASC->getDestAddressSpace());
965 break;
966 }
968 case ISD::BlockAddress: {
970 ID.AddPointer(BA->getBlockAddress());
971 ID.AddInteger(BA->getOffset());
972 ID.AddInteger(BA->getTargetFlags());
973 break;
974 }
975 case ISD::AssertAlign:
976 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
977 break;
978 case ISD::PREFETCH:
981 // Handled by MemIntrinsicSDNode check after the switch.
982 break;
984 ID.AddPointer(cast<MDNodeSDNode>(N)->getMD());
985 break;
986 } // end switch (N->getOpcode())
987
988 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
989 // to check.
990 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
991 ID.AddInteger(MN->getRawSubclassData());
992 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
993 ID.AddInteger(MN->getMemOperand()->getFlags());
994 ID.AddInteger(MN->getMemoryVT().getRawBits());
995 }
996}
997
998/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
999/// data.
1001 AddNodeIDOpcode(ID, N->getOpcode());
1002 // Add the return value info.
1003 AddNodeIDValueTypes(ID, N->getVTList());
1004 // Add the operand info.
1005 AddNodeIDOperands(ID, N->ops());
1006
1007 // Handle SDNode leafs with special info.
1009}
1010
1011//===----------------------------------------------------------------------===//
1012// SelectionDAG Class
1013//===----------------------------------------------------------------------===//
1014
1015/// doNotCSE - Return true if CSE should not be performed for this node.
1016static bool doNotCSE(SDNode *N) {
1017 if (N->getValueType(0) == MVT::Glue)
1018 return true; // Never CSE anything that produces a glue result.
1019
1020 switch (N->getOpcode()) {
1021 default: break;
1022 case ISD::HANDLENODE:
1023 case ISD::EH_LABEL:
1024 return true; // Never CSE these nodes.
1025 }
1026
1027 // Check that remaining values produced are not flags.
1028 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1029 if (N->getValueType(i) == MVT::Glue)
1030 return true; // Never CSE anything that produces a glue result.
1031
1032 return false;
1033}
1034
1035/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1036/// SelectionDAG.
1038 // Create a dummy node (which is not added to allnodes), that adds a reference
1039 // to the root node, preventing it from being deleted.
1040 HandleSDNode Dummy(getRoot());
1041
1042 SmallVector<SDNode*, 128> DeadNodes;
1043
1044 // Add all obviously-dead nodes to the DeadNodes worklist.
1045 for (SDNode &Node : allnodes())
1046 if (Node.use_empty())
1047 DeadNodes.push_back(&Node);
1048
1049 RemoveDeadNodes(DeadNodes);
1050
1051 // If the root changed (e.g. it was a dead load, update the root).
1052 setRoot(Dummy.getValue());
1053}
1054
1055/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1056/// given list, and any nodes that become unreachable as a result.
1058
1059 // Process the worklist, deleting the nodes and adding their uses to the
1060 // worklist.
1061 while (!DeadNodes.empty()) {
1062 SDNode *N = DeadNodes.pop_back_val();
1063 // Skip to next node if we've already managed to delete the node. This could
1064 // happen if replacing a node causes a node previously added to the node to
1065 // be deleted.
1066 if (N->getOpcode() == ISD::DELETED_NODE)
1067 continue;
1068
1069 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1070 DUL->NodeDeleted(N, nullptr);
1071
1072 // Take the node out of the appropriate CSE map.
1073 RemoveNodeFromCSEMaps(N);
1074
1075 // Next, brutally remove the operand list. This is safe to do, as there are
1076 // no cycles in the graph.
1077 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1078 SDUse &Use = *I++;
1079 SDNode *Operand = Use.getNode();
1080 Use.set(SDValue());
1081
1082 // Now that we removed this operand, see if there are no uses of it left.
1083 if (Operand->use_empty())
1084 DeadNodes.push_back(Operand);
1085 }
1086
1087 DeallocateNode(N);
1088 }
1089}
1090
1092 SmallVector<SDNode*, 16> DeadNodes(1, N);
1093
1094 // Create a dummy node that adds a reference to the root node, preventing
1095 // it from being deleted. (This matters if the root is an operand of the
1096 // dead node.)
1097 HandleSDNode Dummy(getRoot());
1098
1099 RemoveDeadNodes(DeadNodes);
1100}
1101
1103 // First take this out of the appropriate CSE map.
1104 RemoveNodeFromCSEMaps(N);
1105
1106 // Finally, remove uses due to operands of this node, remove from the
1107 // AllNodes list, and delete the node.
1108 DeleteNodeNotInCSEMaps(N);
1109}
1110
1111void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1112 assert(N->getIterator() != AllNodes.begin() &&
1113 "Cannot delete the entry node!");
1114 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1115
1116 // Drop all of the operands and decrement used node's use counts.
1117 N->DropOperands();
1118
1119 DeallocateNode(N);
1120}
1121
1122void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1123 assert(!(V->isVariadic() && isParameter));
1124 if (isParameter)
1125 ByvalParmDbgValues.push_back(V);
1126 else
1127 DbgValues.push_back(V);
1128 for (const SDNode *Node : V->getSDNodes())
1129 if (Node)
1130 DbgValMap[Node].push_back(V);
1131}
1132
1134 DbgValMapType::iterator I = DbgValMap.find(Node);
1135 if (I == DbgValMap.end())
1136 return;
1137 for (auto &Val: I->second)
1138 Val->setIsInvalidated();
1139 DbgValMap.erase(I);
1140}
1141
1142void SelectionDAG::DeallocateNode(SDNode *N) {
1143 // If we have operands, deallocate them.
1145
1146 NodeAllocator.Deallocate(AllNodes.remove(N));
1147
1148 // Set the opcode to DELETED_NODE to help catch bugs when node
1149 // memory is reallocated.
1150 // FIXME: There are places in SDag that have grown a dependency on the opcode
1151 // value in the released node.
1152 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1153 N->NodeType = ISD::DELETED_NODE;
1154
1155 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1156 // them and forget about that node.
1157 DbgInfo->erase(N);
1158
1159 // Invalidate extra info.
1160 SDEI.erase(N);
1161}
1162
1163#ifndef NDEBUG
1164/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1165void SelectionDAG::verifyNode(SDNode *N) const {
1166 switch (N->getOpcode()) {
1167 default:
1168 if (N->isTargetOpcode())
1170 break;
1171 case ISD::BUILD_PAIR: {
1172 EVT VT = N->getValueType(0);
1173 assert(N->getNumValues() == 1 && "Too many results!");
1174 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1175 "Wrong return type!");
1176 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1177 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1178 "Mismatched operand types!");
1179 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1180 "Wrong operand type!");
1181 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1182 "Wrong return type size");
1183 break;
1184 }
1185 case ISD::BUILD_VECTOR: {
1186 assert(N->getNumValues() == 1 && "Too many results!");
1187 assert(N->getValueType(0).isVector() && "Wrong return type!");
1188 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1189 "Wrong number of operands!");
1190 EVT EltVT = N->getValueType(0).getVectorElementType();
1191 for (const SDUse &Op : N->ops()) {
1192 assert((Op.getValueType() == EltVT ||
1193 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1194 EltVT.bitsLE(Op.getValueType()))) &&
1195 "Wrong operand type!");
1196 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1197 "Operands must all have the same type");
1198 }
1199 break;
1200 }
1201 }
1202}
1203#endif // NDEBUG
1204
1205/// Insert a newly allocated node into the DAG.
1206///
1207/// Handles insertion into the all nodes list and CSE map, as well as
1208/// verification and other common operations when a new node is allocated.
1209void SelectionDAG::InsertNode(SDNode *N) {
1210 AllNodes.push_back(N);
1211#ifndef NDEBUG
1212 N->PersistentId = NextPersistentId++;
1213 verifyNode(N);
1214#endif
1215 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1216 DUL->NodeInserted(N);
1217}
1218
1219/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1220/// correspond to it. This is useful when we're about to delete or repurpose
1221/// the node. We don't want future request for structurally identical nodes
1222/// to return N anymore.
1223bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1224 bool Erased = false;
1225 switch (N->getOpcode()) {
1226 case ISD::HANDLENODE: return false; // noop.
1227 case ISD::CONDCODE:
1228 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1229 "Cond code doesn't exist!");
1230 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1231 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1232 break;
1234 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1235 break;
1237 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1238 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1239 ESN->getSymbol(), ESN->getTargetFlags()));
1240 break;
1241 }
1242 case ISD::MCSymbol: {
1243 auto *MCSN = cast<MCSymbolSDNode>(N);
1244 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1245 break;
1246 }
1247 case ISD::VALUETYPE: {
1248 EVT VT = cast<VTSDNode>(N)->getVT();
1249 if (VT.isExtended()) {
1250 Erased = ExtendedValueTypeNodes.erase(VT);
1251 } else {
1252 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1253 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1254 }
1255 break;
1256 }
1257 default:
1258 // Remove it from the CSE Map.
1259 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1260 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1261 Erased = CSEMap.RemoveNode(N);
1262 break;
1263 }
1264#ifndef NDEBUG
1265 // Verify that the node was actually in one of the CSE maps, unless it has a
1266 // glue result (which cannot be CSE'd) or is one of the special cases that are
1267 // not subject to CSE.
1268 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1269 !N->isMachineOpcode() && !doNotCSE(N)) {
1270 N->dump(this);
1271 dbgs() << "\n";
1272 llvm_unreachable("Node is not in map!");
1273 }
1274#endif
1275 return Erased;
1276}
1277
1278/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1279/// maps and modified in place. Add it back to the CSE maps, unless an identical
1280/// node already exists, in which case transfer all its users to the existing
1281/// node. This transfer can potentially trigger recursive merging.
1282void
1283SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1284 // For node types that aren't CSE'd, just act as if no identical node
1285 // already exists.
1286 if (!doNotCSE(N)) {
1287 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1288 if (Existing != N) {
1289 // If there was already an existing matching node, use ReplaceAllUsesWith
1290 // to replace the dead one with the existing one. This can cause
1291 // recursive merging of other unrelated nodes down the line.
1292 Existing->intersectFlagsWith(N->getFlags());
1293 if (auto *MemNode = dyn_cast<MemSDNode>(Existing))
1294 MemNode->refineRanges(cast<MemSDNode>(N)->getMemOperand());
1295 ReplaceAllUsesWith(N, Existing);
1296
1297 // N is now dead. Inform the listeners and delete it.
1298 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1299 DUL->NodeDeleted(N, Existing);
1300 DeleteNodeNotInCSEMaps(N);
1301 return;
1302 }
1303 }
1304
1305 // If the node doesn't already exist, we updated it. Inform listeners.
1306 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1307 DUL->NodeUpdated(N);
1308}
1309
1310/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1311/// were replaced with those specified. If this node is never memoized,
1312/// return null, otherwise return a pointer to the slot it would take. If a
1313/// node already exists with these operands, the slot will be non-null.
1314SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1315 void *&InsertPos) {
1316 if (doNotCSE(N))
1317 return nullptr;
1318
1319 SDValue Ops[] = { Op };
1320 FoldingSetNodeID ID;
1321 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1323 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1324 if (Node)
1325 Node->intersectFlagsWith(N->getFlags());
1326 return Node;
1327}
1328
1329/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1330/// were replaced with those specified. If this node is never memoized,
1331/// return null, otherwise return a pointer to the slot it would take. If a
1332/// node already exists with these operands, the slot will be non-null.
1333SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1334 SDValue Op1, SDValue Op2,
1335 void *&InsertPos) {
1336 if (doNotCSE(N))
1337 return nullptr;
1338
1339 SDValue Ops[] = { Op1, Op2 };
1340 FoldingSetNodeID ID;
1341 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1343 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1344 if (Node)
1345 Node->intersectFlagsWith(N->getFlags());
1346 return Node;
1347}
1348
1349/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1350/// were replaced with those specified. If this node is never memoized,
1351/// return null, otherwise return a pointer to the slot it would take. If a
1352/// node already exists with these operands, the slot will be non-null.
1353SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1354 void *&InsertPos) {
1355 if (doNotCSE(N))
1356 return nullptr;
1357
1358 FoldingSetNodeID ID;
1359 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1361 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1362 if (Node)
1363 Node->intersectFlagsWith(N->getFlags());
1364 return Node;
1365}
1366
1368 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1369 : VT.getTypeForEVT(*getContext());
1370
1371 return getDataLayout().getABITypeAlign(Ty);
1372}
1373
1374// EntryNode could meaningfully have debug info if we can find it...
1376 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1377 getVTList(MVT::Other, MVT::Glue)),
1378 Root(getEntryNode()) {
1379 InsertNode(&EntryNode);
1380 DbgInfo = new SDDbgInfo();
1381}
1382
1384 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1385 const TargetLibraryInfo *LibraryInfo,
1386 const LibcallLoweringInfo *LibcallsInfo,
1387 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1389 FunctionVarLocs const *VarLocs) {
1390 MF = &NewMF;
1391 SDAGISelPass = PassPtr;
1392 ORE = &NewORE;
1395 LibInfo = LibraryInfo;
1396 Libcalls = LibcallsInfo;
1397 Context = &MF->getFunction().getContext();
1398 UA = NewUA;
1399 PSI = PSIin;
1400 BFI = BFIin;
1401 MMI = &MMIin;
1402 FnVarLocs = VarLocs;
1403}
1404
1406 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1407 allnodes_clear();
1408 OperandRecycler.clear(OperandAllocator);
1409 delete DbgInfo;
1410}
1411
1413 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1414}
1415
1416void SelectionDAG::allnodes_clear() {
1417 assert(&*AllNodes.begin() == &EntryNode);
1418 AllNodes.remove(AllNodes.begin());
1419 while (!AllNodes.empty())
1420 DeallocateNode(&AllNodes.front());
1421#ifndef NDEBUG
1422 NextPersistentId = 0;
1423#endif
1424}
1425
1426SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1427 void *&InsertPos) {
1428 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1429 if (N) {
1430 switch (N->getOpcode()) {
1431 default: break;
1432 case ISD::Constant:
1433 case ISD::ConstantFP:
1434 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1435 "debug location. Use another overload.");
1436 }
1437 }
1438 return N;
1439}
1440
1441SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1442 const SDLoc &DL, void *&InsertPos) {
1443 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1444 if (N) {
1445 switch (N->getOpcode()) {
1446 case ISD::Constant:
1447 case ISD::ConstantFP:
1448 // Erase debug location from the node if the node is used at several
1449 // different places. Do not propagate one location to all uses as it
1450 // will cause a worse single stepping debugging experience.
1451 if (N->getDebugLoc() != DL.getDebugLoc())
1452 N->setDebugLoc(DebugLoc());
1453 break;
1454 default:
1455 // When the node's point of use is located earlier in the instruction
1456 // sequence than its prior point of use, update its debug info to the
1457 // earlier location.
1458 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1459 N->setDebugLoc(DL.getDebugLoc());
1460 break;
1461 }
1462 }
1463 return N;
1464}
1465
1467 allnodes_clear();
1468 OperandRecycler.clear(OperandAllocator);
1469 OperandAllocator.Reset();
1470 CSEMap.clear();
1471
1472 ExtendedValueTypeNodes.clear();
1473 ExternalSymbols.clear();
1474 TargetExternalSymbols.clear();
1475 MCSymbols.clear();
1476 SDEI.clear();
1477 llvm::fill(CondCodeNodes, nullptr);
1478 llvm::fill(ValueTypeNodes, nullptr);
1479
1480 EntryNode.UseList = nullptr;
1481 InsertNode(&EntryNode);
1482 Root = getEntryNode();
1483 DbgInfo->clear();
1484}
1485
1487 return VT.bitsGT(Op.getValueType())
1488 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1489 : getNode(ISD::FP_ROUND, DL, VT, Op,
1490 getIntPtrConstant(0, DL, /*isTarget=*/true));
1491}
1492
1493std::pair<SDValue, SDValue>
1495 const SDLoc &DL, EVT VT) {
1496 assert(!VT.bitsEq(Op.getValueType()) &&
1497 "Strict no-op FP extend/round not allowed.");
1498 SDValue Res =
1499 VT.bitsGT(Op.getValueType())
1500 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1501 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1502 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1503
1504 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1505}
1506
1508 return VT.bitsGT(Op.getValueType()) ?
1509 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1510 getNode(ISD::TRUNCATE, DL, VT, Op);
1511}
1512
1514 return VT.bitsGT(Op.getValueType()) ?
1515 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1516 getNode(ISD::TRUNCATE, DL, VT, Op);
1517}
1518
1520 return VT.bitsGT(Op.getValueType()) ?
1521 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1522 getNode(ISD::TRUNCATE, DL, VT, Op);
1523}
1524
1526 EVT VT) {
1527 assert(!VT.isVector());
1528 auto Type = Op.getValueType();
1529 SDValue DestOp;
1530 if (Type == VT)
1531 return Op;
1532 auto Size = Op.getValueSizeInBits();
1533 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1534 if (DestOp.getValueType() == VT)
1535 return DestOp;
1536
1537 return getAnyExtOrTrunc(DestOp, DL, VT);
1538}
1539
1541 EVT VT) {
1542 assert(!VT.isVector());
1543 auto Type = Op.getValueType();
1544 SDValue DestOp;
1545 if (Type == VT)
1546 return Op;
1547 auto Size = Op.getValueSizeInBits();
1548 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1549 if (DestOp.getValueType() == VT)
1550 return DestOp;
1551
1552 return getSExtOrTrunc(DestOp, DL, VT);
1553}
1554
1556 EVT VT) {
1557 assert(!VT.isVector());
1558 auto Type = Op.getValueType();
1559 SDValue DestOp;
1560 if (Type == VT)
1561 return Op;
1562 auto Size = Op.getValueSizeInBits();
1563 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1564 if (DestOp.getValueType() == VT)
1565 return DestOp;
1566
1567 return getZExtOrTrunc(DestOp, DL, VT);
1568}
1569
1571 EVT OpVT) {
1572 if (VT.bitsLE(Op.getValueType()))
1573 return getNode(ISD::TRUNCATE, SL, VT, Op);
1574
1575 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1576 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1577}
1578
1580 EVT OpVT = Op.getValueType();
1581 assert(VT.isInteger() && OpVT.isInteger() &&
1582 "Cannot getZeroExtendInReg FP types");
1583 assert(VT.isVector() == OpVT.isVector() &&
1584 "getZeroExtendInReg type should be vector iff the operand "
1585 "type is vector!");
1586 assert((!VT.isVector() ||
1588 "Vector element counts must match in getZeroExtendInReg");
1589 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1590 if (OpVT == VT)
1591 return Op;
1592 // TODO: Use computeKnownBits instead of AssertZext.
1593 if (Op.getOpcode() == ISD::AssertZext && cast<VTSDNode>(Op.getOperand(1))
1594 ->getVT()
1595 .getScalarType()
1596 .bitsLE(VT.getScalarType()))
1597 return Op;
1599 VT.getScalarSizeInBits());
1600 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1601}
1602
1604 SDValue EVL, const SDLoc &DL,
1605 EVT VT) {
1606 EVT OpVT = Op.getValueType();
1607 assert(VT.isInteger() && OpVT.isInteger() &&
1608 "Cannot getVPZeroExtendInReg FP types");
1609 assert(VT.isVector() && OpVT.isVector() &&
1610 "getVPZeroExtendInReg type and operand type should be vector!");
1612 "Vector element counts must match in getZeroExtendInReg");
1613 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1614 if (OpVT == VT)
1615 return Op;
1617 VT.getScalarSizeInBits());
1618 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1619 EVL);
1620}
1621
1623 // Only unsigned pointer semantics are supported right now. In the future this
1624 // might delegate to TLI to check pointer signedness.
1625 return getZExtOrTrunc(Op, DL, VT);
1626}
1627
1629 // Only unsigned pointer semantics are supported right now. In the future this
1630 // might delegate to TLI to check pointer signedness.
1631 return getZeroExtendInReg(Op, DL, VT);
1632}
1633
1635 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1636}
1637
1638/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1640 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1641}
1642
1644 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1645 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1646}
1647
1649 SDValue Mask, SDValue EVL, EVT VT) {
1650 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1651 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1652}
1653
1655 SDValue Mask, SDValue EVL) {
1656 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1657}
1658
1660 SDValue Mask, SDValue EVL) {
1661 if (VT.bitsGT(Op.getValueType()))
1662 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1663 if (VT.bitsLT(Op.getValueType()))
1664 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1665 return Op;
1666}
1667
1669 EVT OpVT) {
1670 if (!V)
1671 return getConstant(0, DL, VT);
1672
1673 switch (TLI->getBooleanContents(OpVT)) {
1676 return getConstant(1, DL, VT);
1678 return getAllOnesConstant(DL, VT);
1679 }
1680 llvm_unreachable("Unexpected boolean content enum!");
1681}
1682
1684 bool isT, bool isO) {
1685 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1686 DL, VT, isT, isO);
1687}
1688
1690 bool isT, bool isO) {
1691 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1692}
1693
1695 EVT VT, bool isT, bool isO) {
1696 assert(VT.isInteger() && "Cannot create FP integer constant!");
1697
1698 EVT EltVT = VT.getScalarType();
1699 const ConstantInt *Elt = &Val;
1700
1701 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1702 // to-be-splatted scalar ConstantInt.
1703 if (isa<VectorType>(Elt->getType()))
1704 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1705
1706 // In some cases the vector type is legal but the element type is illegal and
1707 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1708 // inserted value (the type does not need to match the vector element type).
1709 // Any extra bits introduced will be truncated away.
1710 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1712 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1713 APInt NewVal;
1714 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1715 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1716 else
1717 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1718 Elt = ConstantInt::get(*getContext(), NewVal);
1719 }
1720 // In other cases the element type is illegal and needs to be expanded, for
1721 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1722 // the value into n parts and use a vector type with n-times the elements.
1723 // Then bitcast to the type requested.
1724 // Legalizing constants too early makes the DAGCombiner's job harder so we
1725 // only legalize if the DAG tells us we must produce legal types.
1726 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1727 TLI->getTypeAction(*getContext(), EltVT) ==
1729 const APInt &NewVal = Elt->getValue();
1730 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1731 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1732
1733 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1734 if (VT.isScalableVector() ||
1735 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1736 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1737 "Can only handle an even split!");
1738 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1739
1740 SmallVector<SDValue, 2> ScalarParts;
1741 for (unsigned i = 0; i != Parts; ++i)
1742 ScalarParts.push_back(getConstant(
1743 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1744 ViaEltVT, isT, isO));
1745
1746 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1747 }
1748
1749 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1750 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1751
1752 // Check the temporary vector is the correct size. If this fails then
1753 // getTypeToTransformTo() probably returned a type whose size (in bits)
1754 // isn't a power-of-2 factor of the requested type size.
1755 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1756
1757 SmallVector<SDValue, 2> EltParts;
1758 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1759 EltParts.push_back(getConstant(
1760 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1761 ViaEltVT, isT, isO));
1762
1763 // EltParts is currently in little endian order. If we actually want
1764 // big-endian order then reverse it now.
1765 if (getDataLayout().isBigEndian())
1766 std::reverse(EltParts.begin(), EltParts.end());
1767
1768 // The elements must be reversed when the element order is different
1769 // to the endianness of the elements (because the BITCAST is itself a
1770 // vector shuffle in this situation). However, we do not need any code to
1771 // perform this reversal because getConstant() is producing a vector
1772 // splat.
1773 // This situation occurs in MIPS MSA.
1774
1776 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1777 llvm::append_range(Ops, EltParts);
1778
1779 SDValue V =
1780 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1781 return V;
1782 }
1783
1784 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1785 "APInt size does not match type size!");
1786 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1787 SDVTList VTs = getVTList(EltVT);
1789 AddNodeIDNode(ID, Opc, VTs, {});
1790 ID.AddPointer(Elt);
1791 ID.AddBoolean(isO);
1792 void *IP = nullptr;
1793 SDNode *N = nullptr;
1794 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1795 if (!VT.isVector())
1796 return SDValue(N, 0);
1797
1798 if (!N) {
1799 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1800 CSEMap.InsertNode(N, IP);
1801 InsertNode(N);
1802 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1803 }
1804
1805 SDValue Result(N, 0);
1806 if (VT.isVector())
1807 Result = getSplat(VT, DL, Result);
1808 return Result;
1809}
1810
1812 bool isT, bool isO) {
1813 unsigned Size = VT.getScalarSizeInBits();
1814 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1815}
1816
1818 bool IsOpaque) {
1820 IsTarget, IsOpaque);
1821}
1822
1824 bool isTarget) {
1825 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1826}
1827
1829 const SDLoc &DL) {
1830 assert(VT.isInteger() && "Shift amount is not an integer type!");
1831 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1832 return getConstant(Val, DL, ShiftVT);
1833}
1834
1836 const SDLoc &DL) {
1837 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1838 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1839}
1840
1842 bool isTarget) {
1843 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1844}
1845
1847 bool isTarget) {
1848 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1849}
1850
1852 EVT VT, bool isTarget) {
1853 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1854
1855 EVT EltVT = VT.getScalarType();
1856 const ConstantFP *Elt = &V;
1857
1858 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1859 // the to-be-splatted scalar ConstantFP.
1860 if (isa<VectorType>(Elt->getType()))
1861 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1862
1863 // Do the map lookup using the actual bit pattern for the floating point
1864 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1865 // we don't have issues with SNANs.
1866 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1867 SDVTList VTs = getVTList(EltVT);
1869 AddNodeIDNode(ID, Opc, VTs, {});
1870 ID.AddPointer(Elt);
1871 void *IP = nullptr;
1872 SDNode *N = nullptr;
1873 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1874 if (!VT.isVector())
1875 return SDValue(N, 0);
1876
1877 if (!N) {
1878 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1879 CSEMap.InsertNode(N, IP);
1880 InsertNode(N);
1881 }
1882
1883 SDValue Result(N, 0);
1884 if (VT.isVector())
1885 Result = getSplat(VT, DL, Result);
1886 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1887 return Result;
1888}
1889
1891 bool isTarget) {
1892 EVT EltVT = VT.getScalarType();
1893 if (EltVT == MVT::f32)
1894 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1895 if (EltVT == MVT::f64)
1896 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1897 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1898 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1899 bool Ignored;
1900 APFloat APF = APFloat(Val);
1902 &Ignored);
1903 return getConstantFP(APF, DL, VT, isTarget);
1904 }
1905 llvm_unreachable("Unsupported type in getConstantFP");
1906}
1907
1909 EVT VT, int64_t Offset, bool isTargetGA,
1910 unsigned TargetFlags) {
1911 assert((TargetFlags == 0 || isTargetGA) &&
1912 "Cannot set target flags on target-independent globals");
1913
1914 // Truncate (with sign-extension) the offset value to the pointer size.
1916 if (BitWidth < 64)
1918
1919 unsigned Opc;
1920 if (GV->isThreadLocal())
1922 else
1924
1925 SDVTList VTs = getVTList(VT);
1927 AddNodeIDNode(ID, Opc, VTs, {});
1928 ID.AddPointer(GV);
1929 ID.AddInteger(Offset);
1930 ID.AddInteger(TargetFlags);
1931 void *IP = nullptr;
1932 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1933 return SDValue(E, 0);
1934
1935 auto *N = newSDNode<GlobalAddressSDNode>(
1936 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1937 CSEMap.InsertNode(N, IP);
1938 InsertNode(N);
1939 return SDValue(N, 0);
1940}
1941
1943 SDVTList VTs = getVTList(MVT::Untyped);
1946 ID.AddPointer(GV);
1947 void *IP = nullptr;
1948 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP))
1949 return SDValue(E, 0);
1950
1951 auto *N = newSDNode<DeactivationSymbolSDNode>(GV, VTs);
1952 CSEMap.InsertNode(N, IP);
1953 InsertNode(N);
1954 return SDValue(N, 0);
1955}
1956
1957SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1958 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1959 SDVTList VTs = getVTList(VT);
1961 AddNodeIDNode(ID, Opc, VTs, {});
1962 ID.AddInteger(FI);
1963 void *IP = nullptr;
1964 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1965 return SDValue(E, 0);
1966
1967 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1968 CSEMap.InsertNode(N, IP);
1969 InsertNode(N);
1970 return SDValue(N, 0);
1971}
1972
1973SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1974 unsigned TargetFlags) {
1975 assert((TargetFlags == 0 || isTarget) &&
1976 "Cannot set target flags on target-independent jump tables");
1977 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1978 SDVTList VTs = getVTList(VT);
1980 AddNodeIDNode(ID, Opc, VTs, {});
1981 ID.AddInteger(JTI);
1982 ID.AddInteger(TargetFlags);
1983 void *IP = nullptr;
1984 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1985 return SDValue(E, 0);
1986
1987 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1988 CSEMap.InsertNode(N, IP);
1989 InsertNode(N);
1990 return SDValue(N, 0);
1991}
1992
1994 const SDLoc &DL) {
1996 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Other, Chain,
1997 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1998}
1999
2001 MaybeAlign Alignment, int Offset,
2002 bool isTarget, unsigned TargetFlags) {
2003 assert((TargetFlags == 0 || isTarget) &&
2004 "Cannot set target flags on target-independent globals");
2005 if (!Alignment)
2006 Alignment = shouldOptForSize()
2007 ? getDataLayout().getABITypeAlign(C->getType())
2008 : getDataLayout().getPrefTypeAlign(C->getType());
2009 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2010 SDVTList VTs = getVTList(VT);
2012 AddNodeIDNode(ID, Opc, VTs, {});
2013 ID.AddInteger(Alignment->value());
2014 ID.AddInteger(Offset);
2015 ID.AddPointer(C);
2016 ID.AddInteger(TargetFlags);
2017 void *IP = nullptr;
2018 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2019 return SDValue(E, 0);
2020
2021 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2022 TargetFlags);
2023 CSEMap.InsertNode(N, IP);
2024 InsertNode(N);
2025 SDValue V = SDValue(N, 0);
2026 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
2027 return V;
2028}
2029
2031 MaybeAlign Alignment, int Offset,
2032 bool isTarget, unsigned TargetFlags) {
2033 assert((TargetFlags == 0 || isTarget) &&
2034 "Cannot set target flags on target-independent globals");
2035 if (!Alignment)
2036 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2037 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2038 SDVTList VTs = getVTList(VT);
2040 AddNodeIDNode(ID, Opc, VTs, {});
2041 ID.AddInteger(Alignment->value());
2042 ID.AddInteger(Offset);
2043 C->addSelectionDAGCSEId(ID);
2044 ID.AddInteger(TargetFlags);
2045 void *IP = nullptr;
2046 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2047 return SDValue(E, 0);
2048
2049 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2050 TargetFlags);
2051 CSEMap.InsertNode(N, IP);
2052 InsertNode(N);
2053 return SDValue(N, 0);
2054}
2055
2058 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2059 ID.AddPointer(MBB);
2060 void *IP = nullptr;
2061 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2062 return SDValue(E, 0);
2063
2064 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2065 CSEMap.InsertNode(N, IP);
2066 InsertNode(N);
2067 return SDValue(N, 0);
2068}
2069
2071 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2072 ValueTypeNodes.size())
2073 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2074
2075 SDNode *&N = VT.isExtended() ?
2076 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2077
2078 if (N) return SDValue(N, 0);
2079 N = newSDNode<VTSDNode>(VT);
2080 InsertNode(N);
2081 return SDValue(N, 0);
2082}
2083
2085 SDNode *&N = ExternalSymbols[Sym];
2086 if (N) return SDValue(N, 0);
2087 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2088 InsertNode(N);
2089 return SDValue(N, 0);
2090}
2091
2092SDValue SelectionDAG::getExternalSymbol(RTLIB::LibcallImpl Libcall, EVT VT) {
2094 return getExternalSymbol(SymName.data(), VT);
2095}
2096
2098 SDNode *&N = MCSymbols[Sym];
2099 if (N)
2100 return SDValue(N, 0);
2101 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2102 InsertNode(N);
2103 return SDValue(N, 0);
2104}
2105
2107 unsigned TargetFlags) {
2108 SDNode *&N =
2109 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2110 if (N) return SDValue(N, 0);
2111 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2112 InsertNode(N);
2113 return SDValue(N, 0);
2114}
2115
2117 EVT VT, unsigned TargetFlags) {
2119 return getTargetExternalSymbol(SymName.data(), VT, TargetFlags);
2120}
2121
2123 if ((unsigned)Cond >= CondCodeNodes.size())
2124 CondCodeNodes.resize(Cond+1);
2125
2126 if (!CondCodeNodes[Cond]) {
2127 auto *N = newSDNode<CondCodeSDNode>(Cond);
2128 CondCodeNodes[Cond] = N;
2129 InsertNode(N);
2130 }
2131
2132 return SDValue(CondCodeNodes[Cond], 0);
2133}
2134
2136 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2137 "APInt size does not match type size!");
2138
2139 if (MulImm == 0)
2140 return getConstant(0, DL, VT);
2141
2142 const MachineFunction &MF = getMachineFunction();
2143 const Function &F = MF.getFunction();
2144 ConstantRange CR = getVScaleRange(&F, 64);
2145 if (const APInt *C = CR.getSingleElement())
2146 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2147
2148 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2149}
2150
2151/// \returns a value of type \p VT that represents the runtime value of \p
2152/// Quantity, i.e. scaled by vscale if it's scalable, or a fixed constant
2153/// otherwise. Quantity should be a FixedOrScalableQuantity, i.e. ElementCount
2154/// or TypeSize.
2155template <typename Ty>
2157 EVT VT, Ty Quantity) {
2158 if (Quantity.isScalable())
2159 return DAG.getVScale(
2160 DL, VT, APInt(VT.getSizeInBits(), Quantity.getKnownMinValue()));
2161
2162 return DAG.getConstant(Quantity.getKnownMinValue(), DL, VT);
2163}
2164
2166 ElementCount EC) {
2167 return getFixedOrScalableQuantity(*this, DL, VT, EC);
2168}
2169
2171 return getFixedOrScalableQuantity(*this, DL, VT, TS);
2172}
2173
2175 ElementCount EC) {
2176 EVT IdxVT = TLI->getVectorIdxTy(getDataLayout());
2177 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), DataVT);
2178 return getNode(ISD::GET_ACTIVE_LANE_MASK, DL, MaskVT,
2179 getConstant(0, DL, IdxVT), getElementCount(DL, IdxVT, EC));
2180}
2181
2183 APInt One(ResVT.getScalarSizeInBits(), 1);
2184 return getStepVector(DL, ResVT, One);
2185}
2186
2188 const APInt &StepVal) {
2189 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2190 if (ResVT.isScalableVector())
2191 return getNode(
2192 ISD::STEP_VECTOR, DL, ResVT,
2193 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2194
2195 SmallVector<SDValue, 16> OpsStepConstants;
2196 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2197 OpsStepConstants.push_back(
2198 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2199 return getBuildVector(ResVT, DL, OpsStepConstants);
2200}
2201
2202/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2203/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2208
2210 SDValue N2, ArrayRef<int> Mask) {
2211 assert(VT.getVectorNumElements() == Mask.size() &&
2212 "Must have the same number of vector elements as mask elements!");
2213 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2214 "Invalid VECTOR_SHUFFLE");
2215
2216 // Canonicalize shuffle undef, undef -> undef
2217 if (N1.isUndef() && N2.isUndef())
2218 return getUNDEF(VT);
2219
2220 // Validate that all indices in Mask are within the range of the elements
2221 // input to the shuffle.
2222 int NElts = Mask.size();
2223 assert(llvm::all_of(Mask,
2224 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2225 "Index out of range");
2226
2227 // Copy the mask so we can do any needed cleanup.
2228 SmallVector<int, 8> MaskVec(Mask);
2229
2230 // Canonicalize shuffle v, v -> v, undef
2231 if (N1 == N2) {
2232 N2 = getUNDEF(VT);
2233 for (int i = 0; i != NElts; ++i)
2234 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2235 }
2236
2237 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2238 if (N1.isUndef())
2239 commuteShuffle(N1, N2, MaskVec);
2240
2241 if (TLI->hasVectorBlend()) {
2242 // If shuffling a splat, try to blend the splat instead. We do this here so
2243 // that even when this arises during lowering we don't have to re-handle it.
2244 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2245 BitVector UndefElements;
2246 SDValue Splat = BV->getSplatValue(&UndefElements);
2247 if (!Splat)
2248 return;
2249
2250 for (int i = 0; i < NElts; ++i) {
2251 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2252 continue;
2253
2254 // If this input comes from undef, mark it as such.
2255 if (UndefElements[MaskVec[i] - Offset]) {
2256 MaskVec[i] = -1;
2257 continue;
2258 }
2259
2260 // If we can blend a non-undef lane, use that instead.
2261 if (!UndefElements[i])
2262 MaskVec[i] = i + Offset;
2263 }
2264 };
2265 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2266 BlendSplat(N1BV, 0);
2267 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2268 BlendSplat(N2BV, NElts);
2269 }
2270
2271 // Canonicalize all index into lhs, -> shuffle lhs, undef
2272 // Canonicalize all index into rhs, -> shuffle rhs, undef
2273 bool AllLHS = true, AllRHS = true;
2274 bool N2Undef = N2.isUndef();
2275 for (int i = 0; i != NElts; ++i) {
2276 if (MaskVec[i] >= NElts) {
2277 if (N2Undef)
2278 MaskVec[i] = -1;
2279 else
2280 AllLHS = false;
2281 } else if (MaskVec[i] >= 0) {
2282 AllRHS = false;
2283 }
2284 }
2285 if (AllLHS && AllRHS)
2286 return getUNDEF(VT);
2287 if (AllLHS && !N2Undef)
2288 N2 = getUNDEF(VT);
2289 if (AllRHS) {
2290 N1 = getUNDEF(VT);
2291 commuteShuffle(N1, N2, MaskVec);
2292 }
2293 // Reset our undef status after accounting for the mask.
2294 N2Undef = N2.isUndef();
2295 // Re-check whether both sides ended up undef.
2296 if (N1.isUndef() && N2Undef)
2297 return getUNDEF(VT);
2298
2299 // If Identity shuffle return that node.
2300 bool Identity = true, AllSame = true;
2301 for (int i = 0; i != NElts; ++i) {
2302 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2303 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2304 }
2305 if (Identity && NElts)
2306 return N1;
2307
2308 // Shuffling a constant splat doesn't change the result.
2309 if (N2Undef) {
2310 SDValue V = N1;
2311
2312 // Look through any bitcasts. We check that these don't change the number
2313 // (and size) of elements and just changes their types.
2314 while (V.getOpcode() == ISD::BITCAST)
2315 V = V->getOperand(0);
2316
2317 // A splat should always show up as a build vector node.
2318 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2319 BitVector UndefElements;
2320 SDValue Splat = BV->getSplatValue(&UndefElements);
2321 // If this is a splat of an undef, shuffling it is also undef.
2322 if (Splat && Splat.isUndef())
2323 return getUNDEF(VT);
2324
2325 bool SameNumElts =
2326 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2327
2328 // We only have a splat which can skip shuffles if there is a splatted
2329 // value and no undef lanes rearranged by the shuffle.
2330 if (Splat && UndefElements.none()) {
2331 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2332 // number of elements match or the value splatted is a zero constant.
2333 if (SameNumElts || isNullConstant(Splat))
2334 return N1;
2335 }
2336
2337 // If the shuffle itself creates a splat, build the vector directly.
2338 if (AllSame && SameNumElts) {
2339 EVT BuildVT = BV->getValueType(0);
2340 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2341 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2342
2343 // We may have jumped through bitcasts, so the type of the
2344 // BUILD_VECTOR may not match the type of the shuffle.
2345 if (BuildVT != VT)
2346 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2347 return NewBV;
2348 }
2349 }
2350 }
2351
2352 SDVTList VTs = getVTList(VT);
2354 SDValue Ops[2] = { N1, N2 };
2356 for (int i = 0; i != NElts; ++i)
2357 ID.AddInteger(MaskVec[i]);
2358
2359 void* IP = nullptr;
2360 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2361 return SDValue(E, 0);
2362
2363 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2364 // SDNode doesn't have access to it. This memory will be "leaked" when
2365 // the node is deallocated, but recovered when the NodeAllocator is released.
2366 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2367 llvm::copy(MaskVec, MaskAlloc);
2368
2369 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2370 dl.getDebugLoc(), MaskAlloc);
2371 createOperands(N, Ops);
2372
2373 CSEMap.InsertNode(N, IP);
2374 InsertNode(N);
2375 SDValue V = SDValue(N, 0);
2376 NewSDValueDbgMsg(V, "Creating new node: ", this);
2377 return V;
2378}
2379
2381 EVT VT = SV.getValueType(0);
2382 SmallVector<int, 8> MaskVec(SV.getMask());
2384
2385 SDValue Op0 = SV.getOperand(0);
2386 SDValue Op1 = SV.getOperand(1);
2387 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2388}
2389
2391 SDVTList VTs = getVTList(VT);
2393 AddNodeIDNode(ID, ISD::Register, VTs, {});
2394 ID.AddInteger(Reg.id());
2395 void *IP = nullptr;
2396 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2397 return SDValue(E, 0);
2398
2399 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2400 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2401 CSEMap.InsertNode(N, IP);
2402 InsertNode(N);
2403 return SDValue(N, 0);
2404}
2405
2408 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2409 ID.AddPointer(RegMask);
2410 void *IP = nullptr;
2411 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2412 return SDValue(E, 0);
2413
2414 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2415 CSEMap.InsertNode(N, IP);
2416 InsertNode(N);
2417 return SDValue(N, 0);
2418}
2419
2421 MCSymbol *Label) {
2422 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2423}
2424
2425SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2426 SDValue Root, MCSymbol *Label) {
2428 SDValue Ops[] = { Root };
2429 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2430 ID.AddPointer(Label);
2431 void *IP = nullptr;
2432 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2433 return SDValue(E, 0);
2434
2435 auto *N =
2436 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2437 createOperands(N, Ops);
2438
2439 CSEMap.InsertNode(N, IP);
2440 InsertNode(N);
2441 return SDValue(N, 0);
2442}
2443
2445 int64_t Offset, bool isTarget,
2446 unsigned TargetFlags) {
2447 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2448 SDVTList VTs = getVTList(VT);
2449
2451 AddNodeIDNode(ID, Opc, VTs, {});
2452 ID.AddPointer(BA);
2453 ID.AddInteger(Offset);
2454 ID.AddInteger(TargetFlags);
2455 void *IP = nullptr;
2456 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2457 return SDValue(E, 0);
2458
2459 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2460 CSEMap.InsertNode(N, IP);
2461 InsertNode(N);
2462 return SDValue(N, 0);
2463}
2464
2467 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2468 ID.AddPointer(V);
2469
2470 void *IP = nullptr;
2471 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2472 return SDValue(E, 0);
2473
2474 auto *N = newSDNode<SrcValueSDNode>(V);
2475 CSEMap.InsertNode(N, IP);
2476 InsertNode(N);
2477 return SDValue(N, 0);
2478}
2479
2482 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2483 ID.AddPointer(MD);
2484
2485 void *IP = nullptr;
2486 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2487 return SDValue(E, 0);
2488
2489 auto *N = newSDNode<MDNodeSDNode>(MD);
2490 CSEMap.InsertNode(N, IP);
2491 InsertNode(N);
2492 return SDValue(N, 0);
2493}
2494
2496 if (VT == V.getValueType())
2497 return V;
2498
2499 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2500}
2501
2503 unsigned SrcAS, unsigned DestAS) {
2504 SDVTList VTs = getVTList(VT);
2505 SDValue Ops[] = {Ptr};
2508 ID.AddInteger(SrcAS);
2509 ID.AddInteger(DestAS);
2510
2511 void *IP = nullptr;
2512 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2513 return SDValue(E, 0);
2514
2515 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2516 VTs, SrcAS, DestAS);
2517 createOperands(N, Ops);
2518
2519 CSEMap.InsertNode(N, IP);
2520 InsertNode(N);
2521 return SDValue(N, 0);
2522}
2523
2525 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2526}
2527
2528/// getShiftAmountOperand - Return the specified value casted to
2529/// the target's desired shift amount type.
2531 EVT OpTy = Op.getValueType();
2532 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2533 if (OpTy == ShTy || OpTy.isVector()) return Op;
2534
2535 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2536}
2537
2539 SDLoc dl(Node);
2541 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2542 EVT VT = Node->getValueType(0);
2543 SDValue Tmp1 = Node->getOperand(0);
2544 SDValue Tmp2 = Node->getOperand(1);
2545 const MaybeAlign MA(Node->getConstantOperandVal(3));
2546
2547 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2548 Tmp2, MachinePointerInfo(V));
2549 SDValue VAList = VAListLoad;
2550
2551 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2552 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2553 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2554
2555 VAList = getNode(
2556 ISD::AND, dl, VAList.getValueType(), VAList,
2557 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2558 }
2559
2560 // Increment the pointer, VAList, to the next vaarg
2561 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2562 getConstant(getDataLayout().getTypeAllocSize(
2563 VT.getTypeForEVT(*getContext())),
2564 dl, VAList.getValueType()));
2565 // Store the incremented VAList to the legalized pointer
2566 Tmp1 =
2567 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2568 // Load the actual argument out of the pointer VAList
2569 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2570}
2571
2573 SDLoc dl(Node);
2575 // This defaults to loading a pointer from the input and storing it to the
2576 // output, returning the chain.
2577 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2578 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2579 SDValue Tmp1 =
2580 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2581 Node->getOperand(2), MachinePointerInfo(VS));
2582 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2583 MachinePointerInfo(VD));
2584}
2585
2587 const DataLayout &DL = getDataLayout();
2588 Type *Ty = VT.getTypeForEVT(*getContext());
2589 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2590
2591 if (TLI->isTypeLegal(VT) || !VT.isVector())
2592 return RedAlign;
2593
2594 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2595 const Align StackAlign = TFI->getStackAlign();
2596
2597 // See if we can choose a smaller ABI alignment in cases where it's an
2598 // illegal vector type that will get broken down.
2599 if (RedAlign > StackAlign) {
2600 EVT IntermediateVT;
2601 MVT RegisterVT;
2602 unsigned NumIntermediates;
2603 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2604 NumIntermediates, RegisterVT);
2605 Ty = IntermediateVT.getTypeForEVT(*getContext());
2606 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2607 if (RedAlign2 < RedAlign)
2608 RedAlign = RedAlign2;
2609
2610 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2611 // If the stack is not realignable, the alignment should be limited to the
2612 // StackAlignment
2613 RedAlign = std::min(RedAlign, StackAlign);
2614 }
2615
2616 return RedAlign;
2617}
2618
2620 MachineFrameInfo &MFI = MF->getFrameInfo();
2621 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2622 int StackID = 0;
2623 if (Bytes.isScalable())
2624 StackID = TFI->getStackIDForScalableVectors();
2625 // The stack id gives an indication of whether the object is scalable or
2626 // not, so it's safe to pass in the minimum size here.
2627 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2628 false, nullptr, StackID);
2629 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2630}
2631
2633 Type *Ty = VT.getTypeForEVT(*getContext());
2634 Align StackAlign =
2635 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2636 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2637}
2638
2640 TypeSize VT1Size = VT1.getStoreSize();
2641 TypeSize VT2Size = VT2.getStoreSize();
2642 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2643 "Don't know how to choose the maximum size when creating a stack "
2644 "temporary");
2645 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2646 ? VT1Size
2647 : VT2Size;
2648
2649 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2650 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2651 const DataLayout &DL = getDataLayout();
2652 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2653 return CreateStackTemporary(Bytes, Align);
2654}
2655
2657 ISD::CondCode Cond, const SDLoc &dl) {
2658 EVT OpVT = N1.getValueType();
2659
2660 auto GetUndefBooleanConstant = [&]() {
2661 if (VT.getScalarType() == MVT::i1 ||
2662 TLI->getBooleanContents(OpVT) ==
2664 return getUNDEF(VT);
2665 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2666 // so we cannot use getUNDEF(). Return zero instead.
2667 return getConstant(0, dl, VT);
2668 };
2669
2670 // These setcc operations always fold.
2671 switch (Cond) {
2672 default: break;
2673 case ISD::SETFALSE:
2674 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2675 case ISD::SETTRUE:
2676 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2677
2678 case ISD::SETOEQ:
2679 case ISD::SETOGT:
2680 case ISD::SETOGE:
2681 case ISD::SETOLT:
2682 case ISD::SETOLE:
2683 case ISD::SETONE:
2684 case ISD::SETO:
2685 case ISD::SETUO:
2686 case ISD::SETUEQ:
2687 case ISD::SETUNE:
2688 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2689 break;
2690 }
2691
2692 if (OpVT.isInteger()) {
2693 // For EQ and NE, we can always pick a value for the undef to make the
2694 // predicate pass or fail, so we can return undef.
2695 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2696 // icmp eq/ne X, undef -> undef.
2697 if ((N1.isUndef() || N2.isUndef()) &&
2698 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2699 return GetUndefBooleanConstant();
2700
2701 // If both operands are undef, we can return undef for int comparison.
2702 // icmp undef, undef -> undef.
2703 if (N1.isUndef() && N2.isUndef())
2704 return GetUndefBooleanConstant();
2705
2706 // icmp X, X -> true/false
2707 // icmp X, undef -> true/false because undef could be X.
2708 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2709 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2710 }
2711
2713 const APInt &C2 = N2C->getAPIntValue();
2715 const APInt &C1 = N1C->getAPIntValue();
2716
2718 dl, VT, OpVT);
2719 }
2720 }
2721
2722 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2723 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2724
2725 if (N1CFP && N2CFP) {
2726 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2727 switch (Cond) {
2728 default: break;
2729 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2730 return GetUndefBooleanConstant();
2731 [[fallthrough]];
2732 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2733 OpVT);
2734 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2735 return GetUndefBooleanConstant();
2736 [[fallthrough]];
2738 R==APFloat::cmpLessThan, dl, VT,
2739 OpVT);
2740 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2741 return GetUndefBooleanConstant();
2742 [[fallthrough]];
2743 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2744 OpVT);
2745 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2746 return GetUndefBooleanConstant();
2747 [[fallthrough]];
2749 VT, OpVT);
2750 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2751 return GetUndefBooleanConstant();
2752 [[fallthrough]];
2754 R==APFloat::cmpEqual, dl, VT,
2755 OpVT);
2756 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2757 return GetUndefBooleanConstant();
2758 [[fallthrough]];
2760 R==APFloat::cmpEqual, dl, VT, OpVT);
2761 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2762 OpVT);
2763 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2764 OpVT);
2766 R==APFloat::cmpEqual, dl, VT,
2767 OpVT);
2768 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2769 OpVT);
2771 R==APFloat::cmpLessThan, dl, VT,
2772 OpVT);
2774 R==APFloat::cmpUnordered, dl, VT,
2775 OpVT);
2777 VT, OpVT);
2778 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2779 OpVT);
2780 }
2781 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2782 // Ensure that the constant occurs on the RHS.
2784 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2785 return SDValue();
2786 return getSetCC(dl, VT, N2, N1, SwappedCond);
2787 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2788 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2789 // If an operand is known to be a nan (or undef that could be a nan), we can
2790 // fold it.
2791 // Choosing NaN for the undef will always make unordered comparison succeed
2792 // and ordered comparison fails.
2793 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2794 switch (ISD::getUnorderedFlavor(Cond)) {
2795 default:
2796 llvm_unreachable("Unknown flavor!");
2797 case 0: // Known false.
2798 return getBoolConstant(false, dl, VT, OpVT);
2799 case 1: // Known true.
2800 return getBoolConstant(true, dl, VT, OpVT);
2801 case 2: // Undefined.
2802 return GetUndefBooleanConstant();
2803 }
2804 }
2805
2806 // Could not fold it.
2807 return SDValue();
2808}
2809
2810/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2811/// use this predicate to simplify operations downstream.
2813 unsigned BitWidth = Op.getScalarValueSizeInBits();
2815}
2816
2817// TODO: Should have argument to specify if sign bit of nan is ignorable.
2819 if (Depth >= MaxRecursionDepth)
2820 return false; // Limit search depth.
2821
2822 unsigned Opc = Op.getOpcode();
2823 switch (Opc) {
2824 case ISD::FABS:
2825 return true;
2826 case ISD::AssertNoFPClass: {
2827 FPClassTest NoFPClass =
2828 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2829
2830 const FPClassTest TestMask = fcNan | fcNegative;
2831 return (NoFPClass & TestMask) == TestMask;
2832 }
2833 case ISD::ARITH_FENCE:
2834 return SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2835 case ISD::FEXP:
2836 case ISD::FEXP2:
2837 case ISD::FEXP10:
2838 return Op->getFlags().hasNoNaNs();
2839 case ISD::FMINNUM:
2840 case ISD::FMINNUM_IEEE:
2841 case ISD::FMINIMUM:
2842 case ISD::FMINIMUMNUM:
2843 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2844 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2845 case ISD::FMAXNUM:
2846 case ISD::FMAXNUM_IEEE:
2847 case ISD::FMAXIMUM:
2848 case ISD::FMAXIMUMNUM:
2849 // TODO: If we can ignore the sign bit of nans, only one side being known 0
2850 // is sufficient.
2851 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2852 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2853 default:
2854 return false;
2855 }
2856
2857 llvm_unreachable("covered opcode switch");
2858}
2859
2860/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2861/// this predicate to simplify operations downstream. Mask is known to be zero
2862/// for bits that V cannot have.
2864 unsigned Depth) const {
2865 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2866}
2867
2868/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2869/// DemandedElts. We use this predicate to simplify operations downstream.
2870/// Mask is known to be zero for bits that V cannot have.
2872 const APInt &DemandedElts,
2873 unsigned Depth) const {
2874 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2875}
2876
2877/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2878/// DemandedElts. We use this predicate to simplify operations downstream.
2880 unsigned Depth /* = 0 */) const {
2881 return computeKnownBits(V, DemandedElts, Depth).isZero();
2882}
2883
2884/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2886 unsigned Depth) const {
2887 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2888}
2889
2891 const APInt &DemandedElts,
2892 unsigned Depth) const {
2893 EVT VT = Op.getValueType();
2894 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2895
2896 unsigned NumElts = VT.getVectorNumElements();
2897 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2898
2899 APInt KnownZeroElements = APInt::getZero(NumElts);
2900 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2901 if (!DemandedElts[EltIdx])
2902 continue; // Don't query elements that are not demanded.
2903 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2904 if (MaskedVectorIsZero(Op, Mask, Depth))
2905 KnownZeroElements.setBit(EltIdx);
2906 }
2907 return KnownZeroElements;
2908}
2909
2910/// isSplatValue - Return true if the vector V has the same value
2911/// across all DemandedElts. For scalable vectors, we don't know the
2912/// number of lanes at compile time. Instead, we use a 1 bit APInt
2913/// to represent a conservative value for all lanes; that is, that
2914/// one bit value is implicitly splatted across all lanes.
2915bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2916 APInt &UndefElts, unsigned Depth) const {
2917 unsigned Opcode = V.getOpcode();
2918 EVT VT = V.getValueType();
2919 assert(VT.isVector() && "Vector type expected");
2920 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2921 "scalable demanded bits are ignored");
2922
2923 if (!DemandedElts)
2924 return false; // No demanded elts, better to assume we don't know anything.
2925
2926 if (Depth >= MaxRecursionDepth)
2927 return false; // Limit search depth.
2928
2929 // Deal with some common cases here that work for both fixed and scalable
2930 // vector types.
2931 switch (Opcode) {
2932 case ISD::SPLAT_VECTOR:
2933 UndefElts = V.getOperand(0).isUndef()
2934 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2935 : APInt(DemandedElts.getBitWidth(), 0);
2936 return true;
2937 case ISD::ADD:
2938 case ISD::SUB:
2939 case ISD::AND:
2940 case ISD::XOR:
2941 case ISD::OR: {
2942 APInt UndefLHS, UndefRHS;
2943 SDValue LHS = V.getOperand(0);
2944 SDValue RHS = V.getOperand(1);
2945 // Only recognize splats with the same demanded undef elements for both
2946 // operands, otherwise we might fail to handle binop-specific undef
2947 // handling.
2948 // e.g. (and undef, 0) -> 0 etc.
2949 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2950 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
2951 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
2952 UndefElts = UndefLHS | UndefRHS;
2953 return true;
2954 }
2955 return false;
2956 }
2957 case ISD::ABS:
2958 case ISD::TRUNCATE:
2959 case ISD::SIGN_EXTEND:
2960 case ISD::ZERO_EXTEND:
2961 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2962 default:
2963 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2964 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2965 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2966 Depth);
2967 break;
2968 }
2969
2970 // We don't support other cases than those above for scalable vectors at
2971 // the moment.
2972 if (VT.isScalableVector())
2973 return false;
2974
2975 unsigned NumElts = VT.getVectorNumElements();
2976 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2977 UndefElts = APInt::getZero(NumElts);
2978
2979 switch (Opcode) {
2980 case ISD::BUILD_VECTOR: {
2981 SDValue Scl;
2982 for (unsigned i = 0; i != NumElts; ++i) {
2983 SDValue Op = V.getOperand(i);
2984 if (Op.isUndef()) {
2985 UndefElts.setBit(i);
2986 continue;
2987 }
2988 if (!DemandedElts[i])
2989 continue;
2990 if (Scl && Scl != Op)
2991 return false;
2992 Scl = Op;
2993 }
2994 return true;
2995 }
2996 case ISD::VECTOR_SHUFFLE: {
2997 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2998 APInt DemandedLHS = APInt::getZero(NumElts);
2999 APInt DemandedRHS = APInt::getZero(NumElts);
3000 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
3001 for (int i = 0; i != (int)NumElts; ++i) {
3002 int M = Mask[i];
3003 if (M < 0) {
3004 UndefElts.setBit(i);
3005 continue;
3006 }
3007 if (!DemandedElts[i])
3008 continue;
3009 if (M < (int)NumElts)
3010 DemandedLHS.setBit(M);
3011 else
3012 DemandedRHS.setBit(M - NumElts);
3013 }
3014
3015 // If we aren't demanding either op, assume there's no splat.
3016 // If we are demanding both ops, assume there's no splat.
3017 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
3018 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3019 return false;
3020
3021 // See if the demanded elts of the source op is a splat or we only demand
3022 // one element, which should always be a splat.
3023 // TODO: Handle source ops splats with undefs.
3024 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3025 APInt SrcUndefs;
3026 return (SrcElts.popcount() == 1) ||
3027 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3028 (SrcElts & SrcUndefs).isZero());
3029 };
3030 if (!DemandedLHS.isZero())
3031 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3032 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3033 }
3035 // Offset the demanded elts by the subvector index.
3036 SDValue Src = V.getOperand(0);
3037 // We don't support scalable vectors at the moment.
3038 if (Src.getValueType().isScalableVector())
3039 return false;
3040 uint64_t Idx = V.getConstantOperandVal(1);
3041 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3042 APInt UndefSrcElts;
3043 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3044 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3045 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3046 return true;
3047 }
3048 break;
3049 }
3053 // Widen the demanded elts by the src element count.
3054 SDValue Src = V.getOperand(0);
3055 // We don't support scalable vectors at the moment.
3056 if (Src.getValueType().isScalableVector())
3057 return false;
3058 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3059 APInt UndefSrcElts;
3060 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3061 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3062 UndefElts = UndefSrcElts.trunc(NumElts);
3063 return true;
3064 }
3065 break;
3066 }
3067 case ISD::BITCAST: {
3068 SDValue Src = V.getOperand(0);
3069 EVT SrcVT = Src.getValueType();
3070 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3071 unsigned BitWidth = VT.getScalarSizeInBits();
3072
3073 // Ignore bitcasts from unsupported types.
3074 // TODO: Add fp support?
3075 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3076 break;
3077
3078 // Bitcast 'small element' vector to 'large element' vector.
3079 if ((BitWidth % SrcBitWidth) == 0) {
3080 // See if each sub element is a splat.
3081 unsigned Scale = BitWidth / SrcBitWidth;
3082 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3083 APInt ScaledDemandedElts =
3084 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3085 for (unsigned I = 0; I != Scale; ++I) {
3086 APInt SubUndefElts;
3087 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3088 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3089 SubDemandedElts &= ScaledDemandedElts;
3090 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3091 return false;
3092 // TODO: Add support for merging sub undef elements.
3093 if (!SubUndefElts.isZero())
3094 return false;
3095 }
3096 return true;
3097 }
3098 break;
3099 }
3100 }
3101
3102 return false;
3103}
3104
3105/// Helper wrapper to main isSplatValue function.
3106bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3107 EVT VT = V.getValueType();
3108 assert(VT.isVector() && "Vector type expected");
3109
3110 APInt UndefElts;
3111 // Since the number of lanes in a scalable vector is unknown at compile time,
3112 // we track one bit which is implicitly broadcast to all lanes. This means
3113 // that all lanes in a scalable vector are considered demanded.
3114 APInt DemandedElts
3116 return isSplatValue(V, DemandedElts, UndefElts) &&
3117 (AllowUndefs || !UndefElts);
3118}
3119
3122
3123 EVT VT = V.getValueType();
3124 unsigned Opcode = V.getOpcode();
3125 switch (Opcode) {
3126 default: {
3127 APInt UndefElts;
3128 // Since the number of lanes in a scalable vector is unknown at compile time,
3129 // we track one bit which is implicitly broadcast to all lanes. This means
3130 // that all lanes in a scalable vector are considered demanded.
3131 APInt DemandedElts
3133
3134 if (isSplatValue(V, DemandedElts, UndefElts)) {
3135 if (VT.isScalableVector()) {
3136 // DemandedElts and UndefElts are ignored for scalable vectors, since
3137 // the only supported cases are SPLAT_VECTOR nodes.
3138 SplatIdx = 0;
3139 } else {
3140 // Handle case where all demanded elements are UNDEF.
3141 if (DemandedElts.isSubsetOf(UndefElts)) {
3142 SplatIdx = 0;
3143 return getUNDEF(VT);
3144 }
3145 SplatIdx = (UndefElts & DemandedElts).countr_one();
3146 }
3147 return V;
3148 }
3149 break;
3150 }
3151 case ISD::SPLAT_VECTOR:
3152 SplatIdx = 0;
3153 return V;
3154 case ISD::VECTOR_SHUFFLE: {
3155 assert(!VT.isScalableVector());
3156 // Check if this is a shuffle node doing a splat.
3157 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3158 // getTargetVShiftNode currently struggles without the splat source.
3159 auto *SVN = cast<ShuffleVectorSDNode>(V);
3160 if (!SVN->isSplat())
3161 break;
3162 int Idx = SVN->getSplatIndex();
3163 int NumElts = V.getValueType().getVectorNumElements();
3164 SplatIdx = Idx % NumElts;
3165 return V.getOperand(Idx / NumElts);
3166 }
3167 }
3168
3169 return SDValue();
3170}
3171
3173 int SplatIdx;
3174 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3175 EVT SVT = SrcVector.getValueType().getScalarType();
3176 EVT LegalSVT = SVT;
3177 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3178 if (!SVT.isInteger())
3179 return SDValue();
3180 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3181 if (LegalSVT.bitsLT(SVT))
3182 return SDValue();
3183 }
3184 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3185 }
3186 return SDValue();
3187}
3188
3189std::optional<ConstantRange>
3191 unsigned Depth) const {
3192 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3193 V.getOpcode() == ISD::SRA) &&
3194 "Unknown shift node");
3195 // Shifting more than the bitwidth is not valid.
3196 unsigned BitWidth = V.getScalarValueSizeInBits();
3197
3198 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3199 const APInt &ShAmt = Cst->getAPIntValue();
3200 if (ShAmt.uge(BitWidth))
3201 return std::nullopt;
3202 return ConstantRange(ShAmt);
3203 }
3204
3205 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3206 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3207 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3208 if (!DemandedElts[i])
3209 continue;
3210 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3211 if (!SA) {
3212 MinAmt = MaxAmt = nullptr;
3213 break;
3214 }
3215 const APInt &ShAmt = SA->getAPIntValue();
3216 if (ShAmt.uge(BitWidth))
3217 return std::nullopt;
3218 if (!MinAmt || MinAmt->ugt(ShAmt))
3219 MinAmt = &ShAmt;
3220 if (!MaxAmt || MaxAmt->ult(ShAmt))
3221 MaxAmt = &ShAmt;
3222 }
3223 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3224 "Failed to find matching min/max shift amounts");
3225 if (MinAmt && MaxAmt)
3226 return ConstantRange(*MinAmt, *MaxAmt + 1);
3227 }
3228
3229 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3230 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3231 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3232 if (KnownAmt.getMaxValue().ult(BitWidth))
3233 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3234
3235 return std::nullopt;
3236}
3237
3238std::optional<unsigned>
3240 unsigned Depth) const {
3241 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3242 V.getOpcode() == ISD::SRA) &&
3243 "Unknown shift node");
3244 if (std::optional<ConstantRange> AmtRange =
3245 getValidShiftAmountRange(V, DemandedElts, Depth))
3246 if (const APInt *ShAmt = AmtRange->getSingleElement())
3247 return ShAmt->getZExtValue();
3248 return std::nullopt;
3249}
3250
3251std::optional<unsigned>
3253 EVT VT = V.getValueType();
3254 APInt DemandedElts = VT.isFixedLengthVector()
3256 : APInt(1, 1);
3257 return getValidShiftAmount(V, DemandedElts, Depth);
3258}
3259
3260std::optional<unsigned>
3262 unsigned Depth) const {
3263 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3264 V.getOpcode() == ISD::SRA) &&
3265 "Unknown shift node");
3266 if (std::optional<ConstantRange> AmtRange =
3267 getValidShiftAmountRange(V, DemandedElts, Depth))
3268 return AmtRange->getUnsignedMin().getZExtValue();
3269 return std::nullopt;
3270}
3271
3272std::optional<unsigned>
3274 EVT VT = V.getValueType();
3275 APInt DemandedElts = VT.isFixedLengthVector()
3277 : APInt(1, 1);
3278 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3279}
3280
3281std::optional<unsigned>
3283 unsigned Depth) const {
3284 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3285 V.getOpcode() == ISD::SRA) &&
3286 "Unknown shift node");
3287 if (std::optional<ConstantRange> AmtRange =
3288 getValidShiftAmountRange(V, DemandedElts, Depth))
3289 return AmtRange->getUnsignedMax().getZExtValue();
3290 return std::nullopt;
3291}
3292
3293std::optional<unsigned>
3295 EVT VT = V.getValueType();
3296 APInt DemandedElts = VT.isFixedLengthVector()
3298 : APInt(1, 1);
3299 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3300}
3301
3302/// Determine which bits of Op are known to be either zero or one and return
3303/// them in Known. For vectors, the known bits are those that are shared by
3304/// every vector element.
3306 EVT VT = Op.getValueType();
3307
3308 // Since the number of lanes in a scalable vector is unknown at compile time,
3309 // we track one bit which is implicitly broadcast to all lanes. This means
3310 // that all lanes in a scalable vector are considered demanded.
3311 APInt DemandedElts = VT.isFixedLengthVector()
3313 : APInt(1, 1);
3314 return computeKnownBits(Op, DemandedElts, Depth);
3315}
3316
3317/// Determine which bits of Op are known to be either zero or one and return
3318/// them in Known. The DemandedElts argument allows us to only collect the known
3319/// bits that are shared by the requested vector elements.
3321 unsigned Depth) const {
3322 unsigned BitWidth = Op.getScalarValueSizeInBits();
3323
3324 KnownBits Known(BitWidth); // Don't know anything.
3325
3326 if (auto OptAPInt = Op->bitcastToAPInt()) {
3327 // We know all of the bits for a constant!
3328 return KnownBits::makeConstant(*std::move(OptAPInt));
3329 }
3330
3331 if (Depth >= MaxRecursionDepth)
3332 return Known; // Limit search depth.
3333
3334 KnownBits Known2;
3335 unsigned NumElts = DemandedElts.getBitWidth();
3336 assert((!Op.getValueType().isFixedLengthVector() ||
3337 NumElts == Op.getValueType().getVectorNumElements()) &&
3338 "Unexpected vector size");
3339
3340 if (!DemandedElts)
3341 return Known; // No demanded elts, better to assume we don't know anything.
3342
3343 unsigned Opcode = Op.getOpcode();
3344 switch (Opcode) {
3345 case ISD::MERGE_VALUES:
3346 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3347 Depth + 1);
3348 case ISD::SPLAT_VECTOR: {
3349 SDValue SrcOp = Op.getOperand(0);
3350 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3351 "Expected SPLAT_VECTOR implicit truncation");
3352 // Implicitly truncate the bits to match the official semantics of
3353 // SPLAT_VECTOR.
3354 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3355 break;
3356 }
3358 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3359 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3360 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3361 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3362 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3363 }
3364 break;
3365 }
3366 case ISD::STEP_VECTOR: {
3367 const APInt &Step = Op.getConstantOperandAPInt(0);
3368
3369 if (Step.isPowerOf2())
3370 Known.Zero.setLowBits(Step.logBase2());
3371
3373
3374 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3375 break;
3376 const APInt MinNumElts =
3377 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3378
3379 bool Overflow;
3380 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3382 .umul_ov(MinNumElts, Overflow);
3383 if (Overflow)
3384 break;
3385
3386 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3387 if (Overflow)
3388 break;
3389
3390 Known.Zero.setHighBits(MaxValue.countl_zero());
3391 break;
3392 }
3393 case ISD::BUILD_VECTOR:
3394 assert(!Op.getValueType().isScalableVector());
3395 // Collect the known bits that are shared by every demanded vector element.
3396 Known.setAllConflict();
3397 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3398 if (!DemandedElts[i])
3399 continue;
3400
3401 SDValue SrcOp = Op.getOperand(i);
3402 Known2 = computeKnownBits(SrcOp, Depth + 1);
3403
3404 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3405 if (SrcOp.getValueSizeInBits() != BitWidth) {
3406 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3407 "Expected BUILD_VECTOR implicit truncation");
3408 Known2 = Known2.trunc(BitWidth);
3409 }
3410
3411 // Known bits are the values that are shared by every demanded element.
3412 Known = Known.intersectWith(Known2);
3413
3414 // If we don't know any bits, early out.
3415 if (Known.isUnknown())
3416 break;
3417 }
3418 break;
3419 case ISD::VECTOR_COMPRESS: {
3420 SDValue Vec = Op.getOperand(0);
3421 SDValue PassThru = Op.getOperand(2);
3422 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3423 // If we don't know any bits, early out.
3424 if (Known.isUnknown())
3425 break;
3426 Known2 = computeKnownBits(Vec, Depth + 1);
3427 Known = Known.intersectWith(Known2);
3428 break;
3429 }
3430 case ISD::VECTOR_SHUFFLE: {
3431 assert(!Op.getValueType().isScalableVector());
3432 // Collect the known bits that are shared by every vector element referenced
3433 // by the shuffle.
3434 APInt DemandedLHS, DemandedRHS;
3436 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3437 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3438 DemandedLHS, DemandedRHS))
3439 break;
3440
3441 // Known bits are the values that are shared by every demanded element.
3442 Known.setAllConflict();
3443 if (!!DemandedLHS) {
3444 SDValue LHS = Op.getOperand(0);
3445 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3446 Known = Known.intersectWith(Known2);
3447 }
3448 // If we don't know any bits, early out.
3449 if (Known.isUnknown())
3450 break;
3451 if (!!DemandedRHS) {
3452 SDValue RHS = Op.getOperand(1);
3453 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3454 Known = Known.intersectWith(Known2);
3455 }
3456 break;
3457 }
3458 case ISD::VSCALE: {
3460 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3461 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3462 break;
3463 }
3464 case ISD::CONCAT_VECTORS: {
3465 if (Op.getValueType().isScalableVector())
3466 break;
3467 // Split DemandedElts and test each of the demanded subvectors.
3468 Known.setAllConflict();
3469 EVT SubVectorVT = Op.getOperand(0).getValueType();
3470 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3471 unsigned NumSubVectors = Op.getNumOperands();
3472 for (unsigned i = 0; i != NumSubVectors; ++i) {
3473 APInt DemandedSub =
3474 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3475 if (!!DemandedSub) {
3476 SDValue Sub = Op.getOperand(i);
3477 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3478 Known = Known.intersectWith(Known2);
3479 }
3480 // If we don't know any bits, early out.
3481 if (Known.isUnknown())
3482 break;
3483 }
3484 break;
3485 }
3486 case ISD::INSERT_SUBVECTOR: {
3487 if (Op.getValueType().isScalableVector())
3488 break;
3489 // Demand any elements from the subvector and the remainder from the src its
3490 // inserted into.
3491 SDValue Src = Op.getOperand(0);
3492 SDValue Sub = Op.getOperand(1);
3493 uint64_t Idx = Op.getConstantOperandVal(2);
3494 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3495 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3496 APInt DemandedSrcElts = DemandedElts;
3497 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3498
3499 Known.setAllConflict();
3500 if (!!DemandedSubElts) {
3501 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3502 if (Known.isUnknown())
3503 break; // early-out.
3504 }
3505 if (!!DemandedSrcElts) {
3506 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3507 Known = Known.intersectWith(Known2);
3508 }
3509 break;
3510 }
3512 // Offset the demanded elts by the subvector index.
3513 SDValue Src = Op.getOperand(0);
3514 // Bail until we can represent demanded elements for scalable vectors.
3515 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3516 break;
3517 uint64_t Idx = Op.getConstantOperandVal(1);
3518 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3519 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3520 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3521 break;
3522 }
3523 case ISD::SCALAR_TO_VECTOR: {
3524 if (Op.getValueType().isScalableVector())
3525 break;
3526 // We know about scalar_to_vector as much as we know about it source,
3527 // which becomes the first element of otherwise unknown vector.
3528 if (DemandedElts != 1)
3529 break;
3530
3531 SDValue N0 = Op.getOperand(0);
3532 Known = computeKnownBits(N0, Depth + 1);
3533 if (N0.getValueSizeInBits() != BitWidth)
3534 Known = Known.trunc(BitWidth);
3535
3536 break;
3537 }
3538 case ISD::BITCAST: {
3539 if (Op.getValueType().isScalableVector())
3540 break;
3541
3542 SDValue N0 = Op.getOperand(0);
3543 EVT SubVT = N0.getValueType();
3544 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3545
3546 // Ignore bitcasts from unsupported types.
3547 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3548 break;
3549
3550 // Fast handling of 'identity' bitcasts.
3551 if (BitWidth == SubBitWidth) {
3552 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3553 break;
3554 }
3555
3556 bool IsLE = getDataLayout().isLittleEndian();
3557
3558 // Bitcast 'small element' vector to 'large element' scalar/vector.
3559 if ((BitWidth % SubBitWidth) == 0) {
3560 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3561
3562 // Collect known bits for the (larger) output by collecting the known
3563 // bits from each set of sub elements and shift these into place.
3564 // We need to separately call computeKnownBits for each set of
3565 // sub elements as the knownbits for each is likely to be different.
3566 unsigned SubScale = BitWidth / SubBitWidth;
3567 APInt SubDemandedElts(NumElts * SubScale, 0);
3568 for (unsigned i = 0; i != NumElts; ++i)
3569 if (DemandedElts[i])
3570 SubDemandedElts.setBit(i * SubScale);
3571
3572 for (unsigned i = 0; i != SubScale; ++i) {
3573 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3574 Depth + 1);
3575 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3576 Known.insertBits(Known2, SubBitWidth * Shifts);
3577 }
3578 }
3579
3580 // Bitcast 'large element' scalar/vector to 'small element' vector.
3581 if ((SubBitWidth % BitWidth) == 0) {
3582 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3583
3584 // Collect known bits for the (smaller) output by collecting the known
3585 // bits from the overlapping larger input elements and extracting the
3586 // sub sections we actually care about.
3587 unsigned SubScale = SubBitWidth / BitWidth;
3588 APInt SubDemandedElts =
3589 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3590 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3591
3592 Known.setAllConflict();
3593 for (unsigned i = 0; i != NumElts; ++i)
3594 if (DemandedElts[i]) {
3595 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3596 unsigned Offset = (Shifts % SubScale) * BitWidth;
3597 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3598 // If we don't know any bits, early out.
3599 if (Known.isUnknown())
3600 break;
3601 }
3602 }
3603 break;
3604 }
3605 case ISD::AND:
3606 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3607 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3608
3609 Known &= Known2;
3610 break;
3611 case ISD::OR:
3612 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3613 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3614
3615 Known |= Known2;
3616 break;
3617 case ISD::XOR:
3618 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3619 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3620
3621 Known ^= Known2;
3622 break;
3623 case ISD::MUL: {
3624 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3625 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3626 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3627 // TODO: SelfMultiply can be poison, but not undef.
3628 if (SelfMultiply)
3629 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3630 Op.getOperand(0), DemandedElts, false, Depth + 1);
3631 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3632
3633 // If the multiplication is known not to overflow, the product of a number
3634 // with itself is non-negative. Only do this if we didn't already computed
3635 // the opposite value for the sign bit.
3636 if (Op->getFlags().hasNoSignedWrap() &&
3637 Op.getOperand(0) == Op.getOperand(1) &&
3638 !Known.isNegative())
3639 Known.makeNonNegative();
3640 break;
3641 }
3642 case ISD::MULHU: {
3643 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3644 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3645 Known = KnownBits::mulhu(Known, Known2);
3646 break;
3647 }
3648 case ISD::MULHS: {
3649 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3650 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3651 Known = KnownBits::mulhs(Known, Known2);
3652 break;
3653 }
3654 case ISD::ABDU: {
3655 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3656 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3657 Known = KnownBits::abdu(Known, Known2);
3658 break;
3659 }
3660 case ISD::ABDS: {
3661 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3662 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3663 Known = KnownBits::abds(Known, Known2);
3664 unsigned SignBits1 =
3665 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3666 if (SignBits1 == 1)
3667 break;
3668 unsigned SignBits0 =
3669 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3670 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3671 break;
3672 }
3673 case ISD::UMUL_LOHI: {
3674 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3675 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3676 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3677 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3678 if (Op.getResNo() == 0)
3679 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3680 else
3681 Known = KnownBits::mulhu(Known, Known2);
3682 break;
3683 }
3684 case ISD::SMUL_LOHI: {
3685 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3686 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3687 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3688 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3689 if (Op.getResNo() == 0)
3690 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3691 else
3692 Known = KnownBits::mulhs(Known, Known2);
3693 break;
3694 }
3695 case ISD::AVGFLOORU: {
3696 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3697 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3698 Known = KnownBits::avgFloorU(Known, Known2);
3699 break;
3700 }
3701 case ISD::AVGCEILU: {
3702 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3703 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3704 Known = KnownBits::avgCeilU(Known, Known2);
3705 break;
3706 }
3707 case ISD::AVGFLOORS: {
3708 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3709 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3710 Known = KnownBits::avgFloorS(Known, Known2);
3711 break;
3712 }
3713 case ISD::AVGCEILS: {
3714 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3715 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3716 Known = KnownBits::avgCeilS(Known, Known2);
3717 break;
3718 }
3719 case ISD::SELECT:
3720 case ISD::VSELECT:
3721 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3722 // If we don't know any bits, early out.
3723 if (Known.isUnknown())
3724 break;
3725 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3726
3727 // Only known if known in both the LHS and RHS.
3728 Known = Known.intersectWith(Known2);
3729 break;
3730 case ISD::SELECT_CC:
3731 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3732 // If we don't know any bits, early out.
3733 if (Known.isUnknown())
3734 break;
3735 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3736
3737 // Only known if known in both the LHS and RHS.
3738 Known = Known.intersectWith(Known2);
3739 break;
3740 case ISD::SMULO:
3741 case ISD::UMULO:
3742 if (Op.getResNo() != 1)
3743 break;
3744 // The boolean result conforms to getBooleanContents.
3745 // If we know the result of a setcc has the top bits zero, use this info.
3746 // We know that we have an integer-based boolean since these operations
3747 // are only available for integer.
3748 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3750 BitWidth > 1)
3751 Known.Zero.setBitsFrom(1);
3752 break;
3753 case ISD::SETCC:
3754 case ISD::SETCCCARRY:
3755 case ISD::STRICT_FSETCC:
3756 case ISD::STRICT_FSETCCS: {
3757 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3758 // If we know the result of a setcc has the top bits zero, use this info.
3759 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3761 BitWidth > 1)
3762 Known.Zero.setBitsFrom(1);
3763 break;
3764 }
3765 case ISD::SHL: {
3766 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3767 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3768
3769 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3770 bool NSW = Op->getFlags().hasNoSignedWrap();
3771
3772 bool ShAmtNonZero = Known2.isNonZero();
3773
3774 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3775
3776 // Minimum shift low bits are known zero.
3777 if (std::optional<unsigned> ShMinAmt =
3778 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3779 Known.Zero.setLowBits(*ShMinAmt);
3780 break;
3781 }
3782 case ISD::SRL:
3783 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3784 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3785 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3786 Op->getFlags().hasExact());
3787
3788 // Minimum shift high bits are known zero.
3789 if (std::optional<unsigned> ShMinAmt =
3790 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3791 Known.Zero.setHighBits(*ShMinAmt);
3792 break;
3793 case ISD::SRA:
3794 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3795 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3796 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3797 Op->getFlags().hasExact());
3798 break;
3799 case ISD::ROTL:
3800 case ISD::ROTR:
3801 if (ConstantSDNode *C =
3802 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3803 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3804
3805 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3806
3807 // Canonicalize to ROTR.
3808 if (Opcode == ISD::ROTL && Amt != 0)
3809 Amt = BitWidth - Amt;
3810
3811 Known.Zero = Known.Zero.rotr(Amt);
3812 Known.One = Known.One.rotr(Amt);
3813 }
3814 break;
3815 case ISD::FSHL:
3816 case ISD::FSHR:
3817 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3818 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3819
3820 // For fshl, 0-shift returns the 1st arg.
3821 // For fshr, 0-shift returns the 2nd arg.
3822 if (Amt == 0) {
3823 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3824 DemandedElts, Depth + 1);
3825 break;
3826 }
3827
3828 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3829 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3830 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3831 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3832 if (Opcode == ISD::FSHL) {
3833 Known <<= Amt;
3834 Known2 >>= BitWidth - Amt;
3835 } else {
3836 Known <<= BitWidth - Amt;
3837 Known2 >>= Amt;
3838 }
3839 Known = Known.unionWith(Known2);
3840 }
3841 break;
3842 case ISD::SHL_PARTS:
3843 case ISD::SRA_PARTS:
3844 case ISD::SRL_PARTS: {
3845 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3846
3847 // Collect lo/hi source values and concatenate.
3848 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3849 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3850 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3851 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3852 Known = Known2.concat(Known);
3853
3854 // Collect shift amount.
3855 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3856
3857 if (Opcode == ISD::SHL_PARTS)
3858 Known = KnownBits::shl(Known, Known2);
3859 else if (Opcode == ISD::SRA_PARTS)
3860 Known = KnownBits::ashr(Known, Known2);
3861 else // if (Opcode == ISD::SRL_PARTS)
3862 Known = KnownBits::lshr(Known, Known2);
3863
3864 // TODO: Minimum shift low/high bits are known zero.
3865
3866 if (Op.getResNo() == 0)
3867 Known = Known.extractBits(LoBits, 0);
3868 else
3869 Known = Known.extractBits(HiBits, LoBits);
3870 break;
3871 }
3873 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3874 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3875 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3876 break;
3877 }
3878 case ISD::CTTZ:
3879 case ISD::CTTZ_ZERO_UNDEF: {
3880 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3881 // If we have a known 1, its position is our upper bound.
3882 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3883 unsigned LowBits = llvm::bit_width(PossibleTZ);
3884 Known.Zero.setBitsFrom(LowBits);
3885 break;
3886 }
3887 case ISD::CTLZ:
3888 case ISD::CTLZ_ZERO_UNDEF: {
3889 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3890 // If we have a known 1, its position is our upper bound.
3891 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3892 unsigned LowBits = llvm::bit_width(PossibleLZ);
3893 Known.Zero.setBitsFrom(LowBits);
3894 break;
3895 }
3896 case ISD::CTLS: {
3897 unsigned MinRedundantSignBits =
3898 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1;
3899 ConstantRange Range(APInt(BitWidth, MinRedundantSignBits),
3901 Known = Range.toKnownBits();
3902 break;
3903 }
3904 case ISD::CTPOP: {
3905 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3906 // If we know some of the bits are zero, they can't be one.
3907 unsigned PossibleOnes = Known2.countMaxPopulation();
3908 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3909 break;
3910 }
3911 case ISD::PARITY: {
3912 // Parity returns 0 everywhere but the LSB.
3913 Known.Zero.setBitsFrom(1);
3914 break;
3915 }
3916 case ISD::MGATHER:
3917 case ISD::MLOAD: {
3918 ISD::LoadExtType ETy =
3919 (Opcode == ISD::MGATHER)
3920 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3921 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3922 if (ETy == ISD::ZEXTLOAD) {
3923 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3924 KnownBits Known0(MemVT.getScalarSizeInBits());
3925 return Known0.zext(BitWidth);
3926 }
3927 break;
3928 }
3929 case ISD::LOAD: {
3931 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3932 if (ISD::isNON_EXTLoad(LD) && Cst) {
3933 // Determine any common known bits from the loaded constant pool value.
3934 Type *CstTy = Cst->getType();
3935 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3936 !Op.getValueType().isScalableVector()) {
3937 // If its a vector splat, then we can (quickly) reuse the scalar path.
3938 // NOTE: We assume all elements match and none are UNDEF.
3939 if (CstTy->isVectorTy()) {
3940 if (const Constant *Splat = Cst->getSplatValue()) {
3941 Cst = Splat;
3942 CstTy = Cst->getType();
3943 }
3944 }
3945 // TODO - do we need to handle different bitwidths?
3946 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3947 // Iterate across all vector elements finding common known bits.
3948 Known.setAllConflict();
3949 for (unsigned i = 0; i != NumElts; ++i) {
3950 if (!DemandedElts[i])
3951 continue;
3952 if (Constant *Elt = Cst->getAggregateElement(i)) {
3953 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3954 const APInt &Value = CInt->getValue();
3955 Known.One &= Value;
3956 Known.Zero &= ~Value;
3957 continue;
3958 }
3959 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3960 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3961 Known.One &= Value;
3962 Known.Zero &= ~Value;
3963 continue;
3964 }
3965 }
3966 Known.One.clearAllBits();
3967 Known.Zero.clearAllBits();
3968 break;
3969 }
3970 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3971 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3972 Known = KnownBits::makeConstant(CInt->getValue());
3973 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3974 Known =
3975 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3976 }
3977 }
3978 }
3979 } else if (Op.getResNo() == 0) {
3980 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
3981 KnownBits KnownScalarMemory(ScalarMemorySize);
3982 if (const MDNode *MD = LD->getRanges())
3983 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
3984
3985 // Extend the Known bits from memory to the size of the scalar result.
3986 if (ISD::isZEXTLoad(Op.getNode()))
3987 Known = KnownScalarMemory.zext(BitWidth);
3988 else if (ISD::isSEXTLoad(Op.getNode()))
3989 Known = KnownScalarMemory.sext(BitWidth);
3990 else if (ISD::isEXTLoad(Op.getNode()))
3991 Known = KnownScalarMemory.anyext(BitWidth);
3992 else
3993 Known = KnownScalarMemory;
3994 assert(Known.getBitWidth() == BitWidth);
3995 return Known;
3996 }
3997 break;
3998 }
4000 if (Op.getValueType().isScalableVector())
4001 break;
4002 EVT InVT = Op.getOperand(0).getValueType();
4003 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4004 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4005 Known = Known.zext(BitWidth);
4006 break;
4007 }
4008 case ISD::ZERO_EXTEND: {
4009 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4010 Known = Known.zext(BitWidth);
4011 break;
4012 }
4014 if (Op.getValueType().isScalableVector())
4015 break;
4016 EVT InVT = Op.getOperand(0).getValueType();
4017 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4018 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4019 // If the sign bit is known to be zero or one, then sext will extend
4020 // it to the top bits, else it will just zext.
4021 Known = Known.sext(BitWidth);
4022 break;
4023 }
4024 case ISD::SIGN_EXTEND: {
4025 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4026 // If the sign bit is known to be zero or one, then sext will extend
4027 // it to the top bits, else it will just zext.
4028 Known = Known.sext(BitWidth);
4029 break;
4030 }
4032 if (Op.getValueType().isScalableVector())
4033 break;
4034 EVT InVT = Op.getOperand(0).getValueType();
4035 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4036 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4037 Known = Known.anyext(BitWidth);
4038 break;
4039 }
4040 case ISD::ANY_EXTEND: {
4041 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4042 Known = Known.anyext(BitWidth);
4043 break;
4044 }
4045 case ISD::TRUNCATE: {
4046 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4047 Known = Known.trunc(BitWidth);
4048 break;
4049 }
4050 case ISD::TRUNCATE_SSAT_S: {
4051 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4052 Known = Known.truncSSat(BitWidth);
4053 break;
4054 }
4055 case ISD::TRUNCATE_SSAT_U: {
4056 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4057 Known = Known.truncSSatU(BitWidth);
4058 break;
4059 }
4060 case ISD::TRUNCATE_USAT_U: {
4061 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4062 Known = Known.truncUSat(BitWidth);
4063 break;
4064 }
4065 case ISD::AssertZext: {
4066 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4068 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4069 Known.Zero |= (~InMask);
4070 Known.One &= (~Known.Zero);
4071 break;
4072 }
4073 case ISD::AssertAlign: {
4074 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4075 assert(LogOfAlign != 0);
4076
4077 // TODO: Should use maximum with source
4078 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4079 // well as clearing one bits.
4080 Known.Zero.setLowBits(LogOfAlign);
4081 Known.One.clearLowBits(LogOfAlign);
4082 break;
4083 }
4084 case ISD::AssertNoFPClass: {
4085 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4086
4087 FPClassTest NoFPClass =
4088 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4089 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4090 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4091 // Cannot be negative.
4092 Known.makeNonNegative();
4093 }
4094
4095 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4096 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4097 // Cannot be positive.
4098 Known.makeNegative();
4099 }
4100
4101 break;
4102 }
4103 case ISD::FGETSIGN:
4104 // All bits are zero except the low bit.
4105 Known.Zero.setBitsFrom(1);
4106 break;
4107 case ISD::ADD:
4108 case ISD::SUB: {
4109 SDNodeFlags Flags = Op.getNode()->getFlags();
4110 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4111 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4113 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4114 Flags.hasNoUnsignedWrap(), Known, Known2);
4115 break;
4116 }
4117 case ISD::USUBO:
4118 case ISD::SSUBO:
4119 case ISD::USUBO_CARRY:
4120 case ISD::SSUBO_CARRY:
4121 if (Op.getResNo() == 1) {
4122 // If we know the result of a setcc has the top bits zero, use this info.
4123 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4125 BitWidth > 1)
4126 Known.Zero.setBitsFrom(1);
4127 break;
4128 }
4129 [[fallthrough]];
4130 case ISD::SUBC: {
4131 assert(Op.getResNo() == 0 &&
4132 "We only compute knownbits for the difference here.");
4133
4134 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4135 KnownBits Borrow(1);
4136 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4137 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4138 // Borrow has bit width 1
4139 Borrow = Borrow.trunc(1);
4140 } else {
4141 Borrow.setAllZero();
4142 }
4143
4144 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4145 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4146 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4147 break;
4148 }
4149 case ISD::UADDO:
4150 case ISD::SADDO:
4151 case ISD::UADDO_CARRY:
4152 case ISD::SADDO_CARRY:
4153 if (Op.getResNo() == 1) {
4154 // If we know the result of a setcc has the top bits zero, use this info.
4155 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4157 BitWidth > 1)
4158 Known.Zero.setBitsFrom(1);
4159 break;
4160 }
4161 [[fallthrough]];
4162 case ISD::ADDC:
4163 case ISD::ADDE: {
4164 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4165
4166 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4167 KnownBits Carry(1);
4168 if (Opcode == ISD::ADDE)
4169 // Can't track carry from glue, set carry to unknown.
4170 Carry.resetAll();
4171 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4172 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4173 // Carry has bit width 1
4174 Carry = Carry.trunc(1);
4175 } else {
4176 Carry.setAllZero();
4177 }
4178
4179 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4180 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4181 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4182 break;
4183 }
4184 case ISD::UDIV: {
4185 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4186 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4187 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4188 break;
4189 }
4190 case ISD::SDIV: {
4191 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4192 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4193 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4194 break;
4195 }
4196 case ISD::SREM: {
4197 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4198 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4199 Known = KnownBits::srem(Known, Known2);
4200 break;
4201 }
4202 case ISD::UREM: {
4203 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4204 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4205 Known = KnownBits::urem(Known, Known2);
4206 break;
4207 }
4208 case ISD::EXTRACT_ELEMENT: {
4209 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4210 const unsigned Index = Op.getConstantOperandVal(1);
4211 const unsigned EltBitWidth = Op.getValueSizeInBits();
4212
4213 // Remove low part of known bits mask
4214 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4215 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4216
4217 // Remove high part of known bit mask
4218 Known = Known.trunc(EltBitWidth);
4219 break;
4220 }
4222 SDValue InVec = Op.getOperand(0);
4223 SDValue EltNo = Op.getOperand(1);
4224 EVT VecVT = InVec.getValueType();
4225 // computeKnownBits not yet implemented for scalable vectors.
4226 if (VecVT.isScalableVector())
4227 break;
4228 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4229 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4230
4231 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4232 // anything about the extended bits.
4233 if (BitWidth > EltBitWidth)
4234 Known = Known.trunc(EltBitWidth);
4235
4236 // If we know the element index, just demand that vector element, else for
4237 // an unknown element index, ignore DemandedElts and demand them all.
4238 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4239 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4240 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4241 DemandedSrcElts =
4242 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4243
4244 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4245 if (BitWidth > EltBitWidth)
4246 Known = Known.anyext(BitWidth);
4247 break;
4248 }
4250 if (Op.getValueType().isScalableVector())
4251 break;
4252
4253 // If we know the element index, split the demand between the
4254 // source vector and the inserted element, otherwise assume we need
4255 // the original demanded vector elements and the value.
4256 SDValue InVec = Op.getOperand(0);
4257 SDValue InVal = Op.getOperand(1);
4258 SDValue EltNo = Op.getOperand(2);
4259 bool DemandedVal = true;
4260 APInt DemandedVecElts = DemandedElts;
4261 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4262 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4263 unsigned EltIdx = CEltNo->getZExtValue();
4264 DemandedVal = !!DemandedElts[EltIdx];
4265 DemandedVecElts.clearBit(EltIdx);
4266 }
4267 Known.setAllConflict();
4268 if (DemandedVal) {
4269 Known2 = computeKnownBits(InVal, Depth + 1);
4270 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4271 }
4272 if (!!DemandedVecElts) {
4273 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4274 Known = Known.intersectWith(Known2);
4275 }
4276 break;
4277 }
4278 case ISD::BITREVERSE: {
4279 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4280 Known = Known2.reverseBits();
4281 break;
4282 }
4283 case ISD::BSWAP: {
4284 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4285 Known = Known2.byteSwap();
4286 break;
4287 }
4288 case ISD::ABS: {
4289 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4290 Known = Known2.abs();
4291 Known.Zero.setHighBits(
4292 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4293 break;
4294 }
4295 case ISD::USUBSAT: {
4296 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4297 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4298 Known = KnownBits::usub_sat(Known, Known2);
4299 break;
4300 }
4301 case ISD::UMIN: {
4302 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4303 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4304 Known = KnownBits::umin(Known, Known2);
4305 break;
4306 }
4307 case ISD::UMAX: {
4308 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4309 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4310 Known = KnownBits::umax(Known, Known2);
4311 break;
4312 }
4313 case ISD::SMIN:
4314 case ISD::SMAX: {
4315 // If we have a clamp pattern, we know that the number of sign bits will be
4316 // the minimum of the clamp min/max range.
4317 bool IsMax = (Opcode == ISD::SMAX);
4318 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4319 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4320 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4321 CstHigh =
4322 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4323 if (CstLow && CstHigh) {
4324 if (!IsMax)
4325 std::swap(CstLow, CstHigh);
4326
4327 const APInt &ValueLow = CstLow->getAPIntValue();
4328 const APInt &ValueHigh = CstHigh->getAPIntValue();
4329 if (ValueLow.sle(ValueHigh)) {
4330 unsigned LowSignBits = ValueLow.getNumSignBits();
4331 unsigned HighSignBits = ValueHigh.getNumSignBits();
4332 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4333 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4334 Known.One.setHighBits(MinSignBits);
4335 break;
4336 }
4337 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4338 Known.Zero.setHighBits(MinSignBits);
4339 break;
4340 }
4341 }
4342 }
4343
4344 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4345 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4346 if (IsMax)
4347 Known = KnownBits::smax(Known, Known2);
4348 else
4349 Known = KnownBits::smin(Known, Known2);
4350
4351 // For SMAX, if CstLow is non-negative we know the result will be
4352 // non-negative and thus all sign bits are 0.
4353 // TODO: There's an equivalent of this for smin with negative constant for
4354 // known ones.
4355 if (IsMax && CstLow) {
4356 const APInt &ValueLow = CstLow->getAPIntValue();
4357 if (ValueLow.isNonNegative()) {
4358 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4359 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4360 }
4361 }
4362
4363 break;
4364 }
4365 case ISD::UINT_TO_FP: {
4366 Known.makeNonNegative();
4367 break;
4368 }
4369 case ISD::SINT_TO_FP: {
4370 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4371 if (Known2.isNonNegative())
4372 Known.makeNonNegative();
4373 else if (Known2.isNegative())
4374 Known.makeNegative();
4375 break;
4376 }
4377 case ISD::FP_TO_UINT_SAT: {
4378 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4379 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4381 break;
4382 }
4383 case ISD::ATOMIC_LOAD: {
4384 // If we are looking at the loaded value.
4385 if (Op.getResNo() == 0) {
4386 auto *AT = cast<AtomicSDNode>(Op);
4387 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4388 KnownBits KnownScalarMemory(ScalarMemorySize);
4389 if (const MDNode *MD = AT->getRanges())
4390 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4391
4392 switch (AT->getExtensionType()) {
4393 case ISD::ZEXTLOAD:
4394 Known = KnownScalarMemory.zext(BitWidth);
4395 break;
4396 case ISD::SEXTLOAD:
4397 Known = KnownScalarMemory.sext(BitWidth);
4398 break;
4399 case ISD::EXTLOAD:
4400 switch (TLI->getExtendForAtomicOps()) {
4401 case ISD::ZERO_EXTEND:
4402 Known = KnownScalarMemory.zext(BitWidth);
4403 break;
4404 case ISD::SIGN_EXTEND:
4405 Known = KnownScalarMemory.sext(BitWidth);
4406 break;
4407 default:
4408 Known = KnownScalarMemory.anyext(BitWidth);
4409 break;
4410 }
4411 break;
4412 case ISD::NON_EXTLOAD:
4413 Known = KnownScalarMemory;
4414 break;
4415 }
4416 assert(Known.getBitWidth() == BitWidth);
4417 }
4418 break;
4419 }
4421 if (Op.getResNo() == 1) {
4422 // The boolean result conforms to getBooleanContents.
4423 // If we know the result of a setcc has the top bits zero, use this info.
4424 // We know that we have an integer-based boolean since these operations
4425 // are only available for integer.
4426 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4428 BitWidth > 1)
4429 Known.Zero.setBitsFrom(1);
4430 break;
4431 }
4432 [[fallthrough]];
4434 case ISD::ATOMIC_SWAP:
4445 case ISD::ATOMIC_LOAD_UMAX: {
4446 // If we are looking at the loaded value.
4447 if (Op.getResNo() == 0) {
4448 auto *AT = cast<AtomicSDNode>(Op);
4449 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4450
4451 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4452 Known.Zero.setBitsFrom(MemBits);
4453 }
4454 break;
4455 }
4456 case ISD::FrameIndex:
4458 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4459 Known, getMachineFunction());
4460 break;
4461
4462 default:
4463 if (Opcode < ISD::BUILTIN_OP_END)
4464 break;
4465 [[fallthrough]];
4469 // TODO: Probably okay to remove after audit; here to reduce change size
4470 // in initial enablement patch for scalable vectors
4471 if (Op.getValueType().isScalableVector())
4472 break;
4473
4474 // Allow the target to implement this method for its nodes.
4475 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4476 break;
4477 }
4478
4479 return Known;
4480}
4481
4482/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4495
4498 // X + 0 never overflow
4499 if (isNullConstant(N1))
4500 return OFK_Never;
4501
4502 // If both operands each have at least two sign bits, the addition
4503 // cannot overflow.
4504 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4505 return OFK_Never;
4506
4507 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4508 return OFK_Sometime;
4509}
4510
4513 // X + 0 never overflow
4514 if (isNullConstant(N1))
4515 return OFK_Never;
4516
4517 // mulhi + 1 never overflow
4518 KnownBits N1Known = computeKnownBits(N1);
4519 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4520 N1Known.getMaxValue().ult(2))
4521 return OFK_Never;
4522
4523 KnownBits N0Known = computeKnownBits(N0);
4524 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4525 N0Known.getMaxValue().ult(2))
4526 return OFK_Never;
4527
4528 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4529 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4530 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4531 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4532}
4533
4536 // X - 0 never overflow
4537 if (isNullConstant(N1))
4538 return OFK_Never;
4539
4540 // If both operands each have at least two sign bits, the subtraction
4541 // cannot overflow.
4542 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4543 return OFK_Never;
4544
4545 KnownBits N0Known = computeKnownBits(N0);
4546 KnownBits N1Known = computeKnownBits(N1);
4547 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4548 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4549 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4550}
4551
4554 // X - 0 never overflow
4555 if (isNullConstant(N1))
4556 return OFK_Never;
4557
4558 KnownBits N0Known = computeKnownBits(N0);
4559 KnownBits N1Known = computeKnownBits(N1);
4560 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4561 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4562 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4563}
4564
4567 // X * 0 and X * 1 never overflow.
4568 if (isNullConstant(N1) || isOneConstant(N1))
4569 return OFK_Never;
4570
4571 KnownBits N0Known = computeKnownBits(N0);
4572 KnownBits N1Known = computeKnownBits(N1);
4573 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4574 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4575 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4576}
4577
4580 // X * 0 and X * 1 never overflow.
4581 if (isNullConstant(N1) || isOneConstant(N1))
4582 return OFK_Never;
4583
4584 // Get the size of the result.
4585 unsigned BitWidth = N0.getScalarValueSizeInBits();
4586
4587 // Sum of the sign bits.
4588 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4589
4590 // If we have enough sign bits, then there's no overflow.
4591 if (SignBits > BitWidth + 1)
4592 return OFK_Never;
4593
4594 if (SignBits == BitWidth + 1) {
4595 // The overflow occurs when the true multiplication of the
4596 // the operands is the minimum negative number.
4597 KnownBits N0Known = computeKnownBits(N0);
4598 KnownBits N1Known = computeKnownBits(N1);
4599 // If one of the operands is non-negative, then there's no
4600 // overflow.
4601 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4602 return OFK_Never;
4603 }
4604
4605 return OFK_Sometime;
4606}
4607
4609 if (Depth >= MaxRecursionDepth)
4610 return false; // Limit search depth.
4611
4612 EVT OpVT = Val.getValueType();
4613 unsigned BitWidth = OpVT.getScalarSizeInBits();
4614
4615 // Is the constant a known power of 2?
4617 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4618 }))
4619 return true;
4620
4621 // A left-shift of a constant one will have exactly one bit set because
4622 // shifting the bit off the end is undefined.
4623 if (Val.getOpcode() == ISD::SHL) {
4624 auto *C = isConstOrConstSplat(Val.getOperand(0));
4625 if (C && C->getAPIntValue() == 1)
4626 return true;
4627 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4628 isKnownNeverZero(Val, Depth);
4629 }
4630
4631 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4632 // one bit set.
4633 if (Val.getOpcode() == ISD::SRL) {
4634 auto *C = isConstOrConstSplat(Val.getOperand(0));
4635 if (C && C->getAPIntValue().isSignMask())
4636 return true;
4637 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4638 isKnownNeverZero(Val, Depth);
4639 }
4640
4641 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4642 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4643
4644 // Are all operands of a build vector constant powers of two?
4645 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4646 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4647 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4648 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4649 return false;
4650 }))
4651 return true;
4652
4653 // Is the operand of a splat vector a constant power of two?
4654 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4656 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4657 return true;
4658
4659 // vscale(power-of-two) is a power-of-two for some targets
4660 if (Val.getOpcode() == ISD::VSCALE &&
4661 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4663 return true;
4664
4665 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4666 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4667 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4669
4670 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4671 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4673
4674 // Looking for `x & -x` pattern:
4675 // If x == 0:
4676 // x & -x -> 0
4677 // If x != 0:
4678 // x & -x -> non-zero pow2
4679 // so if we find the pattern return whether we know `x` is non-zero.
4680 SDValue X;
4681 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4682 return isKnownNeverZero(X, Depth);
4683
4684 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4685 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4686
4687 // More could be done here, though the above checks are enough
4688 // to handle some common cases.
4689 return false;
4690}
4691
4693 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4694 return C1->getValueAPF().getExactLog2Abs() >= 0;
4695
4696 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4697 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4698
4699 return false;
4700}
4701
4703 EVT VT = Op.getValueType();
4704
4705 // Since the number of lanes in a scalable vector is unknown at compile time,
4706 // we track one bit which is implicitly broadcast to all lanes. This means
4707 // that all lanes in a scalable vector are considered demanded.
4708 APInt DemandedElts = VT.isFixedLengthVector()
4710 : APInt(1, 1);
4711 return ComputeNumSignBits(Op, DemandedElts, Depth);
4712}
4713
4714unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4715 unsigned Depth) const {
4716 EVT VT = Op.getValueType();
4717 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4718 unsigned VTBits = VT.getScalarSizeInBits();
4719 unsigned NumElts = DemandedElts.getBitWidth();
4720 unsigned Tmp, Tmp2;
4721 unsigned FirstAnswer = 1;
4722
4723 assert((!VT.isScalableVector() || NumElts == 1) &&
4724 "DemandedElts for scalable vectors must be 1 to represent all lanes");
4725
4726 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4727 const APInt &Val = C->getAPIntValue();
4728 return Val.getNumSignBits();
4729 }
4730
4731 if (Depth >= MaxRecursionDepth)
4732 return 1; // Limit search depth.
4733
4734 if (!DemandedElts)
4735 return 1; // No demanded elts, better to assume we don't know anything.
4736
4737 unsigned Opcode = Op.getOpcode();
4738 switch (Opcode) {
4739 default: break;
4740 case ISD::AssertSext:
4741 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4742 return VTBits-Tmp+1;
4743 case ISD::AssertZext:
4744 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4745 return VTBits-Tmp;
4746 case ISD::FREEZE:
4747 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4748 /*PoisonOnly=*/false))
4749 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4750 break;
4751 case ISD::MERGE_VALUES:
4752 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4753 Depth + 1);
4754 case ISD::SPLAT_VECTOR: {
4755 // Check if the sign bits of source go down as far as the truncated value.
4756 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4757 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4758 if (NumSrcSignBits > (NumSrcBits - VTBits))
4759 return NumSrcSignBits - (NumSrcBits - VTBits);
4760 break;
4761 }
4762 case ISD::BUILD_VECTOR:
4763 assert(!VT.isScalableVector());
4764 Tmp = VTBits;
4765 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4766 if (!DemandedElts[i])
4767 continue;
4768
4769 SDValue SrcOp = Op.getOperand(i);
4770 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4771 // for constant nodes to ensure we only look at the sign bits.
4773 APInt T = C->getAPIntValue().trunc(VTBits);
4774 Tmp2 = T.getNumSignBits();
4775 } else {
4776 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4777
4778 if (SrcOp.getValueSizeInBits() != VTBits) {
4779 assert(SrcOp.getValueSizeInBits() > VTBits &&
4780 "Expected BUILD_VECTOR implicit truncation");
4781 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4782 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4783 }
4784 }
4785 Tmp = std::min(Tmp, Tmp2);
4786 }
4787 return Tmp;
4788
4789 case ISD::VECTOR_COMPRESS: {
4790 SDValue Vec = Op.getOperand(0);
4791 SDValue PassThru = Op.getOperand(2);
4792 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4793 if (Tmp == 1)
4794 return 1;
4795 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4796 Tmp = std::min(Tmp, Tmp2);
4797 return Tmp;
4798 }
4799
4800 case ISD::VECTOR_SHUFFLE: {
4801 // Collect the minimum number of sign bits that are shared by every vector
4802 // element referenced by the shuffle.
4803 APInt DemandedLHS, DemandedRHS;
4805 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4806 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4807 DemandedLHS, DemandedRHS))
4808 return 1;
4809
4810 Tmp = std::numeric_limits<unsigned>::max();
4811 if (!!DemandedLHS)
4812 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4813 if (!!DemandedRHS) {
4814 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4815 Tmp = std::min(Tmp, Tmp2);
4816 }
4817 // If we don't know anything, early out and try computeKnownBits fall-back.
4818 if (Tmp == 1)
4819 break;
4820 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4821 return Tmp;
4822 }
4823
4824 case ISD::BITCAST: {
4825 if (VT.isScalableVector())
4826 break;
4827 SDValue N0 = Op.getOperand(0);
4828 EVT SrcVT = N0.getValueType();
4829 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4830
4831 // Ignore bitcasts from unsupported types..
4832 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4833 break;
4834
4835 // Fast handling of 'identity' bitcasts.
4836 if (VTBits == SrcBits)
4837 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4838
4839 bool IsLE = getDataLayout().isLittleEndian();
4840
4841 // Bitcast 'large element' scalar/vector to 'small element' vector.
4842 if ((SrcBits % VTBits) == 0) {
4843 assert(VT.isVector() && "Expected bitcast to vector");
4844
4845 unsigned Scale = SrcBits / VTBits;
4846 APInt SrcDemandedElts =
4847 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4848
4849 // Fast case - sign splat can be simply split across the small elements.
4850 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4851 if (Tmp == SrcBits)
4852 return VTBits;
4853
4854 // Slow case - determine how far the sign extends into each sub-element.
4855 Tmp2 = VTBits;
4856 for (unsigned i = 0; i != NumElts; ++i)
4857 if (DemandedElts[i]) {
4858 unsigned SubOffset = i % Scale;
4859 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4860 SubOffset = SubOffset * VTBits;
4861 if (Tmp <= SubOffset)
4862 return 1;
4863 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4864 }
4865 return Tmp2;
4866 }
4867 break;
4868 }
4869
4871 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4872 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4873 return VTBits - Tmp + 1;
4874 case ISD::SIGN_EXTEND:
4875 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4876 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4878 // Max of the input and what this extends.
4879 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4880 Tmp = VTBits-Tmp+1;
4881 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4882 return std::max(Tmp, Tmp2);
4884 if (VT.isScalableVector())
4885 break;
4886 SDValue Src = Op.getOperand(0);
4887 EVT SrcVT = Src.getValueType();
4888 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4889 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4890 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4891 }
4892 case ISD::SRA:
4893 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4894 // SRA X, C -> adds C sign bits.
4895 if (std::optional<unsigned> ShAmt =
4896 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4897 Tmp = std::min(Tmp + *ShAmt, VTBits);
4898 return Tmp;
4899 case ISD::SHL:
4900 if (std::optional<ConstantRange> ShAmtRange =
4901 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4902 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4903 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4904 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4905 // shifted out, then we can compute the number of sign bits for the
4906 // operand being extended. A future improvement could be to pass along the
4907 // "shifted left by" information in the recursive calls to
4908 // ComputeKnownSignBits. Allowing us to handle this more generically.
4909 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4910 SDValue Ext = Op.getOperand(0);
4911 EVT ExtVT = Ext.getValueType();
4912 SDValue Extendee = Ext.getOperand(0);
4913 EVT ExtendeeVT = Extendee.getValueType();
4914 unsigned SizeDifference =
4915 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4916 if (SizeDifference <= MinShAmt) {
4917 Tmp = SizeDifference +
4918 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4919 if (MaxShAmt < Tmp)
4920 return Tmp - MaxShAmt;
4921 }
4922 }
4923 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4924 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4925 if (MaxShAmt < Tmp)
4926 return Tmp - MaxShAmt;
4927 }
4928 break;
4929 case ISD::AND:
4930 case ISD::OR:
4931 case ISD::XOR: // NOT is handled here.
4932 // Logical binary ops preserve the number of sign bits at the worst.
4933 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4934 if (Tmp != 1) {
4935 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4936 FirstAnswer = std::min(Tmp, Tmp2);
4937 // We computed what we know about the sign bits as our first
4938 // answer. Now proceed to the generic code that uses
4939 // computeKnownBits, and pick whichever answer is better.
4940 }
4941 break;
4942
4943 case ISD::SELECT:
4944 case ISD::VSELECT:
4945 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4946 if (Tmp == 1) return 1; // Early out.
4947 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4948 return std::min(Tmp, Tmp2);
4949 case ISD::SELECT_CC:
4950 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4951 if (Tmp == 1) return 1; // Early out.
4952 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4953 return std::min(Tmp, Tmp2);
4954
4955 case ISD::SMIN:
4956 case ISD::SMAX: {
4957 // If we have a clamp pattern, we know that the number of sign bits will be
4958 // the minimum of the clamp min/max range.
4959 bool IsMax = (Opcode == ISD::SMAX);
4960 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4961 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4962 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4963 CstHigh =
4964 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4965 if (CstLow && CstHigh) {
4966 if (!IsMax)
4967 std::swap(CstLow, CstHigh);
4968 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4969 Tmp = CstLow->getAPIntValue().getNumSignBits();
4970 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4971 return std::min(Tmp, Tmp2);
4972 }
4973 }
4974
4975 // Fallback - just get the minimum number of sign bits of the operands.
4976 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4977 if (Tmp == 1)
4978 return 1; // Early out.
4979 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4980 return std::min(Tmp, Tmp2);
4981 }
4982 case ISD::UMIN:
4983 case ISD::UMAX:
4984 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4985 if (Tmp == 1)
4986 return 1; // Early out.
4987 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4988 return std::min(Tmp, Tmp2);
4989 case ISD::SSUBO_CARRY:
4990 case ISD::USUBO_CARRY:
4991 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4992 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4993 return VTBits;
4994 [[fallthrough]];
4995 case ISD::SADDO:
4996 case ISD::UADDO:
4997 case ISD::SADDO_CARRY:
4998 case ISD::UADDO_CARRY:
4999 case ISD::SSUBO:
5000 case ISD::USUBO:
5001 case ISD::SMULO:
5002 case ISD::UMULO:
5003 if (Op.getResNo() != 1)
5004 break;
5005 // The boolean result conforms to getBooleanContents. Fall through.
5006 // If setcc returns 0/-1, all bits are sign bits.
5007 // We know that we have an integer-based boolean since these operations
5008 // are only available for integer.
5009 if (TLI->getBooleanContents(VT.isVector(), false) ==
5011 return VTBits;
5012 break;
5013 case ISD::SETCC:
5014 case ISD::SETCCCARRY:
5015 case ISD::STRICT_FSETCC:
5016 case ISD::STRICT_FSETCCS: {
5017 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5018 // If setcc returns 0/-1, all bits are sign bits.
5019 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5021 return VTBits;
5022 break;
5023 }
5024 case ISD::ROTL:
5025 case ISD::ROTR:
5026 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5027
5028 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5029 if (Tmp == VTBits)
5030 return VTBits;
5031
5032 if (ConstantSDNode *C =
5033 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5034 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5035
5036 // Handle rotate right by N like a rotate left by 32-N.
5037 if (Opcode == ISD::ROTR)
5038 RotAmt = (VTBits - RotAmt) % VTBits;
5039
5040 // If we aren't rotating out all of the known-in sign bits, return the
5041 // number that are left. This handles rotl(sext(x), 1) for example.
5042 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5043 }
5044 break;
5045 case ISD::ADD:
5046 case ISD::ADDC:
5047 // TODO: Move Operand 1 check before Operand 0 check
5048 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5049 if (Tmp == 1) return 1; // Early out.
5050
5051 // Special case decrementing a value (ADD X, -1):
5052 if (ConstantSDNode *CRHS =
5053 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5054 if (CRHS->isAllOnes()) {
5055 KnownBits Known =
5056 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5057
5058 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5059 // sign bits set.
5060 if ((Known.Zero | 1).isAllOnes())
5061 return VTBits;
5062
5063 // If we are subtracting one from a positive number, there is no carry
5064 // out of the result.
5065 if (Known.isNonNegative())
5066 return Tmp;
5067 }
5068
5069 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5070 if (Tmp2 == 1) return 1; // Early out.
5071
5072 // Add can have at most one carry bit. Thus we know that the output
5073 // is, at worst, one more bit than the inputs.
5074 return std::min(Tmp, Tmp2) - 1;
5075 case ISD::SUB:
5076 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5077 if (Tmp2 == 1) return 1; // Early out.
5078
5079 // Handle NEG.
5080 if (ConstantSDNode *CLHS =
5081 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5082 if (CLHS->isZero()) {
5083 KnownBits Known =
5084 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5085 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5086 // sign bits set.
5087 if ((Known.Zero | 1).isAllOnes())
5088 return VTBits;
5089
5090 // If the input is known to be positive (the sign bit is known clear),
5091 // the output of the NEG has the same number of sign bits as the input.
5092 if (Known.isNonNegative())
5093 return Tmp2;
5094
5095 // Otherwise, we treat this like a SUB.
5096 }
5097
5098 // Sub can have at most one carry bit. Thus we know that the output
5099 // is, at worst, one more bit than the inputs.
5100 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5101 if (Tmp == 1) return 1; // Early out.
5102 return std::min(Tmp, Tmp2) - 1;
5103 case ISD::MUL: {
5104 // The output of the Mul can be at most twice the valid bits in the inputs.
5105 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5106 if (SignBitsOp0 == 1)
5107 break;
5108 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5109 if (SignBitsOp1 == 1)
5110 break;
5111 unsigned OutValidBits =
5112 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5113 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5114 }
5115 case ISD::AVGCEILS:
5116 case ISD::AVGFLOORS:
5117 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5118 if (Tmp == 1)
5119 return 1; // Early out.
5120 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5121 return std::min(Tmp, Tmp2);
5122 case ISD::SREM:
5123 // The sign bit is the LHS's sign bit, except when the result of the
5124 // remainder is zero. The magnitude of the result should be less than or
5125 // equal to the magnitude of the LHS. Therefore, the result should have
5126 // at least as many sign bits as the left hand side.
5127 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5128 case ISD::TRUNCATE: {
5129 // Check if the sign bits of source go down as far as the truncated value.
5130 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5131 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5132 if (NumSrcSignBits > (NumSrcBits - VTBits))
5133 return NumSrcSignBits - (NumSrcBits - VTBits);
5134 break;
5135 }
5136 case ISD::EXTRACT_ELEMENT: {
5137 if (VT.isScalableVector())
5138 break;
5139 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5140 const int BitWidth = Op.getValueSizeInBits();
5141 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5142
5143 // Get reverse index (starting from 1), Op1 value indexes elements from
5144 // little end. Sign starts at big end.
5145 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5146
5147 // If the sign portion ends in our element the subtraction gives correct
5148 // result. Otherwise it gives either negative or > bitwidth result
5149 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5150 }
5152 if (VT.isScalableVector())
5153 break;
5154 // If we know the element index, split the demand between the
5155 // source vector and the inserted element, otherwise assume we need
5156 // the original demanded vector elements and the value.
5157 SDValue InVec = Op.getOperand(0);
5158 SDValue InVal = Op.getOperand(1);
5159 SDValue EltNo = Op.getOperand(2);
5160 bool DemandedVal = true;
5161 APInt DemandedVecElts = DemandedElts;
5162 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5163 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5164 unsigned EltIdx = CEltNo->getZExtValue();
5165 DemandedVal = !!DemandedElts[EltIdx];
5166 DemandedVecElts.clearBit(EltIdx);
5167 }
5168 Tmp = std::numeric_limits<unsigned>::max();
5169 if (DemandedVal) {
5170 // TODO - handle implicit truncation of inserted elements.
5171 if (InVal.getScalarValueSizeInBits() != VTBits)
5172 break;
5173 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5174 Tmp = std::min(Tmp, Tmp2);
5175 }
5176 if (!!DemandedVecElts) {
5177 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5178 Tmp = std::min(Tmp, Tmp2);
5179 }
5180 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5181 return Tmp;
5182 }
5184 SDValue InVec = Op.getOperand(0);
5185 SDValue EltNo = Op.getOperand(1);
5186 EVT VecVT = InVec.getValueType();
5187 // ComputeNumSignBits not yet implemented for scalable vectors.
5188 if (VecVT.isScalableVector())
5189 break;
5190 const unsigned BitWidth = Op.getValueSizeInBits();
5191 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5192 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5193
5194 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5195 // anything about sign bits. But if the sizes match we can derive knowledge
5196 // about sign bits from the vector operand.
5197 if (BitWidth != EltBitWidth)
5198 break;
5199
5200 // If we know the element index, just demand that vector element, else for
5201 // an unknown element index, ignore DemandedElts and demand them all.
5202 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5203 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5204 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5205 DemandedSrcElts =
5206 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5207
5208 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5209 }
5211 // Offset the demanded elts by the subvector index.
5212 SDValue Src = Op.getOperand(0);
5213
5214 APInt DemandedSrcElts;
5215 if (Src.getValueType().isScalableVector())
5216 DemandedSrcElts = APInt(1, 1);
5217 else {
5218 uint64_t Idx = Op.getConstantOperandVal(1);
5219 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5220 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5221 }
5222 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5223 }
5224 case ISD::CONCAT_VECTORS: {
5225 if (VT.isScalableVector())
5226 break;
5227 // Determine the minimum number of sign bits across all demanded
5228 // elts of the input vectors. Early out if the result is already 1.
5229 Tmp = std::numeric_limits<unsigned>::max();
5230 EVT SubVectorVT = Op.getOperand(0).getValueType();
5231 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5232 unsigned NumSubVectors = Op.getNumOperands();
5233 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5234 APInt DemandedSub =
5235 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5236 if (!DemandedSub)
5237 continue;
5238 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5239 Tmp = std::min(Tmp, Tmp2);
5240 }
5241 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5242 return Tmp;
5243 }
5244 case ISD::INSERT_SUBVECTOR: {
5245 if (VT.isScalableVector())
5246 break;
5247 // Demand any elements from the subvector and the remainder from the src its
5248 // inserted into.
5249 SDValue Src = Op.getOperand(0);
5250 SDValue Sub = Op.getOperand(1);
5251 uint64_t Idx = Op.getConstantOperandVal(2);
5252 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5253 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5254 APInt DemandedSrcElts = DemandedElts;
5255 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5256
5257 Tmp = std::numeric_limits<unsigned>::max();
5258 if (!!DemandedSubElts) {
5259 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5260 if (Tmp == 1)
5261 return 1; // early-out
5262 }
5263 if (!!DemandedSrcElts) {
5264 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5265 Tmp = std::min(Tmp, Tmp2);
5266 }
5267 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5268 return Tmp;
5269 }
5270 case ISD::LOAD: {
5271 // If we are looking at the loaded value of the SDNode.
5272 if (Op.getResNo() != 0)
5273 break;
5274
5276 if (const MDNode *Ranges = LD->getRanges()) {
5277 if (DemandedElts != 1)
5278 break;
5279
5281 if (VTBits > CR.getBitWidth()) {
5282 switch (LD->getExtensionType()) {
5283 case ISD::SEXTLOAD:
5284 CR = CR.signExtend(VTBits);
5285 break;
5286 case ISD::ZEXTLOAD:
5287 CR = CR.zeroExtend(VTBits);
5288 break;
5289 default:
5290 break;
5291 }
5292 }
5293
5294 if (VTBits != CR.getBitWidth())
5295 break;
5296 return std::min(CR.getSignedMin().getNumSignBits(),
5298 }
5299
5300 unsigned ExtType = LD->getExtensionType();
5301 switch (ExtType) {
5302 default:
5303 break;
5304 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5305 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5306 return VTBits - Tmp + 1;
5307 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5308 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5309 return VTBits - Tmp;
5310 case ISD::NON_EXTLOAD:
5311 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5312 // We only need to handle vectors - computeKnownBits should handle
5313 // scalar cases.
5314 Type *CstTy = Cst->getType();
5315 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5316 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5317 VTBits == CstTy->getScalarSizeInBits()) {
5318 Tmp = VTBits;
5319 for (unsigned i = 0; i != NumElts; ++i) {
5320 if (!DemandedElts[i])
5321 continue;
5322 if (Constant *Elt = Cst->getAggregateElement(i)) {
5323 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5324 const APInt &Value = CInt->getValue();
5325 Tmp = std::min(Tmp, Value.getNumSignBits());
5326 continue;
5327 }
5328 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5329 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5330 Tmp = std::min(Tmp, Value.getNumSignBits());
5331 continue;
5332 }
5333 }
5334 // Unknown type. Conservatively assume no bits match sign bit.
5335 return 1;
5336 }
5337 return Tmp;
5338 }
5339 }
5340 break;
5341 }
5342
5343 break;
5344 }
5347 case ISD::ATOMIC_SWAP:
5359 case ISD::ATOMIC_LOAD: {
5360 auto *AT = cast<AtomicSDNode>(Op);
5361 // If we are looking at the loaded value.
5362 if (Op.getResNo() == 0) {
5363 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5364 if (Tmp == VTBits)
5365 return 1; // early-out
5366
5367 // For atomic_load, prefer to use the extension type.
5368 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5369 switch (AT->getExtensionType()) {
5370 default:
5371 break;
5372 case ISD::SEXTLOAD:
5373 return VTBits - Tmp + 1;
5374 case ISD::ZEXTLOAD:
5375 return VTBits - Tmp;
5376 }
5377 }
5378
5379 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5380 return VTBits - Tmp + 1;
5381 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5382 return VTBits - Tmp;
5383 }
5384 break;
5385 }
5386 }
5387
5388 // Allow the target to implement this method for its nodes.
5389 if (Opcode >= ISD::BUILTIN_OP_END ||
5390 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5391 Opcode == ISD::INTRINSIC_W_CHAIN ||
5392 Opcode == ISD::INTRINSIC_VOID) {
5393 // TODO: This can probably be removed once target code is audited. This
5394 // is here purely to reduce patch size and review complexity.
5395 if (!VT.isScalableVector()) {
5396 unsigned NumBits =
5397 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5398 if (NumBits > 1)
5399 FirstAnswer = std::max(FirstAnswer, NumBits);
5400 }
5401 }
5402
5403 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5404 // use this information.
5405 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5406 return std::max(FirstAnswer, Known.countMinSignBits());
5407}
5408
5410 unsigned Depth) const {
5411 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5412 return Op.getScalarValueSizeInBits() - SignBits + 1;
5413}
5414
5416 const APInt &DemandedElts,
5417 unsigned Depth) const {
5418 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5419 return Op.getScalarValueSizeInBits() - SignBits + 1;
5420}
5421
5423 unsigned Depth) const {
5424 // Early out for FREEZE.
5425 if (Op.getOpcode() == ISD::FREEZE)
5426 return true;
5427
5428 EVT VT = Op.getValueType();
5429 APInt DemandedElts = VT.isFixedLengthVector()
5431 : APInt(1, 1);
5432 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5433}
5434
5436 const APInt &DemandedElts,
5437 bool PoisonOnly,
5438 unsigned Depth) const {
5439 unsigned Opcode = Op.getOpcode();
5440
5441 // Early out for FREEZE.
5442 if (Opcode == ISD::FREEZE)
5443 return true;
5444
5445 if (Depth >= MaxRecursionDepth)
5446 return false; // Limit search depth.
5447
5448 if (isIntOrFPConstant(Op))
5449 return true;
5450
5451 switch (Opcode) {
5452 case ISD::CONDCODE:
5453 case ISD::VALUETYPE:
5454 case ISD::FrameIndex:
5456 case ISD::CopyFromReg:
5457 return true;
5458
5459 case ISD::POISON:
5460 return false;
5461
5462 case ISD::UNDEF:
5463 return PoisonOnly;
5464
5465 case ISD::BUILD_VECTOR:
5466 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5467 // this shouldn't affect the result.
5468 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5469 if (!DemandedElts[i])
5470 continue;
5472 Depth + 1))
5473 return false;
5474 }
5475 return true;
5476
5478 SDValue Src = Op.getOperand(0);
5479 if (Src.getValueType().isScalableVector())
5480 break;
5481 uint64_t Idx = Op.getConstantOperandVal(1);
5482 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5483 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5484 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5485 Depth + 1);
5486 }
5487
5488 case ISD::INSERT_SUBVECTOR: {
5489 if (Op.getValueType().isScalableVector())
5490 break;
5491 SDValue Src = Op.getOperand(0);
5492 SDValue Sub = Op.getOperand(1);
5493 uint64_t Idx = Op.getConstantOperandVal(2);
5494 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5495 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5496 APInt DemandedSrcElts = DemandedElts;
5497 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5498
5499 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5500 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5501 return false;
5502 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5503 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5504 return false;
5505 return true;
5506 }
5507
5509 SDValue Src = Op.getOperand(0);
5510 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5511 EVT SrcVT = Src.getValueType();
5512 if (SrcVT.isFixedLengthVector() && IndexC &&
5513 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5514 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5515 IndexC->getZExtValue());
5516 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5517 Depth + 1);
5518 }
5519 break;
5520 }
5521
5523 SDValue InVec = Op.getOperand(0);
5524 SDValue InVal = Op.getOperand(1);
5525 SDValue EltNo = Op.getOperand(2);
5526 EVT VT = InVec.getValueType();
5527 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5528 if (IndexC && VT.isFixedLengthVector() &&
5529 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5530 if (DemandedElts[IndexC->getZExtValue()] &&
5532 return false;
5533 APInt InVecDemandedElts = DemandedElts;
5534 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5535 if (!!InVecDemandedElts &&
5537 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5538 InVecDemandedElts, PoisonOnly, Depth + 1))
5539 return false;
5540 return true;
5541 }
5542 break;
5543 }
5544
5546 // Check upper (known undef) elements.
5547 if (DemandedElts.ugt(1) && !PoisonOnly)
5548 return false;
5549 // Check element zero.
5550 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5551 Op.getOperand(0), PoisonOnly, Depth + 1))
5552 return false;
5553 return true;
5554
5555 case ISD::SPLAT_VECTOR:
5556 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5557 Depth + 1);
5558
5559 case ISD::VECTOR_SHUFFLE: {
5560 APInt DemandedLHS, DemandedRHS;
5561 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5562 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5563 DemandedElts, DemandedLHS, DemandedRHS,
5564 /*AllowUndefElts=*/false))
5565 return false;
5566 if (!DemandedLHS.isZero() &&
5567 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5568 PoisonOnly, Depth + 1))
5569 return false;
5570 if (!DemandedRHS.isZero() &&
5571 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5572 PoisonOnly, Depth + 1))
5573 return false;
5574 return true;
5575 }
5576
5577 case ISD::SHL:
5578 case ISD::SRL:
5579 case ISD::SRA:
5580 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5581 // enough to check operand 0 if Op can't create undef/poison.
5582 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5583 /*ConsiderFlags*/ true, Depth) &&
5584 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5585 PoisonOnly, Depth + 1);
5586
5587 case ISD::BSWAP:
5588 case ISD::CTPOP:
5589 case ISD::BITREVERSE:
5590 case ISD::AND:
5591 case ISD::OR:
5592 case ISD::XOR:
5593 case ISD::ADD:
5594 case ISD::SUB:
5595 case ISD::MUL:
5596 case ISD::SADDSAT:
5597 case ISD::UADDSAT:
5598 case ISD::SSUBSAT:
5599 case ISD::USUBSAT:
5600 case ISD::SSHLSAT:
5601 case ISD::USHLSAT:
5602 case ISD::SMIN:
5603 case ISD::SMAX:
5604 case ISD::UMIN:
5605 case ISD::UMAX:
5606 case ISD::ZERO_EXTEND:
5607 case ISD::SIGN_EXTEND:
5608 case ISD::ANY_EXTEND:
5609 case ISD::TRUNCATE:
5610 case ISD::VSELECT: {
5611 // If Op can't create undef/poison and none of its operands are undef/poison
5612 // then Op is never undef/poison. A difference from the more common check
5613 // below, outside the switch, is that we handle elementwise operations for
5614 // which the DemandedElts mask is valid for all operands here.
5615 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5616 /*ConsiderFlags*/ true, Depth) &&
5617 all_of(Op->ops(), [&](SDValue V) {
5618 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5619 PoisonOnly, Depth + 1);
5620 });
5621 }
5622
5623 // TODO: Search for noundef attributes from library functions.
5624
5625 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5626
5627 default:
5628 // Allow the target to implement this method for its nodes.
5629 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5630 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5631 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5632 Op, DemandedElts, *this, PoisonOnly, Depth);
5633 break;
5634 }
5635
5636 // If Op can't create undef/poison and none of its operands are undef/poison
5637 // then Op is never undef/poison.
5638 // NOTE: TargetNodes can handle this in themselves in
5639 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5640 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5641 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5642 Depth) &&
5643 all_of(Op->ops(), [&](SDValue V) {
5644 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5645 });
5646}
5647
5649 bool ConsiderFlags,
5650 unsigned Depth) const {
5651 EVT VT = Op.getValueType();
5652 APInt DemandedElts = VT.isFixedLengthVector()
5654 : APInt(1, 1);
5655 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5656 Depth);
5657}
5658
5660 bool PoisonOnly, bool ConsiderFlags,
5661 unsigned Depth) const {
5662 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5663 return true;
5664
5665 unsigned Opcode = Op.getOpcode();
5666 switch (Opcode) {
5667 case ISD::AssertSext:
5668 case ISD::AssertZext:
5669 case ISD::AssertAlign:
5671 // Assertion nodes can create poison if the assertion fails.
5672 return true;
5673
5674 case ISD::FREEZE:
5678 case ISD::SADDSAT:
5679 case ISD::UADDSAT:
5680 case ISD::SSUBSAT:
5681 case ISD::USUBSAT:
5682 case ISD::MULHU:
5683 case ISD::MULHS:
5684 case ISD::AVGFLOORS:
5685 case ISD::AVGFLOORU:
5686 case ISD::AVGCEILS:
5687 case ISD::AVGCEILU:
5688 case ISD::ABDU:
5689 case ISD::ABDS:
5690 case ISD::SMIN:
5691 case ISD::SMAX:
5692 case ISD::SCMP:
5693 case ISD::UMIN:
5694 case ISD::UMAX:
5695 case ISD::UCMP:
5696 case ISD::AND:
5697 case ISD::XOR:
5698 case ISD::ROTL:
5699 case ISD::ROTR:
5700 case ISD::FSHL:
5701 case ISD::FSHR:
5702 case ISD::BSWAP:
5703 case ISD::CTTZ:
5704 case ISD::CTLZ:
5705 case ISD::CTLS:
5706 case ISD::CTPOP:
5707 case ISD::BITREVERSE:
5708 case ISD::PARITY:
5709 case ISD::SIGN_EXTEND:
5710 case ISD::TRUNCATE:
5714 case ISD::BITCAST:
5715 case ISD::BUILD_VECTOR:
5716 case ISD::BUILD_PAIR:
5717 case ISD::SPLAT_VECTOR:
5718 case ISD::FABS:
5719 return false;
5720
5721 case ISD::ABS:
5722 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5723 // Different to Intrinsic::abs.
5724 return false;
5725
5726 case ISD::ADDC:
5727 case ISD::SUBC:
5728 case ISD::ADDE:
5729 case ISD::SUBE:
5730 case ISD::SADDO:
5731 case ISD::SSUBO:
5732 case ISD::SMULO:
5733 case ISD::SADDO_CARRY:
5734 case ISD::SSUBO_CARRY:
5735 case ISD::UADDO:
5736 case ISD::USUBO:
5737 case ISD::UMULO:
5738 case ISD::UADDO_CARRY:
5739 case ISD::USUBO_CARRY:
5740 // No poison on result or overflow flags.
5741 return false;
5742
5743 case ISD::SELECT_CC:
5744 case ISD::SETCC: {
5745 // Integer setcc cannot create undef or poison.
5746 if (Op.getOperand(0).getValueType().isInteger())
5747 return false;
5748
5749 // FP compares are more complicated. They can create poison for nan/infinity
5750 // based on options and flags. The options and flags also cause special
5751 // nonan condition codes to be used. Those condition codes may be preserved
5752 // even if the nonan flag is dropped somewhere.
5753 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5754 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5755 return (unsigned)CCCode & 0x10U;
5756 }
5757
5758 case ISD::OR:
5759 case ISD::ZERO_EXTEND:
5760 case ISD::SELECT:
5761 case ISD::VSELECT:
5762 case ISD::ADD:
5763 case ISD::SUB:
5764 case ISD::MUL:
5765 case ISD::FNEG:
5766 case ISD::FADD:
5767 case ISD::FSUB:
5768 case ISD::FMUL:
5769 case ISD::FDIV:
5770 case ISD::FREM:
5771 case ISD::FCOPYSIGN:
5772 case ISD::FMA:
5773 case ISD::FMAD:
5774 case ISD::FMULADD:
5775 case ISD::FP_EXTEND:
5781 // No poison except from flags (which is handled above)
5782 return false;
5783
5784 case ISD::SHL:
5785 case ISD::SRL:
5786 case ISD::SRA:
5787 // If the max shift amount isn't in range, then the shift can
5788 // create poison.
5789 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5790
5793 // If the amount is zero then the result will be poison.
5794 // TODO: Add isKnownNeverZero DemandedElts handling.
5795 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5796
5798 // Check if we demand any upper (undef) elements.
5799 return !PoisonOnly && DemandedElts.ugt(1);
5800
5803 // Ensure that the element index is in bounds.
5804 EVT VecVT = Op.getOperand(0).getValueType();
5805 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5806 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5807 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5808 }
5809
5810 case ISD::VECTOR_SHUFFLE: {
5811 // Check for any demanded shuffle element that is undef.
5812 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5813 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5814 if (Elt < 0 && DemandedElts[Idx])
5815 return true;
5816 return false;
5817 }
5818
5820 return false;
5821
5822 default:
5823 // Allow the target to implement this method for its nodes.
5824 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5825 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5826 return TLI->canCreateUndefOrPoisonForTargetNode(
5827 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5828 break;
5829 }
5830
5831 // Be conservative and return true.
5832 return true;
5833}
5834
5835bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5836 unsigned Opcode = Op.getOpcode();
5837 if (Opcode == ISD::OR)
5838 return Op->getFlags().hasDisjoint() ||
5839 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5840 if (Opcode == ISD::XOR)
5841 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5842 return false;
5843}
5844
5846 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5847 (Op.isAnyAdd() || isADDLike(Op));
5848}
5849
5851 unsigned Depth) const {
5852 EVT VT = Op.getValueType();
5853
5854 // Since the number of lanes in a scalable vector is unknown at compile time,
5855 // we track one bit which is implicitly broadcast to all lanes. This means
5856 // that all lanes in a scalable vector are considered demanded.
5857 APInt DemandedElts = VT.isFixedLengthVector()
5859 : APInt(1, 1);
5860
5861 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5862}
5863
5865 bool SNaN, unsigned Depth) const {
5866 assert(!DemandedElts.isZero() && "No demanded elements");
5867
5868 // If we're told that NaNs won't happen, assume they won't.
5869 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5870 return true;
5871
5872 if (Depth >= MaxRecursionDepth)
5873 return false; // Limit search depth.
5874
5875 // If the value is a constant, we can obviously see if it is a NaN or not.
5877 return !C->getValueAPF().isNaN() ||
5878 (SNaN && !C->getValueAPF().isSignaling());
5879 }
5880
5881 unsigned Opcode = Op.getOpcode();
5882 switch (Opcode) {
5883 case ISD::FADD:
5884 case ISD::FSUB:
5885 case ISD::FMUL:
5886 case ISD::FDIV:
5887 case ISD::FREM:
5888 case ISD::FSIN:
5889 case ISD::FCOS:
5890 case ISD::FTAN:
5891 case ISD::FASIN:
5892 case ISD::FACOS:
5893 case ISD::FATAN:
5894 case ISD::FATAN2:
5895 case ISD::FSINH:
5896 case ISD::FCOSH:
5897 case ISD::FTANH:
5898 case ISD::FMA:
5899 case ISD::FMULADD:
5900 case ISD::FMAD: {
5901 if (SNaN)
5902 return true;
5903 // TODO: Need isKnownNeverInfinity
5904 return false;
5905 }
5906 case ISD::FCANONICALIZE:
5907 case ISD::FEXP:
5908 case ISD::FEXP2:
5909 case ISD::FEXP10:
5910 case ISD::FTRUNC:
5911 case ISD::FFLOOR:
5912 case ISD::FCEIL:
5913 case ISD::FROUND:
5914 case ISD::FROUNDEVEN:
5915 case ISD::LROUND:
5916 case ISD::LLROUND:
5917 case ISD::FRINT:
5918 case ISD::LRINT:
5919 case ISD::LLRINT:
5920 case ISD::FNEARBYINT:
5921 case ISD::FLDEXP: {
5922 if (SNaN)
5923 return true;
5924 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5925 }
5926 case ISD::FABS:
5927 case ISD::FNEG:
5928 case ISD::FCOPYSIGN: {
5929 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5930 }
5931 case ISD::SELECT:
5932 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5933 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5934 case ISD::FP_EXTEND:
5935 case ISD::FP_ROUND: {
5936 if (SNaN)
5937 return true;
5938 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5939 }
5940 case ISD::SINT_TO_FP:
5941 case ISD::UINT_TO_FP:
5942 return true;
5943 case ISD::FSQRT: // Need is known positive
5944 case ISD::FLOG:
5945 case ISD::FLOG2:
5946 case ISD::FLOG10:
5947 case ISD::FPOWI:
5948 case ISD::FPOW: {
5949 if (SNaN)
5950 return true;
5951 // TODO: Refine on operand
5952 return false;
5953 }
5954 case ISD::FMINNUM:
5955 case ISD::FMAXNUM:
5956 case ISD::FMINIMUMNUM:
5957 case ISD::FMAXIMUMNUM: {
5958 // Only one needs to be known not-nan, since it will be returned if the
5959 // other ends up being one.
5960 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5961 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5962 }
5963 case ISD::FMINNUM_IEEE:
5964 case ISD::FMAXNUM_IEEE: {
5965 if (SNaN)
5966 return true;
5967 // This can return a NaN if either operand is an sNaN, or if both operands
5968 // are NaN.
5969 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5970 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5971 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5972 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5973 }
5974 case ISD::FMINIMUM:
5975 case ISD::FMAXIMUM: {
5976 // TODO: Does this quiet or return the origina NaN as-is?
5977 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5978 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5979 }
5981 SDValue Src = Op.getOperand(0);
5982 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5983 EVT SrcVT = Src.getValueType();
5984 if (SrcVT.isFixedLengthVector() && Idx &&
5985 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5986 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5987 Idx->getZExtValue());
5988 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5989 }
5990 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5991 }
5993 SDValue Src = Op.getOperand(0);
5994 if (Src.getValueType().isFixedLengthVector()) {
5995 unsigned Idx = Op.getConstantOperandVal(1);
5996 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5997 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5998 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5999 }
6000 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6001 }
6002 case ISD::INSERT_SUBVECTOR: {
6003 SDValue BaseVector = Op.getOperand(0);
6004 SDValue SubVector = Op.getOperand(1);
6005 EVT BaseVectorVT = BaseVector.getValueType();
6006 if (BaseVectorVT.isFixedLengthVector()) {
6007 unsigned Idx = Op.getConstantOperandVal(2);
6008 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
6009 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
6010
6011 // Clear/Extract the bits at the position where the subvector will be
6012 // inserted.
6013 APInt DemandedMask =
6014 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
6015 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
6016 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
6017
6018 bool NeverNaN = true;
6019 if (!DemandedSrcElts.isZero())
6020 NeverNaN &=
6021 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
6022 if (NeverNaN && !DemandedSubElts.isZero())
6023 NeverNaN &=
6024 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
6025 return NeverNaN;
6026 }
6027 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
6028 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
6029 }
6030 case ISD::BUILD_VECTOR: {
6031 unsigned NumElts = Op.getNumOperands();
6032 for (unsigned I = 0; I != NumElts; ++I)
6033 if (DemandedElts[I] &&
6034 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6035 return false;
6036 return true;
6037 }
6038 case ISD::AssertNoFPClass: {
6039 FPClassTest NoFPClass =
6040 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6041 if ((NoFPClass & fcNan) == fcNan)
6042 return true;
6043 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6044 return true;
6045 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6046 }
6047 default:
6048 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6049 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6050 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6051 Depth);
6052 }
6053
6054 return false;
6055 }
6056}
6057
6059 assert(Op.getValueType().isFloatingPoint() &&
6060 "Floating point type expected");
6061
6062 // If the value is a constant, we can obviously see if it is a zero or not.
6064 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
6065}
6066
6068 if (Depth >= MaxRecursionDepth)
6069 return false; // Limit search depth.
6070
6071 assert(!Op.getValueType().isFloatingPoint() &&
6072 "Floating point types unsupported - use isKnownNeverZeroFloat");
6073
6074 // If the value is a constant, we can obviously see if it is a zero or not.
6076 [](ConstantSDNode *C) { return !C->isZero(); }))
6077 return true;
6078
6079 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6080 // some degree.
6081 switch (Op.getOpcode()) {
6082 default:
6083 break;
6084
6085 case ISD::OR:
6086 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6087 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6088
6089 case ISD::VSELECT:
6090 case ISD::SELECT:
6091 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6092 isKnownNeverZero(Op.getOperand(2), Depth + 1);
6093
6094 case ISD::SHL: {
6095 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6096 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6097 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6098 // 1 << X is never zero.
6099 if (ValKnown.One[0])
6100 return true;
6101 // If max shift cnt of known ones is non-zero, result is non-zero.
6102 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6103 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6104 !ValKnown.One.shl(MaxCnt).isZero())
6105 return true;
6106 break;
6107 }
6108 case ISD::UADDSAT:
6109 case ISD::UMAX:
6110 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6111 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6112
6113 // For smin/smax: If either operand is known negative/positive
6114 // respectively we don't need the other to be known at all.
6115 case ISD::SMAX: {
6116 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6117 if (Op1.isStrictlyPositive())
6118 return true;
6119
6120 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6121 if (Op0.isStrictlyPositive())
6122 return true;
6123
6124 if (Op1.isNonZero() && Op0.isNonZero())
6125 return true;
6126
6127 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6128 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6129 }
6130 case ISD::SMIN: {
6131 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6132 if (Op1.isNegative())
6133 return true;
6134
6135 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6136 if (Op0.isNegative())
6137 return true;
6138
6139 if (Op1.isNonZero() && Op0.isNonZero())
6140 return true;
6141
6142 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6143 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6144 }
6145 case ISD::UMIN:
6146 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6147 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6148
6149 case ISD::ROTL:
6150 case ISD::ROTR:
6151 case ISD::BITREVERSE:
6152 case ISD::BSWAP:
6153 case ISD::CTPOP:
6154 case ISD::ABS:
6155 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6156
6157 case ISD::SRA:
6158 case ISD::SRL: {
6159 if (Op->getFlags().hasExact())
6160 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6161 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6162 if (ValKnown.isNegative())
6163 return true;
6164 // If max shift cnt of known ones is non-zero, result is non-zero.
6165 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6166 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6167 !ValKnown.One.lshr(MaxCnt).isZero())
6168 return true;
6169 break;
6170 }
6171 case ISD::UDIV:
6172 case ISD::SDIV:
6173 // div exact can only produce a zero if the dividend is zero.
6174 // TODO: For udiv this is also true if Op1 u<= Op0
6175 if (Op->getFlags().hasExact())
6176 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6177 break;
6178
6179 case ISD::ADD:
6180 if (Op->getFlags().hasNoUnsignedWrap())
6181 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6182 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6183 return true;
6184 // TODO: There are a lot more cases we can prove for add.
6185 break;
6186
6187 case ISD::SUB: {
6188 if (isNullConstant(Op.getOperand(0)))
6189 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6190
6191 std::optional<bool> ne =
6192 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6193 computeKnownBits(Op.getOperand(1), Depth + 1));
6194 return ne && *ne;
6195 }
6196
6197 case ISD::MUL:
6198 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6199 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6200 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6201 return true;
6202 break;
6203
6204 case ISD::ZERO_EXTEND:
6205 case ISD::SIGN_EXTEND:
6206 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6207 case ISD::VSCALE: {
6209 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6210 ConstantRange CR =
6211 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6212 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6213 return true;
6214 break;
6215 }
6216 }
6217
6219}
6220
6222 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6223 return !C1->isNegative();
6224
6225 switch (Op.getOpcode()) {
6226 case ISD::FABS:
6227 case ISD::FEXP:
6228 case ISD::FEXP2:
6229 case ISD::FEXP10:
6230 return true;
6231 default:
6232 return false;
6233 }
6234
6235 llvm_unreachable("covered opcode switch");
6236}
6237
6239 assert(Use.getValueType().isFloatingPoint());
6240 const SDNode *User = Use.getUser();
6241 unsigned OperandNo = Use.getOperandNo();
6242 // Check if this use is insensitive to the sign of zero
6243 switch (User->getOpcode()) {
6244 case ISD::SETCC:
6245 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6246 case ISD::FABS:
6247 // fabs always produces +0.0.
6248 return true;
6249 case ISD::FCOPYSIGN:
6250 // copysign overwrites the sign bit of the first operand.
6251 return OperandNo == 0;
6252 case ISD::FADD:
6253 case ISD::FSUB: {
6254 // Arithmetic with non-zero constants fixes the uncertainty around the
6255 // sign bit.
6256 SDValue Other = User->getOperand(1 - OperandNo);
6258 }
6259 case ISD::FP_TO_SINT:
6260 case ISD::FP_TO_UINT:
6261 // fp-to-int conversions normalize signed zeros.
6262 return true;
6263 default:
6264 return false;
6265 }
6266}
6267
6269 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6270 // regression. Ideally, this should be implemented as a demanded-bits
6271 // optimization that stems from the users.
6272 if (Op->use_size() > 2)
6273 return false;
6274 return all_of(Op->uses(),
6275 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6276}
6277
6279 // Check the obvious case.
6280 if (A == B) return true;
6281
6282 // For negative and positive zero.
6285 if (CA->isZero() && CB->isZero()) return true;
6286
6287 // Otherwise they may not be equal.
6288 return false;
6289}
6290
6291// Only bits set in Mask must be negated, other bits may be arbitrary.
6293 if (isBitwiseNot(V, AllowUndefs))
6294 return V.getOperand(0);
6295
6296 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6297 // bits in the non-extended part.
6298 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6299 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6300 return SDValue();
6301 SDValue ExtArg = V.getOperand(0);
6302 if (ExtArg.getScalarValueSizeInBits() >=
6303 MaskC->getAPIntValue().getActiveBits() &&
6304 isBitwiseNot(ExtArg, AllowUndefs) &&
6305 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6306 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6307 return ExtArg.getOperand(0).getOperand(0);
6308 return SDValue();
6309}
6310
6312 // Match masked merge pattern (X & ~M) op (Y & M)
6313 // Including degenerate case (X & ~M) op M
6314 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6315 SDValue Other) {
6316 if (SDValue NotOperand =
6317 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6318 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6319 NotOperand->getOpcode() == ISD::TRUNCATE)
6320 NotOperand = NotOperand->getOperand(0);
6321
6322 if (Other == NotOperand)
6323 return true;
6324 if (Other->getOpcode() == ISD::AND)
6325 return NotOperand == Other->getOperand(0) ||
6326 NotOperand == Other->getOperand(1);
6327 }
6328 return false;
6329 };
6330
6331 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6332 A = A->getOperand(0);
6333
6334 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6335 B = B->getOperand(0);
6336
6337 if (A->getOpcode() == ISD::AND)
6338 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6339 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6340 return false;
6341}
6342
6343// FIXME: unify with llvm::haveNoCommonBitsSet.
6345 assert(A.getValueType() == B.getValueType() &&
6346 "Values must have the same type");
6349 return true;
6352}
6353
6354static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6355 SelectionDAG &DAG) {
6356 if (cast<ConstantSDNode>(Step)->isZero())
6357 return DAG.getConstant(0, DL, VT);
6358
6359 return SDValue();
6360}
6361
6364 SelectionDAG &DAG) {
6365 int NumOps = Ops.size();
6366 assert(NumOps != 0 && "Can't build an empty vector!");
6367 assert(!VT.isScalableVector() &&
6368 "BUILD_VECTOR cannot be used with scalable types");
6369 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6370 "Incorrect element count in BUILD_VECTOR!");
6371
6372 // BUILD_VECTOR of UNDEFs is UNDEF.
6373 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6374 return DAG.getUNDEF(VT);
6375
6376 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6377 SDValue IdentitySrc;
6378 bool IsIdentity = true;
6379 for (int i = 0; i != NumOps; ++i) {
6381 Ops[i].getOperand(0).getValueType() != VT ||
6382 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6383 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6384 Ops[i].getConstantOperandAPInt(1) != i) {
6385 IsIdentity = false;
6386 break;
6387 }
6388 IdentitySrc = Ops[i].getOperand(0);
6389 }
6390 if (IsIdentity)
6391 return IdentitySrc;
6392
6393 return SDValue();
6394}
6395
6396/// Try to simplify vector concatenation to an input value, undef, or build
6397/// vector.
6400 SelectionDAG &DAG) {
6401 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6403 [Ops](SDValue Op) {
6404 return Ops[0].getValueType() == Op.getValueType();
6405 }) &&
6406 "Concatenation of vectors with inconsistent value types!");
6407 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6408 VT.getVectorElementCount() &&
6409 "Incorrect element count in vector concatenation!");
6410
6411 if (Ops.size() == 1)
6412 return Ops[0];
6413
6414 // Concat of UNDEFs is UNDEF.
6415 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6416 return DAG.getUNDEF(VT);
6417
6418 // Scan the operands and look for extract operations from a single source
6419 // that correspond to insertion at the same location via this concatenation:
6420 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6421 SDValue IdentitySrc;
6422 bool IsIdentity = true;
6423 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6424 SDValue Op = Ops[i];
6425 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6426 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6427 Op.getOperand(0).getValueType() != VT ||
6428 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6429 Op.getConstantOperandVal(1) != IdentityIndex) {
6430 IsIdentity = false;
6431 break;
6432 }
6433 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6434 "Unexpected identity source vector for concat of extracts");
6435 IdentitySrc = Op.getOperand(0);
6436 }
6437 if (IsIdentity) {
6438 assert(IdentitySrc && "Failed to set source vector of extracts");
6439 return IdentitySrc;
6440 }
6441
6442 // The code below this point is only designed to work for fixed width
6443 // vectors, so we bail out for now.
6444 if (VT.isScalableVector())
6445 return SDValue();
6446
6447 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6448 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6449 // BUILD_VECTOR.
6450 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6451 EVT SVT = VT.getScalarType();
6453 for (SDValue Op : Ops) {
6454 EVT OpVT = Op.getValueType();
6455 if (Op.isUndef())
6456 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6457 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6458 Elts.append(Op->op_begin(), Op->op_end());
6459 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6460 OpVT.getVectorNumElements() == 1 &&
6461 isNullConstant(Op.getOperand(2)))
6462 Elts.push_back(Op.getOperand(1));
6463 else
6464 return SDValue();
6465 }
6466
6467 // BUILD_VECTOR requires all inputs to be of the same type, find the
6468 // maximum type and extend them all.
6469 for (SDValue Op : Elts)
6470 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6471
6472 if (SVT.bitsGT(VT.getScalarType())) {
6473 for (SDValue &Op : Elts) {
6474 if (Op.isUndef())
6475 Op = DAG.getUNDEF(SVT);
6476 else
6477 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6478 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6479 : DAG.getSExtOrTrunc(Op, DL, SVT);
6480 }
6481 }
6482
6483 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6484 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6485 return V;
6486}
6487
6488/// Gets or creates the specified node.
6489SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6490 SDVTList VTs = getVTList(VT);
6492 AddNodeIDNode(ID, Opcode, VTs, {});
6493 void *IP = nullptr;
6494 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6495 return SDValue(E, 0);
6496
6497 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6498 CSEMap.InsertNode(N, IP);
6499
6500 InsertNode(N);
6501 SDValue V = SDValue(N, 0);
6502 NewSDValueDbgMsg(V, "Creating new node: ", this);
6503 return V;
6504}
6505
6506SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6507 SDValue N1) {
6508 SDNodeFlags Flags;
6509 if (Inserter)
6510 Flags = Inserter->getFlags();
6511 return getNode(Opcode, DL, VT, N1, Flags);
6512}
6513
6514SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6515 SDValue N1, const SDNodeFlags Flags) {
6516 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6517
6518 // Constant fold unary operations with a vector integer or float operand.
6519 switch (Opcode) {
6520 default:
6521 // FIXME: Entirely reasonable to perform folding of other unary
6522 // operations here as the need arises.
6523 break;
6524 case ISD::FNEG:
6525 case ISD::FABS:
6526 case ISD::FCEIL:
6527 case ISD::FTRUNC:
6528 case ISD::FFLOOR:
6529 case ISD::FP_EXTEND:
6530 case ISD::FP_TO_SINT:
6531 case ISD::FP_TO_UINT:
6532 case ISD::FP_TO_FP16:
6533 case ISD::FP_TO_BF16:
6534 case ISD::TRUNCATE:
6535 case ISD::ANY_EXTEND:
6536 case ISD::ZERO_EXTEND:
6537 case ISD::SIGN_EXTEND:
6538 case ISD::UINT_TO_FP:
6539 case ISD::SINT_TO_FP:
6540 case ISD::FP16_TO_FP:
6541 case ISD::BF16_TO_FP:
6542 case ISD::BITCAST:
6543 case ISD::ABS:
6544 case ISD::BITREVERSE:
6545 case ISD::BSWAP:
6546 case ISD::CTLZ:
6548 case ISD::CTTZ:
6550 case ISD::CTPOP:
6551 case ISD::STEP_VECTOR: {
6552 SDValue Ops = {N1};
6553 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6554 return Fold;
6555 }
6556 }
6557
6558 unsigned OpOpcode = N1.getNode()->getOpcode();
6559 switch (Opcode) {
6560 case ISD::STEP_VECTOR:
6561 assert(VT.isScalableVector() &&
6562 "STEP_VECTOR can only be used with scalable types");
6563 assert(OpOpcode == ISD::TargetConstant &&
6564 VT.getVectorElementType() == N1.getValueType() &&
6565 "Unexpected step operand");
6566 break;
6567 case ISD::FREEZE:
6568 assert(VT == N1.getValueType() && "Unexpected VT!");
6569 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6570 return N1;
6571 break;
6572 case ISD::TokenFactor:
6573 case ISD::MERGE_VALUES:
6575 return N1; // Factor, merge or concat of one node? No need.
6576 case ISD::BUILD_VECTOR: {
6577 // Attempt to simplify BUILD_VECTOR.
6578 SDValue Ops[] = {N1};
6579 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6580 return V;
6581 break;
6582 }
6583 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6584 case ISD::FP_EXTEND:
6586 "Invalid FP cast!");
6587 if (N1.getValueType() == VT) return N1; // noop conversion.
6588 assert((!VT.isVector() || VT.getVectorElementCount() ==
6590 "Vector element count mismatch!");
6591 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6592 if (N1.isUndef())
6593 return getUNDEF(VT);
6594 break;
6595 case ISD::FP_TO_SINT:
6596 case ISD::FP_TO_UINT:
6597 if (N1.isUndef())
6598 return getUNDEF(VT);
6599 break;
6600 case ISD::SINT_TO_FP:
6601 case ISD::UINT_TO_FP:
6602 // [us]itofp(undef) = 0, because the result value is bounded.
6603 if (N1.isUndef())
6604 return getConstantFP(0.0, DL, VT);
6605 break;
6606 case ISD::SIGN_EXTEND:
6607 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6608 "Invalid SIGN_EXTEND!");
6609 assert(VT.isVector() == N1.getValueType().isVector() &&
6610 "SIGN_EXTEND result type type should be vector iff the operand "
6611 "type is vector!");
6612 if (N1.getValueType() == VT) return N1; // noop extension
6613 assert((!VT.isVector() || VT.getVectorElementCount() ==
6615 "Vector element count mismatch!");
6616 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6617 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6618 SDNodeFlags Flags;
6619 if (OpOpcode == ISD::ZERO_EXTEND)
6620 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6621 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6622 transferDbgValues(N1, NewVal);
6623 return NewVal;
6624 }
6625
6626 if (OpOpcode == ISD::POISON)
6627 return getPOISON(VT);
6628
6629 if (N1.isUndef())
6630 // sext(undef) = 0, because the top bits will all be the same.
6631 return getConstant(0, DL, VT);
6632
6633 // Skip unnecessary sext_inreg pattern:
6634 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6635 if (OpOpcode == ISD::TRUNCATE) {
6636 SDValue OpOp = N1.getOperand(0);
6637 if (OpOp.getValueType() == VT) {
6638 unsigned NumSignExtBits =
6640 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6641 transferDbgValues(N1, OpOp);
6642 return OpOp;
6643 }
6644 }
6645 }
6646 break;
6647 case ISD::ZERO_EXTEND:
6648 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6649 "Invalid ZERO_EXTEND!");
6650 assert(VT.isVector() == N1.getValueType().isVector() &&
6651 "ZERO_EXTEND result type type should be vector iff the operand "
6652 "type is vector!");
6653 if (N1.getValueType() == VT) return N1; // noop extension
6654 assert((!VT.isVector() || VT.getVectorElementCount() ==
6656 "Vector element count mismatch!");
6657 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6658 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6659 SDNodeFlags Flags;
6660 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6661 SDValue NewVal =
6662 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6663 transferDbgValues(N1, NewVal);
6664 return NewVal;
6665 }
6666
6667 if (OpOpcode == ISD::POISON)
6668 return getPOISON(VT);
6669
6670 if (N1.isUndef())
6671 // zext(undef) = 0, because the top bits will be zero.
6672 return getConstant(0, DL, VT);
6673
6674 // Skip unnecessary zext_inreg pattern:
6675 // (zext (trunc x)) -> x iff the upper bits are known zero.
6676 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6677 // use to recognise zext_inreg patterns.
6678 if (OpOpcode == ISD::TRUNCATE) {
6679 SDValue OpOp = N1.getOperand(0);
6680 if (OpOp.getValueType() == VT) {
6681 if (OpOp.getOpcode() != ISD::AND) {
6684 if (MaskedValueIsZero(OpOp, HiBits)) {
6685 transferDbgValues(N1, OpOp);
6686 return OpOp;
6687 }
6688 }
6689 }
6690 }
6691 break;
6692 case ISD::ANY_EXTEND:
6693 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6694 "Invalid ANY_EXTEND!");
6695 assert(VT.isVector() == N1.getValueType().isVector() &&
6696 "ANY_EXTEND result type type should be vector iff the operand "
6697 "type is vector!");
6698 if (N1.getValueType() == VT) return N1; // noop extension
6699 assert((!VT.isVector() || VT.getVectorElementCount() ==
6701 "Vector element count mismatch!");
6702 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6703
6704 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6705 OpOpcode == ISD::ANY_EXTEND) {
6706 SDNodeFlags Flags;
6707 if (OpOpcode == ISD::ZERO_EXTEND)
6708 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6709 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6710 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6711 }
6712 if (N1.isUndef())
6713 return getUNDEF(VT);
6714
6715 // (ext (trunc x)) -> x
6716 if (OpOpcode == ISD::TRUNCATE) {
6717 SDValue OpOp = N1.getOperand(0);
6718 if (OpOp.getValueType() == VT) {
6719 transferDbgValues(N1, OpOp);
6720 return OpOp;
6721 }
6722 }
6723 break;
6724 case ISD::TRUNCATE:
6725 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6726 "Invalid TRUNCATE!");
6727 assert(VT.isVector() == N1.getValueType().isVector() &&
6728 "TRUNCATE result type type should be vector iff the operand "
6729 "type is vector!");
6730 if (N1.getValueType() == VT) return N1; // noop truncate
6731 assert((!VT.isVector() || VT.getVectorElementCount() ==
6733 "Vector element count mismatch!");
6734 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6735 if (OpOpcode == ISD::TRUNCATE)
6736 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6737 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6738 OpOpcode == ISD::ANY_EXTEND) {
6739 // If the source is smaller than the dest, we still need an extend.
6741 VT.getScalarType())) {
6742 SDNodeFlags Flags;
6743 if (OpOpcode == ISD::ZERO_EXTEND)
6744 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6745 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6746 }
6747 if (N1.getOperand(0).getValueType().bitsGT(VT))
6748 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6749 return N1.getOperand(0);
6750 }
6751 if (N1.isUndef())
6752 return getUNDEF(VT);
6753 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6754 return getVScale(DL, VT,
6756 break;
6760 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6761 assert(N1.getValueType().bitsLE(VT) &&
6762 "The input must be the same size or smaller than the result.");
6765 "The destination vector type must have fewer lanes than the input.");
6766 break;
6767 case ISD::ABS:
6768 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6769 if (N1.isUndef())
6770 return getConstant(0, DL, VT);
6771 break;
6772 case ISD::BSWAP:
6773 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6774 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6775 "BSWAP types must be a multiple of 16 bits!");
6776 if (N1.isUndef())
6777 return getUNDEF(VT);
6778 // bswap(bswap(X)) -> X.
6779 if (OpOpcode == ISD::BSWAP)
6780 return N1.getOperand(0);
6781 break;
6782 case ISD::BITREVERSE:
6783 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6784 if (N1.isUndef())
6785 return getUNDEF(VT);
6786 break;
6787 case ISD::BITCAST:
6789 "Cannot BITCAST between types of different sizes!");
6790 if (VT == N1.getValueType()) return N1; // noop conversion.
6791 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6792 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6793 if (N1.isUndef())
6794 return getUNDEF(VT);
6795 break;
6797 assert(VT.isVector() && !N1.getValueType().isVector() &&
6798 (VT.getVectorElementType() == N1.getValueType() ||
6800 N1.getValueType().isInteger() &&
6802 "Illegal SCALAR_TO_VECTOR node!");
6803 if (N1.isUndef())
6804 return getUNDEF(VT);
6805 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6806 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6808 N1.getConstantOperandVal(1) == 0 &&
6809 N1.getOperand(0).getValueType() == VT)
6810 return N1.getOperand(0);
6811 break;
6812 case ISD::FNEG:
6813 // Negation of an unknown bag of bits is still completely undefined.
6814 if (N1.isUndef())
6815 return getUNDEF(VT);
6816
6817 if (OpOpcode == ISD::FNEG) // --X -> X
6818 return N1.getOperand(0);
6819 break;
6820 case ISD::FABS:
6821 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6822 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6823 break;
6824 case ISD::VSCALE:
6825 assert(VT == N1.getValueType() && "Unexpected VT!");
6826 break;
6827 case ISD::CTPOP:
6828 if (N1.getValueType().getScalarType() == MVT::i1)
6829 return N1;
6830 break;
6831 case ISD::CTLZ:
6832 case ISD::CTTZ:
6833 if (N1.getValueType().getScalarType() == MVT::i1)
6834 return getNOT(DL, N1, N1.getValueType());
6835 break;
6836 case ISD::VECREDUCE_ADD:
6837 if (N1.getValueType().getScalarType() == MVT::i1)
6838 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6839 break;
6842 if (N1.getValueType().getScalarType() == MVT::i1)
6843 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6844 break;
6847 if (N1.getValueType().getScalarType() == MVT::i1)
6848 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6849 break;
6850 case ISD::SPLAT_VECTOR:
6851 assert(VT.isVector() && "Wrong return type!");
6852 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6853 // that for now.
6855 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6857 N1.getValueType().isInteger() &&
6859 "Wrong operand type!");
6860 break;
6861 }
6862
6863 SDNode *N;
6864 SDVTList VTs = getVTList(VT);
6865 SDValue Ops[] = {N1};
6866 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6868 AddNodeIDNode(ID, Opcode, VTs, Ops);
6869 void *IP = nullptr;
6870 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6871 E->intersectFlagsWith(Flags);
6872 return SDValue(E, 0);
6873 }
6874
6875 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6876 N->setFlags(Flags);
6877 createOperands(N, Ops);
6878 CSEMap.InsertNode(N, IP);
6879 } else {
6880 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6881 createOperands(N, Ops);
6882 }
6883
6884 InsertNode(N);
6885 SDValue V = SDValue(N, 0);
6886 NewSDValueDbgMsg(V, "Creating new node: ", this);
6887 return V;
6888}
6889
6890static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6891 const APInt &C2) {
6892 switch (Opcode) {
6893 case ISD::ADD: return C1 + C2;
6894 case ISD::SUB: return C1 - C2;
6895 case ISD::MUL: return C1 * C2;
6896 case ISD::AND: return C1 & C2;
6897 case ISD::OR: return C1 | C2;
6898 case ISD::XOR: return C1 ^ C2;
6899 case ISD::SHL: return C1 << C2;
6900 case ISD::SRL: return C1.lshr(C2);
6901 case ISD::SRA: return C1.ashr(C2);
6902 case ISD::ROTL: return C1.rotl(C2);
6903 case ISD::ROTR: return C1.rotr(C2);
6904 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6905 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6906 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6907 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6908 case ISD::SADDSAT: return C1.sadd_sat(C2);
6909 case ISD::UADDSAT: return C1.uadd_sat(C2);
6910 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6911 case ISD::USUBSAT: return C1.usub_sat(C2);
6912 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6913 case ISD::USHLSAT: return C1.ushl_sat(C2);
6914 case ISD::UDIV:
6915 if (!C2.getBoolValue())
6916 break;
6917 return C1.udiv(C2);
6918 case ISD::UREM:
6919 if (!C2.getBoolValue())
6920 break;
6921 return C1.urem(C2);
6922 case ISD::SDIV:
6923 if (!C2.getBoolValue())
6924 break;
6925 return C1.sdiv(C2);
6926 case ISD::SREM:
6927 if (!C2.getBoolValue())
6928 break;
6929 return C1.srem(C2);
6930 case ISD::AVGFLOORS:
6931 return APIntOps::avgFloorS(C1, C2);
6932 case ISD::AVGFLOORU:
6933 return APIntOps::avgFloorU(C1, C2);
6934 case ISD::AVGCEILS:
6935 return APIntOps::avgCeilS(C1, C2);
6936 case ISD::AVGCEILU:
6937 return APIntOps::avgCeilU(C1, C2);
6938 case ISD::ABDS:
6939 return APIntOps::abds(C1, C2);
6940 case ISD::ABDU:
6941 return APIntOps::abdu(C1, C2);
6942 case ISD::MULHS:
6943 return APIntOps::mulhs(C1, C2);
6944 case ISD::MULHU:
6945 return APIntOps::mulhu(C1, C2);
6946 case ISD::CLMUL:
6947 return APIntOps::clmul(C1, C2);
6948 case ISD::CLMULR:
6949 return APIntOps::clmulr(C1, C2);
6950 case ISD::CLMULH:
6951 return APIntOps::clmulh(C1, C2);
6952 }
6953 return std::nullopt;
6954}
6955// Handle constant folding with UNDEF.
6956// TODO: Handle more cases.
6957static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6958 bool IsUndef1, const APInt &C2,
6959 bool IsUndef2) {
6960 if (!(IsUndef1 || IsUndef2))
6961 return FoldValue(Opcode, C1, C2);
6962
6963 // Fold and(x, undef) -> 0
6964 // Fold mul(x, undef) -> 0
6965 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6966 return APInt::getZero(C1.getBitWidth());
6967
6968 return std::nullopt;
6969}
6970
6972 const GlobalAddressSDNode *GA,
6973 const SDNode *N2) {
6974 if (GA->getOpcode() != ISD::GlobalAddress)
6975 return SDValue();
6976 if (!TLI->isOffsetFoldingLegal(GA))
6977 return SDValue();
6978 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6979 if (!C2)
6980 return SDValue();
6981 int64_t Offset = C2->getSExtValue();
6982 switch (Opcode) {
6983 case ISD::ADD:
6984 case ISD::PTRADD:
6985 break;
6986 case ISD::SUB: Offset = -uint64_t(Offset); break;
6987 default: return SDValue();
6988 }
6989 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6990 GA->getOffset() + uint64_t(Offset));
6991}
6992
6994 switch (Opcode) {
6995 case ISD::SDIV:
6996 case ISD::UDIV:
6997 case ISD::SREM:
6998 case ISD::UREM: {
6999 // If a divisor is zero/undef or any element of a divisor vector is
7000 // zero/undef, the whole op is undef.
7001 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
7002 SDValue Divisor = Ops[1];
7003 if (Divisor.isUndef() || isNullConstant(Divisor))
7004 return true;
7005
7006 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
7007 llvm::any_of(Divisor->op_values(),
7008 [](SDValue V) { return V.isUndef() ||
7009 isNullConstant(V); });
7010 // TODO: Handle signed overflow.
7011 }
7012 // TODO: Handle oversized shifts.
7013 default:
7014 return false;
7015 }
7016}
7017
7020 SDNodeFlags Flags) {
7021 // If the opcode is a target-specific ISD node, there's nothing we can
7022 // do here and the operand rules may not line up with the below, so
7023 // bail early.
7024 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
7025 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
7026 // foldCONCAT_VECTORS in getNode before this is called.
7027 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
7028 return SDValue();
7029
7030 unsigned NumOps = Ops.size();
7031 if (NumOps == 0)
7032 return SDValue();
7033
7034 if (isUndef(Opcode, Ops))
7035 return getUNDEF(VT);
7036
7037 // Handle unary special cases.
7038 if (NumOps == 1) {
7039 SDValue N1 = Ops[0];
7040
7041 // Constant fold unary operations with an integer constant operand. Even
7042 // opaque constant will be folded, because the folding of unary operations
7043 // doesn't create new constants with different values. Nevertheless, the
7044 // opaque flag is preserved during folding to prevent future folding with
7045 // other constants.
7046 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
7047 const APInt &Val = C->getAPIntValue();
7048 switch (Opcode) {
7049 case ISD::SIGN_EXTEND:
7050 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7051 C->isTargetOpcode(), C->isOpaque());
7052 case ISD::TRUNCATE:
7053 if (C->isOpaque())
7054 break;
7055 [[fallthrough]];
7056 case ISD::ZERO_EXTEND:
7057 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7058 C->isTargetOpcode(), C->isOpaque());
7059 case ISD::ANY_EXTEND:
7060 // Some targets like RISCV prefer to sign extend some types.
7061 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7062 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7063 C->isTargetOpcode(), C->isOpaque());
7064 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7065 C->isTargetOpcode(), C->isOpaque());
7066 case ISD::ABS:
7067 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7068 C->isOpaque());
7069 case ISD::BITREVERSE:
7070 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7071 C->isOpaque());
7072 case ISD::BSWAP:
7073 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7074 C->isOpaque());
7075 case ISD::CTPOP:
7076 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7077 C->isOpaque());
7078 case ISD::CTLZ:
7080 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7081 C->isOpaque());
7082 case ISD::CTTZ:
7084 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7085 C->isOpaque());
7086 case ISD::UINT_TO_FP:
7087 case ISD::SINT_TO_FP: {
7089 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7091 return getConstantFP(FPV, DL, VT);
7092 }
7093 case ISD::FP16_TO_FP:
7094 case ISD::BF16_TO_FP: {
7095 bool Ignored;
7096 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7097 : APFloat::BFloat(),
7098 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7099
7100 // This can return overflow, underflow, or inexact; we don't care.
7101 // FIXME need to be more flexible about rounding mode.
7103 &Ignored);
7104 return getConstantFP(FPV, DL, VT);
7105 }
7106 case ISD::STEP_VECTOR:
7107 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7108 return V;
7109 break;
7110 case ISD::BITCAST:
7111 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7112 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7113 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7114 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7115 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7116 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7117 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7118 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7119 break;
7120 }
7121 }
7122
7123 // Constant fold unary operations with a floating point constant operand.
7124 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7125 APFloat V = C->getValueAPF(); // make copy
7126 switch (Opcode) {
7127 case ISD::FNEG:
7128 V.changeSign();
7129 return getConstantFP(V, DL, VT);
7130 case ISD::FABS:
7131 V.clearSign();
7132 return getConstantFP(V, DL, VT);
7133 case ISD::FCEIL: {
7134 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7136 return getConstantFP(V, DL, VT);
7137 return SDValue();
7138 }
7139 case ISD::FTRUNC: {
7140 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7142 return getConstantFP(V, DL, VT);
7143 return SDValue();
7144 }
7145 case ISD::FFLOOR: {
7146 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7148 return getConstantFP(V, DL, VT);
7149 return SDValue();
7150 }
7151 case ISD::FP_EXTEND: {
7152 bool ignored;
7153 // This can return overflow, underflow, or inexact; we don't care.
7154 // FIXME need to be more flexible about rounding mode.
7155 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7156 &ignored);
7157 return getConstantFP(V, DL, VT);
7158 }
7159 case ISD::FP_TO_SINT:
7160 case ISD::FP_TO_UINT: {
7161 bool ignored;
7162 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7163 // FIXME need to be more flexible about rounding mode.
7165 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7166 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7167 break;
7168 return getConstant(IntVal, DL, VT);
7169 }
7170 case ISD::FP_TO_FP16:
7171 case ISD::FP_TO_BF16: {
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(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7176 : APFloat::BFloat(),
7178 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7179 }
7180 case ISD::BITCAST:
7181 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7182 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7183 VT);
7184 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7185 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7186 VT);
7187 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7188 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7189 VT);
7190 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7191 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7192 break;
7193 }
7194 }
7195
7196 // Early-out if we failed to constant fold a bitcast.
7197 if (Opcode == ISD::BITCAST)
7198 return SDValue();
7199 }
7200
7201 // Handle binops special cases.
7202 if (NumOps == 2) {
7203 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7204 return CFP;
7205
7206 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7207 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7208 if (C1->isOpaque() || C2->isOpaque())
7209 return SDValue();
7210
7211 std::optional<APInt> FoldAttempt =
7212 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7213 if (!FoldAttempt)
7214 return SDValue();
7215
7216 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7217 assert((!Folded || !VT.isVector()) &&
7218 "Can't fold vectors ops with scalar operands");
7219 return Folded;
7220 }
7221 }
7222
7223 // fold (add Sym, c) -> Sym+c
7225 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7226 if (TLI->isCommutativeBinOp(Opcode))
7228 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7229
7230 // fold (sext_in_reg c1) -> c2
7231 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7232 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7233
7234 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7235 unsigned FromBits = EVT.getScalarSizeInBits();
7236 Val <<= Val.getBitWidth() - FromBits;
7237 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7238 return getConstant(Val, DL, ConstantVT);
7239 };
7240
7241 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7242 const APInt &Val = C1->getAPIntValue();
7243 return SignExtendInReg(Val, VT);
7244 }
7245
7247 SmallVector<SDValue, 8> ScalarOps;
7248 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7249 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7250 SDValue Op = Ops[0].getOperand(I);
7251 if (Op.isUndef()) {
7252 ScalarOps.push_back(getUNDEF(OpVT));
7253 continue;
7254 }
7255 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7256 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7257 }
7258 return getBuildVector(VT, DL, ScalarOps);
7259 }
7260
7261 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7262 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7263 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7264 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7265 Ops[0].getOperand(0).getValueType()));
7266 }
7267 }
7268
7269 // Handle fshl/fshr special cases.
7270 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7271 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7272 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7273 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7274
7275 if (C1 && C2 && C3) {
7276 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7277 return SDValue();
7278 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7279 &V3 = C3->getAPIntValue();
7280
7281 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7282 : APIntOps::fshr(V1, V2, V3);
7283 return getConstant(FoldedVal, DL, VT);
7284 }
7285 }
7286
7287 // Handle fma/fmad special cases.
7288 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7289 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7290 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7291 Ops[2].getValueType() == VT && "FMA types must match!");
7295 if (C1 && C2 && C3) {
7296 APFloat V1 = C1->getValueAPF();
7297 const APFloat &V2 = C2->getValueAPF();
7298 const APFloat &V3 = C3->getValueAPF();
7299 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7302 } else
7304 return getConstantFP(V1, DL, VT);
7305 }
7306 }
7307
7308 // This is for vector folding only from here on.
7309 if (!VT.isVector())
7310 return SDValue();
7311
7312 ElementCount NumElts = VT.getVectorElementCount();
7313
7314 // See if we can fold through any bitcasted integer ops.
7315 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7316 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7317 (Ops[0].getOpcode() == ISD::BITCAST ||
7318 Ops[1].getOpcode() == ISD::BITCAST)) {
7321 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7322 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7323 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7324 N2.getValueType().isInteger()) {
7325 bool IsLE = getDataLayout().isLittleEndian();
7326 unsigned EltBits = VT.getScalarSizeInBits();
7327 SmallVector<APInt> RawBits1, RawBits2;
7328 BitVector UndefElts1, UndefElts2;
7329 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7330 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7331 SmallVector<APInt> RawBits;
7332 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7333 std::optional<APInt> Fold = FoldValueWithUndef(
7334 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7335 if (!Fold)
7336 break;
7337 RawBits.push_back(*Fold);
7338 }
7339 if (RawBits.size() == NumElts.getFixedValue()) {
7340 // We have constant folded, but we might need to cast this again back
7341 // to the original (possibly legalized) type.
7342 EVT BVVT, BVEltVT;
7343 if (N1.getValueType() == VT) {
7344 BVVT = N1.getValueType();
7345 BVEltVT = BV1->getOperand(0).getValueType();
7346 } else {
7347 BVVT = N2.getValueType();
7348 BVEltVT = BV2->getOperand(0).getValueType();
7349 }
7350 unsigned BVEltBits = BVEltVT.getSizeInBits();
7351 SmallVector<APInt> DstBits;
7352 BitVector DstUndefs;
7354 DstBits, RawBits, DstUndefs,
7355 BitVector(RawBits.size(), false));
7356 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7357 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7358 if (DstUndefs[I])
7359 continue;
7360 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7361 }
7362 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7363 }
7364 }
7365 }
7366 }
7367
7368 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7369 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7370 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7371 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7372 APInt RHSVal;
7373 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7374 APInt NewStep = Opcode == ISD::MUL
7375 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7376 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7377 return getStepVector(DL, VT, NewStep);
7378 }
7379 }
7380
7381 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7382 return !Op.getValueType().isVector() ||
7383 Op.getValueType().getVectorElementCount() == NumElts;
7384 };
7385
7386 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7387 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7388 Op.getOpcode() == ISD::BUILD_VECTOR ||
7389 Op.getOpcode() == ISD::SPLAT_VECTOR;
7390 };
7391
7392 // All operands must be vector types with the same number of elements as
7393 // the result type and must be either UNDEF or a build/splat vector
7394 // or UNDEF scalars.
7395 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7396 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7397 return SDValue();
7398
7399 // If we are comparing vectors, then the result needs to be a i1 boolean that
7400 // is then extended back to the legal result type depending on how booleans
7401 // are represented.
7402 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7403 ISD::NodeType ExtendCode =
7404 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7405 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7407
7408 // Find legal integer scalar type for constant promotion and
7409 // ensure that its scalar size is at least as large as source.
7410 EVT LegalSVT = VT.getScalarType();
7411 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7412 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7413 if (LegalSVT.bitsLT(VT.getScalarType()))
7414 return SDValue();
7415 }
7416
7417 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7418 // only have one operand to check. For fixed-length vector types we may have
7419 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7420 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7421
7422 // Constant fold each scalar lane separately.
7423 SmallVector<SDValue, 4> ScalarResults;
7424 for (unsigned I = 0; I != NumVectorElts; I++) {
7425 SmallVector<SDValue, 4> ScalarOps;
7426 for (SDValue Op : Ops) {
7427 EVT InSVT = Op.getValueType().getScalarType();
7428 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7429 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7430 if (Op.isUndef())
7431 ScalarOps.push_back(getUNDEF(InSVT));
7432 else
7433 ScalarOps.push_back(Op);
7434 continue;
7435 }
7436
7437 SDValue ScalarOp =
7438 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7439 EVT ScalarVT = ScalarOp.getValueType();
7440
7441 // Build vector (integer) scalar operands may need implicit
7442 // truncation - do this before constant folding.
7443 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7444 // Don't create illegally-typed nodes unless they're constants or undef
7445 // - if we fail to constant fold we can't guarantee the (dead) nodes
7446 // we're creating will be cleaned up before being visited for
7447 // legalization.
7448 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7449 !isa<ConstantSDNode>(ScalarOp) &&
7450 TLI->getTypeAction(*getContext(), InSVT) !=
7452 return SDValue();
7453 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7454 }
7455
7456 ScalarOps.push_back(ScalarOp);
7457 }
7458
7459 // Constant fold the scalar operands.
7460 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7461
7462 // Scalar folding only succeeded if the result is a constant or UNDEF.
7463 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7464 ScalarResult.getOpcode() != ISD::ConstantFP)
7465 return SDValue();
7466
7467 // Legalize the (integer) scalar constant if necessary. We only do
7468 // this once we know the folding succeeded, since otherwise we would
7469 // get a node with illegal type which has a user.
7470 if (LegalSVT != SVT)
7471 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7472
7473 ScalarResults.push_back(ScalarResult);
7474 }
7475
7476 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7477 : getBuildVector(VT, DL, ScalarResults);
7478 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7479 return V;
7480}
7481
7484 // TODO: Add support for unary/ternary fp opcodes.
7485 if (Ops.size() != 2)
7486 return SDValue();
7487
7488 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7489 // should. That will require dealing with a potentially non-default
7490 // rounding mode, checking the "opStatus" return value from the APFloat
7491 // math calculations, and possibly other variations.
7492 SDValue N1 = Ops[0];
7493 SDValue N2 = Ops[1];
7494 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7495 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7496 if (N1CFP && N2CFP) {
7497 APFloat C1 = N1CFP->getValueAPF(); // make copy
7498 const APFloat &C2 = N2CFP->getValueAPF();
7499 switch (Opcode) {
7500 case ISD::FADD:
7502 return getConstantFP(C1, DL, VT);
7503 case ISD::FSUB:
7505 return getConstantFP(C1, DL, VT);
7506 case ISD::FMUL:
7508 return getConstantFP(C1, DL, VT);
7509 case ISD::FDIV:
7511 return getConstantFP(C1, DL, VT);
7512 case ISD::FREM:
7513 C1.mod(C2);
7514 return getConstantFP(C1, DL, VT);
7515 case ISD::FCOPYSIGN:
7516 C1.copySign(C2);
7517 return getConstantFP(C1, DL, VT);
7518 case ISD::FMINNUM:
7519 if (C1.isSignaling() || C2.isSignaling())
7520 return SDValue();
7521 return getConstantFP(minnum(C1, C2), DL, VT);
7522 case ISD::FMAXNUM:
7523 if (C1.isSignaling() || C2.isSignaling())
7524 return SDValue();
7525 return getConstantFP(maxnum(C1, C2), DL, VT);
7526 case ISD::FMINIMUM:
7527 return getConstantFP(minimum(C1, C2), DL, VT);
7528 case ISD::FMAXIMUM:
7529 return getConstantFP(maximum(C1, C2), DL, VT);
7530 case ISD::FMINIMUMNUM:
7531 return getConstantFP(minimumnum(C1, C2), DL, VT);
7532 case ISD::FMAXIMUMNUM:
7533 return getConstantFP(maximumnum(C1, C2), DL, VT);
7534 default: break;
7535 }
7536 }
7537 if (N1CFP && Opcode == ISD::FP_ROUND) {
7538 APFloat C1 = N1CFP->getValueAPF(); // make copy
7539 bool Unused;
7540 // This can return overflow, underflow, or inexact; we don't care.
7541 // FIXME need to be more flexible about rounding mode.
7543 &Unused);
7544 return getConstantFP(C1, DL, VT);
7545 }
7546
7547 switch (Opcode) {
7548 case ISD::FSUB:
7549 // -0.0 - undef --> undef (consistent with "fneg undef")
7550 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7551 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7552 return getUNDEF(VT);
7553 [[fallthrough]];
7554
7555 case ISD::FADD:
7556 case ISD::FMUL:
7557 case ISD::FDIV:
7558 case ISD::FREM:
7559 // If both operands are undef, the result is undef. If 1 operand is undef,
7560 // the result is NaN. This should match the behavior of the IR optimizer.
7561 if (N1.isUndef() && N2.isUndef())
7562 return getUNDEF(VT);
7563 if (N1.isUndef() || N2.isUndef())
7565 }
7566 return SDValue();
7567}
7568
7570 const SDLoc &DL, EVT DstEltVT) {
7571 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7572
7573 // If this is already the right type, we're done.
7574 if (SrcEltVT == DstEltVT)
7575 return SDValue(BV, 0);
7576
7577 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7578 unsigned DstBitSize = DstEltVT.getSizeInBits();
7579
7580 // If this is a conversion of N elements of one type to N elements of another
7581 // type, convert each element. This handles FP<->INT cases.
7582 if (SrcBitSize == DstBitSize) {
7584 for (SDValue Op : BV->op_values()) {
7585 // If the vector element type is not legal, the BUILD_VECTOR operands
7586 // are promoted and implicitly truncated. Make that explicit here.
7587 if (Op.getValueType() != SrcEltVT)
7588 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7589 Ops.push_back(getBitcast(DstEltVT, Op));
7590 }
7591 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7593 return getBuildVector(VT, DL, Ops);
7594 }
7595
7596 // Otherwise, we're growing or shrinking the elements. To avoid having to
7597 // handle annoying details of growing/shrinking FP values, we convert them to
7598 // int first.
7599 if (SrcEltVT.isFloatingPoint()) {
7600 // Convert the input float vector to a int vector where the elements are the
7601 // same sizes.
7602 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7603 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7605 DstEltVT);
7606 return SDValue();
7607 }
7608
7609 // Now we know the input is an integer vector. If the output is a FP type,
7610 // convert to integer first, then to FP of the right size.
7611 if (DstEltVT.isFloatingPoint()) {
7612 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7613 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7615 DstEltVT);
7616 return SDValue();
7617 }
7618
7619 // Okay, we know the src/dst types are both integers of differing types.
7620 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7621
7622 // Extract the constant raw bit data.
7623 BitVector UndefElements;
7624 SmallVector<APInt> RawBits;
7625 bool IsLE = getDataLayout().isLittleEndian();
7626 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7627 return SDValue();
7628
7630 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7631 if (UndefElements[I])
7632 Ops.push_back(getUNDEF(DstEltVT));
7633 else
7634 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7635 }
7636
7637 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7638 return getBuildVector(VT, DL, Ops);
7639}
7640
7642 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7643
7644 // There's no need to assert on a byte-aligned pointer. All pointers are at
7645 // least byte aligned.
7646 if (A == Align(1))
7647 return Val;
7648
7649 SDVTList VTs = getVTList(Val.getValueType());
7651 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7652 ID.AddInteger(A.value());
7653
7654 void *IP = nullptr;
7655 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7656 return SDValue(E, 0);
7657
7658 auto *N =
7659 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7660 createOperands(N, {Val});
7661
7662 CSEMap.InsertNode(N, IP);
7663 InsertNode(N);
7664
7665 SDValue V(N, 0);
7666 NewSDValueDbgMsg(V, "Creating new node: ", this);
7667 return V;
7668}
7669
7670SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7671 SDValue N1, SDValue N2) {
7672 SDNodeFlags Flags;
7673 if (Inserter)
7674 Flags = Inserter->getFlags();
7675 return getNode(Opcode, DL, VT, N1, N2, Flags);
7676}
7677
7679 SDValue &N2) const {
7680 if (!TLI->isCommutativeBinOp(Opcode))
7681 return;
7682
7683 // Canonicalize:
7684 // binop(const, nonconst) -> binop(nonconst, const)
7687 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7688 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7689 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7690 std::swap(N1, N2);
7691
7692 // Canonicalize:
7693 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7694 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7696 std::swap(N1, N2);
7697}
7698
7699SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7700 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7702 N2.getOpcode() != ISD::DELETED_NODE &&
7703 "Operand is DELETED_NODE!");
7704
7705 canonicalizeCommutativeBinop(Opcode, N1, N2);
7706
7707 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7708 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7709
7710 // Don't allow undefs in vector splats - we might be returning N2 when folding
7711 // to zero etc.
7712 ConstantSDNode *N2CV =
7713 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7714
7715 switch (Opcode) {
7716 default: break;
7717 case ISD::TokenFactor:
7718 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7719 N2.getValueType() == MVT::Other && "Invalid token factor!");
7720 // Fold trivial token factors.
7721 if (N1.getOpcode() == ISD::EntryToken) return N2;
7722 if (N2.getOpcode() == ISD::EntryToken) return N1;
7723 if (N1 == N2) return N1;
7724 break;
7725 case ISD::BUILD_VECTOR: {
7726 // Attempt to simplify BUILD_VECTOR.
7727 SDValue Ops[] = {N1, N2};
7728 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7729 return V;
7730 break;
7731 }
7732 case ISD::CONCAT_VECTORS: {
7733 SDValue Ops[] = {N1, N2};
7734 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7735 return V;
7736 break;
7737 }
7738 case ISD::AND:
7739 assert(VT.isInteger() && "This operator does not apply to FP types!");
7740 assert(N1.getValueType() == N2.getValueType() &&
7741 N1.getValueType() == VT && "Binary operator types must match!");
7742 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7743 // worth handling here.
7744 if (N2CV && N2CV->isZero())
7745 return N2;
7746 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7747 return N1;
7748 break;
7749 case ISD::OR:
7750 case ISD::XOR:
7751 case ISD::ADD:
7752 case ISD::PTRADD:
7753 case ISD::SUB:
7754 assert(VT.isInteger() && "This operator does not apply to FP types!");
7755 assert(N1.getValueType() == N2.getValueType() &&
7756 N1.getValueType() == VT && "Binary operator types must match!");
7757 // The equal operand types requirement is unnecessarily strong for PTRADD.
7758 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7759 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7760 // logic everywhere where PTRADDs may be folded or combined to properly
7761 // support them. If/when we introduce pointer types to the SDAG, we will
7762 // need to relax this constraint.
7763
7764 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7765 // it's worth handling here.
7766 if (N2CV && N2CV->isZero())
7767 return N1;
7768 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7769 VT.getScalarType() == MVT::i1)
7770 return getNode(ISD::XOR, DL, VT, N1, N2);
7771 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7772 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7773 N2.getOpcode() == ISD::VSCALE) {
7774 const APInt &C1 = N1->getConstantOperandAPInt(0);
7775 const APInt &C2 = N2->getConstantOperandAPInt(0);
7776 return getVScale(DL, VT, C1 + C2);
7777 }
7778 break;
7779 case ISD::MUL:
7780 assert(VT.isInteger() && "This operator does not apply to FP types!");
7781 assert(N1.getValueType() == N2.getValueType() &&
7782 N1.getValueType() == VT && "Binary operator types must match!");
7783 if (VT.getScalarType() == MVT::i1)
7784 return getNode(ISD::AND, DL, VT, N1, N2);
7785 if (N2CV && N2CV->isZero())
7786 return N2;
7787 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7788 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7789 const APInt &N2CImm = N2C->getAPIntValue();
7790 return getVScale(DL, VT, MulImm * N2CImm);
7791 }
7792 break;
7793 case ISD::UDIV:
7794 case ISD::UREM:
7795 case ISD::MULHU:
7796 case ISD::MULHS:
7797 case ISD::SDIV:
7798 case ISD::SREM:
7799 case ISD::SADDSAT:
7800 case ISD::SSUBSAT:
7801 case ISD::UADDSAT:
7802 case ISD::USUBSAT:
7803 assert(VT.isInteger() && "This operator does not apply to FP types!");
7804 assert(N1.getValueType() == N2.getValueType() &&
7805 N1.getValueType() == VT && "Binary operator types must match!");
7806 if (VT.getScalarType() == MVT::i1) {
7807 // fold (add_sat x, y) -> (or x, y) for bool types.
7808 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7809 return getNode(ISD::OR, DL, VT, N1, N2);
7810 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7811 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7812 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7813 }
7814 break;
7815 case ISD::SCMP:
7816 case ISD::UCMP:
7817 assert(N1.getValueType() == N2.getValueType() &&
7818 "Types of operands of UCMP/SCMP must match");
7819 assert(N1.getValueType().isVector() == VT.isVector() &&
7820 "Operands and return type of must both be scalars or vectors");
7821 if (VT.isVector())
7824 "Result and operands must have the same number of elements");
7825 break;
7826 case ISD::AVGFLOORS:
7827 case ISD::AVGFLOORU:
7828 case ISD::AVGCEILS:
7829 case ISD::AVGCEILU:
7830 assert(VT.isInteger() && "This operator does not apply to FP types!");
7831 assert(N1.getValueType() == N2.getValueType() &&
7832 N1.getValueType() == VT && "Binary operator types must match!");
7833 break;
7834 case ISD::ABDS:
7835 case ISD::ABDU:
7836 assert(VT.isInteger() && "This operator does not apply to FP types!");
7837 assert(N1.getValueType() == N2.getValueType() &&
7838 N1.getValueType() == VT && "Binary operator types must match!");
7839 if (VT.getScalarType() == MVT::i1)
7840 return getNode(ISD::XOR, DL, VT, N1, N2);
7841 break;
7842 case ISD::SMIN:
7843 case ISD::UMAX:
7844 assert(VT.isInteger() && "This operator does not apply to FP types!");
7845 assert(N1.getValueType() == N2.getValueType() &&
7846 N1.getValueType() == VT && "Binary operator types must match!");
7847 if (VT.getScalarType() == MVT::i1)
7848 return getNode(ISD::OR, DL, VT, N1, N2);
7849 break;
7850 case ISD::SMAX:
7851 case ISD::UMIN:
7852 assert(VT.isInteger() && "This operator does not apply to FP types!");
7853 assert(N1.getValueType() == N2.getValueType() &&
7854 N1.getValueType() == VT && "Binary operator types must match!");
7855 if (VT.getScalarType() == MVT::i1)
7856 return getNode(ISD::AND, DL, VT, N1, N2);
7857 break;
7858 case ISD::FADD:
7859 case ISD::FSUB:
7860 case ISD::FMUL:
7861 case ISD::FDIV:
7862 case ISD::FREM:
7863 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7864 assert(N1.getValueType() == N2.getValueType() &&
7865 N1.getValueType() == VT && "Binary operator types must match!");
7866 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7867 return V;
7868 break;
7869 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7870 assert(N1.getValueType() == VT &&
7873 "Invalid FCOPYSIGN!");
7874 break;
7875 case ISD::SHL:
7876 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7877 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7878 const APInt &ShiftImm = N2C->getAPIntValue();
7879 return getVScale(DL, VT, MulImm << ShiftImm);
7880 }
7881 [[fallthrough]];
7882 case ISD::SRA:
7883 case ISD::SRL:
7884 if (SDValue V = simplifyShift(N1, N2))
7885 return V;
7886 [[fallthrough]];
7887 case ISD::ROTL:
7888 case ISD::ROTR:
7889 case ISD::SSHLSAT:
7890 case ISD::USHLSAT:
7891 assert(VT == N1.getValueType() &&
7892 "Shift operators return type must be the same as their first arg");
7893 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7894 "Shifts only work on integers");
7895 assert((!VT.isVector() || VT == N2.getValueType()) &&
7896 "Vector shift amounts must be in the same as their first arg");
7897 // Verify that the shift amount VT is big enough to hold valid shift
7898 // amounts. This catches things like trying to shift an i1024 value by an
7899 // i8, which is easy to fall into in generic code that uses
7900 // TLI.getShiftAmount().
7903 "Invalid use of small shift amount with oversized value!");
7904
7905 // Always fold shifts of i1 values so the code generator doesn't need to
7906 // handle them. Since we know the size of the shift has to be less than the
7907 // size of the value, the shift/rotate count is guaranteed to be zero.
7908 if (VT == MVT::i1)
7909 return N1;
7910 if (N2CV && N2CV->isZero())
7911 return N1;
7912 break;
7913 case ISD::FP_ROUND:
7915 VT.bitsLE(N1.getValueType()) && N2C &&
7916 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7917 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7918 if (N1.getValueType() == VT) return N1; // noop conversion.
7919 break;
7920 case ISD::AssertNoFPClass: {
7922 "AssertNoFPClass is used for a non-floating type");
7923 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7924 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7925 assert(llvm::to_underlying(NoFPClass) <=
7927 "FPClassTest value too large");
7928 (void)NoFPClass;
7929 break;
7930 }
7931 case ISD::AssertSext:
7932 case ISD::AssertZext: {
7933 EVT EVT = cast<VTSDNode>(N2)->getVT();
7934 assert(VT == N1.getValueType() && "Not an inreg extend!");
7935 assert(VT.isInteger() && EVT.isInteger() &&
7936 "Cannot *_EXTEND_INREG FP types");
7937 assert(!EVT.isVector() &&
7938 "AssertSExt/AssertZExt type should be the vector element type "
7939 "rather than the vector type!");
7940 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7941 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7942 break;
7943 }
7945 EVT EVT = cast<VTSDNode>(N2)->getVT();
7946 assert(VT == N1.getValueType() && "Not an inreg extend!");
7947 assert(VT.isInteger() && EVT.isInteger() &&
7948 "Cannot *_EXTEND_INREG FP types");
7949 assert(EVT.isVector() == VT.isVector() &&
7950 "SIGN_EXTEND_INREG type should be vector iff the operand "
7951 "type is vector!");
7952 assert((!EVT.isVector() ||
7954 "Vector element counts must match in SIGN_EXTEND_INREG");
7955 assert(EVT.getScalarType().bitsLE(VT.getScalarType()) && "Not extending!");
7956 if (EVT == VT) return N1; // Not actually extending
7957 break;
7958 }
7960 case ISD::FP_TO_UINT_SAT: {
7961 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7962 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7963 assert(N1.getValueType().isVector() == VT.isVector() &&
7964 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7965 "vector!");
7966 assert((!VT.isVector() || VT.getVectorElementCount() ==
7968 "Vector element counts must match in FP_TO_*INT_SAT");
7969 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7970 "Type to saturate to must be a scalar.");
7971 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7972 "Not extending!");
7973 break;
7974 }
7977 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7978 element type of the vector.");
7979
7980 // Extract from an undefined value or using an undefined index is undefined.
7981 if (N1.isUndef() || N2.isUndef())
7982 return getUNDEF(VT);
7983
7984 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7985 // vectors. For scalable vectors we will provide appropriate support for
7986 // dealing with arbitrary indices.
7987 if (N2C && N1.getValueType().isFixedLengthVector() &&
7988 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7989 return getUNDEF(VT);
7990
7991 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7992 // expanding copies of large vectors from registers. This only works for
7993 // fixed length vectors, since we need to know the exact number of
7994 // elements.
7995 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7997 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7998 return getExtractVectorElt(DL, VT,
7999 N1.getOperand(N2C->getZExtValue() / Factor),
8000 N2C->getZExtValue() % Factor);
8001 }
8002
8003 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
8004 // lowering is expanding large vector constants.
8005 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
8006 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
8009 "BUILD_VECTOR used for scalable vectors");
8010 unsigned Index =
8011 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
8012 SDValue Elt = N1.getOperand(Index);
8013
8014 if (VT != Elt.getValueType())
8015 // If the vector element type is not legal, the BUILD_VECTOR operands
8016 // are promoted and implicitly truncated, and the result implicitly
8017 // extended. Make that explicit here.
8018 Elt = getAnyExtOrTrunc(Elt, DL, VT);
8019
8020 return Elt;
8021 }
8022
8023 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
8024 // operations are lowered to scalars.
8025 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
8026 // If the indices are the same, return the inserted element else
8027 // if the indices are known different, extract the element from
8028 // the original vector.
8029 SDValue N1Op2 = N1.getOperand(2);
8031
8032 if (N1Op2C && N2C) {
8033 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
8034 if (VT == N1.getOperand(1).getValueType())
8035 return N1.getOperand(1);
8036 if (VT.isFloatingPoint()) {
8038 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
8039 }
8040 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
8041 }
8042 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
8043 }
8044 }
8045
8046 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
8047 // when vector types are scalarized and v1iX is legal.
8048 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
8049 // Here we are completely ignoring the extract element index (N2),
8050 // which is fine for fixed width vectors, since any index other than 0
8051 // is undefined anyway. However, this cannot be ignored for scalable
8052 // vectors - in theory we could support this, but we don't want to do this
8053 // without a profitability check.
8054 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8056 N1.getValueType().getVectorNumElements() == 1) {
8057 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8058 N1.getOperand(1));
8059 }
8060 break;
8062 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8063 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8064 (N1.getValueType().isInteger() == VT.isInteger()) &&
8065 N1.getValueType() != VT &&
8066 "Wrong types for EXTRACT_ELEMENT!");
8067
8068 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8069 // 64-bit integers into 32-bit parts. Instead of building the extract of
8070 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8071 if (N1.getOpcode() == ISD::BUILD_PAIR)
8072 return N1.getOperand(N2C->getZExtValue());
8073
8074 // EXTRACT_ELEMENT of a constant int is also very common.
8075 if (N1C) {
8076 unsigned ElementSize = VT.getSizeInBits();
8077 unsigned Shift = ElementSize * N2C->getZExtValue();
8078 const APInt &Val = N1C->getAPIntValue();
8079 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8080 }
8081 break;
8083 EVT N1VT = N1.getValueType();
8084 assert(VT.isVector() && N1VT.isVector() &&
8085 "Extract subvector VTs must be vectors!");
8087 "Extract subvector VTs must have the same element type!");
8088 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8089 "Cannot extract a scalable vector from a fixed length vector!");
8090 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8092 "Extract subvector must be from larger vector to smaller vector!");
8093 assert(N2C && "Extract subvector index must be a constant");
8094 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8095 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8096 N1VT.getVectorMinNumElements()) &&
8097 "Extract subvector overflow!");
8098 assert(N2C->getAPIntValue().getBitWidth() ==
8099 TLI->getVectorIdxWidth(getDataLayout()) &&
8100 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8101 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8102 "Extract index is not a multiple of the output vector length");
8103
8104 // Trivial extraction.
8105 if (VT == N1VT)
8106 return N1;
8107
8108 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8109 if (N1.isUndef())
8110 return getUNDEF(VT);
8111
8112 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8113 // the concat have the same type as the extract.
8114 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8115 VT == N1.getOperand(0).getValueType()) {
8116 unsigned Factor = VT.getVectorMinNumElements();
8117 return N1.getOperand(N2C->getZExtValue() / Factor);
8118 }
8119
8120 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8121 // during shuffle legalization.
8122 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8123 VT == N1.getOperand(1).getValueType())
8124 return N1.getOperand(1);
8125 break;
8126 }
8127 }
8128
8129 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8130 switch (Opcode) {
8131 case ISD::XOR:
8132 case ISD::ADD:
8133 case ISD::PTRADD:
8134 case ISD::SUB:
8136 case ISD::UDIV:
8137 case ISD::SDIV:
8138 case ISD::UREM:
8139 case ISD::SREM:
8140 case ISD::MUL:
8141 case ISD::AND:
8142 case ISD::SSUBSAT:
8143 case ISD::USUBSAT:
8144 case ISD::UMIN:
8145 case ISD::OR:
8146 case ISD::SADDSAT:
8147 case ISD::UADDSAT:
8148 case ISD::UMAX:
8149 case ISD::SMAX:
8150 case ISD::SMIN:
8151 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8152 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8153 }
8154 }
8155
8156 // Canonicalize an UNDEF to the RHS, even over a constant.
8157 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8158 if (TLI->isCommutativeBinOp(Opcode)) {
8159 std::swap(N1, N2);
8160 } else {
8161 switch (Opcode) {
8162 case ISD::PTRADD:
8163 case ISD::SUB:
8164 // fold op(undef, non_undef_arg2) -> undef.
8165 return N1;
8167 case ISD::UDIV:
8168 case ISD::SDIV:
8169 case ISD::UREM:
8170 case ISD::SREM:
8171 case ISD::SSUBSAT:
8172 case ISD::USUBSAT:
8173 // fold op(undef, non_undef_arg2) -> 0.
8174 return getConstant(0, DL, VT);
8175 }
8176 }
8177 }
8178
8179 // Fold a bunch of operators when the RHS is undef.
8180 if (N2.getOpcode() == ISD::UNDEF) {
8181 switch (Opcode) {
8182 case ISD::XOR:
8183 if (N1.getOpcode() == ISD::UNDEF)
8184 // Handle undef ^ undef -> 0 special case. This is a common
8185 // idiom (misuse).
8186 return getConstant(0, DL, VT);
8187 [[fallthrough]];
8188 case ISD::ADD:
8189 case ISD::PTRADD:
8190 case ISD::SUB:
8191 // fold op(arg1, undef) -> undef.
8192 return N2;
8193 case ISD::UDIV:
8194 case ISD::SDIV:
8195 case ISD::UREM:
8196 case ISD::SREM:
8197 // fold op(arg1, undef) -> poison.
8198 return getPOISON(VT);
8199 case ISD::MUL:
8200 case ISD::AND:
8201 case ISD::SSUBSAT:
8202 case ISD::USUBSAT:
8203 case ISD::UMIN:
8204 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8205 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8206 case ISD::OR:
8207 case ISD::SADDSAT:
8208 case ISD::UADDSAT:
8209 case ISD::UMAX:
8210 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8211 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8212 case ISD::SMAX:
8213 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8214 return N1.getOpcode() == ISD::UNDEF
8215 ? N2
8216 : getConstant(
8218 VT);
8219 case ISD::SMIN:
8220 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8221 return N1.getOpcode() == ISD::UNDEF
8222 ? N2
8223 : getConstant(
8225 VT);
8226 }
8227 }
8228
8229 // Perform trivial constant folding.
8230 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8231 return SV;
8232
8233 // Memoize this node if possible.
8234 SDNode *N;
8235 SDVTList VTs = getVTList(VT);
8236 SDValue Ops[] = {N1, N2};
8237 if (VT != MVT::Glue) {
8239 AddNodeIDNode(ID, Opcode, VTs, Ops);
8240 void *IP = nullptr;
8241 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8242 E->intersectFlagsWith(Flags);
8243 return SDValue(E, 0);
8244 }
8245
8246 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8247 N->setFlags(Flags);
8248 createOperands(N, Ops);
8249 CSEMap.InsertNode(N, IP);
8250 } else {
8251 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8252 createOperands(N, Ops);
8253 }
8254
8255 InsertNode(N);
8256 SDValue V = SDValue(N, 0);
8257 NewSDValueDbgMsg(V, "Creating new node: ", this);
8258 return V;
8259}
8260
8261SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8262 SDValue N1, SDValue N2, SDValue N3) {
8263 SDNodeFlags Flags;
8264 if (Inserter)
8265 Flags = Inserter->getFlags();
8266 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8267}
8268
8269SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8270 SDValue N1, SDValue N2, SDValue N3,
8271 const SDNodeFlags Flags) {
8273 N2.getOpcode() != ISD::DELETED_NODE &&
8274 N3.getOpcode() != ISD::DELETED_NODE &&
8275 "Operand is DELETED_NODE!");
8276 // Perform various simplifications.
8277 switch (Opcode) {
8278 case ISD::BUILD_VECTOR: {
8279 // Attempt to simplify BUILD_VECTOR.
8280 SDValue Ops[] = {N1, N2, N3};
8281 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8282 return V;
8283 break;
8284 }
8285 case ISD::CONCAT_VECTORS: {
8286 SDValue Ops[] = {N1, N2, N3};
8287 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8288 return V;
8289 break;
8290 }
8291 case ISD::SETCC: {
8292 assert(VT.isInteger() && "SETCC result type must be an integer!");
8293 assert(N1.getValueType() == N2.getValueType() &&
8294 "SETCC operands must have the same type!");
8295 assert(VT.isVector() == N1.getValueType().isVector() &&
8296 "SETCC type should be vector iff the operand type is vector!");
8297 assert((!VT.isVector() || VT.getVectorElementCount() ==
8299 "SETCC vector element counts must match!");
8300 // Use FoldSetCC to simplify SETCC's.
8301 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8302 return V;
8303 break;
8304 }
8305 case ISD::SELECT:
8306 case ISD::VSELECT:
8307 if (SDValue V = simplifySelect(N1, N2, N3))
8308 return V;
8309 break;
8311 llvm_unreachable("should use getVectorShuffle constructor!");
8313 if (isNullConstant(N3))
8314 return N1;
8315 break;
8317 if (isNullConstant(N3))
8318 return N2;
8319 break;
8321 assert(VT.isVector() && VT == N1.getValueType() &&
8322 "INSERT_VECTOR_ELT vector type mismatch");
8324 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8325 assert((!VT.isFloatingPoint() ||
8326 VT.getVectorElementType() == N2.getValueType()) &&
8327 "INSERT_VECTOR_ELT fp scalar type mismatch");
8328 assert((!VT.isInteger() ||
8330 "INSERT_VECTOR_ELT int scalar size mismatch");
8331
8332 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8333 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8334 // for scalable vectors where we will generate appropriate code to
8335 // deal with out-of-bounds cases correctly.
8336 if (N3C && VT.isFixedLengthVector() &&
8337 N3C->getZExtValue() >= VT.getVectorNumElements())
8338 return getUNDEF(VT);
8339
8340 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8341 if (N3.isUndef())
8342 return getUNDEF(VT);
8343
8344 // If inserting poison, just use the input vector.
8345 if (N2.getOpcode() == ISD::POISON)
8346 return N1;
8347
8348 // Inserting undef into undef/poison is still undef.
8349 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8350 return getUNDEF(VT);
8351
8352 // If the inserted element is an UNDEF, just use the input vector.
8353 // But not if skipping the insert could make the result more poisonous.
8354 if (N2.isUndef()) {
8355 if (N3C && VT.isFixedLengthVector()) {
8356 APInt EltMask =
8357 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8358 if (isGuaranteedNotToBePoison(N1, EltMask))
8359 return N1;
8360 } else if (isGuaranteedNotToBePoison(N1))
8361 return N1;
8362 }
8363 break;
8364 }
8365 case ISD::INSERT_SUBVECTOR: {
8366 // If inserting poison, just use the input vector,
8367 if (N2.getOpcode() == ISD::POISON)
8368 return N1;
8369
8370 // Inserting undef into undef/poison is still undef.
8371 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8372 return getUNDEF(VT);
8373
8374 EVT N2VT = N2.getValueType();
8375 assert(VT == N1.getValueType() &&
8376 "Dest and insert subvector source types must match!");
8377 assert(VT.isVector() && N2VT.isVector() &&
8378 "Insert subvector VTs must be vectors!");
8380 "Insert subvector VTs must have the same element type!");
8381 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8382 "Cannot insert a scalable vector into a fixed length vector!");
8383 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8385 "Insert subvector must be from smaller vector to larger vector!");
8387 "Insert subvector index must be constant");
8388 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8389 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8391 "Insert subvector overflow!");
8393 TLI->getVectorIdxWidth(getDataLayout()) &&
8394 "Constant index for INSERT_SUBVECTOR has an invalid size");
8395
8396 // Trivial insertion.
8397 if (VT == N2VT)
8398 return N2;
8399
8400 // If this is an insert of an extracted vector into an undef/poison vector,
8401 // we can just use the input to the extract. But not if skipping the
8402 // extract+insert could make the result more poisonous.
8403 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8404 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8405 if (N1.getOpcode() == ISD::POISON)
8406 return N2.getOperand(0);
8407 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8408 unsigned LoBit = N3->getAsZExtVal();
8409 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8410 APInt EltMask =
8411 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8412 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8413 return N2.getOperand(0);
8414 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8415 return N2.getOperand(0);
8416 }
8417
8418 // If the inserted subvector is UNDEF, just use the input vector.
8419 // But not if skipping the insert could make the result more poisonous.
8420 if (N2.isUndef()) {
8421 if (VT.isFixedLengthVector()) {
8422 unsigned LoBit = N3->getAsZExtVal();
8423 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8424 APInt EltMask =
8425 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8426 if (isGuaranteedNotToBePoison(N1, EltMask))
8427 return N1;
8428 } else if (isGuaranteedNotToBePoison(N1))
8429 return N1;
8430 }
8431 break;
8432 }
8433 case ISD::BITCAST:
8434 // Fold bit_convert nodes from a type to themselves.
8435 if (N1.getValueType() == VT)
8436 return N1;
8437 break;
8438 case ISD::VP_TRUNCATE:
8439 case ISD::VP_SIGN_EXTEND:
8440 case ISD::VP_ZERO_EXTEND:
8441 // Don't create noop casts.
8442 if (N1.getValueType() == VT)
8443 return N1;
8444 break;
8445 case ISD::VECTOR_COMPRESS: {
8446 [[maybe_unused]] EVT VecVT = N1.getValueType();
8447 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8448 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8449 assert(VT == VecVT && "Vector and result type don't match.");
8450 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8451 "All inputs must be vectors.");
8452 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8454 "Vector and mask must have same number of elements.");
8455
8456 if (N1.isUndef() || N2.isUndef())
8457 return N3;
8458
8459 break;
8460 }
8465 [[maybe_unused]] EVT AccVT = N1.getValueType();
8466 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8467 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8468 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8469 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8470 "node to have the same type!");
8471 assert(VT.isVector() && VT == AccVT &&
8472 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8473 "the same type as its result!");
8475 AccVT.getVectorElementCount()) &&
8476 "Expected the element count of the second and third operands of the "
8477 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8478 "element count of the first operand and the result!");
8480 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8481 "node to have an element type which is the same as or smaller than "
8482 "the element type of the first operand and result!");
8483 break;
8484 }
8485 }
8486
8487 // Perform trivial constant folding for arithmetic operators.
8488 switch (Opcode) {
8489 case ISD::FMA:
8490 case ISD::FMAD:
8491 case ISD::SETCC:
8492 case ISD::FSHL:
8493 case ISD::FSHR:
8494 if (SDValue SV =
8495 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8496 return SV;
8497 break;
8498 }
8499
8500 // Memoize node if it doesn't produce a glue result.
8501 SDNode *N;
8502 SDVTList VTs = getVTList(VT);
8503 SDValue Ops[] = {N1, N2, N3};
8504 if (VT != MVT::Glue) {
8506 AddNodeIDNode(ID, Opcode, VTs, Ops);
8507 void *IP = nullptr;
8508 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8509 E->intersectFlagsWith(Flags);
8510 return SDValue(E, 0);
8511 }
8512
8513 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8514 N->setFlags(Flags);
8515 createOperands(N, Ops);
8516 CSEMap.InsertNode(N, IP);
8517 } else {
8518 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8519 createOperands(N, Ops);
8520 }
8521
8522 InsertNode(N);
8523 SDValue V = SDValue(N, 0);
8524 NewSDValueDbgMsg(V, "Creating new node: ", this);
8525 return V;
8526}
8527
8528SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8529 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8530 const SDNodeFlags Flags) {
8531 SDValue Ops[] = { N1, N2, N3, N4 };
8532 return getNode(Opcode, DL, VT, Ops, Flags);
8533}
8534
8535SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8536 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8537 SDNodeFlags Flags;
8538 if (Inserter)
8539 Flags = Inserter->getFlags();
8540 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8541}
8542
8543SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8544 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8545 SDValue N5, const SDNodeFlags Flags) {
8546 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8547 return getNode(Opcode, DL, VT, Ops, Flags);
8548}
8549
8550SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8551 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8552 SDValue N5) {
8553 SDNodeFlags Flags;
8554 if (Inserter)
8555 Flags = Inserter->getFlags();
8556 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8557}
8558
8559/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8560/// the incoming stack arguments to be loaded from the stack.
8562 SmallVector<SDValue, 8> ArgChains;
8563
8564 // Include the original chain at the beginning of the list. When this is
8565 // used by target LowerCall hooks, this helps legalize find the
8566 // CALLSEQ_BEGIN node.
8567 ArgChains.push_back(Chain);
8568
8569 // Add a chain value for each stack argument.
8570 for (SDNode *U : getEntryNode().getNode()->users())
8571 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8572 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8573 if (FI->getIndex() < 0)
8574 ArgChains.push_back(SDValue(L, 1));
8575
8576 // Build a tokenfactor for all the chains.
8577 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8578}
8579
8580/// getMemsetValue - Vectorized representation of the memset value
8581/// operand.
8583 const SDLoc &dl) {
8584 assert(!Value.isUndef());
8585
8586 unsigned NumBits = VT.getScalarSizeInBits();
8588 assert(C->getAPIntValue().getBitWidth() == 8);
8589 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8590 if (VT.isInteger()) {
8591 bool IsOpaque = VT.getSizeInBits() > 64 ||
8592 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8593 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8594 }
8595 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8596 }
8597
8598 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8599 EVT IntVT = VT.getScalarType();
8600 if (!IntVT.isInteger())
8601 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8602
8603 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8604 if (NumBits > 8) {
8605 // Use a multiplication with 0x010101... to extend the input to the
8606 // required length.
8607 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8608 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8609 DAG.getConstant(Magic, dl, IntVT));
8610 }
8611
8612 if (VT != Value.getValueType() && !VT.isInteger())
8613 Value = DAG.getBitcast(VT.getScalarType(), Value);
8614 if (VT != Value.getValueType())
8615 Value = DAG.getSplatBuildVector(VT, dl, Value);
8616
8617 return Value;
8618}
8619
8620/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8621/// used when a memcpy is turned into a memset when the source is a constant
8622/// string ptr.
8624 const TargetLowering &TLI,
8625 const ConstantDataArraySlice &Slice) {
8626 // Handle vector with all elements zero.
8627 if (Slice.Array == nullptr) {
8628 if (VT.isInteger())
8629 return DAG.getConstant(0, dl, VT);
8630 return DAG.getNode(ISD::BITCAST, dl, VT,
8631 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8632 }
8633
8634 assert(!VT.isVector() && "Can't handle vector type here!");
8635 unsigned NumVTBits = VT.getSizeInBits();
8636 unsigned NumVTBytes = NumVTBits / 8;
8637 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8638
8639 APInt Val(NumVTBits, 0);
8640 if (DAG.getDataLayout().isLittleEndian()) {
8641 for (unsigned i = 0; i != NumBytes; ++i)
8642 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8643 } else {
8644 for (unsigned i = 0; i != NumBytes; ++i)
8645 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8646 }
8647
8648 // If the "cost" of materializing the integer immediate is less than the cost
8649 // of a load, then it is cost effective to turn the load into the immediate.
8650 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8651 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8652 return DAG.getConstant(Val, dl, VT);
8653 return SDValue();
8654}
8655
8657 const SDLoc &DL,
8658 const SDNodeFlags Flags) {
8659 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
8660 return getMemBasePlusOffset(Base, Index, DL, Flags);
8661}
8662
8664 const SDLoc &DL,
8665 const SDNodeFlags Flags) {
8666 assert(Offset.getValueType().isInteger());
8667 EVT BasePtrVT = Ptr.getValueType();
8668 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8669 BasePtrVT))
8670 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8671 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8672 SDNodeFlags AddFlags = Flags;
8673 AddFlags.setInBounds(false);
8674 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8675}
8676
8677/// Returns true if memcpy source is constant data.
8679 uint64_t SrcDelta = 0;
8680 GlobalAddressSDNode *G = nullptr;
8681 if (Src.getOpcode() == ISD::GlobalAddress)
8683 else if (Src->isAnyAdd() &&
8684 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8685 Src.getOperand(1).getOpcode() == ISD::Constant) {
8686 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8687 SrcDelta = Src.getConstantOperandVal(1);
8688 }
8689 if (!G)
8690 return false;
8691
8692 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8693 SrcDelta + G->getOffset());
8694}
8695
8697 SelectionDAG &DAG) {
8698 // On Darwin, -Os means optimize for size without hurting performance, so
8699 // only really optimize for size when -Oz (MinSize) is used.
8701 return MF.getFunction().hasMinSize();
8702 return DAG.shouldOptForSize();
8703}
8704
8706 SmallVector<SDValue, 32> &OutChains, unsigned From,
8707 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8708 SmallVector<SDValue, 16> &OutStoreChains) {
8709 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8710 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8711 SmallVector<SDValue, 16> GluedLoadChains;
8712 for (unsigned i = From; i < To; ++i) {
8713 OutChains.push_back(OutLoadChains[i]);
8714 GluedLoadChains.push_back(OutLoadChains[i]);
8715 }
8716
8717 // Chain for all loads.
8718 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8719 GluedLoadChains);
8720
8721 for (unsigned i = From; i < To; ++i) {
8722 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8723 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8724 ST->getBasePtr(), ST->getMemoryVT(),
8725 ST->getMemOperand());
8726 OutChains.push_back(NewStore);
8727 }
8728}
8729
8731 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8732 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8733 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8734 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8735 // Turn a memcpy of undef to nop.
8736 // FIXME: We need to honor volatile even is Src is undef.
8737 if (Src.isUndef())
8738 return Chain;
8739
8740 // Expand memcpy to a series of load and store ops if the size operand falls
8741 // below a certain threshold.
8742 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8743 // rather than maybe a humongous number of loads and stores.
8744 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8745 const DataLayout &DL = DAG.getDataLayout();
8746 LLVMContext &C = *DAG.getContext();
8747 std::vector<EVT> MemOps;
8748 bool DstAlignCanChange = false;
8750 MachineFrameInfo &MFI = MF.getFrameInfo();
8751 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8753 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8754 DstAlignCanChange = true;
8755 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8756 if (!SrcAlign || Alignment > *SrcAlign)
8757 SrcAlign = Alignment;
8758 assert(SrcAlign && "SrcAlign must be set");
8760 // If marked as volatile, perform a copy even when marked as constant.
8761 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8762 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8763 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8764 const MemOp Op = isZeroConstant
8765 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8766 /*IsZeroMemset*/ true, isVol)
8767 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8768 *SrcAlign, isVol, CopyFromConstant);
8769 if (!TLI.findOptimalMemOpLowering(
8770 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8771 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8772 return SDValue();
8773
8774 if (DstAlignCanChange) {
8775 Type *Ty = MemOps[0].getTypeForEVT(C);
8776 Align NewAlign = DL.getABITypeAlign(Ty);
8777
8778 // Don't promote to an alignment that would require dynamic stack
8779 // realignment which may conflict with optimizations such as tail call
8780 // optimization.
8782 if (!TRI->hasStackRealignment(MF))
8783 if (MaybeAlign StackAlign = DL.getStackAlignment())
8784 NewAlign = std::min(NewAlign, *StackAlign);
8785
8786 if (NewAlign > Alignment) {
8787 // Give the stack frame object a larger alignment if needed.
8788 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8789 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8790 Alignment = NewAlign;
8791 }
8792 }
8793
8794 // Prepare AAInfo for loads/stores after lowering this memcpy.
8795 AAMDNodes NewAAInfo = AAInfo;
8796 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8797
8798 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8799 bool isConstant =
8800 BatchAA && SrcVal &&
8801 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8802
8803 MachineMemOperand::Flags MMOFlags =
8805 SmallVector<SDValue, 16> OutLoadChains;
8806 SmallVector<SDValue, 16> OutStoreChains;
8807 SmallVector<SDValue, 32> OutChains;
8808 unsigned NumMemOps = MemOps.size();
8809 uint64_t SrcOff = 0, DstOff = 0;
8810 for (unsigned i = 0; i != NumMemOps; ++i) {
8811 EVT VT = MemOps[i];
8812 unsigned VTSize = VT.getSizeInBits() / 8;
8813 SDValue Value, Store;
8814
8815 if (VTSize > Size) {
8816 // Issuing an unaligned load / store pair that overlaps with the previous
8817 // pair. Adjust the offset accordingly.
8818 assert(i == NumMemOps-1 && i != 0);
8819 SrcOff -= VTSize - Size;
8820 DstOff -= VTSize - Size;
8821 }
8822
8823 if (CopyFromConstant &&
8824 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8825 // It's unlikely a store of a vector immediate can be done in a single
8826 // instruction. It would require a load from a constantpool first.
8827 // We only handle zero vectors here.
8828 // FIXME: Handle other cases where store of vector immediate is done in
8829 // a single instruction.
8830 ConstantDataArraySlice SubSlice;
8831 if (SrcOff < Slice.Length) {
8832 SubSlice = Slice;
8833 SubSlice.move(SrcOff);
8834 } else {
8835 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8836 SubSlice.Array = nullptr;
8837 SubSlice.Offset = 0;
8838 SubSlice.Length = VTSize;
8839 }
8840 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8841 if (Value.getNode()) {
8842 Store = DAG.getStore(
8843 Chain, dl, Value,
8844 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8845 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8846 OutChains.push_back(Store);
8847 }
8848 }
8849
8850 if (!Store.getNode()) {
8851 // The type might not be legal for the target. This should only happen
8852 // if the type is smaller than a legal type, as on PPC, so the right
8853 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8854 // to Load/Store if NVT==VT.
8855 // FIXME does the case above also need this?
8856 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8857 assert(NVT.bitsGE(VT));
8858
8859 bool isDereferenceable =
8860 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8861 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8862 if (isDereferenceable)
8864 if (isConstant)
8865 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8866
8867 Value = DAG.getExtLoad(
8868 ISD::EXTLOAD, dl, NVT, Chain,
8869 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8870 SrcPtrInfo.getWithOffset(SrcOff), VT,
8871 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8872 OutLoadChains.push_back(Value.getValue(1));
8873
8874 Store = DAG.getTruncStore(
8875 Chain, dl, Value,
8876 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8877 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8878 OutStoreChains.push_back(Store);
8879 }
8880 SrcOff += VTSize;
8881 DstOff += VTSize;
8882 Size -= VTSize;
8883 }
8884
8885 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8887 unsigned NumLdStInMemcpy = OutStoreChains.size();
8888
8889 if (NumLdStInMemcpy) {
8890 // It may be that memcpy might be converted to memset if it's memcpy
8891 // of constants. In such a case, we won't have loads and stores, but
8892 // just stores. In the absence of loads, there is nothing to gang up.
8893 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8894 // If target does not care, just leave as it.
8895 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8896 OutChains.push_back(OutLoadChains[i]);
8897 OutChains.push_back(OutStoreChains[i]);
8898 }
8899 } else {
8900 // Ld/St less than/equal limit set by target.
8901 if (NumLdStInMemcpy <= GluedLdStLimit) {
8902 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8903 NumLdStInMemcpy, OutLoadChains,
8904 OutStoreChains);
8905 } else {
8906 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8907 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8908 unsigned GlueIter = 0;
8909
8910 // Residual ld/st.
8911 if (RemainingLdStInMemcpy) {
8913 DAG, dl, OutChains, NumLdStInMemcpy - RemainingLdStInMemcpy,
8914 NumLdStInMemcpy, OutLoadChains, OutStoreChains);
8915 }
8916
8917 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8918 unsigned IndexFrom = NumLdStInMemcpy - RemainingLdStInMemcpy -
8919 GlueIter - GluedLdStLimit;
8920 unsigned IndexTo = NumLdStInMemcpy - RemainingLdStInMemcpy - GlueIter;
8921 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8922 OutLoadChains, OutStoreChains);
8923 GlueIter += GluedLdStLimit;
8924 }
8925 }
8926 }
8927 }
8928 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8929}
8930
8932 SDValue Chain, SDValue Dst, SDValue Src,
8933 uint64_t Size, Align Alignment,
8934 bool isVol, bool AlwaysInline,
8935 MachinePointerInfo DstPtrInfo,
8936 MachinePointerInfo SrcPtrInfo,
8937 const AAMDNodes &AAInfo) {
8938 // Turn a memmove of undef to nop.
8939 // FIXME: We need to honor volatile even is Src is undef.
8940 if (Src.isUndef())
8941 return Chain;
8942
8943 // Expand memmove to a series of load and store ops if the size operand falls
8944 // below a certain threshold.
8945 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8946 const DataLayout &DL = DAG.getDataLayout();
8947 LLVMContext &C = *DAG.getContext();
8948 std::vector<EVT> MemOps;
8949 bool DstAlignCanChange = false;
8951 MachineFrameInfo &MFI = MF.getFrameInfo();
8952 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8954 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8955 DstAlignCanChange = true;
8956 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8957 if (!SrcAlign || Alignment > *SrcAlign)
8958 SrcAlign = Alignment;
8959 assert(SrcAlign && "SrcAlign must be set");
8960 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8961 if (!TLI.findOptimalMemOpLowering(
8962 C, MemOps, Limit,
8963 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8964 /*IsVolatile*/ true),
8965 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8966 MF.getFunction().getAttributes()))
8967 return SDValue();
8968
8969 if (DstAlignCanChange) {
8970 Type *Ty = MemOps[0].getTypeForEVT(C);
8971 Align NewAlign = DL.getABITypeAlign(Ty);
8972
8973 // Don't promote to an alignment that would require dynamic stack
8974 // realignment which may conflict with optimizations such as tail call
8975 // optimization.
8977 if (!TRI->hasStackRealignment(MF))
8978 if (MaybeAlign StackAlign = DL.getStackAlignment())
8979 NewAlign = std::min(NewAlign, *StackAlign);
8980
8981 if (NewAlign > Alignment) {
8982 // Give the stack frame object a larger alignment if needed.
8983 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8984 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8985 Alignment = NewAlign;
8986 }
8987 }
8988
8989 // Prepare AAInfo for loads/stores after lowering this memmove.
8990 AAMDNodes NewAAInfo = AAInfo;
8991 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8992
8993 MachineMemOperand::Flags MMOFlags =
8995 uint64_t SrcOff = 0, DstOff = 0;
8996 SmallVector<SDValue, 8> LoadValues;
8997 SmallVector<SDValue, 8> LoadChains;
8998 SmallVector<SDValue, 8> OutChains;
8999 unsigned NumMemOps = MemOps.size();
9000 for (unsigned i = 0; i < NumMemOps; i++) {
9001 EVT VT = MemOps[i];
9002 unsigned VTSize = VT.getSizeInBits() / 8;
9003 SDValue Value;
9004
9005 bool isDereferenceable =
9006 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9007 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9008 if (isDereferenceable)
9010
9011 Value = DAG.getLoad(
9012 VT, dl, Chain,
9013 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9014 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
9015 LoadValues.push_back(Value);
9016 LoadChains.push_back(Value.getValue(1));
9017 SrcOff += VTSize;
9018 }
9019 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
9020 OutChains.clear();
9021 for (unsigned i = 0; i < NumMemOps; i++) {
9022 EVT VT = MemOps[i];
9023 unsigned VTSize = VT.getSizeInBits() / 8;
9024 SDValue Store;
9025
9026 Store = DAG.getStore(
9027 Chain, dl, LoadValues[i],
9028 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9029 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
9030 OutChains.push_back(Store);
9031 DstOff += VTSize;
9032 }
9033
9034 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9035}
9036
9037/// Lower the call to 'memset' intrinsic function into a series of store
9038/// operations.
9039///
9040/// \param DAG Selection DAG where lowered code is placed.
9041/// \param dl Link to corresponding IR location.
9042/// \param Chain Control flow dependency.
9043/// \param Dst Pointer to destination memory location.
9044/// \param Src Value of byte to write into the memory.
9045/// \param Size Number of bytes to write.
9046/// \param Alignment Alignment of the destination in bytes.
9047/// \param isVol True if destination is volatile.
9048/// \param AlwaysInline Makes sure no function call is generated.
9049/// \param DstPtrInfo IR information on the memory pointer.
9050/// \returns New head in the control flow, if lowering was successful, empty
9051/// SDValue otherwise.
9052///
9053/// The function tries to replace 'llvm.memset' intrinsic with several store
9054/// operations and value calculation code. This is usually profitable for small
9055/// memory size or when the semantic requires inlining.
9057 SDValue Chain, SDValue Dst, SDValue Src,
9058 uint64_t Size, Align Alignment, bool isVol,
9059 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9060 const AAMDNodes &AAInfo) {
9061 // Turn a memset of undef to nop.
9062 // FIXME: We need to honor volatile even is Src is undef.
9063 if (Src.isUndef())
9064 return Chain;
9065
9066 // Expand memset to a series of load/store ops if the size operand
9067 // falls below a certain threshold.
9068 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9069 std::vector<EVT> MemOps;
9070 bool DstAlignCanChange = false;
9071 LLVMContext &C = *DAG.getContext();
9073 MachineFrameInfo &MFI = MF.getFrameInfo();
9074 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9076 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9077 DstAlignCanChange = true;
9078 bool IsZeroVal = isNullConstant(Src);
9079 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9080
9081 if (!TLI.findOptimalMemOpLowering(
9082 C, MemOps, Limit,
9083 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9084 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
9085 return SDValue();
9086
9087 if (DstAlignCanChange) {
9088 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9089 const DataLayout &DL = DAG.getDataLayout();
9090 Align NewAlign = DL.getABITypeAlign(Ty);
9091
9092 // Don't promote to an alignment that would require dynamic stack
9093 // realignment which may conflict with optimizations such as tail call
9094 // optimization.
9096 if (!TRI->hasStackRealignment(MF))
9097 if (MaybeAlign StackAlign = DL.getStackAlignment())
9098 NewAlign = std::min(NewAlign, *StackAlign);
9099
9100 if (NewAlign > Alignment) {
9101 // Give the stack frame object a larger alignment if needed.
9102 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9103 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9104 Alignment = NewAlign;
9105 }
9106 }
9107
9108 SmallVector<SDValue, 8> OutChains;
9109 uint64_t DstOff = 0;
9110 unsigned NumMemOps = MemOps.size();
9111
9112 // Find the largest store and generate the bit pattern for it.
9113 EVT LargestVT = MemOps[0];
9114 for (unsigned i = 1; i < NumMemOps; i++)
9115 if (MemOps[i].bitsGT(LargestVT))
9116 LargestVT = MemOps[i];
9117 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9118
9119 // Prepare AAInfo for loads/stores after lowering this memset.
9120 AAMDNodes NewAAInfo = AAInfo;
9121 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9122
9123 for (unsigned i = 0; i < NumMemOps; i++) {
9124 EVT VT = MemOps[i];
9125 unsigned VTSize = VT.getSizeInBits() / 8;
9126 if (VTSize > Size) {
9127 // Issuing an unaligned load / store pair that overlaps with the previous
9128 // pair. Adjust the offset accordingly.
9129 assert(i == NumMemOps-1 && i != 0);
9130 DstOff -= VTSize - Size;
9131 }
9132
9133 // If this store is smaller than the largest store see whether we can get
9134 // the smaller value for free with a truncate or extract vector element and
9135 // then store.
9136 SDValue Value = MemSetValue;
9137 if (VT.bitsLT(LargestVT)) {
9138 unsigned Index;
9139 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9140 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9141 if (!LargestVT.isVector() && !VT.isVector() &&
9142 TLI.isTruncateFree(LargestVT, VT))
9143 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9144 else if (LargestVT.isVector() && !VT.isVector() &&
9146 LargestVT.getTypeForEVT(*DAG.getContext()),
9147 VT.getSizeInBits(), Index) &&
9148 TLI.isTypeLegal(SVT) &&
9149 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9150 // Target which can combine store(extractelement VectorTy, Idx) can get
9151 // the smaller value for free.
9152 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9153 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9154 } else
9155 Value = getMemsetValue(Src, VT, DAG, dl);
9156 }
9157 assert(Value.getValueType() == VT && "Value with wrong type.");
9158 SDValue Store = DAG.getStore(
9159 Chain, dl, Value,
9160 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9161 DstPtrInfo.getWithOffset(DstOff), Alignment,
9163 NewAAInfo);
9164 OutChains.push_back(Store);
9165 DstOff += VT.getSizeInBits() / 8;
9166 Size -= VTSize;
9167 }
9168
9169 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9170}
9171
9173 unsigned AS) {
9174 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9175 // pointer operands can be losslessly bitcasted to pointers of address space 0
9176 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9177 report_fatal_error("cannot lower memory intrinsic in address space " +
9178 Twine(AS));
9179 }
9180}
9181
9183 const SelectionDAG *SelDAG,
9184 bool AllowReturnsFirstArg) {
9185 if (!CI || !CI->isTailCall())
9186 return false;
9187 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9188 // helper symbol we lower to.
9189 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9190 AllowReturnsFirstArg &&
9192}
9193
9194static std::pair<SDValue, SDValue>
9197 const CallInst *CI, RTLIB::Libcall Call,
9198 SelectionDAG *DAG, const TargetLowering *TLI) {
9199 RTLIB::LibcallImpl LCImpl = DAG->getLibcalls().getLibcallImpl(Call);
9200
9201 if (LCImpl == RTLIB::Unsupported)
9202 return {};
9203
9205 bool IsTailCall =
9206 isInTailCallPositionWrapper(CI, DAG, /*AllowReturnsFirstArg=*/true);
9207 SDValue Callee =
9208 DAG->getExternalSymbol(LCImpl, TLI->getPointerTy(DAG->getDataLayout()));
9209
9210 CLI.setDebugLoc(dl)
9211 .setChain(Chain)
9213 CI->getType(), Callee, std::move(Args))
9214 .setTailCall(IsTailCall);
9215
9216 return TLI->LowerCallTo(CLI);
9217}
9218
9219std::pair<SDValue, SDValue> SelectionDAG::getStrstr(SDValue Chain,
9220 const SDLoc &dl, SDValue S1,
9221 SDValue S2,
9222 const CallInst *CI) {
9224 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9225 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9226 RTLIB::STRSTR, this, TLI);
9227}
9228
9229std::pair<SDValue, SDValue>
9231 SDValue Mem1, SDValue Size, const CallInst *CI) {
9232 RTLIB::LibcallImpl MemcmpImpl = Libcalls->getLibcallImpl(RTLIB::MEMCMP);
9233 if (MemcmpImpl == RTLIB::Unsupported)
9234 return {};
9235
9238 {Mem0, PT},
9239 {Mem1, PT},
9241
9243 bool IsTailCall =
9244 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9245
9246 CLI.setDebugLoc(dl)
9247 .setChain(Chain)
9248 .setLibCallee(
9249 Libcalls->getLibcallImplCallingConv(MemcmpImpl),
9251 getExternalSymbol(MemcmpImpl, TLI->getPointerTy(getDataLayout())),
9252 std::move(Args))
9253 .setTailCall(IsTailCall);
9254
9255 return TLI->LowerCallTo(CLI);
9256}
9257
9258std::pair<SDValue, SDValue> SelectionDAG::getStrcpy(SDValue Chain,
9259 const SDLoc &dl,
9260 SDValue Dst, SDValue Src,
9261 const CallInst *CI) {
9262 RTLIB::LibcallImpl LCImpl = Libcalls->getLibcallImpl(RTLIB::STRCPY);
9263 if (LCImpl == RTLIB::Unsupported)
9264 return {};
9265
9267 TargetLowering::ArgListTy Args = {{Dst, PT}, {Src, PT}};
9268
9270 bool IsTailCall =
9271 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg=*/true);
9272
9273 CLI.setDebugLoc(dl)
9274 .setChain(Chain)
9275 .setLibCallee(
9276 Libcalls->getLibcallImplCallingConv(LCImpl), CI->getType(),
9277 getExternalSymbol(LCImpl, TLI->getPointerTy(getDataLayout())),
9278 std::move(Args))
9279 .setTailCall(IsTailCall);
9280
9281 return TLI->LowerCallTo(CLI);
9282}
9283
9284std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9285 const SDLoc &dl,
9286 SDValue Src,
9287 const CallInst *CI) {
9288 RTLIB::LibcallImpl StrlenImpl = Libcalls->getLibcallImpl(RTLIB::STRLEN);
9289 if (StrlenImpl == RTLIB::Unsupported)
9290 return {};
9291
9292 // Emit a library call.
9295
9297 bool IsTailCall =
9298 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9299
9300 CLI.setDebugLoc(dl)
9301 .setChain(Chain)
9302 .setLibCallee(Libcalls->getLibcallImplCallingConv(StrlenImpl),
9303 CI->getType(),
9305 StrlenImpl, TLI->getProgramPointerTy(getDataLayout())),
9306 std::move(Args))
9307 .setTailCall(IsTailCall);
9308
9309 return TLI->LowerCallTo(CLI);
9310}
9311
9313 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9314 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9315 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9316 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9317 BatchAAResults *BatchAA) {
9318 // Check to see if we should lower the memcpy to loads and stores first.
9319 // For cases within the target-specified limits, this is the best choice.
9321 if (ConstantSize) {
9322 // Memcpy with size zero? Just return the original chain.
9323 if (ConstantSize->isZero())
9324 return Chain;
9325
9327 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9328 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9329 if (Result.getNode())
9330 return Result;
9331 }
9332
9333 // Then check to see if we should lower the memcpy with target-specific
9334 // code. If the target chooses to do this, this is the next best.
9335 if (TSI) {
9336 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9337 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9338 DstPtrInfo, SrcPtrInfo);
9339 if (Result.getNode())
9340 return Result;
9341 }
9342
9343 // If we really need inline code and the target declined to provide it,
9344 // use a (potentially long) sequence of loads and stores.
9345 if (AlwaysInline) {
9346 assert(ConstantSize && "AlwaysInline requires a constant size!");
9348 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9349 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9350 }
9351
9354
9355 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9356 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9357 // respect volatile, so they may do things like read or write memory
9358 // beyond the given memory regions. But fixing this isn't easy, and most
9359 // people don't care.
9360
9361 // Emit a library call.
9364 Args.emplace_back(Dst, PtrTy);
9365 Args.emplace_back(Src, PtrTy);
9366 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9367 // FIXME: pass in SDLoc
9369 bool IsTailCall = false;
9370 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9371
9372 if (OverrideTailCall.has_value()) {
9373 IsTailCall = *OverrideTailCall;
9374 } else {
9375 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9376 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9377 }
9378
9379 CLI.setDebugLoc(dl)
9380 .setChain(Chain)
9381 .setLibCallee(
9382 Libcalls->getLibcallImplCallingConv(MemCpyImpl),
9383 Dst.getValueType().getTypeForEVT(*getContext()),
9384 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
9385 std::move(Args))
9387 .setTailCall(IsTailCall);
9388
9389 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9390 return CallResult.second;
9391}
9392
9394 SDValue Dst, SDValue Src, SDValue Size,
9395 Type *SizeTy, unsigned ElemSz,
9396 bool isTailCall,
9397 MachinePointerInfo DstPtrInfo,
9398 MachinePointerInfo SrcPtrInfo) {
9399 // Emit a library call.
9402 Args.emplace_back(Dst, ArgTy);
9403 Args.emplace_back(Src, ArgTy);
9404 Args.emplace_back(Size, SizeTy);
9405
9406 RTLIB::Libcall LibraryCall =
9408 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9409 if (LibcallImpl == RTLIB::Unsupported)
9410 report_fatal_error("Unsupported element size");
9411
9413 CLI.setDebugLoc(dl)
9414 .setChain(Chain)
9415 .setLibCallee(
9416 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9418 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9419 std::move(Args))
9421 .setTailCall(isTailCall);
9422
9423 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9424 return CallResult.second;
9425}
9426
9428 SDValue Src, SDValue Size, Align Alignment,
9429 bool isVol, const CallInst *CI,
9430 std::optional<bool> OverrideTailCall,
9431 MachinePointerInfo DstPtrInfo,
9432 MachinePointerInfo SrcPtrInfo,
9433 const AAMDNodes &AAInfo,
9434 BatchAAResults *BatchAA) {
9435 // Check to see if we should lower the memmove to loads and stores first.
9436 // For cases within the target-specified limits, this is the best choice.
9438 if (ConstantSize) {
9439 // Memmove with size zero? Just return the original chain.
9440 if (ConstantSize->isZero())
9441 return Chain;
9442
9444 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9445 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9446 if (Result.getNode())
9447 return Result;
9448 }
9449
9450 // Then check to see if we should lower the memmove with target-specific
9451 // code. If the target chooses to do this, this is the next best.
9452 if (TSI) {
9453 SDValue Result =
9454 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9455 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9456 if (Result.getNode())
9457 return Result;
9458 }
9459
9462
9463 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9464 // not be safe. See memcpy above for more details.
9465
9466 // Emit a library call.
9469 Args.emplace_back(Dst, PtrTy);
9470 Args.emplace_back(Src, PtrTy);
9471 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9472 // FIXME: pass in SDLoc
9474
9475 RTLIB::LibcallImpl MemmoveImpl = Libcalls->getLibcallImpl(RTLIB::MEMMOVE);
9476
9477 bool IsTailCall = false;
9478 if (OverrideTailCall.has_value()) {
9479 IsTailCall = *OverrideTailCall;
9480 } else {
9481 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9482 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9483 }
9484
9485 CLI.setDebugLoc(dl)
9486 .setChain(Chain)
9487 .setLibCallee(
9488 Libcalls->getLibcallImplCallingConv(MemmoveImpl),
9489 Dst.getValueType().getTypeForEVT(*getContext()),
9490 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
9491 std::move(Args))
9493 .setTailCall(IsTailCall);
9494
9495 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9496 return CallResult.second;
9497}
9498
9500 SDValue Dst, SDValue Src, SDValue Size,
9501 Type *SizeTy, unsigned ElemSz,
9502 bool isTailCall,
9503 MachinePointerInfo DstPtrInfo,
9504 MachinePointerInfo SrcPtrInfo) {
9505 // Emit a library call.
9507 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9508 Args.emplace_back(Dst, IntPtrTy);
9509 Args.emplace_back(Src, IntPtrTy);
9510 Args.emplace_back(Size, SizeTy);
9511
9512 RTLIB::Libcall LibraryCall =
9514 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9515 if (LibcallImpl == RTLIB::Unsupported)
9516 report_fatal_error("Unsupported element size");
9517
9519 CLI.setDebugLoc(dl)
9520 .setChain(Chain)
9521 .setLibCallee(
9522 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9524 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9525 std::move(Args))
9527 .setTailCall(isTailCall);
9528
9529 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9530 return CallResult.second;
9531}
9532
9534 SDValue Src, SDValue Size, Align Alignment,
9535 bool isVol, bool AlwaysInline,
9536 const CallInst *CI,
9537 MachinePointerInfo DstPtrInfo,
9538 const AAMDNodes &AAInfo) {
9539 // Check to see if we should lower the memset to stores first.
9540 // For cases within the target-specified limits, this is the best choice.
9542 if (ConstantSize) {
9543 // Memset with size zero? Just return the original chain.
9544 if (ConstantSize->isZero())
9545 return Chain;
9546
9547 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9548 ConstantSize->getZExtValue(), Alignment,
9549 isVol, false, DstPtrInfo, AAInfo);
9550
9551 if (Result.getNode())
9552 return Result;
9553 }
9554
9555 // Then check to see if we should lower the memset with target-specific
9556 // code. If the target chooses to do this, this is the next best.
9557 if (TSI) {
9558 SDValue Result = TSI->EmitTargetCodeForMemset(
9559 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9560 if (Result.getNode())
9561 return Result;
9562 }
9563
9564 // If we really need inline code and the target declined to provide it,
9565 // use a (potentially long) sequence of loads and stores.
9566 if (AlwaysInline) {
9567 assert(ConstantSize && "AlwaysInline requires a constant size!");
9568 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9569 ConstantSize->getZExtValue(), Alignment,
9570 isVol, true, DstPtrInfo, AAInfo);
9571 assert(Result &&
9572 "getMemsetStores must return a valid sequence when AlwaysInline");
9573 return Result;
9574 }
9575
9577
9578 // Emit a library call.
9579 auto &Ctx = *getContext();
9580 const auto& DL = getDataLayout();
9581
9583 // FIXME: pass in SDLoc
9584 CLI.setDebugLoc(dl).setChain(Chain);
9585
9586 RTLIB::LibcallImpl BzeroImpl = Libcalls->getLibcallImpl(RTLIB::BZERO);
9587 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
9588
9589 // If zeroing out and bzero is present, use it.
9590 if (UseBZero) {
9592 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9593 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9594 CLI.setLibCallee(
9595 Libcalls->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
9596 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
9597 } else {
9598 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
9599
9601 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9602 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9603 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9604 CLI.setLibCallee(Libcalls->getLibcallImplCallingConv(MemsetImpl),
9605 Dst.getValueType().getTypeForEVT(Ctx),
9606 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
9607 std::move(Args));
9608 }
9609
9610 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
9611 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9612
9613 // If we're going to use bzero, make sure not to tail call unless the
9614 // subsequent return doesn't need a value, as bzero doesn't return the first
9615 // arg unlike memset.
9616 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9617 bool IsTailCall =
9618 CI && CI->isTailCall() &&
9619 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9620 CLI.setDiscardResult().setTailCall(IsTailCall);
9621
9622 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9623 return CallResult.second;
9624}
9625
9628 Type *SizeTy, unsigned ElemSz,
9629 bool isTailCall,
9630 MachinePointerInfo DstPtrInfo) {
9631 // Emit a library call.
9633 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9634 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9635 Args.emplace_back(Size, SizeTy);
9636
9637 RTLIB::Libcall LibraryCall =
9639 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9640 if (LibcallImpl == RTLIB::Unsupported)
9641 report_fatal_error("Unsupported element size");
9642
9644 CLI.setDebugLoc(dl)
9645 .setChain(Chain)
9646 .setLibCallee(
9647 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9649 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9650 std::move(Args))
9652 .setTailCall(isTailCall);
9653
9654 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9655 return CallResult.second;
9656}
9657
9658SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9660 MachineMemOperand *MMO,
9661 ISD::LoadExtType ExtType) {
9663 AddNodeIDNode(ID, Opcode, VTList, Ops);
9664 ID.AddInteger(MemVT.getRawBits());
9665 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9666 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9667 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9668 ID.AddInteger(MMO->getFlags());
9669 void* IP = nullptr;
9670 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9671 E->refineAlignment(MMO);
9672 E->refineRanges(MMO);
9673 return SDValue(E, 0);
9674 }
9675
9676 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9677 VTList, MemVT, MMO, ExtType);
9678 createOperands(N, Ops);
9679
9680 CSEMap.InsertNode(N, IP);
9681 InsertNode(N);
9682 SDValue V(N, 0);
9683 NewSDValueDbgMsg(V, "Creating new node: ", this);
9684 return V;
9685}
9686
9688 EVT MemVT, SDVTList VTs, SDValue Chain,
9689 SDValue Ptr, SDValue Cmp, SDValue Swp,
9690 MachineMemOperand *MMO) {
9691 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9693 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9694
9695 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9696 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9697}
9698
9699SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9700 SDValue Chain, SDValue Ptr, SDValue Val,
9701 MachineMemOperand *MMO) {
9702 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9703 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9704 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9705 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9706 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9707 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9708 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9709 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9710 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9711 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9712 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9713 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9714 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9715 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9716 Opcode == ISD::ATOMIC_STORE) &&
9717 "Invalid Atomic Op");
9718
9719 EVT VT = Val.getValueType();
9720
9721 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9722 getVTList(VT, MVT::Other);
9723 SDValue Ops[] = {Chain, Ptr, Val};
9724 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9725}
9726
9728 EVT MemVT, EVT VT, SDValue Chain,
9729 SDValue Ptr, MachineMemOperand *MMO) {
9730 SDVTList VTs = getVTList(VT, MVT::Other);
9731 SDValue Ops[] = {Chain, Ptr};
9732 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9733}
9734
9735/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9737 if (Ops.size() == 1)
9738 return Ops[0];
9739
9741 VTs.reserve(Ops.size());
9742 for (const SDValue &Op : Ops)
9743 VTs.push_back(Op.getValueType());
9744 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9745}
9746
9748 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9749 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9751 const AAMDNodes &AAInfo) {
9752 if (Size.hasValue() && !Size.getValue())
9754
9756 MachineMemOperand *MMO =
9757 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9758
9759 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9760}
9761
9763 SDVTList VTList,
9764 ArrayRef<SDValue> Ops, EVT MemVT,
9765 MachineMemOperand *MMO) {
9766 assert(
9767 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9768 Opcode == ISD::PREFETCH ||
9769 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9770 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9771 "Opcode is not a memory-accessing opcode!");
9772
9773 // Memoize the node unless it returns a glue result.
9775 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9777 AddNodeIDNode(ID, Opcode, VTList, Ops);
9778 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9779 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9780 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9781 ID.AddInteger(MMO->getFlags());
9782 ID.AddInteger(MemVT.getRawBits());
9783 void *IP = nullptr;
9784 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9785 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9786 return SDValue(E, 0);
9787 }
9788
9789 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9790 VTList, MemVT, MMO);
9791 createOperands(N, Ops);
9792
9793 CSEMap.InsertNode(N, IP);
9794 } else {
9795 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9796 VTList, MemVT, MMO);
9797 createOperands(N, Ops);
9798 }
9799 InsertNode(N);
9800 SDValue V(N, 0);
9801 NewSDValueDbgMsg(V, "Creating new node: ", this);
9802 return V;
9803}
9804
9806 SDValue Chain, int FrameIndex) {
9807 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9808 const auto VTs = getVTList(MVT::Other);
9809 SDValue Ops[2] = {
9810 Chain,
9811 getFrameIndex(FrameIndex,
9812 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9813 true)};
9814
9816 AddNodeIDNode(ID, Opcode, VTs, Ops);
9817 ID.AddInteger(FrameIndex);
9818 void *IP = nullptr;
9819 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9820 return SDValue(E, 0);
9821
9822 LifetimeSDNode *N =
9823 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9824 createOperands(N, Ops);
9825 CSEMap.InsertNode(N, IP);
9826 InsertNode(N);
9827 SDValue V(N, 0);
9828 NewSDValueDbgMsg(V, "Creating new node: ", this);
9829 return V;
9830}
9831
9833 uint64_t Guid, uint64_t Index,
9834 uint32_t Attr) {
9835 const unsigned Opcode = ISD::PSEUDO_PROBE;
9836 const auto VTs = getVTList(MVT::Other);
9837 SDValue Ops[] = {Chain};
9839 AddNodeIDNode(ID, Opcode, VTs, Ops);
9840 ID.AddInteger(Guid);
9841 ID.AddInteger(Index);
9842 void *IP = nullptr;
9843 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9844 return SDValue(E, 0);
9845
9846 auto *N = newSDNode<PseudoProbeSDNode>(
9847 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9848 createOperands(N, Ops);
9849 CSEMap.InsertNode(N, IP);
9850 InsertNode(N);
9851 SDValue V(N, 0);
9852 NewSDValueDbgMsg(V, "Creating new node: ", this);
9853 return V;
9854}
9855
9856/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9857/// MachinePointerInfo record from it. This is particularly useful because the
9858/// code generator has many cases where it doesn't bother passing in a
9859/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9861 SelectionDAG &DAG, SDValue Ptr,
9862 int64_t Offset = 0) {
9863 // If this is FI+Offset, we can model it.
9864 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9866 FI->getIndex(), Offset);
9867
9868 // If this is (FI+Offset1)+Offset2, we can model it.
9869 if (Ptr.getOpcode() != ISD::ADD ||
9872 return Info;
9873
9874 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9876 DAG.getMachineFunction(), FI,
9877 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9878}
9879
9880/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9881/// MachinePointerInfo record from it. This is particularly useful because the
9882/// code generator has many cases where it doesn't bother passing in a
9883/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9885 SelectionDAG &DAG, SDValue Ptr,
9886 SDValue OffsetOp) {
9887 // If the 'Offset' value isn't a constant, we can't handle this.
9889 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9890 if (OffsetOp.isUndef())
9891 return InferPointerInfo(Info, DAG, Ptr);
9892 return Info;
9893}
9894
9896 EVT VT, const SDLoc &dl, SDValue Chain,
9897 SDValue Ptr, SDValue Offset,
9898 MachinePointerInfo PtrInfo, EVT MemVT,
9899 Align Alignment,
9900 MachineMemOperand::Flags MMOFlags,
9901 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9902 assert(Chain.getValueType() == MVT::Other &&
9903 "Invalid chain type");
9904
9905 MMOFlags |= MachineMemOperand::MOLoad;
9906 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9907 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9908 // clients.
9909 if (PtrInfo.V.isNull())
9910 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9911
9912 TypeSize Size = MemVT.getStoreSize();
9914 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9915 Alignment, AAInfo, Ranges);
9916 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9917}
9918
9920 EVT VT, const SDLoc &dl, SDValue Chain,
9921 SDValue Ptr, SDValue Offset, EVT MemVT,
9922 MachineMemOperand *MMO) {
9923 if (VT == MemVT) {
9924 ExtType = ISD::NON_EXTLOAD;
9925 } else if (ExtType == ISD::NON_EXTLOAD) {
9926 assert(VT == MemVT && "Non-extending load from different memory type!");
9927 } else {
9928 // Extending load.
9929 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9930 "Should only be an extending load, not truncating!");
9931 assert(VT.isInteger() == MemVT.isInteger() &&
9932 "Cannot convert from FP to Int or Int -> FP!");
9933 assert(VT.isVector() == MemVT.isVector() &&
9934 "Cannot use an ext load to convert to or from a vector!");
9935 assert((!VT.isVector() ||
9937 "Cannot use an ext load to change the number of vector elements!");
9938 }
9939
9940 assert((!MMO->getRanges() ||
9942 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9943 MemVT.isInteger())) &&
9944 "Range metadata and load type must match!");
9945
9946 bool Indexed = AM != ISD::UNINDEXED;
9947 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9948
9949 SDVTList VTs = Indexed ?
9950 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9951 SDValue Ops[] = { Chain, Ptr, Offset };
9953 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9954 ID.AddInteger(MemVT.getRawBits());
9955 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9956 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9957 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9958 ID.AddInteger(MMO->getFlags());
9959 void *IP = nullptr;
9960 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9961 E->refineAlignment(MMO);
9962 E->refineRanges(MMO);
9963 return SDValue(E, 0);
9964 }
9965 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9966 ExtType, MemVT, MMO);
9967 createOperands(N, Ops);
9968
9969 CSEMap.InsertNode(N, IP);
9970 InsertNode(N);
9971 SDValue V(N, 0);
9972 NewSDValueDbgMsg(V, "Creating new node: ", this);
9973 return V;
9974}
9975
9977 SDValue Ptr, MachinePointerInfo PtrInfo,
9978 MaybeAlign Alignment,
9979 MachineMemOperand::Flags MMOFlags,
9980 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9981 SDValue Undef = getUNDEF(Ptr.getValueType());
9982 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9983 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9984}
9985
9987 SDValue Ptr, MachineMemOperand *MMO) {
9988 SDValue Undef = getUNDEF(Ptr.getValueType());
9989 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9990 VT, MMO);
9991}
9992
9994 EVT VT, SDValue Chain, SDValue Ptr,
9995 MachinePointerInfo PtrInfo, EVT MemVT,
9996 MaybeAlign Alignment,
9997 MachineMemOperand::Flags MMOFlags,
9998 const AAMDNodes &AAInfo) {
9999 SDValue Undef = getUNDEF(Ptr.getValueType());
10000 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
10001 MemVT, Alignment, MMOFlags, AAInfo);
10002}
10003
10005 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
10006 MachineMemOperand *MMO) {
10007 SDValue Undef = getUNDEF(Ptr.getValueType());
10008 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
10009 MemVT, MMO);
10010}
10011
10015 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
10016 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10017 // Don't propagate the invariant or dereferenceable flags.
10018 auto MMOFlags =
10019 LD->getMemOperand()->getFlags() &
10021 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10022 LD->getChain(), Base, Offset, LD->getPointerInfo(),
10023 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
10024}
10025
10027 SDValue Ptr, MachinePointerInfo PtrInfo,
10028 Align Alignment,
10029 MachineMemOperand::Flags MMOFlags,
10030 const AAMDNodes &AAInfo) {
10031 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10032
10033 MMOFlags |= MachineMemOperand::MOStore;
10034 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10035
10036 if (PtrInfo.V.isNull())
10037 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10038
10041 MachineMemOperand *MMO =
10042 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
10043 return getStore(Chain, dl, Val, Ptr, MMO);
10044}
10045
10047 SDValue Ptr, MachineMemOperand *MMO) {
10048 SDValue Undef = getUNDEF(Ptr.getValueType());
10049 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
10051}
10052
10054 SDValue Ptr, SDValue Offset, EVT SVT,
10056 bool IsTruncating) {
10057 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10058 EVT VT = Val.getValueType();
10059 if (VT == SVT) {
10060 IsTruncating = false;
10061 } else if (!IsTruncating) {
10062 assert(VT == SVT && "No-truncating store from different memory type!");
10063 } else {
10065 "Should only be a truncating store, not extending!");
10066 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10067 assert(VT.isVector() == SVT.isVector() &&
10068 "Cannot use trunc store to convert to or from a vector!");
10069 assert((!VT.isVector() ||
10071 "Cannot use trunc store to change the number of vector elements!");
10072 }
10073
10074 bool Indexed = AM != ISD::UNINDEXED;
10075 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
10076 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10077 : getVTList(MVT::Other);
10078 SDValue Ops[] = {Chain, Val, Ptr, Offset};
10081 ID.AddInteger(SVT.getRawBits());
10082 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
10083 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
10084 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10085 ID.AddInteger(MMO->getFlags());
10086 void *IP = nullptr;
10087 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10088 cast<StoreSDNode>(E)->refineAlignment(MMO);
10089 return SDValue(E, 0);
10090 }
10091 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10092 IsTruncating, SVT, MMO);
10093 createOperands(N, Ops);
10094
10095 CSEMap.InsertNode(N, IP);
10096 InsertNode(N);
10097 SDValue V(N, 0);
10098 NewSDValueDbgMsg(V, "Creating new node: ", this);
10099 return V;
10100}
10101
10103 SDValue Ptr, MachinePointerInfo PtrInfo,
10104 EVT SVT, Align Alignment,
10105 MachineMemOperand::Flags MMOFlags,
10106 const AAMDNodes &AAInfo) {
10107 assert(Chain.getValueType() == MVT::Other &&
10108 "Invalid chain type");
10109
10110 MMOFlags |= MachineMemOperand::MOStore;
10111 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10112
10113 if (PtrInfo.V.isNull())
10114 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10115
10117 MachineMemOperand *MMO = MF.getMachineMemOperand(
10118 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10119 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10120}
10121
10123 SDValue Ptr, EVT SVT,
10124 MachineMemOperand *MMO) {
10125 SDValue Undef = getUNDEF(Ptr.getValueType());
10126 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10127}
10128
10132 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10133 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10134 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10135 ST->getMemoryVT(), ST->getMemOperand(), AM,
10136 ST->isTruncatingStore());
10137}
10138
10140 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10141 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10142 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10143 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10144 const MDNode *Ranges, bool IsExpanding) {
10145 MMOFlags |= MachineMemOperand::MOLoad;
10146 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10147 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10148 // clients.
10149 if (PtrInfo.V.isNull())
10150 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10151
10152 TypeSize Size = MemVT.getStoreSize();
10154 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10155 Alignment, AAInfo, Ranges);
10156 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10157 MMO, IsExpanding);
10158}
10159
10161 ISD::LoadExtType ExtType, EVT VT,
10162 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10163 SDValue Offset, SDValue Mask, SDValue EVL,
10164 EVT MemVT, MachineMemOperand *MMO,
10165 bool IsExpanding) {
10166 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10167 assert(Mask.getValueType().getVectorElementCount() ==
10168 VT.getVectorElementCount() &&
10169 "Vector width mismatch between mask and data");
10170
10171 bool Indexed = AM != ISD::UNINDEXED;
10172 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10173
10174 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10175 : getVTList(VT, MVT::Other);
10176 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10178 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10179 ID.AddInteger(MemVT.getRawBits());
10180 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10181 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10182 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10183 ID.AddInteger(MMO->getFlags());
10184 void *IP = nullptr;
10185 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10186 E->refineAlignment(MMO);
10187 E->refineRanges(MMO);
10188 return SDValue(E, 0);
10189 }
10190 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10191 ExtType, IsExpanding, MemVT, MMO);
10192 createOperands(N, Ops);
10193
10194 CSEMap.InsertNode(N, IP);
10195 InsertNode(N);
10196 SDValue V(N, 0);
10197 NewSDValueDbgMsg(V, "Creating new node: ", this);
10198 return V;
10199}
10200
10202 SDValue Ptr, SDValue Mask, SDValue EVL,
10203 MachinePointerInfo PtrInfo,
10204 MaybeAlign Alignment,
10205 MachineMemOperand::Flags MMOFlags,
10206 const AAMDNodes &AAInfo, const MDNode *Ranges,
10207 bool IsExpanding) {
10208 SDValue Undef = getUNDEF(Ptr.getValueType());
10209 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10210 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10211 IsExpanding);
10212}
10213
10215 SDValue Ptr, SDValue Mask, SDValue EVL,
10216 MachineMemOperand *MMO, bool IsExpanding) {
10217 SDValue Undef = getUNDEF(Ptr.getValueType());
10218 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10219 Mask, EVL, VT, MMO, IsExpanding);
10220}
10221
10223 EVT VT, SDValue Chain, SDValue Ptr,
10224 SDValue Mask, SDValue EVL,
10225 MachinePointerInfo PtrInfo, EVT MemVT,
10226 MaybeAlign Alignment,
10227 MachineMemOperand::Flags MMOFlags,
10228 const AAMDNodes &AAInfo, bool IsExpanding) {
10229 SDValue Undef = getUNDEF(Ptr.getValueType());
10230 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10231 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10232 IsExpanding);
10233}
10234
10236 EVT VT, SDValue Chain, SDValue Ptr,
10237 SDValue Mask, SDValue EVL, EVT MemVT,
10238 MachineMemOperand *MMO, bool IsExpanding) {
10239 SDValue Undef = getUNDEF(Ptr.getValueType());
10240 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10241 EVL, MemVT, MMO, IsExpanding);
10242}
10243
10247 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10248 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10249 // Don't propagate the invariant or dereferenceable flags.
10250 auto MMOFlags =
10251 LD->getMemOperand()->getFlags() &
10253 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10254 LD->getChain(), Base, Offset, LD->getMask(),
10255 LD->getVectorLength(), LD->getPointerInfo(),
10256 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10257 nullptr, LD->isExpandingLoad());
10258}
10259
10261 SDValue Ptr, SDValue Offset, SDValue Mask,
10262 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10263 ISD::MemIndexedMode AM, bool IsTruncating,
10264 bool IsCompressing) {
10265 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10266 assert(Mask.getValueType().getVectorElementCount() ==
10268 "Vector width mismatch between mask and data");
10269
10270 bool Indexed = AM != ISD::UNINDEXED;
10271 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10272 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10273 : getVTList(MVT::Other);
10274 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10276 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10277 ID.AddInteger(MemVT.getRawBits());
10278 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10279 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10280 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10281 ID.AddInteger(MMO->getFlags());
10282 void *IP = nullptr;
10283 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10284 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10285 return SDValue(E, 0);
10286 }
10287 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10288 IsTruncating, IsCompressing, MemVT, MMO);
10289 createOperands(N, Ops);
10290
10291 CSEMap.InsertNode(N, IP);
10292 InsertNode(N);
10293 SDValue V(N, 0);
10294 NewSDValueDbgMsg(V, "Creating new node: ", this);
10295 return V;
10296}
10297
10299 SDValue Val, SDValue Ptr, SDValue Mask,
10300 SDValue EVL, MachinePointerInfo PtrInfo,
10301 EVT SVT, Align Alignment,
10302 MachineMemOperand::Flags MMOFlags,
10303 const AAMDNodes &AAInfo,
10304 bool IsCompressing) {
10305 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10306
10307 MMOFlags |= MachineMemOperand::MOStore;
10308 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10309
10310 if (PtrInfo.V.isNull())
10311 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10312
10314 MachineMemOperand *MMO = MF.getMachineMemOperand(
10315 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10316 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10317 IsCompressing);
10318}
10319
10321 SDValue Val, SDValue Ptr, SDValue Mask,
10322 SDValue EVL, EVT SVT,
10323 MachineMemOperand *MMO,
10324 bool IsCompressing) {
10325 EVT VT = Val.getValueType();
10326
10327 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10328 if (VT == SVT)
10329 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10330 EVL, VT, MMO, ISD::UNINDEXED,
10331 /*IsTruncating*/ false, IsCompressing);
10332
10334 "Should only be a truncating store, not extending!");
10335 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10336 assert(VT.isVector() == SVT.isVector() &&
10337 "Cannot use trunc store to convert to or from a vector!");
10338 assert((!VT.isVector() ||
10340 "Cannot use trunc store to change the number of vector elements!");
10341
10342 SDVTList VTs = getVTList(MVT::Other);
10343 SDValue Undef = getUNDEF(Ptr.getValueType());
10344 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10346 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10347 ID.AddInteger(SVT.getRawBits());
10348 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10349 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10350 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10351 ID.AddInteger(MMO->getFlags());
10352 void *IP = nullptr;
10353 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10354 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10355 return SDValue(E, 0);
10356 }
10357 auto *N =
10358 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10359 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10360 createOperands(N, Ops);
10361
10362 CSEMap.InsertNode(N, IP);
10363 InsertNode(N);
10364 SDValue V(N, 0);
10365 NewSDValueDbgMsg(V, "Creating new node: ", this);
10366 return V;
10367}
10368
10372 auto *ST = cast<VPStoreSDNode>(OrigStore);
10373 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10374 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10375 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10376 Offset, ST->getMask(), ST->getVectorLength()};
10378 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10379 ID.AddInteger(ST->getMemoryVT().getRawBits());
10380 ID.AddInteger(ST->getRawSubclassData());
10381 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10382 ID.AddInteger(ST->getMemOperand()->getFlags());
10383 void *IP = nullptr;
10384 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10385 return SDValue(E, 0);
10386
10387 auto *N = newSDNode<VPStoreSDNode>(
10388 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10389 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10390 createOperands(N, Ops);
10391
10392 CSEMap.InsertNode(N, IP);
10393 InsertNode(N);
10394 SDValue V(N, 0);
10395 NewSDValueDbgMsg(V, "Creating new node: ", this);
10396 return V;
10397}
10398
10400 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10401 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10402 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10403 bool Indexed = AM != ISD::UNINDEXED;
10404 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10405
10406 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10407 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10408 : getVTList(VT, MVT::Other);
10410 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10411 ID.AddInteger(VT.getRawBits());
10412 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10413 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10414 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10415
10416 void *IP = nullptr;
10417 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10418 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10419 return SDValue(E, 0);
10420 }
10421
10422 auto *N =
10423 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10424 ExtType, IsExpanding, MemVT, MMO);
10425 createOperands(N, Ops);
10426 CSEMap.InsertNode(N, IP);
10427 InsertNode(N);
10428 SDValue V(N, 0);
10429 NewSDValueDbgMsg(V, "Creating new node: ", this);
10430 return V;
10431}
10432
10434 SDValue Ptr, SDValue Stride,
10435 SDValue Mask, SDValue EVL,
10436 MachineMemOperand *MMO,
10437 bool IsExpanding) {
10438 SDValue Undef = getUNDEF(Ptr.getValueType());
10439 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10440 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10441}
10442
10444 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10445 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10446 MachineMemOperand *MMO, bool IsExpanding) {
10447 SDValue Undef = getUNDEF(Ptr.getValueType());
10448 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10449 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10450}
10451
10453 SDValue Val, SDValue Ptr,
10454 SDValue Offset, SDValue Stride,
10455 SDValue Mask, SDValue EVL, EVT MemVT,
10456 MachineMemOperand *MMO,
10458 bool IsTruncating, bool IsCompressing) {
10459 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10460 bool Indexed = AM != ISD::UNINDEXED;
10461 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10462 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10463 : getVTList(MVT::Other);
10464 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10466 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10467 ID.AddInteger(MemVT.getRawBits());
10468 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10469 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10470 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10471 void *IP = nullptr;
10472 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10473 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10474 return SDValue(E, 0);
10475 }
10476 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10477 VTs, AM, IsTruncating,
10478 IsCompressing, MemVT, MMO);
10479 createOperands(N, Ops);
10480
10481 CSEMap.InsertNode(N, IP);
10482 InsertNode(N);
10483 SDValue V(N, 0);
10484 NewSDValueDbgMsg(V, "Creating new node: ", this);
10485 return V;
10486}
10487
10489 SDValue Val, SDValue Ptr,
10490 SDValue Stride, SDValue Mask,
10491 SDValue EVL, EVT SVT,
10492 MachineMemOperand *MMO,
10493 bool IsCompressing) {
10494 EVT VT = Val.getValueType();
10495
10496 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10497 if (VT == SVT)
10498 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10499 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10500 /*IsTruncating*/ false, IsCompressing);
10501
10503 "Should only be a truncating store, not extending!");
10504 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10505 assert(VT.isVector() == SVT.isVector() &&
10506 "Cannot use trunc store to convert to or from a vector!");
10507 assert((!VT.isVector() ||
10509 "Cannot use trunc store to change the number of vector elements!");
10510
10511 SDVTList VTs = getVTList(MVT::Other);
10512 SDValue Undef = getUNDEF(Ptr.getValueType());
10513 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10515 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10516 ID.AddInteger(SVT.getRawBits());
10517 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10518 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10519 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10520 void *IP = nullptr;
10521 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10522 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10523 return SDValue(E, 0);
10524 }
10525 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10526 VTs, ISD::UNINDEXED, true,
10527 IsCompressing, SVT, MMO);
10528 createOperands(N, Ops);
10529
10530 CSEMap.InsertNode(N, IP);
10531 InsertNode(N);
10532 SDValue V(N, 0);
10533 NewSDValueDbgMsg(V, "Creating new node: ", this);
10534 return V;
10535}
10536
10539 ISD::MemIndexType IndexType) {
10540 assert(Ops.size() == 6 && "Incompatible number of operands");
10541
10543 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10544 ID.AddInteger(VT.getRawBits());
10545 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10546 dl.getIROrder(), VTs, VT, MMO, IndexType));
10547 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10548 ID.AddInteger(MMO->getFlags());
10549 void *IP = nullptr;
10550 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10551 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10552 return SDValue(E, 0);
10553 }
10554
10555 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10556 VT, MMO, IndexType);
10557 createOperands(N, Ops);
10558
10559 assert(N->getMask().getValueType().getVectorElementCount() ==
10560 N->getValueType(0).getVectorElementCount() &&
10561 "Vector width mismatch between mask and data");
10562 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10563 N->getValueType(0).getVectorElementCount().isScalable() &&
10564 "Scalable flags of index and data do not match");
10566 N->getIndex().getValueType().getVectorElementCount(),
10567 N->getValueType(0).getVectorElementCount()) &&
10568 "Vector width mismatch between index and data");
10569 assert(isa<ConstantSDNode>(N->getScale()) &&
10570 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10571 "Scale should be a constant power of 2");
10572
10573 CSEMap.InsertNode(N, IP);
10574 InsertNode(N);
10575 SDValue V(N, 0);
10576 NewSDValueDbgMsg(V, "Creating new node: ", this);
10577 return V;
10578}
10579
10582 MachineMemOperand *MMO,
10583 ISD::MemIndexType IndexType) {
10584 assert(Ops.size() == 7 && "Incompatible number of operands");
10585
10587 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10588 ID.AddInteger(VT.getRawBits());
10589 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10590 dl.getIROrder(), VTs, VT, MMO, IndexType));
10591 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10592 ID.AddInteger(MMO->getFlags());
10593 void *IP = nullptr;
10594 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10595 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10596 return SDValue(E, 0);
10597 }
10598 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10599 VT, MMO, IndexType);
10600 createOperands(N, Ops);
10601
10602 assert(N->getMask().getValueType().getVectorElementCount() ==
10603 N->getValue().getValueType().getVectorElementCount() &&
10604 "Vector width mismatch between mask and data");
10605 assert(
10606 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10607 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10608 "Scalable flags of index and data do not match");
10610 N->getIndex().getValueType().getVectorElementCount(),
10611 N->getValue().getValueType().getVectorElementCount()) &&
10612 "Vector width mismatch between index and data");
10613 assert(isa<ConstantSDNode>(N->getScale()) &&
10614 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10615 "Scale should be a constant power of 2");
10616
10617 CSEMap.InsertNode(N, IP);
10618 InsertNode(N);
10619 SDValue V(N, 0);
10620 NewSDValueDbgMsg(V, "Creating new node: ", this);
10621 return V;
10622}
10623
10626 SDValue PassThru, EVT MemVT,
10627 MachineMemOperand *MMO,
10629 ISD::LoadExtType ExtTy, bool isExpanding) {
10630 bool Indexed = AM != ISD::UNINDEXED;
10631 assert((Indexed || Offset.isUndef()) &&
10632 "Unindexed masked load with an offset!");
10633 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10634 : getVTList(VT, MVT::Other);
10635 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10638 ID.AddInteger(MemVT.getRawBits());
10639 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10640 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10641 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10642 ID.AddInteger(MMO->getFlags());
10643 void *IP = nullptr;
10644 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10645 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10646 return SDValue(E, 0);
10647 }
10648 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10649 AM, ExtTy, isExpanding, MemVT, MMO);
10650 createOperands(N, Ops);
10651
10652 CSEMap.InsertNode(N, IP);
10653 InsertNode(N);
10654 SDValue V(N, 0);
10655 NewSDValueDbgMsg(V, "Creating new node: ", this);
10656 return V;
10657}
10658
10663 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10664 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10665 Offset, LD->getMask(), LD->getPassThru(),
10666 LD->getMemoryVT(), LD->getMemOperand(), AM,
10667 LD->getExtensionType(), LD->isExpandingLoad());
10668}
10669
10672 SDValue Mask, EVT MemVT,
10673 MachineMemOperand *MMO,
10674 ISD::MemIndexedMode AM, bool IsTruncating,
10675 bool IsCompressing) {
10676 assert(Chain.getValueType() == MVT::Other &&
10677 "Invalid chain type");
10678 bool Indexed = AM != ISD::UNINDEXED;
10679 assert((Indexed || Offset.isUndef()) &&
10680 "Unindexed masked store with an offset!");
10681 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10682 : getVTList(MVT::Other);
10683 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10686 ID.AddInteger(MemVT.getRawBits());
10687 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10688 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10689 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10690 ID.AddInteger(MMO->getFlags());
10691 void *IP = nullptr;
10692 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10693 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10694 return SDValue(E, 0);
10695 }
10696 auto *N =
10697 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10698 IsTruncating, IsCompressing, MemVT, MMO);
10699 createOperands(N, Ops);
10700
10701 CSEMap.InsertNode(N, IP);
10702 InsertNode(N);
10703 SDValue V(N, 0);
10704 NewSDValueDbgMsg(V, "Creating new node: ", this);
10705 return V;
10706}
10707
10712 assert(ST->getOffset().isUndef() &&
10713 "Masked store is already a indexed store!");
10714 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10715 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10716 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10717}
10718
10721 MachineMemOperand *MMO,
10722 ISD::MemIndexType IndexType,
10723 ISD::LoadExtType ExtTy) {
10724 assert(Ops.size() == 6 && "Incompatible number of operands");
10725
10728 ID.AddInteger(MemVT.getRawBits());
10729 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10730 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10731 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10732 ID.AddInteger(MMO->getFlags());
10733 void *IP = nullptr;
10734 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10735 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10736 return SDValue(E, 0);
10737 }
10738
10739 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10740 VTs, MemVT, MMO, IndexType, ExtTy);
10741 createOperands(N, Ops);
10742
10743 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10744 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10745 assert(N->getMask().getValueType().getVectorElementCount() ==
10746 N->getValueType(0).getVectorElementCount() &&
10747 "Vector width mismatch between mask and data");
10748 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10749 N->getValueType(0).getVectorElementCount().isScalable() &&
10750 "Scalable flags of index and data do not match");
10752 N->getIndex().getValueType().getVectorElementCount(),
10753 N->getValueType(0).getVectorElementCount()) &&
10754 "Vector width mismatch between index and data");
10755 assert(isa<ConstantSDNode>(N->getScale()) &&
10756 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10757 "Scale should be a constant power of 2");
10758
10759 CSEMap.InsertNode(N, IP);
10760 InsertNode(N);
10761 SDValue V(N, 0);
10762 NewSDValueDbgMsg(V, "Creating new node: ", this);
10763 return V;
10764}
10765
10768 MachineMemOperand *MMO,
10769 ISD::MemIndexType IndexType,
10770 bool IsTrunc) {
10771 assert(Ops.size() == 6 && "Incompatible number of operands");
10772
10775 ID.AddInteger(MemVT.getRawBits());
10776 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10777 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10778 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10779 ID.AddInteger(MMO->getFlags());
10780 void *IP = nullptr;
10781 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10782 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10783 return SDValue(E, 0);
10784 }
10785
10786 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10787 VTs, MemVT, MMO, IndexType, IsTrunc);
10788 createOperands(N, Ops);
10789
10790 assert(N->getMask().getValueType().getVectorElementCount() ==
10791 N->getValue().getValueType().getVectorElementCount() &&
10792 "Vector width mismatch between mask and data");
10793 assert(
10794 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10795 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10796 "Scalable flags of index and data do not match");
10798 N->getIndex().getValueType().getVectorElementCount(),
10799 N->getValue().getValueType().getVectorElementCount()) &&
10800 "Vector width mismatch between index and data");
10801 assert(isa<ConstantSDNode>(N->getScale()) &&
10802 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10803 "Scale should be a constant power of 2");
10804
10805 CSEMap.InsertNode(N, IP);
10806 InsertNode(N);
10807 SDValue V(N, 0);
10808 NewSDValueDbgMsg(V, "Creating new node: ", this);
10809 return V;
10810}
10811
10813 const SDLoc &dl, ArrayRef<SDValue> Ops,
10814 MachineMemOperand *MMO,
10815 ISD::MemIndexType IndexType) {
10816 assert(Ops.size() == 7 && "Incompatible number of operands");
10817
10820 ID.AddInteger(MemVT.getRawBits());
10821 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10822 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10823 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10824 ID.AddInteger(MMO->getFlags());
10825 void *IP = nullptr;
10826 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10827 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10828 return SDValue(E, 0);
10829 }
10830
10831 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10832 VTs, MemVT, MMO, IndexType);
10833 createOperands(N, Ops);
10834
10835 assert(N->getMask().getValueType().getVectorElementCount() ==
10836 N->getIndex().getValueType().getVectorElementCount() &&
10837 "Vector width mismatch between mask and data");
10838 assert(isa<ConstantSDNode>(N->getScale()) &&
10839 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10840 "Scale should be a constant power of 2");
10841 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10842
10843 CSEMap.InsertNode(N, IP);
10844 InsertNode(N);
10845 SDValue V(N, 0);
10846 NewSDValueDbgMsg(V, "Creating new node: ", this);
10847 return V;
10848}
10849
10851 SDValue Ptr, SDValue Mask, SDValue EVL,
10852 MachineMemOperand *MMO) {
10853 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10854 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10856 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10857 ID.AddInteger(VT.getRawBits());
10858 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10859 VTs, VT, MMO));
10860 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10861 ID.AddInteger(MMO->getFlags());
10862 void *IP = nullptr;
10863 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10864 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10865 return SDValue(E, 0);
10866 }
10867 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10868 VT, MMO);
10869 createOperands(N, Ops);
10870
10871 CSEMap.InsertNode(N, IP);
10872 InsertNode(N);
10873 SDValue V(N, 0);
10874 NewSDValueDbgMsg(V, "Creating new node: ", this);
10875 return V;
10876}
10877
10879 EVT MemVT, MachineMemOperand *MMO) {
10880 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10881 SDVTList VTs = getVTList(MVT::Other);
10882 SDValue Ops[] = {Chain, Ptr};
10885 ID.AddInteger(MemVT.getRawBits());
10886 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10887 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10888 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10889 ID.AddInteger(MMO->getFlags());
10890 void *IP = nullptr;
10891 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10892 return SDValue(E, 0);
10893
10894 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10895 dl.getDebugLoc(), VTs, MemVT, MMO);
10896 createOperands(N, Ops);
10897
10898 CSEMap.InsertNode(N, IP);
10899 InsertNode(N);
10900 SDValue V(N, 0);
10901 NewSDValueDbgMsg(V, "Creating new node: ", this);
10902 return V;
10903}
10904
10906 EVT MemVT, MachineMemOperand *MMO) {
10907 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10908 SDVTList VTs = getVTList(MVT::Other);
10909 SDValue Ops[] = {Chain, Ptr};
10912 ID.AddInteger(MemVT.getRawBits());
10913 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10914 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10915 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10916 ID.AddInteger(MMO->getFlags());
10917 void *IP = nullptr;
10918 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10919 return SDValue(E, 0);
10920
10921 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10922 dl.getDebugLoc(), VTs, MemVT, MMO);
10923 createOperands(N, Ops);
10924
10925 CSEMap.InsertNode(N, IP);
10926 InsertNode(N);
10927 SDValue V(N, 0);
10928 NewSDValueDbgMsg(V, "Creating new node: ", this);
10929 return V;
10930}
10931
10933 // select undef, T, F --> T (if T is a constant), otherwise F
10934 // select, ?, undef, F --> F
10935 // select, ?, T, undef --> T
10936 if (Cond.isUndef())
10937 return isConstantValueOfAnyType(T) ? T : F;
10938 if (T.isUndef())
10940 if (F.isUndef())
10942
10943 // select true, T, F --> T
10944 // select false, T, F --> F
10945 if (auto C = isBoolConstant(Cond))
10946 return *C ? T : F;
10947
10948 // select ?, T, T --> T
10949 if (T == F)
10950 return T;
10951
10952 return SDValue();
10953}
10954
10956 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10957 if (X.isUndef())
10958 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10959 // shift X, undef --> undef (because it may shift by the bitwidth)
10960 if (Y.isUndef())
10961 return getUNDEF(X.getValueType());
10962
10963 // shift 0, Y --> 0
10964 // shift X, 0 --> X
10966 return X;
10967
10968 // shift X, C >= bitwidth(X) --> undef
10969 // All vector elements must be too big (or undef) to avoid partial undefs.
10970 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10971 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10972 };
10973 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10974 return getUNDEF(X.getValueType());
10975
10976 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10977 if (X.getValueType().getScalarType() == MVT::i1)
10978 return X;
10979
10980 return SDValue();
10981}
10982
10984 SDNodeFlags Flags) {
10985 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10986 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10987 // operation is poison. That result can be relaxed to undef.
10988 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10989 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10990 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10991 (YC && YC->getValueAPF().isNaN());
10992 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10993 (YC && YC->getValueAPF().isInfinity());
10994
10995 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10996 return getUNDEF(X.getValueType());
10997
10998 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10999 return getUNDEF(X.getValueType());
11000
11001 if (!YC)
11002 return SDValue();
11003
11004 // X + -0.0 --> X
11005 if (Opcode == ISD::FADD)
11006 if (YC->getValueAPF().isNegZero())
11007 return X;
11008
11009 // X - +0.0 --> X
11010 if (Opcode == ISD::FSUB)
11011 if (YC->getValueAPF().isPosZero())
11012 return X;
11013
11014 // X * 1.0 --> X
11015 // X / 1.0 --> X
11016 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
11017 if (YC->getValueAPF().isExactlyValue(1.0))
11018 return X;
11019
11020 // X * 0.0 --> 0.0
11021 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
11022 if (YC->getValueAPF().isZero())
11023 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
11024
11025 return SDValue();
11026}
11027
11029 SDValue Ptr, SDValue SV, unsigned Align) {
11030 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
11031 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
11032}
11033
11034SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11036 switch (Ops.size()) {
11037 case 0: return getNode(Opcode, DL, VT);
11038 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
11039 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
11040 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
11041 default: break;
11042 }
11043
11044 // Copy from an SDUse array into an SDValue array for use with
11045 // the regular getNode logic.
11047 return getNode(Opcode, DL, VT, NewOps);
11048}
11049
11050SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11052 SDNodeFlags Flags;
11053 if (Inserter)
11054 Flags = Inserter->getFlags();
11055 return getNode(Opcode, DL, VT, Ops, Flags);
11056}
11057
11058SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11059 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11060 unsigned NumOps = Ops.size();
11061 switch (NumOps) {
11062 case 0: return getNode(Opcode, DL, VT);
11063 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
11064 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
11065 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
11066 default: break;
11067 }
11068
11069#ifndef NDEBUG
11070 for (const auto &Op : Ops)
11071 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11072 "Operand is DELETED_NODE!");
11073#endif
11074
11075 switch (Opcode) {
11076 default: break;
11077 case ISD::BUILD_VECTOR:
11078 // Attempt to simplify BUILD_VECTOR.
11079 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
11080 return V;
11081 break;
11083 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
11084 return V;
11085 break;
11086 case ISD::SELECT_CC:
11087 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
11088 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
11089 "LHS and RHS of condition must have same type!");
11090 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11091 "True and False arms of SelectCC must have same type!");
11092 assert(Ops[2].getValueType() == VT &&
11093 "select_cc node must be of same type as true and false value!");
11094 assert((!Ops[0].getValueType().isVector() ||
11095 Ops[0].getValueType().getVectorElementCount() ==
11096 VT.getVectorElementCount()) &&
11097 "Expected select_cc with vector result to have the same sized "
11098 "comparison type!");
11099 break;
11100 case ISD::BR_CC:
11101 assert(NumOps == 5 && "BR_CC takes 5 operands!");
11102 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11103 "LHS/RHS of comparison should match types!");
11104 break;
11105 case ISD::VP_ADD:
11106 case ISD::VP_SUB:
11107 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
11108 if (VT.getScalarType() == MVT::i1)
11109 Opcode = ISD::VP_XOR;
11110 break;
11111 case ISD::VP_MUL:
11112 // If it is VP_MUL mask operation then turn it to VP_AND
11113 if (VT.getScalarType() == MVT::i1)
11114 Opcode = ISD::VP_AND;
11115 break;
11116 case ISD::VP_REDUCE_MUL:
11117 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11118 if (VT == MVT::i1)
11119 Opcode = ISD::VP_REDUCE_AND;
11120 break;
11121 case ISD::VP_REDUCE_ADD:
11122 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11123 if (VT == MVT::i1)
11124 Opcode = ISD::VP_REDUCE_XOR;
11125 break;
11126 case ISD::VP_REDUCE_SMAX:
11127 case ISD::VP_REDUCE_UMIN:
11128 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11129 // VP_REDUCE_AND.
11130 if (VT == MVT::i1)
11131 Opcode = ISD::VP_REDUCE_AND;
11132 break;
11133 case ISD::VP_REDUCE_SMIN:
11134 case ISD::VP_REDUCE_UMAX:
11135 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11136 // VP_REDUCE_OR.
11137 if (VT == MVT::i1)
11138 Opcode = ISD::VP_REDUCE_OR;
11139 break;
11140 }
11141
11142 // Memoize nodes.
11143 SDNode *N;
11144 SDVTList VTs = getVTList(VT);
11145
11146 if (VT != MVT::Glue) {
11148 AddNodeIDNode(ID, Opcode, VTs, Ops);
11149 void *IP = nullptr;
11150
11151 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11152 E->intersectFlagsWith(Flags);
11153 return SDValue(E, 0);
11154 }
11155
11156 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11157 createOperands(N, Ops);
11158
11159 CSEMap.InsertNode(N, IP);
11160 } else {
11161 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11162 createOperands(N, Ops);
11163 }
11164
11165 N->setFlags(Flags);
11166 InsertNode(N);
11167 SDValue V(N, 0);
11168 NewSDValueDbgMsg(V, "Creating new node: ", this);
11169 return V;
11170}
11171
11172SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11173 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11174 SDNodeFlags Flags;
11175 if (Inserter)
11176 Flags = Inserter->getFlags();
11177 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11178}
11179
11180SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11182 const SDNodeFlags Flags) {
11183 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11184}
11185
11186SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11188 SDNodeFlags Flags;
11189 if (Inserter)
11190 Flags = Inserter->getFlags();
11191 return getNode(Opcode, DL, VTList, Ops, Flags);
11192}
11193
11194SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11195 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11196 if (VTList.NumVTs == 1)
11197 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11198
11199#ifndef NDEBUG
11200 for (const auto &Op : Ops)
11201 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11202 "Operand is DELETED_NODE!");
11203#endif
11204
11205 switch (Opcode) {
11206 case ISD::SADDO:
11207 case ISD::UADDO:
11208 case ISD::SSUBO:
11209 case ISD::USUBO: {
11210 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11211 "Invalid add/sub overflow op!");
11212 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11213 Ops[0].getValueType() == Ops[1].getValueType() &&
11214 Ops[0].getValueType() == VTList.VTs[0] &&
11215 "Binary operator types must match!");
11216 SDValue N1 = Ops[0], N2 = Ops[1];
11217 canonicalizeCommutativeBinop(Opcode, N1, N2);
11218
11219 // (X +- 0) -> X with zero-overflow.
11220 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11221 /*AllowTruncation*/ true);
11222 if (N2CV && N2CV->isZero()) {
11223 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11224 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11225 }
11226
11227 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11228 VTList.VTs[1].getScalarType() == MVT::i1) {
11229 SDValue F1 = getFreeze(N1);
11230 SDValue F2 = getFreeze(N2);
11231 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11232 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11233 return getNode(ISD::MERGE_VALUES, DL, VTList,
11234 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11235 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11236 Flags);
11237 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11238 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11239 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11240 return getNode(ISD::MERGE_VALUES, DL, VTList,
11241 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11242 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11243 Flags);
11244 }
11245 }
11246 break;
11247 }
11248 case ISD::SADDO_CARRY:
11249 case ISD::UADDO_CARRY:
11250 case ISD::SSUBO_CARRY:
11251 case ISD::USUBO_CARRY:
11252 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11253 "Invalid add/sub overflow op!");
11254 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11255 Ops[0].getValueType() == Ops[1].getValueType() &&
11256 Ops[0].getValueType() == VTList.VTs[0] &&
11257 Ops[2].getValueType() == VTList.VTs[1] &&
11258 "Binary operator types must match!");
11259 break;
11260 case ISD::SMUL_LOHI:
11261 case ISD::UMUL_LOHI: {
11262 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11263 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11264 VTList.VTs[0] == Ops[0].getValueType() &&
11265 VTList.VTs[0] == Ops[1].getValueType() &&
11266 "Binary operator types must match!");
11267 // Constant fold.
11270 if (LHS && RHS) {
11271 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11272 unsigned OutWidth = Width * 2;
11273 APInt Val = LHS->getAPIntValue();
11274 APInt Mul = RHS->getAPIntValue();
11275 if (Opcode == ISD::SMUL_LOHI) {
11276 Val = Val.sext(OutWidth);
11277 Mul = Mul.sext(OutWidth);
11278 } else {
11279 Val = Val.zext(OutWidth);
11280 Mul = Mul.zext(OutWidth);
11281 }
11282 Val *= Mul;
11283
11284 SDValue Hi =
11285 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11286 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11287 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11288 }
11289 break;
11290 }
11291 case ISD::FFREXP: {
11292 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11293 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11294 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11295
11297 int FrexpExp;
11298 APFloat FrexpMant =
11299 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11300 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11301 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11302 DL, VTList.VTs[1]);
11303 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11304 }
11305
11306 break;
11307 }
11309 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11310 "Invalid STRICT_FP_EXTEND!");
11311 assert(VTList.VTs[0].isFloatingPoint() &&
11312 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11313 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11314 "STRICT_FP_EXTEND result type should be vector iff the operand "
11315 "type is vector!");
11316 assert((!VTList.VTs[0].isVector() ||
11317 VTList.VTs[0].getVectorElementCount() ==
11318 Ops[1].getValueType().getVectorElementCount()) &&
11319 "Vector element count mismatch!");
11320 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11321 "Invalid fpext node, dst <= src!");
11322 break;
11324 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11325 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11326 "STRICT_FP_ROUND result type should be vector iff the operand "
11327 "type is vector!");
11328 assert((!VTList.VTs[0].isVector() ||
11329 VTList.VTs[0].getVectorElementCount() ==
11330 Ops[1].getValueType().getVectorElementCount()) &&
11331 "Vector element count mismatch!");
11332 assert(VTList.VTs[0].isFloatingPoint() &&
11333 Ops[1].getValueType().isFloatingPoint() &&
11334 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11335 Ops[2].getOpcode() == ISD::TargetConstant &&
11336 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11337 "Invalid STRICT_FP_ROUND!");
11338 break;
11339 }
11340
11341 // Memoize the node unless it returns a glue result.
11342 SDNode *N;
11343 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11345 AddNodeIDNode(ID, Opcode, VTList, Ops);
11346 void *IP = nullptr;
11347 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11348 E->intersectFlagsWith(Flags);
11349 return SDValue(E, 0);
11350 }
11351
11352 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11353 createOperands(N, Ops);
11354 CSEMap.InsertNode(N, IP);
11355 } else {
11356 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11357 createOperands(N, Ops);
11358 }
11359
11360 N->setFlags(Flags);
11361 InsertNode(N);
11362 SDValue V(N, 0);
11363 NewSDValueDbgMsg(V, "Creating new node: ", this);
11364 return V;
11365}
11366
11367SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11368 SDVTList VTList) {
11369 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11370}
11371
11372SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11373 SDValue N1) {
11374 SDValue Ops[] = { N1 };
11375 return getNode(Opcode, DL, VTList, Ops);
11376}
11377
11378SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11379 SDValue N1, SDValue N2) {
11380 SDValue Ops[] = { N1, N2 };
11381 return getNode(Opcode, DL, VTList, Ops);
11382}
11383
11384SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11385 SDValue N1, SDValue N2, SDValue N3) {
11386 SDValue Ops[] = { N1, N2, N3 };
11387 return getNode(Opcode, DL, VTList, Ops);
11388}
11389
11390SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11391 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11392 SDValue Ops[] = { N1, N2, N3, N4 };
11393 return getNode(Opcode, DL, VTList, Ops);
11394}
11395
11396SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11397 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11398 SDValue N5) {
11399 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11400 return getNode(Opcode, DL, VTList, Ops);
11401}
11402
11404 if (!VT.isExtended())
11405 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11406
11407 return makeVTList(&(*EVTs.insert(VT).first), 1);
11408}
11409
11412 ID.AddInteger(2U);
11413 ID.AddInteger(VT1.getRawBits());
11414 ID.AddInteger(VT2.getRawBits());
11415
11416 void *IP = nullptr;
11417 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11418 if (!Result) {
11419 EVT *Array = Allocator.Allocate<EVT>(2);
11420 Array[0] = VT1;
11421 Array[1] = VT2;
11422 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11423 VTListMap.InsertNode(Result, IP);
11424 }
11425 return Result->getSDVTList();
11426}
11427
11430 ID.AddInteger(3U);
11431 ID.AddInteger(VT1.getRawBits());
11432 ID.AddInteger(VT2.getRawBits());
11433 ID.AddInteger(VT3.getRawBits());
11434
11435 void *IP = nullptr;
11436 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11437 if (!Result) {
11438 EVT *Array = Allocator.Allocate<EVT>(3);
11439 Array[0] = VT1;
11440 Array[1] = VT2;
11441 Array[2] = VT3;
11442 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11443 VTListMap.InsertNode(Result, IP);
11444 }
11445 return Result->getSDVTList();
11446}
11447
11450 ID.AddInteger(4U);
11451 ID.AddInteger(VT1.getRawBits());
11452 ID.AddInteger(VT2.getRawBits());
11453 ID.AddInteger(VT3.getRawBits());
11454 ID.AddInteger(VT4.getRawBits());
11455
11456 void *IP = nullptr;
11457 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11458 if (!Result) {
11459 EVT *Array = Allocator.Allocate<EVT>(4);
11460 Array[0] = VT1;
11461 Array[1] = VT2;
11462 Array[2] = VT3;
11463 Array[3] = VT4;
11464 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11465 VTListMap.InsertNode(Result, IP);
11466 }
11467 return Result->getSDVTList();
11468}
11469
11471 unsigned NumVTs = VTs.size();
11473 ID.AddInteger(NumVTs);
11474 for (unsigned index = 0; index < NumVTs; index++) {
11475 ID.AddInteger(VTs[index].getRawBits());
11476 }
11477
11478 void *IP = nullptr;
11479 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11480 if (!Result) {
11481 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11482 llvm::copy(VTs, Array);
11483 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11484 VTListMap.InsertNode(Result, IP);
11485 }
11486 return Result->getSDVTList();
11487}
11488
11489
11490/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11491/// specified operands. If the resultant node already exists in the DAG,
11492/// this does not modify the specified node, instead it returns the node that
11493/// already exists. If the resultant node does not exist in the DAG, the
11494/// input node is returned. As a degenerate case, if you specify the same
11495/// input operands as the node already has, the input node is returned.
11497 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11498
11499 // Check to see if there is no change.
11500 if (Op == N->getOperand(0)) return N;
11501
11502 // See if the modified node already exists.
11503 void *InsertPos = nullptr;
11504 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11505 return Existing;
11506
11507 // Nope it doesn't. Remove the node from its current place in the maps.
11508 if (InsertPos)
11509 if (!RemoveNodeFromCSEMaps(N))
11510 InsertPos = nullptr;
11511
11512 // Now we update the operands.
11513 N->OperandList[0].set(Op);
11514
11516 // If this gets put into a CSE map, add it.
11517 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11518 return N;
11519}
11520
11522 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11523
11524 // Check to see if there is no change.
11525 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11526 return N; // No operands changed, just return the input node.
11527
11528 // See if the modified node already exists.
11529 void *InsertPos = nullptr;
11530 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11531 return Existing;
11532
11533 // Nope it doesn't. Remove the node from its current place in the maps.
11534 if (InsertPos)
11535 if (!RemoveNodeFromCSEMaps(N))
11536 InsertPos = nullptr;
11537
11538 // Now we update the operands.
11539 if (N->OperandList[0] != Op1)
11540 N->OperandList[0].set(Op1);
11541 if (N->OperandList[1] != Op2)
11542 N->OperandList[1].set(Op2);
11543
11545 // If this gets put into a CSE map, add it.
11546 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11547 return N;
11548}
11549
11552 SDValue Ops[] = { Op1, Op2, Op3 };
11553 return UpdateNodeOperands(N, Ops);
11554}
11555
11558 SDValue Op3, SDValue Op4) {
11559 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11560 return UpdateNodeOperands(N, Ops);
11561}
11562
11565 SDValue Op3, SDValue Op4, SDValue Op5) {
11566 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11567 return UpdateNodeOperands(N, Ops);
11568}
11569
11572 unsigned NumOps = Ops.size();
11573 assert(N->getNumOperands() == NumOps &&
11574 "Update with wrong number of operands");
11575
11576 // If no operands changed just return the input node.
11577 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11578 return N;
11579
11580 // See if the modified node already exists.
11581 void *InsertPos = nullptr;
11582 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11583 return Existing;
11584
11585 // Nope it doesn't. Remove the node from its current place in the maps.
11586 if (InsertPos)
11587 if (!RemoveNodeFromCSEMaps(N))
11588 InsertPos = nullptr;
11589
11590 // Now we update the operands.
11591 for (unsigned i = 0; i != NumOps; ++i)
11592 if (N->OperandList[i] != Ops[i])
11593 N->OperandList[i].set(Ops[i]);
11594
11596 // If this gets put into a CSE map, add it.
11597 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11598 return N;
11599}
11600
11601/// DropOperands - Release the operands and set this node to have
11602/// zero operands.
11604 // Unlike the code in MorphNodeTo that does this, we don't need to
11605 // watch for dead nodes here.
11606 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11607 SDUse &Use = *I++;
11608 Use.set(SDValue());
11609 }
11610}
11611
11613 ArrayRef<MachineMemOperand *> NewMemRefs) {
11614 if (NewMemRefs.empty()) {
11615 N->clearMemRefs();
11616 return;
11617 }
11618
11619 // Check if we can avoid allocating by storing a single reference directly.
11620 if (NewMemRefs.size() == 1) {
11621 N->MemRefs = NewMemRefs[0];
11622 N->NumMemRefs = 1;
11623 return;
11624 }
11625
11626 MachineMemOperand **MemRefsBuffer =
11627 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11628 llvm::copy(NewMemRefs, MemRefsBuffer);
11629 N->MemRefs = MemRefsBuffer;
11630 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11631}
11632
11633/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11634/// machine opcode.
11635///
11637 EVT VT) {
11638 SDVTList VTs = getVTList(VT);
11639 return SelectNodeTo(N, MachineOpc, VTs, {});
11640}
11641
11643 EVT VT, SDValue Op1) {
11644 SDVTList VTs = getVTList(VT);
11645 SDValue Ops[] = { Op1 };
11646 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11647}
11648
11650 EVT VT, SDValue Op1,
11651 SDValue Op2) {
11652 SDVTList VTs = getVTList(VT);
11653 SDValue Ops[] = { Op1, Op2 };
11654 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11655}
11656
11658 EVT VT, SDValue Op1,
11659 SDValue Op2, SDValue Op3) {
11660 SDVTList VTs = getVTList(VT);
11661 SDValue Ops[] = { Op1, Op2, Op3 };
11662 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11663}
11664
11667 SDVTList VTs = getVTList(VT);
11668 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11669}
11670
11672 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11673 SDVTList VTs = getVTList(VT1, VT2);
11674 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11675}
11676
11678 EVT VT1, EVT VT2) {
11679 SDVTList VTs = getVTList(VT1, VT2);
11680 return SelectNodeTo(N, MachineOpc, VTs, {});
11681}
11682
11684 EVT VT1, EVT VT2, EVT VT3,
11686 SDVTList VTs = getVTList(VT1, VT2, VT3);
11687 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11688}
11689
11691 EVT VT1, EVT VT2,
11692 SDValue Op1, SDValue Op2) {
11693 SDVTList VTs = getVTList(VT1, VT2);
11694 SDValue Ops[] = { Op1, Op2 };
11695 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11696}
11697
11700 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11701 // Reset the NodeID to -1.
11702 New->setNodeId(-1);
11703 if (New != N) {
11704 ReplaceAllUsesWith(N, New);
11706 }
11707 return New;
11708}
11709
11710/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11711/// the line number information on the merged node since it is not possible to
11712/// preserve the information that operation is associated with multiple lines.
11713/// This will make the debugger working better at -O0, were there is a higher
11714/// probability having other instructions associated with that line.
11715///
11716/// For IROrder, we keep the smaller of the two
11717SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11718 DebugLoc NLoc = N->getDebugLoc();
11719 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11720 N->setDebugLoc(DebugLoc());
11721 }
11722 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11723 N->setIROrder(Order);
11724 return N;
11725}
11726
11727/// MorphNodeTo - This *mutates* the specified node to have the specified
11728/// return type, opcode, and operands.
11729///
11730/// Note that MorphNodeTo returns the resultant node. If there is already a
11731/// node of the specified opcode and operands, it returns that node instead of
11732/// the current one. Note that the SDLoc need not be the same.
11733///
11734/// Using MorphNodeTo is faster than creating a new node and swapping it in
11735/// with ReplaceAllUsesWith both because it often avoids allocating a new
11736/// node, and because it doesn't require CSE recalculation for any of
11737/// the node's users.
11738///
11739/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11740/// As a consequence it isn't appropriate to use from within the DAG combiner or
11741/// the legalizer which maintain worklists that would need to be updated when
11742/// deleting things.
11745 // If an identical node already exists, use it.
11746 void *IP = nullptr;
11747 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11749 AddNodeIDNode(ID, Opc, VTs, Ops);
11750 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11751 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11752 }
11753
11754 if (!RemoveNodeFromCSEMaps(N))
11755 IP = nullptr;
11756
11757 // Start the morphing.
11758 N->NodeType = Opc;
11759 N->ValueList = VTs.VTs;
11760 N->NumValues = VTs.NumVTs;
11761
11762 // Clear the operands list, updating used nodes to remove this from their
11763 // use list. Keep track of any operands that become dead as a result.
11764 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11765 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11766 SDUse &Use = *I++;
11767 SDNode *Used = Use.getNode();
11768 Use.set(SDValue());
11769 if (Used->use_empty())
11770 DeadNodeSet.insert(Used);
11771 }
11772
11773 // For MachineNode, initialize the memory references information.
11775 MN->clearMemRefs();
11776
11777 // Swap for an appropriately sized array from the recycler.
11778 removeOperands(N);
11779 createOperands(N, Ops);
11780
11781 // Delete any nodes that are still dead after adding the uses for the
11782 // new operands.
11783 if (!DeadNodeSet.empty()) {
11784 SmallVector<SDNode *, 16> DeadNodes;
11785 for (SDNode *N : DeadNodeSet)
11786 if (N->use_empty())
11787 DeadNodes.push_back(N);
11788 RemoveDeadNodes(DeadNodes);
11789 }
11790
11791 if (IP)
11792 CSEMap.InsertNode(N, IP); // Memoize the new node.
11793 return N;
11794}
11795
11797 unsigned OrigOpc = Node->getOpcode();
11798 unsigned NewOpc;
11799 switch (OrigOpc) {
11800 default:
11801 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11802#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11803 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11804#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11805 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11806#include "llvm/IR/ConstrainedOps.def"
11807 }
11808
11809 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11810
11811 // We're taking this node out of the chain, so we need to re-link things.
11812 SDValue InputChain = Node->getOperand(0);
11813 SDValue OutputChain = SDValue(Node, 1);
11814 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11815
11817 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11818 Ops.push_back(Node->getOperand(i));
11819
11820 SDVTList VTs = getVTList(Node->getValueType(0));
11821 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11822
11823 // MorphNodeTo can operate in two ways: if an existing node with the
11824 // specified operands exists, it can just return it. Otherwise, it
11825 // updates the node in place to have the requested operands.
11826 if (Res == Node) {
11827 // If we updated the node in place, reset the node ID. To the isel,
11828 // this should be just like a newly allocated machine node.
11829 Res->setNodeId(-1);
11830 } else {
11833 }
11834
11835 return Res;
11836}
11837
11838/// getMachineNode - These are used for target selectors to create a new node
11839/// with specified return type(s), MachineInstr opcode, and operands.
11840///
11841/// Note that getMachineNode returns the resultant node. If there is already a
11842/// node of the specified opcode and operands, it returns that node instead of
11843/// the current one.
11845 EVT VT) {
11846 SDVTList VTs = getVTList(VT);
11847 return getMachineNode(Opcode, dl, VTs, {});
11848}
11849
11851 EVT VT, SDValue Op1) {
11852 SDVTList VTs = getVTList(VT);
11853 SDValue Ops[] = { Op1 };
11854 return getMachineNode(Opcode, dl, VTs, Ops);
11855}
11856
11858 EVT VT, SDValue Op1, SDValue Op2) {
11859 SDVTList VTs = getVTList(VT);
11860 SDValue Ops[] = { Op1, Op2 };
11861 return getMachineNode(Opcode, dl, VTs, Ops);
11862}
11863
11865 EVT VT, SDValue Op1, SDValue Op2,
11866 SDValue Op3) {
11867 SDVTList VTs = getVTList(VT);
11868 SDValue Ops[] = { Op1, Op2, Op3 };
11869 return getMachineNode(Opcode, dl, VTs, Ops);
11870}
11871
11874 SDVTList VTs = getVTList(VT);
11875 return getMachineNode(Opcode, dl, VTs, Ops);
11876}
11877
11879 EVT VT1, EVT VT2, SDValue Op1,
11880 SDValue Op2) {
11881 SDVTList VTs = getVTList(VT1, VT2);
11882 SDValue Ops[] = { Op1, Op2 };
11883 return getMachineNode(Opcode, dl, VTs, Ops);
11884}
11885
11887 EVT VT1, EVT VT2, SDValue Op1,
11888 SDValue Op2, SDValue Op3) {
11889 SDVTList VTs = getVTList(VT1, VT2);
11890 SDValue Ops[] = { Op1, Op2, Op3 };
11891 return getMachineNode(Opcode, dl, VTs, Ops);
11892}
11893
11895 EVT VT1, EVT VT2,
11897 SDVTList VTs = getVTList(VT1, VT2);
11898 return getMachineNode(Opcode, dl, VTs, Ops);
11899}
11900
11902 EVT VT1, EVT VT2, EVT VT3,
11903 SDValue Op1, SDValue Op2) {
11904 SDVTList VTs = getVTList(VT1, VT2, VT3);
11905 SDValue Ops[] = { Op1, Op2 };
11906 return getMachineNode(Opcode, dl, VTs, Ops);
11907}
11908
11910 EVT VT1, EVT VT2, EVT VT3,
11911 SDValue Op1, SDValue Op2,
11912 SDValue Op3) {
11913 SDVTList VTs = getVTList(VT1, VT2, VT3);
11914 SDValue Ops[] = { Op1, Op2, Op3 };
11915 return getMachineNode(Opcode, dl, VTs, Ops);
11916}
11917
11919 EVT VT1, EVT VT2, EVT VT3,
11921 SDVTList VTs = getVTList(VT1, VT2, VT3);
11922 return getMachineNode(Opcode, dl, VTs, Ops);
11923}
11924
11926 ArrayRef<EVT> ResultTys,
11928 SDVTList VTs = getVTList(ResultTys);
11929 return getMachineNode(Opcode, dl, VTs, Ops);
11930}
11931
11933 SDVTList VTs,
11935 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11937 void *IP = nullptr;
11938
11939 if (DoCSE) {
11941 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11942 IP = nullptr;
11943 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11944 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11945 }
11946 }
11947
11948 // Allocate a new MachineSDNode.
11949 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11950 createOperands(N, Ops);
11951
11952 if (DoCSE)
11953 CSEMap.InsertNode(N, IP);
11954
11955 InsertNode(N);
11956 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11957 return N;
11958}
11959
11960/// getTargetExtractSubreg - A convenience function for creating
11961/// TargetOpcode::EXTRACT_SUBREG nodes.
11963 SDValue Operand) {
11964 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11965 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11966 VT, Operand, SRIdxVal);
11967 return SDValue(Subreg, 0);
11968}
11969
11970/// getTargetInsertSubreg - A convenience function for creating
11971/// TargetOpcode::INSERT_SUBREG nodes.
11973 SDValue Operand, SDValue Subreg) {
11974 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11975 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11976 VT, Operand, Subreg, SRIdxVal);
11977 return SDValue(Result, 0);
11978}
11979
11980/// getNodeIfExists - Get the specified node if it's already available, or
11981/// else return NULL.
11984 bool AllowCommute) {
11985 SDNodeFlags Flags;
11986 if (Inserter)
11987 Flags = Inserter->getFlags();
11988 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
11989}
11990
11993 const SDNodeFlags Flags,
11994 bool AllowCommute) {
11995 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
11996 return nullptr;
11997
11998 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
12000 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
12001 void *IP = nullptr;
12002 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
12003 E->intersectFlagsWith(Flags);
12004 return E;
12005 }
12006 return nullptr;
12007 };
12008
12009 if (SDNode *Existing = Lookup(Ops))
12010 return Existing;
12011
12012 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
12013 return Lookup({Ops[1], Ops[0]});
12014
12015 return nullptr;
12016}
12017
12018/// doesNodeExist - Check if a node exists without modifying its flags.
12019bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
12021 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
12023 AddNodeIDNode(ID, Opcode, VTList, Ops);
12024 void *IP = nullptr;
12025 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
12026 return true;
12027 }
12028 return false;
12029}
12030
12031/// getDbgValue - Creates a SDDbgValue node.
12032///
12033/// SDNode
12035 SDNode *N, unsigned R, bool IsIndirect,
12036 const DebugLoc &DL, unsigned O) {
12037 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12038 "Expected inlined-at fields to agree");
12039 return new (DbgInfo->getAlloc())
12040 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
12041 {}, IsIndirect, DL, O,
12042 /*IsVariadic=*/false);
12043}
12044
12045/// Constant
12047 DIExpression *Expr,
12048 const Value *C,
12049 const DebugLoc &DL, unsigned O) {
12050 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12051 "Expected inlined-at fields to agree");
12052 return new (DbgInfo->getAlloc())
12053 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
12054 /*IsIndirect=*/false, DL, O,
12055 /*IsVariadic=*/false);
12056}
12057
12058/// FrameIndex
12060 DIExpression *Expr, unsigned FI,
12061 bool IsIndirect,
12062 const DebugLoc &DL,
12063 unsigned O) {
12064 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12065 "Expected inlined-at fields to agree");
12066 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
12067}
12068
12069/// FrameIndex with dependencies
12071 DIExpression *Expr, unsigned FI,
12072 ArrayRef<SDNode *> Dependencies,
12073 bool IsIndirect,
12074 const DebugLoc &DL,
12075 unsigned O) {
12076 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12077 "Expected inlined-at fields to agree");
12078 return new (DbgInfo->getAlloc())
12079 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
12080 Dependencies, IsIndirect, DL, O,
12081 /*IsVariadic=*/false);
12082}
12083
12084/// VReg
12086 Register VReg, bool IsIndirect,
12087 const DebugLoc &DL, unsigned O) {
12088 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12089 "Expected inlined-at fields to agree");
12090 return new (DbgInfo->getAlloc())
12091 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
12092 {}, IsIndirect, DL, O,
12093 /*IsVariadic=*/false);
12094}
12095
12098 ArrayRef<SDNode *> Dependencies,
12099 bool IsIndirect, const DebugLoc &DL,
12100 unsigned O, bool IsVariadic) {
12101 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12102 "Expected inlined-at fields to agree");
12103 return new (DbgInfo->getAlloc())
12104 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
12105 DL, O, IsVariadic);
12106}
12107
12109 unsigned OffsetInBits, unsigned SizeInBits,
12110 bool InvalidateDbg) {
12111 SDNode *FromNode = From.getNode();
12112 SDNode *ToNode = To.getNode();
12113 assert(FromNode && ToNode && "Can't modify dbg values");
12114
12115 // PR35338
12116 // TODO: assert(From != To && "Redundant dbg value transfer");
12117 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12118 if (From == To || FromNode == ToNode)
12119 return;
12120
12121 if (!FromNode->getHasDebugValue())
12122 return;
12123
12124 SDDbgOperand FromLocOp =
12125 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12127
12129 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12130 if (Dbg->isInvalidated())
12131 continue;
12132
12133 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12134
12135 // Create a new location ops vector that is equal to the old vector, but
12136 // with each instance of FromLocOp replaced with ToLocOp.
12137 bool Changed = false;
12138 auto NewLocOps = Dbg->copyLocationOps();
12139 std::replace_if(
12140 NewLocOps.begin(), NewLocOps.end(),
12141 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12142 bool Match = Op == FromLocOp;
12143 Changed |= Match;
12144 return Match;
12145 },
12146 ToLocOp);
12147 // Ignore this SDDbgValue if we didn't find a matching location.
12148 if (!Changed)
12149 continue;
12150
12151 DIVariable *Var = Dbg->getVariable();
12152 auto *Expr = Dbg->getExpression();
12153 // If a fragment is requested, update the expression.
12154 if (SizeInBits) {
12155 // When splitting a larger (e.g., sign-extended) value whose
12156 // lower bits are described with an SDDbgValue, do not attempt
12157 // to transfer the SDDbgValue to the upper bits.
12158 if (auto FI = Expr->getFragmentInfo())
12159 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12160 continue;
12161 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12162 SizeInBits);
12163 if (!Fragment)
12164 continue;
12165 Expr = *Fragment;
12166 }
12167
12168 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12169 // Clone the SDDbgValue and move it to To.
12170 SDDbgValue *Clone = getDbgValueList(
12171 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12172 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12173 Dbg->isVariadic());
12174 ClonedDVs.push_back(Clone);
12175
12176 if (InvalidateDbg) {
12177 // Invalidate value and indicate the SDDbgValue should not be emitted.
12178 Dbg->setIsInvalidated();
12179 Dbg->setIsEmitted();
12180 }
12181 }
12182
12183 for (SDDbgValue *Dbg : ClonedDVs) {
12184 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12185 "Transferred DbgValues should depend on the new SDNode");
12186 AddDbgValue(Dbg, false);
12187 }
12188}
12189
12191 if (!N.getHasDebugValue())
12192 return;
12193
12194 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12195 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12196 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12197 return SDDbgOperand::fromNode(Node, ResNo);
12198 };
12199
12201 for (auto *DV : GetDbgValues(&N)) {
12202 if (DV->isInvalidated())
12203 continue;
12204 switch (N.getOpcode()) {
12205 default:
12206 break;
12207 case ISD::ADD: {
12208 SDValue N0 = N.getOperand(0);
12209 SDValue N1 = N.getOperand(1);
12210 if (!isa<ConstantSDNode>(N0)) {
12211 bool RHSConstant = isa<ConstantSDNode>(N1);
12213 if (RHSConstant)
12214 Offset = N.getConstantOperandVal(1);
12215 // We are not allowed to turn indirect debug values variadic, so
12216 // don't salvage those.
12217 if (!RHSConstant && DV->isIndirect())
12218 continue;
12219
12220 // Rewrite an ADD constant node into a DIExpression. Since we are
12221 // performing arithmetic to compute the variable's *value* in the
12222 // DIExpression, we need to mark the expression with a
12223 // DW_OP_stack_value.
12224 auto *DIExpr = DV->getExpression();
12225 auto NewLocOps = DV->copyLocationOps();
12226 bool Changed = false;
12227 size_t OrigLocOpsSize = NewLocOps.size();
12228 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12229 // We're not given a ResNo to compare against because the whole
12230 // node is going away. We know that any ISD::ADD only has one
12231 // result, so we can assume any node match is using the result.
12232 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12233 NewLocOps[i].getSDNode() != &N)
12234 continue;
12235 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12236 if (RHSConstant) {
12239 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12240 } else {
12241 // Convert to a variadic expression (if not already).
12242 // convertToVariadicExpression() returns a const pointer, so we use
12243 // a temporary const variable here.
12244 const auto *TmpDIExpr =
12248 ExprOps.push_back(NewLocOps.size());
12249 ExprOps.push_back(dwarf::DW_OP_plus);
12250 SDDbgOperand RHS =
12252 NewLocOps.push_back(RHS);
12253 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12254 }
12255 Changed = true;
12256 }
12257 (void)Changed;
12258 assert(Changed && "Salvage target doesn't use N");
12259
12260 bool IsVariadic =
12261 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12262
12263 auto AdditionalDependencies = DV->getAdditionalDependencies();
12264 SDDbgValue *Clone = getDbgValueList(
12265 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12266 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12267 ClonedDVs.push_back(Clone);
12268 DV->setIsInvalidated();
12269 DV->setIsEmitted();
12270 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12271 N0.getNode()->dumprFull(this);
12272 dbgs() << " into " << *DIExpr << '\n');
12273 }
12274 break;
12275 }
12276 case ISD::TRUNCATE: {
12277 SDValue N0 = N.getOperand(0);
12278 TypeSize FromSize = N0.getValueSizeInBits();
12279 TypeSize ToSize = N.getValueSizeInBits(0);
12280
12281 DIExpression *DbgExpression = DV->getExpression();
12282 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12283 auto NewLocOps = DV->copyLocationOps();
12284 bool Changed = false;
12285 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12286 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12287 NewLocOps[i].getSDNode() != &N)
12288 continue;
12289
12290 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12291 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12292 Changed = true;
12293 }
12294 assert(Changed && "Salvage target doesn't use N");
12295 (void)Changed;
12296
12297 SDDbgValue *Clone =
12298 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12299 DV->getAdditionalDependencies(), DV->isIndirect(),
12300 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12301
12302 ClonedDVs.push_back(Clone);
12303 DV->setIsInvalidated();
12304 DV->setIsEmitted();
12305 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12306 dbgs() << " into " << *DbgExpression << '\n');
12307 break;
12308 }
12309 }
12310 }
12311
12312 for (SDDbgValue *Dbg : ClonedDVs) {
12313 assert((!Dbg->getSDNodes().empty() ||
12314 llvm::any_of(Dbg->getLocationOps(),
12315 [&](const SDDbgOperand &Op) {
12316 return Op.getKind() == SDDbgOperand::FRAMEIX;
12317 })) &&
12318 "Salvaged DbgValue should depend on a new SDNode");
12319 AddDbgValue(Dbg, false);
12320 }
12321}
12322
12323/// Creates a SDDbgLabel node.
12325 const DebugLoc &DL, unsigned O) {
12326 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12327 "Expected inlined-at fields to agree");
12328 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12329}
12330
12331namespace {
12332
12333/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12334/// pointed to by a use iterator is deleted, increment the use iterator
12335/// so that it doesn't dangle.
12336///
12337class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12340
12341 void NodeDeleted(SDNode *N, SDNode *E) override {
12342 // Increment the iterator as needed.
12343 while (UI != UE && N == UI->getUser())
12344 ++UI;
12345 }
12346
12347public:
12348 RAUWUpdateListener(SelectionDAG &d,
12351 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12352};
12353
12354} // end anonymous namespace
12355
12356/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12357/// This can cause recursive merging of nodes in the DAG.
12358///
12359/// This version assumes From has a single result value.
12360///
12362 SDNode *From = FromN.getNode();
12363 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12364 "Cannot replace with this method!");
12365 assert(From != To.getNode() && "Cannot replace uses of with self");
12366
12367 // Preserve Debug Values
12368 transferDbgValues(FromN, To);
12369 // Preserve extra info.
12370 copyExtraInfo(From, To.getNode());
12371
12372 // Iterate over all the existing uses of From. New uses will be added
12373 // to the beginning of the use list, which we avoid visiting.
12374 // This specifically avoids visiting uses of From that arise while the
12375 // replacement is happening, because any such uses would be the result
12376 // of CSE: If an existing node looks like From after one of its operands
12377 // is replaced by To, we don't want to replace of all its users with To
12378 // too. See PR3018 for more info.
12379 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12380 RAUWUpdateListener Listener(*this, UI, UE);
12381 while (UI != UE) {
12382 SDNode *User = UI->getUser();
12383
12384 // This node is about to morph, remove its old self from the CSE maps.
12385 RemoveNodeFromCSEMaps(User);
12386
12387 // A user can appear in a use list multiple times, and when this
12388 // happens the uses are usually next to each other in the list.
12389 // To help reduce the number of CSE recomputations, process all
12390 // the uses of this user that we can find this way.
12391 do {
12392 SDUse &Use = *UI;
12393 ++UI;
12394 Use.set(To);
12395 if (To->isDivergent() != From->isDivergent())
12397 } while (UI != UE && UI->getUser() == User);
12398 // Now that we have modified User, add it back to the CSE maps. If it
12399 // already exists there, recursively merge the results together.
12400 AddModifiedNodeToCSEMaps(User);
12401 }
12402
12403 // If we just RAUW'd the root, take note.
12404 if (FromN == getRoot())
12405 setRoot(To);
12406}
12407
12408/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12409/// This can cause recursive merging of nodes in the DAG.
12410///
12411/// This version assumes that for each value of From, there is a
12412/// corresponding value in To in the same position with the same type.
12413///
12415#ifndef NDEBUG
12416 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12417 assert((!From->hasAnyUseOfValue(i) ||
12418 From->getValueType(i) == To->getValueType(i)) &&
12419 "Cannot use this version of ReplaceAllUsesWith!");
12420#endif
12421
12422 // Handle the trivial case.
12423 if (From == To)
12424 return;
12425
12426 // Preserve Debug Info. Only do this if there's a use.
12427 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12428 if (From->hasAnyUseOfValue(i)) {
12429 assert((i < To->getNumValues()) && "Invalid To location");
12430 transferDbgValues(SDValue(From, i), SDValue(To, i));
12431 }
12432 // Preserve extra info.
12433 copyExtraInfo(From, To);
12434
12435 // Iterate over just the existing users of From. See the comments in
12436 // the ReplaceAllUsesWith above.
12437 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12438 RAUWUpdateListener Listener(*this, UI, UE);
12439 while (UI != UE) {
12440 SDNode *User = UI->getUser();
12441
12442 // This node is about to morph, remove its old self from the CSE maps.
12443 RemoveNodeFromCSEMaps(User);
12444
12445 // A user can appear in a use list multiple times, and when this
12446 // happens the uses are usually next to each other in the list.
12447 // To help reduce the number of CSE recomputations, process all
12448 // the uses of this user that we can find this way.
12449 do {
12450 SDUse &Use = *UI;
12451 ++UI;
12452 Use.setNode(To);
12453 if (To->isDivergent() != From->isDivergent())
12455 } while (UI != UE && UI->getUser() == User);
12456
12457 // Now that we have modified User, add it back to the CSE maps. If it
12458 // already exists there, recursively merge the results together.
12459 AddModifiedNodeToCSEMaps(User);
12460 }
12461
12462 // If we just RAUW'd the root, take note.
12463 if (From == getRoot().getNode())
12464 setRoot(SDValue(To, getRoot().getResNo()));
12465}
12466
12467/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12468/// This can cause recursive merging of nodes in the DAG.
12469///
12470/// This version can replace From with any result values. To must match the
12471/// number and types of values returned by From.
12473 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12474 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12475
12476 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12477 // Preserve Debug Info.
12478 transferDbgValues(SDValue(From, i), To[i]);
12479 // Preserve extra info.
12480 copyExtraInfo(From, To[i].getNode());
12481 }
12482
12483 // Iterate over just the existing users of From. See the comments in
12484 // the ReplaceAllUsesWith above.
12485 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12486 RAUWUpdateListener Listener(*this, UI, UE);
12487 while (UI != UE) {
12488 SDNode *User = UI->getUser();
12489
12490 // This node is about to morph, remove its old self from the CSE maps.
12491 RemoveNodeFromCSEMaps(User);
12492
12493 // A user can appear in a use list multiple times, and when this happens the
12494 // uses are usually next to each other in the list. To help reduce the
12495 // number of CSE and divergence recomputations, process all the uses of this
12496 // user that we can find this way.
12497 bool To_IsDivergent = false;
12498 do {
12499 SDUse &Use = *UI;
12500 const SDValue &ToOp = To[Use.getResNo()];
12501 ++UI;
12502 Use.set(ToOp);
12503 if (ToOp.getValueType() != MVT::Other)
12504 To_IsDivergent |= ToOp->isDivergent();
12505 } while (UI != UE && UI->getUser() == User);
12506
12507 if (To_IsDivergent != From->isDivergent())
12509
12510 // Now that we have modified User, add it back to the CSE maps. If it
12511 // already exists there, recursively merge the results together.
12512 AddModifiedNodeToCSEMaps(User);
12513 }
12514
12515 // If we just RAUW'd the root, take note.
12516 if (From == getRoot().getNode())
12517 setRoot(SDValue(To[getRoot().getResNo()]));
12518}
12519
12520/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12521/// uses of other values produced by From.getNode() alone. The Deleted
12522/// vector is handled the same way as for ReplaceAllUsesWith.
12524 // Handle the really simple, really trivial case efficiently.
12525 if (From == To) return;
12526
12527 // Handle the simple, trivial, case efficiently.
12528 if (From.getNode()->getNumValues() == 1) {
12529 ReplaceAllUsesWith(From, To);
12530 return;
12531 }
12532
12533 // Preserve Debug Info.
12534 transferDbgValues(From, To);
12535 copyExtraInfo(From.getNode(), To.getNode());
12536
12537 // Iterate over just the existing users of From. See the comments in
12538 // the ReplaceAllUsesWith above.
12539 SDNode::use_iterator UI = From.getNode()->use_begin(),
12540 UE = From.getNode()->use_end();
12541 RAUWUpdateListener Listener(*this, UI, UE);
12542 while (UI != UE) {
12543 SDNode *User = UI->getUser();
12544 bool UserRemovedFromCSEMaps = false;
12545
12546 // A user can appear in a use list multiple times, and when this
12547 // happens the uses are usually next to each other in the list.
12548 // To help reduce the number of CSE recomputations, process all
12549 // the uses of this user that we can find this way.
12550 do {
12551 SDUse &Use = *UI;
12552
12553 // Skip uses of different values from the same node.
12554 if (Use.getResNo() != From.getResNo()) {
12555 ++UI;
12556 continue;
12557 }
12558
12559 // If this node hasn't been modified yet, it's still in the CSE maps,
12560 // so remove its old self from the CSE maps.
12561 if (!UserRemovedFromCSEMaps) {
12562 RemoveNodeFromCSEMaps(User);
12563 UserRemovedFromCSEMaps = true;
12564 }
12565
12566 ++UI;
12567 Use.set(To);
12568 if (To->isDivergent() != From->isDivergent())
12570 } while (UI != UE && UI->getUser() == User);
12571 // We are iterating over all uses of the From node, so if a use
12572 // doesn't use the specific value, no changes are made.
12573 if (!UserRemovedFromCSEMaps)
12574 continue;
12575
12576 // Now that we have modified User, add it back to the CSE maps. If it
12577 // already exists there, recursively merge the results together.
12578 AddModifiedNodeToCSEMaps(User);
12579 }
12580
12581 // If we just RAUW'd the root, take note.
12582 if (From == getRoot())
12583 setRoot(To);
12584}
12585
12586namespace {
12587
12588/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12589/// to record information about a use.
12590struct UseMemo {
12591 SDNode *User;
12592 unsigned Index;
12593 SDUse *Use;
12594};
12595
12596/// operator< - Sort Memos by User.
12597bool operator<(const UseMemo &L, const UseMemo &R) {
12598 return (intptr_t)L.User < (intptr_t)R.User;
12599}
12600
12601/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12602/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12603/// the node already has been taken care of recursively.
12604class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12605 SmallVectorImpl<UseMemo> &Uses;
12606
12607 void NodeDeleted(SDNode *N, SDNode *E) override {
12608 for (UseMemo &Memo : Uses)
12609 if (Memo.User == N)
12610 Memo.User = nullptr;
12611 }
12612
12613public:
12614 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12615 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12616};
12617
12618} // end anonymous namespace
12619
12620/// Return true if a glue output should propagate divergence information.
12622 switch (Node->getOpcode()) {
12623 case ISD::CopyFromReg:
12624 case ISD::CopyToReg:
12625 return false;
12626 default:
12627 return true;
12628 }
12629
12630 llvm_unreachable("covered opcode switch");
12631}
12632
12634 if (TLI->isSDNodeAlwaysUniform(N)) {
12635 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12636 "Conflicting divergence information!");
12637 return false;
12638 }
12639 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12640 return true;
12641 for (const auto &Op : N->ops()) {
12642 EVT VT = Op.getValueType();
12643
12644 // Skip Chain. It does not carry divergence.
12645 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12646 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12647 return true;
12648 }
12649 return false;
12650}
12651
12653 SmallVector<SDNode *, 16> Worklist(1, N);
12654 do {
12655 N = Worklist.pop_back_val();
12656 bool IsDivergent = calculateDivergence(N);
12657 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12658 N->SDNodeBits.IsDivergent = IsDivergent;
12659 llvm::append_range(Worklist, N->users());
12660 }
12661 } while (!Worklist.empty());
12662}
12663
12664void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12666 Order.reserve(AllNodes.size());
12667 for (auto &N : allnodes()) {
12668 unsigned NOps = N.getNumOperands();
12669 Degree[&N] = NOps;
12670 if (0 == NOps)
12671 Order.push_back(&N);
12672 }
12673 for (size_t I = 0; I != Order.size(); ++I) {
12674 SDNode *N = Order[I];
12675 for (auto *U : N->users()) {
12676 unsigned &UnsortedOps = Degree[U];
12677 if (0 == --UnsortedOps)
12678 Order.push_back(U);
12679 }
12680 }
12681}
12682
12683#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12684void SelectionDAG::VerifyDAGDivergence() {
12685 std::vector<SDNode *> TopoOrder;
12686 CreateTopologicalOrder(TopoOrder);
12687 for (auto *N : TopoOrder) {
12688 assert(calculateDivergence(N) == N->isDivergent() &&
12689 "Divergence bit inconsistency detected");
12690 }
12691}
12692#endif
12693
12694/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12695/// uses of other values produced by From.getNode() alone. The same value
12696/// may appear in both the From and To list. The Deleted vector is
12697/// handled the same way as for ReplaceAllUsesWith.
12699 const SDValue *To,
12700 unsigned Num){
12701 // Handle the simple, trivial case efficiently.
12702 if (Num == 1)
12703 return ReplaceAllUsesOfValueWith(*From, *To);
12704
12705 transferDbgValues(*From, *To);
12706 copyExtraInfo(From->getNode(), To->getNode());
12707
12708 // Read up all the uses and make records of them. This helps
12709 // processing new uses that are introduced during the
12710 // replacement process.
12712 for (unsigned i = 0; i != Num; ++i) {
12713 unsigned FromResNo = From[i].getResNo();
12714 SDNode *FromNode = From[i].getNode();
12715 for (SDUse &Use : FromNode->uses()) {
12716 if (Use.getResNo() == FromResNo) {
12717 UseMemo Memo = {Use.getUser(), i, &Use};
12718 Uses.push_back(Memo);
12719 }
12720 }
12721 }
12722
12723 // Sort the uses, so that all the uses from a given User are together.
12725 RAUOVWUpdateListener Listener(*this, Uses);
12726
12727 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12728 UseIndex != UseIndexEnd; ) {
12729 // We know that this user uses some value of From. If it is the right
12730 // value, update it.
12731 SDNode *User = Uses[UseIndex].User;
12732 // If the node has been deleted by recursive CSE updates when updating
12733 // another node, then just skip this entry.
12734 if (User == nullptr) {
12735 ++UseIndex;
12736 continue;
12737 }
12738
12739 // This node is about to morph, remove its old self from the CSE maps.
12740 RemoveNodeFromCSEMaps(User);
12741
12742 // The Uses array is sorted, so all the uses for a given User
12743 // are next to each other in the list.
12744 // To help reduce the number of CSE recomputations, process all
12745 // the uses of this user that we can find this way.
12746 do {
12747 unsigned i = Uses[UseIndex].Index;
12748 SDUse &Use = *Uses[UseIndex].Use;
12749 ++UseIndex;
12750
12751 Use.set(To[i]);
12752 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12753
12754 // Now that we have modified User, add it back to the CSE maps. If it
12755 // already exists there, recursively merge the results together.
12756 AddModifiedNodeToCSEMaps(User);
12757 }
12758}
12759
12760/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12761/// based on their topological order. It returns the maximum id and a vector
12762/// of the SDNodes* in assigned order by reference.
12764 unsigned DAGSize = 0;
12765
12766 // SortedPos tracks the progress of the algorithm. Nodes before it are
12767 // sorted, nodes after it are unsorted. When the algorithm completes
12768 // it is at the end of the list.
12769 allnodes_iterator SortedPos = allnodes_begin();
12770
12771 // Visit all the nodes. Move nodes with no operands to the front of
12772 // the list immediately. Annotate nodes that do have operands with their
12773 // operand count. Before we do this, the Node Id fields of the nodes
12774 // may contain arbitrary values. After, the Node Id fields for nodes
12775 // before SortedPos will contain the topological sort index, and the
12776 // Node Id fields for nodes At SortedPos and after will contain the
12777 // count of outstanding operands.
12779 checkForCycles(&N, this);
12780 unsigned Degree = N.getNumOperands();
12781 if (Degree == 0) {
12782 // A node with no uses, add it to the result array immediately.
12783 N.setNodeId(DAGSize++);
12784 allnodes_iterator Q(&N);
12785 if (Q != SortedPos)
12786 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12787 assert(SortedPos != AllNodes.end() && "Overran node list");
12788 ++SortedPos;
12789 } else {
12790 // Temporarily use the Node Id as scratch space for the degree count.
12791 N.setNodeId(Degree);
12792 }
12793 }
12794
12795 // Visit all the nodes. As we iterate, move nodes into sorted order,
12796 // such that by the time the end is reached all nodes will be sorted.
12797 for (SDNode &Node : allnodes()) {
12798 SDNode *N = &Node;
12799 checkForCycles(N, this);
12800 // N is in sorted position, so all its uses have one less operand
12801 // that needs to be sorted.
12802 for (SDNode *P : N->users()) {
12803 unsigned Degree = P->getNodeId();
12804 assert(Degree != 0 && "Invalid node degree");
12805 --Degree;
12806 if (Degree == 0) {
12807 // All of P's operands are sorted, so P may sorted now.
12808 P->setNodeId(DAGSize++);
12809 if (P->getIterator() != SortedPos)
12810 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12811 assert(SortedPos != AllNodes.end() && "Overran node list");
12812 ++SortedPos;
12813 } else {
12814 // Update P's outstanding operand count.
12815 P->setNodeId(Degree);
12816 }
12817 }
12818 if (Node.getIterator() == SortedPos) {
12819#ifndef NDEBUG
12821 SDNode *S = &*++I;
12822 dbgs() << "Overran sorted position:\n";
12823 S->dumprFull(this); dbgs() << "\n";
12824 dbgs() << "Checking if this is due to cycles\n";
12825 checkForCycles(this, true);
12826#endif
12827 llvm_unreachable(nullptr);
12828 }
12829 }
12830
12831 assert(SortedPos == AllNodes.end() &&
12832 "Topological sort incomplete!");
12833 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12834 "First node in topological sort is not the entry token!");
12835 assert(AllNodes.front().getNodeId() == 0 &&
12836 "First node in topological sort has non-zero id!");
12837 assert(AllNodes.front().getNumOperands() == 0 &&
12838 "First node in topological sort has operands!");
12839 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12840 "Last node in topologic sort has unexpected id!");
12841 assert(AllNodes.back().use_empty() &&
12842 "Last node in topologic sort has users!");
12843 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12844 return DAGSize;
12845}
12846
12848 SmallVectorImpl<const SDNode *> &SortedNodes) const {
12849 SortedNodes.clear();
12850 // Node -> remaining number of outstanding operands.
12851 DenseMap<const SDNode *, unsigned> RemainingOperands;
12852
12853 // Put nodes without any operands into SortedNodes first.
12854 for (const SDNode &N : allnodes()) {
12855 checkForCycles(&N, this);
12856 unsigned NumOperands = N.getNumOperands();
12857 if (NumOperands == 0)
12858 SortedNodes.push_back(&N);
12859 else
12860 // Record their total number of outstanding operands.
12861 RemainingOperands[&N] = NumOperands;
12862 }
12863
12864 // A node is pushed into SortedNodes when all of its operands (predecessors in
12865 // the graph) are also in SortedNodes.
12866 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
12867 const SDNode *N = SortedNodes[i];
12868 for (const SDNode *U : N->users()) {
12869 // HandleSDNode is never part of a DAG and therefore has no entry in
12870 // RemainingOperands.
12871 if (U->getOpcode() == ISD::HANDLENODE)
12872 continue;
12873 unsigned &NumRemOperands = RemainingOperands[U];
12874 assert(NumRemOperands && "Invalid number of remaining operands");
12875 --NumRemOperands;
12876 if (!NumRemOperands)
12877 SortedNodes.push_back(U);
12878 }
12879 }
12880
12881 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
12882 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
12883 "First node in topological sort is not the entry token");
12884 assert(SortedNodes.front()->getNumOperands() == 0 &&
12885 "First node in topological sort has operands");
12886}
12887
12888/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12889/// value is produced by SD.
12890void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12891 for (SDNode *SD : DB->getSDNodes()) {
12892 if (!SD)
12893 continue;
12894 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12895 SD->setHasDebugValue(true);
12896 }
12897 DbgInfo->add(DB, isParameter);
12898}
12899
12900void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12901
12903 SDValue NewMemOpChain) {
12904 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12905 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12906 // The new memory operation must have the same position as the old load in
12907 // terms of memory dependency. Create a TokenFactor for the old load and new
12908 // memory operation and update uses of the old load's output chain to use that
12909 // TokenFactor.
12910 if (OldChain == NewMemOpChain || OldChain.use_empty())
12911 return NewMemOpChain;
12912
12913 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12914 OldChain, NewMemOpChain);
12915 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12916 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12917 return TokenFactor;
12918}
12919
12921 SDValue NewMemOp) {
12922 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12923 SDValue OldChain = SDValue(OldLoad, 1);
12924 SDValue NewMemOpChain = NewMemOp.getValue(1);
12925 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12926}
12927
12929 Function **OutFunction) {
12930 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12931
12932 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12933 auto *Module = MF->getFunction().getParent();
12934 auto *Function = Module->getFunction(Symbol);
12935
12936 if (OutFunction != nullptr)
12937 *OutFunction = Function;
12938
12939 if (Function != nullptr) {
12940 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12941 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12942 }
12943
12944 std::string ErrorStr;
12945 raw_string_ostream ErrorFormatter(ErrorStr);
12946 ErrorFormatter << "Undefined external symbol ";
12947 ErrorFormatter << '"' << Symbol << '"';
12948 report_fatal_error(Twine(ErrorStr));
12949}
12950
12951//===----------------------------------------------------------------------===//
12952// SDNode Class
12953//===----------------------------------------------------------------------===//
12954
12957 return Const != nullptr && Const->isZero();
12958}
12959
12961 return V.isUndef() || isNullConstant(V);
12962}
12963
12966 return Const != nullptr && Const->isZero() && !Const->isNegative();
12967}
12968
12971 return Const != nullptr && Const->isAllOnes();
12972}
12973
12976 return Const != nullptr && Const->isOne();
12977}
12978
12981 return Const != nullptr && Const->isMinSignedValue();
12982}
12983
12984bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12985 unsigned OperandNo) {
12986 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12987 // TODO: Target-specific opcodes could be added.
12988 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12989 /*AllowTruncation*/ true)) {
12990 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12991 switch (Opcode) {
12992 case ISD::ADD:
12993 case ISD::OR:
12994 case ISD::XOR:
12995 case ISD::UMAX:
12996 return Const.isZero();
12997 case ISD::MUL:
12998 return Const.isOne();
12999 case ISD::AND:
13000 case ISD::UMIN:
13001 return Const.isAllOnes();
13002 case ISD::SMAX:
13003 return Const.isMinSignedValue();
13004 case ISD::SMIN:
13005 return Const.isMaxSignedValue();
13006 case ISD::SUB:
13007 case ISD::SHL:
13008 case ISD::SRA:
13009 case ISD::SRL:
13010 return OperandNo == 1 && Const.isZero();
13011 case ISD::UDIV:
13012 case ISD::SDIV:
13013 return OperandNo == 1 && Const.isOne();
13014 }
13015 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
13016 switch (Opcode) {
13017 case ISD::FADD:
13018 return ConstFP->isZero() &&
13019 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
13020 case ISD::FSUB:
13021 return OperandNo == 1 && ConstFP->isZero() &&
13022 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
13023 case ISD::FMUL:
13024 return ConstFP->isExactlyValue(1.0);
13025 case ISD::FDIV:
13026 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
13027 case ISD::FMINNUM:
13028 case ISD::FMAXNUM: {
13029 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13030 EVT VT = V.getValueType();
13031 const fltSemantics &Semantics = VT.getFltSemantics();
13032 APFloat NeutralAF = !Flags.hasNoNaNs()
13033 ? APFloat::getQNaN(Semantics)
13034 : !Flags.hasNoInfs()
13035 ? APFloat::getInf(Semantics)
13036 : APFloat::getLargest(Semantics);
13037 if (Opcode == ISD::FMAXNUM)
13038 NeutralAF.changeSign();
13039
13040 return ConstFP->isExactlyValue(NeutralAF);
13041 }
13042 }
13043 }
13044 return false;
13045}
13046
13048 while (V.getOpcode() == ISD::BITCAST)
13049 V = V.getOperand(0);
13050 return V;
13051}
13052
13054 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
13055 V = V.getOperand(0);
13056 return V;
13057}
13058
13060 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
13061 V = V.getOperand(0);
13062 return V;
13063}
13064
13066 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13067 SDValue InVec = V.getOperand(0);
13068 SDValue EltNo = V.getOperand(2);
13069 EVT VT = InVec.getValueType();
13070 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
13071 if (IndexC && VT.isFixedLengthVector() &&
13072 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
13073 !DemandedElts[IndexC->getZExtValue()]) {
13074 V = InVec;
13075 continue;
13076 }
13077 break;
13078 }
13079 return V;
13080}
13081
13083 while (V.getOpcode() == ISD::TRUNCATE)
13084 V = V.getOperand(0);
13085 return V;
13086}
13087
13088bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
13089 if (V.getOpcode() != ISD::XOR)
13090 return false;
13091 V = peekThroughBitcasts(V.getOperand(1));
13092 unsigned NumBits = V.getScalarValueSizeInBits();
13093 ConstantSDNode *C =
13094 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
13095 return C && (C->getAPIntValue().countr_one() >= NumBits);
13096}
13097
13099 bool AllowTruncation) {
13100 EVT VT = N.getValueType();
13101 APInt DemandedElts = VT.isFixedLengthVector()
13103 : APInt(1, 1);
13104 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
13105}
13106
13108 bool AllowUndefs,
13109 bool AllowTruncation) {
13111 return CN;
13112
13113 // SplatVectors can truncate their operands. Ignore that case here unless
13114 // AllowTruncation is set.
13115 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
13116 EVT VecEltVT = N->getValueType(0).getVectorElementType();
13117 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
13118 EVT CVT = CN->getValueType(0);
13119 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13120 if (AllowTruncation || CVT == VecEltVT)
13121 return CN;
13122 }
13123 }
13124
13126 BitVector UndefElements;
13127 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13128
13129 // BuildVectors can truncate their operands. Ignore that case here unless
13130 // AllowTruncation is set.
13131 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13132 if (CN && (UndefElements.none() || AllowUndefs)) {
13133 EVT CVT = CN->getValueType(0);
13134 EVT NSVT = N.getValueType().getScalarType();
13135 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13136 if (AllowTruncation || (CVT == NSVT))
13137 return CN;
13138 }
13139 }
13140
13141 return nullptr;
13142}
13143
13145 EVT VT = N.getValueType();
13146 APInt DemandedElts = VT.isFixedLengthVector()
13148 : APInt(1, 1);
13149 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13150}
13151
13153 const APInt &DemandedElts,
13154 bool AllowUndefs) {
13156 return CN;
13157
13159 BitVector UndefElements;
13160 ConstantFPSDNode *CN =
13161 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13162 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13163 if (CN && (UndefElements.none() || AllowUndefs))
13164 return CN;
13165 }
13166
13167 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13168 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13169 return CN;
13170
13171 return nullptr;
13172}
13173
13174bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13175 // TODO: may want to use peekThroughBitcast() here.
13176 ConstantSDNode *C =
13177 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13178 return C && C->isZero();
13179}
13180
13181bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13182 ConstantSDNode *C =
13183 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13184 return C && C->isOne();
13185}
13186
13187bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13188 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13189 return C && C->isExactlyValue(1.0);
13190}
13191
13192bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13194 unsigned BitWidth = N.getScalarValueSizeInBits();
13195 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13196 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13197}
13198
13199bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13200 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13201 return C && APInt::isSameValue(C->getAPIntValue(),
13202 APInt(C->getAPIntValue().getBitWidth(), 1));
13203}
13204
13205bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13207 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13208 return C && C->isZero();
13209}
13210
13211bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13212 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13213 return C && C->isZero();
13214}
13215
13219
13220MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
13221 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
13222 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
13223 MemSDNodeBits.IsVolatile = MMO->isVolatile();
13224 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
13225 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
13226 MemSDNodeBits.IsInvariant = MMO->isInvariant();
13227
13228 // We check here that the size of the memory operand fits within the size of
13229 // the MMO. This is because the MMO might indicate only a possible address
13230 // range instead of specifying the affected memory addresses precisely.
13231 assert(
13232 (!MMO->getType().isValid() ||
13233 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
13234 "Size mismatch!");
13235}
13236
13237/// Profile - Gather unique data for the node.
13238///
13240 AddNodeIDNode(ID, this);
13241}
13242
13243namespace {
13244
13245 struct EVTArray {
13246 std::vector<EVT> VTs;
13247
13248 EVTArray() {
13249 VTs.reserve(MVT::VALUETYPE_SIZE);
13250 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13251 VTs.push_back(MVT((MVT::SimpleValueType)i));
13252 }
13253 };
13254
13255} // end anonymous namespace
13256
13257/// getValueTypeList - Return a pointer to the specified value type.
13258///
13259const EVT *SDNode::getValueTypeList(MVT VT) {
13260 static EVTArray SimpleVTArray;
13261
13262 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13263 return &SimpleVTArray.VTs[VT.SimpleTy];
13264}
13265
13266/// hasAnyUseOfValue - Return true if there are any use of the indicated
13267/// value. This method ignores uses of other values defined by this operation.
13268bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13269 assert(Value < getNumValues() && "Bad value!");
13270
13271 for (SDUse &U : uses())
13272 if (U.getResNo() == Value)
13273 return true;
13274
13275 return false;
13276}
13277
13278/// isOnlyUserOf - Return true if this node is the only use of N.
13279bool SDNode::isOnlyUserOf(const SDNode *N) const {
13280 bool Seen = false;
13281 for (const SDNode *User : N->users()) {
13282 if (User == this)
13283 Seen = true;
13284 else
13285 return false;
13286 }
13287
13288 return Seen;
13289}
13290
13291/// Return true if the only users of N are contained in Nodes.
13293 bool Seen = false;
13294 for (const SDNode *User : N->users()) {
13295 if (llvm::is_contained(Nodes, User))
13296 Seen = true;
13297 else
13298 return false;
13299 }
13300
13301 return Seen;
13302}
13303
13304/// Return true if the referenced return value is an operand of N.
13305bool SDValue::isOperandOf(const SDNode *N) const {
13306 return is_contained(N->op_values(), *this);
13307}
13308
13309bool SDNode::isOperandOf(const SDNode *N) const {
13310 return any_of(N->op_values(),
13311 [this](SDValue Op) { return this == Op.getNode(); });
13312}
13313
13314/// reachesChainWithoutSideEffects - Return true if this operand (which must
13315/// be a chain) reaches the specified operand without crossing any
13316/// side-effecting instructions on any chain path. In practice, this looks
13317/// through token factors and non-volatile loads. In order to remain efficient,
13318/// this only looks a couple of nodes in, it does not do an exhaustive search.
13319///
13320/// Note that we only need to examine chains when we're searching for
13321/// side-effects; SelectionDAG requires that all side-effects are represented
13322/// by chains, even if another operand would force a specific ordering. This
13323/// constraint is necessary to allow transformations like splitting loads.
13325 unsigned Depth) const {
13326 if (*this == Dest) return true;
13327
13328 // Don't search too deeply, we just want to be able to see through
13329 // TokenFactor's etc.
13330 if (Depth == 0) return false;
13331
13332 // If this is a token factor, all inputs to the TF happen in parallel.
13333 if (getOpcode() == ISD::TokenFactor) {
13334 // First, try a shallow search.
13335 if (is_contained((*this)->ops(), Dest)) {
13336 // We found the chain we want as an operand of this TokenFactor.
13337 // Essentially, we reach the chain without side-effects if we could
13338 // serialize the TokenFactor into a simple chain of operations with
13339 // Dest as the last operation. This is automatically true if the
13340 // chain has one use: there are no other ordering constraints.
13341 // If the chain has more than one use, we give up: some other
13342 // use of Dest might force a side-effect between Dest and the current
13343 // node.
13344 if (Dest.hasOneUse())
13345 return true;
13346 }
13347 // Next, try a deep search: check whether every operand of the TokenFactor
13348 // reaches Dest.
13349 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13350 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13351 });
13352 }
13353
13354 // Loads don't have side effects, look through them.
13355 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13356 if (Ld->isUnordered())
13357 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13358 }
13359 return false;
13360}
13361
13362bool SDNode::hasPredecessor(const SDNode *N) const {
13365 Worklist.push_back(this);
13366 return hasPredecessorHelper(N, Visited, Worklist);
13367}
13368
13370 this->Flags &= Flags;
13371}
13372
13373SDValue
13375 ArrayRef<ISD::NodeType> CandidateBinOps,
13376 bool AllowPartials) {
13377 // The pattern must end in an extract from index 0.
13378 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13379 !isNullConstant(Extract->getOperand(1)))
13380 return SDValue();
13381
13382 // Match against one of the candidate binary ops.
13383 SDValue Op = Extract->getOperand(0);
13384 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13385 return Op.getOpcode() == unsigned(BinOp);
13386 }))
13387 return SDValue();
13388
13389 // Floating-point reductions may require relaxed constraints on the final step
13390 // of the reduction because they may reorder intermediate operations.
13391 unsigned CandidateBinOp = Op.getOpcode();
13392 if (Op.getValueType().isFloatingPoint()) {
13393 SDNodeFlags Flags = Op->getFlags();
13394 switch (CandidateBinOp) {
13395 case ISD::FADD:
13396 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13397 return SDValue();
13398 break;
13399 default:
13400 llvm_unreachable("Unhandled FP opcode for binop reduction");
13401 }
13402 }
13403
13404 // Matching failed - attempt to see if we did enough stages that a partial
13405 // reduction from a subvector is possible.
13406 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13407 if (!AllowPartials || !Op)
13408 return SDValue();
13409 EVT OpVT = Op.getValueType();
13410 EVT OpSVT = OpVT.getScalarType();
13411 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13412 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13413 return SDValue();
13414 BinOp = (ISD::NodeType)CandidateBinOp;
13415 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13416 };
13417
13418 // At each stage, we're looking for something that looks like:
13419 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13420 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13421 // i32 undef, i32 undef, i32 undef, i32 undef>
13422 // %a = binop <8 x i32> %op, %s
13423 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13424 // we expect something like:
13425 // <4,5,6,7,u,u,u,u>
13426 // <2,3,u,u,u,u,u,u>
13427 // <1,u,u,u,u,u,u,u>
13428 // While a partial reduction match would be:
13429 // <2,3,u,u,u,u,u,u>
13430 // <1,u,u,u,u,u,u,u>
13431 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13432 SDValue PrevOp;
13433 for (unsigned i = 0; i < Stages; ++i) {
13434 unsigned MaskEnd = (1 << i);
13435
13436 if (Op.getOpcode() != CandidateBinOp)
13437 return PartialReduction(PrevOp, MaskEnd);
13438
13439 SDValue Op0 = Op.getOperand(0);
13440 SDValue Op1 = Op.getOperand(1);
13441
13443 if (Shuffle) {
13444 Op = Op1;
13445 } else {
13446 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13447 Op = Op0;
13448 }
13449
13450 // The first operand of the shuffle should be the same as the other operand
13451 // of the binop.
13452 if (!Shuffle || Shuffle->getOperand(0) != Op)
13453 return PartialReduction(PrevOp, MaskEnd);
13454
13455 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13456 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13457 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13458 return PartialReduction(PrevOp, MaskEnd);
13459
13460 PrevOp = Op;
13461 }
13462
13463 // Handle subvector reductions, which tend to appear after the shuffle
13464 // reduction stages.
13465 while (Op.getOpcode() == CandidateBinOp) {
13466 unsigned NumElts = Op.getValueType().getVectorNumElements();
13467 SDValue Op0 = Op.getOperand(0);
13468 SDValue Op1 = Op.getOperand(1);
13469 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13471 Op0.getOperand(0) != Op1.getOperand(0))
13472 break;
13473 SDValue Src = Op0.getOperand(0);
13474 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13475 if (NumSrcElts != (2 * NumElts))
13476 break;
13477 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13478 Op1.getConstantOperandAPInt(1) == NumElts) &&
13479 !(Op1.getConstantOperandAPInt(1) == 0 &&
13480 Op0.getConstantOperandAPInt(1) == NumElts))
13481 break;
13482 Op = Src;
13483 }
13484
13485 BinOp = (ISD::NodeType)CandidateBinOp;
13486 return Op;
13487}
13488
13490 EVT VT = N->getValueType(0);
13491 EVT EltVT = VT.getVectorElementType();
13492 unsigned NE = VT.getVectorNumElements();
13493
13494 SDLoc dl(N);
13495
13496 // If ResNE is 0, fully unroll the vector op.
13497 if (ResNE == 0)
13498 ResNE = NE;
13499 else if (NE > ResNE)
13500 NE = ResNE;
13501
13502 if (N->getNumValues() == 2) {
13503 SmallVector<SDValue, 8> Scalars0, Scalars1;
13504 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13505 EVT VT1 = N->getValueType(1);
13506 EVT EltVT1 = VT1.getVectorElementType();
13507
13508 unsigned i;
13509 for (i = 0; i != NE; ++i) {
13510 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13511 SDValue Operand = N->getOperand(j);
13512 EVT OperandVT = Operand.getValueType();
13513
13514 // A vector operand; extract a single element.
13515 EVT OperandEltVT = OperandVT.getVectorElementType();
13516 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13517 }
13518
13519 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13520 Scalars0.push_back(EltOp);
13521 Scalars1.push_back(EltOp.getValue(1));
13522 }
13523
13524 for (; i < ResNE; ++i) {
13525 Scalars0.push_back(getUNDEF(EltVT));
13526 Scalars1.push_back(getUNDEF(EltVT1));
13527 }
13528
13529 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13530 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13531 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13532 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13533 return getMergeValues({Vec0, Vec1}, dl);
13534 }
13535
13536 assert(N->getNumValues() == 1 &&
13537 "Can't unroll a vector with multiple results!");
13538
13540 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13541
13542 unsigned i;
13543 for (i= 0; i != NE; ++i) {
13544 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13545 SDValue Operand = N->getOperand(j);
13546 EVT OperandVT = Operand.getValueType();
13547 if (OperandVT.isVector()) {
13548 // A vector operand; extract a single element.
13549 EVT OperandEltVT = OperandVT.getVectorElementType();
13550 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13551 } else {
13552 // A scalar operand; just use it as is.
13553 Operands[j] = Operand;
13554 }
13555 }
13556
13557 switch (N->getOpcode()) {
13558 default: {
13559 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13560 N->getFlags()));
13561 break;
13562 }
13563 case ISD::VSELECT:
13564 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13565 break;
13566 case ISD::SHL:
13567 case ISD::SRA:
13568 case ISD::SRL:
13569 case ISD::ROTL:
13570 case ISD::ROTR:
13571 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13572 getShiftAmountOperand(Operands[0].getValueType(),
13573 Operands[1])));
13574 break;
13576 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13577 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13578 Operands[0],
13579 getValueType(ExtVT)));
13580 break;
13581 }
13582 case ISD::ADDRSPACECAST: {
13583 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13584 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13585 ASC->getSrcAddressSpace(),
13586 ASC->getDestAddressSpace()));
13587 break;
13588 }
13589 }
13590 }
13591
13592 for (; i < ResNE; ++i)
13593 Scalars.push_back(getUNDEF(EltVT));
13594
13595 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13596 return getBuildVector(VecVT, dl, Scalars);
13597}
13598
13599std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13600 SDNode *N, unsigned ResNE) {
13601 unsigned Opcode = N->getOpcode();
13602 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13603 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13604 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13605 "Expected an overflow opcode");
13606
13607 EVT ResVT = N->getValueType(0);
13608 EVT OvVT = N->getValueType(1);
13609 EVT ResEltVT = ResVT.getVectorElementType();
13610 EVT OvEltVT = OvVT.getVectorElementType();
13611 SDLoc dl(N);
13612
13613 // If ResNE is 0, fully unroll the vector op.
13614 unsigned NE = ResVT.getVectorNumElements();
13615 if (ResNE == 0)
13616 ResNE = NE;
13617 else if (NE > ResNE)
13618 NE = ResNE;
13619
13620 SmallVector<SDValue, 8> LHSScalars;
13621 SmallVector<SDValue, 8> RHSScalars;
13622 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13623 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13624
13625 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13626 SDVTList VTs = getVTList(ResEltVT, SVT);
13627 SmallVector<SDValue, 8> ResScalars;
13628 SmallVector<SDValue, 8> OvScalars;
13629 for (unsigned i = 0; i < NE; ++i) {
13630 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13631 SDValue Ov =
13632 getSelect(dl, OvEltVT, Res.getValue(1),
13633 getBoolConstant(true, dl, OvEltVT, ResVT),
13634 getConstant(0, dl, OvEltVT));
13635
13636 ResScalars.push_back(Res);
13637 OvScalars.push_back(Ov);
13638 }
13639
13640 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13641 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13642
13643 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13644 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13645 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13646 getBuildVector(NewOvVT, dl, OvScalars));
13647}
13648
13651 unsigned Bytes,
13652 int Dist) const {
13653 if (LD->isVolatile() || Base->isVolatile())
13654 return false;
13655 // TODO: probably too restrictive for atomics, revisit
13656 if (!LD->isSimple())
13657 return false;
13658 if (LD->isIndexed() || Base->isIndexed())
13659 return false;
13660 if (LD->getChain() != Base->getChain())
13661 return false;
13662 EVT VT = LD->getMemoryVT();
13663 if (VT.getSizeInBits() / 8 != Bytes)
13664 return false;
13665
13666 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13667 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13668
13669 int64_t Offset = 0;
13670 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13671 return (Dist * (int64_t)Bytes == Offset);
13672 return false;
13673}
13674
13675/// InferPtrAlignment - Infer alignment of a load / store address. Return
13676/// std::nullopt if it cannot be inferred.
13678 // If this is a GlobalAddress + cst, return the alignment.
13679 const GlobalValue *GV = nullptr;
13680 int64_t GVOffset = 0;
13681 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13682 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13683 KnownBits Known(PtrWidth);
13685 unsigned AlignBits = Known.countMinTrailingZeros();
13686 if (AlignBits)
13687 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13688 }
13689
13690 // If this is a direct reference to a stack slot, use information about the
13691 // stack slot's alignment.
13692 int FrameIdx = INT_MIN;
13693 int64_t FrameOffset = 0;
13695 FrameIdx = FI->getIndex();
13696 } else if (isBaseWithConstantOffset(Ptr) &&
13698 // Handle FI+Cst
13699 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13700 FrameOffset = Ptr.getConstantOperandVal(1);
13701 }
13702
13703 if (FrameIdx != INT_MIN) {
13705 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13706 }
13707
13708 return std::nullopt;
13709}
13710
13711/// Split the scalar node with EXTRACT_ELEMENT using the provided
13712/// VTs and return the low/high part.
13713std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13714 const SDLoc &DL,
13715 const EVT &LoVT,
13716 const EVT &HiVT) {
13717 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13718 "Split node must be a scalar type");
13719 SDValue Lo =
13721 SDValue Hi =
13723 return std::make_pair(Lo, Hi);
13724}
13725
13726/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13727/// which is split (or expanded) into two not necessarily identical pieces.
13728std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13729 // Currently all types are split in half.
13730 EVT LoVT, HiVT;
13731 if (!VT.isVector())
13732 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13733 else
13734 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13735
13736 return std::make_pair(LoVT, HiVT);
13737}
13738
13739/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13740/// type, dependent on an enveloping VT that has been split into two identical
13741/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13742std::pair<EVT, EVT>
13744 bool *HiIsEmpty) const {
13745 EVT EltTp = VT.getVectorElementType();
13746 // Examples:
13747 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13748 // custom VL=9 with enveloping VL=8/8 yields 8/1
13749 // custom VL=10 with enveloping VL=8/8 yields 8/2
13750 // etc.
13751 ElementCount VTNumElts = VT.getVectorElementCount();
13752 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13753 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13754 "Mixing fixed width and scalable vectors when enveloping a type");
13755 EVT LoVT, HiVT;
13756 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13757 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13758 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13759 *HiIsEmpty = false;
13760 } else {
13761 // Flag that hi type has zero storage size, but return split envelop type
13762 // (this would be easier if vector types with zero elements were allowed).
13763 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13764 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13765 *HiIsEmpty = true;
13766 }
13767 return std::make_pair(LoVT, HiVT);
13768}
13769
13770/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13771/// low/high part.
13772std::pair<SDValue, SDValue>
13773SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13774 const EVT &HiVT) {
13775 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13776 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13777 "Splitting vector with an invalid mixture of fixed and scalable "
13778 "vector types");
13780 N.getValueType().getVectorMinNumElements() &&
13781 "More vector elements requested than available!");
13782 SDValue Lo, Hi;
13783 Lo = getExtractSubvector(DL, LoVT, N, 0);
13784 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13785 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13786 // IDX with the runtime scaling factor of the result vector type. For
13787 // fixed-width result vectors, that runtime scaling factor is 1.
13790 return std::make_pair(Lo, Hi);
13791}
13792
13793std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13794 const SDLoc &DL) {
13795 // Split the vector length parameter.
13796 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13797 EVT VT = N.getValueType();
13799 "Expecting the mask to be an evenly-sized vector");
13800 SDValue HalfNumElts = getElementCount(
13802 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13803 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13804 return std::make_pair(Lo, Hi);
13805}
13806
13807/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13809 EVT VT = N.getValueType();
13812 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13813}
13814
13817 unsigned Start, unsigned Count,
13818 EVT EltVT) {
13819 EVT VT = Op.getValueType();
13820 if (Count == 0)
13822 if (EltVT == EVT())
13823 EltVT = VT.getVectorElementType();
13824 SDLoc SL(Op);
13825 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13826 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13827 }
13828}
13829
13830// getAddressSpace - Return the address space this GlobalAddress belongs to.
13832 return getGlobal()->getType()->getAddressSpace();
13833}
13834
13837 return Val.MachineCPVal->getType();
13838 return Val.ConstVal->getType();
13839}
13840
13841bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13842 unsigned &SplatBitSize,
13843 bool &HasAnyUndefs,
13844 unsigned MinSplatBits,
13845 bool IsBigEndian) const {
13846 EVT VT = getValueType(0);
13847 assert(VT.isVector() && "Expected a vector type");
13848 unsigned VecWidth = VT.getSizeInBits();
13849 if (MinSplatBits > VecWidth)
13850 return false;
13851
13852 // FIXME: The widths are based on this node's type, but build vectors can
13853 // truncate their operands.
13854 SplatValue = APInt(VecWidth, 0);
13855 SplatUndef = APInt(VecWidth, 0);
13856
13857 // Get the bits. Bits with undefined values (when the corresponding element
13858 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13859 // in SplatValue. If any of the values are not constant, give up and return
13860 // false.
13861 unsigned int NumOps = getNumOperands();
13862 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13863 unsigned EltWidth = VT.getScalarSizeInBits();
13864
13865 for (unsigned j = 0; j < NumOps; ++j) {
13866 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13867 SDValue OpVal = getOperand(i);
13868 unsigned BitPos = j * EltWidth;
13869
13870 if (OpVal.isUndef())
13871 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13872 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13873 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13874 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13875 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13876 else
13877 return false;
13878 }
13879
13880 // The build_vector is all constants or undefs. Find the smallest element
13881 // size that splats the vector.
13882 HasAnyUndefs = (SplatUndef != 0);
13883
13884 // FIXME: This does not work for vectors with elements less than 8 bits.
13885 while (VecWidth > 8) {
13886 // If we can't split in half, stop here.
13887 if (VecWidth & 1)
13888 break;
13889
13890 unsigned HalfSize = VecWidth / 2;
13891 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13892 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13893 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13894 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13895
13896 // If the two halves do not match (ignoring undef bits), stop here.
13897 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13898 MinSplatBits > HalfSize)
13899 break;
13900
13901 SplatValue = HighValue | LowValue;
13902 SplatUndef = HighUndef & LowUndef;
13903
13904 VecWidth = HalfSize;
13905 }
13906
13907 // FIXME: The loop above only tries to split in halves. But if the input
13908 // vector for example is <3 x i16> it wouldn't be able to detect a
13909 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13910 // optimizations. I guess that back in the days when this helper was created
13911 // vectors normally was power-of-2 sized.
13912
13913 SplatBitSize = VecWidth;
13914 return true;
13915}
13916
13918 BitVector *UndefElements) const {
13919 unsigned NumOps = getNumOperands();
13920 if (UndefElements) {
13921 UndefElements->clear();
13922 UndefElements->resize(NumOps);
13923 }
13924 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13925 if (!DemandedElts)
13926 return SDValue();
13927 SDValue Splatted;
13928 for (unsigned i = 0; i != NumOps; ++i) {
13929 if (!DemandedElts[i])
13930 continue;
13931 SDValue Op = getOperand(i);
13932 if (Op.isUndef()) {
13933 if (UndefElements)
13934 (*UndefElements)[i] = true;
13935 } else if (!Splatted) {
13936 Splatted = Op;
13937 } else if (Splatted != Op) {
13938 return SDValue();
13939 }
13940 }
13941
13942 if (!Splatted) {
13943 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13944 assert(getOperand(FirstDemandedIdx).isUndef() &&
13945 "Can only have a splat without a constant for all undefs.");
13946 return getOperand(FirstDemandedIdx);
13947 }
13948
13949 return Splatted;
13950}
13951
13953 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13954 return getSplatValue(DemandedElts, UndefElements);
13955}
13956
13958 SmallVectorImpl<SDValue> &Sequence,
13959 BitVector *UndefElements) const {
13960 unsigned NumOps = getNumOperands();
13961 Sequence.clear();
13962 if (UndefElements) {
13963 UndefElements->clear();
13964 UndefElements->resize(NumOps);
13965 }
13966 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13967 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13968 return false;
13969
13970 // Set the undefs even if we don't find a sequence (like getSplatValue).
13971 if (UndefElements)
13972 for (unsigned I = 0; I != NumOps; ++I)
13973 if (DemandedElts[I] && getOperand(I).isUndef())
13974 (*UndefElements)[I] = true;
13975
13976 // Iteratively widen the sequence length looking for repetitions.
13977 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13978 Sequence.append(SeqLen, SDValue());
13979 for (unsigned I = 0; I != NumOps; ++I) {
13980 if (!DemandedElts[I])
13981 continue;
13982 SDValue &SeqOp = Sequence[I % SeqLen];
13984 if (Op.isUndef()) {
13985 if (!SeqOp)
13986 SeqOp = Op;
13987 continue;
13988 }
13989 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13990 Sequence.clear();
13991 break;
13992 }
13993 SeqOp = Op;
13994 }
13995 if (!Sequence.empty())
13996 return true;
13997 }
13998
13999 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
14000 return false;
14001}
14002
14004 BitVector *UndefElements) const {
14005 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14006 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
14007}
14008
14011 BitVector *UndefElements) const {
14013 getSplatValue(DemandedElts, UndefElements));
14014}
14015
14018 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
14019}
14020
14023 BitVector *UndefElements) const {
14025 getSplatValue(DemandedElts, UndefElements));
14026}
14027
14032
14033int32_t
14035 uint32_t BitWidth) const {
14036 if (ConstantFPSDNode *CN =
14038 bool IsExact;
14039 APSInt IntVal(BitWidth);
14040 const APFloat &APF = CN->getValueAPF();
14041 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
14042 APFloat::opOK ||
14043 !IsExact)
14044 return -1;
14045
14046 return IntVal.exactLogBase2();
14047 }
14048 return -1;
14049}
14050
14052 bool IsLittleEndian, unsigned DstEltSizeInBits,
14053 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
14054 // Early-out if this contains anything but Undef/Constant/ConstantFP.
14055 if (!isConstant())
14056 return false;
14057
14058 unsigned NumSrcOps = getNumOperands();
14059 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
14060 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14061 "Invalid bitcast scale");
14062
14063 // Extract raw src bits.
14064 SmallVector<APInt> SrcBitElements(NumSrcOps,
14065 APInt::getZero(SrcEltSizeInBits));
14066 BitVector SrcUndeElements(NumSrcOps, false);
14067
14068 for (unsigned I = 0; I != NumSrcOps; ++I) {
14070 if (Op.isUndef()) {
14071 SrcUndeElements.set(I);
14072 continue;
14073 }
14074 auto *CInt = dyn_cast<ConstantSDNode>(Op);
14075 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
14076 assert((CInt || CFP) && "Unknown constant");
14077 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
14078 : CFP->getValueAPF().bitcastToAPInt();
14079 }
14080
14081 // Recast to dst width.
14082 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
14083 SrcBitElements, UndefElements, SrcUndeElements);
14084 return true;
14085}
14086
14087void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
14088 unsigned DstEltSizeInBits,
14089 SmallVectorImpl<APInt> &DstBitElements,
14090 ArrayRef<APInt> SrcBitElements,
14091 BitVector &DstUndefElements,
14092 const BitVector &SrcUndefElements) {
14093 unsigned NumSrcOps = SrcBitElements.size();
14094 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
14095 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14096 "Invalid bitcast scale");
14097 assert(NumSrcOps == SrcUndefElements.size() &&
14098 "Vector size mismatch");
14099
14100 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
14101 DstUndefElements.clear();
14102 DstUndefElements.resize(NumDstOps, false);
14103 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
14104
14105 // Concatenate src elements constant bits together into dst element.
14106 if (SrcEltSizeInBits <= DstEltSizeInBits) {
14107 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
14108 for (unsigned I = 0; I != NumDstOps; ++I) {
14109 DstUndefElements.set(I);
14110 APInt &DstBits = DstBitElements[I];
14111 for (unsigned J = 0; J != Scale; ++J) {
14112 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14113 if (SrcUndefElements[Idx])
14114 continue;
14115 DstUndefElements.reset(I);
14116 const APInt &SrcBits = SrcBitElements[Idx];
14117 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
14118 "Illegal constant bitwidths");
14119 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
14120 }
14121 }
14122 return;
14123 }
14124
14125 // Split src element constant bits into dst elements.
14126 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14127 for (unsigned I = 0; I != NumSrcOps; ++I) {
14128 if (SrcUndefElements[I]) {
14129 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14130 continue;
14131 }
14132 const APInt &SrcBits = SrcBitElements[I];
14133 for (unsigned J = 0; J != Scale; ++J) {
14134 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14135 APInt &DstBits = DstBitElements[Idx];
14136 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14137 }
14138 }
14139}
14140
14142 for (const SDValue &Op : op_values()) {
14143 unsigned Opc = Op.getOpcode();
14144 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14145 return false;
14146 }
14147 return true;
14148}
14149
14150std::optional<std::pair<APInt, APInt>>
14152 unsigned NumOps = getNumOperands();
14153 if (NumOps < 2)
14154 return std::nullopt;
14155
14158 return std::nullopt;
14159
14160 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14161 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
14162 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
14163
14164 if (Stride.isZero())
14165 return std::nullopt;
14166
14167 for (unsigned i = 2; i < NumOps; ++i) {
14169 return std::nullopt;
14170
14171 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
14172 if (Val != (Start + (Stride * i)))
14173 return std::nullopt;
14174 }
14175
14176 return std::make_pair(Start, Stride);
14177}
14178
14180 // Find the first non-undef value in the shuffle mask.
14181 unsigned i, e;
14182 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14183 /* search */;
14184
14185 // If all elements are undefined, this shuffle can be considered a splat
14186 // (although it should eventually get simplified away completely).
14187 if (i == e)
14188 return true;
14189
14190 // Make sure all remaining elements are either undef or the same as the first
14191 // non-undef value.
14192 for (int Idx = Mask[i]; i != e; ++i)
14193 if (Mask[i] >= 0 && Mask[i] != Idx)
14194 return false;
14195 return true;
14196}
14197
14198// Returns true if it is a constant integer BuildVector or constant integer,
14199// possibly hidden by a bitcast.
14201 SDValue N, bool AllowOpaques) const {
14203
14204 if (auto *C = dyn_cast<ConstantSDNode>(N))
14205 return AllowOpaques || !C->isOpaque();
14206
14208 return true;
14209
14210 // Treat a GlobalAddress supporting constant offset folding as a
14211 // constant integer.
14212 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14213 if (GA->getOpcode() == ISD::GlobalAddress &&
14214 TLI->isOffsetFoldingLegal(GA))
14215 return true;
14216
14217 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14218 isa<ConstantSDNode>(N.getOperand(0)))
14219 return true;
14220 return false;
14221}
14222
14223// Returns true if it is a constant float BuildVector or constant float.
14226 return true;
14227
14229 return true;
14230
14231 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14232 isa<ConstantFPSDNode>(N.getOperand(0)))
14233 return true;
14234
14235 return false;
14236}
14237
14238std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14239 ConstantSDNode *Const =
14240 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14241 if (!Const)
14242 return std::nullopt;
14243
14244 EVT VT = N->getValueType(0);
14245 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14246 switch (TLI->getBooleanContents(N.getValueType())) {
14248 if (CVal.isOne())
14249 return true;
14250 if (CVal.isZero())
14251 return false;
14252 return std::nullopt;
14254 if (CVal.isAllOnes())
14255 return true;
14256 if (CVal.isZero())
14257 return false;
14258 return std::nullopt;
14260 return CVal[0];
14261 }
14262 llvm_unreachable("Unknown BooleanContent enum");
14263}
14264
14265void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14266 assert(!Node->OperandList && "Node already has operands");
14268 "too many operands to fit into SDNode");
14269 SDUse *Ops = OperandRecycler.allocate(
14270 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14271
14272 bool IsDivergent = false;
14273 for (unsigned I = 0; I != Vals.size(); ++I) {
14274 Ops[I].setUser(Node);
14275 Ops[I].setInitial(Vals[I]);
14276 EVT VT = Ops[I].getValueType();
14277
14278 // Skip Chain. It does not carry divergence.
14279 if (VT != MVT::Other &&
14280 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14281 Ops[I].getNode()->isDivergent()) {
14282 IsDivergent = true;
14283 }
14284 }
14285 Node->NumOperands = Vals.size();
14286 Node->OperandList = Ops;
14287 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14288 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14289 Node->SDNodeBits.IsDivergent = IsDivergent;
14290 }
14291 checkForCycles(Node);
14292}
14293
14296 size_t Limit = SDNode::getMaxNumOperands();
14297 while (Vals.size() > Limit) {
14298 unsigned SliceIdx = Vals.size() - Limit;
14299 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14300 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14301 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14302 Vals.emplace_back(NewTF);
14303 }
14304 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14305}
14306
14308 EVT VT, SDNodeFlags Flags) {
14309 switch (Opcode) {
14310 default:
14311 return SDValue();
14312 case ISD::ADD:
14313 case ISD::OR:
14314 case ISD::XOR:
14315 case ISD::UMAX:
14316 return getConstant(0, DL, VT);
14317 case ISD::MUL:
14318 return getConstant(1, DL, VT);
14319 case ISD::AND:
14320 case ISD::UMIN:
14321 return getAllOnesConstant(DL, VT);
14322 case ISD::SMAX:
14324 case ISD::SMIN:
14326 case ISD::FADD:
14327 // If flags allow, prefer positive zero since it's generally cheaper
14328 // to materialize on most targets.
14329 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14330 case ISD::FMUL:
14331 return getConstantFP(1.0, DL, VT);
14332 case ISD::FMINNUM:
14333 case ISD::FMAXNUM: {
14334 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14335 const fltSemantics &Semantics = VT.getFltSemantics();
14336 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14337 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14338 APFloat::getLargest(Semantics);
14339 if (Opcode == ISD::FMAXNUM)
14340 NeutralAF.changeSign();
14341
14342 return getConstantFP(NeutralAF, DL, VT);
14343 }
14344 case ISD::FMINIMUM:
14345 case ISD::FMAXIMUM: {
14346 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14347 const fltSemantics &Semantics = VT.getFltSemantics();
14348 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14349 : APFloat::getLargest(Semantics);
14350 if (Opcode == ISD::FMAXIMUM)
14351 NeutralAF.changeSign();
14352
14353 return getConstantFP(NeutralAF, DL, VT);
14354 }
14355
14356 }
14357}
14358
14359/// Helper used to make a call to a library function that has one argument of
14360/// pointer type.
14361///
14362/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14363/// used to get or set floating-point state. They have one argument of pointer
14364/// type, which points to the memory region containing bits of the
14365/// floating-point state. The value returned by such function is ignored in the
14366/// created call.
14367///
14368/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14369/// \param Ptr Pointer used to save/load state.
14370/// \param InChain Ingoing token chain.
14371/// \returns Outgoing chain token.
14373 SDValue InChain,
14374 const SDLoc &DLoc) {
14375 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14377 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14378 RTLIB::LibcallImpl LibcallImpl =
14379 Libcalls->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
14380 if (LibcallImpl == RTLIB::Unsupported)
14381 reportFatalUsageError("emitting call to unsupported libcall");
14382
14383 SDValue Callee =
14384 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
14386 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14387 Libcalls->getLibcallImplCallingConv(LibcallImpl),
14388 Type::getVoidTy(*getContext()), Callee, std::move(Args));
14389 return TLI->LowerCallTo(CLI).second;
14390}
14391
14393 assert(From && To && "Invalid SDNode; empty source SDValue?");
14394 auto I = SDEI.find(From);
14395 if (I == SDEI.end())
14396 return;
14397
14398 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14399 // the iterator, hence the need to make a copy to prevent a use-after-free.
14400 NodeExtraInfo NEI = I->second;
14401 if (LLVM_LIKELY(!NEI.PCSections)) {
14402 // No deep copy required for the types of extra info set.
14403 //
14404 // FIXME: Investigate if other types of extra info also need deep copy. This
14405 // depends on the types of nodes they can be attached to: if some extra info
14406 // is only ever attached to nodes where a replacement To node is always the
14407 // node where later use and propagation of the extra info has the intended
14408 // semantics, no deep copy is required.
14409 SDEI[To] = std::move(NEI);
14410 return;
14411 }
14412
14413 const SDNode *EntrySDN = getEntryNode().getNode();
14414
14415 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14416 // through the replacement of From with To. Otherwise, replacements of a node
14417 // (From) with more complex nodes (To and its operands) may result in lost
14418 // extra info where the root node (To) is insignificant in further propagating
14419 // and using extra info when further lowering to MIR.
14420 //
14421 // In the first step pre-populate the visited set with the nodes reachable
14422 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14423 // DAG that is not new and should be left untouched.
14424 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14425 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14426 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14427 if (MaxDepth == 0) {
14428 // Remember this node in case we need to increase MaxDepth and continue
14429 // populating FromReach from this node.
14430 Leafs.emplace_back(N);
14431 return;
14432 }
14433 if (!FromReach.insert(N).second)
14434 return;
14435 for (const SDValue &Op : N->op_values())
14436 Self(Self, Op.getNode(), MaxDepth - 1);
14437 };
14438
14439 // Copy extra info to To and all its transitive operands (that are new).
14441 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14442 if (FromReach.contains(N))
14443 return true;
14444 if (!Visited.insert(N).second)
14445 return true;
14446 if (EntrySDN == N)
14447 return false;
14448 for (const SDValue &Op : N->op_values()) {
14449 if (N == To && Op.getNode() == EntrySDN) {
14450 // Special case: New node's operand is the entry node; just need to
14451 // copy extra info to new node.
14452 break;
14453 }
14454 if (!Self(Self, Op.getNode()))
14455 return false;
14456 }
14457 // Copy only if entry node was not reached.
14458 SDEI[N] = NEI;
14459 return true;
14460 };
14461
14462 // We first try with a lower MaxDepth, assuming that the path to common
14463 // operands between From and To is relatively short. This significantly
14464 // improves performance in the common case. The initial MaxDepth is big
14465 // enough to avoid retry in the common case; the last MaxDepth is large
14466 // enough to avoid having to use the fallback below (and protects from
14467 // potential stack exhaustion from recursion).
14468 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14469 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14470 // StartFrom is the previous (or initial) set of leafs reachable at the
14471 // previous maximum depth.
14473 std::swap(StartFrom, Leafs);
14474 for (const SDNode *N : StartFrom)
14475 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14476 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14477 return;
14478 // This should happen very rarely (reached the entry node).
14479 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14480 assert(!Leafs.empty());
14481 }
14482
14483 // This should not happen - but if it did, that means the subgraph reachable
14484 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14485 // could not visit all reachable common operands. Consequently, we were able
14486 // to reach the entry node.
14487 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14488 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14489 // Best-effort fallback if assertions disabled.
14490 SDEI[To] = std::move(NEI);
14491}
14492
14493#ifndef NDEBUG
14494static void checkForCyclesHelper(const SDNode *N,
14497 const llvm::SelectionDAG *DAG) {
14498 // If this node has already been checked, don't check it again.
14499 if (Checked.count(N))
14500 return;
14501
14502 // If a node has already been visited on this depth-first walk, reject it as
14503 // a cycle.
14504 if (!Visited.insert(N).second) {
14505 errs() << "Detected cycle in SelectionDAG\n";
14506 dbgs() << "Offending node:\n";
14507 N->dumprFull(DAG); dbgs() << "\n";
14508 abort();
14509 }
14510
14511 for (const SDValue &Op : N->op_values())
14512 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14513
14514 Checked.insert(N);
14515 Visited.erase(N);
14516}
14517#endif
14518
14520 const llvm::SelectionDAG *DAG,
14521 bool force) {
14522#ifndef NDEBUG
14523 bool check = force;
14524#ifdef EXPENSIVE_CHECKS
14525 check = true;
14526#endif // EXPENSIVE_CHECKS
14527 if (check) {
14528 assert(N && "Checking nonexistent SDNode");
14531 checkForCyclesHelper(N, visited, checked, DAG);
14532 }
14533#endif // !NDEBUG
14534}
14535
14536void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14537 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14538}
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 srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1747
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1151
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:996
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1376
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:874
LLVM_ABI APInt byteSwap() const
Definition APInt.cpp:746
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1258
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
static bool isSameValue(const APInt &I1, const APInt &I2)
Determine if two APInts have the same value, after zero-extending one of them (if needed!...
Definition APInt.h:554
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition APInt.h:1426
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1397
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:482
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1238
bool isOne() const
Determine if this is a value of 1.
Definition APInt.h:390
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:852
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1222
LLVM_ABI APInt ssub_sat(const APInt &RHS) const
Definition APInt.cpp:2056
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
unsigned getSrcAddressSpace() const
unsigned getDestAddressSpace() const
static Capacity get(size_t N)
Get the capacity of an array that can hold at least N elements.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
This is an SDNode representing atomic operations.
static LLVM_ABI BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
BitVector & reset()
Definition BitVector.h:411
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition BitVector.h:360
void clear()
clear - Removes all bits from the bitvector.
Definition BitVector.h:354
BitVector & set()
Definition BitVector.h:370
bool none() const
none - Returns true if none of the bits are set.
Definition BitVector.h:207
size_type size() const
size - Returns the number of bits in this bitvector.
Definition BitVector.h:178
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition Constants.h:904
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
LLVM_ABI bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &RawBitElements, BitVector &UndefElements) const
Extract the raw bit data from a build vector of Undef, Constant or ConstantFP node elements.
static LLVM_ABI void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &DstBitElements, ArrayRef< APInt > SrcBitElements, BitVector &DstUndefElements, const BitVector &SrcUndefElements)
Recast bit data SrcBitElements to DstEltSizeInBits wide elements.
LLVM_ABI bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
LLVM_ABI ConstantFPSDNode * getConstantFPSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant FP or null if this is not a constant FP splat.
LLVM_ABI std::optional< std::pair< APInt, APInt > > isConstantSequence() const
If this BuildVector is constant and represents the numerical series "<a, a+n, a+2n,...
LLVM_ABI SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
LLVM_ABI bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, unsigned &SplatBitSize, bool &HasAnyUndefs, unsigned MinSplatBits=0, bool isBigEndian=false) const
Check if this is a constant splat, and if so, find the smallest element size that splats the vector.
LLVM_ABI ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
LLVM_ABI int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements, uint32_t BitWidth) const
If this is a constant FP splat and the splatted constant FP is an exact power or 2,...
LLVM_ABI bool isConstant() const
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
static LLVM_ABI bool isValueValidForType(EVT VT, const APFloat &Val)
const APFloat & getValueAPF() const
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:282
const APFloat & getValue() const
Definition Constants.h:326
This is the shared class of boolean and integer constants.
Definition Constants.h:87
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:162
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
MachineConstantPoolValue * getMachineCPVal() const
const Constant * getConstVal() const
LLVM_ABI Type * getType() const
This class represents a range of values.
LLVM_ABI ConstantRange multiply(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
uint64_t getZExtValue() const
const APInt & getAPIntValue() const
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
DWARF expression.
static LLVM_ABI ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
Base class for variables.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:214
LLVM_ABI IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
LLVM_ABI Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
A debug info location.
Definition DebugLoc.h:123
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:209
Data structure describing the variable locations in a function.
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h: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:1079
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1443
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
Abstract base class for all machine specific constantpool value subclasses.
virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID)=0
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
const MDNode * getRanges() const
Return the range tag for the memory reference.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
This class contains meta information specific to a module.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
LLVM_ABI MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt, MachineMemOperand *MMO)
MachineMemOperand * MMO
Memory reference information.
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
unsigned getRawSubclassData() const
Return the SubclassData value, without HasDebugValue.
EVT getMemoryVT() const
Return the type of the in-memory value.
Representation for a specific memory location.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition Module.cpp:235
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:298
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
Class to represent pointers.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
unsigned getAddressSpace() const
Return the address space of the Pointer type.
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Analysis providing profile information.
void Deallocate(SubClass *E)
Deallocate - Release storage for the pointed-to object.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Keeps track of dbg_value information through SDISel.
LLVM_ABI void add(SDDbgValue *V, bool isParameter)
LLVM_ABI void erase(const SDNode *Node)
Invalidate all DbgValues attached to the node and remove it from the Node-to-DbgValues map.
Holds the information from a dbg_label node through SDISel.
Holds the information for a single machine location through SDISel; either an SDNode,...
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(Register VReg)
static SDDbgOperand fromConst(const Value *Const)
@ SDNODE
Value is the result of an expression.
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
const DebugLoc & getDebugLoc() const
unsigned getIROrder() const
This class provides iterator support for SDUse operands that use a specific SDNode.
Represents one node in the SelectionDAG.
ArrayRef< SDUse > ops() const
const APInt & getAsAPIntVal() const
Helper method returns the APInt value of a ConstantSDNode.
LLVM_ABI void dumprFull(const SelectionDAG *G=nullptr) const
printrFull to dbgs().
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
LLVM_ABI bool isOnlyUserOf(const SDNode *N) const
Return true if this node is the only use of N.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
static constexpr size_t getMaxNumOperands()
Return the maximum number of operands that a SDNode can hold.
iterator_range< use_iterator > uses()
MemSDNodeBitfields MemSDNodeBits
LLVM_ABI void Profile(FoldingSetNodeID &ID) const
Gather unique data for the node.
bool getHasDebugValue() const
SDNodeFlags getFlags() const
void setNodeId(int Id)
Set unique node id.
LLVM_ABI void intersectFlagsWith(const SDNodeFlags Flags)
Clear any flags in this node that aren't also set in Flags.
static bool hasPredecessorHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallVectorImpl< const SDNode * > &Worklist, unsigned int MaxSteps=0, bool TopologicalPrune=false)
Returns true if N is a predecessor of any node in Worklist.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
bool use_empty() const
Return true if there are no uses of this node.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
static LLVM_ABI bool areOnlyUsersOf(ArrayRef< const SDNode * > Nodes, const SDNode *N)
Return true if all the users of N are contained in Nodes.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
const APInt & getConstantOperandAPInt(unsigned Num) const
Helper method returns the APInt of a ConstantSDNode operand.
std::optional< APInt > bitcastToAPInt() const
LLVM_ABI bool hasPredecessor(const SDNode *N) const
Return true if N is a predecessor of this node.
LLVM_ABI bool hasAnyUseOfValue(unsigned Value) const
Return true if there are any use of the indicated value.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
bool isUndef() const
Returns true if the node type is UNDEF or POISON.
op_iterator op_end() const
op_iterator op_begin() const
static use_iterator use_end()
LLVM_ABI void DropOperands()
Release the operands and set this node to have zero operands.
SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
Create an SDNode.
Represents a use of a SDNode.
SDNode * getUser()
This returns the SDNode that contains this Use.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if the referenced return value is an operand of N.
SDValue()=default
LLVM_ABI bool reachesChainWithoutSideEffects(SDValue Dest, unsigned Depth=2) const
Return true if this operand (which must be a chain) reaches the specified operand without crossing an...
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
bool use_empty() const
Return true if there are no nodes using value ResNo of Node.
const APInt & getConstantOperandAPInt(unsigned i) const
uint64_t getScalarValueSizeInBits() const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
uint64_t getConstantOperandVal(unsigned i) const
unsigned getOpcode() const
virtual void verifyTargetNode(const SelectionDAG &DAG, const SDNode *N) const
Checks that the given target-specific node is valid. Aborts if it is not.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC)
LLVM_ABI Align getReducedAlign(EVT VT, bool UseABI)
In most cases this function returns the ABI alignment for a given type, except for illegal vector typ...
LLVM_ABI SDValue getVPZeroExtendInReg(SDValue Op, SDValue Mask, SDValue EVL, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
LLVM_ABI SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsExpanding=false)
SDValue getExtractVectorElt(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Extract element at Idx from Vec.
LLVM_ABI SDValue getSplatSourceVector(SDValue V, int &SplatIndex)
If V is a splatted value, return the source vector and its splat index.
LLVM_ABI SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI OverflowKind computeOverflowForUnsignedSub(SDValue N0, SDValue N1) const
Determine if the result of the unsigned sub of 2 nodes can overflow.
LLVM_ABI unsigned ComputeMaxSignificantBits(SDValue Op, unsigned Depth=0) const
Get the upper bound on bit size for this Value Op as a signed integer.
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
LLVM_ABI std::pair< SDValue, SDValue > getStrlen(SDValue Chain, const SDLoc &dl, SDValue Src, const CallInst *CI)
Lower a strlen operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
LLVM_ABI SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
bool isKnownNeverSNaN(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
LLVM_ABI std::optional< bool > isBoolConstant(SDValue N) const
Check if a value \op N is a constant using the target's BooleanContent for its type.
LLVM_ABI SDValue getStackArgumentTokenFactor(SDValue Chain)
Compute a TokenFactor to force all the incoming stack arguments to be loaded from the stack.
const TargetSubtargetInfo & getSubtarget() const
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI void updateDivergence(SDNode *N)
LLVM_ABI SDValue getSplatValue(SDValue V, bool LegalTypes=false)
If V is a splat vector, return its scalar source operand by extracting that element from the source v...
LLVM_ABI SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl)
Constant fold a setcc to true or false.
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) neutral element for the given opcode, if it exists.
LLVM_ABI SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
LLVM_ABI SDValue getAtomicLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT MemVT, EVT VT, SDValue Chain, SDValue Ptr, MachineMemOperand *MMO)
LLVM_ABI SDNode * getNodeIfExists(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops, const SDNodeFlags Flags, bool AllowCommute=false)
Get the specified node if it's already available, or else return NULL.
LLVM_ABI SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid, uint64_t Index, uint32_t Attr)
Creates a PseudoProbeSDNode with function GUID Guid and the index of the block Index it is probing,...
LLVM_ABI SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
LLVM_ABI SDNode * SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT)
These are used for target selectors to mutate the specified node to have the specified return type,...
LLVM_ABI void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, const LibcallLoweringInfo *LibcallsInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, MachineModuleInfo &MMI, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
LLVM_ABI SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
LLVM_ABI SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBitcastedSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
Gets a node for an atomic cmpxchg op.
LLVM_ABI SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain)
If an existing load has uses of its chain, create a token factor node with that chain and the new mem...
LLVM_ABI bool isConstantIntBuildVectorOrConstantInt(SDValue N, bool AllowOpaques=true) const
Test whether the given value is a constant int or similar node.
LLVM_ABI void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
LLVM_ABI SDValue getJumpTableDebugInfo(int JTI, SDValue Chain, const SDLoc &DL)
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false)
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
LLVM_ABI std::optional< unsigned > getValidMaximumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
LLVM_ABI SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
OverflowKind
Used to represent the possible overflow behavior of an operation.
static LLVM_ABI unsigned getHasPredecessorMaxSteps()
LLVM_ABI bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
SDValue getExtractSubvector(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Return the VT typed sub-vector of Vec at Idx.
LLVM_ABI bool cannotBeOrderedNegativeFP(SDValue Op) const
Test whether the given float value is known to be positive.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI bool calculateDivergence(SDNode *N)
LLVM_ABI SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
LLVM_ABI SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI bool canIgnoreSignBitOfZero(const SDUse &Use) const
Check if a use of a float value is insensitive to signed zeros.
LLVM_ABI bool SignBitIsZeroFP(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero, for a floating-point value.
LLVM_ABI SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=LocationSize::precise(0), const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
SDValue getInsertSubvector(const SDLoc &DL, SDValue Vec, SDValue SubVec, unsigned Idx)
Insert SubVec at the Idx element of Vec.
LLVM_ABI SDValue getBitcastedZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
LLVM_ABI SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
LLVM_ABI SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
LLVM_ABI SDValue getVPZExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be an integer vector, to the vector-type VT,...
const TargetLowering & getTargetLoweringInfo() const
LLVM_ABI bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
LLVM_ABI SDValue getStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
bool isGuaranteedNotToBePoison(SDValue Op, unsigned Depth=0) const
Return true if this function can prove that Op is never poison.
LLVM_ABI SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
LLVM_ABI SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI void dump(bool Sorted=false) const
Dump the textual format of this DAG.
LLVM_ABI APInt computeVectorKnownZeroElements(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
For each demanded element of a vector, see if it is known to be zero.
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
bool NewNodesMustHaveLegalTypes
When true, additional steps are taken to ensure that getConstant() and similar functions return DAG n...
LLVM_ABI std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
LLVM_ABI void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
LLVM_ABI SDNode * MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, ArrayRef< SDValue > Ops)
This mutates the specified node to have the specified return type, opcode, and operands.
LLVM_ABI std::pair< SDValue, SDValue > UnrollVectorOverflowOp(SDNode *N, unsigned ResNE=0)
Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
allnodes_const_iterator allnodes_begin() const
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
LLVM_ABI SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcastedAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts, unsigned Depth=0) const
Test whether V has a splatted value for all the demanded elements.
LLVM_ABI void DeleteNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
LLVM_ABI std::pair< SDValue, SDValue > getStrcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, const CallInst *CI)
Lower a strcpy operation into a target library call and return the resulting chain and call result as...
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
LLVM_ABI SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
LLVM_ABI std::optional< unsigned > getValidShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has a uniform shift amount that is less than the element bit-width of the shi...
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
LLVM_ABI SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
LLVM_ABI SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI bool isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
const DataLayout & getDataLayout() const
LLVM_ABI SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
LLVM_ABI SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
LLVM_ABI bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops)
Check if a node exists without modifying its flags.
const SelectionDAGTargetInfo & getSelectionDAGInfo() const
LLVM_ABI bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, unsigned Bytes, int Dist) const
Return true if loads are next to each other and can be merged.
LLVM_ABI SDValue getMaskedHistogram(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
LLVM_ABI SDValue getStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI OverflowKind computeOverflowForUnsignedMul(SDValue N0, SDValue N1) const
Determine if the result of the unsigned mul of 2 nodes can overflow.
LLVM_ABI void copyExtraInfo(SDNode *From, SDNode *To)
Copy extra info associated with one node to another.
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue SV, unsigned Align)
VAArg produces a result and token chain, and takes a pointer and a source value as input.
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getLoadFFVP(EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachineMemOperand *MMO)
LLVM_ABI SDValue getTypeSize(const SDLoc &DL, EVT VT, TypeSize TS)
LLVM_ABI SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
LLVM_ABI void clear()
Clear state and free memory necessary to make this SelectionDAG ready to process a new block.
LLVM_ABI std::pair< SDValue, SDValue > getMemcmp(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, const CallInst *CI)
Lower a memcmp operation into a target library call and return the resulting chain and call result as...
LLVM_ABI void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
LLVM_ABI SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV)
Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to the shuffle node in input but with swa...
LLVM_ABI std::pair< SDValue, SDValue > SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the vector with EXTRACT_SUBVECTOR using the provided VTs and return the low/high part.
LLVM_ABI SDValue makeStateFunctionCall(unsigned LibFunc, SDValue Ptr, SDValue InChain, const SDLoc &DLoc)
Helper used to make a call to a library function that has one argument of pointer type.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, bool PoisonOnly=false, unsigned Depth=0) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
LLVM_ABI SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
LLVM_ABI SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI OverflowKind computeOverflowForSignedMul(SDValue N0, SDValue N1) const
Determine if the result of the signed mul of 2 nodes can overflow.
LLVM_ABI MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
LLVM_ABI bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if '(Op & Mask) == Mask'.
LLVM_ABI bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
LLVM_ABI void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
LLVM_ABI void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
bool isConstantValueOfAnyType(SDValue N) const
LLVM_ABI SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
LLVM_ABI SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, Register VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
LLVM_ABI bool isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
LLVM_ABI SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI bool isKnownNeverZero(SDValue Op, unsigned Depth=0) const
Test whether the given SDValue is known to contain non-zero value(s).
LLVM_ABI SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SDNodeFlags Flags=SDNodeFlags())
LLVM_ABI std::optional< unsigned > getValidMinimumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT)
Convert Op, which must be of integer type, to the integer type VT, by using an extension appropriate ...
LLVM_ABI SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
LLVM_ABI std::pair< SDValue, SDValue > getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT)
Convert Op, which must be a STRICT operation of float type, to the float type VT, by either extending...
LLVM_ABI std::pair< SDValue, SDValue > SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL)
Split the explicit vector length parameter of a VP operation.
LLVM_ABI SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
LLVM_ABI SDValue getVPLogicalNOT(const SDLoc &DL, SDValue Val, SDValue Mask, SDValue EVL, EVT VT)
Create a vector-predicated logical NOT operation as (VP_XOR Val, BooleanOne, Mask,...
LLVM_ABI SDValue getMaskFromElementCount(const SDLoc &DL, EVT VT, ElementCount Len)
Return a vector with the first 'Len' lanes set to true and remaining lanes set to false.
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
iterator_range< allnodes_iterator > allnodes()
LLVM_ABI SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI SDValue WidenVector(const SDValue &N, const SDLoc &DL)
Widen the vector up to the next power of two using INSERT_SUBVECTOR.
LLVM_ABI bool isKnownNeverZeroFloat(SDValue Op) const
Test whether the given floating point SDValue is known to never be positive or negative zero.
const LibcallLoweringInfo & getLibcalls() const
LLVM_ABI SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, const MDNode *Ranges=nullptr, bool IsExpanding=false)
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
LLVM_ABI SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the Fra...
ArrayRef< SDDbgValue * > GetDbgValues(const SDNode *SD) const
Get the debug values which reference the given SDNode.
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI OverflowKind computeOverflowForSignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the signed addition of 2 nodes can overflow.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
LLVM_ABI unsigned AssignTopologicalOrder()
Topological-sort the AllNodes list and a assign a unique node id for each node in the DAG based on th...
ilist< SDNode >::size_type allnodes_size() const
LLVM_ABI bool isKnownNeverNaN(SDValue Op, const APInt &DemandedElts, bool SNaN=false, unsigned Depth=0) const
Test whether the given SDValue (or all elements of it, if it is a vector) is known to never be NaN in...
LLVM_ABI SDValue FoldConstantBuildVector(BuildVectorSDNode *BV, const SDLoc &DL, EVT DstEltVT)
Fold BUILD_VECTOR of constants/undefs to the destination type BUILD_VECTOR of constants/undefs elemen...
LLVM_ABI SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsCompressing=false)
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits.
LLVM_ABI bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Return true if 'Op' is known to be zero in DemandedElts.
LLVM_ABI SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT)
Create a true or false constant of type VT using the target's BooleanContent for type OpVT.
LLVM_ABI SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
LLVM_ABI SDValue getExtStridedLoadVP(ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
LLVM_ABI SDValue getVPPtrExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be of integer type, to the vector-type integer type VT,...
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI void getTopologicallyOrderedNodes(SmallVectorImpl< const SDNode * > &SortedNodes) const
Get all the nodes in their topological order without modifying any states.
LLVM_ABI void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
LLVM_ABI std::pair< SDValue, SDValue > getStrstr(SDValue Chain, const SDLoc &dl, SDValue S0, SDValue S1, const CallInst *CI)
Lower a strstr operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to extend the Op as a pointer value assuming it was the smaller SrcTy ...
LLVM_ABI bool canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts, bool PoisonOnly=false, bool ConsiderFlags=true, unsigned Depth=0) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
LLVM_ABI OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT SVT, MachineMemOperand *MMO, bool IsCompressing=false)
LLVM_ABI void canonicalizeCommutativeBinop(unsigned Opcode, SDValue &N1, SDValue &N2) const
Swap N1 and N2 if Opcode is a commutative binary opcode and the canonical form expects the opposite o...
LLVM_ABI KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
LLVM_ABI SDValue getRegisterMask(const uint32_t *RegMask)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVM_ABI SDValue getCondCode(ISD::CondCode Cond)
LLVM_ABI bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
LLVM_ABI bool isKnownToBeAPowerOfTwoFP(SDValue Val, unsigned Depth=0) const
Test if the given fp value is known to be an integer power-of-2, either positive or negative.
LLVM_ABI OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset)
Create an add instruction with appropriate flags when used for addressing some offset of an object.
LLVMContext * getContext() const
LLVM_ABI SDValue simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y, SDNodeFlags Flags)
Try to simplify a floating-point binary operation into 1 of its operands or a constant.
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
LLVM_ABI SDValue getDeactivationSymbol(const GlobalValue *GV)
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
LLVM_ABI SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
LLVM_ABI bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
LLVM_ABI SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
LLVM_ABI SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
LLVM_ABI std::pair< EVT, EVT > GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT, bool *HiIsEmpty) const
Compute the VTs needed for the low/hi parts of a type, dependent on an enveloping VT that has been sp...
LLVM_ABI SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
LLVM_ABI std::optional< ConstantRange > getValidShiftAmountRange(SDValue V, const APInt &DemandedElts, unsigned Depth) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
LLVM_ABI SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
LLVM_ABI SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
LLVM_ABI SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, ISD::LoadExtType, bool IsExpanding=false)
SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op)
Returns a node representing a splat of one value into all lanes of the provided vector type.
LLVM_ABI std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
LLVM_ABI SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp, ArrayRef< ISD::NodeType > CandidateBinOps, bool AllowPartials=false)
Match a binop + shuffle pyramid that represents a horizontal reduction over the elements of a vector ...
LLVM_ABI bool isADDLike(SDValue Op, bool NoWrap=false) const
Return true if the specified operand is an ISD::OR or ISD::XOR node that can be treated as an ISD::AD...
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
LLVM_ABI SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
LLVM_ABI void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits=0, unsigned SizeInBits=0, bool InvalidateDbg=true)
Transfer debug values from one node to another, while optionally generating fragment expressions for ...
LLVM_ABI SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
LLVM_ABI SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, bool IsTruncating=false)
ilist< SDNode >::iterator allnodes_iterator
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
int getMaskElt(unsigned Idx) const
ArrayRef< int > getMask() const
static void commuteMask(MutableArrayRef< int > Mask)
Change values in a shuffle permute mask assuming the two vector operands have swapped position.
static LLVM_ABI bool isSplatMask(ArrayRef< int > Mask)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Completely target-dependent object reference.
unsigned getTargetFlags() const
Provides information about what library functions are available for the current target.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
const TargetMachine & getTargetMachine() const
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
unsigned getMaxStoresPerMemcpy(bool OptSize) const
Get maximum # of store operations permitted for llvm.memcpy.
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
virtual bool shallExtractConstSplatVectorElementToStore(Type *VectorTy, unsigned ElemSizeInBits, unsigned &Index) const
Return true if the target shall perform extract vector element and store given that the vector is kno...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
BooleanContent
Enum that describes how the target represents true/false values.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
std::vector< ArgListEntry > ArgListTy
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool findOptimalMemOpLowering(LLVMContext &Context, std::vector< EVT > &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS, unsigned SrcAS, const AttributeList &FuncAttributes) const
Determines the optimal series of memory ops to replace the memset / memcpy.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
Primary interface to the complete machine description for the target machine.
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition Triple.h:632
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:296
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:280
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:294
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:230
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:35
LLVM_ABI void set(Value *Val)
Definition Value.h:905
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
Value * getOperand(unsigned i) const
Definition User.h:207
This class is used to represent an VP_GATHER node.
This class is used to represent a VP_LOAD node.
This class is used to represent an VP_SCATTER node.
This class is used to represent a VP_STORE node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS.multiplyCoefficientBy(X) will result in a value whos...
Definition TypeSize.h:269
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr bool isKnownEven() const
A return value of true indicates we know at compile time that the number of elements (vscale * Min) i...
Definition TypeSize.h:176
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:252
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:237
A raw_ostream that writes to an std::string.
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt clmulr(const APInt &LHS, const APInt &RHS)
Perform a reversed carry-less multiply.
Definition APInt.cpp:3212
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition APInt.cpp:3142
LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition APInt.cpp:3129
LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition APInt.cpp:3119
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
Definition APInt.cpp:3193
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition APInt.cpp:3134
LLVM_ABI APInt clmul(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, also known as XOR multiplication, and return low-bits.
Definition APInt.cpp:3202
APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition APInt.h:2277
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
Definition APInt.cpp:3184
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3020
LLVM_ABI APInt clmulh(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, and return high-bits.
Definition APInt.cpp:3217
APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition APInt.h:2282
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition APInt.cpp:3114
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition APInt.cpp:3124
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
ISD namespace - This namespace contains an enum which represents all of the SelectionDAG node types a...
Definition ISDOpcodes.h:24
LLVM_ABI CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical AND between different comparisons of identical values: ((X op1 Y) & (X...
LLVM_ABI bool isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are ~0 ...
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h: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.
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2544
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:293
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
LLVM_ABI bool isOneOrOneSplatFP(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant floating-point value, or a splatted vector of a constant float...
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:303
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1706
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2198
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
auto cast_or_null(const Y &Val)
Definition Casting.h:714
LLVM_ABI bool isNullOrNullSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
Definition Utils.cpp:1595
LLVM_ABI bool isMinSignedConstant(SDValue V)
Returns true if V is a constant min signed integer value.
LLVM_ABI ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition APFloat.h:1618
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1744
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1661
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
LLVM_ABI SDValue peekThroughInsertVectorElt(SDValue V, const APInt &DemandedElts)
Recursively peek through INSERT_VECTOR_ELT nodes, returning the source vector operand of V,...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1634
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1692
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI SDValue peekThroughTruncates(SDValue V)
Return the non-truncated source operand of V if it exists.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1751
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI SDValue peekThroughOneUseBitcasts(SDValue V)
Return the non-bitcasted and one-use source operand of V if it exists.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:82
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool isOneOrOneSplat(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
Definition ModRef.h:68
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1642
@ Mul
Product of integers.
@ Sub
Subtraction of integers.
LLVM_ABI bool isNullConstantOrUndef(SDValue V)
Returns true if V is a constant integer zero or an UNDEF node.
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition Analysis.cpp:539
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI ConstantSDNode * isConstOrConstSplat(SDValue N, bool AllowUndefs=false, bool AllowTruncation=false)
Returns the SDNode if it is a constant splat BuildVector or constant int.
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1883
constexpr unsigned BitWidth
bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition Analysis.cpp:719
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isZeroOrZeroSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
LLVM_ABI bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1945
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI bool isNullFPConstant(SDValue V)
Returns true if V is an FP constant with a value of positive zero.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant (+/-)0.0 floating-point value or a splatted vector thereof (wi...
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1679
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1719
LLVM_ABI bool isOnesOrOnesSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo)
Returns true if V is a neutral element of Opc with Flags.
LLVM_ABI bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition MathExtras.h:373
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:762
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition Metadata.h:782
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:779
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
void move(uint64_t Delta)
Moves the Offset and adjusts Length accordingly.
const ConstantDataArray * Array
ConstantDataArray pointer.
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:395
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:137
intptr_t getRawBits() const
Definition ValueTypes.h:512
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:74
EVT changeTypeToInteger() const
Return the type converted to an equivalently sized integer or vector with integer element type.
Definition ValueTypes.h:121
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:284
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:300
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:147
ElementCount getVectorElementCount() const
Definition ValueTypes.h:350
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:373
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:359
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:385
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:316
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:65
bool isFixedLengthVector() const
Definition ValueTypes.h:181
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:323
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:292
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:256
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:328
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition ValueTypes.h:142
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:336
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition ValueTypes.h:308
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:453
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:152
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:314
LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:268
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:127
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:255
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:287
static LLVM_ABI std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
void makeNegative()
Make this value negative.
Definition KnownBits.h:122
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:164
KnownBits byteSwap() const
Definition KnownBits.h:532
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:302
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:536
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:246
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:175
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:334
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
static LLVM_ABI KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:238
static LLVM_ABI KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:324
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:183
static LLVM_ABI KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:199
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:148
static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:60
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition KnownBits.h:114
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:339
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
LLVM_ABI KnownBits truncSSat(unsigned BitWidth) const
Truncate with signed saturation (signed input -> signed output)
static LLVM_ABI KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)
Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
Definition KnownBits.cpp:53
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:293
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:232
static LLVM_ABI KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:170
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
LLVM_ABI KnownBits truncUSat(unsigned BitWidth) const
Truncate with unsigned saturation (unsigned input -> unsigned output)
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
LLVM_ABI KnownBits truncSSatU(unsigned BitWidth) const
Truncate with signed saturation to unsigned (signed input -> unsigned output)
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
This class contains a discriminated union of information about pointers in memory operands,...
LLVM_ABI bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
LLVM_ABI unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
MachinePointerInfo getWithOffset(int64_t O) const
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
static MemOp Set(uint64_t Size, bool DstAlignCanChange, Align DstAlign, bool IsZeroMemset, bool IsVolatile)
static MemOp Copy(uint64_t Size, bool DstAlignCanChange, Align DstAlign, Align SrcAlign, bool IsVolatile, bool MemcpyStrSrc=false)
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.
These are IR-level optimization flags that may be propagated to SDNodes.
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
unsigned int NumVTs
Clients of various APIs that cause global effects on the DAG can optionally implement this interface.
virtual void NodeDeleted(SDNode *N, SDNode *E)
The node N that was deleted and, if E is not null, an equivalent node E that replaced it.
virtual void NodeInserted(SDNode *N)
The node N that was inserted.
virtual void NodeUpdated(SDNode *N)
The node N that was updated.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setLibCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList)
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setChain(SDValue InChain)