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"
74#include <algorithm>
75#include <cassert>
76#include <cstdint>
77#include <cstdlib>
78#include <limits>
79#include <optional>
80#include <string>
81#include <utility>
82#include <vector>
83
84using namespace llvm;
85using namespace llvm::SDPatternMatch;
86
87/// makeVTList - Return an instance of the SDVTList struct initialized with the
88/// specified members.
89static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
90 SDVTList Res = {VTs, NumVTs};
91 return Res;
92}
93
94// Default null implementations of the callbacks.
98
99void SelectionDAG::DAGNodeDeletedListener::anchor() {}
100void SelectionDAG::DAGNodeInsertedListener::anchor() {}
101
102#define DEBUG_TYPE "selectiondag"
103
104static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
105 cl::Hidden, cl::init(true),
106 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
107
108static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
109 cl::desc("Number limit for gluing ld/st of memcpy."),
110 cl::Hidden, cl::init(0));
111
113 MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192),
114 cl::desc("DAG combiner limit number of steps when searching DAG "
115 "for predecessor nodes"));
116
118 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
119}
120
122
123//===----------------------------------------------------------------------===//
124// ConstantFPSDNode Class
125//===----------------------------------------------------------------------===//
126
127/// isExactlyValue - We don't rely on operator== working on double values, as
128/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
129/// As such, this method can be used to do an exact bit-for-bit comparison of
130/// two floating point values.
132 return getValueAPF().bitwiseIsEqual(V);
133}
134
136 const APFloat& Val) {
137 assert(VT.isFloatingPoint() && "Can only convert between FP types");
138
139 // convert modifies in place, so make a copy.
140 APFloat Val2 = APFloat(Val);
141 bool losesInfo;
143 &losesInfo);
144 return !losesInfo;
145}
146
147//===----------------------------------------------------------------------===//
148// ISD Namespace
149//===----------------------------------------------------------------------===//
150
151bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
152 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
153 if (auto OptAPInt = N->getOperand(0)->bitcastToAPInt()) {
154 unsigned EltSize =
155 N->getValueType(0).getVectorElementType().getSizeInBits();
156 SplatVal = OptAPInt->trunc(EltSize);
157 return true;
158 }
159 }
160
161 auto *BV = dyn_cast<BuildVectorSDNode>(N);
162 if (!BV)
163 return false;
164
165 APInt SplatUndef;
166 unsigned SplatBitSize;
167 bool HasUndefs;
168 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
169 // Endianness does not matter here. We are checking for a splat given the
170 // element size of the vector, and if we find such a splat for little endian
171 // layout, then that should be valid also for big endian (as the full vector
172 // size is known to be a multiple of the element size).
173 const bool IsBigEndian = false;
174 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
175 EltSize, IsBigEndian) &&
176 EltSize == SplatBitSize;
177}
178
179// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
180// specializations of the more general isConstantSplatVector()?
181
182bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
183 // Look through a bit convert.
184 while (N->getOpcode() == ISD::BITCAST)
185 N = N->getOperand(0).getNode();
186
187 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
188 APInt SplatVal;
189 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
190 }
191
192 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
193
194 unsigned i = 0, e = N->getNumOperands();
195
196 // Skip over all of the undef values.
197 while (i != e && N->getOperand(i).isUndef())
198 ++i;
199
200 // Do not accept an all-undef vector.
201 if (i == e) return false;
202
203 // Do not accept build_vectors that aren't all constants or which have non-~0
204 // elements. We have to be a bit careful here, as the type of the constant
205 // may not be the same as the type of the vector elements due to type
206 // legalization (the elements are promoted to a legal type for the target and
207 // a vector of a type may be legal when the base element type is not).
208 // We only want to check enough bits to cover the vector elements, because
209 // we care if the resultant vector is all ones, not whether the individual
210 // constants are.
211 SDValue NotZero = N->getOperand(i);
212 if (auto OptAPInt = NotZero->bitcastToAPInt()) {
213 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
214 if (OptAPInt->countr_one() < EltSize)
215 return false;
216 } else
217 return false;
218
219 // Okay, we have at least one ~0 value, check to see if the rest match or are
220 // undefs. Even with the above element type twiddling, this should be OK, as
221 // the same type legalization should have applied to all the elements.
222 for (++i; i != e; ++i)
223 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
224 return false;
225 return true;
226}
227
228bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
229 // Look through a bit convert.
230 while (N->getOpcode() == ISD::BITCAST)
231 N = N->getOperand(0).getNode();
232
233 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
234 APInt SplatVal;
235 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
236 }
237
238 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
239
240 bool IsAllUndef = true;
241 for (const SDValue &Op : N->op_values()) {
242 if (Op.isUndef())
243 continue;
244 IsAllUndef = false;
245 // Do not accept build_vectors that aren't all constants or which have non-0
246 // elements. We have to be a bit careful here, as the type of the constant
247 // may not be the same as the type of the vector elements due to type
248 // legalization (the elements are promoted to a legal type for the target
249 // and a vector of a type may be legal when the base element type is not).
250 // We only want to check enough bits to cover the vector elements, because
251 // we care if the resultant vector is all zeros, not whether the individual
252 // constants are.
253 if (auto OptAPInt = Op->bitcastToAPInt()) {
254 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
255 if (OptAPInt->countr_zero() < EltSize)
256 return false;
257 } else
258 return false;
259 }
260
261 // Do not accept an all-undef vector.
262 if (IsAllUndef)
263 return false;
264 return true;
265}
266
268 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
269}
270
272 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
273}
274
276 if (N->getOpcode() != ISD::BUILD_VECTOR)
277 return false;
278
279 for (const SDValue &Op : N->op_values()) {
280 if (Op.isUndef())
281 continue;
283 return false;
284 }
285 return true;
286}
287
289 if (N->getOpcode() != ISD::BUILD_VECTOR)
290 return false;
291
292 for (const SDValue &Op : N->op_values()) {
293 if (Op.isUndef())
294 continue;
296 return false;
297 }
298 return true;
299}
300
301bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
302 bool Signed) {
303 assert(N->getValueType(0).isVector() && "Expected a vector!");
304
305 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
306 if (EltSize <= NewEltSize)
307 return false;
308
309 if (N->getOpcode() == ISD::ZERO_EXTEND) {
310 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
311 NewEltSize) &&
312 !Signed;
313 }
314 if (N->getOpcode() == ISD::SIGN_EXTEND) {
315 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
316 NewEltSize) &&
317 Signed;
318 }
319 if (N->getOpcode() != ISD::BUILD_VECTOR)
320 return false;
321
322 for (const SDValue &Op : N->op_values()) {
323 if (Op.isUndef())
324 continue;
326 return false;
327
328 APInt C = Op->getAsAPIntVal().trunc(EltSize);
329 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
330 return false;
331 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
332 return false;
333 }
334
335 return true;
336}
337
339 // Return false if the node has no operands.
340 // This is "logically inconsistent" with the definition of "all" but
341 // is probably the desired behavior.
342 if (N->getNumOperands() == 0)
343 return false;
344 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
345}
346
348 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
349}
350
351template <typename ConstNodeType>
353 std::function<bool(ConstNodeType *)> Match,
354 bool AllowUndefs, bool AllowTruncation) {
355 // FIXME: Add support for scalar UNDEF cases?
356 if (auto *C = dyn_cast<ConstNodeType>(Op))
357 return Match(C);
358
359 // FIXME: Add support for vector UNDEF cases?
360 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
361 ISD::SPLAT_VECTOR != Op.getOpcode())
362 return false;
363
364 EVT SVT = Op.getValueType().getScalarType();
365 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
366 if (AllowUndefs && Op.getOperand(i).isUndef()) {
367 if (!Match(nullptr))
368 return false;
369 continue;
370 }
371
372 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
373 if (!Cst || (!AllowTruncation && Cst->getValueType(0) != SVT) ||
374 !Match(Cst))
375 return false;
376 }
377 return true;
378}
379// Build used template types.
381 SDValue, std::function<bool(ConstantSDNode *)>, bool, bool);
383 SDValue, std::function<bool(ConstantFPSDNode *)>, bool, bool);
384
386 SDValue LHS, SDValue RHS,
387 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
388 bool AllowUndefs, bool AllowTypeMismatch) {
389 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
390 return false;
391
392 // TODO: Add support for scalar UNDEF cases?
393 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
394 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
395 return Match(LHSCst, RHSCst);
396
397 // TODO: Add support for vector UNDEF cases?
398 if (LHS.getOpcode() != RHS.getOpcode() ||
399 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
400 LHS.getOpcode() != ISD::SPLAT_VECTOR))
401 return false;
402
403 EVT SVT = LHS.getValueType().getScalarType();
404 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
405 SDValue LHSOp = LHS.getOperand(i);
406 SDValue RHSOp = RHS.getOperand(i);
407 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
408 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
409 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
410 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
411 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
412 return false;
413 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
414 LHSOp.getValueType() != RHSOp.getValueType()))
415 return false;
416 if (!Match(LHSCst, RHSCst))
417 return false;
418 }
419 return true;
420}
421
423 switch (MinMaxOpc) {
424 default:
425 llvm_unreachable("unrecognized opcode");
426 case ISD::UMIN:
427 return ISD::UMAX;
428 case ISD::UMAX:
429 return ISD::UMIN;
430 case ISD::SMIN:
431 return ISD::SMAX;
432 case ISD::SMAX:
433 return ISD::SMIN;
434 }
435}
436
438 switch (MinMaxOpc) {
439 default:
440 llvm_unreachable("unrecognized min/max opcode");
441 case ISD::SMIN:
442 return ISD::UMIN;
443 case ISD::SMAX:
444 return ISD::UMAX;
445 case ISD::UMIN:
446 return ISD::SMIN;
447 case ISD::UMAX:
448 return ISD::SMAX;
449 }
450}
451
453 switch (VecReduceOpcode) {
454 default:
455 llvm_unreachable("Expected VECREDUCE opcode");
458 case ISD::VP_REDUCE_FADD:
459 case ISD::VP_REDUCE_SEQ_FADD:
460 return ISD::FADD;
463 case ISD::VP_REDUCE_FMUL:
464 case ISD::VP_REDUCE_SEQ_FMUL:
465 return ISD::FMUL;
467 case ISD::VP_REDUCE_ADD:
468 return ISD::ADD;
470 case ISD::VP_REDUCE_MUL:
471 return ISD::MUL;
473 case ISD::VP_REDUCE_AND:
474 return ISD::AND;
476 case ISD::VP_REDUCE_OR:
477 return ISD::OR;
479 case ISD::VP_REDUCE_XOR:
480 return ISD::XOR;
482 case ISD::VP_REDUCE_SMAX:
483 return ISD::SMAX;
485 case ISD::VP_REDUCE_SMIN:
486 return ISD::SMIN;
488 case ISD::VP_REDUCE_UMAX:
489 return ISD::UMAX;
491 case ISD::VP_REDUCE_UMIN:
492 return ISD::UMIN;
494 case ISD::VP_REDUCE_FMAX:
495 return ISD::FMAXNUM;
497 case ISD::VP_REDUCE_FMIN:
498 return ISD::FMINNUM;
500 case ISD::VP_REDUCE_FMAXIMUM:
501 return ISD::FMAXIMUM;
503 case ISD::VP_REDUCE_FMINIMUM:
504 return ISD::FMINIMUM;
505 }
506}
507
508bool ISD::isVPOpcode(unsigned Opcode) {
509 switch (Opcode) {
510 default:
511 return false;
512#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
513 case ISD::VPSD: \
514 return true;
515#include "llvm/IR/VPIntrinsics.def"
516 }
517}
518
519bool ISD::isVPBinaryOp(unsigned Opcode) {
520 switch (Opcode) {
521 default:
522 break;
523#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
524#define VP_PROPERTY_BINARYOP return true;
525#define END_REGISTER_VP_SDNODE(VPSD) break;
526#include "llvm/IR/VPIntrinsics.def"
527 }
528 return false;
529}
530
531bool ISD::isVPReduction(unsigned Opcode) {
532 switch (Opcode) {
533 default:
534 return false;
535 case ISD::VP_REDUCE_ADD:
536 case ISD::VP_REDUCE_MUL:
537 case ISD::VP_REDUCE_AND:
538 case ISD::VP_REDUCE_OR:
539 case ISD::VP_REDUCE_XOR:
540 case ISD::VP_REDUCE_SMAX:
541 case ISD::VP_REDUCE_SMIN:
542 case ISD::VP_REDUCE_UMAX:
543 case ISD::VP_REDUCE_UMIN:
544 case ISD::VP_REDUCE_FMAX:
545 case ISD::VP_REDUCE_FMIN:
546 case ISD::VP_REDUCE_FMAXIMUM:
547 case ISD::VP_REDUCE_FMINIMUM:
548 case ISD::VP_REDUCE_FADD:
549 case ISD::VP_REDUCE_FMUL:
550 case ISD::VP_REDUCE_SEQ_FADD:
551 case ISD::VP_REDUCE_SEQ_FMUL:
552 return true;
553 }
554}
555
556/// The operand position of the vector mask.
557std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
558 switch (Opcode) {
559 default:
560 return std::nullopt;
561#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
562 case ISD::VPSD: \
563 return MASKPOS;
564#include "llvm/IR/VPIntrinsics.def"
565 }
566}
567
568/// The operand position of the explicit vector length parameter.
569std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
570 switch (Opcode) {
571 default:
572 return std::nullopt;
573#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
574 case ISD::VPSD: \
575 return EVLPOS;
576#include "llvm/IR/VPIntrinsics.def"
577 }
578}
579
580std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
581 bool hasFPExcept) {
582 // FIXME: Return strict opcodes in case of fp exceptions.
583 switch (VPOpcode) {
584 default:
585 return std::nullopt;
586#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
587#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
588#define END_REGISTER_VP_SDNODE(VPOPC) break;
589#include "llvm/IR/VPIntrinsics.def"
590 }
591 return std::nullopt;
592}
593
594std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
595 switch (Opcode) {
596 default:
597 return std::nullopt;
598#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
599#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
600#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
601#include "llvm/IR/VPIntrinsics.def"
602 }
603}
604
606 switch (ExtType) {
607 case ISD::EXTLOAD:
608 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
609 case ISD::SEXTLOAD:
610 return ISD::SIGN_EXTEND;
611 case ISD::ZEXTLOAD:
612 return ISD::ZERO_EXTEND;
613 default:
614 break;
615 }
616
617 llvm_unreachable("Invalid LoadExtType");
618}
619
621 // To perform this operation, we just need to swap the L and G bits of the
622 // operation.
623 unsigned OldL = (Operation >> 2) & 1;
624 unsigned OldG = (Operation >> 1) & 1;
625 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
626 (OldL << 1) | // New G bit
627 (OldG << 2)); // New L bit.
628}
629
631 unsigned Operation = Op;
632 if (isIntegerLike)
633 Operation ^= 7; // Flip L, G, E bits, but not U.
634 else
635 Operation ^= 15; // Flip all of the condition bits.
636
638 Operation &= ~8; // Don't let N and U bits get set.
639
640 return ISD::CondCode(Operation);
641}
642
646
648 bool isIntegerLike) {
649 return getSetCCInverseImpl(Op, isIntegerLike);
650}
651
652/// For an integer comparison, return 1 if the comparison is a signed operation
653/// and 2 if the result is an unsigned comparison. Return zero if the operation
654/// does not depend on the sign of the input (setne and seteq).
655static int isSignedOp(ISD::CondCode Opcode) {
656 switch (Opcode) {
657 default: llvm_unreachable("Illegal integer setcc operation!");
658 case ISD::SETEQ:
659 case ISD::SETNE: return 0;
660 case ISD::SETLT:
661 case ISD::SETLE:
662 case ISD::SETGT:
663 case ISD::SETGE: return 1;
664 case ISD::SETULT:
665 case ISD::SETULE:
666 case ISD::SETUGT:
667 case ISD::SETUGE: return 2;
668 }
669}
670
672 EVT Type) {
673 bool IsInteger = Type.isInteger();
674 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
675 // Cannot fold a signed integer setcc with an unsigned integer setcc.
676 return ISD::SETCC_INVALID;
677
678 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
679
680 // If the N and U bits get set, then the resultant comparison DOES suddenly
681 // care about orderedness, and it is true when ordered.
682 if (Op > ISD::SETTRUE2)
683 Op &= ~16; // Clear the U bit if the N bit is set.
684
685 // Canonicalize illegal integer setcc's.
686 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
687 Op = ISD::SETNE;
688
689 return ISD::CondCode(Op);
690}
691
693 EVT Type) {
694 bool IsInteger = Type.isInteger();
695 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
696 // Cannot fold a signed setcc with an unsigned setcc.
697 return ISD::SETCC_INVALID;
698
699 // Combine all of the condition bits.
700 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
701
702 // Canonicalize illegal integer setcc's.
703 if (IsInteger) {
704 switch (Result) {
705 default: break;
706 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
707 case ISD::SETOEQ: // SETEQ & SETU[LG]E
708 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
709 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
710 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
711 }
712 }
713
714 return Result;
715}
716
717//===----------------------------------------------------------------------===//
718// SDNode Profile Support
719//===----------------------------------------------------------------------===//
720
721/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
722static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
723 ID.AddInteger(OpC);
724}
725
726/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
727/// solely with their pointer.
729 ID.AddPointer(VTList.VTs);
730}
731
732/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
735 for (const auto &Op : Ops) {
736 ID.AddPointer(Op.getNode());
737 ID.AddInteger(Op.getResNo());
738 }
739}
740
741/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
744 for (const auto &Op : Ops) {
745 ID.AddPointer(Op.getNode());
746 ID.AddInteger(Op.getResNo());
747 }
748}
749
750static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
751 SDVTList VTList, ArrayRef<SDValue> OpList) {
752 AddNodeIDOpcode(ID, OpC);
753 AddNodeIDValueTypes(ID, VTList);
754 AddNodeIDOperands(ID, OpList);
755}
756
757/// If this is an SDNode with special info, add this info to the NodeID data.
759 switch (N->getOpcode()) {
762 case ISD::MCSymbol:
763 llvm_unreachable("Should only be used on nodes with operands");
764 default: break; // Normal nodes don't need extra info.
766 case ISD::Constant: {
768 ID.AddPointer(C->getConstantIntValue());
769 ID.AddBoolean(C->isOpaque());
770 break;
771 }
773 case ISD::ConstantFP:
774 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
775 break;
781 ID.AddPointer(GA->getGlobal());
782 ID.AddInteger(GA->getOffset());
783 ID.AddInteger(GA->getTargetFlags());
784 break;
785 }
786 case ISD::BasicBlock:
788 break;
789 case ISD::Register:
790 ID.AddInteger(cast<RegisterSDNode>(N)->getReg().id());
791 break;
793 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
794 break;
795 case ISD::SRCVALUE:
796 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
797 break;
798 case ISD::FrameIndex:
800 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
801 break;
803 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
804 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
805 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
806 break;
807 case ISD::JumpTable:
809 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
810 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
811 break;
815 ID.AddInteger(CP->getAlign().value());
816 ID.AddInteger(CP->getOffset());
819 else
820 ID.AddPointer(CP->getConstVal());
821 ID.AddInteger(CP->getTargetFlags());
822 break;
823 }
824 case ISD::TargetIndex: {
826 ID.AddInteger(TI->getIndex());
827 ID.AddInteger(TI->getOffset());
828 ID.AddInteger(TI->getTargetFlags());
829 break;
830 }
831 case ISD::LOAD: {
832 const LoadSDNode *LD = cast<LoadSDNode>(N);
833 ID.AddInteger(LD->getMemoryVT().getRawBits());
834 ID.AddInteger(LD->getRawSubclassData());
835 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
836 ID.AddInteger(LD->getMemOperand()->getFlags());
837 break;
838 }
839 case ISD::STORE: {
840 const StoreSDNode *ST = cast<StoreSDNode>(N);
841 ID.AddInteger(ST->getMemoryVT().getRawBits());
842 ID.AddInteger(ST->getRawSubclassData());
843 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
844 ID.AddInteger(ST->getMemOperand()->getFlags());
845 break;
846 }
847 case ISD::VP_LOAD: {
848 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
849 ID.AddInteger(ELD->getMemoryVT().getRawBits());
850 ID.AddInteger(ELD->getRawSubclassData());
851 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
852 ID.AddInteger(ELD->getMemOperand()->getFlags());
853 break;
854 }
855 case ISD::VP_LOAD_FF: {
856 const auto *LD = cast<VPLoadFFSDNode>(N);
857 ID.AddInteger(LD->getMemoryVT().getRawBits());
858 ID.AddInteger(LD->getRawSubclassData());
859 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
860 ID.AddInteger(LD->getMemOperand()->getFlags());
861 break;
862 }
863 case ISD::VP_STORE: {
864 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
865 ID.AddInteger(EST->getMemoryVT().getRawBits());
866 ID.AddInteger(EST->getRawSubclassData());
867 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
868 ID.AddInteger(EST->getMemOperand()->getFlags());
869 break;
870 }
871 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
873 ID.AddInteger(SLD->getMemoryVT().getRawBits());
874 ID.AddInteger(SLD->getRawSubclassData());
875 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
876 break;
877 }
878 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
880 ID.AddInteger(SST->getMemoryVT().getRawBits());
881 ID.AddInteger(SST->getRawSubclassData());
882 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
883 break;
884 }
885 case ISD::VP_GATHER: {
887 ID.AddInteger(EG->getMemoryVT().getRawBits());
888 ID.AddInteger(EG->getRawSubclassData());
889 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
890 ID.AddInteger(EG->getMemOperand()->getFlags());
891 break;
892 }
893 case ISD::VP_SCATTER: {
895 ID.AddInteger(ES->getMemoryVT().getRawBits());
896 ID.AddInteger(ES->getRawSubclassData());
897 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
898 ID.AddInteger(ES->getMemOperand()->getFlags());
899 break;
900 }
901 case ISD::MLOAD: {
903 ID.AddInteger(MLD->getMemoryVT().getRawBits());
904 ID.AddInteger(MLD->getRawSubclassData());
905 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
906 ID.AddInteger(MLD->getMemOperand()->getFlags());
907 break;
908 }
909 case ISD::MSTORE: {
911 ID.AddInteger(MST->getMemoryVT().getRawBits());
912 ID.AddInteger(MST->getRawSubclassData());
913 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
914 ID.AddInteger(MST->getMemOperand()->getFlags());
915 break;
916 }
917 case ISD::MGATHER: {
919 ID.AddInteger(MG->getMemoryVT().getRawBits());
920 ID.AddInteger(MG->getRawSubclassData());
921 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
922 ID.AddInteger(MG->getMemOperand()->getFlags());
923 break;
924 }
925 case ISD::MSCATTER: {
927 ID.AddInteger(MS->getMemoryVT().getRawBits());
928 ID.AddInteger(MS->getRawSubclassData());
929 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
930 ID.AddInteger(MS->getMemOperand()->getFlags());
931 break;
932 }
935 case ISD::ATOMIC_SWAP:
947 case ISD::ATOMIC_LOAD:
948 case ISD::ATOMIC_STORE: {
949 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
950 ID.AddInteger(AT->getMemoryVT().getRawBits());
951 ID.AddInteger(AT->getRawSubclassData());
952 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
953 ID.AddInteger(AT->getMemOperand()->getFlags());
954 break;
955 }
956 case ISD::VECTOR_SHUFFLE: {
957 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
958 for (int M : Mask)
959 ID.AddInteger(M);
960 break;
961 }
962 case ISD::ADDRSPACECAST: {
964 ID.AddInteger(ASC->getSrcAddressSpace());
965 ID.AddInteger(ASC->getDestAddressSpace());
966 break;
967 }
969 case ISD::BlockAddress: {
971 ID.AddPointer(BA->getBlockAddress());
972 ID.AddInteger(BA->getOffset());
973 ID.AddInteger(BA->getTargetFlags());
974 break;
975 }
976 case ISD::AssertAlign:
977 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
978 break;
979 case ISD::PREFETCH:
982 // Handled by MemIntrinsicSDNode check after the switch.
983 break;
985 ID.AddPointer(cast<MDNodeSDNode>(N)->getMD());
986 break;
987 } // end switch (N->getOpcode())
988
989 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
990 // to check.
991 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
992 ID.AddInteger(MN->getRawSubclassData());
993 ID.AddInteger(MN->getMemoryVT().getRawBits());
994 for (const MachineMemOperand *MMO : MN->memoperands()) {
995 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
996 ID.AddInteger(MMO->getFlags());
997 }
998 }
999}
1000
1001/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
1002/// data.
1004 AddNodeIDOpcode(ID, N->getOpcode());
1005 // Add the return value info.
1006 AddNodeIDValueTypes(ID, N->getVTList());
1007 // Add the operand info.
1008 AddNodeIDOperands(ID, N->ops());
1009
1010 // Handle SDNode leafs with special info.
1012}
1013
1014//===----------------------------------------------------------------------===//
1015// SelectionDAG Class
1016//===----------------------------------------------------------------------===//
1017
1018/// doNotCSE - Return true if CSE should not be performed for this node.
1019static bool doNotCSE(SDNode *N) {
1020 if (N->getValueType(0) == MVT::Glue)
1021 return true; // Never CSE anything that produces a glue result.
1022
1023 switch (N->getOpcode()) {
1024 default: break;
1025 case ISD::HANDLENODE:
1026 case ISD::EH_LABEL:
1027 return true; // Never CSE these nodes.
1028 }
1029
1030 // Check that remaining values produced are not flags.
1031 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1032 if (N->getValueType(i) == MVT::Glue)
1033 return true; // Never CSE anything that produces a glue result.
1034
1035 return false;
1036}
1037
1038/// Construct a DemandedElts mask which demands all elements of \p V.
1039/// If \p V is not a fixed-length vector, then this will return a single bit.
1041 EVT VT = V.getValueType();
1042 // Since the number of lanes in a scalable vector is unknown at compile time,
1043 // we track one bit which is implicitly broadcast to all lanes. This means
1044 // that all lanes in a scalable vector are considered demanded.
1046 : APInt(1, 1);
1047}
1048
1049/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1050/// SelectionDAG.
1052 // Create a dummy node (which is not added to allnodes), that adds a reference
1053 // to the root node, preventing it from being deleted.
1054 HandleSDNode Dummy(getRoot());
1055
1056 SmallVector<SDNode*, 128> DeadNodes;
1057
1058 // Add all obviously-dead nodes to the DeadNodes worklist.
1059 for (SDNode &Node : allnodes())
1060 if (Node.use_empty())
1061 DeadNodes.push_back(&Node);
1062
1063 RemoveDeadNodes(DeadNodes);
1064
1065 // If the root changed (e.g. it was a dead load, update the root).
1066 setRoot(Dummy.getValue());
1067}
1068
1069/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1070/// given list, and any nodes that become unreachable as a result.
1072
1073 // Process the worklist, deleting the nodes and adding their uses to the
1074 // worklist.
1075 while (!DeadNodes.empty()) {
1076 SDNode *N = DeadNodes.pop_back_val();
1077 // Skip to next node if we've already managed to delete the node. This could
1078 // happen if replacing a node causes a node previously added to the node to
1079 // be deleted.
1080 if (N->getOpcode() == ISD::DELETED_NODE)
1081 continue;
1082
1083 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1084 DUL->NodeDeleted(N, nullptr);
1085
1086 // Take the node out of the appropriate CSE map.
1087 RemoveNodeFromCSEMaps(N);
1088
1089 // Next, brutally remove the operand list. This is safe to do, as there are
1090 // no cycles in the graph.
1091 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1092 SDUse &Use = *I++;
1093 SDNode *Operand = Use.getNode();
1094 Use.set(SDValue());
1095
1096 // Now that we removed this operand, see if there are no uses of it left.
1097 if (Operand->use_empty())
1098 DeadNodes.push_back(Operand);
1099 }
1100
1101 DeallocateNode(N);
1102 }
1103}
1104
1106 SmallVector<SDNode*, 16> DeadNodes(1, N);
1107
1108 // Create a dummy node that adds a reference to the root node, preventing
1109 // it from being deleted. (This matters if the root is an operand of the
1110 // dead node.)
1111 HandleSDNode Dummy(getRoot());
1112
1113 RemoveDeadNodes(DeadNodes);
1114}
1115
1117 // First take this out of the appropriate CSE map.
1118 RemoveNodeFromCSEMaps(N);
1119
1120 // Finally, remove uses due to operands of this node, remove from the
1121 // AllNodes list, and delete the node.
1122 DeleteNodeNotInCSEMaps(N);
1123}
1124
1125void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1126 assert(N->getIterator() != AllNodes.begin() &&
1127 "Cannot delete the entry node!");
1128 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1129
1130 // Drop all of the operands and decrement used node's use counts.
1131 N->DropOperands();
1132
1133 DeallocateNode(N);
1134}
1135
1136void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1137 assert(!(V->isVariadic() && isParameter));
1138 if (isParameter)
1139 ByvalParmDbgValues.push_back(V);
1140 else
1141 DbgValues.push_back(V);
1142 for (const SDNode *Node : V->getSDNodes())
1143 if (Node)
1144 DbgValMap[Node].push_back(V);
1145}
1146
1148 DbgValMapType::iterator I = DbgValMap.find(Node);
1149 if (I == DbgValMap.end())
1150 return;
1151 for (auto &Val: I->second)
1152 Val->setIsInvalidated();
1153 DbgValMap.erase(I);
1154}
1155
1156void SelectionDAG::DeallocateNode(SDNode *N) {
1157 // If we have operands, deallocate them.
1159
1160 NodeAllocator.Deallocate(AllNodes.remove(N));
1161
1162 // Set the opcode to DELETED_NODE to help catch bugs when node
1163 // memory is reallocated.
1164 // FIXME: There are places in SDag that have grown a dependency on the opcode
1165 // value in the released node.
1166 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1167 N->NodeType = ISD::DELETED_NODE;
1168
1169 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1170 // them and forget about that node.
1171 DbgInfo->erase(N);
1172
1173 // Invalidate extra info.
1174 SDEI.erase(N);
1175}
1176
1177#ifndef NDEBUG
1178/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1179void SelectionDAG::verifyNode(SDNode *N) const {
1180 switch (N->getOpcode()) {
1181 default:
1182 if (N->isTargetOpcode())
1184 break;
1185 case ISD::BUILD_PAIR: {
1186 EVT VT = N->getValueType(0);
1187 assert(N->getNumValues() == 1 && "Too many results!");
1188 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1189 "Wrong return type!");
1190 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1191 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1192 "Mismatched operand types!");
1193 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1194 "Wrong operand type!");
1195 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1196 "Wrong return type size");
1197 break;
1198 }
1199 case ISD::BUILD_VECTOR: {
1200 assert(N->getNumValues() == 1 && "Too many results!");
1201 assert(N->getValueType(0).isVector() && "Wrong return type!");
1202 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1203 "Wrong number of operands!");
1204 EVT EltVT = N->getValueType(0).getVectorElementType();
1205 for (const SDUse &Op : N->ops()) {
1206 assert((Op.getValueType() == EltVT ||
1207 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1208 EltVT.bitsLE(Op.getValueType()))) &&
1209 "Wrong operand type!");
1210 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1211 "Operands must all have the same type");
1212 }
1213 break;
1214 }
1215 case ISD::SADDO:
1216 case ISD::UADDO:
1217 case ISD::SSUBO:
1218 case ISD::USUBO:
1219 assert(N->getNumValues() == 2 && "Wrong number of results!");
1220 assert(N->getVTList().NumVTs == 2 && N->getNumOperands() == 2 &&
1221 "Invalid add/sub overflow op!");
1222 assert(N->getVTList().VTs[0].isInteger() &&
1223 N->getVTList().VTs[1].isInteger() &&
1224 N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1225 N->getOperand(0).getValueType() == N->getVTList().VTs[0] &&
1226 "Binary operator types must match!");
1227 break;
1228 }
1229}
1230#endif // NDEBUG
1231
1232/// Insert a newly allocated node into the DAG.
1233///
1234/// Handles insertion into the all nodes list and CSE map, as well as
1235/// verification and other common operations when a new node is allocated.
1236void SelectionDAG::InsertNode(SDNode *N) {
1237 AllNodes.push_back(N);
1238#ifndef NDEBUG
1239 N->PersistentId = NextPersistentId++;
1240 verifyNode(N);
1241#endif
1242 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1243 DUL->NodeInserted(N);
1244}
1245
1246/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1247/// correspond to it. This is useful when we're about to delete or repurpose
1248/// the node. We don't want future request for structurally identical nodes
1249/// to return N anymore.
1250bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1251 bool Erased = false;
1252 switch (N->getOpcode()) {
1253 case ISD::HANDLENODE: return false; // noop.
1254 case ISD::CONDCODE:
1255 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1256 "Cond code doesn't exist!");
1257 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1258 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1259 break;
1261 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1262 break;
1264 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1265 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1266 ESN->getSymbol(), ESN->getTargetFlags()));
1267 break;
1268 }
1269 case ISD::MCSymbol: {
1270 auto *MCSN = cast<MCSymbolSDNode>(N);
1271 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1272 break;
1273 }
1274 case ISD::VALUETYPE: {
1275 EVT VT = cast<VTSDNode>(N)->getVT();
1276 if (VT.isExtended()) {
1277 Erased = ExtendedValueTypeNodes.erase(VT);
1278 } else {
1279 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1280 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1281 }
1282 break;
1283 }
1284 default:
1285 // Remove it from the CSE Map.
1286 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1287 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1288 Erased = CSEMap.RemoveNode(N);
1289 break;
1290 }
1291#ifndef NDEBUG
1292 // Verify that the node was actually in one of the CSE maps, unless it has a
1293 // glue result (which cannot be CSE'd) or is one of the special cases that are
1294 // not subject to CSE.
1295 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1296 !N->isMachineOpcode() && !doNotCSE(N)) {
1297 N->dump(this);
1298 dbgs() << "\n";
1299 llvm_unreachable("Node is not in map!");
1300 }
1301#endif
1302 return Erased;
1303}
1304
1305/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1306/// maps and modified in place. Add it back to the CSE maps, unless an identical
1307/// node already exists, in which case transfer all its users to the existing
1308/// node. This transfer can potentially trigger recursive merging.
1309void
1310SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1311 // For node types that aren't CSE'd, just act as if no identical node
1312 // already exists.
1313 if (!doNotCSE(N)) {
1314 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1315 if (Existing != N) {
1316 // If there was already an existing matching node, use ReplaceAllUsesWith
1317 // to replace the dead one with the existing one. This can cause
1318 // recursive merging of other unrelated nodes down the line.
1319 Existing->intersectFlagsWith(N->getFlags());
1320 if (auto *MemNode = dyn_cast<MemSDNode>(Existing))
1321 MemNode->refineRanges(cast<MemSDNode>(N)->memoperands());
1322 ReplaceAllUsesWith(N, Existing);
1323
1324 // N is now dead. Inform the listeners and delete it.
1325 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1326 DUL->NodeDeleted(N, Existing);
1327 DeleteNodeNotInCSEMaps(N);
1328 return;
1329 }
1330 }
1331
1332 // If the node doesn't already exist, we updated it. Inform listeners.
1333 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1334 DUL->NodeUpdated(N);
1335}
1336
1337/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1338/// were replaced with those specified. If this node is never memoized,
1339/// return null, otherwise return a pointer to the slot it would take. If a
1340/// node already exists with these operands, the slot will be non-null.
1341SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1342 void *&InsertPos) {
1343 if (doNotCSE(N))
1344 return nullptr;
1345
1346 SDValue Ops[] = { Op };
1347 FoldingSetNodeID ID;
1348 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1350 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1351 if (Node)
1352 Node->intersectFlagsWith(N->getFlags());
1353 return Node;
1354}
1355
1356/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1357/// were replaced with those specified. If this node is never memoized,
1358/// return null, otherwise return a pointer to the slot it would take. If a
1359/// node already exists with these operands, the slot will be non-null.
1360SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1361 SDValue Op1, SDValue Op2,
1362 void *&InsertPos) {
1363 if (doNotCSE(N))
1364 return nullptr;
1365
1366 SDValue Ops[] = { Op1, Op2 };
1367 FoldingSetNodeID ID;
1368 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1370 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1371 if (Node)
1372 Node->intersectFlagsWith(N->getFlags());
1373 return Node;
1374}
1375
1376/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1377/// were replaced with those specified. If this node is never memoized,
1378/// return null, otherwise return a pointer to the slot it would take. If a
1379/// node already exists with these operands, the slot will be non-null.
1380SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1381 void *&InsertPos) {
1382 if (doNotCSE(N))
1383 return nullptr;
1384
1385 FoldingSetNodeID ID;
1386 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1388 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1389 if (Node)
1390 Node->intersectFlagsWith(N->getFlags());
1391 return Node;
1392}
1393
1395 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1396 : VT.getTypeForEVT(*getContext());
1397
1398 return getDataLayout().getABITypeAlign(Ty);
1399}
1400
1401// EntryNode could meaningfully have debug info if we can find it...
1403 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1404 getVTList(MVT::Other, MVT::Glue)),
1405 Root(getEntryNode()) {
1406 InsertNode(&EntryNode);
1407 DbgInfo = new SDDbgInfo();
1408}
1409
1411 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1412 const TargetLibraryInfo *LibraryInfo,
1413 const LibcallLoweringInfo *LibcallsInfo,
1414 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1416 FunctionVarLocs const *VarLocs) {
1417 MF = &NewMF;
1418 SDAGISelPass = PassPtr;
1419 ORE = &NewORE;
1422 LibInfo = LibraryInfo;
1423 Libcalls = LibcallsInfo;
1424 Context = &MF->getFunction().getContext();
1425 UA = NewUA;
1426 PSI = PSIin;
1427 BFI = BFIin;
1428 MMI = &MMIin;
1429 FnVarLocs = VarLocs;
1430}
1431
1433 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1434 allnodes_clear();
1435 OperandRecycler.clear(OperandAllocator);
1436 delete DbgInfo;
1437}
1438
1440 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1441}
1442
1443void SelectionDAG::allnodes_clear() {
1444 assert(&*AllNodes.begin() == &EntryNode);
1445 AllNodes.remove(AllNodes.begin());
1446 while (!AllNodes.empty())
1447 DeallocateNode(&AllNodes.front());
1448#ifndef NDEBUG
1449 NextPersistentId = 0;
1450#endif
1451}
1452
1453SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1454 void *&InsertPos) {
1455 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1456 if (N) {
1457 switch (N->getOpcode()) {
1458 default: break;
1459 case ISD::Constant:
1460 case ISD::ConstantFP:
1461 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1462 "debug location. Use another overload.");
1463 }
1464 }
1465 return N;
1466}
1467
1468SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1469 const SDLoc &DL, void *&InsertPos) {
1470 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1471 if (N) {
1472 switch (N->getOpcode()) {
1473 case ISD::Constant:
1474 case ISD::ConstantFP:
1475 // Erase debug location from the node if the node is used at several
1476 // different places. Do not propagate one location to all uses as it
1477 // will cause a worse single stepping debugging experience.
1478 if (N->getDebugLoc() != DL.getDebugLoc())
1479 N->setDebugLoc(DebugLoc());
1480 break;
1481 default:
1482 // When the node's point of use is located earlier in the instruction
1483 // sequence than its prior point of use, update its debug info to the
1484 // earlier location.
1485 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1486 N->setDebugLoc(DL.getDebugLoc());
1487 break;
1488 }
1489 }
1490 return N;
1491}
1492
1494 allnodes_clear();
1495 OperandRecycler.clear(OperandAllocator);
1496 OperandAllocator.Reset();
1497 CSEMap.clear();
1498
1499 ExtendedValueTypeNodes.clear();
1500 ExternalSymbols.clear();
1501 TargetExternalSymbols.clear();
1502 MCSymbols.clear();
1503 SDEI.clear();
1504 llvm::fill(CondCodeNodes, nullptr);
1505 llvm::fill(ValueTypeNodes, nullptr);
1506
1507 EntryNode.UseList = nullptr;
1508 InsertNode(&EntryNode);
1509 Root = getEntryNode();
1510 DbgInfo->clear();
1511}
1512
1514 return VT.bitsGT(Op.getValueType())
1515 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1516 : getNode(ISD::FP_ROUND, DL, VT, Op,
1517 getIntPtrConstant(0, DL, /*isTarget=*/true));
1518}
1519
1520std::pair<SDValue, SDValue>
1522 const SDLoc &DL, EVT VT) {
1523 assert(!VT.bitsEq(Op.getValueType()) &&
1524 "Strict no-op FP extend/round not allowed.");
1525 SDValue Res =
1526 VT.bitsGT(Op.getValueType())
1527 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1528 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1529 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1530
1531 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1532}
1533
1535 return VT.bitsGT(Op.getValueType()) ?
1536 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1537 getNode(ISD::TRUNCATE, DL, VT, Op);
1538}
1539
1541 return VT.bitsGT(Op.getValueType()) ?
1542 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1543 getNode(ISD::TRUNCATE, DL, VT, Op);
1544}
1545
1547 return VT.bitsGT(Op.getValueType()) ?
1548 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1549 getNode(ISD::TRUNCATE, DL, VT, Op);
1550}
1551
1553 EVT VT) {
1554 assert(!VT.isVector());
1555 auto Type = Op.getValueType();
1556 SDValue DestOp;
1557 if (Type == VT)
1558 return Op;
1559 auto Size = Op.getValueSizeInBits();
1560 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1561 if (DestOp.getValueType() == VT)
1562 return DestOp;
1563
1564 return getAnyExtOrTrunc(DestOp, DL, VT);
1565}
1566
1568 EVT VT) {
1569 assert(!VT.isVector());
1570 auto Type = Op.getValueType();
1571 SDValue DestOp;
1572 if (Type == VT)
1573 return Op;
1574 auto Size = Op.getValueSizeInBits();
1575 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1576 if (DestOp.getValueType() == VT)
1577 return DestOp;
1578
1579 return getSExtOrTrunc(DestOp, DL, VT);
1580}
1581
1583 EVT VT) {
1584 assert(!VT.isVector());
1585 auto Type = Op.getValueType();
1586 SDValue DestOp;
1587 if (Type == VT)
1588 return Op;
1589 auto Size = Op.getValueSizeInBits();
1590 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1591 if (DestOp.getValueType() == VT)
1592 return DestOp;
1593
1594 return getZExtOrTrunc(DestOp, DL, VT);
1595}
1596
1598 EVT OpVT) {
1599 if (VT.bitsLE(Op.getValueType()))
1600 return getNode(ISD::TRUNCATE, SL, VT, Op);
1601
1602 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1603 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1604}
1605
1607 EVT OpVT = Op.getValueType();
1608 assert(VT.isInteger() && OpVT.isInteger() &&
1609 "Cannot getZeroExtendInReg FP types");
1610 assert(VT.isVector() == OpVT.isVector() &&
1611 "getZeroExtendInReg type should be vector iff the operand "
1612 "type is vector!");
1613 assert((!VT.isVector() ||
1615 "Vector element counts must match in getZeroExtendInReg");
1616 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1617 if (OpVT == VT)
1618 return Op;
1619 // TODO: Use computeKnownBits instead of AssertZext.
1620 if (Op.getOpcode() == ISD::AssertZext && cast<VTSDNode>(Op.getOperand(1))
1621 ->getVT()
1622 .getScalarType()
1623 .bitsLE(VT.getScalarType()))
1624 return Op;
1626 VT.getScalarSizeInBits());
1627 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1628}
1629
1631 SDValue EVL, const SDLoc &DL,
1632 EVT VT) {
1633 EVT OpVT = Op.getValueType();
1634 assert(VT.isInteger() && OpVT.isInteger() &&
1635 "Cannot getVPZeroExtendInReg FP types");
1636 assert(VT.isVector() && OpVT.isVector() &&
1637 "getVPZeroExtendInReg type and operand type should be vector!");
1639 "Vector element counts must match in getZeroExtendInReg");
1640 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1641 if (OpVT == VT)
1642 return Op;
1644 VT.getScalarSizeInBits());
1645 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1646 EVL);
1647}
1648
1650 // Only unsigned pointer semantics are supported right now. In the future this
1651 // might delegate to TLI to check pointer signedness.
1652 return getZExtOrTrunc(Op, DL, VT);
1653}
1654
1656 // Only unsigned pointer semantics are supported right now. In the future this
1657 // might delegate to TLI to check pointer signedness.
1658 return getZeroExtendInReg(Op, DL, VT);
1659}
1660
1662 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1663}
1664
1665/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1667 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1668}
1669
1671 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1672 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1673}
1674
1676 SDValue Mask, SDValue EVL, EVT VT) {
1677 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1678 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1679}
1680
1682 SDValue Mask, SDValue EVL) {
1683 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1684}
1685
1687 SDValue Mask, SDValue EVL) {
1688 if (VT.bitsGT(Op.getValueType()))
1689 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1690 if (VT.bitsLT(Op.getValueType()))
1691 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1692 return Op;
1693}
1694
1696 EVT OpVT) {
1697 if (!V)
1698 return getConstant(0, DL, VT);
1699
1700 switch (TLI->getBooleanContents(OpVT)) {
1703 return getConstant(1, DL, VT);
1705 return getAllOnesConstant(DL, VT);
1706 }
1707 llvm_unreachable("Unexpected boolean content enum!");
1708}
1709
1711 bool isT, bool isO) {
1712 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1713 DL, VT, isT, isO);
1714}
1715
1717 bool isT, bool isO) {
1718 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1719}
1720
1722 EVT VT, bool isT, bool isO) {
1723 assert(VT.isInteger() && "Cannot create FP integer constant!");
1724
1725 EVT EltVT = VT.getScalarType();
1726 const ConstantInt *Elt = &Val;
1727
1728 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1729 // to-be-splatted scalar ConstantInt.
1730 if (isa<VectorType>(Elt->getType()))
1731 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1732
1733 // In some cases the vector type is legal but the element type is illegal and
1734 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1735 // inserted value (the type does not need to match the vector element type).
1736 // Any extra bits introduced will be truncated away.
1737 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1739 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1740 APInt NewVal;
1741 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1742 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1743 else
1744 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1745 Elt = ConstantInt::get(*getContext(), NewVal);
1746 }
1747 // In other cases the element type is illegal and needs to be expanded, for
1748 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1749 // the value into n parts and use a vector type with n-times the elements.
1750 // Then bitcast to the type requested.
1751 // Legalizing constants too early makes the DAGCombiner's job harder so we
1752 // only legalize if the DAG tells us we must produce legal types.
1753 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1754 TLI->getTypeAction(*getContext(), EltVT) ==
1756 const APInt &NewVal = Elt->getValue();
1757 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1758 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1759
1760 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1761 if (VT.isScalableVector() ||
1762 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1763 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1764 "Can only handle an even split!");
1765 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1766
1767 SmallVector<SDValue, 2> ScalarParts;
1768 for (unsigned i = 0; i != Parts; ++i)
1769 ScalarParts.push_back(getConstant(
1770 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1771 ViaEltVT, isT, isO));
1772
1773 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1774 }
1775
1776 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1777 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1778
1779 // Check the temporary vector is the correct size. If this fails then
1780 // getTypeToTransformTo() probably returned a type whose size (in bits)
1781 // isn't a power-of-2 factor of the requested type size.
1782 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1783
1784 SmallVector<SDValue, 2> EltParts;
1785 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1786 EltParts.push_back(getConstant(
1787 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1788 ViaEltVT, isT, isO));
1789
1790 // EltParts is currently in little endian order. If we actually want
1791 // big-endian order then reverse it now.
1792 if (getDataLayout().isBigEndian())
1793 std::reverse(EltParts.begin(), EltParts.end());
1794
1795 // The elements must be reversed when the element order is different
1796 // to the endianness of the elements (because the BITCAST is itself a
1797 // vector shuffle in this situation). However, we do not need any code to
1798 // perform this reversal because getConstant() is producing a vector
1799 // splat.
1800 // This situation occurs in MIPS MSA.
1801
1803 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1804 llvm::append_range(Ops, EltParts);
1805
1806 SDValue V =
1807 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1808 return V;
1809 }
1810
1811 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1812 "APInt size does not match type size!");
1813 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1814 SDVTList VTs = getVTList(EltVT);
1816 AddNodeIDNode(ID, Opc, VTs, {});
1817 ID.AddPointer(Elt);
1818 ID.AddBoolean(isO);
1819 void *IP = nullptr;
1820 SDNode *N = nullptr;
1821 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1822 if (!VT.isVector())
1823 return SDValue(N, 0);
1824
1825 if (!N) {
1826 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1827 CSEMap.InsertNode(N, IP);
1828 InsertNode(N);
1829 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1830 }
1831
1832 SDValue Result(N, 0);
1833 if (VT.isVector())
1834 Result = getSplat(VT, DL, Result);
1835 return Result;
1836}
1837
1839 bool isT, bool isO) {
1840 unsigned Size = VT.getScalarSizeInBits();
1841 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1842}
1843
1845 bool IsOpaque) {
1847 IsTarget, IsOpaque);
1848}
1849
1851 bool isTarget) {
1852 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1853}
1854
1856 const SDLoc &DL) {
1857 assert(VT.isInteger() && "Shift amount is not an integer type!");
1858 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1859 return getConstant(Val, DL, ShiftVT);
1860}
1861
1863 const SDLoc &DL) {
1864 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1865 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1866}
1867
1869 bool isTarget) {
1870 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1871}
1872
1874 bool isTarget) {
1875 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1876}
1877
1879 EVT VT, bool isTarget) {
1880 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1881
1882 EVT EltVT = VT.getScalarType();
1883 const ConstantFP *Elt = &V;
1884
1885 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1886 // the to-be-splatted scalar ConstantFP.
1887 if (isa<VectorType>(Elt->getType()))
1888 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1889
1890 // Do the map lookup using the actual bit pattern for the floating point
1891 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1892 // we don't have issues with SNANs.
1893 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1894 SDVTList VTs = getVTList(EltVT);
1896 AddNodeIDNode(ID, Opc, VTs, {});
1897 ID.AddPointer(Elt);
1898 void *IP = nullptr;
1899 SDNode *N = nullptr;
1900 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1901 if (!VT.isVector())
1902 return SDValue(N, 0);
1903
1904 if (!N) {
1905 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1906 CSEMap.InsertNode(N, IP);
1907 InsertNode(N);
1908 }
1909
1910 SDValue Result(N, 0);
1911 if (VT.isVector())
1912 Result = getSplat(VT, DL, Result);
1913 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1914 return Result;
1915}
1916
1918 bool isTarget) {
1919 EVT EltVT = VT.getScalarType();
1920 if (EltVT == MVT::f32)
1921 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1922 if (EltVT == MVT::f64)
1923 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1924 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1925 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1926 bool Ignored;
1927 APFloat APF = APFloat(Val);
1929 &Ignored);
1930 return getConstantFP(APF, DL, VT, isTarget);
1931 }
1932 llvm_unreachable("Unsupported type in getConstantFP");
1933}
1934
1936 EVT VT, int64_t Offset, bool isTargetGA,
1937 unsigned TargetFlags) {
1938 assert((TargetFlags == 0 || isTargetGA) &&
1939 "Cannot set target flags on target-independent globals");
1940
1941 // Truncate (with sign-extension) the offset value to the pointer size.
1943 if (BitWidth < 64)
1945
1946 unsigned Opc;
1947 if (GV->isThreadLocal())
1949 else
1951
1952 SDVTList VTs = getVTList(VT);
1954 AddNodeIDNode(ID, Opc, VTs, {});
1955 ID.AddPointer(GV);
1956 ID.AddInteger(Offset);
1957 ID.AddInteger(TargetFlags);
1958 void *IP = nullptr;
1959 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1960 return SDValue(E, 0);
1961
1962 auto *N = newSDNode<GlobalAddressSDNode>(
1963 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1964 CSEMap.InsertNode(N, IP);
1965 InsertNode(N);
1966 return SDValue(N, 0);
1967}
1968
1970 SDVTList VTs = getVTList(MVT::Untyped);
1973 ID.AddPointer(GV);
1974 void *IP = nullptr;
1975 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP))
1976 return SDValue(E, 0);
1977
1978 auto *N = newSDNode<DeactivationSymbolSDNode>(GV, VTs);
1979 CSEMap.InsertNode(N, IP);
1980 InsertNode(N);
1981 return SDValue(N, 0);
1982}
1983
1984SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1985 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1986 SDVTList VTs = getVTList(VT);
1988 AddNodeIDNode(ID, Opc, VTs, {});
1989 ID.AddInteger(FI);
1990 void *IP = nullptr;
1991 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1992 return SDValue(E, 0);
1993
1994 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1995 CSEMap.InsertNode(N, IP);
1996 InsertNode(N);
1997 return SDValue(N, 0);
1998}
1999
2000SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
2001 unsigned TargetFlags) {
2002 assert((TargetFlags == 0 || isTarget) &&
2003 "Cannot set target flags on target-independent jump tables");
2004 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
2005 SDVTList VTs = getVTList(VT);
2007 AddNodeIDNode(ID, Opc, VTs, {});
2008 ID.AddInteger(JTI);
2009 ID.AddInteger(TargetFlags);
2010 void *IP = nullptr;
2011 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2012 return SDValue(E, 0);
2013
2014 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
2015 CSEMap.InsertNode(N, IP);
2016 InsertNode(N);
2017 return SDValue(N, 0);
2018}
2019
2021 const SDLoc &DL) {
2023 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Other, Chain,
2024 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
2025}
2026
2028 MaybeAlign Alignment, int Offset,
2029 bool isTarget, unsigned TargetFlags) {
2030 assert((TargetFlags == 0 || isTarget) &&
2031 "Cannot set target flags on target-independent globals");
2032 if (!Alignment)
2033 Alignment = shouldOptForSize()
2034 ? getDataLayout().getABITypeAlign(C->getType())
2035 : getDataLayout().getPrefTypeAlign(C->getType());
2036 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2037 SDVTList VTs = getVTList(VT);
2039 AddNodeIDNode(ID, Opc, VTs, {});
2040 ID.AddInteger(Alignment->value());
2041 ID.AddInteger(Offset);
2042 ID.AddPointer(C);
2043 ID.AddInteger(TargetFlags);
2044 void *IP = nullptr;
2045 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2046 return SDValue(E, 0);
2047
2048 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2049 TargetFlags);
2050 CSEMap.InsertNode(N, IP);
2051 InsertNode(N);
2052 SDValue V = SDValue(N, 0);
2053 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
2054 return V;
2055}
2056
2058 MaybeAlign Alignment, int Offset,
2059 bool isTarget, unsigned TargetFlags) {
2060 assert((TargetFlags == 0 || isTarget) &&
2061 "Cannot set target flags on target-independent globals");
2062 if (!Alignment)
2063 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2064 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2065 SDVTList VTs = getVTList(VT);
2067 AddNodeIDNode(ID, Opc, VTs, {});
2068 ID.AddInteger(Alignment->value());
2069 ID.AddInteger(Offset);
2070 C->addSelectionDAGCSEId(ID);
2071 ID.AddInteger(TargetFlags);
2072 void *IP = nullptr;
2073 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2074 return SDValue(E, 0);
2075
2076 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2077 TargetFlags);
2078 CSEMap.InsertNode(N, IP);
2079 InsertNode(N);
2080 return SDValue(N, 0);
2081}
2082
2085 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2086 ID.AddPointer(MBB);
2087 void *IP = nullptr;
2088 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2089 return SDValue(E, 0);
2090
2091 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2092 CSEMap.InsertNode(N, IP);
2093 InsertNode(N);
2094 return SDValue(N, 0);
2095}
2096
2098 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2099 ValueTypeNodes.size())
2100 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2101
2102 SDNode *&N = VT.isExtended() ?
2103 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2104
2105 if (N) return SDValue(N, 0);
2106 N = newSDNode<VTSDNode>(VT);
2107 InsertNode(N);
2108 return SDValue(N, 0);
2109}
2110
2112 SDNode *&N = ExternalSymbols[Sym];
2113 if (N) return SDValue(N, 0);
2114 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2115 InsertNode(N);
2116 return SDValue(N, 0);
2117}
2118
2119SDValue SelectionDAG::getExternalSymbol(RTLIB::LibcallImpl Libcall, EVT VT) {
2121 return getExternalSymbol(SymName.data(), VT);
2122}
2123
2125 SDNode *&N = MCSymbols[Sym];
2126 if (N)
2127 return SDValue(N, 0);
2128 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2129 InsertNode(N);
2130 return SDValue(N, 0);
2131}
2132
2134 unsigned TargetFlags) {
2135 SDNode *&N =
2136 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2137 if (N) return SDValue(N, 0);
2138 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2139 InsertNode(N);
2140 return SDValue(N, 0);
2141}
2142
2144 EVT VT, unsigned TargetFlags) {
2146 return getTargetExternalSymbol(SymName.data(), VT, TargetFlags);
2147}
2148
2150 if ((unsigned)Cond >= CondCodeNodes.size())
2151 CondCodeNodes.resize(Cond+1);
2152
2153 if (!CondCodeNodes[Cond]) {
2154 auto *N = newSDNode<CondCodeSDNode>(Cond);
2155 CondCodeNodes[Cond] = N;
2156 InsertNode(N);
2157 }
2158
2159 return SDValue(CondCodeNodes[Cond], 0);
2160}
2161
2163 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2164 "APInt size does not match type size!");
2165
2166 if (MulImm == 0)
2167 return getConstant(0, DL, VT);
2168
2169 const MachineFunction &MF = getMachineFunction();
2170 const Function &F = MF.getFunction();
2171 ConstantRange CR = getVScaleRange(&F, 64);
2172 if (const APInt *C = CR.getSingleElement())
2173 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2174
2175 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2176}
2177
2178/// \returns a value of type \p VT that represents the runtime value of \p
2179/// Quantity, i.e. scaled by vscale if it's scalable, or a fixed constant
2180/// otherwise. Quantity should be a FixedOrScalableQuantity, i.e. ElementCount
2181/// or TypeSize.
2182template <typename Ty>
2184 EVT VT, Ty Quantity) {
2185 if (Quantity.isScalable())
2186 return DAG.getVScale(
2187 DL, VT, APInt(VT.getSizeInBits(), Quantity.getKnownMinValue()));
2188
2189 return DAG.getConstant(Quantity.getKnownMinValue(), DL, VT);
2190}
2191
2193 ElementCount EC) {
2194 return getFixedOrScalableQuantity(*this, DL, VT, EC);
2195}
2196
2198 return getFixedOrScalableQuantity(*this, DL, VT, TS);
2199}
2200
2202 ElementCount EC) {
2203 EVT IdxVT = TLI->getVectorIdxTy(getDataLayout());
2204 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), DataVT);
2205 return getNode(ISD::GET_ACTIVE_LANE_MASK, DL, MaskVT,
2206 getConstant(0, DL, IdxVT), getElementCount(DL, IdxVT, EC));
2207}
2208
2210 APInt One(ResVT.getScalarSizeInBits(), 1);
2211 return getStepVector(DL, ResVT, One);
2212}
2213
2215 const APInt &StepVal) {
2216 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2217 if (ResVT.isScalableVector())
2218 return getNode(
2219 ISD::STEP_VECTOR, DL, ResVT,
2220 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2221
2222 SmallVector<SDValue, 16> OpsStepConstants;
2223 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2224 OpsStepConstants.push_back(
2225 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2226 return getBuildVector(ResVT, DL, OpsStepConstants);
2227}
2228
2229/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2230/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2235
2237 SDValue N2, ArrayRef<int> Mask) {
2238 assert(VT.getVectorNumElements() == Mask.size() &&
2239 "Must have the same number of vector elements as mask elements!");
2240 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2241 "Invalid VECTOR_SHUFFLE");
2242
2243 // Canonicalize shuffle undef, undef -> undef
2244 if (N1.isUndef() && N2.isUndef())
2245 return getUNDEF(VT);
2246
2247 // Validate that all indices in Mask are within the range of the elements
2248 // input to the shuffle.
2249 int NElts = Mask.size();
2250 assert(llvm::all_of(Mask,
2251 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2252 "Index out of range");
2253
2254 // Copy the mask so we can do any needed cleanup.
2255 SmallVector<int, 8> MaskVec(Mask);
2256
2257 // Canonicalize shuffle v, v -> v, undef
2258 if (N1 == N2) {
2259 N2 = getUNDEF(VT);
2260 for (int i = 0; i != NElts; ++i)
2261 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2262 }
2263
2264 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2265 if (N1.isUndef())
2266 commuteShuffle(N1, N2, MaskVec);
2267
2268 if (TLI->hasVectorBlend()) {
2269 // If shuffling a splat, try to blend the splat instead. We do this here so
2270 // that even when this arises during lowering we don't have to re-handle it.
2271 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2272 BitVector UndefElements;
2273 SDValue Splat = BV->getSplatValue(&UndefElements);
2274 if (!Splat)
2275 return;
2276
2277 for (int i = 0; i < NElts; ++i) {
2278 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2279 continue;
2280
2281 // If this input comes from undef, mark it as such.
2282 if (UndefElements[MaskVec[i] - Offset]) {
2283 MaskVec[i] = -1;
2284 continue;
2285 }
2286
2287 // If we can blend a non-undef lane, use that instead.
2288 if (!UndefElements[i])
2289 MaskVec[i] = i + Offset;
2290 }
2291 };
2292 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2293 BlendSplat(N1BV, 0);
2294 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2295 BlendSplat(N2BV, NElts);
2296 }
2297
2298 // Canonicalize all index into lhs, -> shuffle lhs, undef
2299 // Canonicalize all index into rhs, -> shuffle rhs, undef
2300 bool AllLHS = true, AllRHS = true;
2301 bool N2Undef = N2.isUndef();
2302 for (int i = 0; i != NElts; ++i) {
2303 if (MaskVec[i] >= NElts) {
2304 if (N2Undef)
2305 MaskVec[i] = -1;
2306 else
2307 AllLHS = false;
2308 } else if (MaskVec[i] >= 0) {
2309 AllRHS = false;
2310 }
2311 }
2312 if (AllLHS && AllRHS)
2313 return getUNDEF(VT);
2314 if (AllLHS && !N2Undef)
2315 N2 = getUNDEF(VT);
2316 if (AllRHS) {
2317 N1 = getUNDEF(VT);
2318 commuteShuffle(N1, N2, MaskVec);
2319 }
2320 // Reset our undef status after accounting for the mask.
2321 N2Undef = N2.isUndef();
2322 // Re-check whether both sides ended up undef.
2323 if (N1.isUndef() && N2Undef)
2324 return getUNDEF(VT);
2325
2326 // If Identity shuffle return that node.
2327 bool Identity = true, AllSame = true;
2328 for (int i = 0; i != NElts; ++i) {
2329 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2330 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2331 }
2332 if (Identity && NElts)
2333 return N1;
2334
2335 // Shuffling a constant splat doesn't change the result.
2336 if (N2Undef) {
2337 SDValue V = N1;
2338
2339 // Look through any bitcasts. We check that these don't change the number
2340 // (and size) of elements and just changes their types.
2341 while (V.getOpcode() == ISD::BITCAST)
2342 V = V->getOperand(0);
2343
2344 // A splat should always show up as a build vector node.
2345 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2346 BitVector UndefElements;
2347 SDValue Splat = BV->getSplatValue(&UndefElements);
2348 // If this is a splat of an undef, shuffling it is also undef.
2349 if (Splat && Splat.isUndef())
2350 return getUNDEF(VT);
2351
2352 bool SameNumElts =
2353 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2354
2355 // We only have a splat which can skip shuffles if there is a splatted
2356 // value and no undef lanes rearranged by the shuffle.
2357 if (Splat && UndefElements.none()) {
2358 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2359 // number of elements match or the value splatted is a zero constant.
2360 if (SameNumElts || isNullConstant(Splat))
2361 return N1;
2362 }
2363
2364 // If the shuffle itself creates a splat, build the vector directly.
2365 if (AllSame && SameNumElts) {
2366 EVT BuildVT = BV->getValueType(0);
2367 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2368 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2369
2370 // We may have jumped through bitcasts, so the type of the
2371 // BUILD_VECTOR may not match the type of the shuffle.
2372 if (BuildVT != VT)
2373 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2374 return NewBV;
2375 }
2376 }
2377 }
2378
2379 SDVTList VTs = getVTList(VT);
2381 SDValue Ops[2] = { N1, N2 };
2383 for (int i = 0; i != NElts; ++i)
2384 ID.AddInteger(MaskVec[i]);
2385
2386 void* IP = nullptr;
2387 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2388 return SDValue(E, 0);
2389
2390 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2391 // SDNode doesn't have access to it. This memory will be "leaked" when
2392 // the node is deallocated, but recovered when the NodeAllocator is released.
2393 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2394 llvm::copy(MaskVec, MaskAlloc);
2395
2396 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2397 dl.getDebugLoc(), MaskAlloc);
2398 createOperands(N, Ops);
2399
2400 CSEMap.InsertNode(N, IP);
2401 InsertNode(N);
2402 SDValue V = SDValue(N, 0);
2403 NewSDValueDbgMsg(V, "Creating new node: ", this);
2404 return V;
2405}
2406
2408 EVT VT = SV.getValueType(0);
2409 SmallVector<int, 8> MaskVec(SV.getMask());
2411
2412 SDValue Op0 = SV.getOperand(0);
2413 SDValue Op1 = SV.getOperand(1);
2414 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2415}
2416
2418 SDVTList VTs = getVTList(VT);
2420 AddNodeIDNode(ID, ISD::Register, VTs, {});
2421 ID.AddInteger(Reg.id());
2422 void *IP = nullptr;
2423 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2424 return SDValue(E, 0);
2425
2426 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2427 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2428 CSEMap.InsertNode(N, IP);
2429 InsertNode(N);
2430 return SDValue(N, 0);
2431}
2432
2435 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2436 ID.AddPointer(RegMask);
2437 void *IP = nullptr;
2438 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2439 return SDValue(E, 0);
2440
2441 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2442 CSEMap.InsertNode(N, IP);
2443 InsertNode(N);
2444 return SDValue(N, 0);
2445}
2446
2448 MCSymbol *Label) {
2449 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2450}
2451
2452SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2453 SDValue Root, MCSymbol *Label) {
2455 SDValue Ops[] = { Root };
2456 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2457 ID.AddPointer(Label);
2458 void *IP = nullptr;
2459 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2460 return SDValue(E, 0);
2461
2462 auto *N =
2463 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2464 createOperands(N, Ops);
2465
2466 CSEMap.InsertNode(N, IP);
2467 InsertNode(N);
2468 return SDValue(N, 0);
2469}
2470
2472 int64_t Offset, bool isTarget,
2473 unsigned TargetFlags) {
2474 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2475 SDVTList VTs = getVTList(VT);
2476
2478 AddNodeIDNode(ID, Opc, VTs, {});
2479 ID.AddPointer(BA);
2480 ID.AddInteger(Offset);
2481 ID.AddInteger(TargetFlags);
2482 void *IP = nullptr;
2483 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2484 return SDValue(E, 0);
2485
2486 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2487 CSEMap.InsertNode(N, IP);
2488 InsertNode(N);
2489 return SDValue(N, 0);
2490}
2491
2494 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2495 ID.AddPointer(V);
2496
2497 void *IP = nullptr;
2498 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2499 return SDValue(E, 0);
2500
2501 auto *N = newSDNode<SrcValueSDNode>(V);
2502 CSEMap.InsertNode(N, IP);
2503 InsertNode(N);
2504 return SDValue(N, 0);
2505}
2506
2509 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2510 ID.AddPointer(MD);
2511
2512 void *IP = nullptr;
2513 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2514 return SDValue(E, 0);
2515
2516 auto *N = newSDNode<MDNodeSDNode>(MD);
2517 CSEMap.InsertNode(N, IP);
2518 InsertNode(N);
2519 return SDValue(N, 0);
2520}
2521
2523 if (VT == V.getValueType())
2524 return V;
2525
2526 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2527}
2528
2530 unsigned SrcAS, unsigned DestAS) {
2531 SDVTList VTs = getVTList(VT);
2532 SDValue Ops[] = {Ptr};
2535 ID.AddInteger(SrcAS);
2536 ID.AddInteger(DestAS);
2537
2538 void *IP = nullptr;
2539 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2540 return SDValue(E, 0);
2541
2542 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2543 VTs, SrcAS, DestAS);
2544 createOperands(N, Ops);
2545
2546 CSEMap.InsertNode(N, IP);
2547 InsertNode(N);
2548 return SDValue(N, 0);
2549}
2550
2552 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2553}
2554
2556 bool PoisonOnly) {
2557 if (isGuaranteedNotToBeUndefOrPoison(V, DemandedElts, PoisonOnly))
2558 return V;
2559 return getFreeze(V);
2560}
2561
2562/// getShiftAmountOperand - Return the specified value casted to
2563/// the target's desired shift amount type.
2565 EVT OpTy = Op.getValueType();
2566 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2567 if (OpTy == ShTy || OpTy.isVector()) return Op;
2568
2569 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2570}
2571
2573 SDLoc dl(Node);
2575 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2576 EVT VT = Node->getValueType(0);
2577 SDValue Tmp1 = Node->getOperand(0);
2578 SDValue Tmp2 = Node->getOperand(1);
2579 const MaybeAlign MA(Node->getConstantOperandVal(3));
2580
2581 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2582 Tmp2, MachinePointerInfo(V));
2583 SDValue VAList = VAListLoad;
2584
2585 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2586 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2587 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2588
2589 VAList = getNode(
2590 ISD::AND, dl, VAList.getValueType(), VAList,
2591 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2592 }
2593
2594 // Increment the pointer, VAList, to the next vaarg
2595 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2596 getConstant(getDataLayout().getTypeAllocSize(
2597 VT.getTypeForEVT(*getContext())),
2598 dl, VAList.getValueType()));
2599 // Store the incremented VAList to the legalized pointer
2600 Tmp1 =
2601 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2602 // Load the actual argument out of the pointer VAList
2603 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2604}
2605
2607 SDLoc dl(Node);
2609 // This defaults to loading a pointer from the input and storing it to the
2610 // output, returning the chain.
2611 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2612 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2613 SDValue Tmp1 =
2614 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2615 Node->getOperand(2), MachinePointerInfo(VS));
2616 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2617 MachinePointerInfo(VD));
2618}
2619
2621 const DataLayout &DL = getDataLayout();
2622 Type *Ty = VT.getTypeForEVT(*getContext());
2623 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2624
2625 if (TLI->isTypeLegal(VT) || !VT.isVector())
2626 return RedAlign;
2627
2628 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2629 const Align StackAlign = TFI->getStackAlign();
2630
2631 // See if we can choose a smaller ABI alignment in cases where it's an
2632 // illegal vector type that will get broken down.
2633 if (RedAlign > StackAlign) {
2634 EVT IntermediateVT;
2635 MVT RegisterVT;
2636 unsigned NumIntermediates;
2637 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2638 NumIntermediates, RegisterVT);
2639 Ty = IntermediateVT.getTypeForEVT(*getContext());
2640 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2641 if (RedAlign2 < RedAlign)
2642 RedAlign = RedAlign2;
2643
2644 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2645 // If the stack is not realignable, the alignment should be limited to the
2646 // StackAlignment
2647 RedAlign = std::min(RedAlign, StackAlign);
2648 }
2649
2650 return RedAlign;
2651}
2652
2654 MachineFrameInfo &MFI = MF->getFrameInfo();
2655 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2656 int StackID = 0;
2657 if (Bytes.isScalable())
2658 StackID = TFI->getStackIDForScalableVectors();
2659 // The stack id gives an indication of whether the object is scalable or
2660 // not, so it's safe to pass in the minimum size here.
2661 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2662 false, nullptr, StackID);
2663 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2664}
2665
2667 Type *Ty = VT.getTypeForEVT(*getContext());
2668 Align StackAlign =
2669 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2670 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2671}
2672
2674 TypeSize VT1Size = VT1.getStoreSize();
2675 TypeSize VT2Size = VT2.getStoreSize();
2676 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2677 "Don't know how to choose the maximum size when creating a stack "
2678 "temporary");
2679 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2680 ? VT1Size
2681 : VT2Size;
2682
2683 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2684 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2685 const DataLayout &DL = getDataLayout();
2686 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2687 return CreateStackTemporary(Bytes, Align);
2688}
2689
2691 ISD::CondCode Cond, const SDLoc &dl,
2692 SDNodeFlags Flags) {
2693 EVT OpVT = N1.getValueType();
2694
2695 auto GetUndefBooleanConstant = [&]() {
2696 if (VT.getScalarType() == MVT::i1 ||
2697 TLI->getBooleanContents(OpVT) ==
2699 return getUNDEF(VT);
2700 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2701 // so we cannot use getUNDEF(). Return zero instead.
2702 return getConstant(0, dl, VT);
2703 };
2704
2705 // These setcc operations always fold.
2706 switch (Cond) {
2707 default: break;
2708 case ISD::SETFALSE:
2709 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2710 case ISD::SETTRUE:
2711 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2712
2713 case ISD::SETOEQ:
2714 case ISD::SETOGT:
2715 case ISD::SETOGE:
2716 case ISD::SETOLT:
2717 case ISD::SETOLE:
2718 case ISD::SETONE:
2719 case ISD::SETO:
2720 case ISD::SETUO:
2721 case ISD::SETUEQ:
2722 case ISD::SETUNE:
2723 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2724 break;
2725 }
2726
2727 if (OpVT.isInteger()) {
2728 // For EQ and NE, we can always pick a value for the undef to make the
2729 // predicate pass or fail, so we can return undef.
2730 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2731 // icmp eq/ne X, undef -> undef.
2732 if ((N1.isUndef() || N2.isUndef()) &&
2733 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2734 return GetUndefBooleanConstant();
2735
2736 // If both operands are undef, we can return undef for int comparison.
2737 // icmp undef, undef -> undef.
2738 if (N1.isUndef() && N2.isUndef())
2739 return GetUndefBooleanConstant();
2740
2741 // icmp X, X -> true/false
2742 // icmp X, undef -> true/false because undef could be X.
2743 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2744 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2745 }
2746
2748 const APInt &C2 = N2C->getAPIntValue();
2750 const APInt &C1 = N1C->getAPIntValue();
2751
2753 dl, VT, OpVT);
2754 }
2755 }
2756
2757 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2758 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2759
2760 if (N1CFP && N2CFP) {
2761 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2762 switch (Cond) {
2763 default: break;
2764 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2765 return GetUndefBooleanConstant();
2766 [[fallthrough]];
2767 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2768 OpVT);
2769 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2770 return GetUndefBooleanConstant();
2771 [[fallthrough]];
2773 R==APFloat::cmpLessThan, dl, VT,
2774 OpVT);
2775 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2776 return GetUndefBooleanConstant();
2777 [[fallthrough]];
2778 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2779 OpVT);
2780 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2781 return GetUndefBooleanConstant();
2782 [[fallthrough]];
2784 VT, OpVT);
2785 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2786 return GetUndefBooleanConstant();
2787 [[fallthrough]];
2789 R==APFloat::cmpEqual, dl, VT,
2790 OpVT);
2791 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2792 return GetUndefBooleanConstant();
2793 [[fallthrough]];
2795 R==APFloat::cmpEqual, dl, VT, OpVT);
2796 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2797 OpVT);
2798 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2799 OpVT);
2801 R==APFloat::cmpEqual, dl, VT,
2802 OpVT);
2803 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2804 OpVT);
2806 R==APFloat::cmpLessThan, dl, VT,
2807 OpVT);
2809 R==APFloat::cmpUnordered, dl, VT,
2810 OpVT);
2812 VT, OpVT);
2813 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2814 OpVT);
2815 }
2816 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2817 // Ensure that the constant occurs on the RHS.
2819 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2820 return SDValue();
2821 return getSetCC(dl, VT, N2, N1, SwappedCond, /*Chian=*/{},
2822 /*IsSignaling=*/false, Flags);
2823 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2824 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2825 // If an operand is known to be a nan (or undef that could be a nan), we can
2826 // fold it.
2827 // Choosing NaN for the undef will always make unordered comparison succeed
2828 // and ordered comparison fails.
2829 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2830 switch (ISD::getUnorderedFlavor(Cond)) {
2831 default:
2832 llvm_unreachable("Unknown flavor!");
2833 case 0: // Known false.
2834 return getBoolConstant(false, dl, VT, OpVT);
2835 case 1: // Known true.
2836 return getBoolConstant(true, dl, VT, OpVT);
2837 case 2: // Undefined.
2838 return GetUndefBooleanConstant();
2839 }
2840 }
2841
2842 // Could not fold it.
2843 return SDValue();
2844}
2845
2846/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2847/// use this predicate to simplify operations downstream.
2849 unsigned BitWidth = Op.getScalarValueSizeInBits();
2851}
2852
2853// TODO: Should have argument to specify if sign bit of nan is ignorable.
2855 if (Depth >= MaxRecursionDepth)
2856 return false; // Limit search depth.
2857
2858 unsigned Opc = Op.getOpcode();
2859 switch (Opc) {
2860 case ISD::FABS:
2861 return true;
2862 case ISD::AssertNoFPClass: {
2863 FPClassTest NoFPClass =
2864 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2865
2866 const FPClassTest TestMask = fcNan | fcNegative;
2867 return (NoFPClass & TestMask) == TestMask;
2868 }
2869 case ISD::ARITH_FENCE:
2870 return SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2871 case ISD::FEXP:
2872 case ISD::FEXP2:
2873 case ISD::FEXP10:
2874 return Op->getFlags().hasNoNaNs();
2875 case ISD::FMINNUM:
2876 case ISD::FMINNUM_IEEE:
2877 case ISD::FMINIMUM:
2878 case ISD::FMINIMUMNUM:
2879 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2880 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2881 case ISD::FMAXNUM:
2882 case ISD::FMAXNUM_IEEE:
2883 case ISD::FMAXIMUM:
2884 case ISD::FMAXIMUMNUM:
2885 // TODO: If we can ignore the sign bit of nans, only one side being known 0
2886 // is sufficient.
2887 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2888 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2889 default:
2890 return false;
2891 }
2892
2893 llvm_unreachable("covered opcode switch");
2894}
2895
2896/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2897/// this predicate to simplify operations downstream. Mask is known to be zero
2898/// for bits that V cannot have.
2900 unsigned Depth) const {
2901 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2902}
2903
2904/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2905/// DemandedElts. We use this predicate to simplify operations downstream.
2906/// Mask is known to be zero for bits that V cannot have.
2908 const APInt &DemandedElts,
2909 unsigned Depth) const {
2910 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2911}
2912
2913/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2914/// DemandedElts. We use this predicate to simplify operations downstream.
2916 unsigned Depth /* = 0 */) const {
2917 return computeKnownBits(V, DemandedElts, Depth).isZero();
2918}
2919
2920/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2922 unsigned Depth) const {
2923 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2924}
2925
2927 const APInt &DemandedElts,
2928 unsigned Depth) const {
2929 EVT VT = Op.getValueType();
2930 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2931
2932 unsigned NumElts = VT.getVectorNumElements();
2933 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2934
2935 APInt KnownZeroElements = APInt::getZero(NumElts);
2936 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2937 if (!DemandedElts[EltIdx])
2938 continue; // Don't query elements that are not demanded.
2939 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2940 if (MaskedVectorIsZero(Op, Mask, Depth))
2941 KnownZeroElements.setBit(EltIdx);
2942 }
2943 return KnownZeroElements;
2944}
2945
2946/// isSplatValue - Return true if the vector V has the same value
2947/// across all DemandedElts. For scalable vectors, we don't know the
2948/// number of lanes at compile time. Instead, we use a 1 bit APInt
2949/// to represent a conservative value for all lanes; that is, that
2950/// one bit value is implicitly splatted across all lanes.
2951bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2952 APInt &UndefElts, unsigned Depth) const {
2953 unsigned Opcode = V.getOpcode();
2954 EVT VT = V.getValueType();
2955 assert(VT.isVector() && "Vector type expected");
2956 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2957 "scalable demanded bits are ignored");
2958
2959 if (!DemandedElts)
2960 return false; // No demanded elts, better to assume we don't know anything.
2961
2962 if (Depth >= MaxRecursionDepth)
2963 return false; // Limit search depth.
2964
2965 // Deal with some common cases here that work for both fixed and scalable
2966 // vector types.
2967 switch (Opcode) {
2968 case ISD::SPLAT_VECTOR:
2969 UndefElts = V.getOperand(0).isUndef()
2970 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2971 : APInt(DemandedElts.getBitWidth(), 0);
2972 return true;
2973 case ISD::ADD:
2974 case ISD::SUB:
2975 case ISD::AND:
2976 case ISD::XOR:
2977 case ISD::OR: {
2978 APInt UndefLHS, UndefRHS;
2979 SDValue LHS = V.getOperand(0);
2980 SDValue RHS = V.getOperand(1);
2981 // Only recognize splats with the same demanded undef elements for both
2982 // operands, otherwise we might fail to handle binop-specific undef
2983 // handling.
2984 // e.g. (and undef, 0) -> 0 etc.
2985 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2986 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
2987 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
2988 UndefElts = UndefLHS | UndefRHS;
2989 return true;
2990 }
2991 return false;
2992 }
2993 case ISD::ABS:
2994 case ISD::TRUNCATE:
2995 case ISD::SIGN_EXTEND:
2996 case ISD::ZERO_EXTEND:
2997 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2998 default:
2999 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
3000 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
3001 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
3002 Depth);
3003 break;
3004 }
3005
3006 // We don't support other cases than those above for scalable vectors at
3007 // the moment.
3008 if (VT.isScalableVector())
3009 return false;
3010
3011 unsigned NumElts = VT.getVectorNumElements();
3012 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
3013 UndefElts = APInt::getZero(NumElts);
3014
3015 switch (Opcode) {
3016 case ISD::BUILD_VECTOR: {
3017 SDValue Scl;
3018 for (unsigned i = 0; i != NumElts; ++i) {
3019 SDValue Op = V.getOperand(i);
3020 if (Op.isUndef()) {
3021 UndefElts.setBit(i);
3022 continue;
3023 }
3024 if (!DemandedElts[i])
3025 continue;
3026 if (Scl && Scl != Op)
3027 return false;
3028 Scl = Op;
3029 }
3030 return true;
3031 }
3032 case ISD::VECTOR_SHUFFLE: {
3033 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
3034 APInt DemandedLHS = APInt::getZero(NumElts);
3035 APInt DemandedRHS = APInt::getZero(NumElts);
3036 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
3037 for (int i = 0; i != (int)NumElts; ++i) {
3038 int M = Mask[i];
3039 if (M < 0) {
3040 UndefElts.setBit(i);
3041 continue;
3042 }
3043 if (!DemandedElts[i])
3044 continue;
3045 if (M < (int)NumElts)
3046 DemandedLHS.setBit(M);
3047 else
3048 DemandedRHS.setBit(M - NumElts);
3049 }
3050
3051 // If we aren't demanding either op, assume there's no splat.
3052 // If we are demanding both ops, assume there's no splat.
3053 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
3054 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3055 return false;
3056
3057 // See if the demanded elts of the source op is a splat or we only demand
3058 // one element, which should always be a splat.
3059 // TODO: Handle source ops splats with undefs.
3060 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3061 APInt SrcUndefs;
3062 return (SrcElts.popcount() == 1) ||
3063 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3064 (SrcElts & SrcUndefs).isZero());
3065 };
3066 if (!DemandedLHS.isZero())
3067 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3068 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3069 }
3071 // Offset the demanded elts by the subvector index.
3072 SDValue Src = V.getOperand(0);
3073 // We don't support scalable vectors at the moment.
3074 if (Src.getValueType().isScalableVector())
3075 return false;
3076 uint64_t Idx = V.getConstantOperandVal(1);
3077 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3078 APInt UndefSrcElts;
3079 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3080 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3081 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3082 return true;
3083 }
3084 break;
3085 }
3089 // Widen the demanded elts by the src element count.
3090 SDValue Src = V.getOperand(0);
3091 // We don't support scalable vectors at the moment.
3092 if (Src.getValueType().isScalableVector())
3093 return false;
3094 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3095 APInt UndefSrcElts;
3096 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3097 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3098 UndefElts = UndefSrcElts.trunc(NumElts);
3099 return true;
3100 }
3101 break;
3102 }
3103 case ISD::BITCAST: {
3104 SDValue Src = V.getOperand(0);
3105 EVT SrcVT = Src.getValueType();
3106 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3107 unsigned BitWidth = VT.getScalarSizeInBits();
3108
3109 // Ignore bitcasts from unsupported types.
3110 // TODO: Add fp support?
3111 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3112 break;
3113
3114 // Bitcast 'small element' vector to 'large element' vector.
3115 if ((BitWidth % SrcBitWidth) == 0) {
3116 // See if each sub element is a splat.
3117 unsigned Scale = BitWidth / SrcBitWidth;
3118 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3119 APInt ScaledDemandedElts =
3120 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3121 for (unsigned I = 0; I != Scale; ++I) {
3122 APInt SubUndefElts;
3123 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3124 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3125 SubDemandedElts &= ScaledDemandedElts;
3126 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3127 return false;
3128 // TODO: Add support for merging sub undef elements.
3129 if (!SubUndefElts.isZero())
3130 return false;
3131 }
3132 return true;
3133 }
3134 break;
3135 }
3136 }
3137
3138 return false;
3139}
3140
3141/// Helper wrapper to main isSplatValue function.
3142bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3143 EVT VT = V.getValueType();
3144 assert(VT.isVector() && "Vector type expected");
3145
3146 APInt UndefElts;
3147 // Since the number of lanes in a scalable vector is unknown at compile time,
3148 // we track one bit which is implicitly broadcast to all lanes. This means
3149 // that all lanes in a scalable vector are considered demanded.
3150 APInt DemandedElts
3152 return isSplatValue(V, DemandedElts, UndefElts) &&
3153 (AllowUndefs || !UndefElts);
3154}
3155
3158
3159 EVT VT = V.getValueType();
3160 unsigned Opcode = V.getOpcode();
3161 switch (Opcode) {
3162 default: {
3163 APInt UndefElts;
3164 // Since the number of lanes in a scalable vector is unknown at compile time,
3165 // we track one bit which is implicitly broadcast to all lanes. This means
3166 // that all lanes in a scalable vector are considered demanded.
3167 APInt DemandedElts
3169
3170 if (isSplatValue(V, DemandedElts, UndefElts)) {
3171 if (VT.isScalableVector()) {
3172 // DemandedElts and UndefElts are ignored for scalable vectors, since
3173 // the only supported cases are SPLAT_VECTOR nodes.
3174 SplatIdx = 0;
3175 } else {
3176 // Handle case where all demanded elements are UNDEF.
3177 if (DemandedElts.isSubsetOf(UndefElts)) {
3178 SplatIdx = 0;
3179 return getUNDEF(VT);
3180 }
3181 SplatIdx = (UndefElts & DemandedElts).countr_one();
3182 }
3183 return V;
3184 }
3185 break;
3186 }
3187 case ISD::SPLAT_VECTOR:
3188 SplatIdx = 0;
3189 return V;
3190 case ISD::VECTOR_SHUFFLE: {
3191 assert(!VT.isScalableVector());
3192 // Check if this is a shuffle node doing a splat.
3193 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3194 // getTargetVShiftNode currently struggles without the splat source.
3195 auto *SVN = cast<ShuffleVectorSDNode>(V);
3196 if (!SVN->isSplat())
3197 break;
3198 int Idx = SVN->getSplatIndex();
3199 int NumElts = V.getValueType().getVectorNumElements();
3200 SplatIdx = Idx % NumElts;
3201 return V.getOperand(Idx / NumElts);
3202 }
3203 }
3204
3205 return SDValue();
3206}
3207
3209 int SplatIdx;
3210 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3211 EVT SVT = SrcVector.getValueType().getScalarType();
3212 EVT LegalSVT = SVT;
3213 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3214 if (!SVT.isInteger())
3215 return SDValue();
3216 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3217 if (LegalSVT.bitsLT(SVT))
3218 return SDValue();
3219 }
3220 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3221 }
3222 return SDValue();
3223}
3224
3225std::optional<ConstantRange>
3227 unsigned Depth) const {
3228 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3229 V.getOpcode() == ISD::SRA) &&
3230 "Unknown shift node");
3231 // Shifting more than the bitwidth is not valid.
3232 unsigned BitWidth = V.getScalarValueSizeInBits();
3233
3234 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3235 const APInt &ShAmt = Cst->getAPIntValue();
3236 if (ShAmt.uge(BitWidth))
3237 return std::nullopt;
3238 return ConstantRange(ShAmt);
3239 }
3240
3241 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3242 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3243 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3244 if (!DemandedElts[i])
3245 continue;
3246 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3247 if (!SA) {
3248 MinAmt = MaxAmt = nullptr;
3249 break;
3250 }
3251 const APInt &ShAmt = SA->getAPIntValue();
3252 if (ShAmt.uge(BitWidth))
3253 return std::nullopt;
3254 if (!MinAmt || MinAmt->ugt(ShAmt))
3255 MinAmt = &ShAmt;
3256 if (!MaxAmt || MaxAmt->ult(ShAmt))
3257 MaxAmt = &ShAmt;
3258 }
3259 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3260 "Failed to find matching min/max shift amounts");
3261 if (MinAmt && MaxAmt)
3262 return ConstantRange(*MinAmt, *MaxAmt + 1);
3263 }
3264
3265 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3266 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3267 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3268 if (KnownAmt.getMaxValue().ult(BitWidth))
3269 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3270
3271 return std::nullopt;
3272}
3273
3274std::optional<unsigned>
3276 unsigned Depth) const {
3277 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3278 V.getOpcode() == ISD::SRA) &&
3279 "Unknown shift node");
3280 if (std::optional<ConstantRange> AmtRange =
3281 getValidShiftAmountRange(V, DemandedElts, Depth))
3282 if (const APInt *ShAmt = AmtRange->getSingleElement())
3283 return ShAmt->getZExtValue();
3284 return std::nullopt;
3285}
3286
3287std::optional<unsigned>
3289 APInt DemandedElts = getDemandAllEltsMask(V);
3290 return getValidShiftAmount(V, DemandedElts, Depth);
3291}
3292
3293std::optional<unsigned>
3295 unsigned Depth) const {
3296 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3297 V.getOpcode() == ISD::SRA) &&
3298 "Unknown shift node");
3299 if (std::optional<ConstantRange> AmtRange =
3300 getValidShiftAmountRange(V, DemandedElts, Depth))
3301 return AmtRange->getUnsignedMin().getZExtValue();
3302 return std::nullopt;
3303}
3304
3305std::optional<unsigned>
3307 APInt DemandedElts = getDemandAllEltsMask(V);
3308 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3309}
3310
3311std::optional<unsigned>
3313 unsigned Depth) const {
3314 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3315 V.getOpcode() == ISD::SRA) &&
3316 "Unknown shift node");
3317 if (std::optional<ConstantRange> AmtRange =
3318 getValidShiftAmountRange(V, DemandedElts, Depth))
3319 return AmtRange->getUnsignedMax().getZExtValue();
3320 return std::nullopt;
3321}
3322
3323std::optional<unsigned>
3325 APInt DemandedElts = getDemandAllEltsMask(V);
3326 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3327}
3328
3329/// Determine which bits of Op are known to be either zero or one and return
3330/// them in Known. For vectors, the known bits are those that are shared by
3331/// every vector element.
3333 APInt DemandedElts = getDemandAllEltsMask(Op);
3334 return computeKnownBits(Op, DemandedElts, Depth);
3335}
3336
3337/// Determine which bits of Op are known to be either zero or one and return
3338/// them in Known. The DemandedElts argument allows us to only collect the known
3339/// bits that are shared by the requested vector elements.
3341 unsigned Depth) const {
3342 unsigned BitWidth = Op.getScalarValueSizeInBits();
3343
3344 KnownBits Known(BitWidth); // Don't know anything.
3345
3346 if (auto OptAPInt = Op->bitcastToAPInt()) {
3347 // We know all of the bits for a constant!
3348 return KnownBits::makeConstant(*std::move(OptAPInt));
3349 }
3350
3351 if (Depth >= MaxRecursionDepth)
3352 return Known; // Limit search depth.
3353
3354 KnownBits Known2;
3355 unsigned NumElts = DemandedElts.getBitWidth();
3356 assert((!Op.getValueType().isScalableVector() || NumElts == 1) &&
3357 "DemandedElts for scalable vectors must be 1 to represent all lanes");
3358 assert((!Op.getValueType().isFixedLengthVector() ||
3359 NumElts == Op.getValueType().getVectorNumElements()) &&
3360 "Unexpected vector size");
3361
3362 if (!DemandedElts)
3363 return Known; // No demanded elts, better to assume we don't know anything.
3364
3365 unsigned Opcode = Op.getOpcode();
3366 switch (Opcode) {
3367 case ISD::MERGE_VALUES:
3368 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3369 Depth + 1);
3370 case ISD::SPLAT_VECTOR: {
3371 SDValue SrcOp = Op.getOperand(0);
3372 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3373 "Expected SPLAT_VECTOR implicit truncation");
3374 // Implicitly truncate the bits to match the official semantics of
3375 // SPLAT_VECTOR.
3376 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3377 break;
3378 }
3380 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3381 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3382 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3383 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3384 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3385 }
3386 break;
3387 }
3388 case ISD::STEP_VECTOR: {
3389 const APInt &Step = Op.getConstantOperandAPInt(0);
3390
3391 if (Step.isPowerOf2())
3392 Known.Zero.setLowBits(Step.logBase2());
3393
3395
3396 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3397 break;
3398 const APInt MinNumElts =
3399 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3400
3401 bool Overflow;
3402 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3404 .umul_ov(MinNumElts, Overflow);
3405 if (Overflow)
3406 break;
3407
3408 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3409 if (Overflow)
3410 break;
3411
3412 Known.Zero.setHighBits(MaxValue.countl_zero());
3413 break;
3414 }
3415 case ISD::BUILD_VECTOR:
3416 assert(!Op.getValueType().isScalableVector());
3417 // Collect the known bits that are shared by every demanded vector element.
3418 Known.setAllConflict();
3419 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3420 if (!DemandedElts[i])
3421 continue;
3422
3423 SDValue SrcOp = Op.getOperand(i);
3424 Known2 = computeKnownBits(SrcOp, Depth + 1);
3425
3426 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3427 if (SrcOp.getValueSizeInBits() != BitWidth) {
3428 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3429 "Expected BUILD_VECTOR implicit truncation");
3430 Known2 = Known2.trunc(BitWidth);
3431 }
3432
3433 // Known bits are the values that are shared by every demanded element.
3434 Known = Known.intersectWith(Known2);
3435
3436 // If we don't know any bits, early out.
3437 if (Known.isUnknown())
3438 break;
3439 }
3440 break;
3441 case ISD::VECTOR_COMPRESS: {
3442 SDValue Vec = Op.getOperand(0);
3443 SDValue PassThru = Op.getOperand(2);
3444 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3445 // If we don't know any bits, early out.
3446 if (Known.isUnknown())
3447 break;
3448 Known2 = computeKnownBits(Vec, Depth + 1);
3449 Known = Known.intersectWith(Known2);
3450 break;
3451 }
3452 case ISD::VECTOR_SHUFFLE: {
3453 assert(!Op.getValueType().isScalableVector());
3454 // Collect the known bits that are shared by every vector element referenced
3455 // by the shuffle.
3456 APInt DemandedLHS, DemandedRHS;
3458 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3459 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3460 DemandedLHS, DemandedRHS))
3461 break;
3462
3463 // Known bits are the values that are shared by every demanded element.
3464 Known.setAllConflict();
3465 if (!!DemandedLHS) {
3466 SDValue LHS = Op.getOperand(0);
3467 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3468 Known = Known.intersectWith(Known2);
3469 }
3470 // If we don't know any bits, early out.
3471 if (Known.isUnknown())
3472 break;
3473 if (!!DemandedRHS) {
3474 SDValue RHS = Op.getOperand(1);
3475 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3476 Known = Known.intersectWith(Known2);
3477 }
3478 break;
3479 }
3480 case ISD::VSCALE: {
3482 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3483 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3484 break;
3485 }
3486 case ISD::CONCAT_VECTORS: {
3487 if (Op.getValueType().isScalableVector())
3488 break;
3489 // Split DemandedElts and test each of the demanded subvectors.
3490 Known.setAllConflict();
3491 EVT SubVectorVT = Op.getOperand(0).getValueType();
3492 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3493 unsigned NumSubVectors = Op.getNumOperands();
3494 for (unsigned i = 0; i != NumSubVectors; ++i) {
3495 APInt DemandedSub =
3496 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3497 if (!!DemandedSub) {
3498 SDValue Sub = Op.getOperand(i);
3499 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3500 Known = Known.intersectWith(Known2);
3501 }
3502 // If we don't know any bits, early out.
3503 if (Known.isUnknown())
3504 break;
3505 }
3506 break;
3507 }
3508 case ISD::INSERT_SUBVECTOR: {
3509 if (Op.getValueType().isScalableVector())
3510 break;
3511 // Demand any elements from the subvector and the remainder from the src its
3512 // inserted into.
3513 SDValue Src = Op.getOperand(0);
3514 SDValue Sub = Op.getOperand(1);
3515 uint64_t Idx = Op.getConstantOperandVal(2);
3516 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3517 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3518 APInt DemandedSrcElts = DemandedElts;
3519 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3520
3521 Known.setAllConflict();
3522 if (!!DemandedSubElts) {
3523 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3524 if (Known.isUnknown())
3525 break; // early-out.
3526 }
3527 if (!!DemandedSrcElts) {
3528 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3529 Known = Known.intersectWith(Known2);
3530 }
3531 break;
3532 }
3534 // Offset the demanded elts by the subvector index.
3535 SDValue Src = Op.getOperand(0);
3536
3537 APInt DemandedSrcElts;
3538 if (Src.getValueType().isScalableVector())
3539 DemandedSrcElts = APInt(1, 1); // <=> 'demand all elements'
3540 else {
3541 uint64_t Idx = Op.getConstantOperandVal(1);
3542 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3543 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3544 }
3545 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3546 break;
3547 }
3548 case ISD::SCALAR_TO_VECTOR: {
3549 if (Op.getValueType().isScalableVector())
3550 break;
3551 // We know about scalar_to_vector as much as we know about it source,
3552 // which becomes the first element of otherwise unknown vector.
3553 if (DemandedElts != 1)
3554 break;
3555
3556 SDValue N0 = Op.getOperand(0);
3557 Known = computeKnownBits(N0, Depth + 1);
3558 if (N0.getValueSizeInBits() != BitWidth)
3559 Known = Known.trunc(BitWidth);
3560
3561 break;
3562 }
3563 case ISD::BITCAST: {
3564 if (Op.getValueType().isScalableVector())
3565 break;
3566
3567 SDValue N0 = Op.getOperand(0);
3568 EVT SubVT = N0.getValueType();
3569 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3570
3571 // Ignore bitcasts from unsupported types.
3572 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3573 break;
3574
3575 // Fast handling of 'identity' bitcasts.
3576 if (BitWidth == SubBitWidth) {
3577 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3578 break;
3579 }
3580
3581 bool IsLE = getDataLayout().isLittleEndian();
3582
3583 // Bitcast 'small element' vector to 'large element' scalar/vector.
3584 if ((BitWidth % SubBitWidth) == 0) {
3585 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3586
3587 // Collect known bits for the (larger) output by collecting the known
3588 // bits from each set of sub elements and shift these into place.
3589 // We need to separately call computeKnownBits for each set of
3590 // sub elements as the knownbits for each is likely to be different.
3591 unsigned SubScale = BitWidth / SubBitWidth;
3592 APInt SubDemandedElts(NumElts * SubScale, 0);
3593 for (unsigned i = 0; i != NumElts; ++i)
3594 if (DemandedElts[i])
3595 SubDemandedElts.setBit(i * SubScale);
3596
3597 for (unsigned i = 0; i != SubScale; ++i) {
3598 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3599 Depth + 1);
3600 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3601 Known.insertBits(Known2, SubBitWidth * Shifts);
3602 }
3603 }
3604
3605 // Bitcast 'large element' scalar/vector to 'small element' vector.
3606 if ((SubBitWidth % BitWidth) == 0) {
3607 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3608
3609 // Collect known bits for the (smaller) output by collecting the known
3610 // bits from the overlapping larger input elements and extracting the
3611 // sub sections we actually care about.
3612 unsigned SubScale = SubBitWidth / BitWidth;
3613 APInt SubDemandedElts =
3614 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3615 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3616
3617 Known.setAllConflict();
3618 for (unsigned i = 0; i != NumElts; ++i)
3619 if (DemandedElts[i]) {
3620 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3621 unsigned Offset = (Shifts % SubScale) * BitWidth;
3622 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3623 // If we don't know any bits, early out.
3624 if (Known.isUnknown())
3625 break;
3626 }
3627 }
3628 break;
3629 }
3630 case ISD::AND:
3631 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3632 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3633
3634 Known &= Known2;
3635 break;
3636 case ISD::OR:
3637 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3638 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3639
3640 Known |= Known2;
3641 break;
3642 case ISD::XOR:
3643 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3644 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3645
3646 Known ^= Known2;
3647 break;
3648 case ISD::MUL: {
3649 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3650 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3651 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3652 // TODO: SelfMultiply can be poison, but not undef.
3653 if (SelfMultiply)
3654 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3655 Op.getOperand(0), DemandedElts, false, Depth + 1);
3656 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3657
3658 // If the multiplication is known not to overflow, the product of a number
3659 // with itself is non-negative. Only do this if we didn't already computed
3660 // the opposite value for the sign bit.
3661 if (Op->getFlags().hasNoSignedWrap() &&
3662 Op.getOperand(0) == Op.getOperand(1) &&
3663 !Known.isNegative())
3664 Known.makeNonNegative();
3665 break;
3666 }
3667 case ISD::MULHU: {
3668 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3669 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3670 Known = KnownBits::mulhu(Known, Known2);
3671 break;
3672 }
3673 case ISD::MULHS: {
3674 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3675 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3676 Known = KnownBits::mulhs(Known, Known2);
3677 break;
3678 }
3679 case ISD::ABDU: {
3680 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3681 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3682 Known = KnownBits::abdu(Known, Known2);
3683 break;
3684 }
3685 case ISD::ABDS: {
3686 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3687 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3688 Known = KnownBits::abds(Known, Known2);
3689 unsigned SignBits1 =
3690 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3691 if (SignBits1 == 1)
3692 break;
3693 unsigned SignBits0 =
3694 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3695 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3696 break;
3697 }
3698 case ISD::UMUL_LOHI: {
3699 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3700 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3701 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3702 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3703 if (Op.getResNo() == 0)
3704 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3705 else
3706 Known = KnownBits::mulhu(Known, Known2);
3707 break;
3708 }
3709 case ISD::SMUL_LOHI: {
3710 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3711 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3712 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3713 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3714 if (Op.getResNo() == 0)
3715 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3716 else
3717 Known = KnownBits::mulhs(Known, Known2);
3718 break;
3719 }
3720 case ISD::AVGFLOORU: {
3721 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3722 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3723 Known = KnownBits::avgFloorU(Known, Known2);
3724 break;
3725 }
3726 case ISD::AVGCEILU: {
3727 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3728 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3729 Known = KnownBits::avgCeilU(Known, Known2);
3730 break;
3731 }
3732 case ISD::AVGFLOORS: {
3733 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3734 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3735 Known = KnownBits::avgFloorS(Known, Known2);
3736 break;
3737 }
3738 case ISD::AVGCEILS: {
3739 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3740 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3741 Known = KnownBits::avgCeilS(Known, Known2);
3742 break;
3743 }
3744 case ISD::SELECT:
3745 case ISD::VSELECT:
3746 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3747 // If we don't know any bits, early out.
3748 if (Known.isUnknown())
3749 break;
3750 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3751
3752 // Only known if known in both the LHS and RHS.
3753 Known = Known.intersectWith(Known2);
3754 break;
3755 case ISD::SELECT_CC:
3756 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3757 // If we don't know any bits, early out.
3758 if (Known.isUnknown())
3759 break;
3760 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3761
3762 // Only known if known in both the LHS and RHS.
3763 Known = Known.intersectWith(Known2);
3764 break;
3765 case ISD::SMULO:
3766 case ISD::UMULO:
3767 if (Op.getResNo() != 1)
3768 break;
3769 // The boolean result conforms to getBooleanContents.
3770 // If we know the result of a setcc has the top bits zero, use this info.
3771 // We know that we have an integer-based boolean since these operations
3772 // are only available for integer.
3773 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3775 BitWidth > 1)
3776 Known.Zero.setBitsFrom(1);
3777 break;
3778 case ISD::SETCC:
3779 case ISD::SETCCCARRY:
3780 case ISD::STRICT_FSETCC:
3781 case ISD::STRICT_FSETCCS: {
3782 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3783 // If we know the result of a setcc has the top bits zero, use this info.
3784 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3786 BitWidth > 1)
3787 Known.Zero.setBitsFrom(1);
3788 break;
3789 }
3790 case ISD::SHL: {
3791 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3792 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3793
3794 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3795 bool NSW = Op->getFlags().hasNoSignedWrap();
3796
3797 bool ShAmtNonZero = Known2.isNonZero();
3798
3799 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3800
3801 // Minimum shift low bits are known zero.
3802 if (std::optional<unsigned> ShMinAmt =
3803 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3804 Known.Zero.setLowBits(*ShMinAmt);
3805 break;
3806 }
3807 case ISD::SRL:
3808 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3809 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3810 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3811 Op->getFlags().hasExact());
3812
3813 // Minimum shift high bits are known zero.
3814 if (std::optional<unsigned> ShMinAmt =
3815 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3816 Known.Zero.setHighBits(*ShMinAmt);
3817 break;
3818 case ISD::SRA:
3819 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3820 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3821 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3822 Op->getFlags().hasExact());
3823 break;
3824 case ISD::ROTL:
3825 case ISD::ROTR:
3826 if (ConstantSDNode *C =
3827 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3828 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3829
3830 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3831
3832 // Canonicalize to ROTR.
3833 if (Opcode == ISD::ROTL && Amt != 0)
3834 Amt = BitWidth - Amt;
3835
3836 Known.Zero = Known.Zero.rotr(Amt);
3837 Known.One = Known.One.rotr(Amt);
3838 }
3839 break;
3840 case ISD::FSHL:
3841 case ISD::FSHR:
3842 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3843 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3844
3845 // For fshl, 0-shift returns the 1st arg.
3846 // For fshr, 0-shift returns the 2nd arg.
3847 if (Amt == 0) {
3848 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3849 DemandedElts, Depth + 1);
3850 break;
3851 }
3852
3853 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3854 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3855 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3856 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3857 if (Opcode == ISD::FSHL) {
3858 Known <<= Amt;
3859 Known2 >>= BitWidth - Amt;
3860 } else {
3861 Known <<= BitWidth - Amt;
3862 Known2 >>= Amt;
3863 }
3864 Known = Known.unionWith(Known2);
3865 }
3866 break;
3867 case ISD::SHL_PARTS:
3868 case ISD::SRA_PARTS:
3869 case ISD::SRL_PARTS: {
3870 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3871
3872 // Collect lo/hi source values and concatenate.
3873 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3874 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3875 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3876 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3877 Known = Known2.concat(Known);
3878
3879 // Collect shift amount.
3880 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3881
3882 if (Opcode == ISD::SHL_PARTS)
3883 Known = KnownBits::shl(Known, Known2);
3884 else if (Opcode == ISD::SRA_PARTS)
3885 Known = KnownBits::ashr(Known, Known2);
3886 else // if (Opcode == ISD::SRL_PARTS)
3887 Known = KnownBits::lshr(Known, Known2);
3888
3889 // TODO: Minimum shift low/high bits are known zero.
3890
3891 if (Op.getResNo() == 0)
3892 Known = Known.extractBits(LoBits, 0);
3893 else
3894 Known = Known.extractBits(HiBits, LoBits);
3895 break;
3896 }
3898 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3899 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3900 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3901 break;
3902 }
3903 case ISD::CTTZ:
3904 case ISD::CTTZ_ZERO_UNDEF: {
3905 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3906 // If we have a known 1, its position is our upper bound.
3907 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3908 unsigned LowBits = llvm::bit_width(PossibleTZ);
3909 Known.Zero.setBitsFrom(LowBits);
3910 break;
3911 }
3912 case ISD::CTLZ:
3913 case ISD::CTLZ_ZERO_UNDEF: {
3914 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3915 // If we have a known 1, its position is our upper bound.
3916 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3917 unsigned LowBits = llvm::bit_width(PossibleLZ);
3918 Known.Zero.setBitsFrom(LowBits);
3919 break;
3920 }
3921 case ISD::CTLS: {
3922 unsigned MinRedundantSignBits =
3923 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1;
3924 ConstantRange Range(APInt(BitWidth, MinRedundantSignBits),
3926 Known = Range.toKnownBits();
3927 break;
3928 }
3929 case ISD::CTPOP: {
3930 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3931 // If we know some of the bits are zero, they can't be one.
3932 unsigned PossibleOnes = Known2.countMaxPopulation();
3933 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3934 break;
3935 }
3936 case ISD::PARITY: {
3937 // Parity returns 0 everywhere but the LSB.
3938 Known.Zero.setBitsFrom(1);
3939 break;
3940 }
3941 case ISD::CLMUL: {
3942 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3943 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3944 Known = KnownBits::clmul(Known, Known2);
3945 break;
3946 }
3947 case ISD::MGATHER:
3948 case ISD::MLOAD: {
3949 ISD::LoadExtType ETy =
3950 (Opcode == ISD::MGATHER)
3951 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3952 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3953 if (ETy == ISD::ZEXTLOAD) {
3954 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3955 KnownBits Known0(MemVT.getScalarSizeInBits());
3956 return Known0.zext(BitWidth);
3957 }
3958 break;
3959 }
3960 case ISD::LOAD: {
3962 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3963 if (ISD::isNON_EXTLoad(LD) && Cst) {
3964 // Determine any common known bits from the loaded constant pool value.
3965 Type *CstTy = Cst->getType();
3966 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3967 !Op.getValueType().isScalableVector()) {
3968 // If its a vector splat, then we can (quickly) reuse the scalar path.
3969 // NOTE: We assume all elements match and none are UNDEF.
3970 if (CstTy->isVectorTy()) {
3971 if (const Constant *Splat = Cst->getSplatValue()) {
3972 Cst = Splat;
3973 CstTy = Cst->getType();
3974 }
3975 }
3976 // TODO - do we need to handle different bitwidths?
3977 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3978 // Iterate across all vector elements finding common known bits.
3979 Known.setAllConflict();
3980 for (unsigned i = 0; i != NumElts; ++i) {
3981 if (!DemandedElts[i])
3982 continue;
3983 if (Constant *Elt = Cst->getAggregateElement(i)) {
3984 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3985 const APInt &Value = CInt->getValue();
3986 Known.One &= Value;
3987 Known.Zero &= ~Value;
3988 continue;
3989 }
3990 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3991 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3992 Known.One &= Value;
3993 Known.Zero &= ~Value;
3994 continue;
3995 }
3996 }
3997 Known.One.clearAllBits();
3998 Known.Zero.clearAllBits();
3999 break;
4000 }
4001 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
4002 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
4003 Known = KnownBits::makeConstant(CInt->getValue());
4004 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
4005 Known =
4006 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
4007 }
4008 }
4009 }
4010 } else if (Op.getResNo() == 0) {
4011 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
4012 KnownBits KnownScalarMemory(ScalarMemorySize);
4013 if (const MDNode *MD = LD->getRanges())
4014 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4015
4016 // Extend the Known bits from memory to the size of the scalar result.
4017 if (ISD::isZEXTLoad(Op.getNode()))
4018 Known = KnownScalarMemory.zext(BitWidth);
4019 else if (ISD::isSEXTLoad(Op.getNode()))
4020 Known = KnownScalarMemory.sext(BitWidth);
4021 else if (ISD::isEXTLoad(Op.getNode()))
4022 Known = KnownScalarMemory.anyext(BitWidth);
4023 else
4024 Known = KnownScalarMemory;
4025 assert(Known.getBitWidth() == BitWidth);
4026 return Known;
4027 }
4028 break;
4029 }
4031 if (Op.getValueType().isScalableVector())
4032 break;
4033 EVT InVT = Op.getOperand(0).getValueType();
4034 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4035 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4036 Known = Known.zext(BitWidth);
4037 break;
4038 }
4039 case ISD::ZERO_EXTEND: {
4040 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4041 Known = Known.zext(BitWidth);
4042 break;
4043 }
4045 if (Op.getValueType().isScalableVector())
4046 break;
4047 EVT InVT = Op.getOperand(0).getValueType();
4048 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4049 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4050 // If the sign bit is known to be zero or one, then sext will extend
4051 // it to the top bits, else it will just zext.
4052 Known = Known.sext(BitWidth);
4053 break;
4054 }
4055 case ISD::SIGN_EXTEND: {
4056 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4057 // If the sign bit is known to be zero or one, then sext will extend
4058 // it to the top bits, else it will just zext.
4059 Known = Known.sext(BitWidth);
4060 break;
4061 }
4063 if (Op.getValueType().isScalableVector())
4064 break;
4065 EVT InVT = Op.getOperand(0).getValueType();
4066 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4067 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4068 Known = Known.anyext(BitWidth);
4069 break;
4070 }
4071 case ISD::ANY_EXTEND: {
4072 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4073 Known = Known.anyext(BitWidth);
4074 break;
4075 }
4076 case ISD::TRUNCATE: {
4077 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4078 Known = Known.trunc(BitWidth);
4079 break;
4080 }
4081 case ISD::TRUNCATE_SSAT_S: {
4082 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4083 Known = Known.truncSSat(BitWidth);
4084 break;
4085 }
4086 case ISD::TRUNCATE_SSAT_U: {
4087 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4088 Known = Known.truncSSatU(BitWidth);
4089 break;
4090 }
4091 case ISD::TRUNCATE_USAT_U: {
4092 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4093 Known = Known.truncUSat(BitWidth);
4094 break;
4095 }
4096 case ISD::AssertZext: {
4097 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4099 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4100 Known.Zero |= (~InMask);
4101 Known.One &= (~Known.Zero);
4102 break;
4103 }
4104 case ISD::AssertAlign: {
4105 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4106 assert(LogOfAlign != 0);
4107
4108 // TODO: Should use maximum with source
4109 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4110 // well as clearing one bits.
4111 Known.Zero.setLowBits(LogOfAlign);
4112 Known.One.clearLowBits(LogOfAlign);
4113 break;
4114 }
4115 case ISD::AssertNoFPClass: {
4116 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4117
4118 FPClassTest NoFPClass =
4119 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4120 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4121 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4122 // Cannot be negative.
4123 Known.makeNonNegative();
4124 }
4125
4126 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4127 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4128 // Cannot be positive.
4129 Known.makeNegative();
4130 }
4131
4132 break;
4133 }
4134 case ISD::FABS:
4135 // fabs clears the sign bit
4136 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4137 Known.makeNonNegative();
4138 break;
4139 case ISD::FGETSIGN:
4140 // All bits are zero except the low bit.
4141 Known.Zero.setBitsFrom(1);
4142 break;
4143 case ISD::ADD: {
4144 SDNodeFlags Flags = Op.getNode()->getFlags();
4145 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4146 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4147 bool SelfAdd = Op.getOperand(0) == Op.getOperand(1) &&
4149 Op.getOperand(0), DemandedElts, false, Depth + 1);
4150 Known = KnownBits::add(Known, Known2, Flags.hasNoSignedWrap(),
4151 Flags.hasNoUnsignedWrap(), SelfAdd);
4152 break;
4153 }
4154 case ISD::SUB: {
4155 SDNodeFlags Flags = Op.getNode()->getFlags();
4156 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4157 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4158 Known = KnownBits::sub(Known, Known2, Flags.hasNoSignedWrap(),
4159 Flags.hasNoUnsignedWrap());
4160 break;
4161 }
4162 case ISD::USUBO:
4163 case ISD::SSUBO:
4164 case ISD::USUBO_CARRY:
4165 case ISD::SSUBO_CARRY:
4166 if (Op.getResNo() == 1) {
4167 // If we know the result of a setcc has the top bits zero, use this info.
4168 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4170 BitWidth > 1)
4171 Known.Zero.setBitsFrom(1);
4172 break;
4173 }
4174 [[fallthrough]];
4175 case ISD::SUBC: {
4176 assert(Op.getResNo() == 0 &&
4177 "We only compute knownbits for the difference here.");
4178
4179 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4180 KnownBits Borrow(1);
4181 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4182 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4183 // Borrow has bit width 1
4184 Borrow = Borrow.trunc(1);
4185 } else {
4186 Borrow.setAllZero();
4187 }
4188
4189 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4190 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4191 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4192 break;
4193 }
4194 case ISD::UADDO:
4195 case ISD::SADDO:
4196 case ISD::UADDO_CARRY:
4197 case ISD::SADDO_CARRY:
4198 if (Op.getResNo() == 1) {
4199 // If we know the result of a setcc has the top bits zero, use this info.
4200 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4202 BitWidth > 1)
4203 Known.Zero.setBitsFrom(1);
4204 break;
4205 }
4206 [[fallthrough]];
4207 case ISD::ADDC:
4208 case ISD::ADDE: {
4209 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4210
4211 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4212 KnownBits Carry(1);
4213 if (Opcode == ISD::ADDE)
4214 // Can't track carry from glue, set carry to unknown.
4215 Carry.resetAll();
4216 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4217 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4218 // Carry has bit width 1
4219 Carry = Carry.trunc(1);
4220 } else {
4221 Carry.setAllZero();
4222 }
4223
4224 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4225 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4226 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4227 break;
4228 }
4229 case ISD::UDIV: {
4230 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4231 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4232 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4233 break;
4234 }
4235 case ISD::SDIV: {
4236 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4237 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4238 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4239 break;
4240 }
4241 case ISD::SREM: {
4242 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4243 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4244 Known = KnownBits::srem(Known, Known2);
4245 break;
4246 }
4247 case ISD::UREM: {
4248 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4249 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4250 Known = KnownBits::urem(Known, Known2);
4251 break;
4252 }
4253 case ISD::EXTRACT_ELEMENT: {
4254 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4255 const unsigned Index = Op.getConstantOperandVal(1);
4256 const unsigned EltBitWidth = Op.getValueSizeInBits();
4257
4258 // Remove low part of known bits mask
4259 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4260 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4261
4262 // Remove high part of known bit mask
4263 Known = Known.trunc(EltBitWidth);
4264 break;
4265 }
4267 SDValue InVec = Op.getOperand(0);
4268 SDValue EltNo = Op.getOperand(1);
4269 EVT VecVT = InVec.getValueType();
4270 // computeKnownBits not yet implemented for scalable vectors.
4271 if (VecVT.isScalableVector())
4272 break;
4273 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4274 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4275
4276 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4277 // anything about the extended bits.
4278 if (BitWidth > EltBitWidth)
4279 Known = Known.trunc(EltBitWidth);
4280
4281 // If we know the element index, just demand that vector element, else for
4282 // an unknown element index, ignore DemandedElts and demand them all.
4283 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4284 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4285 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4286 DemandedSrcElts =
4287 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4288
4289 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4290 if (BitWidth > EltBitWidth)
4291 Known = Known.anyext(BitWidth);
4292 break;
4293 }
4295 if (Op.getValueType().isScalableVector())
4296 break;
4297
4298 // If we know the element index, split the demand between the
4299 // source vector and the inserted element, otherwise assume we need
4300 // the original demanded vector elements and the value.
4301 SDValue InVec = Op.getOperand(0);
4302 SDValue InVal = Op.getOperand(1);
4303 SDValue EltNo = Op.getOperand(2);
4304 bool DemandedVal = true;
4305 APInt DemandedVecElts = DemandedElts;
4306 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4307 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4308 unsigned EltIdx = CEltNo->getZExtValue();
4309 DemandedVal = !!DemandedElts[EltIdx];
4310 DemandedVecElts.clearBit(EltIdx);
4311 }
4312 Known.setAllConflict();
4313 if (DemandedVal) {
4314 Known2 = computeKnownBits(InVal, Depth + 1);
4315 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4316 }
4317 if (!!DemandedVecElts) {
4318 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4319 Known = Known.intersectWith(Known2);
4320 }
4321 break;
4322 }
4323 case ISD::BITREVERSE: {
4324 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4325 Known = Known2.reverseBits();
4326 break;
4327 }
4328 case ISD::BSWAP: {
4329 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4330 Known = Known2.byteSwap();
4331 break;
4332 }
4333 case ISD::ABS: {
4334 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4335 Known = Known2.abs();
4336 Known.Zero.setHighBits(
4337 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4338 break;
4339 }
4340 case ISD::USUBSAT: {
4341 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4342 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4343 Known = KnownBits::usub_sat(Known, Known2);
4344 break;
4345 }
4346 case ISD::UMIN: {
4347 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4348 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4349 Known = KnownBits::umin(Known, Known2);
4350 break;
4351 }
4352 case ISD::UMAX: {
4353 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4354 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4355 Known = KnownBits::umax(Known, Known2);
4356 break;
4357 }
4358 case ISD::SMIN:
4359 case ISD::SMAX: {
4360 // If we have a clamp pattern, we know that the number of sign bits will be
4361 // the minimum of the clamp min/max range.
4362 bool IsMax = (Opcode == ISD::SMAX);
4363 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4364 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4365 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4366 CstHigh =
4367 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4368 if (CstLow && CstHigh) {
4369 if (!IsMax)
4370 std::swap(CstLow, CstHigh);
4371
4372 const APInt &ValueLow = CstLow->getAPIntValue();
4373 const APInt &ValueHigh = CstHigh->getAPIntValue();
4374 if (ValueLow.sle(ValueHigh)) {
4375 unsigned LowSignBits = ValueLow.getNumSignBits();
4376 unsigned HighSignBits = ValueHigh.getNumSignBits();
4377 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4378 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4379 Known.One.setHighBits(MinSignBits);
4380 break;
4381 }
4382 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4383 Known.Zero.setHighBits(MinSignBits);
4384 break;
4385 }
4386 }
4387 }
4388
4389 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4390 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4391 if (IsMax)
4392 Known = KnownBits::smax(Known, Known2);
4393 else
4394 Known = KnownBits::smin(Known, Known2);
4395
4396 // For SMAX, if CstLow is non-negative we know the result will be
4397 // non-negative and thus all sign bits are 0.
4398 // TODO: There's an equivalent of this for smin with negative constant for
4399 // known ones.
4400 if (IsMax && CstLow) {
4401 const APInt &ValueLow = CstLow->getAPIntValue();
4402 if (ValueLow.isNonNegative()) {
4403 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4404 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4405 }
4406 }
4407
4408 break;
4409 }
4410 case ISD::UINT_TO_FP: {
4411 Known.makeNonNegative();
4412 break;
4413 }
4414 case ISD::SINT_TO_FP: {
4415 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4416 if (Known2.isNonNegative())
4417 Known.makeNonNegative();
4418 else if (Known2.isNegative())
4419 Known.makeNegative();
4420 break;
4421 }
4422 case ISD::FP_TO_UINT_SAT: {
4423 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4424 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4426 break;
4427 }
4428 case ISD::ATOMIC_LOAD: {
4429 // If we are looking at the loaded value.
4430 if (Op.getResNo() == 0) {
4431 auto *AT = cast<AtomicSDNode>(Op);
4432 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4433 KnownBits KnownScalarMemory(ScalarMemorySize);
4434 if (const MDNode *MD = AT->getRanges())
4435 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4436
4437 switch (AT->getExtensionType()) {
4438 case ISD::ZEXTLOAD:
4439 Known = KnownScalarMemory.zext(BitWidth);
4440 break;
4441 case ISD::SEXTLOAD:
4442 Known = KnownScalarMemory.sext(BitWidth);
4443 break;
4444 case ISD::EXTLOAD:
4445 switch (TLI->getExtendForAtomicOps()) {
4446 case ISD::ZERO_EXTEND:
4447 Known = KnownScalarMemory.zext(BitWidth);
4448 break;
4449 case ISD::SIGN_EXTEND:
4450 Known = KnownScalarMemory.sext(BitWidth);
4451 break;
4452 default:
4453 Known = KnownScalarMemory.anyext(BitWidth);
4454 break;
4455 }
4456 break;
4457 case ISD::NON_EXTLOAD:
4458 Known = KnownScalarMemory;
4459 break;
4460 }
4461 assert(Known.getBitWidth() == BitWidth);
4462 }
4463 break;
4464 }
4466 if (Op.getResNo() == 1) {
4467 // The boolean result conforms to getBooleanContents.
4468 // If we know the result of a setcc has the top bits zero, use this info.
4469 // We know that we have an integer-based boolean since these operations
4470 // are only available for integer.
4471 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4473 BitWidth > 1)
4474 Known.Zero.setBitsFrom(1);
4475 break;
4476 }
4477 [[fallthrough]];
4479 case ISD::ATOMIC_SWAP:
4490 case ISD::ATOMIC_LOAD_UMAX: {
4491 // If we are looking at the loaded value.
4492 if (Op.getResNo() == 0) {
4493 auto *AT = cast<AtomicSDNode>(Op);
4494 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4495
4496 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4497 Known.Zero.setBitsFrom(MemBits);
4498 }
4499 break;
4500 }
4501 case ISD::FrameIndex:
4503 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4504 Known, getMachineFunction());
4505 break;
4506
4507 default:
4508 if (Opcode < ISD::BUILTIN_OP_END)
4509 break;
4510 [[fallthrough]];
4514 // TODO: Probably okay to remove after audit; here to reduce change size
4515 // in initial enablement patch for scalable vectors
4516 if (Op.getValueType().isScalableVector())
4517 break;
4518
4519 // Allow the target to implement this method for its nodes.
4520 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4521 break;
4522 }
4523
4524 return Known;
4525}
4526
4527/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4540
4543 // X + 0 never overflow
4544 if (isNullConstant(N1))
4545 return OFK_Never;
4546
4547 // If both operands each have at least two sign bits, the addition
4548 // cannot overflow.
4549 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4550 return OFK_Never;
4551
4552 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4553 return OFK_Sometime;
4554}
4555
4558 // X + 0 never overflow
4559 if (isNullConstant(N1))
4560 return OFK_Never;
4561
4562 // mulhi + 1 never overflow
4563 KnownBits N1Known = computeKnownBits(N1);
4564 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4565 N1Known.getMaxValue().ult(2))
4566 return OFK_Never;
4567
4568 KnownBits N0Known = computeKnownBits(N0);
4569 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4570 N0Known.getMaxValue().ult(2))
4571 return OFK_Never;
4572
4573 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4574 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4575 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4576 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4577}
4578
4581 // X - 0 never overflow
4582 if (isNullConstant(N1))
4583 return OFK_Never;
4584
4585 // If both operands each have at least two sign bits, the subtraction
4586 // cannot overflow.
4587 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4588 return OFK_Never;
4589
4590 KnownBits N0Known = computeKnownBits(N0);
4591 KnownBits N1Known = computeKnownBits(N1);
4592 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4593 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4594 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4595}
4596
4599 // X - 0 never overflow
4600 if (isNullConstant(N1))
4601 return OFK_Never;
4602
4603 ConstantRange N0Range =
4604 computeConstantRangeIncludingKnownBits(N0, /*ForSigned=*/false);
4605 ConstantRange N1Range =
4606 computeConstantRangeIncludingKnownBits(N1, /*ForSigned=*/false);
4607 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4608}
4609
4612 // X * 0 and X * 1 never overflow.
4613 if (isNullConstant(N1) || isOneConstant(N1))
4614 return OFK_Never;
4615
4618 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4619}
4620
4623 // X * 0 and X * 1 never overflow.
4624 if (isNullConstant(N1) || isOneConstant(N1))
4625 return OFK_Never;
4626
4627 // Get the size of the result.
4628 unsigned BitWidth = N0.getScalarValueSizeInBits();
4629
4630 // Sum of the sign bits.
4631 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4632
4633 // If we have enough sign bits, then there's no overflow.
4634 if (SignBits > BitWidth + 1)
4635 return OFK_Never;
4636
4637 if (SignBits == BitWidth + 1) {
4638 // The overflow occurs when the true multiplication of the
4639 // the operands is the minimum negative number.
4640 KnownBits N0Known = computeKnownBits(N0);
4641 KnownBits N1Known = computeKnownBits(N1);
4642 // If one of the operands is non-negative, then there's no
4643 // overflow.
4644 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4645 return OFK_Never;
4646 }
4647
4648 return OFK_Sometime;
4649}
4650
4652 unsigned Depth) const {
4653 APInt DemandedElts = getDemandAllEltsMask(Op);
4654 return computeConstantRange(Op, DemandedElts, ForSigned, Depth);
4655}
4656
4658 const APInt &DemandedElts,
4659 bool ForSigned,
4660 unsigned Depth) const {
4661 EVT VT = Op.getValueType();
4662 unsigned BitWidth = VT.getScalarSizeInBits();
4663
4664 if (Depth >= MaxRecursionDepth)
4665 return ConstantRange::getFull(BitWidth);
4666
4667 if (ConstantSDNode *C = isConstOrConstSplat(Op, DemandedElts))
4668 return ConstantRange(C->getAPIntValue());
4669
4670 unsigned Opcode = Op.getOpcode();
4671 switch (Opcode) {
4672 case ISD::VSCALE: {
4674 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
4675 return getVScaleRange(&F, BitWidth).multiply(Multiplier);
4676 }
4677 default:
4678 break;
4679 }
4680
4681 return ConstantRange::getFull(BitWidth);
4682}
4683
4686 unsigned Depth) const {
4687 APInt DemandedElts = getDemandAllEltsMask(Op);
4688 return computeConstantRangeIncludingKnownBits(Op, DemandedElts, ForSigned,
4689 Depth);
4690}
4691
4693 SDValue Op, const APInt &DemandedElts, bool ForSigned,
4694 unsigned Depth) const {
4695 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
4696 ConstantRange CR1 = ConstantRange::fromKnownBits(Known, ForSigned);
4697 ConstantRange CR2 = computeConstantRange(Op, DemandedElts, ForSigned, Depth);
4700 return CR1.intersectWith(CR2, RangeType);
4701}
4702
4704 unsigned Depth) const {
4705 APInt DemandedElts = getDemandAllEltsMask(Val);
4706 return isKnownToBeAPowerOfTwo(Val, DemandedElts, OrZero, Depth);
4707}
4708
4710 const APInt &DemandedElts,
4711 bool OrZero, unsigned Depth) const {
4712 if (Depth >= MaxRecursionDepth)
4713 return false; // Limit search depth.
4714
4715 EVT OpVT = Val.getValueType();
4716 unsigned BitWidth = OpVT.getScalarSizeInBits();
4717 [[maybe_unused]] unsigned NumElts = DemandedElts.getBitWidth();
4718 assert((!OpVT.isScalableVector() || NumElts == 1) &&
4719 "DemandedElts for scalable vectors must be 1 to represent all lanes");
4720 assert(
4721 (!OpVT.isFixedLengthVector() || NumElts == OpVT.getVectorNumElements()) &&
4722 "Unexpected vector size");
4723
4724 auto IsPowerOfTwoOrZero = [BitWidth, OrZero](const ConstantSDNode *C) {
4725 APInt V = C->getAPIntValue().zextOrTrunc(BitWidth);
4726 return (OrZero && V.isZero()) || V.isPowerOf2();
4727 };
4728
4729 // Is the constant a known power of 2 or zero?
4730 if (ISD::matchUnaryPredicate(Val, IsPowerOfTwoOrZero))
4731 return true;
4732
4733 switch (Val.getOpcode()) {
4734 case ISD::BUILD_VECTOR:
4735 // Are all operands of a build vector constant powers of two or zero?
4736 if (all_of(enumerate(Val->ops()), [&](auto P) {
4737 auto *C = dyn_cast<ConstantSDNode>(P.value());
4738 return !DemandedElts[P.index()] || (C && IsPowerOfTwoOrZero(C));
4739 }))
4740 return true;
4741 break;
4742
4743 case ISD::SPLAT_VECTOR:
4744 // Is the operand of a splat vector a constant power of two?
4745 if (auto *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4746 if (IsPowerOfTwoOrZero(C))
4747 return true;
4748 break;
4749
4751 SDValue InVec = Val.getOperand(0);
4752 SDValue EltNo = Val.getOperand(1);
4753 EVT VecVT = InVec.getValueType();
4754
4755 // Skip scalable vectors or implicit extensions.
4756 if (VecVT.isScalableVector() ||
4757 OpVT.getScalarSizeInBits() != VecVT.getScalarSizeInBits())
4758 break;
4759
4760 // If we know the element index, just demand that vector element, else for
4761 // an unknown element index, ignore DemandedElts and demand them all.
4762 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4763 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4764 APInt DemandedSrcElts =
4765 ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts)
4766 ? APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue())
4767 : APInt::getAllOnes(NumSrcElts);
4768 return isKnownToBeAPowerOfTwo(InVec, DemandedSrcElts, OrZero, Depth + 1);
4769 }
4770
4771 case ISD::AND: {
4772 // Looking for `x & -x` pattern:
4773 // If x == 0:
4774 // x & -x -> 0
4775 // If x != 0:
4776 // x & -x -> non-zero pow2
4777 // so if we find the pattern return whether we know `x` is non-zero.
4778 SDValue X, Z;
4779 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))) ||
4780 (sd_match(Val, m_And(m_Value(X), m_Sub(m_Value(Z), m_Deferred(X)))) &&
4781 MaskedVectorIsZero(Z, DemandedElts, Depth + 1)))
4782 return OrZero || isKnownNeverZero(X, DemandedElts, Depth);
4783 break;
4784 }
4785
4786 case ISD::SHL: {
4787 // A left-shift of a constant one will have exactly one bit set because
4788 // shifting the bit off the end is undefined.
4789 auto *C = isConstOrConstSplat(Val.getOperand(0), DemandedElts);
4790 if (C && C->getAPIntValue() == 1)
4791 return true;
4792 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4793 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4794 Depth + 1);
4795 }
4796
4797 case ISD::SRL: {
4798 // A logical right-shift of a constant sign-bit will have exactly
4799 // one bit set.
4800 auto *C = isConstOrConstSplat(Val.getOperand(0), DemandedElts);
4801 if (C && C->getAPIntValue().isSignMask())
4802 return true;
4803 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4804 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4805 Depth + 1);
4806 }
4807
4808 case ISD::TRUNCATE:
4809 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4810 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4811 Depth + 1);
4812
4813 case ISD::ROTL:
4814 case ISD::ROTR:
4815 return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4816 Depth + 1);
4817 case ISD::BSWAP:
4818 case ISD::BITREVERSE:
4819 return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4820 Depth + 1);
4821
4822 case ISD::SMIN:
4823 case ISD::SMAX:
4824 case ISD::UMIN:
4825 case ISD::UMAX:
4826 return isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedElts, OrZero,
4827 Depth + 1) &&
4828 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4829 Depth + 1);
4830
4831 case ISD::SELECT:
4832 case ISD::VSELECT:
4833 return isKnownToBeAPowerOfTwo(Val.getOperand(2), DemandedElts, OrZero,
4834 Depth + 1) &&
4835 isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedElts, OrZero,
4836 Depth + 1);
4837
4838 case ISD::ZERO_EXTEND:
4839 return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
4840 Depth + 1);
4841
4842 case ISD::VSCALE:
4843 // vscale(power-of-two) is a power-of-two
4844 return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
4845 Depth + 1);
4846
4847 case ISD::VECTOR_SHUFFLE: {
4849 // Demanded elements with undef shuffle mask elements are unknown
4850 // - we cannot guarantee they are a power of two, so return false.
4851 APInt DemandedLHS, DemandedRHS;
4853 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4854 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4855 DemandedLHS, DemandedRHS))
4856 return false;
4857
4858 // All demanded elements from LHS must be known power of two.
4859 if (!!DemandedLHS && !isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedLHS,
4860 OrZero, Depth + 1))
4861 return false;
4862
4863 // All demanded elements from RHS must be known power of two.
4864 if (!!DemandedRHS && !isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedRHS,
4865 OrZero, Depth + 1))
4866 return false;
4867
4868 return true;
4869 }
4870 }
4871
4872 // More could be done here, though the above checks are enough
4873 // to handle some common cases.
4874 return false;
4875}
4876
4878 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4879 return C1->getValueAPF().getExactLog2Abs() >= 0;
4880
4881 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4882 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4883
4884 return false;
4885}
4886
4888 APInt DemandedElts = getDemandAllEltsMask(Op);
4889 return ComputeNumSignBits(Op, DemandedElts, Depth);
4890}
4891
4892unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4893 unsigned Depth) const {
4894 EVT VT = Op.getValueType();
4895 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4896 unsigned VTBits = VT.getScalarSizeInBits();
4897 unsigned NumElts = DemandedElts.getBitWidth();
4898 unsigned Tmp, Tmp2;
4899 unsigned FirstAnswer = 1;
4900
4901 assert((!VT.isScalableVector() || NumElts == 1) &&
4902 "DemandedElts for scalable vectors must be 1 to represent all lanes");
4903
4904 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4905 const APInt &Val = C->getAPIntValue();
4906 return Val.getNumSignBits();
4907 }
4908
4909 if (Depth >= MaxRecursionDepth)
4910 return 1; // Limit search depth.
4911
4912 if (!DemandedElts)
4913 return 1; // No demanded elts, better to assume we don't know anything.
4914
4915 unsigned Opcode = Op.getOpcode();
4916 switch (Opcode) {
4917 default: break;
4918 case ISD::AssertSext:
4919 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4920 return VTBits-Tmp+1;
4921 case ISD::AssertZext:
4922 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4923 return VTBits-Tmp;
4924 case ISD::FREEZE:
4925 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4926 /*PoisonOnly=*/false))
4927 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4928 break;
4929 case ISD::MERGE_VALUES:
4930 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4931 Depth + 1);
4932 case ISD::SPLAT_VECTOR: {
4933 // Check if the sign bits of source go down as far as the truncated value.
4934 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4935 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4936 if (NumSrcSignBits > (NumSrcBits - VTBits))
4937 return NumSrcSignBits - (NumSrcBits - VTBits);
4938 break;
4939 }
4940 case ISD::BUILD_VECTOR:
4941 assert(!VT.isScalableVector());
4942 Tmp = VTBits;
4943 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4944 if (!DemandedElts[i])
4945 continue;
4946
4947 SDValue SrcOp = Op.getOperand(i);
4948 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4949 // for constant nodes to ensure we only look at the sign bits.
4951 APInt T = C->getAPIntValue().trunc(VTBits);
4952 Tmp2 = T.getNumSignBits();
4953 } else {
4954 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4955
4956 if (SrcOp.getValueSizeInBits() != VTBits) {
4957 assert(SrcOp.getValueSizeInBits() > VTBits &&
4958 "Expected BUILD_VECTOR implicit truncation");
4959 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4960 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4961 }
4962 }
4963 Tmp = std::min(Tmp, Tmp2);
4964 }
4965 return Tmp;
4966
4967 case ISD::VECTOR_COMPRESS: {
4968 SDValue Vec = Op.getOperand(0);
4969 SDValue PassThru = Op.getOperand(2);
4970 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4971 if (Tmp == 1)
4972 return 1;
4973 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4974 Tmp = std::min(Tmp, Tmp2);
4975 return Tmp;
4976 }
4977
4978 case ISD::VECTOR_SHUFFLE: {
4979 // Collect the minimum number of sign bits that are shared by every vector
4980 // element referenced by the shuffle.
4981 APInt DemandedLHS, DemandedRHS;
4983 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4984 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4985 DemandedLHS, DemandedRHS))
4986 return 1;
4987
4988 Tmp = std::numeric_limits<unsigned>::max();
4989 if (!!DemandedLHS)
4990 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4991 if (!!DemandedRHS) {
4992 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4993 Tmp = std::min(Tmp, Tmp2);
4994 }
4995 // If we don't know anything, early out and try computeKnownBits fall-back.
4996 if (Tmp == 1)
4997 break;
4998 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4999 return Tmp;
5000 }
5001
5002 case ISD::BITCAST: {
5003 if (VT.isScalableVector())
5004 break;
5005 SDValue N0 = Op.getOperand(0);
5006 EVT SrcVT = N0.getValueType();
5007 unsigned SrcBits = SrcVT.getScalarSizeInBits();
5008
5009 // Ignore bitcasts from unsupported types..
5010 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
5011 break;
5012
5013 // Fast handling of 'identity' bitcasts.
5014 if (VTBits == SrcBits)
5015 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
5016
5017 bool IsLE = getDataLayout().isLittleEndian();
5018
5019 // Bitcast 'large element' scalar/vector to 'small element' vector.
5020 if ((SrcBits % VTBits) == 0) {
5021 assert(VT.isVector() && "Expected bitcast to vector");
5022
5023 unsigned Scale = SrcBits / VTBits;
5024 APInt SrcDemandedElts =
5025 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
5026
5027 // Fast case - sign splat can be simply split across the small elements.
5028 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
5029 if (Tmp == SrcBits)
5030 return VTBits;
5031
5032 // Slow case - determine how far the sign extends into each sub-element.
5033 Tmp2 = VTBits;
5034 for (unsigned i = 0; i != NumElts; ++i)
5035 if (DemandedElts[i]) {
5036 unsigned SubOffset = i % Scale;
5037 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
5038 SubOffset = SubOffset * VTBits;
5039 if (Tmp <= SubOffset)
5040 return 1;
5041 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
5042 }
5043 return Tmp2;
5044 }
5045 break;
5046 }
5047
5049 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
5050 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
5051 return VTBits - Tmp + 1;
5052 case ISD::SIGN_EXTEND:
5053 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
5054 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
5056 // Max of the input and what this extends.
5057 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
5058 Tmp = VTBits-Tmp+1;
5059 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
5060 return std::max(Tmp, Tmp2);
5062 if (VT.isScalableVector())
5063 break;
5064 SDValue Src = Op.getOperand(0);
5065 EVT SrcVT = Src.getValueType();
5066 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
5067 Tmp = VTBits - SrcVT.getScalarSizeInBits();
5068 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
5069 }
5070 case ISD::SRA:
5071 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5072 // SRA X, C -> adds C sign bits.
5073 if (std::optional<unsigned> ShAmt =
5074 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
5075 Tmp = std::min(Tmp + *ShAmt, VTBits);
5076 return Tmp;
5077 case ISD::SHL:
5078 if (std::optional<ConstantRange> ShAmtRange =
5079 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
5080 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
5081 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
5082 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
5083 // shifted out, then we can compute the number of sign bits for the
5084 // operand being extended. A future improvement could be to pass along the
5085 // "shifted left by" information in the recursive calls to
5086 // ComputeKnownSignBits. Allowing us to handle this more generically.
5087 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
5088 SDValue Ext = Op.getOperand(0);
5089 EVT ExtVT = Ext.getValueType();
5090 SDValue Extendee = Ext.getOperand(0);
5091 EVT ExtendeeVT = Extendee.getValueType();
5092 unsigned SizeDifference =
5093 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
5094 if (SizeDifference <= MinShAmt) {
5095 Tmp = SizeDifference +
5096 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
5097 if (MaxShAmt < Tmp)
5098 return Tmp - MaxShAmt;
5099 }
5100 }
5101 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
5102 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5103 if (MaxShAmt < Tmp)
5104 return Tmp - MaxShAmt;
5105 }
5106 break;
5107 case ISD::AND:
5108 case ISD::OR:
5109 case ISD::XOR: // NOT is handled here.
5110 // Logical binary ops preserve the number of sign bits at the worst.
5111 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
5112 if (Tmp != 1) {
5113 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
5114 FirstAnswer = std::min(Tmp, Tmp2);
5115 // We computed what we know about the sign bits as our first
5116 // answer. Now proceed to the generic code that uses
5117 // computeKnownBits, and pick whichever answer is better.
5118 }
5119 break;
5120
5121 case ISD::SELECT:
5122 case ISD::VSELECT:
5123 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
5124 if (Tmp == 1) return 1; // Early out.
5125 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
5126 return std::min(Tmp, Tmp2);
5127 case ISD::SELECT_CC:
5128 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
5129 if (Tmp == 1) return 1; // Early out.
5130 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
5131 return std::min(Tmp, Tmp2);
5132
5133 case ISD::SMIN:
5134 case ISD::SMAX: {
5135 // If we have a clamp pattern, we know that the number of sign bits will be
5136 // the minimum of the clamp min/max range.
5137 bool IsMax = (Opcode == ISD::SMAX);
5138 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
5139 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
5140 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
5141 CstHigh =
5142 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
5143 if (CstLow && CstHigh) {
5144 if (!IsMax)
5145 std::swap(CstLow, CstHigh);
5146 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
5147 Tmp = CstLow->getAPIntValue().getNumSignBits();
5148 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
5149 return std::min(Tmp, Tmp2);
5150 }
5151 }
5152
5153 // Fallback - just get the minimum number of sign bits of the operands.
5154 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5155 if (Tmp == 1)
5156 return 1; // Early out.
5157 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5158 return std::min(Tmp, Tmp2);
5159 }
5160 case ISD::UMIN:
5161 case ISD::UMAX:
5162 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5163 if (Tmp == 1)
5164 return 1; // Early out.
5165 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5166 return std::min(Tmp, Tmp2);
5167 case ISD::SSUBO_CARRY:
5168 case ISD::USUBO_CARRY:
5169 // sub_carry(x,x,c) -> 0/-1 (sext carry)
5170 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
5171 return VTBits;
5172 [[fallthrough]];
5173 case ISD::SADDO:
5174 case ISD::UADDO:
5175 case ISD::SADDO_CARRY:
5176 case ISD::UADDO_CARRY:
5177 case ISD::SSUBO:
5178 case ISD::USUBO:
5179 case ISD::SMULO:
5180 case ISD::UMULO:
5181 if (Op.getResNo() != 1)
5182 break;
5183 // The boolean result conforms to getBooleanContents. Fall through.
5184 // If setcc returns 0/-1, all bits are sign bits.
5185 // We know that we have an integer-based boolean since these operations
5186 // are only available for integer.
5187 if (TLI->getBooleanContents(VT.isVector(), false) ==
5189 return VTBits;
5190 break;
5191 case ISD::SETCC:
5192 case ISD::SETCCCARRY:
5193 case ISD::STRICT_FSETCC:
5194 case ISD::STRICT_FSETCCS: {
5195 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5196 // If setcc returns 0/-1, all bits are sign bits.
5197 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5199 return VTBits;
5200 break;
5201 }
5202 case ISD::ROTL:
5203 case ISD::ROTR:
5204 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5205
5206 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5207 if (Tmp == VTBits)
5208 return VTBits;
5209
5210 if (ConstantSDNode *C =
5211 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5212 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5213
5214 // Handle rotate right by N like a rotate left by 32-N.
5215 if (Opcode == ISD::ROTR)
5216 RotAmt = (VTBits - RotAmt) % VTBits;
5217
5218 // If we aren't rotating out all of the known-in sign bits, return the
5219 // number that are left. This handles rotl(sext(x), 1) for example.
5220 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5221 }
5222 break;
5223 case ISD::ADD:
5224 case ISD::ADDC:
5225 // TODO: Move Operand 1 check before Operand 0 check
5226 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5227 if (Tmp == 1) return 1; // Early out.
5228
5229 // Special case decrementing a value (ADD X, -1):
5230 if (ConstantSDNode *CRHS =
5231 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5232 if (CRHS->isAllOnes()) {
5233 KnownBits Known =
5234 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5235
5236 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5237 // sign bits set.
5238 if ((Known.Zero | 1).isAllOnes())
5239 return VTBits;
5240
5241 // If we are subtracting one from a positive number, there is no carry
5242 // out of the result.
5243 if (Known.isNonNegative())
5244 return Tmp;
5245 }
5246
5247 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5248 if (Tmp2 == 1) return 1; // Early out.
5249
5250 // Add can have at most one carry bit. Thus we know that the output
5251 // is, at worst, one more bit than the inputs.
5252 return std::min(Tmp, Tmp2) - 1;
5253 case ISD::SUB:
5254 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5255 if (Tmp2 == 1) return 1; // Early out.
5256
5257 // Handle NEG.
5258 if (ConstantSDNode *CLHS =
5259 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5260 if (CLHS->isZero()) {
5261 KnownBits Known =
5262 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5263 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5264 // sign bits set.
5265 if ((Known.Zero | 1).isAllOnes())
5266 return VTBits;
5267
5268 // If the input is known to be positive (the sign bit is known clear),
5269 // the output of the NEG has the same number of sign bits as the input.
5270 if (Known.isNonNegative())
5271 return Tmp2;
5272
5273 // Otherwise, we treat this like a SUB.
5274 }
5275
5276 // Sub can have at most one carry bit. Thus we know that the output
5277 // is, at worst, one more bit than the inputs.
5278 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5279 if (Tmp == 1) return 1; // Early out.
5280 return std::min(Tmp, Tmp2) - 1;
5281 case ISD::MUL: {
5282 // The output of the Mul can be at most twice the valid bits in the inputs.
5283 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5284 if (SignBitsOp0 == 1)
5285 break;
5286 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5287 if (SignBitsOp1 == 1)
5288 break;
5289 unsigned OutValidBits =
5290 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5291 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5292 }
5293 case ISD::AVGCEILS:
5294 case ISD::AVGFLOORS:
5295 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5296 if (Tmp == 1)
5297 return 1; // Early out.
5298 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5299 return std::min(Tmp, Tmp2);
5300 case ISD::SREM:
5301 // The sign bit is the LHS's sign bit, except when the result of the
5302 // remainder is zero. The magnitude of the result should be less than or
5303 // equal to the magnitude of the LHS. Therefore, the result should have
5304 // at least as many sign bits as the left hand side.
5305 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5306 case ISD::TRUNCATE: {
5307 // Check if the sign bits of source go down as far as the truncated value.
5308 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5309 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5310 if (NumSrcSignBits > (NumSrcBits - VTBits))
5311 return NumSrcSignBits - (NumSrcBits - VTBits);
5312 break;
5313 }
5314 case ISD::EXTRACT_ELEMENT: {
5315 if (VT.isScalableVector())
5316 break;
5317 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5318 const int BitWidth = Op.getValueSizeInBits();
5319 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5320
5321 // Get reverse index (starting from 1), Op1 value indexes elements from
5322 // little end. Sign starts at big end.
5323 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5324
5325 // If the sign portion ends in our element the subtraction gives correct
5326 // result. Otherwise it gives either negative or > bitwidth result
5327 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5328 }
5330 if (VT.isScalableVector())
5331 break;
5332 // If we know the element index, split the demand between the
5333 // source vector and the inserted element, otherwise assume we need
5334 // the original demanded vector elements and the value.
5335 SDValue InVec = Op.getOperand(0);
5336 SDValue InVal = Op.getOperand(1);
5337 SDValue EltNo = Op.getOperand(2);
5338 bool DemandedVal = true;
5339 APInt DemandedVecElts = DemandedElts;
5340 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5341 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5342 unsigned EltIdx = CEltNo->getZExtValue();
5343 DemandedVal = !!DemandedElts[EltIdx];
5344 DemandedVecElts.clearBit(EltIdx);
5345 }
5346 Tmp = std::numeric_limits<unsigned>::max();
5347 if (DemandedVal) {
5348 // TODO - handle implicit truncation of inserted elements.
5349 if (InVal.getScalarValueSizeInBits() != VTBits)
5350 break;
5351 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5352 Tmp = std::min(Tmp, Tmp2);
5353 }
5354 if (!!DemandedVecElts) {
5355 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5356 Tmp = std::min(Tmp, Tmp2);
5357 }
5358 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5359 return Tmp;
5360 }
5362 SDValue InVec = Op.getOperand(0);
5363 SDValue EltNo = Op.getOperand(1);
5364 EVT VecVT = InVec.getValueType();
5365 // ComputeNumSignBits not yet implemented for scalable vectors.
5366 if (VecVT.isScalableVector())
5367 break;
5368 const unsigned BitWidth = Op.getValueSizeInBits();
5369 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5370 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5371
5372 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5373 // anything about sign bits. But if the sizes match we can derive knowledge
5374 // about sign bits from the vector operand.
5375 if (BitWidth != EltBitWidth)
5376 break;
5377
5378 // If we know the element index, just demand that vector element, else for
5379 // an unknown element index, ignore DemandedElts and demand them all.
5380 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5381 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5382 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5383 DemandedSrcElts =
5384 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5385
5386 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5387 }
5389 // Offset the demanded elts by the subvector index.
5390 SDValue Src = Op.getOperand(0);
5391
5392 APInt DemandedSrcElts;
5393 if (Src.getValueType().isScalableVector())
5394 DemandedSrcElts = APInt(1, 1);
5395 else {
5396 uint64_t Idx = Op.getConstantOperandVal(1);
5397 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5398 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5399 }
5400 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5401 }
5402 case ISD::CONCAT_VECTORS: {
5403 if (VT.isScalableVector())
5404 break;
5405 // Determine the minimum number of sign bits across all demanded
5406 // elts of the input vectors. Early out if the result is already 1.
5407 Tmp = std::numeric_limits<unsigned>::max();
5408 EVT SubVectorVT = Op.getOperand(0).getValueType();
5409 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5410 unsigned NumSubVectors = Op.getNumOperands();
5411 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5412 APInt DemandedSub =
5413 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5414 if (!DemandedSub)
5415 continue;
5416 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5417 Tmp = std::min(Tmp, Tmp2);
5418 }
5419 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5420 return Tmp;
5421 }
5422 case ISD::INSERT_SUBVECTOR: {
5423 if (VT.isScalableVector())
5424 break;
5425 // Demand any elements from the subvector and the remainder from the src its
5426 // inserted into.
5427 SDValue Src = Op.getOperand(0);
5428 SDValue Sub = Op.getOperand(1);
5429 uint64_t Idx = Op.getConstantOperandVal(2);
5430 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5431 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5432 APInt DemandedSrcElts = DemandedElts;
5433 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5434
5435 Tmp = std::numeric_limits<unsigned>::max();
5436 if (!!DemandedSubElts) {
5437 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5438 if (Tmp == 1)
5439 return 1; // early-out
5440 }
5441 if (!!DemandedSrcElts) {
5442 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5443 Tmp = std::min(Tmp, Tmp2);
5444 }
5445 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5446 return Tmp;
5447 }
5448 case ISD::LOAD: {
5449 // If we are looking at the loaded value of the SDNode.
5450 if (Op.getResNo() != 0)
5451 break;
5452
5454 if (const MDNode *Ranges = LD->getRanges()) {
5455 if (DemandedElts != 1)
5456 break;
5457
5459 if (VTBits > CR.getBitWidth()) {
5460 switch (LD->getExtensionType()) {
5461 case ISD::SEXTLOAD:
5462 CR = CR.signExtend(VTBits);
5463 break;
5464 case ISD::ZEXTLOAD:
5465 CR = CR.zeroExtend(VTBits);
5466 break;
5467 default:
5468 break;
5469 }
5470 }
5471
5472 if (VTBits != CR.getBitWidth())
5473 break;
5474 return std::min(CR.getSignedMin().getNumSignBits(),
5476 }
5477
5478 unsigned ExtType = LD->getExtensionType();
5479 switch (ExtType) {
5480 default:
5481 break;
5482 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5483 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5484 return VTBits - Tmp + 1;
5485 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5486 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5487 return VTBits - Tmp;
5488 case ISD::NON_EXTLOAD:
5489 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5490 // We only need to handle vectors - computeKnownBits should handle
5491 // scalar cases.
5492 Type *CstTy = Cst->getType();
5493 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5494 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5495 VTBits == CstTy->getScalarSizeInBits()) {
5496 Tmp = VTBits;
5497 for (unsigned i = 0; i != NumElts; ++i) {
5498 if (!DemandedElts[i])
5499 continue;
5500 if (Constant *Elt = Cst->getAggregateElement(i)) {
5501 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5502 const APInt &Value = CInt->getValue();
5503 Tmp = std::min(Tmp, Value.getNumSignBits());
5504 continue;
5505 }
5506 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5507 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5508 Tmp = std::min(Tmp, Value.getNumSignBits());
5509 continue;
5510 }
5511 }
5512 // Unknown type. Conservatively assume no bits match sign bit.
5513 return 1;
5514 }
5515 return Tmp;
5516 }
5517 }
5518 break;
5519 }
5520
5521 break;
5522 }
5525 case ISD::ATOMIC_SWAP:
5537 case ISD::ATOMIC_LOAD: {
5538 auto *AT = cast<AtomicSDNode>(Op);
5539 // If we are looking at the loaded value.
5540 if (Op.getResNo() == 0) {
5541 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5542 if (Tmp == VTBits)
5543 return 1; // early-out
5544
5545 // For atomic_load, prefer to use the extension type.
5546 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5547 switch (AT->getExtensionType()) {
5548 default:
5549 break;
5550 case ISD::SEXTLOAD:
5551 return VTBits - Tmp + 1;
5552 case ISD::ZEXTLOAD:
5553 return VTBits - Tmp;
5554 }
5555 }
5556
5557 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5558 return VTBits - Tmp + 1;
5559 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5560 return VTBits - Tmp;
5561 }
5562 break;
5563 }
5564 }
5565
5566 // Allow the target to implement this method for its nodes.
5567 if (Opcode >= ISD::BUILTIN_OP_END ||
5568 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5569 Opcode == ISD::INTRINSIC_W_CHAIN ||
5570 Opcode == ISD::INTRINSIC_VOID) {
5571 // TODO: This can probably be removed once target code is audited. This
5572 // is here purely to reduce patch size and review complexity.
5573 if (!VT.isScalableVector()) {
5574 unsigned NumBits =
5575 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5576 if (NumBits > 1)
5577 FirstAnswer = std::max(FirstAnswer, NumBits);
5578 }
5579 }
5580
5581 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5582 // use this information.
5583 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5584 return std::max(FirstAnswer, Known.countMinSignBits());
5585}
5586
5588 unsigned Depth) const {
5589 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5590 return Op.getScalarValueSizeInBits() - SignBits + 1;
5591}
5592
5594 const APInt &DemandedElts,
5595 unsigned Depth) const {
5596 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5597 return Op.getScalarValueSizeInBits() - SignBits + 1;
5598}
5599
5601 unsigned Depth) const {
5602 // Early out for FREEZE.
5603 if (Op.getOpcode() == ISD::FREEZE)
5604 return true;
5605
5606 APInt DemandedElts = getDemandAllEltsMask(Op);
5607 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5608}
5609
5611 const APInt &DemandedElts,
5612 bool PoisonOnly,
5613 unsigned Depth) const {
5614 unsigned Opcode = Op.getOpcode();
5615
5616 // Early out for FREEZE.
5617 if (Opcode == ISD::FREEZE)
5618 return true;
5619
5620 if (Depth >= MaxRecursionDepth)
5621 return false; // Limit search depth.
5622
5623 if (isIntOrFPConstant(Op))
5624 return true;
5625
5626 switch (Opcode) {
5627 case ISD::CONDCODE:
5628 case ISD::VALUETYPE:
5629 case ISD::FrameIndex:
5631 case ISD::CopyFromReg:
5632 return true;
5633
5634 case ISD::POISON:
5635 return false;
5636
5637 case ISD::UNDEF:
5638 return PoisonOnly;
5639
5640 case ISD::BUILD_VECTOR:
5641 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5642 // this shouldn't affect the result.
5643 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5644 if (!DemandedElts[i])
5645 continue;
5647 Depth + 1))
5648 return false;
5649 }
5650 return true;
5651
5653 SDValue Src = Op.getOperand(0);
5654 if (Src.getValueType().isScalableVector())
5655 break;
5656 uint64_t Idx = Op.getConstantOperandVal(1);
5657 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5658 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5659 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5660 Depth + 1);
5661 }
5662
5663 case ISD::INSERT_SUBVECTOR: {
5664 if (Op.getValueType().isScalableVector())
5665 break;
5666 SDValue Src = Op.getOperand(0);
5667 SDValue Sub = Op.getOperand(1);
5668 uint64_t Idx = Op.getConstantOperandVal(2);
5669 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5670 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5671 APInt DemandedSrcElts = DemandedElts;
5672 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5673
5674 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5675 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5676 return false;
5677 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5678 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5679 return false;
5680 return true;
5681 }
5682
5684 SDValue Src = Op.getOperand(0);
5685 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5686 EVT SrcVT = Src.getValueType();
5687 if (SrcVT.isFixedLengthVector() && IndexC &&
5688 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5689 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5690 IndexC->getZExtValue());
5691 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5692 Depth + 1);
5693 }
5694 break;
5695 }
5696
5698 SDValue InVec = Op.getOperand(0);
5699 SDValue InVal = Op.getOperand(1);
5700 SDValue EltNo = Op.getOperand(2);
5701 EVT VT = InVec.getValueType();
5702 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5703 if (IndexC && VT.isFixedLengthVector() &&
5704 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5705 if (DemandedElts[IndexC->getZExtValue()] &&
5707 return false;
5708 APInt InVecDemandedElts = DemandedElts;
5709 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5710 if (!!InVecDemandedElts &&
5712 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5713 InVecDemandedElts, PoisonOnly, Depth + 1))
5714 return false;
5715 return true;
5716 }
5717 break;
5718 }
5719
5721 // Check upper (known undef) elements.
5722 if (DemandedElts.ugt(1) && !PoisonOnly)
5723 return false;
5724 // Check element zero.
5725 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5726 Op.getOperand(0), PoisonOnly, Depth + 1))
5727 return false;
5728 return true;
5729
5730 case ISD::SPLAT_VECTOR:
5731 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5732 Depth + 1);
5733
5734 case ISD::VECTOR_SHUFFLE: {
5735 APInt DemandedLHS, DemandedRHS;
5736 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5737 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5738 DemandedElts, DemandedLHS, DemandedRHS,
5739 /*AllowUndefElts=*/false))
5740 return false;
5741 if (!DemandedLHS.isZero() &&
5742 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5743 PoisonOnly, Depth + 1))
5744 return false;
5745 if (!DemandedRHS.isZero() &&
5746 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5747 PoisonOnly, Depth + 1))
5748 return false;
5749 return true;
5750 }
5751
5752 case ISD::SHL:
5753 case ISD::SRL:
5754 case ISD::SRA:
5755 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5756 // enough to check operand 0 if Op can't create undef/poison.
5757 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5758 /*ConsiderFlags*/ true, Depth) &&
5759 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5760 PoisonOnly, Depth + 1);
5761
5762 case ISD::BSWAP:
5763 case ISD::CTPOP:
5764 case ISD::BITREVERSE:
5765 case ISD::AND:
5766 case ISD::OR:
5767 case ISD::XOR:
5768 case ISD::ADD:
5769 case ISD::SUB:
5770 case ISD::MUL:
5771 case ISD::SADDSAT:
5772 case ISD::UADDSAT:
5773 case ISD::SSUBSAT:
5774 case ISD::USUBSAT:
5775 case ISD::SSHLSAT:
5776 case ISD::USHLSAT:
5777 case ISD::SMIN:
5778 case ISD::SMAX:
5779 case ISD::UMIN:
5780 case ISD::UMAX:
5781 case ISD::ZERO_EXTEND:
5782 case ISD::SIGN_EXTEND:
5783 case ISD::ANY_EXTEND:
5784 case ISD::TRUNCATE:
5785 case ISD::VSELECT: {
5786 // If Op can't create undef/poison and none of its operands are undef/poison
5787 // then Op is never undef/poison. A difference from the more common check
5788 // below, outside the switch, is that we handle elementwise operations for
5789 // which the DemandedElts mask is valid for all operands here.
5790 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5791 /*ConsiderFlags*/ true, Depth) &&
5792 all_of(Op->ops(), [&](SDValue V) {
5793 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5794 PoisonOnly, Depth + 1);
5795 });
5796 }
5797
5798 // TODO: Search for noundef attributes from library functions.
5799
5800 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5801
5802 default:
5803 // Allow the target to implement this method for its nodes.
5804 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5805 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5806 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5807 Op, DemandedElts, *this, PoisonOnly, Depth);
5808 break;
5809 }
5810
5811 // If Op can't create undef/poison and none of its operands are undef/poison
5812 // then Op is never undef/poison.
5813 // NOTE: TargetNodes can handle this in themselves in
5814 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5815 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5816 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5817 Depth) &&
5818 all_of(Op->ops(), [&](SDValue V) {
5819 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5820 });
5821}
5822
5824 bool ConsiderFlags,
5825 unsigned Depth) const {
5826 APInt DemandedElts = getDemandAllEltsMask(Op);
5827 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5828 Depth);
5829}
5830
5832 bool PoisonOnly, bool ConsiderFlags,
5833 unsigned Depth) const {
5834 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5835 return true;
5836
5837 unsigned Opcode = Op.getOpcode();
5838 switch (Opcode) {
5839 case ISD::AssertSext:
5840 case ISD::AssertZext:
5841 case ISD::AssertAlign:
5843 // Assertion nodes can create poison if the assertion fails.
5844 return true;
5845
5846 case ISD::FREEZE:
5850 case ISD::SADDSAT:
5851 case ISD::UADDSAT:
5852 case ISD::SSUBSAT:
5853 case ISD::USUBSAT:
5854 case ISD::MULHU:
5855 case ISD::MULHS:
5856 case ISD::AVGFLOORS:
5857 case ISD::AVGFLOORU:
5858 case ISD::AVGCEILS:
5859 case ISD::AVGCEILU:
5860 case ISD::ABDU:
5861 case ISD::ABDS:
5862 case ISD::SMIN:
5863 case ISD::SMAX:
5864 case ISD::SCMP:
5865 case ISD::UMIN:
5866 case ISD::UMAX:
5867 case ISD::UCMP:
5868 case ISD::AND:
5869 case ISD::XOR:
5870 case ISD::ROTL:
5871 case ISD::ROTR:
5872 case ISD::FSHL:
5873 case ISD::FSHR:
5874 case ISD::BSWAP:
5875 case ISD::CTTZ:
5876 case ISD::CTLZ:
5877 case ISD::CTLS:
5878 case ISD::CTPOP:
5879 case ISD::BITREVERSE:
5880 case ISD::PARITY:
5881 case ISD::SIGN_EXTEND:
5882 case ISD::TRUNCATE:
5886 case ISD::BITCAST:
5887 case ISD::BUILD_VECTOR:
5888 case ISD::BUILD_PAIR:
5889 case ISD::SPLAT_VECTOR:
5890 case ISD::FABS:
5891 return false;
5892
5893 case ISD::ABS:
5894 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5895 // Different to Intrinsic::abs.
5896 return false;
5897
5898 case ISD::ADDC:
5899 case ISD::SUBC:
5900 case ISD::ADDE:
5901 case ISD::SUBE:
5902 case ISD::SADDO:
5903 case ISD::SSUBO:
5904 case ISD::SMULO:
5905 case ISD::SADDO_CARRY:
5906 case ISD::SSUBO_CARRY:
5907 case ISD::UADDO:
5908 case ISD::USUBO:
5909 case ISD::UMULO:
5910 case ISD::UADDO_CARRY:
5911 case ISD::USUBO_CARRY:
5912 // No poison on result or overflow flags.
5913 return false;
5914
5915 case ISD::SELECT_CC:
5916 case ISD::SETCC: {
5917 // Integer setcc cannot create undef or poison.
5918 if (Op.getOperand(0).getValueType().isInteger())
5919 return false;
5920
5921 // FP compares are more complicated. They can create poison for nan/infinity
5922 // based on options and flags. The options and flags also cause special
5923 // nonan condition codes to be used. Those condition codes may be preserved
5924 // even if the nonan flag is dropped somewhere.
5925 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5926 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5927 return (unsigned)CCCode & 0x10U;
5928 }
5929
5930 case ISD::OR:
5931 case ISD::ZERO_EXTEND:
5932 case ISD::SELECT:
5933 case ISD::VSELECT:
5934 case ISD::ADD:
5935 case ISD::SUB:
5936 case ISD::MUL:
5937 case ISD::FNEG:
5938 case ISD::FADD:
5939 case ISD::FSUB:
5940 case ISD::FMUL:
5941 case ISD::FDIV:
5942 case ISD::FREM:
5943 case ISD::FCOPYSIGN:
5944 case ISD::FMA:
5945 case ISD::FMAD:
5946 case ISD::FMULADD:
5947 case ISD::FP_EXTEND:
5953 // No poison except from flags (which is handled above)
5954 return false;
5955
5956 case ISD::SHL:
5957 case ISD::SRL:
5958 case ISD::SRA:
5959 // If the max shift amount isn't in range, then the shift can
5960 // create poison.
5961 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5962
5965 // If the amount is zero then the result will be poison.
5966 // TODO: Add isKnownNeverZero DemandedElts handling.
5967 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5968
5970 // Check if we demand any upper (undef) elements.
5971 return !PoisonOnly && DemandedElts.ugt(1);
5972
5975 // Ensure that the element index is in bounds.
5976 EVT VecVT = Op.getOperand(0).getValueType();
5977 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5978 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5979 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5980 }
5981
5982 case ISD::VECTOR_SHUFFLE: {
5983 // Check for any demanded shuffle element that is undef.
5984 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5985 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5986 if (Elt < 0 && DemandedElts[Idx])
5987 return true;
5988 return false;
5989 }
5990
5992 return false;
5993
5994 default:
5995 // Allow the target to implement this method for its nodes.
5996 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5997 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5998 return TLI->canCreateUndefOrPoisonForTargetNode(
5999 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
6000 break;
6001 }
6002
6003 // Be conservative and return true.
6004 return true;
6005}
6006
6007bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
6008 unsigned Opcode = Op.getOpcode();
6009 if (Opcode == ISD::OR)
6010 return Op->getFlags().hasDisjoint() ||
6011 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
6012 if (Opcode == ISD::XOR)
6013 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
6014 return false;
6015}
6016
6018 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
6019 (Op.isAnyAdd() || isADDLike(Op));
6020}
6021
6023 FPClassTest InterestedClasses,
6024 unsigned Depth) const {
6025 APInt DemandedElts = getDemandAllEltsMask(Op);
6026 return computeKnownFPClass(Op, DemandedElts, InterestedClasses, Depth);
6027}
6028
6030 const APInt &DemandedElts,
6031 FPClassTest InterestedClasses,
6032 unsigned Depth) const {
6033 KnownFPClass Known;
6034
6035 if (const auto *CFP = dyn_cast<ConstantFPSDNode>(Op))
6036 return KnownFPClass(CFP->getValueAPF());
6037
6038 if (Depth >= MaxRecursionDepth)
6039 return Known;
6040
6041 if (Op.getOpcode() == ISD::UNDEF)
6042 return Known;
6043
6044 EVT VT = Op.getValueType();
6045 assert(VT.isFloatingPoint() && "Computing KnownFPClass on non-FP op!");
6046 assert((!VT.isFixedLengthVector() ||
6047 DemandedElts.getBitWidth() == VT.getVectorNumElements()) &&
6048 "Unexpected vector size");
6049
6050 if (!DemandedElts)
6051 return Known;
6052
6053 unsigned Opcode = Op.getOpcode();
6054 switch (Opcode) {
6055 case ISD::POISON: {
6056 Known.KnownFPClasses = fcNone;
6057 Known.SignBit = false;
6058 break;
6059 }
6060 case ISD::BUILD_VECTOR: {
6061 assert(!VT.isScalableVector());
6062 bool First = true;
6063 for (unsigned I = 0, E = Op.getNumOperands(); I != E; ++I) {
6064 if (!DemandedElts[I])
6065 continue;
6066
6067 if (First) {
6068 Known =
6069 computeKnownFPClass(Op.getOperand(I), InterestedClasses, Depth + 1);
6070 First = false;
6071 } else {
6072 Known |=
6073 computeKnownFPClass(Op.getOperand(I), InterestedClasses, Depth + 1);
6074 }
6075
6076 if (Known.isUnknown())
6077 break;
6078 }
6079 break;
6080 }
6081 case ISD::BITCAST: {
6082 // FIXME: It should not be necessary to check for an elementwise bitcast.
6083 // If a bitcast is not elementwise between vector / scalar types,
6084 // computeKnownBits already splices the known bits of the source elements
6085 // appropriately so as to line up with the bits of the result's demanded
6086 // elements.
6087 EVT SrcVT = Op.getOperand(0).getValueType();
6088 if (VT.isScalableVector() || SrcVT.isScalableVector())
6089 break;
6090 unsigned VTNumElts = VT.isVector() ? VT.getVectorNumElements() : 1;
6091 unsigned SrcVTNumElts = SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1;
6092 if (VTNumElts != SrcVTNumElts)
6093 break;
6094
6095 KnownBits Bits = computeKnownBits(Op, DemandedElts, Depth + 1);
6096 Known = KnownFPClass::bitcast(VT.getFltSemantics(), Bits);
6097 break;
6098 }
6099 case ISD::FABS: {
6100 Known = computeKnownFPClass(Op.getOperand(0), DemandedElts,
6101 InterestedClasses, Depth + 1);
6102 Known.fabs();
6103 break;
6104 }
6105 default:
6106 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6107 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6108 TLI->computeKnownFPClassForTargetNode(Op, Known, DemandedElts, *this,
6109 Depth);
6110 }
6111 break;
6112 }
6113
6114 return Known;
6115}
6116
6118 unsigned Depth) const {
6119 APInt DemandedElts = getDemandAllEltsMask(Op);
6120 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
6121}
6122
6124 bool SNaN, unsigned Depth) const {
6125 assert(!DemandedElts.isZero() && "No demanded elements");
6126
6127 // If we're told that NaNs won't happen, assume they won't.
6128 if (Op->getFlags().hasNoNaNs())
6129 return true;
6130
6131 if (Depth >= MaxRecursionDepth)
6132 return false; // Limit search depth.
6133
6134 unsigned Opcode = Op.getOpcode();
6135 switch (Opcode) {
6136 case ISD::FADD:
6137 case ISD::FSUB:
6138 case ISD::FMUL:
6139 case ISD::FDIV:
6140 case ISD::FREM:
6141 case ISD::FSIN:
6142 case ISD::FCOS:
6143 case ISD::FTAN:
6144 case ISD::FASIN:
6145 case ISD::FACOS:
6146 case ISD::FATAN:
6147 case ISD::FATAN2:
6148 case ISD::FSINH:
6149 case ISD::FCOSH:
6150 case ISD::FTANH:
6151 case ISD::FMA:
6152 case ISD::FMULADD:
6153 case ISD::FMAD: {
6154 if (SNaN)
6155 return true;
6156 // TODO: Need isKnownNeverInfinity
6157 return false;
6158 }
6159 case ISD::FCANONICALIZE:
6160 case ISD::FEXP:
6161 case ISD::FEXP2:
6162 case ISD::FEXP10:
6163 case ISD::FTRUNC:
6164 case ISD::FFLOOR:
6165 case ISD::FCEIL:
6166 case ISD::FROUND:
6167 case ISD::FROUNDEVEN:
6168 case ISD::LROUND:
6169 case ISD::LLROUND:
6170 case ISD::FRINT:
6171 case ISD::LRINT:
6172 case ISD::LLRINT:
6173 case ISD::FNEARBYINT:
6174 case ISD::FLDEXP: {
6175 if (SNaN)
6176 return true;
6177 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6178 }
6179 case ISD::FABS:
6180 case ISD::FNEG:
6181 case ISD::FCOPYSIGN: {
6182 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6183 }
6184 case ISD::SELECT:
6185 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
6186 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
6187 case ISD::FP_EXTEND:
6188 case ISD::FP_ROUND: {
6189 if (SNaN)
6190 return true;
6191 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6192 }
6193 case ISD::SINT_TO_FP:
6194 case ISD::UINT_TO_FP:
6195 return true;
6196 case ISD::FSQRT: // Need is known positive
6197 case ISD::FLOG:
6198 case ISD::FLOG2:
6199 case ISD::FLOG10:
6200 case ISD::FPOWI:
6201 case ISD::FPOW: {
6202 if (SNaN)
6203 return true;
6204 // TODO: Refine on operand
6205 return false;
6206 }
6207 case ISD::FMINNUM:
6208 case ISD::FMAXNUM:
6209 case ISD::FMINIMUMNUM:
6210 case ISD::FMAXIMUMNUM: {
6211 // Only one needs to be known not-nan, since it will be returned if the
6212 // other ends up being one.
6213 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
6214 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
6215 }
6216 case ISD::FMINNUM_IEEE:
6217 case ISD::FMAXNUM_IEEE: {
6218 if (SNaN)
6219 return true;
6220 // This can return a NaN if either operand is an sNaN, or if both operands
6221 // are NaN.
6222 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
6223 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
6224 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
6225 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
6226 }
6227 case ISD::FMINIMUM:
6228 case ISD::FMAXIMUM: {
6229 // TODO: Does this quiet or return the origina NaN as-is?
6230 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
6231 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
6232 }
6234 SDValue Src = Op.getOperand(0);
6235 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6236 EVT SrcVT = Src.getValueType();
6237 if (SrcVT.isFixedLengthVector() && Idx &&
6238 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
6239 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
6240 Idx->getZExtValue());
6241 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6242 }
6243 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6244 }
6246 SDValue Src = Op.getOperand(0);
6247 if (Src.getValueType().isFixedLengthVector()) {
6248 unsigned Idx = Op.getConstantOperandVal(1);
6249 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
6250 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
6251 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6252 }
6253 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6254 }
6255 case ISD::INSERT_SUBVECTOR: {
6256 SDValue BaseVector = Op.getOperand(0);
6257 SDValue SubVector = Op.getOperand(1);
6258 EVT BaseVectorVT = BaseVector.getValueType();
6259 if (BaseVectorVT.isFixedLengthVector()) {
6260 unsigned Idx = Op.getConstantOperandVal(2);
6261 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
6262 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
6263
6264 // Clear/Extract the bits at the position where the subvector will be
6265 // inserted.
6266 APInt DemandedMask =
6267 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
6268 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
6269 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
6270
6271 bool NeverNaN = true;
6272 if (!DemandedSrcElts.isZero())
6273 NeverNaN &=
6274 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
6275 if (NeverNaN && !DemandedSubElts.isZero())
6276 NeverNaN &=
6277 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
6278 return NeverNaN;
6279 }
6280 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
6281 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
6282 }
6283 case ISD::BUILD_VECTOR: {
6284 unsigned NumElts = Op.getNumOperands();
6285 for (unsigned I = 0; I != NumElts; ++I)
6286 if (DemandedElts[I] &&
6287 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6288 return false;
6289 return true;
6290 }
6291 case ISD::SPLAT_VECTOR:
6292 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
6293 case ISD::AssertNoFPClass: {
6294 FPClassTest NoFPClass =
6295 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6296 if ((NoFPClass & fcNan) == fcNan)
6297 return true;
6298 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6299 return true;
6300 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6301 }
6302 default:
6303 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6304 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6305 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6306 Depth);
6307 }
6308 break;
6309 }
6310
6311 FPClassTest NanMask = SNaN ? fcSNan : fcNan;
6312 KnownFPClass Known = computeKnownFPClass(Op, DemandedElts, NanMask, Depth);
6313 return Known.isKnownNever(NanMask);
6314}
6315
6317 assert(Op.getValueType().isFloatingPoint() &&
6318 "Floating point type expected");
6319
6320 // If the value is a constant, we can obviously see if it is a zero or not.
6322 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
6323}
6324
6326 APInt DemandedElts = getDemandAllEltsMask(Op);
6327 return isKnownNeverZero(Op, DemandedElts, Depth);
6328}
6329
6331 unsigned Depth) const {
6332 if (Depth >= MaxRecursionDepth)
6333 return false; // Limit search depth.
6334
6335 EVT OpVT = Op.getValueType();
6336 unsigned BitWidth = OpVT.getScalarSizeInBits();
6337
6338 assert(!Op.getValueType().isFloatingPoint() &&
6339 "Floating point types unsupported - use isKnownNeverZeroFloat");
6340
6341 // If the value is a constant, we can obviously see if it is a zero or not.
6342 auto IsNeverZero = [BitWidth](const ConstantSDNode *C) {
6343 APInt V = C->getAPIntValue().zextOrTrunc(BitWidth);
6344 return !V.isZero();
6345 };
6346
6347 if (ISD::matchUnaryPredicate(Op, IsNeverZero))
6348 return true;
6349
6350 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6351 // some degree.
6352 switch (Op.getOpcode()) {
6353 default:
6354 break;
6355
6356 case ISD::BUILD_VECTOR:
6357 // Are all operands of a build vector constant non-zero?
6358 if (all_of(enumerate(Op->ops()), [&](auto P) {
6359 auto *C = dyn_cast<ConstantSDNode>(P.value());
6360 return !DemandedElts[P.index()] || (C && IsNeverZero(C));
6361 }))
6362 return true;
6363 break;
6364
6365 case ISD::SPLAT_VECTOR:
6366 // Is the operand of a splat vector a constant non-zero?
6367 if (auto *C = dyn_cast<ConstantSDNode>(Op->getOperand(0)))
6368 if (IsNeverZero(C))
6369 return true;
6370 break;
6371
6373 SDValue InVec = Op.getOperand(0);
6374 SDValue EltNo = Op.getOperand(1);
6375 EVT VecVT = InVec.getValueType();
6376
6377 // Skip scalable vectors or implicit extensions.
6378 if (VecVT.isScalableVector() ||
6379 OpVT.getScalarSizeInBits() != VecVT.getScalarSizeInBits())
6380 break;
6381
6382 // If we know the element index, just demand that vector element, else for
6383 // an unknown element index, ignore DemandedElts and demand them all.
6384 const unsigned NumSrcElts = VecVT.getVectorNumElements();
6385 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
6386 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
6387 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
6388 DemandedSrcElts =
6389 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
6390
6391 return isKnownNeverZero(InVec, DemandedSrcElts, Depth + 1);
6392 }
6393
6394 case ISD::OR:
6395 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6396 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6397
6398 case ISD::VSELECT:
6399 case ISD::SELECT:
6400 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6401 isKnownNeverZero(Op.getOperand(2), DemandedElts, Depth + 1);
6402
6403 case ISD::SHL: {
6404 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6405 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6406 KnownBits ValKnown =
6407 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6408 // 1 << X is never zero.
6409 if (ValKnown.One[0])
6410 return true;
6411 // If max shift cnt of known ones is non-zero, result is non-zero.
6412 APInt MaxCnt = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1)
6413 .getMaxValue();
6414 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6415 !ValKnown.One.shl(MaxCnt).isZero())
6416 return true;
6417 break;
6418 }
6419
6420 case ISD::VECTOR_SHUFFLE: {
6421 if (Op.getValueType().isScalableVector())
6422 return false;
6423
6424 unsigned NumElts = DemandedElts.getBitWidth();
6425
6426 // All demanded elements from LHS and RHS must be known non-zero.
6427 // Demanded elements with undef shuffle mask elements are unknown.
6428
6429 APInt DemandedLHS, DemandedRHS;
6430 auto *SVN = cast<ShuffleVectorSDNode>(Op);
6431 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
6432 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
6433 DemandedLHS, DemandedRHS))
6434 return false;
6435
6436 return (!DemandedLHS ||
6437 isKnownNeverZero(Op.getOperand(0), DemandedLHS, Depth + 1)) &&
6438 (!DemandedRHS ||
6439 isKnownNeverZero(Op.getOperand(1), DemandedRHS, Depth + 1));
6440 }
6441
6442 case ISD::UADDSAT:
6443 case ISD::UMAX:
6444 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6445 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6446
6447 case ISD::UMIN:
6448 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6449 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6450
6451 // For smin/smax: If either operand is known negative/positive
6452 // respectively we don't need the other to be known at all.
6453 case ISD::SMAX: {
6454 KnownBits Op1 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
6455 if (Op1.isStrictlyPositive())
6456 return true;
6457
6458 KnownBits Op0 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6459 if (Op0.isStrictlyPositive())
6460 return true;
6461
6462 if (Op1.isNonZero() && Op0.isNonZero())
6463 return true;
6464
6465 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6466 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6467 }
6468 case ISD::SMIN: {
6469 KnownBits Op1 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
6470 if (Op1.isNegative())
6471 return true;
6472
6473 KnownBits Op0 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6474 if (Op0.isNegative())
6475 return true;
6476
6477 if (Op1.isNonZero() && Op0.isNonZero())
6478 return true;
6479
6480 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6481 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6482 }
6483
6484 case ISD::ROTL:
6485 case ISD::ROTR:
6486 case ISD::BITREVERSE:
6487 case ISD::BSWAP:
6488 case ISD::CTPOP:
6489 case ISD::ABS:
6490 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6491
6492 case ISD::SRA:
6493 case ISD::SRL: {
6494 if (Op->getFlags().hasExact())
6495 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6496 KnownBits ValKnown =
6497 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6498 if (ValKnown.isNegative())
6499 return true;
6500 // If max shift cnt of known ones is non-zero, result is non-zero.
6501 APInt MaxCnt = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1)
6502 .getMaxValue();
6503 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6504 !ValKnown.One.lshr(MaxCnt).isZero())
6505 return true;
6506 break;
6507 }
6508 case ISD::UDIV:
6509 case ISD::SDIV:
6510 // div exact can only produce a zero if the dividend is zero.
6511 // TODO: For udiv this is also true if Op1 u<= Op0
6512 if (Op->getFlags().hasExact())
6513 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6514 break;
6515
6516 case ISD::ADD:
6517 if (Op->getFlags().hasNoUnsignedWrap())
6518 if (isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6519 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1))
6520 return true;
6521 // TODO: There are a lot more cases we can prove for add.
6522 break;
6523
6524 case ISD::SUB: {
6525 if (isNullConstant(Op.getOperand(0)))
6526 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1);
6527
6528 std::optional<bool> ne = KnownBits::ne(
6529 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1),
6530 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1));
6531 return ne && *ne;
6532 }
6533
6534 case ISD::MUL:
6535 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6536 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6537 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6538 return true;
6539 break;
6540
6541 case ISD::ZERO_EXTEND:
6542 case ISD::SIGN_EXTEND:
6543 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6544 case ISD::VSCALE: {
6546 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6547 ConstantRange CR =
6548 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6549 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6550 return true;
6551 break;
6552 }
6553 }
6554
6556}
6557
6559 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6560 return !C1->isNegative();
6561
6562 switch (Op.getOpcode()) {
6563 case ISD::FABS:
6564 case ISD::FEXP:
6565 case ISD::FEXP2:
6566 case ISD::FEXP10:
6567 return true;
6568 default:
6569 return false;
6570 }
6571
6572 llvm_unreachable("covered opcode switch");
6573}
6574
6576 assert(Use.getValueType().isFloatingPoint());
6577 const SDNode *User = Use.getUser();
6578 if (User->getFlags().hasNoSignedZeros())
6579 return true;
6580
6581 unsigned OperandNo = Use.getOperandNo();
6582 // Check if this use is insensitive to the sign of zero
6583 switch (User->getOpcode()) {
6584 case ISD::SETCC:
6585 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6586 case ISD::FABS:
6587 // fabs always produces +0.0.
6588 return true;
6589 case ISD::FCOPYSIGN:
6590 // copysign overwrites the sign bit of the first operand.
6591 return OperandNo == 0;
6592 case ISD::FADD:
6593 case ISD::FSUB: {
6594 // Arithmetic with non-zero constants fixes the uncertainty around the
6595 // sign bit.
6596 SDValue Other = User->getOperand(1 - OperandNo);
6598 }
6599 case ISD::FP_TO_SINT:
6600 case ISD::FP_TO_UINT:
6601 // fp-to-int conversions normalize signed zeros.
6602 return true;
6603 default:
6604 return false;
6605 }
6606}
6607
6609 if (Op->getFlags().hasNoSignedZeros())
6610 return true;
6611 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6612 // regression. Ideally, this should be implemented as a demanded-bits
6613 // optimization that stems from the users.
6614 if (Op->use_size() > 2)
6615 return false;
6616 return all_of(Op->uses(),
6617 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6618}
6619
6621 // Check the obvious case.
6622 if (A == B) return true;
6623
6624 // For negative and positive zero.
6627 if (CA->isZero() && CB->isZero()) return true;
6628
6629 // Otherwise they may not be equal.
6630 return false;
6631}
6632
6633// Only bits set in Mask must be negated, other bits may be arbitrary.
6635 if (isBitwiseNot(V, AllowUndefs))
6636 return V.getOperand(0);
6637
6638 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6639 // bits in the non-extended part.
6640 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6641 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6642 return SDValue();
6643 SDValue ExtArg = V.getOperand(0);
6644 if (ExtArg.getScalarValueSizeInBits() >=
6645 MaskC->getAPIntValue().getActiveBits() &&
6646 isBitwiseNot(ExtArg, AllowUndefs) &&
6647 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6648 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6649 return ExtArg.getOperand(0).getOperand(0);
6650 return SDValue();
6651}
6652
6654 // Match masked merge pattern (X & ~M) op (Y & M)
6655 // Including degenerate case (X & ~M) op M
6656 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6657 SDValue Other) {
6658 if (SDValue NotOperand =
6659 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6660 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6661 NotOperand->getOpcode() == ISD::TRUNCATE)
6662 NotOperand = NotOperand->getOperand(0);
6663
6664 if (Other == NotOperand)
6665 return true;
6666 if (Other->getOpcode() == ISD::AND)
6667 return NotOperand == Other->getOperand(0) ||
6668 NotOperand == Other->getOperand(1);
6669 }
6670 return false;
6671 };
6672
6673 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6674 A = A->getOperand(0);
6675
6676 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6677 B = B->getOperand(0);
6678
6679 if (A->getOpcode() == ISD::AND)
6680 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6681 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6682 return false;
6683}
6684
6685// FIXME: unify with llvm::haveNoCommonBitsSet.
6687 assert(A.getValueType() == B.getValueType() &&
6688 "Values must have the same type");
6691 return true;
6694}
6695
6696static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6697 SelectionDAG &DAG) {
6698 if (cast<ConstantSDNode>(Step)->isZero())
6699 return DAG.getConstant(0, DL, VT);
6700
6701 return SDValue();
6702}
6703
6706 SelectionDAG &DAG) {
6707 int NumOps = Ops.size();
6708 assert(NumOps != 0 && "Can't build an empty vector!");
6709 assert(!VT.isScalableVector() &&
6710 "BUILD_VECTOR cannot be used with scalable types");
6711 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6712 "Incorrect element count in BUILD_VECTOR!");
6713
6714 // BUILD_VECTOR of UNDEFs is UNDEF.
6715 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6716 return DAG.getUNDEF(VT);
6717
6718 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6719 SDValue IdentitySrc;
6720 bool IsIdentity = true;
6721 for (int i = 0; i != NumOps; ++i) {
6723 Ops[i].getOperand(0).getValueType() != VT ||
6724 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6725 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6726 Ops[i].getConstantOperandAPInt(1) != i) {
6727 IsIdentity = false;
6728 break;
6729 }
6730 IdentitySrc = Ops[i].getOperand(0);
6731 }
6732 if (IsIdentity)
6733 return IdentitySrc;
6734
6735 return SDValue();
6736}
6737
6738/// Try to simplify vector concatenation to an input value, undef, or build
6739/// vector.
6742 SelectionDAG &DAG) {
6743 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6745 [Ops](SDValue Op) {
6746 return Ops[0].getValueType() == Op.getValueType();
6747 }) &&
6748 "Concatenation of vectors with inconsistent value types!");
6749 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6750 VT.getVectorElementCount() &&
6751 "Incorrect element count in vector concatenation!");
6752
6753 if (Ops.size() == 1)
6754 return Ops[0];
6755
6756 // Concat of UNDEFs is UNDEF.
6757 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6758 return DAG.getUNDEF(VT);
6759
6760 // Scan the operands and look for extract operations from a single source
6761 // that correspond to insertion at the same location via this concatenation:
6762 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6763 SDValue IdentitySrc;
6764 bool IsIdentity = true;
6765 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6766 SDValue Op = Ops[i];
6767 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6768 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6769 Op.getOperand(0).getValueType() != VT ||
6770 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6771 Op.getConstantOperandVal(1) != IdentityIndex) {
6772 IsIdentity = false;
6773 break;
6774 }
6775 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6776 "Unexpected identity source vector for concat of extracts");
6777 IdentitySrc = Op.getOperand(0);
6778 }
6779 if (IsIdentity) {
6780 assert(IdentitySrc && "Failed to set source vector of extracts");
6781 return IdentitySrc;
6782 }
6783
6784 // The code below this point is only designed to work for fixed width
6785 // vectors, so we bail out for now.
6786 if (VT.isScalableVector())
6787 return SDValue();
6788
6789 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6790 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6791 // BUILD_VECTOR.
6792 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6793 EVT SVT = VT.getScalarType();
6795 for (SDValue Op : Ops) {
6796 EVT OpVT = Op.getValueType();
6797 if (Op.isUndef())
6798 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6799 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6800 Elts.append(Op->op_begin(), Op->op_end());
6801 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6802 OpVT.getVectorNumElements() == 1 &&
6803 isNullConstant(Op.getOperand(2)))
6804 Elts.push_back(Op.getOperand(1));
6805 else
6806 return SDValue();
6807 }
6808
6809 // BUILD_VECTOR requires all inputs to be of the same type, find the
6810 // maximum type and extend them all.
6811 for (SDValue Op : Elts)
6812 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6813
6814 if (SVT.bitsGT(VT.getScalarType())) {
6815 for (SDValue &Op : Elts) {
6816 if (Op.isUndef())
6817 Op = DAG.getUNDEF(SVT);
6818 else
6819 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6820 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6821 : DAG.getSExtOrTrunc(Op, DL, SVT);
6822 }
6823 }
6824
6825 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6826 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6827 return V;
6828}
6829
6830/// Gets or creates the specified node.
6831SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6832 SDVTList VTs = getVTList(VT);
6834 AddNodeIDNode(ID, Opcode, VTs, {});
6835 void *IP = nullptr;
6836 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6837 return SDValue(E, 0);
6838
6839 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6840 CSEMap.InsertNode(N, IP);
6841
6842 InsertNode(N);
6843 SDValue V = SDValue(N, 0);
6844 NewSDValueDbgMsg(V, "Creating new node: ", this);
6845 return V;
6846}
6847
6848SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6849 SDValue N1) {
6850 SDNodeFlags Flags;
6851 if (Inserter)
6852 Flags = Inserter->getFlags();
6853 return getNode(Opcode, DL, VT, N1, Flags);
6854}
6855
6856SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6857 SDValue N1, const SDNodeFlags Flags) {
6858 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6859
6860 // Constant fold unary operations with a vector integer or float operand.
6861 switch (Opcode) {
6862 default:
6863 // FIXME: Entirely reasonable to perform folding of other unary
6864 // operations here as the need arises.
6865 break;
6866 case ISD::FNEG:
6867 case ISD::FABS:
6868 case ISD::FCEIL:
6869 case ISD::FTRUNC:
6870 case ISD::FFLOOR:
6871 case ISD::FP_EXTEND:
6872 case ISD::FP_TO_SINT:
6873 case ISD::FP_TO_UINT:
6874 case ISD::FP_TO_FP16:
6875 case ISD::FP_TO_BF16:
6876 case ISD::TRUNCATE:
6877 case ISD::ANY_EXTEND:
6878 case ISD::ZERO_EXTEND:
6879 case ISD::SIGN_EXTEND:
6880 case ISD::UINT_TO_FP:
6881 case ISD::SINT_TO_FP:
6882 case ISD::FP16_TO_FP:
6883 case ISD::BF16_TO_FP:
6884 case ISD::BITCAST:
6885 case ISD::ABS:
6886 case ISD::BITREVERSE:
6887 case ISD::BSWAP:
6888 case ISD::CTLZ:
6890 case ISD::CTTZ:
6892 case ISD::CTPOP:
6893 case ISD::CTLS:
6894 case ISD::STEP_VECTOR: {
6895 SDValue Ops = {N1};
6896 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6897 return Fold;
6898 }
6899 }
6900
6901 unsigned OpOpcode = N1.getNode()->getOpcode();
6902 switch (Opcode) {
6903 case ISD::STEP_VECTOR:
6904 assert(VT.isScalableVector() &&
6905 "STEP_VECTOR can only be used with scalable types");
6906 assert(OpOpcode == ISD::TargetConstant &&
6907 VT.getVectorElementType() == N1.getValueType() &&
6908 "Unexpected step operand");
6909 break;
6910 case ISD::FREEZE:
6911 assert(VT == N1.getValueType() && "Unexpected VT!");
6912 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6913 return N1;
6914 break;
6915 case ISD::TokenFactor:
6916 case ISD::MERGE_VALUES:
6918 return N1; // Factor, merge or concat of one node? No need.
6919 case ISD::BUILD_VECTOR: {
6920 // Attempt to simplify BUILD_VECTOR.
6921 SDValue Ops[] = {N1};
6922 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6923 return V;
6924 break;
6925 }
6926 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6927 case ISD::FP_EXTEND:
6929 "Invalid FP cast!");
6930 if (N1.getValueType() == VT) return N1; // noop conversion.
6931 assert((!VT.isVector() || VT.getVectorElementCount() ==
6933 "Vector element count mismatch!");
6934 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6935 if (N1.isUndef())
6936 return getUNDEF(VT);
6937 break;
6938 case ISD::FP_TO_SINT:
6939 case ISD::FP_TO_UINT:
6940 if (N1.isUndef())
6941 return getUNDEF(VT);
6942 break;
6943 case ISD::SINT_TO_FP:
6944 case ISD::UINT_TO_FP:
6945 // [us]itofp(undef) = 0, because the result value is bounded.
6946 if (N1.isUndef())
6947 return getConstantFP(0.0, DL, VT);
6948 break;
6949 case ISD::SIGN_EXTEND:
6950 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6951 "Invalid SIGN_EXTEND!");
6952 assert(VT.isVector() == N1.getValueType().isVector() &&
6953 "SIGN_EXTEND result type type should be vector iff the operand "
6954 "type is vector!");
6955 if (N1.getValueType() == VT) return N1; // noop extension
6956 assert((!VT.isVector() || VT.getVectorElementCount() ==
6958 "Vector element count mismatch!");
6959 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6960 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6961 SDNodeFlags Flags;
6962 if (OpOpcode == ISD::ZERO_EXTEND)
6963 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6964 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6965 transferDbgValues(N1, NewVal);
6966 return NewVal;
6967 }
6968
6969 if (OpOpcode == ISD::POISON)
6970 return getPOISON(VT);
6971
6972 if (N1.isUndef())
6973 // sext(undef) = 0, because the top bits will all be the same.
6974 return getConstant(0, DL, VT);
6975
6976 // Skip unnecessary sext_inreg pattern:
6977 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6978 if (OpOpcode == ISD::TRUNCATE) {
6979 SDValue OpOp = N1.getOperand(0);
6980 if (OpOp.getValueType() == VT) {
6981 unsigned NumSignExtBits =
6983 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6984 transferDbgValues(N1, OpOp);
6985 return OpOp;
6986 }
6987 }
6988 }
6989 break;
6990 case ISD::ZERO_EXTEND:
6991 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6992 "Invalid ZERO_EXTEND!");
6993 assert(VT.isVector() == N1.getValueType().isVector() &&
6994 "ZERO_EXTEND result type type should be vector iff the operand "
6995 "type is vector!");
6996 if (N1.getValueType() == VT) return N1; // noop extension
6997 assert((!VT.isVector() || VT.getVectorElementCount() ==
6999 "Vector element count mismatch!");
7000 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
7001 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
7002 SDNodeFlags Flags;
7003 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7004 SDValue NewVal =
7005 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
7006 transferDbgValues(N1, NewVal);
7007 return NewVal;
7008 }
7009
7010 if (OpOpcode == ISD::POISON)
7011 return getPOISON(VT);
7012
7013 if (N1.isUndef())
7014 // zext(undef) = 0, because the top bits will be zero.
7015 return getConstant(0, DL, VT);
7016
7017 // Skip unnecessary zext_inreg pattern:
7018 // (zext (trunc x)) -> x iff the upper bits are known zero.
7019 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
7020 // use to recognise zext_inreg patterns.
7021 if (OpOpcode == ISD::TRUNCATE) {
7022 SDValue OpOp = N1.getOperand(0);
7023 if (OpOp.getValueType() == VT) {
7024 if (OpOp.getOpcode() != ISD::AND) {
7027 if (MaskedValueIsZero(OpOp, HiBits)) {
7028 transferDbgValues(N1, OpOp);
7029 return OpOp;
7030 }
7031 }
7032 }
7033 }
7034 break;
7035 case ISD::ANY_EXTEND:
7036 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7037 "Invalid ANY_EXTEND!");
7038 assert(VT.isVector() == N1.getValueType().isVector() &&
7039 "ANY_EXTEND result type type should be vector iff the operand "
7040 "type is vector!");
7041 if (N1.getValueType() == VT) return N1; // noop extension
7042 assert((!VT.isVector() || VT.getVectorElementCount() ==
7044 "Vector element count mismatch!");
7045 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
7046
7047 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
7048 OpOpcode == ISD::ANY_EXTEND) {
7049 SDNodeFlags Flags;
7050 if (OpOpcode == ISD::ZERO_EXTEND)
7051 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7052 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
7053 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
7054 }
7055 if (N1.isUndef())
7056 return getUNDEF(VT);
7057
7058 // (ext (trunc x)) -> x
7059 if (OpOpcode == ISD::TRUNCATE) {
7060 SDValue OpOp = N1.getOperand(0);
7061 if (OpOp.getValueType() == VT) {
7062 transferDbgValues(N1, OpOp);
7063 return OpOp;
7064 }
7065 }
7066 break;
7067 case ISD::TRUNCATE:
7068 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7069 "Invalid TRUNCATE!");
7070 assert(VT.isVector() == N1.getValueType().isVector() &&
7071 "TRUNCATE result type type should be vector iff the operand "
7072 "type is vector!");
7073 if (N1.getValueType() == VT) return N1; // noop truncate
7074 assert((!VT.isVector() || VT.getVectorElementCount() ==
7076 "Vector element count mismatch!");
7077 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
7078 if (OpOpcode == ISD::TRUNCATE)
7079 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
7080 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
7081 OpOpcode == ISD::ANY_EXTEND) {
7082 // If the source is smaller than the dest, we still need an extend.
7084 VT.getScalarType())) {
7085 SDNodeFlags Flags;
7086 if (OpOpcode == ISD::ZERO_EXTEND)
7087 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7088 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
7089 }
7090 if (N1.getOperand(0).getValueType().bitsGT(VT))
7091 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
7092 return N1.getOperand(0);
7093 }
7094 if (N1.isUndef())
7095 return getUNDEF(VT);
7096 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
7097 return getVScale(DL, VT,
7099 break;
7103 assert(VT.isVector() && "This DAG node is restricted to vector types.");
7104 assert(N1.getValueType().bitsLE(VT) &&
7105 "The input must be the same size or smaller than the result.");
7108 "The destination vector type must have fewer lanes than the input.");
7109 break;
7110 case ISD::ABS:
7111 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
7112 if (N1.isUndef())
7113 return getConstant(0, DL, VT);
7114 break;
7115 case ISD::BSWAP:
7116 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
7117 assert((VT.getScalarSizeInBits() % 16 == 0) &&
7118 "BSWAP types must be a multiple of 16 bits!");
7119 if (N1.isUndef())
7120 return getUNDEF(VT);
7121 // bswap(bswap(X)) -> X.
7122 if (OpOpcode == ISD::BSWAP)
7123 return N1.getOperand(0);
7124 break;
7125 case ISD::BITREVERSE:
7126 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
7127 if (N1.isUndef())
7128 return getUNDEF(VT);
7129 break;
7130 case ISD::BITCAST:
7132 "Cannot BITCAST between types of different sizes!");
7133 if (VT == N1.getValueType()) return N1; // noop conversion.
7134 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
7135 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
7136 if (N1.isUndef())
7137 return getUNDEF(VT);
7138 break;
7140 assert(VT.isVector() && !N1.getValueType().isVector() &&
7141 (VT.getVectorElementType() == N1.getValueType() ||
7143 N1.getValueType().isInteger() &&
7145 "Illegal SCALAR_TO_VECTOR node!");
7146 if (N1.isUndef())
7147 return getUNDEF(VT);
7148 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
7149 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
7151 N1.getConstantOperandVal(1) == 0 &&
7152 N1.getOperand(0).getValueType() == VT)
7153 return N1.getOperand(0);
7154 break;
7155 case ISD::FNEG:
7156 // Negation of an unknown bag of bits is still completely undefined.
7157 if (N1.isUndef())
7158 return getUNDEF(VT);
7159
7160 if (OpOpcode == ISD::FNEG) // --X -> X
7161 return N1.getOperand(0);
7162 break;
7163 case ISD::FABS:
7164 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
7165 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
7166 break;
7167 case ISD::VSCALE:
7168 assert(VT == N1.getValueType() && "Unexpected VT!");
7169 break;
7170 case ISD::CTPOP:
7171 if (N1.getValueType().getScalarType() == MVT::i1)
7172 return N1;
7173 break;
7174 case ISD::CTLZ:
7175 case ISD::CTTZ:
7176 if (N1.getValueType().getScalarType() == MVT::i1)
7177 return getNOT(DL, N1, N1.getValueType());
7178 break;
7179 case ISD::CTLS:
7180 if (N1.getValueType().getScalarType() == MVT::i1)
7181 return getConstant(0, DL, VT);
7182 break;
7183 case ISD::VECREDUCE_ADD:
7184 if (N1.getValueType().getScalarType() == MVT::i1)
7185 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
7186 break;
7189 if (N1.getValueType().getScalarType() == MVT::i1)
7190 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
7191 break;
7194 if (N1.getValueType().getScalarType() == MVT::i1)
7195 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
7196 break;
7197 case ISD::SPLAT_VECTOR:
7198 assert(VT.isVector() && "Wrong return type!");
7199 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
7200 // that for now.
7202 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
7204 N1.getValueType().isInteger() &&
7206 "Wrong operand type!");
7207 break;
7208 }
7209
7210 SDNode *N;
7211 SDVTList VTs = getVTList(VT);
7212 SDValue Ops[] = {N1};
7213 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
7215 AddNodeIDNode(ID, Opcode, VTs, Ops);
7216 void *IP = nullptr;
7217 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7218 E->intersectFlagsWith(Flags);
7219 return SDValue(E, 0);
7220 }
7221
7222 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7223 N->setFlags(Flags);
7224 createOperands(N, Ops);
7225 CSEMap.InsertNode(N, IP);
7226 } else {
7227 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7228 createOperands(N, Ops);
7229 }
7230
7231 InsertNode(N);
7232 SDValue V = SDValue(N, 0);
7233 NewSDValueDbgMsg(V, "Creating new node: ", this);
7234 return V;
7235}
7236
7237static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
7238 const APInt &C2) {
7239 switch (Opcode) {
7240 case ISD::ADD: return C1 + C2;
7241 case ISD::SUB: return C1 - C2;
7242 case ISD::MUL: return C1 * C2;
7243 case ISD::AND: return C1 & C2;
7244 case ISD::OR: return C1 | C2;
7245 case ISD::XOR: return C1 ^ C2;
7246 case ISD::SHL: return C1 << C2;
7247 case ISD::SRL: return C1.lshr(C2);
7248 case ISD::SRA: return C1.ashr(C2);
7249 case ISD::ROTL: return C1.rotl(C2);
7250 case ISD::ROTR: return C1.rotr(C2);
7251 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
7252 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
7253 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
7254 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
7255 case ISD::SADDSAT: return C1.sadd_sat(C2);
7256 case ISD::UADDSAT: return C1.uadd_sat(C2);
7257 case ISD::SSUBSAT: return C1.ssub_sat(C2);
7258 case ISD::USUBSAT: return C1.usub_sat(C2);
7259 case ISD::SSHLSAT: return C1.sshl_sat(C2);
7260 case ISD::USHLSAT: return C1.ushl_sat(C2);
7261 case ISD::UDIV:
7262 if (!C2.getBoolValue())
7263 break;
7264 return C1.udiv(C2);
7265 case ISD::UREM:
7266 if (!C2.getBoolValue())
7267 break;
7268 return C1.urem(C2);
7269 case ISD::SDIV:
7270 if (!C2.getBoolValue())
7271 break;
7272 return C1.sdiv(C2);
7273 case ISD::SREM:
7274 if (!C2.getBoolValue())
7275 break;
7276 return C1.srem(C2);
7277 case ISD::AVGFLOORS:
7278 return APIntOps::avgFloorS(C1, C2);
7279 case ISD::AVGFLOORU:
7280 return APIntOps::avgFloorU(C1, C2);
7281 case ISD::AVGCEILS:
7282 return APIntOps::avgCeilS(C1, C2);
7283 case ISD::AVGCEILU:
7284 return APIntOps::avgCeilU(C1, C2);
7285 case ISD::ABDS:
7286 return APIntOps::abds(C1, C2);
7287 case ISD::ABDU:
7288 return APIntOps::abdu(C1, C2);
7289 case ISD::MULHS:
7290 return APIntOps::mulhs(C1, C2);
7291 case ISD::MULHU:
7292 return APIntOps::mulhu(C1, C2);
7293 case ISD::CLMUL:
7294 return APIntOps::clmul(C1, C2);
7295 case ISD::CLMULR:
7296 return APIntOps::clmulr(C1, C2);
7297 case ISD::CLMULH:
7298 return APIntOps::clmulh(C1, C2);
7299 }
7300 return std::nullopt;
7301}
7302// Handle constant folding with UNDEF.
7303// TODO: Handle more cases.
7304static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
7305 bool IsUndef1, const APInt &C2,
7306 bool IsUndef2) {
7307 if (!(IsUndef1 || IsUndef2))
7308 return FoldValue(Opcode, C1, C2);
7309
7310 // Fold and(x, undef) -> 0
7311 // Fold mul(x, undef) -> 0
7312 if (Opcode == ISD::AND || Opcode == ISD::MUL)
7313 return APInt::getZero(C1.getBitWidth());
7314
7315 return std::nullopt;
7316}
7317
7319 const GlobalAddressSDNode *GA,
7320 const SDNode *N2) {
7321 if (GA->getOpcode() != ISD::GlobalAddress)
7322 return SDValue();
7323 if (!TLI->isOffsetFoldingLegal(GA))
7324 return SDValue();
7325 auto *C2 = dyn_cast<ConstantSDNode>(N2);
7326 if (!C2)
7327 return SDValue();
7328 int64_t Offset = C2->getSExtValue();
7329 switch (Opcode) {
7330 case ISD::ADD:
7331 case ISD::PTRADD:
7332 break;
7333 case ISD::SUB: Offset = -uint64_t(Offset); break;
7334 default: return SDValue();
7335 }
7336 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
7337 GA->getOffset() + uint64_t(Offset));
7338}
7339
7341 switch (Opcode) {
7342 case ISD::SDIV:
7343 case ISD::UDIV:
7344 case ISD::SREM:
7345 case ISD::UREM: {
7346 // If a divisor is zero/undef or any element of a divisor vector is
7347 // zero/undef, the whole op is undef.
7348 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
7349 SDValue Divisor = Ops[1];
7350 if (Divisor.isUndef() || isNullConstant(Divisor))
7351 return true;
7352
7353 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
7354 llvm::any_of(Divisor->op_values(),
7355 [](SDValue V) { return V.isUndef() ||
7356 isNullConstant(V); });
7357 // TODO: Handle signed overflow.
7358 }
7359 // TODO: Handle oversized shifts.
7360 default:
7361 return false;
7362 }
7363}
7364
7367 SDNodeFlags Flags) {
7368 // If the opcode is a target-specific ISD node, there's nothing we can
7369 // do here and the operand rules may not line up with the below, so
7370 // bail early.
7371 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
7372 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
7373 // foldCONCAT_VECTORS in getNode before this is called.
7374 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
7375 return SDValue();
7376
7377 unsigned NumOps = Ops.size();
7378 if (NumOps == 0)
7379 return SDValue();
7380
7381 if (isUndef(Opcode, Ops))
7382 return getUNDEF(VT);
7383
7384 // Handle unary special cases.
7385 if (NumOps == 1) {
7386 SDValue N1 = Ops[0];
7387
7388 // Constant fold unary operations with an integer constant operand. Even
7389 // opaque constant will be folded, because the folding of unary operations
7390 // doesn't create new constants with different values. Nevertheless, the
7391 // opaque flag is preserved during folding to prevent future folding with
7392 // other constants.
7393 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
7394 const APInt &Val = C->getAPIntValue();
7395 switch (Opcode) {
7396 case ISD::SIGN_EXTEND:
7397 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7398 C->isTargetOpcode(), C->isOpaque());
7399 case ISD::TRUNCATE:
7400 if (C->isOpaque())
7401 break;
7402 [[fallthrough]];
7403 case ISD::ZERO_EXTEND:
7404 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7405 C->isTargetOpcode(), C->isOpaque());
7406 case ISD::ANY_EXTEND:
7407 // Some targets like RISCV prefer to sign extend some types.
7408 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7409 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7410 C->isTargetOpcode(), C->isOpaque());
7411 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7412 C->isTargetOpcode(), C->isOpaque());
7413 case ISD::ABS:
7414 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7415 C->isOpaque());
7416 case ISD::BITREVERSE:
7417 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7418 C->isOpaque());
7419 case ISD::BSWAP:
7420 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7421 C->isOpaque());
7422 case ISD::CTPOP:
7423 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7424 C->isOpaque());
7425 case ISD::CTLZ:
7427 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7428 C->isOpaque());
7429 case ISD::CTTZ:
7431 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7432 C->isOpaque());
7433 case ISD::CTLS:
7434 // CTLS returns the number of extra sign bits so subtract one.
7435 return getConstant(Val.getNumSignBits() - 1, DL, VT,
7436 C->isTargetOpcode(), C->isOpaque());
7437 case ISD::UINT_TO_FP:
7438 case ISD::SINT_TO_FP: {
7440 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7442 return getConstantFP(FPV, DL, VT);
7443 }
7444 case ISD::FP16_TO_FP:
7445 case ISD::BF16_TO_FP: {
7446 bool Ignored;
7447 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7448 : APFloat::BFloat(),
7449 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7450
7451 // This can return overflow, underflow, or inexact; we don't care.
7452 // FIXME need to be more flexible about rounding mode.
7454 &Ignored);
7455 return getConstantFP(FPV, DL, VT);
7456 }
7457 case ISD::STEP_VECTOR:
7458 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7459 return V;
7460 break;
7461 case ISD::BITCAST:
7462 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7463 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7464 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7465 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7466 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7467 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7468 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7469 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7470 break;
7471 }
7472 }
7473
7474 // Constant fold unary operations with a floating point constant operand.
7475 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7476 APFloat V = C->getValueAPF(); // make copy
7477 switch (Opcode) {
7478 case ISD::FNEG:
7479 V.changeSign();
7480 return getConstantFP(V, DL, VT);
7481 case ISD::FABS:
7482 V.clearSign();
7483 return getConstantFP(V, DL, VT);
7484 case ISD::FCEIL: {
7485 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7487 return getConstantFP(V, DL, VT);
7488 return SDValue();
7489 }
7490 case ISD::FTRUNC: {
7491 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7493 return getConstantFP(V, DL, VT);
7494 return SDValue();
7495 }
7496 case ISD::FFLOOR: {
7497 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7499 return getConstantFP(V, DL, VT);
7500 return SDValue();
7501 }
7502 case ISD::FP_EXTEND: {
7503 bool ignored;
7504 // This can return overflow, underflow, or inexact; we don't care.
7505 // FIXME need to be more flexible about rounding mode.
7506 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7507 &ignored);
7508 return getConstantFP(V, DL, VT);
7509 }
7510 case ISD::FP_TO_SINT:
7511 case ISD::FP_TO_UINT: {
7512 bool ignored;
7513 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7514 // FIXME need to be more flexible about rounding mode.
7516 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7517 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7518 break;
7519 return getConstant(IntVal, DL, VT);
7520 }
7521 case ISD::FP_TO_FP16:
7522 case ISD::FP_TO_BF16: {
7523 bool Ignored;
7524 // This can return overflow, underflow, or inexact; we don't care.
7525 // FIXME need to be more flexible about rounding mode.
7526 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7527 : APFloat::BFloat(),
7529 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7530 }
7531 case ISD::BITCAST:
7532 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7533 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7534 VT);
7535 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7536 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7537 VT);
7538 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7539 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7540 VT);
7541 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7542 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7543 break;
7544 }
7545 }
7546
7547 // Early-out if we failed to constant fold a bitcast.
7548 if (Opcode == ISD::BITCAST)
7549 return SDValue();
7550 }
7551
7552 // Handle binops special cases.
7553 if (NumOps == 2) {
7554 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7555 return CFP;
7556
7557 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7558 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7559 if (C1->isOpaque() || C2->isOpaque())
7560 return SDValue();
7561
7562 std::optional<APInt> FoldAttempt =
7563 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7564 if (!FoldAttempt)
7565 return SDValue();
7566
7567 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7568 assert((!Folded || !VT.isVector()) &&
7569 "Can't fold vectors ops with scalar operands");
7570 return Folded;
7571 }
7572 }
7573
7574 // fold (add Sym, c) -> Sym+c
7576 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7577 if (TLI->isCommutativeBinOp(Opcode))
7579 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7580
7581 // fold (sext_in_reg c1) -> c2
7582 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7583 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7584
7585 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7586 unsigned FromBits = EVT.getScalarSizeInBits();
7587 Val <<= Val.getBitWidth() - FromBits;
7588 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7589 return getConstant(Val, DL, ConstantVT);
7590 };
7591
7592 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7593 const APInt &Val = C1->getAPIntValue();
7594 return SignExtendInReg(Val, VT);
7595 }
7596
7598 SmallVector<SDValue, 8> ScalarOps;
7599 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7600 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7601 SDValue Op = Ops[0].getOperand(I);
7602 if (Op.isUndef()) {
7603 ScalarOps.push_back(getUNDEF(OpVT));
7604 continue;
7605 }
7606 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7607 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7608 }
7609 return getBuildVector(VT, DL, ScalarOps);
7610 }
7611
7612 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7613 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7614 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7615 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7616 Ops[0].getOperand(0).getValueType()));
7617 }
7618 }
7619
7620 // Handle fshl/fshr special cases.
7621 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7622 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7623 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7624 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7625
7626 if (C1 && C2 && C3) {
7627 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7628 return SDValue();
7629 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7630 &V3 = C3->getAPIntValue();
7631
7632 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7633 : APIntOps::fshr(V1, V2, V3);
7634 return getConstant(FoldedVal, DL, VT);
7635 }
7636 }
7637
7638 // Handle fma/fmad special cases.
7639 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7640 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7641 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7642 Ops[2].getValueType() == VT && "FMA types must match!");
7646 if (C1 && C2 && C3) {
7647 APFloat V1 = C1->getValueAPF();
7648 const APFloat &V2 = C2->getValueAPF();
7649 const APFloat &V3 = C3->getValueAPF();
7650 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7653 } else
7655 return getConstantFP(V1, DL, VT);
7656 }
7657 }
7658
7659 // This is for vector folding only from here on.
7660 if (!VT.isVector())
7661 return SDValue();
7662
7663 ElementCount NumElts = VT.getVectorElementCount();
7664
7665 // See if we can fold through any bitcasted integer ops.
7666 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7667 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7668 (Ops[0].getOpcode() == ISD::BITCAST ||
7669 Ops[1].getOpcode() == ISD::BITCAST)) {
7672 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7673 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7674 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7675 N2.getValueType().isInteger()) {
7676 bool IsLE = getDataLayout().isLittleEndian();
7677 unsigned EltBits = VT.getScalarSizeInBits();
7678 SmallVector<APInt> RawBits1, RawBits2;
7679 BitVector UndefElts1, UndefElts2;
7680 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7681 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7682 SmallVector<APInt> RawBits;
7683 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7684 std::optional<APInt> Fold = FoldValueWithUndef(
7685 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7686 if (!Fold)
7687 break;
7688 RawBits.push_back(*Fold);
7689 }
7690 if (RawBits.size() == NumElts.getFixedValue()) {
7691 // We have constant folded, but we might need to cast this again back
7692 // to the original (possibly legalized) type.
7693 EVT BVVT, BVEltVT;
7694 if (N1.getValueType() == VT) {
7695 BVVT = N1.getValueType();
7696 BVEltVT = BV1->getOperand(0).getValueType();
7697 } else {
7698 BVVT = N2.getValueType();
7699 BVEltVT = BV2->getOperand(0).getValueType();
7700 }
7701 unsigned BVEltBits = BVEltVT.getSizeInBits();
7702 SmallVector<APInt> DstBits;
7703 BitVector DstUndefs;
7705 DstBits, RawBits, DstUndefs,
7706 BitVector(RawBits.size(), false));
7707 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7708 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7709 if (DstUndefs[I])
7710 continue;
7711 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7712 }
7713 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7714 }
7715 }
7716 }
7717 }
7718
7719 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7720 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7721 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7722 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7723 APInt RHSVal;
7724 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7725 APInt NewStep = Opcode == ISD::MUL
7726 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7727 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7728 return getStepVector(DL, VT, NewStep);
7729 }
7730 }
7731
7732 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7733 return !Op.getValueType().isVector() ||
7734 Op.getValueType().getVectorElementCount() == NumElts;
7735 };
7736
7737 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7738 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7739 Op.getOpcode() == ISD::BUILD_VECTOR ||
7740 Op.getOpcode() == ISD::SPLAT_VECTOR;
7741 };
7742
7743 // All operands must be vector types with the same number of elements as
7744 // the result type and must be either UNDEF or a build/splat vector
7745 // or UNDEF scalars.
7746 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7747 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7748 return SDValue();
7749
7750 // If we are comparing vectors, then the result needs to be a i1 boolean that
7751 // is then extended back to the legal result type depending on how booleans
7752 // are represented.
7753 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7754 ISD::NodeType ExtendCode =
7755 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7756 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7758
7759 // Find legal integer scalar type for constant promotion and
7760 // ensure that its scalar size is at least as large as source.
7761 EVT LegalSVT = VT.getScalarType();
7762 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7763 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7764 if (LegalSVT.bitsLT(VT.getScalarType()))
7765 return SDValue();
7766 }
7767
7768 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7769 // only have one operand to check. For fixed-length vector types we may have
7770 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7771 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7772
7773 // Constant fold each scalar lane separately.
7774 SmallVector<SDValue, 4> ScalarResults;
7775 for (unsigned I = 0; I != NumVectorElts; I++) {
7776 SmallVector<SDValue, 4> ScalarOps;
7777 for (SDValue Op : Ops) {
7778 EVT InSVT = Op.getValueType().getScalarType();
7779 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7780 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7781 if (Op.isUndef())
7782 ScalarOps.push_back(getUNDEF(InSVT));
7783 else
7784 ScalarOps.push_back(Op);
7785 continue;
7786 }
7787
7788 SDValue ScalarOp =
7789 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7790 EVT ScalarVT = ScalarOp.getValueType();
7791
7792 // Build vector (integer) scalar operands may need implicit
7793 // truncation - do this before constant folding.
7794 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7795 // Don't create illegally-typed nodes unless they're constants or undef
7796 // - if we fail to constant fold we can't guarantee the (dead) nodes
7797 // we're creating will be cleaned up before being visited for
7798 // legalization.
7799 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7800 !isa<ConstantSDNode>(ScalarOp) &&
7801 TLI->getTypeAction(*getContext(), InSVT) !=
7803 return SDValue();
7804 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7805 }
7806
7807 ScalarOps.push_back(ScalarOp);
7808 }
7809
7810 // Constant fold the scalar operands.
7811 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7812
7813 // Scalar folding only succeeded if the result is a constant or UNDEF.
7814 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7815 ScalarResult.getOpcode() != ISD::ConstantFP)
7816 return SDValue();
7817
7818 // Legalize the (integer) scalar constant if necessary. We only do
7819 // this once we know the folding succeeded, since otherwise we would
7820 // get a node with illegal type which has a user.
7821 if (LegalSVT != SVT)
7822 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7823
7824 ScalarResults.push_back(ScalarResult);
7825 }
7826
7827 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7828 : getBuildVector(VT, DL, ScalarResults);
7829 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7830 return V;
7831}
7832
7835 // TODO: Add support for unary/ternary fp opcodes.
7836 if (Ops.size() != 2)
7837 return SDValue();
7838
7839 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7840 // should. That will require dealing with a potentially non-default
7841 // rounding mode, checking the "opStatus" return value from the APFloat
7842 // math calculations, and possibly other variations.
7843 SDValue N1 = Ops[0];
7844 SDValue N2 = Ops[1];
7845 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7846 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7847 if (N1CFP && N2CFP) {
7848 APFloat C1 = N1CFP->getValueAPF(); // make copy
7849 const APFloat &C2 = N2CFP->getValueAPF();
7850 switch (Opcode) {
7851 case ISD::FADD:
7853 return getConstantFP(C1, DL, VT);
7854 case ISD::FSUB:
7856 return getConstantFP(C1, DL, VT);
7857 case ISD::FMUL:
7859 return getConstantFP(C1, DL, VT);
7860 case ISD::FDIV:
7862 return getConstantFP(C1, DL, VT);
7863 case ISD::FREM:
7864 C1.mod(C2);
7865 return getConstantFP(C1, DL, VT);
7866 case ISD::FCOPYSIGN:
7867 C1.copySign(C2);
7868 return getConstantFP(C1, DL, VT);
7869 case ISD::FMINNUM:
7870 return getConstantFP(minnum(C1, C2), DL, VT);
7871 case ISD::FMAXNUM:
7872 return getConstantFP(maxnum(C1, C2), DL, VT);
7873 case ISD::FMINIMUM:
7874 return getConstantFP(minimum(C1, C2), DL, VT);
7875 case ISD::FMAXIMUM:
7876 return getConstantFP(maximum(C1, C2), DL, VT);
7877 case ISD::FMINIMUMNUM:
7878 return getConstantFP(minimumnum(C1, C2), DL, VT);
7879 case ISD::FMAXIMUMNUM:
7880 return getConstantFP(maximumnum(C1, C2), DL, VT);
7881 default: break;
7882 }
7883 }
7884 if (N1CFP && Opcode == ISD::FP_ROUND) {
7885 APFloat C1 = N1CFP->getValueAPF(); // make copy
7886 bool Unused;
7887 // This can return overflow, underflow, or inexact; we don't care.
7888 // FIXME need to be more flexible about rounding mode.
7890 &Unused);
7891 return getConstantFP(C1, DL, VT);
7892 }
7893
7894 switch (Opcode) {
7895 case ISD::FSUB:
7896 // -0.0 - undef --> undef (consistent with "fneg undef")
7897 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7898 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7899 return getUNDEF(VT);
7900 [[fallthrough]];
7901
7902 case ISD::FADD:
7903 case ISD::FMUL:
7904 case ISD::FDIV:
7905 case ISD::FREM:
7906 // If both operands are undef, the result is undef. If 1 operand is undef,
7907 // the result is NaN. This should match the behavior of the IR optimizer.
7908 if (N1.isUndef() && N2.isUndef())
7909 return getUNDEF(VT);
7910 if (N1.isUndef() || N2.isUndef())
7912 }
7913 return SDValue();
7914}
7915
7917 const SDLoc &DL, EVT DstEltVT) {
7918 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7919
7920 // If this is already the right type, we're done.
7921 if (SrcEltVT == DstEltVT)
7922 return SDValue(BV, 0);
7923
7924 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7925 unsigned DstBitSize = DstEltVT.getSizeInBits();
7926
7927 // If this is a conversion of N elements of one type to N elements of another
7928 // type, convert each element. This handles FP<->INT cases.
7929 if (SrcBitSize == DstBitSize) {
7931 for (SDValue Op : BV->op_values()) {
7932 // If the vector element type is not legal, the BUILD_VECTOR operands
7933 // are promoted and implicitly truncated. Make that explicit here.
7934 if (Op.getValueType() != SrcEltVT)
7935 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7936 Ops.push_back(getBitcast(DstEltVT, Op));
7937 }
7938 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7940 return getBuildVector(VT, DL, Ops);
7941 }
7942
7943 // Otherwise, we're growing or shrinking the elements. To avoid having to
7944 // handle annoying details of growing/shrinking FP values, we convert them to
7945 // int first.
7946 if (SrcEltVT.isFloatingPoint()) {
7947 // Convert the input float vector to a int vector where the elements are the
7948 // same sizes.
7949 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7950 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7952 DstEltVT);
7953 return SDValue();
7954 }
7955
7956 // Now we know the input is an integer vector. If the output is a FP type,
7957 // convert to integer first, then to FP of the right size.
7958 if (DstEltVT.isFloatingPoint()) {
7959 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7960 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7962 DstEltVT);
7963 return SDValue();
7964 }
7965
7966 // Okay, we know the src/dst types are both integers of differing types.
7967 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7968
7969 // Extract the constant raw bit data.
7970 BitVector UndefElements;
7971 SmallVector<APInt> RawBits;
7972 bool IsLE = getDataLayout().isLittleEndian();
7973 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7974 return SDValue();
7975
7977 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7978 if (UndefElements[I])
7979 Ops.push_back(getUNDEF(DstEltVT));
7980 else
7981 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7982 }
7983
7984 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7985 return getBuildVector(VT, DL, Ops);
7986}
7987
7989 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7990
7991 // There's no need to assert on a byte-aligned pointer. All pointers are at
7992 // least byte aligned.
7993 if (A == Align(1))
7994 return Val;
7995
7996 SDVTList VTs = getVTList(Val.getValueType());
7998 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7999 ID.AddInteger(A.value());
8000
8001 void *IP = nullptr;
8002 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
8003 return SDValue(E, 0);
8004
8005 auto *N =
8006 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
8007 createOperands(N, {Val});
8008
8009 CSEMap.InsertNode(N, IP);
8010 InsertNode(N);
8011
8012 SDValue V(N, 0);
8013 NewSDValueDbgMsg(V, "Creating new node: ", this);
8014 return V;
8015}
8016
8017SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8018 SDValue N1, SDValue N2) {
8019 SDNodeFlags Flags;
8020 if (Inserter)
8021 Flags = Inserter->getFlags();
8022 return getNode(Opcode, DL, VT, N1, N2, Flags);
8023}
8024
8026 SDValue &N2) const {
8027 if (!TLI->isCommutativeBinOp(Opcode))
8028 return;
8029
8030 // Canonicalize:
8031 // binop(const, nonconst) -> binop(nonconst, const)
8034 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
8035 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
8036 if ((N1C && !N2C) || (N1CFP && !N2CFP))
8037 std::swap(N1, N2);
8038
8039 // Canonicalize:
8040 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
8041 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
8043 std::swap(N1, N2);
8044}
8045
8046SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8047 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
8049 N2.getOpcode() != ISD::DELETED_NODE &&
8050 "Operand is DELETED_NODE!");
8051
8052 canonicalizeCommutativeBinop(Opcode, N1, N2);
8053
8054 auto *N1C = dyn_cast<ConstantSDNode>(N1);
8055 auto *N2C = dyn_cast<ConstantSDNode>(N2);
8056
8057 // Don't allow undefs in vector splats - we might be returning N2 when folding
8058 // to zero etc.
8059 ConstantSDNode *N2CV =
8060 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
8061
8062 switch (Opcode) {
8063 default: break;
8064 case ISD::TokenFactor:
8065 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
8066 N2.getValueType() == MVT::Other && "Invalid token factor!");
8067 // Fold trivial token factors.
8068 if (N1.getOpcode() == ISD::EntryToken) return N2;
8069 if (N2.getOpcode() == ISD::EntryToken) return N1;
8070 if (N1 == N2) return N1;
8071 break;
8072 case ISD::BUILD_VECTOR: {
8073 // Attempt to simplify BUILD_VECTOR.
8074 SDValue Ops[] = {N1, N2};
8075 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8076 return V;
8077 break;
8078 }
8079 case ISD::CONCAT_VECTORS: {
8080 SDValue Ops[] = {N1, N2};
8081 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8082 return V;
8083 break;
8084 }
8085 case ISD::AND:
8086 assert(VT.isInteger() && "This operator does not apply to FP types!");
8087 assert(N1.getValueType() == N2.getValueType() &&
8088 N1.getValueType() == VT && "Binary operator types must match!");
8089 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
8090 // worth handling here.
8091 if (N2CV && N2CV->isZero())
8092 return N2;
8093 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
8094 return N1;
8095 break;
8096 case ISD::OR:
8097 case ISD::XOR:
8098 case ISD::ADD:
8099 case ISD::PTRADD:
8100 case ISD::SUB:
8101 assert(VT.isInteger() && "This operator does not apply to FP types!");
8102 assert(N1.getValueType() == N2.getValueType() &&
8103 N1.getValueType() == VT && "Binary operator types must match!");
8104 // The equal operand types requirement is unnecessarily strong for PTRADD.
8105 // However, the SelectionDAGBuilder does not generate PTRADDs with different
8106 // operand types, and we'd need to re-implement GEP's non-standard wrapping
8107 // logic everywhere where PTRADDs may be folded or combined to properly
8108 // support them. If/when we introduce pointer types to the SDAG, we will
8109 // need to relax this constraint.
8110
8111 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
8112 // it's worth handling here.
8113 if (N2CV && N2CV->isZero())
8114 return N1;
8115 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
8116 VT.getScalarType() == MVT::i1)
8117 return getNode(ISD::XOR, DL, VT, N1, N2);
8118 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
8119 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
8120 N2.getOpcode() == ISD::VSCALE) {
8121 const APInt &C1 = N1->getConstantOperandAPInt(0);
8122 const APInt &C2 = N2->getConstantOperandAPInt(0);
8123 return getVScale(DL, VT, C1 + C2);
8124 }
8125 break;
8126 case ISD::MUL:
8127 assert(VT.isInteger() && "This operator does not apply to FP types!");
8128 assert(N1.getValueType() == N2.getValueType() &&
8129 N1.getValueType() == VT && "Binary operator types must match!");
8130 if (VT.getScalarType() == MVT::i1)
8131 return getNode(ISD::AND, DL, VT, N1, N2);
8132 if (N2CV && N2CV->isZero())
8133 return N2;
8134 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
8135 const APInt &MulImm = N1->getConstantOperandAPInt(0);
8136 const APInt &N2CImm = N2C->getAPIntValue();
8137 return getVScale(DL, VT, MulImm * N2CImm);
8138 }
8139 break;
8140 case ISD::UDIV:
8141 case ISD::UREM:
8142 case ISD::MULHU:
8143 case ISD::MULHS:
8144 case ISD::SDIV:
8145 case ISD::SREM:
8146 case ISD::SADDSAT:
8147 case ISD::SSUBSAT:
8148 case ISD::UADDSAT:
8149 case ISD::USUBSAT:
8150 assert(VT.isInteger() && "This operator does not apply to FP types!");
8151 assert(N1.getValueType() == N2.getValueType() &&
8152 N1.getValueType() == VT && "Binary operator types must match!");
8153 if (VT.getScalarType() == MVT::i1) {
8154 // fold (add_sat x, y) -> (or x, y) for bool types.
8155 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
8156 return getNode(ISD::OR, DL, VT, N1, N2);
8157 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
8158 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
8159 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
8160 }
8161 break;
8162 case ISD::SCMP:
8163 case ISD::UCMP:
8164 assert(N1.getValueType() == N2.getValueType() &&
8165 "Types of operands of UCMP/SCMP must match");
8166 assert(N1.getValueType().isVector() == VT.isVector() &&
8167 "Operands and return type of must both be scalars or vectors");
8168 if (VT.isVector())
8171 "Result and operands must have the same number of elements");
8172 break;
8173 case ISD::AVGFLOORS:
8174 case ISD::AVGFLOORU:
8175 case ISD::AVGCEILS:
8176 case ISD::AVGCEILU:
8177 assert(VT.isInteger() && "This operator does not apply to FP types!");
8178 assert(N1.getValueType() == N2.getValueType() &&
8179 N1.getValueType() == VT && "Binary operator types must match!");
8180 break;
8181 case ISD::ABDS:
8182 case ISD::ABDU:
8183 assert(VT.isInteger() && "This operator does not apply to FP types!");
8184 assert(N1.getValueType() == N2.getValueType() &&
8185 N1.getValueType() == VT && "Binary operator types must match!");
8186 if (VT.getScalarType() == MVT::i1)
8187 return getNode(ISD::XOR, DL, VT, N1, N2);
8188 break;
8189 case ISD::SMIN:
8190 case ISD::UMAX:
8191 assert(VT.isInteger() && "This operator does not apply to FP types!");
8192 assert(N1.getValueType() == N2.getValueType() &&
8193 N1.getValueType() == VT && "Binary operator types must match!");
8194 if (VT.getScalarType() == MVT::i1)
8195 return getNode(ISD::OR, DL, VT, N1, N2);
8196 break;
8197 case ISD::SMAX:
8198 case ISD::UMIN:
8199 assert(VT.isInteger() && "This operator does not apply to FP types!");
8200 assert(N1.getValueType() == N2.getValueType() &&
8201 N1.getValueType() == VT && "Binary operator types must match!");
8202 if (VT.getScalarType() == MVT::i1)
8203 return getNode(ISD::AND, DL, VT, N1, N2);
8204 break;
8205 case ISD::FADD:
8206 case ISD::FSUB:
8207 case ISD::FMUL:
8208 case ISD::FDIV:
8209 case ISD::FREM:
8210 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
8211 assert(N1.getValueType() == N2.getValueType() &&
8212 N1.getValueType() == VT && "Binary operator types must match!");
8213 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
8214 return V;
8215 break;
8216 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
8217 assert(N1.getValueType() == VT &&
8220 "Invalid FCOPYSIGN!");
8221 break;
8222 case ISD::SHL:
8223 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
8224 const APInt &MulImm = N1->getConstantOperandAPInt(0);
8225 const APInt &ShiftImm = N2C->getAPIntValue();
8226 return getVScale(DL, VT, MulImm << ShiftImm);
8227 }
8228 [[fallthrough]];
8229 case ISD::SRA:
8230 case ISD::SRL:
8231 if (SDValue V = simplifyShift(N1, N2))
8232 return V;
8233 [[fallthrough]];
8234 case ISD::ROTL:
8235 case ISD::ROTR:
8236 case ISD::SSHLSAT:
8237 case ISD::USHLSAT:
8238 assert(VT == N1.getValueType() &&
8239 "Shift operators return type must be the same as their first arg");
8240 assert(VT.isInteger() && N2.getValueType().isInteger() &&
8241 "Shifts only work on integers");
8242 assert((!VT.isVector() || VT == N2.getValueType()) &&
8243 "Vector shift amounts must be in the same as their first arg");
8244 // Verify that the shift amount VT is big enough to hold valid shift
8245 // amounts. This catches things like trying to shift an i1024 value by an
8246 // i8, which is easy to fall into in generic code that uses
8247 // TLI.getShiftAmount().
8250 "Invalid use of small shift amount with oversized value!");
8251
8252 // Always fold shifts of i1 values so the code generator doesn't need to
8253 // handle them. Since we know the size of the shift has to be less than the
8254 // size of the value, the shift/rotate count is guaranteed to be zero.
8255 if (VT == MVT::i1)
8256 return N1;
8257 if (N2CV && N2CV->isZero())
8258 return N1;
8259 break;
8260 case ISD::FP_ROUND:
8262 VT.bitsLE(N1.getValueType()) && N2C &&
8263 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
8264 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
8265 if (N1.getValueType() == VT) return N1; // noop conversion.
8266 break;
8267 case ISD::IS_FPCLASS: {
8269 "IS_FPCLASS is used for a non-floating type");
8270 assert(isa<ConstantSDNode>(N2) && "FPClassTest is not Constant");
8271 FPClassTest Mask = static_cast<FPClassTest>(N2->getAsZExtVal());
8272 // If all tests are made, it doesn't matter what the value is.
8273 if ((Mask & fcAllFlags) == fcAllFlags)
8274 return getBoolConstant(true, DL, VT, N1.getValueType());
8275 if ((Mask & fcAllFlags) == 0)
8276 return getBoolConstant(false, DL, VT, N1.getValueType());
8277 break;
8278 }
8279 case ISD::AssertNoFPClass: {
8281 "AssertNoFPClass is used for a non-floating type");
8282 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
8283 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
8284 assert(llvm::to_underlying(NoFPClass) <=
8286 "FPClassTest value too large");
8287 (void)NoFPClass;
8288 break;
8289 }
8290 case ISD::AssertSext:
8291 case ISD::AssertZext: {
8292 EVT EVT = cast<VTSDNode>(N2)->getVT();
8293 assert(VT == N1.getValueType() && "Not an inreg extend!");
8294 assert(VT.isInteger() && EVT.isInteger() &&
8295 "Cannot *_EXTEND_INREG FP types");
8296 assert(!EVT.isVector() &&
8297 "AssertSExt/AssertZExt type should be the vector element type "
8298 "rather than the vector type!");
8299 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
8300 if (VT.getScalarType() == EVT) return N1; // noop assertion.
8301 break;
8302 }
8304 EVT EVT = cast<VTSDNode>(N2)->getVT();
8305 assert(VT == N1.getValueType() && "Not an inreg extend!");
8306 assert(VT.isInteger() && EVT.isInteger() &&
8307 "Cannot *_EXTEND_INREG FP types");
8308 assert(EVT.isVector() == VT.isVector() &&
8309 "SIGN_EXTEND_INREG type should be vector iff the operand "
8310 "type is vector!");
8311 assert((!EVT.isVector() ||
8313 "Vector element counts must match in SIGN_EXTEND_INREG");
8314 assert(EVT.getScalarType().bitsLE(VT.getScalarType()) && "Not extending!");
8315 if (EVT == VT) return N1; // Not actually extending
8316 break;
8317 }
8319 case ISD::FP_TO_UINT_SAT: {
8320 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
8321 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
8322 assert(N1.getValueType().isVector() == VT.isVector() &&
8323 "FP_TO_*INT_SAT type should be vector iff the operand type is "
8324 "vector!");
8325 assert((!VT.isVector() || VT.getVectorElementCount() ==
8327 "Vector element counts must match in FP_TO_*INT_SAT");
8328 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
8329 "Type to saturate to must be a scalar.");
8330 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
8331 "Not extending!");
8332 break;
8333 }
8336 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
8337 element type of the vector.");
8338
8339 // Extract from an undefined value or using an undefined index is undefined.
8340 if (N1.isUndef() || N2.isUndef())
8341 return getUNDEF(VT);
8342
8343 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
8344 // vectors. For scalable vectors we will provide appropriate support for
8345 // dealing with arbitrary indices.
8346 if (N2C && N1.getValueType().isFixedLengthVector() &&
8347 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
8348 return getUNDEF(VT);
8349
8350 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
8351 // expanding copies of large vectors from registers. This only works for
8352 // fixed length vectors, since we need to know the exact number of
8353 // elements.
8354 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
8356 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
8357 return getExtractVectorElt(DL, VT,
8358 N1.getOperand(N2C->getZExtValue() / Factor),
8359 N2C->getZExtValue() % Factor);
8360 }
8361
8362 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
8363 // lowering is expanding large vector constants.
8364 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
8365 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
8368 "BUILD_VECTOR used for scalable vectors");
8369 unsigned Index =
8370 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
8371 SDValue Elt = N1.getOperand(Index);
8372
8373 if (VT != Elt.getValueType())
8374 // If the vector element type is not legal, the BUILD_VECTOR operands
8375 // are promoted and implicitly truncated, and the result implicitly
8376 // extended. Make that explicit here.
8377 Elt = getAnyExtOrTrunc(Elt, DL, VT);
8378
8379 return Elt;
8380 }
8381
8382 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
8383 // operations are lowered to scalars.
8384 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
8385 // If the indices are the same, return the inserted element else
8386 // if the indices are known different, extract the element from
8387 // the original vector.
8388 SDValue N1Op2 = N1.getOperand(2);
8390
8391 if (N1Op2C && N2C) {
8392 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
8393 if (VT == N1.getOperand(1).getValueType())
8394 return N1.getOperand(1);
8395 if (VT.isFloatingPoint()) {
8397 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
8398 }
8399 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
8400 }
8401 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
8402 }
8403 }
8404
8405 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
8406 // when vector types are scalarized and v1iX is legal.
8407 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
8408 // Here we are completely ignoring the extract element index (N2),
8409 // which is fine for fixed width vectors, since any index other than 0
8410 // is undefined anyway. However, this cannot be ignored for scalable
8411 // vectors - in theory we could support this, but we don't want to do this
8412 // without a profitability check.
8413 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8415 N1.getValueType().getVectorNumElements() == 1) {
8416 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8417 N1.getOperand(1));
8418 }
8419 break;
8421 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8422 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8423 (N1.getValueType().isInteger() == VT.isInteger()) &&
8424 N1.getValueType() != VT &&
8425 "Wrong types for EXTRACT_ELEMENT!");
8426
8427 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8428 // 64-bit integers into 32-bit parts. Instead of building the extract of
8429 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8430 if (N1.getOpcode() == ISD::BUILD_PAIR)
8431 return N1.getOperand(N2C->getZExtValue());
8432
8433 // EXTRACT_ELEMENT of a constant int is also very common.
8434 if (N1C) {
8435 unsigned ElementSize = VT.getSizeInBits();
8436 unsigned Shift = ElementSize * N2C->getZExtValue();
8437 const APInt &Val = N1C->getAPIntValue();
8438 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8439 }
8440 break;
8442 EVT N1VT = N1.getValueType();
8443 assert(VT.isVector() && N1VT.isVector() &&
8444 "Extract subvector VTs must be vectors!");
8446 "Extract subvector VTs must have the same element type!");
8447 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8448 "Cannot extract a scalable vector from a fixed length vector!");
8449 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8451 "Extract subvector must be from larger vector to smaller vector!");
8452 assert(N2C && "Extract subvector index must be a constant");
8453 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8454 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8455 N1VT.getVectorMinNumElements()) &&
8456 "Extract subvector overflow!");
8457 assert(N2C->getAPIntValue().getBitWidth() ==
8458 TLI->getVectorIdxWidth(getDataLayout()) &&
8459 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8460 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8461 "Extract index is not a multiple of the output vector length");
8462
8463 // Trivial extraction.
8464 if (VT == N1VT)
8465 return N1;
8466
8467 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8468 if (N1.isUndef())
8469 return getUNDEF(VT);
8470
8471 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8472 // the concat have the same type as the extract.
8473 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8474 VT == N1.getOperand(0).getValueType()) {
8475 unsigned Factor = VT.getVectorMinNumElements();
8476 return N1.getOperand(N2C->getZExtValue() / Factor);
8477 }
8478
8479 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8480 // during shuffle legalization.
8481 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8482 VT == N1.getOperand(1).getValueType())
8483 return N1.getOperand(1);
8484 break;
8485 }
8486 }
8487
8488 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8489 switch (Opcode) {
8490 case ISD::XOR:
8491 case ISD::ADD:
8492 case ISD::PTRADD:
8493 case ISD::SUB:
8495 case ISD::UDIV:
8496 case ISD::SDIV:
8497 case ISD::UREM:
8498 case ISD::SREM:
8499 case ISD::MUL:
8500 case ISD::AND:
8501 case ISD::SSUBSAT:
8502 case ISD::USUBSAT:
8503 case ISD::UMIN:
8504 case ISD::OR:
8505 case ISD::SADDSAT:
8506 case ISD::UADDSAT:
8507 case ISD::UMAX:
8508 case ISD::SMAX:
8509 case ISD::SMIN:
8510 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8511 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8512 }
8513 }
8514
8515 // Canonicalize an UNDEF to the RHS, even over a constant.
8516 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8517 if (TLI->isCommutativeBinOp(Opcode)) {
8518 std::swap(N1, N2);
8519 } else {
8520 switch (Opcode) {
8521 case ISD::PTRADD:
8522 case ISD::SUB:
8523 // fold op(undef, non_undef_arg2) -> undef.
8524 return N1;
8526 case ISD::UDIV:
8527 case ISD::SDIV:
8528 case ISD::UREM:
8529 case ISD::SREM:
8530 case ISD::SSUBSAT:
8531 case ISD::USUBSAT:
8532 // fold op(undef, non_undef_arg2) -> 0.
8533 return getConstant(0, DL, VT);
8534 }
8535 }
8536 }
8537
8538 // Fold a bunch of operators when the RHS is undef.
8539 if (N2.getOpcode() == ISD::UNDEF) {
8540 switch (Opcode) {
8541 case ISD::XOR:
8542 if (N1.getOpcode() == ISD::UNDEF)
8543 // Handle undef ^ undef -> 0 special case. This is a common
8544 // idiom (misuse).
8545 return getConstant(0, DL, VT);
8546 [[fallthrough]];
8547 case ISD::ADD:
8548 case ISD::PTRADD:
8549 case ISD::SUB:
8550 // fold op(arg1, undef) -> undef.
8551 return N2;
8552 case ISD::UDIV:
8553 case ISD::SDIV:
8554 case ISD::UREM:
8555 case ISD::SREM:
8556 // fold op(arg1, undef) -> poison.
8557 return getPOISON(VT);
8558 case ISD::MUL:
8559 case ISD::AND:
8560 case ISD::SSUBSAT:
8561 case ISD::USUBSAT:
8562 case ISD::UMIN:
8563 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8564 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8565 case ISD::OR:
8566 case ISD::SADDSAT:
8567 case ISD::UADDSAT:
8568 case ISD::UMAX:
8569 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8570 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8571 case ISD::SMAX:
8572 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8573 return N1.getOpcode() == ISD::UNDEF
8574 ? N2
8575 : getConstant(
8577 VT);
8578 case ISD::SMIN:
8579 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8580 return N1.getOpcode() == ISD::UNDEF
8581 ? N2
8582 : getConstant(
8584 VT);
8585 }
8586 }
8587
8588 // Perform trivial constant folding.
8589 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8590 return SV;
8591
8592 // Memoize this node if possible.
8593 SDNode *N;
8594 SDVTList VTs = getVTList(VT);
8595 SDValue Ops[] = {N1, N2};
8596 if (VT != MVT::Glue) {
8598 AddNodeIDNode(ID, Opcode, VTs, Ops);
8599 void *IP = nullptr;
8600 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8601 E->intersectFlagsWith(Flags);
8602 return SDValue(E, 0);
8603 }
8604
8605 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8606 N->setFlags(Flags);
8607 createOperands(N, Ops);
8608 CSEMap.InsertNode(N, IP);
8609 } else {
8610 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8611 createOperands(N, Ops);
8612 }
8613
8614 InsertNode(N);
8615 SDValue V = SDValue(N, 0);
8616 NewSDValueDbgMsg(V, "Creating new node: ", this);
8617 return V;
8618}
8619
8620SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8621 SDValue N1, SDValue N2, SDValue N3) {
8622 SDNodeFlags Flags;
8623 if (Inserter)
8624 Flags = Inserter->getFlags();
8625 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8626}
8627
8628SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8629 SDValue N1, SDValue N2, SDValue N3,
8630 const SDNodeFlags Flags) {
8632 N2.getOpcode() != ISD::DELETED_NODE &&
8633 N3.getOpcode() != ISD::DELETED_NODE &&
8634 "Operand is DELETED_NODE!");
8635 // Perform various simplifications.
8636 switch (Opcode) {
8637 case ISD::BUILD_VECTOR: {
8638 // Attempt to simplify BUILD_VECTOR.
8639 SDValue Ops[] = {N1, N2, N3};
8640 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8641 return V;
8642 break;
8643 }
8644 case ISD::CONCAT_VECTORS: {
8645 SDValue Ops[] = {N1, N2, N3};
8646 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8647 return V;
8648 break;
8649 }
8650 case ISD::SETCC: {
8651 assert(VT.isInteger() && "SETCC result type must be an integer!");
8652 assert(N1.getValueType() == N2.getValueType() &&
8653 "SETCC operands must have the same type!");
8654 assert(VT.isVector() == N1.getValueType().isVector() &&
8655 "SETCC type should be vector iff the operand type is vector!");
8656 assert((!VT.isVector() || VT.getVectorElementCount() ==
8658 "SETCC vector element counts must match!");
8659 // Use FoldSetCC to simplify SETCC's.
8660 if (SDValue V =
8661 FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL, Flags))
8662 return V;
8663 break;
8664 }
8665 case ISD::SELECT:
8666 case ISD::VSELECT:
8667 if (SDValue V = simplifySelect(N1, N2, N3))
8668 return V;
8669 break;
8671 llvm_unreachable("should use getVectorShuffle constructor!");
8673 if (isNullConstant(N3))
8674 return N1;
8675 break;
8677 if (isNullConstant(N3))
8678 return N2;
8679 break;
8681 assert(VT.isVector() && VT == N1.getValueType() &&
8682 "INSERT_VECTOR_ELT vector type mismatch");
8684 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8685 assert((!VT.isFloatingPoint() ||
8686 VT.getVectorElementType() == N2.getValueType()) &&
8687 "INSERT_VECTOR_ELT fp scalar type mismatch");
8688 assert((!VT.isInteger() ||
8690 "INSERT_VECTOR_ELT int scalar size mismatch");
8691
8692 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8693 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8694 // for scalable vectors where we will generate appropriate code to
8695 // deal with out-of-bounds cases correctly.
8696 if (N3C && VT.isFixedLengthVector() &&
8697 N3C->getZExtValue() >= VT.getVectorNumElements())
8698 return getUNDEF(VT);
8699
8700 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8701 if (N3.isUndef())
8702 return getUNDEF(VT);
8703
8704 // If inserting poison, just use the input vector.
8705 if (N2.getOpcode() == ISD::POISON)
8706 return N1;
8707
8708 // Inserting undef into undef/poison is still undef.
8709 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8710 return getUNDEF(VT);
8711
8712 // If the inserted element is an UNDEF, just use the input vector.
8713 // But not if skipping the insert could make the result more poisonous.
8714 if (N2.isUndef()) {
8715 if (N3C && VT.isFixedLengthVector()) {
8716 APInt EltMask =
8717 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8718 if (isGuaranteedNotToBePoison(N1, EltMask))
8719 return N1;
8720 } else if (isGuaranteedNotToBePoison(N1))
8721 return N1;
8722 }
8723 break;
8724 }
8725 case ISD::INSERT_SUBVECTOR: {
8726 // If inserting poison, just use the input vector,
8727 if (N2.getOpcode() == ISD::POISON)
8728 return N1;
8729
8730 // Inserting undef into undef/poison is still undef.
8731 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8732 return getUNDEF(VT);
8733
8734 EVT N2VT = N2.getValueType();
8735 assert(VT == N1.getValueType() &&
8736 "Dest and insert subvector source types must match!");
8737 assert(VT.isVector() && N2VT.isVector() &&
8738 "Insert subvector VTs must be vectors!");
8740 "Insert subvector VTs must have the same element type!");
8741 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8742 "Cannot insert a scalable vector into a fixed length vector!");
8743 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8745 "Insert subvector must be from smaller vector to larger vector!");
8747 "Insert subvector index must be constant");
8748 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8749 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8751 "Insert subvector overflow!");
8753 TLI->getVectorIdxWidth(getDataLayout()) &&
8754 "Constant index for INSERT_SUBVECTOR has an invalid size");
8755
8756 // Trivial insertion.
8757 if (VT == N2VT)
8758 return N2;
8759
8760 // If this is an insert of an extracted vector into an undef/poison vector,
8761 // we can just use the input to the extract. But not if skipping the
8762 // extract+insert could make the result more poisonous.
8763 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8764 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8765 if (N1.getOpcode() == ISD::POISON)
8766 return N2.getOperand(0);
8767 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8768 unsigned LoBit = N3->getAsZExtVal();
8769 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8770 APInt EltMask =
8771 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8772 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8773 return N2.getOperand(0);
8774 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8775 return N2.getOperand(0);
8776 }
8777
8778 // If the inserted subvector is UNDEF, just use the input vector.
8779 // But not if skipping the insert could make the result more poisonous.
8780 if (N2.isUndef()) {
8781 if (VT.isFixedLengthVector()) {
8782 unsigned LoBit = N3->getAsZExtVal();
8783 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8784 APInt EltMask =
8785 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8786 if (isGuaranteedNotToBePoison(N1, EltMask))
8787 return N1;
8788 } else if (isGuaranteedNotToBePoison(N1))
8789 return N1;
8790 }
8791 break;
8792 }
8793 case ISD::BITCAST:
8794 // Fold bit_convert nodes from a type to themselves.
8795 if (N1.getValueType() == VT)
8796 return N1;
8797 break;
8798 case ISD::VP_TRUNCATE:
8799 case ISD::VP_SIGN_EXTEND:
8800 case ISD::VP_ZERO_EXTEND:
8801 // Don't create noop casts.
8802 if (N1.getValueType() == VT)
8803 return N1;
8804 break;
8805 case ISD::VECTOR_COMPRESS: {
8806 [[maybe_unused]] EVT VecVT = N1.getValueType();
8807 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8808 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8809 assert(VT == VecVT && "Vector and result type don't match.");
8810 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8811 "All inputs must be vectors.");
8812 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8814 "Vector and mask must have same number of elements.");
8815
8816 if (N1.isUndef() || N2.isUndef())
8817 return N3;
8818
8819 break;
8820 }
8825 [[maybe_unused]] EVT AccVT = N1.getValueType();
8826 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8827 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8828 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8829 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8830 "node to have the same type!");
8831 assert(VT.isVector() && VT == AccVT &&
8832 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8833 "the same type as its result!");
8835 AccVT.getVectorElementCount()) &&
8836 "Expected the element count of the second and third operands of the "
8837 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8838 "element count of the first operand and the result!");
8840 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8841 "node to have an element type which is the same as or smaller than "
8842 "the element type of the first operand and result!");
8843 break;
8844 }
8845 }
8846
8847 // Perform trivial constant folding for arithmetic operators.
8848 switch (Opcode) {
8849 case ISD::FMA:
8850 case ISD::FMAD:
8851 case ISD::SETCC:
8852 case ISD::FSHL:
8853 case ISD::FSHR:
8854 if (SDValue SV =
8855 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8856 return SV;
8857 break;
8858 }
8859
8860 // Memoize node if it doesn't produce a glue result.
8861 SDNode *N;
8862 SDVTList VTs = getVTList(VT);
8863 SDValue Ops[] = {N1, N2, N3};
8864 if (VT != MVT::Glue) {
8866 AddNodeIDNode(ID, Opcode, VTs, Ops);
8867 void *IP = nullptr;
8868 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8869 E->intersectFlagsWith(Flags);
8870 return SDValue(E, 0);
8871 }
8872
8873 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8874 N->setFlags(Flags);
8875 createOperands(N, Ops);
8876 CSEMap.InsertNode(N, IP);
8877 } else {
8878 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8879 createOperands(N, Ops);
8880 }
8881
8882 InsertNode(N);
8883 SDValue V = SDValue(N, 0);
8884 NewSDValueDbgMsg(V, "Creating new node: ", this);
8885 return V;
8886}
8887
8888SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8889 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8890 const SDNodeFlags Flags) {
8891 SDValue Ops[] = { N1, N2, N3, N4 };
8892 return getNode(Opcode, DL, VT, Ops, Flags);
8893}
8894
8895SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8896 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8897 SDNodeFlags Flags;
8898 if (Inserter)
8899 Flags = Inserter->getFlags();
8900 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8901}
8902
8903SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8904 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8905 SDValue N5, const SDNodeFlags Flags) {
8906 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8907 return getNode(Opcode, DL, VT, Ops, Flags);
8908}
8909
8910SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8911 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8912 SDValue N5) {
8913 SDNodeFlags Flags;
8914 if (Inserter)
8915 Flags = Inserter->getFlags();
8916 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8917}
8918
8919/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8920/// the incoming stack arguments to be loaded from the stack.
8922 SmallVector<SDValue, 8> ArgChains;
8923
8924 // Include the original chain at the beginning of the list. When this is
8925 // used by target LowerCall hooks, this helps legalize find the
8926 // CALLSEQ_BEGIN node.
8927 ArgChains.push_back(Chain);
8928
8929 // Add a chain value for each stack argument.
8930 for (SDNode *U : getEntryNode().getNode()->users())
8931 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8932 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8933 if (FI->getIndex() < 0)
8934 ArgChains.push_back(SDValue(L, 1));
8935
8936 // Build a tokenfactor for all the chains.
8937 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8938}
8939
8940/// getMemsetValue - Vectorized representation of the memset value
8941/// operand.
8943 const SDLoc &dl) {
8944 assert(!Value.isUndef());
8945
8946 unsigned NumBits = VT.getScalarSizeInBits();
8948 assert(C->getAPIntValue().getBitWidth() == 8);
8949 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8950 if (VT.isInteger()) {
8951 bool IsOpaque = VT.getSizeInBits() > 64 ||
8952 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8953 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8954 }
8955 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8956 }
8957
8958 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8959 EVT IntVT = VT.getScalarType();
8960 if (!IntVT.isInteger())
8961 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8962
8963 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8964 if (NumBits > 8) {
8965 // Use a multiplication with 0x010101... to extend the input to the
8966 // required length.
8967 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8968 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8969 DAG.getConstant(Magic, dl, IntVT));
8970 }
8971
8972 if (VT != Value.getValueType() && !VT.isInteger())
8973 Value = DAG.getBitcast(VT.getScalarType(), Value);
8974 if (VT != Value.getValueType())
8975 Value = DAG.getSplatBuildVector(VT, dl, Value);
8976
8977 return Value;
8978}
8979
8980/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8981/// used when a memcpy is turned into a memset when the source is a constant
8982/// string ptr.
8984 const TargetLowering &TLI,
8985 const ConstantDataArraySlice &Slice) {
8986 // Handle vector with all elements zero.
8987 if (Slice.Array == nullptr) {
8988 if (VT.isInteger())
8989 return DAG.getConstant(0, dl, VT);
8990 return DAG.getNode(ISD::BITCAST, dl, VT,
8991 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8992 }
8993
8994 assert(!VT.isVector() && "Can't handle vector type here!");
8995 unsigned NumVTBits = VT.getSizeInBits();
8996 unsigned NumVTBytes = NumVTBits / 8;
8997 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8998
8999 APInt Val(NumVTBits, 0);
9000 if (DAG.getDataLayout().isLittleEndian()) {
9001 for (unsigned i = 0; i != NumBytes; ++i)
9002 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
9003 } else {
9004 for (unsigned i = 0; i != NumBytes; ++i)
9005 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
9006 }
9007
9008 // If the "cost" of materializing the integer immediate is less than the cost
9009 // of a load, then it is cost effective to turn the load into the immediate.
9010 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
9011 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
9012 return DAG.getConstant(Val, dl, VT);
9013 return SDValue();
9014}
9015
9017 const SDLoc &DL,
9018 const SDNodeFlags Flags) {
9019 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
9020 return getMemBasePlusOffset(Base, Index, DL, Flags);
9021}
9022
9024 const SDLoc &DL,
9025 const SDNodeFlags Flags) {
9026 assert(Offset.getValueType().isInteger());
9027 EVT BasePtrVT = Ptr.getValueType();
9028 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
9029 BasePtrVT))
9030 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
9031 // InBounds only applies to PTRADD, don't set it if we generate ADD.
9032 SDNodeFlags AddFlags = Flags;
9033 AddFlags.setInBounds(false);
9034 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
9035}
9036
9037/// Returns true if memcpy source is constant data.
9039 uint64_t SrcDelta = 0;
9040 GlobalAddressSDNode *G = nullptr;
9041 if (Src.getOpcode() == ISD::GlobalAddress)
9043 else if (Src->isAnyAdd() &&
9044 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
9045 Src.getOperand(1).getOpcode() == ISD::Constant) {
9046 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
9047 SrcDelta = Src.getConstantOperandVal(1);
9048 }
9049 if (!G)
9050 return false;
9051
9052 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
9053 SrcDelta + G->getOffset());
9054}
9055
9057 SelectionDAG &DAG) {
9058 // On Darwin, -Os means optimize for size without hurting performance, so
9059 // only really optimize for size when -Oz (MinSize) is used.
9061 return MF.getFunction().hasMinSize();
9062 return DAG.shouldOptForSize();
9063}
9064
9066 SmallVector<SDValue, 32> &OutChains, unsigned From,
9067 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
9068 SmallVector<SDValue, 16> &OutStoreChains) {
9069 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
9070 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
9071 SmallVector<SDValue, 16> GluedLoadChains;
9072 for (unsigned i = From; i < To; ++i) {
9073 OutChains.push_back(OutLoadChains[i]);
9074 GluedLoadChains.push_back(OutLoadChains[i]);
9075 }
9076
9077 // Chain for all loads.
9078 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
9079 GluedLoadChains);
9080
9081 for (unsigned i = From; i < To; ++i) {
9082 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
9083 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
9084 ST->getBasePtr(), ST->getMemoryVT(),
9085 ST->getMemOperand());
9086 OutChains.push_back(NewStore);
9087 }
9088}
9089
9091 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
9092 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
9093 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
9094 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
9095 // Turn a memcpy of undef to nop.
9096 // FIXME: We need to honor volatile even is Src is undef.
9097 if (Src.isUndef())
9098 return Chain;
9099
9100 // Expand memcpy to a series of load and store ops if the size operand falls
9101 // below a certain threshold.
9102 // TODO: In the AlwaysInline case, if the size is big then generate a loop
9103 // rather than maybe a humongous number of loads and stores.
9104 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9105 const DataLayout &DL = DAG.getDataLayout();
9106 LLVMContext &C = *DAG.getContext();
9107 std::vector<EVT> MemOps;
9108 bool DstAlignCanChange = false;
9110 MachineFrameInfo &MFI = MF.getFrameInfo();
9111 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9113 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9114 DstAlignCanChange = true;
9115 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
9116 if (!SrcAlign || Alignment > *SrcAlign)
9117 SrcAlign = Alignment;
9118 assert(SrcAlign && "SrcAlign must be set");
9120 // If marked as volatile, perform a copy even when marked as constant.
9121 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
9122 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
9123 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
9124 const MemOp Op = isZeroConstant
9125 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
9126 /*IsZeroMemset*/ true, isVol)
9127 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
9128 *SrcAlign, isVol, CopyFromConstant);
9129 if (!TLI.findOptimalMemOpLowering(
9130 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
9131 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes(), nullptr))
9132 return SDValue();
9133
9134 if (DstAlignCanChange) {
9135 Type *Ty = MemOps[0].getTypeForEVT(C);
9136 Align NewAlign = DL.getABITypeAlign(Ty);
9137
9138 // Don't promote to an alignment that would require dynamic stack
9139 // realignment which may conflict with optimizations such as tail call
9140 // optimization.
9142 if (!TRI->hasStackRealignment(MF))
9143 if (MaybeAlign StackAlign = DL.getStackAlignment())
9144 NewAlign = std::min(NewAlign, *StackAlign);
9145
9146 if (NewAlign > Alignment) {
9147 // Give the stack frame object a larger alignment if needed.
9148 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9149 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9150 Alignment = NewAlign;
9151 }
9152 }
9153
9154 // Prepare AAInfo for loads/stores after lowering this memcpy.
9155 AAMDNodes NewAAInfo = AAInfo;
9156 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9157
9158 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
9159 bool isConstant =
9160 BatchAA && SrcVal &&
9161 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
9162
9163 MachineMemOperand::Flags MMOFlags =
9165 SmallVector<SDValue, 16> OutLoadChains;
9166 SmallVector<SDValue, 16> OutStoreChains;
9167 SmallVector<SDValue, 32> OutChains;
9168 unsigned NumMemOps = MemOps.size();
9169 uint64_t SrcOff = 0, DstOff = 0;
9170 for (unsigned i = 0; i != NumMemOps; ++i) {
9171 EVT VT = MemOps[i];
9172 unsigned VTSize = VT.getSizeInBits() / 8;
9173 SDValue Value, Store;
9174
9175 if (VTSize > Size) {
9176 // Issuing an unaligned load / store pair that overlaps with the previous
9177 // pair. Adjust the offset accordingly.
9178 assert(i == NumMemOps-1 && i != 0);
9179 SrcOff -= VTSize - Size;
9180 DstOff -= VTSize - Size;
9181 }
9182
9183 if (CopyFromConstant &&
9184 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
9185 // It's unlikely a store of a vector immediate can be done in a single
9186 // instruction. It would require a load from a constantpool first.
9187 // We only handle zero vectors here.
9188 // FIXME: Handle other cases where store of vector immediate is done in
9189 // a single instruction.
9190 ConstantDataArraySlice SubSlice;
9191 if (SrcOff < Slice.Length) {
9192 SubSlice = Slice;
9193 SubSlice.move(SrcOff);
9194 } else {
9195 // This is an out-of-bounds access and hence UB. Pretend we read zero.
9196 SubSlice.Array = nullptr;
9197 SubSlice.Offset = 0;
9198 SubSlice.Length = VTSize;
9199 }
9200 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
9201 if (Value.getNode()) {
9202 Store = DAG.getStore(
9203 Chain, dl, Value,
9204 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9205 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
9206 OutChains.push_back(Store);
9207 }
9208 }
9209
9210 if (!Store.getNode()) {
9211 // The type might not be legal for the target. This should only happen
9212 // if the type is smaller than a legal type, as on PPC, so the right
9213 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
9214 // to Load/Store if NVT==VT.
9215 // FIXME does the case above also need this?
9216 EVT NVT = TLI.getTypeToTransformTo(C, VT);
9217 assert(NVT.bitsGE(VT));
9218
9219 bool isDereferenceable =
9220 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9221 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9222 if (isDereferenceable)
9224 if (isConstant)
9225 SrcMMOFlags |= MachineMemOperand::MOInvariant;
9226
9227 Value = DAG.getExtLoad(
9228 ISD::EXTLOAD, dl, NVT, Chain,
9229 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9230 SrcPtrInfo.getWithOffset(SrcOff), VT,
9231 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
9232 OutLoadChains.push_back(Value.getValue(1));
9233
9234 Store = DAG.getTruncStore(
9235 Chain, dl, Value,
9236 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9237 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
9238 OutStoreChains.push_back(Store);
9239 }
9240 SrcOff += VTSize;
9241 DstOff += VTSize;
9242 Size -= VTSize;
9243 }
9244
9245 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
9247 unsigned NumLdStInMemcpy = OutStoreChains.size();
9248
9249 if (NumLdStInMemcpy) {
9250 // It may be that memcpy might be converted to memset if it's memcpy
9251 // of constants. In such a case, we won't have loads and stores, but
9252 // just stores. In the absence of loads, there is nothing to gang up.
9253 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
9254 // If target does not care, just leave as it.
9255 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
9256 OutChains.push_back(OutLoadChains[i]);
9257 OutChains.push_back(OutStoreChains[i]);
9258 }
9259 } else {
9260 // Ld/St less than/equal limit set by target.
9261 if (NumLdStInMemcpy <= GluedLdStLimit) {
9262 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
9263 NumLdStInMemcpy, OutLoadChains,
9264 OutStoreChains);
9265 } else {
9266 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
9267 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
9268 unsigned GlueIter = 0;
9269
9270 // Residual ld/st.
9271 if (RemainingLdStInMemcpy) {
9273 DAG, dl, OutChains, NumLdStInMemcpy - RemainingLdStInMemcpy,
9274 NumLdStInMemcpy, OutLoadChains, OutStoreChains);
9275 }
9276
9277 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
9278 unsigned IndexFrom = NumLdStInMemcpy - RemainingLdStInMemcpy -
9279 GlueIter - GluedLdStLimit;
9280 unsigned IndexTo = NumLdStInMemcpy - RemainingLdStInMemcpy - GlueIter;
9281 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
9282 OutLoadChains, OutStoreChains);
9283 GlueIter += GluedLdStLimit;
9284 }
9285 }
9286 }
9287 }
9288 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9289}
9290
9292 SDValue Chain, SDValue Dst, SDValue Src,
9293 uint64_t Size, Align Alignment,
9294 bool isVol, bool AlwaysInline,
9295 MachinePointerInfo DstPtrInfo,
9296 MachinePointerInfo SrcPtrInfo,
9297 const AAMDNodes &AAInfo) {
9298 // Turn a memmove of undef to nop.
9299 // FIXME: We need to honor volatile even is Src is undef.
9300 if (Src.isUndef())
9301 return Chain;
9302
9303 // Expand memmove to a series of load and store ops if the size operand falls
9304 // below a certain threshold.
9305 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9306 const DataLayout &DL = DAG.getDataLayout();
9307 LLVMContext &C = *DAG.getContext();
9308 std::vector<EVT> MemOps;
9309 bool DstAlignCanChange = false;
9311 MachineFrameInfo &MFI = MF.getFrameInfo();
9312 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9314 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9315 DstAlignCanChange = true;
9316 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
9317 if (!SrcAlign || Alignment > *SrcAlign)
9318 SrcAlign = Alignment;
9319 assert(SrcAlign && "SrcAlign must be set");
9320 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
9321 if (!TLI.findOptimalMemOpLowering(
9322 C, MemOps, Limit,
9323 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign, isVol),
9324 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
9325 MF.getFunction().getAttributes(), nullptr))
9326 return SDValue();
9327
9328 if (DstAlignCanChange) {
9329 Type *Ty = MemOps[0].getTypeForEVT(C);
9330 Align NewAlign = DL.getABITypeAlign(Ty);
9331
9332 // Don't promote to an alignment that would require dynamic stack
9333 // realignment which may conflict with optimizations such as tail call
9334 // optimization.
9336 if (!TRI->hasStackRealignment(MF))
9337 if (MaybeAlign StackAlign = DL.getStackAlignment())
9338 NewAlign = std::min(NewAlign, *StackAlign);
9339
9340 if (NewAlign > Alignment) {
9341 // Give the stack frame object a larger alignment if needed.
9342 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9343 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9344 Alignment = NewAlign;
9345 }
9346 }
9347
9348 // Prepare AAInfo for loads/stores after lowering this memmove.
9349 AAMDNodes NewAAInfo = AAInfo;
9350 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9351
9352 MachineMemOperand::Flags MMOFlags =
9354 uint64_t SrcOff = 0;
9355 SmallVector<SDValue, 8> LoadValues;
9356 SmallVector<SDValue, 8> LoadChains;
9357 SmallVector<SDValue, 8> OutChains;
9358 unsigned NumMemOps = MemOps.size();
9359 for (unsigned i = 0; i < NumMemOps; i++) {
9360 EVT VT = MemOps[i];
9361 unsigned VTSize = VT.getSizeInBits() / 8;
9362 SDValue Value;
9363 bool IsOverlapping = false;
9364
9365 if (i == NumMemOps - 1 && i != 0 && VTSize > Size - SrcOff) {
9366 // Issuing an unaligned load / store pair that overlaps with the previous
9367 // pair. Adjust the offset accordingly.
9368 SrcOff = Size - VTSize;
9369 IsOverlapping = true;
9370 }
9371
9372 // Calculate the actual alignment at the current offset. The alignment at
9373 // SrcOff may be lower than the base alignment, especially when using
9374 // overlapping loads.
9375 Align SrcAlignAtOffset = commonAlignment(*SrcAlign, SrcOff);
9376 if (IsOverlapping) {
9377 // Verify that the target allows misaligned memory accesses at the
9378 // adjusted offset when using overlapping loads.
9379 unsigned Fast;
9380 if (!TLI.allowsMisalignedMemoryAccesses(VT, SrcPtrInfo.getAddrSpace(),
9381 SrcAlignAtOffset, MMOFlags,
9382 &Fast) ||
9383 !Fast) {
9384 // This should have been caught by findOptimalMemOpLowering, but verify
9385 // here for safety.
9386 return SDValue();
9387 }
9388 }
9389
9390 bool isDereferenceable =
9391 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9392 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9393 if (isDereferenceable)
9395 Value =
9396 DAG.getLoad(VT, dl, Chain,
9397 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9398 SrcPtrInfo.getWithOffset(SrcOff), SrcAlignAtOffset,
9399 SrcMMOFlags, NewAAInfo);
9400 LoadValues.push_back(Value);
9401 LoadChains.push_back(Value.getValue(1));
9402 SrcOff += VTSize;
9403 }
9404 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
9405 OutChains.clear();
9406 uint64_t DstOff = 0;
9407 for (unsigned i = 0; i < NumMemOps; i++) {
9408 EVT VT = MemOps[i];
9409 unsigned VTSize = VT.getSizeInBits() / 8;
9410 SDValue Store;
9411 bool IsOverlapping = false;
9412
9413 if (i == NumMemOps - 1 && i != 0 && VTSize > Size - DstOff) {
9414 // Issuing an unaligned load / store pair that overlaps with the previous
9415 // pair. Adjust the offset accordingly.
9416 DstOff = Size - VTSize;
9417 IsOverlapping = true;
9418 }
9419
9420 // Calculate the actual alignment at the current offset. The alignment at
9421 // DstOff may be lower than the base alignment, especially when using
9422 // overlapping stores.
9423 Align DstAlignAtOffset = commonAlignment(Alignment, DstOff);
9424 if (IsOverlapping) {
9425 // Verify that the target allows misaligned memory accesses at the
9426 // adjusted offset when using overlapping stores.
9427 unsigned Fast;
9428 if (!TLI.allowsMisalignedMemoryAccesses(VT, DstPtrInfo.getAddrSpace(),
9429 DstAlignAtOffset, MMOFlags,
9430 &Fast) ||
9431 !Fast) {
9432 // This should have been caught by findOptimalMemOpLowering, but verify
9433 // here for safety.
9434 return SDValue();
9435 }
9436 }
9437 Store = DAG.getStore(
9438 Chain, dl, LoadValues[i],
9439 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9440 DstPtrInfo.getWithOffset(DstOff), DstAlignAtOffset, MMOFlags,
9441 NewAAInfo);
9442 OutChains.push_back(Store);
9443 DstOff += VTSize;
9444 }
9445
9446 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9447}
9448
9449/// Lower the call to 'memset' intrinsic function into a series of store
9450/// operations.
9451///
9452/// \param DAG Selection DAG where lowered code is placed.
9453/// \param dl Link to corresponding IR location.
9454/// \param Chain Control flow dependency.
9455/// \param Dst Pointer to destination memory location.
9456/// \param Src Value of byte to write into the memory.
9457/// \param Size Number of bytes to write.
9458/// \param Alignment Alignment of the destination in bytes.
9459/// \param isVol True if destination is volatile.
9460/// \param AlwaysInline Makes sure no function call is generated.
9461/// \param DstPtrInfo IR information on the memory pointer.
9462/// \returns New head in the control flow, if lowering was successful, empty
9463/// SDValue otherwise.
9464///
9465/// The function tries to replace 'llvm.memset' intrinsic with several store
9466/// operations and value calculation code. This is usually profitable for small
9467/// memory size or when the semantic requires inlining.
9469 SDValue Chain, SDValue Dst, SDValue Src,
9470 uint64_t Size, Align Alignment, bool isVol,
9471 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9472 const AAMDNodes &AAInfo) {
9473 // Turn a memset of undef to nop.
9474 // FIXME: We need to honor volatile even is Src is undef.
9475 if (Src.isUndef())
9476 return Chain;
9477
9478 // Expand memset to a series of load/store ops if the size operand
9479 // falls below a certain threshold.
9480 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9481 std::vector<EVT> MemOps;
9482 bool DstAlignCanChange = false;
9483 LLVMContext &C = *DAG.getContext();
9485 MachineFrameInfo &MFI = MF.getFrameInfo();
9486 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9488 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9489 DstAlignCanChange = true;
9490 bool IsZeroVal = isNullConstant(Src);
9491 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9492
9493 EVT LargestVT;
9494 if (!TLI.findOptimalMemOpLowering(
9495 C, MemOps, Limit,
9496 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9497 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes(),
9498 &LargestVT))
9499 return SDValue();
9500
9501 if (DstAlignCanChange) {
9502 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9503 const DataLayout &DL = DAG.getDataLayout();
9504 Align NewAlign = DL.getABITypeAlign(Ty);
9505
9506 // Don't promote to an alignment that would require dynamic stack
9507 // realignment which may conflict with optimizations such as tail call
9508 // optimization.
9510 if (!TRI->hasStackRealignment(MF))
9511 if (MaybeAlign StackAlign = DL.getStackAlignment())
9512 NewAlign = std::min(NewAlign, *StackAlign);
9513
9514 if (NewAlign > Alignment) {
9515 // Give the stack frame object a larger alignment if needed.
9516 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9517 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9518 Alignment = NewAlign;
9519 }
9520 }
9521
9522 SmallVector<SDValue, 8> OutChains;
9523 uint64_t DstOff = 0;
9524 unsigned NumMemOps = MemOps.size();
9525
9526 // Find the largest store and generate the bit pattern for it.
9527 // If target didn't set LargestVT, compute it from MemOps.
9528 if (!LargestVT.isSimple()) {
9529 LargestVT = MemOps[0];
9530 for (unsigned i = 1; i < NumMemOps; i++)
9531 if (MemOps[i].bitsGT(LargestVT))
9532 LargestVT = MemOps[i];
9533 }
9534 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9535
9536 // Prepare AAInfo for loads/stores after lowering this memset.
9537 AAMDNodes NewAAInfo = AAInfo;
9538 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9539
9540 for (unsigned i = 0; i < NumMemOps; i++) {
9541 EVT VT = MemOps[i];
9542 unsigned VTSize = VT.getSizeInBits() / 8;
9543 // The target should specify store types that exactly cover the memset size
9544 // (with the last store potentially being oversized for overlapping stores).
9545 assert(Size > 0 && "Target specified more stores than needed in "
9546 "findOptimalMemOpLowering");
9547 if (VTSize > Size) {
9548 // Issuing an unaligned load / store pair that overlaps with the previous
9549 // pair. Adjust the offset accordingly.
9550 assert(i == NumMemOps-1 && i != 0);
9551 DstOff -= VTSize - Size;
9552 }
9553
9554 // If this store is smaller than the largest store see whether we can get
9555 // the smaller value for free with a truncate or extract vector element and
9556 // then store.
9557 SDValue Value = MemSetValue;
9558 if (VT.bitsLT(LargestVT)) {
9559 unsigned Index;
9560 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9561 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9562 if (!LargestVT.isVector() && !VT.isVector() &&
9563 TLI.isTruncateFree(LargestVT, VT))
9564 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9565 else if (LargestVT.isVector() && !VT.isVector() &&
9567 LargestVT.getTypeForEVT(*DAG.getContext()),
9568 VT.getSizeInBits(), Index) &&
9569 TLI.isTypeLegal(SVT) &&
9570 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9571 // Target which can combine store(extractelement VectorTy, Idx) can get
9572 // the smaller value for free.
9573 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9574 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9575 } else
9576 Value = getMemsetValue(Src, VT, DAG, dl);
9577 }
9578 assert(Value.getValueType() == VT && "Value with wrong type.");
9579 SDValue Store = DAG.getStore(
9580 Chain, dl, Value,
9581 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9582 DstPtrInfo.getWithOffset(DstOff), Alignment,
9584 NewAAInfo);
9585 OutChains.push_back(Store);
9586 DstOff += VT.getSizeInBits() / 8;
9587 // For oversized overlapping stores, only subtract the remaining bytes.
9588 // For normal stores, subtract the full store size.
9589 if (VTSize > Size) {
9590 Size = 0;
9591 } else {
9592 Size -= VTSize;
9593 }
9594 }
9595
9596 // After processing all stores, Size should be exactly 0. Any remaining bytes
9597 // indicate a bug in the target's findOptimalMemOpLowering implementation.
9598 assert(Size == 0 && "Target's findOptimalMemOpLowering did not specify "
9599 "stores that exactly cover the memset size");
9600
9601 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9602}
9603
9605 unsigned AS) {
9606 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9607 // pointer operands can be losslessly bitcasted to pointers of address space 0
9608 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9609 report_fatal_error("cannot lower memory intrinsic in address space " +
9610 Twine(AS));
9611 }
9612}
9613
9615 const SelectionDAG *SelDAG,
9616 bool AllowReturnsFirstArg) {
9617 if (!CI || !CI->isTailCall())
9618 return false;
9619 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9620 // helper symbol we lower to.
9621 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9622 AllowReturnsFirstArg &&
9624}
9625
9626static std::pair<SDValue, SDValue>
9629 const CallInst *CI, RTLIB::Libcall Call,
9630 SelectionDAG *DAG, const TargetLowering *TLI) {
9631 RTLIB::LibcallImpl LCImpl = DAG->getLibcalls().getLibcallImpl(Call);
9632
9633 if (LCImpl == RTLIB::Unsupported)
9634 return {};
9635
9637 bool IsTailCall =
9638 isInTailCallPositionWrapper(CI, DAG, /*AllowReturnsFirstArg=*/true);
9639 SDValue Callee =
9640 DAG->getExternalSymbol(LCImpl, TLI->getPointerTy(DAG->getDataLayout()));
9641
9642 CLI.setDebugLoc(dl)
9643 .setChain(Chain)
9645 CI->getType(), Callee, std::move(Args))
9646 .setTailCall(IsTailCall);
9647
9648 return TLI->LowerCallTo(CLI);
9649}
9650
9651std::pair<SDValue, SDValue> SelectionDAG::getStrcmp(SDValue Chain,
9652 const SDLoc &dl, SDValue S1,
9653 SDValue S2,
9654 const CallInst *CI) {
9656 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9657 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9658 RTLIB::STRCMP, this, TLI);
9659}
9660
9661std::pair<SDValue, SDValue> SelectionDAG::getStrstr(SDValue Chain,
9662 const SDLoc &dl, SDValue S1,
9663 SDValue S2,
9664 const CallInst *CI) {
9666 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9667 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9668 RTLIB::STRSTR, this, TLI);
9669}
9670
9671std::pair<SDValue, SDValue> SelectionDAG::getMemccpy(SDValue Chain,
9672 const SDLoc &dl,
9673 SDValue Dst, SDValue Src,
9675 const CallInst *CI) {
9677
9679 {Dst, PT},
9680 {Src, PT},
9683 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9684 RTLIB::MEMCCPY, this, TLI);
9685}
9686
9687std::pair<SDValue, SDValue>
9689 SDValue Mem1, SDValue Size, const CallInst *CI) {
9692 {Mem0, PT},
9693 {Mem1, PT},
9695 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9696 RTLIB::MEMCMP, this, TLI);
9697}
9698
9699std::pair<SDValue, SDValue> SelectionDAG::getStrcpy(SDValue Chain,
9700 const SDLoc &dl,
9701 SDValue Dst, SDValue Src,
9702 const CallInst *CI) {
9704 TargetLowering::ArgListTy Args = {{Dst, PT}, {Src, PT}};
9705 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9706 RTLIB::STRCPY, this, TLI);
9707}
9708
9709std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9710 const SDLoc &dl,
9711 SDValue Src,
9712 const CallInst *CI) {
9713 // Emit a library call.
9716 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9717 RTLIB::STRLEN, this, TLI);
9718}
9719
9721 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9722 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9723 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9724 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9725 BatchAAResults *BatchAA) {
9726 // Check to see if we should lower the memcpy to loads and stores first.
9727 // For cases within the target-specified limits, this is the best choice.
9729 if (ConstantSize) {
9730 // Memcpy with size zero? Just return the original chain.
9731 if (ConstantSize->isZero())
9732 return Chain;
9733
9735 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9736 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9737 if (Result.getNode())
9738 return Result;
9739 }
9740
9741 // Then check to see if we should lower the memcpy with target-specific
9742 // code. If the target chooses to do this, this is the next best.
9743 if (TSI) {
9744 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9745 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9746 DstPtrInfo, SrcPtrInfo);
9747 if (Result.getNode())
9748 return Result;
9749 }
9750
9751 // If we really need inline code and the target declined to provide it,
9752 // use a (potentially long) sequence of loads and stores.
9753 if (AlwaysInline) {
9754 assert(ConstantSize && "AlwaysInline requires a constant size!");
9756 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9757 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9758 }
9759
9762
9763 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9764 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9765 // respect volatile, so they may do things like read or write memory
9766 // beyond the given memory regions. But fixing this isn't easy, and most
9767 // people don't care.
9768
9769 // Emit a library call.
9772 Args.emplace_back(Dst, PtrTy);
9773 Args.emplace_back(Src, PtrTy);
9774 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9775 // FIXME: pass in SDLoc
9777 bool IsTailCall = false;
9778 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9779
9780 if (OverrideTailCall.has_value()) {
9781 IsTailCall = *OverrideTailCall;
9782 } else {
9783 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9784 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9785 }
9786
9787 CLI.setDebugLoc(dl)
9788 .setChain(Chain)
9789 .setLibCallee(
9790 Libcalls->getLibcallImplCallingConv(MemCpyImpl),
9791 Dst.getValueType().getTypeForEVT(*getContext()),
9792 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
9793 std::move(Args))
9795 .setTailCall(IsTailCall);
9796
9797 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9798 return CallResult.second;
9799}
9800
9802 SDValue Dst, SDValue Src, SDValue Size,
9803 Type *SizeTy, unsigned ElemSz,
9804 bool isTailCall,
9805 MachinePointerInfo DstPtrInfo,
9806 MachinePointerInfo SrcPtrInfo) {
9807 // Emit a library call.
9810 Args.emplace_back(Dst, ArgTy);
9811 Args.emplace_back(Src, ArgTy);
9812 Args.emplace_back(Size, SizeTy);
9813
9814 RTLIB::Libcall LibraryCall =
9816 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9817 if (LibcallImpl == RTLIB::Unsupported)
9818 report_fatal_error("Unsupported element size");
9819
9821 CLI.setDebugLoc(dl)
9822 .setChain(Chain)
9823 .setLibCallee(
9824 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9826 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9827 std::move(Args))
9829 .setTailCall(isTailCall);
9830
9831 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9832 return CallResult.second;
9833}
9834
9836 SDValue Src, SDValue Size, Align Alignment,
9837 bool isVol, const CallInst *CI,
9838 std::optional<bool> OverrideTailCall,
9839 MachinePointerInfo DstPtrInfo,
9840 MachinePointerInfo SrcPtrInfo,
9841 const AAMDNodes &AAInfo,
9842 BatchAAResults *BatchAA) {
9843 // Check to see if we should lower the memmove to loads and stores first.
9844 // For cases within the target-specified limits, this is the best choice.
9846 if (ConstantSize) {
9847 // Memmove with size zero? Just return the original chain.
9848 if (ConstantSize->isZero())
9849 return Chain;
9850
9852 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9853 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9854 if (Result.getNode())
9855 return Result;
9856 }
9857
9858 // Then check to see if we should lower the memmove with target-specific
9859 // code. If the target chooses to do this, this is the next best.
9860 if (TSI) {
9861 SDValue Result =
9862 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9863 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9864 if (Result.getNode())
9865 return Result;
9866 }
9867
9870
9871 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9872 // not be safe. See memcpy above for more details.
9873
9874 // Emit a library call.
9877 Args.emplace_back(Dst, PtrTy);
9878 Args.emplace_back(Src, PtrTy);
9879 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9880 // FIXME: pass in SDLoc
9882
9883 RTLIB::LibcallImpl MemmoveImpl = Libcalls->getLibcallImpl(RTLIB::MEMMOVE);
9884
9885 bool IsTailCall = false;
9886 if (OverrideTailCall.has_value()) {
9887 IsTailCall = *OverrideTailCall;
9888 } else {
9889 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9890 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9891 }
9892
9893 CLI.setDebugLoc(dl)
9894 .setChain(Chain)
9895 .setLibCallee(
9896 Libcalls->getLibcallImplCallingConv(MemmoveImpl),
9897 Dst.getValueType().getTypeForEVT(*getContext()),
9898 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
9899 std::move(Args))
9901 .setTailCall(IsTailCall);
9902
9903 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9904 return CallResult.second;
9905}
9906
9908 SDValue Dst, SDValue Src, SDValue Size,
9909 Type *SizeTy, unsigned ElemSz,
9910 bool isTailCall,
9911 MachinePointerInfo DstPtrInfo,
9912 MachinePointerInfo SrcPtrInfo) {
9913 // Emit a library call.
9915 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9916 Args.emplace_back(Dst, IntPtrTy);
9917 Args.emplace_back(Src, IntPtrTy);
9918 Args.emplace_back(Size, SizeTy);
9919
9920 RTLIB::Libcall LibraryCall =
9922 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9923 if (LibcallImpl == RTLIB::Unsupported)
9924 report_fatal_error("Unsupported element size");
9925
9927 CLI.setDebugLoc(dl)
9928 .setChain(Chain)
9929 .setLibCallee(
9930 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9932 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9933 std::move(Args))
9935 .setTailCall(isTailCall);
9936
9937 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9938 return CallResult.second;
9939}
9940
9942 SDValue Src, SDValue Size, Align Alignment,
9943 bool isVol, bool AlwaysInline,
9944 const CallInst *CI,
9945 MachinePointerInfo DstPtrInfo,
9946 const AAMDNodes &AAInfo) {
9947 // Check to see if we should lower the memset to stores first.
9948 // For cases within the target-specified limits, this is the best choice.
9950 if (ConstantSize) {
9951 // Memset with size zero? Just return the original chain.
9952 if (ConstantSize->isZero())
9953 return Chain;
9954
9955 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9956 ConstantSize->getZExtValue(), Alignment,
9957 isVol, false, DstPtrInfo, AAInfo);
9958
9959 if (Result.getNode())
9960 return Result;
9961 }
9962
9963 // Then check to see if we should lower the memset with target-specific
9964 // code. If the target chooses to do this, this is the next best.
9965 if (TSI) {
9966 SDValue Result = TSI->EmitTargetCodeForMemset(
9967 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9968 if (Result.getNode())
9969 return Result;
9970 }
9971
9972 // If we really need inline code and the target declined to provide it,
9973 // use a (potentially long) sequence of loads and stores.
9974 if (AlwaysInline) {
9975 assert(ConstantSize && "AlwaysInline requires a constant size!");
9976 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9977 ConstantSize->getZExtValue(), Alignment,
9978 isVol, true, DstPtrInfo, AAInfo);
9979 assert(Result &&
9980 "getMemsetStores must return a valid sequence when AlwaysInline");
9981 return Result;
9982 }
9983
9985
9986 // Emit a library call.
9987 auto &Ctx = *getContext();
9988 const auto& DL = getDataLayout();
9989
9991 // FIXME: pass in SDLoc
9992 CLI.setDebugLoc(dl).setChain(Chain);
9993
9994 RTLIB::LibcallImpl BzeroImpl = Libcalls->getLibcallImpl(RTLIB::BZERO);
9995 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
9996
9997 // If zeroing out and bzero is present, use it.
9998 if (UseBZero) {
10000 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
10001 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
10002 CLI.setLibCallee(
10003 Libcalls->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
10004 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
10005 } else {
10006 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
10007
10009 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
10010 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
10011 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
10012 CLI.setLibCallee(Libcalls->getLibcallImplCallingConv(MemsetImpl),
10013 Dst.getValueType().getTypeForEVT(Ctx),
10014 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
10015 std::move(Args));
10016 }
10017
10018 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
10019 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
10020
10021 // If we're going to use bzero, make sure not to tail call unless the
10022 // subsequent return doesn't need a value, as bzero doesn't return the first
10023 // arg unlike memset.
10024 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
10025 bool IsTailCall =
10026 CI && CI->isTailCall() &&
10027 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
10028 CLI.setDiscardResult().setTailCall(IsTailCall);
10029
10030 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
10031 return CallResult.second;
10032}
10033
10036 Type *SizeTy, unsigned ElemSz,
10037 bool isTailCall,
10038 MachinePointerInfo DstPtrInfo) {
10039 // Emit a library call.
10041 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
10042 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
10043 Args.emplace_back(Size, SizeTy);
10044
10045 RTLIB::Libcall LibraryCall =
10047 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
10048 if (LibcallImpl == RTLIB::Unsupported)
10049 report_fatal_error("Unsupported element size");
10050
10052 CLI.setDebugLoc(dl)
10053 .setChain(Chain)
10054 .setLibCallee(
10055 Libcalls->getLibcallImplCallingConv(LibcallImpl),
10057 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
10058 std::move(Args))
10060 .setTailCall(isTailCall);
10061
10062 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
10063 return CallResult.second;
10064}
10065
10066SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
10068 MachineMemOperand *MMO,
10069 ISD::LoadExtType ExtType) {
10071 AddNodeIDNode(ID, Opcode, VTList, Ops);
10072 ID.AddInteger(MemVT.getRawBits());
10073 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
10074 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
10075 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10076 ID.AddInteger(MMO->getFlags());
10077 void* IP = nullptr;
10078 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10079 E->refineAlignment(MMO);
10080 E->refineRanges(MMO);
10081 return SDValue(E, 0);
10082 }
10083
10084 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
10085 VTList, MemVT, MMO, ExtType);
10086 createOperands(N, Ops);
10087
10088 CSEMap.InsertNode(N, IP);
10089 InsertNode(N);
10090 SDValue V(N, 0);
10091 NewSDValueDbgMsg(V, "Creating new node: ", this);
10092 return V;
10093}
10094
10096 EVT MemVT, SDVTList VTs, SDValue Chain,
10097 SDValue Ptr, SDValue Cmp, SDValue Swp,
10098 MachineMemOperand *MMO) {
10099 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
10101 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
10102
10103 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
10104 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
10105}
10106
10107SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
10108 SDValue Chain, SDValue Ptr, SDValue Val,
10109 MachineMemOperand *MMO) {
10110 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
10111 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
10112 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
10113 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
10114 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
10115 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
10116 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
10117 Opcode == ISD::ATOMIC_LOAD_FMIN ||
10118 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
10119 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
10120 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
10121 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
10122 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
10123 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
10124 Opcode == ISD::ATOMIC_STORE) &&
10125 "Invalid Atomic Op");
10126
10127 EVT VT = Val.getValueType();
10128
10129 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
10130 getVTList(VT, MVT::Other);
10131 SDValue Ops[] = {Chain, Ptr, Val};
10132 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
10133}
10134
10136 EVT MemVT, EVT VT, SDValue Chain,
10137 SDValue Ptr, MachineMemOperand *MMO) {
10138 SDVTList VTs = getVTList(VT, MVT::Other);
10139 SDValue Ops[] = {Chain, Ptr};
10140 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
10141}
10142
10143/// getMergeValues - Create a MERGE_VALUES node from the given operands.
10145 if (Ops.size() == 1)
10146 return Ops[0];
10147
10149 VTs.reserve(Ops.size());
10150 for (const SDValue &Op : Ops)
10151 VTs.push_back(Op.getValueType());
10152 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
10153}
10154
10156 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
10157 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
10159 const AAMDNodes &AAInfo) {
10160 if (Size.hasValue() && !Size.getValue())
10162
10164 MachineMemOperand *MMO =
10165 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
10166
10167 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
10168}
10169
10171 SDVTList VTList,
10172 ArrayRef<SDValue> Ops, EVT MemVT,
10173 MachineMemOperand *MMO) {
10174 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, ArrayRef(MMO));
10175}
10176
10178 SDVTList VTList,
10179 ArrayRef<SDValue> Ops, EVT MemVT,
10181 assert(!MMOs.empty() && "Must have at least one MMO");
10182 assert(
10183 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
10184 Opcode == ISD::PREFETCH ||
10185 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
10186 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
10187 "Opcode is not a memory-accessing opcode!");
10188
10190 if (MMOs.size() == 1) {
10191 MemRefs = MMOs[0];
10192 } else {
10193 // Allocate: [size_t count][MMO*][MMO*]...
10194 size_t AllocSize =
10195 sizeof(size_t) + MMOs.size() * sizeof(MachineMemOperand *);
10196 void *Buffer = Allocator.Allocate(AllocSize, alignof(size_t));
10197 size_t *CountPtr = static_cast<size_t *>(Buffer);
10198 *CountPtr = MMOs.size();
10199 MachineMemOperand **Array =
10200 reinterpret_cast<MachineMemOperand **>(CountPtr + 1);
10201 llvm::copy(MMOs, Array);
10202 MemRefs = Array;
10203 }
10204
10205 // Memoize the node unless it returns a glue result.
10207 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10209 AddNodeIDNode(ID, Opcode, VTList, Ops);
10210 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
10211 Opcode, dl.getIROrder(), VTList, MemVT, MemRefs));
10212 ID.AddInteger(MemVT.getRawBits());
10213 for (const MachineMemOperand *MMO : MMOs) {
10214 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10215 ID.AddInteger(MMO->getFlags());
10216 }
10217 void *IP = nullptr;
10218 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10219 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMOs);
10220 return SDValue(E, 0);
10221 }
10222
10223 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
10224 VTList, MemVT, MemRefs);
10225 createOperands(N, Ops);
10226 CSEMap.InsertNode(N, IP);
10227 } else {
10228 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
10229 VTList, MemVT, MemRefs);
10230 createOperands(N, Ops);
10231 }
10232 InsertNode(N);
10233 SDValue V(N, 0);
10234 NewSDValueDbgMsg(V, "Creating new node: ", this);
10235 return V;
10236}
10237
10239 SDValue Chain, int FrameIndex) {
10240 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
10241 const auto VTs = getVTList(MVT::Other);
10242 SDValue Ops[2] = {
10243 Chain,
10244 getFrameIndex(FrameIndex,
10245 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
10246 true)};
10247
10249 AddNodeIDNode(ID, Opcode, VTs, Ops);
10250 ID.AddInteger(FrameIndex);
10251 void *IP = nullptr;
10252 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10253 return SDValue(E, 0);
10254
10255 LifetimeSDNode *N =
10256 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
10257 createOperands(N, Ops);
10258 CSEMap.InsertNode(N, IP);
10259 InsertNode(N);
10260 SDValue V(N, 0);
10261 NewSDValueDbgMsg(V, "Creating new node: ", this);
10262 return V;
10263}
10264
10266 uint64_t Guid, uint64_t Index,
10267 uint32_t Attr) {
10268 const unsigned Opcode = ISD::PSEUDO_PROBE;
10269 const auto VTs = getVTList(MVT::Other);
10270 SDValue Ops[] = {Chain};
10272 AddNodeIDNode(ID, Opcode, VTs, Ops);
10273 ID.AddInteger(Guid);
10274 ID.AddInteger(Index);
10275 void *IP = nullptr;
10276 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
10277 return SDValue(E, 0);
10278
10279 auto *N = newSDNode<PseudoProbeSDNode>(
10280 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
10281 createOperands(N, Ops);
10282 CSEMap.InsertNode(N, IP);
10283 InsertNode(N);
10284 SDValue V(N, 0);
10285 NewSDValueDbgMsg(V, "Creating new node: ", this);
10286 return V;
10287}
10288
10289/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
10290/// MachinePointerInfo record from it. This is particularly useful because the
10291/// code generator has many cases where it doesn't bother passing in a
10292/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
10294 SelectionDAG &DAG, SDValue Ptr,
10295 int64_t Offset = 0) {
10296 // If this is FI+Offset, we can model it.
10297 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
10299 FI->getIndex(), Offset);
10300
10301 // If this is (FI+Offset1)+Offset2, we can model it.
10302 if (Ptr.getOpcode() != ISD::ADD ||
10305 return Info;
10306
10307 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
10309 DAG.getMachineFunction(), FI,
10310 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
10311}
10312
10313/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
10314/// MachinePointerInfo record from it. This is particularly useful because the
10315/// code generator has many cases where it doesn't bother passing in a
10316/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
10318 SelectionDAG &DAG, SDValue Ptr,
10319 SDValue OffsetOp) {
10320 // If the 'Offset' value isn't a constant, we can't handle this.
10322 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
10323 if (OffsetOp.isUndef())
10324 return InferPointerInfo(Info, DAG, Ptr);
10325 return Info;
10326}
10327
10329 EVT VT, const SDLoc &dl, SDValue Chain,
10330 SDValue Ptr, SDValue Offset,
10331 MachinePointerInfo PtrInfo, EVT MemVT,
10332 Align Alignment,
10333 MachineMemOperand::Flags MMOFlags,
10334 const AAMDNodes &AAInfo, const MDNode *Ranges) {
10335 assert(Chain.getValueType() == MVT::Other &&
10336 "Invalid chain type");
10337
10338 MMOFlags |= MachineMemOperand::MOLoad;
10339 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10340 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10341 // clients.
10342 if (PtrInfo.V.isNull())
10343 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10344
10345 TypeSize Size = MemVT.getStoreSize();
10347 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10348 Alignment, AAInfo, Ranges);
10349 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
10350}
10351
10353 EVT VT, const SDLoc &dl, SDValue Chain,
10354 SDValue Ptr, SDValue Offset, EVT MemVT,
10355 MachineMemOperand *MMO) {
10356 if (VT == MemVT) {
10357 ExtType = ISD::NON_EXTLOAD;
10358 } else if (ExtType == ISD::NON_EXTLOAD) {
10359 assert(VT == MemVT && "Non-extending load from different memory type!");
10360 } else {
10361 // Extending load.
10362 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
10363 "Should only be an extending load, not truncating!");
10364 assert(VT.isInteger() == MemVT.isInteger() &&
10365 "Cannot convert from FP to Int or Int -> FP!");
10366 assert(VT.isVector() == MemVT.isVector() &&
10367 "Cannot use an ext load to convert to or from a vector!");
10368 assert((!VT.isVector() ||
10370 "Cannot use an ext load to change the number of vector elements!");
10371 }
10372
10373 assert((!MMO->getRanges() ||
10375 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
10376 MemVT.isInteger())) &&
10377 "Range metadata and load type must match!");
10378
10379 bool Indexed = AM != ISD::UNINDEXED;
10380 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10381
10382 SDVTList VTs = Indexed ?
10383 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
10384 SDValue Ops[] = { Chain, Ptr, Offset };
10386 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
10387 ID.AddInteger(MemVT.getRawBits());
10388 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
10389 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
10390 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10391 ID.AddInteger(MMO->getFlags());
10392 void *IP = nullptr;
10393 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10394 E->refineAlignment(MMO);
10395 E->refineRanges(MMO);
10396 return SDValue(E, 0);
10397 }
10398 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10399 ExtType, MemVT, MMO);
10400 createOperands(N, Ops);
10401
10402 CSEMap.InsertNode(N, IP);
10403 InsertNode(N);
10404 SDValue V(N, 0);
10405 NewSDValueDbgMsg(V, "Creating new node: ", this);
10406 return V;
10407}
10408
10410 SDValue Ptr, MachinePointerInfo PtrInfo,
10411 MaybeAlign Alignment,
10412 MachineMemOperand::Flags MMOFlags,
10413 const AAMDNodes &AAInfo, const MDNode *Ranges) {
10415 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10416 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
10417}
10418
10420 SDValue Ptr, MachineMemOperand *MMO) {
10422 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10423 VT, MMO);
10424}
10425
10427 EVT VT, SDValue Chain, SDValue Ptr,
10428 MachinePointerInfo PtrInfo, EVT MemVT,
10429 MaybeAlign Alignment,
10430 MachineMemOperand::Flags MMOFlags,
10431 const AAMDNodes &AAInfo) {
10433 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
10434 MemVT, Alignment, MMOFlags, AAInfo);
10435}
10436
10438 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
10439 MachineMemOperand *MMO) {
10441 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
10442 MemVT, MMO);
10443}
10444
10448 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
10449 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10450 // Don't propagate the invariant or dereferenceable flags.
10451 auto MMOFlags =
10452 LD->getMemOperand()->getFlags() &
10454 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10455 LD->getChain(), Base, Offset, LD->getPointerInfo(),
10456 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
10457}
10458
10460 SDValue Ptr, MachinePointerInfo PtrInfo,
10461 Align Alignment,
10462 MachineMemOperand::Flags MMOFlags,
10463 const AAMDNodes &AAInfo) {
10464 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10465
10466 MMOFlags |= MachineMemOperand::MOStore;
10467 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10468
10469 if (PtrInfo.V.isNull())
10470 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10471
10474 MachineMemOperand *MMO =
10475 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
10476 return getStore(Chain, dl, Val, Ptr, MMO);
10477}
10478
10480 SDValue Ptr, MachineMemOperand *MMO) {
10482 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
10484}
10485
10487 SDValue Ptr, SDValue Offset, EVT SVT,
10489 bool IsTruncating) {
10490 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10491 EVT VT = Val.getValueType();
10492 if (VT == SVT) {
10493 IsTruncating = false;
10494 } else if (!IsTruncating) {
10495 assert(VT == SVT && "No-truncating store from different memory type!");
10496 } else {
10498 "Should only be a truncating store, not extending!");
10499 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10500 assert(VT.isVector() == SVT.isVector() &&
10501 "Cannot use trunc store to convert to or from a vector!");
10502 assert((!VT.isVector() ||
10504 "Cannot use trunc store to change the number of vector elements!");
10505 }
10506
10507 bool Indexed = AM != ISD::UNINDEXED;
10508 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
10509 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10510 : getVTList(MVT::Other);
10511 SDValue Ops[] = {Chain, Val, Ptr, Offset};
10514 ID.AddInteger(SVT.getRawBits());
10515 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
10516 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
10517 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10518 ID.AddInteger(MMO->getFlags());
10519 void *IP = nullptr;
10520 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10521 cast<StoreSDNode>(E)->refineAlignment(MMO);
10522 return SDValue(E, 0);
10523 }
10524 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10525 IsTruncating, SVT, MMO);
10526 createOperands(N, Ops);
10527
10528 CSEMap.InsertNode(N, IP);
10529 InsertNode(N);
10530 SDValue V(N, 0);
10531 NewSDValueDbgMsg(V, "Creating new node: ", this);
10532 return V;
10533}
10534
10536 SDValue Ptr, MachinePointerInfo PtrInfo,
10537 EVT SVT, Align Alignment,
10538 MachineMemOperand::Flags MMOFlags,
10539 const AAMDNodes &AAInfo) {
10540 assert(Chain.getValueType() == MVT::Other &&
10541 "Invalid chain type");
10542
10543 MMOFlags |= MachineMemOperand::MOStore;
10544 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10545
10546 if (PtrInfo.V.isNull())
10547 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10548
10550 MachineMemOperand *MMO = MF.getMachineMemOperand(
10551 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10552 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10553}
10554
10556 SDValue Ptr, EVT SVT,
10557 MachineMemOperand *MMO) {
10559 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10560}
10561
10565 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10566 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10567 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10568 ST->getMemoryVT(), ST->getMemOperand(), AM,
10569 ST->isTruncatingStore());
10570}
10571
10573 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10574 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10575 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10576 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10577 const MDNode *Ranges, bool IsExpanding) {
10578 MMOFlags |= MachineMemOperand::MOLoad;
10579 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10580 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10581 // clients.
10582 if (PtrInfo.V.isNull())
10583 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10584
10585 TypeSize Size = MemVT.getStoreSize();
10587 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10588 Alignment, AAInfo, Ranges);
10589 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10590 MMO, IsExpanding);
10591}
10592
10594 ISD::LoadExtType ExtType, EVT VT,
10595 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10596 SDValue Offset, SDValue Mask, SDValue EVL,
10597 EVT MemVT, MachineMemOperand *MMO,
10598 bool IsExpanding) {
10599 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10600 assert(Mask.getValueType().getVectorElementCount() ==
10601 VT.getVectorElementCount() &&
10602 "Vector width mismatch between mask and data");
10603
10604 bool Indexed = AM != ISD::UNINDEXED;
10605 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10606
10607 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10608 : getVTList(VT, MVT::Other);
10609 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10611 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10612 ID.AddInteger(MemVT.getRawBits());
10613 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10614 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10615 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10616 ID.AddInteger(MMO->getFlags());
10617 void *IP = nullptr;
10618 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10619 E->refineAlignment(MMO);
10620 E->refineRanges(MMO);
10621 return SDValue(E, 0);
10622 }
10623 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10624 ExtType, IsExpanding, MemVT, MMO);
10625 createOperands(N, Ops);
10626
10627 CSEMap.InsertNode(N, IP);
10628 InsertNode(N);
10629 SDValue V(N, 0);
10630 NewSDValueDbgMsg(V, "Creating new node: ", this);
10631 return V;
10632}
10633
10635 SDValue Ptr, SDValue Mask, SDValue EVL,
10636 MachinePointerInfo PtrInfo,
10637 MaybeAlign Alignment,
10638 MachineMemOperand::Flags MMOFlags,
10639 const AAMDNodes &AAInfo, const MDNode *Ranges,
10640 bool IsExpanding) {
10642 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10643 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10644 IsExpanding);
10645}
10646
10648 SDValue Ptr, SDValue Mask, SDValue EVL,
10649 MachineMemOperand *MMO, bool IsExpanding) {
10651 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10652 Mask, EVL, VT, MMO, IsExpanding);
10653}
10654
10656 EVT VT, SDValue Chain, SDValue Ptr,
10657 SDValue Mask, SDValue EVL,
10658 MachinePointerInfo PtrInfo, EVT MemVT,
10659 MaybeAlign Alignment,
10660 MachineMemOperand::Flags MMOFlags,
10661 const AAMDNodes &AAInfo, bool IsExpanding) {
10663 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10664 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10665 IsExpanding);
10666}
10667
10669 EVT VT, SDValue Chain, SDValue Ptr,
10670 SDValue Mask, SDValue EVL, EVT MemVT,
10671 MachineMemOperand *MMO, bool IsExpanding) {
10673 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10674 EVL, MemVT, MMO, IsExpanding);
10675}
10676
10680 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10681 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10682 // Don't propagate the invariant or dereferenceable flags.
10683 auto MMOFlags =
10684 LD->getMemOperand()->getFlags() &
10686 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10687 LD->getChain(), Base, Offset, LD->getMask(),
10688 LD->getVectorLength(), LD->getPointerInfo(),
10689 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10690 nullptr, LD->isExpandingLoad());
10691}
10692
10694 SDValue Ptr, SDValue Offset, SDValue Mask,
10695 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10696 ISD::MemIndexedMode AM, bool IsTruncating,
10697 bool IsCompressing) {
10698 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10699 assert(Mask.getValueType().getVectorElementCount() ==
10701 "Vector width mismatch between mask and data");
10702
10703 bool Indexed = AM != ISD::UNINDEXED;
10704 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10705 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10706 : getVTList(MVT::Other);
10707 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10709 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10710 ID.AddInteger(MemVT.getRawBits());
10711 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10712 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10713 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10714 ID.AddInteger(MMO->getFlags());
10715 void *IP = nullptr;
10716 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10717 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10718 return SDValue(E, 0);
10719 }
10720 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10721 IsTruncating, IsCompressing, MemVT, MMO);
10722 createOperands(N, Ops);
10723
10724 CSEMap.InsertNode(N, IP);
10725 InsertNode(N);
10726 SDValue V(N, 0);
10727 NewSDValueDbgMsg(V, "Creating new node: ", this);
10728 return V;
10729}
10730
10732 SDValue Val, SDValue Ptr, SDValue Mask,
10733 SDValue EVL, MachinePointerInfo PtrInfo,
10734 EVT SVT, Align Alignment,
10735 MachineMemOperand::Flags MMOFlags,
10736 const AAMDNodes &AAInfo,
10737 bool IsCompressing) {
10738 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10739
10740 MMOFlags |= MachineMemOperand::MOStore;
10741 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10742
10743 if (PtrInfo.V.isNull())
10744 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10745
10747 MachineMemOperand *MMO = MF.getMachineMemOperand(
10748 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10749 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10750 IsCompressing);
10751}
10752
10754 SDValue Val, SDValue Ptr, SDValue Mask,
10755 SDValue EVL, EVT SVT,
10756 MachineMemOperand *MMO,
10757 bool IsCompressing) {
10758 EVT VT = Val.getValueType();
10759
10760 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10761 if (VT == SVT)
10762 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10763 EVL, VT, MMO, ISD::UNINDEXED,
10764 /*IsTruncating*/ false, IsCompressing);
10765
10767 "Should only be a truncating store, not extending!");
10768 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10769 assert(VT.isVector() == SVT.isVector() &&
10770 "Cannot use trunc store to convert to or from a vector!");
10771 assert((!VT.isVector() ||
10773 "Cannot use trunc store to change the number of vector elements!");
10774
10775 SDVTList VTs = getVTList(MVT::Other);
10777 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10779 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10780 ID.AddInteger(SVT.getRawBits());
10781 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10782 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10783 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10784 ID.AddInteger(MMO->getFlags());
10785 void *IP = nullptr;
10786 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10787 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10788 return SDValue(E, 0);
10789 }
10790 auto *N =
10791 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10792 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10793 createOperands(N, Ops);
10794
10795 CSEMap.InsertNode(N, IP);
10796 InsertNode(N);
10797 SDValue V(N, 0);
10798 NewSDValueDbgMsg(V, "Creating new node: ", this);
10799 return V;
10800}
10801
10805 auto *ST = cast<VPStoreSDNode>(OrigStore);
10806 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10807 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10808 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10809 Offset, ST->getMask(), ST->getVectorLength()};
10811 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10812 ID.AddInteger(ST->getMemoryVT().getRawBits());
10813 ID.AddInteger(ST->getRawSubclassData());
10814 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10815 ID.AddInteger(ST->getMemOperand()->getFlags());
10816 void *IP = nullptr;
10817 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10818 return SDValue(E, 0);
10819
10820 auto *N = newSDNode<VPStoreSDNode>(
10821 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10822 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10823 createOperands(N, Ops);
10824
10825 CSEMap.InsertNode(N, IP);
10826 InsertNode(N);
10827 SDValue V(N, 0);
10828 NewSDValueDbgMsg(V, "Creating new node: ", this);
10829 return V;
10830}
10831
10833 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10834 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10835 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10836 bool Indexed = AM != ISD::UNINDEXED;
10837 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10838
10839 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10840 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10841 : getVTList(VT, MVT::Other);
10843 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10844 ID.AddInteger(VT.getRawBits());
10845 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10846 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10847 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10848
10849 void *IP = nullptr;
10850 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10851 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10852 return SDValue(E, 0);
10853 }
10854
10855 auto *N =
10856 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10857 ExtType, IsExpanding, MemVT, MMO);
10858 createOperands(N, Ops);
10859 CSEMap.InsertNode(N, IP);
10860 InsertNode(N);
10861 SDValue V(N, 0);
10862 NewSDValueDbgMsg(V, "Creating new node: ", this);
10863 return V;
10864}
10865
10867 SDValue Ptr, SDValue Stride,
10868 SDValue Mask, SDValue EVL,
10869 MachineMemOperand *MMO,
10870 bool IsExpanding) {
10872 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10873 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10874}
10875
10877 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10878 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10879 MachineMemOperand *MMO, bool IsExpanding) {
10881 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10882 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10883}
10884
10886 SDValue Val, SDValue Ptr,
10887 SDValue Offset, SDValue Stride,
10888 SDValue Mask, SDValue EVL, EVT MemVT,
10889 MachineMemOperand *MMO,
10891 bool IsTruncating, bool IsCompressing) {
10892 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10893 bool Indexed = AM != ISD::UNINDEXED;
10894 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10895 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10896 : getVTList(MVT::Other);
10897 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10899 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10900 ID.AddInteger(MemVT.getRawBits());
10901 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10902 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10903 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10904 void *IP = nullptr;
10905 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10906 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10907 return SDValue(E, 0);
10908 }
10909 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10910 VTs, AM, IsTruncating,
10911 IsCompressing, MemVT, MMO);
10912 createOperands(N, Ops);
10913
10914 CSEMap.InsertNode(N, IP);
10915 InsertNode(N);
10916 SDValue V(N, 0);
10917 NewSDValueDbgMsg(V, "Creating new node: ", this);
10918 return V;
10919}
10920
10922 SDValue Val, SDValue Ptr,
10923 SDValue Stride, SDValue Mask,
10924 SDValue EVL, EVT SVT,
10925 MachineMemOperand *MMO,
10926 bool IsCompressing) {
10927 EVT VT = Val.getValueType();
10928
10929 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10930 if (VT == SVT)
10931 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10932 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10933 /*IsTruncating*/ false, IsCompressing);
10934
10936 "Should only be a truncating store, not extending!");
10937 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10938 assert(VT.isVector() == SVT.isVector() &&
10939 "Cannot use trunc store to convert to or from a vector!");
10940 assert((!VT.isVector() ||
10942 "Cannot use trunc store to change the number of vector elements!");
10943
10944 SDVTList VTs = getVTList(MVT::Other);
10946 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10948 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10949 ID.AddInteger(SVT.getRawBits());
10950 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10951 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10952 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10953 void *IP = nullptr;
10954 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10955 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10956 return SDValue(E, 0);
10957 }
10958 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10959 VTs, ISD::UNINDEXED, true,
10960 IsCompressing, SVT, MMO);
10961 createOperands(N, Ops);
10962
10963 CSEMap.InsertNode(N, IP);
10964 InsertNode(N);
10965 SDValue V(N, 0);
10966 NewSDValueDbgMsg(V, "Creating new node: ", this);
10967 return V;
10968}
10969
10972 ISD::MemIndexType IndexType) {
10973 assert(Ops.size() == 6 && "Incompatible number of operands");
10974
10976 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10977 ID.AddInteger(VT.getRawBits());
10978 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10979 dl.getIROrder(), VTs, VT, MMO, IndexType));
10980 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10981 ID.AddInteger(MMO->getFlags());
10982 void *IP = nullptr;
10983 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10984 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10985 return SDValue(E, 0);
10986 }
10987
10988 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10989 VT, MMO, IndexType);
10990 createOperands(N, Ops);
10991
10992 assert(N->getMask().getValueType().getVectorElementCount() ==
10993 N->getValueType(0).getVectorElementCount() &&
10994 "Vector width mismatch between mask and data");
10995 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10996 N->getValueType(0).getVectorElementCount().isScalable() &&
10997 "Scalable flags of index and data do not match");
10999 N->getIndex().getValueType().getVectorElementCount(),
11000 N->getValueType(0).getVectorElementCount()) &&
11001 "Vector width mismatch between index and data");
11002 assert(isa<ConstantSDNode>(N->getScale()) &&
11003 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11004 "Scale should be a constant power of 2");
11005
11006 CSEMap.InsertNode(N, IP);
11007 InsertNode(N);
11008 SDValue V(N, 0);
11009 NewSDValueDbgMsg(V, "Creating new node: ", this);
11010 return V;
11011}
11012
11015 MachineMemOperand *MMO,
11016 ISD::MemIndexType IndexType) {
11017 assert(Ops.size() == 7 && "Incompatible number of operands");
11018
11020 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
11021 ID.AddInteger(VT.getRawBits());
11022 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
11023 dl.getIROrder(), VTs, VT, MMO, IndexType));
11024 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11025 ID.AddInteger(MMO->getFlags());
11026 void *IP = nullptr;
11027 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11028 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
11029 return SDValue(E, 0);
11030 }
11031 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11032 VT, MMO, IndexType);
11033 createOperands(N, Ops);
11034
11035 assert(N->getMask().getValueType().getVectorElementCount() ==
11036 N->getValue().getValueType().getVectorElementCount() &&
11037 "Vector width mismatch between mask and data");
11038 assert(
11039 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11040 N->getValue().getValueType().getVectorElementCount().isScalable() &&
11041 "Scalable flags of index and data do not match");
11043 N->getIndex().getValueType().getVectorElementCount(),
11044 N->getValue().getValueType().getVectorElementCount()) &&
11045 "Vector width mismatch between index and data");
11046 assert(isa<ConstantSDNode>(N->getScale()) &&
11047 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11048 "Scale should be a constant power of 2");
11049
11050 CSEMap.InsertNode(N, IP);
11051 InsertNode(N);
11052 SDValue V(N, 0);
11053 NewSDValueDbgMsg(V, "Creating new node: ", this);
11054 return V;
11055}
11056
11059 SDValue PassThru, EVT MemVT,
11060 MachineMemOperand *MMO,
11062 ISD::LoadExtType ExtTy, bool isExpanding) {
11063 bool Indexed = AM != ISD::UNINDEXED;
11064 assert((Indexed || Offset.isUndef()) &&
11065 "Unindexed masked load with an offset!");
11066 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
11067 : getVTList(VT, MVT::Other);
11068 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
11071 ID.AddInteger(MemVT.getRawBits());
11072 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
11073 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
11074 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11075 ID.AddInteger(MMO->getFlags());
11076 void *IP = nullptr;
11077 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11078 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
11079 return SDValue(E, 0);
11080 }
11081 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11082 AM, ExtTy, isExpanding, MemVT, MMO);
11083 createOperands(N, Ops);
11084
11085 CSEMap.InsertNode(N, IP);
11086 InsertNode(N);
11087 SDValue V(N, 0);
11088 NewSDValueDbgMsg(V, "Creating new node: ", this);
11089 return V;
11090}
11091
11096 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
11097 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
11098 Offset, LD->getMask(), LD->getPassThru(),
11099 LD->getMemoryVT(), LD->getMemOperand(), AM,
11100 LD->getExtensionType(), LD->isExpandingLoad());
11101}
11102
11105 SDValue Mask, EVT MemVT,
11106 MachineMemOperand *MMO,
11107 ISD::MemIndexedMode AM, bool IsTruncating,
11108 bool IsCompressing) {
11109 assert(Chain.getValueType() == MVT::Other &&
11110 "Invalid chain type");
11111 bool Indexed = AM != ISD::UNINDEXED;
11112 assert((Indexed || Offset.isUndef()) &&
11113 "Unindexed masked store with an offset!");
11114 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
11115 : getVTList(MVT::Other);
11116 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
11119 ID.AddInteger(MemVT.getRawBits());
11120 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
11121 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
11122 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11123 ID.AddInteger(MMO->getFlags());
11124 void *IP = nullptr;
11125 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11126 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
11127 return SDValue(E, 0);
11128 }
11129 auto *N =
11130 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
11131 IsTruncating, IsCompressing, MemVT, MMO);
11132 createOperands(N, Ops);
11133
11134 CSEMap.InsertNode(N, IP);
11135 InsertNode(N);
11136 SDValue V(N, 0);
11137 NewSDValueDbgMsg(V, "Creating new node: ", this);
11138 return V;
11139}
11140
11145 assert(ST->getOffset().isUndef() &&
11146 "Masked store is already a indexed store!");
11147 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
11148 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
11149 AM, ST->isTruncatingStore(), ST->isCompressingStore());
11150}
11151
11154 MachineMemOperand *MMO,
11155 ISD::MemIndexType IndexType,
11156 ISD::LoadExtType ExtTy) {
11157 assert(Ops.size() == 6 && "Incompatible number of operands");
11158
11161 ID.AddInteger(MemVT.getRawBits());
11162 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
11163 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
11164 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11165 ID.AddInteger(MMO->getFlags());
11166 void *IP = nullptr;
11167 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11168 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
11169 return SDValue(E, 0);
11170 }
11171
11172 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11173 VTs, MemVT, MMO, IndexType, ExtTy);
11174 createOperands(N, Ops);
11175
11176 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
11177 "Incompatible type of the PassThru value in MaskedGatherSDNode");
11178 assert(N->getMask().getValueType().getVectorElementCount() ==
11179 N->getValueType(0).getVectorElementCount() &&
11180 "Vector width mismatch between mask and data");
11181 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11182 N->getValueType(0).getVectorElementCount().isScalable() &&
11183 "Scalable flags of index and data do not match");
11185 N->getIndex().getValueType().getVectorElementCount(),
11186 N->getValueType(0).getVectorElementCount()) &&
11187 "Vector width mismatch between index and data");
11188 assert(isa<ConstantSDNode>(N->getScale()) &&
11189 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11190 "Scale should be a constant power of 2");
11191
11192 CSEMap.InsertNode(N, IP);
11193 InsertNode(N);
11194 SDValue V(N, 0);
11195 NewSDValueDbgMsg(V, "Creating new node: ", this);
11196 return V;
11197}
11198
11201 MachineMemOperand *MMO,
11202 ISD::MemIndexType IndexType,
11203 bool IsTrunc) {
11204 assert(Ops.size() == 6 && "Incompatible number of operands");
11205
11208 ID.AddInteger(MemVT.getRawBits());
11209 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
11210 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
11211 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11212 ID.AddInteger(MMO->getFlags());
11213 void *IP = nullptr;
11214 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11215 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
11216 return SDValue(E, 0);
11217 }
11218
11219 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11220 VTs, MemVT, MMO, IndexType, IsTrunc);
11221 createOperands(N, Ops);
11222
11223 assert(N->getMask().getValueType().getVectorElementCount() ==
11224 N->getValue().getValueType().getVectorElementCount() &&
11225 "Vector width mismatch between mask and data");
11226 assert(
11227 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11228 N->getValue().getValueType().getVectorElementCount().isScalable() &&
11229 "Scalable flags of index and data do not match");
11231 N->getIndex().getValueType().getVectorElementCount(),
11232 N->getValue().getValueType().getVectorElementCount()) &&
11233 "Vector width mismatch between index and data");
11234 assert(isa<ConstantSDNode>(N->getScale()) &&
11235 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11236 "Scale should be a constant power of 2");
11237
11238 CSEMap.InsertNode(N, IP);
11239 InsertNode(N);
11240 SDValue V(N, 0);
11241 NewSDValueDbgMsg(V, "Creating new node: ", this);
11242 return V;
11243}
11244
11246 const SDLoc &dl, ArrayRef<SDValue> Ops,
11247 MachineMemOperand *MMO,
11248 ISD::MemIndexType IndexType) {
11249 assert(Ops.size() == 7 && "Incompatible number of operands");
11250
11253 ID.AddInteger(MemVT.getRawBits());
11254 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
11255 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
11256 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11257 ID.AddInteger(MMO->getFlags());
11258 void *IP = nullptr;
11259 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11260 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
11261 return SDValue(E, 0);
11262 }
11263
11264 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11265 VTs, MemVT, MMO, IndexType);
11266 createOperands(N, Ops);
11267
11268 assert(N->getMask().getValueType().getVectorElementCount() ==
11269 N->getIndex().getValueType().getVectorElementCount() &&
11270 "Vector width mismatch between mask and data");
11271 assert(isa<ConstantSDNode>(N->getScale()) &&
11272 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11273 "Scale should be a constant power of 2");
11274 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
11275
11276 CSEMap.InsertNode(N, IP);
11277 InsertNode(N);
11278 SDValue V(N, 0);
11279 NewSDValueDbgMsg(V, "Creating new node: ", this);
11280 return V;
11281}
11282
11284 SDValue Ptr, SDValue Mask, SDValue EVL,
11285 MachineMemOperand *MMO) {
11286 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
11287 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
11289 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
11290 ID.AddInteger(VT.getRawBits());
11291 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
11292 VTs, VT, MMO));
11293 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11294 ID.AddInteger(MMO->getFlags());
11295 void *IP = nullptr;
11296 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11297 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
11298 return SDValue(E, 0);
11299 }
11300 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
11301 VT, MMO);
11302 createOperands(N, Ops);
11303
11304 CSEMap.InsertNode(N, IP);
11305 InsertNode(N);
11306 SDValue V(N, 0);
11307 NewSDValueDbgMsg(V, "Creating new node: ", this);
11308 return V;
11309}
11310
11312 EVT MemVT, MachineMemOperand *MMO) {
11313 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11314 SDVTList VTs = getVTList(MVT::Other);
11315 SDValue Ops[] = {Chain, Ptr};
11318 ID.AddInteger(MemVT.getRawBits());
11319 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
11320 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
11321 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11322 ID.AddInteger(MMO->getFlags());
11323 void *IP = nullptr;
11324 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
11325 return SDValue(E, 0);
11326
11327 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
11328 dl.getDebugLoc(), VTs, MemVT, MMO);
11329 createOperands(N, Ops);
11330
11331 CSEMap.InsertNode(N, IP);
11332 InsertNode(N);
11333 SDValue V(N, 0);
11334 NewSDValueDbgMsg(V, "Creating new node: ", this);
11335 return V;
11336}
11337
11339 EVT MemVT, MachineMemOperand *MMO) {
11340 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11341 SDVTList VTs = getVTList(MVT::Other);
11342 SDValue Ops[] = {Chain, Ptr};
11345 ID.AddInteger(MemVT.getRawBits());
11346 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
11347 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
11348 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11349 ID.AddInteger(MMO->getFlags());
11350 void *IP = nullptr;
11351 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
11352 return SDValue(E, 0);
11353
11354 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
11355 dl.getDebugLoc(), VTs, MemVT, MMO);
11356 createOperands(N, Ops);
11357
11358 CSEMap.InsertNode(N, IP);
11359 InsertNode(N);
11360 SDValue V(N, 0);
11361 NewSDValueDbgMsg(V, "Creating new node: ", this);
11362 return V;
11363}
11364
11366 // select undef, T, F --> T (if T is a constant), otherwise F
11367 // select, ?, undef, F --> F
11368 // select, ?, T, undef --> T
11369 if (Cond.isUndef())
11370 return isConstantValueOfAnyType(T) ? T : F;
11371 if (T.isUndef())
11373 if (F.isUndef())
11375
11376 // select true, T, F --> T
11377 // select false, T, F --> F
11378 if (auto C = isBoolConstant(Cond))
11379 return *C ? T : F;
11380
11381 // select ?, T, T --> T
11382 if (T == F)
11383 return T;
11384
11385 return SDValue();
11386}
11387
11389 // shift undef, Y --> 0 (can always assume that the undef value is 0)
11390 if (X.isUndef())
11391 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
11392 // shift X, undef --> undef (because it may shift by the bitwidth)
11393 if (Y.isUndef())
11394 return getUNDEF(X.getValueType());
11395
11396 // shift 0, Y --> 0
11397 // shift X, 0 --> X
11399 return X;
11400
11401 // shift X, C >= bitwidth(X) --> undef
11402 // All vector elements must be too big (or undef) to avoid partial undefs.
11403 auto isShiftTooBig = [X](ConstantSDNode *Val) {
11404 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
11405 };
11406 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
11407 return getUNDEF(X.getValueType());
11408
11409 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
11410 if (X.getValueType().getScalarType() == MVT::i1)
11411 return X;
11412
11413 return SDValue();
11414}
11415
11417 SDNodeFlags Flags) {
11418 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
11419 // (an undef operand can be chosen to be Nan/Inf), then the result of this
11420 // operation is poison. That result can be relaxed to undef.
11421 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
11422 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
11423 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
11424 (YC && YC->getValueAPF().isNaN());
11425 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
11426 (YC && YC->getValueAPF().isInfinity());
11427
11428 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
11429 return getUNDEF(X.getValueType());
11430
11431 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
11432 return getUNDEF(X.getValueType());
11433
11434 if (!YC)
11435 return SDValue();
11436
11437 // X + -0.0 --> X
11438 if (Opcode == ISD::FADD)
11439 if (YC->getValueAPF().isNegZero())
11440 return X;
11441
11442 // X - +0.0 --> X
11443 if (Opcode == ISD::FSUB)
11444 if (YC->getValueAPF().isPosZero())
11445 return X;
11446
11447 // X * 1.0 --> X
11448 // X / 1.0 --> X
11449 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
11450 if (YC->getValueAPF().isExactlyValue(1.0))
11451 return X;
11452
11453 // X * 0.0 --> 0.0
11454 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
11455 if (YC->getValueAPF().isZero())
11456 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
11457
11458 return SDValue();
11459}
11460
11462 SDValue Ptr, SDValue SV, unsigned Align) {
11463 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
11464 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
11465}
11466
11467SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11469 switch (Ops.size()) {
11470 case 0: return getNode(Opcode, DL, VT);
11471 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
11472 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
11473 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
11474 default: break;
11475 }
11476
11477 // Copy from an SDUse array into an SDValue array for use with
11478 // the regular getNode logic.
11480 return getNode(Opcode, DL, VT, NewOps);
11481}
11482
11483SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11485 SDNodeFlags Flags;
11486 if (Inserter)
11487 Flags = Inserter->getFlags();
11488 return getNode(Opcode, DL, VT, Ops, Flags);
11489}
11490
11491SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11492 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11493 unsigned NumOps = Ops.size();
11494 switch (NumOps) {
11495 case 0: return getNode(Opcode, DL, VT);
11496 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
11497 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
11498 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
11499 default: break;
11500 }
11501
11502#ifndef NDEBUG
11503 for (const auto &Op : Ops)
11504 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11505 "Operand is DELETED_NODE!");
11506#endif
11507
11508 switch (Opcode) {
11509 default: break;
11510 case ISD::BUILD_VECTOR:
11511 // Attempt to simplify BUILD_VECTOR.
11512 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
11513 return V;
11514 break;
11516 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
11517 return V;
11518 break;
11519 case ISD::SELECT_CC:
11520 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
11521 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
11522 "LHS and RHS of condition must have same type!");
11523 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11524 "True and False arms of SelectCC must have same type!");
11525 assert(Ops[2].getValueType() == VT &&
11526 "select_cc node must be of same type as true and false value!");
11527 assert((!Ops[0].getValueType().isVector() ||
11528 Ops[0].getValueType().getVectorElementCount() ==
11529 VT.getVectorElementCount()) &&
11530 "Expected select_cc with vector result to have the same sized "
11531 "comparison type!");
11532 break;
11533 case ISD::BR_CC:
11534 assert(NumOps == 5 && "BR_CC takes 5 operands!");
11535 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11536 "LHS/RHS of comparison should match types!");
11537 break;
11538 case ISD::VP_ADD:
11539 case ISD::VP_SUB:
11540 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
11541 if (VT.getScalarType() == MVT::i1)
11542 Opcode = ISD::VP_XOR;
11543 break;
11544 case ISD::VP_MUL:
11545 // If it is VP_MUL mask operation then turn it to VP_AND
11546 if (VT.getScalarType() == MVT::i1)
11547 Opcode = ISD::VP_AND;
11548 break;
11549 case ISD::VP_REDUCE_MUL:
11550 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11551 if (VT == MVT::i1)
11552 Opcode = ISD::VP_REDUCE_AND;
11553 break;
11554 case ISD::VP_REDUCE_ADD:
11555 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11556 if (VT == MVT::i1)
11557 Opcode = ISD::VP_REDUCE_XOR;
11558 break;
11559 case ISD::VP_REDUCE_SMAX:
11560 case ISD::VP_REDUCE_UMIN:
11561 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11562 // VP_REDUCE_AND.
11563 if (VT == MVT::i1)
11564 Opcode = ISD::VP_REDUCE_AND;
11565 break;
11566 case ISD::VP_REDUCE_SMIN:
11567 case ISD::VP_REDUCE_UMAX:
11568 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11569 // VP_REDUCE_OR.
11570 if (VT == MVT::i1)
11571 Opcode = ISD::VP_REDUCE_OR;
11572 break;
11573 }
11574
11575 // Memoize nodes.
11576 SDNode *N;
11577 SDVTList VTs = getVTList(VT);
11578
11579 if (VT != MVT::Glue) {
11581 AddNodeIDNode(ID, Opcode, VTs, Ops);
11582 void *IP = nullptr;
11583
11584 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11585 E->intersectFlagsWith(Flags);
11586 return SDValue(E, 0);
11587 }
11588
11589 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11590 createOperands(N, Ops);
11591
11592 CSEMap.InsertNode(N, IP);
11593 } else {
11594 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11595 createOperands(N, Ops);
11596 }
11597
11598 N->setFlags(Flags);
11599 InsertNode(N);
11600 SDValue V(N, 0);
11601 NewSDValueDbgMsg(V, "Creating new node: ", this);
11602 return V;
11603}
11604
11605SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11606 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11607 SDNodeFlags Flags;
11608 if (Inserter)
11609 Flags = Inserter->getFlags();
11610 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11611}
11612
11613SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11615 const SDNodeFlags Flags) {
11616 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11617}
11618
11619SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11621 SDNodeFlags Flags;
11622 if (Inserter)
11623 Flags = Inserter->getFlags();
11624 return getNode(Opcode, DL, VTList, Ops, Flags);
11625}
11626
11627SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11628 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11629 if (VTList.NumVTs == 1)
11630 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11631
11632#ifndef NDEBUG
11633 for (const auto &Op : Ops)
11634 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11635 "Operand is DELETED_NODE!");
11636#endif
11637
11638 switch (Opcode) {
11639 case ISD::SADDO:
11640 case ISD::UADDO:
11641 case ISD::SSUBO:
11642 case ISD::USUBO: {
11643 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11644 "Invalid add/sub overflow op!");
11645 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11646 Ops[0].getValueType() == Ops[1].getValueType() &&
11647 Ops[0].getValueType() == VTList.VTs[0] &&
11648 "Binary operator types must match!");
11649 SDValue N1 = Ops[0], N2 = Ops[1];
11650 canonicalizeCommutativeBinop(Opcode, N1, N2);
11651
11652 // (X +- 0) -> X with zero-overflow.
11653 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11654 /*AllowTruncation*/ true);
11655 if (N2CV && N2CV->isZero()) {
11656 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11657 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11658 }
11659
11660 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11661 VTList.VTs[1].getScalarType() == MVT::i1) {
11662 SDValue F1 = getFreeze(N1);
11663 SDValue F2 = getFreeze(N2);
11664 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11665 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11666 return getNode(ISD::MERGE_VALUES, DL, VTList,
11667 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11668 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11669 Flags);
11670 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11671 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11672 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11673 return getNode(ISD::MERGE_VALUES, DL, VTList,
11674 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11675 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11676 Flags);
11677 }
11678 }
11679 break;
11680 }
11681 case ISD::SADDO_CARRY:
11682 case ISD::UADDO_CARRY:
11683 case ISD::SSUBO_CARRY:
11684 case ISD::USUBO_CARRY:
11685 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11686 "Invalid add/sub overflow op!");
11687 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11688 Ops[0].getValueType() == Ops[1].getValueType() &&
11689 Ops[0].getValueType() == VTList.VTs[0] &&
11690 Ops[2].getValueType() == VTList.VTs[1] &&
11691 "Binary operator types must match!");
11692 break;
11693 case ISD::SMUL_LOHI:
11694 case ISD::UMUL_LOHI: {
11695 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11696 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11697 VTList.VTs[0] == Ops[0].getValueType() &&
11698 VTList.VTs[0] == Ops[1].getValueType() &&
11699 "Binary operator types must match!");
11700 // Constant fold.
11703 if (LHS && RHS) {
11704 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11705 unsigned OutWidth = Width * 2;
11706 APInt Val = LHS->getAPIntValue();
11707 APInt Mul = RHS->getAPIntValue();
11708 if (Opcode == ISD::SMUL_LOHI) {
11709 Val = Val.sext(OutWidth);
11710 Mul = Mul.sext(OutWidth);
11711 } else {
11712 Val = Val.zext(OutWidth);
11713 Mul = Mul.zext(OutWidth);
11714 }
11715 Val *= Mul;
11716
11717 SDValue Hi =
11718 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11719 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11720 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11721 }
11722 break;
11723 }
11724 case ISD::FFREXP: {
11725 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11726 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11727 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11728
11730 int FrexpExp;
11731 APFloat FrexpMant =
11732 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11733 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11734 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11735 DL, VTList.VTs[1]);
11736 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11737 }
11738
11739 break;
11740 }
11742 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11743 "Invalid STRICT_FP_EXTEND!");
11744 assert(VTList.VTs[0].isFloatingPoint() &&
11745 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11746 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11747 "STRICT_FP_EXTEND result type should be vector iff the operand "
11748 "type is vector!");
11749 assert((!VTList.VTs[0].isVector() ||
11750 VTList.VTs[0].getVectorElementCount() ==
11751 Ops[1].getValueType().getVectorElementCount()) &&
11752 "Vector element count mismatch!");
11753 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11754 "Invalid fpext node, dst <= src!");
11755 break;
11757 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11758 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11759 "STRICT_FP_ROUND result type should be vector iff the operand "
11760 "type is vector!");
11761 assert((!VTList.VTs[0].isVector() ||
11762 VTList.VTs[0].getVectorElementCount() ==
11763 Ops[1].getValueType().getVectorElementCount()) &&
11764 "Vector element count mismatch!");
11765 assert(VTList.VTs[0].isFloatingPoint() &&
11766 Ops[1].getValueType().isFloatingPoint() &&
11767 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11768 Ops[2].getOpcode() == ISD::TargetConstant &&
11769 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11770 "Invalid STRICT_FP_ROUND!");
11771 break;
11772 }
11773
11774 // Memoize the node unless it returns a glue result.
11775 SDNode *N;
11776 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11778 AddNodeIDNode(ID, Opcode, VTList, Ops);
11779 void *IP = nullptr;
11780 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11781 E->intersectFlagsWith(Flags);
11782 return SDValue(E, 0);
11783 }
11784
11785 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11786 createOperands(N, Ops);
11787 CSEMap.InsertNode(N, IP);
11788 } else {
11789 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11790 createOperands(N, Ops);
11791 }
11792
11793 N->setFlags(Flags);
11794 InsertNode(N);
11795 SDValue V(N, 0);
11796 NewSDValueDbgMsg(V, "Creating new node: ", this);
11797 return V;
11798}
11799
11800SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11801 SDVTList VTList) {
11802 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11803}
11804
11805SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11806 SDValue N1) {
11807 SDValue Ops[] = { N1 };
11808 return getNode(Opcode, DL, VTList, Ops);
11809}
11810
11811SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11812 SDValue N1, SDValue N2) {
11813 SDValue Ops[] = { N1, N2 };
11814 return getNode(Opcode, DL, VTList, Ops);
11815}
11816
11817SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11818 SDValue N1, SDValue N2, SDValue N3) {
11819 SDValue Ops[] = { N1, N2, N3 };
11820 return getNode(Opcode, DL, VTList, Ops);
11821}
11822
11823SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11824 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11825 SDValue Ops[] = { N1, N2, N3, N4 };
11826 return getNode(Opcode, DL, VTList, Ops);
11827}
11828
11829SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11830 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11831 SDValue N5) {
11832 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11833 return getNode(Opcode, DL, VTList, Ops);
11834}
11835
11837 if (!VT.isExtended())
11838 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11839
11840 return makeVTList(&(*EVTs.insert(VT).first), 1);
11841}
11842
11845 ID.AddInteger(2U);
11846 ID.AddInteger(VT1.getRawBits());
11847 ID.AddInteger(VT2.getRawBits());
11848
11849 void *IP = nullptr;
11850 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11851 if (!Result) {
11852 EVT *Array = Allocator.Allocate<EVT>(2);
11853 Array[0] = VT1;
11854 Array[1] = VT2;
11855 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11856 VTListMap.InsertNode(Result, IP);
11857 }
11858 return Result->getSDVTList();
11859}
11860
11863 ID.AddInteger(3U);
11864 ID.AddInteger(VT1.getRawBits());
11865 ID.AddInteger(VT2.getRawBits());
11866 ID.AddInteger(VT3.getRawBits());
11867
11868 void *IP = nullptr;
11869 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11870 if (!Result) {
11871 EVT *Array = Allocator.Allocate<EVT>(3);
11872 Array[0] = VT1;
11873 Array[1] = VT2;
11874 Array[2] = VT3;
11875 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11876 VTListMap.InsertNode(Result, IP);
11877 }
11878 return Result->getSDVTList();
11879}
11880
11883 ID.AddInteger(4U);
11884 ID.AddInteger(VT1.getRawBits());
11885 ID.AddInteger(VT2.getRawBits());
11886 ID.AddInteger(VT3.getRawBits());
11887 ID.AddInteger(VT4.getRawBits());
11888
11889 void *IP = nullptr;
11890 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11891 if (!Result) {
11892 EVT *Array = Allocator.Allocate<EVT>(4);
11893 Array[0] = VT1;
11894 Array[1] = VT2;
11895 Array[2] = VT3;
11896 Array[3] = VT4;
11897 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11898 VTListMap.InsertNode(Result, IP);
11899 }
11900 return Result->getSDVTList();
11901}
11902
11904 unsigned NumVTs = VTs.size();
11906 ID.AddInteger(NumVTs);
11907 for (unsigned index = 0; index < NumVTs; index++) {
11908 ID.AddInteger(VTs[index].getRawBits());
11909 }
11910
11911 void *IP = nullptr;
11912 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11913 if (!Result) {
11914 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11915 llvm::copy(VTs, Array);
11916 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11917 VTListMap.InsertNode(Result, IP);
11918 }
11919 return Result->getSDVTList();
11920}
11921
11922
11923/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11924/// specified operands. If the resultant node already exists in the DAG,
11925/// this does not modify the specified node, instead it returns the node that
11926/// already exists. If the resultant node does not exist in the DAG, the
11927/// input node is returned. As a degenerate case, if you specify the same
11928/// input operands as the node already has, the input node is returned.
11930 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11931
11932 // Check to see if there is no change.
11933 if (Op == N->getOperand(0)) return N;
11934
11935 // See if the modified node already exists.
11936 void *InsertPos = nullptr;
11937 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11938 return Existing;
11939
11940 // Nope it doesn't. Remove the node from its current place in the maps.
11941 if (InsertPos)
11942 if (!RemoveNodeFromCSEMaps(N))
11943 InsertPos = nullptr;
11944
11945 // Now we update the operands.
11946 N->OperandList[0].set(Op);
11947
11949 // If this gets put into a CSE map, add it.
11950 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11951 return N;
11952}
11953
11955 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11956
11957 // Check to see if there is no change.
11958 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11959 return N; // No operands changed, just return the input node.
11960
11961 // See if the modified node already exists.
11962 void *InsertPos = nullptr;
11963 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11964 return Existing;
11965
11966 // Nope it doesn't. Remove the node from its current place in the maps.
11967 if (InsertPos)
11968 if (!RemoveNodeFromCSEMaps(N))
11969 InsertPos = nullptr;
11970
11971 // Now we update the operands.
11972 if (N->OperandList[0] != Op1)
11973 N->OperandList[0].set(Op1);
11974 if (N->OperandList[1] != Op2)
11975 N->OperandList[1].set(Op2);
11976
11978 // If this gets put into a CSE map, add it.
11979 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11980 return N;
11981}
11982
11985 SDValue Ops[] = { Op1, Op2, Op3 };
11986 return UpdateNodeOperands(N, Ops);
11987}
11988
11991 SDValue Op3, SDValue Op4) {
11992 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11993 return UpdateNodeOperands(N, Ops);
11994}
11995
11998 SDValue Op3, SDValue Op4, SDValue Op5) {
11999 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
12000 return UpdateNodeOperands(N, Ops);
12001}
12002
12005 unsigned NumOps = Ops.size();
12006 assert(N->getNumOperands() == NumOps &&
12007 "Update with wrong number of operands");
12008
12009 // If no operands changed just return the input node.
12010 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
12011 return N;
12012
12013 // See if the modified node already exists.
12014 void *InsertPos = nullptr;
12015 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
12016 return Existing;
12017
12018 // Nope it doesn't. Remove the node from its current place in the maps.
12019 if (InsertPos)
12020 if (!RemoveNodeFromCSEMaps(N))
12021 InsertPos = nullptr;
12022
12023 // Now we update the operands.
12024 for (unsigned i = 0; i != NumOps; ++i)
12025 if (N->OperandList[i] != Ops[i])
12026 N->OperandList[i].set(Ops[i]);
12027
12029 // If this gets put into a CSE map, add it.
12030 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
12031 return N;
12032}
12033
12034/// DropOperands - Release the operands and set this node to have
12035/// zero operands.
12037 // Unlike the code in MorphNodeTo that does this, we don't need to
12038 // watch for dead nodes here.
12039 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
12040 SDUse &Use = *I++;
12041 Use.set(SDValue());
12042 }
12043}
12044
12046 ArrayRef<MachineMemOperand *> NewMemRefs) {
12047 if (NewMemRefs.empty()) {
12048 N->clearMemRefs();
12049 return;
12050 }
12051
12052 // Check if we can avoid allocating by storing a single reference directly.
12053 if (NewMemRefs.size() == 1) {
12054 N->MemRefs = NewMemRefs[0];
12055 N->NumMemRefs = 1;
12056 return;
12057 }
12058
12059 MachineMemOperand **MemRefsBuffer =
12060 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
12061 llvm::copy(NewMemRefs, MemRefsBuffer);
12062 N->MemRefs = MemRefsBuffer;
12063 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
12064}
12065
12066/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
12067/// machine opcode.
12068///
12070 EVT VT) {
12071 SDVTList VTs = getVTList(VT);
12072 return SelectNodeTo(N, MachineOpc, VTs, {});
12073}
12074
12076 EVT VT, SDValue Op1) {
12077 SDVTList VTs = getVTList(VT);
12078 SDValue Ops[] = { Op1 };
12079 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12080}
12081
12083 EVT VT, SDValue Op1,
12084 SDValue Op2) {
12085 SDVTList VTs = getVTList(VT);
12086 SDValue Ops[] = { Op1, Op2 };
12087 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12088}
12089
12091 EVT VT, SDValue Op1,
12092 SDValue Op2, SDValue Op3) {
12093 SDVTList VTs = getVTList(VT);
12094 SDValue Ops[] = { Op1, Op2, Op3 };
12095 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12096}
12097
12100 SDVTList VTs = getVTList(VT);
12101 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12102}
12103
12105 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
12106 SDVTList VTs = getVTList(VT1, VT2);
12107 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12108}
12109
12111 EVT VT1, EVT VT2) {
12112 SDVTList VTs = getVTList(VT1, VT2);
12113 return SelectNodeTo(N, MachineOpc, VTs, {});
12114}
12115
12117 EVT VT1, EVT VT2, EVT VT3,
12119 SDVTList VTs = getVTList(VT1, VT2, VT3);
12120 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12121}
12122
12124 EVT VT1, EVT VT2,
12125 SDValue Op1, SDValue Op2) {
12126 SDVTList VTs = getVTList(VT1, VT2);
12127 SDValue Ops[] = { Op1, Op2 };
12128 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12129}
12130
12133 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
12134 // Reset the NodeID to -1.
12135 New->setNodeId(-1);
12136 if (New != N) {
12137 ReplaceAllUsesWith(N, New);
12139 }
12140 return New;
12141}
12142
12143/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
12144/// the line number information on the merged node since it is not possible to
12145/// preserve the information that operation is associated with multiple lines.
12146/// This will make the debugger working better at -O0, were there is a higher
12147/// probability having other instructions associated with that line.
12148///
12149/// For IROrder, we keep the smaller of the two
12150SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
12151 DebugLoc NLoc = N->getDebugLoc();
12152 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
12153 N->setDebugLoc(DebugLoc());
12154 }
12155 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
12156 N->setIROrder(Order);
12157 return N;
12158}
12159
12160/// MorphNodeTo - This *mutates* the specified node to have the specified
12161/// return type, opcode, and operands.
12162///
12163/// Note that MorphNodeTo returns the resultant node. If there is already a
12164/// node of the specified opcode and operands, it returns that node instead of
12165/// the current one. Note that the SDLoc need not be the same.
12166///
12167/// Using MorphNodeTo is faster than creating a new node and swapping it in
12168/// with ReplaceAllUsesWith both because it often avoids allocating a new
12169/// node, and because it doesn't require CSE recalculation for any of
12170/// the node's users.
12171///
12172/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
12173/// As a consequence it isn't appropriate to use from within the DAG combiner or
12174/// the legalizer which maintain worklists that would need to be updated when
12175/// deleting things.
12178 // If an identical node already exists, use it.
12179 void *IP = nullptr;
12180 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
12182 AddNodeIDNode(ID, Opc, VTs, Ops);
12183 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
12184 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
12185 }
12186
12187 if (!RemoveNodeFromCSEMaps(N))
12188 IP = nullptr;
12189
12190 // Start the morphing.
12191 N->NodeType = Opc;
12192 N->ValueList = VTs.VTs;
12193 N->NumValues = VTs.NumVTs;
12194
12195 // Clear the operands list, updating used nodes to remove this from their
12196 // use list. Keep track of any operands that become dead as a result.
12197 SmallPtrSet<SDNode*, 16> DeadNodeSet;
12198 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
12199 SDUse &Use = *I++;
12200 SDNode *Used = Use.getNode();
12201 Use.set(SDValue());
12202 if (Used->use_empty())
12203 DeadNodeSet.insert(Used);
12204 }
12205
12206 // For MachineNode, initialize the memory references information.
12208 MN->clearMemRefs();
12209
12210 // Swap for an appropriately sized array from the recycler.
12211 removeOperands(N);
12212 createOperands(N, Ops);
12213
12214 // Delete any nodes that are still dead after adding the uses for the
12215 // new operands.
12216 if (!DeadNodeSet.empty()) {
12217 SmallVector<SDNode *, 16> DeadNodes;
12218 for (SDNode *N : DeadNodeSet)
12219 if (N->use_empty())
12220 DeadNodes.push_back(N);
12221 RemoveDeadNodes(DeadNodes);
12222 }
12223
12224 if (IP)
12225 CSEMap.InsertNode(N, IP); // Memoize the new node.
12226 return N;
12227}
12228
12230 unsigned OrigOpc = Node->getOpcode();
12231 unsigned NewOpc;
12232 switch (OrigOpc) {
12233 default:
12234 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
12235#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
12236 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
12237#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
12238 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
12239#include "llvm/IR/ConstrainedOps.def"
12240 }
12241
12242 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
12243
12244 // We're taking this node out of the chain, so we need to re-link things.
12245 SDValue InputChain = Node->getOperand(0);
12246 SDValue OutputChain = SDValue(Node, 1);
12247 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
12248
12250 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
12251 Ops.push_back(Node->getOperand(i));
12252
12253 SDVTList VTs = getVTList(Node->getValueType(0));
12254 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
12255
12256 // MorphNodeTo can operate in two ways: if an existing node with the
12257 // specified operands exists, it can just return it. Otherwise, it
12258 // updates the node in place to have the requested operands.
12259 if (Res == Node) {
12260 // If we updated the node in place, reset the node ID. To the isel,
12261 // this should be just like a newly allocated machine node.
12262 Res->setNodeId(-1);
12263 } else {
12266 }
12267
12268 return Res;
12269}
12270
12271/// getMachineNode - These are used for target selectors to create a new node
12272/// with specified return type(s), MachineInstr opcode, and operands.
12273///
12274/// Note that getMachineNode returns the resultant node. If there is already a
12275/// node of the specified opcode and operands, it returns that node instead of
12276/// the current one.
12278 EVT VT) {
12279 SDVTList VTs = getVTList(VT);
12280 return getMachineNode(Opcode, dl, VTs, {});
12281}
12282
12284 EVT VT, SDValue Op1) {
12285 SDVTList VTs = getVTList(VT);
12286 SDValue Ops[] = { Op1 };
12287 return getMachineNode(Opcode, dl, VTs, Ops);
12288}
12289
12291 EVT VT, SDValue Op1, SDValue Op2) {
12292 SDVTList VTs = getVTList(VT);
12293 SDValue Ops[] = { Op1, Op2 };
12294 return getMachineNode(Opcode, dl, VTs, Ops);
12295}
12296
12298 EVT VT, SDValue Op1, SDValue Op2,
12299 SDValue Op3) {
12300 SDVTList VTs = getVTList(VT);
12301 SDValue Ops[] = { Op1, Op2, Op3 };
12302 return getMachineNode(Opcode, dl, VTs, Ops);
12303}
12304
12307 SDVTList VTs = getVTList(VT);
12308 return getMachineNode(Opcode, dl, VTs, Ops);
12309}
12310
12312 EVT VT1, EVT VT2, SDValue Op1,
12313 SDValue Op2) {
12314 SDVTList VTs = getVTList(VT1, VT2);
12315 SDValue Ops[] = { Op1, Op2 };
12316 return getMachineNode(Opcode, dl, VTs, Ops);
12317}
12318
12320 EVT VT1, EVT VT2, SDValue Op1,
12321 SDValue Op2, SDValue Op3) {
12322 SDVTList VTs = getVTList(VT1, VT2);
12323 SDValue Ops[] = { Op1, Op2, Op3 };
12324 return getMachineNode(Opcode, dl, VTs, Ops);
12325}
12326
12328 EVT VT1, EVT VT2,
12330 SDVTList VTs = getVTList(VT1, VT2);
12331 return getMachineNode(Opcode, dl, VTs, Ops);
12332}
12333
12335 EVT VT1, EVT VT2, EVT VT3,
12336 SDValue Op1, SDValue Op2) {
12337 SDVTList VTs = getVTList(VT1, VT2, VT3);
12338 SDValue Ops[] = { Op1, Op2 };
12339 return getMachineNode(Opcode, dl, VTs, Ops);
12340}
12341
12343 EVT VT1, EVT VT2, EVT VT3,
12344 SDValue Op1, SDValue Op2,
12345 SDValue Op3) {
12346 SDVTList VTs = getVTList(VT1, VT2, VT3);
12347 SDValue Ops[] = { Op1, Op2, Op3 };
12348 return getMachineNode(Opcode, dl, VTs, Ops);
12349}
12350
12352 EVT VT1, EVT VT2, EVT VT3,
12354 SDVTList VTs = getVTList(VT1, VT2, VT3);
12355 return getMachineNode(Opcode, dl, VTs, Ops);
12356}
12357
12359 ArrayRef<EVT> ResultTys,
12361 SDVTList VTs = getVTList(ResultTys);
12362 return getMachineNode(Opcode, dl, VTs, Ops);
12363}
12364
12366 SDVTList VTs,
12368 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
12370 void *IP = nullptr;
12371
12372 if (DoCSE) {
12374 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
12375 IP = nullptr;
12376 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
12377 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
12378 }
12379 }
12380
12381 // Allocate a new MachineSDNode.
12382 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
12383 createOperands(N, Ops);
12384
12385 if (DoCSE)
12386 CSEMap.InsertNode(N, IP);
12387
12388 InsertNode(N);
12389 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
12390 return N;
12391}
12392
12393/// getTargetExtractSubreg - A convenience function for creating
12394/// TargetOpcode::EXTRACT_SUBREG nodes.
12396 SDValue Operand) {
12397 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12398 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
12399 VT, Operand, SRIdxVal);
12400 return SDValue(Subreg, 0);
12401}
12402
12403/// getTargetInsertSubreg - A convenience function for creating
12404/// TargetOpcode::INSERT_SUBREG nodes.
12406 SDValue Operand, SDValue Subreg) {
12407 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12408 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
12409 VT, Operand, Subreg, SRIdxVal);
12410 return SDValue(Result, 0);
12411}
12412
12413/// getNodeIfExists - Get the specified node if it's already available, or
12414/// else return NULL.
12417 bool AllowCommute) {
12418 SDNodeFlags Flags;
12419 if (Inserter)
12420 Flags = Inserter->getFlags();
12421 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
12422}
12423
12426 const SDNodeFlags Flags,
12427 bool AllowCommute) {
12428 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
12429 return nullptr;
12430
12431 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
12433 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
12434 void *IP = nullptr;
12435 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
12436 E->intersectFlagsWith(Flags);
12437 return E;
12438 }
12439 return nullptr;
12440 };
12441
12442 if (SDNode *Existing = Lookup(Ops))
12443 return Existing;
12444
12445 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
12446 return Lookup({Ops[1], Ops[0]});
12447
12448 return nullptr;
12449}
12450
12451/// doesNodeExist - Check if a node exists without modifying its flags.
12452bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
12454 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
12456 AddNodeIDNode(ID, Opcode, VTList, Ops);
12457 void *IP = nullptr;
12458 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
12459 return true;
12460 }
12461 return false;
12462}
12463
12464/// getDbgValue - Creates a SDDbgValue node.
12465///
12466/// SDNode
12468 SDNode *N, unsigned R, bool IsIndirect,
12469 const DebugLoc &DL, unsigned O) {
12470 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12471 "Expected inlined-at fields to agree");
12472 return new (DbgInfo->getAlloc())
12473 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
12474 {}, IsIndirect, DL, O,
12475 /*IsVariadic=*/false);
12476}
12477
12478/// Constant
12480 DIExpression *Expr,
12481 const Value *C,
12482 const DebugLoc &DL, unsigned O) {
12483 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12484 "Expected inlined-at fields to agree");
12485 return new (DbgInfo->getAlloc())
12486 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
12487 /*IsIndirect=*/false, DL, O,
12488 /*IsVariadic=*/false);
12489}
12490
12491/// FrameIndex
12493 DIExpression *Expr, unsigned FI,
12494 bool IsIndirect,
12495 const DebugLoc &DL,
12496 unsigned O) {
12497 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12498 "Expected inlined-at fields to agree");
12499 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
12500}
12501
12502/// FrameIndex with dependencies
12504 DIExpression *Expr, unsigned FI,
12505 ArrayRef<SDNode *> Dependencies,
12506 bool IsIndirect,
12507 const DebugLoc &DL,
12508 unsigned O) {
12509 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12510 "Expected inlined-at fields to agree");
12511 return new (DbgInfo->getAlloc())
12512 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
12513 Dependencies, IsIndirect, DL, O,
12514 /*IsVariadic=*/false);
12515}
12516
12517/// VReg
12519 Register VReg, bool IsIndirect,
12520 const DebugLoc &DL, unsigned O) {
12521 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12522 "Expected inlined-at fields to agree");
12523 return new (DbgInfo->getAlloc())
12524 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
12525 {}, IsIndirect, DL, O,
12526 /*IsVariadic=*/false);
12527}
12528
12531 ArrayRef<SDNode *> Dependencies,
12532 bool IsIndirect, const DebugLoc &DL,
12533 unsigned O, bool IsVariadic) {
12534 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12535 "Expected inlined-at fields to agree");
12536 return new (DbgInfo->getAlloc())
12537 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
12538 DL, O, IsVariadic);
12539}
12540
12542 unsigned OffsetInBits, unsigned SizeInBits,
12543 bool InvalidateDbg) {
12544 SDNode *FromNode = From.getNode();
12545 SDNode *ToNode = To.getNode();
12546 assert(FromNode && ToNode && "Can't modify dbg values");
12547
12548 // PR35338
12549 // TODO: assert(From != To && "Redundant dbg value transfer");
12550 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12551 if (From == To || FromNode == ToNode)
12552 return;
12553
12554 if (!FromNode->getHasDebugValue())
12555 return;
12556
12557 SDDbgOperand FromLocOp =
12558 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12560
12562 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12563 if (Dbg->isInvalidated())
12564 continue;
12565
12566 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12567
12568 // Create a new location ops vector that is equal to the old vector, but
12569 // with each instance of FromLocOp replaced with ToLocOp.
12570 bool Changed = false;
12571 auto NewLocOps = Dbg->copyLocationOps();
12572 std::replace_if(
12573 NewLocOps.begin(), NewLocOps.end(),
12574 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12575 bool Match = Op == FromLocOp;
12576 Changed |= Match;
12577 return Match;
12578 },
12579 ToLocOp);
12580 // Ignore this SDDbgValue if we didn't find a matching location.
12581 if (!Changed)
12582 continue;
12583
12584 DIVariable *Var = Dbg->getVariable();
12585 auto *Expr = Dbg->getExpression();
12586 // If a fragment is requested, update the expression.
12587 if (SizeInBits) {
12588 // When splitting a larger (e.g., sign-extended) value whose
12589 // lower bits are described with an SDDbgValue, do not attempt
12590 // to transfer the SDDbgValue to the upper bits.
12591 if (auto FI = Expr->getFragmentInfo())
12592 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12593 continue;
12594 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12595 SizeInBits);
12596 if (!Fragment)
12597 continue;
12598 Expr = *Fragment;
12599 }
12600
12601 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12602 // Clone the SDDbgValue and move it to To.
12603 SDDbgValue *Clone = getDbgValueList(
12604 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12605 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12606 Dbg->isVariadic());
12607 ClonedDVs.push_back(Clone);
12608
12609 if (InvalidateDbg) {
12610 // Invalidate value and indicate the SDDbgValue should not be emitted.
12611 Dbg->setIsInvalidated();
12612 Dbg->setIsEmitted();
12613 }
12614 }
12615
12616 for (SDDbgValue *Dbg : ClonedDVs) {
12617 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12618 "Transferred DbgValues should depend on the new SDNode");
12619 AddDbgValue(Dbg, false);
12620 }
12621}
12622
12624 if (!N.getHasDebugValue())
12625 return;
12626
12627 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12628 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12629 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12630 return SDDbgOperand::fromNode(Node, ResNo);
12631 };
12632
12634 for (auto *DV : GetDbgValues(&N)) {
12635 if (DV->isInvalidated())
12636 continue;
12637 switch (N.getOpcode()) {
12638 default:
12639 break;
12640 case ISD::ADD: {
12641 SDValue N0 = N.getOperand(0);
12642 SDValue N1 = N.getOperand(1);
12643 if (!isa<ConstantSDNode>(N0)) {
12644 bool RHSConstant = isa<ConstantSDNode>(N1);
12646 if (RHSConstant)
12647 Offset = N.getConstantOperandVal(1);
12648 // We are not allowed to turn indirect debug values variadic, so
12649 // don't salvage those.
12650 if (!RHSConstant && DV->isIndirect())
12651 continue;
12652
12653 // Rewrite an ADD constant node into a DIExpression. Since we are
12654 // performing arithmetic to compute the variable's *value* in the
12655 // DIExpression, we need to mark the expression with a
12656 // DW_OP_stack_value.
12657 auto *DIExpr = DV->getExpression();
12658 auto NewLocOps = DV->copyLocationOps();
12659 bool Changed = false;
12660 size_t OrigLocOpsSize = NewLocOps.size();
12661 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12662 // We're not given a ResNo to compare against because the whole
12663 // node is going away. We know that any ISD::ADD only has one
12664 // result, so we can assume any node match is using the result.
12665 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12666 NewLocOps[i].getSDNode() != &N)
12667 continue;
12668 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12669 if (RHSConstant) {
12672 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12673 } else {
12674 // Convert to a variadic expression (if not already).
12675 // convertToVariadicExpression() returns a const pointer, so we use
12676 // a temporary const variable here.
12677 const auto *TmpDIExpr =
12681 ExprOps.push_back(NewLocOps.size());
12682 ExprOps.push_back(dwarf::DW_OP_plus);
12683 SDDbgOperand RHS =
12685 NewLocOps.push_back(RHS);
12686 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12687 }
12688 Changed = true;
12689 }
12690 (void)Changed;
12691 assert(Changed && "Salvage target doesn't use N");
12692
12693 bool IsVariadic =
12694 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12695
12696 auto AdditionalDependencies = DV->getAdditionalDependencies();
12697 SDDbgValue *Clone = getDbgValueList(
12698 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12699 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12700 ClonedDVs.push_back(Clone);
12701 DV->setIsInvalidated();
12702 DV->setIsEmitted();
12703 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12704 N0.getNode()->dumprFull(this);
12705 dbgs() << " into " << *DIExpr << '\n');
12706 }
12707 break;
12708 }
12709 case ISD::TRUNCATE: {
12710 SDValue N0 = N.getOperand(0);
12711 TypeSize FromSize = N0.getValueSizeInBits();
12712 TypeSize ToSize = N.getValueSizeInBits(0);
12713
12714 DIExpression *DbgExpression = DV->getExpression();
12715 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12716 auto NewLocOps = DV->copyLocationOps();
12717 bool Changed = false;
12718 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12719 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12720 NewLocOps[i].getSDNode() != &N)
12721 continue;
12722
12723 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12724 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12725 Changed = true;
12726 }
12727 assert(Changed && "Salvage target doesn't use N");
12728 (void)Changed;
12729
12730 SDDbgValue *Clone =
12731 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12732 DV->getAdditionalDependencies(), DV->isIndirect(),
12733 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12734
12735 ClonedDVs.push_back(Clone);
12736 DV->setIsInvalidated();
12737 DV->setIsEmitted();
12738 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12739 dbgs() << " into " << *DbgExpression << '\n');
12740 break;
12741 }
12742 }
12743 }
12744
12745 for (SDDbgValue *Dbg : ClonedDVs) {
12746 assert((!Dbg->getSDNodes().empty() ||
12747 llvm::any_of(Dbg->getLocationOps(),
12748 [&](const SDDbgOperand &Op) {
12749 return Op.getKind() == SDDbgOperand::FRAMEIX;
12750 })) &&
12751 "Salvaged DbgValue should depend on a new SDNode");
12752 AddDbgValue(Dbg, false);
12753 }
12754}
12755
12756/// Creates a SDDbgLabel node.
12758 const DebugLoc &DL, unsigned O) {
12759 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12760 "Expected inlined-at fields to agree");
12761 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12762}
12763
12764namespace {
12765
12766/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12767/// pointed to by a use iterator is deleted, increment the use iterator
12768/// so that it doesn't dangle.
12769///
12770class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12773
12774 void NodeDeleted(SDNode *N, SDNode *E) override {
12775 // Increment the iterator as needed.
12776 while (UI != UE && N == UI->getUser())
12777 ++UI;
12778 }
12779
12780public:
12781 RAUWUpdateListener(SelectionDAG &d,
12784 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12785};
12786
12787} // end anonymous namespace
12788
12789/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12790/// This can cause recursive merging of nodes in the DAG.
12791///
12792/// This version assumes From has a single result value.
12793///
12795 SDNode *From = FromN.getNode();
12796 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12797 "Cannot replace with this method!");
12798 assert(From != To.getNode() && "Cannot replace uses of with self");
12799
12800 // Preserve Debug Values
12801 transferDbgValues(FromN, To);
12802 // Preserve extra info.
12803 copyExtraInfo(From, To.getNode());
12804
12805 // Iterate over all the existing uses of From. New uses will be added
12806 // to the beginning of the use list, which we avoid visiting.
12807 // This specifically avoids visiting uses of From that arise while the
12808 // replacement is happening, because any such uses would be the result
12809 // of CSE: If an existing node looks like From after one of its operands
12810 // is replaced by To, we don't want to replace of all its users with To
12811 // too. See PR3018 for more info.
12812 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12813 RAUWUpdateListener Listener(*this, UI, UE);
12814 while (UI != UE) {
12815 SDNode *User = UI->getUser();
12816
12817 // This node is about to morph, remove its old self from the CSE maps.
12818 RemoveNodeFromCSEMaps(User);
12819
12820 // A user can appear in a use list multiple times, and when this
12821 // happens the uses are usually next to each other in the list.
12822 // To help reduce the number of CSE recomputations, process all
12823 // the uses of this user that we can find this way.
12824 do {
12825 SDUse &Use = *UI;
12826 ++UI;
12827 Use.set(To);
12828 if (To->isDivergent() != From->isDivergent())
12830 } while (UI != UE && UI->getUser() == User);
12831 // Now that we have modified User, add it back to the CSE maps. If it
12832 // already exists there, recursively merge the results together.
12833 AddModifiedNodeToCSEMaps(User);
12834 }
12835
12836 // If we just RAUW'd the root, take note.
12837 if (FromN == getRoot())
12838 setRoot(To);
12839}
12840
12841/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12842/// This can cause recursive merging of nodes in the DAG.
12843///
12844/// This version assumes that for each value of From, there is a
12845/// corresponding value in To in the same position with the same type.
12846///
12848#ifndef NDEBUG
12849 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12850 assert((!From->hasAnyUseOfValue(i) ||
12851 From->getValueType(i) == To->getValueType(i)) &&
12852 "Cannot use this version of ReplaceAllUsesWith!");
12853#endif
12854
12855 // Handle the trivial case.
12856 if (From == To)
12857 return;
12858
12859 // Preserve Debug Info. Only do this if there's a use.
12860 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12861 if (From->hasAnyUseOfValue(i)) {
12862 assert((i < To->getNumValues()) && "Invalid To location");
12863 transferDbgValues(SDValue(From, i), SDValue(To, i));
12864 }
12865 // Preserve extra info.
12866 copyExtraInfo(From, To);
12867
12868 // Iterate over just the existing users of From. See the comments in
12869 // the ReplaceAllUsesWith above.
12870 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12871 RAUWUpdateListener Listener(*this, UI, UE);
12872 while (UI != UE) {
12873 SDNode *User = UI->getUser();
12874
12875 // This node is about to morph, remove its old self from the CSE maps.
12876 RemoveNodeFromCSEMaps(User);
12877
12878 // A user can appear in a use list multiple times, and when this
12879 // happens the uses are usually next to each other in the list.
12880 // To help reduce the number of CSE recomputations, process all
12881 // the uses of this user that we can find this way.
12882 do {
12883 SDUse &Use = *UI;
12884 ++UI;
12885 Use.setNode(To);
12886 if (To->isDivergent() != From->isDivergent())
12888 } while (UI != UE && UI->getUser() == User);
12889
12890 // Now that we have modified User, add it back to the CSE maps. If it
12891 // already exists there, recursively merge the results together.
12892 AddModifiedNodeToCSEMaps(User);
12893 }
12894
12895 // If we just RAUW'd the root, take note.
12896 if (From == getRoot().getNode())
12897 setRoot(SDValue(To, getRoot().getResNo()));
12898}
12899
12900/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12901/// This can cause recursive merging of nodes in the DAG.
12902///
12903/// This version can replace From with any result values. To must match the
12904/// number and types of values returned by From.
12906 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12907 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12908
12909 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12910 // Preserve Debug Info.
12911 transferDbgValues(SDValue(From, i), To[i]);
12912 // Preserve extra info.
12913 copyExtraInfo(From, To[i].getNode());
12914 }
12915
12916 // Iterate over just the existing users of From. See the comments in
12917 // the ReplaceAllUsesWith above.
12918 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12919 RAUWUpdateListener Listener(*this, UI, UE);
12920 while (UI != UE) {
12921 SDNode *User = UI->getUser();
12922
12923 // This node is about to morph, remove its old self from the CSE maps.
12924 RemoveNodeFromCSEMaps(User);
12925
12926 // A user can appear in a use list multiple times, and when this happens the
12927 // uses are usually next to each other in the list. To help reduce the
12928 // number of CSE and divergence recomputations, process all the uses of this
12929 // user that we can find this way.
12930 bool To_IsDivergent = false;
12931 do {
12932 SDUse &Use = *UI;
12933 const SDValue &ToOp = To[Use.getResNo()];
12934 ++UI;
12935 Use.set(ToOp);
12936 if (ToOp.getValueType() != MVT::Other)
12937 To_IsDivergent |= ToOp->isDivergent();
12938 } while (UI != UE && UI->getUser() == User);
12939
12940 if (To_IsDivergent != From->isDivergent())
12942
12943 // Now that we have modified User, add it back to the CSE maps. If it
12944 // already exists there, recursively merge the results together.
12945 AddModifiedNodeToCSEMaps(User);
12946 }
12947
12948 // If we just RAUW'd the root, take note.
12949 if (From == getRoot().getNode())
12950 setRoot(SDValue(To[getRoot().getResNo()]));
12951}
12952
12953/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12954/// uses of other values produced by From.getNode() alone. The Deleted
12955/// vector is handled the same way as for ReplaceAllUsesWith.
12957 // Handle the really simple, really trivial case efficiently.
12958 if (From == To) return;
12959
12960 // Handle the simple, trivial, case efficiently.
12961 if (From.getNode()->getNumValues() == 1) {
12962 ReplaceAllUsesWith(From, To);
12963 return;
12964 }
12965
12966 // Preserve Debug Info.
12967 transferDbgValues(From, To);
12968 copyExtraInfo(From.getNode(), To.getNode());
12969
12970 // Iterate over just the existing users of From. See the comments in
12971 // the ReplaceAllUsesWith above.
12972 SDNode::use_iterator UI = From.getNode()->use_begin(),
12973 UE = From.getNode()->use_end();
12974 RAUWUpdateListener Listener(*this, UI, UE);
12975 while (UI != UE) {
12976 SDNode *User = UI->getUser();
12977 bool UserRemovedFromCSEMaps = false;
12978
12979 // A user can appear in a use list multiple times, and when this
12980 // happens the uses are usually next to each other in the list.
12981 // To help reduce the number of CSE recomputations, process all
12982 // the uses of this user that we can find this way.
12983 do {
12984 SDUse &Use = *UI;
12985
12986 // Skip uses of different values from the same node.
12987 if (Use.getResNo() != From.getResNo()) {
12988 ++UI;
12989 continue;
12990 }
12991
12992 // If this node hasn't been modified yet, it's still in the CSE maps,
12993 // so remove its old self from the CSE maps.
12994 if (!UserRemovedFromCSEMaps) {
12995 RemoveNodeFromCSEMaps(User);
12996 UserRemovedFromCSEMaps = true;
12997 }
12998
12999 ++UI;
13000 Use.set(To);
13001 if (To->isDivergent() != From->isDivergent())
13003 } while (UI != UE && UI->getUser() == User);
13004 // We are iterating over all uses of the From node, so if a use
13005 // doesn't use the specific value, no changes are made.
13006 if (!UserRemovedFromCSEMaps)
13007 continue;
13008
13009 // Now that we have modified User, add it back to the CSE maps. If it
13010 // already exists there, recursively merge the results together.
13011 AddModifiedNodeToCSEMaps(User);
13012 }
13013
13014 // If we just RAUW'd the root, take note.
13015 if (From == getRoot())
13016 setRoot(To);
13017}
13018
13019namespace {
13020
13021/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
13022/// to record information about a use.
13023struct UseMemo {
13024 SDNode *User;
13025 unsigned Index;
13026 SDUse *Use;
13027};
13028
13029/// operator< - Sort Memos by User.
13030bool operator<(const UseMemo &L, const UseMemo &R) {
13031 return (intptr_t)L.User < (intptr_t)R.User;
13032}
13033
13034/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
13035/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
13036/// the node already has been taken care of recursively.
13037class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
13038 SmallVectorImpl<UseMemo> &Uses;
13039
13040 void NodeDeleted(SDNode *N, SDNode *E) override {
13041 for (UseMemo &Memo : Uses)
13042 if (Memo.User == N)
13043 Memo.User = nullptr;
13044 }
13045
13046public:
13047 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
13048 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
13049};
13050
13051} // end anonymous namespace
13052
13053/// Return true if a glue output should propagate divergence information.
13055 switch (Node->getOpcode()) {
13056 case ISD::CopyFromReg:
13057 case ISD::CopyToReg:
13058 return false;
13059 default:
13060 return true;
13061 }
13062
13063 llvm_unreachable("covered opcode switch");
13064}
13065
13067 if (TLI->isSDNodeAlwaysUniform(N)) {
13068 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
13069 "Conflicting divergence information!");
13070 return false;
13071 }
13072 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
13073 return true;
13074 for (const auto &Op : N->ops()) {
13075 EVT VT = Op.getValueType();
13076
13077 // Skip Chain. It does not carry divergence.
13078 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
13079 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
13080 return true;
13081 }
13082 return false;
13083}
13084
13086 SmallVector<SDNode *, 16> Worklist(1, N);
13087 do {
13088 N = Worklist.pop_back_val();
13089 bool IsDivergent = calculateDivergence(N);
13090 if (N->SDNodeBits.IsDivergent != IsDivergent) {
13091 N->SDNodeBits.IsDivergent = IsDivergent;
13092 llvm::append_range(Worklist, N->users());
13093 }
13094 } while (!Worklist.empty());
13095}
13096
13097void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
13099 Order.reserve(AllNodes.size());
13100 for (auto &N : allnodes()) {
13101 unsigned NOps = N.getNumOperands();
13102 Degree[&N] = NOps;
13103 if (0 == NOps)
13104 Order.push_back(&N);
13105 }
13106 for (size_t I = 0; I != Order.size(); ++I) {
13107 SDNode *N = Order[I];
13108 for (auto *U : N->users()) {
13109 unsigned &UnsortedOps = Degree[U];
13110 if (0 == --UnsortedOps)
13111 Order.push_back(U);
13112 }
13113 }
13114}
13115
13116#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
13117void SelectionDAG::VerifyDAGDivergence() {
13118 std::vector<SDNode *> TopoOrder;
13119 CreateTopologicalOrder(TopoOrder);
13120 for (auto *N : TopoOrder) {
13121 assert(calculateDivergence(N) == N->isDivergent() &&
13122 "Divergence bit inconsistency detected");
13123 }
13124}
13125#endif
13126
13127/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
13128/// uses of other values produced by From.getNode() alone. The same value
13129/// may appear in both the From and To list. The Deleted vector is
13130/// handled the same way as for ReplaceAllUsesWith.
13132 const SDValue *To,
13133 unsigned Num){
13134 // Handle the simple, trivial case efficiently.
13135 if (Num == 1)
13136 return ReplaceAllUsesOfValueWith(*From, *To);
13137
13138 transferDbgValues(*From, *To);
13139 copyExtraInfo(From->getNode(), To->getNode());
13140
13141 // Read up all the uses and make records of them. This helps
13142 // processing new uses that are introduced during the
13143 // replacement process.
13145 for (unsigned i = 0; i != Num; ++i) {
13146 unsigned FromResNo = From[i].getResNo();
13147 SDNode *FromNode = From[i].getNode();
13148 for (SDUse &Use : FromNode->uses()) {
13149 if (Use.getResNo() == FromResNo) {
13150 UseMemo Memo = {Use.getUser(), i, &Use};
13151 Uses.push_back(Memo);
13152 }
13153 }
13154 }
13155
13156 // Sort the uses, so that all the uses from a given User are together.
13158 RAUOVWUpdateListener Listener(*this, Uses);
13159
13160 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
13161 UseIndex != UseIndexEnd; ) {
13162 // We know that this user uses some value of From. If it is the right
13163 // value, update it.
13164 SDNode *User = Uses[UseIndex].User;
13165 // If the node has been deleted by recursive CSE updates when updating
13166 // another node, then just skip this entry.
13167 if (User == nullptr) {
13168 ++UseIndex;
13169 continue;
13170 }
13171
13172 // This node is about to morph, remove its old self from the CSE maps.
13173 RemoveNodeFromCSEMaps(User);
13174
13175 // The Uses array is sorted, so all the uses for a given User
13176 // are next to each other in the list.
13177 // To help reduce the number of CSE recomputations, process all
13178 // the uses of this user that we can find this way.
13179 do {
13180 unsigned i = Uses[UseIndex].Index;
13181 SDUse &Use = *Uses[UseIndex].Use;
13182 ++UseIndex;
13183
13184 Use.set(To[i]);
13185 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
13186
13187 // Now that we have modified User, add it back to the CSE maps. If it
13188 // already exists there, recursively merge the results together.
13189 AddModifiedNodeToCSEMaps(User);
13190 }
13191}
13192
13193/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
13194/// based on their topological order. It returns the maximum id and a vector
13195/// of the SDNodes* in assigned order by reference.
13197 unsigned DAGSize = 0;
13198
13199 // SortedPos tracks the progress of the algorithm. Nodes before it are
13200 // sorted, nodes after it are unsorted. When the algorithm completes
13201 // it is at the end of the list.
13202 allnodes_iterator SortedPos = allnodes_begin();
13203
13204 // Visit all the nodes. Move nodes with no operands to the front of
13205 // the list immediately. Annotate nodes that do have operands with their
13206 // operand count. Before we do this, the Node Id fields of the nodes
13207 // may contain arbitrary values. After, the Node Id fields for nodes
13208 // before SortedPos will contain the topological sort index, and the
13209 // Node Id fields for nodes At SortedPos and after will contain the
13210 // count of outstanding operands.
13212 checkForCycles(&N, this);
13213 unsigned Degree = N.getNumOperands();
13214 if (Degree == 0) {
13215 // A node with no uses, add it to the result array immediately.
13216 N.setNodeId(DAGSize++);
13217 allnodes_iterator Q(&N);
13218 if (Q != SortedPos)
13219 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
13220 assert(SortedPos != AllNodes.end() && "Overran node list");
13221 ++SortedPos;
13222 } else {
13223 // Temporarily use the Node Id as scratch space for the degree count.
13224 N.setNodeId(Degree);
13225 }
13226 }
13227
13228 // Visit all the nodes. As we iterate, move nodes into sorted order,
13229 // such that by the time the end is reached all nodes will be sorted.
13230 for (SDNode &Node : allnodes()) {
13231 SDNode *N = &Node;
13232 checkForCycles(N, this);
13233 // N is in sorted position, so all its uses have one less operand
13234 // that needs to be sorted.
13235 for (SDNode *P : N->users()) {
13236 unsigned Degree = P->getNodeId();
13237 assert(Degree != 0 && "Invalid node degree");
13238 --Degree;
13239 if (Degree == 0) {
13240 // All of P's operands are sorted, so P may sorted now.
13241 P->setNodeId(DAGSize++);
13242 if (P->getIterator() != SortedPos)
13243 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
13244 assert(SortedPos != AllNodes.end() && "Overran node list");
13245 ++SortedPos;
13246 } else {
13247 // Update P's outstanding operand count.
13248 P->setNodeId(Degree);
13249 }
13250 }
13251 if (Node.getIterator() == SortedPos) {
13252#ifndef NDEBUG
13254 SDNode *S = &*++I;
13255 dbgs() << "Overran sorted position:\n";
13256 S->dumprFull(this); dbgs() << "\n";
13257 dbgs() << "Checking if this is due to cycles\n";
13258 checkForCycles(this, true);
13259#endif
13260 llvm_unreachable(nullptr);
13261 }
13262 }
13263
13264 assert(SortedPos == AllNodes.end() &&
13265 "Topological sort incomplete!");
13266 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
13267 "First node in topological sort is not the entry token!");
13268 assert(AllNodes.front().getNodeId() == 0 &&
13269 "First node in topological sort has non-zero id!");
13270 assert(AllNodes.front().getNumOperands() == 0 &&
13271 "First node in topological sort has operands!");
13272 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
13273 "Last node in topologic sort has unexpected id!");
13274 assert(AllNodes.back().use_empty() &&
13275 "Last node in topologic sort has users!");
13276 assert(DAGSize == allnodes_size() && "Node count mismatch!");
13277 return DAGSize;
13278}
13279
13281 SmallVectorImpl<const SDNode *> &SortedNodes) const {
13282 SortedNodes.clear();
13283 // Node -> remaining number of outstanding operands.
13284 DenseMap<const SDNode *, unsigned> RemainingOperands;
13285
13286 // Put nodes without any operands into SortedNodes first.
13287 for (const SDNode &N : allnodes()) {
13288 checkForCycles(&N, this);
13289 unsigned NumOperands = N.getNumOperands();
13290 if (NumOperands == 0)
13291 SortedNodes.push_back(&N);
13292 else
13293 // Record their total number of outstanding operands.
13294 RemainingOperands[&N] = NumOperands;
13295 }
13296
13297 // A node is pushed into SortedNodes when all of its operands (predecessors in
13298 // the graph) are also in SortedNodes.
13299 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
13300 const SDNode *N = SortedNodes[i];
13301 for (const SDNode *U : N->users()) {
13302 // HandleSDNode is never part of a DAG and therefore has no entry in
13303 // RemainingOperands.
13304 if (U->getOpcode() == ISD::HANDLENODE)
13305 continue;
13306 unsigned &NumRemOperands = RemainingOperands[U];
13307 assert(NumRemOperands && "Invalid number of remaining operands");
13308 --NumRemOperands;
13309 if (!NumRemOperands)
13310 SortedNodes.push_back(U);
13311 }
13312 }
13313
13314 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
13315 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
13316 "First node in topological sort is not the entry token");
13317 assert(SortedNodes.front()->getNumOperands() == 0 &&
13318 "First node in topological sort has operands");
13319}
13320
13321/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
13322/// value is produced by SD.
13323void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
13324 for (SDNode *SD : DB->getSDNodes()) {
13325 if (!SD)
13326 continue;
13327 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
13328 SD->setHasDebugValue(true);
13329 }
13330 DbgInfo->add(DB, isParameter);
13331}
13332
13333void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
13334
13336 SDValue NewMemOpChain) {
13337 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
13338 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
13339 // The new memory operation must have the same position as the old load in
13340 // terms of memory dependency. Create a TokenFactor for the old load and new
13341 // memory operation and update uses of the old load's output chain to use that
13342 // TokenFactor.
13343 if (OldChain == NewMemOpChain || OldChain.use_empty())
13344 return NewMemOpChain;
13345
13346 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
13347 OldChain, NewMemOpChain);
13348 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
13349 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
13350 return TokenFactor;
13351}
13352
13354 SDValue NewMemOp) {
13355 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
13356 SDValue OldChain = SDValue(OldLoad, 1);
13357 SDValue NewMemOpChain = NewMemOp.getValue(1);
13358 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
13359}
13360
13362 Function **OutFunction) {
13363 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
13364
13365 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
13366 auto *Module = MF->getFunction().getParent();
13367 auto *Function = Module->getFunction(Symbol);
13368
13369 if (OutFunction != nullptr)
13370 *OutFunction = Function;
13371
13372 if (Function != nullptr) {
13373 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
13374 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
13375 }
13376
13377 std::string ErrorStr;
13378 raw_string_ostream ErrorFormatter(ErrorStr);
13379 ErrorFormatter << "Undefined external symbol ";
13380 ErrorFormatter << '"' << Symbol << '"';
13381 report_fatal_error(Twine(ErrorStr));
13382}
13383
13384//===----------------------------------------------------------------------===//
13385// SDNode Class
13386//===----------------------------------------------------------------------===//
13387
13390 return Const != nullptr && Const->isZero();
13391}
13392
13394 return V.isUndef() || isNullConstant(V);
13395}
13396
13399 return Const != nullptr && Const->isZero() && !Const->isNegative();
13400}
13401
13404 return Const != nullptr && Const->isAllOnes();
13405}
13406
13409 return Const != nullptr && Const->isOne();
13410}
13411
13414 return Const != nullptr && Const->isMinSignedValue();
13415}
13416
13417bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
13418 unsigned OperandNo) {
13419 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
13420 // TODO: Target-specific opcodes could be added.
13421 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
13422 /*AllowTruncation*/ true)) {
13423 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
13424 switch (Opcode) {
13425 case ISD::ADD:
13426 case ISD::OR:
13427 case ISD::XOR:
13428 case ISD::UMAX:
13429 return Const.isZero();
13430 case ISD::MUL:
13431 return Const.isOne();
13432 case ISD::AND:
13433 case ISD::UMIN:
13434 return Const.isAllOnes();
13435 case ISD::SMAX:
13436 return Const.isMinSignedValue();
13437 case ISD::SMIN:
13438 return Const.isMaxSignedValue();
13439 case ISD::SUB:
13440 case ISD::SHL:
13441 case ISD::SRA:
13442 case ISD::SRL:
13443 return OperandNo == 1 && Const.isZero();
13444 case ISD::UDIV:
13445 case ISD::SDIV:
13446 return OperandNo == 1 && Const.isOne();
13447 }
13448 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
13449 switch (Opcode) {
13450 case ISD::FADD:
13451 return ConstFP->isZero() &&
13452 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
13453 case ISD::FSUB:
13454 return OperandNo == 1 && ConstFP->isZero() &&
13455 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
13456 case ISD::FMUL:
13457 return ConstFP->isExactlyValue(1.0);
13458 case ISD::FDIV:
13459 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
13460 case ISD::FMINNUM:
13461 case ISD::FMAXNUM: {
13462 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13463 EVT VT = V.getValueType();
13464 const fltSemantics &Semantics = VT.getFltSemantics();
13465 APFloat NeutralAF = !Flags.hasNoNaNs()
13466 ? APFloat::getQNaN(Semantics)
13467 : !Flags.hasNoInfs()
13468 ? APFloat::getInf(Semantics)
13469 : APFloat::getLargest(Semantics);
13470 if (Opcode == ISD::FMAXNUM)
13471 NeutralAF.changeSign();
13472
13473 return ConstFP->isExactlyValue(NeutralAF);
13474 }
13475 }
13476 }
13477 return false;
13478}
13479
13481 while (V.getOpcode() == ISD::BITCAST)
13482 V = V.getOperand(0);
13483 return V;
13484}
13485
13487 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
13488 V = V.getOperand(0);
13489 return V;
13490}
13491
13493 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
13494 V = V.getOperand(0);
13495 return V;
13496}
13497
13499 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13500 SDValue InVec = V.getOperand(0);
13501 SDValue EltNo = V.getOperand(2);
13502 EVT VT = InVec.getValueType();
13503 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
13504 if (IndexC && VT.isFixedLengthVector() &&
13505 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
13506 !DemandedElts[IndexC->getZExtValue()]) {
13507 V = InVec;
13508 continue;
13509 }
13510 break;
13511 }
13512 return V;
13513}
13514
13516 while (V.getOpcode() == ISD::TRUNCATE)
13517 V = V.getOperand(0);
13518 return V;
13519}
13520
13521bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
13522 if (V.getOpcode() != ISD::XOR)
13523 return false;
13524 V = peekThroughBitcasts(V.getOperand(1));
13525 unsigned NumBits = V.getScalarValueSizeInBits();
13526 ConstantSDNode *C =
13527 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
13528 return C && (C->getAPIntValue().countr_one() >= NumBits);
13529}
13530
13532 bool AllowTruncation) {
13533 APInt DemandedElts = getDemandAllEltsMask(N);
13534 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
13535}
13536
13538 bool AllowUndefs,
13539 bool AllowTruncation) {
13541 return CN;
13542
13543 // SplatVectors can truncate their operands. Ignore that case here unless
13544 // AllowTruncation is set.
13545 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
13546 EVT VecEltVT = N->getValueType(0).getVectorElementType();
13547 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
13548 EVT CVT = CN->getValueType(0);
13549 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13550 if (AllowTruncation || CVT == VecEltVT)
13551 return CN;
13552 }
13553 }
13554
13556 BitVector UndefElements;
13557 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13558
13559 // BuildVectors can truncate their operands. Ignore that case here unless
13560 // AllowTruncation is set.
13561 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13562 if (CN && (UndefElements.none() || AllowUndefs)) {
13563 EVT CVT = CN->getValueType(0);
13564 EVT NSVT = N.getValueType().getScalarType();
13565 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13566 if (AllowTruncation || (CVT == NSVT))
13567 return CN;
13568 }
13569 }
13570
13571 return nullptr;
13572}
13573
13575 APInt DemandedElts = getDemandAllEltsMask(N);
13576 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13577}
13578
13580 const APInt &DemandedElts,
13581 bool AllowUndefs) {
13583 return CN;
13584
13586 BitVector UndefElements;
13587 ConstantFPSDNode *CN =
13588 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13589 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13590 if (CN && (UndefElements.none() || AllowUndefs))
13591 return CN;
13592 }
13593
13594 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13595 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13596 return CN;
13597
13598 return nullptr;
13599}
13600
13601bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13602 // TODO: may want to use peekThroughBitcast() here.
13603 ConstantSDNode *C =
13604 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13605 return C && C->isZero();
13606}
13607
13608bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13609 ConstantSDNode *C =
13610 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13611 return C && C->isOne();
13612}
13613
13614bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13615 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13616 return C && C->isExactlyValue(1.0);
13617}
13618
13619bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13621 unsigned BitWidth = N.getScalarValueSizeInBits();
13622 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13623 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13624}
13625
13626bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13627 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13628 return C && APInt::isSameValue(C->getAPIntValue(),
13629 APInt(C->getAPIntValue().getBitWidth(), 1));
13630}
13631
13632bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13634 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13635 return C && C->isZero();
13636}
13637
13638bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13639 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13640 return C && C->isZero();
13641}
13642
13646
13648 unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt,
13650 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MemRefs(memrefs) {
13651 bool IsVolatile = false;
13652 bool IsNonTemporal = false;
13653 bool IsDereferenceable = true;
13654 bool IsInvariant = true;
13655 for (const MachineMemOperand *MMO : memoperands()) {
13656 IsVolatile |= MMO->isVolatile();
13657 IsNonTemporal |= MMO->isNonTemporal();
13658 IsDereferenceable &= MMO->isDereferenceable();
13659 IsInvariant &= MMO->isInvariant();
13660 }
13661 MemSDNodeBits.IsVolatile = IsVolatile;
13662 MemSDNodeBits.IsNonTemporal = IsNonTemporal;
13663 MemSDNodeBits.IsDereferenceable = IsDereferenceable;
13664 MemSDNodeBits.IsInvariant = IsInvariant;
13665
13666 // For the single-MMO case, we check here that the size of the memory operand
13667 // fits within the size of the MMO. This is because the MMO might indicate
13668 // only a possible address range instead of specifying the affected memory
13669 // addresses precisely.
13672 getMemOperand()->getSize().getValue())) &&
13673 "Size mismatch!");
13674}
13675
13676/// Profile - Gather unique data for the node.
13677///
13679 AddNodeIDNode(ID, this);
13680}
13681
13682namespace {
13683
13684 struct EVTArray {
13685 std::vector<EVT> VTs;
13686
13687 EVTArray() {
13688 VTs.reserve(MVT::VALUETYPE_SIZE);
13689 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13690 VTs.push_back(MVT((MVT::SimpleValueType)i));
13691 }
13692 };
13693
13694} // end anonymous namespace
13695
13696/// getValueTypeList - Return a pointer to the specified value type.
13697///
13698const EVT *SDNode::getValueTypeList(MVT VT) {
13699 static EVTArray SimpleVTArray;
13700
13701 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13702 return &SimpleVTArray.VTs[VT.SimpleTy];
13703}
13704
13705/// hasAnyUseOfValue - Return true if there are any use of the indicated
13706/// value. This method ignores uses of other values defined by this operation.
13707bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13708 assert(Value < getNumValues() && "Bad value!");
13709
13710 for (SDUse &U : uses())
13711 if (U.getResNo() == Value)
13712 return true;
13713
13714 return false;
13715}
13716
13717/// isOnlyUserOf - Return true if this node is the only use of N.
13718bool SDNode::isOnlyUserOf(const SDNode *N) const {
13719 bool Seen = false;
13720 for (const SDNode *User : N->users()) {
13721 if (User == this)
13722 Seen = true;
13723 else
13724 return false;
13725 }
13726
13727 return Seen;
13728}
13729
13730/// Return true if the only users of N are contained in Nodes.
13732 bool Seen = false;
13733 for (const SDNode *User : N->users()) {
13734 if (llvm::is_contained(Nodes, User))
13735 Seen = true;
13736 else
13737 return false;
13738 }
13739
13740 return Seen;
13741}
13742
13743/// Return true if the referenced return value is an operand of N.
13744bool SDValue::isOperandOf(const SDNode *N) const {
13745 return is_contained(N->op_values(), *this);
13746}
13747
13748bool SDNode::isOperandOf(const SDNode *N) const {
13749 return any_of(N->op_values(),
13750 [this](SDValue Op) { return this == Op.getNode(); });
13751}
13752
13753/// reachesChainWithoutSideEffects - Return true if this operand (which must
13754/// be a chain) reaches the specified operand without crossing any
13755/// side-effecting instructions on any chain path. In practice, this looks
13756/// through token factors and non-volatile loads. In order to remain efficient,
13757/// this only looks a couple of nodes in, it does not do an exhaustive search.
13758///
13759/// Note that we only need to examine chains when we're searching for
13760/// side-effects; SelectionDAG requires that all side-effects are represented
13761/// by chains, even if another operand would force a specific ordering. This
13762/// constraint is necessary to allow transformations like splitting loads.
13764 unsigned Depth) const {
13765 if (*this == Dest) return true;
13766
13767 // Don't search too deeply, we just want to be able to see through
13768 // TokenFactor's etc.
13769 if (Depth == 0) return false;
13770
13771 // If this is a token factor, all inputs to the TF happen in parallel.
13772 if (getOpcode() == ISD::TokenFactor) {
13773 // First, try a shallow search.
13774 if (is_contained((*this)->ops(), Dest)) {
13775 // We found the chain we want as an operand of this TokenFactor.
13776 // Essentially, we reach the chain without side-effects if we could
13777 // serialize the TokenFactor into a simple chain of operations with
13778 // Dest as the last operation. This is automatically true if the
13779 // chain has one use: there are no other ordering constraints.
13780 // If the chain has more than one use, we give up: some other
13781 // use of Dest might force a side-effect between Dest and the current
13782 // node.
13783 if (Dest.hasOneUse())
13784 return true;
13785 }
13786 // Next, try a deep search: check whether every operand of the TokenFactor
13787 // reaches Dest.
13788 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13789 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13790 });
13791 }
13792
13793 // Loads don't have side effects, look through them.
13794 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13795 if (Ld->isUnordered())
13796 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13797 }
13798 return false;
13799}
13800
13801bool SDNode::hasPredecessor(const SDNode *N) const {
13804 Worklist.push_back(this);
13805 return hasPredecessorHelper(N, Visited, Worklist);
13806}
13807
13809 this->Flags &= Flags;
13810}
13811
13812SDValue
13814 ArrayRef<ISD::NodeType> CandidateBinOps,
13815 bool AllowPartials) {
13816 // The pattern must end in an extract from index 0.
13817 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13818 !isNullConstant(Extract->getOperand(1)))
13819 return SDValue();
13820
13821 // Match against one of the candidate binary ops.
13822 SDValue Op = Extract->getOperand(0);
13823 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13824 return Op.getOpcode() == unsigned(BinOp);
13825 }))
13826 return SDValue();
13827
13828 // Floating-point reductions may require relaxed constraints on the final step
13829 // of the reduction because they may reorder intermediate operations.
13830 unsigned CandidateBinOp = Op.getOpcode();
13831 if (Op.getValueType().isFloatingPoint()) {
13832 SDNodeFlags Flags = Op->getFlags();
13833 switch (CandidateBinOp) {
13834 case ISD::FADD:
13835 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13836 return SDValue();
13837 break;
13838 default:
13839 llvm_unreachable("Unhandled FP opcode for binop reduction");
13840 }
13841 }
13842
13843 // Matching failed - attempt to see if we did enough stages that a partial
13844 // reduction from a subvector is possible.
13845 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13846 if (!AllowPartials || !Op)
13847 return SDValue();
13848 EVT OpVT = Op.getValueType();
13849 EVT OpSVT = OpVT.getScalarType();
13850 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13851 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13852 return SDValue();
13853 BinOp = (ISD::NodeType)CandidateBinOp;
13854 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13855 };
13856
13857 // At each stage, we're looking for something that looks like:
13858 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13859 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13860 // i32 undef, i32 undef, i32 undef, i32 undef>
13861 // %a = binop <8 x i32> %op, %s
13862 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13863 // we expect something like:
13864 // <4,5,6,7,u,u,u,u>
13865 // <2,3,u,u,u,u,u,u>
13866 // <1,u,u,u,u,u,u,u>
13867 // While a partial reduction match would be:
13868 // <2,3,u,u,u,u,u,u>
13869 // <1,u,u,u,u,u,u,u>
13870 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13871 SDValue PrevOp;
13872 for (unsigned i = 0; i < Stages; ++i) {
13873 unsigned MaskEnd = (1 << i);
13874
13875 if (Op.getOpcode() != CandidateBinOp)
13876 return PartialReduction(PrevOp, MaskEnd);
13877
13878 SDValue Op0 = Op.getOperand(0);
13879 SDValue Op1 = Op.getOperand(1);
13880
13882 if (Shuffle) {
13883 Op = Op1;
13884 } else {
13885 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13886 Op = Op0;
13887 }
13888
13889 // The first operand of the shuffle should be the same as the other operand
13890 // of the binop.
13891 if (!Shuffle || Shuffle->getOperand(0) != Op)
13892 return PartialReduction(PrevOp, MaskEnd);
13893
13894 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13895 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13896 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13897 return PartialReduction(PrevOp, MaskEnd);
13898
13899 PrevOp = Op;
13900 }
13901
13902 // Handle subvector reductions, which tend to appear after the shuffle
13903 // reduction stages.
13904 while (Op.getOpcode() == CandidateBinOp) {
13905 unsigned NumElts = Op.getValueType().getVectorNumElements();
13906 SDValue Op0 = Op.getOperand(0);
13907 SDValue Op1 = Op.getOperand(1);
13908 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13910 Op0.getOperand(0) != Op1.getOperand(0))
13911 break;
13912 SDValue Src = Op0.getOperand(0);
13913 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13914 if (NumSrcElts != (2 * NumElts))
13915 break;
13916 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13917 Op1.getConstantOperandAPInt(1) == NumElts) &&
13918 !(Op1.getConstantOperandAPInt(1) == 0 &&
13919 Op0.getConstantOperandAPInt(1) == NumElts))
13920 break;
13921 Op = Src;
13922 }
13923
13924 BinOp = (ISD::NodeType)CandidateBinOp;
13925 return Op;
13926}
13927
13929 EVT VT = N->getValueType(0);
13930 EVT EltVT = VT.getVectorElementType();
13931 unsigned NE = VT.getVectorNumElements();
13932
13933 SDLoc dl(N);
13934
13935 // If ResNE is 0, fully unroll the vector op.
13936 if (ResNE == 0)
13937 ResNE = NE;
13938 else if (NE > ResNE)
13939 NE = ResNE;
13940
13941 if (N->getNumValues() == 2) {
13942 SmallVector<SDValue, 8> Scalars0, Scalars1;
13943 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13944 EVT VT1 = N->getValueType(1);
13945 EVT EltVT1 = VT1.getVectorElementType();
13946
13947 unsigned i;
13948 for (i = 0; i != NE; ++i) {
13949 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13950 SDValue Operand = N->getOperand(j);
13951 EVT OperandVT = Operand.getValueType();
13952
13953 // A vector operand; extract a single element.
13954 EVT OperandEltVT = OperandVT.getVectorElementType();
13955 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13956 }
13957
13958 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13959 Scalars0.push_back(EltOp);
13960 Scalars1.push_back(EltOp.getValue(1));
13961 }
13962
13963 for (; i < ResNE; ++i) {
13964 Scalars0.push_back(getUNDEF(EltVT));
13965 Scalars1.push_back(getUNDEF(EltVT1));
13966 }
13967
13968 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13969 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13970 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13971 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13972 return getMergeValues({Vec0, Vec1}, dl);
13973 }
13974
13975 assert(N->getNumValues() == 1 &&
13976 "Can't unroll a vector with multiple results!");
13977
13979 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13980
13981 unsigned i;
13982 for (i= 0; i != NE; ++i) {
13983 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13984 SDValue Operand = N->getOperand(j);
13985 EVT OperandVT = Operand.getValueType();
13986 if (OperandVT.isVector()) {
13987 // A vector operand; extract a single element.
13988 EVT OperandEltVT = OperandVT.getVectorElementType();
13989 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13990 } else {
13991 // A scalar operand; just use it as is.
13992 Operands[j] = Operand;
13993 }
13994 }
13995
13996 switch (N->getOpcode()) {
13997 default: {
13998 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13999 N->getFlags()));
14000 break;
14001 }
14002 case ISD::VSELECT:
14003 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
14004 break;
14005 case ISD::SHL:
14006 case ISD::SRA:
14007 case ISD::SRL:
14008 case ISD::ROTL:
14009 case ISD::ROTR:
14010 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
14011 getShiftAmountOperand(Operands[0].getValueType(),
14012 Operands[1])));
14013 break;
14015 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
14016 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
14017 Operands[0],
14018 getValueType(ExtVT)));
14019 break;
14020 }
14021 case ISD::ADDRSPACECAST: {
14022 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
14023 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
14024 ASC->getSrcAddressSpace(),
14025 ASC->getDestAddressSpace()));
14026 break;
14027 }
14028 }
14029 }
14030
14031 for (; i < ResNE; ++i)
14032 Scalars.push_back(getUNDEF(EltVT));
14033
14034 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
14035 return getBuildVector(VecVT, dl, Scalars);
14036}
14037
14038std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
14039 SDNode *N, unsigned ResNE) {
14040 unsigned Opcode = N->getOpcode();
14041 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
14042 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
14043 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
14044 "Expected an overflow opcode");
14045
14046 EVT ResVT = N->getValueType(0);
14047 EVT OvVT = N->getValueType(1);
14048 EVT ResEltVT = ResVT.getVectorElementType();
14049 EVT OvEltVT = OvVT.getVectorElementType();
14050 SDLoc dl(N);
14051
14052 // If ResNE is 0, fully unroll the vector op.
14053 unsigned NE = ResVT.getVectorNumElements();
14054 if (ResNE == 0)
14055 ResNE = NE;
14056 else if (NE > ResNE)
14057 NE = ResNE;
14058
14059 SmallVector<SDValue, 8> LHSScalars;
14060 SmallVector<SDValue, 8> RHSScalars;
14061 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
14062 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
14063
14064 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
14065 SDVTList VTs = getVTList(ResEltVT, SVT);
14066 SmallVector<SDValue, 8> ResScalars;
14067 SmallVector<SDValue, 8> OvScalars;
14068 for (unsigned i = 0; i < NE; ++i) {
14069 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
14070 SDValue Ov =
14071 getSelect(dl, OvEltVT, Res.getValue(1),
14072 getBoolConstant(true, dl, OvEltVT, ResVT),
14073 getConstant(0, dl, OvEltVT));
14074
14075 ResScalars.push_back(Res);
14076 OvScalars.push_back(Ov);
14077 }
14078
14079 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
14080 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
14081
14082 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
14083 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
14084 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
14085 getBuildVector(NewOvVT, dl, OvScalars));
14086}
14087
14090 unsigned Bytes,
14091 int Dist) const {
14092 if (LD->isVolatile() || Base->isVolatile())
14093 return false;
14094 // TODO: probably too restrictive for atomics, revisit
14095 if (!LD->isSimple())
14096 return false;
14097 if (LD->isIndexed() || Base->isIndexed())
14098 return false;
14099 if (LD->getChain() != Base->getChain())
14100 return false;
14101 EVT VT = LD->getMemoryVT();
14102 if (VT.getSizeInBits() / 8 != Bytes)
14103 return false;
14104
14105 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
14106 auto LocDecomp = BaseIndexOffset::match(LD, *this);
14107
14108 int64_t Offset = 0;
14109 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
14110 return (Dist * (int64_t)Bytes == Offset);
14111 return false;
14112}
14113
14114/// InferPtrAlignment - Infer alignment of a load / store address. Return
14115/// std::nullopt if it cannot be inferred.
14117 // If this is a GlobalAddress + cst, return the alignment.
14118 const GlobalValue *GV = nullptr;
14119 int64_t GVOffset = 0;
14120 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
14121 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
14122 KnownBits Known(PtrWidth);
14124 unsigned AlignBits = Known.countMinTrailingZeros();
14125 if (AlignBits)
14126 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
14127 }
14128
14129 // If this is a direct reference to a stack slot, use information about the
14130 // stack slot's alignment.
14131 int FrameIdx = INT_MIN;
14132 int64_t FrameOffset = 0;
14134 FrameIdx = FI->getIndex();
14135 } else if (isBaseWithConstantOffset(Ptr) &&
14137 // Handle FI+Cst
14138 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
14139 FrameOffset = Ptr.getConstantOperandVal(1);
14140 }
14141
14142 if (FrameIdx != INT_MIN) {
14144 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
14145 }
14146
14147 return std::nullopt;
14148}
14149
14150/// Split the scalar node with EXTRACT_ELEMENT using the provided
14151/// VTs and return the low/high part.
14152std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
14153 const SDLoc &DL,
14154 const EVT &LoVT,
14155 const EVT &HiVT) {
14156 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
14157 "Split node must be a scalar type");
14158 SDValue Lo =
14160 SDValue Hi =
14162 return std::make_pair(Lo, Hi);
14163}
14164
14165/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
14166/// which is split (or expanded) into two not necessarily identical pieces.
14167std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
14168 // Currently all types are split in half.
14169 EVT LoVT, HiVT;
14170 if (!VT.isVector())
14171 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
14172 else
14173 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
14174
14175 return std::make_pair(LoVT, HiVT);
14176}
14177
14178/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
14179/// type, dependent on an enveloping VT that has been split into two identical
14180/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
14181std::pair<EVT, EVT>
14183 bool *HiIsEmpty) const {
14184 EVT EltTp = VT.getVectorElementType();
14185 // Examples:
14186 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
14187 // custom VL=9 with enveloping VL=8/8 yields 8/1
14188 // custom VL=10 with enveloping VL=8/8 yields 8/2
14189 // etc.
14190 ElementCount VTNumElts = VT.getVectorElementCount();
14191 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
14192 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
14193 "Mixing fixed width and scalable vectors when enveloping a type");
14194 EVT LoVT, HiVT;
14195 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
14196 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
14197 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
14198 *HiIsEmpty = false;
14199 } else {
14200 // Flag that hi type has zero storage size, but return split envelop type
14201 // (this would be easier if vector types with zero elements were allowed).
14202 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
14203 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
14204 *HiIsEmpty = true;
14205 }
14206 return std::make_pair(LoVT, HiVT);
14207}
14208
14209/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
14210/// low/high part.
14211std::pair<SDValue, SDValue>
14212SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
14213 const EVT &HiVT) {
14214 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
14215 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
14216 "Splitting vector with an invalid mixture of fixed and scalable "
14217 "vector types");
14219 N.getValueType().getVectorMinNumElements() &&
14220 "More vector elements requested than available!");
14221 SDValue Lo, Hi;
14222 Lo = getExtractSubvector(DL, LoVT, N, 0);
14223 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
14224 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
14225 // IDX with the runtime scaling factor of the result vector type. For
14226 // fixed-width result vectors, that runtime scaling factor is 1.
14229 return std::make_pair(Lo, Hi);
14230}
14231
14232std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
14233 const SDLoc &DL) {
14234 // Split the vector length parameter.
14235 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
14236 EVT VT = N.getValueType();
14238 "Expecting the mask to be an evenly-sized vector");
14239 SDValue HalfNumElts = getElementCount(
14241 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
14242 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
14243 return std::make_pair(Lo, Hi);
14244}
14245
14246/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
14248 EVT VT = N.getValueType();
14251 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
14252}
14253
14256 unsigned Start, unsigned Count,
14257 EVT EltVT) {
14258 EVT VT = Op.getValueType();
14259 if (Count == 0)
14261 if (EltVT == EVT())
14262 EltVT = VT.getVectorElementType();
14263 SDLoc SL(Op);
14264 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
14265 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
14266 }
14267}
14268
14269// getAddressSpace - Return the address space this GlobalAddress belongs to.
14271 return getGlobal()->getType()->getAddressSpace();
14272}
14273
14276 return Val.MachineCPVal->getType();
14277 return Val.ConstVal->getType();
14278}
14279
14280bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
14281 unsigned &SplatBitSize,
14282 bool &HasAnyUndefs,
14283 unsigned MinSplatBits,
14284 bool IsBigEndian) const {
14285 EVT VT = getValueType(0);
14286 assert(VT.isVector() && "Expected a vector type");
14287 unsigned VecWidth = VT.getSizeInBits();
14288 if (MinSplatBits > VecWidth)
14289 return false;
14290
14291 // FIXME: The widths are based on this node's type, but build vectors can
14292 // truncate their operands.
14293 SplatValue = APInt(VecWidth, 0);
14294 SplatUndef = APInt(VecWidth, 0);
14295
14296 // Get the bits. Bits with undefined values (when the corresponding element
14297 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
14298 // in SplatValue. If any of the values are not constant, give up and return
14299 // false.
14300 unsigned int NumOps = getNumOperands();
14301 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
14302 unsigned EltWidth = VT.getScalarSizeInBits();
14303
14304 for (unsigned j = 0; j < NumOps; ++j) {
14305 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
14306 SDValue OpVal = getOperand(i);
14307 unsigned BitPos = j * EltWidth;
14308
14309 if (OpVal.isUndef())
14310 SplatUndef.setBits(BitPos, BitPos + EltWidth);
14311 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
14312 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
14313 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
14314 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
14315 else
14316 return false;
14317 }
14318
14319 // The build_vector is all constants or undefs. Find the smallest element
14320 // size that splats the vector.
14321 HasAnyUndefs = (SplatUndef != 0);
14322
14323 // FIXME: This does not work for vectors with elements less than 8 bits.
14324 while (VecWidth > 8) {
14325 // If we can't split in half, stop here.
14326 if (VecWidth & 1)
14327 break;
14328
14329 unsigned HalfSize = VecWidth / 2;
14330 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
14331 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
14332 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
14333 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
14334
14335 // If the two halves do not match (ignoring undef bits), stop here.
14336 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
14337 MinSplatBits > HalfSize)
14338 break;
14339
14340 SplatValue = HighValue | LowValue;
14341 SplatUndef = HighUndef & LowUndef;
14342
14343 VecWidth = HalfSize;
14344 }
14345
14346 // FIXME: The loop above only tries to split in halves. But if the input
14347 // vector for example is <3 x i16> it wouldn't be able to detect a
14348 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
14349 // optimizations. I guess that back in the days when this helper was created
14350 // vectors normally was power-of-2 sized.
14351
14352 SplatBitSize = VecWidth;
14353 return true;
14354}
14355
14357 BitVector *UndefElements) const {
14358 unsigned NumOps = getNumOperands();
14359 if (UndefElements) {
14360 UndefElements->clear();
14361 UndefElements->resize(NumOps);
14362 }
14363 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
14364 if (!DemandedElts)
14365 return SDValue();
14366 SDValue Splatted;
14367 for (unsigned i = 0; i != NumOps; ++i) {
14368 if (!DemandedElts[i])
14369 continue;
14370 SDValue Op = getOperand(i);
14371 if (Op.isUndef()) {
14372 if (UndefElements)
14373 (*UndefElements)[i] = true;
14374 } else if (!Splatted) {
14375 Splatted = Op;
14376 } else if (Splatted != Op) {
14377 return SDValue();
14378 }
14379 }
14380
14381 if (!Splatted) {
14382 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
14383 assert(getOperand(FirstDemandedIdx).isUndef() &&
14384 "Can only have a splat without a constant for all undefs.");
14385 return getOperand(FirstDemandedIdx);
14386 }
14387
14388 return Splatted;
14389}
14390
14392 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14393 return getSplatValue(DemandedElts, UndefElements);
14394}
14395
14397 SmallVectorImpl<SDValue> &Sequence,
14398 BitVector *UndefElements) const {
14399 unsigned NumOps = getNumOperands();
14400 Sequence.clear();
14401 if (UndefElements) {
14402 UndefElements->clear();
14403 UndefElements->resize(NumOps);
14404 }
14405 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
14406 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
14407 return false;
14408
14409 // Set the undefs even if we don't find a sequence (like getSplatValue).
14410 if (UndefElements)
14411 for (unsigned I = 0; I != NumOps; ++I)
14412 if (DemandedElts[I] && getOperand(I).isUndef())
14413 (*UndefElements)[I] = true;
14414
14415 // Iteratively widen the sequence length looking for repetitions.
14416 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
14417 Sequence.append(SeqLen, SDValue());
14418 for (unsigned I = 0; I != NumOps; ++I) {
14419 if (!DemandedElts[I])
14420 continue;
14421 SDValue &SeqOp = Sequence[I % SeqLen];
14423 if (Op.isUndef()) {
14424 if (!SeqOp)
14425 SeqOp = Op;
14426 continue;
14427 }
14428 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
14429 Sequence.clear();
14430 break;
14431 }
14432 SeqOp = Op;
14433 }
14434 if (!Sequence.empty())
14435 return true;
14436 }
14437
14438 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
14439 return false;
14440}
14441
14443 BitVector *UndefElements) const {
14444 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14445 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
14446}
14447
14450 BitVector *UndefElements) const {
14452 getSplatValue(DemandedElts, UndefElements));
14453}
14454
14457 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
14458}
14459
14462 BitVector *UndefElements) const {
14464 getSplatValue(DemandedElts, UndefElements));
14465}
14466
14471
14472int32_t
14474 uint32_t BitWidth) const {
14475 if (ConstantFPSDNode *CN =
14477 bool IsExact;
14478 APSInt IntVal(BitWidth);
14479 const APFloat &APF = CN->getValueAPF();
14480 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
14481 APFloat::opOK ||
14482 !IsExact)
14483 return -1;
14484
14485 return IntVal.exactLogBase2();
14486 }
14487 return -1;
14488}
14489
14491 bool IsLittleEndian, unsigned DstEltSizeInBits,
14492 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
14493 // Early-out if this contains anything but Undef/Constant/ConstantFP.
14494 if (!isConstant())
14495 return false;
14496
14497 unsigned NumSrcOps = getNumOperands();
14498 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
14499 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14500 "Invalid bitcast scale");
14501
14502 // Extract raw src bits.
14503 SmallVector<APInt> SrcBitElements(NumSrcOps,
14504 APInt::getZero(SrcEltSizeInBits));
14505 BitVector SrcUndeElements(NumSrcOps, false);
14506
14507 for (unsigned I = 0; I != NumSrcOps; ++I) {
14509 if (Op.isUndef()) {
14510 SrcUndeElements.set(I);
14511 continue;
14512 }
14513 auto *CInt = dyn_cast<ConstantSDNode>(Op);
14514 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
14515 assert((CInt || CFP) && "Unknown constant");
14516 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
14517 : CFP->getValueAPF().bitcastToAPInt();
14518 }
14519
14520 // Recast to dst width.
14521 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
14522 SrcBitElements, UndefElements, SrcUndeElements);
14523 return true;
14524}
14525
14526void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
14527 unsigned DstEltSizeInBits,
14528 SmallVectorImpl<APInt> &DstBitElements,
14529 ArrayRef<APInt> SrcBitElements,
14530 BitVector &DstUndefElements,
14531 const BitVector &SrcUndefElements) {
14532 unsigned NumSrcOps = SrcBitElements.size();
14533 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
14534 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14535 "Invalid bitcast scale");
14536 assert(NumSrcOps == SrcUndefElements.size() &&
14537 "Vector size mismatch");
14538
14539 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
14540 DstUndefElements.clear();
14541 DstUndefElements.resize(NumDstOps, false);
14542 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
14543
14544 // Concatenate src elements constant bits together into dst element.
14545 if (SrcEltSizeInBits <= DstEltSizeInBits) {
14546 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
14547 for (unsigned I = 0; I != NumDstOps; ++I) {
14548 DstUndefElements.set(I);
14549 APInt &DstBits = DstBitElements[I];
14550 for (unsigned J = 0; J != Scale; ++J) {
14551 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14552 if (SrcUndefElements[Idx])
14553 continue;
14554 DstUndefElements.reset(I);
14555 const APInt &SrcBits = SrcBitElements[Idx];
14556 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
14557 "Illegal constant bitwidths");
14558 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
14559 }
14560 }
14561 return;
14562 }
14563
14564 // Split src element constant bits into dst elements.
14565 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14566 for (unsigned I = 0; I != NumSrcOps; ++I) {
14567 if (SrcUndefElements[I]) {
14568 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14569 continue;
14570 }
14571 const APInt &SrcBits = SrcBitElements[I];
14572 for (unsigned J = 0; J != Scale; ++J) {
14573 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14574 APInt &DstBits = DstBitElements[Idx];
14575 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14576 }
14577 }
14578}
14579
14581 for (const SDValue &Op : op_values()) {
14582 unsigned Opc = Op.getOpcode();
14583 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14584 return false;
14585 }
14586 return true;
14587}
14588
14589std::optional<std::pair<APInt, APInt>>
14591 unsigned NumOps = getNumOperands();
14592 if (NumOps < 2)
14593 return std::nullopt;
14594
14595 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14596 APInt Start, Stride;
14597 int FirstIdx = -1, SecondIdx = -1;
14598
14599 // Find the first two non-undef constant elements to determine Start and
14600 // Stride, then verify all remaining elements match the sequence.
14601 for (unsigned I = 0; I < NumOps; ++I) {
14603 if (Op->isUndef())
14604 continue;
14605 if (!isa<ConstantSDNode>(Op))
14606 return std::nullopt;
14607
14608 APInt Val = getConstantOperandAPInt(I).trunc(EltSize);
14609 if (FirstIdx < 0) {
14610 FirstIdx = I;
14611 Start = Val;
14612 } else if (SecondIdx < 0) {
14613 SecondIdx = I;
14614 // Compute stride using modular arithmetic. Simple division would handle
14615 // common strides (1, 2, -1, etc.), but modular inverse maximizes matches.
14616 // Example: <0, poison, poison, 0xFF> has stride 0x55 since 3*0x55 = 0xFF
14617 // Note that modular arithmetic is agnostic to signed/unsigned.
14618 unsigned IdxDiff = I - FirstIdx;
14619 APInt ValDiff = Val - Start;
14620
14621 // Step 1: Factor out common powers of 2 from IdxDiff and ValDiff.
14622 unsigned CommonPow2Bits = llvm::countr_zero(IdxDiff);
14623 if (ValDiff.countr_zero() < CommonPow2Bits)
14624 return std::nullopt; // ValDiff not divisible by 2^CommonPow2Bits
14625 IdxDiff >>= CommonPow2Bits;
14626 ValDiff.lshrInPlace(CommonPow2Bits);
14627
14628 // Step 2: IdxDiff is now odd, so its inverse mod 2^EltSize exists.
14629 // TODO: There are 2^CommonPow2Bits valid strides; currently we only try
14630 // one, but we could try all candidates to handle more cases.
14631 Stride = ValDiff * APInt(EltSize, IdxDiff).multiplicativeInverse();
14632 if (Stride.isZero())
14633 return std::nullopt;
14634
14635 // Step 3: Adjust Start based on the first defined element's index.
14636 Start -= Stride * FirstIdx;
14637 } else {
14638 // Verify this element matches the sequence.
14639 if (Val != Start + Stride * I)
14640 return std::nullopt;
14641 }
14642 }
14643
14644 // Need at least two defined elements.
14645 if (SecondIdx < 0)
14646 return std::nullopt;
14647
14648 return std::make_pair(Start, Stride);
14649}
14650
14652 // Find the first non-undef value in the shuffle mask.
14653 unsigned i, e;
14654 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14655 /* search */;
14656
14657 // If all elements are undefined, this shuffle can be considered a splat
14658 // (although it should eventually get simplified away completely).
14659 if (i == e)
14660 return true;
14661
14662 // Make sure all remaining elements are either undef or the same as the first
14663 // non-undef value.
14664 for (int Idx = Mask[i]; i != e; ++i)
14665 if (Mask[i] >= 0 && Mask[i] != Idx)
14666 return false;
14667 return true;
14668}
14669
14670// Returns true if it is a constant integer BuildVector or constant integer,
14671// possibly hidden by a bitcast.
14673 SDValue N, bool AllowOpaques) const {
14675
14676 if (auto *C = dyn_cast<ConstantSDNode>(N))
14677 return AllowOpaques || !C->isOpaque();
14678
14680 return true;
14681
14682 // Treat a GlobalAddress supporting constant offset folding as a
14683 // constant integer.
14684 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14685 if (GA->getOpcode() == ISD::GlobalAddress &&
14686 TLI->isOffsetFoldingLegal(GA))
14687 return true;
14688
14689 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14690 isa<ConstantSDNode>(N.getOperand(0)))
14691 return true;
14692 return false;
14693}
14694
14695// Returns true if it is a constant float BuildVector or constant float.
14698 return true;
14699
14701 return true;
14702
14703 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14704 isa<ConstantFPSDNode>(N.getOperand(0)))
14705 return true;
14706
14707 return false;
14708}
14709
14710std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14711 ConstantSDNode *Const =
14712 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14713 if (!Const)
14714 return std::nullopt;
14715
14716 EVT VT = N->getValueType(0);
14717 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14718 switch (TLI->getBooleanContents(N.getValueType())) {
14720 if (CVal.isOne())
14721 return true;
14722 if (CVal.isZero())
14723 return false;
14724 return std::nullopt;
14726 if (CVal.isAllOnes())
14727 return true;
14728 if (CVal.isZero())
14729 return false;
14730 return std::nullopt;
14732 return CVal[0];
14733 }
14734 llvm_unreachable("Unknown BooleanContent enum");
14735}
14736
14737void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14738 assert(!Node->OperandList && "Node already has operands");
14740 "too many operands to fit into SDNode");
14741 SDUse *Ops = OperandRecycler.allocate(
14742 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14743
14744 bool IsDivergent = false;
14745 for (unsigned I = 0; I != Vals.size(); ++I) {
14746 Ops[I].setUser(Node);
14747 Ops[I].setInitial(Vals[I]);
14748 EVT VT = Ops[I].getValueType();
14749
14750 // Skip Chain. It does not carry divergence.
14751 if (VT != MVT::Other &&
14752 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14753 Ops[I].getNode()->isDivergent()) {
14754 IsDivergent = true;
14755 }
14756 }
14757 Node->NumOperands = Vals.size();
14758 Node->OperandList = Ops;
14759 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14760 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14761 Node->SDNodeBits.IsDivergent = IsDivergent;
14762 }
14763 checkForCycles(Node);
14764}
14765
14768 size_t Limit = SDNode::getMaxNumOperands();
14769 while (Vals.size() > Limit) {
14770 unsigned SliceIdx = Vals.size() - Limit;
14771 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14772 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14773 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14774 Vals.emplace_back(NewTF);
14775 }
14776 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14777}
14778
14780 EVT VT, SDNodeFlags Flags) {
14781 switch (Opcode) {
14782 default:
14783 return SDValue();
14784 case ISD::ADD:
14785 case ISD::OR:
14786 case ISD::XOR:
14787 case ISD::UMAX:
14788 return getConstant(0, DL, VT);
14789 case ISD::MUL:
14790 return getConstant(1, DL, VT);
14791 case ISD::AND:
14792 case ISD::UMIN:
14793 return getAllOnesConstant(DL, VT);
14794 case ISD::SMAX:
14796 case ISD::SMIN:
14798 case ISD::FADD:
14799 // If flags allow, prefer positive zero since it's generally cheaper
14800 // to materialize on most targets.
14801 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14802 case ISD::FMUL:
14803 return getConstantFP(1.0, DL, VT);
14804 case ISD::FMINNUM:
14805 case ISD::FMAXNUM: {
14806 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14807 const fltSemantics &Semantics = VT.getFltSemantics();
14808 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14809 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14810 APFloat::getLargest(Semantics);
14811 if (Opcode == ISD::FMAXNUM)
14812 NeutralAF.changeSign();
14813
14814 return getConstantFP(NeutralAF, DL, VT);
14815 }
14816 case ISD::FMINIMUM:
14817 case ISD::FMAXIMUM: {
14818 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14819 const fltSemantics &Semantics = VT.getFltSemantics();
14820 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14821 : APFloat::getLargest(Semantics);
14822 if (Opcode == ISD::FMAXIMUM)
14823 NeutralAF.changeSign();
14824
14825 return getConstantFP(NeutralAF, DL, VT);
14826 }
14827
14828 }
14829}
14830
14831/// Helper used to make a call to a library function that has one argument of
14832/// pointer type.
14833///
14834/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14835/// used to get or set floating-point state. They have one argument of pointer
14836/// type, which points to the memory region containing bits of the
14837/// floating-point state. The value returned by such function is ignored in the
14838/// created call.
14839///
14840/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14841/// \param Ptr Pointer used to save/load state.
14842/// \param InChain Ingoing token chain.
14843/// \returns Outgoing chain token.
14845 SDValue InChain,
14846 const SDLoc &DLoc) {
14847 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14849 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14850 RTLIB::LibcallImpl LibcallImpl =
14851 Libcalls->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
14852 if (LibcallImpl == RTLIB::Unsupported)
14853 reportFatalUsageError("emitting call to unsupported libcall");
14854
14855 SDValue Callee =
14856 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
14858 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14859 Libcalls->getLibcallImplCallingConv(LibcallImpl),
14860 Type::getVoidTy(*getContext()), Callee, std::move(Args));
14861 return TLI->LowerCallTo(CLI).second;
14862}
14863
14865 assert(From && To && "Invalid SDNode; empty source SDValue?");
14866 auto I = SDEI.find(From);
14867 if (I == SDEI.end())
14868 return;
14869
14870 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14871 // the iterator, hence the need to make a copy to prevent a use-after-free.
14872 NodeExtraInfo NEI = I->second;
14873 if (LLVM_LIKELY(!NEI.PCSections)) {
14874 // No deep copy required for the types of extra info set.
14875 //
14876 // FIXME: Investigate if other types of extra info also need deep copy. This
14877 // depends on the types of nodes they can be attached to: if some extra info
14878 // is only ever attached to nodes where a replacement To node is always the
14879 // node where later use and propagation of the extra info has the intended
14880 // semantics, no deep copy is required.
14881 SDEI[To] = std::move(NEI);
14882 return;
14883 }
14884
14885 const SDNode *EntrySDN = getEntryNode().getNode();
14886
14887 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14888 // through the replacement of From with To. Otherwise, replacements of a node
14889 // (From) with more complex nodes (To and its operands) may result in lost
14890 // extra info where the root node (To) is insignificant in further propagating
14891 // and using extra info when further lowering to MIR.
14892 //
14893 // In the first step pre-populate the visited set with the nodes reachable
14894 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14895 // DAG that is not new and should be left untouched.
14896 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14897 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14898 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14899 if (MaxDepth == 0) {
14900 // Remember this node in case we need to increase MaxDepth and continue
14901 // populating FromReach from this node.
14902 Leafs.emplace_back(N);
14903 return;
14904 }
14905 if (!FromReach.insert(N).second)
14906 return;
14907 for (const SDValue &Op : N->op_values())
14908 Self(Self, Op.getNode(), MaxDepth - 1);
14909 };
14910
14911 // Copy extra info to To and all its transitive operands (that are new).
14913 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14914 if (FromReach.contains(N))
14915 return true;
14916 if (!Visited.insert(N).second)
14917 return true;
14918 if (EntrySDN == N)
14919 return false;
14920 for (const SDValue &Op : N->op_values()) {
14921 if (N == To && Op.getNode() == EntrySDN) {
14922 // Special case: New node's operand is the entry node; just need to
14923 // copy extra info to new node.
14924 break;
14925 }
14926 if (!Self(Self, Op.getNode()))
14927 return false;
14928 }
14929 // Copy only if entry node was not reached.
14930 SDEI[N] = std::move(NEI);
14931 return true;
14932 };
14933
14934 // We first try with a lower MaxDepth, assuming that the path to common
14935 // operands between From and To is relatively short. This significantly
14936 // improves performance in the common case. The initial MaxDepth is big
14937 // enough to avoid retry in the common case; the last MaxDepth is large
14938 // enough to avoid having to use the fallback below (and protects from
14939 // potential stack exhaustion from recursion).
14940 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14941 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14942 // StartFrom is the previous (or initial) set of leafs reachable at the
14943 // previous maximum depth.
14945 std::swap(StartFrom, Leafs);
14946 for (const SDNode *N : StartFrom)
14947 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14948 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14949 return;
14950 // This should happen very rarely (reached the entry node).
14951 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14952 assert(!Leafs.empty());
14953 }
14954
14955 // This should not happen - but if it did, that means the subgraph reachable
14956 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14957 // could not visit all reachable common operands. Consequently, we were able
14958 // to reach the entry node.
14959 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14960 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14961 // Best-effort fallback if assertions disabled.
14962 SDEI[To] = std::move(NEI);
14963}
14964
14965#ifndef NDEBUG
14966static void checkForCyclesHelper(const SDNode *N,
14969 const llvm::SelectionDAG *DAG) {
14970 // If this node has already been checked, don't check it again.
14971 if (Checked.count(N))
14972 return;
14973
14974 // If a node has already been visited on this depth-first walk, reject it as
14975 // a cycle.
14976 if (!Visited.insert(N).second) {
14977 errs() << "Detected cycle in SelectionDAG\n";
14978 dbgs() << "Offending node:\n";
14979 N->dumprFull(DAG); dbgs() << "\n";
14980 abort();
14981 }
14982
14983 for (const SDValue &Op : N->op_values())
14984 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14985
14986 Checked.insert(N);
14987 Visited.erase(N);
14988}
14989#endif
14990
14992 const llvm::SelectionDAG *DAG,
14993 bool force) {
14994#ifndef NDEBUG
14995 bool check = force;
14996#ifdef EXPENSIVE_CHECKS
14997 check = true;
14998#endif // EXPENSIVE_CHECKS
14999 if (check) {
15000 assert(N && "Checking nonexistent SDNode");
15003 checkForCyclesHelper(N, visited, checked, DAG);
15004 }
15005#endif // !NDEBUG
15006}
15007
15008void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
15009 checkForCycles(DAG->getRoot().getNode(), DAG, force);
15010}
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isConstant(const MachineInstr &MI)
constexpr LLT S1
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define X(NUM, ENUM, NAME)
Definition ELF.h:851
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static std::optional< bool > isBigEndian(const SmallDenseMap< int64_t, int64_t, 8 > &MemOffset2Idx, int64_t LowestIdx)
Given a map from byte offsets in memory to indices in a load/store, determine if that map corresponds...
#define __asan_unpoison_memory_region(p, size)
Definition Compiler.h:592
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:335
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
This file defines a hash set that can be used to remove duplication of nodes in a graph.
iv users
Definition IVUsers.cpp:48
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static Register getMemsetValue(Register Val, LLT Ty, MachineIRBuilder &MIB)
static bool shouldLowerMemFuncForSize(const MachineFunction &MF)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
static Align getPrefTypeAlign(EVT VT, SelectionDAG &DAG)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register const TargetRegisterInfo * TRI
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
#define T
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
PowerPC Reduce CR logical Operation
const SmallVectorImpl< MachineOperand > & Cond
Remove Loads Into Fake Uses
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
Contains matchers for matching SelectionDAG nodes and values.
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, BatchAAResults *BatchAA)
static SDValue getFixedOrScalableQuantity(SelectionDAG &DAG, const SDLoc &DL, EVT VT, Ty Quantity)
static std::pair< SDValue, SDValue > getRuntimeCallSDValueHelper(SDValue Chain, const SDLoc &dl, TargetLowering::ArgListTy &&Args, const CallInst *CI, RTLIB::Libcall Call, SelectionDAG *DAG, const TargetLowering *TLI)
static SDValue getMemsetStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo)
Lower the call to 'memset' intrinsic function into a series of store operations.
static std::optional< APInt > FoldValueWithUndef(unsigned Opcode, const APInt &C1, bool IsUndef1, const APInt &C2, bool IsUndef2)
static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step, SelectionDAG &DAG)
static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC, SDVTList VTList, ArrayRef< SDValue > OpList)
static SDValue getMemsetStringVal(EVT VT, const SDLoc &dl, SelectionDAG &DAG, const TargetLowering &TLI, const ConstantDataArraySlice &Slice)
getMemsetStringVal - Similar to getMemsetValue.
static cl::opt< bool > EnableMemCpyDAGOpt("enable-memcpy-dag-opt", cl::Hidden, cl::init(true), cl::desc("Gang up loads and stores generated by inlining of memcpy"))
static bool haveNoCommonBitsSetCommutative(SDValue A, SDValue B)
static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList)
AddNodeIDValueTypes - Value type lists are intern'd so we can represent them solely with their pointe...
static void commuteShuffle(SDValue &N1, SDValue &N2, MutableArrayRef< int > M)
Swaps the values of N1 and N2.
static bool isMemSrcFromConstant(SDValue Src, ConstantDataArraySlice &Slice)
Returns true if memcpy source is constant data.
static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo)
static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC)
AddNodeIDOpcode - Add the node opcode to the NodeID data.
static ISD::CondCode getSetCCInverseImpl(ISD::CondCode Op, bool isIntegerLike)
static bool doNotCSE(SDNode *N)
doNotCSE - Return true if CSE should not be performed for this node.
static cl::opt< int > MaxLdStGlue("ldstmemcpy-glue-max", cl::desc("Number limit for gluing ld/st of memcpy."), cl::Hidden, cl::init(0))
static void AddNodeIDOperands(FoldingSetNodeID &ID, ArrayRef< SDValue > Ops)
AddNodeIDOperands - Various routines for adding operands to the NodeID data.
static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
Try to simplify vector concatenation to an input value, undef, or build vector.
static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info, SelectionDAG &DAG, SDValue Ptr, int64_t Offset=0)
InferPointerInfo - If the specified ptr/offset is a frame index, infer a MachinePointerInfo record fr...
static bool isInTailCallPositionWrapper(const CallInst *CI, const SelectionDAG *SelDAG, bool AllowReturnsFirstArg)
static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N)
If this is an SDNode with special info, add this info to the NodeID data.
static bool gluePropagatesDivergence(const SDNode *Node)
Return true if a glue output should propagate divergence information.
static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G)
static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs)
makeVTList - Return an instance of the SDVTList struct initialized with the specified members.
static void checkForCyclesHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallPtrSetImpl< const SDNode * > &Checked, const llvm::SelectionDAG *DAG)
static void chainLoadsAndStoresForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SmallVector< SDValue, 32 > &OutChains, unsigned From, unsigned To, SmallVector< SDValue, 16 > &OutLoadChains, SmallVector< SDValue, 16 > &OutStoreChains)
static int isSignedOp(ISD::CondCode Opcode)
For an integer comparison, return 1 if the comparison is a signed operation and 2 if the result is an...
static std::optional< APInt > FoldValue(unsigned Opcode, const APInt &C1, const APInt &C2)
static SDValue FoldBUILD_VECTOR(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI, unsigned AS)
static cl::opt< unsigned > MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192), cl::desc("DAG combiner limit number of steps when searching DAG " "for predecessor nodes"))
static APInt getDemandAllEltsMask(SDValue V)
Construct a DemandedElts mask which demands all elements of V.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
This file describes how to lower LLVM code to machine code.
static void removeOperands(MachineInstr &MI, unsigned i)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
static unsigned getSize(unsigned Kind)
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition APFloat.h:334
static constexpr roundingMode rmTowardZero
Definition APFloat.h:348
static const fltSemantics & BFloat()
Definition APFloat.h:295
static const fltSemantics & IEEEquad()
Definition APFloat.h:298
static const fltSemantics & IEEEdouble()
Definition APFloat.h:297
static constexpr roundingMode rmTowardNegative
Definition APFloat.h:347
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
static constexpr roundingMode rmTowardPositive
Definition APFloat.h:346
static const fltSemantics & IEEEhalf()
Definition APFloat.h:294
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:360
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition APFloat.h:1175
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1263
void copySign(const APFloat &RHS)
Definition APFloat.h:1357
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:5890
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1245
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
Definition APFloat.h:1499
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1236
bool isFinite() const
Definition APFloat.h:1521
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1402
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1254
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition APFloat.h:1290
bool isZero() const
Definition APFloat.h:1512
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1193
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1387
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1153
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1281
bool isPosZero() const
Definition APFloat.h:1527
bool isNegZero() const
Definition APFloat.h:1528
void changeSign()
Definition APFloat.h:1352
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1164
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2011
LLVM_ABI APInt usub_sat(const APInt &RHS) const
Definition APInt.cpp:2095
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1604
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1421
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1043
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1555
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1406
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1685
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1400
LLVM_ABI APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition APInt.cpp:639
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1064
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1527
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:956
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1345
APInt abs() const
Get the absolute value.
Definition APInt.h:1810
LLVM_ABI APInt sadd_sat(const APInt &RHS) const
Definition APInt.cpp:2066
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1189
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:259
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1697
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1503
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1118
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1675
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1411
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition APInt.cpp:1185
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:778
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition APInt.h:841
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1173
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1654
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1643
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1613
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition APInt.cpp:651
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sshl_sat(const APInt &RHS) const
Definition APInt.cpp:2126
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
Definition APInt.cpp:2140
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1072
static bool isSameValue(const APInt &I1, const APInt &I2, bool SignedCompare=false)
Determine if two APInts have the same value, after zero-extending or sign-extending (if SignedCompare...
Definition APInt.h:555
LLVM_ABI APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition APInt.cpp:1172
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition APInt.cpp:397
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition APInt.h:1450
unsigned logBase2() const
Definition APInt.h:1776
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition APInt.cpp:2076
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:834
LLVM_ABI APInt multiplicativeInverse() const
Definition APInt.cpp:1305
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1776
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1157
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:1016
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1382
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:880
LLVM_ABI APInt byteSwap() const
Definition APInt.cpp:756
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1264
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition APInt.h:1432
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1403
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:482
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1244
bool isOne() const
Determine if this is a value of 1.
Definition APInt.h:390
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition APInt.h:865
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:858
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1228
LLVM_ABI APInt ssub_sat(const APInt &RHS) const
Definition APInt.cpp:2085
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
unsigned getSrcAddressSpace() const
unsigned getDestAddressSpace() const
static Capacity get(size_t N)
Get the capacity of an array that can hold at least N elements.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
This is an SDNode representing atomic operations.
static LLVM_ABI BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
BitVector & reset()
Definition BitVector.h:411
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition BitVector.h:360
void clear()
clear - Removes all bits from the bitvector.
Definition BitVector.h:354
BitVector & set()
Definition BitVector.h:370
bool none() const
none - Returns true if none of the bits are set.
Definition BitVector.h:207
size_type size() const
size - Returns the number of bits in this bitvector.
Definition BitVector.h:178
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition Constants.h:1065
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
LLVM_ABI bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &RawBitElements, BitVector &UndefElements) const
Extract the raw bit data from a build vector of Undef, Constant or ConstantFP node elements.
static LLVM_ABI void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &DstBitElements, ArrayRef< APInt > SrcBitElements, BitVector &DstUndefElements, const BitVector &SrcUndefElements)
Recast bit data SrcBitElements to DstEltSizeInBits wide elements.
LLVM_ABI bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
LLVM_ABI ConstantFPSDNode * getConstantFPSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant FP or null if this is not a constant FP splat.
LLVM_ABI SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
LLVM_ABI bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, unsigned &SplatBitSize, bool &HasAnyUndefs, unsigned MinSplatBits=0, bool isBigEndian=false) const
Check if this is a constant splat, and if so, find the smallest element size that splats the vector.
LLVM_ABI ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
LLVM_ABI int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements, uint32_t BitWidth) const
If this is a constant FP splat and the splatted constant FP is an exact power or 2,...
LLVM_ABI std::optional< std::pair< APInt, APInt > > isArithmeticSequence() const
If this BuildVector is constant and represents an arithmetic sequence "<a, a+n, a+2n,...
LLVM_ABI bool isConstant() const
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
static LLVM_ABI bool isValueValidForType(EVT VT, const APFloat &Val)
const APFloat & getValueAPF() const
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
const APFloat & getValue() const
Definition Constants.h:464
This is the shared class of boolean and integer constants.
Definition Constants.h:87
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:162
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
MachineConstantPoolValue * getMachineCPVal() const
const Constant * getConstVal() const
LLVM_ABI Type * getType() const
This class represents a range of values.
LLVM_ABI ConstantRange multiply(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
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 ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
uint64_t getZExtValue() const
const APInt & getAPIntValue() const
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
DWARF expression.
static LLVM_ABI ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
Base class for variables.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:215
LLVM_ABI IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
LLVM_ABI Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
A debug info location.
Definition DebugLoc.h:123
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:209
Data structure describing the variable locations in a function.
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:711
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:354
LLVM_ABI unsigned getAddressSpace() const
const GlobalValue * getGlobal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
unsigned getAddressSpace() const
Module * getParent()
Get the module that this global value is contained inside of...
PointerType * getType() const
Global values are always pointers.
This class is used to form a handle around another node that is persistent and is updated across invo...
const SDValue & getValue() const
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Tracks which library functions to use for a particular subtarget.
LLVM_ABI CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall.
LLVM_ABI RTLIB::LibcallImpl getLibcallImpl(RTLIB::Libcall Call) const
Return the lowering's selection of implementation call for Call.
This SDNode is used for LIFETIME_START/LIFETIME_END values.
This class is used to represent ISD::LOAD nodes.
static LocationSize precise(uint64_t Value)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h:1080
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1444
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
Abstract base class for all machine specific constantpool value subclasses.
virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID)=0
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
const MDNode * getRanges() const
Return the range tag for the memory reference.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
This class contains meta information specific to a module.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
size_t getNumMemOperands() const
Return the number of memory operands.
LLVM_ABI MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt, PointerUnion< MachineMemOperand *, MachineMemOperand ** > memrefs)
Constructor that supports single or multiple MMOs.
PointerUnion< MachineMemOperand *, MachineMemOperand ** > MemRefs
Memory reference information.
MachineMemOperand * getMemOperand() const
Return the unique MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
ArrayRef< MachineMemOperand * > memoperands() const
Return the memory operands for this node.
unsigned getRawSubclassData() const
Return the SubclassData value, without HasDebugValue.
EVT getMemoryVT() const
Return the type of the in-memory value.
Representation for a specific memory location.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition Module.cpp:235
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:298
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
Class to represent pointers.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
unsigned getAddressSpace() const
Return the address space of the Pointer type.
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
A discriminated union of two or more pointer types, with the discriminator in the low bits of the poi...
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Analysis providing profile information.
void Deallocate(SubClass *E)
Deallocate - Release storage for the pointed-to object.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Keeps track of dbg_value information through SDISel.
LLVM_ABI void add(SDDbgValue *V, bool isParameter)
LLVM_ABI void erase(const SDNode *Node)
Invalidate all DbgValues attached to the node and remove it from the Node-to-DbgValues map.
Holds the information from a dbg_label node through SDISel.
Holds the information for a single machine location through SDISel; either an SDNode,...
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(Register VReg)
static SDDbgOperand fromConst(const Value *Const)
@ SDNODE
Value is the result of an expression.
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
const DebugLoc & getDebugLoc() const
unsigned getIROrder() const
This class provides iterator support for SDUse operands that use a specific SDNode.
Represents one node in the SelectionDAG.
ArrayRef< SDUse > ops() const
const APInt & getAsAPIntVal() const
Helper method returns the APInt value of a ConstantSDNode.
LLVM_ABI void dumprFull(const SelectionDAG *G=nullptr) const
printrFull to dbgs().
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
LLVM_ABI bool isOnlyUserOf(const SDNode *N) const
Return true if this node is the only use of N.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
static constexpr size_t getMaxNumOperands()
Return the maximum number of operands that a SDNode can hold.
iterator_range< use_iterator > uses()
MemSDNodeBitfields MemSDNodeBits
LLVM_ABI void Profile(FoldingSetNodeID &ID) const
Gather unique data for the node.
bool getHasDebugValue() const
SDNodeFlags getFlags() const
void setNodeId(int Id)
Set unique node id.
LLVM_ABI void intersectFlagsWith(const SDNodeFlags Flags)
Clear any flags in this node that aren't also set in Flags.
static bool hasPredecessorHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallVectorImpl< const SDNode * > &Worklist, unsigned int MaxSteps=0, bool TopologicalPrune=false)
Returns true if N is a predecessor of any node in Worklist.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
bool use_empty() const
Return true if there are no uses of this node.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
static LLVM_ABI bool areOnlyUsersOf(ArrayRef< const SDNode * > Nodes, const SDNode *N)
Return true if all the users of N are contained in Nodes.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
const APInt & getConstantOperandAPInt(unsigned Num) const
Helper method returns the APInt of a ConstantSDNode operand.
std::optional< APInt > bitcastToAPInt() const
LLVM_ABI bool hasPredecessor(const SDNode *N) const
Return true if N is a predecessor of this node.
LLVM_ABI bool hasAnyUseOfValue(unsigned Value) const
Return true if there are any use of the indicated value.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
bool isUndef() const
Returns true if the node type is UNDEF or POISON.
op_iterator op_end() const
op_iterator op_begin() const
static use_iterator use_end()
LLVM_ABI void DropOperands()
Release the operands and set this node to have zero operands.
SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
Create an SDNode.
Represents a use of a SDNode.
SDNode * getUser()
This returns the SDNode that contains this Use.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if the referenced return value is an operand of N.
SDValue()=default
LLVM_ABI bool reachesChainWithoutSideEffects(SDValue Dest, unsigned Depth=2) const
Return true if this operand (which must be a chain) reaches the specified operand without crossing an...
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
bool use_empty() const
Return true if there are no nodes using value ResNo of Node.
const APInt & getConstantOperandAPInt(unsigned i) const
uint64_t getScalarValueSizeInBits() const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
uint64_t getConstantOperandVal(unsigned i) const
unsigned getOpcode() const
virtual void verifyTargetNode(const SelectionDAG &DAG, const SDNode *N) const
Checks that the given target-specific node is valid. Aborts if it is not.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC)
LLVM_ABI Align getReducedAlign(EVT VT, bool UseABI)
In most cases this function returns the ABI alignment for a given type, except for illegal vector typ...
LLVM_ABI SDValue getVPZeroExtendInReg(SDValue Op, SDValue Mask, SDValue EVL, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
LLVM_ABI SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI std::pair< SDValue, SDValue > getMemccpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue C, SDValue Size, const CallInst *CI)
Lower a memccpy operation into a target library call and return the resulting chain and call result a...
LLVM_ABI SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsExpanding=false)
SDValue getExtractVectorElt(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Extract element at Idx from Vec.
LLVM_ABI SDValue getSplatSourceVector(SDValue V, int &SplatIndex)
If V is a splatted value, return the source vector and its splat index.
LLVM_ABI SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI OverflowKind computeOverflowForUnsignedSub(SDValue N0, SDValue N1) const
Determine if the result of the unsigned sub of 2 nodes can overflow.
LLVM_ABI unsigned ComputeMaxSignificantBits(SDValue Op, unsigned Depth=0) const
Get the upper bound on bit size for this Value Op as a signed integer.
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
LLVM_ABI std::pair< SDValue, SDValue > getStrlen(SDValue Chain, const SDLoc &dl, SDValue Src, const CallInst *CI)
Lower a strlen operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
LLVM_ABI SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
LLVM_ABI SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl, SDNodeFlags Flags={})
Constant fold a setcc to true or false.
bool isKnownNeverSNaN(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
LLVM_ABI std::optional< bool > isBoolConstant(SDValue N) const
Check if a value \op N is a constant using the target's BooleanContent for its type.
LLVM_ABI SDValue getStackArgumentTokenFactor(SDValue Chain)
Compute a TokenFactor to force all the incoming stack arguments to be loaded from the stack.
const TargetSubtargetInfo & getSubtarget() const
LLVM_ABI ConstantRange computeConstantRange(SDValue Op, bool ForSigned, unsigned Depth=0) const
Determine the possible constant range of an integer or vector of integers.
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI void updateDivergence(SDNode *N)
LLVM_ABI SDValue getSplatValue(SDValue V, bool LegalTypes=false)
If V is a splat vector, return its scalar source operand by extracting that element from the source v...
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) neutral element for the given opcode, if it exists.
LLVM_ABI SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
LLVM_ABI SDValue getAtomicLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT MemVT, EVT VT, SDValue Chain, SDValue Ptr, MachineMemOperand *MMO)
LLVM_ABI SDNode * getNodeIfExists(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops, const SDNodeFlags Flags, bool AllowCommute=false)
Get the specified node if it's already available, or else return NULL.
LLVM_ABI SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid, uint64_t Index, uint32_t Attr)
Creates a PseudoProbeSDNode with function GUID Guid and the index of the block Index it is probing,...
LLVM_ABI SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
LLVM_ABI SDNode * SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT)
These are used for target selectors to mutate the specified node to have the specified return type,...
LLVM_ABI void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, const LibcallLoweringInfo *LibcallsInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, MachineModuleInfo &MMI, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
LLVM_ABI SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
LLVM_ABI SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBitcastedSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
Gets a node for an atomic cmpxchg op.
LLVM_ABI SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain)
If an existing load has uses of its chain, create a token factor node with that chain and the new mem...
LLVM_ABI bool isConstantIntBuildVectorOrConstantInt(SDValue N, bool AllowOpaques=true) const
Test whether the given value is a constant int or similar node.
LLVM_ABI void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
LLVM_ABI SDValue getJumpTableDebugInfo(int JTI, SDValue Chain, const SDLoc &DL)
LLVM_ABI SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
LLVM_ABI std::optional< unsigned > getValidMaximumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
LLVM_ABI SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
OverflowKind
Used to represent the possible overflow behavior of an operation.
static LLVM_ABI unsigned getHasPredecessorMaxSteps()
LLVM_ABI bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
SDValue getExtractSubvector(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Return the VT typed sub-vector of Vec at Idx.
LLVM_ABI bool cannotBeOrderedNegativeFP(SDValue Op) const
Test whether the given float value is known to be positive.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI bool calculateDivergence(SDNode *N)
LLVM_ABI std::pair< SDValue, SDValue > getStrcmp(SDValue Chain, const SDLoc &dl, SDValue S0, SDValue S1, const CallInst *CI)
Lower a strcmp operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
LLVM_ABI SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI bool canIgnoreSignBitOfZero(const SDUse &Use) const
Check if a use of a float value is insensitive to signed zeros.
LLVM_ABI bool SignBitIsZeroFP(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero, for a floating-point value.
LLVM_ABI SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=LocationSize::precise(0), const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
SDValue getInsertSubvector(const SDLoc &DL, SDValue Vec, SDValue SubVec, unsigned Idx)
Insert SubVec at the Idx element of Vec.
LLVM_ABI SDValue getBitcastedZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false, SDNodeFlags Flags={})
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
LLVM_ABI SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
LLVM_ABI SDValue getVPZExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be an integer vector, to the vector-type VT,...
const TargetLowering & getTargetLoweringInfo() const
LLVM_ABI bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
LLVM_ABI SDValue getStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
bool isGuaranteedNotToBePoison(SDValue Op, unsigned Depth=0) const
Return true if this function can prove that Op is never poison.
LLVM_ABI SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
LLVM_ABI SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI APInt computeVectorKnownZeroElements(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
For each demanded element of a vector, see if it is known to be zero.
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
bool NewNodesMustHaveLegalTypes
When true, additional steps are taken to ensure that getConstant() and similar functions return DAG n...
LLVM_ABI std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
LLVM_ABI void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
LLVM_ABI SDNode * MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, ArrayRef< SDValue > Ops)
This mutates the specified node to have the specified return type, opcode, and operands.
LLVM_ABI std::pair< SDValue, SDValue > UnrollVectorOverflowOp(SDNode *N, unsigned ResNE=0)
Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
allnodes_const_iterator allnodes_begin() const
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
LLVM_ABI SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcastedAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts, unsigned Depth=0) const
Test whether V has a splatted value for all the demanded elements.
LLVM_ABI void DeleteNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
LLVM_ABI std::pair< SDValue, SDValue > getStrcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, const CallInst *CI)
Lower a strcpy operation into a target library call and return the resulting chain and call result as...
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
LLVM_ABI SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
LLVM_ABI std::optional< unsigned > getValidShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has a uniform shift amount that is less than the element bit-width of the shi...
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
LLVM_ABI SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
LLVM_ABI SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI bool isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
const DataLayout & getDataLayout() const
LLVM_ABI SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
LLVM_ABI SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
LLVM_ABI bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops)
Check if a node exists without modifying its flags.
LLVM_ABI ConstantRange computeConstantRangeIncludingKnownBits(SDValue Op, bool ForSigned, unsigned Depth=0) const
Combine constant ranges from computeConstantRange() and computeKnownBits().
const SelectionDAGTargetInfo & getSelectionDAGInfo() const
LLVM_ABI bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, unsigned Bytes, int Dist) const
Return true if loads are next to each other and can be merged.
LLVM_ABI SDValue getMaskedHistogram(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
LLVM_ABI SDValue getStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI OverflowKind computeOverflowForUnsignedMul(SDValue N0, SDValue N1) const
Determine if the result of the unsigned mul of 2 nodes can overflow.
LLVM_ABI void copyExtraInfo(SDNode *From, SDNode *To)
Copy extra info associated with one node to another.
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue SV, unsigned Align)
VAArg produces a result and token chain, and takes a pointer and a source value as input.
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getLoadFFVP(EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachineMemOperand *MMO)
LLVM_ABI SDValue getTypeSize(const SDLoc &DL, EVT VT, TypeSize TS)
LLVM_ABI SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
LLVM_ABI void clear()
Clear state and free memory necessary to make this SelectionDAG ready to process a new block.
LLVM_ABI std::pair< SDValue, SDValue > getMemcmp(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, const CallInst *CI)
Lower a memcmp operation into a target library call and return the resulting chain and call result as...
LLVM_ABI void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
LLVM_ABI SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV)
Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to the shuffle node in input but with swa...
LLVM_ABI std::pair< SDValue, SDValue > SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the vector with EXTRACT_SUBVECTOR using the provided VTs and return the low/high part.
LLVM_ABI SDValue makeStateFunctionCall(unsigned LibFunc, SDValue Ptr, SDValue InChain, const SDLoc &DLoc)
Helper used to make a call to a library function that has one argument of pointer type.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, bool PoisonOnly=false, unsigned Depth=0) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
LLVM_ABI SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
LLVM_ABI SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI OverflowKind computeOverflowForSignedMul(SDValue N0, SDValue N1) const
Determine if the result of the signed mul of 2 nodes can overflow.
LLVM_ABI MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
LLVM_ABI void dump() const
Dump the textual format of this DAG.
LLVM_ABI bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if '(Op & Mask) == Mask'.
LLVM_ABI bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
LLVM_ABI void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
LLVM_ABI void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
bool isConstantValueOfAnyType(SDValue N) const
LLVM_ABI SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
LLVM_ABI SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, Register VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
LLVM_ABI KnownFPClass computeKnownFPClass(SDValue Op, FPClassTest InterestedClasses, unsigned Depth=0) const
Determine floating-point class information about Op.
LLVM_ABI SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI bool isKnownNeverZero(SDValue Op, unsigned Depth=0) const
Test whether the given SDValue is known to contain non-zero value(s).
LLVM_ABI SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SDNodeFlags Flags=SDNodeFlags())
LLVM_ABI std::optional< unsigned > getValidMinimumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT)
Convert Op, which must be of integer type, to the integer type VT, by using an extension appropriate ...
LLVM_ABI SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
LLVM_ABI std::pair< SDValue, SDValue > getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT)
Convert Op, which must be a STRICT operation of float type, to the float type VT, by either extending...
LLVM_ABI std::pair< SDValue, SDValue > SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL)
Split the explicit vector length parameter of a VP operation.
LLVM_ABI SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
LLVM_ABI SDValue getVPLogicalNOT(const SDLoc &DL, SDValue Val, SDValue Mask, SDValue EVL, EVT VT)
Create a vector-predicated logical NOT operation as (VP_XOR Val, BooleanOne, Mask,...
LLVM_ABI SDValue getMaskFromElementCount(const SDLoc &DL, EVT VT, ElementCount Len)
Return a vector with the first 'Len' lanes set to true and remaining lanes set to false.
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
iterator_range< allnodes_iterator > allnodes()
LLVM_ABI SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI SDValue WidenVector(const SDValue &N, const SDLoc &DL)
Widen the vector up to the next power of two using INSERT_SUBVECTOR.
LLVM_ABI bool isKnownNeverZeroFloat(SDValue Op) const
Test whether the given floating point SDValue is known to never be positive or negative zero.
const LibcallLoweringInfo & getLibcalls() const
LLVM_ABI SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, const MDNode *Ranges=nullptr, bool IsExpanding=false)
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
LLVM_ABI SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the Fra...
ArrayRef< SDDbgValue * > GetDbgValues(const SDNode *SD) const
Get the debug values which reference the given SDNode.
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI OverflowKind computeOverflowForSignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the signed addition of 2 nodes can overflow.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
LLVM_ABI unsigned AssignTopologicalOrder()
Topological-sort the AllNodes list and a assign a unique node id for each node in the DAG based on th...
ilist< SDNode >::size_type allnodes_size() const
LLVM_ABI bool isKnownNeverNaN(SDValue Op, const APInt &DemandedElts, bool SNaN=false, unsigned Depth=0) const
Test whether the given SDValue (or all elements of it, if it is a vector) is known to never be NaN in...
LLVM_ABI SDValue FoldConstantBuildVector(BuildVectorSDNode *BV, const SDLoc &DL, EVT DstEltVT)
Fold BUILD_VECTOR of constants/undefs to the destination type BUILD_VECTOR of constants/undefs elemen...
LLVM_ABI SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsCompressing=false)
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits.
LLVM_ABI bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Return true if 'Op' is known to be zero in DemandedElts.
LLVM_ABI SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT)
Create a true or false constant of type VT using the target's BooleanContent for type OpVT.
LLVM_ABI SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
LLVM_ABI SDValue getExtStridedLoadVP(ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
LLVM_ABI SDValue getVPPtrExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be of integer type, to the vector-type integer type VT,...
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI void getTopologicallyOrderedNodes(SmallVectorImpl< const SDNode * > &SortedNodes) const
Get all the nodes in their topological order without modifying any states.
LLVM_ABI void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
LLVM_ABI std::pair< SDValue, SDValue > getStrstr(SDValue Chain, const SDLoc &dl, SDValue S0, SDValue S1, const CallInst *CI)
Lower a strstr operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to extend the Op as a pointer value assuming it was the smaller SrcTy ...
LLVM_ABI bool canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts, bool PoisonOnly=false, bool ConsiderFlags=true, unsigned Depth=0) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
LLVM_ABI OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT SVT, MachineMemOperand *MMO, bool IsCompressing=false)
LLVM_ABI void canonicalizeCommutativeBinop(unsigned Opcode, SDValue &N1, SDValue &N2) const
Swap N1 and N2 if Opcode is a commutative binary opcode and the canonical form expects the opposite o...
LLVM_ABI KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
LLVM_ABI SDValue getRegisterMask(const uint32_t *RegMask)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVM_ABI SDValue getCondCode(ISD::CondCode Cond)
LLVM_ABI bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
LLVM_ABI bool isKnownToBeAPowerOfTwoFP(SDValue Val, unsigned Depth=0) const
Test if the given fp value is known to be an integer power-of-2, either positive or negative.
LLVM_ABI OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset)
Create an add instruction with appropriate flags when used for addressing some offset of an object.
LLVMContext * getContext() const
LLVM_ABI SDValue simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y, SDNodeFlags Flags)
Try to simplify a floating-point binary operation into 1 of its operands or a constant.
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
LLVM_ABI bool isKnownToBeAPowerOfTwo(SDValue Val, bool OrZero=false, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
LLVM_ABI SDValue getDeactivationSymbol(const GlobalValue *GV)
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
LLVM_ABI SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
LLVM_ABI bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
LLVM_ABI SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
LLVM_ABI SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
LLVM_ABI std::pair< EVT, EVT > GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT, bool *HiIsEmpty) const
Compute the VTs needed for the low/hi parts of a type, dependent on an enveloping VT that has been sp...
LLVM_ABI SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
LLVM_ABI std::optional< ConstantRange > getValidShiftAmountRange(SDValue V, const APInt &DemandedElts, unsigned Depth) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
LLVM_ABI SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
LLVM_ABI SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
LLVM_ABI SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, ISD::LoadExtType, bool IsExpanding=false)
SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op)
Returns a node representing a splat of one value into all lanes of the provided vector type.
LLVM_ABI std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
LLVM_ABI SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp, ArrayRef< ISD::NodeType > CandidateBinOps, bool AllowPartials=false)
Match a binop + shuffle pyramid that represents a horizontal reduction over the elements of a vector ...
LLVM_ABI bool isADDLike(SDValue Op, bool NoWrap=false) const
Return true if the specified operand is an ISD::OR or ISD::XOR node that can be treated as an ISD::AD...
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
LLVM_ABI SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
LLVM_ABI void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits=0, unsigned SizeInBits=0, bool InvalidateDbg=true)
Transfer debug values from one node to another, while optionally generating fragment expressions for ...
LLVM_ABI SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
LLVM_ABI SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, bool IsTruncating=false)
ilist< SDNode >::iterator allnodes_iterator
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
int getMaskElt(unsigned Idx) const
ArrayRef< int > getMask() const
static void commuteMask(MutableArrayRef< int > Mask)
Change values in a shuffle permute mask assuming the two vector operands have swapped position.
static LLVM_ABI bool isSplatMask(ArrayRef< int > Mask)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:137
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Completely target-dependent object reference.
unsigned getTargetFlags() const
Provides information about what library functions are available for the current target.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
const TargetMachine & getTargetMachine() const
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
unsigned getMaxStoresPerMemcpy(bool OptSize) const
Get maximum # of store operations permitted for llvm.memcpy.
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
virtual bool allowsMisalignedMemoryAccesses(EVT, unsigned AddrSpace=0, Align Alignment=Align(1), MachineMemOperand::Flags Flags=MachineMemOperand::MONone, unsigned *=nullptr) const
Determine if the target supports unaligned memory accesses.
virtual bool shallExtractConstSplatVectorElementToStore(Type *VectorTy, unsigned ElemSizeInBits, unsigned &Index) const
Return true if the target shall perform extract vector element and store given that the vector is kno...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
BooleanContent
Enum that describes how the target represents true/false values.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
std::vector< ArgListEntry > ArgListTy
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool findOptimalMemOpLowering(LLVMContext &Context, std::vector< EVT > &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS, unsigned SrcAS, const AttributeList &FuncAttributes, EVT *LargestVT=nullptr) const
Determines the optimal series of memory ops to replace the memset / memcpy.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
Primary interface to the complete machine description for the target machine.
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition Triple.h:646
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:290
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:313
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:286
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:311
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:201
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:236
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:35
LLVM_ABI void set(Value *Val)
Definition Value.h:883
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:255
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:3241
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition APInt.cpp:3171
LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition APInt.cpp:3158
LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition APInt.cpp:3148
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
Definition APInt.cpp:3222
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition APInt.cpp:3163
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:3231
APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition APInt.h:2286
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
Definition APInt.cpp:3213
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:3049
LLVM_ABI APInt clmulh(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, and return high-bits.
Definition APInt.cpp:3246
APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition APInt.h:2291
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition APInt.cpp:3143
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition APInt.cpp:3153
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
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition CallingConv.h:41
@ 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
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:548
@ 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.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
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.
GenericUniformityInfo< SSAContext > UniformityInfo
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:532
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:360
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:237
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1759
LLVM_ABI SDValue peekThroughExtractSubvectors(SDValue V)
Return the non-extracted vector source operand of V if it exists.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
LLVM_ABI bool isAllOnesOrAllOnesSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant -1 integer or a splatted vector of a constant -1 integer (with...
Definition Utils.cpp:1607
LLVM_ABI SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
@ Undef
Value of the register doesn't matter.
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:313
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:323
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1710
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:634
auto cast_or_null(const Y &Val)
Definition Casting.h:714
LLVM_ABI bool isNullOrNullSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
Definition Utils.cpp:1589
LLVM_ABI bool isMinSignedConstant(SDValue V)
Returns true if V is a constant min signed integer value.
LLVM_ABI ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition APFloat.h:1622
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:202
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1665
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
LLVM_ABI SDValue peekThroughInsertVectorElt(SDValue V, const APInt &DemandedElts)
Recursively peek through INSERT_VECTOR_ELT nodes, returning the source vector operand of V,...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1696
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI SDValue peekThroughTruncates(SDValue V)
Return the non-truncated source operand of V if it exists.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1753
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI SDValue peekThroughOneUseBitcasts(SDValue V)
Return the non-bitcasted and one-use source operand of V if it exists.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:82
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool isOneOrOneSplat(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1646
@ Mul
Product of integers.
@ Sub
Subtraction of integers.
LLVM_ABI bool isNullConstantOrUndef(SDValue V)
Returns true if V is a constant integer zero or an UNDEF node.
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition Analysis.cpp:539
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI ConstantSDNode * isConstOrConstSplat(SDValue N, bool AllowUndefs=false, bool AllowTruncation=false)
Returns the SDNode if it is a constant splat BuildVector or constant int.
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1885
constexpr unsigned BitWidth
bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition Analysis.cpp:719
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isZeroOrZeroSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
LLVM_ABI bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI bool isNullFPConstant(SDValue V)
Returns true if V is an FP constant with a value of positive zero.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant (+/-)0.0 floating-point value or a splatted vector thereof (wi...
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1683
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1723
LLVM_ABI bool isOnesOrOnesSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo)
Returns true if V is a neutral element of Opc with Flags.
LLVM_ABI bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition MathExtras.h:373
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:763
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition Metadata.h:783
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:780
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
void move(uint64_t Delta)
Moves the Offset and adjusts Length accordingly.
const ConstantDataArray * Array
ConstantDataArray pointer.
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:403
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:145
intptr_t getRawBits() const
Definition ValueTypes.h:528
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:70
EVT changeTypeToInteger() const
Return the type converted to an equivalently sized integer or vector with integer element type.
Definition ValueTypes.h:129
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:292
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:308
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:155
ElementCount getVectorElementCount() const
Definition ValueTypes.h:358
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:381
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:367
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:393
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:324
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:61
bool isFixedLengthVector() const
Definition ValueTypes.h:189
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:176
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:331
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:300
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:264
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:182
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:336
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition ValueTypes.h:150
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:344
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition ValueTypes.h:316
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:469
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:160
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:317
LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:271
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:127
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:258
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:290
static LLVM_ABI std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
void makeNegative()
Make this value negative.
Definition KnownBits.h:122
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:167
KnownBits byteSwap() const
Definition KnownBits.h:547
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:305
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:551
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:249
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:178
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false, bool SelfAdd=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:363
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:337
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
static LLVM_ABI KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:241
static LLVM_ABI KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:327
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:186
static LLVM_ABI KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:202
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:148
static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition KnownBits.h:114
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:342
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
LLVM_ABI KnownBits truncSSat(unsigned BitWidth) const
Truncate with signed saturation (signed input -> signed output)
static LLVM_ABI KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)
Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
Definition KnownBits.cpp:54
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:378
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:296
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:235
static LLVM_ABI KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:173
static LLVM_ABI KnownBits clmul(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for clmul(LHS, RHS).
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
LLVM_ABI KnownBits truncUSat(unsigned BitWidth) const
Truncate with unsigned saturation (unsigned input -> unsigned output)
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
LLVM_ABI KnownBits truncSSatU(unsigned BitWidth) const
Truncate with signed saturation to unsigned (signed input -> unsigned output)
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isUnknown() const
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
static LLVM_ABI KnownFPClass bitcast(const fltSemantics &FltSemantics, const KnownBits &Bits)
Report known values for a bitcast into a float with provided semantics.
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)