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/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1039/// SelectionDAG.
1041 // Create a dummy node (which is not added to allnodes), that adds a reference
1042 // to the root node, preventing it from being deleted.
1043 HandleSDNode Dummy(getRoot());
1044
1045 SmallVector<SDNode*, 128> DeadNodes;
1046
1047 // Add all obviously-dead nodes to the DeadNodes worklist.
1048 for (SDNode &Node : allnodes())
1049 if (Node.use_empty())
1050 DeadNodes.push_back(&Node);
1051
1052 RemoveDeadNodes(DeadNodes);
1053
1054 // If the root changed (e.g. it was a dead load, update the root).
1055 setRoot(Dummy.getValue());
1056}
1057
1058/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1059/// given list, and any nodes that become unreachable as a result.
1061
1062 // Process the worklist, deleting the nodes and adding their uses to the
1063 // worklist.
1064 while (!DeadNodes.empty()) {
1065 SDNode *N = DeadNodes.pop_back_val();
1066 // Skip to next node if we've already managed to delete the node. This could
1067 // happen if replacing a node causes a node previously added to the node to
1068 // be deleted.
1069 if (N->getOpcode() == ISD::DELETED_NODE)
1070 continue;
1071
1072 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1073 DUL->NodeDeleted(N, nullptr);
1074
1075 // Take the node out of the appropriate CSE map.
1076 RemoveNodeFromCSEMaps(N);
1077
1078 // Next, brutally remove the operand list. This is safe to do, as there are
1079 // no cycles in the graph.
1080 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1081 SDUse &Use = *I++;
1082 SDNode *Operand = Use.getNode();
1083 Use.set(SDValue());
1084
1085 // Now that we removed this operand, see if there are no uses of it left.
1086 if (Operand->use_empty())
1087 DeadNodes.push_back(Operand);
1088 }
1089
1090 DeallocateNode(N);
1091 }
1092}
1093
1095 SmallVector<SDNode*, 16> DeadNodes(1, N);
1096
1097 // Create a dummy node that adds a reference to the root node, preventing
1098 // it from being deleted. (This matters if the root is an operand of the
1099 // dead node.)
1100 HandleSDNode Dummy(getRoot());
1101
1102 RemoveDeadNodes(DeadNodes);
1103}
1104
1106 // First take this out of the appropriate CSE map.
1107 RemoveNodeFromCSEMaps(N);
1108
1109 // Finally, remove uses due to operands of this node, remove from the
1110 // AllNodes list, and delete the node.
1111 DeleteNodeNotInCSEMaps(N);
1112}
1113
1114void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1115 assert(N->getIterator() != AllNodes.begin() &&
1116 "Cannot delete the entry node!");
1117 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1118
1119 // Drop all of the operands and decrement used node's use counts.
1120 N->DropOperands();
1121
1122 DeallocateNode(N);
1123}
1124
1125void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1126 assert(!(V->isVariadic() && isParameter));
1127 if (isParameter)
1128 ByvalParmDbgValues.push_back(V);
1129 else
1130 DbgValues.push_back(V);
1131 for (const SDNode *Node : V->getSDNodes())
1132 if (Node)
1133 DbgValMap[Node].push_back(V);
1134}
1135
1137 DbgValMapType::iterator I = DbgValMap.find(Node);
1138 if (I == DbgValMap.end())
1139 return;
1140 for (auto &Val: I->second)
1141 Val->setIsInvalidated();
1142 DbgValMap.erase(I);
1143}
1144
1145void SelectionDAG::DeallocateNode(SDNode *N) {
1146 // If we have operands, deallocate them.
1148
1149 NodeAllocator.Deallocate(AllNodes.remove(N));
1150
1151 // Set the opcode to DELETED_NODE to help catch bugs when node
1152 // memory is reallocated.
1153 // FIXME: There are places in SDag that have grown a dependency on the opcode
1154 // value in the released node.
1155 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1156 N->NodeType = ISD::DELETED_NODE;
1157
1158 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1159 // them and forget about that node.
1160 DbgInfo->erase(N);
1161
1162 // Invalidate extra info.
1163 SDEI.erase(N);
1164}
1165
1166#ifndef NDEBUG
1167/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1168void SelectionDAG::verifyNode(SDNode *N) const {
1169 switch (N->getOpcode()) {
1170 default:
1171 if (N->isTargetOpcode())
1173 break;
1174 case ISD::BUILD_PAIR: {
1175 EVT VT = N->getValueType(0);
1176 assert(N->getNumValues() == 1 && "Too many results!");
1177 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1178 "Wrong return type!");
1179 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1180 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1181 "Mismatched operand types!");
1182 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1183 "Wrong operand type!");
1184 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1185 "Wrong return type size");
1186 break;
1187 }
1188 case ISD::BUILD_VECTOR: {
1189 assert(N->getNumValues() == 1 && "Too many results!");
1190 assert(N->getValueType(0).isVector() && "Wrong return type!");
1191 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1192 "Wrong number of operands!");
1193 EVT EltVT = N->getValueType(0).getVectorElementType();
1194 for (const SDUse &Op : N->ops()) {
1195 assert((Op.getValueType() == EltVT ||
1196 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1197 EltVT.bitsLE(Op.getValueType()))) &&
1198 "Wrong operand type!");
1199 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1200 "Operands must all have the same type");
1201 }
1202 break;
1203 }
1204 case ISD::SADDO:
1205 case ISD::UADDO:
1206 case ISD::SSUBO:
1207 case ISD::USUBO:
1208 assert(N->getNumValues() == 2 && "Wrong number of results!");
1209 assert(N->getVTList().NumVTs == 2 && N->getNumOperands() == 2 &&
1210 "Invalid add/sub overflow op!");
1211 assert(N->getVTList().VTs[0].isInteger() &&
1212 N->getVTList().VTs[1].isInteger() &&
1213 N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1214 N->getOperand(0).getValueType() == N->getVTList().VTs[0] &&
1215 "Binary operator types must match!");
1216 break;
1217 }
1218}
1219#endif // NDEBUG
1220
1221/// Insert a newly allocated node into the DAG.
1222///
1223/// Handles insertion into the all nodes list and CSE map, as well as
1224/// verification and other common operations when a new node is allocated.
1225void SelectionDAG::InsertNode(SDNode *N) {
1226 AllNodes.push_back(N);
1227#ifndef NDEBUG
1228 N->PersistentId = NextPersistentId++;
1229 verifyNode(N);
1230#endif
1231 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1232 DUL->NodeInserted(N);
1233}
1234
1235/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1236/// correspond to it. This is useful when we're about to delete or repurpose
1237/// the node. We don't want future request for structurally identical nodes
1238/// to return N anymore.
1239bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1240 bool Erased = false;
1241 switch (N->getOpcode()) {
1242 case ISD::HANDLENODE: return false; // noop.
1243 case ISD::CONDCODE:
1244 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1245 "Cond code doesn't exist!");
1246 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1247 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1248 break;
1250 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1251 break;
1253 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1254 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1255 ESN->getSymbol(), ESN->getTargetFlags()));
1256 break;
1257 }
1258 case ISD::MCSymbol: {
1259 auto *MCSN = cast<MCSymbolSDNode>(N);
1260 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1261 break;
1262 }
1263 case ISD::VALUETYPE: {
1264 EVT VT = cast<VTSDNode>(N)->getVT();
1265 if (VT.isExtended()) {
1266 Erased = ExtendedValueTypeNodes.erase(VT);
1267 } else {
1268 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1269 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1270 }
1271 break;
1272 }
1273 default:
1274 // Remove it from the CSE Map.
1275 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1276 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1277 Erased = CSEMap.RemoveNode(N);
1278 break;
1279 }
1280#ifndef NDEBUG
1281 // Verify that the node was actually in one of the CSE maps, unless it has a
1282 // glue result (which cannot be CSE'd) or is one of the special cases that are
1283 // not subject to CSE.
1284 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1285 !N->isMachineOpcode() && !doNotCSE(N)) {
1286 N->dump(this);
1287 dbgs() << "\n";
1288 llvm_unreachable("Node is not in map!");
1289 }
1290#endif
1291 return Erased;
1292}
1293
1294/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1295/// maps and modified in place. Add it back to the CSE maps, unless an identical
1296/// node already exists, in which case transfer all its users to the existing
1297/// node. This transfer can potentially trigger recursive merging.
1298void
1299SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1300 // For node types that aren't CSE'd, just act as if no identical node
1301 // already exists.
1302 if (!doNotCSE(N)) {
1303 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1304 if (Existing != N) {
1305 // If there was already an existing matching node, use ReplaceAllUsesWith
1306 // to replace the dead one with the existing one. This can cause
1307 // recursive merging of other unrelated nodes down the line.
1308 Existing->intersectFlagsWith(N->getFlags());
1309 if (auto *MemNode = dyn_cast<MemSDNode>(Existing))
1310 MemNode->refineRanges(cast<MemSDNode>(N)->memoperands());
1311 ReplaceAllUsesWith(N, Existing);
1312
1313 // N is now dead. Inform the listeners and delete it.
1314 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1315 DUL->NodeDeleted(N, Existing);
1316 DeleteNodeNotInCSEMaps(N);
1317 return;
1318 }
1319 }
1320
1321 // If the node doesn't already exist, we updated it. Inform listeners.
1322 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1323 DUL->NodeUpdated(N);
1324}
1325
1326/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1327/// were replaced with those specified. If this node is never memoized,
1328/// return null, otherwise return a pointer to the slot it would take. If a
1329/// node already exists with these operands, the slot will be non-null.
1330SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1331 void *&InsertPos) {
1332 if (doNotCSE(N))
1333 return nullptr;
1334
1335 SDValue Ops[] = { Op };
1336 FoldingSetNodeID ID;
1337 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1339 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1340 if (Node)
1341 Node->intersectFlagsWith(N->getFlags());
1342 return Node;
1343}
1344
1345/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1346/// were replaced with those specified. If this node is never memoized,
1347/// return null, otherwise return a pointer to the slot it would take. If a
1348/// node already exists with these operands, the slot will be non-null.
1349SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1350 SDValue Op1, SDValue Op2,
1351 void *&InsertPos) {
1352 if (doNotCSE(N))
1353 return nullptr;
1354
1355 SDValue Ops[] = { Op1, Op2 };
1356 FoldingSetNodeID ID;
1357 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1359 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1360 if (Node)
1361 Node->intersectFlagsWith(N->getFlags());
1362 return Node;
1363}
1364
1365/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1366/// were replaced with those specified. If this node is never memoized,
1367/// return null, otherwise return a pointer to the slot it would take. If a
1368/// node already exists with these operands, the slot will be non-null.
1369SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1370 void *&InsertPos) {
1371 if (doNotCSE(N))
1372 return nullptr;
1373
1374 FoldingSetNodeID ID;
1375 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1377 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1378 if (Node)
1379 Node->intersectFlagsWith(N->getFlags());
1380 return Node;
1381}
1382
1384 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1385 : VT.getTypeForEVT(*getContext());
1386
1387 return getDataLayout().getABITypeAlign(Ty);
1388}
1389
1390// EntryNode could meaningfully have debug info if we can find it...
1392 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1393 getVTList(MVT::Other, MVT::Glue)),
1394 Root(getEntryNode()) {
1395 InsertNode(&EntryNode);
1396 DbgInfo = new SDDbgInfo();
1397}
1398
1400 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1401 const TargetLibraryInfo *LibraryInfo,
1402 const LibcallLoweringInfo *LibcallsInfo,
1403 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1405 FunctionVarLocs const *VarLocs) {
1406 MF = &NewMF;
1407 SDAGISelPass = PassPtr;
1408 ORE = &NewORE;
1411 LibInfo = LibraryInfo;
1412 Libcalls = LibcallsInfo;
1413 Context = &MF->getFunction().getContext();
1414 UA = NewUA;
1415 PSI = PSIin;
1416 BFI = BFIin;
1417 MMI = &MMIin;
1418 FnVarLocs = VarLocs;
1419}
1420
1422 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1423 allnodes_clear();
1424 OperandRecycler.clear(OperandAllocator);
1425 delete DbgInfo;
1426}
1427
1429 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1430}
1431
1432void SelectionDAG::allnodes_clear() {
1433 assert(&*AllNodes.begin() == &EntryNode);
1434 AllNodes.remove(AllNodes.begin());
1435 while (!AllNodes.empty())
1436 DeallocateNode(&AllNodes.front());
1437#ifndef NDEBUG
1438 NextPersistentId = 0;
1439#endif
1440}
1441
1442SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1443 void *&InsertPos) {
1444 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1445 if (N) {
1446 switch (N->getOpcode()) {
1447 default: break;
1448 case ISD::Constant:
1449 case ISD::ConstantFP:
1450 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1451 "debug location. Use another overload.");
1452 }
1453 }
1454 return N;
1455}
1456
1457SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1458 const SDLoc &DL, void *&InsertPos) {
1459 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1460 if (N) {
1461 switch (N->getOpcode()) {
1462 case ISD::Constant:
1463 case ISD::ConstantFP:
1464 // Erase debug location from the node if the node is used at several
1465 // different places. Do not propagate one location to all uses as it
1466 // will cause a worse single stepping debugging experience.
1467 if (N->getDebugLoc() != DL.getDebugLoc())
1468 N->setDebugLoc(DebugLoc());
1469 break;
1470 default:
1471 // When the node's point of use is located earlier in the instruction
1472 // sequence than its prior point of use, update its debug info to the
1473 // earlier location.
1474 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1475 N->setDebugLoc(DL.getDebugLoc());
1476 break;
1477 }
1478 }
1479 return N;
1480}
1481
1483 allnodes_clear();
1484 OperandRecycler.clear(OperandAllocator);
1485 OperandAllocator.Reset();
1486 CSEMap.clear();
1487
1488 ExtendedValueTypeNodes.clear();
1489 ExternalSymbols.clear();
1490 TargetExternalSymbols.clear();
1491 MCSymbols.clear();
1492 SDEI.clear();
1493 llvm::fill(CondCodeNodes, nullptr);
1494 llvm::fill(ValueTypeNodes, nullptr);
1495
1496 EntryNode.UseList = nullptr;
1497 InsertNode(&EntryNode);
1498 Root = getEntryNode();
1499 DbgInfo->clear();
1500}
1501
1503 return VT.bitsGT(Op.getValueType())
1504 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1505 : getNode(ISD::FP_ROUND, DL, VT, Op,
1506 getIntPtrConstant(0, DL, /*isTarget=*/true));
1507}
1508
1509std::pair<SDValue, SDValue>
1511 const SDLoc &DL, EVT VT) {
1512 assert(!VT.bitsEq(Op.getValueType()) &&
1513 "Strict no-op FP extend/round not allowed.");
1514 SDValue Res =
1515 VT.bitsGT(Op.getValueType())
1516 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1517 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1518 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1519
1520 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1521}
1522
1524 return VT.bitsGT(Op.getValueType()) ?
1525 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1526 getNode(ISD::TRUNCATE, DL, VT, Op);
1527}
1528
1530 return VT.bitsGT(Op.getValueType()) ?
1531 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1532 getNode(ISD::TRUNCATE, DL, VT, Op);
1533}
1534
1536 return VT.bitsGT(Op.getValueType()) ?
1537 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1538 getNode(ISD::TRUNCATE, DL, VT, Op);
1539}
1540
1542 EVT VT) {
1543 assert(!VT.isVector());
1544 auto Type = Op.getValueType();
1545 SDValue DestOp;
1546 if (Type == VT)
1547 return Op;
1548 auto Size = Op.getValueSizeInBits();
1549 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1550 if (DestOp.getValueType() == VT)
1551 return DestOp;
1552
1553 return getAnyExtOrTrunc(DestOp, DL, VT);
1554}
1555
1557 EVT VT) {
1558 assert(!VT.isVector());
1559 auto Type = Op.getValueType();
1560 SDValue DestOp;
1561 if (Type == VT)
1562 return Op;
1563 auto Size = Op.getValueSizeInBits();
1564 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1565 if (DestOp.getValueType() == VT)
1566 return DestOp;
1567
1568 return getSExtOrTrunc(DestOp, DL, VT);
1569}
1570
1572 EVT VT) {
1573 assert(!VT.isVector());
1574 auto Type = Op.getValueType();
1575 SDValue DestOp;
1576 if (Type == VT)
1577 return Op;
1578 auto Size = Op.getValueSizeInBits();
1579 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1580 if (DestOp.getValueType() == VT)
1581 return DestOp;
1582
1583 return getZExtOrTrunc(DestOp, DL, VT);
1584}
1585
1587 EVT OpVT) {
1588 if (VT.bitsLE(Op.getValueType()))
1589 return getNode(ISD::TRUNCATE, SL, VT, Op);
1590
1591 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1592 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1593}
1594
1596 EVT OpVT = Op.getValueType();
1597 assert(VT.isInteger() && OpVT.isInteger() &&
1598 "Cannot getZeroExtendInReg FP types");
1599 assert(VT.isVector() == OpVT.isVector() &&
1600 "getZeroExtendInReg type should be vector iff the operand "
1601 "type is vector!");
1602 assert((!VT.isVector() ||
1604 "Vector element counts must match in getZeroExtendInReg");
1605 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1606 if (OpVT == VT)
1607 return Op;
1608 // TODO: Use computeKnownBits instead of AssertZext.
1609 if (Op.getOpcode() == ISD::AssertZext && cast<VTSDNode>(Op.getOperand(1))
1610 ->getVT()
1611 .getScalarType()
1612 .bitsLE(VT.getScalarType()))
1613 return Op;
1615 VT.getScalarSizeInBits());
1616 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1617}
1618
1620 SDValue EVL, const SDLoc &DL,
1621 EVT VT) {
1622 EVT OpVT = Op.getValueType();
1623 assert(VT.isInteger() && OpVT.isInteger() &&
1624 "Cannot getVPZeroExtendInReg FP types");
1625 assert(VT.isVector() && OpVT.isVector() &&
1626 "getVPZeroExtendInReg type and operand type should be vector!");
1628 "Vector element counts must match in getZeroExtendInReg");
1629 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1630 if (OpVT == VT)
1631 return Op;
1633 VT.getScalarSizeInBits());
1634 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1635 EVL);
1636}
1637
1639 // Only unsigned pointer semantics are supported right now. In the future this
1640 // might delegate to TLI to check pointer signedness.
1641 return getZExtOrTrunc(Op, DL, VT);
1642}
1643
1645 // Only unsigned pointer semantics are supported right now. In the future this
1646 // might delegate to TLI to check pointer signedness.
1647 return getZeroExtendInReg(Op, DL, VT);
1648}
1649
1651 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1652}
1653
1654/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1656 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1657}
1658
1660 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1661 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1662}
1663
1665 SDValue Mask, SDValue EVL, EVT VT) {
1666 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1667 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1668}
1669
1671 SDValue Mask, SDValue EVL) {
1672 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1673}
1674
1676 SDValue Mask, SDValue EVL) {
1677 if (VT.bitsGT(Op.getValueType()))
1678 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1679 if (VT.bitsLT(Op.getValueType()))
1680 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1681 return Op;
1682}
1683
1685 EVT OpVT) {
1686 if (!V)
1687 return getConstant(0, DL, VT);
1688
1689 switch (TLI->getBooleanContents(OpVT)) {
1692 return getConstant(1, DL, VT);
1694 return getAllOnesConstant(DL, VT);
1695 }
1696 llvm_unreachable("Unexpected boolean content enum!");
1697}
1698
1700 bool isT, bool isO) {
1701 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1702 DL, VT, isT, isO);
1703}
1704
1706 bool isT, bool isO) {
1707 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1708}
1709
1711 EVT VT, bool isT, bool isO) {
1712 assert(VT.isInteger() && "Cannot create FP integer constant!");
1713
1714 EVT EltVT = VT.getScalarType();
1715 const ConstantInt *Elt = &Val;
1716
1717 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1718 // to-be-splatted scalar ConstantInt.
1719 if (isa<VectorType>(Elt->getType()))
1720 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1721
1722 // In some cases the vector type is legal but the element type is illegal and
1723 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1724 // inserted value (the type does not need to match the vector element type).
1725 // Any extra bits introduced will be truncated away.
1726 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1728 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1729 APInt NewVal;
1730 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1731 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1732 else
1733 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1734 Elt = ConstantInt::get(*getContext(), NewVal);
1735 }
1736 // In other cases the element type is illegal and needs to be expanded, for
1737 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1738 // the value into n parts and use a vector type with n-times the elements.
1739 // Then bitcast to the type requested.
1740 // Legalizing constants too early makes the DAGCombiner's job harder so we
1741 // only legalize if the DAG tells us we must produce legal types.
1742 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1743 TLI->getTypeAction(*getContext(), EltVT) ==
1745 const APInt &NewVal = Elt->getValue();
1746 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1747 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1748
1749 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1750 if (VT.isScalableVector() ||
1751 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1752 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1753 "Can only handle an even split!");
1754 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1755
1756 SmallVector<SDValue, 2> ScalarParts;
1757 for (unsigned i = 0; i != Parts; ++i)
1758 ScalarParts.push_back(getConstant(
1759 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1760 ViaEltVT, isT, isO));
1761
1762 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1763 }
1764
1765 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1766 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1767
1768 // Check the temporary vector is the correct size. If this fails then
1769 // getTypeToTransformTo() probably returned a type whose size (in bits)
1770 // isn't a power-of-2 factor of the requested type size.
1771 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1772
1773 SmallVector<SDValue, 2> EltParts;
1774 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1775 EltParts.push_back(getConstant(
1776 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1777 ViaEltVT, isT, isO));
1778
1779 // EltParts is currently in little endian order. If we actually want
1780 // big-endian order then reverse it now.
1781 if (getDataLayout().isBigEndian())
1782 std::reverse(EltParts.begin(), EltParts.end());
1783
1784 // The elements must be reversed when the element order is different
1785 // to the endianness of the elements (because the BITCAST is itself a
1786 // vector shuffle in this situation). However, we do not need any code to
1787 // perform this reversal because getConstant() is producing a vector
1788 // splat.
1789 // This situation occurs in MIPS MSA.
1790
1792 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1793 llvm::append_range(Ops, EltParts);
1794
1795 SDValue V =
1796 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1797 return V;
1798 }
1799
1800 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1801 "APInt size does not match type size!");
1802 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1803 SDVTList VTs = getVTList(EltVT);
1805 AddNodeIDNode(ID, Opc, VTs, {});
1806 ID.AddPointer(Elt);
1807 ID.AddBoolean(isO);
1808 void *IP = nullptr;
1809 SDNode *N = nullptr;
1810 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1811 if (!VT.isVector())
1812 return SDValue(N, 0);
1813
1814 if (!N) {
1815 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1816 CSEMap.InsertNode(N, IP);
1817 InsertNode(N);
1818 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1819 }
1820
1821 SDValue Result(N, 0);
1822 if (VT.isVector())
1823 Result = getSplat(VT, DL, Result);
1824 return Result;
1825}
1826
1828 bool isT, bool isO) {
1829 unsigned Size = VT.getScalarSizeInBits();
1830 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1831}
1832
1834 bool IsOpaque) {
1836 IsTarget, IsOpaque);
1837}
1838
1840 bool isTarget) {
1841 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1842}
1843
1845 const SDLoc &DL) {
1846 assert(VT.isInteger() && "Shift amount is not an integer type!");
1847 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1848 return getConstant(Val, DL, ShiftVT);
1849}
1850
1852 const SDLoc &DL) {
1853 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1854 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1855}
1856
1858 bool isTarget) {
1859 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1860}
1861
1863 bool isTarget) {
1864 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1865}
1866
1868 EVT VT, bool isTarget) {
1869 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1870
1871 EVT EltVT = VT.getScalarType();
1872 const ConstantFP *Elt = &V;
1873
1874 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1875 // the to-be-splatted scalar ConstantFP.
1876 if (isa<VectorType>(Elt->getType()))
1877 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1878
1879 // Do the map lookup using the actual bit pattern for the floating point
1880 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1881 // we don't have issues with SNANs.
1882 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1883 SDVTList VTs = getVTList(EltVT);
1885 AddNodeIDNode(ID, Opc, VTs, {});
1886 ID.AddPointer(Elt);
1887 void *IP = nullptr;
1888 SDNode *N = nullptr;
1889 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1890 if (!VT.isVector())
1891 return SDValue(N, 0);
1892
1893 if (!N) {
1894 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1895 CSEMap.InsertNode(N, IP);
1896 InsertNode(N);
1897 }
1898
1899 SDValue Result(N, 0);
1900 if (VT.isVector())
1901 Result = getSplat(VT, DL, Result);
1902 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1903 return Result;
1904}
1905
1907 bool isTarget) {
1908 EVT EltVT = VT.getScalarType();
1909 if (EltVT == MVT::f32)
1910 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1911 if (EltVT == MVT::f64)
1912 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1913 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1914 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1915 bool Ignored;
1916 APFloat APF = APFloat(Val);
1918 &Ignored);
1919 return getConstantFP(APF, DL, VT, isTarget);
1920 }
1921 llvm_unreachable("Unsupported type in getConstantFP");
1922}
1923
1925 EVT VT, int64_t Offset, bool isTargetGA,
1926 unsigned TargetFlags) {
1927 assert((TargetFlags == 0 || isTargetGA) &&
1928 "Cannot set target flags on target-independent globals");
1929
1930 // Truncate (with sign-extension) the offset value to the pointer size.
1932 if (BitWidth < 64)
1934
1935 unsigned Opc;
1936 if (GV->isThreadLocal())
1938 else
1940
1941 SDVTList VTs = getVTList(VT);
1943 AddNodeIDNode(ID, Opc, VTs, {});
1944 ID.AddPointer(GV);
1945 ID.AddInteger(Offset);
1946 ID.AddInteger(TargetFlags);
1947 void *IP = nullptr;
1948 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1949 return SDValue(E, 0);
1950
1951 auto *N = newSDNode<GlobalAddressSDNode>(
1952 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1953 CSEMap.InsertNode(N, IP);
1954 InsertNode(N);
1955 return SDValue(N, 0);
1956}
1957
1959 SDVTList VTs = getVTList(MVT::Untyped);
1962 ID.AddPointer(GV);
1963 void *IP = nullptr;
1964 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP))
1965 return SDValue(E, 0);
1966
1967 auto *N = newSDNode<DeactivationSymbolSDNode>(GV, VTs);
1968 CSEMap.InsertNode(N, IP);
1969 InsertNode(N);
1970 return SDValue(N, 0);
1971}
1972
1973SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1974 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1975 SDVTList VTs = getVTList(VT);
1977 AddNodeIDNode(ID, Opc, VTs, {});
1978 ID.AddInteger(FI);
1979 void *IP = nullptr;
1980 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1981 return SDValue(E, 0);
1982
1983 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1984 CSEMap.InsertNode(N, IP);
1985 InsertNode(N);
1986 return SDValue(N, 0);
1987}
1988
1989SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1990 unsigned TargetFlags) {
1991 assert((TargetFlags == 0 || isTarget) &&
1992 "Cannot set target flags on target-independent jump tables");
1993 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1994 SDVTList VTs = getVTList(VT);
1996 AddNodeIDNode(ID, Opc, VTs, {});
1997 ID.AddInteger(JTI);
1998 ID.AddInteger(TargetFlags);
1999 void *IP = nullptr;
2000 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2001 return SDValue(E, 0);
2002
2003 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
2004 CSEMap.InsertNode(N, IP);
2005 InsertNode(N);
2006 return SDValue(N, 0);
2007}
2008
2010 const SDLoc &DL) {
2012 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Other, Chain,
2013 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
2014}
2015
2017 MaybeAlign Alignment, int Offset,
2018 bool isTarget, unsigned TargetFlags) {
2019 assert((TargetFlags == 0 || isTarget) &&
2020 "Cannot set target flags on target-independent globals");
2021 if (!Alignment)
2022 Alignment = shouldOptForSize()
2023 ? getDataLayout().getABITypeAlign(C->getType())
2024 : getDataLayout().getPrefTypeAlign(C->getType());
2025 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2026 SDVTList VTs = getVTList(VT);
2028 AddNodeIDNode(ID, Opc, VTs, {});
2029 ID.AddInteger(Alignment->value());
2030 ID.AddInteger(Offset);
2031 ID.AddPointer(C);
2032 ID.AddInteger(TargetFlags);
2033 void *IP = nullptr;
2034 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2035 return SDValue(E, 0);
2036
2037 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2038 TargetFlags);
2039 CSEMap.InsertNode(N, IP);
2040 InsertNode(N);
2041 SDValue V = SDValue(N, 0);
2042 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
2043 return V;
2044}
2045
2047 MaybeAlign Alignment, int Offset,
2048 bool isTarget, unsigned TargetFlags) {
2049 assert((TargetFlags == 0 || isTarget) &&
2050 "Cannot set target flags on target-independent globals");
2051 if (!Alignment)
2052 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2053 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2054 SDVTList VTs = getVTList(VT);
2056 AddNodeIDNode(ID, Opc, VTs, {});
2057 ID.AddInteger(Alignment->value());
2058 ID.AddInteger(Offset);
2059 C->addSelectionDAGCSEId(ID);
2060 ID.AddInteger(TargetFlags);
2061 void *IP = nullptr;
2062 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2063 return SDValue(E, 0);
2064
2065 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2066 TargetFlags);
2067 CSEMap.InsertNode(N, IP);
2068 InsertNode(N);
2069 return SDValue(N, 0);
2070}
2071
2074 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2075 ID.AddPointer(MBB);
2076 void *IP = nullptr;
2077 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2078 return SDValue(E, 0);
2079
2080 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2081 CSEMap.InsertNode(N, IP);
2082 InsertNode(N);
2083 return SDValue(N, 0);
2084}
2085
2087 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2088 ValueTypeNodes.size())
2089 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2090
2091 SDNode *&N = VT.isExtended() ?
2092 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2093
2094 if (N) return SDValue(N, 0);
2095 N = newSDNode<VTSDNode>(VT);
2096 InsertNode(N);
2097 return SDValue(N, 0);
2098}
2099
2101 SDNode *&N = ExternalSymbols[Sym];
2102 if (N) return SDValue(N, 0);
2103 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2104 InsertNode(N);
2105 return SDValue(N, 0);
2106}
2107
2108SDValue SelectionDAG::getExternalSymbol(RTLIB::LibcallImpl Libcall, EVT VT) {
2110 return getExternalSymbol(SymName.data(), VT);
2111}
2112
2114 SDNode *&N = MCSymbols[Sym];
2115 if (N)
2116 return SDValue(N, 0);
2117 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2118 InsertNode(N);
2119 return SDValue(N, 0);
2120}
2121
2123 unsigned TargetFlags) {
2124 SDNode *&N =
2125 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2126 if (N) return SDValue(N, 0);
2127 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2128 InsertNode(N);
2129 return SDValue(N, 0);
2130}
2131
2133 EVT VT, unsigned TargetFlags) {
2135 return getTargetExternalSymbol(SymName.data(), VT, TargetFlags);
2136}
2137
2139 if ((unsigned)Cond >= CondCodeNodes.size())
2140 CondCodeNodes.resize(Cond+1);
2141
2142 if (!CondCodeNodes[Cond]) {
2143 auto *N = newSDNode<CondCodeSDNode>(Cond);
2144 CondCodeNodes[Cond] = N;
2145 InsertNode(N);
2146 }
2147
2148 return SDValue(CondCodeNodes[Cond], 0);
2149}
2150
2152 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2153 "APInt size does not match type size!");
2154
2155 if (MulImm == 0)
2156 return getConstant(0, DL, VT);
2157
2158 const MachineFunction &MF = getMachineFunction();
2159 const Function &F = MF.getFunction();
2160 ConstantRange CR = getVScaleRange(&F, 64);
2161 if (const APInt *C = CR.getSingleElement())
2162 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2163
2164 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2165}
2166
2167/// \returns a value of type \p VT that represents the runtime value of \p
2168/// Quantity, i.e. scaled by vscale if it's scalable, or a fixed constant
2169/// otherwise. Quantity should be a FixedOrScalableQuantity, i.e. ElementCount
2170/// or TypeSize.
2171template <typename Ty>
2173 EVT VT, Ty Quantity) {
2174 if (Quantity.isScalable())
2175 return DAG.getVScale(
2176 DL, VT, APInt(VT.getSizeInBits(), Quantity.getKnownMinValue()));
2177
2178 return DAG.getConstant(Quantity.getKnownMinValue(), DL, VT);
2179}
2180
2182 ElementCount EC) {
2183 return getFixedOrScalableQuantity(*this, DL, VT, EC);
2184}
2185
2187 return getFixedOrScalableQuantity(*this, DL, VT, TS);
2188}
2189
2191 ElementCount EC) {
2192 EVT IdxVT = TLI->getVectorIdxTy(getDataLayout());
2193 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), DataVT);
2194 return getNode(ISD::GET_ACTIVE_LANE_MASK, DL, MaskVT,
2195 getConstant(0, DL, IdxVT), getElementCount(DL, IdxVT, EC));
2196}
2197
2199 APInt One(ResVT.getScalarSizeInBits(), 1);
2200 return getStepVector(DL, ResVT, One);
2201}
2202
2204 const APInt &StepVal) {
2205 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2206 if (ResVT.isScalableVector())
2207 return getNode(
2208 ISD::STEP_VECTOR, DL, ResVT,
2209 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2210
2211 SmallVector<SDValue, 16> OpsStepConstants;
2212 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2213 OpsStepConstants.push_back(
2214 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2215 return getBuildVector(ResVT, DL, OpsStepConstants);
2216}
2217
2218/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2219/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2224
2226 SDValue N2, ArrayRef<int> Mask) {
2227 assert(VT.getVectorNumElements() == Mask.size() &&
2228 "Must have the same number of vector elements as mask elements!");
2229 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2230 "Invalid VECTOR_SHUFFLE");
2231
2232 // Canonicalize shuffle undef, undef -> undef
2233 if (N1.isUndef() && N2.isUndef())
2234 return getUNDEF(VT);
2235
2236 // Validate that all indices in Mask are within the range of the elements
2237 // input to the shuffle.
2238 int NElts = Mask.size();
2239 assert(llvm::all_of(Mask,
2240 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2241 "Index out of range");
2242
2243 // Copy the mask so we can do any needed cleanup.
2244 SmallVector<int, 8> MaskVec(Mask);
2245
2246 // Canonicalize shuffle v, v -> v, undef
2247 if (N1 == N2) {
2248 N2 = getUNDEF(VT);
2249 for (int i = 0; i != NElts; ++i)
2250 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2251 }
2252
2253 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2254 if (N1.isUndef())
2255 commuteShuffle(N1, N2, MaskVec);
2256
2257 if (TLI->hasVectorBlend()) {
2258 // If shuffling a splat, try to blend the splat instead. We do this here so
2259 // that even when this arises during lowering we don't have to re-handle it.
2260 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2261 BitVector UndefElements;
2262 SDValue Splat = BV->getSplatValue(&UndefElements);
2263 if (!Splat)
2264 return;
2265
2266 for (int i = 0; i < NElts; ++i) {
2267 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2268 continue;
2269
2270 // If this input comes from undef, mark it as such.
2271 if (UndefElements[MaskVec[i] - Offset]) {
2272 MaskVec[i] = -1;
2273 continue;
2274 }
2275
2276 // If we can blend a non-undef lane, use that instead.
2277 if (!UndefElements[i])
2278 MaskVec[i] = i + Offset;
2279 }
2280 };
2281 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2282 BlendSplat(N1BV, 0);
2283 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2284 BlendSplat(N2BV, NElts);
2285 }
2286
2287 // Canonicalize all index into lhs, -> shuffle lhs, undef
2288 // Canonicalize all index into rhs, -> shuffle rhs, undef
2289 bool AllLHS = true, AllRHS = true;
2290 bool N2Undef = N2.isUndef();
2291 for (int i = 0; i != NElts; ++i) {
2292 if (MaskVec[i] >= NElts) {
2293 if (N2Undef)
2294 MaskVec[i] = -1;
2295 else
2296 AllLHS = false;
2297 } else if (MaskVec[i] >= 0) {
2298 AllRHS = false;
2299 }
2300 }
2301 if (AllLHS && AllRHS)
2302 return getUNDEF(VT);
2303 if (AllLHS && !N2Undef)
2304 N2 = getUNDEF(VT);
2305 if (AllRHS) {
2306 N1 = getUNDEF(VT);
2307 commuteShuffle(N1, N2, MaskVec);
2308 }
2309 // Reset our undef status after accounting for the mask.
2310 N2Undef = N2.isUndef();
2311 // Re-check whether both sides ended up undef.
2312 if (N1.isUndef() && N2Undef)
2313 return getUNDEF(VT);
2314
2315 // If Identity shuffle return that node.
2316 bool Identity = true, AllSame = true;
2317 for (int i = 0; i != NElts; ++i) {
2318 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2319 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2320 }
2321 if (Identity && NElts)
2322 return N1;
2323
2324 // Shuffling a constant splat doesn't change the result.
2325 if (N2Undef) {
2326 SDValue V = N1;
2327
2328 // Look through any bitcasts. We check that these don't change the number
2329 // (and size) of elements and just changes their types.
2330 while (V.getOpcode() == ISD::BITCAST)
2331 V = V->getOperand(0);
2332
2333 // A splat should always show up as a build vector node.
2334 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2335 BitVector UndefElements;
2336 SDValue Splat = BV->getSplatValue(&UndefElements);
2337 // If this is a splat of an undef, shuffling it is also undef.
2338 if (Splat && Splat.isUndef())
2339 return getUNDEF(VT);
2340
2341 bool SameNumElts =
2342 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2343
2344 // We only have a splat which can skip shuffles if there is a splatted
2345 // value and no undef lanes rearranged by the shuffle.
2346 if (Splat && UndefElements.none()) {
2347 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2348 // number of elements match or the value splatted is a zero constant.
2349 if (SameNumElts || isNullConstant(Splat))
2350 return N1;
2351 }
2352
2353 // If the shuffle itself creates a splat, build the vector directly.
2354 if (AllSame && SameNumElts) {
2355 EVT BuildVT = BV->getValueType(0);
2356 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2357 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2358
2359 // We may have jumped through bitcasts, so the type of the
2360 // BUILD_VECTOR may not match the type of the shuffle.
2361 if (BuildVT != VT)
2362 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2363 return NewBV;
2364 }
2365 }
2366 }
2367
2368 SDVTList VTs = getVTList(VT);
2370 SDValue Ops[2] = { N1, N2 };
2372 for (int i = 0; i != NElts; ++i)
2373 ID.AddInteger(MaskVec[i]);
2374
2375 void* IP = nullptr;
2376 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2377 return SDValue(E, 0);
2378
2379 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2380 // SDNode doesn't have access to it. This memory will be "leaked" when
2381 // the node is deallocated, but recovered when the NodeAllocator is released.
2382 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2383 llvm::copy(MaskVec, MaskAlloc);
2384
2385 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2386 dl.getDebugLoc(), MaskAlloc);
2387 createOperands(N, Ops);
2388
2389 CSEMap.InsertNode(N, IP);
2390 InsertNode(N);
2391 SDValue V = SDValue(N, 0);
2392 NewSDValueDbgMsg(V, "Creating new node: ", this);
2393 return V;
2394}
2395
2397 EVT VT = SV.getValueType(0);
2398 SmallVector<int, 8> MaskVec(SV.getMask());
2400
2401 SDValue Op0 = SV.getOperand(0);
2402 SDValue Op1 = SV.getOperand(1);
2403 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2404}
2405
2407 SDVTList VTs = getVTList(VT);
2409 AddNodeIDNode(ID, ISD::Register, VTs, {});
2410 ID.AddInteger(Reg.id());
2411 void *IP = nullptr;
2412 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2413 return SDValue(E, 0);
2414
2415 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2416 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2417 CSEMap.InsertNode(N, IP);
2418 InsertNode(N);
2419 return SDValue(N, 0);
2420}
2421
2424 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2425 ID.AddPointer(RegMask);
2426 void *IP = nullptr;
2427 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2428 return SDValue(E, 0);
2429
2430 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2431 CSEMap.InsertNode(N, IP);
2432 InsertNode(N);
2433 return SDValue(N, 0);
2434}
2435
2437 MCSymbol *Label) {
2438 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2439}
2440
2441SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2442 SDValue Root, MCSymbol *Label) {
2444 SDValue Ops[] = { Root };
2445 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2446 ID.AddPointer(Label);
2447 void *IP = nullptr;
2448 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2449 return SDValue(E, 0);
2450
2451 auto *N =
2452 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2453 createOperands(N, Ops);
2454
2455 CSEMap.InsertNode(N, IP);
2456 InsertNode(N);
2457 return SDValue(N, 0);
2458}
2459
2461 int64_t Offset, bool isTarget,
2462 unsigned TargetFlags) {
2463 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2464 SDVTList VTs = getVTList(VT);
2465
2467 AddNodeIDNode(ID, Opc, VTs, {});
2468 ID.AddPointer(BA);
2469 ID.AddInteger(Offset);
2470 ID.AddInteger(TargetFlags);
2471 void *IP = nullptr;
2472 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2473 return SDValue(E, 0);
2474
2475 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2476 CSEMap.InsertNode(N, IP);
2477 InsertNode(N);
2478 return SDValue(N, 0);
2479}
2480
2483 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2484 ID.AddPointer(V);
2485
2486 void *IP = nullptr;
2487 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2488 return SDValue(E, 0);
2489
2490 auto *N = newSDNode<SrcValueSDNode>(V);
2491 CSEMap.InsertNode(N, IP);
2492 InsertNode(N);
2493 return SDValue(N, 0);
2494}
2495
2498 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2499 ID.AddPointer(MD);
2500
2501 void *IP = nullptr;
2502 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2503 return SDValue(E, 0);
2504
2505 auto *N = newSDNode<MDNodeSDNode>(MD);
2506 CSEMap.InsertNode(N, IP);
2507 InsertNode(N);
2508 return SDValue(N, 0);
2509}
2510
2512 if (VT == V.getValueType())
2513 return V;
2514
2515 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2516}
2517
2519 unsigned SrcAS, unsigned DestAS) {
2520 SDVTList VTs = getVTList(VT);
2521 SDValue Ops[] = {Ptr};
2524 ID.AddInteger(SrcAS);
2525 ID.AddInteger(DestAS);
2526
2527 void *IP = nullptr;
2528 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2529 return SDValue(E, 0);
2530
2531 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2532 VTs, SrcAS, DestAS);
2533 createOperands(N, Ops);
2534
2535 CSEMap.InsertNode(N, IP);
2536 InsertNode(N);
2537 return SDValue(N, 0);
2538}
2539
2541 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2542}
2543
2545 bool PoisonOnly) {
2546 if (isGuaranteedNotToBeUndefOrPoison(V, DemandedElts, PoisonOnly))
2547 return V;
2548 return getFreeze(V);
2549}
2550
2551/// getShiftAmountOperand - Return the specified value casted to
2552/// the target's desired shift amount type.
2554 EVT OpTy = Op.getValueType();
2555 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2556 if (OpTy == ShTy || OpTy.isVector()) return Op;
2557
2558 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2559}
2560
2562 SDLoc dl(Node);
2564 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2565 EVT VT = Node->getValueType(0);
2566 SDValue Tmp1 = Node->getOperand(0);
2567 SDValue Tmp2 = Node->getOperand(1);
2568 const MaybeAlign MA(Node->getConstantOperandVal(3));
2569
2570 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2571 Tmp2, MachinePointerInfo(V));
2572 SDValue VAList = VAListLoad;
2573
2574 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2575 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2576 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2577
2578 VAList = getNode(
2579 ISD::AND, dl, VAList.getValueType(), VAList,
2580 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2581 }
2582
2583 // Increment the pointer, VAList, to the next vaarg
2584 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2585 getConstant(getDataLayout().getTypeAllocSize(
2586 VT.getTypeForEVT(*getContext())),
2587 dl, VAList.getValueType()));
2588 // Store the incremented VAList to the legalized pointer
2589 Tmp1 =
2590 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2591 // Load the actual argument out of the pointer VAList
2592 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2593}
2594
2596 SDLoc dl(Node);
2598 // This defaults to loading a pointer from the input and storing it to the
2599 // output, returning the chain.
2600 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2601 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2602 SDValue Tmp1 =
2603 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2604 Node->getOperand(2), MachinePointerInfo(VS));
2605 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2606 MachinePointerInfo(VD));
2607}
2608
2610 const DataLayout &DL = getDataLayout();
2611 Type *Ty = VT.getTypeForEVT(*getContext());
2612 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2613
2614 if (TLI->isTypeLegal(VT) || !VT.isVector())
2615 return RedAlign;
2616
2617 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2618 const Align StackAlign = TFI->getStackAlign();
2619
2620 // See if we can choose a smaller ABI alignment in cases where it's an
2621 // illegal vector type that will get broken down.
2622 if (RedAlign > StackAlign) {
2623 EVT IntermediateVT;
2624 MVT RegisterVT;
2625 unsigned NumIntermediates;
2626 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2627 NumIntermediates, RegisterVT);
2628 Ty = IntermediateVT.getTypeForEVT(*getContext());
2629 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2630 if (RedAlign2 < RedAlign)
2631 RedAlign = RedAlign2;
2632
2633 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2634 // If the stack is not realignable, the alignment should be limited to the
2635 // StackAlignment
2636 RedAlign = std::min(RedAlign, StackAlign);
2637 }
2638
2639 return RedAlign;
2640}
2641
2643 MachineFrameInfo &MFI = MF->getFrameInfo();
2644 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2645 int StackID = 0;
2646 if (Bytes.isScalable())
2647 StackID = TFI->getStackIDForScalableVectors();
2648 // The stack id gives an indication of whether the object is scalable or
2649 // not, so it's safe to pass in the minimum size here.
2650 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2651 false, nullptr, StackID);
2652 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2653}
2654
2656 Type *Ty = VT.getTypeForEVT(*getContext());
2657 Align StackAlign =
2658 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2659 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2660}
2661
2663 TypeSize VT1Size = VT1.getStoreSize();
2664 TypeSize VT2Size = VT2.getStoreSize();
2665 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2666 "Don't know how to choose the maximum size when creating a stack "
2667 "temporary");
2668 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2669 ? VT1Size
2670 : VT2Size;
2671
2672 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2673 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2674 const DataLayout &DL = getDataLayout();
2675 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2676 return CreateStackTemporary(Bytes, Align);
2677}
2678
2680 ISD::CondCode Cond, const SDLoc &dl,
2681 SDNodeFlags Flags) {
2682 EVT OpVT = N1.getValueType();
2683
2684 auto GetUndefBooleanConstant = [&]() {
2685 if (VT.getScalarType() == MVT::i1 ||
2686 TLI->getBooleanContents(OpVT) ==
2688 return getUNDEF(VT);
2689 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2690 // so we cannot use getUNDEF(). Return zero instead.
2691 return getConstant(0, dl, VT);
2692 };
2693
2694 // These setcc operations always fold.
2695 switch (Cond) {
2696 default: break;
2697 case ISD::SETFALSE:
2698 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2699 case ISD::SETTRUE:
2700 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2701
2702 case ISD::SETOEQ:
2703 case ISD::SETOGT:
2704 case ISD::SETOGE:
2705 case ISD::SETOLT:
2706 case ISD::SETOLE:
2707 case ISD::SETONE:
2708 case ISD::SETO:
2709 case ISD::SETUO:
2710 case ISD::SETUEQ:
2711 case ISD::SETUNE:
2712 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2713 break;
2714 }
2715
2716 if (OpVT.isInteger()) {
2717 // For EQ and NE, we can always pick a value for the undef to make the
2718 // predicate pass or fail, so we can return undef.
2719 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2720 // icmp eq/ne X, undef -> undef.
2721 if ((N1.isUndef() || N2.isUndef()) &&
2722 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2723 return GetUndefBooleanConstant();
2724
2725 // If both operands are undef, we can return undef for int comparison.
2726 // icmp undef, undef -> undef.
2727 if (N1.isUndef() && N2.isUndef())
2728 return GetUndefBooleanConstant();
2729
2730 // icmp X, X -> true/false
2731 // icmp X, undef -> true/false because undef could be X.
2732 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2733 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2734 }
2735
2737 const APInt &C2 = N2C->getAPIntValue();
2739 const APInt &C1 = N1C->getAPIntValue();
2740
2742 dl, VT, OpVT);
2743 }
2744 }
2745
2746 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2747 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2748
2749 if (N1CFP && N2CFP) {
2750 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2751 switch (Cond) {
2752 default: break;
2753 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2754 return GetUndefBooleanConstant();
2755 [[fallthrough]];
2756 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2757 OpVT);
2758 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2759 return GetUndefBooleanConstant();
2760 [[fallthrough]];
2762 R==APFloat::cmpLessThan, dl, VT,
2763 OpVT);
2764 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2765 return GetUndefBooleanConstant();
2766 [[fallthrough]];
2767 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2768 OpVT);
2769 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2770 return GetUndefBooleanConstant();
2771 [[fallthrough]];
2773 VT, OpVT);
2774 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2775 return GetUndefBooleanConstant();
2776 [[fallthrough]];
2778 R==APFloat::cmpEqual, dl, VT,
2779 OpVT);
2780 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2781 return GetUndefBooleanConstant();
2782 [[fallthrough]];
2784 R==APFloat::cmpEqual, dl, VT, OpVT);
2785 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2786 OpVT);
2787 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2788 OpVT);
2790 R==APFloat::cmpEqual, dl, VT,
2791 OpVT);
2792 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2793 OpVT);
2795 R==APFloat::cmpLessThan, dl, VT,
2796 OpVT);
2798 R==APFloat::cmpUnordered, dl, VT,
2799 OpVT);
2801 VT, OpVT);
2802 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2803 OpVT);
2804 }
2805 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2806 // Ensure that the constant occurs on the RHS.
2808 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2809 return SDValue();
2810 return getSetCC(dl, VT, N2, N1, SwappedCond, /*Chian=*/{},
2811 /*IsSignaling=*/false, Flags);
2812 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2813 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2814 // If an operand is known to be a nan (or undef that could be a nan), we can
2815 // fold it.
2816 // Choosing NaN for the undef will always make unordered comparison succeed
2817 // and ordered comparison fails.
2818 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2819 switch (ISD::getUnorderedFlavor(Cond)) {
2820 default:
2821 llvm_unreachable("Unknown flavor!");
2822 case 0: // Known false.
2823 return getBoolConstant(false, dl, VT, OpVT);
2824 case 1: // Known true.
2825 return getBoolConstant(true, dl, VT, OpVT);
2826 case 2: // Undefined.
2827 return GetUndefBooleanConstant();
2828 }
2829 }
2830
2831 // Could not fold it.
2832 return SDValue();
2833}
2834
2835/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2836/// use this predicate to simplify operations downstream.
2838 unsigned BitWidth = Op.getScalarValueSizeInBits();
2840}
2841
2842// TODO: Should have argument to specify if sign bit of nan is ignorable.
2844 if (Depth >= MaxRecursionDepth)
2845 return false; // Limit search depth.
2846
2847 unsigned Opc = Op.getOpcode();
2848 switch (Opc) {
2849 case ISD::FABS:
2850 return true;
2851 case ISD::AssertNoFPClass: {
2852 FPClassTest NoFPClass =
2853 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2854
2855 const FPClassTest TestMask = fcNan | fcNegative;
2856 return (NoFPClass & TestMask) == TestMask;
2857 }
2858 case ISD::ARITH_FENCE:
2859 return SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2860 case ISD::FEXP:
2861 case ISD::FEXP2:
2862 case ISD::FEXP10:
2863 return Op->getFlags().hasNoNaNs();
2864 case ISD::FMINNUM:
2865 case ISD::FMINNUM_IEEE:
2866 case ISD::FMINIMUM:
2867 case ISD::FMINIMUMNUM:
2868 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2869 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2870 case ISD::FMAXNUM:
2871 case ISD::FMAXNUM_IEEE:
2872 case ISD::FMAXIMUM:
2873 case ISD::FMAXIMUMNUM:
2874 // TODO: If we can ignore the sign bit of nans, only one side being known 0
2875 // is sufficient.
2876 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2877 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2878 default:
2879 return false;
2880 }
2881
2882 llvm_unreachable("covered opcode switch");
2883}
2884
2885/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2886/// this predicate to simplify operations downstream. Mask is known to be zero
2887/// for bits that V cannot have.
2889 unsigned Depth) const {
2890 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2891}
2892
2893/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2894/// DemandedElts. We use this predicate to simplify operations downstream.
2895/// Mask is known to be zero for bits that V cannot have.
2897 const APInt &DemandedElts,
2898 unsigned Depth) const {
2899 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2900}
2901
2902/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2903/// DemandedElts. We use this predicate to simplify operations downstream.
2905 unsigned Depth /* = 0 */) const {
2906 return computeKnownBits(V, DemandedElts, Depth).isZero();
2907}
2908
2909/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2911 unsigned Depth) const {
2912 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2913}
2914
2916 const APInt &DemandedElts,
2917 unsigned Depth) const {
2918 EVT VT = Op.getValueType();
2919 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2920
2921 unsigned NumElts = VT.getVectorNumElements();
2922 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2923
2924 APInt KnownZeroElements = APInt::getZero(NumElts);
2925 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2926 if (!DemandedElts[EltIdx])
2927 continue; // Don't query elements that are not demanded.
2928 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2929 if (MaskedVectorIsZero(Op, Mask, Depth))
2930 KnownZeroElements.setBit(EltIdx);
2931 }
2932 return KnownZeroElements;
2933}
2934
2935/// isSplatValue - Return true if the vector V has the same value
2936/// across all DemandedElts. For scalable vectors, we don't know the
2937/// number of lanes at compile time. Instead, we use a 1 bit APInt
2938/// to represent a conservative value for all lanes; that is, that
2939/// one bit value is implicitly splatted across all lanes.
2940bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2941 APInt &UndefElts, unsigned Depth) const {
2942 unsigned Opcode = V.getOpcode();
2943 EVT VT = V.getValueType();
2944 assert(VT.isVector() && "Vector type expected");
2945 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2946 "scalable demanded bits are ignored");
2947
2948 if (!DemandedElts)
2949 return false; // No demanded elts, better to assume we don't know anything.
2950
2951 if (Depth >= MaxRecursionDepth)
2952 return false; // Limit search depth.
2953
2954 // Deal with some common cases here that work for both fixed and scalable
2955 // vector types.
2956 switch (Opcode) {
2957 case ISD::SPLAT_VECTOR:
2958 UndefElts = V.getOperand(0).isUndef()
2959 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2960 : APInt(DemandedElts.getBitWidth(), 0);
2961 return true;
2962 case ISD::ADD:
2963 case ISD::SUB:
2964 case ISD::AND:
2965 case ISD::XOR:
2966 case ISD::OR: {
2967 APInt UndefLHS, UndefRHS;
2968 SDValue LHS = V.getOperand(0);
2969 SDValue RHS = V.getOperand(1);
2970 // Only recognize splats with the same demanded undef elements for both
2971 // operands, otherwise we might fail to handle binop-specific undef
2972 // handling.
2973 // e.g. (and undef, 0) -> 0 etc.
2974 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2975 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
2976 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
2977 UndefElts = UndefLHS | UndefRHS;
2978 return true;
2979 }
2980 return false;
2981 }
2982 case ISD::ABS:
2983 case ISD::TRUNCATE:
2984 case ISD::SIGN_EXTEND:
2985 case ISD::ZERO_EXTEND:
2986 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2987 default:
2988 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2989 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2990 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2991 Depth);
2992 break;
2993 }
2994
2995 // We don't support other cases than those above for scalable vectors at
2996 // the moment.
2997 if (VT.isScalableVector())
2998 return false;
2999
3000 unsigned NumElts = VT.getVectorNumElements();
3001 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
3002 UndefElts = APInt::getZero(NumElts);
3003
3004 switch (Opcode) {
3005 case ISD::BUILD_VECTOR: {
3006 SDValue Scl;
3007 for (unsigned i = 0; i != NumElts; ++i) {
3008 SDValue Op = V.getOperand(i);
3009 if (Op.isUndef()) {
3010 UndefElts.setBit(i);
3011 continue;
3012 }
3013 if (!DemandedElts[i])
3014 continue;
3015 if (Scl && Scl != Op)
3016 return false;
3017 Scl = Op;
3018 }
3019 return true;
3020 }
3021 case ISD::VECTOR_SHUFFLE: {
3022 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
3023 APInt DemandedLHS = APInt::getZero(NumElts);
3024 APInt DemandedRHS = APInt::getZero(NumElts);
3025 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
3026 for (int i = 0; i != (int)NumElts; ++i) {
3027 int M = Mask[i];
3028 if (M < 0) {
3029 UndefElts.setBit(i);
3030 continue;
3031 }
3032 if (!DemandedElts[i])
3033 continue;
3034 if (M < (int)NumElts)
3035 DemandedLHS.setBit(M);
3036 else
3037 DemandedRHS.setBit(M - NumElts);
3038 }
3039
3040 // If we aren't demanding either op, assume there's no splat.
3041 // If we are demanding both ops, assume there's no splat.
3042 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
3043 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3044 return false;
3045
3046 // See if the demanded elts of the source op is a splat or we only demand
3047 // one element, which should always be a splat.
3048 // TODO: Handle source ops splats with undefs.
3049 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3050 APInt SrcUndefs;
3051 return (SrcElts.popcount() == 1) ||
3052 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3053 (SrcElts & SrcUndefs).isZero());
3054 };
3055 if (!DemandedLHS.isZero())
3056 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3057 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3058 }
3060 // Offset the demanded elts by the subvector index.
3061 SDValue Src = V.getOperand(0);
3062 // We don't support scalable vectors at the moment.
3063 if (Src.getValueType().isScalableVector())
3064 return false;
3065 uint64_t Idx = V.getConstantOperandVal(1);
3066 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3067 APInt UndefSrcElts;
3068 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3069 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3070 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3071 return true;
3072 }
3073 break;
3074 }
3078 // Widen the demanded elts by the src element count.
3079 SDValue Src = V.getOperand(0);
3080 // We don't support scalable vectors at the moment.
3081 if (Src.getValueType().isScalableVector())
3082 return false;
3083 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3084 APInt UndefSrcElts;
3085 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3086 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3087 UndefElts = UndefSrcElts.trunc(NumElts);
3088 return true;
3089 }
3090 break;
3091 }
3092 case ISD::BITCAST: {
3093 SDValue Src = V.getOperand(0);
3094 EVT SrcVT = Src.getValueType();
3095 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3096 unsigned BitWidth = VT.getScalarSizeInBits();
3097
3098 // Ignore bitcasts from unsupported types.
3099 // TODO: Add fp support?
3100 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3101 break;
3102
3103 // Bitcast 'small element' vector to 'large element' vector.
3104 if ((BitWidth % SrcBitWidth) == 0) {
3105 // See if each sub element is a splat.
3106 unsigned Scale = BitWidth / SrcBitWidth;
3107 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3108 APInt ScaledDemandedElts =
3109 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3110 for (unsigned I = 0; I != Scale; ++I) {
3111 APInt SubUndefElts;
3112 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3113 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3114 SubDemandedElts &= ScaledDemandedElts;
3115 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3116 return false;
3117 // TODO: Add support for merging sub undef elements.
3118 if (!SubUndefElts.isZero())
3119 return false;
3120 }
3121 return true;
3122 }
3123 break;
3124 }
3125 }
3126
3127 return false;
3128}
3129
3130/// Helper wrapper to main isSplatValue function.
3131bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3132 EVT VT = V.getValueType();
3133 assert(VT.isVector() && "Vector type expected");
3134
3135 APInt UndefElts;
3136 // Since the number of lanes in a scalable vector is unknown at compile time,
3137 // we track one bit which is implicitly broadcast to all lanes. This means
3138 // that all lanes in a scalable vector are considered demanded.
3139 APInt DemandedElts
3141 return isSplatValue(V, DemandedElts, UndefElts) &&
3142 (AllowUndefs || !UndefElts);
3143}
3144
3147
3148 EVT VT = V.getValueType();
3149 unsigned Opcode = V.getOpcode();
3150 switch (Opcode) {
3151 default: {
3152 APInt UndefElts;
3153 // Since the number of lanes in a scalable vector is unknown at compile time,
3154 // we track one bit which is implicitly broadcast to all lanes. This means
3155 // that all lanes in a scalable vector are considered demanded.
3156 APInt DemandedElts
3158
3159 if (isSplatValue(V, DemandedElts, UndefElts)) {
3160 if (VT.isScalableVector()) {
3161 // DemandedElts and UndefElts are ignored for scalable vectors, since
3162 // the only supported cases are SPLAT_VECTOR nodes.
3163 SplatIdx = 0;
3164 } else {
3165 // Handle case where all demanded elements are UNDEF.
3166 if (DemandedElts.isSubsetOf(UndefElts)) {
3167 SplatIdx = 0;
3168 return getUNDEF(VT);
3169 }
3170 SplatIdx = (UndefElts & DemandedElts).countr_one();
3171 }
3172 return V;
3173 }
3174 break;
3175 }
3176 case ISD::SPLAT_VECTOR:
3177 SplatIdx = 0;
3178 return V;
3179 case ISD::VECTOR_SHUFFLE: {
3180 assert(!VT.isScalableVector());
3181 // Check if this is a shuffle node doing a splat.
3182 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3183 // getTargetVShiftNode currently struggles without the splat source.
3184 auto *SVN = cast<ShuffleVectorSDNode>(V);
3185 if (!SVN->isSplat())
3186 break;
3187 int Idx = SVN->getSplatIndex();
3188 int NumElts = V.getValueType().getVectorNumElements();
3189 SplatIdx = Idx % NumElts;
3190 return V.getOperand(Idx / NumElts);
3191 }
3192 }
3193
3194 return SDValue();
3195}
3196
3198 int SplatIdx;
3199 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3200 EVT SVT = SrcVector.getValueType().getScalarType();
3201 EVT LegalSVT = SVT;
3202 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3203 if (!SVT.isInteger())
3204 return SDValue();
3205 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3206 if (LegalSVT.bitsLT(SVT))
3207 return SDValue();
3208 }
3209 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3210 }
3211 return SDValue();
3212}
3213
3214std::optional<ConstantRange>
3216 unsigned Depth) const {
3217 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3218 V.getOpcode() == ISD::SRA) &&
3219 "Unknown shift node");
3220 // Shifting more than the bitwidth is not valid.
3221 unsigned BitWidth = V.getScalarValueSizeInBits();
3222
3223 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3224 const APInt &ShAmt = Cst->getAPIntValue();
3225 if (ShAmt.uge(BitWidth))
3226 return std::nullopt;
3227 return ConstantRange(ShAmt);
3228 }
3229
3230 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3231 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3232 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3233 if (!DemandedElts[i])
3234 continue;
3235 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3236 if (!SA) {
3237 MinAmt = MaxAmt = nullptr;
3238 break;
3239 }
3240 const APInt &ShAmt = SA->getAPIntValue();
3241 if (ShAmt.uge(BitWidth))
3242 return std::nullopt;
3243 if (!MinAmt || MinAmt->ugt(ShAmt))
3244 MinAmt = &ShAmt;
3245 if (!MaxAmt || MaxAmt->ult(ShAmt))
3246 MaxAmt = &ShAmt;
3247 }
3248 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3249 "Failed to find matching min/max shift amounts");
3250 if (MinAmt && MaxAmt)
3251 return ConstantRange(*MinAmt, *MaxAmt + 1);
3252 }
3253
3254 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3255 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3256 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3257 if (KnownAmt.getMaxValue().ult(BitWidth))
3258 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3259
3260 return std::nullopt;
3261}
3262
3263std::optional<unsigned>
3265 unsigned Depth) const {
3266 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3267 V.getOpcode() == ISD::SRA) &&
3268 "Unknown shift node");
3269 if (std::optional<ConstantRange> AmtRange =
3270 getValidShiftAmountRange(V, DemandedElts, Depth))
3271 if (const APInt *ShAmt = AmtRange->getSingleElement())
3272 return ShAmt->getZExtValue();
3273 return std::nullopt;
3274}
3275
3276std::optional<unsigned>
3278 EVT VT = V.getValueType();
3279 APInt DemandedElts = VT.isFixedLengthVector()
3281 : APInt(1, 1);
3282 return getValidShiftAmount(V, DemandedElts, Depth);
3283}
3284
3285std::optional<unsigned>
3287 unsigned Depth) const {
3288 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3289 V.getOpcode() == ISD::SRA) &&
3290 "Unknown shift node");
3291 if (std::optional<ConstantRange> AmtRange =
3292 getValidShiftAmountRange(V, DemandedElts, Depth))
3293 return AmtRange->getUnsignedMin().getZExtValue();
3294 return std::nullopt;
3295}
3296
3297std::optional<unsigned>
3299 EVT VT = V.getValueType();
3300 APInt DemandedElts = VT.isFixedLengthVector()
3302 : APInt(1, 1);
3303 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3304}
3305
3306std::optional<unsigned>
3308 unsigned Depth) const {
3309 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3310 V.getOpcode() == ISD::SRA) &&
3311 "Unknown shift node");
3312 if (std::optional<ConstantRange> AmtRange =
3313 getValidShiftAmountRange(V, DemandedElts, Depth))
3314 return AmtRange->getUnsignedMax().getZExtValue();
3315 return std::nullopt;
3316}
3317
3318std::optional<unsigned>
3320 EVT VT = V.getValueType();
3321 APInt DemandedElts = VT.isFixedLengthVector()
3323 : APInt(1, 1);
3324 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3325}
3326
3327/// Determine which bits of Op are known to be either zero or one and return
3328/// them in Known. For vectors, the known bits are those that are shared by
3329/// every vector element.
3331 EVT VT = Op.getValueType();
3332
3333 // Since the number of lanes in a scalable vector is unknown at compile time,
3334 // we track one bit which is implicitly broadcast to all lanes. This means
3335 // that all lanes in a scalable vector are considered demanded.
3336 APInt DemandedElts = VT.isFixedLengthVector()
3338 : APInt(1, 1);
3339 return computeKnownBits(Op, DemandedElts, Depth);
3340}
3341
3342/// Determine which bits of Op are known to be either zero or one and return
3343/// them in Known. The DemandedElts argument allows us to only collect the known
3344/// bits that are shared by the requested vector elements.
3346 unsigned Depth) const {
3347 unsigned BitWidth = Op.getScalarValueSizeInBits();
3348
3349 KnownBits Known(BitWidth); // Don't know anything.
3350
3351 if (auto OptAPInt = Op->bitcastToAPInt()) {
3352 // We know all of the bits for a constant!
3353 return KnownBits::makeConstant(*std::move(OptAPInt));
3354 }
3355
3356 if (Depth >= MaxRecursionDepth)
3357 return Known; // Limit search depth.
3358
3359 KnownBits Known2;
3360 unsigned NumElts = DemandedElts.getBitWidth();
3361 assert((!Op.getValueType().isScalableVector() || NumElts == 1) &&
3362 "DemandedElts for scalable vectors must be 1 to represent all lanes");
3363 assert((!Op.getValueType().isFixedLengthVector() ||
3364 NumElts == Op.getValueType().getVectorNumElements()) &&
3365 "Unexpected vector size");
3366
3367 if (!DemandedElts)
3368 return Known; // No demanded elts, better to assume we don't know anything.
3369
3370 unsigned Opcode = Op.getOpcode();
3371 switch (Opcode) {
3372 case ISD::MERGE_VALUES:
3373 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3374 Depth + 1);
3375 case ISD::SPLAT_VECTOR: {
3376 SDValue SrcOp = Op.getOperand(0);
3377 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3378 "Expected SPLAT_VECTOR implicit truncation");
3379 // Implicitly truncate the bits to match the official semantics of
3380 // SPLAT_VECTOR.
3381 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3382 break;
3383 }
3385 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3386 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3387 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3388 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3389 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3390 }
3391 break;
3392 }
3393 case ISD::STEP_VECTOR: {
3394 const APInt &Step = Op.getConstantOperandAPInt(0);
3395
3396 if (Step.isPowerOf2())
3397 Known.Zero.setLowBits(Step.logBase2());
3398
3400
3401 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3402 break;
3403 const APInt MinNumElts =
3404 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3405
3406 bool Overflow;
3407 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3409 .umul_ov(MinNumElts, Overflow);
3410 if (Overflow)
3411 break;
3412
3413 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3414 if (Overflow)
3415 break;
3416
3417 Known.Zero.setHighBits(MaxValue.countl_zero());
3418 break;
3419 }
3420 case ISD::BUILD_VECTOR:
3421 assert(!Op.getValueType().isScalableVector());
3422 // Collect the known bits that are shared by every demanded vector element.
3423 Known.setAllConflict();
3424 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3425 if (!DemandedElts[i])
3426 continue;
3427
3428 SDValue SrcOp = Op.getOperand(i);
3429 Known2 = computeKnownBits(SrcOp, Depth + 1);
3430
3431 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3432 if (SrcOp.getValueSizeInBits() != BitWidth) {
3433 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3434 "Expected BUILD_VECTOR implicit truncation");
3435 Known2 = Known2.trunc(BitWidth);
3436 }
3437
3438 // Known bits are the values that are shared by every demanded element.
3439 Known = Known.intersectWith(Known2);
3440
3441 // If we don't know any bits, early out.
3442 if (Known.isUnknown())
3443 break;
3444 }
3445 break;
3446 case ISD::VECTOR_COMPRESS: {
3447 SDValue Vec = Op.getOperand(0);
3448 SDValue PassThru = Op.getOperand(2);
3449 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3450 // If we don't know any bits, early out.
3451 if (Known.isUnknown())
3452 break;
3453 Known2 = computeKnownBits(Vec, Depth + 1);
3454 Known = Known.intersectWith(Known2);
3455 break;
3456 }
3457 case ISD::VECTOR_SHUFFLE: {
3458 assert(!Op.getValueType().isScalableVector());
3459 // Collect the known bits that are shared by every vector element referenced
3460 // by the shuffle.
3461 APInt DemandedLHS, DemandedRHS;
3463 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3464 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3465 DemandedLHS, DemandedRHS))
3466 break;
3467
3468 // Known bits are the values that are shared by every demanded element.
3469 Known.setAllConflict();
3470 if (!!DemandedLHS) {
3471 SDValue LHS = Op.getOperand(0);
3472 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3473 Known = Known.intersectWith(Known2);
3474 }
3475 // If we don't know any bits, early out.
3476 if (Known.isUnknown())
3477 break;
3478 if (!!DemandedRHS) {
3479 SDValue RHS = Op.getOperand(1);
3480 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3481 Known = Known.intersectWith(Known2);
3482 }
3483 break;
3484 }
3485 case ISD::VSCALE: {
3487 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3488 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3489 break;
3490 }
3491 case ISD::CONCAT_VECTORS: {
3492 if (Op.getValueType().isScalableVector())
3493 break;
3494 // Split DemandedElts and test each of the demanded subvectors.
3495 Known.setAllConflict();
3496 EVT SubVectorVT = Op.getOperand(0).getValueType();
3497 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3498 unsigned NumSubVectors = Op.getNumOperands();
3499 for (unsigned i = 0; i != NumSubVectors; ++i) {
3500 APInt DemandedSub =
3501 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3502 if (!!DemandedSub) {
3503 SDValue Sub = Op.getOperand(i);
3504 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3505 Known = Known.intersectWith(Known2);
3506 }
3507 // If we don't know any bits, early out.
3508 if (Known.isUnknown())
3509 break;
3510 }
3511 break;
3512 }
3513 case ISD::INSERT_SUBVECTOR: {
3514 if (Op.getValueType().isScalableVector())
3515 break;
3516 // Demand any elements from the subvector and the remainder from the src its
3517 // inserted into.
3518 SDValue Src = Op.getOperand(0);
3519 SDValue Sub = Op.getOperand(1);
3520 uint64_t Idx = Op.getConstantOperandVal(2);
3521 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3522 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3523 APInt DemandedSrcElts = DemandedElts;
3524 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3525
3526 Known.setAllConflict();
3527 if (!!DemandedSubElts) {
3528 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3529 if (Known.isUnknown())
3530 break; // early-out.
3531 }
3532 if (!!DemandedSrcElts) {
3533 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3534 Known = Known.intersectWith(Known2);
3535 }
3536 break;
3537 }
3539 // Offset the demanded elts by the subvector index.
3540 SDValue Src = Op.getOperand(0);
3541
3542 APInt DemandedSrcElts;
3543 if (Src.getValueType().isScalableVector())
3544 DemandedSrcElts = APInt(1, 1); // <=> 'demand all elements'
3545 else {
3546 uint64_t Idx = Op.getConstantOperandVal(1);
3547 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3548 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3549 }
3550 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3551 break;
3552 }
3553 case ISD::SCALAR_TO_VECTOR: {
3554 if (Op.getValueType().isScalableVector())
3555 break;
3556 // We know about scalar_to_vector as much as we know about it source,
3557 // which becomes the first element of otherwise unknown vector.
3558 if (DemandedElts != 1)
3559 break;
3560
3561 SDValue N0 = Op.getOperand(0);
3562 Known = computeKnownBits(N0, Depth + 1);
3563 if (N0.getValueSizeInBits() != BitWidth)
3564 Known = Known.trunc(BitWidth);
3565
3566 break;
3567 }
3568 case ISD::BITCAST: {
3569 if (Op.getValueType().isScalableVector())
3570 break;
3571
3572 SDValue N0 = Op.getOperand(0);
3573 EVT SubVT = N0.getValueType();
3574 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3575
3576 // Ignore bitcasts from unsupported types.
3577 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3578 break;
3579
3580 // Fast handling of 'identity' bitcasts.
3581 if (BitWidth == SubBitWidth) {
3582 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3583 break;
3584 }
3585
3586 bool IsLE = getDataLayout().isLittleEndian();
3587
3588 // Bitcast 'small element' vector to 'large element' scalar/vector.
3589 if ((BitWidth % SubBitWidth) == 0) {
3590 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3591
3592 // Collect known bits for the (larger) output by collecting the known
3593 // bits from each set of sub elements and shift these into place.
3594 // We need to separately call computeKnownBits for each set of
3595 // sub elements as the knownbits for each is likely to be different.
3596 unsigned SubScale = BitWidth / SubBitWidth;
3597 APInt SubDemandedElts(NumElts * SubScale, 0);
3598 for (unsigned i = 0; i != NumElts; ++i)
3599 if (DemandedElts[i])
3600 SubDemandedElts.setBit(i * SubScale);
3601
3602 for (unsigned i = 0; i != SubScale; ++i) {
3603 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3604 Depth + 1);
3605 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3606 Known.insertBits(Known2, SubBitWidth * Shifts);
3607 }
3608 }
3609
3610 // Bitcast 'large element' scalar/vector to 'small element' vector.
3611 if ((SubBitWidth % BitWidth) == 0) {
3612 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3613
3614 // Collect known bits for the (smaller) output by collecting the known
3615 // bits from the overlapping larger input elements and extracting the
3616 // sub sections we actually care about.
3617 unsigned SubScale = SubBitWidth / BitWidth;
3618 APInt SubDemandedElts =
3619 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3620 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3621
3622 Known.setAllConflict();
3623 for (unsigned i = 0; i != NumElts; ++i)
3624 if (DemandedElts[i]) {
3625 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3626 unsigned Offset = (Shifts % SubScale) * BitWidth;
3627 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3628 // If we don't know any bits, early out.
3629 if (Known.isUnknown())
3630 break;
3631 }
3632 }
3633 break;
3634 }
3635 case ISD::AND:
3636 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3637 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3638
3639 Known &= Known2;
3640 break;
3641 case ISD::OR:
3642 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3643 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3644
3645 Known |= Known2;
3646 break;
3647 case ISD::XOR:
3648 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3649 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3650
3651 Known ^= Known2;
3652 break;
3653 case ISD::MUL: {
3654 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3655 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3656 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3657 // TODO: SelfMultiply can be poison, but not undef.
3658 if (SelfMultiply)
3659 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3660 Op.getOperand(0), DemandedElts, false, Depth + 1);
3661 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3662
3663 // If the multiplication is known not to overflow, the product of a number
3664 // with itself is non-negative. Only do this if we didn't already computed
3665 // the opposite value for the sign bit.
3666 if (Op->getFlags().hasNoSignedWrap() &&
3667 Op.getOperand(0) == Op.getOperand(1) &&
3668 !Known.isNegative())
3669 Known.makeNonNegative();
3670 break;
3671 }
3672 case ISD::MULHU: {
3673 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3674 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3675 Known = KnownBits::mulhu(Known, Known2);
3676 break;
3677 }
3678 case ISD::MULHS: {
3679 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3680 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3681 Known = KnownBits::mulhs(Known, Known2);
3682 break;
3683 }
3684 case ISD::ABDU: {
3685 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3686 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3687 Known = KnownBits::abdu(Known, Known2);
3688 break;
3689 }
3690 case ISD::ABDS: {
3691 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3692 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3693 Known = KnownBits::abds(Known, Known2);
3694 unsigned SignBits1 =
3695 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3696 if (SignBits1 == 1)
3697 break;
3698 unsigned SignBits0 =
3699 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3700 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3701 break;
3702 }
3703 case ISD::UMUL_LOHI: {
3704 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3705 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3706 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3707 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3708 if (Op.getResNo() == 0)
3709 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3710 else
3711 Known = KnownBits::mulhu(Known, Known2);
3712 break;
3713 }
3714 case ISD::SMUL_LOHI: {
3715 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3716 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3717 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3718 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3719 if (Op.getResNo() == 0)
3720 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3721 else
3722 Known = KnownBits::mulhs(Known, Known2);
3723 break;
3724 }
3725 case ISD::AVGFLOORU: {
3726 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3727 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3728 Known = KnownBits::avgFloorU(Known, Known2);
3729 break;
3730 }
3731 case ISD::AVGCEILU: {
3732 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3733 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3734 Known = KnownBits::avgCeilU(Known, Known2);
3735 break;
3736 }
3737 case ISD::AVGFLOORS: {
3738 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3739 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3740 Known = KnownBits::avgFloorS(Known, Known2);
3741 break;
3742 }
3743 case ISD::AVGCEILS: {
3744 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3745 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3746 Known = KnownBits::avgCeilS(Known, Known2);
3747 break;
3748 }
3749 case ISD::SELECT:
3750 case ISD::VSELECT:
3751 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3752 // If we don't know any bits, early out.
3753 if (Known.isUnknown())
3754 break;
3755 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3756
3757 // Only known if known in both the LHS and RHS.
3758 Known = Known.intersectWith(Known2);
3759 break;
3760 case ISD::SELECT_CC:
3761 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3762 // If we don't know any bits, early out.
3763 if (Known.isUnknown())
3764 break;
3765 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3766
3767 // Only known if known in both the LHS and RHS.
3768 Known = Known.intersectWith(Known2);
3769 break;
3770 case ISD::SMULO:
3771 case ISD::UMULO:
3772 if (Op.getResNo() != 1)
3773 break;
3774 // The boolean result conforms to getBooleanContents.
3775 // If we know the result of a setcc has the top bits zero, use this info.
3776 // We know that we have an integer-based boolean since these operations
3777 // are only available for integer.
3778 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3780 BitWidth > 1)
3781 Known.Zero.setBitsFrom(1);
3782 break;
3783 case ISD::SETCC:
3784 case ISD::SETCCCARRY:
3785 case ISD::STRICT_FSETCC:
3786 case ISD::STRICT_FSETCCS: {
3787 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3788 // If we know the result of a setcc has the top bits zero, use this info.
3789 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3791 BitWidth > 1)
3792 Known.Zero.setBitsFrom(1);
3793 break;
3794 }
3795 case ISD::SHL: {
3796 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3797 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3798
3799 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3800 bool NSW = Op->getFlags().hasNoSignedWrap();
3801
3802 bool ShAmtNonZero = Known2.isNonZero();
3803
3804 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3805
3806 // Minimum shift low bits are known zero.
3807 if (std::optional<unsigned> ShMinAmt =
3808 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3809 Known.Zero.setLowBits(*ShMinAmt);
3810 break;
3811 }
3812 case ISD::SRL:
3813 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3814 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3815 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3816 Op->getFlags().hasExact());
3817
3818 // Minimum shift high bits are known zero.
3819 if (std::optional<unsigned> ShMinAmt =
3820 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3821 Known.Zero.setHighBits(*ShMinAmt);
3822 break;
3823 case ISD::SRA:
3824 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3825 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3826 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3827 Op->getFlags().hasExact());
3828 break;
3829 case ISD::ROTL:
3830 case ISD::ROTR:
3831 if (ConstantSDNode *C =
3832 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3833 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3834
3835 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3836
3837 // Canonicalize to ROTR.
3838 if (Opcode == ISD::ROTL && Amt != 0)
3839 Amt = BitWidth - Amt;
3840
3841 Known.Zero = Known.Zero.rotr(Amt);
3842 Known.One = Known.One.rotr(Amt);
3843 }
3844 break;
3845 case ISD::FSHL:
3846 case ISD::FSHR:
3847 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3848 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3849
3850 // For fshl, 0-shift returns the 1st arg.
3851 // For fshr, 0-shift returns the 2nd arg.
3852 if (Amt == 0) {
3853 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3854 DemandedElts, Depth + 1);
3855 break;
3856 }
3857
3858 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3859 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3860 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3861 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3862 if (Opcode == ISD::FSHL) {
3863 Known <<= Amt;
3864 Known2 >>= BitWidth - Amt;
3865 } else {
3866 Known <<= BitWidth - Amt;
3867 Known2 >>= Amt;
3868 }
3869 Known = Known.unionWith(Known2);
3870 }
3871 break;
3872 case ISD::SHL_PARTS:
3873 case ISD::SRA_PARTS:
3874 case ISD::SRL_PARTS: {
3875 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3876
3877 // Collect lo/hi source values and concatenate.
3878 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3879 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3880 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3881 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3882 Known = Known2.concat(Known);
3883
3884 // Collect shift amount.
3885 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3886
3887 if (Opcode == ISD::SHL_PARTS)
3888 Known = KnownBits::shl(Known, Known2);
3889 else if (Opcode == ISD::SRA_PARTS)
3890 Known = KnownBits::ashr(Known, Known2);
3891 else // if (Opcode == ISD::SRL_PARTS)
3892 Known = KnownBits::lshr(Known, Known2);
3893
3894 // TODO: Minimum shift low/high bits are known zero.
3895
3896 if (Op.getResNo() == 0)
3897 Known = Known.extractBits(LoBits, 0);
3898 else
3899 Known = Known.extractBits(HiBits, LoBits);
3900 break;
3901 }
3903 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3904 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3905 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3906 break;
3907 }
3908 case ISD::CTTZ:
3909 case ISD::CTTZ_ZERO_UNDEF: {
3910 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3911 // If we have a known 1, its position is our upper bound.
3912 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3913 unsigned LowBits = llvm::bit_width(PossibleTZ);
3914 Known.Zero.setBitsFrom(LowBits);
3915 break;
3916 }
3917 case ISD::CTLZ:
3918 case ISD::CTLZ_ZERO_UNDEF: {
3919 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3920 // If we have a known 1, its position is our upper bound.
3921 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3922 unsigned LowBits = llvm::bit_width(PossibleLZ);
3923 Known.Zero.setBitsFrom(LowBits);
3924 break;
3925 }
3926 case ISD::CTLS: {
3927 unsigned MinRedundantSignBits =
3928 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1;
3929 ConstantRange Range(APInt(BitWidth, MinRedundantSignBits),
3931 Known = Range.toKnownBits();
3932 break;
3933 }
3934 case ISD::CTPOP: {
3935 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3936 // If we know some of the bits are zero, they can't be one.
3937 unsigned PossibleOnes = Known2.countMaxPopulation();
3938 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3939 break;
3940 }
3941 case ISD::PARITY: {
3942 // Parity returns 0 everywhere but the LSB.
3943 Known.Zero.setBitsFrom(1);
3944 break;
3945 }
3946 case ISD::CLMUL: {
3947 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3948 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3949 Known = KnownBits::clmul(Known, Known2);
3950 break;
3951 }
3952 case ISD::MGATHER:
3953 case ISD::MLOAD: {
3954 ISD::LoadExtType ETy =
3955 (Opcode == ISD::MGATHER)
3956 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3957 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3958 if (ETy == ISD::ZEXTLOAD) {
3959 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3960 KnownBits Known0(MemVT.getScalarSizeInBits());
3961 return Known0.zext(BitWidth);
3962 }
3963 break;
3964 }
3965 case ISD::LOAD: {
3967 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3968 if (ISD::isNON_EXTLoad(LD) && Cst) {
3969 // Determine any common known bits from the loaded constant pool value.
3970 Type *CstTy = Cst->getType();
3971 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3972 !Op.getValueType().isScalableVector()) {
3973 // If its a vector splat, then we can (quickly) reuse the scalar path.
3974 // NOTE: We assume all elements match and none are UNDEF.
3975 if (CstTy->isVectorTy()) {
3976 if (const Constant *Splat = Cst->getSplatValue()) {
3977 Cst = Splat;
3978 CstTy = Cst->getType();
3979 }
3980 }
3981 // TODO - do we need to handle different bitwidths?
3982 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3983 // Iterate across all vector elements finding common known bits.
3984 Known.setAllConflict();
3985 for (unsigned i = 0; i != NumElts; ++i) {
3986 if (!DemandedElts[i])
3987 continue;
3988 if (Constant *Elt = Cst->getAggregateElement(i)) {
3989 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3990 const APInt &Value = CInt->getValue();
3991 Known.One &= Value;
3992 Known.Zero &= ~Value;
3993 continue;
3994 }
3995 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3996 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3997 Known.One &= Value;
3998 Known.Zero &= ~Value;
3999 continue;
4000 }
4001 }
4002 Known.One.clearAllBits();
4003 Known.Zero.clearAllBits();
4004 break;
4005 }
4006 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
4007 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
4008 Known = KnownBits::makeConstant(CInt->getValue());
4009 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
4010 Known =
4011 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
4012 }
4013 }
4014 }
4015 } else if (Op.getResNo() == 0) {
4016 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
4017 KnownBits KnownScalarMemory(ScalarMemorySize);
4018 if (const MDNode *MD = LD->getRanges())
4019 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4020
4021 // Extend the Known bits from memory to the size of the scalar result.
4022 if (ISD::isZEXTLoad(Op.getNode()))
4023 Known = KnownScalarMemory.zext(BitWidth);
4024 else if (ISD::isSEXTLoad(Op.getNode()))
4025 Known = KnownScalarMemory.sext(BitWidth);
4026 else if (ISD::isEXTLoad(Op.getNode()))
4027 Known = KnownScalarMemory.anyext(BitWidth);
4028 else
4029 Known = KnownScalarMemory;
4030 assert(Known.getBitWidth() == BitWidth);
4031 return Known;
4032 }
4033 break;
4034 }
4036 if (Op.getValueType().isScalableVector())
4037 break;
4038 EVT InVT = Op.getOperand(0).getValueType();
4039 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4040 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4041 Known = Known.zext(BitWidth);
4042 break;
4043 }
4044 case ISD::ZERO_EXTEND: {
4045 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4046 Known = Known.zext(BitWidth);
4047 break;
4048 }
4050 if (Op.getValueType().isScalableVector())
4051 break;
4052 EVT InVT = Op.getOperand(0).getValueType();
4053 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4054 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4055 // If the sign bit is known to be zero or one, then sext will extend
4056 // it to the top bits, else it will just zext.
4057 Known = Known.sext(BitWidth);
4058 break;
4059 }
4060 case ISD::SIGN_EXTEND: {
4061 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4062 // If the sign bit is known to be zero or one, then sext will extend
4063 // it to the top bits, else it will just zext.
4064 Known = Known.sext(BitWidth);
4065 break;
4066 }
4068 if (Op.getValueType().isScalableVector())
4069 break;
4070 EVT InVT = Op.getOperand(0).getValueType();
4071 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4072 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4073 Known = Known.anyext(BitWidth);
4074 break;
4075 }
4076 case ISD::ANY_EXTEND: {
4077 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4078 Known = Known.anyext(BitWidth);
4079 break;
4080 }
4081 case ISD::TRUNCATE: {
4082 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4083 Known = Known.trunc(BitWidth);
4084 break;
4085 }
4086 case ISD::TRUNCATE_SSAT_S: {
4087 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4088 Known = Known.truncSSat(BitWidth);
4089 break;
4090 }
4091 case ISD::TRUNCATE_SSAT_U: {
4092 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4093 Known = Known.truncSSatU(BitWidth);
4094 break;
4095 }
4096 case ISD::TRUNCATE_USAT_U: {
4097 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4098 Known = Known.truncUSat(BitWidth);
4099 break;
4100 }
4101 case ISD::AssertZext: {
4102 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4104 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4105 Known.Zero |= (~InMask);
4106 Known.One &= (~Known.Zero);
4107 break;
4108 }
4109 case ISD::AssertAlign: {
4110 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4111 assert(LogOfAlign != 0);
4112
4113 // TODO: Should use maximum with source
4114 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4115 // well as clearing one bits.
4116 Known.Zero.setLowBits(LogOfAlign);
4117 Known.One.clearLowBits(LogOfAlign);
4118 break;
4119 }
4120 case ISD::AssertNoFPClass: {
4121 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4122
4123 FPClassTest NoFPClass =
4124 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4125 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4126 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4127 // Cannot be negative.
4128 Known.makeNonNegative();
4129 }
4130
4131 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4132 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4133 // Cannot be positive.
4134 Known.makeNegative();
4135 }
4136
4137 break;
4138 }
4139 case ISD::FABS:
4140 // fabs clears the sign bit
4141 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4142 Known.makeNonNegative();
4143 break;
4144 case ISD::FGETSIGN:
4145 // All bits are zero except the low bit.
4146 Known.Zero.setBitsFrom(1);
4147 break;
4148 case ISD::ADD: {
4149 SDNodeFlags Flags = Op.getNode()->getFlags();
4150 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4151 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4152 bool SelfAdd = Op.getOperand(0) == Op.getOperand(1) &&
4154 Op.getOperand(0), DemandedElts, false, Depth + 1);
4155 Known = KnownBits::add(Known, Known2, Flags.hasNoSignedWrap(),
4156 Flags.hasNoUnsignedWrap(), SelfAdd);
4157 break;
4158 }
4159 case ISD::SUB: {
4160 SDNodeFlags Flags = Op.getNode()->getFlags();
4161 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4162 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4163 Known = KnownBits::sub(Known, Known2, Flags.hasNoSignedWrap(),
4164 Flags.hasNoUnsignedWrap());
4165 break;
4166 }
4167 case ISD::USUBO:
4168 case ISD::SSUBO:
4169 case ISD::USUBO_CARRY:
4170 case ISD::SSUBO_CARRY:
4171 if (Op.getResNo() == 1) {
4172 // If we know the result of a setcc has the top bits zero, use this info.
4173 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4175 BitWidth > 1)
4176 Known.Zero.setBitsFrom(1);
4177 break;
4178 }
4179 [[fallthrough]];
4180 case ISD::SUBC: {
4181 assert(Op.getResNo() == 0 &&
4182 "We only compute knownbits for the difference here.");
4183
4184 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4185 KnownBits Borrow(1);
4186 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4187 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4188 // Borrow has bit width 1
4189 Borrow = Borrow.trunc(1);
4190 } else {
4191 Borrow.setAllZero();
4192 }
4193
4194 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4195 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4196 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4197 break;
4198 }
4199 case ISD::UADDO:
4200 case ISD::SADDO:
4201 case ISD::UADDO_CARRY:
4202 case ISD::SADDO_CARRY:
4203 if (Op.getResNo() == 1) {
4204 // If we know the result of a setcc has the top bits zero, use this info.
4205 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4207 BitWidth > 1)
4208 Known.Zero.setBitsFrom(1);
4209 break;
4210 }
4211 [[fallthrough]];
4212 case ISD::ADDC:
4213 case ISD::ADDE: {
4214 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4215
4216 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4217 KnownBits Carry(1);
4218 if (Opcode == ISD::ADDE)
4219 // Can't track carry from glue, set carry to unknown.
4220 Carry.resetAll();
4221 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4222 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4223 // Carry has bit width 1
4224 Carry = Carry.trunc(1);
4225 } else {
4226 Carry.setAllZero();
4227 }
4228
4229 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4230 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4231 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4232 break;
4233 }
4234 case ISD::UDIV: {
4235 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4236 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4237 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4238 break;
4239 }
4240 case ISD::SDIV: {
4241 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4242 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4243 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4244 break;
4245 }
4246 case ISD::SREM: {
4247 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4248 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4249 Known = KnownBits::srem(Known, Known2);
4250 break;
4251 }
4252 case ISD::UREM: {
4253 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4254 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4255 Known = KnownBits::urem(Known, Known2);
4256 break;
4257 }
4258 case ISD::EXTRACT_ELEMENT: {
4259 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4260 const unsigned Index = Op.getConstantOperandVal(1);
4261 const unsigned EltBitWidth = Op.getValueSizeInBits();
4262
4263 // Remove low part of known bits mask
4264 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4265 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4266
4267 // Remove high part of known bit mask
4268 Known = Known.trunc(EltBitWidth);
4269 break;
4270 }
4272 SDValue InVec = Op.getOperand(0);
4273 SDValue EltNo = Op.getOperand(1);
4274 EVT VecVT = InVec.getValueType();
4275 // computeKnownBits not yet implemented for scalable vectors.
4276 if (VecVT.isScalableVector())
4277 break;
4278 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4279 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4280
4281 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4282 // anything about the extended bits.
4283 if (BitWidth > EltBitWidth)
4284 Known = Known.trunc(EltBitWidth);
4285
4286 // If we know the element index, just demand that vector element, else for
4287 // an unknown element index, ignore DemandedElts and demand them all.
4288 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4289 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4290 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4291 DemandedSrcElts =
4292 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4293
4294 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4295 if (BitWidth > EltBitWidth)
4296 Known = Known.anyext(BitWidth);
4297 break;
4298 }
4300 if (Op.getValueType().isScalableVector())
4301 break;
4302
4303 // If we know the element index, split the demand between the
4304 // source vector and the inserted element, otherwise assume we need
4305 // the original demanded vector elements and the value.
4306 SDValue InVec = Op.getOperand(0);
4307 SDValue InVal = Op.getOperand(1);
4308 SDValue EltNo = Op.getOperand(2);
4309 bool DemandedVal = true;
4310 APInt DemandedVecElts = DemandedElts;
4311 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4312 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4313 unsigned EltIdx = CEltNo->getZExtValue();
4314 DemandedVal = !!DemandedElts[EltIdx];
4315 DemandedVecElts.clearBit(EltIdx);
4316 }
4317 Known.setAllConflict();
4318 if (DemandedVal) {
4319 Known2 = computeKnownBits(InVal, Depth + 1);
4320 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4321 }
4322 if (!!DemandedVecElts) {
4323 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4324 Known = Known.intersectWith(Known2);
4325 }
4326 break;
4327 }
4328 case ISD::BITREVERSE: {
4329 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4330 Known = Known2.reverseBits();
4331 break;
4332 }
4333 case ISD::BSWAP: {
4334 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4335 Known = Known2.byteSwap();
4336 break;
4337 }
4338 case ISD::ABS: {
4339 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4340 Known = Known2.abs();
4341 Known.Zero.setHighBits(
4342 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4343 break;
4344 }
4345 case ISD::USUBSAT: {
4346 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4347 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4348 Known = KnownBits::usub_sat(Known, Known2);
4349 break;
4350 }
4351 case ISD::UMIN: {
4352 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4353 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4354 Known = KnownBits::umin(Known, Known2);
4355 break;
4356 }
4357 case ISD::UMAX: {
4358 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4359 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4360 Known = KnownBits::umax(Known, Known2);
4361 break;
4362 }
4363 case ISD::SMIN:
4364 case ISD::SMAX: {
4365 // If we have a clamp pattern, we know that the number of sign bits will be
4366 // the minimum of the clamp min/max range.
4367 bool IsMax = (Opcode == ISD::SMAX);
4368 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4369 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4370 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4371 CstHigh =
4372 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4373 if (CstLow && CstHigh) {
4374 if (!IsMax)
4375 std::swap(CstLow, CstHigh);
4376
4377 const APInt &ValueLow = CstLow->getAPIntValue();
4378 const APInt &ValueHigh = CstHigh->getAPIntValue();
4379 if (ValueLow.sle(ValueHigh)) {
4380 unsigned LowSignBits = ValueLow.getNumSignBits();
4381 unsigned HighSignBits = ValueHigh.getNumSignBits();
4382 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4383 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4384 Known.One.setHighBits(MinSignBits);
4385 break;
4386 }
4387 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4388 Known.Zero.setHighBits(MinSignBits);
4389 break;
4390 }
4391 }
4392 }
4393
4394 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4395 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4396 if (IsMax)
4397 Known = KnownBits::smax(Known, Known2);
4398 else
4399 Known = KnownBits::smin(Known, Known2);
4400
4401 // For SMAX, if CstLow is non-negative we know the result will be
4402 // non-negative and thus all sign bits are 0.
4403 // TODO: There's an equivalent of this for smin with negative constant for
4404 // known ones.
4405 if (IsMax && CstLow) {
4406 const APInt &ValueLow = CstLow->getAPIntValue();
4407 if (ValueLow.isNonNegative()) {
4408 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4409 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4410 }
4411 }
4412
4413 break;
4414 }
4415 case ISD::UINT_TO_FP: {
4416 Known.makeNonNegative();
4417 break;
4418 }
4419 case ISD::SINT_TO_FP: {
4420 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4421 if (Known2.isNonNegative())
4422 Known.makeNonNegative();
4423 else if (Known2.isNegative())
4424 Known.makeNegative();
4425 break;
4426 }
4427 case ISD::FP_TO_UINT_SAT: {
4428 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4429 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4431 break;
4432 }
4433 case ISD::ATOMIC_LOAD: {
4434 // If we are looking at the loaded value.
4435 if (Op.getResNo() == 0) {
4436 auto *AT = cast<AtomicSDNode>(Op);
4437 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4438 KnownBits KnownScalarMemory(ScalarMemorySize);
4439 if (const MDNode *MD = AT->getRanges())
4440 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4441
4442 switch (AT->getExtensionType()) {
4443 case ISD::ZEXTLOAD:
4444 Known = KnownScalarMemory.zext(BitWidth);
4445 break;
4446 case ISD::SEXTLOAD:
4447 Known = KnownScalarMemory.sext(BitWidth);
4448 break;
4449 case ISD::EXTLOAD:
4450 switch (TLI->getExtendForAtomicOps()) {
4451 case ISD::ZERO_EXTEND:
4452 Known = KnownScalarMemory.zext(BitWidth);
4453 break;
4454 case ISD::SIGN_EXTEND:
4455 Known = KnownScalarMemory.sext(BitWidth);
4456 break;
4457 default:
4458 Known = KnownScalarMemory.anyext(BitWidth);
4459 break;
4460 }
4461 break;
4462 case ISD::NON_EXTLOAD:
4463 Known = KnownScalarMemory;
4464 break;
4465 }
4466 assert(Known.getBitWidth() == BitWidth);
4467 }
4468 break;
4469 }
4471 if (Op.getResNo() == 1) {
4472 // The boolean result conforms to getBooleanContents.
4473 // If we know the result of a setcc has the top bits zero, use this info.
4474 // We know that we have an integer-based boolean since these operations
4475 // are only available for integer.
4476 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4478 BitWidth > 1)
4479 Known.Zero.setBitsFrom(1);
4480 break;
4481 }
4482 [[fallthrough]];
4484 case ISD::ATOMIC_SWAP:
4495 case ISD::ATOMIC_LOAD_UMAX: {
4496 // If we are looking at the loaded value.
4497 if (Op.getResNo() == 0) {
4498 auto *AT = cast<AtomicSDNode>(Op);
4499 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4500
4501 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4502 Known.Zero.setBitsFrom(MemBits);
4503 }
4504 break;
4505 }
4506 case ISD::FrameIndex:
4508 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4509 Known, getMachineFunction());
4510 break;
4511
4512 default:
4513 if (Opcode < ISD::BUILTIN_OP_END)
4514 break;
4515 [[fallthrough]];
4519 // TODO: Probably okay to remove after audit; here to reduce change size
4520 // in initial enablement patch for scalable vectors
4521 if (Op.getValueType().isScalableVector())
4522 break;
4523
4524 // Allow the target to implement this method for its nodes.
4525 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4526 break;
4527 }
4528
4529 return Known;
4530}
4531
4532/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4545
4548 // X + 0 never overflow
4549 if (isNullConstant(N1))
4550 return OFK_Never;
4551
4552 // If both operands each have at least two sign bits, the addition
4553 // cannot overflow.
4554 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4555 return OFK_Never;
4556
4557 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4558 return OFK_Sometime;
4559}
4560
4563 // X + 0 never overflow
4564 if (isNullConstant(N1))
4565 return OFK_Never;
4566
4567 // mulhi + 1 never overflow
4568 KnownBits N1Known = computeKnownBits(N1);
4569 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4570 N1Known.getMaxValue().ult(2))
4571 return OFK_Never;
4572
4573 KnownBits N0Known = computeKnownBits(N0);
4574 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4575 N0Known.getMaxValue().ult(2))
4576 return OFK_Never;
4577
4578 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4579 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4580 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4581 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4582}
4583
4586 // X - 0 never overflow
4587 if (isNullConstant(N1))
4588 return OFK_Never;
4589
4590 // If both operands each have at least two sign bits, the subtraction
4591 // cannot overflow.
4592 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4593 return OFK_Never;
4594
4595 KnownBits N0Known = computeKnownBits(N0);
4596 KnownBits N1Known = computeKnownBits(N1);
4597 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4598 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4599 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4600}
4601
4604 // X - 0 never overflow
4605 if (isNullConstant(N1))
4606 return OFK_Never;
4607
4608 ConstantRange N0Range =
4609 computeConstantRangeIncludingKnownBits(N0, /*ForSigned=*/false);
4610 ConstantRange N1Range =
4611 computeConstantRangeIncludingKnownBits(N1, /*ForSigned=*/false);
4612 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4613}
4614
4617 // X * 0 and X * 1 never overflow.
4618 if (isNullConstant(N1) || isOneConstant(N1))
4619 return OFK_Never;
4620
4623 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4624}
4625
4628 // X * 0 and X * 1 never overflow.
4629 if (isNullConstant(N1) || isOneConstant(N1))
4630 return OFK_Never;
4631
4632 // Get the size of the result.
4633 unsigned BitWidth = N0.getScalarValueSizeInBits();
4634
4635 // Sum of the sign bits.
4636 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4637
4638 // If we have enough sign bits, then there's no overflow.
4639 if (SignBits > BitWidth + 1)
4640 return OFK_Never;
4641
4642 if (SignBits == BitWidth + 1) {
4643 // The overflow occurs when the true multiplication of the
4644 // the operands is the minimum negative number.
4645 KnownBits N0Known = computeKnownBits(N0);
4646 KnownBits N1Known = computeKnownBits(N1);
4647 // If one of the operands is non-negative, then there's no
4648 // overflow.
4649 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4650 return OFK_Never;
4651 }
4652
4653 return OFK_Sometime;
4654}
4655
4657 unsigned Depth) const {
4658 EVT VT = Op.getValueType();
4659 APInt DemandedElts = VT.isFixedLengthVector()
4661 : APInt(1, 1);
4662 return computeConstantRange(Op, DemandedElts, ForSigned, Depth);
4663}
4664
4666 const APInt &DemandedElts,
4667 bool ForSigned,
4668 unsigned Depth) const {
4669 EVT VT = Op.getValueType();
4670 unsigned BitWidth = VT.getScalarSizeInBits();
4671
4672 if (Depth >= MaxRecursionDepth)
4673 return ConstantRange::getFull(BitWidth);
4674
4675 if (ConstantSDNode *C = isConstOrConstSplat(Op, DemandedElts))
4676 return ConstantRange(C->getAPIntValue());
4677
4678 unsigned Opcode = Op.getOpcode();
4679 switch (Opcode) {
4680 case ISD::VSCALE: {
4682 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
4683 return getVScaleRange(&F, BitWidth).multiply(Multiplier);
4684 }
4685 default:
4686 break;
4687 }
4688
4689 return ConstantRange::getFull(BitWidth);
4690}
4691
4694 unsigned Depth) const {
4695 EVT VT = Op.getValueType();
4696
4697 APInt DemandedElts = VT.isFixedLengthVector()
4699 : APInt(1, 1);
4700
4701 return computeConstantRangeIncludingKnownBits(Op, DemandedElts, ForSigned,
4702 Depth);
4703}
4704
4706 SDValue Op, const APInt &DemandedElts, bool ForSigned,
4707 unsigned Depth) const {
4708 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
4709 ConstantRange CR1 = ConstantRange::fromKnownBits(Known, ForSigned);
4710 ConstantRange CR2 = computeConstantRange(Op, DemandedElts, ForSigned, Depth);
4713 return CR1.intersectWith(CR2, RangeType);
4714}
4715
4717 unsigned Depth) const {
4718 EVT VT = Val.getValueType();
4719
4720 // Since the number of lanes in a scalable vector is unknown at compile time,
4721 // we track one bit which is implicitly broadcast to all lanes. This means
4722 // that all lanes in a scalable vector are considered demanded.
4723 APInt DemandedElts = VT.isFixedLengthVector()
4725 : APInt(1, 1);
4726
4727 return isKnownToBeAPowerOfTwo(Val, DemandedElts, OrZero, Depth);
4728}
4729
4731 const APInt &DemandedElts,
4732 bool OrZero, unsigned Depth) const {
4733 if (Depth >= MaxRecursionDepth)
4734 return false; // Limit search depth.
4735
4736 EVT OpVT = Val.getValueType();
4737 unsigned BitWidth = OpVT.getScalarSizeInBits();
4738 [[maybe_unused]] unsigned NumElts = DemandedElts.getBitWidth();
4739 assert((!OpVT.isScalableVector() || NumElts == 1) &&
4740 "DemandedElts for scalable vectors must be 1 to represent all lanes");
4741 assert(
4742 (!OpVT.isFixedLengthVector() || NumElts == OpVT.getVectorNumElements()) &&
4743 "Unexpected vector size");
4744
4745 auto IsPowerOfTwoOrZero = [BitWidth, OrZero](const ConstantSDNode *C) {
4746 APInt V = C->getAPIntValue().zextOrTrunc(BitWidth);
4747 return (OrZero && V.isZero()) || V.isPowerOf2();
4748 };
4749
4750 // Is the constant a known power of 2 or zero?
4751 if (ISD::matchUnaryPredicate(Val, IsPowerOfTwoOrZero))
4752 return true;
4753
4754 switch (Val.getOpcode()) {
4755 case ISD::BUILD_VECTOR:
4756 // Are all operands of a build vector constant powers of two or zero?
4757 if (all_of(enumerate(Val->ops()), [&](auto P) {
4758 auto *C = dyn_cast<ConstantSDNode>(P.value());
4759 return !DemandedElts[P.index()] || (C && IsPowerOfTwoOrZero(C));
4760 }))
4761 return true;
4762 break;
4763
4764 case ISD::SPLAT_VECTOR:
4765 // Is the operand of a splat vector a constant power of two?
4766 if (auto *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4767 if (IsPowerOfTwoOrZero(C))
4768 return true;
4769 break;
4770
4772 SDValue InVec = Val.getOperand(0);
4773 SDValue EltNo = Val.getOperand(1);
4774 EVT VecVT = InVec.getValueType();
4775
4776 // Skip scalable vectors or implicit extensions.
4777 if (VecVT.isScalableVector() ||
4778 OpVT.getScalarSizeInBits() != VecVT.getScalarSizeInBits())
4779 break;
4780
4781 // If we know the element index, just demand that vector element, else for
4782 // an unknown element index, ignore DemandedElts and demand them all.
4783 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4784 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4785 APInt DemandedSrcElts =
4786 ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts)
4787 ? APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue())
4788 : APInt::getAllOnes(NumSrcElts);
4789 return isKnownToBeAPowerOfTwo(InVec, DemandedSrcElts, OrZero, Depth + 1);
4790 }
4791
4792 case ISD::AND: {
4793 // Looking for `x & -x` pattern:
4794 // If x == 0:
4795 // x & -x -> 0
4796 // If x != 0:
4797 // x & -x -> non-zero pow2
4798 // so if we find the pattern return whether we know `x` is non-zero.
4799 SDValue X;
4800 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4801 return OrZero || isKnownNeverZero(X, DemandedElts, Depth);
4802 break;
4803 }
4804
4805 case ISD::SHL: {
4806 // A left-shift of a constant one will have exactly one bit set because
4807 // shifting the bit off the end is undefined.
4808 auto *C = isConstOrConstSplat(Val.getOperand(0), DemandedElts);
4809 if (C && C->getAPIntValue() == 1)
4810 return true;
4811 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4812 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4813 Depth + 1);
4814 }
4815
4816 case ISD::SRL: {
4817 // A logical right-shift of a constant sign-bit will have exactly
4818 // one bit set.
4819 auto *C = isConstOrConstSplat(Val.getOperand(0), DemandedElts);
4820 if (C && C->getAPIntValue().isSignMask())
4821 return true;
4822 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4823 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4824 Depth + 1);
4825 }
4826
4827 case ISD::TRUNCATE: {
4828 return (OrZero || isKnownNeverZero(Val, Depth)) &&
4829 isKnownToBeAPowerOfTwo(Val.getOperand(0), OrZero, Depth + 1);
4830 }
4831
4832 case ISD::ROTL:
4833 case ISD::ROTR:
4834 return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4835 Depth + 1);
4836 case ISD::BSWAP:
4837 case ISD::BITREVERSE:
4838 return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4839 Depth + 1);
4840
4841 case ISD::SMIN:
4842 case ISD::SMAX:
4843 case ISD::UMIN:
4844 case ISD::UMAX:
4845 return isKnownToBeAPowerOfTwo(Val.getOperand(1), /*OrZero=*/false,
4846 Depth + 1) &&
4847 isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
4848 Depth + 1);
4849
4850 case ISD::SELECT:
4851 case ISD::VSELECT:
4852 return isKnownToBeAPowerOfTwo(Val.getOperand(2), DemandedElts, OrZero,
4853 Depth + 1) &&
4854 isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedElts, OrZero,
4855 Depth + 1);
4856
4857 case ISD::ZERO_EXTEND:
4858 return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
4859 Depth + 1);
4860
4861 case ISD::VSCALE:
4862 // vscale(power-of-two) is a power-of-two
4863 return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
4864 Depth + 1);
4865
4866 case ISD::VECTOR_SHUFFLE: {
4868 // Demanded elements with undef shuffle mask elements are unknown
4869 // - we cannot guarantee they are a power of two, so return false.
4870 APInt DemandedLHS, DemandedRHS;
4872 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4873 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4874 DemandedLHS, DemandedRHS))
4875 return false;
4876
4877 // All demanded elements from LHS must be known power of two.
4878 if (!!DemandedLHS && !isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedLHS,
4879 OrZero, Depth + 1))
4880 return false;
4881
4882 // All demanded elements from RHS must be known power of two.
4883 if (!!DemandedRHS && !isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedRHS,
4884 OrZero, Depth + 1))
4885 return false;
4886
4887 return true;
4888 }
4889 }
4890
4891 // More could be done here, though the above checks are enough
4892 // to handle some common cases.
4893 return false;
4894}
4895
4897 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4898 return C1->getValueAPF().getExactLog2Abs() >= 0;
4899
4900 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4901 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4902
4903 return false;
4904}
4905
4907 EVT VT = Op.getValueType();
4908
4909 // Since the number of lanes in a scalable vector is unknown at compile time,
4910 // we track one bit which is implicitly broadcast to all lanes. This means
4911 // that all lanes in a scalable vector are considered demanded.
4912 APInt DemandedElts = VT.isFixedLengthVector()
4914 : APInt(1, 1);
4915 return ComputeNumSignBits(Op, DemandedElts, Depth);
4916}
4917
4918unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4919 unsigned Depth) const {
4920 EVT VT = Op.getValueType();
4921 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4922 unsigned VTBits = VT.getScalarSizeInBits();
4923 unsigned NumElts = DemandedElts.getBitWidth();
4924 unsigned Tmp, Tmp2;
4925 unsigned FirstAnswer = 1;
4926
4927 assert((!VT.isScalableVector() || NumElts == 1) &&
4928 "DemandedElts for scalable vectors must be 1 to represent all lanes");
4929
4930 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4931 const APInt &Val = C->getAPIntValue();
4932 return Val.getNumSignBits();
4933 }
4934
4935 if (Depth >= MaxRecursionDepth)
4936 return 1; // Limit search depth.
4937
4938 if (!DemandedElts)
4939 return 1; // No demanded elts, better to assume we don't know anything.
4940
4941 unsigned Opcode = Op.getOpcode();
4942 switch (Opcode) {
4943 default: break;
4944 case ISD::AssertSext:
4945 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4946 return VTBits-Tmp+1;
4947 case ISD::AssertZext:
4948 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4949 return VTBits-Tmp;
4950 case ISD::FREEZE:
4951 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4952 /*PoisonOnly=*/false))
4953 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4954 break;
4955 case ISD::MERGE_VALUES:
4956 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4957 Depth + 1);
4958 case ISD::SPLAT_VECTOR: {
4959 // Check if the sign bits of source go down as far as the truncated value.
4960 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4961 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4962 if (NumSrcSignBits > (NumSrcBits - VTBits))
4963 return NumSrcSignBits - (NumSrcBits - VTBits);
4964 break;
4965 }
4966 case ISD::BUILD_VECTOR:
4967 assert(!VT.isScalableVector());
4968 Tmp = VTBits;
4969 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4970 if (!DemandedElts[i])
4971 continue;
4972
4973 SDValue SrcOp = Op.getOperand(i);
4974 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4975 // for constant nodes to ensure we only look at the sign bits.
4977 APInt T = C->getAPIntValue().trunc(VTBits);
4978 Tmp2 = T.getNumSignBits();
4979 } else {
4980 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4981
4982 if (SrcOp.getValueSizeInBits() != VTBits) {
4983 assert(SrcOp.getValueSizeInBits() > VTBits &&
4984 "Expected BUILD_VECTOR implicit truncation");
4985 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4986 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4987 }
4988 }
4989 Tmp = std::min(Tmp, Tmp2);
4990 }
4991 return Tmp;
4992
4993 case ISD::VECTOR_COMPRESS: {
4994 SDValue Vec = Op.getOperand(0);
4995 SDValue PassThru = Op.getOperand(2);
4996 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4997 if (Tmp == 1)
4998 return 1;
4999 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
5000 Tmp = std::min(Tmp, Tmp2);
5001 return Tmp;
5002 }
5003
5004 case ISD::VECTOR_SHUFFLE: {
5005 // Collect the minimum number of sign bits that are shared by every vector
5006 // element referenced by the shuffle.
5007 APInt DemandedLHS, DemandedRHS;
5009 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
5010 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
5011 DemandedLHS, DemandedRHS))
5012 return 1;
5013
5014 Tmp = std::numeric_limits<unsigned>::max();
5015 if (!!DemandedLHS)
5016 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
5017 if (!!DemandedRHS) {
5018 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
5019 Tmp = std::min(Tmp, Tmp2);
5020 }
5021 // If we don't know anything, early out and try computeKnownBits fall-back.
5022 if (Tmp == 1)
5023 break;
5024 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5025 return Tmp;
5026 }
5027
5028 case ISD::BITCAST: {
5029 if (VT.isScalableVector())
5030 break;
5031 SDValue N0 = Op.getOperand(0);
5032 EVT SrcVT = N0.getValueType();
5033 unsigned SrcBits = SrcVT.getScalarSizeInBits();
5034
5035 // Ignore bitcasts from unsupported types..
5036 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
5037 break;
5038
5039 // Fast handling of 'identity' bitcasts.
5040 if (VTBits == SrcBits)
5041 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
5042
5043 bool IsLE = getDataLayout().isLittleEndian();
5044
5045 // Bitcast 'large element' scalar/vector to 'small element' vector.
5046 if ((SrcBits % VTBits) == 0) {
5047 assert(VT.isVector() && "Expected bitcast to vector");
5048
5049 unsigned Scale = SrcBits / VTBits;
5050 APInt SrcDemandedElts =
5051 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
5052
5053 // Fast case - sign splat can be simply split across the small elements.
5054 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
5055 if (Tmp == SrcBits)
5056 return VTBits;
5057
5058 // Slow case - determine how far the sign extends into each sub-element.
5059 Tmp2 = VTBits;
5060 for (unsigned i = 0; i != NumElts; ++i)
5061 if (DemandedElts[i]) {
5062 unsigned SubOffset = i % Scale;
5063 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
5064 SubOffset = SubOffset * VTBits;
5065 if (Tmp <= SubOffset)
5066 return 1;
5067 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
5068 }
5069 return Tmp2;
5070 }
5071 break;
5072 }
5073
5075 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
5076 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
5077 return VTBits - Tmp + 1;
5078 case ISD::SIGN_EXTEND:
5079 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
5080 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
5082 // Max of the input and what this extends.
5083 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
5084 Tmp = VTBits-Tmp+1;
5085 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
5086 return std::max(Tmp, Tmp2);
5088 if (VT.isScalableVector())
5089 break;
5090 SDValue Src = Op.getOperand(0);
5091 EVT SrcVT = Src.getValueType();
5092 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
5093 Tmp = VTBits - SrcVT.getScalarSizeInBits();
5094 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
5095 }
5096 case ISD::SRA:
5097 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5098 // SRA X, C -> adds C sign bits.
5099 if (std::optional<unsigned> ShAmt =
5100 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
5101 Tmp = std::min(Tmp + *ShAmt, VTBits);
5102 return Tmp;
5103 case ISD::SHL:
5104 if (std::optional<ConstantRange> ShAmtRange =
5105 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
5106 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
5107 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
5108 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
5109 // shifted out, then we can compute the number of sign bits for the
5110 // operand being extended. A future improvement could be to pass along the
5111 // "shifted left by" information in the recursive calls to
5112 // ComputeKnownSignBits. Allowing us to handle this more generically.
5113 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
5114 SDValue Ext = Op.getOperand(0);
5115 EVT ExtVT = Ext.getValueType();
5116 SDValue Extendee = Ext.getOperand(0);
5117 EVT ExtendeeVT = Extendee.getValueType();
5118 unsigned SizeDifference =
5119 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
5120 if (SizeDifference <= MinShAmt) {
5121 Tmp = SizeDifference +
5122 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
5123 if (MaxShAmt < Tmp)
5124 return Tmp - MaxShAmt;
5125 }
5126 }
5127 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
5128 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5129 if (MaxShAmt < Tmp)
5130 return Tmp - MaxShAmt;
5131 }
5132 break;
5133 case ISD::AND:
5134 case ISD::OR:
5135 case ISD::XOR: // NOT is handled here.
5136 // Logical binary ops preserve the number of sign bits at the worst.
5137 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
5138 if (Tmp != 1) {
5139 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
5140 FirstAnswer = std::min(Tmp, Tmp2);
5141 // We computed what we know about the sign bits as our first
5142 // answer. Now proceed to the generic code that uses
5143 // computeKnownBits, and pick whichever answer is better.
5144 }
5145 break;
5146
5147 case ISD::SELECT:
5148 case ISD::VSELECT:
5149 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
5150 if (Tmp == 1) return 1; // Early out.
5151 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
5152 return std::min(Tmp, Tmp2);
5153 case ISD::SELECT_CC:
5154 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
5155 if (Tmp == 1) return 1; // Early out.
5156 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
5157 return std::min(Tmp, Tmp2);
5158
5159 case ISD::SMIN:
5160 case ISD::SMAX: {
5161 // If we have a clamp pattern, we know that the number of sign bits will be
5162 // the minimum of the clamp min/max range.
5163 bool IsMax = (Opcode == ISD::SMAX);
5164 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
5165 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
5166 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
5167 CstHigh =
5168 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
5169 if (CstLow && CstHigh) {
5170 if (!IsMax)
5171 std::swap(CstLow, CstHigh);
5172 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
5173 Tmp = CstLow->getAPIntValue().getNumSignBits();
5174 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
5175 return std::min(Tmp, Tmp2);
5176 }
5177 }
5178
5179 // Fallback - just get the minimum number of sign bits of the operands.
5180 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5181 if (Tmp == 1)
5182 return 1; // Early out.
5183 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5184 return std::min(Tmp, Tmp2);
5185 }
5186 case ISD::UMIN:
5187 case ISD::UMAX:
5188 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5189 if (Tmp == 1)
5190 return 1; // Early out.
5191 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5192 return std::min(Tmp, Tmp2);
5193 case ISD::SSUBO_CARRY:
5194 case ISD::USUBO_CARRY:
5195 // sub_carry(x,x,c) -> 0/-1 (sext carry)
5196 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
5197 return VTBits;
5198 [[fallthrough]];
5199 case ISD::SADDO:
5200 case ISD::UADDO:
5201 case ISD::SADDO_CARRY:
5202 case ISD::UADDO_CARRY:
5203 case ISD::SSUBO:
5204 case ISD::USUBO:
5205 case ISD::SMULO:
5206 case ISD::UMULO:
5207 if (Op.getResNo() != 1)
5208 break;
5209 // The boolean result conforms to getBooleanContents. Fall through.
5210 // If setcc returns 0/-1, all bits are sign bits.
5211 // We know that we have an integer-based boolean since these operations
5212 // are only available for integer.
5213 if (TLI->getBooleanContents(VT.isVector(), false) ==
5215 return VTBits;
5216 break;
5217 case ISD::SETCC:
5218 case ISD::SETCCCARRY:
5219 case ISD::STRICT_FSETCC:
5220 case ISD::STRICT_FSETCCS: {
5221 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5222 // If setcc returns 0/-1, all bits are sign bits.
5223 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5225 return VTBits;
5226 break;
5227 }
5228 case ISD::ROTL:
5229 case ISD::ROTR:
5230 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5231
5232 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5233 if (Tmp == VTBits)
5234 return VTBits;
5235
5236 if (ConstantSDNode *C =
5237 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5238 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5239
5240 // Handle rotate right by N like a rotate left by 32-N.
5241 if (Opcode == ISD::ROTR)
5242 RotAmt = (VTBits - RotAmt) % VTBits;
5243
5244 // If we aren't rotating out all of the known-in sign bits, return the
5245 // number that are left. This handles rotl(sext(x), 1) for example.
5246 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5247 }
5248 break;
5249 case ISD::ADD:
5250 case ISD::ADDC:
5251 // TODO: Move Operand 1 check before Operand 0 check
5252 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5253 if (Tmp == 1) return 1; // Early out.
5254
5255 // Special case decrementing a value (ADD X, -1):
5256 if (ConstantSDNode *CRHS =
5257 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5258 if (CRHS->isAllOnes()) {
5259 KnownBits Known =
5260 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5261
5262 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5263 // sign bits set.
5264 if ((Known.Zero | 1).isAllOnes())
5265 return VTBits;
5266
5267 // If we are subtracting one from a positive number, there is no carry
5268 // out of the result.
5269 if (Known.isNonNegative())
5270 return Tmp;
5271 }
5272
5273 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5274 if (Tmp2 == 1) return 1; // Early out.
5275
5276 // Add can have at most one carry bit. Thus we know that the output
5277 // is, at worst, one more bit than the inputs.
5278 return std::min(Tmp, Tmp2) - 1;
5279 case ISD::SUB:
5280 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5281 if (Tmp2 == 1) return 1; // Early out.
5282
5283 // Handle NEG.
5284 if (ConstantSDNode *CLHS =
5285 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5286 if (CLHS->isZero()) {
5287 KnownBits Known =
5288 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5289 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5290 // sign bits set.
5291 if ((Known.Zero | 1).isAllOnes())
5292 return VTBits;
5293
5294 // If the input is known to be positive (the sign bit is known clear),
5295 // the output of the NEG has the same number of sign bits as the input.
5296 if (Known.isNonNegative())
5297 return Tmp2;
5298
5299 // Otherwise, we treat this like a SUB.
5300 }
5301
5302 // Sub can have at most one carry bit. Thus we know that the output
5303 // is, at worst, one more bit than the inputs.
5304 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5305 if (Tmp == 1) return 1; // Early out.
5306 return std::min(Tmp, Tmp2) - 1;
5307 case ISD::MUL: {
5308 // The output of the Mul can be at most twice the valid bits in the inputs.
5309 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5310 if (SignBitsOp0 == 1)
5311 break;
5312 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5313 if (SignBitsOp1 == 1)
5314 break;
5315 unsigned OutValidBits =
5316 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5317 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5318 }
5319 case ISD::AVGCEILS:
5320 case ISD::AVGFLOORS:
5321 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5322 if (Tmp == 1)
5323 return 1; // Early out.
5324 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5325 return std::min(Tmp, Tmp2);
5326 case ISD::SREM:
5327 // The sign bit is the LHS's sign bit, except when the result of the
5328 // remainder is zero. The magnitude of the result should be less than or
5329 // equal to the magnitude of the LHS. Therefore, the result should have
5330 // at least as many sign bits as the left hand side.
5331 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5332 case ISD::TRUNCATE: {
5333 // Check if the sign bits of source go down as far as the truncated value.
5334 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5335 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5336 if (NumSrcSignBits > (NumSrcBits - VTBits))
5337 return NumSrcSignBits - (NumSrcBits - VTBits);
5338 break;
5339 }
5340 case ISD::EXTRACT_ELEMENT: {
5341 if (VT.isScalableVector())
5342 break;
5343 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5344 const int BitWidth = Op.getValueSizeInBits();
5345 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5346
5347 // Get reverse index (starting from 1), Op1 value indexes elements from
5348 // little end. Sign starts at big end.
5349 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5350
5351 // If the sign portion ends in our element the subtraction gives correct
5352 // result. Otherwise it gives either negative or > bitwidth result
5353 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5354 }
5356 if (VT.isScalableVector())
5357 break;
5358 // If we know the element index, split the demand between the
5359 // source vector and the inserted element, otherwise assume we need
5360 // the original demanded vector elements and the value.
5361 SDValue InVec = Op.getOperand(0);
5362 SDValue InVal = Op.getOperand(1);
5363 SDValue EltNo = Op.getOperand(2);
5364 bool DemandedVal = true;
5365 APInt DemandedVecElts = DemandedElts;
5366 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5367 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5368 unsigned EltIdx = CEltNo->getZExtValue();
5369 DemandedVal = !!DemandedElts[EltIdx];
5370 DemandedVecElts.clearBit(EltIdx);
5371 }
5372 Tmp = std::numeric_limits<unsigned>::max();
5373 if (DemandedVal) {
5374 // TODO - handle implicit truncation of inserted elements.
5375 if (InVal.getScalarValueSizeInBits() != VTBits)
5376 break;
5377 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5378 Tmp = std::min(Tmp, Tmp2);
5379 }
5380 if (!!DemandedVecElts) {
5381 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5382 Tmp = std::min(Tmp, Tmp2);
5383 }
5384 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5385 return Tmp;
5386 }
5388 SDValue InVec = Op.getOperand(0);
5389 SDValue EltNo = Op.getOperand(1);
5390 EVT VecVT = InVec.getValueType();
5391 // ComputeNumSignBits not yet implemented for scalable vectors.
5392 if (VecVT.isScalableVector())
5393 break;
5394 const unsigned BitWidth = Op.getValueSizeInBits();
5395 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5396 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5397
5398 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5399 // anything about sign bits. But if the sizes match we can derive knowledge
5400 // about sign bits from the vector operand.
5401 if (BitWidth != EltBitWidth)
5402 break;
5403
5404 // If we know the element index, just demand that vector element, else for
5405 // an unknown element index, ignore DemandedElts and demand them all.
5406 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5407 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5408 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5409 DemandedSrcElts =
5410 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5411
5412 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5413 }
5415 // Offset the demanded elts by the subvector index.
5416 SDValue Src = Op.getOperand(0);
5417
5418 APInt DemandedSrcElts;
5419 if (Src.getValueType().isScalableVector())
5420 DemandedSrcElts = APInt(1, 1);
5421 else {
5422 uint64_t Idx = Op.getConstantOperandVal(1);
5423 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5424 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5425 }
5426 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5427 }
5428 case ISD::CONCAT_VECTORS: {
5429 if (VT.isScalableVector())
5430 break;
5431 // Determine the minimum number of sign bits across all demanded
5432 // elts of the input vectors. Early out if the result is already 1.
5433 Tmp = std::numeric_limits<unsigned>::max();
5434 EVT SubVectorVT = Op.getOperand(0).getValueType();
5435 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5436 unsigned NumSubVectors = Op.getNumOperands();
5437 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5438 APInt DemandedSub =
5439 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5440 if (!DemandedSub)
5441 continue;
5442 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, 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::INSERT_SUBVECTOR: {
5449 if (VT.isScalableVector())
5450 break;
5451 // Demand any elements from the subvector and the remainder from the src its
5452 // inserted into.
5453 SDValue Src = Op.getOperand(0);
5454 SDValue Sub = Op.getOperand(1);
5455 uint64_t Idx = Op.getConstantOperandVal(2);
5456 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5457 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5458 APInt DemandedSrcElts = DemandedElts;
5459 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5460
5461 Tmp = std::numeric_limits<unsigned>::max();
5462 if (!!DemandedSubElts) {
5463 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5464 if (Tmp == 1)
5465 return 1; // early-out
5466 }
5467 if (!!DemandedSrcElts) {
5468 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5469 Tmp = std::min(Tmp, Tmp2);
5470 }
5471 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5472 return Tmp;
5473 }
5474 case ISD::LOAD: {
5475 // If we are looking at the loaded value of the SDNode.
5476 if (Op.getResNo() != 0)
5477 break;
5478
5480 if (const MDNode *Ranges = LD->getRanges()) {
5481 if (DemandedElts != 1)
5482 break;
5483
5485 if (VTBits > CR.getBitWidth()) {
5486 switch (LD->getExtensionType()) {
5487 case ISD::SEXTLOAD:
5488 CR = CR.signExtend(VTBits);
5489 break;
5490 case ISD::ZEXTLOAD:
5491 CR = CR.zeroExtend(VTBits);
5492 break;
5493 default:
5494 break;
5495 }
5496 }
5497
5498 if (VTBits != CR.getBitWidth())
5499 break;
5500 return std::min(CR.getSignedMin().getNumSignBits(),
5502 }
5503
5504 unsigned ExtType = LD->getExtensionType();
5505 switch (ExtType) {
5506 default:
5507 break;
5508 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5509 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5510 return VTBits - Tmp + 1;
5511 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5512 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5513 return VTBits - Tmp;
5514 case ISD::NON_EXTLOAD:
5515 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5516 // We only need to handle vectors - computeKnownBits should handle
5517 // scalar cases.
5518 Type *CstTy = Cst->getType();
5519 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5520 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5521 VTBits == CstTy->getScalarSizeInBits()) {
5522 Tmp = VTBits;
5523 for (unsigned i = 0; i != NumElts; ++i) {
5524 if (!DemandedElts[i])
5525 continue;
5526 if (Constant *Elt = Cst->getAggregateElement(i)) {
5527 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5528 const APInt &Value = CInt->getValue();
5529 Tmp = std::min(Tmp, Value.getNumSignBits());
5530 continue;
5531 }
5532 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5533 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5534 Tmp = std::min(Tmp, Value.getNumSignBits());
5535 continue;
5536 }
5537 }
5538 // Unknown type. Conservatively assume no bits match sign bit.
5539 return 1;
5540 }
5541 return Tmp;
5542 }
5543 }
5544 break;
5545 }
5546
5547 break;
5548 }
5551 case ISD::ATOMIC_SWAP:
5563 case ISD::ATOMIC_LOAD: {
5564 auto *AT = cast<AtomicSDNode>(Op);
5565 // If we are looking at the loaded value.
5566 if (Op.getResNo() == 0) {
5567 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5568 if (Tmp == VTBits)
5569 return 1; // early-out
5570
5571 // For atomic_load, prefer to use the extension type.
5572 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5573 switch (AT->getExtensionType()) {
5574 default:
5575 break;
5576 case ISD::SEXTLOAD:
5577 return VTBits - Tmp + 1;
5578 case ISD::ZEXTLOAD:
5579 return VTBits - Tmp;
5580 }
5581 }
5582
5583 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5584 return VTBits - Tmp + 1;
5585 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5586 return VTBits - Tmp;
5587 }
5588 break;
5589 }
5590 }
5591
5592 // Allow the target to implement this method for its nodes.
5593 if (Opcode >= ISD::BUILTIN_OP_END ||
5594 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5595 Opcode == ISD::INTRINSIC_W_CHAIN ||
5596 Opcode == ISD::INTRINSIC_VOID) {
5597 // TODO: This can probably be removed once target code is audited. This
5598 // is here purely to reduce patch size and review complexity.
5599 if (!VT.isScalableVector()) {
5600 unsigned NumBits =
5601 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5602 if (NumBits > 1)
5603 FirstAnswer = std::max(FirstAnswer, NumBits);
5604 }
5605 }
5606
5607 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5608 // use this information.
5609 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5610 return std::max(FirstAnswer, Known.countMinSignBits());
5611}
5612
5614 unsigned Depth) const {
5615 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5616 return Op.getScalarValueSizeInBits() - SignBits + 1;
5617}
5618
5620 const APInt &DemandedElts,
5621 unsigned Depth) const {
5622 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5623 return Op.getScalarValueSizeInBits() - SignBits + 1;
5624}
5625
5627 unsigned Depth) const {
5628 // Early out for FREEZE.
5629 if (Op.getOpcode() == ISD::FREEZE)
5630 return true;
5631
5632 EVT VT = Op.getValueType();
5633 APInt DemandedElts = VT.isFixedLengthVector()
5635 : APInt(1, 1);
5636 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5637}
5638
5640 const APInt &DemandedElts,
5641 bool PoisonOnly,
5642 unsigned Depth) const {
5643 unsigned Opcode = Op.getOpcode();
5644
5645 // Early out for FREEZE.
5646 if (Opcode == ISD::FREEZE)
5647 return true;
5648
5649 if (Depth >= MaxRecursionDepth)
5650 return false; // Limit search depth.
5651
5652 if (isIntOrFPConstant(Op))
5653 return true;
5654
5655 switch (Opcode) {
5656 case ISD::CONDCODE:
5657 case ISD::VALUETYPE:
5658 case ISD::FrameIndex:
5660 case ISD::CopyFromReg:
5661 return true;
5662
5663 case ISD::POISON:
5664 return false;
5665
5666 case ISD::UNDEF:
5667 return PoisonOnly;
5668
5669 case ISD::BUILD_VECTOR:
5670 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5671 // this shouldn't affect the result.
5672 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5673 if (!DemandedElts[i])
5674 continue;
5676 Depth + 1))
5677 return false;
5678 }
5679 return true;
5680
5682 SDValue Src = Op.getOperand(0);
5683 if (Src.getValueType().isScalableVector())
5684 break;
5685 uint64_t Idx = Op.getConstantOperandVal(1);
5686 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5687 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5688 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5689 Depth + 1);
5690 }
5691
5692 case ISD::INSERT_SUBVECTOR: {
5693 if (Op.getValueType().isScalableVector())
5694 break;
5695 SDValue Src = Op.getOperand(0);
5696 SDValue Sub = Op.getOperand(1);
5697 uint64_t Idx = Op.getConstantOperandVal(2);
5698 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5699 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5700 APInt DemandedSrcElts = DemandedElts;
5701 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5702
5703 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5704 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5705 return false;
5706 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5707 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5708 return false;
5709 return true;
5710 }
5711
5713 SDValue Src = Op.getOperand(0);
5714 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5715 EVT SrcVT = Src.getValueType();
5716 if (SrcVT.isFixedLengthVector() && IndexC &&
5717 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5718 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5719 IndexC->getZExtValue());
5720 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5721 Depth + 1);
5722 }
5723 break;
5724 }
5725
5727 SDValue InVec = Op.getOperand(0);
5728 SDValue InVal = Op.getOperand(1);
5729 SDValue EltNo = Op.getOperand(2);
5730 EVT VT = InVec.getValueType();
5731 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5732 if (IndexC && VT.isFixedLengthVector() &&
5733 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5734 if (DemandedElts[IndexC->getZExtValue()] &&
5736 return false;
5737 APInt InVecDemandedElts = DemandedElts;
5738 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5739 if (!!InVecDemandedElts &&
5741 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5742 InVecDemandedElts, PoisonOnly, Depth + 1))
5743 return false;
5744 return true;
5745 }
5746 break;
5747 }
5748
5750 // Check upper (known undef) elements.
5751 if (DemandedElts.ugt(1) && !PoisonOnly)
5752 return false;
5753 // Check element zero.
5754 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5755 Op.getOperand(0), PoisonOnly, Depth + 1))
5756 return false;
5757 return true;
5758
5759 case ISD::SPLAT_VECTOR:
5760 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5761 Depth + 1);
5762
5763 case ISD::VECTOR_SHUFFLE: {
5764 APInt DemandedLHS, DemandedRHS;
5765 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5766 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5767 DemandedElts, DemandedLHS, DemandedRHS,
5768 /*AllowUndefElts=*/false))
5769 return false;
5770 if (!DemandedLHS.isZero() &&
5771 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5772 PoisonOnly, Depth + 1))
5773 return false;
5774 if (!DemandedRHS.isZero() &&
5775 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5776 PoisonOnly, Depth + 1))
5777 return false;
5778 return true;
5779 }
5780
5781 case ISD::SHL:
5782 case ISD::SRL:
5783 case ISD::SRA:
5784 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5785 // enough to check operand 0 if Op can't create undef/poison.
5786 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5787 /*ConsiderFlags*/ true, Depth) &&
5788 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5789 PoisonOnly, Depth + 1);
5790
5791 case ISD::BSWAP:
5792 case ISD::CTPOP:
5793 case ISD::BITREVERSE:
5794 case ISD::AND:
5795 case ISD::OR:
5796 case ISD::XOR:
5797 case ISD::ADD:
5798 case ISD::SUB:
5799 case ISD::MUL:
5800 case ISD::SADDSAT:
5801 case ISD::UADDSAT:
5802 case ISD::SSUBSAT:
5803 case ISD::USUBSAT:
5804 case ISD::SSHLSAT:
5805 case ISD::USHLSAT:
5806 case ISD::SMIN:
5807 case ISD::SMAX:
5808 case ISD::UMIN:
5809 case ISD::UMAX:
5810 case ISD::ZERO_EXTEND:
5811 case ISD::SIGN_EXTEND:
5812 case ISD::ANY_EXTEND:
5813 case ISD::TRUNCATE:
5814 case ISD::VSELECT: {
5815 // If Op can't create undef/poison and none of its operands are undef/poison
5816 // then Op is never undef/poison. A difference from the more common check
5817 // below, outside the switch, is that we handle elementwise operations for
5818 // which the DemandedElts mask is valid for all operands here.
5819 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5820 /*ConsiderFlags*/ true, Depth) &&
5821 all_of(Op->ops(), [&](SDValue V) {
5822 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5823 PoisonOnly, Depth + 1);
5824 });
5825 }
5826
5827 // TODO: Search for noundef attributes from library functions.
5828
5829 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5830
5831 default:
5832 // Allow the target to implement this method for its nodes.
5833 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5834 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5835 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5836 Op, DemandedElts, *this, PoisonOnly, Depth);
5837 break;
5838 }
5839
5840 // If Op can't create undef/poison and none of its operands are undef/poison
5841 // then Op is never undef/poison.
5842 // NOTE: TargetNodes can handle this in themselves in
5843 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5844 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5845 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5846 Depth) &&
5847 all_of(Op->ops(), [&](SDValue V) {
5848 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5849 });
5850}
5851
5853 bool ConsiderFlags,
5854 unsigned Depth) const {
5855 EVT VT = Op.getValueType();
5856 APInt DemandedElts = VT.isFixedLengthVector()
5858 : APInt(1, 1);
5859 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5860 Depth);
5861}
5862
5864 bool PoisonOnly, bool ConsiderFlags,
5865 unsigned Depth) const {
5866 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5867 return true;
5868
5869 unsigned Opcode = Op.getOpcode();
5870 switch (Opcode) {
5871 case ISD::AssertSext:
5872 case ISD::AssertZext:
5873 case ISD::AssertAlign:
5875 // Assertion nodes can create poison if the assertion fails.
5876 return true;
5877
5878 case ISD::FREEZE:
5882 case ISD::SADDSAT:
5883 case ISD::UADDSAT:
5884 case ISD::SSUBSAT:
5885 case ISD::USUBSAT:
5886 case ISD::MULHU:
5887 case ISD::MULHS:
5888 case ISD::AVGFLOORS:
5889 case ISD::AVGFLOORU:
5890 case ISD::AVGCEILS:
5891 case ISD::AVGCEILU:
5892 case ISD::ABDU:
5893 case ISD::ABDS:
5894 case ISD::SMIN:
5895 case ISD::SMAX:
5896 case ISD::SCMP:
5897 case ISD::UMIN:
5898 case ISD::UMAX:
5899 case ISD::UCMP:
5900 case ISD::AND:
5901 case ISD::XOR:
5902 case ISD::ROTL:
5903 case ISD::ROTR:
5904 case ISD::FSHL:
5905 case ISD::FSHR:
5906 case ISD::BSWAP:
5907 case ISD::CTTZ:
5908 case ISD::CTLZ:
5909 case ISD::CTLS:
5910 case ISD::CTPOP:
5911 case ISD::BITREVERSE:
5912 case ISD::PARITY:
5913 case ISD::SIGN_EXTEND:
5914 case ISD::TRUNCATE:
5918 case ISD::BITCAST:
5919 case ISD::BUILD_VECTOR:
5920 case ISD::BUILD_PAIR:
5921 case ISD::SPLAT_VECTOR:
5922 case ISD::FABS:
5923 return false;
5924
5925 case ISD::ABS:
5926 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5927 // Different to Intrinsic::abs.
5928 return false;
5929
5930 case ISD::ADDC:
5931 case ISD::SUBC:
5932 case ISD::ADDE:
5933 case ISD::SUBE:
5934 case ISD::SADDO:
5935 case ISD::SSUBO:
5936 case ISD::SMULO:
5937 case ISD::SADDO_CARRY:
5938 case ISD::SSUBO_CARRY:
5939 case ISD::UADDO:
5940 case ISD::USUBO:
5941 case ISD::UMULO:
5942 case ISD::UADDO_CARRY:
5943 case ISD::USUBO_CARRY:
5944 // No poison on result or overflow flags.
5945 return false;
5946
5947 case ISD::SELECT_CC:
5948 case ISD::SETCC: {
5949 // Integer setcc cannot create undef or poison.
5950 if (Op.getOperand(0).getValueType().isInteger())
5951 return false;
5952
5953 // FP compares are more complicated. They can create poison for nan/infinity
5954 // based on options and flags. The options and flags also cause special
5955 // nonan condition codes to be used. Those condition codes may be preserved
5956 // even if the nonan flag is dropped somewhere.
5957 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5958 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5959 return (unsigned)CCCode & 0x10U;
5960 }
5961
5962 case ISD::OR:
5963 case ISD::ZERO_EXTEND:
5964 case ISD::SELECT:
5965 case ISD::VSELECT:
5966 case ISD::ADD:
5967 case ISD::SUB:
5968 case ISD::MUL:
5969 case ISD::FNEG:
5970 case ISD::FADD:
5971 case ISD::FSUB:
5972 case ISD::FMUL:
5973 case ISD::FDIV:
5974 case ISD::FREM:
5975 case ISD::FCOPYSIGN:
5976 case ISD::FMA:
5977 case ISD::FMAD:
5978 case ISD::FMULADD:
5979 case ISD::FP_EXTEND:
5985 // No poison except from flags (which is handled above)
5986 return false;
5987
5988 case ISD::SHL:
5989 case ISD::SRL:
5990 case ISD::SRA:
5991 // If the max shift amount isn't in range, then the shift can
5992 // create poison.
5993 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5994
5997 // If the amount is zero then the result will be poison.
5998 // TODO: Add isKnownNeverZero DemandedElts handling.
5999 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
6000
6002 // Check if we demand any upper (undef) elements.
6003 return !PoisonOnly && DemandedElts.ugt(1);
6004
6007 // Ensure that the element index is in bounds.
6008 EVT VecVT = Op.getOperand(0).getValueType();
6009 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
6010 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
6011 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
6012 }
6013
6014 case ISD::VECTOR_SHUFFLE: {
6015 // Check for any demanded shuffle element that is undef.
6016 auto *SVN = cast<ShuffleVectorSDNode>(Op);
6017 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
6018 if (Elt < 0 && DemandedElts[Idx])
6019 return true;
6020 return false;
6021 }
6022
6024 return false;
6025
6026 default:
6027 // Allow the target to implement this method for its nodes.
6028 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6029 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
6030 return TLI->canCreateUndefOrPoisonForTargetNode(
6031 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
6032 break;
6033 }
6034
6035 // Be conservative and return true.
6036 return true;
6037}
6038
6039bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
6040 unsigned Opcode = Op.getOpcode();
6041 if (Opcode == ISD::OR)
6042 return Op->getFlags().hasDisjoint() ||
6043 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
6044 if (Opcode == ISD::XOR)
6045 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
6046 return false;
6047}
6048
6050 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
6051 (Op.isAnyAdd() || isADDLike(Op));
6052}
6053
6055 FPClassTest InterestedClasses,
6056 unsigned Depth) const {
6057 EVT VT = Op.getValueType();
6058 APInt DemandedElts = VT.isFixedLengthVector()
6060 : APInt(1, 1);
6061 return computeKnownFPClass(Op, DemandedElts, InterestedClasses, Depth);
6062}
6063
6065 const APInt &DemandedElts,
6066 FPClassTest InterestedClasses,
6067 unsigned Depth) const {
6068 KnownFPClass Known;
6069
6070 if (const auto *CFP = dyn_cast<ConstantFPSDNode>(Op))
6071 return KnownFPClass(CFP->getValueAPF());
6072
6073 if (Depth >= MaxRecursionDepth)
6074 return Known;
6075
6076 if (Op.getOpcode() == ISD::UNDEF)
6077 return Known;
6078
6079 [[maybe_unused]] EVT VT = Op.getValueType();
6080 assert((!VT.isFixedLengthVector() ||
6081 DemandedElts.getBitWidth() == VT.getVectorNumElements()) &&
6082 "Unexpected vector size");
6083
6084 if (!DemandedElts)
6085 return Known;
6086
6087 unsigned Opcode = Op.getOpcode();
6088 switch (Opcode) {
6089 case ISD::POISON: {
6090 Known.KnownFPClasses = fcNone;
6091 Known.SignBit = false;
6092 break;
6093 }
6094 case ISD::BUILD_VECTOR: {
6095 assert(!VT.isScalableVector());
6096 bool First = true;
6097 for (unsigned I = 0, E = Op.getNumOperands(); I != E; ++I) {
6098 if (!DemandedElts[I])
6099 continue;
6100
6101 if (First) {
6102 Known =
6103 computeKnownFPClass(Op.getOperand(I), InterestedClasses, Depth + 1);
6104 First = false;
6105 } else {
6106 Known |=
6107 computeKnownFPClass(Op.getOperand(I), InterestedClasses, Depth + 1);
6108 }
6109
6110 if (Known.isUnknown())
6111 break;
6112 }
6113 break;
6114 }
6115 default:
6116 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6117 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6118 TLI->computeKnownFPClassForTargetNode(Op, Known, DemandedElts, *this,
6119 Depth);
6120 }
6121 break;
6122 }
6123
6124 return Known;
6125}
6126
6128 unsigned Depth) const {
6129 EVT VT = Op.getValueType();
6130
6131 // Since the number of lanes in a scalable vector is unknown at compile time,
6132 // we track one bit which is implicitly broadcast to all lanes. This means
6133 // that all lanes in a scalable vector are considered demanded.
6134 APInt DemandedElts = VT.isFixedLengthVector()
6136 : APInt(1, 1);
6137
6138 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
6139}
6140
6142 bool SNaN, unsigned Depth) const {
6143 assert(!DemandedElts.isZero() && "No demanded elements");
6144
6145 // If we're told that NaNs won't happen, assume they won't.
6146 if (Op->getFlags().hasNoNaNs())
6147 return true;
6148
6149 if (Depth >= MaxRecursionDepth)
6150 return false; // Limit search depth.
6151
6152 unsigned Opcode = Op.getOpcode();
6153 switch (Opcode) {
6154 case ISD::FADD:
6155 case ISD::FSUB:
6156 case ISD::FMUL:
6157 case ISD::FDIV:
6158 case ISD::FREM:
6159 case ISD::FSIN:
6160 case ISD::FCOS:
6161 case ISD::FTAN:
6162 case ISD::FASIN:
6163 case ISD::FACOS:
6164 case ISD::FATAN:
6165 case ISD::FATAN2:
6166 case ISD::FSINH:
6167 case ISD::FCOSH:
6168 case ISD::FTANH:
6169 case ISD::FMA:
6170 case ISD::FMULADD:
6171 case ISD::FMAD: {
6172 if (SNaN)
6173 return true;
6174 // TODO: Need isKnownNeverInfinity
6175 return false;
6176 }
6177 case ISD::FCANONICALIZE:
6178 case ISD::FEXP:
6179 case ISD::FEXP2:
6180 case ISD::FEXP10:
6181 case ISD::FTRUNC:
6182 case ISD::FFLOOR:
6183 case ISD::FCEIL:
6184 case ISD::FROUND:
6185 case ISD::FROUNDEVEN:
6186 case ISD::LROUND:
6187 case ISD::LLROUND:
6188 case ISD::FRINT:
6189 case ISD::LRINT:
6190 case ISD::LLRINT:
6191 case ISD::FNEARBYINT:
6192 case ISD::FLDEXP: {
6193 if (SNaN)
6194 return true;
6195 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6196 }
6197 case ISD::FABS:
6198 case ISD::FNEG:
6199 case ISD::FCOPYSIGN: {
6200 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6201 }
6202 case ISD::SELECT:
6203 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
6204 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
6205 case ISD::FP_EXTEND:
6206 case ISD::FP_ROUND: {
6207 if (SNaN)
6208 return true;
6209 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6210 }
6211 case ISD::SINT_TO_FP:
6212 case ISD::UINT_TO_FP:
6213 return true;
6214 case ISD::FSQRT: // Need is known positive
6215 case ISD::FLOG:
6216 case ISD::FLOG2:
6217 case ISD::FLOG10:
6218 case ISD::FPOWI:
6219 case ISD::FPOW: {
6220 if (SNaN)
6221 return true;
6222 // TODO: Refine on operand
6223 return false;
6224 }
6225 case ISD::FMINNUM:
6226 case ISD::FMAXNUM:
6227 case ISD::FMINIMUMNUM:
6228 case ISD::FMAXIMUMNUM: {
6229 // Only one needs to be known not-nan, since it will be returned if the
6230 // other ends up being one.
6231 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
6232 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
6233 }
6234 case ISD::FMINNUM_IEEE:
6235 case ISD::FMAXNUM_IEEE: {
6236 if (SNaN)
6237 return true;
6238 // This can return a NaN if either operand is an sNaN, or if both operands
6239 // are NaN.
6240 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
6241 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
6242 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
6243 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
6244 }
6245 case ISD::FMINIMUM:
6246 case ISD::FMAXIMUM: {
6247 // TODO: Does this quiet or return the origina NaN as-is?
6248 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
6249 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
6250 }
6252 SDValue Src = Op.getOperand(0);
6253 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6254 EVT SrcVT = Src.getValueType();
6255 if (SrcVT.isFixedLengthVector() && Idx &&
6256 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
6257 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
6258 Idx->getZExtValue());
6259 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6260 }
6261 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6262 }
6264 SDValue Src = Op.getOperand(0);
6265 if (Src.getValueType().isFixedLengthVector()) {
6266 unsigned Idx = Op.getConstantOperandVal(1);
6267 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
6268 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
6269 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6270 }
6271 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6272 }
6273 case ISD::INSERT_SUBVECTOR: {
6274 SDValue BaseVector = Op.getOperand(0);
6275 SDValue SubVector = Op.getOperand(1);
6276 EVT BaseVectorVT = BaseVector.getValueType();
6277 if (BaseVectorVT.isFixedLengthVector()) {
6278 unsigned Idx = Op.getConstantOperandVal(2);
6279 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
6280 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
6281
6282 // Clear/Extract the bits at the position where the subvector will be
6283 // inserted.
6284 APInt DemandedMask =
6285 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
6286 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
6287 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
6288
6289 bool NeverNaN = true;
6290 if (!DemandedSrcElts.isZero())
6291 NeverNaN &=
6292 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
6293 if (NeverNaN && !DemandedSubElts.isZero())
6294 NeverNaN &=
6295 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
6296 return NeverNaN;
6297 }
6298 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
6299 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
6300 }
6301 case ISD::BUILD_VECTOR: {
6302 unsigned NumElts = Op.getNumOperands();
6303 for (unsigned I = 0; I != NumElts; ++I)
6304 if (DemandedElts[I] &&
6305 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6306 return false;
6307 return true;
6308 }
6309 case ISD::SPLAT_VECTOR:
6310 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
6311 case ISD::AssertNoFPClass: {
6312 FPClassTest NoFPClass =
6313 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6314 if ((NoFPClass & fcNan) == fcNan)
6315 return true;
6316 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6317 return true;
6318 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6319 }
6320 default:
6321 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6322 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6323 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6324 Depth);
6325 }
6326 break;
6327 }
6328
6329 FPClassTest NanMask = SNaN ? fcSNan : fcNan;
6330 KnownFPClass Known = computeKnownFPClass(Op, DemandedElts, NanMask, Depth);
6331 return Known.isKnownNever(NanMask);
6332}
6333
6335 assert(Op.getValueType().isFloatingPoint() &&
6336 "Floating point type expected");
6337
6338 // If the value is a constant, we can obviously see if it is a zero or not.
6340 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
6341}
6342
6344 EVT VT = Op.getValueType();
6345
6346 // Since the number of lanes in a scalable vector is unknown at compile time,
6347 // we track one bit which is implicitly broadcast to all lanes. This means
6348 // that all lanes in a scalable vector are considered demanded.
6349 APInt DemandedElts = VT.isFixedLengthVector()
6351 : APInt(1, 1);
6352
6353 return isKnownNeverZero(Op, DemandedElts, Depth);
6354}
6355
6357 unsigned Depth) const {
6358 if (Depth >= MaxRecursionDepth)
6359 return false; // Limit search depth.
6360
6361 EVT OpVT = Op.getValueType();
6362 unsigned BitWidth = OpVT.getScalarSizeInBits();
6363
6364 assert(!Op.getValueType().isFloatingPoint() &&
6365 "Floating point types unsupported - use isKnownNeverZeroFloat");
6366
6367 // If the value is a constant, we can obviously see if it is a zero or not.
6368 auto IsNeverZero = [BitWidth](const ConstantSDNode *C) {
6369 APInt V = C->getAPIntValue().zextOrTrunc(BitWidth);
6370 return !V.isZero();
6371 };
6372
6373 if (ISD::matchUnaryPredicate(Op, IsNeverZero))
6374 return true;
6375
6376 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6377 // some degree.
6378 switch (Op.getOpcode()) {
6379 default:
6380 break;
6381
6382 case ISD::BUILD_VECTOR:
6383 // Are all operands of a build vector constant non-zero?
6384 if (all_of(enumerate(Op->ops()), [&](auto P) {
6385 auto *C = dyn_cast<ConstantSDNode>(P.value());
6386 return !DemandedElts[P.index()] || (C && IsNeverZero(C));
6387 }))
6388 return true;
6389 break;
6390
6391 case ISD::SPLAT_VECTOR:
6392 // Is the operand of a splat vector a constant non-zero?
6393 if (auto *C = dyn_cast<ConstantSDNode>(Op->getOperand(0)))
6394 if (IsNeverZero(C))
6395 return true;
6396 break;
6397
6399 SDValue InVec = Op.getOperand(0);
6400 SDValue EltNo = Op.getOperand(1);
6401 EVT VecVT = InVec.getValueType();
6402
6403 // Skip scalable vectors or implicit extensions.
6404 if (VecVT.isScalableVector() ||
6405 OpVT.getScalarSizeInBits() != VecVT.getScalarSizeInBits())
6406 break;
6407
6408 // If we know the element index, just demand that vector element, else for
6409 // an unknown element index, ignore DemandedElts and demand them all.
6410 const unsigned NumSrcElts = VecVT.getVectorNumElements();
6411 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
6412 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
6413 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
6414 DemandedSrcElts =
6415 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
6416
6417 return isKnownNeverZero(InVec, DemandedSrcElts, Depth + 1);
6418 }
6419
6420 case ISD::OR:
6421 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6422 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6423
6424 case ISD::VSELECT:
6425 case ISD::SELECT:
6426 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6427 isKnownNeverZero(Op.getOperand(2), DemandedElts, Depth + 1);
6428
6429 case ISD::SHL: {
6430 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6431 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6432 KnownBits ValKnown =
6433 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6434 // 1 << X is never zero.
6435 if (ValKnown.One[0])
6436 return true;
6437 // If max shift cnt of known ones is non-zero, result is non-zero.
6438 APInt MaxCnt = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1)
6439 .getMaxValue();
6440 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6441 !ValKnown.One.shl(MaxCnt).isZero())
6442 return true;
6443 break;
6444 }
6445
6446 case ISD::VECTOR_SHUFFLE: {
6447 if (Op.getValueType().isScalableVector())
6448 return false;
6449
6450 unsigned NumElts = DemandedElts.getBitWidth();
6451
6452 // All demanded elements from LHS and RHS must be known non-zero.
6453 // Demanded elements with undef shuffle mask elements are unknown.
6454
6455 APInt DemandedLHS, DemandedRHS;
6456 auto *SVN = cast<ShuffleVectorSDNode>(Op);
6457 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
6458 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
6459 DemandedLHS, DemandedRHS))
6460 return false;
6461
6462 return (!DemandedLHS ||
6463 isKnownNeverZero(Op.getOperand(0), DemandedLHS, Depth + 1)) &&
6464 (!DemandedRHS ||
6465 isKnownNeverZero(Op.getOperand(1), DemandedRHS, Depth + 1));
6466 }
6467
6468 case ISD::UADDSAT:
6469 case ISD::UMAX:
6470 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6471 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6472
6473 case ISD::UMIN:
6474 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6475 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6476
6477 // For smin/smax: If either operand is known negative/positive
6478 // respectively we don't need the other to be known at all.
6479 case ISD::SMAX: {
6480 KnownBits Op1 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
6481 if (Op1.isStrictlyPositive())
6482 return true;
6483
6484 KnownBits Op0 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6485 if (Op0.isStrictlyPositive())
6486 return true;
6487
6488 if (Op1.isNonZero() && Op0.isNonZero())
6489 return true;
6490
6491 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6492 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6493 }
6494 case ISD::SMIN: {
6495 KnownBits Op1 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
6496 if (Op1.isNegative())
6497 return true;
6498
6499 KnownBits Op0 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6500 if (Op0.isNegative())
6501 return true;
6502
6503 if (Op1.isNonZero() && Op0.isNonZero())
6504 return true;
6505
6506 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6507 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6508 }
6509
6510 case ISD::ROTL:
6511 case ISD::ROTR:
6512 case ISD::BITREVERSE:
6513 case ISD::BSWAP:
6514 case ISD::CTPOP:
6515 case ISD::ABS:
6516 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6517
6518 case ISD::SRA:
6519 case ISD::SRL: {
6520 if (Op->getFlags().hasExact())
6521 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6522 KnownBits ValKnown =
6523 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6524 if (ValKnown.isNegative())
6525 return true;
6526 // If max shift cnt of known ones is non-zero, result is non-zero.
6527 APInt MaxCnt = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1)
6528 .getMaxValue();
6529 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6530 !ValKnown.One.lshr(MaxCnt).isZero())
6531 return true;
6532 break;
6533 }
6534 case ISD::UDIV:
6535 case ISD::SDIV:
6536 // div exact can only produce a zero if the dividend is zero.
6537 // TODO: For udiv this is also true if Op1 u<= Op0
6538 if (Op->getFlags().hasExact())
6539 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6540 break;
6541
6542 case ISD::ADD:
6543 if (Op->getFlags().hasNoUnsignedWrap())
6544 if (isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6545 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1))
6546 return true;
6547 // TODO: There are a lot more cases we can prove for add.
6548 break;
6549
6550 case ISD::SUB: {
6551 if (isNullConstant(Op.getOperand(0)))
6552 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1);
6553
6554 std::optional<bool> ne = KnownBits::ne(
6555 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1),
6556 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1));
6557 return ne && *ne;
6558 }
6559
6560 case ISD::MUL:
6561 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6562 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6563 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6564 return true;
6565 break;
6566
6567 case ISD::ZERO_EXTEND:
6568 case ISD::SIGN_EXTEND:
6569 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6570 case ISD::VSCALE: {
6572 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6573 ConstantRange CR =
6574 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6575 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6576 return true;
6577 break;
6578 }
6579 }
6580
6582}
6583
6585 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6586 return !C1->isNegative();
6587
6588 switch (Op.getOpcode()) {
6589 case ISD::FABS:
6590 case ISD::FEXP:
6591 case ISD::FEXP2:
6592 case ISD::FEXP10:
6593 return true;
6594 default:
6595 return false;
6596 }
6597
6598 llvm_unreachable("covered opcode switch");
6599}
6600
6602 assert(Use.getValueType().isFloatingPoint());
6603 const SDNode *User = Use.getUser();
6604 if (User->getFlags().hasNoSignedZeros())
6605 return true;
6606
6607 unsigned OperandNo = Use.getOperandNo();
6608 // Check if this use is insensitive to the sign of zero
6609 switch (User->getOpcode()) {
6610 case ISD::SETCC:
6611 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6612 case ISD::FABS:
6613 // fabs always produces +0.0.
6614 return true;
6615 case ISD::FCOPYSIGN:
6616 // copysign overwrites the sign bit of the first operand.
6617 return OperandNo == 0;
6618 case ISD::FADD:
6619 case ISD::FSUB: {
6620 // Arithmetic with non-zero constants fixes the uncertainty around the
6621 // sign bit.
6622 SDValue Other = User->getOperand(1 - OperandNo);
6624 }
6625 case ISD::FP_TO_SINT:
6626 case ISD::FP_TO_UINT:
6627 // fp-to-int conversions normalize signed zeros.
6628 return true;
6629 default:
6630 return false;
6631 }
6632}
6633
6635 if (Op->getFlags().hasNoSignedZeros())
6636 return true;
6637 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6638 // regression. Ideally, this should be implemented as a demanded-bits
6639 // optimization that stems from the users.
6640 if (Op->use_size() > 2)
6641 return false;
6642 return all_of(Op->uses(),
6643 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6644}
6645
6647 // Check the obvious case.
6648 if (A == B) return true;
6649
6650 // For negative and positive zero.
6653 if (CA->isZero() && CB->isZero()) return true;
6654
6655 // Otherwise they may not be equal.
6656 return false;
6657}
6658
6659// Only bits set in Mask must be negated, other bits may be arbitrary.
6661 if (isBitwiseNot(V, AllowUndefs))
6662 return V.getOperand(0);
6663
6664 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6665 // bits in the non-extended part.
6666 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6667 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6668 return SDValue();
6669 SDValue ExtArg = V.getOperand(0);
6670 if (ExtArg.getScalarValueSizeInBits() >=
6671 MaskC->getAPIntValue().getActiveBits() &&
6672 isBitwiseNot(ExtArg, AllowUndefs) &&
6673 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6674 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6675 return ExtArg.getOperand(0).getOperand(0);
6676 return SDValue();
6677}
6678
6680 // Match masked merge pattern (X & ~M) op (Y & M)
6681 // Including degenerate case (X & ~M) op M
6682 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6683 SDValue Other) {
6684 if (SDValue NotOperand =
6685 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6686 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6687 NotOperand->getOpcode() == ISD::TRUNCATE)
6688 NotOperand = NotOperand->getOperand(0);
6689
6690 if (Other == NotOperand)
6691 return true;
6692 if (Other->getOpcode() == ISD::AND)
6693 return NotOperand == Other->getOperand(0) ||
6694 NotOperand == Other->getOperand(1);
6695 }
6696 return false;
6697 };
6698
6699 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6700 A = A->getOperand(0);
6701
6702 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6703 B = B->getOperand(0);
6704
6705 if (A->getOpcode() == ISD::AND)
6706 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6707 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6708 return false;
6709}
6710
6711// FIXME: unify with llvm::haveNoCommonBitsSet.
6713 assert(A.getValueType() == B.getValueType() &&
6714 "Values must have the same type");
6717 return true;
6720}
6721
6722static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6723 SelectionDAG &DAG) {
6724 if (cast<ConstantSDNode>(Step)->isZero())
6725 return DAG.getConstant(0, DL, VT);
6726
6727 return SDValue();
6728}
6729
6732 SelectionDAG &DAG) {
6733 int NumOps = Ops.size();
6734 assert(NumOps != 0 && "Can't build an empty vector!");
6735 assert(!VT.isScalableVector() &&
6736 "BUILD_VECTOR cannot be used with scalable types");
6737 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6738 "Incorrect element count in BUILD_VECTOR!");
6739
6740 // BUILD_VECTOR of UNDEFs is UNDEF.
6741 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6742 return DAG.getUNDEF(VT);
6743
6744 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6745 SDValue IdentitySrc;
6746 bool IsIdentity = true;
6747 for (int i = 0; i != NumOps; ++i) {
6749 Ops[i].getOperand(0).getValueType() != VT ||
6750 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6751 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6752 Ops[i].getConstantOperandAPInt(1) != i) {
6753 IsIdentity = false;
6754 break;
6755 }
6756 IdentitySrc = Ops[i].getOperand(0);
6757 }
6758 if (IsIdentity)
6759 return IdentitySrc;
6760
6761 return SDValue();
6762}
6763
6764/// Try to simplify vector concatenation to an input value, undef, or build
6765/// vector.
6768 SelectionDAG &DAG) {
6769 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6771 [Ops](SDValue Op) {
6772 return Ops[0].getValueType() == Op.getValueType();
6773 }) &&
6774 "Concatenation of vectors with inconsistent value types!");
6775 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6776 VT.getVectorElementCount() &&
6777 "Incorrect element count in vector concatenation!");
6778
6779 if (Ops.size() == 1)
6780 return Ops[0];
6781
6782 // Concat of UNDEFs is UNDEF.
6783 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6784 return DAG.getUNDEF(VT);
6785
6786 // Scan the operands and look for extract operations from a single source
6787 // that correspond to insertion at the same location via this concatenation:
6788 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6789 SDValue IdentitySrc;
6790 bool IsIdentity = true;
6791 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6792 SDValue Op = Ops[i];
6793 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6794 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6795 Op.getOperand(0).getValueType() != VT ||
6796 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6797 Op.getConstantOperandVal(1) != IdentityIndex) {
6798 IsIdentity = false;
6799 break;
6800 }
6801 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6802 "Unexpected identity source vector for concat of extracts");
6803 IdentitySrc = Op.getOperand(0);
6804 }
6805 if (IsIdentity) {
6806 assert(IdentitySrc && "Failed to set source vector of extracts");
6807 return IdentitySrc;
6808 }
6809
6810 // The code below this point is only designed to work for fixed width
6811 // vectors, so we bail out for now.
6812 if (VT.isScalableVector())
6813 return SDValue();
6814
6815 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6816 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6817 // BUILD_VECTOR.
6818 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6819 EVT SVT = VT.getScalarType();
6821 for (SDValue Op : Ops) {
6822 EVT OpVT = Op.getValueType();
6823 if (Op.isUndef())
6824 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6825 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6826 Elts.append(Op->op_begin(), Op->op_end());
6827 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6828 OpVT.getVectorNumElements() == 1 &&
6829 isNullConstant(Op.getOperand(2)))
6830 Elts.push_back(Op.getOperand(1));
6831 else
6832 return SDValue();
6833 }
6834
6835 // BUILD_VECTOR requires all inputs to be of the same type, find the
6836 // maximum type and extend them all.
6837 for (SDValue Op : Elts)
6838 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6839
6840 if (SVT.bitsGT(VT.getScalarType())) {
6841 for (SDValue &Op : Elts) {
6842 if (Op.isUndef())
6843 Op = DAG.getUNDEF(SVT);
6844 else
6845 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6846 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6847 : DAG.getSExtOrTrunc(Op, DL, SVT);
6848 }
6849 }
6850
6851 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6852 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6853 return V;
6854}
6855
6856/// Gets or creates the specified node.
6857SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6858 SDVTList VTs = getVTList(VT);
6860 AddNodeIDNode(ID, Opcode, VTs, {});
6861 void *IP = nullptr;
6862 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6863 return SDValue(E, 0);
6864
6865 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6866 CSEMap.InsertNode(N, IP);
6867
6868 InsertNode(N);
6869 SDValue V = SDValue(N, 0);
6870 NewSDValueDbgMsg(V, "Creating new node: ", this);
6871 return V;
6872}
6873
6874SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6875 SDValue N1) {
6876 SDNodeFlags Flags;
6877 if (Inserter)
6878 Flags = Inserter->getFlags();
6879 return getNode(Opcode, DL, VT, N1, Flags);
6880}
6881
6882SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6883 SDValue N1, const SDNodeFlags Flags) {
6884 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6885
6886 // Constant fold unary operations with a vector integer or float operand.
6887 switch (Opcode) {
6888 default:
6889 // FIXME: Entirely reasonable to perform folding of other unary
6890 // operations here as the need arises.
6891 break;
6892 case ISD::FNEG:
6893 case ISD::FABS:
6894 case ISD::FCEIL:
6895 case ISD::FTRUNC:
6896 case ISD::FFLOOR:
6897 case ISD::FP_EXTEND:
6898 case ISD::FP_TO_SINT:
6899 case ISD::FP_TO_UINT:
6900 case ISD::FP_TO_FP16:
6901 case ISD::FP_TO_BF16:
6902 case ISD::TRUNCATE:
6903 case ISD::ANY_EXTEND:
6904 case ISD::ZERO_EXTEND:
6905 case ISD::SIGN_EXTEND:
6906 case ISD::UINT_TO_FP:
6907 case ISD::SINT_TO_FP:
6908 case ISD::FP16_TO_FP:
6909 case ISD::BF16_TO_FP:
6910 case ISD::BITCAST:
6911 case ISD::ABS:
6912 case ISD::BITREVERSE:
6913 case ISD::BSWAP:
6914 case ISD::CTLZ:
6916 case ISD::CTTZ:
6918 case ISD::CTPOP:
6919 case ISD::CTLS:
6920 case ISD::STEP_VECTOR: {
6921 SDValue Ops = {N1};
6922 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6923 return Fold;
6924 }
6925 }
6926
6927 unsigned OpOpcode = N1.getNode()->getOpcode();
6928 switch (Opcode) {
6929 case ISD::STEP_VECTOR:
6930 assert(VT.isScalableVector() &&
6931 "STEP_VECTOR can only be used with scalable types");
6932 assert(OpOpcode == ISD::TargetConstant &&
6933 VT.getVectorElementType() == N1.getValueType() &&
6934 "Unexpected step operand");
6935 break;
6936 case ISD::FREEZE:
6937 assert(VT == N1.getValueType() && "Unexpected VT!");
6938 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6939 return N1;
6940 break;
6941 case ISD::TokenFactor:
6942 case ISD::MERGE_VALUES:
6944 return N1; // Factor, merge or concat of one node? No need.
6945 case ISD::BUILD_VECTOR: {
6946 // Attempt to simplify BUILD_VECTOR.
6947 SDValue Ops[] = {N1};
6948 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6949 return V;
6950 break;
6951 }
6952 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6953 case ISD::FP_EXTEND:
6955 "Invalid FP cast!");
6956 if (N1.getValueType() == VT) return N1; // noop conversion.
6957 assert((!VT.isVector() || VT.getVectorElementCount() ==
6959 "Vector element count mismatch!");
6960 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6961 if (N1.isUndef())
6962 return getUNDEF(VT);
6963 break;
6964 case ISD::FP_TO_SINT:
6965 case ISD::FP_TO_UINT:
6966 if (N1.isUndef())
6967 return getUNDEF(VT);
6968 break;
6969 case ISD::SINT_TO_FP:
6970 case ISD::UINT_TO_FP:
6971 // [us]itofp(undef) = 0, because the result value is bounded.
6972 if (N1.isUndef())
6973 return getConstantFP(0.0, DL, VT);
6974 break;
6975 case ISD::SIGN_EXTEND:
6976 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6977 "Invalid SIGN_EXTEND!");
6978 assert(VT.isVector() == N1.getValueType().isVector() &&
6979 "SIGN_EXTEND result type type should be vector iff the operand "
6980 "type is vector!");
6981 if (N1.getValueType() == VT) return N1; // noop extension
6982 assert((!VT.isVector() || VT.getVectorElementCount() ==
6984 "Vector element count mismatch!");
6985 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6986 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6987 SDNodeFlags Flags;
6988 if (OpOpcode == ISD::ZERO_EXTEND)
6989 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6990 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6991 transferDbgValues(N1, NewVal);
6992 return NewVal;
6993 }
6994
6995 if (OpOpcode == ISD::POISON)
6996 return getPOISON(VT);
6997
6998 if (N1.isUndef())
6999 // sext(undef) = 0, because the top bits will all be the same.
7000 return getConstant(0, DL, VT);
7001
7002 // Skip unnecessary sext_inreg pattern:
7003 // (sext (trunc x)) -> x iff the upper bits are all signbits.
7004 if (OpOpcode == ISD::TRUNCATE) {
7005 SDValue OpOp = N1.getOperand(0);
7006 if (OpOp.getValueType() == VT) {
7007 unsigned NumSignExtBits =
7009 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
7010 transferDbgValues(N1, OpOp);
7011 return OpOp;
7012 }
7013 }
7014 }
7015 break;
7016 case ISD::ZERO_EXTEND:
7017 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7018 "Invalid ZERO_EXTEND!");
7019 assert(VT.isVector() == N1.getValueType().isVector() &&
7020 "ZERO_EXTEND result type type should be vector iff the operand "
7021 "type is vector!");
7022 if (N1.getValueType() == VT) return N1; // noop extension
7023 assert((!VT.isVector() || VT.getVectorElementCount() ==
7025 "Vector element count mismatch!");
7026 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
7027 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
7028 SDNodeFlags Flags;
7029 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7030 SDValue NewVal =
7031 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
7032 transferDbgValues(N1, NewVal);
7033 return NewVal;
7034 }
7035
7036 if (OpOpcode == ISD::POISON)
7037 return getPOISON(VT);
7038
7039 if (N1.isUndef())
7040 // zext(undef) = 0, because the top bits will be zero.
7041 return getConstant(0, DL, VT);
7042
7043 // Skip unnecessary zext_inreg pattern:
7044 // (zext (trunc x)) -> x iff the upper bits are known zero.
7045 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
7046 // use to recognise zext_inreg patterns.
7047 if (OpOpcode == ISD::TRUNCATE) {
7048 SDValue OpOp = N1.getOperand(0);
7049 if (OpOp.getValueType() == VT) {
7050 if (OpOp.getOpcode() != ISD::AND) {
7053 if (MaskedValueIsZero(OpOp, HiBits)) {
7054 transferDbgValues(N1, OpOp);
7055 return OpOp;
7056 }
7057 }
7058 }
7059 }
7060 break;
7061 case ISD::ANY_EXTEND:
7062 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7063 "Invalid ANY_EXTEND!");
7064 assert(VT.isVector() == N1.getValueType().isVector() &&
7065 "ANY_EXTEND result type type should be vector iff the operand "
7066 "type is vector!");
7067 if (N1.getValueType() == VT) return N1; // noop extension
7068 assert((!VT.isVector() || VT.getVectorElementCount() ==
7070 "Vector element count mismatch!");
7071 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
7072
7073 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
7074 OpOpcode == ISD::ANY_EXTEND) {
7075 SDNodeFlags Flags;
7076 if (OpOpcode == ISD::ZERO_EXTEND)
7077 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7078 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
7079 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
7080 }
7081 if (N1.isUndef())
7082 return getUNDEF(VT);
7083
7084 // (ext (trunc x)) -> x
7085 if (OpOpcode == ISD::TRUNCATE) {
7086 SDValue OpOp = N1.getOperand(0);
7087 if (OpOp.getValueType() == VT) {
7088 transferDbgValues(N1, OpOp);
7089 return OpOp;
7090 }
7091 }
7092 break;
7093 case ISD::TRUNCATE:
7094 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7095 "Invalid TRUNCATE!");
7096 assert(VT.isVector() == N1.getValueType().isVector() &&
7097 "TRUNCATE result type type should be vector iff the operand "
7098 "type is vector!");
7099 if (N1.getValueType() == VT) return N1; // noop truncate
7100 assert((!VT.isVector() || VT.getVectorElementCount() ==
7102 "Vector element count mismatch!");
7103 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
7104 if (OpOpcode == ISD::TRUNCATE)
7105 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
7106 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
7107 OpOpcode == ISD::ANY_EXTEND) {
7108 // If the source is smaller than the dest, we still need an extend.
7110 VT.getScalarType())) {
7111 SDNodeFlags Flags;
7112 if (OpOpcode == ISD::ZERO_EXTEND)
7113 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7114 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
7115 }
7116 if (N1.getOperand(0).getValueType().bitsGT(VT))
7117 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
7118 return N1.getOperand(0);
7119 }
7120 if (N1.isUndef())
7121 return getUNDEF(VT);
7122 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
7123 return getVScale(DL, VT,
7125 break;
7129 assert(VT.isVector() && "This DAG node is restricted to vector types.");
7130 assert(N1.getValueType().bitsLE(VT) &&
7131 "The input must be the same size or smaller than the result.");
7134 "The destination vector type must have fewer lanes than the input.");
7135 break;
7136 case ISD::ABS:
7137 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
7138 if (N1.isUndef())
7139 return getConstant(0, DL, VT);
7140 break;
7141 case ISD::BSWAP:
7142 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
7143 assert((VT.getScalarSizeInBits() % 16 == 0) &&
7144 "BSWAP types must be a multiple of 16 bits!");
7145 if (N1.isUndef())
7146 return getUNDEF(VT);
7147 // bswap(bswap(X)) -> X.
7148 if (OpOpcode == ISD::BSWAP)
7149 return N1.getOperand(0);
7150 break;
7151 case ISD::BITREVERSE:
7152 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
7153 if (N1.isUndef())
7154 return getUNDEF(VT);
7155 break;
7156 case ISD::BITCAST:
7158 "Cannot BITCAST between types of different sizes!");
7159 if (VT == N1.getValueType()) return N1; // noop conversion.
7160 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
7161 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
7162 if (N1.isUndef())
7163 return getUNDEF(VT);
7164 break;
7166 assert(VT.isVector() && !N1.getValueType().isVector() &&
7167 (VT.getVectorElementType() == N1.getValueType() ||
7169 N1.getValueType().isInteger() &&
7171 "Illegal SCALAR_TO_VECTOR node!");
7172 if (N1.isUndef())
7173 return getUNDEF(VT);
7174 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
7175 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
7177 N1.getConstantOperandVal(1) == 0 &&
7178 N1.getOperand(0).getValueType() == VT)
7179 return N1.getOperand(0);
7180 break;
7181 case ISD::FNEG:
7182 // Negation of an unknown bag of bits is still completely undefined.
7183 if (N1.isUndef())
7184 return getUNDEF(VT);
7185
7186 if (OpOpcode == ISD::FNEG) // --X -> X
7187 return N1.getOperand(0);
7188 break;
7189 case ISD::FABS:
7190 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
7191 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
7192 break;
7193 case ISD::VSCALE:
7194 assert(VT == N1.getValueType() && "Unexpected VT!");
7195 break;
7196 case ISD::CTPOP:
7197 if (N1.getValueType().getScalarType() == MVT::i1)
7198 return N1;
7199 break;
7200 case ISD::CTLZ:
7201 case ISD::CTTZ:
7202 if (N1.getValueType().getScalarType() == MVT::i1)
7203 return getNOT(DL, N1, N1.getValueType());
7204 break;
7205 case ISD::CTLS:
7206 if (N1.getValueType().getScalarType() == MVT::i1)
7207 return getConstant(0, DL, VT);
7208 break;
7209 case ISD::VECREDUCE_ADD:
7210 if (N1.getValueType().getScalarType() == MVT::i1)
7211 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
7212 break;
7215 if (N1.getValueType().getScalarType() == MVT::i1)
7216 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
7217 break;
7220 if (N1.getValueType().getScalarType() == MVT::i1)
7221 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
7222 break;
7223 case ISD::SPLAT_VECTOR:
7224 assert(VT.isVector() && "Wrong return type!");
7225 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
7226 // that for now.
7228 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
7230 N1.getValueType().isInteger() &&
7232 "Wrong operand type!");
7233 break;
7234 }
7235
7236 SDNode *N;
7237 SDVTList VTs = getVTList(VT);
7238 SDValue Ops[] = {N1};
7239 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
7241 AddNodeIDNode(ID, Opcode, VTs, Ops);
7242 void *IP = nullptr;
7243 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7244 E->intersectFlagsWith(Flags);
7245 return SDValue(E, 0);
7246 }
7247
7248 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7249 N->setFlags(Flags);
7250 createOperands(N, Ops);
7251 CSEMap.InsertNode(N, IP);
7252 } else {
7253 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7254 createOperands(N, Ops);
7255 }
7256
7257 InsertNode(N);
7258 SDValue V = SDValue(N, 0);
7259 NewSDValueDbgMsg(V, "Creating new node: ", this);
7260 return V;
7261}
7262
7263static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
7264 const APInt &C2) {
7265 switch (Opcode) {
7266 case ISD::ADD: return C1 + C2;
7267 case ISD::SUB: return C1 - C2;
7268 case ISD::MUL: return C1 * C2;
7269 case ISD::AND: return C1 & C2;
7270 case ISD::OR: return C1 | C2;
7271 case ISD::XOR: return C1 ^ C2;
7272 case ISD::SHL: return C1 << C2;
7273 case ISD::SRL: return C1.lshr(C2);
7274 case ISD::SRA: return C1.ashr(C2);
7275 case ISD::ROTL: return C1.rotl(C2);
7276 case ISD::ROTR: return C1.rotr(C2);
7277 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
7278 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
7279 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
7280 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
7281 case ISD::SADDSAT: return C1.sadd_sat(C2);
7282 case ISD::UADDSAT: return C1.uadd_sat(C2);
7283 case ISD::SSUBSAT: return C1.ssub_sat(C2);
7284 case ISD::USUBSAT: return C1.usub_sat(C2);
7285 case ISD::SSHLSAT: return C1.sshl_sat(C2);
7286 case ISD::USHLSAT: return C1.ushl_sat(C2);
7287 case ISD::UDIV:
7288 if (!C2.getBoolValue())
7289 break;
7290 return C1.udiv(C2);
7291 case ISD::UREM:
7292 if (!C2.getBoolValue())
7293 break;
7294 return C1.urem(C2);
7295 case ISD::SDIV:
7296 if (!C2.getBoolValue())
7297 break;
7298 return C1.sdiv(C2);
7299 case ISD::SREM:
7300 if (!C2.getBoolValue())
7301 break;
7302 return C1.srem(C2);
7303 case ISD::AVGFLOORS:
7304 return APIntOps::avgFloorS(C1, C2);
7305 case ISD::AVGFLOORU:
7306 return APIntOps::avgFloorU(C1, C2);
7307 case ISD::AVGCEILS:
7308 return APIntOps::avgCeilS(C1, C2);
7309 case ISD::AVGCEILU:
7310 return APIntOps::avgCeilU(C1, C2);
7311 case ISD::ABDS:
7312 return APIntOps::abds(C1, C2);
7313 case ISD::ABDU:
7314 return APIntOps::abdu(C1, C2);
7315 case ISD::MULHS:
7316 return APIntOps::mulhs(C1, C2);
7317 case ISD::MULHU:
7318 return APIntOps::mulhu(C1, C2);
7319 case ISD::CLMUL:
7320 return APIntOps::clmul(C1, C2);
7321 case ISD::CLMULR:
7322 return APIntOps::clmulr(C1, C2);
7323 case ISD::CLMULH:
7324 return APIntOps::clmulh(C1, C2);
7325 }
7326 return std::nullopt;
7327}
7328// Handle constant folding with UNDEF.
7329// TODO: Handle more cases.
7330static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
7331 bool IsUndef1, const APInt &C2,
7332 bool IsUndef2) {
7333 if (!(IsUndef1 || IsUndef2))
7334 return FoldValue(Opcode, C1, C2);
7335
7336 // Fold and(x, undef) -> 0
7337 // Fold mul(x, undef) -> 0
7338 if (Opcode == ISD::AND || Opcode == ISD::MUL)
7339 return APInt::getZero(C1.getBitWidth());
7340
7341 return std::nullopt;
7342}
7343
7345 const GlobalAddressSDNode *GA,
7346 const SDNode *N2) {
7347 if (GA->getOpcode() != ISD::GlobalAddress)
7348 return SDValue();
7349 if (!TLI->isOffsetFoldingLegal(GA))
7350 return SDValue();
7351 auto *C2 = dyn_cast<ConstantSDNode>(N2);
7352 if (!C2)
7353 return SDValue();
7354 int64_t Offset = C2->getSExtValue();
7355 switch (Opcode) {
7356 case ISD::ADD:
7357 case ISD::PTRADD:
7358 break;
7359 case ISD::SUB: Offset = -uint64_t(Offset); break;
7360 default: return SDValue();
7361 }
7362 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
7363 GA->getOffset() + uint64_t(Offset));
7364}
7365
7367 switch (Opcode) {
7368 case ISD::SDIV:
7369 case ISD::UDIV:
7370 case ISD::SREM:
7371 case ISD::UREM: {
7372 // If a divisor is zero/undef or any element of a divisor vector is
7373 // zero/undef, the whole op is undef.
7374 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
7375 SDValue Divisor = Ops[1];
7376 if (Divisor.isUndef() || isNullConstant(Divisor))
7377 return true;
7378
7379 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
7380 llvm::any_of(Divisor->op_values(),
7381 [](SDValue V) { return V.isUndef() ||
7382 isNullConstant(V); });
7383 // TODO: Handle signed overflow.
7384 }
7385 // TODO: Handle oversized shifts.
7386 default:
7387 return false;
7388 }
7389}
7390
7393 SDNodeFlags Flags) {
7394 // If the opcode is a target-specific ISD node, there's nothing we can
7395 // do here and the operand rules may not line up with the below, so
7396 // bail early.
7397 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
7398 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
7399 // foldCONCAT_VECTORS in getNode before this is called.
7400 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
7401 return SDValue();
7402
7403 unsigned NumOps = Ops.size();
7404 if (NumOps == 0)
7405 return SDValue();
7406
7407 if (isUndef(Opcode, Ops))
7408 return getUNDEF(VT);
7409
7410 // Handle unary special cases.
7411 if (NumOps == 1) {
7412 SDValue N1 = Ops[0];
7413
7414 // Constant fold unary operations with an integer constant operand. Even
7415 // opaque constant will be folded, because the folding of unary operations
7416 // doesn't create new constants with different values. Nevertheless, the
7417 // opaque flag is preserved during folding to prevent future folding with
7418 // other constants.
7419 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
7420 const APInt &Val = C->getAPIntValue();
7421 switch (Opcode) {
7422 case ISD::SIGN_EXTEND:
7423 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7424 C->isTargetOpcode(), C->isOpaque());
7425 case ISD::TRUNCATE:
7426 if (C->isOpaque())
7427 break;
7428 [[fallthrough]];
7429 case ISD::ZERO_EXTEND:
7430 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7431 C->isTargetOpcode(), C->isOpaque());
7432 case ISD::ANY_EXTEND:
7433 // Some targets like RISCV prefer to sign extend some types.
7434 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7435 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7436 C->isTargetOpcode(), C->isOpaque());
7437 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7438 C->isTargetOpcode(), C->isOpaque());
7439 case ISD::ABS:
7440 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7441 C->isOpaque());
7442 case ISD::BITREVERSE:
7443 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7444 C->isOpaque());
7445 case ISD::BSWAP:
7446 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7447 C->isOpaque());
7448 case ISD::CTPOP:
7449 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7450 C->isOpaque());
7451 case ISD::CTLZ:
7453 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7454 C->isOpaque());
7455 case ISD::CTTZ:
7457 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7458 C->isOpaque());
7459 case ISD::CTLS:
7460 // CTLS returns the number of extra sign bits so subtract one.
7461 return getConstant(Val.getNumSignBits() - 1, DL, VT,
7462 C->isTargetOpcode(), C->isOpaque());
7463 case ISD::UINT_TO_FP:
7464 case ISD::SINT_TO_FP: {
7466 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7468 return getConstantFP(FPV, DL, VT);
7469 }
7470 case ISD::FP16_TO_FP:
7471 case ISD::BF16_TO_FP: {
7472 bool Ignored;
7473 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7474 : APFloat::BFloat(),
7475 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7476
7477 // This can return overflow, underflow, or inexact; we don't care.
7478 // FIXME need to be more flexible about rounding mode.
7480 &Ignored);
7481 return getConstantFP(FPV, DL, VT);
7482 }
7483 case ISD::STEP_VECTOR:
7484 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7485 return V;
7486 break;
7487 case ISD::BITCAST:
7488 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7489 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7490 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7491 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7492 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7493 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7494 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7495 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7496 break;
7497 }
7498 }
7499
7500 // Constant fold unary operations with a floating point constant operand.
7501 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7502 APFloat V = C->getValueAPF(); // make copy
7503 switch (Opcode) {
7504 case ISD::FNEG:
7505 V.changeSign();
7506 return getConstantFP(V, DL, VT);
7507 case ISD::FABS:
7508 V.clearSign();
7509 return getConstantFP(V, DL, VT);
7510 case ISD::FCEIL: {
7511 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7513 return getConstantFP(V, DL, VT);
7514 return SDValue();
7515 }
7516 case ISD::FTRUNC: {
7517 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7519 return getConstantFP(V, DL, VT);
7520 return SDValue();
7521 }
7522 case ISD::FFLOOR: {
7523 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7525 return getConstantFP(V, DL, VT);
7526 return SDValue();
7527 }
7528 case ISD::FP_EXTEND: {
7529 bool ignored;
7530 // This can return overflow, underflow, or inexact; we don't care.
7531 // FIXME need to be more flexible about rounding mode.
7532 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7533 &ignored);
7534 return getConstantFP(V, DL, VT);
7535 }
7536 case ISD::FP_TO_SINT:
7537 case ISD::FP_TO_UINT: {
7538 bool ignored;
7539 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7540 // FIXME need to be more flexible about rounding mode.
7542 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7543 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7544 break;
7545 return getConstant(IntVal, DL, VT);
7546 }
7547 case ISD::FP_TO_FP16:
7548 case ISD::FP_TO_BF16: {
7549 bool Ignored;
7550 // This can return overflow, underflow, or inexact; we don't care.
7551 // FIXME need to be more flexible about rounding mode.
7552 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7553 : APFloat::BFloat(),
7555 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7556 }
7557 case ISD::BITCAST:
7558 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7559 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7560 VT);
7561 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7562 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7563 VT);
7564 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7565 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7566 VT);
7567 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7568 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7569 break;
7570 }
7571 }
7572
7573 // Early-out if we failed to constant fold a bitcast.
7574 if (Opcode == ISD::BITCAST)
7575 return SDValue();
7576 }
7577
7578 // Handle binops special cases.
7579 if (NumOps == 2) {
7580 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7581 return CFP;
7582
7583 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7584 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7585 if (C1->isOpaque() || C2->isOpaque())
7586 return SDValue();
7587
7588 std::optional<APInt> FoldAttempt =
7589 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7590 if (!FoldAttempt)
7591 return SDValue();
7592
7593 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7594 assert((!Folded || !VT.isVector()) &&
7595 "Can't fold vectors ops with scalar operands");
7596 return Folded;
7597 }
7598 }
7599
7600 // fold (add Sym, c) -> Sym+c
7602 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7603 if (TLI->isCommutativeBinOp(Opcode))
7605 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7606
7607 // fold (sext_in_reg c1) -> c2
7608 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7609 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7610
7611 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7612 unsigned FromBits = EVT.getScalarSizeInBits();
7613 Val <<= Val.getBitWidth() - FromBits;
7614 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7615 return getConstant(Val, DL, ConstantVT);
7616 };
7617
7618 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7619 const APInt &Val = C1->getAPIntValue();
7620 return SignExtendInReg(Val, VT);
7621 }
7622
7624 SmallVector<SDValue, 8> ScalarOps;
7625 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7626 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7627 SDValue Op = Ops[0].getOperand(I);
7628 if (Op.isUndef()) {
7629 ScalarOps.push_back(getUNDEF(OpVT));
7630 continue;
7631 }
7632 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7633 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7634 }
7635 return getBuildVector(VT, DL, ScalarOps);
7636 }
7637
7638 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7639 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7640 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7641 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7642 Ops[0].getOperand(0).getValueType()));
7643 }
7644 }
7645
7646 // Handle fshl/fshr special cases.
7647 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7648 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7649 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7650 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7651
7652 if (C1 && C2 && C3) {
7653 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7654 return SDValue();
7655 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7656 &V3 = C3->getAPIntValue();
7657
7658 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7659 : APIntOps::fshr(V1, V2, V3);
7660 return getConstant(FoldedVal, DL, VT);
7661 }
7662 }
7663
7664 // Handle fma/fmad special cases.
7665 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7666 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7667 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7668 Ops[2].getValueType() == VT && "FMA types must match!");
7672 if (C1 && C2 && C3) {
7673 APFloat V1 = C1->getValueAPF();
7674 const APFloat &V2 = C2->getValueAPF();
7675 const APFloat &V3 = C3->getValueAPF();
7676 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7679 } else
7681 return getConstantFP(V1, DL, VT);
7682 }
7683 }
7684
7685 // This is for vector folding only from here on.
7686 if (!VT.isVector())
7687 return SDValue();
7688
7689 ElementCount NumElts = VT.getVectorElementCount();
7690
7691 // See if we can fold through any bitcasted integer ops.
7692 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7693 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7694 (Ops[0].getOpcode() == ISD::BITCAST ||
7695 Ops[1].getOpcode() == ISD::BITCAST)) {
7698 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7699 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7700 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7701 N2.getValueType().isInteger()) {
7702 bool IsLE = getDataLayout().isLittleEndian();
7703 unsigned EltBits = VT.getScalarSizeInBits();
7704 SmallVector<APInt> RawBits1, RawBits2;
7705 BitVector UndefElts1, UndefElts2;
7706 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7707 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7708 SmallVector<APInt> RawBits;
7709 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7710 std::optional<APInt> Fold = FoldValueWithUndef(
7711 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7712 if (!Fold)
7713 break;
7714 RawBits.push_back(*Fold);
7715 }
7716 if (RawBits.size() == NumElts.getFixedValue()) {
7717 // We have constant folded, but we might need to cast this again back
7718 // to the original (possibly legalized) type.
7719 EVT BVVT, BVEltVT;
7720 if (N1.getValueType() == VT) {
7721 BVVT = N1.getValueType();
7722 BVEltVT = BV1->getOperand(0).getValueType();
7723 } else {
7724 BVVT = N2.getValueType();
7725 BVEltVT = BV2->getOperand(0).getValueType();
7726 }
7727 unsigned BVEltBits = BVEltVT.getSizeInBits();
7728 SmallVector<APInt> DstBits;
7729 BitVector DstUndefs;
7731 DstBits, RawBits, DstUndefs,
7732 BitVector(RawBits.size(), false));
7733 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7734 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7735 if (DstUndefs[I])
7736 continue;
7737 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7738 }
7739 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7740 }
7741 }
7742 }
7743 }
7744
7745 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7746 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7747 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7748 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7749 APInt RHSVal;
7750 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7751 APInt NewStep = Opcode == ISD::MUL
7752 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7753 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7754 return getStepVector(DL, VT, NewStep);
7755 }
7756 }
7757
7758 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7759 return !Op.getValueType().isVector() ||
7760 Op.getValueType().getVectorElementCount() == NumElts;
7761 };
7762
7763 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7764 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7765 Op.getOpcode() == ISD::BUILD_VECTOR ||
7766 Op.getOpcode() == ISD::SPLAT_VECTOR;
7767 };
7768
7769 // All operands must be vector types with the same number of elements as
7770 // the result type and must be either UNDEF or a build/splat vector
7771 // or UNDEF scalars.
7772 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7773 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7774 return SDValue();
7775
7776 // If we are comparing vectors, then the result needs to be a i1 boolean that
7777 // is then extended back to the legal result type depending on how booleans
7778 // are represented.
7779 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7780 ISD::NodeType ExtendCode =
7781 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7782 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7784
7785 // Find legal integer scalar type for constant promotion and
7786 // ensure that its scalar size is at least as large as source.
7787 EVT LegalSVT = VT.getScalarType();
7788 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7789 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7790 if (LegalSVT.bitsLT(VT.getScalarType()))
7791 return SDValue();
7792 }
7793
7794 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7795 // only have one operand to check. For fixed-length vector types we may have
7796 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7797 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7798
7799 // Constant fold each scalar lane separately.
7800 SmallVector<SDValue, 4> ScalarResults;
7801 for (unsigned I = 0; I != NumVectorElts; I++) {
7802 SmallVector<SDValue, 4> ScalarOps;
7803 for (SDValue Op : Ops) {
7804 EVT InSVT = Op.getValueType().getScalarType();
7805 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7806 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7807 if (Op.isUndef())
7808 ScalarOps.push_back(getUNDEF(InSVT));
7809 else
7810 ScalarOps.push_back(Op);
7811 continue;
7812 }
7813
7814 SDValue ScalarOp =
7815 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7816 EVT ScalarVT = ScalarOp.getValueType();
7817
7818 // Build vector (integer) scalar operands may need implicit
7819 // truncation - do this before constant folding.
7820 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7821 // Don't create illegally-typed nodes unless they're constants or undef
7822 // - if we fail to constant fold we can't guarantee the (dead) nodes
7823 // we're creating will be cleaned up before being visited for
7824 // legalization.
7825 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7826 !isa<ConstantSDNode>(ScalarOp) &&
7827 TLI->getTypeAction(*getContext(), InSVT) !=
7829 return SDValue();
7830 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7831 }
7832
7833 ScalarOps.push_back(ScalarOp);
7834 }
7835
7836 // Constant fold the scalar operands.
7837 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7838
7839 // Scalar folding only succeeded if the result is a constant or UNDEF.
7840 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7841 ScalarResult.getOpcode() != ISD::ConstantFP)
7842 return SDValue();
7843
7844 // Legalize the (integer) scalar constant if necessary. We only do
7845 // this once we know the folding succeeded, since otherwise we would
7846 // get a node with illegal type which has a user.
7847 if (LegalSVT != SVT)
7848 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7849
7850 ScalarResults.push_back(ScalarResult);
7851 }
7852
7853 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7854 : getBuildVector(VT, DL, ScalarResults);
7855 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7856 return V;
7857}
7858
7861 // TODO: Add support for unary/ternary fp opcodes.
7862 if (Ops.size() != 2)
7863 return SDValue();
7864
7865 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7866 // should. That will require dealing with a potentially non-default
7867 // rounding mode, checking the "opStatus" return value from the APFloat
7868 // math calculations, and possibly other variations.
7869 SDValue N1 = Ops[0];
7870 SDValue N2 = Ops[1];
7871 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7872 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7873 if (N1CFP && N2CFP) {
7874 APFloat C1 = N1CFP->getValueAPF(); // make copy
7875 const APFloat &C2 = N2CFP->getValueAPF();
7876 switch (Opcode) {
7877 case ISD::FADD:
7879 return getConstantFP(C1, DL, VT);
7880 case ISD::FSUB:
7882 return getConstantFP(C1, DL, VT);
7883 case ISD::FMUL:
7885 return getConstantFP(C1, DL, VT);
7886 case ISD::FDIV:
7888 return getConstantFP(C1, DL, VT);
7889 case ISD::FREM:
7890 C1.mod(C2);
7891 return getConstantFP(C1, DL, VT);
7892 case ISD::FCOPYSIGN:
7893 C1.copySign(C2);
7894 return getConstantFP(C1, DL, VT);
7895 case ISD::FMINNUM:
7896 return getConstantFP(minnum(C1, C2), DL, VT);
7897 case ISD::FMAXNUM:
7898 return getConstantFP(maxnum(C1, C2), DL, VT);
7899 case ISD::FMINIMUM:
7900 return getConstantFP(minimum(C1, C2), DL, VT);
7901 case ISD::FMAXIMUM:
7902 return getConstantFP(maximum(C1, C2), DL, VT);
7903 case ISD::FMINIMUMNUM:
7904 return getConstantFP(minimumnum(C1, C2), DL, VT);
7905 case ISD::FMAXIMUMNUM:
7906 return getConstantFP(maximumnum(C1, C2), DL, VT);
7907 default: break;
7908 }
7909 }
7910 if (N1CFP && Opcode == ISD::FP_ROUND) {
7911 APFloat C1 = N1CFP->getValueAPF(); // make copy
7912 bool Unused;
7913 // This can return overflow, underflow, or inexact; we don't care.
7914 // FIXME need to be more flexible about rounding mode.
7916 &Unused);
7917 return getConstantFP(C1, DL, VT);
7918 }
7919
7920 switch (Opcode) {
7921 case ISD::FSUB:
7922 // -0.0 - undef --> undef (consistent with "fneg undef")
7923 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7924 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7925 return getUNDEF(VT);
7926 [[fallthrough]];
7927
7928 case ISD::FADD:
7929 case ISD::FMUL:
7930 case ISD::FDIV:
7931 case ISD::FREM:
7932 // If both operands are undef, the result is undef. If 1 operand is undef,
7933 // the result is NaN. This should match the behavior of the IR optimizer.
7934 if (N1.isUndef() && N2.isUndef())
7935 return getUNDEF(VT);
7936 if (N1.isUndef() || N2.isUndef())
7938 }
7939 return SDValue();
7940}
7941
7943 const SDLoc &DL, EVT DstEltVT) {
7944 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7945
7946 // If this is already the right type, we're done.
7947 if (SrcEltVT == DstEltVT)
7948 return SDValue(BV, 0);
7949
7950 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7951 unsigned DstBitSize = DstEltVT.getSizeInBits();
7952
7953 // If this is a conversion of N elements of one type to N elements of another
7954 // type, convert each element. This handles FP<->INT cases.
7955 if (SrcBitSize == DstBitSize) {
7957 for (SDValue Op : BV->op_values()) {
7958 // If the vector element type is not legal, the BUILD_VECTOR operands
7959 // are promoted and implicitly truncated. Make that explicit here.
7960 if (Op.getValueType() != SrcEltVT)
7961 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7962 Ops.push_back(getBitcast(DstEltVT, Op));
7963 }
7964 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7966 return getBuildVector(VT, DL, Ops);
7967 }
7968
7969 // Otherwise, we're growing or shrinking the elements. To avoid having to
7970 // handle annoying details of growing/shrinking FP values, we convert them to
7971 // int first.
7972 if (SrcEltVT.isFloatingPoint()) {
7973 // Convert the input float vector to a int vector where the elements are the
7974 // same sizes.
7975 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7976 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7978 DstEltVT);
7979 return SDValue();
7980 }
7981
7982 // Now we know the input is an integer vector. If the output is a FP type,
7983 // convert to integer first, then to FP of the right size.
7984 if (DstEltVT.isFloatingPoint()) {
7985 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7986 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7988 DstEltVT);
7989 return SDValue();
7990 }
7991
7992 // Okay, we know the src/dst types are both integers of differing types.
7993 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7994
7995 // Extract the constant raw bit data.
7996 BitVector UndefElements;
7997 SmallVector<APInt> RawBits;
7998 bool IsLE = getDataLayout().isLittleEndian();
7999 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
8000 return SDValue();
8001
8003 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
8004 if (UndefElements[I])
8005 Ops.push_back(getUNDEF(DstEltVT));
8006 else
8007 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
8008 }
8009
8010 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
8011 return getBuildVector(VT, DL, Ops);
8012}
8013
8015 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
8016
8017 // There's no need to assert on a byte-aligned pointer. All pointers are at
8018 // least byte aligned.
8019 if (A == Align(1))
8020 return Val;
8021
8022 SDVTList VTs = getVTList(Val.getValueType());
8024 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
8025 ID.AddInteger(A.value());
8026
8027 void *IP = nullptr;
8028 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
8029 return SDValue(E, 0);
8030
8031 auto *N =
8032 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
8033 createOperands(N, {Val});
8034
8035 CSEMap.InsertNode(N, IP);
8036 InsertNode(N);
8037
8038 SDValue V(N, 0);
8039 NewSDValueDbgMsg(V, "Creating new node: ", this);
8040 return V;
8041}
8042
8043SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8044 SDValue N1, SDValue N2) {
8045 SDNodeFlags Flags;
8046 if (Inserter)
8047 Flags = Inserter->getFlags();
8048 return getNode(Opcode, DL, VT, N1, N2, Flags);
8049}
8050
8052 SDValue &N2) const {
8053 if (!TLI->isCommutativeBinOp(Opcode))
8054 return;
8055
8056 // Canonicalize:
8057 // binop(const, nonconst) -> binop(nonconst, const)
8060 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
8061 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
8062 if ((N1C && !N2C) || (N1CFP && !N2CFP))
8063 std::swap(N1, N2);
8064
8065 // Canonicalize:
8066 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
8067 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
8069 std::swap(N1, N2);
8070}
8071
8072SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8073 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
8075 N2.getOpcode() != ISD::DELETED_NODE &&
8076 "Operand is DELETED_NODE!");
8077
8078 canonicalizeCommutativeBinop(Opcode, N1, N2);
8079
8080 auto *N1C = dyn_cast<ConstantSDNode>(N1);
8081 auto *N2C = dyn_cast<ConstantSDNode>(N2);
8082
8083 // Don't allow undefs in vector splats - we might be returning N2 when folding
8084 // to zero etc.
8085 ConstantSDNode *N2CV =
8086 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
8087
8088 switch (Opcode) {
8089 default: break;
8090 case ISD::TokenFactor:
8091 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
8092 N2.getValueType() == MVT::Other && "Invalid token factor!");
8093 // Fold trivial token factors.
8094 if (N1.getOpcode() == ISD::EntryToken) return N2;
8095 if (N2.getOpcode() == ISD::EntryToken) return N1;
8096 if (N1 == N2) return N1;
8097 break;
8098 case ISD::BUILD_VECTOR: {
8099 // Attempt to simplify BUILD_VECTOR.
8100 SDValue Ops[] = {N1, N2};
8101 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8102 return V;
8103 break;
8104 }
8105 case ISD::CONCAT_VECTORS: {
8106 SDValue Ops[] = {N1, N2};
8107 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8108 return V;
8109 break;
8110 }
8111 case ISD::AND:
8112 assert(VT.isInteger() && "This operator does not apply to FP types!");
8113 assert(N1.getValueType() == N2.getValueType() &&
8114 N1.getValueType() == VT && "Binary operator types must match!");
8115 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
8116 // worth handling here.
8117 if (N2CV && N2CV->isZero())
8118 return N2;
8119 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
8120 return N1;
8121 break;
8122 case ISD::OR:
8123 case ISD::XOR:
8124 case ISD::ADD:
8125 case ISD::PTRADD:
8126 case ISD::SUB:
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 // The equal operand types requirement is unnecessarily strong for PTRADD.
8131 // However, the SelectionDAGBuilder does not generate PTRADDs with different
8132 // operand types, and we'd need to re-implement GEP's non-standard wrapping
8133 // logic everywhere where PTRADDs may be folded or combined to properly
8134 // support them. If/when we introduce pointer types to the SDAG, we will
8135 // need to relax this constraint.
8136
8137 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
8138 // it's worth handling here.
8139 if (N2CV && N2CV->isZero())
8140 return N1;
8141 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
8142 VT.getScalarType() == MVT::i1)
8143 return getNode(ISD::XOR, DL, VT, N1, N2);
8144 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
8145 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
8146 N2.getOpcode() == ISD::VSCALE) {
8147 const APInt &C1 = N1->getConstantOperandAPInt(0);
8148 const APInt &C2 = N2->getConstantOperandAPInt(0);
8149 return getVScale(DL, VT, C1 + C2);
8150 }
8151 break;
8152 case ISD::MUL:
8153 assert(VT.isInteger() && "This operator does not apply to FP types!");
8154 assert(N1.getValueType() == N2.getValueType() &&
8155 N1.getValueType() == VT && "Binary operator types must match!");
8156 if (VT.getScalarType() == MVT::i1)
8157 return getNode(ISD::AND, DL, VT, N1, N2);
8158 if (N2CV && N2CV->isZero())
8159 return N2;
8160 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
8161 const APInt &MulImm = N1->getConstantOperandAPInt(0);
8162 const APInt &N2CImm = N2C->getAPIntValue();
8163 return getVScale(DL, VT, MulImm * N2CImm);
8164 }
8165 break;
8166 case ISD::UDIV:
8167 case ISD::UREM:
8168 case ISD::MULHU:
8169 case ISD::MULHS:
8170 case ISD::SDIV:
8171 case ISD::SREM:
8172 case ISD::SADDSAT:
8173 case ISD::SSUBSAT:
8174 case ISD::UADDSAT:
8175 case ISD::USUBSAT:
8176 assert(VT.isInteger() && "This operator does not apply to FP types!");
8177 assert(N1.getValueType() == N2.getValueType() &&
8178 N1.getValueType() == VT && "Binary operator types must match!");
8179 if (VT.getScalarType() == MVT::i1) {
8180 // fold (add_sat x, y) -> (or x, y) for bool types.
8181 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
8182 return getNode(ISD::OR, DL, VT, N1, N2);
8183 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
8184 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
8185 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
8186 }
8187 break;
8188 case ISD::SCMP:
8189 case ISD::UCMP:
8190 assert(N1.getValueType() == N2.getValueType() &&
8191 "Types of operands of UCMP/SCMP must match");
8192 assert(N1.getValueType().isVector() == VT.isVector() &&
8193 "Operands and return type of must both be scalars or vectors");
8194 if (VT.isVector())
8197 "Result and operands must have the same number of elements");
8198 break;
8199 case ISD::AVGFLOORS:
8200 case ISD::AVGFLOORU:
8201 case ISD::AVGCEILS:
8202 case ISD::AVGCEILU:
8203 assert(VT.isInteger() && "This operator does not apply to FP types!");
8204 assert(N1.getValueType() == N2.getValueType() &&
8205 N1.getValueType() == VT && "Binary operator types must match!");
8206 break;
8207 case ISD::ABDS:
8208 case ISD::ABDU:
8209 assert(VT.isInteger() && "This operator does not apply to FP types!");
8210 assert(N1.getValueType() == N2.getValueType() &&
8211 N1.getValueType() == VT && "Binary operator types must match!");
8212 if (VT.getScalarType() == MVT::i1)
8213 return getNode(ISD::XOR, DL, VT, N1, N2);
8214 break;
8215 case ISD::SMIN:
8216 case ISD::UMAX:
8217 assert(VT.isInteger() && "This operator does not apply to FP types!");
8218 assert(N1.getValueType() == N2.getValueType() &&
8219 N1.getValueType() == VT && "Binary operator types must match!");
8220 if (VT.getScalarType() == MVT::i1)
8221 return getNode(ISD::OR, DL, VT, N1, N2);
8222 break;
8223 case ISD::SMAX:
8224 case ISD::UMIN:
8225 assert(VT.isInteger() && "This operator does not apply to FP types!");
8226 assert(N1.getValueType() == N2.getValueType() &&
8227 N1.getValueType() == VT && "Binary operator types must match!");
8228 if (VT.getScalarType() == MVT::i1)
8229 return getNode(ISD::AND, DL, VT, N1, N2);
8230 break;
8231 case ISD::FADD:
8232 case ISD::FSUB:
8233 case ISD::FMUL:
8234 case ISD::FDIV:
8235 case ISD::FREM:
8236 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
8237 assert(N1.getValueType() == N2.getValueType() &&
8238 N1.getValueType() == VT && "Binary operator types must match!");
8239 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
8240 return V;
8241 break;
8242 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
8243 assert(N1.getValueType() == VT &&
8246 "Invalid FCOPYSIGN!");
8247 break;
8248 case ISD::SHL:
8249 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
8250 const APInt &MulImm = N1->getConstantOperandAPInt(0);
8251 const APInt &ShiftImm = N2C->getAPIntValue();
8252 return getVScale(DL, VT, MulImm << ShiftImm);
8253 }
8254 [[fallthrough]];
8255 case ISD::SRA:
8256 case ISD::SRL:
8257 if (SDValue V = simplifyShift(N1, N2))
8258 return V;
8259 [[fallthrough]];
8260 case ISD::ROTL:
8261 case ISD::ROTR:
8262 case ISD::SSHLSAT:
8263 case ISD::USHLSAT:
8264 assert(VT == N1.getValueType() &&
8265 "Shift operators return type must be the same as their first arg");
8266 assert(VT.isInteger() && N2.getValueType().isInteger() &&
8267 "Shifts only work on integers");
8268 assert((!VT.isVector() || VT == N2.getValueType()) &&
8269 "Vector shift amounts must be in the same as their first arg");
8270 // Verify that the shift amount VT is big enough to hold valid shift
8271 // amounts. This catches things like trying to shift an i1024 value by an
8272 // i8, which is easy to fall into in generic code that uses
8273 // TLI.getShiftAmount().
8276 "Invalid use of small shift amount with oversized value!");
8277
8278 // Always fold shifts of i1 values so the code generator doesn't need to
8279 // handle them. Since we know the size of the shift has to be less than the
8280 // size of the value, the shift/rotate count is guaranteed to be zero.
8281 if (VT == MVT::i1)
8282 return N1;
8283 if (N2CV && N2CV->isZero())
8284 return N1;
8285 break;
8286 case ISD::FP_ROUND:
8288 VT.bitsLE(N1.getValueType()) && N2C &&
8289 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
8290 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
8291 if (N1.getValueType() == VT) return N1; // noop conversion.
8292 break;
8293 case ISD::AssertNoFPClass: {
8295 "AssertNoFPClass is used for a non-floating type");
8296 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
8297 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
8298 assert(llvm::to_underlying(NoFPClass) <=
8300 "FPClassTest value too large");
8301 (void)NoFPClass;
8302 break;
8303 }
8304 case ISD::AssertSext:
8305 case ISD::AssertZext: {
8306 EVT EVT = cast<VTSDNode>(N2)->getVT();
8307 assert(VT == N1.getValueType() && "Not an inreg extend!");
8308 assert(VT.isInteger() && EVT.isInteger() &&
8309 "Cannot *_EXTEND_INREG FP types");
8310 assert(!EVT.isVector() &&
8311 "AssertSExt/AssertZExt type should be the vector element type "
8312 "rather than the vector type!");
8313 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
8314 if (VT.getScalarType() == EVT) return N1; // noop assertion.
8315 break;
8316 }
8318 EVT EVT = cast<VTSDNode>(N2)->getVT();
8319 assert(VT == N1.getValueType() && "Not an inreg extend!");
8320 assert(VT.isInteger() && EVT.isInteger() &&
8321 "Cannot *_EXTEND_INREG FP types");
8322 assert(EVT.isVector() == VT.isVector() &&
8323 "SIGN_EXTEND_INREG type should be vector iff the operand "
8324 "type is vector!");
8325 assert((!EVT.isVector() ||
8327 "Vector element counts must match in SIGN_EXTEND_INREG");
8328 assert(EVT.getScalarType().bitsLE(VT.getScalarType()) && "Not extending!");
8329 if (EVT == VT) return N1; // Not actually extending
8330 break;
8331 }
8333 case ISD::FP_TO_UINT_SAT: {
8334 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
8335 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
8336 assert(N1.getValueType().isVector() == VT.isVector() &&
8337 "FP_TO_*INT_SAT type should be vector iff the operand type is "
8338 "vector!");
8339 assert((!VT.isVector() || VT.getVectorElementCount() ==
8341 "Vector element counts must match in FP_TO_*INT_SAT");
8342 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
8343 "Type to saturate to must be a scalar.");
8344 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
8345 "Not extending!");
8346 break;
8347 }
8350 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
8351 element type of the vector.");
8352
8353 // Extract from an undefined value or using an undefined index is undefined.
8354 if (N1.isUndef() || N2.isUndef())
8355 return getUNDEF(VT);
8356
8357 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
8358 // vectors. For scalable vectors we will provide appropriate support for
8359 // dealing with arbitrary indices.
8360 if (N2C && N1.getValueType().isFixedLengthVector() &&
8361 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
8362 return getUNDEF(VT);
8363
8364 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
8365 // expanding copies of large vectors from registers. This only works for
8366 // fixed length vectors, since we need to know the exact number of
8367 // elements.
8368 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
8370 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
8371 return getExtractVectorElt(DL, VT,
8372 N1.getOperand(N2C->getZExtValue() / Factor),
8373 N2C->getZExtValue() % Factor);
8374 }
8375
8376 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
8377 // lowering is expanding large vector constants.
8378 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
8379 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
8382 "BUILD_VECTOR used for scalable vectors");
8383 unsigned Index =
8384 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
8385 SDValue Elt = N1.getOperand(Index);
8386
8387 if (VT != Elt.getValueType())
8388 // If the vector element type is not legal, the BUILD_VECTOR operands
8389 // are promoted and implicitly truncated, and the result implicitly
8390 // extended. Make that explicit here.
8391 Elt = getAnyExtOrTrunc(Elt, DL, VT);
8392
8393 return Elt;
8394 }
8395
8396 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
8397 // operations are lowered to scalars.
8398 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
8399 // If the indices are the same, return the inserted element else
8400 // if the indices are known different, extract the element from
8401 // the original vector.
8402 SDValue N1Op2 = N1.getOperand(2);
8404
8405 if (N1Op2C && N2C) {
8406 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
8407 if (VT == N1.getOperand(1).getValueType())
8408 return N1.getOperand(1);
8409 if (VT.isFloatingPoint()) {
8411 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
8412 }
8413 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
8414 }
8415 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
8416 }
8417 }
8418
8419 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
8420 // when vector types are scalarized and v1iX is legal.
8421 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
8422 // Here we are completely ignoring the extract element index (N2),
8423 // which is fine for fixed width vectors, since any index other than 0
8424 // is undefined anyway. However, this cannot be ignored for scalable
8425 // vectors - in theory we could support this, but we don't want to do this
8426 // without a profitability check.
8427 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8429 N1.getValueType().getVectorNumElements() == 1) {
8430 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8431 N1.getOperand(1));
8432 }
8433 break;
8435 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8436 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8437 (N1.getValueType().isInteger() == VT.isInteger()) &&
8438 N1.getValueType() != VT &&
8439 "Wrong types for EXTRACT_ELEMENT!");
8440
8441 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8442 // 64-bit integers into 32-bit parts. Instead of building the extract of
8443 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8444 if (N1.getOpcode() == ISD::BUILD_PAIR)
8445 return N1.getOperand(N2C->getZExtValue());
8446
8447 // EXTRACT_ELEMENT of a constant int is also very common.
8448 if (N1C) {
8449 unsigned ElementSize = VT.getSizeInBits();
8450 unsigned Shift = ElementSize * N2C->getZExtValue();
8451 const APInt &Val = N1C->getAPIntValue();
8452 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8453 }
8454 break;
8456 EVT N1VT = N1.getValueType();
8457 assert(VT.isVector() && N1VT.isVector() &&
8458 "Extract subvector VTs must be vectors!");
8460 "Extract subvector VTs must have the same element type!");
8461 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8462 "Cannot extract a scalable vector from a fixed length vector!");
8463 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8465 "Extract subvector must be from larger vector to smaller vector!");
8466 assert(N2C && "Extract subvector index must be a constant");
8467 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8468 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8469 N1VT.getVectorMinNumElements()) &&
8470 "Extract subvector overflow!");
8471 assert(N2C->getAPIntValue().getBitWidth() ==
8472 TLI->getVectorIdxWidth(getDataLayout()) &&
8473 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8474 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8475 "Extract index is not a multiple of the output vector length");
8476
8477 // Trivial extraction.
8478 if (VT == N1VT)
8479 return N1;
8480
8481 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8482 if (N1.isUndef())
8483 return getUNDEF(VT);
8484
8485 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8486 // the concat have the same type as the extract.
8487 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8488 VT == N1.getOperand(0).getValueType()) {
8489 unsigned Factor = VT.getVectorMinNumElements();
8490 return N1.getOperand(N2C->getZExtValue() / Factor);
8491 }
8492
8493 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8494 // during shuffle legalization.
8495 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8496 VT == N1.getOperand(1).getValueType())
8497 return N1.getOperand(1);
8498 break;
8499 }
8500 }
8501
8502 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8503 switch (Opcode) {
8504 case ISD::XOR:
8505 case ISD::ADD:
8506 case ISD::PTRADD:
8507 case ISD::SUB:
8509 case ISD::UDIV:
8510 case ISD::SDIV:
8511 case ISD::UREM:
8512 case ISD::SREM:
8513 case ISD::MUL:
8514 case ISD::AND:
8515 case ISD::SSUBSAT:
8516 case ISD::USUBSAT:
8517 case ISD::UMIN:
8518 case ISD::OR:
8519 case ISD::SADDSAT:
8520 case ISD::UADDSAT:
8521 case ISD::UMAX:
8522 case ISD::SMAX:
8523 case ISD::SMIN:
8524 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8525 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8526 }
8527 }
8528
8529 // Canonicalize an UNDEF to the RHS, even over a constant.
8530 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8531 if (TLI->isCommutativeBinOp(Opcode)) {
8532 std::swap(N1, N2);
8533 } else {
8534 switch (Opcode) {
8535 case ISD::PTRADD:
8536 case ISD::SUB:
8537 // fold op(undef, non_undef_arg2) -> undef.
8538 return N1;
8540 case ISD::UDIV:
8541 case ISD::SDIV:
8542 case ISD::UREM:
8543 case ISD::SREM:
8544 case ISD::SSUBSAT:
8545 case ISD::USUBSAT:
8546 // fold op(undef, non_undef_arg2) -> 0.
8547 return getConstant(0, DL, VT);
8548 }
8549 }
8550 }
8551
8552 // Fold a bunch of operators when the RHS is undef.
8553 if (N2.getOpcode() == ISD::UNDEF) {
8554 switch (Opcode) {
8555 case ISD::XOR:
8556 if (N1.getOpcode() == ISD::UNDEF)
8557 // Handle undef ^ undef -> 0 special case. This is a common
8558 // idiom (misuse).
8559 return getConstant(0, DL, VT);
8560 [[fallthrough]];
8561 case ISD::ADD:
8562 case ISD::PTRADD:
8563 case ISD::SUB:
8564 // fold op(arg1, undef) -> undef.
8565 return N2;
8566 case ISD::UDIV:
8567 case ISD::SDIV:
8568 case ISD::UREM:
8569 case ISD::SREM:
8570 // fold op(arg1, undef) -> poison.
8571 return getPOISON(VT);
8572 case ISD::MUL:
8573 case ISD::AND:
8574 case ISD::SSUBSAT:
8575 case ISD::USUBSAT:
8576 case ISD::UMIN:
8577 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8578 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8579 case ISD::OR:
8580 case ISD::SADDSAT:
8581 case ISD::UADDSAT:
8582 case ISD::UMAX:
8583 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8584 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8585 case ISD::SMAX:
8586 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8587 return N1.getOpcode() == ISD::UNDEF
8588 ? N2
8589 : getConstant(
8591 VT);
8592 case ISD::SMIN:
8593 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8594 return N1.getOpcode() == ISD::UNDEF
8595 ? N2
8596 : getConstant(
8598 VT);
8599 }
8600 }
8601
8602 // Perform trivial constant folding.
8603 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8604 return SV;
8605
8606 // Memoize this node if possible.
8607 SDNode *N;
8608 SDVTList VTs = getVTList(VT);
8609 SDValue Ops[] = {N1, N2};
8610 if (VT != MVT::Glue) {
8612 AddNodeIDNode(ID, Opcode, VTs, Ops);
8613 void *IP = nullptr;
8614 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8615 E->intersectFlagsWith(Flags);
8616 return SDValue(E, 0);
8617 }
8618
8619 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8620 N->setFlags(Flags);
8621 createOperands(N, Ops);
8622 CSEMap.InsertNode(N, IP);
8623 } else {
8624 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8625 createOperands(N, Ops);
8626 }
8627
8628 InsertNode(N);
8629 SDValue V = SDValue(N, 0);
8630 NewSDValueDbgMsg(V, "Creating new node: ", this);
8631 return V;
8632}
8633
8634SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8635 SDValue N1, SDValue N2, SDValue N3) {
8636 SDNodeFlags Flags;
8637 if (Inserter)
8638 Flags = Inserter->getFlags();
8639 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8640}
8641
8642SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8643 SDValue N1, SDValue N2, SDValue N3,
8644 const SDNodeFlags Flags) {
8646 N2.getOpcode() != ISD::DELETED_NODE &&
8647 N3.getOpcode() != ISD::DELETED_NODE &&
8648 "Operand is DELETED_NODE!");
8649 // Perform various simplifications.
8650 switch (Opcode) {
8651 case ISD::BUILD_VECTOR: {
8652 // Attempt to simplify BUILD_VECTOR.
8653 SDValue Ops[] = {N1, N2, N3};
8654 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8655 return V;
8656 break;
8657 }
8658 case ISD::CONCAT_VECTORS: {
8659 SDValue Ops[] = {N1, N2, N3};
8660 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8661 return V;
8662 break;
8663 }
8664 case ISD::SETCC: {
8665 assert(VT.isInteger() && "SETCC result type must be an integer!");
8666 assert(N1.getValueType() == N2.getValueType() &&
8667 "SETCC operands must have the same type!");
8668 assert(VT.isVector() == N1.getValueType().isVector() &&
8669 "SETCC type should be vector iff the operand type is vector!");
8670 assert((!VT.isVector() || VT.getVectorElementCount() ==
8672 "SETCC vector element counts must match!");
8673 // Use FoldSetCC to simplify SETCC's.
8674 if (SDValue V =
8675 FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL, Flags))
8676 return V;
8677 break;
8678 }
8679 case ISD::SELECT:
8680 case ISD::VSELECT:
8681 if (SDValue V = simplifySelect(N1, N2, N3))
8682 return V;
8683 break;
8685 llvm_unreachable("should use getVectorShuffle constructor!");
8687 if (isNullConstant(N3))
8688 return N1;
8689 break;
8691 if (isNullConstant(N3))
8692 return N2;
8693 break;
8695 assert(VT.isVector() && VT == N1.getValueType() &&
8696 "INSERT_VECTOR_ELT vector type mismatch");
8698 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8699 assert((!VT.isFloatingPoint() ||
8700 VT.getVectorElementType() == N2.getValueType()) &&
8701 "INSERT_VECTOR_ELT fp scalar type mismatch");
8702 assert((!VT.isInteger() ||
8704 "INSERT_VECTOR_ELT int scalar size mismatch");
8705
8706 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8707 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8708 // for scalable vectors where we will generate appropriate code to
8709 // deal with out-of-bounds cases correctly.
8710 if (N3C && VT.isFixedLengthVector() &&
8711 N3C->getZExtValue() >= VT.getVectorNumElements())
8712 return getUNDEF(VT);
8713
8714 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8715 if (N3.isUndef())
8716 return getUNDEF(VT);
8717
8718 // If inserting poison, just use the input vector.
8719 if (N2.getOpcode() == ISD::POISON)
8720 return N1;
8721
8722 // Inserting undef into undef/poison is still undef.
8723 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8724 return getUNDEF(VT);
8725
8726 // If the inserted element is an UNDEF, just use the input vector.
8727 // But not if skipping the insert could make the result more poisonous.
8728 if (N2.isUndef()) {
8729 if (N3C && VT.isFixedLengthVector()) {
8730 APInt EltMask =
8731 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8732 if (isGuaranteedNotToBePoison(N1, EltMask))
8733 return N1;
8734 } else if (isGuaranteedNotToBePoison(N1))
8735 return N1;
8736 }
8737 break;
8738 }
8739 case ISD::INSERT_SUBVECTOR: {
8740 // If inserting poison, just use the input vector,
8741 if (N2.getOpcode() == ISD::POISON)
8742 return N1;
8743
8744 // Inserting undef into undef/poison is still undef.
8745 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8746 return getUNDEF(VT);
8747
8748 EVT N2VT = N2.getValueType();
8749 assert(VT == N1.getValueType() &&
8750 "Dest and insert subvector source types must match!");
8751 assert(VT.isVector() && N2VT.isVector() &&
8752 "Insert subvector VTs must be vectors!");
8754 "Insert subvector VTs must have the same element type!");
8755 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8756 "Cannot insert a scalable vector into a fixed length vector!");
8757 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8759 "Insert subvector must be from smaller vector to larger vector!");
8761 "Insert subvector index must be constant");
8762 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8763 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8765 "Insert subvector overflow!");
8767 TLI->getVectorIdxWidth(getDataLayout()) &&
8768 "Constant index for INSERT_SUBVECTOR has an invalid size");
8769
8770 // Trivial insertion.
8771 if (VT == N2VT)
8772 return N2;
8773
8774 // If this is an insert of an extracted vector into an undef/poison vector,
8775 // we can just use the input to the extract. But not if skipping the
8776 // extract+insert could make the result more poisonous.
8777 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8778 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8779 if (N1.getOpcode() == ISD::POISON)
8780 return N2.getOperand(0);
8781 if (VT.isFixedLengthVector() && N2VT.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(N2.getOperand(0), ~EltMask))
8787 return N2.getOperand(0);
8788 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8789 return N2.getOperand(0);
8790 }
8791
8792 // If the inserted subvector is UNDEF, just use the input vector.
8793 // But not if skipping the insert could make the result more poisonous.
8794 if (N2.isUndef()) {
8795 if (VT.isFixedLengthVector()) {
8796 unsigned LoBit = N3->getAsZExtVal();
8797 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8798 APInt EltMask =
8799 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8800 if (isGuaranteedNotToBePoison(N1, EltMask))
8801 return N1;
8802 } else if (isGuaranteedNotToBePoison(N1))
8803 return N1;
8804 }
8805 break;
8806 }
8807 case ISD::BITCAST:
8808 // Fold bit_convert nodes from a type to themselves.
8809 if (N1.getValueType() == VT)
8810 return N1;
8811 break;
8812 case ISD::VP_TRUNCATE:
8813 case ISD::VP_SIGN_EXTEND:
8814 case ISD::VP_ZERO_EXTEND:
8815 // Don't create noop casts.
8816 if (N1.getValueType() == VT)
8817 return N1;
8818 break;
8819 case ISD::VECTOR_COMPRESS: {
8820 [[maybe_unused]] EVT VecVT = N1.getValueType();
8821 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8822 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8823 assert(VT == VecVT && "Vector and result type don't match.");
8824 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8825 "All inputs must be vectors.");
8826 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8828 "Vector and mask must have same number of elements.");
8829
8830 if (N1.isUndef() || N2.isUndef())
8831 return N3;
8832
8833 break;
8834 }
8839 [[maybe_unused]] EVT AccVT = N1.getValueType();
8840 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8841 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8842 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8843 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8844 "node to have the same type!");
8845 assert(VT.isVector() && VT == AccVT &&
8846 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8847 "the same type as its result!");
8849 AccVT.getVectorElementCount()) &&
8850 "Expected the element count of the second and third operands of the "
8851 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8852 "element count of the first operand and the result!");
8854 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8855 "node to have an element type which is the same as or smaller than "
8856 "the element type of the first operand and result!");
8857 break;
8858 }
8859 }
8860
8861 // Perform trivial constant folding for arithmetic operators.
8862 switch (Opcode) {
8863 case ISD::FMA:
8864 case ISD::FMAD:
8865 case ISD::SETCC:
8866 case ISD::FSHL:
8867 case ISD::FSHR:
8868 if (SDValue SV =
8869 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8870 return SV;
8871 break;
8872 }
8873
8874 // Memoize node if it doesn't produce a glue result.
8875 SDNode *N;
8876 SDVTList VTs = getVTList(VT);
8877 SDValue Ops[] = {N1, N2, N3};
8878 if (VT != MVT::Glue) {
8880 AddNodeIDNode(ID, Opcode, VTs, Ops);
8881 void *IP = nullptr;
8882 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8883 E->intersectFlagsWith(Flags);
8884 return SDValue(E, 0);
8885 }
8886
8887 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8888 N->setFlags(Flags);
8889 createOperands(N, Ops);
8890 CSEMap.InsertNode(N, IP);
8891 } else {
8892 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8893 createOperands(N, Ops);
8894 }
8895
8896 InsertNode(N);
8897 SDValue V = SDValue(N, 0);
8898 NewSDValueDbgMsg(V, "Creating new node: ", this);
8899 return V;
8900}
8901
8902SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8903 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8904 const SDNodeFlags Flags) {
8905 SDValue Ops[] = { N1, N2, N3, N4 };
8906 return getNode(Opcode, DL, VT, Ops, Flags);
8907}
8908
8909SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8910 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8911 SDNodeFlags Flags;
8912 if (Inserter)
8913 Flags = Inserter->getFlags();
8914 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8915}
8916
8917SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8918 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8919 SDValue N5, const SDNodeFlags Flags) {
8920 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8921 return getNode(Opcode, DL, VT, Ops, Flags);
8922}
8923
8924SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8925 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8926 SDValue N5) {
8927 SDNodeFlags Flags;
8928 if (Inserter)
8929 Flags = Inserter->getFlags();
8930 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8931}
8932
8933/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8934/// the incoming stack arguments to be loaded from the stack.
8936 SmallVector<SDValue, 8> ArgChains;
8937
8938 // Include the original chain at the beginning of the list. When this is
8939 // used by target LowerCall hooks, this helps legalize find the
8940 // CALLSEQ_BEGIN node.
8941 ArgChains.push_back(Chain);
8942
8943 // Add a chain value for each stack argument.
8944 for (SDNode *U : getEntryNode().getNode()->users())
8945 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8946 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8947 if (FI->getIndex() < 0)
8948 ArgChains.push_back(SDValue(L, 1));
8949
8950 // Build a tokenfactor for all the chains.
8951 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8952}
8953
8954/// getMemsetValue - Vectorized representation of the memset value
8955/// operand.
8957 const SDLoc &dl) {
8958 assert(!Value.isUndef());
8959
8960 unsigned NumBits = VT.getScalarSizeInBits();
8962 assert(C->getAPIntValue().getBitWidth() == 8);
8963 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8964 if (VT.isInteger()) {
8965 bool IsOpaque = VT.getSizeInBits() > 64 ||
8966 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8967 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8968 }
8969 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8970 }
8971
8972 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8973 EVT IntVT = VT.getScalarType();
8974 if (!IntVT.isInteger())
8975 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8976
8977 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8978 if (NumBits > 8) {
8979 // Use a multiplication with 0x010101... to extend the input to the
8980 // required length.
8981 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8982 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8983 DAG.getConstant(Magic, dl, IntVT));
8984 }
8985
8986 if (VT != Value.getValueType() && !VT.isInteger())
8987 Value = DAG.getBitcast(VT.getScalarType(), Value);
8988 if (VT != Value.getValueType())
8989 Value = DAG.getSplatBuildVector(VT, dl, Value);
8990
8991 return Value;
8992}
8993
8994/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8995/// used when a memcpy is turned into a memset when the source is a constant
8996/// string ptr.
8998 const TargetLowering &TLI,
8999 const ConstantDataArraySlice &Slice) {
9000 // Handle vector with all elements zero.
9001 if (Slice.Array == nullptr) {
9002 if (VT.isInteger())
9003 return DAG.getConstant(0, dl, VT);
9004 return DAG.getNode(ISD::BITCAST, dl, VT,
9005 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
9006 }
9007
9008 assert(!VT.isVector() && "Can't handle vector type here!");
9009 unsigned NumVTBits = VT.getSizeInBits();
9010 unsigned NumVTBytes = NumVTBits / 8;
9011 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
9012
9013 APInt Val(NumVTBits, 0);
9014 if (DAG.getDataLayout().isLittleEndian()) {
9015 for (unsigned i = 0; i != NumBytes; ++i)
9016 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
9017 } else {
9018 for (unsigned i = 0; i != NumBytes; ++i)
9019 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
9020 }
9021
9022 // If the "cost" of materializing the integer immediate is less than the cost
9023 // of a load, then it is cost effective to turn the load into the immediate.
9024 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
9025 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
9026 return DAG.getConstant(Val, dl, VT);
9027 return SDValue();
9028}
9029
9031 const SDLoc &DL,
9032 const SDNodeFlags Flags) {
9033 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
9034 return getMemBasePlusOffset(Base, Index, DL, Flags);
9035}
9036
9038 const SDLoc &DL,
9039 const SDNodeFlags Flags) {
9040 assert(Offset.getValueType().isInteger());
9041 EVT BasePtrVT = Ptr.getValueType();
9042 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
9043 BasePtrVT))
9044 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
9045 // InBounds only applies to PTRADD, don't set it if we generate ADD.
9046 SDNodeFlags AddFlags = Flags;
9047 AddFlags.setInBounds(false);
9048 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
9049}
9050
9051/// Returns true if memcpy source is constant data.
9053 uint64_t SrcDelta = 0;
9054 GlobalAddressSDNode *G = nullptr;
9055 if (Src.getOpcode() == ISD::GlobalAddress)
9057 else if (Src->isAnyAdd() &&
9058 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
9059 Src.getOperand(1).getOpcode() == ISD::Constant) {
9060 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
9061 SrcDelta = Src.getConstantOperandVal(1);
9062 }
9063 if (!G)
9064 return false;
9065
9066 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
9067 SrcDelta + G->getOffset());
9068}
9069
9071 SelectionDAG &DAG) {
9072 // On Darwin, -Os means optimize for size without hurting performance, so
9073 // only really optimize for size when -Oz (MinSize) is used.
9075 return MF.getFunction().hasMinSize();
9076 return DAG.shouldOptForSize();
9077}
9078
9080 SmallVector<SDValue, 32> &OutChains, unsigned From,
9081 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
9082 SmallVector<SDValue, 16> &OutStoreChains) {
9083 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
9084 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
9085 SmallVector<SDValue, 16> GluedLoadChains;
9086 for (unsigned i = From; i < To; ++i) {
9087 OutChains.push_back(OutLoadChains[i]);
9088 GluedLoadChains.push_back(OutLoadChains[i]);
9089 }
9090
9091 // Chain for all loads.
9092 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
9093 GluedLoadChains);
9094
9095 for (unsigned i = From; i < To; ++i) {
9096 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
9097 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
9098 ST->getBasePtr(), ST->getMemoryVT(),
9099 ST->getMemOperand());
9100 OutChains.push_back(NewStore);
9101 }
9102}
9103
9105 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
9106 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
9107 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
9108 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
9109 // Turn a memcpy of undef to nop.
9110 // FIXME: We need to honor volatile even is Src is undef.
9111 if (Src.isUndef())
9112 return Chain;
9113
9114 // Expand memcpy to a series of load and store ops if the size operand falls
9115 // below a certain threshold.
9116 // TODO: In the AlwaysInline case, if the size is big then generate a loop
9117 // rather than maybe a humongous number of loads and stores.
9118 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9119 const DataLayout &DL = DAG.getDataLayout();
9120 LLVMContext &C = *DAG.getContext();
9121 std::vector<EVT> MemOps;
9122 bool DstAlignCanChange = false;
9124 MachineFrameInfo &MFI = MF.getFrameInfo();
9125 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9127 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9128 DstAlignCanChange = true;
9129 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
9130 if (!SrcAlign || Alignment > *SrcAlign)
9131 SrcAlign = Alignment;
9132 assert(SrcAlign && "SrcAlign must be set");
9134 // If marked as volatile, perform a copy even when marked as constant.
9135 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
9136 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
9137 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
9138 const MemOp Op = isZeroConstant
9139 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
9140 /*IsZeroMemset*/ true, isVol)
9141 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
9142 *SrcAlign, isVol, CopyFromConstant);
9143 if (!TLI.findOptimalMemOpLowering(
9144 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
9145 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes(), nullptr))
9146 return SDValue();
9147
9148 if (DstAlignCanChange) {
9149 Type *Ty = MemOps[0].getTypeForEVT(C);
9150 Align NewAlign = DL.getABITypeAlign(Ty);
9151
9152 // Don't promote to an alignment that would require dynamic stack
9153 // realignment which may conflict with optimizations such as tail call
9154 // optimization.
9156 if (!TRI->hasStackRealignment(MF))
9157 if (MaybeAlign StackAlign = DL.getStackAlignment())
9158 NewAlign = std::min(NewAlign, *StackAlign);
9159
9160 if (NewAlign > Alignment) {
9161 // Give the stack frame object a larger alignment if needed.
9162 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9163 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9164 Alignment = NewAlign;
9165 }
9166 }
9167
9168 // Prepare AAInfo for loads/stores after lowering this memcpy.
9169 AAMDNodes NewAAInfo = AAInfo;
9170 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9171
9172 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
9173 bool isConstant =
9174 BatchAA && SrcVal &&
9175 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
9176
9177 MachineMemOperand::Flags MMOFlags =
9179 SmallVector<SDValue, 16> OutLoadChains;
9180 SmallVector<SDValue, 16> OutStoreChains;
9181 SmallVector<SDValue, 32> OutChains;
9182 unsigned NumMemOps = MemOps.size();
9183 uint64_t SrcOff = 0, DstOff = 0;
9184 for (unsigned i = 0; i != NumMemOps; ++i) {
9185 EVT VT = MemOps[i];
9186 unsigned VTSize = VT.getSizeInBits() / 8;
9187 SDValue Value, Store;
9188
9189 if (VTSize > Size) {
9190 // Issuing an unaligned load / store pair that overlaps with the previous
9191 // pair. Adjust the offset accordingly.
9192 assert(i == NumMemOps-1 && i != 0);
9193 SrcOff -= VTSize - Size;
9194 DstOff -= VTSize - Size;
9195 }
9196
9197 if (CopyFromConstant &&
9198 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
9199 // It's unlikely a store of a vector immediate can be done in a single
9200 // instruction. It would require a load from a constantpool first.
9201 // We only handle zero vectors here.
9202 // FIXME: Handle other cases where store of vector immediate is done in
9203 // a single instruction.
9204 ConstantDataArraySlice SubSlice;
9205 if (SrcOff < Slice.Length) {
9206 SubSlice = Slice;
9207 SubSlice.move(SrcOff);
9208 } else {
9209 // This is an out-of-bounds access and hence UB. Pretend we read zero.
9210 SubSlice.Array = nullptr;
9211 SubSlice.Offset = 0;
9212 SubSlice.Length = VTSize;
9213 }
9214 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
9215 if (Value.getNode()) {
9216 Store = DAG.getStore(
9217 Chain, dl, Value,
9218 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9219 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
9220 OutChains.push_back(Store);
9221 }
9222 }
9223
9224 if (!Store.getNode()) {
9225 // The type might not be legal for the target. This should only happen
9226 // if the type is smaller than a legal type, as on PPC, so the right
9227 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
9228 // to Load/Store if NVT==VT.
9229 // FIXME does the case above also need this?
9230 EVT NVT = TLI.getTypeToTransformTo(C, VT);
9231 assert(NVT.bitsGE(VT));
9232
9233 bool isDereferenceable =
9234 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9235 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9236 if (isDereferenceable)
9238 if (isConstant)
9239 SrcMMOFlags |= MachineMemOperand::MOInvariant;
9240
9241 Value = DAG.getExtLoad(
9242 ISD::EXTLOAD, dl, NVT, Chain,
9243 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9244 SrcPtrInfo.getWithOffset(SrcOff), VT,
9245 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
9246 OutLoadChains.push_back(Value.getValue(1));
9247
9248 Store = DAG.getTruncStore(
9249 Chain, dl, Value,
9250 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9251 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
9252 OutStoreChains.push_back(Store);
9253 }
9254 SrcOff += VTSize;
9255 DstOff += VTSize;
9256 Size -= VTSize;
9257 }
9258
9259 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
9261 unsigned NumLdStInMemcpy = OutStoreChains.size();
9262
9263 if (NumLdStInMemcpy) {
9264 // It may be that memcpy might be converted to memset if it's memcpy
9265 // of constants. In such a case, we won't have loads and stores, but
9266 // just stores. In the absence of loads, there is nothing to gang up.
9267 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
9268 // If target does not care, just leave as it.
9269 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
9270 OutChains.push_back(OutLoadChains[i]);
9271 OutChains.push_back(OutStoreChains[i]);
9272 }
9273 } else {
9274 // Ld/St less than/equal limit set by target.
9275 if (NumLdStInMemcpy <= GluedLdStLimit) {
9276 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
9277 NumLdStInMemcpy, OutLoadChains,
9278 OutStoreChains);
9279 } else {
9280 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
9281 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
9282 unsigned GlueIter = 0;
9283
9284 // Residual ld/st.
9285 if (RemainingLdStInMemcpy) {
9287 DAG, dl, OutChains, NumLdStInMemcpy - RemainingLdStInMemcpy,
9288 NumLdStInMemcpy, OutLoadChains, OutStoreChains);
9289 }
9290
9291 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
9292 unsigned IndexFrom = NumLdStInMemcpy - RemainingLdStInMemcpy -
9293 GlueIter - GluedLdStLimit;
9294 unsigned IndexTo = NumLdStInMemcpy - RemainingLdStInMemcpy - GlueIter;
9295 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
9296 OutLoadChains, OutStoreChains);
9297 GlueIter += GluedLdStLimit;
9298 }
9299 }
9300 }
9301 }
9302 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9303}
9304
9306 SDValue Chain, SDValue Dst, SDValue Src,
9307 uint64_t Size, Align Alignment,
9308 bool isVol, bool AlwaysInline,
9309 MachinePointerInfo DstPtrInfo,
9310 MachinePointerInfo SrcPtrInfo,
9311 const AAMDNodes &AAInfo) {
9312 // Turn a memmove of undef to nop.
9313 // FIXME: We need to honor volatile even is Src is undef.
9314 if (Src.isUndef())
9315 return Chain;
9316
9317 // Expand memmove to a series of load and store ops if the size operand falls
9318 // below a certain threshold.
9319 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9320 const DataLayout &DL = DAG.getDataLayout();
9321 LLVMContext &C = *DAG.getContext();
9322 std::vector<EVT> MemOps;
9323 bool DstAlignCanChange = false;
9325 MachineFrameInfo &MFI = MF.getFrameInfo();
9326 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9328 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9329 DstAlignCanChange = true;
9330 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
9331 if (!SrcAlign || Alignment > *SrcAlign)
9332 SrcAlign = Alignment;
9333 assert(SrcAlign && "SrcAlign must be set");
9334 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
9335 if (!TLI.findOptimalMemOpLowering(
9336 C, MemOps, Limit,
9337 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign, isVol),
9338 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
9339 MF.getFunction().getAttributes(), nullptr))
9340 return SDValue();
9341
9342 if (DstAlignCanChange) {
9343 Type *Ty = MemOps[0].getTypeForEVT(C);
9344 Align NewAlign = DL.getABITypeAlign(Ty);
9345
9346 // Don't promote to an alignment that would require dynamic stack
9347 // realignment which may conflict with optimizations such as tail call
9348 // optimization.
9350 if (!TRI->hasStackRealignment(MF))
9351 if (MaybeAlign StackAlign = DL.getStackAlignment())
9352 NewAlign = std::min(NewAlign, *StackAlign);
9353
9354 if (NewAlign > Alignment) {
9355 // Give the stack frame object a larger alignment if needed.
9356 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9357 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9358 Alignment = NewAlign;
9359 }
9360 }
9361
9362 // Prepare AAInfo for loads/stores after lowering this memmove.
9363 AAMDNodes NewAAInfo = AAInfo;
9364 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9365
9366 MachineMemOperand::Flags MMOFlags =
9368 uint64_t SrcOff = 0;
9369 SmallVector<SDValue, 8> LoadValues;
9370 SmallVector<SDValue, 8> LoadChains;
9371 SmallVector<SDValue, 8> OutChains;
9372 unsigned NumMemOps = MemOps.size();
9373 for (unsigned i = 0; i < NumMemOps; i++) {
9374 EVT VT = MemOps[i];
9375 unsigned VTSize = VT.getSizeInBits() / 8;
9376 SDValue Value;
9377 bool IsOverlapping = false;
9378
9379 if (i == NumMemOps - 1 && i != 0 && VTSize > Size - SrcOff) {
9380 // Issuing an unaligned load / store pair that overlaps with the previous
9381 // pair. Adjust the offset accordingly.
9382 SrcOff = Size - VTSize;
9383 IsOverlapping = true;
9384 }
9385
9386 // Calculate the actual alignment at the current offset. The alignment at
9387 // SrcOff may be lower than the base alignment, especially when using
9388 // overlapping loads.
9389 Align SrcAlignAtOffset = commonAlignment(*SrcAlign, SrcOff);
9390 if (IsOverlapping) {
9391 // Verify that the target allows misaligned memory accesses at the
9392 // adjusted offset when using overlapping loads.
9393 unsigned Fast;
9394 if (!TLI.allowsMisalignedMemoryAccesses(VT, SrcPtrInfo.getAddrSpace(),
9395 SrcAlignAtOffset, MMOFlags,
9396 &Fast) ||
9397 !Fast) {
9398 // This should have been caught by findOptimalMemOpLowering, but verify
9399 // here for safety.
9400 return SDValue();
9401 }
9402 }
9403
9404 bool isDereferenceable =
9405 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9406 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9407 if (isDereferenceable)
9409 Value =
9410 DAG.getLoad(VT, dl, Chain,
9411 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9412 SrcPtrInfo.getWithOffset(SrcOff), SrcAlignAtOffset,
9413 SrcMMOFlags, NewAAInfo);
9414 LoadValues.push_back(Value);
9415 LoadChains.push_back(Value.getValue(1));
9416 SrcOff += VTSize;
9417 }
9418 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
9419 OutChains.clear();
9420 uint64_t DstOff = 0;
9421 for (unsigned i = 0; i < NumMemOps; i++) {
9422 EVT VT = MemOps[i];
9423 unsigned VTSize = VT.getSizeInBits() / 8;
9424 SDValue Store;
9425 bool IsOverlapping = false;
9426
9427 if (i == NumMemOps - 1 && i != 0 && VTSize > Size - DstOff) {
9428 // Issuing an unaligned load / store pair that overlaps with the previous
9429 // pair. Adjust the offset accordingly.
9430 DstOff = Size - VTSize;
9431 IsOverlapping = true;
9432 }
9433
9434 // Calculate the actual alignment at the current offset. The alignment at
9435 // DstOff may be lower than the base alignment, especially when using
9436 // overlapping stores.
9437 Align DstAlignAtOffset = commonAlignment(Alignment, DstOff);
9438 if (IsOverlapping) {
9439 // Verify that the target allows misaligned memory accesses at the
9440 // adjusted offset when using overlapping stores.
9441 unsigned Fast;
9442 if (!TLI.allowsMisalignedMemoryAccesses(VT, DstPtrInfo.getAddrSpace(),
9443 DstAlignAtOffset, MMOFlags,
9444 &Fast) ||
9445 !Fast) {
9446 // This should have been caught by findOptimalMemOpLowering, but verify
9447 // here for safety.
9448 return SDValue();
9449 }
9450 }
9451 Store = DAG.getStore(
9452 Chain, dl, LoadValues[i],
9453 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9454 DstPtrInfo.getWithOffset(DstOff), DstAlignAtOffset, MMOFlags,
9455 NewAAInfo);
9456 OutChains.push_back(Store);
9457 DstOff += VTSize;
9458 }
9459
9460 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9461}
9462
9463/// Lower the call to 'memset' intrinsic function into a series of store
9464/// operations.
9465///
9466/// \param DAG Selection DAG where lowered code is placed.
9467/// \param dl Link to corresponding IR location.
9468/// \param Chain Control flow dependency.
9469/// \param Dst Pointer to destination memory location.
9470/// \param Src Value of byte to write into the memory.
9471/// \param Size Number of bytes to write.
9472/// \param Alignment Alignment of the destination in bytes.
9473/// \param isVol True if destination is volatile.
9474/// \param AlwaysInline Makes sure no function call is generated.
9475/// \param DstPtrInfo IR information on the memory pointer.
9476/// \returns New head in the control flow, if lowering was successful, empty
9477/// SDValue otherwise.
9478///
9479/// The function tries to replace 'llvm.memset' intrinsic with several store
9480/// operations and value calculation code. This is usually profitable for small
9481/// memory size or when the semantic requires inlining.
9483 SDValue Chain, SDValue Dst, SDValue Src,
9484 uint64_t Size, Align Alignment, bool isVol,
9485 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9486 const AAMDNodes &AAInfo) {
9487 // Turn a memset of undef to nop.
9488 // FIXME: We need to honor volatile even is Src is undef.
9489 if (Src.isUndef())
9490 return Chain;
9491
9492 // Expand memset to a series of load/store ops if the size operand
9493 // falls below a certain threshold.
9494 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9495 std::vector<EVT> MemOps;
9496 bool DstAlignCanChange = false;
9497 LLVMContext &C = *DAG.getContext();
9499 MachineFrameInfo &MFI = MF.getFrameInfo();
9500 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9502 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9503 DstAlignCanChange = true;
9504 bool IsZeroVal = isNullConstant(Src);
9505 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9506
9507 EVT LargestVT;
9508 if (!TLI.findOptimalMemOpLowering(
9509 C, MemOps, Limit,
9510 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9511 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes(),
9512 &LargestVT))
9513 return SDValue();
9514
9515 if (DstAlignCanChange) {
9516 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9517 const DataLayout &DL = DAG.getDataLayout();
9518 Align NewAlign = DL.getABITypeAlign(Ty);
9519
9520 // Don't promote to an alignment that would require dynamic stack
9521 // realignment which may conflict with optimizations such as tail call
9522 // optimization.
9524 if (!TRI->hasStackRealignment(MF))
9525 if (MaybeAlign StackAlign = DL.getStackAlignment())
9526 NewAlign = std::min(NewAlign, *StackAlign);
9527
9528 if (NewAlign > Alignment) {
9529 // Give the stack frame object a larger alignment if needed.
9530 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9531 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9532 Alignment = NewAlign;
9533 }
9534 }
9535
9536 SmallVector<SDValue, 8> OutChains;
9537 uint64_t DstOff = 0;
9538 unsigned NumMemOps = MemOps.size();
9539
9540 // Find the largest store and generate the bit pattern for it.
9541 // If target didn't set LargestVT, compute it from MemOps.
9542 if (!LargestVT.isSimple()) {
9543 LargestVT = MemOps[0];
9544 for (unsigned i = 1; i < NumMemOps; i++)
9545 if (MemOps[i].bitsGT(LargestVT))
9546 LargestVT = MemOps[i];
9547 }
9548 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9549
9550 // Prepare AAInfo for loads/stores after lowering this memset.
9551 AAMDNodes NewAAInfo = AAInfo;
9552 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9553
9554 for (unsigned i = 0; i < NumMemOps; i++) {
9555 EVT VT = MemOps[i];
9556 unsigned VTSize = VT.getSizeInBits() / 8;
9557 // The target should specify store types that exactly cover the memset size
9558 // (with the last store potentially being oversized for overlapping stores).
9559 assert(Size > 0 && "Target specified more stores than needed in "
9560 "findOptimalMemOpLowering");
9561 if (VTSize > Size) {
9562 // Issuing an unaligned load / store pair that overlaps with the previous
9563 // pair. Adjust the offset accordingly.
9564 assert(i == NumMemOps-1 && i != 0);
9565 DstOff -= VTSize - Size;
9566 }
9567
9568 // If this store is smaller than the largest store see whether we can get
9569 // the smaller value for free with a truncate or extract vector element and
9570 // then store.
9571 SDValue Value = MemSetValue;
9572 if (VT.bitsLT(LargestVT)) {
9573 unsigned Index;
9574 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9575 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9576 if (!LargestVT.isVector() && !VT.isVector() &&
9577 TLI.isTruncateFree(LargestVT, VT))
9578 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9579 else if (LargestVT.isVector() && !VT.isVector() &&
9581 LargestVT.getTypeForEVT(*DAG.getContext()),
9582 VT.getSizeInBits(), Index) &&
9583 TLI.isTypeLegal(SVT) &&
9584 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9585 // Target which can combine store(extractelement VectorTy, Idx) can get
9586 // the smaller value for free.
9587 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9588 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9589 } else
9590 Value = getMemsetValue(Src, VT, DAG, dl);
9591 }
9592 assert(Value.getValueType() == VT && "Value with wrong type.");
9593 SDValue Store = DAG.getStore(
9594 Chain, dl, Value,
9595 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9596 DstPtrInfo.getWithOffset(DstOff), Alignment,
9598 NewAAInfo);
9599 OutChains.push_back(Store);
9600 DstOff += VT.getSizeInBits() / 8;
9601 // For oversized overlapping stores, only subtract the remaining bytes.
9602 // For normal stores, subtract the full store size.
9603 if (VTSize > Size) {
9604 Size = 0;
9605 } else {
9606 Size -= VTSize;
9607 }
9608 }
9609
9610 // After processing all stores, Size should be exactly 0. Any remaining bytes
9611 // indicate a bug in the target's findOptimalMemOpLowering implementation.
9612 assert(Size == 0 && "Target's findOptimalMemOpLowering did not specify "
9613 "stores that exactly cover the memset size");
9614
9615 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9616}
9617
9619 unsigned AS) {
9620 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9621 // pointer operands can be losslessly bitcasted to pointers of address space 0
9622 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9623 report_fatal_error("cannot lower memory intrinsic in address space " +
9624 Twine(AS));
9625 }
9626}
9627
9629 const SelectionDAG *SelDAG,
9630 bool AllowReturnsFirstArg) {
9631 if (!CI || !CI->isTailCall())
9632 return false;
9633 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9634 // helper symbol we lower to.
9635 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9636 AllowReturnsFirstArg &&
9638}
9639
9640static std::pair<SDValue, SDValue>
9643 const CallInst *CI, RTLIB::Libcall Call,
9644 SelectionDAG *DAG, const TargetLowering *TLI) {
9645 RTLIB::LibcallImpl LCImpl = DAG->getLibcalls().getLibcallImpl(Call);
9646
9647 if (LCImpl == RTLIB::Unsupported)
9648 return {};
9649
9651 bool IsTailCall =
9652 isInTailCallPositionWrapper(CI, DAG, /*AllowReturnsFirstArg=*/true);
9653 SDValue Callee =
9654 DAG->getExternalSymbol(LCImpl, TLI->getPointerTy(DAG->getDataLayout()));
9655
9656 CLI.setDebugLoc(dl)
9657 .setChain(Chain)
9659 CI->getType(), Callee, std::move(Args))
9660 .setTailCall(IsTailCall);
9661
9662 return TLI->LowerCallTo(CLI);
9663}
9664
9665std::pair<SDValue, SDValue> SelectionDAG::getStrcmp(SDValue Chain,
9666 const SDLoc &dl, SDValue S1,
9667 SDValue S2,
9668 const CallInst *CI) {
9670 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9671 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9672 RTLIB::STRCMP, this, TLI);
9673}
9674
9675std::pair<SDValue, SDValue> SelectionDAG::getStrstr(SDValue Chain,
9676 const SDLoc &dl, SDValue S1,
9677 SDValue S2,
9678 const CallInst *CI) {
9680 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9681 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9682 RTLIB::STRSTR, this, TLI);
9683}
9684
9685std::pair<SDValue, SDValue> SelectionDAG::getMemccpy(SDValue Chain,
9686 const SDLoc &dl,
9687 SDValue Dst, SDValue Src,
9689 const CallInst *CI) {
9691
9693 {Dst, PT},
9694 {Src, PT},
9697 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9698 RTLIB::MEMCCPY, this, TLI);
9699}
9700
9701std::pair<SDValue, SDValue>
9703 SDValue Mem1, SDValue Size, const CallInst *CI) {
9706 {Mem0, PT},
9707 {Mem1, PT},
9709 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9710 RTLIB::MEMCMP, this, TLI);
9711}
9712
9713std::pair<SDValue, SDValue> SelectionDAG::getStrcpy(SDValue Chain,
9714 const SDLoc &dl,
9715 SDValue Dst, SDValue Src,
9716 const CallInst *CI) {
9718 TargetLowering::ArgListTy Args = {{Dst, PT}, {Src, PT}};
9719 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9720 RTLIB::STRCPY, this, TLI);
9721}
9722
9723std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9724 const SDLoc &dl,
9725 SDValue Src,
9726 const CallInst *CI) {
9727 // Emit a library call.
9730 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9731 RTLIB::STRLEN, this, TLI);
9732}
9733
9735 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9736 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9737 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9738 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9739 BatchAAResults *BatchAA) {
9740 // Check to see if we should lower the memcpy to loads and stores first.
9741 // For cases within the target-specified limits, this is the best choice.
9743 if (ConstantSize) {
9744 // Memcpy with size zero? Just return the original chain.
9745 if (ConstantSize->isZero())
9746 return Chain;
9747
9749 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9750 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9751 if (Result.getNode())
9752 return Result;
9753 }
9754
9755 // Then check to see if we should lower the memcpy with target-specific
9756 // code. If the target chooses to do this, this is the next best.
9757 if (TSI) {
9758 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9759 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9760 DstPtrInfo, SrcPtrInfo);
9761 if (Result.getNode())
9762 return Result;
9763 }
9764
9765 // If we really need inline code and the target declined to provide it,
9766 // use a (potentially long) sequence of loads and stores.
9767 if (AlwaysInline) {
9768 assert(ConstantSize && "AlwaysInline requires a constant size!");
9770 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9771 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9772 }
9773
9776
9777 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9778 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9779 // respect volatile, so they may do things like read or write memory
9780 // beyond the given memory regions. But fixing this isn't easy, and most
9781 // people don't care.
9782
9783 // Emit a library call.
9786 Args.emplace_back(Dst, PtrTy);
9787 Args.emplace_back(Src, PtrTy);
9788 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9789 // FIXME: pass in SDLoc
9791 bool IsTailCall = false;
9792 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9793
9794 if (OverrideTailCall.has_value()) {
9795 IsTailCall = *OverrideTailCall;
9796 } else {
9797 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9798 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9799 }
9800
9801 CLI.setDebugLoc(dl)
9802 .setChain(Chain)
9803 .setLibCallee(
9804 Libcalls->getLibcallImplCallingConv(MemCpyImpl),
9805 Dst.getValueType().getTypeForEVT(*getContext()),
9806 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
9807 std::move(Args))
9809 .setTailCall(IsTailCall);
9810
9811 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9812 return CallResult.second;
9813}
9814
9816 SDValue Dst, SDValue Src, SDValue Size,
9817 Type *SizeTy, unsigned ElemSz,
9818 bool isTailCall,
9819 MachinePointerInfo DstPtrInfo,
9820 MachinePointerInfo SrcPtrInfo) {
9821 // Emit a library call.
9824 Args.emplace_back(Dst, ArgTy);
9825 Args.emplace_back(Src, ArgTy);
9826 Args.emplace_back(Size, SizeTy);
9827
9828 RTLIB::Libcall LibraryCall =
9830 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9831 if (LibcallImpl == RTLIB::Unsupported)
9832 report_fatal_error("Unsupported element size");
9833
9835 CLI.setDebugLoc(dl)
9836 .setChain(Chain)
9837 .setLibCallee(
9838 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9840 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9841 std::move(Args))
9843 .setTailCall(isTailCall);
9844
9845 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9846 return CallResult.second;
9847}
9848
9850 SDValue Src, SDValue Size, Align Alignment,
9851 bool isVol, const CallInst *CI,
9852 std::optional<bool> OverrideTailCall,
9853 MachinePointerInfo DstPtrInfo,
9854 MachinePointerInfo SrcPtrInfo,
9855 const AAMDNodes &AAInfo,
9856 BatchAAResults *BatchAA) {
9857 // Check to see if we should lower the memmove to loads and stores first.
9858 // For cases within the target-specified limits, this is the best choice.
9860 if (ConstantSize) {
9861 // Memmove with size zero? Just return the original chain.
9862 if (ConstantSize->isZero())
9863 return Chain;
9864
9866 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9867 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9868 if (Result.getNode())
9869 return Result;
9870 }
9871
9872 // Then check to see if we should lower the memmove with target-specific
9873 // code. If the target chooses to do this, this is the next best.
9874 if (TSI) {
9875 SDValue Result =
9876 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9877 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9878 if (Result.getNode())
9879 return Result;
9880 }
9881
9884
9885 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9886 // not be safe. See memcpy above for more details.
9887
9888 // Emit a library call.
9891 Args.emplace_back(Dst, PtrTy);
9892 Args.emplace_back(Src, PtrTy);
9893 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9894 // FIXME: pass in SDLoc
9896
9897 RTLIB::LibcallImpl MemmoveImpl = Libcalls->getLibcallImpl(RTLIB::MEMMOVE);
9898
9899 bool IsTailCall = false;
9900 if (OverrideTailCall.has_value()) {
9901 IsTailCall = *OverrideTailCall;
9902 } else {
9903 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9904 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9905 }
9906
9907 CLI.setDebugLoc(dl)
9908 .setChain(Chain)
9909 .setLibCallee(
9910 Libcalls->getLibcallImplCallingConv(MemmoveImpl),
9911 Dst.getValueType().getTypeForEVT(*getContext()),
9912 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
9913 std::move(Args))
9915 .setTailCall(IsTailCall);
9916
9917 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9918 return CallResult.second;
9919}
9920
9922 SDValue Dst, SDValue Src, SDValue Size,
9923 Type *SizeTy, unsigned ElemSz,
9924 bool isTailCall,
9925 MachinePointerInfo DstPtrInfo,
9926 MachinePointerInfo SrcPtrInfo) {
9927 // Emit a library call.
9929 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9930 Args.emplace_back(Dst, IntPtrTy);
9931 Args.emplace_back(Src, IntPtrTy);
9932 Args.emplace_back(Size, SizeTy);
9933
9934 RTLIB::Libcall LibraryCall =
9936 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9937 if (LibcallImpl == RTLIB::Unsupported)
9938 report_fatal_error("Unsupported element size");
9939
9941 CLI.setDebugLoc(dl)
9942 .setChain(Chain)
9943 .setLibCallee(
9944 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9946 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9947 std::move(Args))
9949 .setTailCall(isTailCall);
9950
9951 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9952 return CallResult.second;
9953}
9954
9956 SDValue Src, SDValue Size, Align Alignment,
9957 bool isVol, bool AlwaysInline,
9958 const CallInst *CI,
9959 MachinePointerInfo DstPtrInfo,
9960 const AAMDNodes &AAInfo) {
9961 // Check to see if we should lower the memset to stores first.
9962 // For cases within the target-specified limits, this is the best choice.
9964 if (ConstantSize) {
9965 // Memset with size zero? Just return the original chain.
9966 if (ConstantSize->isZero())
9967 return Chain;
9968
9969 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9970 ConstantSize->getZExtValue(), Alignment,
9971 isVol, false, DstPtrInfo, AAInfo);
9972
9973 if (Result.getNode())
9974 return Result;
9975 }
9976
9977 // Then check to see if we should lower the memset with target-specific
9978 // code. If the target chooses to do this, this is the next best.
9979 if (TSI) {
9980 SDValue Result = TSI->EmitTargetCodeForMemset(
9981 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9982 if (Result.getNode())
9983 return Result;
9984 }
9985
9986 // If we really need inline code and the target declined to provide it,
9987 // use a (potentially long) sequence of loads and stores.
9988 if (AlwaysInline) {
9989 assert(ConstantSize && "AlwaysInline requires a constant size!");
9990 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9991 ConstantSize->getZExtValue(), Alignment,
9992 isVol, true, DstPtrInfo, AAInfo);
9993 assert(Result &&
9994 "getMemsetStores must return a valid sequence when AlwaysInline");
9995 return Result;
9996 }
9997
9999
10000 // Emit a library call.
10001 auto &Ctx = *getContext();
10002 const auto& DL = getDataLayout();
10003
10005 // FIXME: pass in SDLoc
10006 CLI.setDebugLoc(dl).setChain(Chain);
10007
10008 RTLIB::LibcallImpl BzeroImpl = Libcalls->getLibcallImpl(RTLIB::BZERO);
10009 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
10010
10011 // If zeroing out and bzero is present, use it.
10012 if (UseBZero) {
10014 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
10015 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
10016 CLI.setLibCallee(
10017 Libcalls->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
10018 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
10019 } else {
10020 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
10021
10023 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
10024 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
10025 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
10026 CLI.setLibCallee(Libcalls->getLibcallImplCallingConv(MemsetImpl),
10027 Dst.getValueType().getTypeForEVT(Ctx),
10028 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
10029 std::move(Args));
10030 }
10031
10032 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
10033 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
10034
10035 // If we're going to use bzero, make sure not to tail call unless the
10036 // subsequent return doesn't need a value, as bzero doesn't return the first
10037 // arg unlike memset.
10038 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
10039 bool IsTailCall =
10040 CI && CI->isTailCall() &&
10041 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
10042 CLI.setDiscardResult().setTailCall(IsTailCall);
10043
10044 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
10045 return CallResult.second;
10046}
10047
10050 Type *SizeTy, unsigned ElemSz,
10051 bool isTailCall,
10052 MachinePointerInfo DstPtrInfo) {
10053 // Emit a library call.
10055 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
10056 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
10057 Args.emplace_back(Size, SizeTy);
10058
10059 RTLIB::Libcall LibraryCall =
10061 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
10062 if (LibcallImpl == RTLIB::Unsupported)
10063 report_fatal_error("Unsupported element size");
10064
10066 CLI.setDebugLoc(dl)
10067 .setChain(Chain)
10068 .setLibCallee(
10069 Libcalls->getLibcallImplCallingConv(LibcallImpl),
10071 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
10072 std::move(Args))
10074 .setTailCall(isTailCall);
10075
10076 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
10077 return CallResult.second;
10078}
10079
10080SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
10082 MachineMemOperand *MMO,
10083 ISD::LoadExtType ExtType) {
10085 AddNodeIDNode(ID, Opcode, VTList, Ops);
10086 ID.AddInteger(MemVT.getRawBits());
10087 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
10088 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
10089 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10090 ID.AddInteger(MMO->getFlags());
10091 void* IP = nullptr;
10092 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10093 E->refineAlignment(MMO);
10094 E->refineRanges(MMO);
10095 return SDValue(E, 0);
10096 }
10097
10098 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
10099 VTList, MemVT, MMO, ExtType);
10100 createOperands(N, Ops);
10101
10102 CSEMap.InsertNode(N, IP);
10103 InsertNode(N);
10104 SDValue V(N, 0);
10105 NewSDValueDbgMsg(V, "Creating new node: ", this);
10106 return V;
10107}
10108
10110 EVT MemVT, SDVTList VTs, SDValue Chain,
10111 SDValue Ptr, SDValue Cmp, SDValue Swp,
10112 MachineMemOperand *MMO) {
10113 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
10115 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
10116
10117 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
10118 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
10119}
10120
10121SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
10122 SDValue Chain, SDValue Ptr, SDValue Val,
10123 MachineMemOperand *MMO) {
10124 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
10125 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
10126 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
10127 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
10128 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
10129 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
10130 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
10131 Opcode == ISD::ATOMIC_LOAD_FMIN ||
10132 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
10133 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
10134 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
10135 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
10136 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
10137 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
10138 Opcode == ISD::ATOMIC_STORE) &&
10139 "Invalid Atomic Op");
10140
10141 EVT VT = Val.getValueType();
10142
10143 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
10144 getVTList(VT, MVT::Other);
10145 SDValue Ops[] = {Chain, Ptr, Val};
10146 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
10147}
10148
10150 EVT MemVT, EVT VT, SDValue Chain,
10151 SDValue Ptr, MachineMemOperand *MMO) {
10152 SDVTList VTs = getVTList(VT, MVT::Other);
10153 SDValue Ops[] = {Chain, Ptr};
10154 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
10155}
10156
10157/// getMergeValues - Create a MERGE_VALUES node from the given operands.
10159 if (Ops.size() == 1)
10160 return Ops[0];
10161
10163 VTs.reserve(Ops.size());
10164 for (const SDValue &Op : Ops)
10165 VTs.push_back(Op.getValueType());
10166 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
10167}
10168
10170 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
10171 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
10173 const AAMDNodes &AAInfo) {
10174 if (Size.hasValue() && !Size.getValue())
10176
10178 MachineMemOperand *MMO =
10179 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
10180
10181 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
10182}
10183
10185 SDVTList VTList,
10186 ArrayRef<SDValue> Ops, EVT MemVT,
10187 MachineMemOperand *MMO) {
10188 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, ArrayRef(MMO));
10189}
10190
10192 SDVTList VTList,
10193 ArrayRef<SDValue> Ops, EVT MemVT,
10195 assert(!MMOs.empty() && "Must have at least one MMO");
10196 assert(
10197 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
10198 Opcode == ISD::PREFETCH ||
10199 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
10200 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
10201 "Opcode is not a memory-accessing opcode!");
10202
10204 if (MMOs.size() == 1) {
10205 MemRefs = MMOs[0];
10206 } else {
10207 // Allocate: [size_t count][MMO*][MMO*]...
10208 size_t AllocSize =
10209 sizeof(size_t) + MMOs.size() * sizeof(MachineMemOperand *);
10210 void *Buffer = Allocator.Allocate(AllocSize, alignof(size_t));
10211 size_t *CountPtr = static_cast<size_t *>(Buffer);
10212 *CountPtr = MMOs.size();
10213 MachineMemOperand **Array =
10214 reinterpret_cast<MachineMemOperand **>(CountPtr + 1);
10215 llvm::copy(MMOs, Array);
10216 MemRefs = Array;
10217 }
10218
10219 // Memoize the node unless it returns a glue result.
10221 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10223 AddNodeIDNode(ID, Opcode, VTList, Ops);
10224 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
10225 Opcode, dl.getIROrder(), VTList, MemVT, MemRefs));
10226 ID.AddInteger(MemVT.getRawBits());
10227 for (const MachineMemOperand *MMO : MMOs) {
10228 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10229 ID.AddInteger(MMO->getFlags());
10230 }
10231 void *IP = nullptr;
10232 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10233 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMOs);
10234 return SDValue(E, 0);
10235 }
10236
10237 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
10238 VTList, MemVT, MemRefs);
10239 createOperands(N, Ops);
10240 CSEMap.InsertNode(N, IP);
10241 } else {
10242 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
10243 VTList, MemVT, MemRefs);
10244 createOperands(N, Ops);
10245 }
10246 InsertNode(N);
10247 SDValue V(N, 0);
10248 NewSDValueDbgMsg(V, "Creating new node: ", this);
10249 return V;
10250}
10251
10253 SDValue Chain, int FrameIndex) {
10254 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
10255 const auto VTs = getVTList(MVT::Other);
10256 SDValue Ops[2] = {
10257 Chain,
10258 getFrameIndex(FrameIndex,
10259 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
10260 true)};
10261
10263 AddNodeIDNode(ID, Opcode, VTs, Ops);
10264 ID.AddInteger(FrameIndex);
10265 void *IP = nullptr;
10266 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10267 return SDValue(E, 0);
10268
10269 LifetimeSDNode *N =
10270 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
10271 createOperands(N, Ops);
10272 CSEMap.InsertNode(N, IP);
10273 InsertNode(N);
10274 SDValue V(N, 0);
10275 NewSDValueDbgMsg(V, "Creating new node: ", this);
10276 return V;
10277}
10278
10280 uint64_t Guid, uint64_t Index,
10281 uint32_t Attr) {
10282 const unsigned Opcode = ISD::PSEUDO_PROBE;
10283 const auto VTs = getVTList(MVT::Other);
10284 SDValue Ops[] = {Chain};
10286 AddNodeIDNode(ID, Opcode, VTs, Ops);
10287 ID.AddInteger(Guid);
10288 ID.AddInteger(Index);
10289 void *IP = nullptr;
10290 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
10291 return SDValue(E, 0);
10292
10293 auto *N = newSDNode<PseudoProbeSDNode>(
10294 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
10295 createOperands(N, Ops);
10296 CSEMap.InsertNode(N, IP);
10297 InsertNode(N);
10298 SDValue V(N, 0);
10299 NewSDValueDbgMsg(V, "Creating new node: ", this);
10300 return V;
10301}
10302
10303/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
10304/// MachinePointerInfo record from it. This is particularly useful because the
10305/// code generator has many cases where it doesn't bother passing in a
10306/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
10308 SelectionDAG &DAG, SDValue Ptr,
10309 int64_t Offset = 0) {
10310 // If this is FI+Offset, we can model it.
10311 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
10313 FI->getIndex(), Offset);
10314
10315 // If this is (FI+Offset1)+Offset2, we can model it.
10316 if (Ptr.getOpcode() != ISD::ADD ||
10319 return Info;
10320
10321 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
10323 DAG.getMachineFunction(), FI,
10324 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
10325}
10326
10327/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
10328/// MachinePointerInfo record from it. This is particularly useful because the
10329/// code generator has many cases where it doesn't bother passing in a
10330/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
10332 SelectionDAG &DAG, SDValue Ptr,
10333 SDValue OffsetOp) {
10334 // If the 'Offset' value isn't a constant, we can't handle this.
10336 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
10337 if (OffsetOp.isUndef())
10338 return InferPointerInfo(Info, DAG, Ptr);
10339 return Info;
10340}
10341
10343 EVT VT, const SDLoc &dl, SDValue Chain,
10344 SDValue Ptr, SDValue Offset,
10345 MachinePointerInfo PtrInfo, EVT MemVT,
10346 Align Alignment,
10347 MachineMemOperand::Flags MMOFlags,
10348 const AAMDNodes &AAInfo, const MDNode *Ranges) {
10349 assert(Chain.getValueType() == MVT::Other &&
10350 "Invalid chain type");
10351
10352 MMOFlags |= MachineMemOperand::MOLoad;
10353 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10354 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10355 // clients.
10356 if (PtrInfo.V.isNull())
10357 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10358
10359 TypeSize Size = MemVT.getStoreSize();
10361 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10362 Alignment, AAInfo, Ranges);
10363 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
10364}
10365
10367 EVT VT, const SDLoc &dl, SDValue Chain,
10368 SDValue Ptr, SDValue Offset, EVT MemVT,
10369 MachineMemOperand *MMO) {
10370 if (VT == MemVT) {
10371 ExtType = ISD::NON_EXTLOAD;
10372 } else if (ExtType == ISD::NON_EXTLOAD) {
10373 assert(VT == MemVT && "Non-extending load from different memory type!");
10374 } else {
10375 // Extending load.
10376 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
10377 "Should only be an extending load, not truncating!");
10378 assert(VT.isInteger() == MemVT.isInteger() &&
10379 "Cannot convert from FP to Int or Int -> FP!");
10380 assert(VT.isVector() == MemVT.isVector() &&
10381 "Cannot use an ext load to convert to or from a vector!");
10382 assert((!VT.isVector() ||
10384 "Cannot use an ext load to change the number of vector elements!");
10385 }
10386
10387 assert((!MMO->getRanges() ||
10389 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
10390 MemVT.isInteger())) &&
10391 "Range metadata and load type must match!");
10392
10393 bool Indexed = AM != ISD::UNINDEXED;
10394 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10395
10396 SDVTList VTs = Indexed ?
10397 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
10398 SDValue Ops[] = { Chain, Ptr, Offset };
10400 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
10401 ID.AddInteger(MemVT.getRawBits());
10402 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
10403 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
10404 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10405 ID.AddInteger(MMO->getFlags());
10406 void *IP = nullptr;
10407 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10408 E->refineAlignment(MMO);
10409 E->refineRanges(MMO);
10410 return SDValue(E, 0);
10411 }
10412 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10413 ExtType, MemVT, MMO);
10414 createOperands(N, Ops);
10415
10416 CSEMap.InsertNode(N, IP);
10417 InsertNode(N);
10418 SDValue V(N, 0);
10419 NewSDValueDbgMsg(V, "Creating new node: ", this);
10420 return V;
10421}
10422
10424 SDValue Ptr, MachinePointerInfo PtrInfo,
10425 MaybeAlign Alignment,
10426 MachineMemOperand::Flags MMOFlags,
10427 const AAMDNodes &AAInfo, const MDNode *Ranges) {
10429 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10430 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
10431}
10432
10434 SDValue Ptr, MachineMemOperand *MMO) {
10436 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10437 VT, MMO);
10438}
10439
10441 EVT VT, SDValue Chain, SDValue Ptr,
10442 MachinePointerInfo PtrInfo, EVT MemVT,
10443 MaybeAlign Alignment,
10444 MachineMemOperand::Flags MMOFlags,
10445 const AAMDNodes &AAInfo) {
10447 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
10448 MemVT, Alignment, MMOFlags, AAInfo);
10449}
10450
10452 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
10453 MachineMemOperand *MMO) {
10455 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
10456 MemVT, MMO);
10457}
10458
10462 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
10463 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10464 // Don't propagate the invariant or dereferenceable flags.
10465 auto MMOFlags =
10466 LD->getMemOperand()->getFlags() &
10468 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10469 LD->getChain(), Base, Offset, LD->getPointerInfo(),
10470 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
10471}
10472
10474 SDValue Ptr, MachinePointerInfo PtrInfo,
10475 Align Alignment,
10476 MachineMemOperand::Flags MMOFlags,
10477 const AAMDNodes &AAInfo) {
10478 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10479
10480 MMOFlags |= MachineMemOperand::MOStore;
10481 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10482
10483 if (PtrInfo.V.isNull())
10484 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10485
10488 MachineMemOperand *MMO =
10489 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
10490 return getStore(Chain, dl, Val, Ptr, MMO);
10491}
10492
10494 SDValue Ptr, MachineMemOperand *MMO) {
10496 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
10498}
10499
10501 SDValue Ptr, SDValue Offset, EVT SVT,
10503 bool IsTruncating) {
10504 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10505 EVT VT = Val.getValueType();
10506 if (VT == SVT) {
10507 IsTruncating = false;
10508 } else if (!IsTruncating) {
10509 assert(VT == SVT && "No-truncating store from different memory type!");
10510 } else {
10512 "Should only be a truncating store, not extending!");
10513 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10514 assert(VT.isVector() == SVT.isVector() &&
10515 "Cannot use trunc store to convert to or from a vector!");
10516 assert((!VT.isVector() ||
10518 "Cannot use trunc store to change the number of vector elements!");
10519 }
10520
10521 bool Indexed = AM != ISD::UNINDEXED;
10522 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
10523 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10524 : getVTList(MVT::Other);
10525 SDValue Ops[] = {Chain, Val, Ptr, Offset};
10528 ID.AddInteger(SVT.getRawBits());
10529 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
10530 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
10531 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10532 ID.AddInteger(MMO->getFlags());
10533 void *IP = nullptr;
10534 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10535 cast<StoreSDNode>(E)->refineAlignment(MMO);
10536 return SDValue(E, 0);
10537 }
10538 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10539 IsTruncating, SVT, MMO);
10540 createOperands(N, Ops);
10541
10542 CSEMap.InsertNode(N, IP);
10543 InsertNode(N);
10544 SDValue V(N, 0);
10545 NewSDValueDbgMsg(V, "Creating new node: ", this);
10546 return V;
10547}
10548
10550 SDValue Ptr, MachinePointerInfo PtrInfo,
10551 EVT SVT, Align Alignment,
10552 MachineMemOperand::Flags MMOFlags,
10553 const AAMDNodes &AAInfo) {
10554 assert(Chain.getValueType() == MVT::Other &&
10555 "Invalid chain type");
10556
10557 MMOFlags |= MachineMemOperand::MOStore;
10558 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10559
10560 if (PtrInfo.V.isNull())
10561 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10562
10564 MachineMemOperand *MMO = MF.getMachineMemOperand(
10565 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10566 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10567}
10568
10570 SDValue Ptr, EVT SVT,
10571 MachineMemOperand *MMO) {
10573 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10574}
10575
10579 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10580 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10581 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10582 ST->getMemoryVT(), ST->getMemOperand(), AM,
10583 ST->isTruncatingStore());
10584}
10585
10587 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10588 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10589 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10590 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10591 const MDNode *Ranges, bool IsExpanding) {
10592 MMOFlags |= MachineMemOperand::MOLoad;
10593 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10594 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10595 // clients.
10596 if (PtrInfo.V.isNull())
10597 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10598
10599 TypeSize Size = MemVT.getStoreSize();
10601 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10602 Alignment, AAInfo, Ranges);
10603 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10604 MMO, IsExpanding);
10605}
10606
10608 ISD::LoadExtType ExtType, EVT VT,
10609 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10610 SDValue Offset, SDValue Mask, SDValue EVL,
10611 EVT MemVT, MachineMemOperand *MMO,
10612 bool IsExpanding) {
10613 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10614 assert(Mask.getValueType().getVectorElementCount() ==
10615 VT.getVectorElementCount() &&
10616 "Vector width mismatch between mask and data");
10617
10618 bool Indexed = AM != ISD::UNINDEXED;
10619 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10620
10621 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10622 : getVTList(VT, MVT::Other);
10623 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10625 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10626 ID.AddInteger(MemVT.getRawBits());
10627 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10628 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10629 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10630 ID.AddInteger(MMO->getFlags());
10631 void *IP = nullptr;
10632 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10633 E->refineAlignment(MMO);
10634 E->refineRanges(MMO);
10635 return SDValue(E, 0);
10636 }
10637 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10638 ExtType, IsExpanding, MemVT, MMO);
10639 createOperands(N, Ops);
10640
10641 CSEMap.InsertNode(N, IP);
10642 InsertNode(N);
10643 SDValue V(N, 0);
10644 NewSDValueDbgMsg(V, "Creating new node: ", this);
10645 return V;
10646}
10647
10649 SDValue Ptr, SDValue Mask, SDValue EVL,
10650 MachinePointerInfo PtrInfo,
10651 MaybeAlign Alignment,
10652 MachineMemOperand::Flags MMOFlags,
10653 const AAMDNodes &AAInfo, const MDNode *Ranges,
10654 bool IsExpanding) {
10656 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10657 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10658 IsExpanding);
10659}
10660
10662 SDValue Ptr, SDValue Mask, SDValue EVL,
10663 MachineMemOperand *MMO, bool IsExpanding) {
10665 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10666 Mask, EVL, VT, MMO, IsExpanding);
10667}
10668
10670 EVT VT, SDValue Chain, SDValue Ptr,
10671 SDValue Mask, SDValue EVL,
10672 MachinePointerInfo PtrInfo, EVT MemVT,
10673 MaybeAlign Alignment,
10674 MachineMemOperand::Flags MMOFlags,
10675 const AAMDNodes &AAInfo, bool IsExpanding) {
10677 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10678 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10679 IsExpanding);
10680}
10681
10683 EVT VT, SDValue Chain, SDValue Ptr,
10684 SDValue Mask, SDValue EVL, EVT MemVT,
10685 MachineMemOperand *MMO, bool IsExpanding) {
10687 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10688 EVL, MemVT, MMO, IsExpanding);
10689}
10690
10694 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10695 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10696 // Don't propagate the invariant or dereferenceable flags.
10697 auto MMOFlags =
10698 LD->getMemOperand()->getFlags() &
10700 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10701 LD->getChain(), Base, Offset, LD->getMask(),
10702 LD->getVectorLength(), LD->getPointerInfo(),
10703 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10704 nullptr, LD->isExpandingLoad());
10705}
10706
10708 SDValue Ptr, SDValue Offset, SDValue Mask,
10709 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10710 ISD::MemIndexedMode AM, bool IsTruncating,
10711 bool IsCompressing) {
10712 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10713 assert(Mask.getValueType().getVectorElementCount() ==
10715 "Vector width mismatch between mask and data");
10716
10717 bool Indexed = AM != ISD::UNINDEXED;
10718 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10719 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10720 : getVTList(MVT::Other);
10721 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10723 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10724 ID.AddInteger(MemVT.getRawBits());
10725 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10726 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10727 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10728 ID.AddInteger(MMO->getFlags());
10729 void *IP = nullptr;
10730 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10731 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10732 return SDValue(E, 0);
10733 }
10734 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10735 IsTruncating, IsCompressing, MemVT, MMO);
10736 createOperands(N, Ops);
10737
10738 CSEMap.InsertNode(N, IP);
10739 InsertNode(N);
10740 SDValue V(N, 0);
10741 NewSDValueDbgMsg(V, "Creating new node: ", this);
10742 return V;
10743}
10744
10746 SDValue Val, SDValue Ptr, SDValue Mask,
10747 SDValue EVL, MachinePointerInfo PtrInfo,
10748 EVT SVT, Align Alignment,
10749 MachineMemOperand::Flags MMOFlags,
10750 const AAMDNodes &AAInfo,
10751 bool IsCompressing) {
10752 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10753
10754 MMOFlags |= MachineMemOperand::MOStore;
10755 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10756
10757 if (PtrInfo.V.isNull())
10758 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10759
10761 MachineMemOperand *MMO = MF.getMachineMemOperand(
10762 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10763 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10764 IsCompressing);
10765}
10766
10768 SDValue Val, SDValue Ptr, SDValue Mask,
10769 SDValue EVL, EVT SVT,
10770 MachineMemOperand *MMO,
10771 bool IsCompressing) {
10772 EVT VT = Val.getValueType();
10773
10774 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10775 if (VT == SVT)
10776 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10777 EVL, VT, MMO, ISD::UNINDEXED,
10778 /*IsTruncating*/ false, IsCompressing);
10779
10781 "Should only be a truncating store, not extending!");
10782 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10783 assert(VT.isVector() == SVT.isVector() &&
10784 "Cannot use trunc store to convert to or from a vector!");
10785 assert((!VT.isVector() ||
10787 "Cannot use trunc store to change the number of vector elements!");
10788
10789 SDVTList VTs = getVTList(MVT::Other);
10791 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10793 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10794 ID.AddInteger(SVT.getRawBits());
10795 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10796 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10797 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10798 ID.AddInteger(MMO->getFlags());
10799 void *IP = nullptr;
10800 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10801 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10802 return SDValue(E, 0);
10803 }
10804 auto *N =
10805 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10806 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10807 createOperands(N, Ops);
10808
10809 CSEMap.InsertNode(N, IP);
10810 InsertNode(N);
10811 SDValue V(N, 0);
10812 NewSDValueDbgMsg(V, "Creating new node: ", this);
10813 return V;
10814}
10815
10819 auto *ST = cast<VPStoreSDNode>(OrigStore);
10820 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10821 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10822 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10823 Offset, ST->getMask(), ST->getVectorLength()};
10825 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10826 ID.AddInteger(ST->getMemoryVT().getRawBits());
10827 ID.AddInteger(ST->getRawSubclassData());
10828 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10829 ID.AddInteger(ST->getMemOperand()->getFlags());
10830 void *IP = nullptr;
10831 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10832 return SDValue(E, 0);
10833
10834 auto *N = newSDNode<VPStoreSDNode>(
10835 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10836 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10837 createOperands(N, Ops);
10838
10839 CSEMap.InsertNode(N, IP);
10840 InsertNode(N);
10841 SDValue V(N, 0);
10842 NewSDValueDbgMsg(V, "Creating new node: ", this);
10843 return V;
10844}
10845
10847 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10848 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10849 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10850 bool Indexed = AM != ISD::UNINDEXED;
10851 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10852
10853 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10854 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10855 : getVTList(VT, MVT::Other);
10857 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10858 ID.AddInteger(VT.getRawBits());
10859 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10860 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10861 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10862
10863 void *IP = nullptr;
10864 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10865 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10866 return SDValue(E, 0);
10867 }
10868
10869 auto *N =
10870 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10871 ExtType, IsExpanding, MemVT, MMO);
10872 createOperands(N, Ops);
10873 CSEMap.InsertNode(N, IP);
10874 InsertNode(N);
10875 SDValue V(N, 0);
10876 NewSDValueDbgMsg(V, "Creating new node: ", this);
10877 return V;
10878}
10879
10881 SDValue Ptr, SDValue Stride,
10882 SDValue Mask, SDValue EVL,
10883 MachineMemOperand *MMO,
10884 bool IsExpanding) {
10886 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10887 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10888}
10889
10891 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10892 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10893 MachineMemOperand *MMO, bool IsExpanding) {
10895 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10896 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10897}
10898
10900 SDValue Val, SDValue Ptr,
10901 SDValue Offset, SDValue Stride,
10902 SDValue Mask, SDValue EVL, EVT MemVT,
10903 MachineMemOperand *MMO,
10905 bool IsTruncating, bool IsCompressing) {
10906 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10907 bool Indexed = AM != ISD::UNINDEXED;
10908 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10909 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10910 : getVTList(MVT::Other);
10911 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10913 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10914 ID.AddInteger(MemVT.getRawBits());
10915 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10916 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10917 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10918 void *IP = nullptr;
10919 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10920 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10921 return SDValue(E, 0);
10922 }
10923 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10924 VTs, AM, IsTruncating,
10925 IsCompressing, MemVT, MMO);
10926 createOperands(N, Ops);
10927
10928 CSEMap.InsertNode(N, IP);
10929 InsertNode(N);
10930 SDValue V(N, 0);
10931 NewSDValueDbgMsg(V, "Creating new node: ", this);
10932 return V;
10933}
10934
10936 SDValue Val, SDValue Ptr,
10937 SDValue Stride, SDValue Mask,
10938 SDValue EVL, EVT SVT,
10939 MachineMemOperand *MMO,
10940 bool IsCompressing) {
10941 EVT VT = Val.getValueType();
10942
10943 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10944 if (VT == SVT)
10945 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10946 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10947 /*IsTruncating*/ false, IsCompressing);
10948
10950 "Should only be a truncating store, not extending!");
10951 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10952 assert(VT.isVector() == SVT.isVector() &&
10953 "Cannot use trunc store to convert to or from a vector!");
10954 assert((!VT.isVector() ||
10956 "Cannot use trunc store to change the number of vector elements!");
10957
10958 SDVTList VTs = getVTList(MVT::Other);
10960 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10962 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10963 ID.AddInteger(SVT.getRawBits());
10964 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10965 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10966 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10967 void *IP = nullptr;
10968 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10969 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10970 return SDValue(E, 0);
10971 }
10972 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10973 VTs, ISD::UNINDEXED, true,
10974 IsCompressing, SVT, MMO);
10975 createOperands(N, Ops);
10976
10977 CSEMap.InsertNode(N, IP);
10978 InsertNode(N);
10979 SDValue V(N, 0);
10980 NewSDValueDbgMsg(V, "Creating new node: ", this);
10981 return V;
10982}
10983
10986 ISD::MemIndexType IndexType) {
10987 assert(Ops.size() == 6 && "Incompatible number of operands");
10988
10990 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10991 ID.AddInteger(VT.getRawBits());
10992 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10993 dl.getIROrder(), VTs, VT, MMO, IndexType));
10994 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10995 ID.AddInteger(MMO->getFlags());
10996 void *IP = nullptr;
10997 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10998 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10999 return SDValue(E, 0);
11000 }
11001
11002 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11003 VT, MMO, IndexType);
11004 createOperands(N, Ops);
11005
11006 assert(N->getMask().getValueType().getVectorElementCount() ==
11007 N->getValueType(0).getVectorElementCount() &&
11008 "Vector width mismatch between mask and data");
11009 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11010 N->getValueType(0).getVectorElementCount().isScalable() &&
11011 "Scalable flags of index and data do not match");
11013 N->getIndex().getValueType().getVectorElementCount(),
11014 N->getValueType(0).getVectorElementCount()) &&
11015 "Vector width mismatch between index and data");
11016 assert(isa<ConstantSDNode>(N->getScale()) &&
11017 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11018 "Scale should be a constant power of 2");
11019
11020 CSEMap.InsertNode(N, IP);
11021 InsertNode(N);
11022 SDValue V(N, 0);
11023 NewSDValueDbgMsg(V, "Creating new node: ", this);
11024 return V;
11025}
11026
11029 MachineMemOperand *MMO,
11030 ISD::MemIndexType IndexType) {
11031 assert(Ops.size() == 7 && "Incompatible number of operands");
11032
11034 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
11035 ID.AddInteger(VT.getRawBits());
11036 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
11037 dl.getIROrder(), VTs, VT, MMO, IndexType));
11038 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11039 ID.AddInteger(MMO->getFlags());
11040 void *IP = nullptr;
11041 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11042 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
11043 return SDValue(E, 0);
11044 }
11045 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11046 VT, MMO, IndexType);
11047 createOperands(N, Ops);
11048
11049 assert(N->getMask().getValueType().getVectorElementCount() ==
11050 N->getValue().getValueType().getVectorElementCount() &&
11051 "Vector width mismatch between mask and data");
11052 assert(
11053 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11054 N->getValue().getValueType().getVectorElementCount().isScalable() &&
11055 "Scalable flags of index and data do not match");
11057 N->getIndex().getValueType().getVectorElementCount(),
11058 N->getValue().getValueType().getVectorElementCount()) &&
11059 "Vector width mismatch between index and data");
11060 assert(isa<ConstantSDNode>(N->getScale()) &&
11061 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11062 "Scale should be a constant power of 2");
11063
11064 CSEMap.InsertNode(N, IP);
11065 InsertNode(N);
11066 SDValue V(N, 0);
11067 NewSDValueDbgMsg(V, "Creating new node: ", this);
11068 return V;
11069}
11070
11073 SDValue PassThru, EVT MemVT,
11074 MachineMemOperand *MMO,
11076 ISD::LoadExtType ExtTy, bool isExpanding) {
11077 bool Indexed = AM != ISD::UNINDEXED;
11078 assert((Indexed || Offset.isUndef()) &&
11079 "Unindexed masked load with an offset!");
11080 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
11081 : getVTList(VT, MVT::Other);
11082 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
11085 ID.AddInteger(MemVT.getRawBits());
11086 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
11087 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
11088 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11089 ID.AddInteger(MMO->getFlags());
11090 void *IP = nullptr;
11091 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11092 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
11093 return SDValue(E, 0);
11094 }
11095 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11096 AM, ExtTy, isExpanding, MemVT, MMO);
11097 createOperands(N, Ops);
11098
11099 CSEMap.InsertNode(N, IP);
11100 InsertNode(N);
11101 SDValue V(N, 0);
11102 NewSDValueDbgMsg(V, "Creating new node: ", this);
11103 return V;
11104}
11105
11110 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
11111 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
11112 Offset, LD->getMask(), LD->getPassThru(),
11113 LD->getMemoryVT(), LD->getMemOperand(), AM,
11114 LD->getExtensionType(), LD->isExpandingLoad());
11115}
11116
11119 SDValue Mask, EVT MemVT,
11120 MachineMemOperand *MMO,
11121 ISD::MemIndexedMode AM, bool IsTruncating,
11122 bool IsCompressing) {
11123 assert(Chain.getValueType() == MVT::Other &&
11124 "Invalid chain type");
11125 bool Indexed = AM != ISD::UNINDEXED;
11126 assert((Indexed || Offset.isUndef()) &&
11127 "Unindexed masked store with an offset!");
11128 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
11129 : getVTList(MVT::Other);
11130 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
11133 ID.AddInteger(MemVT.getRawBits());
11134 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
11135 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
11136 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11137 ID.AddInteger(MMO->getFlags());
11138 void *IP = nullptr;
11139 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11140 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
11141 return SDValue(E, 0);
11142 }
11143 auto *N =
11144 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
11145 IsTruncating, IsCompressing, MemVT, MMO);
11146 createOperands(N, Ops);
11147
11148 CSEMap.InsertNode(N, IP);
11149 InsertNode(N);
11150 SDValue V(N, 0);
11151 NewSDValueDbgMsg(V, "Creating new node: ", this);
11152 return V;
11153}
11154
11159 assert(ST->getOffset().isUndef() &&
11160 "Masked store is already a indexed store!");
11161 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
11162 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
11163 AM, ST->isTruncatingStore(), ST->isCompressingStore());
11164}
11165
11168 MachineMemOperand *MMO,
11169 ISD::MemIndexType IndexType,
11170 ISD::LoadExtType ExtTy) {
11171 assert(Ops.size() == 6 && "Incompatible number of operands");
11172
11175 ID.AddInteger(MemVT.getRawBits());
11176 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
11177 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
11178 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11179 ID.AddInteger(MMO->getFlags());
11180 void *IP = nullptr;
11181 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11182 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
11183 return SDValue(E, 0);
11184 }
11185
11186 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11187 VTs, MemVT, MMO, IndexType, ExtTy);
11188 createOperands(N, Ops);
11189
11190 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
11191 "Incompatible type of the PassThru value in MaskedGatherSDNode");
11192 assert(N->getMask().getValueType().getVectorElementCount() ==
11193 N->getValueType(0).getVectorElementCount() &&
11194 "Vector width mismatch between mask and data");
11195 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11196 N->getValueType(0).getVectorElementCount().isScalable() &&
11197 "Scalable flags of index and data do not match");
11199 N->getIndex().getValueType().getVectorElementCount(),
11200 N->getValueType(0).getVectorElementCount()) &&
11201 "Vector width mismatch between index and data");
11202 assert(isa<ConstantSDNode>(N->getScale()) &&
11203 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11204 "Scale should be a constant power of 2");
11205
11206 CSEMap.InsertNode(N, IP);
11207 InsertNode(N);
11208 SDValue V(N, 0);
11209 NewSDValueDbgMsg(V, "Creating new node: ", this);
11210 return V;
11211}
11212
11215 MachineMemOperand *MMO,
11216 ISD::MemIndexType IndexType,
11217 bool IsTrunc) {
11218 assert(Ops.size() == 6 && "Incompatible number of operands");
11219
11222 ID.AddInteger(MemVT.getRawBits());
11223 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
11224 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
11225 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11226 ID.AddInteger(MMO->getFlags());
11227 void *IP = nullptr;
11228 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11229 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
11230 return SDValue(E, 0);
11231 }
11232
11233 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11234 VTs, MemVT, MMO, IndexType, IsTrunc);
11235 createOperands(N, Ops);
11236
11237 assert(N->getMask().getValueType().getVectorElementCount() ==
11238 N->getValue().getValueType().getVectorElementCount() &&
11239 "Vector width mismatch between mask and data");
11240 assert(
11241 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11242 N->getValue().getValueType().getVectorElementCount().isScalable() &&
11243 "Scalable flags of index and data do not match");
11245 N->getIndex().getValueType().getVectorElementCount(),
11246 N->getValue().getValueType().getVectorElementCount()) &&
11247 "Vector width mismatch between index and data");
11248 assert(isa<ConstantSDNode>(N->getScale()) &&
11249 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11250 "Scale should be a constant power of 2");
11251
11252 CSEMap.InsertNode(N, IP);
11253 InsertNode(N);
11254 SDValue V(N, 0);
11255 NewSDValueDbgMsg(V, "Creating new node: ", this);
11256 return V;
11257}
11258
11260 const SDLoc &dl, ArrayRef<SDValue> Ops,
11261 MachineMemOperand *MMO,
11262 ISD::MemIndexType IndexType) {
11263 assert(Ops.size() == 7 && "Incompatible number of operands");
11264
11267 ID.AddInteger(MemVT.getRawBits());
11268 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
11269 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
11270 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11271 ID.AddInteger(MMO->getFlags());
11272 void *IP = nullptr;
11273 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11274 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
11275 return SDValue(E, 0);
11276 }
11277
11278 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11279 VTs, MemVT, MMO, IndexType);
11280 createOperands(N, Ops);
11281
11282 assert(N->getMask().getValueType().getVectorElementCount() ==
11283 N->getIndex().getValueType().getVectorElementCount() &&
11284 "Vector width mismatch between mask and data");
11285 assert(isa<ConstantSDNode>(N->getScale()) &&
11286 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11287 "Scale should be a constant power of 2");
11288 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
11289
11290 CSEMap.InsertNode(N, IP);
11291 InsertNode(N);
11292 SDValue V(N, 0);
11293 NewSDValueDbgMsg(V, "Creating new node: ", this);
11294 return V;
11295}
11296
11298 SDValue Ptr, SDValue Mask, SDValue EVL,
11299 MachineMemOperand *MMO) {
11300 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
11301 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
11303 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
11304 ID.AddInteger(VT.getRawBits());
11305 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
11306 VTs, VT, MMO));
11307 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11308 ID.AddInteger(MMO->getFlags());
11309 void *IP = nullptr;
11310 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11311 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
11312 return SDValue(E, 0);
11313 }
11314 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
11315 VT, MMO);
11316 createOperands(N, Ops);
11317
11318 CSEMap.InsertNode(N, IP);
11319 InsertNode(N);
11320 SDValue V(N, 0);
11321 NewSDValueDbgMsg(V, "Creating new node: ", this);
11322 return V;
11323}
11324
11326 EVT MemVT, MachineMemOperand *MMO) {
11327 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11328 SDVTList VTs = getVTList(MVT::Other);
11329 SDValue Ops[] = {Chain, Ptr};
11332 ID.AddInteger(MemVT.getRawBits());
11333 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
11334 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
11335 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11336 ID.AddInteger(MMO->getFlags());
11337 void *IP = nullptr;
11338 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
11339 return SDValue(E, 0);
11340
11341 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
11342 dl.getDebugLoc(), VTs, MemVT, MMO);
11343 createOperands(N, Ops);
11344
11345 CSEMap.InsertNode(N, IP);
11346 InsertNode(N);
11347 SDValue V(N, 0);
11348 NewSDValueDbgMsg(V, "Creating new node: ", this);
11349 return V;
11350}
11351
11353 EVT MemVT, MachineMemOperand *MMO) {
11354 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11355 SDVTList VTs = getVTList(MVT::Other);
11356 SDValue Ops[] = {Chain, Ptr};
11359 ID.AddInteger(MemVT.getRawBits());
11360 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
11361 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
11362 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11363 ID.AddInteger(MMO->getFlags());
11364 void *IP = nullptr;
11365 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
11366 return SDValue(E, 0);
11367
11368 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
11369 dl.getDebugLoc(), VTs, MemVT, MMO);
11370 createOperands(N, Ops);
11371
11372 CSEMap.InsertNode(N, IP);
11373 InsertNode(N);
11374 SDValue V(N, 0);
11375 NewSDValueDbgMsg(V, "Creating new node: ", this);
11376 return V;
11377}
11378
11380 // select undef, T, F --> T (if T is a constant), otherwise F
11381 // select, ?, undef, F --> F
11382 // select, ?, T, undef --> T
11383 if (Cond.isUndef())
11384 return isConstantValueOfAnyType(T) ? T : F;
11385 if (T.isUndef())
11387 if (F.isUndef())
11389
11390 // select true, T, F --> T
11391 // select false, T, F --> F
11392 if (auto C = isBoolConstant(Cond))
11393 return *C ? T : F;
11394
11395 // select ?, T, T --> T
11396 if (T == F)
11397 return T;
11398
11399 return SDValue();
11400}
11401
11403 // shift undef, Y --> 0 (can always assume that the undef value is 0)
11404 if (X.isUndef())
11405 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
11406 // shift X, undef --> undef (because it may shift by the bitwidth)
11407 if (Y.isUndef())
11408 return getUNDEF(X.getValueType());
11409
11410 // shift 0, Y --> 0
11411 // shift X, 0 --> X
11413 return X;
11414
11415 // shift X, C >= bitwidth(X) --> undef
11416 // All vector elements must be too big (or undef) to avoid partial undefs.
11417 auto isShiftTooBig = [X](ConstantSDNode *Val) {
11418 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
11419 };
11420 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
11421 return getUNDEF(X.getValueType());
11422
11423 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
11424 if (X.getValueType().getScalarType() == MVT::i1)
11425 return X;
11426
11427 return SDValue();
11428}
11429
11431 SDNodeFlags Flags) {
11432 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
11433 // (an undef operand can be chosen to be Nan/Inf), then the result of this
11434 // operation is poison. That result can be relaxed to undef.
11435 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
11436 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
11437 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
11438 (YC && YC->getValueAPF().isNaN());
11439 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
11440 (YC && YC->getValueAPF().isInfinity());
11441
11442 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
11443 return getUNDEF(X.getValueType());
11444
11445 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
11446 return getUNDEF(X.getValueType());
11447
11448 if (!YC)
11449 return SDValue();
11450
11451 // X + -0.0 --> X
11452 if (Opcode == ISD::FADD)
11453 if (YC->getValueAPF().isNegZero())
11454 return X;
11455
11456 // X - +0.0 --> X
11457 if (Opcode == ISD::FSUB)
11458 if (YC->getValueAPF().isPosZero())
11459 return X;
11460
11461 // X * 1.0 --> X
11462 // X / 1.0 --> X
11463 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
11464 if (YC->getValueAPF().isExactlyValue(1.0))
11465 return X;
11466
11467 // X * 0.0 --> 0.0
11468 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
11469 if (YC->getValueAPF().isZero())
11470 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
11471
11472 return SDValue();
11473}
11474
11476 SDValue Ptr, SDValue SV, unsigned Align) {
11477 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
11478 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
11479}
11480
11481SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11483 switch (Ops.size()) {
11484 case 0: return getNode(Opcode, DL, VT);
11485 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
11486 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
11487 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
11488 default: break;
11489 }
11490
11491 // Copy from an SDUse array into an SDValue array for use with
11492 // the regular getNode logic.
11494 return getNode(Opcode, DL, VT, NewOps);
11495}
11496
11497SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11499 SDNodeFlags Flags;
11500 if (Inserter)
11501 Flags = Inserter->getFlags();
11502 return getNode(Opcode, DL, VT, Ops, Flags);
11503}
11504
11505SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11506 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11507 unsigned NumOps = Ops.size();
11508 switch (NumOps) {
11509 case 0: return getNode(Opcode, DL, VT);
11510 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
11511 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
11512 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
11513 default: break;
11514 }
11515
11516#ifndef NDEBUG
11517 for (const auto &Op : Ops)
11518 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11519 "Operand is DELETED_NODE!");
11520#endif
11521
11522 switch (Opcode) {
11523 default: break;
11524 case ISD::BUILD_VECTOR:
11525 // Attempt to simplify BUILD_VECTOR.
11526 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
11527 return V;
11528 break;
11530 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
11531 return V;
11532 break;
11533 case ISD::SELECT_CC:
11534 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
11535 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
11536 "LHS and RHS of condition must have same type!");
11537 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11538 "True and False arms of SelectCC must have same type!");
11539 assert(Ops[2].getValueType() == VT &&
11540 "select_cc node must be of same type as true and false value!");
11541 assert((!Ops[0].getValueType().isVector() ||
11542 Ops[0].getValueType().getVectorElementCount() ==
11543 VT.getVectorElementCount()) &&
11544 "Expected select_cc with vector result to have the same sized "
11545 "comparison type!");
11546 break;
11547 case ISD::BR_CC:
11548 assert(NumOps == 5 && "BR_CC takes 5 operands!");
11549 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11550 "LHS/RHS of comparison should match types!");
11551 break;
11552 case ISD::VP_ADD:
11553 case ISD::VP_SUB:
11554 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
11555 if (VT.getScalarType() == MVT::i1)
11556 Opcode = ISD::VP_XOR;
11557 break;
11558 case ISD::VP_MUL:
11559 // If it is VP_MUL mask operation then turn it to VP_AND
11560 if (VT.getScalarType() == MVT::i1)
11561 Opcode = ISD::VP_AND;
11562 break;
11563 case ISD::VP_REDUCE_MUL:
11564 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11565 if (VT == MVT::i1)
11566 Opcode = ISD::VP_REDUCE_AND;
11567 break;
11568 case ISD::VP_REDUCE_ADD:
11569 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11570 if (VT == MVT::i1)
11571 Opcode = ISD::VP_REDUCE_XOR;
11572 break;
11573 case ISD::VP_REDUCE_SMAX:
11574 case ISD::VP_REDUCE_UMIN:
11575 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11576 // VP_REDUCE_AND.
11577 if (VT == MVT::i1)
11578 Opcode = ISD::VP_REDUCE_AND;
11579 break;
11580 case ISD::VP_REDUCE_SMIN:
11581 case ISD::VP_REDUCE_UMAX:
11582 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11583 // VP_REDUCE_OR.
11584 if (VT == MVT::i1)
11585 Opcode = ISD::VP_REDUCE_OR;
11586 break;
11587 }
11588
11589 // Memoize nodes.
11590 SDNode *N;
11591 SDVTList VTs = getVTList(VT);
11592
11593 if (VT != MVT::Glue) {
11595 AddNodeIDNode(ID, Opcode, VTs, Ops);
11596 void *IP = nullptr;
11597
11598 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11599 E->intersectFlagsWith(Flags);
11600 return SDValue(E, 0);
11601 }
11602
11603 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11604 createOperands(N, Ops);
11605
11606 CSEMap.InsertNode(N, IP);
11607 } else {
11608 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11609 createOperands(N, Ops);
11610 }
11611
11612 N->setFlags(Flags);
11613 InsertNode(N);
11614 SDValue V(N, 0);
11615 NewSDValueDbgMsg(V, "Creating new node: ", this);
11616 return V;
11617}
11618
11619SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11620 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11621 SDNodeFlags Flags;
11622 if (Inserter)
11623 Flags = Inserter->getFlags();
11624 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11625}
11626
11627SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11629 const SDNodeFlags Flags) {
11630 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11631}
11632
11633SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11635 SDNodeFlags Flags;
11636 if (Inserter)
11637 Flags = Inserter->getFlags();
11638 return getNode(Opcode, DL, VTList, Ops, Flags);
11639}
11640
11641SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11642 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11643 if (VTList.NumVTs == 1)
11644 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11645
11646#ifndef NDEBUG
11647 for (const auto &Op : Ops)
11648 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11649 "Operand is DELETED_NODE!");
11650#endif
11651
11652 switch (Opcode) {
11653 case ISD::SADDO:
11654 case ISD::UADDO:
11655 case ISD::SSUBO:
11656 case ISD::USUBO: {
11657 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11658 "Invalid add/sub overflow op!");
11659 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11660 Ops[0].getValueType() == Ops[1].getValueType() &&
11661 Ops[0].getValueType() == VTList.VTs[0] &&
11662 "Binary operator types must match!");
11663 SDValue N1 = Ops[0], N2 = Ops[1];
11664 canonicalizeCommutativeBinop(Opcode, N1, N2);
11665
11666 // (X +- 0) -> X with zero-overflow.
11667 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11668 /*AllowTruncation*/ true);
11669 if (N2CV && N2CV->isZero()) {
11670 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11671 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11672 }
11673
11674 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11675 VTList.VTs[1].getScalarType() == MVT::i1) {
11676 SDValue F1 = getFreeze(N1);
11677 SDValue F2 = getFreeze(N2);
11678 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11679 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11680 return getNode(ISD::MERGE_VALUES, DL, VTList,
11681 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11682 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11683 Flags);
11684 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11685 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11686 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11687 return getNode(ISD::MERGE_VALUES, DL, VTList,
11688 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11689 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11690 Flags);
11691 }
11692 }
11693 break;
11694 }
11695 case ISD::SADDO_CARRY:
11696 case ISD::UADDO_CARRY:
11697 case ISD::SSUBO_CARRY:
11698 case ISD::USUBO_CARRY:
11699 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11700 "Invalid add/sub overflow op!");
11701 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11702 Ops[0].getValueType() == Ops[1].getValueType() &&
11703 Ops[0].getValueType() == VTList.VTs[0] &&
11704 Ops[2].getValueType() == VTList.VTs[1] &&
11705 "Binary operator types must match!");
11706 break;
11707 case ISD::SMUL_LOHI:
11708 case ISD::UMUL_LOHI: {
11709 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11710 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11711 VTList.VTs[0] == Ops[0].getValueType() &&
11712 VTList.VTs[0] == Ops[1].getValueType() &&
11713 "Binary operator types must match!");
11714 // Constant fold.
11717 if (LHS && RHS) {
11718 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11719 unsigned OutWidth = Width * 2;
11720 APInt Val = LHS->getAPIntValue();
11721 APInt Mul = RHS->getAPIntValue();
11722 if (Opcode == ISD::SMUL_LOHI) {
11723 Val = Val.sext(OutWidth);
11724 Mul = Mul.sext(OutWidth);
11725 } else {
11726 Val = Val.zext(OutWidth);
11727 Mul = Mul.zext(OutWidth);
11728 }
11729 Val *= Mul;
11730
11731 SDValue Hi =
11732 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11733 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11734 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11735 }
11736 break;
11737 }
11738 case ISD::FFREXP: {
11739 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11740 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11741 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11742
11744 int FrexpExp;
11745 APFloat FrexpMant =
11746 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11747 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11748 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11749 DL, VTList.VTs[1]);
11750 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11751 }
11752
11753 break;
11754 }
11756 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11757 "Invalid STRICT_FP_EXTEND!");
11758 assert(VTList.VTs[0].isFloatingPoint() &&
11759 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11760 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11761 "STRICT_FP_EXTEND result type should be vector iff the operand "
11762 "type is vector!");
11763 assert((!VTList.VTs[0].isVector() ||
11764 VTList.VTs[0].getVectorElementCount() ==
11765 Ops[1].getValueType().getVectorElementCount()) &&
11766 "Vector element count mismatch!");
11767 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11768 "Invalid fpext node, dst <= src!");
11769 break;
11771 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11772 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11773 "STRICT_FP_ROUND result type should be vector iff the operand "
11774 "type is vector!");
11775 assert((!VTList.VTs[0].isVector() ||
11776 VTList.VTs[0].getVectorElementCount() ==
11777 Ops[1].getValueType().getVectorElementCount()) &&
11778 "Vector element count mismatch!");
11779 assert(VTList.VTs[0].isFloatingPoint() &&
11780 Ops[1].getValueType().isFloatingPoint() &&
11781 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11782 Ops[2].getOpcode() == ISD::TargetConstant &&
11783 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11784 "Invalid STRICT_FP_ROUND!");
11785 break;
11786 }
11787
11788 // Memoize the node unless it returns a glue result.
11789 SDNode *N;
11790 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11792 AddNodeIDNode(ID, Opcode, VTList, Ops);
11793 void *IP = nullptr;
11794 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11795 E->intersectFlagsWith(Flags);
11796 return SDValue(E, 0);
11797 }
11798
11799 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11800 createOperands(N, Ops);
11801 CSEMap.InsertNode(N, IP);
11802 } else {
11803 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11804 createOperands(N, Ops);
11805 }
11806
11807 N->setFlags(Flags);
11808 InsertNode(N);
11809 SDValue V(N, 0);
11810 NewSDValueDbgMsg(V, "Creating new node: ", this);
11811 return V;
11812}
11813
11814SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11815 SDVTList VTList) {
11816 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11817}
11818
11819SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11820 SDValue N1) {
11821 SDValue Ops[] = { N1 };
11822 return getNode(Opcode, DL, VTList, Ops);
11823}
11824
11825SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11826 SDValue N1, SDValue N2) {
11827 SDValue Ops[] = { N1, N2 };
11828 return getNode(Opcode, DL, VTList, Ops);
11829}
11830
11831SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11832 SDValue N1, SDValue N2, SDValue N3) {
11833 SDValue Ops[] = { N1, N2, N3 };
11834 return getNode(Opcode, DL, VTList, Ops);
11835}
11836
11837SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11838 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11839 SDValue Ops[] = { N1, N2, N3, N4 };
11840 return getNode(Opcode, DL, VTList, Ops);
11841}
11842
11843SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11844 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11845 SDValue N5) {
11846 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11847 return getNode(Opcode, DL, VTList, Ops);
11848}
11849
11851 if (!VT.isExtended())
11852 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11853
11854 return makeVTList(&(*EVTs.insert(VT).first), 1);
11855}
11856
11859 ID.AddInteger(2U);
11860 ID.AddInteger(VT1.getRawBits());
11861 ID.AddInteger(VT2.getRawBits());
11862
11863 void *IP = nullptr;
11864 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11865 if (!Result) {
11866 EVT *Array = Allocator.Allocate<EVT>(2);
11867 Array[0] = VT1;
11868 Array[1] = VT2;
11869 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11870 VTListMap.InsertNode(Result, IP);
11871 }
11872 return Result->getSDVTList();
11873}
11874
11877 ID.AddInteger(3U);
11878 ID.AddInteger(VT1.getRawBits());
11879 ID.AddInteger(VT2.getRawBits());
11880 ID.AddInteger(VT3.getRawBits());
11881
11882 void *IP = nullptr;
11883 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11884 if (!Result) {
11885 EVT *Array = Allocator.Allocate<EVT>(3);
11886 Array[0] = VT1;
11887 Array[1] = VT2;
11888 Array[2] = VT3;
11889 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11890 VTListMap.InsertNode(Result, IP);
11891 }
11892 return Result->getSDVTList();
11893}
11894
11897 ID.AddInteger(4U);
11898 ID.AddInteger(VT1.getRawBits());
11899 ID.AddInteger(VT2.getRawBits());
11900 ID.AddInteger(VT3.getRawBits());
11901 ID.AddInteger(VT4.getRawBits());
11902
11903 void *IP = nullptr;
11904 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11905 if (!Result) {
11906 EVT *Array = Allocator.Allocate<EVT>(4);
11907 Array[0] = VT1;
11908 Array[1] = VT2;
11909 Array[2] = VT3;
11910 Array[3] = VT4;
11911 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11912 VTListMap.InsertNode(Result, IP);
11913 }
11914 return Result->getSDVTList();
11915}
11916
11918 unsigned NumVTs = VTs.size();
11920 ID.AddInteger(NumVTs);
11921 for (unsigned index = 0; index < NumVTs; index++) {
11922 ID.AddInteger(VTs[index].getRawBits());
11923 }
11924
11925 void *IP = nullptr;
11926 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11927 if (!Result) {
11928 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11929 llvm::copy(VTs, Array);
11930 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11931 VTListMap.InsertNode(Result, IP);
11932 }
11933 return Result->getSDVTList();
11934}
11935
11936
11937/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11938/// specified operands. If the resultant node already exists in the DAG,
11939/// this does not modify the specified node, instead it returns the node that
11940/// already exists. If the resultant node does not exist in the DAG, the
11941/// input node is returned. As a degenerate case, if you specify the same
11942/// input operands as the node already has, the input node is returned.
11944 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11945
11946 // Check to see if there is no change.
11947 if (Op == N->getOperand(0)) return N;
11948
11949 // See if the modified node already exists.
11950 void *InsertPos = nullptr;
11951 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11952 return Existing;
11953
11954 // Nope it doesn't. Remove the node from its current place in the maps.
11955 if (InsertPos)
11956 if (!RemoveNodeFromCSEMaps(N))
11957 InsertPos = nullptr;
11958
11959 // Now we update the operands.
11960 N->OperandList[0].set(Op);
11961
11963 // If this gets put into a CSE map, add it.
11964 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11965 return N;
11966}
11967
11969 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11970
11971 // Check to see if there is no change.
11972 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11973 return N; // No operands changed, just return the input node.
11974
11975 // See if the modified node already exists.
11976 void *InsertPos = nullptr;
11977 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11978 return Existing;
11979
11980 // Nope it doesn't. Remove the node from its current place in the maps.
11981 if (InsertPos)
11982 if (!RemoveNodeFromCSEMaps(N))
11983 InsertPos = nullptr;
11984
11985 // Now we update the operands.
11986 if (N->OperandList[0] != Op1)
11987 N->OperandList[0].set(Op1);
11988 if (N->OperandList[1] != Op2)
11989 N->OperandList[1].set(Op2);
11990
11992 // If this gets put into a CSE map, add it.
11993 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11994 return N;
11995}
11996
11999 SDValue Ops[] = { Op1, Op2, Op3 };
12000 return UpdateNodeOperands(N, Ops);
12001}
12002
12005 SDValue Op3, SDValue Op4) {
12006 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
12007 return UpdateNodeOperands(N, Ops);
12008}
12009
12012 SDValue Op3, SDValue Op4, SDValue Op5) {
12013 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
12014 return UpdateNodeOperands(N, Ops);
12015}
12016
12019 unsigned NumOps = Ops.size();
12020 assert(N->getNumOperands() == NumOps &&
12021 "Update with wrong number of operands");
12022
12023 // If no operands changed just return the input node.
12024 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
12025 return N;
12026
12027 // See if the modified node already exists.
12028 void *InsertPos = nullptr;
12029 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
12030 return Existing;
12031
12032 // Nope it doesn't. Remove the node from its current place in the maps.
12033 if (InsertPos)
12034 if (!RemoveNodeFromCSEMaps(N))
12035 InsertPos = nullptr;
12036
12037 // Now we update the operands.
12038 for (unsigned i = 0; i != NumOps; ++i)
12039 if (N->OperandList[i] != Ops[i])
12040 N->OperandList[i].set(Ops[i]);
12041
12043 // If this gets put into a CSE map, add it.
12044 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
12045 return N;
12046}
12047
12048/// DropOperands - Release the operands and set this node to have
12049/// zero operands.
12051 // Unlike the code in MorphNodeTo that does this, we don't need to
12052 // watch for dead nodes here.
12053 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
12054 SDUse &Use = *I++;
12055 Use.set(SDValue());
12056 }
12057}
12058
12060 ArrayRef<MachineMemOperand *> NewMemRefs) {
12061 if (NewMemRefs.empty()) {
12062 N->clearMemRefs();
12063 return;
12064 }
12065
12066 // Check if we can avoid allocating by storing a single reference directly.
12067 if (NewMemRefs.size() == 1) {
12068 N->MemRefs = NewMemRefs[0];
12069 N->NumMemRefs = 1;
12070 return;
12071 }
12072
12073 MachineMemOperand **MemRefsBuffer =
12074 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
12075 llvm::copy(NewMemRefs, MemRefsBuffer);
12076 N->MemRefs = MemRefsBuffer;
12077 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
12078}
12079
12080/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
12081/// machine opcode.
12082///
12084 EVT VT) {
12085 SDVTList VTs = getVTList(VT);
12086 return SelectNodeTo(N, MachineOpc, VTs, {});
12087}
12088
12090 EVT VT, SDValue Op1) {
12091 SDVTList VTs = getVTList(VT);
12092 SDValue Ops[] = { Op1 };
12093 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12094}
12095
12097 EVT VT, SDValue Op1,
12098 SDValue Op2) {
12099 SDVTList VTs = getVTList(VT);
12100 SDValue Ops[] = { Op1, Op2 };
12101 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12102}
12103
12105 EVT VT, SDValue Op1,
12106 SDValue Op2, SDValue Op3) {
12107 SDVTList VTs = getVTList(VT);
12108 SDValue Ops[] = { Op1, Op2, Op3 };
12109 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12110}
12111
12114 SDVTList VTs = getVTList(VT);
12115 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12116}
12117
12119 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
12120 SDVTList VTs = getVTList(VT1, VT2);
12121 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12122}
12123
12125 EVT VT1, EVT VT2) {
12126 SDVTList VTs = getVTList(VT1, VT2);
12127 return SelectNodeTo(N, MachineOpc, VTs, {});
12128}
12129
12131 EVT VT1, EVT VT2, EVT VT3,
12133 SDVTList VTs = getVTList(VT1, VT2, VT3);
12134 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12135}
12136
12138 EVT VT1, EVT VT2,
12139 SDValue Op1, SDValue Op2) {
12140 SDVTList VTs = getVTList(VT1, VT2);
12141 SDValue Ops[] = { Op1, Op2 };
12142 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12143}
12144
12147 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
12148 // Reset the NodeID to -1.
12149 New->setNodeId(-1);
12150 if (New != N) {
12151 ReplaceAllUsesWith(N, New);
12153 }
12154 return New;
12155}
12156
12157/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
12158/// the line number information on the merged node since it is not possible to
12159/// preserve the information that operation is associated with multiple lines.
12160/// This will make the debugger working better at -O0, were there is a higher
12161/// probability having other instructions associated with that line.
12162///
12163/// For IROrder, we keep the smaller of the two
12164SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
12165 DebugLoc NLoc = N->getDebugLoc();
12166 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
12167 N->setDebugLoc(DebugLoc());
12168 }
12169 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
12170 N->setIROrder(Order);
12171 return N;
12172}
12173
12174/// MorphNodeTo - This *mutates* the specified node to have the specified
12175/// return type, opcode, and operands.
12176///
12177/// Note that MorphNodeTo returns the resultant node. If there is already a
12178/// node of the specified opcode and operands, it returns that node instead of
12179/// the current one. Note that the SDLoc need not be the same.
12180///
12181/// Using MorphNodeTo is faster than creating a new node and swapping it in
12182/// with ReplaceAllUsesWith both because it often avoids allocating a new
12183/// node, and because it doesn't require CSE recalculation for any of
12184/// the node's users.
12185///
12186/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
12187/// As a consequence it isn't appropriate to use from within the DAG combiner or
12188/// the legalizer which maintain worklists that would need to be updated when
12189/// deleting things.
12192 // If an identical node already exists, use it.
12193 void *IP = nullptr;
12194 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
12196 AddNodeIDNode(ID, Opc, VTs, Ops);
12197 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
12198 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
12199 }
12200
12201 if (!RemoveNodeFromCSEMaps(N))
12202 IP = nullptr;
12203
12204 // Start the morphing.
12205 N->NodeType = Opc;
12206 N->ValueList = VTs.VTs;
12207 N->NumValues = VTs.NumVTs;
12208
12209 // Clear the operands list, updating used nodes to remove this from their
12210 // use list. Keep track of any operands that become dead as a result.
12211 SmallPtrSet<SDNode*, 16> DeadNodeSet;
12212 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
12213 SDUse &Use = *I++;
12214 SDNode *Used = Use.getNode();
12215 Use.set(SDValue());
12216 if (Used->use_empty())
12217 DeadNodeSet.insert(Used);
12218 }
12219
12220 // For MachineNode, initialize the memory references information.
12222 MN->clearMemRefs();
12223
12224 // Swap for an appropriately sized array from the recycler.
12225 removeOperands(N);
12226 createOperands(N, Ops);
12227
12228 // Delete any nodes that are still dead after adding the uses for the
12229 // new operands.
12230 if (!DeadNodeSet.empty()) {
12231 SmallVector<SDNode *, 16> DeadNodes;
12232 for (SDNode *N : DeadNodeSet)
12233 if (N->use_empty())
12234 DeadNodes.push_back(N);
12235 RemoveDeadNodes(DeadNodes);
12236 }
12237
12238 if (IP)
12239 CSEMap.InsertNode(N, IP); // Memoize the new node.
12240 return N;
12241}
12242
12244 unsigned OrigOpc = Node->getOpcode();
12245 unsigned NewOpc;
12246 switch (OrigOpc) {
12247 default:
12248 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
12249#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
12250 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
12251#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
12252 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
12253#include "llvm/IR/ConstrainedOps.def"
12254 }
12255
12256 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
12257
12258 // We're taking this node out of the chain, so we need to re-link things.
12259 SDValue InputChain = Node->getOperand(0);
12260 SDValue OutputChain = SDValue(Node, 1);
12261 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
12262
12264 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
12265 Ops.push_back(Node->getOperand(i));
12266
12267 SDVTList VTs = getVTList(Node->getValueType(0));
12268 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
12269
12270 // MorphNodeTo can operate in two ways: if an existing node with the
12271 // specified operands exists, it can just return it. Otherwise, it
12272 // updates the node in place to have the requested operands.
12273 if (Res == Node) {
12274 // If we updated the node in place, reset the node ID. To the isel,
12275 // this should be just like a newly allocated machine node.
12276 Res->setNodeId(-1);
12277 } else {
12280 }
12281
12282 return Res;
12283}
12284
12285/// getMachineNode - These are used for target selectors to create a new node
12286/// with specified return type(s), MachineInstr opcode, and operands.
12287///
12288/// Note that getMachineNode returns the resultant node. If there is already a
12289/// node of the specified opcode and operands, it returns that node instead of
12290/// the current one.
12292 EVT VT) {
12293 SDVTList VTs = getVTList(VT);
12294 return getMachineNode(Opcode, dl, VTs, {});
12295}
12296
12298 EVT VT, SDValue Op1) {
12299 SDVTList VTs = getVTList(VT);
12300 SDValue Ops[] = { Op1 };
12301 return getMachineNode(Opcode, dl, VTs, Ops);
12302}
12303
12305 EVT VT, SDValue Op1, SDValue Op2) {
12306 SDVTList VTs = getVTList(VT);
12307 SDValue Ops[] = { Op1, Op2 };
12308 return getMachineNode(Opcode, dl, VTs, Ops);
12309}
12310
12312 EVT VT, SDValue Op1, SDValue Op2,
12313 SDValue Op3) {
12314 SDVTList VTs = getVTList(VT);
12315 SDValue Ops[] = { Op1, Op2, Op3 };
12316 return getMachineNode(Opcode, dl, VTs, Ops);
12317}
12318
12321 SDVTList VTs = getVTList(VT);
12322 return getMachineNode(Opcode, dl, VTs, Ops);
12323}
12324
12326 EVT VT1, EVT VT2, SDValue Op1,
12327 SDValue Op2) {
12328 SDVTList VTs = getVTList(VT1, VT2);
12329 SDValue Ops[] = { Op1, Op2 };
12330 return getMachineNode(Opcode, dl, VTs, Ops);
12331}
12332
12334 EVT VT1, EVT VT2, SDValue Op1,
12335 SDValue Op2, SDValue Op3) {
12336 SDVTList VTs = getVTList(VT1, VT2);
12337 SDValue Ops[] = { Op1, Op2, Op3 };
12338 return getMachineNode(Opcode, dl, VTs, Ops);
12339}
12340
12342 EVT VT1, EVT VT2,
12344 SDVTList VTs = getVTList(VT1, VT2);
12345 return getMachineNode(Opcode, dl, VTs, Ops);
12346}
12347
12349 EVT VT1, EVT VT2, EVT VT3,
12350 SDValue Op1, SDValue Op2) {
12351 SDVTList VTs = getVTList(VT1, VT2, VT3);
12352 SDValue Ops[] = { Op1, Op2 };
12353 return getMachineNode(Opcode, dl, VTs, Ops);
12354}
12355
12357 EVT VT1, EVT VT2, EVT VT3,
12358 SDValue Op1, SDValue Op2,
12359 SDValue Op3) {
12360 SDVTList VTs = getVTList(VT1, VT2, VT3);
12361 SDValue Ops[] = { Op1, Op2, Op3 };
12362 return getMachineNode(Opcode, dl, VTs, Ops);
12363}
12364
12366 EVT VT1, EVT VT2, EVT VT3,
12368 SDVTList VTs = getVTList(VT1, VT2, VT3);
12369 return getMachineNode(Opcode, dl, VTs, Ops);
12370}
12371
12373 ArrayRef<EVT> ResultTys,
12375 SDVTList VTs = getVTList(ResultTys);
12376 return getMachineNode(Opcode, dl, VTs, Ops);
12377}
12378
12380 SDVTList VTs,
12382 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
12384 void *IP = nullptr;
12385
12386 if (DoCSE) {
12388 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
12389 IP = nullptr;
12390 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
12391 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
12392 }
12393 }
12394
12395 // Allocate a new MachineSDNode.
12396 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
12397 createOperands(N, Ops);
12398
12399 if (DoCSE)
12400 CSEMap.InsertNode(N, IP);
12401
12402 InsertNode(N);
12403 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
12404 return N;
12405}
12406
12407/// getTargetExtractSubreg - A convenience function for creating
12408/// TargetOpcode::EXTRACT_SUBREG nodes.
12410 SDValue Operand) {
12411 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12412 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
12413 VT, Operand, SRIdxVal);
12414 return SDValue(Subreg, 0);
12415}
12416
12417/// getTargetInsertSubreg - A convenience function for creating
12418/// TargetOpcode::INSERT_SUBREG nodes.
12420 SDValue Operand, SDValue Subreg) {
12421 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12422 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
12423 VT, Operand, Subreg, SRIdxVal);
12424 return SDValue(Result, 0);
12425}
12426
12427/// getNodeIfExists - Get the specified node if it's already available, or
12428/// else return NULL.
12431 bool AllowCommute) {
12432 SDNodeFlags Flags;
12433 if (Inserter)
12434 Flags = Inserter->getFlags();
12435 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
12436}
12437
12440 const SDNodeFlags Flags,
12441 bool AllowCommute) {
12442 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
12443 return nullptr;
12444
12445 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
12447 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
12448 void *IP = nullptr;
12449 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
12450 E->intersectFlagsWith(Flags);
12451 return E;
12452 }
12453 return nullptr;
12454 };
12455
12456 if (SDNode *Existing = Lookup(Ops))
12457 return Existing;
12458
12459 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
12460 return Lookup({Ops[1], Ops[0]});
12461
12462 return nullptr;
12463}
12464
12465/// doesNodeExist - Check if a node exists without modifying its flags.
12466bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
12468 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
12470 AddNodeIDNode(ID, Opcode, VTList, Ops);
12471 void *IP = nullptr;
12472 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
12473 return true;
12474 }
12475 return false;
12476}
12477
12478/// getDbgValue - Creates a SDDbgValue node.
12479///
12480/// SDNode
12482 SDNode *N, unsigned R, bool IsIndirect,
12483 const DebugLoc &DL, unsigned O) {
12484 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12485 "Expected inlined-at fields to agree");
12486 return new (DbgInfo->getAlloc())
12487 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
12488 {}, IsIndirect, DL, O,
12489 /*IsVariadic=*/false);
12490}
12491
12492/// Constant
12494 DIExpression *Expr,
12495 const Value *C,
12496 const DebugLoc &DL, unsigned O) {
12497 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12498 "Expected inlined-at fields to agree");
12499 return new (DbgInfo->getAlloc())
12500 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
12501 /*IsIndirect=*/false, DL, O,
12502 /*IsVariadic=*/false);
12503}
12504
12505/// FrameIndex
12507 DIExpression *Expr, unsigned FI,
12508 bool IsIndirect,
12509 const DebugLoc &DL,
12510 unsigned O) {
12511 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12512 "Expected inlined-at fields to agree");
12513 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
12514}
12515
12516/// FrameIndex with dependencies
12518 DIExpression *Expr, unsigned FI,
12519 ArrayRef<SDNode *> Dependencies,
12520 bool IsIndirect,
12521 const DebugLoc &DL,
12522 unsigned O) {
12523 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12524 "Expected inlined-at fields to agree");
12525 return new (DbgInfo->getAlloc())
12526 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
12527 Dependencies, IsIndirect, DL, O,
12528 /*IsVariadic=*/false);
12529}
12530
12531/// VReg
12533 Register VReg, bool IsIndirect,
12534 const DebugLoc &DL, unsigned O) {
12535 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12536 "Expected inlined-at fields to agree");
12537 return new (DbgInfo->getAlloc())
12538 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
12539 {}, IsIndirect, DL, O,
12540 /*IsVariadic=*/false);
12541}
12542
12545 ArrayRef<SDNode *> Dependencies,
12546 bool IsIndirect, const DebugLoc &DL,
12547 unsigned O, bool IsVariadic) {
12548 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12549 "Expected inlined-at fields to agree");
12550 return new (DbgInfo->getAlloc())
12551 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
12552 DL, O, IsVariadic);
12553}
12554
12556 unsigned OffsetInBits, unsigned SizeInBits,
12557 bool InvalidateDbg) {
12558 SDNode *FromNode = From.getNode();
12559 SDNode *ToNode = To.getNode();
12560 assert(FromNode && ToNode && "Can't modify dbg values");
12561
12562 // PR35338
12563 // TODO: assert(From != To && "Redundant dbg value transfer");
12564 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12565 if (From == To || FromNode == ToNode)
12566 return;
12567
12568 if (!FromNode->getHasDebugValue())
12569 return;
12570
12571 SDDbgOperand FromLocOp =
12572 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12574
12576 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12577 if (Dbg->isInvalidated())
12578 continue;
12579
12580 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12581
12582 // Create a new location ops vector that is equal to the old vector, but
12583 // with each instance of FromLocOp replaced with ToLocOp.
12584 bool Changed = false;
12585 auto NewLocOps = Dbg->copyLocationOps();
12586 std::replace_if(
12587 NewLocOps.begin(), NewLocOps.end(),
12588 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12589 bool Match = Op == FromLocOp;
12590 Changed |= Match;
12591 return Match;
12592 },
12593 ToLocOp);
12594 // Ignore this SDDbgValue if we didn't find a matching location.
12595 if (!Changed)
12596 continue;
12597
12598 DIVariable *Var = Dbg->getVariable();
12599 auto *Expr = Dbg->getExpression();
12600 // If a fragment is requested, update the expression.
12601 if (SizeInBits) {
12602 // When splitting a larger (e.g., sign-extended) value whose
12603 // lower bits are described with an SDDbgValue, do not attempt
12604 // to transfer the SDDbgValue to the upper bits.
12605 if (auto FI = Expr->getFragmentInfo())
12606 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12607 continue;
12608 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12609 SizeInBits);
12610 if (!Fragment)
12611 continue;
12612 Expr = *Fragment;
12613 }
12614
12615 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12616 // Clone the SDDbgValue and move it to To.
12617 SDDbgValue *Clone = getDbgValueList(
12618 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12619 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12620 Dbg->isVariadic());
12621 ClonedDVs.push_back(Clone);
12622
12623 if (InvalidateDbg) {
12624 // Invalidate value and indicate the SDDbgValue should not be emitted.
12625 Dbg->setIsInvalidated();
12626 Dbg->setIsEmitted();
12627 }
12628 }
12629
12630 for (SDDbgValue *Dbg : ClonedDVs) {
12631 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12632 "Transferred DbgValues should depend on the new SDNode");
12633 AddDbgValue(Dbg, false);
12634 }
12635}
12636
12638 if (!N.getHasDebugValue())
12639 return;
12640
12641 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12642 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12643 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12644 return SDDbgOperand::fromNode(Node, ResNo);
12645 };
12646
12648 for (auto *DV : GetDbgValues(&N)) {
12649 if (DV->isInvalidated())
12650 continue;
12651 switch (N.getOpcode()) {
12652 default:
12653 break;
12654 case ISD::ADD: {
12655 SDValue N0 = N.getOperand(0);
12656 SDValue N1 = N.getOperand(1);
12657 if (!isa<ConstantSDNode>(N0)) {
12658 bool RHSConstant = isa<ConstantSDNode>(N1);
12660 if (RHSConstant)
12661 Offset = N.getConstantOperandVal(1);
12662 // We are not allowed to turn indirect debug values variadic, so
12663 // don't salvage those.
12664 if (!RHSConstant && DV->isIndirect())
12665 continue;
12666
12667 // Rewrite an ADD constant node into a DIExpression. Since we are
12668 // performing arithmetic to compute the variable's *value* in the
12669 // DIExpression, we need to mark the expression with a
12670 // DW_OP_stack_value.
12671 auto *DIExpr = DV->getExpression();
12672 auto NewLocOps = DV->copyLocationOps();
12673 bool Changed = false;
12674 size_t OrigLocOpsSize = NewLocOps.size();
12675 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12676 // We're not given a ResNo to compare against because the whole
12677 // node is going away. We know that any ISD::ADD only has one
12678 // result, so we can assume any node match is using the result.
12679 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12680 NewLocOps[i].getSDNode() != &N)
12681 continue;
12682 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12683 if (RHSConstant) {
12686 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12687 } else {
12688 // Convert to a variadic expression (if not already).
12689 // convertToVariadicExpression() returns a const pointer, so we use
12690 // a temporary const variable here.
12691 const auto *TmpDIExpr =
12695 ExprOps.push_back(NewLocOps.size());
12696 ExprOps.push_back(dwarf::DW_OP_plus);
12697 SDDbgOperand RHS =
12699 NewLocOps.push_back(RHS);
12700 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12701 }
12702 Changed = true;
12703 }
12704 (void)Changed;
12705 assert(Changed && "Salvage target doesn't use N");
12706
12707 bool IsVariadic =
12708 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12709
12710 auto AdditionalDependencies = DV->getAdditionalDependencies();
12711 SDDbgValue *Clone = getDbgValueList(
12712 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12713 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12714 ClonedDVs.push_back(Clone);
12715 DV->setIsInvalidated();
12716 DV->setIsEmitted();
12717 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12718 N0.getNode()->dumprFull(this);
12719 dbgs() << " into " << *DIExpr << '\n');
12720 }
12721 break;
12722 }
12723 case ISD::TRUNCATE: {
12724 SDValue N0 = N.getOperand(0);
12725 TypeSize FromSize = N0.getValueSizeInBits();
12726 TypeSize ToSize = N.getValueSizeInBits(0);
12727
12728 DIExpression *DbgExpression = DV->getExpression();
12729 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12730 auto NewLocOps = DV->copyLocationOps();
12731 bool Changed = false;
12732 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12733 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12734 NewLocOps[i].getSDNode() != &N)
12735 continue;
12736
12737 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12738 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12739 Changed = true;
12740 }
12741 assert(Changed && "Salvage target doesn't use N");
12742 (void)Changed;
12743
12744 SDDbgValue *Clone =
12745 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12746 DV->getAdditionalDependencies(), DV->isIndirect(),
12747 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12748
12749 ClonedDVs.push_back(Clone);
12750 DV->setIsInvalidated();
12751 DV->setIsEmitted();
12752 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12753 dbgs() << " into " << *DbgExpression << '\n');
12754 break;
12755 }
12756 }
12757 }
12758
12759 for (SDDbgValue *Dbg : ClonedDVs) {
12760 assert((!Dbg->getSDNodes().empty() ||
12761 llvm::any_of(Dbg->getLocationOps(),
12762 [&](const SDDbgOperand &Op) {
12763 return Op.getKind() == SDDbgOperand::FRAMEIX;
12764 })) &&
12765 "Salvaged DbgValue should depend on a new SDNode");
12766 AddDbgValue(Dbg, false);
12767 }
12768}
12769
12770/// Creates a SDDbgLabel node.
12772 const DebugLoc &DL, unsigned O) {
12773 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12774 "Expected inlined-at fields to agree");
12775 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12776}
12777
12778namespace {
12779
12780/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12781/// pointed to by a use iterator is deleted, increment the use iterator
12782/// so that it doesn't dangle.
12783///
12784class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12787
12788 void NodeDeleted(SDNode *N, SDNode *E) override {
12789 // Increment the iterator as needed.
12790 while (UI != UE && N == UI->getUser())
12791 ++UI;
12792 }
12793
12794public:
12795 RAUWUpdateListener(SelectionDAG &d,
12798 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12799};
12800
12801} // end anonymous namespace
12802
12803/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12804/// This can cause recursive merging of nodes in the DAG.
12805///
12806/// This version assumes From has a single result value.
12807///
12809 SDNode *From = FromN.getNode();
12810 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12811 "Cannot replace with this method!");
12812 assert(From != To.getNode() && "Cannot replace uses of with self");
12813
12814 // Preserve Debug Values
12815 transferDbgValues(FromN, To);
12816 // Preserve extra info.
12817 copyExtraInfo(From, To.getNode());
12818
12819 // Iterate over all the existing uses of From. New uses will be added
12820 // to the beginning of the use list, which we avoid visiting.
12821 // This specifically avoids visiting uses of From that arise while the
12822 // replacement is happening, because any such uses would be the result
12823 // of CSE: If an existing node looks like From after one of its operands
12824 // is replaced by To, we don't want to replace of all its users with To
12825 // too. See PR3018 for more info.
12826 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12827 RAUWUpdateListener Listener(*this, UI, UE);
12828 while (UI != UE) {
12829 SDNode *User = UI->getUser();
12830
12831 // This node is about to morph, remove its old self from the CSE maps.
12832 RemoveNodeFromCSEMaps(User);
12833
12834 // A user can appear in a use list multiple times, and when this
12835 // happens the uses are usually next to each other in the list.
12836 // To help reduce the number of CSE recomputations, process all
12837 // the uses of this user that we can find this way.
12838 do {
12839 SDUse &Use = *UI;
12840 ++UI;
12841 Use.set(To);
12842 if (To->isDivergent() != From->isDivergent())
12844 } while (UI != UE && UI->getUser() == User);
12845 // Now that we have modified User, add it back to the CSE maps. If it
12846 // already exists there, recursively merge the results together.
12847 AddModifiedNodeToCSEMaps(User);
12848 }
12849
12850 // If we just RAUW'd the root, take note.
12851 if (FromN == getRoot())
12852 setRoot(To);
12853}
12854
12855/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12856/// This can cause recursive merging of nodes in the DAG.
12857///
12858/// This version assumes that for each value of From, there is a
12859/// corresponding value in To in the same position with the same type.
12860///
12862#ifndef NDEBUG
12863 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12864 assert((!From->hasAnyUseOfValue(i) ||
12865 From->getValueType(i) == To->getValueType(i)) &&
12866 "Cannot use this version of ReplaceAllUsesWith!");
12867#endif
12868
12869 // Handle the trivial case.
12870 if (From == To)
12871 return;
12872
12873 // Preserve Debug Info. Only do this if there's a use.
12874 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12875 if (From->hasAnyUseOfValue(i)) {
12876 assert((i < To->getNumValues()) && "Invalid To location");
12877 transferDbgValues(SDValue(From, i), SDValue(To, i));
12878 }
12879 // Preserve extra info.
12880 copyExtraInfo(From, To);
12881
12882 // Iterate over just the existing users of From. See the comments in
12883 // the ReplaceAllUsesWith above.
12884 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12885 RAUWUpdateListener Listener(*this, UI, UE);
12886 while (UI != UE) {
12887 SDNode *User = UI->getUser();
12888
12889 // This node is about to morph, remove its old self from the CSE maps.
12890 RemoveNodeFromCSEMaps(User);
12891
12892 // A user can appear in a use list multiple times, and when this
12893 // happens the uses are usually next to each other in the list.
12894 // To help reduce the number of CSE recomputations, process all
12895 // the uses of this user that we can find this way.
12896 do {
12897 SDUse &Use = *UI;
12898 ++UI;
12899 Use.setNode(To);
12900 if (To->isDivergent() != From->isDivergent())
12902 } while (UI != UE && UI->getUser() == User);
12903
12904 // Now that we have modified User, add it back to the CSE maps. If it
12905 // already exists there, recursively merge the results together.
12906 AddModifiedNodeToCSEMaps(User);
12907 }
12908
12909 // If we just RAUW'd the root, take note.
12910 if (From == getRoot().getNode())
12911 setRoot(SDValue(To, getRoot().getResNo()));
12912}
12913
12914/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12915/// This can cause recursive merging of nodes in the DAG.
12916///
12917/// This version can replace From with any result values. To must match the
12918/// number and types of values returned by From.
12920 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12921 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12922
12923 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12924 // Preserve Debug Info.
12925 transferDbgValues(SDValue(From, i), To[i]);
12926 // Preserve extra info.
12927 copyExtraInfo(From, To[i].getNode());
12928 }
12929
12930 // Iterate over just the existing users of From. See the comments in
12931 // the ReplaceAllUsesWith above.
12932 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12933 RAUWUpdateListener Listener(*this, UI, UE);
12934 while (UI != UE) {
12935 SDNode *User = UI->getUser();
12936
12937 // This node is about to morph, remove its old self from the CSE maps.
12938 RemoveNodeFromCSEMaps(User);
12939
12940 // A user can appear in a use list multiple times, and when this happens the
12941 // uses are usually next to each other in the list. To help reduce the
12942 // number of CSE and divergence recomputations, process all the uses of this
12943 // user that we can find this way.
12944 bool To_IsDivergent = false;
12945 do {
12946 SDUse &Use = *UI;
12947 const SDValue &ToOp = To[Use.getResNo()];
12948 ++UI;
12949 Use.set(ToOp);
12950 if (ToOp.getValueType() != MVT::Other)
12951 To_IsDivergent |= ToOp->isDivergent();
12952 } while (UI != UE && UI->getUser() == User);
12953
12954 if (To_IsDivergent != From->isDivergent())
12956
12957 // Now that we have modified User, add it back to the CSE maps. If it
12958 // already exists there, recursively merge the results together.
12959 AddModifiedNodeToCSEMaps(User);
12960 }
12961
12962 // If we just RAUW'd the root, take note.
12963 if (From == getRoot().getNode())
12964 setRoot(SDValue(To[getRoot().getResNo()]));
12965}
12966
12967/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12968/// uses of other values produced by From.getNode() alone. The Deleted
12969/// vector is handled the same way as for ReplaceAllUsesWith.
12971 // Handle the really simple, really trivial case efficiently.
12972 if (From == To) return;
12973
12974 // Handle the simple, trivial, case efficiently.
12975 if (From.getNode()->getNumValues() == 1) {
12976 ReplaceAllUsesWith(From, To);
12977 return;
12978 }
12979
12980 // Preserve Debug Info.
12981 transferDbgValues(From, To);
12982 copyExtraInfo(From.getNode(), To.getNode());
12983
12984 // Iterate over just the existing users of From. See the comments in
12985 // the ReplaceAllUsesWith above.
12986 SDNode::use_iterator UI = From.getNode()->use_begin(),
12987 UE = From.getNode()->use_end();
12988 RAUWUpdateListener Listener(*this, UI, UE);
12989 while (UI != UE) {
12990 SDNode *User = UI->getUser();
12991 bool UserRemovedFromCSEMaps = false;
12992
12993 // A user can appear in a use list multiple times, and when this
12994 // happens the uses are usually next to each other in the list.
12995 // To help reduce the number of CSE recomputations, process all
12996 // the uses of this user that we can find this way.
12997 do {
12998 SDUse &Use = *UI;
12999
13000 // Skip uses of different values from the same node.
13001 if (Use.getResNo() != From.getResNo()) {
13002 ++UI;
13003 continue;
13004 }
13005
13006 // If this node hasn't been modified yet, it's still in the CSE maps,
13007 // so remove its old self from the CSE maps.
13008 if (!UserRemovedFromCSEMaps) {
13009 RemoveNodeFromCSEMaps(User);
13010 UserRemovedFromCSEMaps = true;
13011 }
13012
13013 ++UI;
13014 Use.set(To);
13015 if (To->isDivergent() != From->isDivergent())
13017 } while (UI != UE && UI->getUser() == User);
13018 // We are iterating over all uses of the From node, so if a use
13019 // doesn't use the specific value, no changes are made.
13020 if (!UserRemovedFromCSEMaps)
13021 continue;
13022
13023 // Now that we have modified User, add it back to the CSE maps. If it
13024 // already exists there, recursively merge the results together.
13025 AddModifiedNodeToCSEMaps(User);
13026 }
13027
13028 // If we just RAUW'd the root, take note.
13029 if (From == getRoot())
13030 setRoot(To);
13031}
13032
13033namespace {
13034
13035/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
13036/// to record information about a use.
13037struct UseMemo {
13038 SDNode *User;
13039 unsigned Index;
13040 SDUse *Use;
13041};
13042
13043/// operator< - Sort Memos by User.
13044bool operator<(const UseMemo &L, const UseMemo &R) {
13045 return (intptr_t)L.User < (intptr_t)R.User;
13046}
13047
13048/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
13049/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
13050/// the node already has been taken care of recursively.
13051class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
13052 SmallVectorImpl<UseMemo> &Uses;
13053
13054 void NodeDeleted(SDNode *N, SDNode *E) override {
13055 for (UseMemo &Memo : Uses)
13056 if (Memo.User == N)
13057 Memo.User = nullptr;
13058 }
13059
13060public:
13061 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
13062 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
13063};
13064
13065} // end anonymous namespace
13066
13067/// Return true if a glue output should propagate divergence information.
13069 switch (Node->getOpcode()) {
13070 case ISD::CopyFromReg:
13071 case ISD::CopyToReg:
13072 return false;
13073 default:
13074 return true;
13075 }
13076
13077 llvm_unreachable("covered opcode switch");
13078}
13079
13081 if (TLI->isSDNodeAlwaysUniform(N)) {
13082 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
13083 "Conflicting divergence information!");
13084 return false;
13085 }
13086 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
13087 return true;
13088 for (const auto &Op : N->ops()) {
13089 EVT VT = Op.getValueType();
13090
13091 // Skip Chain. It does not carry divergence.
13092 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
13093 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
13094 return true;
13095 }
13096 return false;
13097}
13098
13100 SmallVector<SDNode *, 16> Worklist(1, N);
13101 do {
13102 N = Worklist.pop_back_val();
13103 bool IsDivergent = calculateDivergence(N);
13104 if (N->SDNodeBits.IsDivergent != IsDivergent) {
13105 N->SDNodeBits.IsDivergent = IsDivergent;
13106 llvm::append_range(Worklist, N->users());
13107 }
13108 } while (!Worklist.empty());
13109}
13110
13111void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
13113 Order.reserve(AllNodes.size());
13114 for (auto &N : allnodes()) {
13115 unsigned NOps = N.getNumOperands();
13116 Degree[&N] = NOps;
13117 if (0 == NOps)
13118 Order.push_back(&N);
13119 }
13120 for (size_t I = 0; I != Order.size(); ++I) {
13121 SDNode *N = Order[I];
13122 for (auto *U : N->users()) {
13123 unsigned &UnsortedOps = Degree[U];
13124 if (0 == --UnsortedOps)
13125 Order.push_back(U);
13126 }
13127 }
13128}
13129
13130#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
13131void SelectionDAG::VerifyDAGDivergence() {
13132 std::vector<SDNode *> TopoOrder;
13133 CreateTopologicalOrder(TopoOrder);
13134 for (auto *N : TopoOrder) {
13135 assert(calculateDivergence(N) == N->isDivergent() &&
13136 "Divergence bit inconsistency detected");
13137 }
13138}
13139#endif
13140
13141/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
13142/// uses of other values produced by From.getNode() alone. The same value
13143/// may appear in both the From and To list. The Deleted vector is
13144/// handled the same way as for ReplaceAllUsesWith.
13146 const SDValue *To,
13147 unsigned Num){
13148 // Handle the simple, trivial case efficiently.
13149 if (Num == 1)
13150 return ReplaceAllUsesOfValueWith(*From, *To);
13151
13152 transferDbgValues(*From, *To);
13153 copyExtraInfo(From->getNode(), To->getNode());
13154
13155 // Read up all the uses and make records of them. This helps
13156 // processing new uses that are introduced during the
13157 // replacement process.
13159 for (unsigned i = 0; i != Num; ++i) {
13160 unsigned FromResNo = From[i].getResNo();
13161 SDNode *FromNode = From[i].getNode();
13162 for (SDUse &Use : FromNode->uses()) {
13163 if (Use.getResNo() == FromResNo) {
13164 UseMemo Memo = {Use.getUser(), i, &Use};
13165 Uses.push_back(Memo);
13166 }
13167 }
13168 }
13169
13170 // Sort the uses, so that all the uses from a given User are together.
13172 RAUOVWUpdateListener Listener(*this, Uses);
13173
13174 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
13175 UseIndex != UseIndexEnd; ) {
13176 // We know that this user uses some value of From. If it is the right
13177 // value, update it.
13178 SDNode *User = Uses[UseIndex].User;
13179 // If the node has been deleted by recursive CSE updates when updating
13180 // another node, then just skip this entry.
13181 if (User == nullptr) {
13182 ++UseIndex;
13183 continue;
13184 }
13185
13186 // This node is about to morph, remove its old self from the CSE maps.
13187 RemoveNodeFromCSEMaps(User);
13188
13189 // The Uses array is sorted, so all the uses for a given User
13190 // are next to each other in the list.
13191 // To help reduce the number of CSE recomputations, process all
13192 // the uses of this user that we can find this way.
13193 do {
13194 unsigned i = Uses[UseIndex].Index;
13195 SDUse &Use = *Uses[UseIndex].Use;
13196 ++UseIndex;
13197
13198 Use.set(To[i]);
13199 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
13200
13201 // Now that we have modified User, add it back to the CSE maps. If it
13202 // already exists there, recursively merge the results together.
13203 AddModifiedNodeToCSEMaps(User);
13204 }
13205}
13206
13207/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
13208/// based on their topological order. It returns the maximum id and a vector
13209/// of the SDNodes* in assigned order by reference.
13211 unsigned DAGSize = 0;
13212
13213 // SortedPos tracks the progress of the algorithm. Nodes before it are
13214 // sorted, nodes after it are unsorted. When the algorithm completes
13215 // it is at the end of the list.
13216 allnodes_iterator SortedPos = allnodes_begin();
13217
13218 // Visit all the nodes. Move nodes with no operands to the front of
13219 // the list immediately. Annotate nodes that do have operands with their
13220 // operand count. Before we do this, the Node Id fields of the nodes
13221 // may contain arbitrary values. After, the Node Id fields for nodes
13222 // before SortedPos will contain the topological sort index, and the
13223 // Node Id fields for nodes At SortedPos and after will contain the
13224 // count of outstanding operands.
13226 checkForCycles(&N, this);
13227 unsigned Degree = N.getNumOperands();
13228 if (Degree == 0) {
13229 // A node with no uses, add it to the result array immediately.
13230 N.setNodeId(DAGSize++);
13231 allnodes_iterator Q(&N);
13232 if (Q != SortedPos)
13233 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
13234 assert(SortedPos != AllNodes.end() && "Overran node list");
13235 ++SortedPos;
13236 } else {
13237 // Temporarily use the Node Id as scratch space for the degree count.
13238 N.setNodeId(Degree);
13239 }
13240 }
13241
13242 // Visit all the nodes. As we iterate, move nodes into sorted order,
13243 // such that by the time the end is reached all nodes will be sorted.
13244 for (SDNode &Node : allnodes()) {
13245 SDNode *N = &Node;
13246 checkForCycles(N, this);
13247 // N is in sorted position, so all its uses have one less operand
13248 // that needs to be sorted.
13249 for (SDNode *P : N->users()) {
13250 unsigned Degree = P->getNodeId();
13251 assert(Degree != 0 && "Invalid node degree");
13252 --Degree;
13253 if (Degree == 0) {
13254 // All of P's operands are sorted, so P may sorted now.
13255 P->setNodeId(DAGSize++);
13256 if (P->getIterator() != SortedPos)
13257 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
13258 assert(SortedPos != AllNodes.end() && "Overran node list");
13259 ++SortedPos;
13260 } else {
13261 // Update P's outstanding operand count.
13262 P->setNodeId(Degree);
13263 }
13264 }
13265 if (Node.getIterator() == SortedPos) {
13266#ifndef NDEBUG
13268 SDNode *S = &*++I;
13269 dbgs() << "Overran sorted position:\n";
13270 S->dumprFull(this); dbgs() << "\n";
13271 dbgs() << "Checking if this is due to cycles\n";
13272 checkForCycles(this, true);
13273#endif
13274 llvm_unreachable(nullptr);
13275 }
13276 }
13277
13278 assert(SortedPos == AllNodes.end() &&
13279 "Topological sort incomplete!");
13280 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
13281 "First node in topological sort is not the entry token!");
13282 assert(AllNodes.front().getNodeId() == 0 &&
13283 "First node in topological sort has non-zero id!");
13284 assert(AllNodes.front().getNumOperands() == 0 &&
13285 "First node in topological sort has operands!");
13286 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
13287 "Last node in topologic sort has unexpected id!");
13288 assert(AllNodes.back().use_empty() &&
13289 "Last node in topologic sort has users!");
13290 assert(DAGSize == allnodes_size() && "Node count mismatch!");
13291 return DAGSize;
13292}
13293
13295 SmallVectorImpl<const SDNode *> &SortedNodes) const {
13296 SortedNodes.clear();
13297 // Node -> remaining number of outstanding operands.
13298 DenseMap<const SDNode *, unsigned> RemainingOperands;
13299
13300 // Put nodes without any operands into SortedNodes first.
13301 for (const SDNode &N : allnodes()) {
13302 checkForCycles(&N, this);
13303 unsigned NumOperands = N.getNumOperands();
13304 if (NumOperands == 0)
13305 SortedNodes.push_back(&N);
13306 else
13307 // Record their total number of outstanding operands.
13308 RemainingOperands[&N] = NumOperands;
13309 }
13310
13311 // A node is pushed into SortedNodes when all of its operands (predecessors in
13312 // the graph) are also in SortedNodes.
13313 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
13314 const SDNode *N = SortedNodes[i];
13315 for (const SDNode *U : N->users()) {
13316 // HandleSDNode is never part of a DAG and therefore has no entry in
13317 // RemainingOperands.
13318 if (U->getOpcode() == ISD::HANDLENODE)
13319 continue;
13320 unsigned &NumRemOperands = RemainingOperands[U];
13321 assert(NumRemOperands && "Invalid number of remaining operands");
13322 --NumRemOperands;
13323 if (!NumRemOperands)
13324 SortedNodes.push_back(U);
13325 }
13326 }
13327
13328 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
13329 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
13330 "First node in topological sort is not the entry token");
13331 assert(SortedNodes.front()->getNumOperands() == 0 &&
13332 "First node in topological sort has operands");
13333}
13334
13335/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
13336/// value is produced by SD.
13337void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
13338 for (SDNode *SD : DB->getSDNodes()) {
13339 if (!SD)
13340 continue;
13341 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
13342 SD->setHasDebugValue(true);
13343 }
13344 DbgInfo->add(DB, isParameter);
13345}
13346
13347void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
13348
13350 SDValue NewMemOpChain) {
13351 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
13352 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
13353 // The new memory operation must have the same position as the old load in
13354 // terms of memory dependency. Create a TokenFactor for the old load and new
13355 // memory operation and update uses of the old load's output chain to use that
13356 // TokenFactor.
13357 if (OldChain == NewMemOpChain || OldChain.use_empty())
13358 return NewMemOpChain;
13359
13360 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
13361 OldChain, NewMemOpChain);
13362 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
13363 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
13364 return TokenFactor;
13365}
13366
13368 SDValue NewMemOp) {
13369 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
13370 SDValue OldChain = SDValue(OldLoad, 1);
13371 SDValue NewMemOpChain = NewMemOp.getValue(1);
13372 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
13373}
13374
13376 Function **OutFunction) {
13377 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
13378
13379 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
13380 auto *Module = MF->getFunction().getParent();
13381 auto *Function = Module->getFunction(Symbol);
13382
13383 if (OutFunction != nullptr)
13384 *OutFunction = Function;
13385
13386 if (Function != nullptr) {
13387 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
13388 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
13389 }
13390
13391 std::string ErrorStr;
13392 raw_string_ostream ErrorFormatter(ErrorStr);
13393 ErrorFormatter << "Undefined external symbol ";
13394 ErrorFormatter << '"' << Symbol << '"';
13395 report_fatal_error(Twine(ErrorStr));
13396}
13397
13398//===----------------------------------------------------------------------===//
13399// SDNode Class
13400//===----------------------------------------------------------------------===//
13401
13404 return Const != nullptr && Const->isZero();
13405}
13406
13408 return V.isUndef() || isNullConstant(V);
13409}
13410
13413 return Const != nullptr && Const->isZero() && !Const->isNegative();
13414}
13415
13418 return Const != nullptr && Const->isAllOnes();
13419}
13420
13423 return Const != nullptr && Const->isOne();
13424}
13425
13428 return Const != nullptr && Const->isMinSignedValue();
13429}
13430
13431bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
13432 unsigned OperandNo) {
13433 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
13434 // TODO: Target-specific opcodes could be added.
13435 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
13436 /*AllowTruncation*/ true)) {
13437 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
13438 switch (Opcode) {
13439 case ISD::ADD:
13440 case ISD::OR:
13441 case ISD::XOR:
13442 case ISD::UMAX:
13443 return Const.isZero();
13444 case ISD::MUL:
13445 return Const.isOne();
13446 case ISD::AND:
13447 case ISD::UMIN:
13448 return Const.isAllOnes();
13449 case ISD::SMAX:
13450 return Const.isMinSignedValue();
13451 case ISD::SMIN:
13452 return Const.isMaxSignedValue();
13453 case ISD::SUB:
13454 case ISD::SHL:
13455 case ISD::SRA:
13456 case ISD::SRL:
13457 return OperandNo == 1 && Const.isZero();
13458 case ISD::UDIV:
13459 case ISD::SDIV:
13460 return OperandNo == 1 && Const.isOne();
13461 }
13462 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
13463 switch (Opcode) {
13464 case ISD::FADD:
13465 return ConstFP->isZero() &&
13466 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
13467 case ISD::FSUB:
13468 return OperandNo == 1 && ConstFP->isZero() &&
13469 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
13470 case ISD::FMUL:
13471 return ConstFP->isExactlyValue(1.0);
13472 case ISD::FDIV:
13473 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
13474 case ISD::FMINNUM:
13475 case ISD::FMAXNUM: {
13476 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13477 EVT VT = V.getValueType();
13478 const fltSemantics &Semantics = VT.getFltSemantics();
13479 APFloat NeutralAF = !Flags.hasNoNaNs()
13480 ? APFloat::getQNaN(Semantics)
13481 : !Flags.hasNoInfs()
13482 ? APFloat::getInf(Semantics)
13483 : APFloat::getLargest(Semantics);
13484 if (Opcode == ISD::FMAXNUM)
13485 NeutralAF.changeSign();
13486
13487 return ConstFP->isExactlyValue(NeutralAF);
13488 }
13489 }
13490 }
13491 return false;
13492}
13493
13495 while (V.getOpcode() == ISD::BITCAST)
13496 V = V.getOperand(0);
13497 return V;
13498}
13499
13501 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
13502 V = V.getOperand(0);
13503 return V;
13504}
13505
13507 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
13508 V = V.getOperand(0);
13509 return V;
13510}
13511
13513 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13514 SDValue InVec = V.getOperand(0);
13515 SDValue EltNo = V.getOperand(2);
13516 EVT VT = InVec.getValueType();
13517 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
13518 if (IndexC && VT.isFixedLengthVector() &&
13519 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
13520 !DemandedElts[IndexC->getZExtValue()]) {
13521 V = InVec;
13522 continue;
13523 }
13524 break;
13525 }
13526 return V;
13527}
13528
13530 while (V.getOpcode() == ISD::TRUNCATE)
13531 V = V.getOperand(0);
13532 return V;
13533}
13534
13535bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
13536 if (V.getOpcode() != ISD::XOR)
13537 return false;
13538 V = peekThroughBitcasts(V.getOperand(1));
13539 unsigned NumBits = V.getScalarValueSizeInBits();
13540 ConstantSDNode *C =
13541 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
13542 return C && (C->getAPIntValue().countr_one() >= NumBits);
13543}
13544
13546 bool AllowTruncation) {
13547 EVT VT = N.getValueType();
13548 APInt DemandedElts = VT.isFixedLengthVector()
13550 : APInt(1, 1);
13551 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
13552}
13553
13555 bool AllowUndefs,
13556 bool AllowTruncation) {
13558 return CN;
13559
13560 // SplatVectors can truncate their operands. Ignore that case here unless
13561 // AllowTruncation is set.
13562 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
13563 EVT VecEltVT = N->getValueType(0).getVectorElementType();
13564 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
13565 EVT CVT = CN->getValueType(0);
13566 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13567 if (AllowTruncation || CVT == VecEltVT)
13568 return CN;
13569 }
13570 }
13571
13573 BitVector UndefElements;
13574 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13575
13576 // BuildVectors can truncate their operands. Ignore that case here unless
13577 // AllowTruncation is set.
13578 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13579 if (CN && (UndefElements.none() || AllowUndefs)) {
13580 EVT CVT = CN->getValueType(0);
13581 EVT NSVT = N.getValueType().getScalarType();
13582 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13583 if (AllowTruncation || (CVT == NSVT))
13584 return CN;
13585 }
13586 }
13587
13588 return nullptr;
13589}
13590
13592 EVT VT = N.getValueType();
13593 APInt DemandedElts = VT.isFixedLengthVector()
13595 : APInt(1, 1);
13596 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13597}
13598
13600 const APInt &DemandedElts,
13601 bool AllowUndefs) {
13603 return CN;
13604
13606 BitVector UndefElements;
13607 ConstantFPSDNode *CN =
13608 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13609 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13610 if (CN && (UndefElements.none() || AllowUndefs))
13611 return CN;
13612 }
13613
13614 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13615 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13616 return CN;
13617
13618 return nullptr;
13619}
13620
13621bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13622 // TODO: may want to use peekThroughBitcast() here.
13623 ConstantSDNode *C =
13624 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13625 return C && C->isZero();
13626}
13627
13628bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13629 ConstantSDNode *C =
13630 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13631 return C && C->isOne();
13632}
13633
13634bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13635 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13636 return C && C->isExactlyValue(1.0);
13637}
13638
13639bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13641 unsigned BitWidth = N.getScalarValueSizeInBits();
13642 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13643 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13644}
13645
13646bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13647 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13648 return C && APInt::isSameValue(C->getAPIntValue(),
13649 APInt(C->getAPIntValue().getBitWidth(), 1));
13650}
13651
13652bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13654 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13655 return C && C->isZero();
13656}
13657
13658bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13659 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13660 return C && C->isZero();
13661}
13662
13666
13668 unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt,
13670 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MemRefs(memrefs) {
13671 bool IsVolatile = false;
13672 bool IsNonTemporal = false;
13673 bool IsDereferenceable = true;
13674 bool IsInvariant = true;
13675 for (const MachineMemOperand *MMO : memoperands()) {
13676 IsVolatile |= MMO->isVolatile();
13677 IsNonTemporal |= MMO->isNonTemporal();
13678 IsDereferenceable &= MMO->isDereferenceable();
13679 IsInvariant &= MMO->isInvariant();
13680 }
13681 MemSDNodeBits.IsVolatile = IsVolatile;
13682 MemSDNodeBits.IsNonTemporal = IsNonTemporal;
13683 MemSDNodeBits.IsDereferenceable = IsDereferenceable;
13684 MemSDNodeBits.IsInvariant = IsInvariant;
13685
13686 // For the single-MMO case, we check here that the size of the memory operand
13687 // fits within the size of the MMO. This is because the MMO might indicate
13688 // only a possible address range instead of specifying the affected memory
13689 // addresses precisely.
13692 getMemOperand()->getSize().getValue())) &&
13693 "Size mismatch!");
13694}
13695
13696/// Profile - Gather unique data for the node.
13697///
13699 AddNodeIDNode(ID, this);
13700}
13701
13702namespace {
13703
13704 struct EVTArray {
13705 std::vector<EVT> VTs;
13706
13707 EVTArray() {
13708 VTs.reserve(MVT::VALUETYPE_SIZE);
13709 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13710 VTs.push_back(MVT((MVT::SimpleValueType)i));
13711 }
13712 };
13713
13714} // end anonymous namespace
13715
13716/// getValueTypeList - Return a pointer to the specified value type.
13717///
13718const EVT *SDNode::getValueTypeList(MVT VT) {
13719 static EVTArray SimpleVTArray;
13720
13721 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13722 return &SimpleVTArray.VTs[VT.SimpleTy];
13723}
13724
13725/// hasAnyUseOfValue - Return true if there are any use of the indicated
13726/// value. This method ignores uses of other values defined by this operation.
13727bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13728 assert(Value < getNumValues() && "Bad value!");
13729
13730 for (SDUse &U : uses())
13731 if (U.getResNo() == Value)
13732 return true;
13733
13734 return false;
13735}
13736
13737/// isOnlyUserOf - Return true if this node is the only use of N.
13738bool SDNode::isOnlyUserOf(const SDNode *N) const {
13739 bool Seen = false;
13740 for (const SDNode *User : N->users()) {
13741 if (User == this)
13742 Seen = true;
13743 else
13744 return false;
13745 }
13746
13747 return Seen;
13748}
13749
13750/// Return true if the only users of N are contained in Nodes.
13752 bool Seen = false;
13753 for (const SDNode *User : N->users()) {
13754 if (llvm::is_contained(Nodes, User))
13755 Seen = true;
13756 else
13757 return false;
13758 }
13759
13760 return Seen;
13761}
13762
13763/// Return true if the referenced return value is an operand of N.
13764bool SDValue::isOperandOf(const SDNode *N) const {
13765 return is_contained(N->op_values(), *this);
13766}
13767
13768bool SDNode::isOperandOf(const SDNode *N) const {
13769 return any_of(N->op_values(),
13770 [this](SDValue Op) { return this == Op.getNode(); });
13771}
13772
13773/// reachesChainWithoutSideEffects - Return true if this operand (which must
13774/// be a chain) reaches the specified operand without crossing any
13775/// side-effecting instructions on any chain path. In practice, this looks
13776/// through token factors and non-volatile loads. In order to remain efficient,
13777/// this only looks a couple of nodes in, it does not do an exhaustive search.
13778///
13779/// Note that we only need to examine chains when we're searching for
13780/// side-effects; SelectionDAG requires that all side-effects are represented
13781/// by chains, even if another operand would force a specific ordering. This
13782/// constraint is necessary to allow transformations like splitting loads.
13784 unsigned Depth) const {
13785 if (*this == Dest) return true;
13786
13787 // Don't search too deeply, we just want to be able to see through
13788 // TokenFactor's etc.
13789 if (Depth == 0) return false;
13790
13791 // If this is a token factor, all inputs to the TF happen in parallel.
13792 if (getOpcode() == ISD::TokenFactor) {
13793 // First, try a shallow search.
13794 if (is_contained((*this)->ops(), Dest)) {
13795 // We found the chain we want as an operand of this TokenFactor.
13796 // Essentially, we reach the chain without side-effects if we could
13797 // serialize the TokenFactor into a simple chain of operations with
13798 // Dest as the last operation. This is automatically true if the
13799 // chain has one use: there are no other ordering constraints.
13800 // If the chain has more than one use, we give up: some other
13801 // use of Dest might force a side-effect between Dest and the current
13802 // node.
13803 if (Dest.hasOneUse())
13804 return true;
13805 }
13806 // Next, try a deep search: check whether every operand of the TokenFactor
13807 // reaches Dest.
13808 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13809 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13810 });
13811 }
13812
13813 // Loads don't have side effects, look through them.
13814 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13815 if (Ld->isUnordered())
13816 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13817 }
13818 return false;
13819}
13820
13821bool SDNode::hasPredecessor(const SDNode *N) const {
13824 Worklist.push_back(this);
13825 return hasPredecessorHelper(N, Visited, Worklist);
13826}
13827
13829 this->Flags &= Flags;
13830}
13831
13832SDValue
13834 ArrayRef<ISD::NodeType> CandidateBinOps,
13835 bool AllowPartials) {
13836 // The pattern must end in an extract from index 0.
13837 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13838 !isNullConstant(Extract->getOperand(1)))
13839 return SDValue();
13840
13841 // Match against one of the candidate binary ops.
13842 SDValue Op = Extract->getOperand(0);
13843 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13844 return Op.getOpcode() == unsigned(BinOp);
13845 }))
13846 return SDValue();
13847
13848 // Floating-point reductions may require relaxed constraints on the final step
13849 // of the reduction because they may reorder intermediate operations.
13850 unsigned CandidateBinOp = Op.getOpcode();
13851 if (Op.getValueType().isFloatingPoint()) {
13852 SDNodeFlags Flags = Op->getFlags();
13853 switch (CandidateBinOp) {
13854 case ISD::FADD:
13855 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13856 return SDValue();
13857 break;
13858 default:
13859 llvm_unreachable("Unhandled FP opcode for binop reduction");
13860 }
13861 }
13862
13863 // Matching failed - attempt to see if we did enough stages that a partial
13864 // reduction from a subvector is possible.
13865 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13866 if (!AllowPartials || !Op)
13867 return SDValue();
13868 EVT OpVT = Op.getValueType();
13869 EVT OpSVT = OpVT.getScalarType();
13870 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13871 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13872 return SDValue();
13873 BinOp = (ISD::NodeType)CandidateBinOp;
13874 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13875 };
13876
13877 // At each stage, we're looking for something that looks like:
13878 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13879 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13880 // i32 undef, i32 undef, i32 undef, i32 undef>
13881 // %a = binop <8 x i32> %op, %s
13882 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13883 // we expect something like:
13884 // <4,5,6,7,u,u,u,u>
13885 // <2,3,u,u,u,u,u,u>
13886 // <1,u,u,u,u,u,u,u>
13887 // While a partial reduction match would be:
13888 // <2,3,u,u,u,u,u,u>
13889 // <1,u,u,u,u,u,u,u>
13890 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13891 SDValue PrevOp;
13892 for (unsigned i = 0; i < Stages; ++i) {
13893 unsigned MaskEnd = (1 << i);
13894
13895 if (Op.getOpcode() != CandidateBinOp)
13896 return PartialReduction(PrevOp, MaskEnd);
13897
13898 SDValue Op0 = Op.getOperand(0);
13899 SDValue Op1 = Op.getOperand(1);
13900
13902 if (Shuffle) {
13903 Op = Op1;
13904 } else {
13905 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13906 Op = Op0;
13907 }
13908
13909 // The first operand of the shuffle should be the same as the other operand
13910 // of the binop.
13911 if (!Shuffle || Shuffle->getOperand(0) != Op)
13912 return PartialReduction(PrevOp, MaskEnd);
13913
13914 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13915 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13916 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13917 return PartialReduction(PrevOp, MaskEnd);
13918
13919 PrevOp = Op;
13920 }
13921
13922 // Handle subvector reductions, which tend to appear after the shuffle
13923 // reduction stages.
13924 while (Op.getOpcode() == CandidateBinOp) {
13925 unsigned NumElts = Op.getValueType().getVectorNumElements();
13926 SDValue Op0 = Op.getOperand(0);
13927 SDValue Op1 = Op.getOperand(1);
13928 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13930 Op0.getOperand(0) != Op1.getOperand(0))
13931 break;
13932 SDValue Src = Op0.getOperand(0);
13933 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13934 if (NumSrcElts != (2 * NumElts))
13935 break;
13936 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13937 Op1.getConstantOperandAPInt(1) == NumElts) &&
13938 !(Op1.getConstantOperandAPInt(1) == 0 &&
13939 Op0.getConstantOperandAPInt(1) == NumElts))
13940 break;
13941 Op = Src;
13942 }
13943
13944 BinOp = (ISD::NodeType)CandidateBinOp;
13945 return Op;
13946}
13947
13949 EVT VT = N->getValueType(0);
13950 EVT EltVT = VT.getVectorElementType();
13951 unsigned NE = VT.getVectorNumElements();
13952
13953 SDLoc dl(N);
13954
13955 // If ResNE is 0, fully unroll the vector op.
13956 if (ResNE == 0)
13957 ResNE = NE;
13958 else if (NE > ResNE)
13959 NE = ResNE;
13960
13961 if (N->getNumValues() == 2) {
13962 SmallVector<SDValue, 8> Scalars0, Scalars1;
13963 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13964 EVT VT1 = N->getValueType(1);
13965 EVT EltVT1 = VT1.getVectorElementType();
13966
13967 unsigned i;
13968 for (i = 0; i != NE; ++i) {
13969 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13970 SDValue Operand = N->getOperand(j);
13971 EVT OperandVT = Operand.getValueType();
13972
13973 // A vector operand; extract a single element.
13974 EVT OperandEltVT = OperandVT.getVectorElementType();
13975 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13976 }
13977
13978 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13979 Scalars0.push_back(EltOp);
13980 Scalars1.push_back(EltOp.getValue(1));
13981 }
13982
13983 for (; i < ResNE; ++i) {
13984 Scalars0.push_back(getUNDEF(EltVT));
13985 Scalars1.push_back(getUNDEF(EltVT1));
13986 }
13987
13988 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13989 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13990 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13991 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13992 return getMergeValues({Vec0, Vec1}, dl);
13993 }
13994
13995 assert(N->getNumValues() == 1 &&
13996 "Can't unroll a vector with multiple results!");
13997
13999 SmallVector<SDValue, 4> Operands(N->getNumOperands());
14000
14001 unsigned i;
14002 for (i= 0; i != NE; ++i) {
14003 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
14004 SDValue Operand = N->getOperand(j);
14005 EVT OperandVT = Operand.getValueType();
14006 if (OperandVT.isVector()) {
14007 // A vector operand; extract a single element.
14008 EVT OperandEltVT = OperandVT.getVectorElementType();
14009 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
14010 } else {
14011 // A scalar operand; just use it as is.
14012 Operands[j] = Operand;
14013 }
14014 }
14015
14016 switch (N->getOpcode()) {
14017 default: {
14018 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
14019 N->getFlags()));
14020 break;
14021 }
14022 case ISD::VSELECT:
14023 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
14024 break;
14025 case ISD::SHL:
14026 case ISD::SRA:
14027 case ISD::SRL:
14028 case ISD::ROTL:
14029 case ISD::ROTR:
14030 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
14031 getShiftAmountOperand(Operands[0].getValueType(),
14032 Operands[1])));
14033 break;
14035 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
14036 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
14037 Operands[0],
14038 getValueType(ExtVT)));
14039 break;
14040 }
14041 case ISD::ADDRSPACECAST: {
14042 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
14043 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
14044 ASC->getSrcAddressSpace(),
14045 ASC->getDestAddressSpace()));
14046 break;
14047 }
14048 }
14049 }
14050
14051 for (; i < ResNE; ++i)
14052 Scalars.push_back(getUNDEF(EltVT));
14053
14054 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
14055 return getBuildVector(VecVT, dl, Scalars);
14056}
14057
14058std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
14059 SDNode *N, unsigned ResNE) {
14060 unsigned Opcode = N->getOpcode();
14061 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
14062 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
14063 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
14064 "Expected an overflow opcode");
14065
14066 EVT ResVT = N->getValueType(0);
14067 EVT OvVT = N->getValueType(1);
14068 EVT ResEltVT = ResVT.getVectorElementType();
14069 EVT OvEltVT = OvVT.getVectorElementType();
14070 SDLoc dl(N);
14071
14072 // If ResNE is 0, fully unroll the vector op.
14073 unsigned NE = ResVT.getVectorNumElements();
14074 if (ResNE == 0)
14075 ResNE = NE;
14076 else if (NE > ResNE)
14077 NE = ResNE;
14078
14079 SmallVector<SDValue, 8> LHSScalars;
14080 SmallVector<SDValue, 8> RHSScalars;
14081 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
14082 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
14083
14084 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
14085 SDVTList VTs = getVTList(ResEltVT, SVT);
14086 SmallVector<SDValue, 8> ResScalars;
14087 SmallVector<SDValue, 8> OvScalars;
14088 for (unsigned i = 0; i < NE; ++i) {
14089 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
14090 SDValue Ov =
14091 getSelect(dl, OvEltVT, Res.getValue(1),
14092 getBoolConstant(true, dl, OvEltVT, ResVT),
14093 getConstant(0, dl, OvEltVT));
14094
14095 ResScalars.push_back(Res);
14096 OvScalars.push_back(Ov);
14097 }
14098
14099 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
14100 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
14101
14102 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
14103 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
14104 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
14105 getBuildVector(NewOvVT, dl, OvScalars));
14106}
14107
14110 unsigned Bytes,
14111 int Dist) const {
14112 if (LD->isVolatile() || Base->isVolatile())
14113 return false;
14114 // TODO: probably too restrictive for atomics, revisit
14115 if (!LD->isSimple())
14116 return false;
14117 if (LD->isIndexed() || Base->isIndexed())
14118 return false;
14119 if (LD->getChain() != Base->getChain())
14120 return false;
14121 EVT VT = LD->getMemoryVT();
14122 if (VT.getSizeInBits() / 8 != Bytes)
14123 return false;
14124
14125 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
14126 auto LocDecomp = BaseIndexOffset::match(LD, *this);
14127
14128 int64_t Offset = 0;
14129 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
14130 return (Dist * (int64_t)Bytes == Offset);
14131 return false;
14132}
14133
14134/// InferPtrAlignment - Infer alignment of a load / store address. Return
14135/// std::nullopt if it cannot be inferred.
14137 // If this is a GlobalAddress + cst, return the alignment.
14138 const GlobalValue *GV = nullptr;
14139 int64_t GVOffset = 0;
14140 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
14141 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
14142 KnownBits Known(PtrWidth);
14144 unsigned AlignBits = Known.countMinTrailingZeros();
14145 if (AlignBits)
14146 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
14147 }
14148
14149 // If this is a direct reference to a stack slot, use information about the
14150 // stack slot's alignment.
14151 int FrameIdx = INT_MIN;
14152 int64_t FrameOffset = 0;
14154 FrameIdx = FI->getIndex();
14155 } else if (isBaseWithConstantOffset(Ptr) &&
14157 // Handle FI+Cst
14158 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
14159 FrameOffset = Ptr.getConstantOperandVal(1);
14160 }
14161
14162 if (FrameIdx != INT_MIN) {
14164 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
14165 }
14166
14167 return std::nullopt;
14168}
14169
14170/// Split the scalar node with EXTRACT_ELEMENT using the provided
14171/// VTs and return the low/high part.
14172std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
14173 const SDLoc &DL,
14174 const EVT &LoVT,
14175 const EVT &HiVT) {
14176 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
14177 "Split node must be a scalar type");
14178 SDValue Lo =
14180 SDValue Hi =
14182 return std::make_pair(Lo, Hi);
14183}
14184
14185/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
14186/// which is split (or expanded) into two not necessarily identical pieces.
14187std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
14188 // Currently all types are split in half.
14189 EVT LoVT, HiVT;
14190 if (!VT.isVector())
14191 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
14192 else
14193 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
14194
14195 return std::make_pair(LoVT, HiVT);
14196}
14197
14198/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
14199/// type, dependent on an enveloping VT that has been split into two identical
14200/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
14201std::pair<EVT, EVT>
14203 bool *HiIsEmpty) const {
14204 EVT EltTp = VT.getVectorElementType();
14205 // Examples:
14206 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
14207 // custom VL=9 with enveloping VL=8/8 yields 8/1
14208 // custom VL=10 with enveloping VL=8/8 yields 8/2
14209 // etc.
14210 ElementCount VTNumElts = VT.getVectorElementCount();
14211 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
14212 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
14213 "Mixing fixed width and scalable vectors when enveloping a type");
14214 EVT LoVT, HiVT;
14215 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
14216 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
14217 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
14218 *HiIsEmpty = false;
14219 } else {
14220 // Flag that hi type has zero storage size, but return split envelop type
14221 // (this would be easier if vector types with zero elements were allowed).
14222 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
14223 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
14224 *HiIsEmpty = true;
14225 }
14226 return std::make_pair(LoVT, HiVT);
14227}
14228
14229/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
14230/// low/high part.
14231std::pair<SDValue, SDValue>
14232SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
14233 const EVT &HiVT) {
14234 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
14235 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
14236 "Splitting vector with an invalid mixture of fixed and scalable "
14237 "vector types");
14239 N.getValueType().getVectorMinNumElements() &&
14240 "More vector elements requested than available!");
14241 SDValue Lo, Hi;
14242 Lo = getExtractSubvector(DL, LoVT, N, 0);
14243 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
14244 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
14245 // IDX with the runtime scaling factor of the result vector type. For
14246 // fixed-width result vectors, that runtime scaling factor is 1.
14249 return std::make_pair(Lo, Hi);
14250}
14251
14252std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
14253 const SDLoc &DL) {
14254 // Split the vector length parameter.
14255 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
14256 EVT VT = N.getValueType();
14258 "Expecting the mask to be an evenly-sized vector");
14259 SDValue HalfNumElts = getElementCount(
14261 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
14262 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
14263 return std::make_pair(Lo, Hi);
14264}
14265
14266/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
14268 EVT VT = N.getValueType();
14271 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
14272}
14273
14276 unsigned Start, unsigned Count,
14277 EVT EltVT) {
14278 EVT VT = Op.getValueType();
14279 if (Count == 0)
14281 if (EltVT == EVT())
14282 EltVT = VT.getVectorElementType();
14283 SDLoc SL(Op);
14284 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
14285 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
14286 }
14287}
14288
14289// getAddressSpace - Return the address space this GlobalAddress belongs to.
14291 return getGlobal()->getType()->getAddressSpace();
14292}
14293
14296 return Val.MachineCPVal->getType();
14297 return Val.ConstVal->getType();
14298}
14299
14300bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
14301 unsigned &SplatBitSize,
14302 bool &HasAnyUndefs,
14303 unsigned MinSplatBits,
14304 bool IsBigEndian) const {
14305 EVT VT = getValueType(0);
14306 assert(VT.isVector() && "Expected a vector type");
14307 unsigned VecWidth = VT.getSizeInBits();
14308 if (MinSplatBits > VecWidth)
14309 return false;
14310
14311 // FIXME: The widths are based on this node's type, but build vectors can
14312 // truncate their operands.
14313 SplatValue = APInt(VecWidth, 0);
14314 SplatUndef = APInt(VecWidth, 0);
14315
14316 // Get the bits. Bits with undefined values (when the corresponding element
14317 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
14318 // in SplatValue. If any of the values are not constant, give up and return
14319 // false.
14320 unsigned int NumOps = getNumOperands();
14321 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
14322 unsigned EltWidth = VT.getScalarSizeInBits();
14323
14324 for (unsigned j = 0; j < NumOps; ++j) {
14325 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
14326 SDValue OpVal = getOperand(i);
14327 unsigned BitPos = j * EltWidth;
14328
14329 if (OpVal.isUndef())
14330 SplatUndef.setBits(BitPos, BitPos + EltWidth);
14331 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
14332 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
14333 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
14334 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
14335 else
14336 return false;
14337 }
14338
14339 // The build_vector is all constants or undefs. Find the smallest element
14340 // size that splats the vector.
14341 HasAnyUndefs = (SplatUndef != 0);
14342
14343 // FIXME: This does not work for vectors with elements less than 8 bits.
14344 while (VecWidth > 8) {
14345 // If we can't split in half, stop here.
14346 if (VecWidth & 1)
14347 break;
14348
14349 unsigned HalfSize = VecWidth / 2;
14350 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
14351 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
14352 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
14353 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
14354
14355 // If the two halves do not match (ignoring undef bits), stop here.
14356 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
14357 MinSplatBits > HalfSize)
14358 break;
14359
14360 SplatValue = HighValue | LowValue;
14361 SplatUndef = HighUndef & LowUndef;
14362
14363 VecWidth = HalfSize;
14364 }
14365
14366 // FIXME: The loop above only tries to split in halves. But if the input
14367 // vector for example is <3 x i16> it wouldn't be able to detect a
14368 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
14369 // optimizations. I guess that back in the days when this helper was created
14370 // vectors normally was power-of-2 sized.
14371
14372 SplatBitSize = VecWidth;
14373 return true;
14374}
14375
14377 BitVector *UndefElements) const {
14378 unsigned NumOps = getNumOperands();
14379 if (UndefElements) {
14380 UndefElements->clear();
14381 UndefElements->resize(NumOps);
14382 }
14383 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
14384 if (!DemandedElts)
14385 return SDValue();
14386 SDValue Splatted;
14387 for (unsigned i = 0; i != NumOps; ++i) {
14388 if (!DemandedElts[i])
14389 continue;
14390 SDValue Op = getOperand(i);
14391 if (Op.isUndef()) {
14392 if (UndefElements)
14393 (*UndefElements)[i] = true;
14394 } else if (!Splatted) {
14395 Splatted = Op;
14396 } else if (Splatted != Op) {
14397 return SDValue();
14398 }
14399 }
14400
14401 if (!Splatted) {
14402 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
14403 assert(getOperand(FirstDemandedIdx).isUndef() &&
14404 "Can only have a splat without a constant for all undefs.");
14405 return getOperand(FirstDemandedIdx);
14406 }
14407
14408 return Splatted;
14409}
14410
14412 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14413 return getSplatValue(DemandedElts, UndefElements);
14414}
14415
14417 SmallVectorImpl<SDValue> &Sequence,
14418 BitVector *UndefElements) const {
14419 unsigned NumOps = getNumOperands();
14420 Sequence.clear();
14421 if (UndefElements) {
14422 UndefElements->clear();
14423 UndefElements->resize(NumOps);
14424 }
14425 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
14426 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
14427 return false;
14428
14429 // Set the undefs even if we don't find a sequence (like getSplatValue).
14430 if (UndefElements)
14431 for (unsigned I = 0; I != NumOps; ++I)
14432 if (DemandedElts[I] && getOperand(I).isUndef())
14433 (*UndefElements)[I] = true;
14434
14435 // Iteratively widen the sequence length looking for repetitions.
14436 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
14437 Sequence.append(SeqLen, SDValue());
14438 for (unsigned I = 0; I != NumOps; ++I) {
14439 if (!DemandedElts[I])
14440 continue;
14441 SDValue &SeqOp = Sequence[I % SeqLen];
14443 if (Op.isUndef()) {
14444 if (!SeqOp)
14445 SeqOp = Op;
14446 continue;
14447 }
14448 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
14449 Sequence.clear();
14450 break;
14451 }
14452 SeqOp = Op;
14453 }
14454 if (!Sequence.empty())
14455 return true;
14456 }
14457
14458 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
14459 return false;
14460}
14461
14463 BitVector *UndefElements) const {
14464 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14465 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
14466}
14467
14470 BitVector *UndefElements) const {
14472 getSplatValue(DemandedElts, UndefElements));
14473}
14474
14477 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
14478}
14479
14482 BitVector *UndefElements) const {
14484 getSplatValue(DemandedElts, UndefElements));
14485}
14486
14491
14492int32_t
14494 uint32_t BitWidth) const {
14495 if (ConstantFPSDNode *CN =
14497 bool IsExact;
14498 APSInt IntVal(BitWidth);
14499 const APFloat &APF = CN->getValueAPF();
14500 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
14501 APFloat::opOK ||
14502 !IsExact)
14503 return -1;
14504
14505 return IntVal.exactLogBase2();
14506 }
14507 return -1;
14508}
14509
14511 bool IsLittleEndian, unsigned DstEltSizeInBits,
14512 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
14513 // Early-out if this contains anything but Undef/Constant/ConstantFP.
14514 if (!isConstant())
14515 return false;
14516
14517 unsigned NumSrcOps = getNumOperands();
14518 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
14519 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14520 "Invalid bitcast scale");
14521
14522 // Extract raw src bits.
14523 SmallVector<APInt> SrcBitElements(NumSrcOps,
14524 APInt::getZero(SrcEltSizeInBits));
14525 BitVector SrcUndeElements(NumSrcOps, false);
14526
14527 for (unsigned I = 0; I != NumSrcOps; ++I) {
14529 if (Op.isUndef()) {
14530 SrcUndeElements.set(I);
14531 continue;
14532 }
14533 auto *CInt = dyn_cast<ConstantSDNode>(Op);
14534 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
14535 assert((CInt || CFP) && "Unknown constant");
14536 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
14537 : CFP->getValueAPF().bitcastToAPInt();
14538 }
14539
14540 // Recast to dst width.
14541 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
14542 SrcBitElements, UndefElements, SrcUndeElements);
14543 return true;
14544}
14545
14546void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
14547 unsigned DstEltSizeInBits,
14548 SmallVectorImpl<APInt> &DstBitElements,
14549 ArrayRef<APInt> SrcBitElements,
14550 BitVector &DstUndefElements,
14551 const BitVector &SrcUndefElements) {
14552 unsigned NumSrcOps = SrcBitElements.size();
14553 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
14554 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14555 "Invalid bitcast scale");
14556 assert(NumSrcOps == SrcUndefElements.size() &&
14557 "Vector size mismatch");
14558
14559 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
14560 DstUndefElements.clear();
14561 DstUndefElements.resize(NumDstOps, false);
14562 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
14563
14564 // Concatenate src elements constant bits together into dst element.
14565 if (SrcEltSizeInBits <= DstEltSizeInBits) {
14566 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
14567 for (unsigned I = 0; I != NumDstOps; ++I) {
14568 DstUndefElements.set(I);
14569 APInt &DstBits = DstBitElements[I];
14570 for (unsigned J = 0; J != Scale; ++J) {
14571 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14572 if (SrcUndefElements[Idx])
14573 continue;
14574 DstUndefElements.reset(I);
14575 const APInt &SrcBits = SrcBitElements[Idx];
14576 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
14577 "Illegal constant bitwidths");
14578 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
14579 }
14580 }
14581 return;
14582 }
14583
14584 // Split src element constant bits into dst elements.
14585 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14586 for (unsigned I = 0; I != NumSrcOps; ++I) {
14587 if (SrcUndefElements[I]) {
14588 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14589 continue;
14590 }
14591 const APInt &SrcBits = SrcBitElements[I];
14592 for (unsigned J = 0; J != Scale; ++J) {
14593 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14594 APInt &DstBits = DstBitElements[Idx];
14595 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14596 }
14597 }
14598}
14599
14601 for (const SDValue &Op : op_values()) {
14602 unsigned Opc = Op.getOpcode();
14603 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14604 return false;
14605 }
14606 return true;
14607}
14608
14609std::optional<std::pair<APInt, APInt>>
14611 unsigned NumOps = getNumOperands();
14612 if (NumOps < 2)
14613 return std::nullopt;
14614
14615 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14616 APInt Start, Stride;
14617 int FirstIdx = -1, SecondIdx = -1;
14618
14619 // Find the first two non-undef constant elements to determine Start and
14620 // Stride, then verify all remaining elements match the sequence.
14621 for (unsigned I = 0; I < NumOps; ++I) {
14623 if (Op->isUndef())
14624 continue;
14625 if (!isa<ConstantSDNode>(Op))
14626 return std::nullopt;
14627
14628 APInt Val = getConstantOperandAPInt(I).trunc(EltSize);
14629 if (FirstIdx < 0) {
14630 FirstIdx = I;
14631 Start = Val;
14632 } else if (SecondIdx < 0) {
14633 SecondIdx = I;
14634 // Compute stride using modular arithmetic. Simple division would handle
14635 // common strides (1, 2, -1, etc.), but modular inverse maximizes matches.
14636 // Example: <0, poison, poison, 0xFF> has stride 0x55 since 3*0x55 = 0xFF
14637 // Note that modular arithmetic is agnostic to signed/unsigned.
14638 unsigned IdxDiff = I - FirstIdx;
14639 APInt ValDiff = Val - Start;
14640
14641 // Step 1: Factor out common powers of 2 from IdxDiff and ValDiff.
14642 unsigned CommonPow2Bits = llvm::countr_zero(IdxDiff);
14643 if (ValDiff.countr_zero() < CommonPow2Bits)
14644 return std::nullopt; // ValDiff not divisible by 2^CommonPow2Bits
14645 IdxDiff >>= CommonPow2Bits;
14646 ValDiff.lshrInPlace(CommonPow2Bits);
14647
14648 // Step 2: IdxDiff is now odd, so its inverse mod 2^EltSize exists.
14649 // TODO: There are 2^CommonPow2Bits valid strides; currently we only try
14650 // one, but we could try all candidates to handle more cases.
14651 Stride = ValDiff * APInt(EltSize, IdxDiff).multiplicativeInverse();
14652 if (Stride.isZero())
14653 return std::nullopt;
14654
14655 // Step 3: Adjust Start based on the first defined element's index.
14656 Start -= Stride * FirstIdx;
14657 } else {
14658 // Verify this element matches the sequence.
14659 if (Val != Start + Stride * I)
14660 return std::nullopt;
14661 }
14662 }
14663
14664 // Need at least two defined elements.
14665 if (SecondIdx < 0)
14666 return std::nullopt;
14667
14668 return std::make_pair(Start, Stride);
14669}
14670
14672 // Find the first non-undef value in the shuffle mask.
14673 unsigned i, e;
14674 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14675 /* search */;
14676
14677 // If all elements are undefined, this shuffle can be considered a splat
14678 // (although it should eventually get simplified away completely).
14679 if (i == e)
14680 return true;
14681
14682 // Make sure all remaining elements are either undef or the same as the first
14683 // non-undef value.
14684 for (int Idx = Mask[i]; i != e; ++i)
14685 if (Mask[i] >= 0 && Mask[i] != Idx)
14686 return false;
14687 return true;
14688}
14689
14690// Returns true if it is a constant integer BuildVector or constant integer,
14691// possibly hidden by a bitcast.
14693 SDValue N, bool AllowOpaques) const {
14695
14696 if (auto *C = dyn_cast<ConstantSDNode>(N))
14697 return AllowOpaques || !C->isOpaque();
14698
14700 return true;
14701
14702 // Treat a GlobalAddress supporting constant offset folding as a
14703 // constant integer.
14704 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14705 if (GA->getOpcode() == ISD::GlobalAddress &&
14706 TLI->isOffsetFoldingLegal(GA))
14707 return true;
14708
14709 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14710 isa<ConstantSDNode>(N.getOperand(0)))
14711 return true;
14712 return false;
14713}
14714
14715// Returns true if it is a constant float BuildVector or constant float.
14718 return true;
14719
14721 return true;
14722
14723 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14724 isa<ConstantFPSDNode>(N.getOperand(0)))
14725 return true;
14726
14727 return false;
14728}
14729
14730std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14731 ConstantSDNode *Const =
14732 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14733 if (!Const)
14734 return std::nullopt;
14735
14736 EVT VT = N->getValueType(0);
14737 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14738 switch (TLI->getBooleanContents(N.getValueType())) {
14740 if (CVal.isOne())
14741 return true;
14742 if (CVal.isZero())
14743 return false;
14744 return std::nullopt;
14746 if (CVal.isAllOnes())
14747 return true;
14748 if (CVal.isZero())
14749 return false;
14750 return std::nullopt;
14752 return CVal[0];
14753 }
14754 llvm_unreachable("Unknown BooleanContent enum");
14755}
14756
14757void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14758 assert(!Node->OperandList && "Node already has operands");
14760 "too many operands to fit into SDNode");
14761 SDUse *Ops = OperandRecycler.allocate(
14762 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14763
14764 bool IsDivergent = false;
14765 for (unsigned I = 0; I != Vals.size(); ++I) {
14766 Ops[I].setUser(Node);
14767 Ops[I].setInitial(Vals[I]);
14768 EVT VT = Ops[I].getValueType();
14769
14770 // Skip Chain. It does not carry divergence.
14771 if (VT != MVT::Other &&
14772 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14773 Ops[I].getNode()->isDivergent()) {
14774 IsDivergent = true;
14775 }
14776 }
14777 Node->NumOperands = Vals.size();
14778 Node->OperandList = Ops;
14779 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14780 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14781 Node->SDNodeBits.IsDivergent = IsDivergent;
14782 }
14783 checkForCycles(Node);
14784}
14785
14788 size_t Limit = SDNode::getMaxNumOperands();
14789 while (Vals.size() > Limit) {
14790 unsigned SliceIdx = Vals.size() - Limit;
14791 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14792 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14793 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14794 Vals.emplace_back(NewTF);
14795 }
14796 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14797}
14798
14800 EVT VT, SDNodeFlags Flags) {
14801 switch (Opcode) {
14802 default:
14803 return SDValue();
14804 case ISD::ADD:
14805 case ISD::OR:
14806 case ISD::XOR:
14807 case ISD::UMAX:
14808 return getConstant(0, DL, VT);
14809 case ISD::MUL:
14810 return getConstant(1, DL, VT);
14811 case ISD::AND:
14812 case ISD::UMIN:
14813 return getAllOnesConstant(DL, VT);
14814 case ISD::SMAX:
14816 case ISD::SMIN:
14818 case ISD::FADD:
14819 // If flags allow, prefer positive zero since it's generally cheaper
14820 // to materialize on most targets.
14821 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14822 case ISD::FMUL:
14823 return getConstantFP(1.0, DL, VT);
14824 case ISD::FMINNUM:
14825 case ISD::FMAXNUM: {
14826 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14827 const fltSemantics &Semantics = VT.getFltSemantics();
14828 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14829 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14830 APFloat::getLargest(Semantics);
14831 if (Opcode == ISD::FMAXNUM)
14832 NeutralAF.changeSign();
14833
14834 return getConstantFP(NeutralAF, DL, VT);
14835 }
14836 case ISD::FMINIMUM:
14837 case ISD::FMAXIMUM: {
14838 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14839 const fltSemantics &Semantics = VT.getFltSemantics();
14840 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14841 : APFloat::getLargest(Semantics);
14842 if (Opcode == ISD::FMAXIMUM)
14843 NeutralAF.changeSign();
14844
14845 return getConstantFP(NeutralAF, DL, VT);
14846 }
14847
14848 }
14849}
14850
14851/// Helper used to make a call to a library function that has one argument of
14852/// pointer type.
14853///
14854/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14855/// used to get or set floating-point state. They have one argument of pointer
14856/// type, which points to the memory region containing bits of the
14857/// floating-point state. The value returned by such function is ignored in the
14858/// created call.
14859///
14860/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14861/// \param Ptr Pointer used to save/load state.
14862/// \param InChain Ingoing token chain.
14863/// \returns Outgoing chain token.
14865 SDValue InChain,
14866 const SDLoc &DLoc) {
14867 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14869 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14870 RTLIB::LibcallImpl LibcallImpl =
14871 Libcalls->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
14872 if (LibcallImpl == RTLIB::Unsupported)
14873 reportFatalUsageError("emitting call to unsupported libcall");
14874
14875 SDValue Callee =
14876 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
14878 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14879 Libcalls->getLibcallImplCallingConv(LibcallImpl),
14880 Type::getVoidTy(*getContext()), Callee, std::move(Args));
14881 return TLI->LowerCallTo(CLI).second;
14882}
14883
14885 assert(From && To && "Invalid SDNode; empty source SDValue?");
14886 auto I = SDEI.find(From);
14887 if (I == SDEI.end())
14888 return;
14889
14890 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14891 // the iterator, hence the need to make a copy to prevent a use-after-free.
14892 NodeExtraInfo NEI = I->second;
14893 if (LLVM_LIKELY(!NEI.PCSections)) {
14894 // No deep copy required for the types of extra info set.
14895 //
14896 // FIXME: Investigate if other types of extra info also need deep copy. This
14897 // depends on the types of nodes they can be attached to: if some extra info
14898 // is only ever attached to nodes where a replacement To node is always the
14899 // node where later use and propagation of the extra info has the intended
14900 // semantics, no deep copy is required.
14901 SDEI[To] = std::move(NEI);
14902 return;
14903 }
14904
14905 const SDNode *EntrySDN = getEntryNode().getNode();
14906
14907 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14908 // through the replacement of From with To. Otherwise, replacements of a node
14909 // (From) with more complex nodes (To and its operands) may result in lost
14910 // extra info where the root node (To) is insignificant in further propagating
14911 // and using extra info when further lowering to MIR.
14912 //
14913 // In the first step pre-populate the visited set with the nodes reachable
14914 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14915 // DAG that is not new and should be left untouched.
14916 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14917 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14918 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14919 if (MaxDepth == 0) {
14920 // Remember this node in case we need to increase MaxDepth and continue
14921 // populating FromReach from this node.
14922 Leafs.emplace_back(N);
14923 return;
14924 }
14925 if (!FromReach.insert(N).second)
14926 return;
14927 for (const SDValue &Op : N->op_values())
14928 Self(Self, Op.getNode(), MaxDepth - 1);
14929 };
14930
14931 // Copy extra info to To and all its transitive operands (that are new).
14933 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14934 if (FromReach.contains(N))
14935 return true;
14936 if (!Visited.insert(N).second)
14937 return true;
14938 if (EntrySDN == N)
14939 return false;
14940 for (const SDValue &Op : N->op_values()) {
14941 if (N == To && Op.getNode() == EntrySDN) {
14942 // Special case: New node's operand is the entry node; just need to
14943 // copy extra info to new node.
14944 break;
14945 }
14946 if (!Self(Self, Op.getNode()))
14947 return false;
14948 }
14949 // Copy only if entry node was not reached.
14950 SDEI[N] = std::move(NEI);
14951 return true;
14952 };
14953
14954 // We first try with a lower MaxDepth, assuming that the path to common
14955 // operands between From and To is relatively short. This significantly
14956 // improves performance in the common case. The initial MaxDepth is big
14957 // enough to avoid retry in the common case; the last MaxDepth is large
14958 // enough to avoid having to use the fallback below (and protects from
14959 // potential stack exhaustion from recursion).
14960 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14961 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14962 // StartFrom is the previous (or initial) set of leafs reachable at the
14963 // previous maximum depth.
14965 std::swap(StartFrom, Leafs);
14966 for (const SDNode *N : StartFrom)
14967 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14968 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14969 return;
14970 // This should happen very rarely (reached the entry node).
14971 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14972 assert(!Leafs.empty());
14973 }
14974
14975 // This should not happen - but if it did, that means the subgraph reachable
14976 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14977 // could not visit all reachable common operands. Consequently, we were able
14978 // to reach the entry node.
14979 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14980 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14981 // Best-effort fallback if assertions disabled.
14982 SDEI[To] = std::move(NEI);
14983}
14984
14985#ifndef NDEBUG
14986static void checkForCyclesHelper(const SDNode *N,
14989 const llvm::SelectionDAG *DAG) {
14990 // If this node has already been checked, don't check it again.
14991 if (Checked.count(N))
14992 return;
14993
14994 // If a node has already been visited on this depth-first walk, reject it as
14995 // a cycle.
14996 if (!Visited.insert(N).second) {
14997 errs() << "Detected cycle in SelectionDAG\n";
14998 dbgs() << "Offending node:\n";
14999 N->dumprFull(DAG); dbgs() << "\n";
15000 abort();
15001 }
15002
15003 for (const SDValue &Op : N->op_values())
15004 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
15005
15006 Checked.insert(N);
15007 Visited.erase(N);
15008}
15009#endif
15010
15012 const llvm::SelectionDAG *DAG,
15013 bool force) {
15014#ifndef NDEBUG
15015 bool check = force;
15016#ifdef EXPENSIVE_CHECKS
15017 check = true;
15018#endif // EXPENSIVE_CHECKS
15019 if (check) {
15020 assert(N && "Checking nonexistent SDNode");
15023 checkForCyclesHelper(N, visited, checked, DAG);
15024 }
15025#endif // !NDEBUG
15026}
15027
15028void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
15029 checkForCycles(DAG->getRoot().getNode(), DAG, force);
15030}
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:849
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"))
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:912
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
Value * getOperand(unsigned i) const
Definition User.h:207
This class is used to represent an VP_GATHER node.
This class is used to represent a VP_LOAD node.
This class is used to represent an VP_SCATTER node.
This class is used to represent a VP_STORE node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS.multiplyCoefficientBy(X) will result in a value whos...
Definition TypeSize.h:269
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr bool isKnownEven() const
A return value of true indicates we know at compile time that the number of elements (vscale * Min) i...
Definition TypeSize.h:176
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:252
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:237
A raw_ostream that writes to an std::string.
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt clmulr(const APInt &LHS, const APInt &RHS)
Perform a reversed carry-less multiply.
Definition APInt.cpp: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
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:374
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:796
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:233
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:247
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:672
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition ISDOpcodes.h:69
@ GET_ACTIVE_LANE_MASK
GET_ACTIVE_LANE_MASK - this corrosponds to the llvm.get.active.lane.mask intrinsic.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:230
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:348
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition ISDOpcodes.h:185
@ ARITH_FENCE
ARITH_FENCE - This corresponds to a arithmetic fence intrinsic.
@ CTLS
Count leading redundant sign bits.
Definition ISDOpcodes.h:792
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:704
@ ATOMIC_LOAD_FMAXIMUM
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:765
@ AssertNoFPClass
AssertNoFPClass - These nodes record if a register contains a float value that is known to be not som...
Definition ISDOpcodes.h:78
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:649
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:614
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:48
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:576
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:224
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:850
@ TargetConstantFP
Definition ISDOpcodes.h:180
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:811
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:386
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:356
@ ATOMIC_LOAD_FMINIMUM
@ TargetFrameIndex
Definition ISDOpcodes.h:187
@ VECTOR_SPLICE_LEFT
VECTOR_SPLICE_LEFT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by OFFSET elements an...
Definition ISDOpcodes.h:653
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:899
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:888
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:727
@ LIFETIME_START
This corresponds to the llvm.lifetime.
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:978
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:805
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:328
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
@ ATOMIC_LOAD_UDEC_WRAP
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:500
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:926
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition ISDOpcodes.h:179
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:505
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:739
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:205
@ GET_FPENV_MEM
Gets the current floating-point environment.
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:735
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:710
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1,VEC2) right by OFFSET elements a...
Definition ISDOpcodes.h:657
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:304
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition ISDOpcodes.h:681
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:241
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:565
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ ExternalSymbol
Definition ISDOpcodes.h:93
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:959
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:699
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:921
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition ISDOpcodes.h:997
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Experimental vector histogram intrinsic Operands: Input Chain, Inc, Mask, Base, Index,...
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:945
@ VECREDUCE_FMINIMUM
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:856
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
@ VECREDUCE_SEQ_FMUL
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:833
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ ATOMIC_LOAD_UINC_WRAP
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:534
@ PARTIAL_REDUCE_SUMLA
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:365
@ SET_FPENV_MEM
Sets the current floating point environment.
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ TRUNCATE_SSAT_S
TRUNCATE_[SU]SAT_[SU] - Truncate for saturated operand [SU] located in middle, prefix for SAT means i...
Definition ISDOpcodes.h:871
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:722
@ TRUNCATE_USAT_U
Definition ISDOpcodes.h:875
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:338
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:213
@ TargetGlobalTLSAddress
Definition ISDOpcodes.h:186
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:556
LLVM_ABI NodeType getOppositeSignednessMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns the corresponding opcode with the opposi...
LLVM_ABI bool isBuildVectorOfConstantSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantSDNode or undef.
LLVM_ABI NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
bool isZEXTLoad(const SDNode *N)
Returns true if the specified node is a ZEXTLOAD.
bool matchUnaryFpPredicate(SDValue Op, std::function< bool(ConstantFPSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantFPSDNode predicate.
bool isExtOpcode(unsigned Opcode)
LLVM_ABI bool isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are 0 o...
LLVM_ABI bool isVectorShrinkable(const SDNode *N, unsigned NewEltSize, bool Signed)
Returns true if the specified node is a vector where all elements can be truncated to the specified e...
LLVM_ABI bool isVPBinaryOp(unsigned Opcode)
Whether this is a vector-predicated binary operation opcode.
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
LLVM_ABI std::optional< unsigned > getBaseOpcodeForVP(unsigned Opcode, bool hasFPExcept)
Translate this VP Opcode to its corresponding non-VP Opcode.
bool isTrueWhenEqual(CondCode Cond)
Return true if the specified condition returns true if the two operands to the condition are equal.
LLVM_ABI std::optional< unsigned > getVPMaskIdx(unsigned Opcode)
The operand position of the vector mask.
unsigned getUnorderedFlavor(CondCode Cond)
This function returns 0 if the condition is always false if an operand is a NaN, 1 if the condition i...
LLVM_ABI std::optional< unsigned > getVPExplicitVectorLengthIdx(unsigned Opcode)
The operand position of the explicit vector length parameter.
bool isEXTLoad(const SDNode *N)
Returns true if the specified node is a EXTLOAD.
LLVM_ABI bool allOperandsUndef(const SDNode *N)
Return true if the node has at least one operand and all operands of the specified node are ISD::UNDE...
LLVM_ABI bool isFreezeUndef(const SDNode *N)
Return true if the specified node is FREEZE(UNDEF).
LLVM_ABI CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
LLVM_ABI std::optional< unsigned > getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
LLVM_ABI bool isBuildVectorAllZeros(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are 0 or undef.
bool matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
LLVM_ABI bool isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
LLVM_ABI NodeType getInverseMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns ISD::(U|S)MAX and ISD::(U|S)MIN,...
LLVM_ABI bool matchBinaryPredicate(SDValue LHS, SDValue RHS, std::function< bool(ConstantSDNode *, ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTypeMismatch=false)
Attempt to match a binary predicate against a pair of scalar/splat constants or every element of a pa...
LLVM_ABI bool isVPReduction(unsigned Opcode)
Whether this is a vector-predicated reduction opcode.
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Hook for matching ConstantSDNode predicate.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
LLVM_ABI bool isBuildVectorOfConstantFPSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantFPSDNode or undef.
bool isSEXTLoad(const SDNode *N)
Returns true if the specified node is a SEXTLOAD.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LLVM_ABI bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef.
LLVM_ABI NodeType getVecReduceBaseOpcode(unsigned VecReduceOpcode)
Get underlying scalar opcode for VECREDUCE opcode.
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
LLVM_ABI bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
LLVM_ABI CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical OR between different comparisons of identical values: ((X op1 Y) | (X ...
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
LLVM_ABI Libcall getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMCPY_ELEMENT_UNORDERED_ATOMIC - Return MEMCPY_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMSET_ELEMENT_UNORDERED_ATOMIC - Return MEMSET_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMMOVE_ELEMENT_UNORDERED_ATOMIC - Return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_* value for the given e...
bool sd_match(SDNode *N, const SelectionDAG *DAG, Pattern &&P)
initializer< Ty > init(const Ty &Val)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:668
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
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.
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)