LLVM 23.0.0git
SelectionDAG.cpp
Go to the documentation of this file.
1//===- SelectionDAG.cpp - Implement the SelectionDAG data structures ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements the SelectionDAG class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/Twine.h"
51#include "llvm/IR/Constant.h"
52#include "llvm/IR/Constants.h"
53#include "llvm/IR/DataLayout.h"
55#include "llvm/IR/DebugLoc.h"
57#include "llvm/IR/Function.h"
58#include "llvm/IR/GlobalValue.h"
59#include "llvm/IR/Metadata.h"
60#include "llvm/IR/Type.h"
64#include "llvm/Support/Debug.h"
73#include <algorithm>
74#include <cassert>
75#include <cstdint>
76#include <cstdlib>
77#include <limits>
78#include <optional>
79#include <string>
80#include <utility>
81#include <vector>
82
83using namespace llvm;
84using namespace llvm::SDPatternMatch;
85
86/// makeVTList - Return an instance of the SDVTList struct initialized with the
87/// specified members.
88static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
89 SDVTList Res = {VTs, NumVTs};
90 return Res;
91}
92
93// Default null implementations of the callbacks.
97
98void SelectionDAG::DAGNodeDeletedListener::anchor() {}
99void SelectionDAG::DAGNodeInsertedListener::anchor() {}
100
101#define DEBUG_TYPE "selectiondag"
102
103static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
104 cl::Hidden, cl::init(true),
105 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
106
107static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
108 cl::desc("Number limit for gluing ld/st of memcpy."),
109 cl::Hidden, cl::init(0));
110
112 MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192),
113 cl::desc("DAG combiner limit number of steps when searching DAG "
114 "for predecessor nodes"));
115
117 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
118}
119
121
122//===----------------------------------------------------------------------===//
123// ConstantFPSDNode Class
124//===----------------------------------------------------------------------===//
125
126/// isExactlyValue - We don't rely on operator== working on double values, as
127/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
128/// As such, this method can be used to do an exact bit-for-bit comparison of
129/// two floating point values.
131 return getValueAPF().bitwiseIsEqual(V);
132}
133
135 const APFloat& Val) {
136 assert(VT.isFloatingPoint() && "Can only convert between FP types");
137
138 // convert modifies in place, so make a copy.
139 APFloat Val2 = APFloat(Val);
140 bool losesInfo;
142 &losesInfo);
143 return !losesInfo;
144}
145
146//===----------------------------------------------------------------------===//
147// ISD Namespace
148//===----------------------------------------------------------------------===//
149
150bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
151 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
152 if (auto OptAPInt = N->getOperand(0)->bitcastToAPInt()) {
153 unsigned EltSize =
154 N->getValueType(0).getVectorElementType().getSizeInBits();
155 SplatVal = OptAPInt->trunc(EltSize);
156 return true;
157 }
158 }
159
160 auto *BV = dyn_cast<BuildVectorSDNode>(N);
161 if (!BV)
162 return false;
163
164 APInt SplatUndef;
165 unsigned SplatBitSize;
166 bool HasUndefs;
167 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
168 // Endianness does not matter here. We are checking for a splat given the
169 // element size of the vector, and if we find such a splat for little endian
170 // layout, then that should be valid also for big endian (as the full vector
171 // size is known to be a multiple of the element size).
172 const bool IsBigEndian = false;
173 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
174 EltSize, IsBigEndian) &&
175 EltSize == SplatBitSize;
176}
177
178// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
179// specializations of the more general isConstantSplatVector()?
180
181bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
182 // Look through a bit convert.
183 while (N->getOpcode() == ISD::BITCAST)
184 N = N->getOperand(0).getNode();
185
186 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
187 APInt SplatVal;
188 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
189 }
190
191 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
192
193 unsigned i = 0, e = N->getNumOperands();
194
195 // Skip over all of the undef values.
196 while (i != e && N->getOperand(i).isUndef())
197 ++i;
198
199 // Do not accept an all-undef vector.
200 if (i == e) return false;
201
202 // Do not accept build_vectors that aren't all constants or which have non-~0
203 // elements. We have to be a bit careful here, as the type of the constant
204 // may not be the same as the type of the vector elements due to type
205 // legalization (the elements are promoted to a legal type for the target and
206 // a vector of a type may be legal when the base element type is not).
207 // We only want to check enough bits to cover the vector elements, because
208 // we care if the resultant vector is all ones, not whether the individual
209 // constants are.
210 SDValue NotZero = N->getOperand(i);
211 if (auto OptAPInt = NotZero->bitcastToAPInt()) {
212 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
213 if (OptAPInt->countr_one() < EltSize)
214 return false;
215 } else
216 return false;
217
218 // Okay, we have at least one ~0 value, check to see if the rest match or are
219 // undefs. Even with the above element type twiddling, this should be OK, as
220 // the same type legalization should have applied to all the elements.
221 for (++i; i != e; ++i)
222 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
223 return false;
224 return true;
225}
226
227bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
228 // Look through a bit convert.
229 while (N->getOpcode() == ISD::BITCAST)
230 N = N->getOperand(0).getNode();
231
232 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
233 APInt SplatVal;
234 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
235 }
236
237 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
238
239 bool IsAllUndef = true;
240 for (const SDValue &Op : N->op_values()) {
241 if (Op.isUndef())
242 continue;
243 IsAllUndef = false;
244 // Do not accept build_vectors that aren't all constants or which have non-0
245 // elements. We have to be a bit careful here, as the type of the constant
246 // may not be the same as the type of the vector elements due to type
247 // legalization (the elements are promoted to a legal type for the target
248 // and a vector of a type may be legal when the base element type is not).
249 // We only want to check enough bits to cover the vector elements, because
250 // we care if the resultant vector is all zeros, not whether the individual
251 // constants are.
252 if (auto OptAPInt = Op->bitcastToAPInt()) {
253 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
254 if (OptAPInt->countr_zero() < EltSize)
255 return false;
256 } else
257 return false;
258 }
259
260 // Do not accept an all-undef vector.
261 if (IsAllUndef)
262 return false;
263 return true;
264}
265
267 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
268}
269
271 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
272}
273
275 if (N->getOpcode() != ISD::BUILD_VECTOR)
276 return false;
277
278 for (const SDValue &Op : N->op_values()) {
279 if (Op.isUndef())
280 continue;
282 return false;
283 }
284 return true;
285}
286
288 if (N->getOpcode() != ISD::BUILD_VECTOR)
289 return false;
290
291 for (const SDValue &Op : N->op_values()) {
292 if (Op.isUndef())
293 continue;
295 return false;
296 }
297 return true;
298}
299
300bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
301 bool Signed) {
302 assert(N->getValueType(0).isVector() && "Expected a vector!");
303
304 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
305 if (EltSize <= NewEltSize)
306 return false;
307
308 if (N->getOpcode() == ISD::ZERO_EXTEND) {
309 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
310 NewEltSize) &&
311 !Signed;
312 }
313 if (N->getOpcode() == ISD::SIGN_EXTEND) {
314 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
315 NewEltSize) &&
316 Signed;
317 }
318 if (N->getOpcode() != ISD::BUILD_VECTOR)
319 return false;
320
321 for (const SDValue &Op : N->op_values()) {
322 if (Op.isUndef())
323 continue;
325 return false;
326
327 APInt C = Op->getAsAPIntVal().trunc(EltSize);
328 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
329 return false;
330 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
331 return false;
332 }
333
334 return true;
335}
336
338 // Return false if the node has no operands.
339 // This is "logically inconsistent" with the definition of "all" but
340 // is probably the desired behavior.
341 if (N->getNumOperands() == 0)
342 return false;
343 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
344}
345
347 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
348}
349
350template <typename ConstNodeType>
352 std::function<bool(ConstNodeType *)> Match,
353 bool AllowUndefs, bool AllowTruncation) {
354 // FIXME: Add support for scalar UNDEF cases?
355 if (auto *C = dyn_cast<ConstNodeType>(Op))
356 return Match(C);
357
358 // FIXME: Add support for vector UNDEF cases?
359 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
360 ISD::SPLAT_VECTOR != Op.getOpcode())
361 return false;
362
363 EVT SVT = Op.getValueType().getScalarType();
364 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
365 if (AllowUndefs && Op.getOperand(i).isUndef()) {
366 if (!Match(nullptr))
367 return false;
368 continue;
369 }
370
371 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
372 if (!Cst || (!AllowTruncation && Cst->getValueType(0) != SVT) ||
373 !Match(Cst))
374 return false;
375 }
376 return true;
377}
378// Build used template types.
380 SDValue, std::function<bool(ConstantSDNode *)>, bool, bool);
382 SDValue, std::function<bool(ConstantFPSDNode *)>, bool, bool);
383
385 SDValue LHS, SDValue RHS,
386 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
387 bool AllowUndefs, bool AllowTypeMismatch) {
388 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
389 return false;
390
391 // TODO: Add support for scalar UNDEF cases?
392 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
393 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
394 return Match(LHSCst, RHSCst);
395
396 // TODO: Add support for vector UNDEF cases?
397 if (LHS.getOpcode() != RHS.getOpcode() ||
398 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
399 LHS.getOpcode() != ISD::SPLAT_VECTOR))
400 return false;
401
402 EVT SVT = LHS.getValueType().getScalarType();
403 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
404 SDValue LHSOp = LHS.getOperand(i);
405 SDValue RHSOp = RHS.getOperand(i);
406 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
407 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
408 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
409 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
410 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
411 return false;
412 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
413 LHSOp.getValueType() != RHSOp.getValueType()))
414 return false;
415 if (!Match(LHSCst, RHSCst))
416 return false;
417 }
418 return true;
419}
420
422 switch (MinMaxOpc) {
423 default:
424 llvm_unreachable("unrecognized opcode");
425 case ISD::UMIN:
426 return ISD::UMAX;
427 case ISD::UMAX:
428 return ISD::UMIN;
429 case ISD::SMIN:
430 return ISD::SMAX;
431 case ISD::SMAX:
432 return ISD::SMIN;
433 }
434}
435
437 switch (MinMaxOpc) {
438 default:
439 llvm_unreachable("unrecognized min/max opcode");
440 case ISD::SMIN:
441 return ISD::UMIN;
442 case ISD::SMAX:
443 return ISD::UMAX;
444 case ISD::UMIN:
445 return ISD::SMIN;
446 case ISD::UMAX:
447 return ISD::SMAX;
448 }
449}
450
452 switch (VecReduceOpcode) {
453 default:
454 llvm_unreachable("Expected VECREDUCE opcode");
457 case ISD::VP_REDUCE_FADD:
458 case ISD::VP_REDUCE_SEQ_FADD:
459 return ISD::FADD;
462 case ISD::VP_REDUCE_FMUL:
463 case ISD::VP_REDUCE_SEQ_FMUL:
464 return ISD::FMUL;
466 case ISD::VP_REDUCE_ADD:
467 return ISD::ADD;
469 case ISD::VP_REDUCE_MUL:
470 return ISD::MUL;
472 case ISD::VP_REDUCE_AND:
473 return ISD::AND;
475 case ISD::VP_REDUCE_OR:
476 return ISD::OR;
478 case ISD::VP_REDUCE_XOR:
479 return ISD::XOR;
481 case ISD::VP_REDUCE_SMAX:
482 return ISD::SMAX;
484 case ISD::VP_REDUCE_SMIN:
485 return ISD::SMIN;
487 case ISD::VP_REDUCE_UMAX:
488 return ISD::UMAX;
490 case ISD::VP_REDUCE_UMIN:
491 return ISD::UMIN;
493 case ISD::VP_REDUCE_FMAX:
494 return ISD::FMAXNUM;
496 case ISD::VP_REDUCE_FMIN:
497 return ISD::FMINNUM;
499 case ISD::VP_REDUCE_FMAXIMUM:
500 return ISD::FMAXIMUM;
502 case ISD::VP_REDUCE_FMINIMUM:
503 return ISD::FMINIMUM;
504 }
505}
506
507bool ISD::isVPOpcode(unsigned Opcode) {
508 switch (Opcode) {
509 default:
510 return false;
511#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
512 case ISD::VPSD: \
513 return true;
514#include "llvm/IR/VPIntrinsics.def"
515 }
516}
517
518bool ISD::isVPBinaryOp(unsigned Opcode) {
519 switch (Opcode) {
520 default:
521 break;
522#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
523#define VP_PROPERTY_BINARYOP return true;
524#define END_REGISTER_VP_SDNODE(VPSD) break;
525#include "llvm/IR/VPIntrinsics.def"
526 }
527 return false;
528}
529
530bool ISD::isVPReduction(unsigned Opcode) {
531 switch (Opcode) {
532 default:
533 return false;
534 case ISD::VP_REDUCE_ADD:
535 case ISD::VP_REDUCE_MUL:
536 case ISD::VP_REDUCE_AND:
537 case ISD::VP_REDUCE_OR:
538 case ISD::VP_REDUCE_XOR:
539 case ISD::VP_REDUCE_SMAX:
540 case ISD::VP_REDUCE_SMIN:
541 case ISD::VP_REDUCE_UMAX:
542 case ISD::VP_REDUCE_UMIN:
543 case ISD::VP_REDUCE_FMAX:
544 case ISD::VP_REDUCE_FMIN:
545 case ISD::VP_REDUCE_FMAXIMUM:
546 case ISD::VP_REDUCE_FMINIMUM:
547 case ISD::VP_REDUCE_FADD:
548 case ISD::VP_REDUCE_FMUL:
549 case ISD::VP_REDUCE_SEQ_FADD:
550 case ISD::VP_REDUCE_SEQ_FMUL:
551 return true;
552 }
553}
554
555/// The operand position of the vector mask.
556std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
557 switch (Opcode) {
558 default:
559 return std::nullopt;
560#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
561 case ISD::VPSD: \
562 return MASKPOS;
563#include "llvm/IR/VPIntrinsics.def"
564 }
565}
566
567/// The operand position of the explicit vector length parameter.
568std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
569 switch (Opcode) {
570 default:
571 return std::nullopt;
572#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
573 case ISD::VPSD: \
574 return EVLPOS;
575#include "llvm/IR/VPIntrinsics.def"
576 }
577}
578
579std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
580 bool hasFPExcept) {
581 // FIXME: Return strict opcodes in case of fp exceptions.
582 switch (VPOpcode) {
583 default:
584 return std::nullopt;
585#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
586#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
587#define END_REGISTER_VP_SDNODE(VPOPC) break;
588#include "llvm/IR/VPIntrinsics.def"
589 }
590 return std::nullopt;
591}
592
593std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
594 switch (Opcode) {
595 default:
596 return std::nullopt;
597#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
598#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
599#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
600#include "llvm/IR/VPIntrinsics.def"
601 }
602}
603
605 switch (ExtType) {
606 case ISD::EXTLOAD:
607 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
608 case ISD::SEXTLOAD:
609 return ISD::SIGN_EXTEND;
610 case ISD::ZEXTLOAD:
611 return ISD::ZERO_EXTEND;
612 default:
613 break;
614 }
615
616 llvm_unreachable("Invalid LoadExtType");
617}
618
620 // To perform this operation, we just need to swap the L and G bits of the
621 // operation.
622 unsigned OldL = (Operation >> 2) & 1;
623 unsigned OldG = (Operation >> 1) & 1;
624 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
625 (OldL << 1) | // New G bit
626 (OldG << 2)); // New L bit.
627}
628
630 unsigned Operation = Op;
631 if (isIntegerLike)
632 Operation ^= 7; // Flip L, G, E bits, but not U.
633 else
634 Operation ^= 15; // Flip all of the condition bits.
635
637 Operation &= ~8; // Don't let N and U bits get set.
638
639 return ISD::CondCode(Operation);
640}
641
645
647 bool isIntegerLike) {
648 return getSetCCInverseImpl(Op, isIntegerLike);
649}
650
651/// For an integer comparison, return 1 if the comparison is a signed operation
652/// and 2 if the result is an unsigned comparison. Return zero if the operation
653/// does not depend on the sign of the input (setne and seteq).
654static int isSignedOp(ISD::CondCode Opcode) {
655 switch (Opcode) {
656 default: llvm_unreachable("Illegal integer setcc operation!");
657 case ISD::SETEQ:
658 case ISD::SETNE: return 0;
659 case ISD::SETLT:
660 case ISD::SETLE:
661 case ISD::SETGT:
662 case ISD::SETGE: return 1;
663 case ISD::SETULT:
664 case ISD::SETULE:
665 case ISD::SETUGT:
666 case ISD::SETUGE: return 2;
667 }
668}
669
671 EVT Type) {
672 bool IsInteger = Type.isInteger();
673 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
674 // Cannot fold a signed integer setcc with an unsigned integer setcc.
675 return ISD::SETCC_INVALID;
676
677 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
678
679 // If the N and U bits get set, then the resultant comparison DOES suddenly
680 // care about orderedness, and it is true when ordered.
681 if (Op > ISD::SETTRUE2)
682 Op &= ~16; // Clear the U bit if the N bit is set.
683
684 // Canonicalize illegal integer setcc's.
685 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
686 Op = ISD::SETNE;
687
688 return ISD::CondCode(Op);
689}
690
692 EVT Type) {
693 bool IsInteger = Type.isInteger();
694 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
695 // Cannot fold a signed setcc with an unsigned setcc.
696 return ISD::SETCC_INVALID;
697
698 // Combine all of the condition bits.
699 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
700
701 // Canonicalize illegal integer setcc's.
702 if (IsInteger) {
703 switch (Result) {
704 default: break;
705 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
706 case ISD::SETOEQ: // SETEQ & SETU[LG]E
707 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
708 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
709 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
710 }
711 }
712
713 return Result;
714}
715
716//===----------------------------------------------------------------------===//
717// SDNode Profile Support
718//===----------------------------------------------------------------------===//
719
720/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
721static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
722 ID.AddInteger(OpC);
723}
724
725/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
726/// solely with their pointer.
728 ID.AddPointer(VTList.VTs);
729}
730
731/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
734 for (const auto &Op : Ops) {
735 ID.AddPointer(Op.getNode());
736 ID.AddInteger(Op.getResNo());
737 }
738}
739
740/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
743 for (const auto &Op : Ops) {
744 ID.AddPointer(Op.getNode());
745 ID.AddInteger(Op.getResNo());
746 }
747}
748
749static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
750 SDVTList VTList, ArrayRef<SDValue> OpList) {
751 AddNodeIDOpcode(ID, OpC);
752 AddNodeIDValueTypes(ID, VTList);
753 AddNodeIDOperands(ID, OpList);
754}
755
756/// If this is an SDNode with special info, add this info to the NodeID data.
758 switch (N->getOpcode()) {
761 case ISD::MCSymbol:
762 llvm_unreachable("Should only be used on nodes with operands");
763 default: break; // Normal nodes don't need extra info.
765 case ISD::Constant: {
767 ID.AddPointer(C->getConstantIntValue());
768 ID.AddBoolean(C->isOpaque());
769 break;
770 }
772 case ISD::ConstantFP:
773 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
774 break;
780 ID.AddPointer(GA->getGlobal());
781 ID.AddInteger(GA->getOffset());
782 ID.AddInteger(GA->getTargetFlags());
783 break;
784 }
785 case ISD::BasicBlock:
787 break;
788 case ISD::Register:
789 ID.AddInteger(cast<RegisterSDNode>(N)->getReg().id());
790 break;
792 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
793 break;
794 case ISD::SRCVALUE:
795 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
796 break;
797 case ISD::FrameIndex:
799 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
800 break;
802 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
803 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
804 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
805 break;
806 case ISD::JumpTable:
808 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
809 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
810 break;
814 ID.AddInteger(CP->getAlign().value());
815 ID.AddInteger(CP->getOffset());
818 else
819 ID.AddPointer(CP->getConstVal());
820 ID.AddInteger(CP->getTargetFlags());
821 break;
822 }
823 case ISD::TargetIndex: {
825 ID.AddInteger(TI->getIndex());
826 ID.AddInteger(TI->getOffset());
827 ID.AddInteger(TI->getTargetFlags());
828 break;
829 }
830 case ISD::LOAD: {
831 const LoadSDNode *LD = cast<LoadSDNode>(N);
832 ID.AddInteger(LD->getMemoryVT().getRawBits());
833 ID.AddInteger(LD->getRawSubclassData());
834 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
835 ID.AddInteger(LD->getMemOperand()->getFlags());
836 break;
837 }
838 case ISD::STORE: {
839 const StoreSDNode *ST = cast<StoreSDNode>(N);
840 ID.AddInteger(ST->getMemoryVT().getRawBits());
841 ID.AddInteger(ST->getRawSubclassData());
842 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
843 ID.AddInteger(ST->getMemOperand()->getFlags());
844 break;
845 }
846 case ISD::VP_LOAD: {
847 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
848 ID.AddInteger(ELD->getMemoryVT().getRawBits());
849 ID.AddInteger(ELD->getRawSubclassData());
850 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
851 ID.AddInteger(ELD->getMemOperand()->getFlags());
852 break;
853 }
854 case ISD::VP_LOAD_FF: {
855 const auto *LD = cast<VPLoadFFSDNode>(N);
856 ID.AddInteger(LD->getMemoryVT().getRawBits());
857 ID.AddInteger(LD->getRawSubclassData());
858 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
859 ID.AddInteger(LD->getMemOperand()->getFlags());
860 break;
861 }
862 case ISD::VP_STORE: {
863 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
864 ID.AddInteger(EST->getMemoryVT().getRawBits());
865 ID.AddInteger(EST->getRawSubclassData());
866 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
867 ID.AddInteger(EST->getMemOperand()->getFlags());
868 break;
869 }
870 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
872 ID.AddInteger(SLD->getMemoryVT().getRawBits());
873 ID.AddInteger(SLD->getRawSubclassData());
874 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
875 break;
876 }
877 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
879 ID.AddInteger(SST->getMemoryVT().getRawBits());
880 ID.AddInteger(SST->getRawSubclassData());
881 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
882 break;
883 }
884 case ISD::VP_GATHER: {
886 ID.AddInteger(EG->getMemoryVT().getRawBits());
887 ID.AddInteger(EG->getRawSubclassData());
888 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
889 ID.AddInteger(EG->getMemOperand()->getFlags());
890 break;
891 }
892 case ISD::VP_SCATTER: {
894 ID.AddInteger(ES->getMemoryVT().getRawBits());
895 ID.AddInteger(ES->getRawSubclassData());
896 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
897 ID.AddInteger(ES->getMemOperand()->getFlags());
898 break;
899 }
900 case ISD::MLOAD: {
902 ID.AddInteger(MLD->getMemoryVT().getRawBits());
903 ID.AddInteger(MLD->getRawSubclassData());
904 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
905 ID.AddInteger(MLD->getMemOperand()->getFlags());
906 break;
907 }
908 case ISD::MSTORE: {
910 ID.AddInteger(MST->getMemoryVT().getRawBits());
911 ID.AddInteger(MST->getRawSubclassData());
912 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
913 ID.AddInteger(MST->getMemOperand()->getFlags());
914 break;
915 }
916 case ISD::MGATHER: {
918 ID.AddInteger(MG->getMemoryVT().getRawBits());
919 ID.AddInteger(MG->getRawSubclassData());
920 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
921 ID.AddInteger(MG->getMemOperand()->getFlags());
922 break;
923 }
924 case ISD::MSCATTER: {
926 ID.AddInteger(MS->getMemoryVT().getRawBits());
927 ID.AddInteger(MS->getRawSubclassData());
928 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
929 ID.AddInteger(MS->getMemOperand()->getFlags());
930 break;
931 }
934 case ISD::ATOMIC_SWAP:
946 case ISD::ATOMIC_LOAD:
947 case ISD::ATOMIC_STORE: {
948 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
949 ID.AddInteger(AT->getMemoryVT().getRawBits());
950 ID.AddInteger(AT->getRawSubclassData());
951 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
952 ID.AddInteger(AT->getMemOperand()->getFlags());
953 break;
954 }
955 case ISD::VECTOR_SHUFFLE: {
956 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
957 for (int M : Mask)
958 ID.AddInteger(M);
959 break;
960 }
961 case ISD::ADDRSPACECAST: {
963 ID.AddInteger(ASC->getSrcAddressSpace());
964 ID.AddInteger(ASC->getDestAddressSpace());
965 break;
966 }
968 case ISD::BlockAddress: {
970 ID.AddPointer(BA->getBlockAddress());
971 ID.AddInteger(BA->getOffset());
972 ID.AddInteger(BA->getTargetFlags());
973 break;
974 }
975 case ISD::AssertAlign:
976 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
977 break;
978 case ISD::PREFETCH:
981 // Handled by MemIntrinsicSDNode check after the switch.
982 break;
984 ID.AddPointer(cast<MDNodeSDNode>(N)->getMD());
985 break;
986 } // end switch (N->getOpcode())
987
988 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
989 // to check.
990 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
991 ID.AddInteger(MN->getRawSubclassData());
992 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
993 ID.AddInteger(MN->getMemOperand()->getFlags());
994 ID.AddInteger(MN->getMemoryVT().getRawBits());
995 }
996}
997
998/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
999/// data.
1001 AddNodeIDOpcode(ID, N->getOpcode());
1002 // Add the return value info.
1003 AddNodeIDValueTypes(ID, N->getVTList());
1004 // Add the operand info.
1005 AddNodeIDOperands(ID, N->ops());
1006
1007 // Handle SDNode leafs with special info.
1009}
1010
1011//===----------------------------------------------------------------------===//
1012// SelectionDAG Class
1013//===----------------------------------------------------------------------===//
1014
1015/// doNotCSE - Return true if CSE should not be performed for this node.
1016static bool doNotCSE(SDNode *N) {
1017 if (N->getValueType(0) == MVT::Glue)
1018 return true; // Never CSE anything that produces a glue result.
1019
1020 switch (N->getOpcode()) {
1021 default: break;
1022 case ISD::HANDLENODE:
1023 case ISD::EH_LABEL:
1024 return true; // Never CSE these nodes.
1025 }
1026
1027 // Check that remaining values produced are not flags.
1028 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1029 if (N->getValueType(i) == MVT::Glue)
1030 return true; // Never CSE anything that produces a glue result.
1031
1032 return false;
1033}
1034
1035/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1036/// SelectionDAG.
1038 // Create a dummy node (which is not added to allnodes), that adds a reference
1039 // to the root node, preventing it from being deleted.
1040 HandleSDNode Dummy(getRoot());
1041
1042 SmallVector<SDNode*, 128> DeadNodes;
1043
1044 // Add all obviously-dead nodes to the DeadNodes worklist.
1045 for (SDNode &Node : allnodes())
1046 if (Node.use_empty())
1047 DeadNodes.push_back(&Node);
1048
1049 RemoveDeadNodes(DeadNodes);
1050
1051 // If the root changed (e.g. it was a dead load, update the root).
1052 setRoot(Dummy.getValue());
1053}
1054
1055/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1056/// given list, and any nodes that become unreachable as a result.
1058
1059 // Process the worklist, deleting the nodes and adding their uses to the
1060 // worklist.
1061 while (!DeadNodes.empty()) {
1062 SDNode *N = DeadNodes.pop_back_val();
1063 // Skip to next node if we've already managed to delete the node. This could
1064 // happen if replacing a node causes a node previously added to the node to
1065 // be deleted.
1066 if (N->getOpcode() == ISD::DELETED_NODE)
1067 continue;
1068
1069 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1070 DUL->NodeDeleted(N, nullptr);
1071
1072 // Take the node out of the appropriate CSE map.
1073 RemoveNodeFromCSEMaps(N);
1074
1075 // Next, brutally remove the operand list. This is safe to do, as there are
1076 // no cycles in the graph.
1077 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1078 SDUse &Use = *I++;
1079 SDNode *Operand = Use.getNode();
1080 Use.set(SDValue());
1081
1082 // Now that we removed this operand, see if there are no uses of it left.
1083 if (Operand->use_empty())
1084 DeadNodes.push_back(Operand);
1085 }
1086
1087 DeallocateNode(N);
1088 }
1089}
1090
1092 SmallVector<SDNode*, 16> DeadNodes(1, N);
1093
1094 // Create a dummy node that adds a reference to the root node, preventing
1095 // it from being deleted. (This matters if the root is an operand of the
1096 // dead node.)
1097 HandleSDNode Dummy(getRoot());
1098
1099 RemoveDeadNodes(DeadNodes);
1100}
1101
1103 // First take this out of the appropriate CSE map.
1104 RemoveNodeFromCSEMaps(N);
1105
1106 // Finally, remove uses due to operands of this node, remove from the
1107 // AllNodes list, and delete the node.
1108 DeleteNodeNotInCSEMaps(N);
1109}
1110
1111void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1112 assert(N->getIterator() != AllNodes.begin() &&
1113 "Cannot delete the entry node!");
1114 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1115
1116 // Drop all of the operands and decrement used node's use counts.
1117 N->DropOperands();
1118
1119 DeallocateNode(N);
1120}
1121
1122void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1123 assert(!(V->isVariadic() && isParameter));
1124 if (isParameter)
1125 ByvalParmDbgValues.push_back(V);
1126 else
1127 DbgValues.push_back(V);
1128 for (const SDNode *Node : V->getSDNodes())
1129 if (Node)
1130 DbgValMap[Node].push_back(V);
1131}
1132
1134 DbgValMapType::iterator I = DbgValMap.find(Node);
1135 if (I == DbgValMap.end())
1136 return;
1137 for (auto &Val: I->second)
1138 Val->setIsInvalidated();
1139 DbgValMap.erase(I);
1140}
1141
1142void SelectionDAG::DeallocateNode(SDNode *N) {
1143 // If we have operands, deallocate them.
1145
1146 NodeAllocator.Deallocate(AllNodes.remove(N));
1147
1148 // Set the opcode to DELETED_NODE to help catch bugs when node
1149 // memory is reallocated.
1150 // FIXME: There are places in SDag that have grown a dependency on the opcode
1151 // value in the released node.
1152 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1153 N->NodeType = ISD::DELETED_NODE;
1154
1155 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1156 // them and forget about that node.
1157 DbgInfo->erase(N);
1158
1159 // Invalidate extra info.
1160 SDEI.erase(N);
1161}
1162
1163#ifndef NDEBUG
1164/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1165void SelectionDAG::verifyNode(SDNode *N) const {
1166 switch (N->getOpcode()) {
1167 default:
1168 if (N->isTargetOpcode())
1170 break;
1171 case ISD::BUILD_PAIR: {
1172 EVT VT = N->getValueType(0);
1173 assert(N->getNumValues() == 1 && "Too many results!");
1174 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1175 "Wrong return type!");
1176 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1177 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1178 "Mismatched operand types!");
1179 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1180 "Wrong operand type!");
1181 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1182 "Wrong return type size");
1183 break;
1184 }
1185 case ISD::BUILD_VECTOR: {
1186 assert(N->getNumValues() == 1 && "Too many results!");
1187 assert(N->getValueType(0).isVector() && "Wrong return type!");
1188 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1189 "Wrong number of operands!");
1190 EVT EltVT = N->getValueType(0).getVectorElementType();
1191 for (const SDUse &Op : N->ops()) {
1192 assert((Op.getValueType() == EltVT ||
1193 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1194 EltVT.bitsLE(Op.getValueType()))) &&
1195 "Wrong operand type!");
1196 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1197 "Operands must all have the same type");
1198 }
1199 break;
1200 }
1201 }
1202}
1203#endif // NDEBUG
1204
1205/// Insert a newly allocated node into the DAG.
1206///
1207/// Handles insertion into the all nodes list and CSE map, as well as
1208/// verification and other common operations when a new node is allocated.
1209void SelectionDAG::InsertNode(SDNode *N) {
1210 AllNodes.push_back(N);
1211#ifndef NDEBUG
1212 N->PersistentId = NextPersistentId++;
1213 verifyNode(N);
1214#endif
1215 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1216 DUL->NodeInserted(N);
1217}
1218
1219/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1220/// correspond to it. This is useful when we're about to delete or repurpose
1221/// the node. We don't want future request for structurally identical nodes
1222/// to return N anymore.
1223bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1224 bool Erased = false;
1225 switch (N->getOpcode()) {
1226 case ISD::HANDLENODE: return false; // noop.
1227 case ISD::CONDCODE:
1228 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1229 "Cond code doesn't exist!");
1230 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1231 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1232 break;
1234 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1235 break;
1237 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1238 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1239 ESN->getSymbol(), ESN->getTargetFlags()));
1240 break;
1241 }
1242 case ISD::MCSymbol: {
1243 auto *MCSN = cast<MCSymbolSDNode>(N);
1244 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1245 break;
1246 }
1247 case ISD::VALUETYPE: {
1248 EVT VT = cast<VTSDNode>(N)->getVT();
1249 if (VT.isExtended()) {
1250 Erased = ExtendedValueTypeNodes.erase(VT);
1251 } else {
1252 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1253 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1254 }
1255 break;
1256 }
1257 default:
1258 // Remove it from the CSE Map.
1259 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1260 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1261 Erased = CSEMap.RemoveNode(N);
1262 break;
1263 }
1264#ifndef NDEBUG
1265 // Verify that the node was actually in one of the CSE maps, unless it has a
1266 // glue result (which cannot be CSE'd) or is one of the special cases that are
1267 // not subject to CSE.
1268 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1269 !N->isMachineOpcode() && !doNotCSE(N)) {
1270 N->dump(this);
1271 dbgs() << "\n";
1272 llvm_unreachable("Node is not in map!");
1273 }
1274#endif
1275 return Erased;
1276}
1277
1278/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1279/// maps and modified in place. Add it back to the CSE maps, unless an identical
1280/// node already exists, in which case transfer all its users to the existing
1281/// node. This transfer can potentially trigger recursive merging.
1282void
1283SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1284 // For node types that aren't CSE'd, just act as if no identical node
1285 // already exists.
1286 if (!doNotCSE(N)) {
1287 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1288 if (Existing != N) {
1289 // If there was already an existing matching node, use ReplaceAllUsesWith
1290 // to replace the dead one with the existing one. This can cause
1291 // recursive merging of other unrelated nodes down the line.
1292 Existing->intersectFlagsWith(N->getFlags());
1293 if (auto *MemNode = dyn_cast<MemSDNode>(Existing))
1294 MemNode->refineRanges(cast<MemSDNode>(N)->getMemOperand());
1295 ReplaceAllUsesWith(N, Existing);
1296
1297 // N is now dead. Inform the listeners and delete it.
1298 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1299 DUL->NodeDeleted(N, Existing);
1300 DeleteNodeNotInCSEMaps(N);
1301 return;
1302 }
1303 }
1304
1305 // If the node doesn't already exist, we updated it. Inform listeners.
1306 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1307 DUL->NodeUpdated(N);
1308}
1309
1310/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1311/// were replaced with those specified. If this node is never memoized,
1312/// return null, otherwise return a pointer to the slot it would take. If a
1313/// node already exists with these operands, the slot will be non-null.
1314SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1315 void *&InsertPos) {
1316 if (doNotCSE(N))
1317 return nullptr;
1318
1319 SDValue Ops[] = { Op };
1320 FoldingSetNodeID ID;
1321 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1323 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1324 if (Node)
1325 Node->intersectFlagsWith(N->getFlags());
1326 return Node;
1327}
1328
1329/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1330/// were replaced with those specified. If this node is never memoized,
1331/// return null, otherwise return a pointer to the slot it would take. If a
1332/// node already exists with these operands, the slot will be non-null.
1333SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1334 SDValue Op1, SDValue Op2,
1335 void *&InsertPos) {
1336 if (doNotCSE(N))
1337 return nullptr;
1338
1339 SDValue Ops[] = { Op1, Op2 };
1340 FoldingSetNodeID ID;
1341 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1343 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1344 if (Node)
1345 Node->intersectFlagsWith(N->getFlags());
1346 return Node;
1347}
1348
1349/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1350/// were replaced with those specified. If this node is never memoized,
1351/// return null, otherwise return a pointer to the slot it would take. If a
1352/// node already exists with these operands, the slot will be non-null.
1353SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1354 void *&InsertPos) {
1355 if (doNotCSE(N))
1356 return nullptr;
1357
1358 FoldingSetNodeID ID;
1359 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1361 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1362 if (Node)
1363 Node->intersectFlagsWith(N->getFlags());
1364 return Node;
1365}
1366
1368 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1369 : VT.getTypeForEVT(*getContext());
1370
1371 return getDataLayout().getABITypeAlign(Ty);
1372}
1373
1374// EntryNode could meaningfully have debug info if we can find it...
1376 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1377 getVTList(MVT::Other, MVT::Glue)),
1378 Root(getEntryNode()) {
1379 InsertNode(&EntryNode);
1380 DbgInfo = new SDDbgInfo();
1381}
1382
1384 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1385 const TargetLibraryInfo *LibraryInfo,
1386 const LibcallLoweringInfo *LibcallsInfo,
1387 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1389 FunctionVarLocs const *VarLocs) {
1390 MF = &NewMF;
1391 SDAGISelPass = PassPtr;
1392 ORE = &NewORE;
1395 LibInfo = LibraryInfo;
1396 Libcalls = LibcallsInfo;
1397 Context = &MF->getFunction().getContext();
1398 UA = NewUA;
1399 PSI = PSIin;
1400 BFI = BFIin;
1401 MMI = &MMIin;
1402 FnVarLocs = VarLocs;
1403}
1404
1406 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1407 allnodes_clear();
1408 OperandRecycler.clear(OperandAllocator);
1409 delete DbgInfo;
1410}
1411
1413 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1414}
1415
1416void SelectionDAG::allnodes_clear() {
1417 assert(&*AllNodes.begin() == &EntryNode);
1418 AllNodes.remove(AllNodes.begin());
1419 while (!AllNodes.empty())
1420 DeallocateNode(&AllNodes.front());
1421#ifndef NDEBUG
1422 NextPersistentId = 0;
1423#endif
1424}
1425
1426SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1427 void *&InsertPos) {
1428 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1429 if (N) {
1430 switch (N->getOpcode()) {
1431 default: break;
1432 case ISD::Constant:
1433 case ISD::ConstantFP:
1434 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1435 "debug location. Use another overload.");
1436 }
1437 }
1438 return N;
1439}
1440
1441SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1442 const SDLoc &DL, void *&InsertPos) {
1443 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1444 if (N) {
1445 switch (N->getOpcode()) {
1446 case ISD::Constant:
1447 case ISD::ConstantFP:
1448 // Erase debug location from the node if the node is used at several
1449 // different places. Do not propagate one location to all uses as it
1450 // will cause a worse single stepping debugging experience.
1451 if (N->getDebugLoc() != DL.getDebugLoc())
1452 N->setDebugLoc(DebugLoc());
1453 break;
1454 default:
1455 // When the node's point of use is located earlier in the instruction
1456 // sequence than its prior point of use, update its debug info to the
1457 // earlier location.
1458 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1459 N->setDebugLoc(DL.getDebugLoc());
1460 break;
1461 }
1462 }
1463 return N;
1464}
1465
1467 allnodes_clear();
1468 OperandRecycler.clear(OperandAllocator);
1469 OperandAllocator.Reset();
1470 CSEMap.clear();
1471
1472 ExtendedValueTypeNodes.clear();
1473 ExternalSymbols.clear();
1474 TargetExternalSymbols.clear();
1475 MCSymbols.clear();
1476 SDEI.clear();
1477 llvm::fill(CondCodeNodes, nullptr);
1478 llvm::fill(ValueTypeNodes, nullptr);
1479
1480 EntryNode.UseList = nullptr;
1481 InsertNode(&EntryNode);
1482 Root = getEntryNode();
1483 DbgInfo->clear();
1484}
1485
1487 return VT.bitsGT(Op.getValueType())
1488 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1489 : getNode(ISD::FP_ROUND, DL, VT, Op,
1490 getIntPtrConstant(0, DL, /*isTarget=*/true));
1491}
1492
1493std::pair<SDValue, SDValue>
1495 const SDLoc &DL, EVT VT) {
1496 assert(!VT.bitsEq(Op.getValueType()) &&
1497 "Strict no-op FP extend/round not allowed.");
1498 SDValue Res =
1499 VT.bitsGT(Op.getValueType())
1500 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1501 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1502 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1503
1504 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1505}
1506
1508 return VT.bitsGT(Op.getValueType()) ?
1509 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1510 getNode(ISD::TRUNCATE, DL, VT, Op);
1511}
1512
1514 return VT.bitsGT(Op.getValueType()) ?
1515 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1516 getNode(ISD::TRUNCATE, DL, VT, Op);
1517}
1518
1520 return VT.bitsGT(Op.getValueType()) ?
1521 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1522 getNode(ISD::TRUNCATE, DL, VT, Op);
1523}
1524
1526 EVT VT) {
1527 assert(!VT.isVector());
1528 auto Type = Op.getValueType();
1529 SDValue DestOp;
1530 if (Type == VT)
1531 return Op;
1532 auto Size = Op.getValueSizeInBits();
1533 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1534 if (DestOp.getValueType() == VT)
1535 return DestOp;
1536
1537 return getAnyExtOrTrunc(DestOp, DL, VT);
1538}
1539
1541 EVT VT) {
1542 assert(!VT.isVector());
1543 auto Type = Op.getValueType();
1544 SDValue DestOp;
1545 if (Type == VT)
1546 return Op;
1547 auto Size = Op.getValueSizeInBits();
1548 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1549 if (DestOp.getValueType() == VT)
1550 return DestOp;
1551
1552 return getSExtOrTrunc(DestOp, DL, VT);
1553}
1554
1556 EVT VT) {
1557 assert(!VT.isVector());
1558 auto Type = Op.getValueType();
1559 SDValue DestOp;
1560 if (Type == VT)
1561 return Op;
1562 auto Size = Op.getValueSizeInBits();
1563 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1564 if (DestOp.getValueType() == VT)
1565 return DestOp;
1566
1567 return getZExtOrTrunc(DestOp, DL, VT);
1568}
1569
1571 EVT OpVT) {
1572 if (VT.bitsLE(Op.getValueType()))
1573 return getNode(ISD::TRUNCATE, SL, VT, Op);
1574
1575 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1576 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1577}
1578
1580 EVT OpVT = Op.getValueType();
1581 assert(VT.isInteger() && OpVT.isInteger() &&
1582 "Cannot getZeroExtendInReg FP types");
1583 assert(VT.isVector() == OpVT.isVector() &&
1584 "getZeroExtendInReg type should be vector iff the operand "
1585 "type is vector!");
1586 assert((!VT.isVector() ||
1588 "Vector element counts must match in getZeroExtendInReg");
1589 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1590 if (OpVT == VT)
1591 return Op;
1592 // TODO: Use computeKnownBits instead of AssertZext.
1593 if (Op.getOpcode() == ISD::AssertZext && cast<VTSDNode>(Op.getOperand(1))
1594 ->getVT()
1595 .getScalarType()
1596 .bitsLE(VT.getScalarType()))
1597 return Op;
1599 VT.getScalarSizeInBits());
1600 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1601}
1602
1604 SDValue EVL, const SDLoc &DL,
1605 EVT VT) {
1606 EVT OpVT = Op.getValueType();
1607 assert(VT.isInteger() && OpVT.isInteger() &&
1608 "Cannot getVPZeroExtendInReg FP types");
1609 assert(VT.isVector() && OpVT.isVector() &&
1610 "getVPZeroExtendInReg type and operand type should be vector!");
1612 "Vector element counts must match in getZeroExtendInReg");
1613 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1614 if (OpVT == VT)
1615 return Op;
1617 VT.getScalarSizeInBits());
1618 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1619 EVL);
1620}
1621
1623 // Only unsigned pointer semantics are supported right now. In the future this
1624 // might delegate to TLI to check pointer signedness.
1625 return getZExtOrTrunc(Op, DL, VT);
1626}
1627
1629 // Only unsigned pointer semantics are supported right now. In the future this
1630 // might delegate to TLI to check pointer signedness.
1631 return getZeroExtendInReg(Op, DL, VT);
1632}
1633
1635 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1636}
1637
1638/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1640 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1641}
1642
1644 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1645 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1646}
1647
1649 SDValue Mask, SDValue EVL, EVT VT) {
1650 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1651 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1652}
1653
1655 SDValue Mask, SDValue EVL) {
1656 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1657}
1658
1660 SDValue Mask, SDValue EVL) {
1661 if (VT.bitsGT(Op.getValueType()))
1662 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1663 if (VT.bitsLT(Op.getValueType()))
1664 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1665 return Op;
1666}
1667
1669 EVT OpVT) {
1670 if (!V)
1671 return getConstant(0, DL, VT);
1672
1673 switch (TLI->getBooleanContents(OpVT)) {
1676 return getConstant(1, DL, VT);
1678 return getAllOnesConstant(DL, VT);
1679 }
1680 llvm_unreachable("Unexpected boolean content enum!");
1681}
1682
1684 bool isT, bool isO) {
1685 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1686 DL, VT, isT, isO);
1687}
1688
1690 bool isT, bool isO) {
1691 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1692}
1693
1695 EVT VT, bool isT, bool isO) {
1696 assert(VT.isInteger() && "Cannot create FP integer constant!");
1697
1698 EVT EltVT = VT.getScalarType();
1699 const ConstantInt *Elt = &Val;
1700
1701 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1702 // to-be-splatted scalar ConstantInt.
1703 if (isa<VectorType>(Elt->getType()))
1704 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1705
1706 // In some cases the vector type is legal but the element type is illegal and
1707 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1708 // inserted value (the type does not need to match the vector element type).
1709 // Any extra bits introduced will be truncated away.
1710 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1712 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1713 APInt NewVal;
1714 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1715 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1716 else
1717 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1718 Elt = ConstantInt::get(*getContext(), NewVal);
1719 }
1720 // In other cases the element type is illegal and needs to be expanded, for
1721 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1722 // the value into n parts and use a vector type with n-times the elements.
1723 // Then bitcast to the type requested.
1724 // Legalizing constants too early makes the DAGCombiner's job harder so we
1725 // only legalize if the DAG tells us we must produce legal types.
1726 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1727 TLI->getTypeAction(*getContext(), EltVT) ==
1729 const APInt &NewVal = Elt->getValue();
1730 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1731 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1732
1733 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1734 if (VT.isScalableVector() ||
1735 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1736 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1737 "Can only handle an even split!");
1738 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1739
1740 SmallVector<SDValue, 2> ScalarParts;
1741 for (unsigned i = 0; i != Parts; ++i)
1742 ScalarParts.push_back(getConstant(
1743 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1744 ViaEltVT, isT, isO));
1745
1746 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1747 }
1748
1749 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1750 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1751
1752 // Check the temporary vector is the correct size. If this fails then
1753 // getTypeToTransformTo() probably returned a type whose size (in bits)
1754 // isn't a power-of-2 factor of the requested type size.
1755 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1756
1757 SmallVector<SDValue, 2> EltParts;
1758 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1759 EltParts.push_back(getConstant(
1760 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1761 ViaEltVT, isT, isO));
1762
1763 // EltParts is currently in little endian order. If we actually want
1764 // big-endian order then reverse it now.
1765 if (getDataLayout().isBigEndian())
1766 std::reverse(EltParts.begin(), EltParts.end());
1767
1768 // The elements must be reversed when the element order is different
1769 // to the endianness of the elements (because the BITCAST is itself a
1770 // vector shuffle in this situation). However, we do not need any code to
1771 // perform this reversal because getConstant() is producing a vector
1772 // splat.
1773 // This situation occurs in MIPS MSA.
1774
1776 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1777 llvm::append_range(Ops, EltParts);
1778
1779 SDValue V =
1780 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1781 return V;
1782 }
1783
1784 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1785 "APInt size does not match type size!");
1786 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1787 SDVTList VTs = getVTList(EltVT);
1789 AddNodeIDNode(ID, Opc, VTs, {});
1790 ID.AddPointer(Elt);
1791 ID.AddBoolean(isO);
1792 void *IP = nullptr;
1793 SDNode *N = nullptr;
1794 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1795 if (!VT.isVector())
1796 return SDValue(N, 0);
1797
1798 if (!N) {
1799 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1800 CSEMap.InsertNode(N, IP);
1801 InsertNode(N);
1802 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1803 }
1804
1805 SDValue Result(N, 0);
1806 if (VT.isVector())
1807 Result = getSplat(VT, DL, Result);
1808 return Result;
1809}
1810
1812 bool isT, bool isO) {
1813 unsigned Size = VT.getScalarSizeInBits();
1814 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1815}
1816
1818 bool IsOpaque) {
1820 IsTarget, IsOpaque);
1821}
1822
1824 bool isTarget) {
1825 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1826}
1827
1829 const SDLoc &DL) {
1830 assert(VT.isInteger() && "Shift amount is not an integer type!");
1831 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1832 return getConstant(Val, DL, ShiftVT);
1833}
1834
1836 const SDLoc &DL) {
1837 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1838 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1839}
1840
1842 bool isTarget) {
1843 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1844}
1845
1847 bool isTarget) {
1848 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1849}
1850
1852 EVT VT, bool isTarget) {
1853 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1854
1855 EVT EltVT = VT.getScalarType();
1856 const ConstantFP *Elt = &V;
1857
1858 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1859 // the to-be-splatted scalar ConstantFP.
1860 if (isa<VectorType>(Elt->getType()))
1861 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1862
1863 // Do the map lookup using the actual bit pattern for the floating point
1864 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1865 // we don't have issues with SNANs.
1866 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1867 SDVTList VTs = getVTList(EltVT);
1869 AddNodeIDNode(ID, Opc, VTs, {});
1870 ID.AddPointer(Elt);
1871 void *IP = nullptr;
1872 SDNode *N = nullptr;
1873 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1874 if (!VT.isVector())
1875 return SDValue(N, 0);
1876
1877 if (!N) {
1878 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1879 CSEMap.InsertNode(N, IP);
1880 InsertNode(N);
1881 }
1882
1883 SDValue Result(N, 0);
1884 if (VT.isVector())
1885 Result = getSplat(VT, DL, Result);
1886 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1887 return Result;
1888}
1889
1891 bool isTarget) {
1892 EVT EltVT = VT.getScalarType();
1893 if (EltVT == MVT::f32)
1894 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1895 if (EltVT == MVT::f64)
1896 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1897 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1898 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1899 bool Ignored;
1900 APFloat APF = APFloat(Val);
1902 &Ignored);
1903 return getConstantFP(APF, DL, VT, isTarget);
1904 }
1905 llvm_unreachable("Unsupported type in getConstantFP");
1906}
1907
1909 EVT VT, int64_t Offset, bool isTargetGA,
1910 unsigned TargetFlags) {
1911 assert((TargetFlags == 0 || isTargetGA) &&
1912 "Cannot set target flags on target-independent globals");
1913
1914 // Truncate (with sign-extension) the offset value to the pointer size.
1916 if (BitWidth < 64)
1918
1919 unsigned Opc;
1920 if (GV->isThreadLocal())
1922 else
1924
1925 SDVTList VTs = getVTList(VT);
1927 AddNodeIDNode(ID, Opc, VTs, {});
1928 ID.AddPointer(GV);
1929 ID.AddInteger(Offset);
1930 ID.AddInteger(TargetFlags);
1931 void *IP = nullptr;
1932 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1933 return SDValue(E, 0);
1934
1935 auto *N = newSDNode<GlobalAddressSDNode>(
1936 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1937 CSEMap.InsertNode(N, IP);
1938 InsertNode(N);
1939 return SDValue(N, 0);
1940}
1941
1943 SDVTList VTs = getVTList(MVT::Untyped);
1946 ID.AddPointer(GV);
1947 void *IP = nullptr;
1948 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP))
1949 return SDValue(E, 0);
1950
1951 auto *N = newSDNode<DeactivationSymbolSDNode>(GV, VTs);
1952 CSEMap.InsertNode(N, IP);
1953 InsertNode(N);
1954 return SDValue(N, 0);
1955}
1956
1957SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1958 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1959 SDVTList VTs = getVTList(VT);
1961 AddNodeIDNode(ID, Opc, VTs, {});
1962 ID.AddInteger(FI);
1963 void *IP = nullptr;
1964 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1965 return SDValue(E, 0);
1966
1967 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1968 CSEMap.InsertNode(N, IP);
1969 InsertNode(N);
1970 return SDValue(N, 0);
1971}
1972
1973SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1974 unsigned TargetFlags) {
1975 assert((TargetFlags == 0 || isTarget) &&
1976 "Cannot set target flags on target-independent jump tables");
1977 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1978 SDVTList VTs = getVTList(VT);
1980 AddNodeIDNode(ID, Opc, VTs, {});
1981 ID.AddInteger(JTI);
1982 ID.AddInteger(TargetFlags);
1983 void *IP = nullptr;
1984 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1985 return SDValue(E, 0);
1986
1987 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1988 CSEMap.InsertNode(N, IP);
1989 InsertNode(N);
1990 return SDValue(N, 0);
1991}
1992
1994 const SDLoc &DL) {
1996 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Other, Chain,
1997 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1998}
1999
2001 MaybeAlign Alignment, int Offset,
2002 bool isTarget, unsigned TargetFlags) {
2003 assert((TargetFlags == 0 || isTarget) &&
2004 "Cannot set target flags on target-independent globals");
2005 if (!Alignment)
2006 Alignment = shouldOptForSize()
2007 ? getDataLayout().getABITypeAlign(C->getType())
2008 : getDataLayout().getPrefTypeAlign(C->getType());
2009 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2010 SDVTList VTs = getVTList(VT);
2012 AddNodeIDNode(ID, Opc, VTs, {});
2013 ID.AddInteger(Alignment->value());
2014 ID.AddInteger(Offset);
2015 ID.AddPointer(C);
2016 ID.AddInteger(TargetFlags);
2017 void *IP = nullptr;
2018 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2019 return SDValue(E, 0);
2020
2021 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2022 TargetFlags);
2023 CSEMap.InsertNode(N, IP);
2024 InsertNode(N);
2025 SDValue V = SDValue(N, 0);
2026 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
2027 return V;
2028}
2029
2031 MaybeAlign Alignment, int Offset,
2032 bool isTarget, unsigned TargetFlags) {
2033 assert((TargetFlags == 0 || isTarget) &&
2034 "Cannot set target flags on target-independent globals");
2035 if (!Alignment)
2036 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2037 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2038 SDVTList VTs = getVTList(VT);
2040 AddNodeIDNode(ID, Opc, VTs, {});
2041 ID.AddInteger(Alignment->value());
2042 ID.AddInteger(Offset);
2043 C->addSelectionDAGCSEId(ID);
2044 ID.AddInteger(TargetFlags);
2045 void *IP = nullptr;
2046 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2047 return SDValue(E, 0);
2048
2049 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2050 TargetFlags);
2051 CSEMap.InsertNode(N, IP);
2052 InsertNode(N);
2053 return SDValue(N, 0);
2054}
2055
2058 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2059 ID.AddPointer(MBB);
2060 void *IP = nullptr;
2061 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2062 return SDValue(E, 0);
2063
2064 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2065 CSEMap.InsertNode(N, IP);
2066 InsertNode(N);
2067 return SDValue(N, 0);
2068}
2069
2071 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2072 ValueTypeNodes.size())
2073 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2074
2075 SDNode *&N = VT.isExtended() ?
2076 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2077
2078 if (N) return SDValue(N, 0);
2079 N = newSDNode<VTSDNode>(VT);
2080 InsertNode(N);
2081 return SDValue(N, 0);
2082}
2083
2085 SDNode *&N = ExternalSymbols[Sym];
2086 if (N) return SDValue(N, 0);
2087 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2088 InsertNode(N);
2089 return SDValue(N, 0);
2090}
2091
2092SDValue SelectionDAG::getExternalSymbol(RTLIB::LibcallImpl Libcall, EVT VT) {
2094 return getExternalSymbol(SymName.data(), VT);
2095}
2096
2098 SDNode *&N = MCSymbols[Sym];
2099 if (N)
2100 return SDValue(N, 0);
2101 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2102 InsertNode(N);
2103 return SDValue(N, 0);
2104}
2105
2107 unsigned TargetFlags) {
2108 SDNode *&N =
2109 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2110 if (N) return SDValue(N, 0);
2111 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2112 InsertNode(N);
2113 return SDValue(N, 0);
2114}
2115
2117 EVT VT, unsigned TargetFlags) {
2119 return getTargetExternalSymbol(SymName.data(), VT, TargetFlags);
2120}
2121
2123 if ((unsigned)Cond >= CondCodeNodes.size())
2124 CondCodeNodes.resize(Cond+1);
2125
2126 if (!CondCodeNodes[Cond]) {
2127 auto *N = newSDNode<CondCodeSDNode>(Cond);
2128 CondCodeNodes[Cond] = N;
2129 InsertNode(N);
2130 }
2131
2132 return SDValue(CondCodeNodes[Cond], 0);
2133}
2134
2136 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2137 "APInt size does not match type size!");
2138
2139 if (MulImm == 0)
2140 return getConstant(0, DL, VT);
2141
2142 const MachineFunction &MF = getMachineFunction();
2143 const Function &F = MF.getFunction();
2144 ConstantRange CR = getVScaleRange(&F, 64);
2145 if (const APInt *C = CR.getSingleElement())
2146 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2147
2148 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2149}
2150
2151/// \returns a value of type \p VT that represents the runtime value of \p
2152/// Quantity, i.e. scaled by vscale if it's scalable, or a fixed constant
2153/// otherwise. Quantity should be a FixedOrScalableQuantity, i.e. ElementCount
2154/// or TypeSize.
2155template <typename Ty>
2157 EVT VT, Ty Quantity) {
2158 if (Quantity.isScalable())
2159 return DAG.getVScale(
2160 DL, VT, APInt(VT.getSizeInBits(), Quantity.getKnownMinValue()));
2161
2162 return DAG.getConstant(Quantity.getKnownMinValue(), DL, VT);
2163}
2164
2166 ElementCount EC) {
2167 return getFixedOrScalableQuantity(*this, DL, VT, EC);
2168}
2169
2171 return getFixedOrScalableQuantity(*this, DL, VT, TS);
2172}
2173
2175 ElementCount EC) {
2176 EVT IdxVT = TLI->getVectorIdxTy(getDataLayout());
2177 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), DataVT);
2178 return getNode(ISD::GET_ACTIVE_LANE_MASK, DL, MaskVT,
2179 getConstant(0, DL, IdxVT), getElementCount(DL, IdxVT, EC));
2180}
2181
2183 APInt One(ResVT.getScalarSizeInBits(), 1);
2184 return getStepVector(DL, ResVT, One);
2185}
2186
2188 const APInt &StepVal) {
2189 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2190 if (ResVT.isScalableVector())
2191 return getNode(
2192 ISD::STEP_VECTOR, DL, ResVT,
2193 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2194
2195 SmallVector<SDValue, 16> OpsStepConstants;
2196 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2197 OpsStepConstants.push_back(
2198 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2199 return getBuildVector(ResVT, DL, OpsStepConstants);
2200}
2201
2202/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2203/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2208
2210 SDValue N2, ArrayRef<int> Mask) {
2211 assert(VT.getVectorNumElements() == Mask.size() &&
2212 "Must have the same number of vector elements as mask elements!");
2213 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2214 "Invalid VECTOR_SHUFFLE");
2215
2216 // Canonicalize shuffle undef, undef -> undef
2217 if (N1.isUndef() && N2.isUndef())
2218 return getUNDEF(VT);
2219
2220 // Validate that all indices in Mask are within the range of the elements
2221 // input to the shuffle.
2222 int NElts = Mask.size();
2223 assert(llvm::all_of(Mask,
2224 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2225 "Index out of range");
2226
2227 // Copy the mask so we can do any needed cleanup.
2228 SmallVector<int, 8> MaskVec(Mask);
2229
2230 // Canonicalize shuffle v, v -> v, undef
2231 if (N1 == N2) {
2232 N2 = getUNDEF(VT);
2233 for (int i = 0; i != NElts; ++i)
2234 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2235 }
2236
2237 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2238 if (N1.isUndef())
2239 commuteShuffle(N1, N2, MaskVec);
2240
2241 if (TLI->hasVectorBlend()) {
2242 // If shuffling a splat, try to blend the splat instead. We do this here so
2243 // that even when this arises during lowering we don't have to re-handle it.
2244 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2245 BitVector UndefElements;
2246 SDValue Splat = BV->getSplatValue(&UndefElements);
2247 if (!Splat)
2248 return;
2249
2250 for (int i = 0; i < NElts; ++i) {
2251 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2252 continue;
2253
2254 // If this input comes from undef, mark it as such.
2255 if (UndefElements[MaskVec[i] - Offset]) {
2256 MaskVec[i] = -1;
2257 continue;
2258 }
2259
2260 // If we can blend a non-undef lane, use that instead.
2261 if (!UndefElements[i])
2262 MaskVec[i] = i + Offset;
2263 }
2264 };
2265 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2266 BlendSplat(N1BV, 0);
2267 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2268 BlendSplat(N2BV, NElts);
2269 }
2270
2271 // Canonicalize all index into lhs, -> shuffle lhs, undef
2272 // Canonicalize all index into rhs, -> shuffle rhs, undef
2273 bool AllLHS = true, AllRHS = true;
2274 bool N2Undef = N2.isUndef();
2275 for (int i = 0; i != NElts; ++i) {
2276 if (MaskVec[i] >= NElts) {
2277 if (N2Undef)
2278 MaskVec[i] = -1;
2279 else
2280 AllLHS = false;
2281 } else if (MaskVec[i] >= 0) {
2282 AllRHS = false;
2283 }
2284 }
2285 if (AllLHS && AllRHS)
2286 return getUNDEF(VT);
2287 if (AllLHS && !N2Undef)
2288 N2 = getUNDEF(VT);
2289 if (AllRHS) {
2290 N1 = getUNDEF(VT);
2291 commuteShuffle(N1, N2, MaskVec);
2292 }
2293 // Reset our undef status after accounting for the mask.
2294 N2Undef = N2.isUndef();
2295 // Re-check whether both sides ended up undef.
2296 if (N1.isUndef() && N2Undef)
2297 return getUNDEF(VT);
2298
2299 // If Identity shuffle return that node.
2300 bool Identity = true, AllSame = true;
2301 for (int i = 0; i != NElts; ++i) {
2302 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2303 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2304 }
2305 if (Identity && NElts)
2306 return N1;
2307
2308 // Shuffling a constant splat doesn't change the result.
2309 if (N2Undef) {
2310 SDValue V = N1;
2311
2312 // Look through any bitcasts. We check that these don't change the number
2313 // (and size) of elements and just changes their types.
2314 while (V.getOpcode() == ISD::BITCAST)
2315 V = V->getOperand(0);
2316
2317 // A splat should always show up as a build vector node.
2318 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2319 BitVector UndefElements;
2320 SDValue Splat = BV->getSplatValue(&UndefElements);
2321 // If this is a splat of an undef, shuffling it is also undef.
2322 if (Splat && Splat.isUndef())
2323 return getUNDEF(VT);
2324
2325 bool SameNumElts =
2326 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2327
2328 // We only have a splat which can skip shuffles if there is a splatted
2329 // value and no undef lanes rearranged by the shuffle.
2330 if (Splat && UndefElements.none()) {
2331 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2332 // number of elements match or the value splatted is a zero constant.
2333 if (SameNumElts || isNullConstant(Splat))
2334 return N1;
2335 }
2336
2337 // If the shuffle itself creates a splat, build the vector directly.
2338 if (AllSame && SameNumElts) {
2339 EVT BuildVT = BV->getValueType(0);
2340 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2341 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2342
2343 // We may have jumped through bitcasts, so the type of the
2344 // BUILD_VECTOR may not match the type of the shuffle.
2345 if (BuildVT != VT)
2346 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2347 return NewBV;
2348 }
2349 }
2350 }
2351
2352 SDVTList VTs = getVTList(VT);
2354 SDValue Ops[2] = { N1, N2 };
2356 for (int i = 0; i != NElts; ++i)
2357 ID.AddInteger(MaskVec[i]);
2358
2359 void* IP = nullptr;
2360 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2361 return SDValue(E, 0);
2362
2363 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2364 // SDNode doesn't have access to it. This memory will be "leaked" when
2365 // the node is deallocated, but recovered when the NodeAllocator is released.
2366 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2367 llvm::copy(MaskVec, MaskAlloc);
2368
2369 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2370 dl.getDebugLoc(), MaskAlloc);
2371 createOperands(N, Ops);
2372
2373 CSEMap.InsertNode(N, IP);
2374 InsertNode(N);
2375 SDValue V = SDValue(N, 0);
2376 NewSDValueDbgMsg(V, "Creating new node: ", this);
2377 return V;
2378}
2379
2381 EVT VT = SV.getValueType(0);
2382 SmallVector<int, 8> MaskVec(SV.getMask());
2384
2385 SDValue Op0 = SV.getOperand(0);
2386 SDValue Op1 = SV.getOperand(1);
2387 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2388}
2389
2391 SDVTList VTs = getVTList(VT);
2393 AddNodeIDNode(ID, ISD::Register, VTs, {});
2394 ID.AddInteger(Reg.id());
2395 void *IP = nullptr;
2396 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2397 return SDValue(E, 0);
2398
2399 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2400 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2401 CSEMap.InsertNode(N, IP);
2402 InsertNode(N);
2403 return SDValue(N, 0);
2404}
2405
2408 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2409 ID.AddPointer(RegMask);
2410 void *IP = nullptr;
2411 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2412 return SDValue(E, 0);
2413
2414 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2415 CSEMap.InsertNode(N, IP);
2416 InsertNode(N);
2417 return SDValue(N, 0);
2418}
2419
2421 MCSymbol *Label) {
2422 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2423}
2424
2425SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2426 SDValue Root, MCSymbol *Label) {
2428 SDValue Ops[] = { Root };
2429 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2430 ID.AddPointer(Label);
2431 void *IP = nullptr;
2432 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2433 return SDValue(E, 0);
2434
2435 auto *N =
2436 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2437 createOperands(N, Ops);
2438
2439 CSEMap.InsertNode(N, IP);
2440 InsertNode(N);
2441 return SDValue(N, 0);
2442}
2443
2445 int64_t Offset, bool isTarget,
2446 unsigned TargetFlags) {
2447 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2448 SDVTList VTs = getVTList(VT);
2449
2451 AddNodeIDNode(ID, Opc, VTs, {});
2452 ID.AddPointer(BA);
2453 ID.AddInteger(Offset);
2454 ID.AddInteger(TargetFlags);
2455 void *IP = nullptr;
2456 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2457 return SDValue(E, 0);
2458
2459 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2460 CSEMap.InsertNode(N, IP);
2461 InsertNode(N);
2462 return SDValue(N, 0);
2463}
2464
2467 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2468 ID.AddPointer(V);
2469
2470 void *IP = nullptr;
2471 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2472 return SDValue(E, 0);
2473
2474 auto *N = newSDNode<SrcValueSDNode>(V);
2475 CSEMap.InsertNode(N, IP);
2476 InsertNode(N);
2477 return SDValue(N, 0);
2478}
2479
2482 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2483 ID.AddPointer(MD);
2484
2485 void *IP = nullptr;
2486 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2487 return SDValue(E, 0);
2488
2489 auto *N = newSDNode<MDNodeSDNode>(MD);
2490 CSEMap.InsertNode(N, IP);
2491 InsertNode(N);
2492 return SDValue(N, 0);
2493}
2494
2496 if (VT == V.getValueType())
2497 return V;
2498
2499 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2500}
2501
2503 unsigned SrcAS, unsigned DestAS) {
2504 SDVTList VTs = getVTList(VT);
2505 SDValue Ops[] = {Ptr};
2508 ID.AddInteger(SrcAS);
2509 ID.AddInteger(DestAS);
2510
2511 void *IP = nullptr;
2512 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2513 return SDValue(E, 0);
2514
2515 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2516 VTs, SrcAS, DestAS);
2517 createOperands(N, Ops);
2518
2519 CSEMap.InsertNode(N, IP);
2520 InsertNode(N);
2521 return SDValue(N, 0);
2522}
2523
2525 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2526}
2527
2528/// getShiftAmountOperand - Return the specified value casted to
2529/// the target's desired shift amount type.
2531 EVT OpTy = Op.getValueType();
2532 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2533 if (OpTy == ShTy || OpTy.isVector()) return Op;
2534
2535 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2536}
2537
2539 SDLoc dl(Node);
2541 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2542 EVT VT = Node->getValueType(0);
2543 SDValue Tmp1 = Node->getOperand(0);
2544 SDValue Tmp2 = Node->getOperand(1);
2545 const MaybeAlign MA(Node->getConstantOperandVal(3));
2546
2547 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2548 Tmp2, MachinePointerInfo(V));
2549 SDValue VAList = VAListLoad;
2550
2551 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2552 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2553 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2554
2555 VAList = getNode(
2556 ISD::AND, dl, VAList.getValueType(), VAList,
2557 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2558 }
2559
2560 // Increment the pointer, VAList, to the next vaarg
2561 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2562 getConstant(getDataLayout().getTypeAllocSize(
2563 VT.getTypeForEVT(*getContext())),
2564 dl, VAList.getValueType()));
2565 // Store the incremented VAList to the legalized pointer
2566 Tmp1 =
2567 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2568 // Load the actual argument out of the pointer VAList
2569 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2570}
2571
2573 SDLoc dl(Node);
2575 // This defaults to loading a pointer from the input and storing it to the
2576 // output, returning the chain.
2577 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2578 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2579 SDValue Tmp1 =
2580 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2581 Node->getOperand(2), MachinePointerInfo(VS));
2582 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2583 MachinePointerInfo(VD));
2584}
2585
2587 const DataLayout &DL = getDataLayout();
2588 Type *Ty = VT.getTypeForEVT(*getContext());
2589 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2590
2591 if (TLI->isTypeLegal(VT) || !VT.isVector())
2592 return RedAlign;
2593
2594 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2595 const Align StackAlign = TFI->getStackAlign();
2596
2597 // See if we can choose a smaller ABI alignment in cases where it's an
2598 // illegal vector type that will get broken down.
2599 if (RedAlign > StackAlign) {
2600 EVT IntermediateVT;
2601 MVT RegisterVT;
2602 unsigned NumIntermediates;
2603 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2604 NumIntermediates, RegisterVT);
2605 Ty = IntermediateVT.getTypeForEVT(*getContext());
2606 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2607 if (RedAlign2 < RedAlign)
2608 RedAlign = RedAlign2;
2609
2610 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2611 // If the stack is not realignable, the alignment should be limited to the
2612 // StackAlignment
2613 RedAlign = std::min(RedAlign, StackAlign);
2614 }
2615
2616 return RedAlign;
2617}
2618
2620 MachineFrameInfo &MFI = MF->getFrameInfo();
2621 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2622 int StackID = 0;
2623 if (Bytes.isScalable())
2624 StackID = TFI->getStackIDForScalableVectors();
2625 // The stack id gives an indication of whether the object is scalable or
2626 // not, so it's safe to pass in the minimum size here.
2627 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2628 false, nullptr, StackID);
2629 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2630}
2631
2633 Type *Ty = VT.getTypeForEVT(*getContext());
2634 Align StackAlign =
2635 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2636 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2637}
2638
2640 TypeSize VT1Size = VT1.getStoreSize();
2641 TypeSize VT2Size = VT2.getStoreSize();
2642 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2643 "Don't know how to choose the maximum size when creating a stack "
2644 "temporary");
2645 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2646 ? VT1Size
2647 : VT2Size;
2648
2649 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2650 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2651 const DataLayout &DL = getDataLayout();
2652 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2653 return CreateStackTemporary(Bytes, Align);
2654}
2655
2657 ISD::CondCode Cond, const SDLoc &dl) {
2658 EVT OpVT = N1.getValueType();
2659
2660 auto GetUndefBooleanConstant = [&]() {
2661 if (VT.getScalarType() == MVT::i1 ||
2662 TLI->getBooleanContents(OpVT) ==
2664 return getUNDEF(VT);
2665 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2666 // so we cannot use getUNDEF(). Return zero instead.
2667 return getConstant(0, dl, VT);
2668 };
2669
2670 // These setcc operations always fold.
2671 switch (Cond) {
2672 default: break;
2673 case ISD::SETFALSE:
2674 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2675 case ISD::SETTRUE:
2676 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2677
2678 case ISD::SETOEQ:
2679 case ISD::SETOGT:
2680 case ISD::SETOGE:
2681 case ISD::SETOLT:
2682 case ISD::SETOLE:
2683 case ISD::SETONE:
2684 case ISD::SETO:
2685 case ISD::SETUO:
2686 case ISD::SETUEQ:
2687 case ISD::SETUNE:
2688 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2689 break;
2690 }
2691
2692 if (OpVT.isInteger()) {
2693 // For EQ and NE, we can always pick a value for the undef to make the
2694 // predicate pass or fail, so we can return undef.
2695 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2696 // icmp eq/ne X, undef -> undef.
2697 if ((N1.isUndef() || N2.isUndef()) &&
2698 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2699 return GetUndefBooleanConstant();
2700
2701 // If both operands are undef, we can return undef for int comparison.
2702 // icmp undef, undef -> undef.
2703 if (N1.isUndef() && N2.isUndef())
2704 return GetUndefBooleanConstant();
2705
2706 // icmp X, X -> true/false
2707 // icmp X, undef -> true/false because undef could be X.
2708 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2709 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2710 }
2711
2713 const APInt &C2 = N2C->getAPIntValue();
2715 const APInt &C1 = N1C->getAPIntValue();
2716
2718 dl, VT, OpVT);
2719 }
2720 }
2721
2722 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2723 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2724
2725 if (N1CFP && N2CFP) {
2726 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2727 switch (Cond) {
2728 default: break;
2729 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2730 return GetUndefBooleanConstant();
2731 [[fallthrough]];
2732 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2733 OpVT);
2734 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2735 return GetUndefBooleanConstant();
2736 [[fallthrough]];
2738 R==APFloat::cmpLessThan, dl, VT,
2739 OpVT);
2740 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2741 return GetUndefBooleanConstant();
2742 [[fallthrough]];
2743 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2744 OpVT);
2745 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2746 return GetUndefBooleanConstant();
2747 [[fallthrough]];
2749 VT, OpVT);
2750 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2751 return GetUndefBooleanConstant();
2752 [[fallthrough]];
2754 R==APFloat::cmpEqual, dl, VT,
2755 OpVT);
2756 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2757 return GetUndefBooleanConstant();
2758 [[fallthrough]];
2760 R==APFloat::cmpEqual, dl, VT, OpVT);
2761 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2762 OpVT);
2763 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2764 OpVT);
2766 R==APFloat::cmpEqual, dl, VT,
2767 OpVT);
2768 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2769 OpVT);
2771 R==APFloat::cmpLessThan, dl, VT,
2772 OpVT);
2774 R==APFloat::cmpUnordered, dl, VT,
2775 OpVT);
2777 VT, OpVT);
2778 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2779 OpVT);
2780 }
2781 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2782 // Ensure that the constant occurs on the RHS.
2784 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2785 return SDValue();
2786 return getSetCC(dl, VT, N2, N1, SwappedCond);
2787 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2788 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2789 // If an operand is known to be a nan (or undef that could be a nan), we can
2790 // fold it.
2791 // Choosing NaN for the undef will always make unordered comparison succeed
2792 // and ordered comparison fails.
2793 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2794 switch (ISD::getUnorderedFlavor(Cond)) {
2795 default:
2796 llvm_unreachable("Unknown flavor!");
2797 case 0: // Known false.
2798 return getBoolConstant(false, dl, VT, OpVT);
2799 case 1: // Known true.
2800 return getBoolConstant(true, dl, VT, OpVT);
2801 case 2: // Undefined.
2802 return GetUndefBooleanConstant();
2803 }
2804 }
2805
2806 // Could not fold it.
2807 return SDValue();
2808}
2809
2810/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2811/// use this predicate to simplify operations downstream.
2813 unsigned BitWidth = Op.getScalarValueSizeInBits();
2815}
2816
2817// TODO: Should have argument to specify if sign bit of nan is ignorable.
2819 if (Depth >= MaxRecursionDepth)
2820 return false; // Limit search depth.
2821
2822 unsigned Opc = Op.getOpcode();
2823 switch (Opc) {
2824 case ISD::FABS:
2825 return true;
2826 case ISD::AssertNoFPClass: {
2827 FPClassTest NoFPClass =
2828 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2829
2830 const FPClassTest TestMask = fcNan | fcNegative;
2831 return (NoFPClass & TestMask) == TestMask;
2832 }
2833 case ISD::ARITH_FENCE:
2834 return SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2835 case ISD::FEXP:
2836 case ISD::FEXP2:
2837 case ISD::FEXP10:
2838 return Op->getFlags().hasNoNaNs();
2839 case ISD::FMINNUM:
2840 case ISD::FMINNUM_IEEE:
2841 case ISD::FMINIMUM:
2842 case ISD::FMINIMUMNUM:
2843 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2844 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2845 case ISD::FMAXNUM:
2846 case ISD::FMAXNUM_IEEE:
2847 case ISD::FMAXIMUM:
2848 case ISD::FMAXIMUMNUM:
2849 // TODO: If we can ignore the sign bit of nans, only one side being known 0
2850 // is sufficient.
2851 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2852 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2853 default:
2854 return false;
2855 }
2856
2857 llvm_unreachable("covered opcode switch");
2858}
2859
2860/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2861/// this predicate to simplify operations downstream. Mask is known to be zero
2862/// for bits that V cannot have.
2864 unsigned Depth) const {
2865 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2866}
2867
2868/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2869/// DemandedElts. We use this predicate to simplify operations downstream.
2870/// Mask is known to be zero for bits that V cannot have.
2872 const APInt &DemandedElts,
2873 unsigned Depth) const {
2874 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2875}
2876
2877/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2878/// DemandedElts. We use this predicate to simplify operations downstream.
2880 unsigned Depth /* = 0 */) const {
2881 return computeKnownBits(V, DemandedElts, Depth).isZero();
2882}
2883
2884/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2886 unsigned Depth) const {
2887 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2888}
2889
2891 const APInt &DemandedElts,
2892 unsigned Depth) const {
2893 EVT VT = Op.getValueType();
2894 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2895
2896 unsigned NumElts = VT.getVectorNumElements();
2897 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2898
2899 APInt KnownZeroElements = APInt::getZero(NumElts);
2900 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2901 if (!DemandedElts[EltIdx])
2902 continue; // Don't query elements that are not demanded.
2903 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2904 if (MaskedVectorIsZero(Op, Mask, Depth))
2905 KnownZeroElements.setBit(EltIdx);
2906 }
2907 return KnownZeroElements;
2908}
2909
2910/// isSplatValue - Return true if the vector V has the same value
2911/// across all DemandedElts. For scalable vectors, we don't know the
2912/// number of lanes at compile time. Instead, we use a 1 bit APInt
2913/// to represent a conservative value for all lanes; that is, that
2914/// one bit value is implicitly splatted across all lanes.
2915bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2916 APInt &UndefElts, unsigned Depth) const {
2917 unsigned Opcode = V.getOpcode();
2918 EVT VT = V.getValueType();
2919 assert(VT.isVector() && "Vector type expected");
2920 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2921 "scalable demanded bits are ignored");
2922
2923 if (!DemandedElts)
2924 return false; // No demanded elts, better to assume we don't know anything.
2925
2926 if (Depth >= MaxRecursionDepth)
2927 return false; // Limit search depth.
2928
2929 // Deal with some common cases here that work for both fixed and scalable
2930 // vector types.
2931 switch (Opcode) {
2932 case ISD::SPLAT_VECTOR:
2933 UndefElts = V.getOperand(0).isUndef()
2934 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2935 : APInt(DemandedElts.getBitWidth(), 0);
2936 return true;
2937 case ISD::ADD:
2938 case ISD::SUB:
2939 case ISD::AND:
2940 case ISD::XOR:
2941 case ISD::OR: {
2942 APInt UndefLHS, UndefRHS;
2943 SDValue LHS = V.getOperand(0);
2944 SDValue RHS = V.getOperand(1);
2945 // Only recognize splats with the same demanded undef elements for both
2946 // operands, otherwise we might fail to handle binop-specific undef
2947 // handling.
2948 // e.g. (and undef, 0) -> 0 etc.
2949 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2950 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
2951 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
2952 UndefElts = UndefLHS | UndefRHS;
2953 return true;
2954 }
2955 return false;
2956 }
2957 case ISD::ABS:
2958 case ISD::TRUNCATE:
2959 case ISD::SIGN_EXTEND:
2960 case ISD::ZERO_EXTEND:
2961 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2962 default:
2963 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2964 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2965 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2966 Depth);
2967 break;
2968 }
2969
2970 // We don't support other cases than those above for scalable vectors at
2971 // the moment.
2972 if (VT.isScalableVector())
2973 return false;
2974
2975 unsigned NumElts = VT.getVectorNumElements();
2976 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2977 UndefElts = APInt::getZero(NumElts);
2978
2979 switch (Opcode) {
2980 case ISD::BUILD_VECTOR: {
2981 SDValue Scl;
2982 for (unsigned i = 0; i != NumElts; ++i) {
2983 SDValue Op = V.getOperand(i);
2984 if (Op.isUndef()) {
2985 UndefElts.setBit(i);
2986 continue;
2987 }
2988 if (!DemandedElts[i])
2989 continue;
2990 if (Scl && Scl != Op)
2991 return false;
2992 Scl = Op;
2993 }
2994 return true;
2995 }
2996 case ISD::VECTOR_SHUFFLE: {
2997 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2998 APInt DemandedLHS = APInt::getZero(NumElts);
2999 APInt DemandedRHS = APInt::getZero(NumElts);
3000 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
3001 for (int i = 0; i != (int)NumElts; ++i) {
3002 int M = Mask[i];
3003 if (M < 0) {
3004 UndefElts.setBit(i);
3005 continue;
3006 }
3007 if (!DemandedElts[i])
3008 continue;
3009 if (M < (int)NumElts)
3010 DemandedLHS.setBit(M);
3011 else
3012 DemandedRHS.setBit(M - NumElts);
3013 }
3014
3015 // If we aren't demanding either op, assume there's no splat.
3016 // If we are demanding both ops, assume there's no splat.
3017 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
3018 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3019 return false;
3020
3021 // See if the demanded elts of the source op is a splat or we only demand
3022 // one element, which should always be a splat.
3023 // TODO: Handle source ops splats with undefs.
3024 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3025 APInt SrcUndefs;
3026 return (SrcElts.popcount() == 1) ||
3027 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3028 (SrcElts & SrcUndefs).isZero());
3029 };
3030 if (!DemandedLHS.isZero())
3031 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3032 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3033 }
3035 // Offset the demanded elts by the subvector index.
3036 SDValue Src = V.getOperand(0);
3037 // We don't support scalable vectors at the moment.
3038 if (Src.getValueType().isScalableVector())
3039 return false;
3040 uint64_t Idx = V.getConstantOperandVal(1);
3041 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3042 APInt UndefSrcElts;
3043 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3044 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3045 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3046 return true;
3047 }
3048 break;
3049 }
3053 // Widen the demanded elts by the src element count.
3054 SDValue Src = V.getOperand(0);
3055 // We don't support scalable vectors at the moment.
3056 if (Src.getValueType().isScalableVector())
3057 return false;
3058 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3059 APInt UndefSrcElts;
3060 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3061 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3062 UndefElts = UndefSrcElts.trunc(NumElts);
3063 return true;
3064 }
3065 break;
3066 }
3067 case ISD::BITCAST: {
3068 SDValue Src = V.getOperand(0);
3069 EVT SrcVT = Src.getValueType();
3070 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3071 unsigned BitWidth = VT.getScalarSizeInBits();
3072
3073 // Ignore bitcasts from unsupported types.
3074 // TODO: Add fp support?
3075 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3076 break;
3077
3078 // Bitcast 'small element' vector to 'large element' vector.
3079 if ((BitWidth % SrcBitWidth) == 0) {
3080 // See if each sub element is a splat.
3081 unsigned Scale = BitWidth / SrcBitWidth;
3082 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3083 APInt ScaledDemandedElts =
3084 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3085 for (unsigned I = 0; I != Scale; ++I) {
3086 APInt SubUndefElts;
3087 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3088 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3089 SubDemandedElts &= ScaledDemandedElts;
3090 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3091 return false;
3092 // TODO: Add support for merging sub undef elements.
3093 if (!SubUndefElts.isZero())
3094 return false;
3095 }
3096 return true;
3097 }
3098 break;
3099 }
3100 }
3101
3102 return false;
3103}
3104
3105/// Helper wrapper to main isSplatValue function.
3106bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3107 EVT VT = V.getValueType();
3108 assert(VT.isVector() && "Vector type expected");
3109
3110 APInt UndefElts;
3111 // Since the number of lanes in a scalable vector is unknown at compile time,
3112 // we track one bit which is implicitly broadcast to all lanes. This means
3113 // that all lanes in a scalable vector are considered demanded.
3114 APInt DemandedElts
3116 return isSplatValue(V, DemandedElts, UndefElts) &&
3117 (AllowUndefs || !UndefElts);
3118}
3119
3122
3123 EVT VT = V.getValueType();
3124 unsigned Opcode = V.getOpcode();
3125 switch (Opcode) {
3126 default: {
3127 APInt UndefElts;
3128 // Since the number of lanes in a scalable vector is unknown at compile time,
3129 // we track one bit which is implicitly broadcast to all lanes. This means
3130 // that all lanes in a scalable vector are considered demanded.
3131 APInt DemandedElts
3133
3134 if (isSplatValue(V, DemandedElts, UndefElts)) {
3135 if (VT.isScalableVector()) {
3136 // DemandedElts and UndefElts are ignored for scalable vectors, since
3137 // the only supported cases are SPLAT_VECTOR nodes.
3138 SplatIdx = 0;
3139 } else {
3140 // Handle case where all demanded elements are UNDEF.
3141 if (DemandedElts.isSubsetOf(UndefElts)) {
3142 SplatIdx = 0;
3143 return getUNDEF(VT);
3144 }
3145 SplatIdx = (UndefElts & DemandedElts).countr_one();
3146 }
3147 return V;
3148 }
3149 break;
3150 }
3151 case ISD::SPLAT_VECTOR:
3152 SplatIdx = 0;
3153 return V;
3154 case ISD::VECTOR_SHUFFLE: {
3155 assert(!VT.isScalableVector());
3156 // Check if this is a shuffle node doing a splat.
3157 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3158 // getTargetVShiftNode currently struggles without the splat source.
3159 auto *SVN = cast<ShuffleVectorSDNode>(V);
3160 if (!SVN->isSplat())
3161 break;
3162 int Idx = SVN->getSplatIndex();
3163 int NumElts = V.getValueType().getVectorNumElements();
3164 SplatIdx = Idx % NumElts;
3165 return V.getOperand(Idx / NumElts);
3166 }
3167 }
3168
3169 return SDValue();
3170}
3171
3173 int SplatIdx;
3174 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3175 EVT SVT = SrcVector.getValueType().getScalarType();
3176 EVT LegalSVT = SVT;
3177 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3178 if (!SVT.isInteger())
3179 return SDValue();
3180 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3181 if (LegalSVT.bitsLT(SVT))
3182 return SDValue();
3183 }
3184 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3185 }
3186 return SDValue();
3187}
3188
3189std::optional<ConstantRange>
3191 unsigned Depth) const {
3192 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3193 V.getOpcode() == ISD::SRA) &&
3194 "Unknown shift node");
3195 // Shifting more than the bitwidth is not valid.
3196 unsigned BitWidth = V.getScalarValueSizeInBits();
3197
3198 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3199 const APInt &ShAmt = Cst->getAPIntValue();
3200 if (ShAmt.uge(BitWidth))
3201 return std::nullopt;
3202 return ConstantRange(ShAmt);
3203 }
3204
3205 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3206 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3207 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3208 if (!DemandedElts[i])
3209 continue;
3210 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3211 if (!SA) {
3212 MinAmt = MaxAmt = nullptr;
3213 break;
3214 }
3215 const APInt &ShAmt = SA->getAPIntValue();
3216 if (ShAmt.uge(BitWidth))
3217 return std::nullopt;
3218 if (!MinAmt || MinAmt->ugt(ShAmt))
3219 MinAmt = &ShAmt;
3220 if (!MaxAmt || MaxAmt->ult(ShAmt))
3221 MaxAmt = &ShAmt;
3222 }
3223 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3224 "Failed to find matching min/max shift amounts");
3225 if (MinAmt && MaxAmt)
3226 return ConstantRange(*MinAmt, *MaxAmt + 1);
3227 }
3228
3229 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3230 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3231 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3232 if (KnownAmt.getMaxValue().ult(BitWidth))
3233 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3234
3235 return std::nullopt;
3236}
3237
3238std::optional<unsigned>
3240 unsigned Depth) const {
3241 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3242 V.getOpcode() == ISD::SRA) &&
3243 "Unknown shift node");
3244 if (std::optional<ConstantRange> AmtRange =
3245 getValidShiftAmountRange(V, DemandedElts, Depth))
3246 if (const APInt *ShAmt = AmtRange->getSingleElement())
3247 return ShAmt->getZExtValue();
3248 return std::nullopt;
3249}
3250
3251std::optional<unsigned>
3253 EVT VT = V.getValueType();
3254 APInt DemandedElts = VT.isFixedLengthVector()
3256 : APInt(1, 1);
3257 return getValidShiftAmount(V, DemandedElts, Depth);
3258}
3259
3260std::optional<unsigned>
3262 unsigned Depth) const {
3263 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3264 V.getOpcode() == ISD::SRA) &&
3265 "Unknown shift node");
3266 if (std::optional<ConstantRange> AmtRange =
3267 getValidShiftAmountRange(V, DemandedElts, Depth))
3268 return AmtRange->getUnsignedMin().getZExtValue();
3269 return std::nullopt;
3270}
3271
3272std::optional<unsigned>
3274 EVT VT = V.getValueType();
3275 APInt DemandedElts = VT.isFixedLengthVector()
3277 : APInt(1, 1);
3278 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3279}
3280
3281std::optional<unsigned>
3283 unsigned Depth) const {
3284 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3285 V.getOpcode() == ISD::SRA) &&
3286 "Unknown shift node");
3287 if (std::optional<ConstantRange> AmtRange =
3288 getValidShiftAmountRange(V, DemandedElts, Depth))
3289 return AmtRange->getUnsignedMax().getZExtValue();
3290 return std::nullopt;
3291}
3292
3293std::optional<unsigned>
3295 EVT VT = V.getValueType();
3296 APInt DemandedElts = VT.isFixedLengthVector()
3298 : APInt(1, 1);
3299 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3300}
3301
3302/// Determine which bits of Op are known to be either zero or one and return
3303/// them in Known. For vectors, the known bits are those that are shared by
3304/// every vector element.
3306 EVT VT = Op.getValueType();
3307
3308 // Since the number of lanes in a scalable vector is unknown at compile time,
3309 // we track one bit which is implicitly broadcast to all lanes. This means
3310 // that all lanes in a scalable vector are considered demanded.
3311 APInt DemandedElts = VT.isFixedLengthVector()
3313 : APInt(1, 1);
3314 return computeKnownBits(Op, DemandedElts, Depth);
3315}
3316
3317/// Determine which bits of Op are known to be either zero or one and return
3318/// them in Known. The DemandedElts argument allows us to only collect the known
3319/// bits that are shared by the requested vector elements.
3321 unsigned Depth) const {
3322 unsigned BitWidth = Op.getScalarValueSizeInBits();
3323
3324 KnownBits Known(BitWidth); // Don't know anything.
3325
3326 if (auto OptAPInt = Op->bitcastToAPInt()) {
3327 // We know all of the bits for a constant!
3328 return KnownBits::makeConstant(*std::move(OptAPInt));
3329 }
3330
3331 if (Depth >= MaxRecursionDepth)
3332 return Known; // Limit search depth.
3333
3334 KnownBits Known2;
3335 unsigned NumElts = DemandedElts.getBitWidth();
3336 assert((!Op.getValueType().isFixedLengthVector() ||
3337 NumElts == Op.getValueType().getVectorNumElements()) &&
3338 "Unexpected vector size");
3339
3340 if (!DemandedElts)
3341 return Known; // No demanded elts, better to assume we don't know anything.
3342
3343 unsigned Opcode = Op.getOpcode();
3344 switch (Opcode) {
3345 case ISD::MERGE_VALUES:
3346 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3347 Depth + 1);
3348 case ISD::SPLAT_VECTOR: {
3349 SDValue SrcOp = Op.getOperand(0);
3350 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3351 "Expected SPLAT_VECTOR implicit truncation");
3352 // Implicitly truncate the bits to match the official semantics of
3353 // SPLAT_VECTOR.
3354 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3355 break;
3356 }
3358 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3359 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3360 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3361 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3362 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3363 }
3364 break;
3365 }
3366 case ISD::STEP_VECTOR: {
3367 const APInt &Step = Op.getConstantOperandAPInt(0);
3368
3369 if (Step.isPowerOf2())
3370 Known.Zero.setLowBits(Step.logBase2());
3371
3373
3374 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3375 break;
3376 const APInt MinNumElts =
3377 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3378
3379 bool Overflow;
3380 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3382 .umul_ov(MinNumElts, Overflow);
3383 if (Overflow)
3384 break;
3385
3386 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3387 if (Overflow)
3388 break;
3389
3390 Known.Zero.setHighBits(MaxValue.countl_zero());
3391 break;
3392 }
3393 case ISD::BUILD_VECTOR:
3394 assert(!Op.getValueType().isScalableVector());
3395 // Collect the known bits that are shared by every demanded vector element.
3396 Known.setAllConflict();
3397 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3398 if (!DemandedElts[i])
3399 continue;
3400
3401 SDValue SrcOp = Op.getOperand(i);
3402 Known2 = computeKnownBits(SrcOp, Depth + 1);
3403
3404 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3405 if (SrcOp.getValueSizeInBits() != BitWidth) {
3406 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3407 "Expected BUILD_VECTOR implicit truncation");
3408 Known2 = Known2.trunc(BitWidth);
3409 }
3410
3411 // Known bits are the values that are shared by every demanded element.
3412 Known = Known.intersectWith(Known2);
3413
3414 // If we don't know any bits, early out.
3415 if (Known.isUnknown())
3416 break;
3417 }
3418 break;
3419 case ISD::VECTOR_COMPRESS: {
3420 SDValue Vec = Op.getOperand(0);
3421 SDValue PassThru = Op.getOperand(2);
3422 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3423 // If we don't know any bits, early out.
3424 if (Known.isUnknown())
3425 break;
3426 Known2 = computeKnownBits(Vec, Depth + 1);
3427 Known = Known.intersectWith(Known2);
3428 break;
3429 }
3430 case ISD::VECTOR_SHUFFLE: {
3431 assert(!Op.getValueType().isScalableVector());
3432 // Collect the known bits that are shared by every vector element referenced
3433 // by the shuffle.
3434 APInt DemandedLHS, DemandedRHS;
3436 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3437 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3438 DemandedLHS, DemandedRHS))
3439 break;
3440
3441 // Known bits are the values that are shared by every demanded element.
3442 Known.setAllConflict();
3443 if (!!DemandedLHS) {
3444 SDValue LHS = Op.getOperand(0);
3445 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3446 Known = Known.intersectWith(Known2);
3447 }
3448 // If we don't know any bits, early out.
3449 if (Known.isUnknown())
3450 break;
3451 if (!!DemandedRHS) {
3452 SDValue RHS = Op.getOperand(1);
3453 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3454 Known = Known.intersectWith(Known2);
3455 }
3456 break;
3457 }
3458 case ISD::VSCALE: {
3460 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3461 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3462 break;
3463 }
3464 case ISD::CONCAT_VECTORS: {
3465 if (Op.getValueType().isScalableVector())
3466 break;
3467 // Split DemandedElts and test each of the demanded subvectors.
3468 Known.setAllConflict();
3469 EVT SubVectorVT = Op.getOperand(0).getValueType();
3470 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3471 unsigned NumSubVectors = Op.getNumOperands();
3472 for (unsigned i = 0; i != NumSubVectors; ++i) {
3473 APInt DemandedSub =
3474 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3475 if (!!DemandedSub) {
3476 SDValue Sub = Op.getOperand(i);
3477 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3478 Known = Known.intersectWith(Known2);
3479 }
3480 // If we don't know any bits, early out.
3481 if (Known.isUnknown())
3482 break;
3483 }
3484 break;
3485 }
3486 case ISD::INSERT_SUBVECTOR: {
3487 if (Op.getValueType().isScalableVector())
3488 break;
3489 // Demand any elements from the subvector and the remainder from the src its
3490 // inserted into.
3491 SDValue Src = Op.getOperand(0);
3492 SDValue Sub = Op.getOperand(1);
3493 uint64_t Idx = Op.getConstantOperandVal(2);
3494 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3495 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3496 APInt DemandedSrcElts = DemandedElts;
3497 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3498
3499 Known.setAllConflict();
3500 if (!!DemandedSubElts) {
3501 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3502 if (Known.isUnknown())
3503 break; // early-out.
3504 }
3505 if (!!DemandedSrcElts) {
3506 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3507 Known = Known.intersectWith(Known2);
3508 }
3509 break;
3510 }
3512 // Offset the demanded elts by the subvector index.
3513 SDValue Src = Op.getOperand(0);
3514 // Bail until we can represent demanded elements for scalable vectors.
3515 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3516 break;
3517 uint64_t Idx = Op.getConstantOperandVal(1);
3518 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3519 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3520 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3521 break;
3522 }
3523 case ISD::SCALAR_TO_VECTOR: {
3524 if (Op.getValueType().isScalableVector())
3525 break;
3526 // We know about scalar_to_vector as much as we know about it source,
3527 // which becomes the first element of otherwise unknown vector.
3528 if (DemandedElts != 1)
3529 break;
3530
3531 SDValue N0 = Op.getOperand(0);
3532 Known = computeKnownBits(N0, Depth + 1);
3533 if (N0.getValueSizeInBits() != BitWidth)
3534 Known = Known.trunc(BitWidth);
3535
3536 break;
3537 }
3538 case ISD::BITCAST: {
3539 if (Op.getValueType().isScalableVector())
3540 break;
3541
3542 SDValue N0 = Op.getOperand(0);
3543 EVT SubVT = N0.getValueType();
3544 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3545
3546 // Ignore bitcasts from unsupported types.
3547 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3548 break;
3549
3550 // Fast handling of 'identity' bitcasts.
3551 if (BitWidth == SubBitWidth) {
3552 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3553 break;
3554 }
3555
3556 bool IsLE = getDataLayout().isLittleEndian();
3557
3558 // Bitcast 'small element' vector to 'large element' scalar/vector.
3559 if ((BitWidth % SubBitWidth) == 0) {
3560 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3561
3562 // Collect known bits for the (larger) output by collecting the known
3563 // bits from each set of sub elements and shift these into place.
3564 // We need to separately call computeKnownBits for each set of
3565 // sub elements as the knownbits for each is likely to be different.
3566 unsigned SubScale = BitWidth / SubBitWidth;
3567 APInt SubDemandedElts(NumElts * SubScale, 0);
3568 for (unsigned i = 0; i != NumElts; ++i)
3569 if (DemandedElts[i])
3570 SubDemandedElts.setBit(i * SubScale);
3571
3572 for (unsigned i = 0; i != SubScale; ++i) {
3573 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3574 Depth + 1);
3575 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3576 Known.insertBits(Known2, SubBitWidth * Shifts);
3577 }
3578 }
3579
3580 // Bitcast 'large element' scalar/vector to 'small element' vector.
3581 if ((SubBitWidth % BitWidth) == 0) {
3582 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3583
3584 // Collect known bits for the (smaller) output by collecting the known
3585 // bits from the overlapping larger input elements and extracting the
3586 // sub sections we actually care about.
3587 unsigned SubScale = SubBitWidth / BitWidth;
3588 APInt SubDemandedElts =
3589 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3590 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3591
3592 Known.setAllConflict();
3593 for (unsigned i = 0; i != NumElts; ++i)
3594 if (DemandedElts[i]) {
3595 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3596 unsigned Offset = (Shifts % SubScale) * BitWidth;
3597 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3598 // If we don't know any bits, early out.
3599 if (Known.isUnknown())
3600 break;
3601 }
3602 }
3603 break;
3604 }
3605 case ISD::AND:
3606 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3607 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3608
3609 Known &= Known2;
3610 break;
3611 case ISD::OR:
3612 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3613 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3614
3615 Known |= Known2;
3616 break;
3617 case ISD::XOR:
3618 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3619 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3620
3621 Known ^= Known2;
3622 break;
3623 case ISD::MUL: {
3624 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3625 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3626 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3627 // TODO: SelfMultiply can be poison, but not undef.
3628 if (SelfMultiply)
3629 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3630 Op.getOperand(0), DemandedElts, false, Depth + 1);
3631 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3632
3633 // If the multiplication is known not to overflow, the product of a number
3634 // with itself is non-negative. Only do this if we didn't already computed
3635 // the opposite value for the sign bit.
3636 if (Op->getFlags().hasNoSignedWrap() &&
3637 Op.getOperand(0) == Op.getOperand(1) &&
3638 !Known.isNegative())
3639 Known.makeNonNegative();
3640 break;
3641 }
3642 case ISD::MULHU: {
3643 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3644 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3645 Known = KnownBits::mulhu(Known, Known2);
3646 break;
3647 }
3648 case ISD::MULHS: {
3649 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3650 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3651 Known = KnownBits::mulhs(Known, Known2);
3652 break;
3653 }
3654 case ISD::ABDU: {
3655 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3656 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3657 Known = KnownBits::abdu(Known, Known2);
3658 break;
3659 }
3660 case ISD::ABDS: {
3661 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3662 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3663 Known = KnownBits::abds(Known, Known2);
3664 unsigned SignBits1 =
3665 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3666 if (SignBits1 == 1)
3667 break;
3668 unsigned SignBits0 =
3669 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3670 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3671 break;
3672 }
3673 case ISD::UMUL_LOHI: {
3674 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3675 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3676 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3677 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3678 if (Op.getResNo() == 0)
3679 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3680 else
3681 Known = KnownBits::mulhu(Known, Known2);
3682 break;
3683 }
3684 case ISD::SMUL_LOHI: {
3685 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3686 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3687 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3688 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3689 if (Op.getResNo() == 0)
3690 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3691 else
3692 Known = KnownBits::mulhs(Known, Known2);
3693 break;
3694 }
3695 case ISD::AVGFLOORU: {
3696 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3697 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3698 Known = KnownBits::avgFloorU(Known, Known2);
3699 break;
3700 }
3701 case ISD::AVGCEILU: {
3702 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3703 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3704 Known = KnownBits::avgCeilU(Known, Known2);
3705 break;
3706 }
3707 case ISD::AVGFLOORS: {
3708 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3709 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3710 Known = KnownBits::avgFloorS(Known, Known2);
3711 break;
3712 }
3713 case ISD::AVGCEILS: {
3714 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3715 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3716 Known = KnownBits::avgCeilS(Known, Known2);
3717 break;
3718 }
3719 case ISD::SELECT:
3720 case ISD::VSELECT:
3721 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3722 // If we don't know any bits, early out.
3723 if (Known.isUnknown())
3724 break;
3725 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3726
3727 // Only known if known in both the LHS and RHS.
3728 Known = Known.intersectWith(Known2);
3729 break;
3730 case ISD::SELECT_CC:
3731 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3732 // If we don't know any bits, early out.
3733 if (Known.isUnknown())
3734 break;
3735 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3736
3737 // Only known if known in both the LHS and RHS.
3738 Known = Known.intersectWith(Known2);
3739 break;
3740 case ISD::SMULO:
3741 case ISD::UMULO:
3742 if (Op.getResNo() != 1)
3743 break;
3744 // The boolean result conforms to getBooleanContents.
3745 // If we know the result of a setcc has the top bits zero, use this info.
3746 // We know that we have an integer-based boolean since these operations
3747 // are only available for integer.
3748 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3750 BitWidth > 1)
3751 Known.Zero.setBitsFrom(1);
3752 break;
3753 case ISD::SETCC:
3754 case ISD::SETCCCARRY:
3755 case ISD::STRICT_FSETCC:
3756 case ISD::STRICT_FSETCCS: {
3757 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3758 // If we know the result of a setcc has the top bits zero, use this info.
3759 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3761 BitWidth > 1)
3762 Known.Zero.setBitsFrom(1);
3763 break;
3764 }
3765 case ISD::SHL: {
3766 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3767 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3768
3769 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3770 bool NSW = Op->getFlags().hasNoSignedWrap();
3771
3772 bool ShAmtNonZero = Known2.isNonZero();
3773
3774 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3775
3776 // Minimum shift low bits are known zero.
3777 if (std::optional<unsigned> ShMinAmt =
3778 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3779 Known.Zero.setLowBits(*ShMinAmt);
3780 break;
3781 }
3782 case ISD::SRL:
3783 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3784 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3785 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3786 Op->getFlags().hasExact());
3787
3788 // Minimum shift high bits are known zero.
3789 if (std::optional<unsigned> ShMinAmt =
3790 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3791 Known.Zero.setHighBits(*ShMinAmt);
3792 break;
3793 case ISD::SRA:
3794 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3795 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3796 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3797 Op->getFlags().hasExact());
3798 break;
3799 case ISD::ROTL:
3800 case ISD::ROTR:
3801 if (ConstantSDNode *C =
3802 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3803 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3804
3805 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3806
3807 // Canonicalize to ROTR.
3808 if (Opcode == ISD::ROTL && Amt != 0)
3809 Amt = BitWidth - Amt;
3810
3811 Known.Zero = Known.Zero.rotr(Amt);
3812 Known.One = Known.One.rotr(Amt);
3813 }
3814 break;
3815 case ISD::FSHL:
3816 case ISD::FSHR:
3817 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3818 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3819
3820 // For fshl, 0-shift returns the 1st arg.
3821 // For fshr, 0-shift returns the 2nd arg.
3822 if (Amt == 0) {
3823 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3824 DemandedElts, Depth + 1);
3825 break;
3826 }
3827
3828 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3829 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3830 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3831 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3832 if (Opcode == ISD::FSHL) {
3833 Known <<= Amt;
3834 Known2 >>= BitWidth - Amt;
3835 } else {
3836 Known <<= BitWidth - Amt;
3837 Known2 >>= Amt;
3838 }
3839 Known = Known.unionWith(Known2);
3840 }
3841 break;
3842 case ISD::SHL_PARTS:
3843 case ISD::SRA_PARTS:
3844 case ISD::SRL_PARTS: {
3845 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3846
3847 // Collect lo/hi source values and concatenate.
3848 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3849 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3850 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3851 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3852 Known = Known2.concat(Known);
3853
3854 // Collect shift amount.
3855 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3856
3857 if (Opcode == ISD::SHL_PARTS)
3858 Known = KnownBits::shl(Known, Known2);
3859 else if (Opcode == ISD::SRA_PARTS)
3860 Known = KnownBits::ashr(Known, Known2);
3861 else // if (Opcode == ISD::SRL_PARTS)
3862 Known = KnownBits::lshr(Known, Known2);
3863
3864 // TODO: Minimum shift low/high bits are known zero.
3865
3866 if (Op.getResNo() == 0)
3867 Known = Known.extractBits(LoBits, 0);
3868 else
3869 Known = Known.extractBits(HiBits, LoBits);
3870 break;
3871 }
3873 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3874 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3875 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3876 break;
3877 }
3878 case ISD::CTTZ:
3879 case ISD::CTTZ_ZERO_UNDEF: {
3880 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3881 // If we have a known 1, its position is our upper bound.
3882 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3883 unsigned LowBits = llvm::bit_width(PossibleTZ);
3884 Known.Zero.setBitsFrom(LowBits);
3885 break;
3886 }
3887 case ISD::CTLZ:
3888 case ISD::CTLZ_ZERO_UNDEF: {
3889 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3890 // If we have a known 1, its position is our upper bound.
3891 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3892 unsigned LowBits = llvm::bit_width(PossibleLZ);
3893 Known.Zero.setBitsFrom(LowBits);
3894 break;
3895 }
3896 case ISD::CTLS: {
3897 unsigned MinRedundantSignBits =
3898 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1;
3899 ConstantRange Range(APInt(BitWidth, MinRedundantSignBits),
3901 Known = Range.toKnownBits();
3902 break;
3903 }
3904 case ISD::CTPOP: {
3905 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3906 // If we know some of the bits are zero, they can't be one.
3907 unsigned PossibleOnes = Known2.countMaxPopulation();
3908 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3909 break;
3910 }
3911 case ISD::PARITY: {
3912 // Parity returns 0 everywhere but the LSB.
3913 Known.Zero.setBitsFrom(1);
3914 break;
3915 }
3916 case ISD::MGATHER:
3917 case ISD::MLOAD: {
3918 ISD::LoadExtType ETy =
3919 (Opcode == ISD::MGATHER)
3920 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3921 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3922 if (ETy == ISD::ZEXTLOAD) {
3923 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3924 KnownBits Known0(MemVT.getScalarSizeInBits());
3925 return Known0.zext(BitWidth);
3926 }
3927 break;
3928 }
3929 case ISD::LOAD: {
3931 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3932 if (ISD::isNON_EXTLoad(LD) && Cst) {
3933 // Determine any common known bits from the loaded constant pool value.
3934 Type *CstTy = Cst->getType();
3935 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3936 !Op.getValueType().isScalableVector()) {
3937 // If its a vector splat, then we can (quickly) reuse the scalar path.
3938 // NOTE: We assume all elements match and none are UNDEF.
3939 if (CstTy->isVectorTy()) {
3940 if (const Constant *Splat = Cst->getSplatValue()) {
3941 Cst = Splat;
3942 CstTy = Cst->getType();
3943 }
3944 }
3945 // TODO - do we need to handle different bitwidths?
3946 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3947 // Iterate across all vector elements finding common known bits.
3948 Known.setAllConflict();
3949 for (unsigned i = 0; i != NumElts; ++i) {
3950 if (!DemandedElts[i])
3951 continue;
3952 if (Constant *Elt = Cst->getAggregateElement(i)) {
3953 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3954 const APInt &Value = CInt->getValue();
3955 Known.One &= Value;
3956 Known.Zero &= ~Value;
3957 continue;
3958 }
3959 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3960 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3961 Known.One &= Value;
3962 Known.Zero &= ~Value;
3963 continue;
3964 }
3965 }
3966 Known.One.clearAllBits();
3967 Known.Zero.clearAllBits();
3968 break;
3969 }
3970 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3971 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3972 Known = KnownBits::makeConstant(CInt->getValue());
3973 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3974 Known =
3975 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3976 }
3977 }
3978 }
3979 } else if (Op.getResNo() == 0) {
3980 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
3981 KnownBits KnownScalarMemory(ScalarMemorySize);
3982 if (const MDNode *MD = LD->getRanges())
3983 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
3984
3985 // Extend the Known bits from memory to the size of the scalar result.
3986 if (ISD::isZEXTLoad(Op.getNode()))
3987 Known = KnownScalarMemory.zext(BitWidth);
3988 else if (ISD::isSEXTLoad(Op.getNode()))
3989 Known = KnownScalarMemory.sext(BitWidth);
3990 else if (ISD::isEXTLoad(Op.getNode()))
3991 Known = KnownScalarMemory.anyext(BitWidth);
3992 else
3993 Known = KnownScalarMemory;
3994 assert(Known.getBitWidth() == BitWidth);
3995 return Known;
3996 }
3997 break;
3998 }
4000 if (Op.getValueType().isScalableVector())
4001 break;
4002 EVT InVT = Op.getOperand(0).getValueType();
4003 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4004 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4005 Known = Known.zext(BitWidth);
4006 break;
4007 }
4008 case ISD::ZERO_EXTEND: {
4009 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4010 Known = Known.zext(BitWidth);
4011 break;
4012 }
4014 if (Op.getValueType().isScalableVector())
4015 break;
4016 EVT InVT = Op.getOperand(0).getValueType();
4017 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4018 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4019 // If the sign bit is known to be zero or one, then sext will extend
4020 // it to the top bits, else it will just zext.
4021 Known = Known.sext(BitWidth);
4022 break;
4023 }
4024 case ISD::SIGN_EXTEND: {
4025 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4026 // If the sign bit is known to be zero or one, then sext will extend
4027 // it to the top bits, else it will just zext.
4028 Known = Known.sext(BitWidth);
4029 break;
4030 }
4032 if (Op.getValueType().isScalableVector())
4033 break;
4034 EVT InVT = Op.getOperand(0).getValueType();
4035 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4036 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4037 Known = Known.anyext(BitWidth);
4038 break;
4039 }
4040 case ISD::ANY_EXTEND: {
4041 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4042 Known = Known.anyext(BitWidth);
4043 break;
4044 }
4045 case ISD::TRUNCATE: {
4046 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4047 Known = Known.trunc(BitWidth);
4048 break;
4049 }
4050 case ISD::AssertZext: {
4051 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4053 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4054 Known.Zero |= (~InMask);
4055 Known.One &= (~Known.Zero);
4056 break;
4057 }
4058 case ISD::AssertAlign: {
4059 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4060 assert(LogOfAlign != 0);
4061
4062 // TODO: Should use maximum with source
4063 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4064 // well as clearing one bits.
4065 Known.Zero.setLowBits(LogOfAlign);
4066 Known.One.clearLowBits(LogOfAlign);
4067 break;
4068 }
4069 case ISD::AssertNoFPClass: {
4070 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4071
4072 FPClassTest NoFPClass =
4073 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4074 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4075 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4076 // Cannot be negative.
4077 Known.makeNonNegative();
4078 }
4079
4080 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4081 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4082 // Cannot be positive.
4083 Known.makeNegative();
4084 }
4085
4086 break;
4087 }
4088 case ISD::FGETSIGN:
4089 // All bits are zero except the low bit.
4090 Known.Zero.setBitsFrom(1);
4091 break;
4092 case ISD::ADD:
4093 case ISD::SUB: {
4094 SDNodeFlags Flags = Op.getNode()->getFlags();
4095 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4096 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4098 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4099 Flags.hasNoUnsignedWrap(), Known, Known2);
4100 break;
4101 }
4102 case ISD::USUBO:
4103 case ISD::SSUBO:
4104 case ISD::USUBO_CARRY:
4105 case ISD::SSUBO_CARRY:
4106 if (Op.getResNo() == 1) {
4107 // If we know the result of a setcc has the top bits zero, use this info.
4108 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4110 BitWidth > 1)
4111 Known.Zero.setBitsFrom(1);
4112 break;
4113 }
4114 [[fallthrough]];
4115 case ISD::SUBC: {
4116 assert(Op.getResNo() == 0 &&
4117 "We only compute knownbits for the difference here.");
4118
4119 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4120 KnownBits Borrow(1);
4121 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4122 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4123 // Borrow has bit width 1
4124 Borrow = Borrow.trunc(1);
4125 } else {
4126 Borrow.setAllZero();
4127 }
4128
4129 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4130 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4131 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4132 break;
4133 }
4134 case ISD::UADDO:
4135 case ISD::SADDO:
4136 case ISD::UADDO_CARRY:
4137 case ISD::SADDO_CARRY:
4138 if (Op.getResNo() == 1) {
4139 // If we know the result of a setcc has the top bits zero, use this info.
4140 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4142 BitWidth > 1)
4143 Known.Zero.setBitsFrom(1);
4144 break;
4145 }
4146 [[fallthrough]];
4147 case ISD::ADDC:
4148 case ISD::ADDE: {
4149 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4150
4151 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4152 KnownBits Carry(1);
4153 if (Opcode == ISD::ADDE)
4154 // Can't track carry from glue, set carry to unknown.
4155 Carry.resetAll();
4156 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4157 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4158 // Carry has bit width 1
4159 Carry = Carry.trunc(1);
4160 } else {
4161 Carry.setAllZero();
4162 }
4163
4164 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4165 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4166 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4167 break;
4168 }
4169 case ISD::UDIV: {
4170 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4171 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4172 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4173 break;
4174 }
4175 case ISD::SDIV: {
4176 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4177 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4178 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4179 break;
4180 }
4181 case ISD::SREM: {
4182 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4183 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4184 Known = KnownBits::srem(Known, Known2);
4185 break;
4186 }
4187 case ISD::UREM: {
4188 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4189 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4190 Known = KnownBits::urem(Known, Known2);
4191 break;
4192 }
4193 case ISD::EXTRACT_ELEMENT: {
4194 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4195 const unsigned Index = Op.getConstantOperandVal(1);
4196 const unsigned EltBitWidth = Op.getValueSizeInBits();
4197
4198 // Remove low part of known bits mask
4199 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4200 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4201
4202 // Remove high part of known bit mask
4203 Known = Known.trunc(EltBitWidth);
4204 break;
4205 }
4207 SDValue InVec = Op.getOperand(0);
4208 SDValue EltNo = Op.getOperand(1);
4209 EVT VecVT = InVec.getValueType();
4210 // computeKnownBits not yet implemented for scalable vectors.
4211 if (VecVT.isScalableVector())
4212 break;
4213 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4214 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4215
4216 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4217 // anything about the extended bits.
4218 if (BitWidth > EltBitWidth)
4219 Known = Known.trunc(EltBitWidth);
4220
4221 // If we know the element index, just demand that vector element, else for
4222 // an unknown element index, ignore DemandedElts and demand them all.
4223 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4224 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4225 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4226 DemandedSrcElts =
4227 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4228
4229 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4230 if (BitWidth > EltBitWidth)
4231 Known = Known.anyext(BitWidth);
4232 break;
4233 }
4235 if (Op.getValueType().isScalableVector())
4236 break;
4237
4238 // If we know the element index, split the demand between the
4239 // source vector and the inserted element, otherwise assume we need
4240 // the original demanded vector elements and the value.
4241 SDValue InVec = Op.getOperand(0);
4242 SDValue InVal = Op.getOperand(1);
4243 SDValue EltNo = Op.getOperand(2);
4244 bool DemandedVal = true;
4245 APInt DemandedVecElts = DemandedElts;
4246 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4247 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4248 unsigned EltIdx = CEltNo->getZExtValue();
4249 DemandedVal = !!DemandedElts[EltIdx];
4250 DemandedVecElts.clearBit(EltIdx);
4251 }
4252 Known.setAllConflict();
4253 if (DemandedVal) {
4254 Known2 = computeKnownBits(InVal, Depth + 1);
4255 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4256 }
4257 if (!!DemandedVecElts) {
4258 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4259 Known = Known.intersectWith(Known2);
4260 }
4261 break;
4262 }
4263 case ISD::BITREVERSE: {
4264 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4265 Known = Known2.reverseBits();
4266 break;
4267 }
4268 case ISD::BSWAP: {
4269 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4270 Known = Known2.byteSwap();
4271 break;
4272 }
4273 case ISD::ABS: {
4274 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4275 Known = Known2.abs();
4276 Known.Zero.setHighBits(
4277 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4278 break;
4279 }
4280 case ISD::USUBSAT: {
4281 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4282 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4283 Known = KnownBits::usub_sat(Known, Known2);
4284 break;
4285 }
4286 case ISD::UMIN: {
4287 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4288 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4289 Known = KnownBits::umin(Known, Known2);
4290 break;
4291 }
4292 case ISD::UMAX: {
4293 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4294 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4295 Known = KnownBits::umax(Known, Known2);
4296 break;
4297 }
4298 case ISD::SMIN:
4299 case ISD::SMAX: {
4300 // If we have a clamp pattern, we know that the number of sign bits will be
4301 // the minimum of the clamp min/max range.
4302 bool IsMax = (Opcode == ISD::SMAX);
4303 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4304 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4305 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4306 CstHigh =
4307 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4308 if (CstLow && CstHigh) {
4309 if (!IsMax)
4310 std::swap(CstLow, CstHigh);
4311
4312 const APInt &ValueLow = CstLow->getAPIntValue();
4313 const APInt &ValueHigh = CstHigh->getAPIntValue();
4314 if (ValueLow.sle(ValueHigh)) {
4315 unsigned LowSignBits = ValueLow.getNumSignBits();
4316 unsigned HighSignBits = ValueHigh.getNumSignBits();
4317 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4318 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4319 Known.One.setHighBits(MinSignBits);
4320 break;
4321 }
4322 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4323 Known.Zero.setHighBits(MinSignBits);
4324 break;
4325 }
4326 }
4327 }
4328
4329 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4330 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4331 if (IsMax)
4332 Known = KnownBits::smax(Known, Known2);
4333 else
4334 Known = KnownBits::smin(Known, Known2);
4335
4336 // For SMAX, if CstLow is non-negative we know the result will be
4337 // non-negative and thus all sign bits are 0.
4338 // TODO: There's an equivalent of this for smin with negative constant for
4339 // known ones.
4340 if (IsMax && CstLow) {
4341 const APInt &ValueLow = CstLow->getAPIntValue();
4342 if (ValueLow.isNonNegative()) {
4343 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4344 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4345 }
4346 }
4347
4348 break;
4349 }
4350 case ISD::UINT_TO_FP: {
4351 Known.makeNonNegative();
4352 break;
4353 }
4354 case ISD::SINT_TO_FP: {
4355 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4356 if (Known2.isNonNegative())
4357 Known.makeNonNegative();
4358 else if (Known2.isNegative())
4359 Known.makeNegative();
4360 break;
4361 }
4362 case ISD::FP_TO_UINT_SAT: {
4363 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4364 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4366 break;
4367 }
4368 case ISD::ATOMIC_LOAD: {
4369 // If we are looking at the loaded value.
4370 if (Op.getResNo() == 0) {
4371 auto *AT = cast<AtomicSDNode>(Op);
4372 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4373 KnownBits KnownScalarMemory(ScalarMemorySize);
4374 if (const MDNode *MD = AT->getRanges())
4375 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4376
4377 switch (AT->getExtensionType()) {
4378 case ISD::ZEXTLOAD:
4379 Known = KnownScalarMemory.zext(BitWidth);
4380 break;
4381 case ISD::SEXTLOAD:
4382 Known = KnownScalarMemory.sext(BitWidth);
4383 break;
4384 case ISD::EXTLOAD:
4385 switch (TLI->getExtendForAtomicOps()) {
4386 case ISD::ZERO_EXTEND:
4387 Known = KnownScalarMemory.zext(BitWidth);
4388 break;
4389 case ISD::SIGN_EXTEND:
4390 Known = KnownScalarMemory.sext(BitWidth);
4391 break;
4392 default:
4393 Known = KnownScalarMemory.anyext(BitWidth);
4394 break;
4395 }
4396 break;
4397 case ISD::NON_EXTLOAD:
4398 Known = KnownScalarMemory;
4399 break;
4400 }
4401 assert(Known.getBitWidth() == BitWidth);
4402 }
4403 break;
4404 }
4406 if (Op.getResNo() == 1) {
4407 // The boolean result conforms to getBooleanContents.
4408 // If we know the result of a setcc has the top bits zero, use this info.
4409 // We know that we have an integer-based boolean since these operations
4410 // are only available for integer.
4411 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4413 BitWidth > 1)
4414 Known.Zero.setBitsFrom(1);
4415 break;
4416 }
4417 [[fallthrough]];
4419 case ISD::ATOMIC_SWAP:
4430 case ISD::ATOMIC_LOAD_UMAX: {
4431 // If we are looking at the loaded value.
4432 if (Op.getResNo() == 0) {
4433 auto *AT = cast<AtomicSDNode>(Op);
4434 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4435
4436 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4437 Known.Zero.setBitsFrom(MemBits);
4438 }
4439 break;
4440 }
4441 case ISD::FrameIndex:
4443 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4444 Known, getMachineFunction());
4445 break;
4446
4447 default:
4448 if (Opcode < ISD::BUILTIN_OP_END)
4449 break;
4450 [[fallthrough]];
4454 // TODO: Probably okay to remove after audit; here to reduce change size
4455 // in initial enablement patch for scalable vectors
4456 if (Op.getValueType().isScalableVector())
4457 break;
4458
4459 // Allow the target to implement this method for its nodes.
4460 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4461 break;
4462 }
4463
4464 return Known;
4465}
4466
4467/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4480
4483 // X + 0 never overflow
4484 if (isNullConstant(N1))
4485 return OFK_Never;
4486
4487 // If both operands each have at least two sign bits, the addition
4488 // cannot overflow.
4489 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4490 return OFK_Never;
4491
4492 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4493 return OFK_Sometime;
4494}
4495
4498 // X + 0 never overflow
4499 if (isNullConstant(N1))
4500 return OFK_Never;
4501
4502 // mulhi + 1 never overflow
4503 KnownBits N1Known = computeKnownBits(N1);
4504 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4505 N1Known.getMaxValue().ult(2))
4506 return OFK_Never;
4507
4508 KnownBits N0Known = computeKnownBits(N0);
4509 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4510 N0Known.getMaxValue().ult(2))
4511 return OFK_Never;
4512
4513 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4514 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4515 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4516 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4517}
4518
4521 // X - 0 never overflow
4522 if (isNullConstant(N1))
4523 return OFK_Never;
4524
4525 // If both operands each have at least two sign bits, the subtraction
4526 // cannot overflow.
4527 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4528 return OFK_Never;
4529
4530 KnownBits N0Known = computeKnownBits(N0);
4531 KnownBits N1Known = computeKnownBits(N1);
4532 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4533 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4534 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4535}
4536
4539 // X - 0 never overflow
4540 if (isNullConstant(N1))
4541 return OFK_Never;
4542
4543 KnownBits N0Known = computeKnownBits(N0);
4544 KnownBits N1Known = computeKnownBits(N1);
4545 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4546 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4547 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4548}
4549
4552 // X * 0 and X * 1 never overflow.
4553 if (isNullConstant(N1) || isOneConstant(N1))
4554 return OFK_Never;
4555
4556 KnownBits N0Known = computeKnownBits(N0);
4557 KnownBits N1Known = computeKnownBits(N1);
4558 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4559 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4560 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4561}
4562
4565 // X * 0 and X * 1 never overflow.
4566 if (isNullConstant(N1) || isOneConstant(N1))
4567 return OFK_Never;
4568
4569 // Get the size of the result.
4570 unsigned BitWidth = N0.getScalarValueSizeInBits();
4571
4572 // Sum of the sign bits.
4573 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4574
4575 // If we have enough sign bits, then there's no overflow.
4576 if (SignBits > BitWidth + 1)
4577 return OFK_Never;
4578
4579 if (SignBits == BitWidth + 1) {
4580 // The overflow occurs when the true multiplication of the
4581 // the operands is the minimum negative number.
4582 KnownBits N0Known = computeKnownBits(N0);
4583 KnownBits N1Known = computeKnownBits(N1);
4584 // If one of the operands is non-negative, then there's no
4585 // overflow.
4586 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4587 return OFK_Never;
4588 }
4589
4590 return OFK_Sometime;
4591}
4592
4594 if (Depth >= MaxRecursionDepth)
4595 return false; // Limit search depth.
4596
4597 EVT OpVT = Val.getValueType();
4598 unsigned BitWidth = OpVT.getScalarSizeInBits();
4599
4600 // Is the constant a known power of 2?
4602 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4603 }))
4604 return true;
4605
4606 // A left-shift of a constant one will have exactly one bit set because
4607 // shifting the bit off the end is undefined.
4608 if (Val.getOpcode() == ISD::SHL) {
4609 auto *C = isConstOrConstSplat(Val.getOperand(0));
4610 if (C && C->getAPIntValue() == 1)
4611 return true;
4612 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4613 isKnownNeverZero(Val, Depth);
4614 }
4615
4616 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4617 // one bit set.
4618 if (Val.getOpcode() == ISD::SRL) {
4619 auto *C = isConstOrConstSplat(Val.getOperand(0));
4620 if (C && C->getAPIntValue().isSignMask())
4621 return true;
4622 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4623 isKnownNeverZero(Val, Depth);
4624 }
4625
4626 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4627 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4628
4629 // Are all operands of a build vector constant powers of two?
4630 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4631 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4632 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4633 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4634 return false;
4635 }))
4636 return true;
4637
4638 // Is the operand of a splat vector a constant power of two?
4639 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4641 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4642 return true;
4643
4644 // vscale(power-of-two) is a power-of-two for some targets
4645 if (Val.getOpcode() == ISD::VSCALE &&
4646 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4648 return true;
4649
4650 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4651 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4652 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4654
4655 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4656 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4658
4659 // Looking for `x & -x` pattern:
4660 // If x == 0:
4661 // x & -x -> 0
4662 // If x != 0:
4663 // x & -x -> non-zero pow2
4664 // so if we find the pattern return whether we know `x` is non-zero.
4665 SDValue X;
4666 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4667 return isKnownNeverZero(X, Depth);
4668
4669 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4670 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4671
4672 // More could be done here, though the above checks are enough
4673 // to handle some common cases.
4674 return false;
4675}
4676
4678 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4679 return C1->getValueAPF().getExactLog2Abs() >= 0;
4680
4681 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4682 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4683
4684 return false;
4685}
4686
4688 EVT VT = Op.getValueType();
4689
4690 // Since the number of lanes in a scalable vector is unknown at compile time,
4691 // we track one bit which is implicitly broadcast to all lanes. This means
4692 // that all lanes in a scalable vector are considered demanded.
4693 APInt DemandedElts = VT.isFixedLengthVector()
4695 : APInt(1, 1);
4696 return ComputeNumSignBits(Op, DemandedElts, Depth);
4697}
4698
4699unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4700 unsigned Depth) const {
4701 EVT VT = Op.getValueType();
4702 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4703 unsigned VTBits = VT.getScalarSizeInBits();
4704 unsigned NumElts = DemandedElts.getBitWidth();
4705 unsigned Tmp, Tmp2;
4706 unsigned FirstAnswer = 1;
4707
4708 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4709 const APInt &Val = C->getAPIntValue();
4710 return Val.getNumSignBits();
4711 }
4712
4713 if (Depth >= MaxRecursionDepth)
4714 return 1; // Limit search depth.
4715
4716 if (!DemandedElts)
4717 return 1; // No demanded elts, better to assume we don't know anything.
4718
4719 unsigned Opcode = Op.getOpcode();
4720 switch (Opcode) {
4721 default: break;
4722 case ISD::AssertSext:
4723 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4724 return VTBits-Tmp+1;
4725 case ISD::AssertZext:
4726 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4727 return VTBits-Tmp;
4728 case ISD::FREEZE:
4729 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4730 /*PoisonOnly=*/false))
4731 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4732 break;
4733 case ISD::MERGE_VALUES:
4734 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4735 Depth + 1);
4736 case ISD::SPLAT_VECTOR: {
4737 // Check if the sign bits of source go down as far as the truncated value.
4738 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4739 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4740 if (NumSrcSignBits > (NumSrcBits - VTBits))
4741 return NumSrcSignBits - (NumSrcBits - VTBits);
4742 break;
4743 }
4744 case ISD::BUILD_VECTOR:
4745 assert(!VT.isScalableVector());
4746 Tmp = VTBits;
4747 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4748 if (!DemandedElts[i])
4749 continue;
4750
4751 SDValue SrcOp = Op.getOperand(i);
4752 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4753 // for constant nodes to ensure we only look at the sign bits.
4755 APInt T = C->getAPIntValue().trunc(VTBits);
4756 Tmp2 = T.getNumSignBits();
4757 } else {
4758 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4759
4760 if (SrcOp.getValueSizeInBits() != VTBits) {
4761 assert(SrcOp.getValueSizeInBits() > VTBits &&
4762 "Expected BUILD_VECTOR implicit truncation");
4763 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4764 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4765 }
4766 }
4767 Tmp = std::min(Tmp, Tmp2);
4768 }
4769 return Tmp;
4770
4771 case ISD::VECTOR_COMPRESS: {
4772 SDValue Vec = Op.getOperand(0);
4773 SDValue PassThru = Op.getOperand(2);
4774 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4775 if (Tmp == 1)
4776 return 1;
4777 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4778 Tmp = std::min(Tmp, Tmp2);
4779 return Tmp;
4780 }
4781
4782 case ISD::VECTOR_SHUFFLE: {
4783 // Collect the minimum number of sign bits that are shared by every vector
4784 // element referenced by the shuffle.
4785 APInt DemandedLHS, DemandedRHS;
4787 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4788 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4789 DemandedLHS, DemandedRHS))
4790 return 1;
4791
4792 Tmp = std::numeric_limits<unsigned>::max();
4793 if (!!DemandedLHS)
4794 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4795 if (!!DemandedRHS) {
4796 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4797 Tmp = std::min(Tmp, Tmp2);
4798 }
4799 // If we don't know anything, early out and try computeKnownBits fall-back.
4800 if (Tmp == 1)
4801 break;
4802 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4803 return Tmp;
4804 }
4805
4806 case ISD::BITCAST: {
4807 if (VT.isScalableVector())
4808 break;
4809 SDValue N0 = Op.getOperand(0);
4810 EVT SrcVT = N0.getValueType();
4811 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4812
4813 // Ignore bitcasts from unsupported types..
4814 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4815 break;
4816
4817 // Fast handling of 'identity' bitcasts.
4818 if (VTBits == SrcBits)
4819 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4820
4821 bool IsLE = getDataLayout().isLittleEndian();
4822
4823 // Bitcast 'large element' scalar/vector to 'small element' vector.
4824 if ((SrcBits % VTBits) == 0) {
4825 assert(VT.isVector() && "Expected bitcast to vector");
4826
4827 unsigned Scale = SrcBits / VTBits;
4828 APInt SrcDemandedElts =
4829 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4830
4831 // Fast case - sign splat can be simply split across the small elements.
4832 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4833 if (Tmp == SrcBits)
4834 return VTBits;
4835
4836 // Slow case - determine how far the sign extends into each sub-element.
4837 Tmp2 = VTBits;
4838 for (unsigned i = 0; i != NumElts; ++i)
4839 if (DemandedElts[i]) {
4840 unsigned SubOffset = i % Scale;
4841 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4842 SubOffset = SubOffset * VTBits;
4843 if (Tmp <= SubOffset)
4844 return 1;
4845 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4846 }
4847 return Tmp2;
4848 }
4849 break;
4850 }
4851
4853 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4854 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4855 return VTBits - Tmp + 1;
4856 case ISD::SIGN_EXTEND:
4857 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4858 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4860 // Max of the input and what this extends.
4861 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4862 Tmp = VTBits-Tmp+1;
4863 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4864 return std::max(Tmp, Tmp2);
4866 if (VT.isScalableVector())
4867 break;
4868 SDValue Src = Op.getOperand(0);
4869 EVT SrcVT = Src.getValueType();
4870 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4871 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4872 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4873 }
4874 case ISD::SRA:
4875 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4876 // SRA X, C -> adds C sign bits.
4877 if (std::optional<unsigned> ShAmt =
4878 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4879 Tmp = std::min(Tmp + *ShAmt, VTBits);
4880 return Tmp;
4881 case ISD::SHL:
4882 if (std::optional<ConstantRange> ShAmtRange =
4883 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4884 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4885 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4886 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4887 // shifted out, then we can compute the number of sign bits for the
4888 // operand being extended. A future improvement could be to pass along the
4889 // "shifted left by" information in the recursive calls to
4890 // ComputeKnownSignBits. Allowing us to handle this more generically.
4891 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4892 SDValue Ext = Op.getOperand(0);
4893 EVT ExtVT = Ext.getValueType();
4894 SDValue Extendee = Ext.getOperand(0);
4895 EVT ExtendeeVT = Extendee.getValueType();
4896 unsigned SizeDifference =
4897 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4898 if (SizeDifference <= MinShAmt) {
4899 Tmp = SizeDifference +
4900 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4901 if (MaxShAmt < Tmp)
4902 return Tmp - MaxShAmt;
4903 }
4904 }
4905 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4906 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4907 if (MaxShAmt < Tmp)
4908 return Tmp - MaxShAmt;
4909 }
4910 break;
4911 case ISD::AND:
4912 case ISD::OR:
4913 case ISD::XOR: // NOT is handled here.
4914 // Logical binary ops preserve the number of sign bits at the worst.
4915 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4916 if (Tmp != 1) {
4917 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4918 FirstAnswer = std::min(Tmp, Tmp2);
4919 // We computed what we know about the sign bits as our first
4920 // answer. Now proceed to the generic code that uses
4921 // computeKnownBits, and pick whichever answer is better.
4922 }
4923 break;
4924
4925 case ISD::SELECT:
4926 case ISD::VSELECT:
4927 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4928 if (Tmp == 1) return 1; // Early out.
4929 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4930 return std::min(Tmp, Tmp2);
4931 case ISD::SELECT_CC:
4932 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4933 if (Tmp == 1) return 1; // Early out.
4934 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4935 return std::min(Tmp, Tmp2);
4936
4937 case ISD::SMIN:
4938 case ISD::SMAX: {
4939 // If we have a clamp pattern, we know that the number of sign bits will be
4940 // the minimum of the clamp min/max range.
4941 bool IsMax = (Opcode == ISD::SMAX);
4942 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4943 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4944 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4945 CstHigh =
4946 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4947 if (CstLow && CstHigh) {
4948 if (!IsMax)
4949 std::swap(CstLow, CstHigh);
4950 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4951 Tmp = CstLow->getAPIntValue().getNumSignBits();
4952 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4953 return std::min(Tmp, Tmp2);
4954 }
4955 }
4956
4957 // Fallback - just get the minimum number of sign bits of the operands.
4958 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4959 if (Tmp == 1)
4960 return 1; // Early out.
4961 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4962 return std::min(Tmp, Tmp2);
4963 }
4964 case ISD::UMIN:
4965 case ISD::UMAX:
4966 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4967 if (Tmp == 1)
4968 return 1; // Early out.
4969 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4970 return std::min(Tmp, Tmp2);
4971 case ISD::SSUBO_CARRY:
4972 case ISD::USUBO_CARRY:
4973 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4974 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4975 return VTBits;
4976 [[fallthrough]];
4977 case ISD::SADDO:
4978 case ISD::UADDO:
4979 case ISD::SADDO_CARRY:
4980 case ISD::UADDO_CARRY:
4981 case ISD::SSUBO:
4982 case ISD::USUBO:
4983 case ISD::SMULO:
4984 case ISD::UMULO:
4985 if (Op.getResNo() != 1)
4986 break;
4987 // The boolean result conforms to getBooleanContents. Fall through.
4988 // If setcc returns 0/-1, all bits are sign bits.
4989 // We know that we have an integer-based boolean since these operations
4990 // are only available for integer.
4991 if (TLI->getBooleanContents(VT.isVector(), false) ==
4993 return VTBits;
4994 break;
4995 case ISD::SETCC:
4996 case ISD::SETCCCARRY:
4997 case ISD::STRICT_FSETCC:
4998 case ISD::STRICT_FSETCCS: {
4999 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5000 // If setcc returns 0/-1, all bits are sign bits.
5001 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5003 return VTBits;
5004 break;
5005 }
5006 case ISD::ROTL:
5007 case ISD::ROTR:
5008 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5009
5010 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5011 if (Tmp == VTBits)
5012 return VTBits;
5013
5014 if (ConstantSDNode *C =
5015 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5016 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5017
5018 // Handle rotate right by N like a rotate left by 32-N.
5019 if (Opcode == ISD::ROTR)
5020 RotAmt = (VTBits - RotAmt) % VTBits;
5021
5022 // If we aren't rotating out all of the known-in sign bits, return the
5023 // number that are left. This handles rotl(sext(x), 1) for example.
5024 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5025 }
5026 break;
5027 case ISD::ADD:
5028 case ISD::ADDC:
5029 // TODO: Move Operand 1 check before Operand 0 check
5030 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5031 if (Tmp == 1) return 1; // Early out.
5032
5033 // Special case decrementing a value (ADD X, -1):
5034 if (ConstantSDNode *CRHS =
5035 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5036 if (CRHS->isAllOnes()) {
5037 KnownBits Known =
5038 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5039
5040 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5041 // sign bits set.
5042 if ((Known.Zero | 1).isAllOnes())
5043 return VTBits;
5044
5045 // If we are subtracting one from a positive number, there is no carry
5046 // out of the result.
5047 if (Known.isNonNegative())
5048 return Tmp;
5049 }
5050
5051 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5052 if (Tmp2 == 1) return 1; // Early out.
5053
5054 // Add can have at most one carry bit. Thus we know that the output
5055 // is, at worst, one more bit than the inputs.
5056 return std::min(Tmp, Tmp2) - 1;
5057 case ISD::SUB:
5058 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5059 if (Tmp2 == 1) return 1; // Early out.
5060
5061 // Handle NEG.
5062 if (ConstantSDNode *CLHS =
5063 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5064 if (CLHS->isZero()) {
5065 KnownBits Known =
5066 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5067 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5068 // sign bits set.
5069 if ((Known.Zero | 1).isAllOnes())
5070 return VTBits;
5071
5072 // If the input is known to be positive (the sign bit is known clear),
5073 // the output of the NEG has the same number of sign bits as the input.
5074 if (Known.isNonNegative())
5075 return Tmp2;
5076
5077 // Otherwise, we treat this like a SUB.
5078 }
5079
5080 // Sub can have at most one carry bit. Thus we know that the output
5081 // is, at worst, one more bit than the inputs.
5082 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5083 if (Tmp == 1) return 1; // Early out.
5084 return std::min(Tmp, Tmp2) - 1;
5085 case ISD::MUL: {
5086 // The output of the Mul can be at most twice the valid bits in the inputs.
5087 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5088 if (SignBitsOp0 == 1)
5089 break;
5090 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5091 if (SignBitsOp1 == 1)
5092 break;
5093 unsigned OutValidBits =
5094 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5095 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5096 }
5097 case ISD::AVGCEILS:
5098 case ISD::AVGFLOORS:
5099 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5100 if (Tmp == 1)
5101 return 1; // Early out.
5102 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5103 return std::min(Tmp, Tmp2);
5104 case ISD::SREM:
5105 // The sign bit is the LHS's sign bit, except when the result of the
5106 // remainder is zero. The magnitude of the result should be less than or
5107 // equal to the magnitude of the LHS. Therefore, the result should have
5108 // at least as many sign bits as the left hand side.
5109 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5110 case ISD::TRUNCATE: {
5111 // Check if the sign bits of source go down as far as the truncated value.
5112 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5113 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5114 if (NumSrcSignBits > (NumSrcBits - VTBits))
5115 return NumSrcSignBits - (NumSrcBits - VTBits);
5116 break;
5117 }
5118 case ISD::EXTRACT_ELEMENT: {
5119 if (VT.isScalableVector())
5120 break;
5121 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5122 const int BitWidth = Op.getValueSizeInBits();
5123 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5124
5125 // Get reverse index (starting from 1), Op1 value indexes elements from
5126 // little end. Sign starts at big end.
5127 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5128
5129 // If the sign portion ends in our element the subtraction gives correct
5130 // result. Otherwise it gives either negative or > bitwidth result
5131 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5132 }
5134 if (VT.isScalableVector())
5135 break;
5136 // If we know the element index, split the demand between the
5137 // source vector and the inserted element, otherwise assume we need
5138 // the original demanded vector elements and the value.
5139 SDValue InVec = Op.getOperand(0);
5140 SDValue InVal = Op.getOperand(1);
5141 SDValue EltNo = Op.getOperand(2);
5142 bool DemandedVal = true;
5143 APInt DemandedVecElts = DemandedElts;
5144 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5145 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5146 unsigned EltIdx = CEltNo->getZExtValue();
5147 DemandedVal = !!DemandedElts[EltIdx];
5148 DemandedVecElts.clearBit(EltIdx);
5149 }
5150 Tmp = std::numeric_limits<unsigned>::max();
5151 if (DemandedVal) {
5152 // TODO - handle implicit truncation of inserted elements.
5153 if (InVal.getScalarValueSizeInBits() != VTBits)
5154 break;
5155 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5156 Tmp = std::min(Tmp, Tmp2);
5157 }
5158 if (!!DemandedVecElts) {
5159 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5160 Tmp = std::min(Tmp, Tmp2);
5161 }
5162 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5163 return Tmp;
5164 }
5166 assert(!VT.isScalableVector());
5167 SDValue InVec = Op.getOperand(0);
5168 SDValue EltNo = Op.getOperand(1);
5169 EVT VecVT = InVec.getValueType();
5170 // ComputeNumSignBits not yet implemented for scalable vectors.
5171 if (VecVT.isScalableVector())
5172 break;
5173 const unsigned BitWidth = Op.getValueSizeInBits();
5174 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5175 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5176
5177 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5178 // anything about sign bits. But if the sizes match we can derive knowledge
5179 // about sign bits from the vector operand.
5180 if (BitWidth != EltBitWidth)
5181 break;
5182
5183 // If we know the element index, just demand that vector element, else for
5184 // an unknown element index, ignore DemandedElts and demand them all.
5185 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5186 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5187 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5188 DemandedSrcElts =
5189 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5190
5191 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5192 }
5194 // Offset the demanded elts by the subvector index.
5195 SDValue Src = Op.getOperand(0);
5196 // Bail until we can represent demanded elements for scalable vectors.
5197 if (Src.getValueType().isScalableVector())
5198 break;
5199 uint64_t Idx = Op.getConstantOperandVal(1);
5200 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5201 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5202 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5203 }
5204 case ISD::CONCAT_VECTORS: {
5205 if (VT.isScalableVector())
5206 break;
5207 // Determine the minimum number of sign bits across all demanded
5208 // elts of the input vectors. Early out if the result is already 1.
5209 Tmp = std::numeric_limits<unsigned>::max();
5210 EVT SubVectorVT = Op.getOperand(0).getValueType();
5211 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5212 unsigned NumSubVectors = Op.getNumOperands();
5213 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5214 APInt DemandedSub =
5215 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5216 if (!DemandedSub)
5217 continue;
5218 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5219 Tmp = std::min(Tmp, Tmp2);
5220 }
5221 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5222 return Tmp;
5223 }
5224 case ISD::INSERT_SUBVECTOR: {
5225 if (VT.isScalableVector())
5226 break;
5227 // Demand any elements from the subvector and the remainder from the src its
5228 // inserted into.
5229 SDValue Src = Op.getOperand(0);
5230 SDValue Sub = Op.getOperand(1);
5231 uint64_t Idx = Op.getConstantOperandVal(2);
5232 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5233 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5234 APInt DemandedSrcElts = DemandedElts;
5235 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5236
5237 Tmp = std::numeric_limits<unsigned>::max();
5238 if (!!DemandedSubElts) {
5239 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5240 if (Tmp == 1)
5241 return 1; // early-out
5242 }
5243 if (!!DemandedSrcElts) {
5244 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5245 Tmp = std::min(Tmp, Tmp2);
5246 }
5247 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5248 return Tmp;
5249 }
5250 case ISD::LOAD: {
5251 // If we are looking at the loaded value of the SDNode.
5252 if (Op.getResNo() != 0)
5253 break;
5254
5256 if (const MDNode *Ranges = LD->getRanges()) {
5257 if (DemandedElts != 1)
5258 break;
5259
5261 if (VTBits > CR.getBitWidth()) {
5262 switch (LD->getExtensionType()) {
5263 case ISD::SEXTLOAD:
5264 CR = CR.signExtend(VTBits);
5265 break;
5266 case ISD::ZEXTLOAD:
5267 CR = CR.zeroExtend(VTBits);
5268 break;
5269 default:
5270 break;
5271 }
5272 }
5273
5274 if (VTBits != CR.getBitWidth())
5275 break;
5276 return std::min(CR.getSignedMin().getNumSignBits(),
5278 }
5279
5280 unsigned ExtType = LD->getExtensionType();
5281 switch (ExtType) {
5282 default:
5283 break;
5284 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5285 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5286 return VTBits - Tmp + 1;
5287 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5288 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5289 return VTBits - Tmp;
5290 case ISD::NON_EXTLOAD:
5291 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5292 // We only need to handle vectors - computeKnownBits should handle
5293 // scalar cases.
5294 Type *CstTy = Cst->getType();
5295 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5296 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5297 VTBits == CstTy->getScalarSizeInBits()) {
5298 Tmp = VTBits;
5299 for (unsigned i = 0; i != NumElts; ++i) {
5300 if (!DemandedElts[i])
5301 continue;
5302 if (Constant *Elt = Cst->getAggregateElement(i)) {
5303 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5304 const APInt &Value = CInt->getValue();
5305 Tmp = std::min(Tmp, Value.getNumSignBits());
5306 continue;
5307 }
5308 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5309 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5310 Tmp = std::min(Tmp, Value.getNumSignBits());
5311 continue;
5312 }
5313 }
5314 // Unknown type. Conservatively assume no bits match sign bit.
5315 return 1;
5316 }
5317 return Tmp;
5318 }
5319 }
5320 break;
5321 }
5322
5323 break;
5324 }
5327 case ISD::ATOMIC_SWAP:
5339 case ISD::ATOMIC_LOAD: {
5340 auto *AT = cast<AtomicSDNode>(Op);
5341 // If we are looking at the loaded value.
5342 if (Op.getResNo() == 0) {
5343 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5344 if (Tmp == VTBits)
5345 return 1; // early-out
5346
5347 // For atomic_load, prefer to use the extension type.
5348 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5349 switch (AT->getExtensionType()) {
5350 default:
5351 break;
5352 case ISD::SEXTLOAD:
5353 return VTBits - Tmp + 1;
5354 case ISD::ZEXTLOAD:
5355 return VTBits - Tmp;
5356 }
5357 }
5358
5359 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5360 return VTBits - Tmp + 1;
5361 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5362 return VTBits - Tmp;
5363 }
5364 break;
5365 }
5366 }
5367
5368 // Allow the target to implement this method for its nodes.
5369 if (Opcode >= ISD::BUILTIN_OP_END ||
5370 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5371 Opcode == ISD::INTRINSIC_W_CHAIN ||
5372 Opcode == ISD::INTRINSIC_VOID) {
5373 // TODO: This can probably be removed once target code is audited. This
5374 // is here purely to reduce patch size and review complexity.
5375 if (!VT.isScalableVector()) {
5376 unsigned NumBits =
5377 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5378 if (NumBits > 1)
5379 FirstAnswer = std::max(FirstAnswer, NumBits);
5380 }
5381 }
5382
5383 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5384 // use this information.
5385 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5386 return std::max(FirstAnswer, Known.countMinSignBits());
5387}
5388
5390 unsigned Depth) const {
5391 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5392 return Op.getScalarValueSizeInBits() - SignBits + 1;
5393}
5394
5396 const APInt &DemandedElts,
5397 unsigned Depth) const {
5398 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5399 return Op.getScalarValueSizeInBits() - SignBits + 1;
5400}
5401
5403 unsigned Depth) const {
5404 // Early out for FREEZE.
5405 if (Op.getOpcode() == ISD::FREEZE)
5406 return true;
5407
5408 EVT VT = Op.getValueType();
5409 APInt DemandedElts = VT.isFixedLengthVector()
5411 : APInt(1, 1);
5412 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5413}
5414
5416 const APInt &DemandedElts,
5417 bool PoisonOnly,
5418 unsigned Depth) const {
5419 unsigned Opcode = Op.getOpcode();
5420
5421 // Early out for FREEZE.
5422 if (Opcode == ISD::FREEZE)
5423 return true;
5424
5425 if (Depth >= MaxRecursionDepth)
5426 return false; // Limit search depth.
5427
5428 if (isIntOrFPConstant(Op))
5429 return true;
5430
5431 switch (Opcode) {
5432 case ISD::CONDCODE:
5433 case ISD::VALUETYPE:
5434 case ISD::FrameIndex:
5436 case ISD::CopyFromReg:
5437 return true;
5438
5439 case ISD::POISON:
5440 return false;
5441
5442 case ISD::UNDEF:
5443 return PoisonOnly;
5444
5445 case ISD::BUILD_VECTOR:
5446 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5447 // this shouldn't affect the result.
5448 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5449 if (!DemandedElts[i])
5450 continue;
5452 Depth + 1))
5453 return false;
5454 }
5455 return true;
5456
5458 SDValue Src = Op.getOperand(0);
5459 if (Src.getValueType().isScalableVector())
5460 break;
5461 uint64_t Idx = Op.getConstantOperandVal(1);
5462 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5463 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5464 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5465 Depth + 1);
5466 }
5467
5468 case ISD::INSERT_SUBVECTOR: {
5469 if (Op.getValueType().isScalableVector())
5470 break;
5471 SDValue Src = Op.getOperand(0);
5472 SDValue Sub = Op.getOperand(1);
5473 uint64_t Idx = Op.getConstantOperandVal(2);
5474 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5475 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5476 APInt DemandedSrcElts = DemandedElts;
5477 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5478
5479 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5480 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5481 return false;
5482 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5483 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5484 return false;
5485 return true;
5486 }
5487
5489 SDValue Src = Op.getOperand(0);
5490 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5491 EVT SrcVT = Src.getValueType();
5492 if (SrcVT.isFixedLengthVector() && IndexC &&
5493 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5494 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5495 IndexC->getZExtValue());
5496 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5497 Depth + 1);
5498 }
5499 break;
5500 }
5501
5503 SDValue InVec = Op.getOperand(0);
5504 SDValue InVal = Op.getOperand(1);
5505 SDValue EltNo = Op.getOperand(2);
5506 EVT VT = InVec.getValueType();
5507 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5508 if (IndexC && VT.isFixedLengthVector() &&
5509 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5510 if (DemandedElts[IndexC->getZExtValue()] &&
5512 return false;
5513 APInt InVecDemandedElts = DemandedElts;
5514 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5515 if (!!InVecDemandedElts &&
5517 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5518 InVecDemandedElts, PoisonOnly, Depth + 1))
5519 return false;
5520 return true;
5521 }
5522 break;
5523 }
5524
5526 // Check upper (known undef) elements.
5527 if (DemandedElts.ugt(1) && !PoisonOnly)
5528 return false;
5529 // Check element zero.
5530 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5531 Op.getOperand(0), PoisonOnly, Depth + 1))
5532 return false;
5533 return true;
5534
5535 case ISD::SPLAT_VECTOR:
5536 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5537 Depth + 1);
5538
5539 case ISD::VECTOR_SHUFFLE: {
5540 APInt DemandedLHS, DemandedRHS;
5541 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5542 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5543 DemandedElts, DemandedLHS, DemandedRHS,
5544 /*AllowUndefElts=*/false))
5545 return false;
5546 if (!DemandedLHS.isZero() &&
5547 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5548 PoisonOnly, Depth + 1))
5549 return false;
5550 if (!DemandedRHS.isZero() &&
5551 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5552 PoisonOnly, Depth + 1))
5553 return false;
5554 return true;
5555 }
5556
5557 case ISD::SHL:
5558 case ISD::SRL:
5559 case ISD::SRA:
5560 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5561 // enough to check operand 0 if Op can't create undef/poison.
5562 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5563 /*ConsiderFlags*/ true, Depth) &&
5564 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5565 PoisonOnly, Depth + 1);
5566
5567 case ISD::BSWAP:
5568 case ISD::CTPOP:
5569 case ISD::BITREVERSE:
5570 case ISD::AND:
5571 case ISD::OR:
5572 case ISD::XOR:
5573 case ISD::ADD:
5574 case ISD::SUB:
5575 case ISD::MUL:
5576 case ISD::SADDSAT:
5577 case ISD::UADDSAT:
5578 case ISD::SSUBSAT:
5579 case ISD::USUBSAT:
5580 case ISD::SSHLSAT:
5581 case ISD::USHLSAT:
5582 case ISD::SMIN:
5583 case ISD::SMAX:
5584 case ISD::UMIN:
5585 case ISD::UMAX:
5586 case ISD::ZERO_EXTEND:
5587 case ISD::SIGN_EXTEND:
5588 case ISD::ANY_EXTEND:
5589 case ISD::TRUNCATE:
5590 case ISD::VSELECT: {
5591 // If Op can't create undef/poison and none of its operands are undef/poison
5592 // then Op is never undef/poison. A difference from the more common check
5593 // below, outside the switch, is that we handle elementwise operations for
5594 // which the DemandedElts mask is valid for all operands here.
5595 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5596 /*ConsiderFlags*/ true, Depth) &&
5597 all_of(Op->ops(), [&](SDValue V) {
5598 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5599 PoisonOnly, Depth + 1);
5600 });
5601 }
5602
5603 // TODO: Search for noundef attributes from library functions.
5604
5605 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5606
5607 default:
5608 // Allow the target to implement this method for its nodes.
5609 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5610 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5611 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5612 Op, DemandedElts, *this, PoisonOnly, Depth);
5613 break;
5614 }
5615
5616 // If Op can't create undef/poison and none of its operands are undef/poison
5617 // then Op is never undef/poison.
5618 // NOTE: TargetNodes can handle this in themselves in
5619 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5620 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5621 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5622 Depth) &&
5623 all_of(Op->ops(), [&](SDValue V) {
5624 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5625 });
5626}
5627
5629 bool ConsiderFlags,
5630 unsigned Depth) const {
5631 EVT VT = Op.getValueType();
5632 APInt DemandedElts = VT.isFixedLengthVector()
5634 : APInt(1, 1);
5635 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5636 Depth);
5637}
5638
5640 bool PoisonOnly, bool ConsiderFlags,
5641 unsigned Depth) const {
5642 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5643 return true;
5644
5645 unsigned Opcode = Op.getOpcode();
5646 switch (Opcode) {
5647 case ISD::AssertSext:
5648 case ISD::AssertZext:
5649 case ISD::AssertAlign:
5651 // Assertion nodes can create poison if the assertion fails.
5652 return true;
5653
5654 case ISD::FREEZE:
5658 case ISD::SADDSAT:
5659 case ISD::UADDSAT:
5660 case ISD::SSUBSAT:
5661 case ISD::USUBSAT:
5662 case ISD::MULHU:
5663 case ISD::MULHS:
5664 case ISD::AVGFLOORS:
5665 case ISD::AVGFLOORU:
5666 case ISD::AVGCEILS:
5667 case ISD::AVGCEILU:
5668 case ISD::ABDU:
5669 case ISD::ABDS:
5670 case ISD::SMIN:
5671 case ISD::SMAX:
5672 case ISD::SCMP:
5673 case ISD::UMIN:
5674 case ISD::UMAX:
5675 case ISD::UCMP:
5676 case ISD::AND:
5677 case ISD::XOR:
5678 case ISD::ROTL:
5679 case ISD::ROTR:
5680 case ISD::FSHL:
5681 case ISD::FSHR:
5682 case ISD::BSWAP:
5683 case ISD::CTTZ:
5684 case ISD::CTLZ:
5685 case ISD::CTLS:
5686 case ISD::CTPOP:
5687 case ISD::BITREVERSE:
5688 case ISD::PARITY:
5689 case ISD::SIGN_EXTEND:
5690 case ISD::TRUNCATE:
5694 case ISD::BITCAST:
5695 case ISD::BUILD_VECTOR:
5696 case ISD::BUILD_PAIR:
5697 case ISD::SPLAT_VECTOR:
5698 case ISD::FABS:
5699 return false;
5700
5701 case ISD::ABS:
5702 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5703 // Different to Intrinsic::abs.
5704 return false;
5705
5706 case ISD::ADDC:
5707 case ISD::SUBC:
5708 case ISD::ADDE:
5709 case ISD::SUBE:
5710 case ISD::SADDO:
5711 case ISD::SSUBO:
5712 case ISD::SMULO:
5713 case ISD::SADDO_CARRY:
5714 case ISD::SSUBO_CARRY:
5715 case ISD::UADDO:
5716 case ISD::USUBO:
5717 case ISD::UMULO:
5718 case ISD::UADDO_CARRY:
5719 case ISD::USUBO_CARRY:
5720 // No poison on result or overflow flags.
5721 return false;
5722
5723 case ISD::SELECT_CC:
5724 case ISD::SETCC: {
5725 // Integer setcc cannot create undef or poison.
5726 if (Op.getOperand(0).getValueType().isInteger())
5727 return false;
5728
5729 // FP compares are more complicated. They can create poison for nan/infinity
5730 // based on options and flags. The options and flags also cause special
5731 // nonan condition codes to be used. Those condition codes may be preserved
5732 // even if the nonan flag is dropped somewhere.
5733 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5734 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5735 return (unsigned)CCCode & 0x10U;
5736 }
5737
5738 case ISD::OR:
5739 case ISD::ZERO_EXTEND:
5740 case ISD::SELECT:
5741 case ISD::VSELECT:
5742 case ISD::ADD:
5743 case ISD::SUB:
5744 case ISD::MUL:
5745 case ISD::FNEG:
5746 case ISD::FADD:
5747 case ISD::FSUB:
5748 case ISD::FMUL:
5749 case ISD::FDIV:
5750 case ISD::FREM:
5751 case ISD::FCOPYSIGN:
5752 case ISD::FMA:
5753 case ISD::FMAD:
5754 case ISD::FMULADD:
5755 case ISD::FP_EXTEND:
5758 // No poison except from flags (which is handled above)
5759 return false;
5760
5761 case ISD::SHL:
5762 case ISD::SRL:
5763 case ISD::SRA:
5764 // If the max shift amount isn't in range, then the shift can
5765 // create poison.
5766 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5767
5770 // If the amount is zero then the result will be poison.
5771 // TODO: Add isKnownNeverZero DemandedElts handling.
5772 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5773
5775 // Check if we demand any upper (undef) elements.
5776 return !PoisonOnly && DemandedElts.ugt(1);
5777
5780 // Ensure that the element index is in bounds.
5781 EVT VecVT = Op.getOperand(0).getValueType();
5782 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5783 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5784 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5785 }
5786
5787 case ISD::VECTOR_SHUFFLE: {
5788 // Check for any demanded shuffle element that is undef.
5789 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5790 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5791 if (Elt < 0 && DemandedElts[Idx])
5792 return true;
5793 return false;
5794 }
5795
5797 return false;
5798
5799 default:
5800 // Allow the target to implement this method for its nodes.
5801 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5802 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5803 return TLI->canCreateUndefOrPoisonForTargetNode(
5804 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5805 break;
5806 }
5807
5808 // Be conservative and return true.
5809 return true;
5810}
5811
5812bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5813 unsigned Opcode = Op.getOpcode();
5814 if (Opcode == ISD::OR)
5815 return Op->getFlags().hasDisjoint() ||
5816 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5817 if (Opcode == ISD::XOR)
5818 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5819 return false;
5820}
5821
5823 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5824 (Op.isAnyAdd() || isADDLike(Op));
5825}
5826
5828 unsigned Depth) const {
5829 EVT VT = Op.getValueType();
5830
5831 // Since the number of lanes in a scalable vector is unknown at compile time,
5832 // we track one bit which is implicitly broadcast to all lanes. This means
5833 // that all lanes in a scalable vector are considered demanded.
5834 APInt DemandedElts = VT.isFixedLengthVector()
5836 : APInt(1, 1);
5837
5838 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5839}
5840
5842 bool SNaN, unsigned Depth) const {
5843 assert(!DemandedElts.isZero() && "No demanded elements");
5844
5845 // If we're told that NaNs won't happen, assume they won't.
5846 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5847 return true;
5848
5849 if (Depth >= MaxRecursionDepth)
5850 return false; // Limit search depth.
5851
5852 // If the value is a constant, we can obviously see if it is a NaN or not.
5854 return !C->getValueAPF().isNaN() ||
5855 (SNaN && !C->getValueAPF().isSignaling());
5856 }
5857
5858 unsigned Opcode = Op.getOpcode();
5859 switch (Opcode) {
5860 case ISD::FADD:
5861 case ISD::FSUB:
5862 case ISD::FMUL:
5863 case ISD::FDIV:
5864 case ISD::FREM:
5865 case ISD::FSIN:
5866 case ISD::FCOS:
5867 case ISD::FTAN:
5868 case ISD::FASIN:
5869 case ISD::FACOS:
5870 case ISD::FATAN:
5871 case ISD::FATAN2:
5872 case ISD::FSINH:
5873 case ISD::FCOSH:
5874 case ISD::FTANH:
5875 case ISD::FMA:
5876 case ISD::FMULADD:
5877 case ISD::FMAD: {
5878 if (SNaN)
5879 return true;
5880 // TODO: Need isKnownNeverInfinity
5881 return false;
5882 }
5883 case ISD::FCANONICALIZE:
5884 case ISD::FEXP:
5885 case ISD::FEXP2:
5886 case ISD::FEXP10:
5887 case ISD::FTRUNC:
5888 case ISD::FFLOOR:
5889 case ISD::FCEIL:
5890 case ISD::FROUND:
5891 case ISD::FROUNDEVEN:
5892 case ISD::LROUND:
5893 case ISD::LLROUND:
5894 case ISD::FRINT:
5895 case ISD::LRINT:
5896 case ISD::LLRINT:
5897 case ISD::FNEARBYINT:
5898 case ISD::FLDEXP: {
5899 if (SNaN)
5900 return true;
5901 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5902 }
5903 case ISD::FABS:
5904 case ISD::FNEG:
5905 case ISD::FCOPYSIGN: {
5906 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5907 }
5908 case ISD::SELECT:
5909 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5910 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5911 case ISD::FP_EXTEND:
5912 case ISD::FP_ROUND: {
5913 if (SNaN)
5914 return true;
5915 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5916 }
5917 case ISD::SINT_TO_FP:
5918 case ISD::UINT_TO_FP:
5919 return true;
5920 case ISD::FSQRT: // Need is known positive
5921 case ISD::FLOG:
5922 case ISD::FLOG2:
5923 case ISD::FLOG10:
5924 case ISD::FPOWI:
5925 case ISD::FPOW: {
5926 if (SNaN)
5927 return true;
5928 // TODO: Refine on operand
5929 return false;
5930 }
5931 case ISD::FMINNUM:
5932 case ISD::FMAXNUM:
5933 case ISD::FMINIMUMNUM:
5934 case ISD::FMAXIMUMNUM: {
5935 // Only one needs to be known not-nan, since it will be returned if the
5936 // other ends up being one.
5937 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5938 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5939 }
5940 case ISD::FMINNUM_IEEE:
5941 case ISD::FMAXNUM_IEEE: {
5942 if (SNaN)
5943 return true;
5944 // This can return a NaN if either operand is an sNaN, or if both operands
5945 // are NaN.
5946 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5947 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5948 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5949 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5950 }
5951 case ISD::FMINIMUM:
5952 case ISD::FMAXIMUM: {
5953 // TODO: Does this quiet or return the origina NaN as-is?
5954 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5955 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5956 }
5958 SDValue Src = Op.getOperand(0);
5959 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5960 EVT SrcVT = Src.getValueType();
5961 if (SrcVT.isFixedLengthVector() && Idx &&
5962 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5963 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5964 Idx->getZExtValue());
5965 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5966 }
5967 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5968 }
5970 SDValue Src = Op.getOperand(0);
5971 if (Src.getValueType().isFixedLengthVector()) {
5972 unsigned Idx = Op.getConstantOperandVal(1);
5973 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5974 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5975 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5976 }
5977 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5978 }
5979 case ISD::INSERT_SUBVECTOR: {
5980 SDValue BaseVector = Op.getOperand(0);
5981 SDValue SubVector = Op.getOperand(1);
5982 EVT BaseVectorVT = BaseVector.getValueType();
5983 if (BaseVectorVT.isFixedLengthVector()) {
5984 unsigned Idx = Op.getConstantOperandVal(2);
5985 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
5986 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
5987
5988 // Clear/Extract the bits at the position where the subvector will be
5989 // inserted.
5990 APInt DemandedMask =
5991 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
5992 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
5993 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5994
5995 bool NeverNaN = true;
5996 if (!DemandedSrcElts.isZero())
5997 NeverNaN &=
5998 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
5999 if (NeverNaN && !DemandedSubElts.isZero())
6000 NeverNaN &=
6001 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
6002 return NeverNaN;
6003 }
6004 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
6005 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
6006 }
6007 case ISD::BUILD_VECTOR: {
6008 unsigned NumElts = Op.getNumOperands();
6009 for (unsigned I = 0; I != NumElts; ++I)
6010 if (DemandedElts[I] &&
6011 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6012 return false;
6013 return true;
6014 }
6015 case ISD::AssertNoFPClass: {
6016 FPClassTest NoFPClass =
6017 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6018 if ((NoFPClass & fcNan) == fcNan)
6019 return true;
6020 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6021 return true;
6022 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6023 }
6024 default:
6025 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6026 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6027 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6028 Depth);
6029 }
6030
6031 return false;
6032 }
6033}
6034
6036 assert(Op.getValueType().isFloatingPoint() &&
6037 "Floating point type expected");
6038
6039 // If the value is a constant, we can obviously see if it is a zero or not.
6041 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
6042}
6043
6045 if (Depth >= MaxRecursionDepth)
6046 return false; // Limit search depth.
6047
6048 assert(!Op.getValueType().isFloatingPoint() &&
6049 "Floating point types unsupported - use isKnownNeverZeroFloat");
6050
6051 // If the value is a constant, we can obviously see if it is a zero or not.
6053 [](ConstantSDNode *C) { return !C->isZero(); }))
6054 return true;
6055
6056 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6057 // some degree.
6058 switch (Op.getOpcode()) {
6059 default:
6060 break;
6061
6062 case ISD::OR:
6063 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6064 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6065
6066 case ISD::VSELECT:
6067 case ISD::SELECT:
6068 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6069 isKnownNeverZero(Op.getOperand(2), Depth + 1);
6070
6071 case ISD::SHL: {
6072 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6073 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6074 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6075 // 1 << X is never zero.
6076 if (ValKnown.One[0])
6077 return true;
6078 // If max shift cnt of known ones is non-zero, result is non-zero.
6079 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6080 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6081 !ValKnown.One.shl(MaxCnt).isZero())
6082 return true;
6083 break;
6084 }
6085 case ISD::UADDSAT:
6086 case ISD::UMAX:
6087 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6088 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6089
6090 // For smin/smax: If either operand is known negative/positive
6091 // respectively we don't need the other to be known at all.
6092 case ISD::SMAX: {
6093 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6094 if (Op1.isStrictlyPositive())
6095 return true;
6096
6097 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6098 if (Op0.isStrictlyPositive())
6099 return true;
6100
6101 if (Op1.isNonZero() && Op0.isNonZero())
6102 return true;
6103
6104 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6105 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6106 }
6107 case ISD::SMIN: {
6108 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6109 if (Op1.isNegative())
6110 return true;
6111
6112 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6113 if (Op0.isNegative())
6114 return true;
6115
6116 if (Op1.isNonZero() && Op0.isNonZero())
6117 return true;
6118
6119 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6120 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6121 }
6122 case ISD::UMIN:
6123 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6124 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6125
6126 case ISD::ROTL:
6127 case ISD::ROTR:
6128 case ISD::BITREVERSE:
6129 case ISD::BSWAP:
6130 case ISD::CTPOP:
6131 case ISD::ABS:
6132 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6133
6134 case ISD::SRA:
6135 case ISD::SRL: {
6136 if (Op->getFlags().hasExact())
6137 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6138 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6139 if (ValKnown.isNegative())
6140 return true;
6141 // If max shift cnt of known ones is non-zero, result is non-zero.
6142 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6143 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6144 !ValKnown.One.lshr(MaxCnt).isZero())
6145 return true;
6146 break;
6147 }
6148 case ISD::UDIV:
6149 case ISD::SDIV:
6150 // div exact can only produce a zero if the dividend is zero.
6151 // TODO: For udiv this is also true if Op1 u<= Op0
6152 if (Op->getFlags().hasExact())
6153 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6154 break;
6155
6156 case ISD::ADD:
6157 if (Op->getFlags().hasNoUnsignedWrap())
6158 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6159 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6160 return true;
6161 // TODO: There are a lot more cases we can prove for add.
6162 break;
6163
6164 case ISD::SUB: {
6165 if (isNullConstant(Op.getOperand(0)))
6166 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6167
6168 std::optional<bool> ne =
6169 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6170 computeKnownBits(Op.getOperand(1), Depth + 1));
6171 return ne && *ne;
6172 }
6173
6174 case ISD::MUL:
6175 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6176 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6177 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6178 return true;
6179 break;
6180
6181 case ISD::ZERO_EXTEND:
6182 case ISD::SIGN_EXTEND:
6183 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6184 case ISD::VSCALE: {
6186 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6187 ConstantRange CR =
6188 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6189 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6190 return true;
6191 break;
6192 }
6193 }
6194
6196}
6197
6199 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6200 return !C1->isNegative();
6201
6202 switch (Op.getOpcode()) {
6203 case ISD::FABS:
6204 case ISD::FEXP:
6205 case ISD::FEXP2:
6206 case ISD::FEXP10:
6207 return true;
6208 default:
6209 return false;
6210 }
6211
6212 llvm_unreachable("covered opcode switch");
6213}
6214
6216 assert(Use.getValueType().isFloatingPoint());
6217 const SDNode *User = Use.getUser();
6218 unsigned OperandNo = Use.getOperandNo();
6219 // Check if this use is insensitive to the sign of zero
6220 switch (User->getOpcode()) {
6221 case ISD::SETCC:
6222 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6223 case ISD::FABS:
6224 // fabs always produces +0.0.
6225 return true;
6226 case ISD::FCOPYSIGN:
6227 // copysign overwrites the sign bit of the first operand.
6228 return OperandNo == 0;
6229 case ISD::FADD:
6230 case ISD::FSUB: {
6231 // Arithmetic with non-zero constants fixes the uncertainty around the
6232 // sign bit.
6233 SDValue Other = User->getOperand(1 - OperandNo);
6235 }
6236 case ISD::FP_TO_SINT:
6237 case ISD::FP_TO_UINT:
6238 // fp-to-int conversions normalize signed zeros.
6239 return true;
6240 default:
6241 return false;
6242 }
6243}
6244
6246 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6247 // regression. Ideally, this should be implemented as a demanded-bits
6248 // optimization that stems from the users.
6249 if (Op->use_size() > 2)
6250 return false;
6251 return all_of(Op->uses(),
6252 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6253}
6254
6256 // Check the obvious case.
6257 if (A == B) return true;
6258
6259 // For negative and positive zero.
6262 if (CA->isZero() && CB->isZero()) return true;
6263
6264 // Otherwise they may not be equal.
6265 return false;
6266}
6267
6268// Only bits set in Mask must be negated, other bits may be arbitrary.
6270 if (isBitwiseNot(V, AllowUndefs))
6271 return V.getOperand(0);
6272
6273 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6274 // bits in the non-extended part.
6275 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6276 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6277 return SDValue();
6278 SDValue ExtArg = V.getOperand(0);
6279 if (ExtArg.getScalarValueSizeInBits() >=
6280 MaskC->getAPIntValue().getActiveBits() &&
6281 isBitwiseNot(ExtArg, AllowUndefs) &&
6282 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6283 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6284 return ExtArg.getOperand(0).getOperand(0);
6285 return SDValue();
6286}
6287
6289 // Match masked merge pattern (X & ~M) op (Y & M)
6290 // Including degenerate case (X & ~M) op M
6291 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6292 SDValue Other) {
6293 if (SDValue NotOperand =
6294 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6295 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6296 NotOperand->getOpcode() == ISD::TRUNCATE)
6297 NotOperand = NotOperand->getOperand(0);
6298
6299 if (Other == NotOperand)
6300 return true;
6301 if (Other->getOpcode() == ISD::AND)
6302 return NotOperand == Other->getOperand(0) ||
6303 NotOperand == Other->getOperand(1);
6304 }
6305 return false;
6306 };
6307
6308 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6309 A = A->getOperand(0);
6310
6311 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6312 B = B->getOperand(0);
6313
6314 if (A->getOpcode() == ISD::AND)
6315 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6316 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6317 return false;
6318}
6319
6320// FIXME: unify with llvm::haveNoCommonBitsSet.
6322 assert(A.getValueType() == B.getValueType() &&
6323 "Values must have the same type");
6326 return true;
6329}
6330
6331static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6332 SelectionDAG &DAG) {
6333 if (cast<ConstantSDNode>(Step)->isZero())
6334 return DAG.getConstant(0, DL, VT);
6335
6336 return SDValue();
6337}
6338
6341 SelectionDAG &DAG) {
6342 int NumOps = Ops.size();
6343 assert(NumOps != 0 && "Can't build an empty vector!");
6344 assert(!VT.isScalableVector() &&
6345 "BUILD_VECTOR cannot be used with scalable types");
6346 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6347 "Incorrect element count in BUILD_VECTOR!");
6348
6349 // BUILD_VECTOR of UNDEFs is UNDEF.
6350 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6351 return DAG.getUNDEF(VT);
6352
6353 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6354 SDValue IdentitySrc;
6355 bool IsIdentity = true;
6356 for (int i = 0; i != NumOps; ++i) {
6358 Ops[i].getOperand(0).getValueType() != VT ||
6359 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6360 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6361 Ops[i].getConstantOperandAPInt(1) != i) {
6362 IsIdentity = false;
6363 break;
6364 }
6365 IdentitySrc = Ops[i].getOperand(0);
6366 }
6367 if (IsIdentity)
6368 return IdentitySrc;
6369
6370 return SDValue();
6371}
6372
6373/// Try to simplify vector concatenation to an input value, undef, or build
6374/// vector.
6377 SelectionDAG &DAG) {
6378 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6380 [Ops](SDValue Op) {
6381 return Ops[0].getValueType() == Op.getValueType();
6382 }) &&
6383 "Concatenation of vectors with inconsistent value types!");
6384 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6385 VT.getVectorElementCount() &&
6386 "Incorrect element count in vector concatenation!");
6387
6388 if (Ops.size() == 1)
6389 return Ops[0];
6390
6391 // Concat of UNDEFs is UNDEF.
6392 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6393 return DAG.getUNDEF(VT);
6394
6395 // Scan the operands and look for extract operations from a single source
6396 // that correspond to insertion at the same location via this concatenation:
6397 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6398 SDValue IdentitySrc;
6399 bool IsIdentity = true;
6400 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6401 SDValue Op = Ops[i];
6402 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6403 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6404 Op.getOperand(0).getValueType() != VT ||
6405 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6406 Op.getConstantOperandVal(1) != IdentityIndex) {
6407 IsIdentity = false;
6408 break;
6409 }
6410 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6411 "Unexpected identity source vector for concat of extracts");
6412 IdentitySrc = Op.getOperand(0);
6413 }
6414 if (IsIdentity) {
6415 assert(IdentitySrc && "Failed to set source vector of extracts");
6416 return IdentitySrc;
6417 }
6418
6419 // The code below this point is only designed to work for fixed width
6420 // vectors, so we bail out for now.
6421 if (VT.isScalableVector())
6422 return SDValue();
6423
6424 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6425 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6426 // BUILD_VECTOR.
6427 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6428 EVT SVT = VT.getScalarType();
6430 for (SDValue Op : Ops) {
6431 EVT OpVT = Op.getValueType();
6432 if (Op.isUndef())
6433 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6434 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6435 Elts.append(Op->op_begin(), Op->op_end());
6436 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6437 OpVT.getVectorNumElements() == 1 &&
6438 isNullConstant(Op.getOperand(2)))
6439 Elts.push_back(Op.getOperand(1));
6440 else
6441 return SDValue();
6442 }
6443
6444 // BUILD_VECTOR requires all inputs to be of the same type, find the
6445 // maximum type and extend them all.
6446 for (SDValue Op : Elts)
6447 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6448
6449 if (SVT.bitsGT(VT.getScalarType())) {
6450 for (SDValue &Op : Elts) {
6451 if (Op.isUndef())
6452 Op = DAG.getUNDEF(SVT);
6453 else
6454 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6455 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6456 : DAG.getSExtOrTrunc(Op, DL, SVT);
6457 }
6458 }
6459
6460 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6461 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6462 return V;
6463}
6464
6465/// Gets or creates the specified node.
6466SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6467 SDVTList VTs = getVTList(VT);
6469 AddNodeIDNode(ID, Opcode, VTs, {});
6470 void *IP = nullptr;
6471 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6472 return SDValue(E, 0);
6473
6474 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6475 CSEMap.InsertNode(N, IP);
6476
6477 InsertNode(N);
6478 SDValue V = SDValue(N, 0);
6479 NewSDValueDbgMsg(V, "Creating new node: ", this);
6480 return V;
6481}
6482
6483SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6484 SDValue N1) {
6485 SDNodeFlags Flags;
6486 if (Inserter)
6487 Flags = Inserter->getFlags();
6488 return getNode(Opcode, DL, VT, N1, Flags);
6489}
6490
6491SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6492 SDValue N1, const SDNodeFlags Flags) {
6493 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6494
6495 // Constant fold unary operations with a vector integer or float operand.
6496 switch (Opcode) {
6497 default:
6498 // FIXME: Entirely reasonable to perform folding of other unary
6499 // operations here as the need arises.
6500 break;
6501 case ISD::FNEG:
6502 case ISD::FABS:
6503 case ISD::FCEIL:
6504 case ISD::FTRUNC:
6505 case ISD::FFLOOR:
6506 case ISD::FP_EXTEND:
6507 case ISD::FP_TO_SINT:
6508 case ISD::FP_TO_UINT:
6509 case ISD::FP_TO_FP16:
6510 case ISD::FP_TO_BF16:
6511 case ISD::TRUNCATE:
6512 case ISD::ANY_EXTEND:
6513 case ISD::ZERO_EXTEND:
6514 case ISD::SIGN_EXTEND:
6515 case ISD::UINT_TO_FP:
6516 case ISD::SINT_TO_FP:
6517 case ISD::FP16_TO_FP:
6518 case ISD::BF16_TO_FP:
6519 case ISD::BITCAST:
6520 case ISD::ABS:
6521 case ISD::BITREVERSE:
6522 case ISD::BSWAP:
6523 case ISD::CTLZ:
6525 case ISD::CTTZ:
6527 case ISD::CTPOP:
6528 case ISD::STEP_VECTOR: {
6529 SDValue Ops = {N1};
6530 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6531 return Fold;
6532 }
6533 }
6534
6535 unsigned OpOpcode = N1.getNode()->getOpcode();
6536 switch (Opcode) {
6537 case ISD::STEP_VECTOR:
6538 assert(VT.isScalableVector() &&
6539 "STEP_VECTOR can only be used with scalable types");
6540 assert(OpOpcode == ISD::TargetConstant &&
6541 VT.getVectorElementType() == N1.getValueType() &&
6542 "Unexpected step operand");
6543 break;
6544 case ISD::FREEZE:
6545 assert(VT == N1.getValueType() && "Unexpected VT!");
6546 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6547 return N1;
6548 break;
6549 case ISD::TokenFactor:
6550 case ISD::MERGE_VALUES:
6552 return N1; // Factor, merge or concat of one node? No need.
6553 case ISD::BUILD_VECTOR: {
6554 // Attempt to simplify BUILD_VECTOR.
6555 SDValue Ops[] = {N1};
6556 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6557 return V;
6558 break;
6559 }
6560 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6561 case ISD::FP_EXTEND:
6563 "Invalid FP cast!");
6564 if (N1.getValueType() == VT) return N1; // noop conversion.
6565 assert((!VT.isVector() || VT.getVectorElementCount() ==
6567 "Vector element count mismatch!");
6568 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6569 if (N1.isUndef())
6570 return getUNDEF(VT);
6571 break;
6572 case ISD::FP_TO_SINT:
6573 case ISD::FP_TO_UINT:
6574 if (N1.isUndef())
6575 return getUNDEF(VT);
6576 break;
6577 case ISD::SINT_TO_FP:
6578 case ISD::UINT_TO_FP:
6579 // [us]itofp(undef) = 0, because the result value is bounded.
6580 if (N1.isUndef())
6581 return getConstantFP(0.0, DL, VT);
6582 break;
6583 case ISD::SIGN_EXTEND:
6584 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6585 "Invalid SIGN_EXTEND!");
6586 assert(VT.isVector() == N1.getValueType().isVector() &&
6587 "SIGN_EXTEND result type type should be vector iff the operand "
6588 "type is vector!");
6589 if (N1.getValueType() == VT) return N1; // noop extension
6590 assert((!VT.isVector() || VT.getVectorElementCount() ==
6592 "Vector element count mismatch!");
6593 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6594 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6595 SDNodeFlags Flags;
6596 if (OpOpcode == ISD::ZERO_EXTEND)
6597 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6598 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6599 transferDbgValues(N1, NewVal);
6600 return NewVal;
6601 }
6602
6603 if (OpOpcode == ISD::POISON)
6604 return getPOISON(VT);
6605
6606 if (N1.isUndef())
6607 // sext(undef) = 0, because the top bits will all be the same.
6608 return getConstant(0, DL, VT);
6609
6610 // Skip unnecessary sext_inreg pattern:
6611 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6612 if (OpOpcode == ISD::TRUNCATE) {
6613 SDValue OpOp = N1.getOperand(0);
6614 if (OpOp.getValueType() == VT) {
6615 unsigned NumSignExtBits =
6617 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6618 transferDbgValues(N1, OpOp);
6619 return OpOp;
6620 }
6621 }
6622 }
6623 break;
6624 case ISD::ZERO_EXTEND:
6625 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6626 "Invalid ZERO_EXTEND!");
6627 assert(VT.isVector() == N1.getValueType().isVector() &&
6628 "ZERO_EXTEND result type type should be vector iff the operand "
6629 "type is vector!");
6630 if (N1.getValueType() == VT) return N1; // noop extension
6631 assert((!VT.isVector() || VT.getVectorElementCount() ==
6633 "Vector element count mismatch!");
6634 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6635 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6636 SDNodeFlags Flags;
6637 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6638 SDValue NewVal =
6639 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6640 transferDbgValues(N1, NewVal);
6641 return NewVal;
6642 }
6643
6644 if (OpOpcode == ISD::POISON)
6645 return getPOISON(VT);
6646
6647 if (N1.isUndef())
6648 // zext(undef) = 0, because the top bits will be zero.
6649 return getConstant(0, DL, VT);
6650
6651 // Skip unnecessary zext_inreg pattern:
6652 // (zext (trunc x)) -> x iff the upper bits are known zero.
6653 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6654 // use to recognise zext_inreg patterns.
6655 if (OpOpcode == ISD::TRUNCATE) {
6656 SDValue OpOp = N1.getOperand(0);
6657 if (OpOp.getValueType() == VT) {
6658 if (OpOp.getOpcode() != ISD::AND) {
6661 if (MaskedValueIsZero(OpOp, HiBits)) {
6662 transferDbgValues(N1, OpOp);
6663 return OpOp;
6664 }
6665 }
6666 }
6667 }
6668 break;
6669 case ISD::ANY_EXTEND:
6670 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6671 "Invalid ANY_EXTEND!");
6672 assert(VT.isVector() == N1.getValueType().isVector() &&
6673 "ANY_EXTEND result type type should be vector iff the operand "
6674 "type is vector!");
6675 if (N1.getValueType() == VT) return N1; // noop extension
6676 assert((!VT.isVector() || VT.getVectorElementCount() ==
6678 "Vector element count mismatch!");
6679 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6680
6681 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6682 OpOpcode == ISD::ANY_EXTEND) {
6683 SDNodeFlags Flags;
6684 if (OpOpcode == ISD::ZERO_EXTEND)
6685 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6686 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6687 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6688 }
6689 if (N1.isUndef())
6690 return getUNDEF(VT);
6691
6692 // (ext (trunc x)) -> x
6693 if (OpOpcode == ISD::TRUNCATE) {
6694 SDValue OpOp = N1.getOperand(0);
6695 if (OpOp.getValueType() == VT) {
6696 transferDbgValues(N1, OpOp);
6697 return OpOp;
6698 }
6699 }
6700 break;
6701 case ISD::TRUNCATE:
6702 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6703 "Invalid TRUNCATE!");
6704 assert(VT.isVector() == N1.getValueType().isVector() &&
6705 "TRUNCATE result type type should be vector iff the operand "
6706 "type is vector!");
6707 if (N1.getValueType() == VT) return N1; // noop truncate
6708 assert((!VT.isVector() || VT.getVectorElementCount() ==
6710 "Vector element count mismatch!");
6711 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6712 if (OpOpcode == ISD::TRUNCATE)
6713 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6714 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6715 OpOpcode == ISD::ANY_EXTEND) {
6716 // If the source is smaller than the dest, we still need an extend.
6718 VT.getScalarType())) {
6719 SDNodeFlags Flags;
6720 if (OpOpcode == ISD::ZERO_EXTEND)
6721 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6722 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6723 }
6724 if (N1.getOperand(0).getValueType().bitsGT(VT))
6725 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6726 return N1.getOperand(0);
6727 }
6728 if (N1.isUndef())
6729 return getUNDEF(VT);
6730 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6731 return getVScale(DL, VT,
6733 break;
6737 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6738 assert(N1.getValueType().bitsLE(VT) &&
6739 "The input must be the same size or smaller than the result.");
6742 "The destination vector type must have fewer lanes than the input.");
6743 break;
6744 case ISD::ABS:
6745 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6746 if (N1.isUndef())
6747 return getConstant(0, DL, VT);
6748 break;
6749 case ISD::BSWAP:
6750 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6751 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6752 "BSWAP types must be a multiple of 16 bits!");
6753 if (N1.isUndef())
6754 return getUNDEF(VT);
6755 // bswap(bswap(X)) -> X.
6756 if (OpOpcode == ISD::BSWAP)
6757 return N1.getOperand(0);
6758 break;
6759 case ISD::BITREVERSE:
6760 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6761 if (N1.isUndef())
6762 return getUNDEF(VT);
6763 break;
6764 case ISD::BITCAST:
6766 "Cannot BITCAST between types of different sizes!");
6767 if (VT == N1.getValueType()) return N1; // noop conversion.
6768 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6769 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6770 if (N1.isUndef())
6771 return getUNDEF(VT);
6772 break;
6774 assert(VT.isVector() && !N1.getValueType().isVector() &&
6775 (VT.getVectorElementType() == N1.getValueType() ||
6777 N1.getValueType().isInteger() &&
6779 "Illegal SCALAR_TO_VECTOR node!");
6780 if (N1.isUndef())
6781 return getUNDEF(VT);
6782 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6783 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6785 N1.getConstantOperandVal(1) == 0 &&
6786 N1.getOperand(0).getValueType() == VT)
6787 return N1.getOperand(0);
6788 break;
6789 case ISD::FNEG:
6790 // Negation of an unknown bag of bits is still completely undefined.
6791 if (N1.isUndef())
6792 return getUNDEF(VT);
6793
6794 if (OpOpcode == ISD::FNEG) // --X -> X
6795 return N1.getOperand(0);
6796 break;
6797 case ISD::FABS:
6798 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6799 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6800 break;
6801 case ISD::VSCALE:
6802 assert(VT == N1.getValueType() && "Unexpected VT!");
6803 break;
6804 case ISD::CTPOP:
6805 if (N1.getValueType().getScalarType() == MVT::i1)
6806 return N1;
6807 break;
6808 case ISD::CTLZ:
6809 case ISD::CTTZ:
6810 if (N1.getValueType().getScalarType() == MVT::i1)
6811 return getNOT(DL, N1, N1.getValueType());
6812 break;
6813 case ISD::VECREDUCE_ADD:
6814 if (N1.getValueType().getScalarType() == MVT::i1)
6815 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6816 break;
6819 if (N1.getValueType().getScalarType() == MVT::i1)
6820 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6821 break;
6824 if (N1.getValueType().getScalarType() == MVT::i1)
6825 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6826 break;
6827 case ISD::SPLAT_VECTOR:
6828 assert(VT.isVector() && "Wrong return type!");
6829 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6830 // that for now.
6832 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6834 N1.getValueType().isInteger() &&
6836 "Wrong operand type!");
6837 break;
6838 }
6839
6840 SDNode *N;
6841 SDVTList VTs = getVTList(VT);
6842 SDValue Ops[] = {N1};
6843 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6845 AddNodeIDNode(ID, Opcode, VTs, Ops);
6846 void *IP = nullptr;
6847 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6848 E->intersectFlagsWith(Flags);
6849 return SDValue(E, 0);
6850 }
6851
6852 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6853 N->setFlags(Flags);
6854 createOperands(N, Ops);
6855 CSEMap.InsertNode(N, IP);
6856 } else {
6857 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6858 createOperands(N, Ops);
6859 }
6860
6861 InsertNode(N);
6862 SDValue V = SDValue(N, 0);
6863 NewSDValueDbgMsg(V, "Creating new node: ", this);
6864 return V;
6865}
6866
6867static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6868 const APInt &C2) {
6869 switch (Opcode) {
6870 case ISD::ADD: return C1 + C2;
6871 case ISD::SUB: return C1 - C2;
6872 case ISD::MUL: return C1 * C2;
6873 case ISD::AND: return C1 & C2;
6874 case ISD::OR: return C1 | C2;
6875 case ISD::XOR: return C1 ^ C2;
6876 case ISD::SHL: return C1 << C2;
6877 case ISD::SRL: return C1.lshr(C2);
6878 case ISD::SRA: return C1.ashr(C2);
6879 case ISD::ROTL: return C1.rotl(C2);
6880 case ISD::ROTR: return C1.rotr(C2);
6881 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6882 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6883 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6884 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6885 case ISD::SADDSAT: return C1.sadd_sat(C2);
6886 case ISD::UADDSAT: return C1.uadd_sat(C2);
6887 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6888 case ISD::USUBSAT: return C1.usub_sat(C2);
6889 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6890 case ISD::USHLSAT: return C1.ushl_sat(C2);
6891 case ISD::UDIV:
6892 if (!C2.getBoolValue())
6893 break;
6894 return C1.udiv(C2);
6895 case ISD::UREM:
6896 if (!C2.getBoolValue())
6897 break;
6898 return C1.urem(C2);
6899 case ISD::SDIV:
6900 if (!C2.getBoolValue())
6901 break;
6902 return C1.sdiv(C2);
6903 case ISD::SREM:
6904 if (!C2.getBoolValue())
6905 break;
6906 return C1.srem(C2);
6907 case ISD::AVGFLOORS:
6908 return APIntOps::avgFloorS(C1, C2);
6909 case ISD::AVGFLOORU:
6910 return APIntOps::avgFloorU(C1, C2);
6911 case ISD::AVGCEILS:
6912 return APIntOps::avgCeilS(C1, C2);
6913 case ISD::AVGCEILU:
6914 return APIntOps::avgCeilU(C1, C2);
6915 case ISD::ABDS:
6916 return APIntOps::abds(C1, C2);
6917 case ISD::ABDU:
6918 return APIntOps::abdu(C1, C2);
6919 case ISD::MULHS:
6920 return APIntOps::mulhs(C1, C2);
6921 case ISD::MULHU:
6922 return APIntOps::mulhu(C1, C2);
6923 case ISD::CLMUL:
6924 return APIntOps::clmul(C1, C2);
6925 case ISD::CLMULR:
6926 return APIntOps::clmulr(C1, C2);
6927 case ISD::CLMULH:
6928 return APIntOps::clmulh(C1, C2);
6929 }
6930 return std::nullopt;
6931}
6932// Handle constant folding with UNDEF.
6933// TODO: Handle more cases.
6934static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6935 bool IsUndef1, const APInt &C2,
6936 bool IsUndef2) {
6937 if (!(IsUndef1 || IsUndef2))
6938 return FoldValue(Opcode, C1, C2);
6939
6940 // Fold and(x, undef) -> 0
6941 // Fold mul(x, undef) -> 0
6942 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6943 return APInt::getZero(C1.getBitWidth());
6944
6945 return std::nullopt;
6946}
6947
6949 const GlobalAddressSDNode *GA,
6950 const SDNode *N2) {
6951 if (GA->getOpcode() != ISD::GlobalAddress)
6952 return SDValue();
6953 if (!TLI->isOffsetFoldingLegal(GA))
6954 return SDValue();
6955 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6956 if (!C2)
6957 return SDValue();
6958 int64_t Offset = C2->getSExtValue();
6959 switch (Opcode) {
6960 case ISD::ADD:
6961 case ISD::PTRADD:
6962 break;
6963 case ISD::SUB: Offset = -uint64_t(Offset); break;
6964 default: return SDValue();
6965 }
6966 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6967 GA->getOffset() + uint64_t(Offset));
6968}
6969
6971 switch (Opcode) {
6972 case ISD::SDIV:
6973 case ISD::UDIV:
6974 case ISD::SREM:
6975 case ISD::UREM: {
6976 // If a divisor is zero/undef or any element of a divisor vector is
6977 // zero/undef, the whole op is undef.
6978 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6979 SDValue Divisor = Ops[1];
6980 if (Divisor.isUndef() || isNullConstant(Divisor))
6981 return true;
6982
6983 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6984 llvm::any_of(Divisor->op_values(),
6985 [](SDValue V) { return V.isUndef() ||
6986 isNullConstant(V); });
6987 // TODO: Handle signed overflow.
6988 }
6989 // TODO: Handle oversized shifts.
6990 default:
6991 return false;
6992 }
6993}
6994
6997 SDNodeFlags Flags) {
6998 // If the opcode is a target-specific ISD node, there's nothing we can
6999 // do here and the operand rules may not line up with the below, so
7000 // bail early.
7001 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
7002 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
7003 // foldCONCAT_VECTORS in getNode before this is called.
7004 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
7005 return SDValue();
7006
7007 unsigned NumOps = Ops.size();
7008 if (NumOps == 0)
7009 return SDValue();
7010
7011 if (isUndef(Opcode, Ops))
7012 return getUNDEF(VT);
7013
7014 // Handle unary special cases.
7015 if (NumOps == 1) {
7016 SDValue N1 = Ops[0];
7017
7018 // Constant fold unary operations with an integer constant operand. Even
7019 // opaque constant will be folded, because the folding of unary operations
7020 // doesn't create new constants with different values. Nevertheless, the
7021 // opaque flag is preserved during folding to prevent future folding with
7022 // other constants.
7023 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
7024 const APInt &Val = C->getAPIntValue();
7025 switch (Opcode) {
7026 case ISD::SIGN_EXTEND:
7027 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7028 C->isTargetOpcode(), C->isOpaque());
7029 case ISD::TRUNCATE:
7030 if (C->isOpaque())
7031 break;
7032 [[fallthrough]];
7033 case ISD::ZERO_EXTEND:
7034 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7035 C->isTargetOpcode(), C->isOpaque());
7036 case ISD::ANY_EXTEND:
7037 // Some targets like RISCV prefer to sign extend some types.
7038 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7039 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7040 C->isTargetOpcode(), C->isOpaque());
7041 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7042 C->isTargetOpcode(), C->isOpaque());
7043 case ISD::ABS:
7044 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7045 C->isOpaque());
7046 case ISD::BITREVERSE:
7047 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7048 C->isOpaque());
7049 case ISD::BSWAP:
7050 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7051 C->isOpaque());
7052 case ISD::CTPOP:
7053 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7054 C->isOpaque());
7055 case ISD::CTLZ:
7057 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7058 C->isOpaque());
7059 case ISD::CTTZ:
7061 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7062 C->isOpaque());
7063 case ISD::UINT_TO_FP:
7064 case ISD::SINT_TO_FP: {
7066 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7068 return getConstantFP(FPV, DL, VT);
7069 }
7070 case ISD::FP16_TO_FP:
7071 case ISD::BF16_TO_FP: {
7072 bool Ignored;
7073 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7074 : APFloat::BFloat(),
7075 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7076
7077 // This can return overflow, underflow, or inexact; we don't care.
7078 // FIXME need to be more flexible about rounding mode.
7080 &Ignored);
7081 return getConstantFP(FPV, DL, VT);
7082 }
7083 case ISD::STEP_VECTOR:
7084 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7085 return V;
7086 break;
7087 case ISD::BITCAST:
7088 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7089 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7090 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7091 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7092 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7093 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7094 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7095 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7096 break;
7097 }
7098 }
7099
7100 // Constant fold unary operations with a floating point constant operand.
7101 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7102 APFloat V = C->getValueAPF(); // make copy
7103 switch (Opcode) {
7104 case ISD::FNEG:
7105 V.changeSign();
7106 return getConstantFP(V, DL, VT);
7107 case ISD::FABS:
7108 V.clearSign();
7109 return getConstantFP(V, DL, VT);
7110 case ISD::FCEIL: {
7111 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7113 return getConstantFP(V, DL, VT);
7114 return SDValue();
7115 }
7116 case ISD::FTRUNC: {
7117 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7119 return getConstantFP(V, DL, VT);
7120 return SDValue();
7121 }
7122 case ISD::FFLOOR: {
7123 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7125 return getConstantFP(V, DL, VT);
7126 return SDValue();
7127 }
7128 case ISD::FP_EXTEND: {
7129 bool ignored;
7130 // This can return overflow, underflow, or inexact; we don't care.
7131 // FIXME need to be more flexible about rounding mode.
7132 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7133 &ignored);
7134 return getConstantFP(V, DL, VT);
7135 }
7136 case ISD::FP_TO_SINT:
7137 case ISD::FP_TO_UINT: {
7138 bool ignored;
7139 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7140 // FIXME need to be more flexible about rounding mode.
7142 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7143 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7144 break;
7145 return getConstant(IntVal, DL, VT);
7146 }
7147 case ISD::FP_TO_FP16:
7148 case ISD::FP_TO_BF16: {
7149 bool Ignored;
7150 // This can return overflow, underflow, or inexact; we don't care.
7151 // FIXME need to be more flexible about rounding mode.
7152 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7153 : APFloat::BFloat(),
7155 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7156 }
7157 case ISD::BITCAST:
7158 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7159 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7160 VT);
7161 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7162 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7163 VT);
7164 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7165 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7166 VT);
7167 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7168 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7169 break;
7170 }
7171 }
7172
7173 // Early-out if we failed to constant fold a bitcast.
7174 if (Opcode == ISD::BITCAST)
7175 return SDValue();
7176 }
7177
7178 // Handle binops special cases.
7179 if (NumOps == 2) {
7180 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7181 return CFP;
7182
7183 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7184 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7185 if (C1->isOpaque() || C2->isOpaque())
7186 return SDValue();
7187
7188 std::optional<APInt> FoldAttempt =
7189 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7190 if (!FoldAttempt)
7191 return SDValue();
7192
7193 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7194 assert((!Folded || !VT.isVector()) &&
7195 "Can't fold vectors ops with scalar operands");
7196 return Folded;
7197 }
7198 }
7199
7200 // fold (add Sym, c) -> Sym+c
7202 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7203 if (TLI->isCommutativeBinOp(Opcode))
7205 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7206
7207 // fold (sext_in_reg c1) -> c2
7208 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7209 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7210
7211 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7212 unsigned FromBits = EVT.getScalarSizeInBits();
7213 Val <<= Val.getBitWidth() - FromBits;
7214 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7215 return getConstant(Val, DL, ConstantVT);
7216 };
7217
7218 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7219 const APInt &Val = C1->getAPIntValue();
7220 return SignExtendInReg(Val, VT);
7221 }
7222
7224 SmallVector<SDValue, 8> ScalarOps;
7225 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7226 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7227 SDValue Op = Ops[0].getOperand(I);
7228 if (Op.isUndef()) {
7229 ScalarOps.push_back(getUNDEF(OpVT));
7230 continue;
7231 }
7232 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7233 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7234 }
7235 return getBuildVector(VT, DL, ScalarOps);
7236 }
7237
7238 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7239 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7240 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7241 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7242 Ops[0].getOperand(0).getValueType()));
7243 }
7244 }
7245
7246 // Handle fshl/fshr special cases.
7247 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7248 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7249 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7250 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7251
7252 if (C1 && C2 && C3) {
7253 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7254 return SDValue();
7255 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7256 &V3 = C3->getAPIntValue();
7257
7258 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7259 : APIntOps::fshr(V1, V2, V3);
7260 return getConstant(FoldedVal, DL, VT);
7261 }
7262 }
7263
7264 // Handle fma/fmad special cases.
7265 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7266 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7267 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7268 Ops[2].getValueType() == VT && "FMA types must match!");
7272 if (C1 && C2 && C3) {
7273 APFloat V1 = C1->getValueAPF();
7274 const APFloat &V2 = C2->getValueAPF();
7275 const APFloat &V3 = C3->getValueAPF();
7276 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7279 } else
7281 return getConstantFP(V1, DL, VT);
7282 }
7283 }
7284
7285 // This is for vector folding only from here on.
7286 if (!VT.isVector())
7287 return SDValue();
7288
7289 ElementCount NumElts = VT.getVectorElementCount();
7290
7291 // See if we can fold through any bitcasted integer ops.
7292 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7293 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7294 (Ops[0].getOpcode() == ISD::BITCAST ||
7295 Ops[1].getOpcode() == ISD::BITCAST)) {
7298 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7299 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7300 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7301 N2.getValueType().isInteger()) {
7302 bool IsLE = getDataLayout().isLittleEndian();
7303 unsigned EltBits = VT.getScalarSizeInBits();
7304 SmallVector<APInt> RawBits1, RawBits2;
7305 BitVector UndefElts1, UndefElts2;
7306 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7307 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7308 SmallVector<APInt> RawBits;
7309 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7310 std::optional<APInt> Fold = FoldValueWithUndef(
7311 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7312 if (!Fold)
7313 break;
7314 RawBits.push_back(*Fold);
7315 }
7316 if (RawBits.size() == NumElts.getFixedValue()) {
7317 // We have constant folded, but we might need to cast this again back
7318 // to the original (possibly legalized) type.
7319 EVT BVVT, BVEltVT;
7320 if (N1.getValueType() == VT) {
7321 BVVT = N1.getValueType();
7322 BVEltVT = BV1->getOperand(0).getValueType();
7323 } else {
7324 BVVT = N2.getValueType();
7325 BVEltVT = BV2->getOperand(0).getValueType();
7326 }
7327 unsigned BVEltBits = BVEltVT.getSizeInBits();
7328 SmallVector<APInt> DstBits;
7329 BitVector DstUndefs;
7331 DstBits, RawBits, DstUndefs,
7332 BitVector(RawBits.size(), false));
7333 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7334 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7335 if (DstUndefs[I])
7336 continue;
7337 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7338 }
7339 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7340 }
7341 }
7342 }
7343 }
7344
7345 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7346 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7347 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7348 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7349 APInt RHSVal;
7350 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7351 APInt NewStep = Opcode == ISD::MUL
7352 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7353 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7354 return getStepVector(DL, VT, NewStep);
7355 }
7356 }
7357
7358 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7359 return !Op.getValueType().isVector() ||
7360 Op.getValueType().getVectorElementCount() == NumElts;
7361 };
7362
7363 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7364 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7365 Op.getOpcode() == ISD::BUILD_VECTOR ||
7366 Op.getOpcode() == ISD::SPLAT_VECTOR;
7367 };
7368
7369 // All operands must be vector types with the same number of elements as
7370 // the result type and must be either UNDEF or a build/splat vector
7371 // or UNDEF scalars.
7372 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7373 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7374 return SDValue();
7375
7376 // If we are comparing vectors, then the result needs to be a i1 boolean that
7377 // is then extended back to the legal result type depending on how booleans
7378 // are represented.
7379 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7380 ISD::NodeType ExtendCode =
7381 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7382 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7384
7385 // Find legal integer scalar type for constant promotion and
7386 // ensure that its scalar size is at least as large as source.
7387 EVT LegalSVT = VT.getScalarType();
7388 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7389 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7390 if (LegalSVT.bitsLT(VT.getScalarType()))
7391 return SDValue();
7392 }
7393
7394 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7395 // only have one operand to check. For fixed-length vector types we may have
7396 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7397 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7398
7399 // Constant fold each scalar lane separately.
7400 SmallVector<SDValue, 4> ScalarResults;
7401 for (unsigned I = 0; I != NumVectorElts; I++) {
7402 SmallVector<SDValue, 4> ScalarOps;
7403 for (SDValue Op : Ops) {
7404 EVT InSVT = Op.getValueType().getScalarType();
7405 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7406 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7407 if (Op.isUndef())
7408 ScalarOps.push_back(getUNDEF(InSVT));
7409 else
7410 ScalarOps.push_back(Op);
7411 continue;
7412 }
7413
7414 SDValue ScalarOp =
7415 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7416 EVT ScalarVT = ScalarOp.getValueType();
7417
7418 // Build vector (integer) scalar operands may need implicit
7419 // truncation - do this before constant folding.
7420 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7421 // Don't create illegally-typed nodes unless they're constants or undef
7422 // - if we fail to constant fold we can't guarantee the (dead) nodes
7423 // we're creating will be cleaned up before being visited for
7424 // legalization.
7425 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7426 !isa<ConstantSDNode>(ScalarOp) &&
7427 TLI->getTypeAction(*getContext(), InSVT) !=
7429 return SDValue();
7430 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7431 }
7432
7433 ScalarOps.push_back(ScalarOp);
7434 }
7435
7436 // Constant fold the scalar operands.
7437 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7438
7439 // Scalar folding only succeeded if the result is a constant or UNDEF.
7440 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7441 ScalarResult.getOpcode() != ISD::ConstantFP)
7442 return SDValue();
7443
7444 // Legalize the (integer) scalar constant if necessary. We only do
7445 // this once we know the folding succeeded, since otherwise we would
7446 // get a node with illegal type which has a user.
7447 if (LegalSVT != SVT)
7448 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7449
7450 ScalarResults.push_back(ScalarResult);
7451 }
7452
7453 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7454 : getBuildVector(VT, DL, ScalarResults);
7455 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7456 return V;
7457}
7458
7461 // TODO: Add support for unary/ternary fp opcodes.
7462 if (Ops.size() != 2)
7463 return SDValue();
7464
7465 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7466 // should. That will require dealing with a potentially non-default
7467 // rounding mode, checking the "opStatus" return value from the APFloat
7468 // math calculations, and possibly other variations.
7469 SDValue N1 = Ops[0];
7470 SDValue N2 = Ops[1];
7471 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7472 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7473 if (N1CFP && N2CFP) {
7474 APFloat C1 = N1CFP->getValueAPF(); // make copy
7475 const APFloat &C2 = N2CFP->getValueAPF();
7476 switch (Opcode) {
7477 case ISD::FADD:
7479 return getConstantFP(C1, DL, VT);
7480 case ISD::FSUB:
7482 return getConstantFP(C1, DL, VT);
7483 case ISD::FMUL:
7485 return getConstantFP(C1, DL, VT);
7486 case ISD::FDIV:
7488 return getConstantFP(C1, DL, VT);
7489 case ISD::FREM:
7490 C1.mod(C2);
7491 return getConstantFP(C1, DL, VT);
7492 case ISD::FCOPYSIGN:
7493 C1.copySign(C2);
7494 return getConstantFP(C1, DL, VT);
7495 case ISD::FMINNUM:
7496 if (C1.isSignaling() || C2.isSignaling())
7497 return SDValue();
7498 return getConstantFP(minnum(C1, C2), DL, VT);
7499 case ISD::FMAXNUM:
7500 if (C1.isSignaling() || C2.isSignaling())
7501 return SDValue();
7502 return getConstantFP(maxnum(C1, C2), DL, VT);
7503 case ISD::FMINIMUM:
7504 return getConstantFP(minimum(C1, C2), DL, VT);
7505 case ISD::FMAXIMUM:
7506 return getConstantFP(maximum(C1, C2), DL, VT);
7507 case ISD::FMINIMUMNUM:
7508 return getConstantFP(minimumnum(C1, C2), DL, VT);
7509 case ISD::FMAXIMUMNUM:
7510 return getConstantFP(maximumnum(C1, C2), DL, VT);
7511 default: break;
7512 }
7513 }
7514 if (N1CFP && Opcode == ISD::FP_ROUND) {
7515 APFloat C1 = N1CFP->getValueAPF(); // make copy
7516 bool Unused;
7517 // This can return overflow, underflow, or inexact; we don't care.
7518 // FIXME need to be more flexible about rounding mode.
7520 &Unused);
7521 return getConstantFP(C1, DL, VT);
7522 }
7523
7524 switch (Opcode) {
7525 case ISD::FSUB:
7526 // -0.0 - undef --> undef (consistent with "fneg undef")
7527 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7528 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7529 return getUNDEF(VT);
7530 [[fallthrough]];
7531
7532 case ISD::FADD:
7533 case ISD::FMUL:
7534 case ISD::FDIV:
7535 case ISD::FREM:
7536 // If both operands are undef, the result is undef. If 1 operand is undef,
7537 // the result is NaN. This should match the behavior of the IR optimizer.
7538 if (N1.isUndef() && N2.isUndef())
7539 return getUNDEF(VT);
7540 if (N1.isUndef() || N2.isUndef())
7542 }
7543 return SDValue();
7544}
7545
7547 const SDLoc &DL, EVT DstEltVT) {
7548 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7549
7550 // If this is already the right type, we're done.
7551 if (SrcEltVT == DstEltVT)
7552 return SDValue(BV, 0);
7553
7554 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7555 unsigned DstBitSize = DstEltVT.getSizeInBits();
7556
7557 // If this is a conversion of N elements of one type to N elements of another
7558 // type, convert each element. This handles FP<->INT cases.
7559 if (SrcBitSize == DstBitSize) {
7561 for (SDValue Op : BV->op_values()) {
7562 // If the vector element type is not legal, the BUILD_VECTOR operands
7563 // are promoted and implicitly truncated. Make that explicit here.
7564 if (Op.getValueType() != SrcEltVT)
7565 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7566 Ops.push_back(getBitcast(DstEltVT, Op));
7567 }
7568 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7570 return getBuildVector(VT, DL, Ops);
7571 }
7572
7573 // Otherwise, we're growing or shrinking the elements. To avoid having to
7574 // handle annoying details of growing/shrinking FP values, we convert them to
7575 // int first.
7576 if (SrcEltVT.isFloatingPoint()) {
7577 // Convert the input float vector to a int vector where the elements are the
7578 // same sizes.
7579 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7580 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7582 DstEltVT);
7583 return SDValue();
7584 }
7585
7586 // Now we know the input is an integer vector. If the output is a FP type,
7587 // convert to integer first, then to FP of the right size.
7588 if (DstEltVT.isFloatingPoint()) {
7589 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7590 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7592 DstEltVT);
7593 return SDValue();
7594 }
7595
7596 // Okay, we know the src/dst types are both integers of differing types.
7597 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7598
7599 // Extract the constant raw bit data.
7600 BitVector UndefElements;
7601 SmallVector<APInt> RawBits;
7602 bool IsLE = getDataLayout().isLittleEndian();
7603 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7604 return SDValue();
7605
7607 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7608 if (UndefElements[I])
7609 Ops.push_back(getUNDEF(DstEltVT));
7610 else
7611 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7612 }
7613
7614 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7615 return getBuildVector(VT, DL, Ops);
7616}
7617
7619 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7620
7621 // There's no need to assert on a byte-aligned pointer. All pointers are at
7622 // least byte aligned.
7623 if (A == Align(1))
7624 return Val;
7625
7626 SDVTList VTs = getVTList(Val.getValueType());
7628 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7629 ID.AddInteger(A.value());
7630
7631 void *IP = nullptr;
7632 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7633 return SDValue(E, 0);
7634
7635 auto *N =
7636 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7637 createOperands(N, {Val});
7638
7639 CSEMap.InsertNode(N, IP);
7640 InsertNode(N);
7641
7642 SDValue V(N, 0);
7643 NewSDValueDbgMsg(V, "Creating new node: ", this);
7644 return V;
7645}
7646
7647SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7648 SDValue N1, SDValue N2) {
7649 SDNodeFlags Flags;
7650 if (Inserter)
7651 Flags = Inserter->getFlags();
7652 return getNode(Opcode, DL, VT, N1, N2, Flags);
7653}
7654
7656 SDValue &N2) const {
7657 if (!TLI->isCommutativeBinOp(Opcode))
7658 return;
7659
7660 // Canonicalize:
7661 // binop(const, nonconst) -> binop(nonconst, const)
7664 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7665 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7666 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7667 std::swap(N1, N2);
7668
7669 // Canonicalize:
7670 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7671 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7673 std::swap(N1, N2);
7674}
7675
7676SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7677 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7679 N2.getOpcode() != ISD::DELETED_NODE &&
7680 "Operand is DELETED_NODE!");
7681
7682 canonicalizeCommutativeBinop(Opcode, N1, N2);
7683
7684 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7685 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7686
7687 // Don't allow undefs in vector splats - we might be returning N2 when folding
7688 // to zero etc.
7689 ConstantSDNode *N2CV =
7690 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7691
7692 switch (Opcode) {
7693 default: break;
7694 case ISD::TokenFactor:
7695 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7696 N2.getValueType() == MVT::Other && "Invalid token factor!");
7697 // Fold trivial token factors.
7698 if (N1.getOpcode() == ISD::EntryToken) return N2;
7699 if (N2.getOpcode() == ISD::EntryToken) return N1;
7700 if (N1 == N2) return N1;
7701 break;
7702 case ISD::BUILD_VECTOR: {
7703 // Attempt to simplify BUILD_VECTOR.
7704 SDValue Ops[] = {N1, N2};
7705 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7706 return V;
7707 break;
7708 }
7709 case ISD::CONCAT_VECTORS: {
7710 SDValue Ops[] = {N1, N2};
7711 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7712 return V;
7713 break;
7714 }
7715 case ISD::AND:
7716 assert(VT.isInteger() && "This operator does not apply to FP types!");
7717 assert(N1.getValueType() == N2.getValueType() &&
7718 N1.getValueType() == VT && "Binary operator types must match!");
7719 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7720 // worth handling here.
7721 if (N2CV && N2CV->isZero())
7722 return N2;
7723 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7724 return N1;
7725 break;
7726 case ISD::OR:
7727 case ISD::XOR:
7728 case ISD::ADD:
7729 case ISD::PTRADD:
7730 case ISD::SUB:
7731 assert(VT.isInteger() && "This operator does not apply to FP types!");
7732 assert(N1.getValueType() == N2.getValueType() &&
7733 N1.getValueType() == VT && "Binary operator types must match!");
7734 // The equal operand types requirement is unnecessarily strong for PTRADD.
7735 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7736 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7737 // logic everywhere where PTRADDs may be folded or combined to properly
7738 // support them. If/when we introduce pointer types to the SDAG, we will
7739 // need to relax this constraint.
7740
7741 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7742 // it's worth handling here.
7743 if (N2CV && N2CV->isZero())
7744 return N1;
7745 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7746 VT.getScalarType() == MVT::i1)
7747 return getNode(ISD::XOR, DL, VT, N1, N2);
7748 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7749 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7750 N2.getOpcode() == ISD::VSCALE) {
7751 const APInt &C1 = N1->getConstantOperandAPInt(0);
7752 const APInt &C2 = N2->getConstantOperandAPInt(0);
7753 return getVScale(DL, VT, C1 + C2);
7754 }
7755 break;
7756 case ISD::MUL:
7757 assert(VT.isInteger() && "This operator does not apply to FP types!");
7758 assert(N1.getValueType() == N2.getValueType() &&
7759 N1.getValueType() == VT && "Binary operator types must match!");
7760 if (VT.getScalarType() == MVT::i1)
7761 return getNode(ISD::AND, DL, VT, N1, N2);
7762 if (N2CV && N2CV->isZero())
7763 return N2;
7764 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7765 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7766 const APInt &N2CImm = N2C->getAPIntValue();
7767 return getVScale(DL, VT, MulImm * N2CImm);
7768 }
7769 break;
7770 case ISD::UDIV:
7771 case ISD::UREM:
7772 case ISD::MULHU:
7773 case ISD::MULHS:
7774 case ISD::SDIV:
7775 case ISD::SREM:
7776 case ISD::SADDSAT:
7777 case ISD::SSUBSAT:
7778 case ISD::UADDSAT:
7779 case ISD::USUBSAT:
7780 assert(VT.isInteger() && "This operator does not apply to FP types!");
7781 assert(N1.getValueType() == N2.getValueType() &&
7782 N1.getValueType() == VT && "Binary operator types must match!");
7783 if (VT.getScalarType() == MVT::i1) {
7784 // fold (add_sat x, y) -> (or x, y) for bool types.
7785 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7786 return getNode(ISD::OR, DL, VT, N1, N2);
7787 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7788 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7789 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7790 }
7791 break;
7792 case ISD::SCMP:
7793 case ISD::UCMP:
7794 assert(N1.getValueType() == N2.getValueType() &&
7795 "Types of operands of UCMP/SCMP must match");
7796 assert(N1.getValueType().isVector() == VT.isVector() &&
7797 "Operands and return type of must both be scalars or vectors");
7798 if (VT.isVector())
7801 "Result and operands must have the same number of elements");
7802 break;
7803 case ISD::AVGFLOORS:
7804 case ISD::AVGFLOORU:
7805 case ISD::AVGCEILS:
7806 case ISD::AVGCEILU:
7807 assert(VT.isInteger() && "This operator does not apply to FP types!");
7808 assert(N1.getValueType() == N2.getValueType() &&
7809 N1.getValueType() == VT && "Binary operator types must match!");
7810 break;
7811 case ISD::ABDS:
7812 case ISD::ABDU:
7813 assert(VT.isInteger() && "This operator does not apply to FP types!");
7814 assert(N1.getValueType() == N2.getValueType() &&
7815 N1.getValueType() == VT && "Binary operator types must match!");
7816 if (VT.getScalarType() == MVT::i1)
7817 return getNode(ISD::XOR, DL, VT, N1, N2);
7818 break;
7819 case ISD::SMIN:
7820 case ISD::UMAX:
7821 assert(VT.isInteger() && "This operator does not apply to FP types!");
7822 assert(N1.getValueType() == N2.getValueType() &&
7823 N1.getValueType() == VT && "Binary operator types must match!");
7824 if (VT.getScalarType() == MVT::i1)
7825 return getNode(ISD::OR, DL, VT, N1, N2);
7826 break;
7827 case ISD::SMAX:
7828 case ISD::UMIN:
7829 assert(VT.isInteger() && "This operator does not apply to FP types!");
7830 assert(N1.getValueType() == N2.getValueType() &&
7831 N1.getValueType() == VT && "Binary operator types must match!");
7832 if (VT.getScalarType() == MVT::i1)
7833 return getNode(ISD::AND, DL, VT, N1, N2);
7834 break;
7835 case ISD::FADD:
7836 case ISD::FSUB:
7837 case ISD::FMUL:
7838 case ISD::FDIV:
7839 case ISD::FREM:
7840 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7841 assert(N1.getValueType() == N2.getValueType() &&
7842 N1.getValueType() == VT && "Binary operator types must match!");
7843 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7844 return V;
7845 break;
7846 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7847 assert(N1.getValueType() == VT &&
7850 "Invalid FCOPYSIGN!");
7851 break;
7852 case ISD::SHL:
7853 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7854 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7855 const APInt &ShiftImm = N2C->getAPIntValue();
7856 return getVScale(DL, VT, MulImm << ShiftImm);
7857 }
7858 [[fallthrough]];
7859 case ISD::SRA:
7860 case ISD::SRL:
7861 if (SDValue V = simplifyShift(N1, N2))
7862 return V;
7863 [[fallthrough]];
7864 case ISD::ROTL:
7865 case ISD::ROTR:
7866 case ISD::SSHLSAT:
7867 case ISD::USHLSAT:
7868 assert(VT == N1.getValueType() &&
7869 "Shift operators return type must be the same as their first arg");
7870 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7871 "Shifts only work on integers");
7872 assert((!VT.isVector() || VT == N2.getValueType()) &&
7873 "Vector shift amounts must be in the same as their first arg");
7874 // Verify that the shift amount VT is big enough to hold valid shift
7875 // amounts. This catches things like trying to shift an i1024 value by an
7876 // i8, which is easy to fall into in generic code that uses
7877 // TLI.getShiftAmount().
7880 "Invalid use of small shift amount with oversized value!");
7881
7882 // Always fold shifts of i1 values so the code generator doesn't need to
7883 // handle them. Since we know the size of the shift has to be less than the
7884 // size of the value, the shift/rotate count is guaranteed to be zero.
7885 if (VT == MVT::i1)
7886 return N1;
7887 if (N2CV && N2CV->isZero())
7888 return N1;
7889 break;
7890 case ISD::FP_ROUND:
7892 VT.bitsLE(N1.getValueType()) && N2C &&
7893 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7894 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7895 if (N1.getValueType() == VT) return N1; // noop conversion.
7896 break;
7897 case ISD::AssertNoFPClass: {
7899 "AssertNoFPClass is used for a non-floating type");
7900 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7901 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7902 assert(llvm::to_underlying(NoFPClass) <=
7904 "FPClassTest value too large");
7905 (void)NoFPClass;
7906 break;
7907 }
7908 case ISD::AssertSext:
7909 case ISD::AssertZext: {
7910 EVT EVT = cast<VTSDNode>(N2)->getVT();
7911 assert(VT == N1.getValueType() && "Not an inreg extend!");
7912 assert(VT.isInteger() && EVT.isInteger() &&
7913 "Cannot *_EXTEND_INREG FP types");
7914 assert(!EVT.isVector() &&
7915 "AssertSExt/AssertZExt type should be the vector element type "
7916 "rather than the vector type!");
7917 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7918 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7919 break;
7920 }
7922 EVT EVT = cast<VTSDNode>(N2)->getVT();
7923 assert(VT == N1.getValueType() && "Not an inreg extend!");
7924 assert(VT.isInteger() && EVT.isInteger() &&
7925 "Cannot *_EXTEND_INREG FP types");
7926 assert(EVT.isVector() == VT.isVector() &&
7927 "SIGN_EXTEND_INREG type should be vector iff the operand "
7928 "type is vector!");
7929 assert((!EVT.isVector() ||
7931 "Vector element counts must match in SIGN_EXTEND_INREG");
7932 assert(EVT.getScalarType().bitsLE(VT.getScalarType()) && "Not extending!");
7933 if (EVT == VT) return N1; // Not actually extending
7934 break;
7935 }
7937 case ISD::FP_TO_UINT_SAT: {
7938 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7939 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7940 assert(N1.getValueType().isVector() == VT.isVector() &&
7941 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7942 "vector!");
7943 assert((!VT.isVector() || VT.getVectorElementCount() ==
7945 "Vector element counts must match in FP_TO_*INT_SAT");
7946 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7947 "Type to saturate to must be a scalar.");
7948 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7949 "Not extending!");
7950 break;
7951 }
7954 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7955 element type of the vector.");
7956
7957 // Extract from an undefined value or using an undefined index is undefined.
7958 if (N1.isUndef() || N2.isUndef())
7959 return getUNDEF(VT);
7960
7961 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7962 // vectors. For scalable vectors we will provide appropriate support for
7963 // dealing with arbitrary indices.
7964 if (N2C && N1.getValueType().isFixedLengthVector() &&
7965 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7966 return getUNDEF(VT);
7967
7968 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7969 // expanding copies of large vectors from registers. This only works for
7970 // fixed length vectors, since we need to know the exact number of
7971 // elements.
7972 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7974 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7975 return getExtractVectorElt(DL, VT,
7976 N1.getOperand(N2C->getZExtValue() / Factor),
7977 N2C->getZExtValue() % Factor);
7978 }
7979
7980 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7981 // lowering is expanding large vector constants.
7982 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7983 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7986 "BUILD_VECTOR used for scalable vectors");
7987 unsigned Index =
7988 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7989 SDValue Elt = N1.getOperand(Index);
7990
7991 if (VT != Elt.getValueType())
7992 // If the vector element type is not legal, the BUILD_VECTOR operands
7993 // are promoted and implicitly truncated, and the result implicitly
7994 // extended. Make that explicit here.
7995 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7996
7997 return Elt;
7998 }
7999
8000 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
8001 // operations are lowered to scalars.
8002 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
8003 // If the indices are the same, return the inserted element else
8004 // if the indices are known different, extract the element from
8005 // the original vector.
8006 SDValue N1Op2 = N1.getOperand(2);
8008
8009 if (N1Op2C && N2C) {
8010 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
8011 if (VT == N1.getOperand(1).getValueType())
8012 return N1.getOperand(1);
8013 if (VT.isFloatingPoint()) {
8015 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
8016 }
8017 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
8018 }
8019 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
8020 }
8021 }
8022
8023 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
8024 // when vector types are scalarized and v1iX is legal.
8025 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
8026 // Here we are completely ignoring the extract element index (N2),
8027 // which is fine for fixed width vectors, since any index other than 0
8028 // is undefined anyway. However, this cannot be ignored for scalable
8029 // vectors - in theory we could support this, but we don't want to do this
8030 // without a profitability check.
8031 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8033 N1.getValueType().getVectorNumElements() == 1) {
8034 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8035 N1.getOperand(1));
8036 }
8037 break;
8039 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8040 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8041 (N1.getValueType().isInteger() == VT.isInteger()) &&
8042 N1.getValueType() != VT &&
8043 "Wrong types for EXTRACT_ELEMENT!");
8044
8045 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8046 // 64-bit integers into 32-bit parts. Instead of building the extract of
8047 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8048 if (N1.getOpcode() == ISD::BUILD_PAIR)
8049 return N1.getOperand(N2C->getZExtValue());
8050
8051 // EXTRACT_ELEMENT of a constant int is also very common.
8052 if (N1C) {
8053 unsigned ElementSize = VT.getSizeInBits();
8054 unsigned Shift = ElementSize * N2C->getZExtValue();
8055 const APInt &Val = N1C->getAPIntValue();
8056 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8057 }
8058 break;
8060 EVT N1VT = N1.getValueType();
8061 assert(VT.isVector() && N1VT.isVector() &&
8062 "Extract subvector VTs must be vectors!");
8064 "Extract subvector VTs must have the same element type!");
8065 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8066 "Cannot extract a scalable vector from a fixed length vector!");
8067 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8069 "Extract subvector must be from larger vector to smaller vector!");
8070 assert(N2C && "Extract subvector index must be a constant");
8071 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8072 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8073 N1VT.getVectorMinNumElements()) &&
8074 "Extract subvector overflow!");
8075 assert(N2C->getAPIntValue().getBitWidth() ==
8076 TLI->getVectorIdxWidth(getDataLayout()) &&
8077 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8078 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8079 "Extract index is not a multiple of the output vector length");
8080
8081 // Trivial extraction.
8082 if (VT == N1VT)
8083 return N1;
8084
8085 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8086 if (N1.isUndef())
8087 return getUNDEF(VT);
8088
8089 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8090 // the concat have the same type as the extract.
8091 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8092 VT == N1.getOperand(0).getValueType()) {
8093 unsigned Factor = VT.getVectorMinNumElements();
8094 return N1.getOperand(N2C->getZExtValue() / Factor);
8095 }
8096
8097 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8098 // during shuffle legalization.
8099 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8100 VT == N1.getOperand(1).getValueType())
8101 return N1.getOperand(1);
8102 break;
8103 }
8104 }
8105
8106 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8107 switch (Opcode) {
8108 case ISD::XOR:
8109 case ISD::ADD:
8110 case ISD::PTRADD:
8111 case ISD::SUB:
8113 case ISD::UDIV:
8114 case ISD::SDIV:
8115 case ISD::UREM:
8116 case ISD::SREM:
8117 case ISD::MUL:
8118 case ISD::AND:
8119 case ISD::SSUBSAT:
8120 case ISD::USUBSAT:
8121 case ISD::UMIN:
8122 case ISD::OR:
8123 case ISD::SADDSAT:
8124 case ISD::UADDSAT:
8125 case ISD::UMAX:
8126 case ISD::SMAX:
8127 case ISD::SMIN:
8128 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8129 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8130 }
8131 }
8132
8133 // Canonicalize an UNDEF to the RHS, even over a constant.
8134 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8135 if (TLI->isCommutativeBinOp(Opcode)) {
8136 std::swap(N1, N2);
8137 } else {
8138 switch (Opcode) {
8139 case ISD::PTRADD:
8140 case ISD::SUB:
8141 // fold op(undef, non_undef_arg2) -> undef.
8142 return N1;
8144 case ISD::UDIV:
8145 case ISD::SDIV:
8146 case ISD::UREM:
8147 case ISD::SREM:
8148 case ISD::SSUBSAT:
8149 case ISD::USUBSAT:
8150 // fold op(undef, non_undef_arg2) -> 0.
8151 return getConstant(0, DL, VT);
8152 }
8153 }
8154 }
8155
8156 // Fold a bunch of operators when the RHS is undef.
8157 if (N2.getOpcode() == ISD::UNDEF) {
8158 switch (Opcode) {
8159 case ISD::XOR:
8160 if (N1.getOpcode() == ISD::UNDEF)
8161 // Handle undef ^ undef -> 0 special case. This is a common
8162 // idiom (misuse).
8163 return getConstant(0, DL, VT);
8164 [[fallthrough]];
8165 case ISD::ADD:
8166 case ISD::PTRADD:
8167 case ISD::SUB:
8168 // fold op(arg1, undef) -> undef.
8169 return N2;
8170 case ISD::UDIV:
8171 case ISD::SDIV:
8172 case ISD::UREM:
8173 case ISD::SREM:
8174 // fold op(arg1, undef) -> poison.
8175 return getPOISON(VT);
8176 case ISD::MUL:
8177 case ISD::AND:
8178 case ISD::SSUBSAT:
8179 case ISD::USUBSAT:
8180 case ISD::UMIN:
8181 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8182 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8183 case ISD::OR:
8184 case ISD::SADDSAT:
8185 case ISD::UADDSAT:
8186 case ISD::UMAX:
8187 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8188 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8189 case ISD::SMAX:
8190 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8191 return N1.getOpcode() == ISD::UNDEF
8192 ? N2
8193 : getConstant(
8195 VT);
8196 case ISD::SMIN:
8197 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8198 return N1.getOpcode() == ISD::UNDEF
8199 ? N2
8200 : getConstant(
8202 VT);
8203 }
8204 }
8205
8206 // Perform trivial constant folding.
8207 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8208 return SV;
8209
8210 // Memoize this node if possible.
8211 SDNode *N;
8212 SDVTList VTs = getVTList(VT);
8213 SDValue Ops[] = {N1, N2};
8214 if (VT != MVT::Glue) {
8216 AddNodeIDNode(ID, Opcode, VTs, Ops);
8217 void *IP = nullptr;
8218 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8219 E->intersectFlagsWith(Flags);
8220 return SDValue(E, 0);
8221 }
8222
8223 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8224 N->setFlags(Flags);
8225 createOperands(N, Ops);
8226 CSEMap.InsertNode(N, IP);
8227 } else {
8228 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8229 createOperands(N, Ops);
8230 }
8231
8232 InsertNode(N);
8233 SDValue V = SDValue(N, 0);
8234 NewSDValueDbgMsg(V, "Creating new node: ", this);
8235 return V;
8236}
8237
8238SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8239 SDValue N1, SDValue N2, SDValue N3) {
8240 SDNodeFlags Flags;
8241 if (Inserter)
8242 Flags = Inserter->getFlags();
8243 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8244}
8245
8246SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8247 SDValue N1, SDValue N2, SDValue N3,
8248 const SDNodeFlags Flags) {
8250 N2.getOpcode() != ISD::DELETED_NODE &&
8251 N3.getOpcode() != ISD::DELETED_NODE &&
8252 "Operand is DELETED_NODE!");
8253 // Perform various simplifications.
8254 switch (Opcode) {
8255 case ISD::BUILD_VECTOR: {
8256 // Attempt to simplify BUILD_VECTOR.
8257 SDValue Ops[] = {N1, N2, N3};
8258 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8259 return V;
8260 break;
8261 }
8262 case ISD::CONCAT_VECTORS: {
8263 SDValue Ops[] = {N1, N2, N3};
8264 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8265 return V;
8266 break;
8267 }
8268 case ISD::SETCC: {
8269 assert(VT.isInteger() && "SETCC result type must be an integer!");
8270 assert(N1.getValueType() == N2.getValueType() &&
8271 "SETCC operands must have the same type!");
8272 assert(VT.isVector() == N1.getValueType().isVector() &&
8273 "SETCC type should be vector iff the operand type is vector!");
8274 assert((!VT.isVector() || VT.getVectorElementCount() ==
8276 "SETCC vector element counts must match!");
8277 // Use FoldSetCC to simplify SETCC's.
8278 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8279 return V;
8280 break;
8281 }
8282 case ISD::SELECT:
8283 case ISD::VSELECT:
8284 if (SDValue V = simplifySelect(N1, N2, N3))
8285 return V;
8286 break;
8288 llvm_unreachable("should use getVectorShuffle constructor!");
8290 if (isNullConstant(N3))
8291 return N1;
8292 break;
8294 if (isNullConstant(N3))
8295 return N2;
8296 break;
8298 assert(VT.isVector() && VT == N1.getValueType() &&
8299 "INSERT_VECTOR_ELT vector type mismatch");
8301 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8302 assert((!VT.isFloatingPoint() ||
8303 VT.getVectorElementType() == N2.getValueType()) &&
8304 "INSERT_VECTOR_ELT fp scalar type mismatch");
8305 assert((!VT.isInteger() ||
8307 "INSERT_VECTOR_ELT int scalar size mismatch");
8308
8309 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8310 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8311 // for scalable vectors where we will generate appropriate code to
8312 // deal with out-of-bounds cases correctly.
8313 if (N3C && VT.isFixedLengthVector() &&
8314 N3C->getZExtValue() >= VT.getVectorNumElements())
8315 return getUNDEF(VT);
8316
8317 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8318 if (N3.isUndef())
8319 return getUNDEF(VT);
8320
8321 // If inserting poison, just use the input vector.
8322 if (N2.getOpcode() == ISD::POISON)
8323 return N1;
8324
8325 // Inserting undef into undef/poison is still undef.
8326 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8327 return getUNDEF(VT);
8328
8329 // If the inserted element is an UNDEF, just use the input vector.
8330 // But not if skipping the insert could make the result more poisonous.
8331 if (N2.isUndef()) {
8332 if (N3C && VT.isFixedLengthVector()) {
8333 APInt EltMask =
8334 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8335 if (isGuaranteedNotToBePoison(N1, EltMask))
8336 return N1;
8337 } else if (isGuaranteedNotToBePoison(N1))
8338 return N1;
8339 }
8340 break;
8341 }
8342 case ISD::INSERT_SUBVECTOR: {
8343 // If inserting poison, just use the input vector,
8344 if (N2.getOpcode() == ISD::POISON)
8345 return N1;
8346
8347 // Inserting undef into undef/poison is still undef.
8348 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8349 return getUNDEF(VT);
8350
8351 EVT N2VT = N2.getValueType();
8352 assert(VT == N1.getValueType() &&
8353 "Dest and insert subvector source types must match!");
8354 assert(VT.isVector() && N2VT.isVector() &&
8355 "Insert subvector VTs must be vectors!");
8357 "Insert subvector VTs must have the same element type!");
8358 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8359 "Cannot insert a scalable vector into a fixed length vector!");
8360 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8362 "Insert subvector must be from smaller vector to larger vector!");
8364 "Insert subvector index must be constant");
8365 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8366 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8368 "Insert subvector overflow!");
8370 TLI->getVectorIdxWidth(getDataLayout()) &&
8371 "Constant index for INSERT_SUBVECTOR has an invalid size");
8372
8373 // Trivial insertion.
8374 if (VT == N2VT)
8375 return N2;
8376
8377 // If this is an insert of an extracted vector into an undef/poison vector,
8378 // we can just use the input to the extract. But not if skipping the
8379 // extract+insert could make the result more poisonous.
8380 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8381 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8382 if (N1.getOpcode() == ISD::POISON)
8383 return N2.getOperand(0);
8384 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8385 unsigned LoBit = N3->getAsZExtVal();
8386 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8387 APInt EltMask =
8388 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8389 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8390 return N2.getOperand(0);
8391 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8392 return N2.getOperand(0);
8393 }
8394
8395 // If the inserted subvector is UNDEF, just use the input vector.
8396 // But not if skipping the insert could make the result more poisonous.
8397 if (N2.isUndef()) {
8398 if (VT.isFixedLengthVector()) {
8399 unsigned LoBit = N3->getAsZExtVal();
8400 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8401 APInt EltMask =
8402 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8403 if (isGuaranteedNotToBePoison(N1, EltMask))
8404 return N1;
8405 } else if (isGuaranteedNotToBePoison(N1))
8406 return N1;
8407 }
8408 break;
8409 }
8410 case ISD::BITCAST:
8411 // Fold bit_convert nodes from a type to themselves.
8412 if (N1.getValueType() == VT)
8413 return N1;
8414 break;
8415 case ISD::VP_TRUNCATE:
8416 case ISD::VP_SIGN_EXTEND:
8417 case ISD::VP_ZERO_EXTEND:
8418 // Don't create noop casts.
8419 if (N1.getValueType() == VT)
8420 return N1;
8421 break;
8422 case ISD::VECTOR_COMPRESS: {
8423 [[maybe_unused]] EVT VecVT = N1.getValueType();
8424 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8425 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8426 assert(VT == VecVT && "Vector and result type don't match.");
8427 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8428 "All inputs must be vectors.");
8429 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8431 "Vector and mask must have same number of elements.");
8432
8433 if (N1.isUndef() || N2.isUndef())
8434 return N3;
8435
8436 break;
8437 }
8442 [[maybe_unused]] EVT AccVT = N1.getValueType();
8443 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8444 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8445 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8446 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8447 "node to have the same type!");
8448 assert(VT.isVector() && VT == AccVT &&
8449 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8450 "the same type as its result!");
8452 AccVT.getVectorElementCount()) &&
8453 "Expected the element count of the second and third operands of the "
8454 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8455 "element count of the first operand and the result!");
8457 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8458 "node to have an element type which is the same as or smaller than "
8459 "the element type of the first operand and result!");
8460 break;
8461 }
8462 }
8463
8464 // Perform trivial constant folding for arithmetic operators.
8465 switch (Opcode) {
8466 case ISD::FMA:
8467 case ISD::FMAD:
8468 case ISD::SETCC:
8469 case ISD::FSHL:
8470 case ISD::FSHR:
8471 if (SDValue SV =
8472 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8473 return SV;
8474 break;
8475 }
8476
8477 // Memoize node if it doesn't produce a glue result.
8478 SDNode *N;
8479 SDVTList VTs = getVTList(VT);
8480 SDValue Ops[] = {N1, N2, N3};
8481 if (VT != MVT::Glue) {
8483 AddNodeIDNode(ID, Opcode, VTs, Ops);
8484 void *IP = nullptr;
8485 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8486 E->intersectFlagsWith(Flags);
8487 return SDValue(E, 0);
8488 }
8489
8490 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8491 N->setFlags(Flags);
8492 createOperands(N, Ops);
8493 CSEMap.InsertNode(N, IP);
8494 } else {
8495 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8496 createOperands(N, Ops);
8497 }
8498
8499 InsertNode(N);
8500 SDValue V = SDValue(N, 0);
8501 NewSDValueDbgMsg(V, "Creating new node: ", this);
8502 return V;
8503}
8504
8505SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8506 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8507 const SDNodeFlags Flags) {
8508 SDValue Ops[] = { N1, N2, N3, N4 };
8509 return getNode(Opcode, DL, VT, Ops, Flags);
8510}
8511
8512SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8513 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8514 SDNodeFlags Flags;
8515 if (Inserter)
8516 Flags = Inserter->getFlags();
8517 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8518}
8519
8520SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8521 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8522 SDValue N5, const SDNodeFlags Flags) {
8523 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8524 return getNode(Opcode, DL, VT, Ops, Flags);
8525}
8526
8527SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8528 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8529 SDValue N5) {
8530 SDNodeFlags Flags;
8531 if (Inserter)
8532 Flags = Inserter->getFlags();
8533 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8534}
8535
8536/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8537/// the incoming stack arguments to be loaded from the stack.
8539 SmallVector<SDValue, 8> ArgChains;
8540
8541 // Include the original chain at the beginning of the list. When this is
8542 // used by target LowerCall hooks, this helps legalize find the
8543 // CALLSEQ_BEGIN node.
8544 ArgChains.push_back(Chain);
8545
8546 // Add a chain value for each stack argument.
8547 for (SDNode *U : getEntryNode().getNode()->users())
8548 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8549 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8550 if (FI->getIndex() < 0)
8551 ArgChains.push_back(SDValue(L, 1));
8552
8553 // Build a tokenfactor for all the chains.
8554 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8555}
8556
8557/// getMemsetValue - Vectorized representation of the memset value
8558/// operand.
8560 const SDLoc &dl) {
8561 assert(!Value.isUndef());
8562
8563 unsigned NumBits = VT.getScalarSizeInBits();
8565 assert(C->getAPIntValue().getBitWidth() == 8);
8566 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8567 if (VT.isInteger()) {
8568 bool IsOpaque = VT.getSizeInBits() > 64 ||
8569 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8570 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8571 }
8572 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8573 }
8574
8575 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8576 EVT IntVT = VT.getScalarType();
8577 if (!IntVT.isInteger())
8578 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8579
8580 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8581 if (NumBits > 8) {
8582 // Use a multiplication with 0x010101... to extend the input to the
8583 // required length.
8584 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8585 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8586 DAG.getConstant(Magic, dl, IntVT));
8587 }
8588
8589 if (VT != Value.getValueType() && !VT.isInteger())
8590 Value = DAG.getBitcast(VT.getScalarType(), Value);
8591 if (VT != Value.getValueType())
8592 Value = DAG.getSplatBuildVector(VT, dl, Value);
8593
8594 return Value;
8595}
8596
8597/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8598/// used when a memcpy is turned into a memset when the source is a constant
8599/// string ptr.
8601 const TargetLowering &TLI,
8602 const ConstantDataArraySlice &Slice) {
8603 // Handle vector with all elements zero.
8604 if (Slice.Array == nullptr) {
8605 if (VT.isInteger())
8606 return DAG.getConstant(0, dl, VT);
8607 return DAG.getNode(ISD::BITCAST, dl, VT,
8608 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8609 }
8610
8611 assert(!VT.isVector() && "Can't handle vector type here!");
8612 unsigned NumVTBits = VT.getSizeInBits();
8613 unsigned NumVTBytes = NumVTBits / 8;
8614 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8615
8616 APInt Val(NumVTBits, 0);
8617 if (DAG.getDataLayout().isLittleEndian()) {
8618 for (unsigned i = 0; i != NumBytes; ++i)
8619 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8620 } else {
8621 for (unsigned i = 0; i != NumBytes; ++i)
8622 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8623 }
8624
8625 // If the "cost" of materializing the integer immediate is less than the cost
8626 // of a load, then it is cost effective to turn the load into the immediate.
8627 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8628 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8629 return DAG.getConstant(Val, dl, VT);
8630 return SDValue();
8631}
8632
8634 const SDLoc &DL,
8635 const SDNodeFlags Flags) {
8636 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
8637 return getMemBasePlusOffset(Base, Index, DL, Flags);
8638}
8639
8641 const SDLoc &DL,
8642 const SDNodeFlags Flags) {
8643 assert(Offset.getValueType().isInteger());
8644 EVT BasePtrVT = Ptr.getValueType();
8645 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8646 BasePtrVT))
8647 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8648 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8649 SDNodeFlags AddFlags = Flags;
8650 AddFlags.setInBounds(false);
8651 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8652}
8653
8654/// Returns true if memcpy source is constant data.
8656 uint64_t SrcDelta = 0;
8657 GlobalAddressSDNode *G = nullptr;
8658 if (Src.getOpcode() == ISD::GlobalAddress)
8660 else if (Src->isAnyAdd() &&
8661 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8662 Src.getOperand(1).getOpcode() == ISD::Constant) {
8663 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8664 SrcDelta = Src.getConstantOperandVal(1);
8665 }
8666 if (!G)
8667 return false;
8668
8669 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8670 SrcDelta + G->getOffset());
8671}
8672
8674 SelectionDAG &DAG) {
8675 // On Darwin, -Os means optimize for size without hurting performance, so
8676 // only really optimize for size when -Oz (MinSize) is used.
8678 return MF.getFunction().hasMinSize();
8679 return DAG.shouldOptForSize();
8680}
8681
8683 SmallVector<SDValue, 32> &OutChains, unsigned From,
8684 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8685 SmallVector<SDValue, 16> &OutStoreChains) {
8686 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8687 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8688 SmallVector<SDValue, 16> GluedLoadChains;
8689 for (unsigned i = From; i < To; ++i) {
8690 OutChains.push_back(OutLoadChains[i]);
8691 GluedLoadChains.push_back(OutLoadChains[i]);
8692 }
8693
8694 // Chain for all loads.
8695 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8696 GluedLoadChains);
8697
8698 for (unsigned i = From; i < To; ++i) {
8699 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8700 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8701 ST->getBasePtr(), ST->getMemoryVT(),
8702 ST->getMemOperand());
8703 OutChains.push_back(NewStore);
8704 }
8705}
8706
8708 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8709 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8710 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8711 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8712 // Turn a memcpy of undef to nop.
8713 // FIXME: We need to honor volatile even is Src is undef.
8714 if (Src.isUndef())
8715 return Chain;
8716
8717 // Expand memcpy to a series of load and store ops if the size operand falls
8718 // below a certain threshold.
8719 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8720 // rather than maybe a humongous number of loads and stores.
8721 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8722 const DataLayout &DL = DAG.getDataLayout();
8723 LLVMContext &C = *DAG.getContext();
8724 std::vector<EVT> MemOps;
8725 bool DstAlignCanChange = false;
8727 MachineFrameInfo &MFI = MF.getFrameInfo();
8728 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8730 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8731 DstAlignCanChange = true;
8732 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8733 if (!SrcAlign || Alignment > *SrcAlign)
8734 SrcAlign = Alignment;
8735 assert(SrcAlign && "SrcAlign must be set");
8737 // If marked as volatile, perform a copy even when marked as constant.
8738 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8739 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8740 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8741 const MemOp Op = isZeroConstant
8742 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8743 /*IsZeroMemset*/ true, isVol)
8744 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8745 *SrcAlign, isVol, CopyFromConstant);
8746 if (!TLI.findOptimalMemOpLowering(
8747 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8748 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8749 return SDValue();
8750
8751 if (DstAlignCanChange) {
8752 Type *Ty = MemOps[0].getTypeForEVT(C);
8753 Align NewAlign = DL.getABITypeAlign(Ty);
8754
8755 // Don't promote to an alignment that would require dynamic stack
8756 // realignment which may conflict with optimizations such as tail call
8757 // optimization.
8759 if (!TRI->hasStackRealignment(MF))
8760 if (MaybeAlign StackAlign = DL.getStackAlignment())
8761 NewAlign = std::min(NewAlign, *StackAlign);
8762
8763 if (NewAlign > Alignment) {
8764 // Give the stack frame object a larger alignment if needed.
8765 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8766 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8767 Alignment = NewAlign;
8768 }
8769 }
8770
8771 // Prepare AAInfo for loads/stores after lowering this memcpy.
8772 AAMDNodes NewAAInfo = AAInfo;
8773 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8774
8775 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8776 bool isConstant =
8777 BatchAA && SrcVal &&
8778 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8779
8780 MachineMemOperand::Flags MMOFlags =
8782 SmallVector<SDValue, 16> OutLoadChains;
8783 SmallVector<SDValue, 16> OutStoreChains;
8784 SmallVector<SDValue, 32> OutChains;
8785 unsigned NumMemOps = MemOps.size();
8786 uint64_t SrcOff = 0, DstOff = 0;
8787 for (unsigned i = 0; i != NumMemOps; ++i) {
8788 EVT VT = MemOps[i];
8789 unsigned VTSize = VT.getSizeInBits() / 8;
8790 SDValue Value, Store;
8791
8792 if (VTSize > Size) {
8793 // Issuing an unaligned load / store pair that overlaps with the previous
8794 // pair. Adjust the offset accordingly.
8795 assert(i == NumMemOps-1 && i != 0);
8796 SrcOff -= VTSize - Size;
8797 DstOff -= VTSize - Size;
8798 }
8799
8800 if (CopyFromConstant &&
8801 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8802 // It's unlikely a store of a vector immediate can be done in a single
8803 // instruction. It would require a load from a constantpool first.
8804 // We only handle zero vectors here.
8805 // FIXME: Handle other cases where store of vector immediate is done in
8806 // a single instruction.
8807 ConstantDataArraySlice SubSlice;
8808 if (SrcOff < Slice.Length) {
8809 SubSlice = Slice;
8810 SubSlice.move(SrcOff);
8811 } else {
8812 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8813 SubSlice.Array = nullptr;
8814 SubSlice.Offset = 0;
8815 SubSlice.Length = VTSize;
8816 }
8817 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8818 if (Value.getNode()) {
8819 Store = DAG.getStore(
8820 Chain, dl, Value,
8821 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8822 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8823 OutChains.push_back(Store);
8824 }
8825 }
8826
8827 if (!Store.getNode()) {
8828 // The type might not be legal for the target. This should only happen
8829 // if the type is smaller than a legal type, as on PPC, so the right
8830 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8831 // to Load/Store if NVT==VT.
8832 // FIXME does the case above also need this?
8833 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8834 assert(NVT.bitsGE(VT));
8835
8836 bool isDereferenceable =
8837 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8838 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8839 if (isDereferenceable)
8841 if (isConstant)
8842 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8843
8844 Value = DAG.getExtLoad(
8845 ISD::EXTLOAD, dl, NVT, Chain,
8846 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8847 SrcPtrInfo.getWithOffset(SrcOff), VT,
8848 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8849 OutLoadChains.push_back(Value.getValue(1));
8850
8851 Store = DAG.getTruncStore(
8852 Chain, dl, Value,
8853 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8854 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8855 OutStoreChains.push_back(Store);
8856 }
8857 SrcOff += VTSize;
8858 DstOff += VTSize;
8859 Size -= VTSize;
8860 }
8861
8862 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8864 unsigned NumLdStInMemcpy = OutStoreChains.size();
8865
8866 if (NumLdStInMemcpy) {
8867 // It may be that memcpy might be converted to memset if it's memcpy
8868 // of constants. In such a case, we won't have loads and stores, but
8869 // just stores. In the absence of loads, there is nothing to gang up.
8870 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8871 // If target does not care, just leave as it.
8872 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8873 OutChains.push_back(OutLoadChains[i]);
8874 OutChains.push_back(OutStoreChains[i]);
8875 }
8876 } else {
8877 // Ld/St less than/equal limit set by target.
8878 if (NumLdStInMemcpy <= GluedLdStLimit) {
8879 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8880 NumLdStInMemcpy, OutLoadChains,
8881 OutStoreChains);
8882 } else {
8883 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8884 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8885 unsigned GlueIter = 0;
8886
8887 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8888 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8889 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8890
8891 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8892 OutLoadChains, OutStoreChains);
8893 GlueIter += GluedLdStLimit;
8894 }
8895
8896 // Residual ld/st.
8897 if (RemainingLdStInMemcpy) {
8898 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8899 RemainingLdStInMemcpy, OutLoadChains,
8900 OutStoreChains);
8901 }
8902 }
8903 }
8904 }
8905 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8906}
8907
8909 SDValue Chain, SDValue Dst, SDValue Src,
8910 uint64_t Size, Align Alignment,
8911 bool isVol, bool AlwaysInline,
8912 MachinePointerInfo DstPtrInfo,
8913 MachinePointerInfo SrcPtrInfo,
8914 const AAMDNodes &AAInfo) {
8915 // Turn a memmove of undef to nop.
8916 // FIXME: We need to honor volatile even is Src is undef.
8917 if (Src.isUndef())
8918 return Chain;
8919
8920 // Expand memmove to a series of load and store ops if the size operand falls
8921 // below a certain threshold.
8922 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8923 const DataLayout &DL = DAG.getDataLayout();
8924 LLVMContext &C = *DAG.getContext();
8925 std::vector<EVT> MemOps;
8926 bool DstAlignCanChange = false;
8928 MachineFrameInfo &MFI = MF.getFrameInfo();
8929 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8931 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8932 DstAlignCanChange = true;
8933 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8934 if (!SrcAlign || Alignment > *SrcAlign)
8935 SrcAlign = Alignment;
8936 assert(SrcAlign && "SrcAlign must be set");
8937 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8938 if (!TLI.findOptimalMemOpLowering(
8939 C, MemOps, Limit,
8940 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8941 /*IsVolatile*/ true),
8942 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8943 MF.getFunction().getAttributes()))
8944 return SDValue();
8945
8946 if (DstAlignCanChange) {
8947 Type *Ty = MemOps[0].getTypeForEVT(C);
8948 Align NewAlign = DL.getABITypeAlign(Ty);
8949
8950 // Don't promote to an alignment that would require dynamic stack
8951 // realignment which may conflict with optimizations such as tail call
8952 // optimization.
8954 if (!TRI->hasStackRealignment(MF))
8955 if (MaybeAlign StackAlign = DL.getStackAlignment())
8956 NewAlign = std::min(NewAlign, *StackAlign);
8957
8958 if (NewAlign > Alignment) {
8959 // Give the stack frame object a larger alignment if needed.
8960 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8961 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8962 Alignment = NewAlign;
8963 }
8964 }
8965
8966 // Prepare AAInfo for loads/stores after lowering this memmove.
8967 AAMDNodes NewAAInfo = AAInfo;
8968 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8969
8970 MachineMemOperand::Flags MMOFlags =
8972 uint64_t SrcOff = 0, DstOff = 0;
8973 SmallVector<SDValue, 8> LoadValues;
8974 SmallVector<SDValue, 8> LoadChains;
8975 SmallVector<SDValue, 8> OutChains;
8976 unsigned NumMemOps = MemOps.size();
8977 for (unsigned i = 0; i < NumMemOps; i++) {
8978 EVT VT = MemOps[i];
8979 unsigned VTSize = VT.getSizeInBits() / 8;
8980 SDValue Value;
8981
8982 bool isDereferenceable =
8983 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8984 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8985 if (isDereferenceable)
8987
8988 Value = DAG.getLoad(
8989 VT, dl, Chain,
8990 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8991 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8992 LoadValues.push_back(Value);
8993 LoadChains.push_back(Value.getValue(1));
8994 SrcOff += VTSize;
8995 }
8996 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8997 OutChains.clear();
8998 for (unsigned i = 0; i < NumMemOps; i++) {
8999 EVT VT = MemOps[i];
9000 unsigned VTSize = VT.getSizeInBits() / 8;
9001 SDValue Store;
9002
9003 Store = DAG.getStore(
9004 Chain, dl, LoadValues[i],
9005 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9006 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
9007 OutChains.push_back(Store);
9008 DstOff += VTSize;
9009 }
9010
9011 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9012}
9013
9014/// Lower the call to 'memset' intrinsic function into a series of store
9015/// operations.
9016///
9017/// \param DAG Selection DAG where lowered code is placed.
9018/// \param dl Link to corresponding IR location.
9019/// \param Chain Control flow dependency.
9020/// \param Dst Pointer to destination memory location.
9021/// \param Src Value of byte to write into the memory.
9022/// \param Size Number of bytes to write.
9023/// \param Alignment Alignment of the destination in bytes.
9024/// \param isVol True if destination is volatile.
9025/// \param AlwaysInline Makes sure no function call is generated.
9026/// \param DstPtrInfo IR information on the memory pointer.
9027/// \returns New head in the control flow, if lowering was successful, empty
9028/// SDValue otherwise.
9029///
9030/// The function tries to replace 'llvm.memset' intrinsic with several store
9031/// operations and value calculation code. This is usually profitable for small
9032/// memory size or when the semantic requires inlining.
9034 SDValue Chain, SDValue Dst, SDValue Src,
9035 uint64_t Size, Align Alignment, bool isVol,
9036 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9037 const AAMDNodes &AAInfo) {
9038 // Turn a memset of undef to nop.
9039 // FIXME: We need to honor volatile even is Src is undef.
9040 if (Src.isUndef())
9041 return Chain;
9042
9043 // Expand memset to a series of load/store ops if the size operand
9044 // falls below a certain threshold.
9045 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9046 std::vector<EVT> MemOps;
9047 bool DstAlignCanChange = false;
9048 LLVMContext &C = *DAG.getContext();
9050 MachineFrameInfo &MFI = MF.getFrameInfo();
9051 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9053 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9054 DstAlignCanChange = true;
9055 bool IsZeroVal = isNullConstant(Src);
9056 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9057
9058 if (!TLI.findOptimalMemOpLowering(
9059 C, MemOps, Limit,
9060 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9061 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
9062 return SDValue();
9063
9064 if (DstAlignCanChange) {
9065 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9066 const DataLayout &DL = DAG.getDataLayout();
9067 Align NewAlign = DL.getABITypeAlign(Ty);
9068
9069 // Don't promote to an alignment that would require dynamic stack
9070 // realignment which may conflict with optimizations such as tail call
9071 // optimization.
9073 if (!TRI->hasStackRealignment(MF))
9074 if (MaybeAlign StackAlign = DL.getStackAlignment())
9075 NewAlign = std::min(NewAlign, *StackAlign);
9076
9077 if (NewAlign > Alignment) {
9078 // Give the stack frame object a larger alignment if needed.
9079 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9080 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9081 Alignment = NewAlign;
9082 }
9083 }
9084
9085 SmallVector<SDValue, 8> OutChains;
9086 uint64_t DstOff = 0;
9087 unsigned NumMemOps = MemOps.size();
9088
9089 // Find the largest store and generate the bit pattern for it.
9090 EVT LargestVT = MemOps[0];
9091 for (unsigned i = 1; i < NumMemOps; i++)
9092 if (MemOps[i].bitsGT(LargestVT))
9093 LargestVT = MemOps[i];
9094 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9095
9096 // Prepare AAInfo for loads/stores after lowering this memset.
9097 AAMDNodes NewAAInfo = AAInfo;
9098 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9099
9100 for (unsigned i = 0; i < NumMemOps; i++) {
9101 EVT VT = MemOps[i];
9102 unsigned VTSize = VT.getSizeInBits() / 8;
9103 if (VTSize > Size) {
9104 // Issuing an unaligned load / store pair that overlaps with the previous
9105 // pair. Adjust the offset accordingly.
9106 assert(i == NumMemOps-1 && i != 0);
9107 DstOff -= VTSize - Size;
9108 }
9109
9110 // If this store is smaller than the largest store see whether we can get
9111 // the smaller value for free with a truncate or extract vector element and
9112 // then store.
9113 SDValue Value = MemSetValue;
9114 if (VT.bitsLT(LargestVT)) {
9115 unsigned Index;
9116 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9117 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9118 if (!LargestVT.isVector() && !VT.isVector() &&
9119 TLI.isTruncateFree(LargestVT, VT))
9120 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9121 else if (LargestVT.isVector() && !VT.isVector() &&
9123 LargestVT.getTypeForEVT(*DAG.getContext()),
9124 VT.getSizeInBits(), Index) &&
9125 TLI.isTypeLegal(SVT) &&
9126 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9127 // Target which can combine store(extractelement VectorTy, Idx) can get
9128 // the smaller value for free.
9129 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9130 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9131 } else
9132 Value = getMemsetValue(Src, VT, DAG, dl);
9133 }
9134 assert(Value.getValueType() == VT && "Value with wrong type.");
9135 SDValue Store = DAG.getStore(
9136 Chain, dl, Value,
9137 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9138 DstPtrInfo.getWithOffset(DstOff), Alignment,
9140 NewAAInfo);
9141 OutChains.push_back(Store);
9142 DstOff += VT.getSizeInBits() / 8;
9143 Size -= VTSize;
9144 }
9145
9146 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9147}
9148
9150 unsigned AS) {
9151 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9152 // pointer operands can be losslessly bitcasted to pointers of address space 0
9153 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9154 report_fatal_error("cannot lower memory intrinsic in address space " +
9155 Twine(AS));
9156 }
9157}
9158
9160 const SelectionDAG *SelDAG,
9161 bool AllowReturnsFirstArg) {
9162 if (!CI || !CI->isTailCall())
9163 return false;
9164 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9165 // helper symbol we lower to.
9166 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9167 AllowReturnsFirstArg &&
9169}
9170
9171static std::pair<SDValue, SDValue>
9174 const CallInst *CI, RTLIB::Libcall Call,
9175 SelectionDAG *DAG, const TargetLowering *TLI) {
9176 RTLIB::LibcallImpl LCImpl = TLI->getLibcallImpl(Call);
9177
9178 if (LCImpl == RTLIB::Unsupported)
9179 return {};
9180
9182 bool IsTailCall =
9183 isInTailCallPositionWrapper(CI, DAG, /*AllowReturnsFirstArg=*/true);
9184 SDValue Callee =
9185 DAG->getExternalSymbol(LCImpl, TLI->getPointerTy(DAG->getDataLayout()));
9186
9187 CLI.setDebugLoc(dl)
9188 .setChain(Chain)
9189 .setLibCallee(TLI->getLibcallImplCallingConv(LCImpl), CI->getType(),
9190 Callee, std::move(Args))
9191 .setTailCall(IsTailCall);
9192
9193 return TLI->LowerCallTo(CLI);
9194}
9195
9196std::pair<SDValue, SDValue> SelectionDAG::getStrstr(SDValue Chain,
9197 const SDLoc &dl, SDValue S1,
9198 SDValue S2,
9199 const CallInst *CI) {
9201 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9202 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9203 RTLIB::STRSTR, this, TLI);
9204}
9205
9206std::pair<SDValue, SDValue>
9208 SDValue Mem1, SDValue Size, const CallInst *CI) {
9209 RTLIB::LibcallImpl MemcmpImpl = getLibcalls().getLibcallImpl(RTLIB::MEMCMP);
9210 if (MemcmpImpl == RTLIB::Unsupported)
9211 return {};
9212
9215 {Mem0, PT},
9216 {Mem1, PT},
9218
9220 bool IsTailCall =
9221 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9222
9223 CLI.setDebugLoc(dl)
9224 .setChain(Chain)
9225 .setLibCallee(
9226 getLibcalls().getLibcallImplCallingConv(MemcmpImpl),
9228 getExternalSymbol(MemcmpImpl, TLI->getPointerTy(getDataLayout())),
9229 std::move(Args))
9230 .setTailCall(IsTailCall);
9231
9232 return TLI->LowerCallTo(CLI);
9233}
9234
9235std::pair<SDValue, SDValue> SelectionDAG::getStrcpy(SDValue Chain,
9236 const SDLoc &dl,
9237 SDValue Dst, SDValue Src,
9238 const CallInst *CI) {
9239 RTLIB::LibcallImpl LCImpl = TLI->getLibcallImpl(RTLIB::STRCPY);
9240 if (LCImpl == RTLIB::Unsupported)
9241 return {};
9242
9244 TargetLowering::ArgListTy Args = {{Dst, PT}, {Src, PT}};
9245
9247 bool IsTailCall =
9248 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg=*/true);
9249
9250 CLI.setDebugLoc(dl)
9251 .setChain(Chain)
9252 .setLibCallee(
9253 TLI->getLibcallImplCallingConv(LCImpl), CI->getType(),
9254 getExternalSymbol(LCImpl, TLI->getPointerTy(getDataLayout())),
9255 std::move(Args))
9256 .setTailCall(IsTailCall);
9257
9258 return TLI->LowerCallTo(CLI);
9259}
9260
9261std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9262 const SDLoc &dl,
9263 SDValue Src,
9264 const CallInst *CI) {
9265 RTLIB::LibcallImpl StrlenImpl = getLibcalls().getLibcallImpl(RTLIB::STRLEN);
9266 if (StrlenImpl == RTLIB::Unsupported)
9267 return {};
9268
9269 // Emit a library call.
9272
9274 bool IsTailCall =
9275 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9276
9277 CLI.setDebugLoc(dl)
9278 .setChain(Chain)
9279 .setLibCallee(TLI->getLibcallImplCallingConv(StrlenImpl), CI->getType(),
9281 StrlenImpl, TLI->getProgramPointerTy(getDataLayout())),
9282 std::move(Args))
9283 .setTailCall(IsTailCall);
9284
9285 return TLI->LowerCallTo(CLI);
9286}
9287
9289 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9290 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9291 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9292 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9293 BatchAAResults *BatchAA) {
9294 // Check to see if we should lower the memcpy to loads and stores first.
9295 // For cases within the target-specified limits, this is the best choice.
9297 if (ConstantSize) {
9298 // Memcpy with size zero? Just return the original chain.
9299 if (ConstantSize->isZero())
9300 return Chain;
9301
9303 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9304 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9305 if (Result.getNode())
9306 return Result;
9307 }
9308
9309 // Then check to see if we should lower the memcpy with target-specific
9310 // code. If the target chooses to do this, this is the next best.
9311 if (TSI) {
9312 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9313 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9314 DstPtrInfo, SrcPtrInfo);
9315 if (Result.getNode())
9316 return Result;
9317 }
9318
9319 // If we really need inline code and the target declined to provide it,
9320 // use a (potentially long) sequence of loads and stores.
9321 if (AlwaysInline) {
9322 assert(ConstantSize && "AlwaysInline requires a constant size!");
9324 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9325 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9326 }
9327
9330
9331 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9332 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9333 // respect volatile, so they may do things like read or write memory
9334 // beyond the given memory regions. But fixing this isn't easy, and most
9335 // people don't care.
9336
9337 // Emit a library call.
9340 Args.emplace_back(Dst, PtrTy);
9341 Args.emplace_back(Src, PtrTy);
9342 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9343 // FIXME: pass in SDLoc
9345 bool IsTailCall = false;
9346 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9347
9348 if (OverrideTailCall.has_value()) {
9349 IsTailCall = *OverrideTailCall;
9350 } else {
9351 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9352 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9353 }
9354
9355 CLI.setDebugLoc(dl)
9356 .setChain(Chain)
9357 .setLibCallee(
9358 getLibcalls().getLibcallImplCallingConv(MemCpyImpl),
9359 Dst.getValueType().getTypeForEVT(*getContext()),
9360 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
9361 std::move(Args))
9363 .setTailCall(IsTailCall);
9364
9365 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9366 return CallResult.second;
9367}
9368
9370 SDValue Dst, SDValue Src, SDValue Size,
9371 Type *SizeTy, unsigned ElemSz,
9372 bool isTailCall,
9373 MachinePointerInfo DstPtrInfo,
9374 MachinePointerInfo SrcPtrInfo) {
9375 // Emit a library call.
9378 Args.emplace_back(Dst, ArgTy);
9379 Args.emplace_back(Src, ArgTy);
9380 Args.emplace_back(Size, SizeTy);
9381
9382 RTLIB::Libcall LibraryCall =
9384 RTLIB::LibcallImpl LibcallImpl = getLibcalls().getLibcallImpl(LibraryCall);
9385 if (LibcallImpl == RTLIB::Unsupported)
9386 report_fatal_error("Unsupported element size");
9387
9389 CLI.setDebugLoc(dl)
9390 .setChain(Chain)
9391 .setLibCallee(
9392 getLibcalls().getLibcallImplCallingConv(LibcallImpl),
9394 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9395 std::move(Args))
9397 .setTailCall(isTailCall);
9398
9399 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9400 return CallResult.second;
9401}
9402
9404 SDValue Src, SDValue Size, Align Alignment,
9405 bool isVol, const CallInst *CI,
9406 std::optional<bool> OverrideTailCall,
9407 MachinePointerInfo DstPtrInfo,
9408 MachinePointerInfo SrcPtrInfo,
9409 const AAMDNodes &AAInfo,
9410 BatchAAResults *BatchAA) {
9411 // Check to see if we should lower the memmove to loads and stores first.
9412 // For cases within the target-specified limits, this is the best choice.
9414 if (ConstantSize) {
9415 // Memmove with size zero? Just return the original chain.
9416 if (ConstantSize->isZero())
9417 return Chain;
9418
9420 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9421 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9422 if (Result.getNode())
9423 return Result;
9424 }
9425
9426 // Then check to see if we should lower the memmove with target-specific
9427 // code. If the target chooses to do this, this is the next best.
9428 if (TSI) {
9429 SDValue Result =
9430 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9431 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9432 if (Result.getNode())
9433 return Result;
9434 }
9435
9438
9439 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9440 // not be safe. See memcpy above for more details.
9441
9442 // Emit a library call.
9445 Args.emplace_back(Dst, PtrTy);
9446 Args.emplace_back(Src, PtrTy);
9447 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9448 // FIXME: pass in SDLoc
9450
9451 RTLIB::LibcallImpl MemmoveImpl = getLibcalls().getLibcallImpl(RTLIB::MEMMOVE);
9452
9453 bool IsTailCall = false;
9454 if (OverrideTailCall.has_value()) {
9455 IsTailCall = *OverrideTailCall;
9456 } else {
9457 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9458 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9459 }
9460
9461 CLI.setDebugLoc(dl)
9462 .setChain(Chain)
9463 .setLibCallee(
9464 getLibcalls().getLibcallImplCallingConv(MemmoveImpl),
9465 Dst.getValueType().getTypeForEVT(*getContext()),
9466 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
9467 std::move(Args))
9469 .setTailCall(IsTailCall);
9470
9471 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9472 return CallResult.second;
9473}
9474
9476 SDValue Dst, SDValue Src, SDValue Size,
9477 Type *SizeTy, unsigned ElemSz,
9478 bool isTailCall,
9479 MachinePointerInfo DstPtrInfo,
9480 MachinePointerInfo SrcPtrInfo) {
9481 // Emit a library call.
9483 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9484 Args.emplace_back(Dst, IntPtrTy);
9485 Args.emplace_back(Src, IntPtrTy);
9486 Args.emplace_back(Size, SizeTy);
9487
9488 RTLIB::Libcall LibraryCall =
9490 RTLIB::LibcallImpl LibcallImpl = getLibcalls().getLibcallImpl(LibraryCall);
9491 if (LibcallImpl == RTLIB::Unsupported)
9492 report_fatal_error("Unsupported element size");
9493
9495 CLI.setDebugLoc(dl)
9496 .setChain(Chain)
9497 .setLibCallee(
9498 getLibcalls().getLibcallImplCallingConv(LibcallImpl),
9500 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9501 std::move(Args))
9503 .setTailCall(isTailCall);
9504
9505 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9506 return CallResult.second;
9507}
9508
9510 SDValue Src, SDValue Size, Align Alignment,
9511 bool isVol, bool AlwaysInline,
9512 const CallInst *CI,
9513 MachinePointerInfo DstPtrInfo,
9514 const AAMDNodes &AAInfo) {
9515 // Check to see if we should lower the memset to stores first.
9516 // For cases within the target-specified limits, this is the best choice.
9518 if (ConstantSize) {
9519 // Memset with size zero? Just return the original chain.
9520 if (ConstantSize->isZero())
9521 return Chain;
9522
9523 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9524 ConstantSize->getZExtValue(), Alignment,
9525 isVol, false, DstPtrInfo, AAInfo);
9526
9527 if (Result.getNode())
9528 return Result;
9529 }
9530
9531 // Then check to see if we should lower the memset with target-specific
9532 // code. If the target chooses to do this, this is the next best.
9533 if (TSI) {
9534 SDValue Result = TSI->EmitTargetCodeForMemset(
9535 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9536 if (Result.getNode())
9537 return Result;
9538 }
9539
9540 // If we really need inline code and the target declined to provide it,
9541 // use a (potentially long) sequence of loads and stores.
9542 if (AlwaysInline) {
9543 assert(ConstantSize && "AlwaysInline requires a constant size!");
9544 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9545 ConstantSize->getZExtValue(), Alignment,
9546 isVol, true, DstPtrInfo, AAInfo);
9547 assert(Result &&
9548 "getMemsetStores must return a valid sequence when AlwaysInline");
9549 return Result;
9550 }
9551
9553
9554 // Emit a library call.
9555 auto &Ctx = *getContext();
9556 const auto& DL = getDataLayout();
9557
9559 // FIXME: pass in SDLoc
9560 CLI.setDebugLoc(dl).setChain(Chain);
9561
9562 RTLIB::LibcallImpl BzeroImpl = TLI->getLibcallImpl(RTLIB::BZERO);
9563 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
9564
9565 // If zeroing out and bzero is present, use it.
9566 if (UseBZero) {
9568 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9569 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9570 CLI.setLibCallee(
9571 TLI->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
9572 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
9573 } else {
9574 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9575
9577 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9578 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9579 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9580 CLI.setLibCallee(TLI->getLibcallImplCallingConv(MemsetImpl),
9581 Dst.getValueType().getTypeForEVT(Ctx),
9582 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
9583 std::move(Args));
9584 }
9585
9586 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9587 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9588
9589 // If we're going to use bzero, make sure not to tail call unless the
9590 // subsequent return doesn't need a value, as bzero doesn't return the first
9591 // arg unlike memset.
9592 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9593 bool IsTailCall =
9594 CI && CI->isTailCall() &&
9595 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9596 CLI.setDiscardResult().setTailCall(IsTailCall);
9597
9598 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9599 return CallResult.second;
9600}
9601
9604 Type *SizeTy, unsigned ElemSz,
9605 bool isTailCall,
9606 MachinePointerInfo DstPtrInfo) {
9607 // Emit a library call.
9609 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9610 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9611 Args.emplace_back(Size, SizeTy);
9612
9613 RTLIB::Libcall LibraryCall =
9615 RTLIB::LibcallImpl LibcallImpl = TLI->getLibcallImpl(LibraryCall);
9616 if (LibcallImpl == RTLIB::Unsupported)
9617 report_fatal_error("Unsupported element size");
9618
9620 CLI.setDebugLoc(dl)
9621 .setChain(Chain)
9622 .setLibCallee(
9623 TLI->getLibcallImplCallingConv(LibcallImpl),
9625 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9626 std::move(Args))
9628 .setTailCall(isTailCall);
9629
9630 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9631 return CallResult.second;
9632}
9633
9634SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9636 MachineMemOperand *MMO,
9637 ISD::LoadExtType ExtType) {
9639 AddNodeIDNode(ID, Opcode, VTList, Ops);
9640 ID.AddInteger(MemVT.getRawBits());
9641 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9642 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9643 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9644 ID.AddInteger(MMO->getFlags());
9645 void* IP = nullptr;
9646 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9647 E->refineAlignment(MMO);
9648 E->refineRanges(MMO);
9649 return SDValue(E, 0);
9650 }
9651
9652 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9653 VTList, MemVT, MMO, ExtType);
9654 createOperands(N, Ops);
9655
9656 CSEMap.InsertNode(N, IP);
9657 InsertNode(N);
9658 SDValue V(N, 0);
9659 NewSDValueDbgMsg(V, "Creating new node: ", this);
9660 return V;
9661}
9662
9664 EVT MemVT, SDVTList VTs, SDValue Chain,
9665 SDValue Ptr, SDValue Cmp, SDValue Swp,
9666 MachineMemOperand *MMO) {
9667 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9669 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9670
9671 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9672 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9673}
9674
9675SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9676 SDValue Chain, SDValue Ptr, SDValue Val,
9677 MachineMemOperand *MMO) {
9678 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9679 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9680 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9681 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9682 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9683 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9684 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9685 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9686 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9687 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9688 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9689 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9690 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9691 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9692 Opcode == ISD::ATOMIC_STORE) &&
9693 "Invalid Atomic Op");
9694
9695 EVT VT = Val.getValueType();
9696
9697 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9698 getVTList(VT, MVT::Other);
9699 SDValue Ops[] = {Chain, Ptr, Val};
9700 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9701}
9702
9704 EVT MemVT, EVT VT, SDValue Chain,
9705 SDValue Ptr, MachineMemOperand *MMO) {
9706 SDVTList VTs = getVTList(VT, MVT::Other);
9707 SDValue Ops[] = {Chain, Ptr};
9708 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9709}
9710
9711/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9713 if (Ops.size() == 1)
9714 return Ops[0];
9715
9717 VTs.reserve(Ops.size());
9718 for (const SDValue &Op : Ops)
9719 VTs.push_back(Op.getValueType());
9720 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9721}
9722
9724 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9725 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9727 const AAMDNodes &AAInfo) {
9728 if (Size.hasValue() && !Size.getValue())
9730
9732 MachineMemOperand *MMO =
9733 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9734
9735 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9736}
9737
9739 SDVTList VTList,
9740 ArrayRef<SDValue> Ops, EVT MemVT,
9741 MachineMemOperand *MMO) {
9742 assert(
9743 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9744 Opcode == ISD::PREFETCH ||
9745 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9746 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9747 "Opcode is not a memory-accessing opcode!");
9748
9749 // Memoize the node unless it returns a glue result.
9751 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9753 AddNodeIDNode(ID, Opcode, VTList, Ops);
9754 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9755 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9756 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9757 ID.AddInteger(MMO->getFlags());
9758 ID.AddInteger(MemVT.getRawBits());
9759 void *IP = nullptr;
9760 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9761 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9762 return SDValue(E, 0);
9763 }
9764
9765 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9766 VTList, MemVT, MMO);
9767 createOperands(N, Ops);
9768
9769 CSEMap.InsertNode(N, IP);
9770 } else {
9771 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9772 VTList, MemVT, MMO);
9773 createOperands(N, Ops);
9774 }
9775 InsertNode(N);
9776 SDValue V(N, 0);
9777 NewSDValueDbgMsg(V, "Creating new node: ", this);
9778 return V;
9779}
9780
9782 SDValue Chain, int FrameIndex) {
9783 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9784 const auto VTs = getVTList(MVT::Other);
9785 SDValue Ops[2] = {
9786 Chain,
9787 getFrameIndex(FrameIndex,
9788 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9789 true)};
9790
9792 AddNodeIDNode(ID, Opcode, VTs, Ops);
9793 ID.AddInteger(FrameIndex);
9794 void *IP = nullptr;
9795 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9796 return SDValue(E, 0);
9797
9798 LifetimeSDNode *N =
9799 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9800 createOperands(N, Ops);
9801 CSEMap.InsertNode(N, IP);
9802 InsertNode(N);
9803 SDValue V(N, 0);
9804 NewSDValueDbgMsg(V, "Creating new node: ", this);
9805 return V;
9806}
9807
9809 uint64_t Guid, uint64_t Index,
9810 uint32_t Attr) {
9811 const unsigned Opcode = ISD::PSEUDO_PROBE;
9812 const auto VTs = getVTList(MVT::Other);
9813 SDValue Ops[] = {Chain};
9815 AddNodeIDNode(ID, Opcode, VTs, Ops);
9816 ID.AddInteger(Guid);
9817 ID.AddInteger(Index);
9818 void *IP = nullptr;
9819 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9820 return SDValue(E, 0);
9821
9822 auto *N = newSDNode<PseudoProbeSDNode>(
9823 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9824 createOperands(N, Ops);
9825 CSEMap.InsertNode(N, IP);
9826 InsertNode(N);
9827 SDValue V(N, 0);
9828 NewSDValueDbgMsg(V, "Creating new node: ", this);
9829 return V;
9830}
9831
9832/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9833/// MachinePointerInfo record from it. This is particularly useful because the
9834/// code generator has many cases where it doesn't bother passing in a
9835/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9837 SelectionDAG &DAG, SDValue Ptr,
9838 int64_t Offset = 0) {
9839 // If this is FI+Offset, we can model it.
9840 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9842 FI->getIndex(), Offset);
9843
9844 // If this is (FI+Offset1)+Offset2, we can model it.
9845 if (Ptr.getOpcode() != ISD::ADD ||
9848 return Info;
9849
9850 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9852 DAG.getMachineFunction(), FI,
9853 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9854}
9855
9856/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9857/// MachinePointerInfo record from it. This is particularly useful because the
9858/// code generator has many cases where it doesn't bother passing in a
9859/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9861 SelectionDAG &DAG, SDValue Ptr,
9862 SDValue OffsetOp) {
9863 // If the 'Offset' value isn't a constant, we can't handle this.
9865 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9866 if (OffsetOp.isUndef())
9867 return InferPointerInfo(Info, DAG, Ptr);
9868 return Info;
9869}
9870
9872 EVT VT, const SDLoc &dl, SDValue Chain,
9873 SDValue Ptr, SDValue Offset,
9874 MachinePointerInfo PtrInfo, EVT MemVT,
9875 Align Alignment,
9876 MachineMemOperand::Flags MMOFlags,
9877 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9878 assert(Chain.getValueType() == MVT::Other &&
9879 "Invalid chain type");
9880
9881 MMOFlags |= MachineMemOperand::MOLoad;
9882 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9883 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9884 // clients.
9885 if (PtrInfo.V.isNull())
9886 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9887
9888 TypeSize Size = MemVT.getStoreSize();
9890 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9891 Alignment, AAInfo, Ranges);
9892 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9893}
9894
9896 EVT VT, const SDLoc &dl, SDValue Chain,
9897 SDValue Ptr, SDValue Offset, EVT MemVT,
9898 MachineMemOperand *MMO) {
9899 if (VT == MemVT) {
9900 ExtType = ISD::NON_EXTLOAD;
9901 } else if (ExtType == ISD::NON_EXTLOAD) {
9902 assert(VT == MemVT && "Non-extending load from different memory type!");
9903 } else {
9904 // Extending load.
9905 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9906 "Should only be an extending load, not truncating!");
9907 assert(VT.isInteger() == MemVT.isInteger() &&
9908 "Cannot convert from FP to Int or Int -> FP!");
9909 assert(VT.isVector() == MemVT.isVector() &&
9910 "Cannot use an ext load to convert to or from a vector!");
9911 assert((!VT.isVector() ||
9913 "Cannot use an ext load to change the number of vector elements!");
9914 }
9915
9916 assert((!MMO->getRanges() ||
9918 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9919 MemVT.isInteger())) &&
9920 "Range metadata and load type must match!");
9921
9922 bool Indexed = AM != ISD::UNINDEXED;
9923 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9924
9925 SDVTList VTs = Indexed ?
9926 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9927 SDValue Ops[] = { Chain, Ptr, Offset };
9929 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9930 ID.AddInteger(MemVT.getRawBits());
9931 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9932 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9933 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9934 ID.AddInteger(MMO->getFlags());
9935 void *IP = nullptr;
9936 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9937 E->refineAlignment(MMO);
9938 E->refineRanges(MMO);
9939 return SDValue(E, 0);
9940 }
9941 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9942 ExtType, MemVT, MMO);
9943 createOperands(N, Ops);
9944
9945 CSEMap.InsertNode(N, IP);
9946 InsertNode(N);
9947 SDValue V(N, 0);
9948 NewSDValueDbgMsg(V, "Creating new node: ", this);
9949 return V;
9950}
9951
9953 SDValue Ptr, MachinePointerInfo PtrInfo,
9954 MaybeAlign Alignment,
9955 MachineMemOperand::Flags MMOFlags,
9956 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9957 SDValue Undef = getUNDEF(Ptr.getValueType());
9958 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9959 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9960}
9961
9963 SDValue Ptr, MachineMemOperand *MMO) {
9964 SDValue Undef = getUNDEF(Ptr.getValueType());
9965 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9966 VT, MMO);
9967}
9968
9970 EVT VT, SDValue Chain, SDValue Ptr,
9971 MachinePointerInfo PtrInfo, EVT MemVT,
9972 MaybeAlign Alignment,
9973 MachineMemOperand::Flags MMOFlags,
9974 const AAMDNodes &AAInfo) {
9975 SDValue Undef = getUNDEF(Ptr.getValueType());
9976 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9977 MemVT, Alignment, MMOFlags, AAInfo);
9978}
9979
9981 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9982 MachineMemOperand *MMO) {
9983 SDValue Undef = getUNDEF(Ptr.getValueType());
9984 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9985 MemVT, MMO);
9986}
9987
9991 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9992 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9993 // Don't propagate the invariant or dereferenceable flags.
9994 auto MMOFlags =
9995 LD->getMemOperand()->getFlags() &
9997 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9998 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9999 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
10000}
10001
10003 SDValue Ptr, MachinePointerInfo PtrInfo,
10004 Align Alignment,
10005 MachineMemOperand::Flags MMOFlags,
10006 const AAMDNodes &AAInfo) {
10007 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10008
10009 MMOFlags |= MachineMemOperand::MOStore;
10010 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10011
10012 if (PtrInfo.V.isNull())
10013 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10014
10017 MachineMemOperand *MMO =
10018 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
10019 return getStore(Chain, dl, Val, Ptr, MMO);
10020}
10021
10023 SDValue Ptr, MachineMemOperand *MMO) {
10024 SDValue Undef = getUNDEF(Ptr.getValueType());
10025 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
10027}
10028
10030 SDValue Ptr, SDValue Offset, EVT SVT,
10032 bool IsTruncating) {
10033 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10034 EVT VT = Val.getValueType();
10035 if (VT == SVT) {
10036 IsTruncating = false;
10037 } else if (!IsTruncating) {
10038 assert(VT == SVT && "No-truncating store from different memory type!");
10039 } else {
10041 "Should only be a truncating store, not extending!");
10042 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10043 assert(VT.isVector() == SVT.isVector() &&
10044 "Cannot use trunc store to convert to or from a vector!");
10045 assert((!VT.isVector() ||
10047 "Cannot use trunc store to change the number of vector elements!");
10048 }
10049
10050 bool Indexed = AM != ISD::UNINDEXED;
10051 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
10052 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10053 : getVTList(MVT::Other);
10054 SDValue Ops[] = {Chain, Val, Ptr, Offset};
10057 ID.AddInteger(SVT.getRawBits());
10058 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
10059 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
10060 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10061 ID.AddInteger(MMO->getFlags());
10062 void *IP = nullptr;
10063 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10064 cast<StoreSDNode>(E)->refineAlignment(MMO);
10065 return SDValue(E, 0);
10066 }
10067 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10068 IsTruncating, SVT, MMO);
10069 createOperands(N, Ops);
10070
10071 CSEMap.InsertNode(N, IP);
10072 InsertNode(N);
10073 SDValue V(N, 0);
10074 NewSDValueDbgMsg(V, "Creating new node: ", this);
10075 return V;
10076}
10077
10079 SDValue Ptr, MachinePointerInfo PtrInfo,
10080 EVT SVT, Align Alignment,
10081 MachineMemOperand::Flags MMOFlags,
10082 const AAMDNodes &AAInfo) {
10083 assert(Chain.getValueType() == MVT::Other &&
10084 "Invalid chain type");
10085
10086 MMOFlags |= MachineMemOperand::MOStore;
10087 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10088
10089 if (PtrInfo.V.isNull())
10090 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10091
10093 MachineMemOperand *MMO = MF.getMachineMemOperand(
10094 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10095 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10096}
10097
10099 SDValue Ptr, EVT SVT,
10100 MachineMemOperand *MMO) {
10101 SDValue Undef = getUNDEF(Ptr.getValueType());
10102 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10103}
10104
10108 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10109 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10110 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10111 ST->getMemoryVT(), ST->getMemOperand(), AM,
10112 ST->isTruncatingStore());
10113}
10114
10116 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10117 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10118 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10119 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10120 const MDNode *Ranges, bool IsExpanding) {
10121 MMOFlags |= MachineMemOperand::MOLoad;
10122 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10123 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10124 // clients.
10125 if (PtrInfo.V.isNull())
10126 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10127
10128 TypeSize Size = MemVT.getStoreSize();
10130 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10131 Alignment, AAInfo, Ranges);
10132 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10133 MMO, IsExpanding);
10134}
10135
10137 ISD::LoadExtType ExtType, EVT VT,
10138 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10139 SDValue Offset, SDValue Mask, SDValue EVL,
10140 EVT MemVT, MachineMemOperand *MMO,
10141 bool IsExpanding) {
10142 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10143 assert(Mask.getValueType().getVectorElementCount() ==
10144 VT.getVectorElementCount() &&
10145 "Vector width mismatch between mask and data");
10146
10147 bool Indexed = AM != ISD::UNINDEXED;
10148 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10149
10150 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10151 : getVTList(VT, MVT::Other);
10152 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10154 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10155 ID.AddInteger(MemVT.getRawBits());
10156 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10157 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10158 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10159 ID.AddInteger(MMO->getFlags());
10160 void *IP = nullptr;
10161 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10162 E->refineAlignment(MMO);
10163 E->refineRanges(MMO);
10164 return SDValue(E, 0);
10165 }
10166 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10167 ExtType, IsExpanding, MemVT, MMO);
10168 createOperands(N, Ops);
10169
10170 CSEMap.InsertNode(N, IP);
10171 InsertNode(N);
10172 SDValue V(N, 0);
10173 NewSDValueDbgMsg(V, "Creating new node: ", this);
10174 return V;
10175}
10176
10178 SDValue Ptr, SDValue Mask, SDValue EVL,
10179 MachinePointerInfo PtrInfo,
10180 MaybeAlign Alignment,
10181 MachineMemOperand::Flags MMOFlags,
10182 const AAMDNodes &AAInfo, const MDNode *Ranges,
10183 bool IsExpanding) {
10184 SDValue Undef = getUNDEF(Ptr.getValueType());
10185 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10186 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10187 IsExpanding);
10188}
10189
10191 SDValue Ptr, SDValue Mask, SDValue EVL,
10192 MachineMemOperand *MMO, bool IsExpanding) {
10193 SDValue Undef = getUNDEF(Ptr.getValueType());
10194 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10195 Mask, EVL, VT, MMO, IsExpanding);
10196}
10197
10199 EVT VT, SDValue Chain, SDValue Ptr,
10200 SDValue Mask, SDValue EVL,
10201 MachinePointerInfo PtrInfo, EVT MemVT,
10202 MaybeAlign Alignment,
10203 MachineMemOperand::Flags MMOFlags,
10204 const AAMDNodes &AAInfo, bool IsExpanding) {
10205 SDValue Undef = getUNDEF(Ptr.getValueType());
10206 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10207 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10208 IsExpanding);
10209}
10210
10212 EVT VT, SDValue Chain, SDValue Ptr,
10213 SDValue Mask, SDValue EVL, EVT MemVT,
10214 MachineMemOperand *MMO, bool IsExpanding) {
10215 SDValue Undef = getUNDEF(Ptr.getValueType());
10216 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10217 EVL, MemVT, MMO, IsExpanding);
10218}
10219
10223 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10224 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10225 // Don't propagate the invariant or dereferenceable flags.
10226 auto MMOFlags =
10227 LD->getMemOperand()->getFlags() &
10229 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10230 LD->getChain(), Base, Offset, LD->getMask(),
10231 LD->getVectorLength(), LD->getPointerInfo(),
10232 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10233 nullptr, LD->isExpandingLoad());
10234}
10235
10237 SDValue Ptr, SDValue Offset, SDValue Mask,
10238 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10239 ISD::MemIndexedMode AM, bool IsTruncating,
10240 bool IsCompressing) {
10241 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10242 assert(Mask.getValueType().getVectorElementCount() ==
10244 "Vector width mismatch between mask and data");
10245
10246 bool Indexed = AM != ISD::UNINDEXED;
10247 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10248 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10249 : getVTList(MVT::Other);
10250 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10252 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10253 ID.AddInteger(MemVT.getRawBits());
10254 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10255 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10256 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10257 ID.AddInteger(MMO->getFlags());
10258 void *IP = nullptr;
10259 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10260 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10261 return SDValue(E, 0);
10262 }
10263 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10264 IsTruncating, IsCompressing, MemVT, MMO);
10265 createOperands(N, Ops);
10266
10267 CSEMap.InsertNode(N, IP);
10268 InsertNode(N);
10269 SDValue V(N, 0);
10270 NewSDValueDbgMsg(V, "Creating new node: ", this);
10271 return V;
10272}
10273
10275 SDValue Val, SDValue Ptr, SDValue Mask,
10276 SDValue EVL, MachinePointerInfo PtrInfo,
10277 EVT SVT, Align Alignment,
10278 MachineMemOperand::Flags MMOFlags,
10279 const AAMDNodes &AAInfo,
10280 bool IsCompressing) {
10281 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10282
10283 MMOFlags |= MachineMemOperand::MOStore;
10284 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10285
10286 if (PtrInfo.V.isNull())
10287 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10288
10290 MachineMemOperand *MMO = MF.getMachineMemOperand(
10291 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10292 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10293 IsCompressing);
10294}
10295
10297 SDValue Val, SDValue Ptr, SDValue Mask,
10298 SDValue EVL, EVT SVT,
10299 MachineMemOperand *MMO,
10300 bool IsCompressing) {
10301 EVT VT = Val.getValueType();
10302
10303 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10304 if (VT == SVT)
10305 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10306 EVL, VT, MMO, ISD::UNINDEXED,
10307 /*IsTruncating*/ false, IsCompressing);
10308
10310 "Should only be a truncating store, not extending!");
10311 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10312 assert(VT.isVector() == SVT.isVector() &&
10313 "Cannot use trunc store to convert to or from a vector!");
10314 assert((!VT.isVector() ||
10316 "Cannot use trunc store to change the number of vector elements!");
10317
10318 SDVTList VTs = getVTList(MVT::Other);
10319 SDValue Undef = getUNDEF(Ptr.getValueType());
10320 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10322 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10323 ID.AddInteger(SVT.getRawBits());
10324 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10325 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10326 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10327 ID.AddInteger(MMO->getFlags());
10328 void *IP = nullptr;
10329 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10330 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10331 return SDValue(E, 0);
10332 }
10333 auto *N =
10334 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10335 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10336 createOperands(N, Ops);
10337
10338 CSEMap.InsertNode(N, IP);
10339 InsertNode(N);
10340 SDValue V(N, 0);
10341 NewSDValueDbgMsg(V, "Creating new node: ", this);
10342 return V;
10343}
10344
10348 auto *ST = cast<VPStoreSDNode>(OrigStore);
10349 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10350 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10351 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10352 Offset, ST->getMask(), ST->getVectorLength()};
10354 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10355 ID.AddInteger(ST->getMemoryVT().getRawBits());
10356 ID.AddInteger(ST->getRawSubclassData());
10357 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10358 ID.AddInteger(ST->getMemOperand()->getFlags());
10359 void *IP = nullptr;
10360 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10361 return SDValue(E, 0);
10362
10363 auto *N = newSDNode<VPStoreSDNode>(
10364 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10365 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10366 createOperands(N, Ops);
10367
10368 CSEMap.InsertNode(N, IP);
10369 InsertNode(N);
10370 SDValue V(N, 0);
10371 NewSDValueDbgMsg(V, "Creating new node: ", this);
10372 return V;
10373}
10374
10376 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10377 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10378 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10379 bool Indexed = AM != ISD::UNINDEXED;
10380 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10381
10382 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10383 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10384 : getVTList(VT, MVT::Other);
10386 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10387 ID.AddInteger(VT.getRawBits());
10388 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10389 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10390 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10391
10392 void *IP = nullptr;
10393 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10394 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10395 return SDValue(E, 0);
10396 }
10397
10398 auto *N =
10399 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10400 ExtType, IsExpanding, MemVT, MMO);
10401 createOperands(N, Ops);
10402 CSEMap.InsertNode(N, IP);
10403 InsertNode(N);
10404 SDValue V(N, 0);
10405 NewSDValueDbgMsg(V, "Creating new node: ", this);
10406 return V;
10407}
10408
10410 SDValue Ptr, SDValue Stride,
10411 SDValue Mask, SDValue EVL,
10412 MachineMemOperand *MMO,
10413 bool IsExpanding) {
10414 SDValue Undef = getUNDEF(Ptr.getValueType());
10415 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10416 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10417}
10418
10420 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10421 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10422 MachineMemOperand *MMO, bool IsExpanding) {
10423 SDValue Undef = getUNDEF(Ptr.getValueType());
10424 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10425 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10426}
10427
10429 SDValue Val, SDValue Ptr,
10430 SDValue Offset, SDValue Stride,
10431 SDValue Mask, SDValue EVL, EVT MemVT,
10432 MachineMemOperand *MMO,
10434 bool IsTruncating, bool IsCompressing) {
10435 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10436 bool Indexed = AM != ISD::UNINDEXED;
10437 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10438 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10439 : getVTList(MVT::Other);
10440 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10442 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10443 ID.AddInteger(MemVT.getRawBits());
10444 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10445 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10446 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10447 void *IP = nullptr;
10448 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10449 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10450 return SDValue(E, 0);
10451 }
10452 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10453 VTs, AM, IsTruncating,
10454 IsCompressing, MemVT, MMO);
10455 createOperands(N, Ops);
10456
10457 CSEMap.InsertNode(N, IP);
10458 InsertNode(N);
10459 SDValue V(N, 0);
10460 NewSDValueDbgMsg(V, "Creating new node: ", this);
10461 return V;
10462}
10463
10465 SDValue Val, SDValue Ptr,
10466 SDValue Stride, SDValue Mask,
10467 SDValue EVL, EVT SVT,
10468 MachineMemOperand *MMO,
10469 bool IsCompressing) {
10470 EVT VT = Val.getValueType();
10471
10472 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10473 if (VT == SVT)
10474 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10475 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10476 /*IsTruncating*/ false, IsCompressing);
10477
10479 "Should only be a truncating store, not extending!");
10480 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10481 assert(VT.isVector() == SVT.isVector() &&
10482 "Cannot use trunc store to convert to or from a vector!");
10483 assert((!VT.isVector() ||
10485 "Cannot use trunc store to change the number of vector elements!");
10486
10487 SDVTList VTs = getVTList(MVT::Other);
10488 SDValue Undef = getUNDEF(Ptr.getValueType());
10489 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10491 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10492 ID.AddInteger(SVT.getRawBits());
10493 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10494 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10495 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10496 void *IP = nullptr;
10497 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10498 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10499 return SDValue(E, 0);
10500 }
10501 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10502 VTs, ISD::UNINDEXED, true,
10503 IsCompressing, SVT, MMO);
10504 createOperands(N, Ops);
10505
10506 CSEMap.InsertNode(N, IP);
10507 InsertNode(N);
10508 SDValue V(N, 0);
10509 NewSDValueDbgMsg(V, "Creating new node: ", this);
10510 return V;
10511}
10512
10515 ISD::MemIndexType IndexType) {
10516 assert(Ops.size() == 6 && "Incompatible number of operands");
10517
10519 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10520 ID.AddInteger(VT.getRawBits());
10521 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10522 dl.getIROrder(), VTs, VT, MMO, IndexType));
10523 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10524 ID.AddInteger(MMO->getFlags());
10525 void *IP = nullptr;
10526 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10527 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10528 return SDValue(E, 0);
10529 }
10530
10531 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10532 VT, MMO, IndexType);
10533 createOperands(N, Ops);
10534
10535 assert(N->getMask().getValueType().getVectorElementCount() ==
10536 N->getValueType(0).getVectorElementCount() &&
10537 "Vector width mismatch between mask and data");
10538 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10539 N->getValueType(0).getVectorElementCount().isScalable() &&
10540 "Scalable flags of index and data do not match");
10542 N->getIndex().getValueType().getVectorElementCount(),
10543 N->getValueType(0).getVectorElementCount()) &&
10544 "Vector width mismatch between index and data");
10545 assert(isa<ConstantSDNode>(N->getScale()) &&
10546 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10547 "Scale should be a constant power of 2");
10548
10549 CSEMap.InsertNode(N, IP);
10550 InsertNode(N);
10551 SDValue V(N, 0);
10552 NewSDValueDbgMsg(V, "Creating new node: ", this);
10553 return V;
10554}
10555
10558 MachineMemOperand *MMO,
10559 ISD::MemIndexType IndexType) {
10560 assert(Ops.size() == 7 && "Incompatible number of operands");
10561
10563 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10564 ID.AddInteger(VT.getRawBits());
10565 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10566 dl.getIROrder(), VTs, VT, MMO, IndexType));
10567 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10568 ID.AddInteger(MMO->getFlags());
10569 void *IP = nullptr;
10570 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10571 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10572 return SDValue(E, 0);
10573 }
10574 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10575 VT, MMO, IndexType);
10576 createOperands(N, Ops);
10577
10578 assert(N->getMask().getValueType().getVectorElementCount() ==
10579 N->getValue().getValueType().getVectorElementCount() &&
10580 "Vector width mismatch between mask and data");
10581 assert(
10582 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10583 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10584 "Scalable flags of index and data do not match");
10586 N->getIndex().getValueType().getVectorElementCount(),
10587 N->getValue().getValueType().getVectorElementCount()) &&
10588 "Vector width mismatch between index and data");
10589 assert(isa<ConstantSDNode>(N->getScale()) &&
10590 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10591 "Scale should be a constant power of 2");
10592
10593 CSEMap.InsertNode(N, IP);
10594 InsertNode(N);
10595 SDValue V(N, 0);
10596 NewSDValueDbgMsg(V, "Creating new node: ", this);
10597 return V;
10598}
10599
10602 SDValue PassThru, EVT MemVT,
10603 MachineMemOperand *MMO,
10605 ISD::LoadExtType ExtTy, bool isExpanding) {
10606 bool Indexed = AM != ISD::UNINDEXED;
10607 assert((Indexed || Offset.isUndef()) &&
10608 "Unindexed masked load with an offset!");
10609 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10610 : getVTList(VT, MVT::Other);
10611 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10614 ID.AddInteger(MemVT.getRawBits());
10615 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10616 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10617 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10618 ID.AddInteger(MMO->getFlags());
10619 void *IP = nullptr;
10620 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10621 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10622 return SDValue(E, 0);
10623 }
10624 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10625 AM, ExtTy, isExpanding, MemVT, MMO);
10626 createOperands(N, Ops);
10627
10628 CSEMap.InsertNode(N, IP);
10629 InsertNode(N);
10630 SDValue V(N, 0);
10631 NewSDValueDbgMsg(V, "Creating new node: ", this);
10632 return V;
10633}
10634
10639 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10640 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10641 Offset, LD->getMask(), LD->getPassThru(),
10642 LD->getMemoryVT(), LD->getMemOperand(), AM,
10643 LD->getExtensionType(), LD->isExpandingLoad());
10644}
10645
10648 SDValue Mask, EVT MemVT,
10649 MachineMemOperand *MMO,
10650 ISD::MemIndexedMode AM, bool IsTruncating,
10651 bool IsCompressing) {
10652 assert(Chain.getValueType() == MVT::Other &&
10653 "Invalid chain type");
10654 bool Indexed = AM != ISD::UNINDEXED;
10655 assert((Indexed || Offset.isUndef()) &&
10656 "Unindexed masked store with an offset!");
10657 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10658 : getVTList(MVT::Other);
10659 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10662 ID.AddInteger(MemVT.getRawBits());
10663 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10664 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10665 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10666 ID.AddInteger(MMO->getFlags());
10667 void *IP = nullptr;
10668 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10669 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10670 return SDValue(E, 0);
10671 }
10672 auto *N =
10673 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10674 IsTruncating, IsCompressing, MemVT, MMO);
10675 createOperands(N, Ops);
10676
10677 CSEMap.InsertNode(N, IP);
10678 InsertNode(N);
10679 SDValue V(N, 0);
10680 NewSDValueDbgMsg(V, "Creating new node: ", this);
10681 return V;
10682}
10683
10688 assert(ST->getOffset().isUndef() &&
10689 "Masked store is already a indexed store!");
10690 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10691 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10692 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10693}
10694
10697 MachineMemOperand *MMO,
10698 ISD::MemIndexType IndexType,
10699 ISD::LoadExtType ExtTy) {
10700 assert(Ops.size() == 6 && "Incompatible number of operands");
10701
10704 ID.AddInteger(MemVT.getRawBits());
10705 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10706 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10707 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10708 ID.AddInteger(MMO->getFlags());
10709 void *IP = nullptr;
10710 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10711 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10712 return SDValue(E, 0);
10713 }
10714
10715 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10716 VTs, MemVT, MMO, IndexType, ExtTy);
10717 createOperands(N, Ops);
10718
10719 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10720 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10721 assert(N->getMask().getValueType().getVectorElementCount() ==
10722 N->getValueType(0).getVectorElementCount() &&
10723 "Vector width mismatch between mask and data");
10724 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10725 N->getValueType(0).getVectorElementCount().isScalable() &&
10726 "Scalable flags of index and data do not match");
10728 N->getIndex().getValueType().getVectorElementCount(),
10729 N->getValueType(0).getVectorElementCount()) &&
10730 "Vector width mismatch between index and data");
10731 assert(isa<ConstantSDNode>(N->getScale()) &&
10732 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10733 "Scale should be a constant power of 2");
10734
10735 CSEMap.InsertNode(N, IP);
10736 InsertNode(N);
10737 SDValue V(N, 0);
10738 NewSDValueDbgMsg(V, "Creating new node: ", this);
10739 return V;
10740}
10741
10744 MachineMemOperand *MMO,
10745 ISD::MemIndexType IndexType,
10746 bool IsTrunc) {
10747 assert(Ops.size() == 6 && "Incompatible number of operands");
10748
10751 ID.AddInteger(MemVT.getRawBits());
10752 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10753 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10754 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10755 ID.AddInteger(MMO->getFlags());
10756 void *IP = nullptr;
10757 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10758 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10759 return SDValue(E, 0);
10760 }
10761
10762 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10763 VTs, MemVT, MMO, IndexType, IsTrunc);
10764 createOperands(N, Ops);
10765
10766 assert(N->getMask().getValueType().getVectorElementCount() ==
10767 N->getValue().getValueType().getVectorElementCount() &&
10768 "Vector width mismatch between mask and data");
10769 assert(
10770 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10771 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10772 "Scalable flags of index and data do not match");
10774 N->getIndex().getValueType().getVectorElementCount(),
10775 N->getValue().getValueType().getVectorElementCount()) &&
10776 "Vector width mismatch between index and data");
10777 assert(isa<ConstantSDNode>(N->getScale()) &&
10778 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10779 "Scale should be a constant power of 2");
10780
10781 CSEMap.InsertNode(N, IP);
10782 InsertNode(N);
10783 SDValue V(N, 0);
10784 NewSDValueDbgMsg(V, "Creating new node: ", this);
10785 return V;
10786}
10787
10789 const SDLoc &dl, ArrayRef<SDValue> Ops,
10790 MachineMemOperand *MMO,
10791 ISD::MemIndexType IndexType) {
10792 assert(Ops.size() == 7 && "Incompatible number of operands");
10793
10796 ID.AddInteger(MemVT.getRawBits());
10797 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10798 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10799 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10800 ID.AddInteger(MMO->getFlags());
10801 void *IP = nullptr;
10802 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10803 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10804 return SDValue(E, 0);
10805 }
10806
10807 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10808 VTs, MemVT, MMO, IndexType);
10809 createOperands(N, Ops);
10810
10811 assert(N->getMask().getValueType().getVectorElementCount() ==
10812 N->getIndex().getValueType().getVectorElementCount() &&
10813 "Vector width mismatch between mask and data");
10814 assert(isa<ConstantSDNode>(N->getScale()) &&
10815 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10816 "Scale should be a constant power of 2");
10817 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10818
10819 CSEMap.InsertNode(N, IP);
10820 InsertNode(N);
10821 SDValue V(N, 0);
10822 NewSDValueDbgMsg(V, "Creating new node: ", this);
10823 return V;
10824}
10825
10827 SDValue Ptr, SDValue Mask, SDValue EVL,
10828 MachineMemOperand *MMO) {
10829 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10830 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10832 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10833 ID.AddInteger(VT.getRawBits());
10834 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10835 VTs, VT, MMO));
10836 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10837 ID.AddInteger(MMO->getFlags());
10838 void *IP = nullptr;
10839 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10840 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10841 return SDValue(E, 0);
10842 }
10843 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10844 VT, MMO);
10845 createOperands(N, Ops);
10846
10847 CSEMap.InsertNode(N, IP);
10848 InsertNode(N);
10849 SDValue V(N, 0);
10850 NewSDValueDbgMsg(V, "Creating new node: ", this);
10851 return V;
10852}
10853
10855 EVT MemVT, MachineMemOperand *MMO) {
10856 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10857 SDVTList VTs = getVTList(MVT::Other);
10858 SDValue Ops[] = {Chain, Ptr};
10861 ID.AddInteger(MemVT.getRawBits());
10862 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10863 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10864 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10865 ID.AddInteger(MMO->getFlags());
10866 void *IP = nullptr;
10867 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10868 return SDValue(E, 0);
10869
10870 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10871 dl.getDebugLoc(), VTs, MemVT, MMO);
10872 createOperands(N, Ops);
10873
10874 CSEMap.InsertNode(N, IP);
10875 InsertNode(N);
10876 SDValue V(N, 0);
10877 NewSDValueDbgMsg(V, "Creating new node: ", this);
10878 return V;
10879}
10880
10882 EVT MemVT, MachineMemOperand *MMO) {
10883 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10884 SDVTList VTs = getVTList(MVT::Other);
10885 SDValue Ops[] = {Chain, Ptr};
10888 ID.AddInteger(MemVT.getRawBits());
10889 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10890 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10891 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10892 ID.AddInteger(MMO->getFlags());
10893 void *IP = nullptr;
10894 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10895 return SDValue(E, 0);
10896
10897 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10898 dl.getDebugLoc(), VTs, MemVT, MMO);
10899 createOperands(N, Ops);
10900
10901 CSEMap.InsertNode(N, IP);
10902 InsertNode(N);
10903 SDValue V(N, 0);
10904 NewSDValueDbgMsg(V, "Creating new node: ", this);
10905 return V;
10906}
10907
10909 // select undef, T, F --> T (if T is a constant), otherwise F
10910 // select, ?, undef, F --> F
10911 // select, ?, T, undef --> T
10912 if (Cond.isUndef())
10913 return isConstantValueOfAnyType(T) ? T : F;
10914 if (T.isUndef())
10916 if (F.isUndef())
10918
10919 // select true, T, F --> T
10920 // select false, T, F --> F
10921 if (auto C = isBoolConstant(Cond))
10922 return *C ? T : F;
10923
10924 // select ?, T, T --> T
10925 if (T == F)
10926 return T;
10927
10928 return SDValue();
10929}
10930
10932 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10933 if (X.isUndef())
10934 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10935 // shift X, undef --> undef (because it may shift by the bitwidth)
10936 if (Y.isUndef())
10937 return getUNDEF(X.getValueType());
10938
10939 // shift 0, Y --> 0
10940 // shift X, 0 --> X
10942 return X;
10943
10944 // shift X, C >= bitwidth(X) --> undef
10945 // All vector elements must be too big (or undef) to avoid partial undefs.
10946 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10947 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10948 };
10949 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10950 return getUNDEF(X.getValueType());
10951
10952 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10953 if (X.getValueType().getScalarType() == MVT::i1)
10954 return X;
10955
10956 return SDValue();
10957}
10958
10960 SDNodeFlags Flags) {
10961 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10962 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10963 // operation is poison. That result can be relaxed to undef.
10964 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10965 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10966 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10967 (YC && YC->getValueAPF().isNaN());
10968 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10969 (YC && YC->getValueAPF().isInfinity());
10970
10971 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10972 return getUNDEF(X.getValueType());
10973
10974 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10975 return getUNDEF(X.getValueType());
10976
10977 if (!YC)
10978 return SDValue();
10979
10980 // X + -0.0 --> X
10981 if (Opcode == ISD::FADD)
10982 if (YC->getValueAPF().isNegZero())
10983 return X;
10984
10985 // X - +0.0 --> X
10986 if (Opcode == ISD::FSUB)
10987 if (YC->getValueAPF().isPosZero())
10988 return X;
10989
10990 // X * 1.0 --> X
10991 // X / 1.0 --> X
10992 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10993 if (YC->getValueAPF().isExactlyValue(1.0))
10994 return X;
10995
10996 // X * 0.0 --> 0.0
10997 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10998 if (YC->getValueAPF().isZero())
10999 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
11000
11001 return SDValue();
11002}
11003
11005 SDValue Ptr, SDValue SV, unsigned Align) {
11006 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
11007 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
11008}
11009
11010SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11012 switch (Ops.size()) {
11013 case 0: return getNode(Opcode, DL, VT);
11014 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
11015 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
11016 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
11017 default: break;
11018 }
11019
11020 // Copy from an SDUse array into an SDValue array for use with
11021 // the regular getNode logic.
11023 return getNode(Opcode, DL, VT, NewOps);
11024}
11025
11026SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11028 SDNodeFlags Flags;
11029 if (Inserter)
11030 Flags = Inserter->getFlags();
11031 return getNode(Opcode, DL, VT, Ops, Flags);
11032}
11033
11034SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11035 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11036 unsigned NumOps = Ops.size();
11037 switch (NumOps) {
11038 case 0: return getNode(Opcode, DL, VT);
11039 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
11040 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
11041 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
11042 default: break;
11043 }
11044
11045#ifndef NDEBUG
11046 for (const auto &Op : Ops)
11047 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11048 "Operand is DELETED_NODE!");
11049#endif
11050
11051 switch (Opcode) {
11052 default: break;
11053 case ISD::BUILD_VECTOR:
11054 // Attempt to simplify BUILD_VECTOR.
11055 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
11056 return V;
11057 break;
11059 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
11060 return V;
11061 break;
11062 case ISD::SELECT_CC:
11063 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
11064 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
11065 "LHS and RHS of condition must have same type!");
11066 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11067 "True and False arms of SelectCC must have same type!");
11068 assert(Ops[2].getValueType() == VT &&
11069 "select_cc node must be of same type as true and false value!");
11070 assert((!Ops[0].getValueType().isVector() ||
11071 Ops[0].getValueType().getVectorElementCount() ==
11072 VT.getVectorElementCount()) &&
11073 "Expected select_cc with vector result to have the same sized "
11074 "comparison type!");
11075 break;
11076 case ISD::BR_CC:
11077 assert(NumOps == 5 && "BR_CC takes 5 operands!");
11078 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11079 "LHS/RHS of comparison should match types!");
11080 break;
11081 case ISD::VP_ADD:
11082 case ISD::VP_SUB:
11083 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
11084 if (VT.getScalarType() == MVT::i1)
11085 Opcode = ISD::VP_XOR;
11086 break;
11087 case ISD::VP_MUL:
11088 // If it is VP_MUL mask operation then turn it to VP_AND
11089 if (VT.getScalarType() == MVT::i1)
11090 Opcode = ISD::VP_AND;
11091 break;
11092 case ISD::VP_REDUCE_MUL:
11093 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11094 if (VT == MVT::i1)
11095 Opcode = ISD::VP_REDUCE_AND;
11096 break;
11097 case ISD::VP_REDUCE_ADD:
11098 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11099 if (VT == MVT::i1)
11100 Opcode = ISD::VP_REDUCE_XOR;
11101 break;
11102 case ISD::VP_REDUCE_SMAX:
11103 case ISD::VP_REDUCE_UMIN:
11104 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11105 // VP_REDUCE_AND.
11106 if (VT == MVT::i1)
11107 Opcode = ISD::VP_REDUCE_AND;
11108 break;
11109 case ISD::VP_REDUCE_SMIN:
11110 case ISD::VP_REDUCE_UMAX:
11111 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11112 // VP_REDUCE_OR.
11113 if (VT == MVT::i1)
11114 Opcode = ISD::VP_REDUCE_OR;
11115 break;
11116 }
11117
11118 // Memoize nodes.
11119 SDNode *N;
11120 SDVTList VTs = getVTList(VT);
11121
11122 if (VT != MVT::Glue) {
11124 AddNodeIDNode(ID, Opcode, VTs, Ops);
11125 void *IP = nullptr;
11126
11127 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11128 E->intersectFlagsWith(Flags);
11129 return SDValue(E, 0);
11130 }
11131
11132 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11133 createOperands(N, Ops);
11134
11135 CSEMap.InsertNode(N, IP);
11136 } else {
11137 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11138 createOperands(N, Ops);
11139 }
11140
11141 N->setFlags(Flags);
11142 InsertNode(N);
11143 SDValue V(N, 0);
11144 NewSDValueDbgMsg(V, "Creating new node: ", this);
11145 return V;
11146}
11147
11148SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11149 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11150 SDNodeFlags Flags;
11151 if (Inserter)
11152 Flags = Inserter->getFlags();
11153 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11154}
11155
11156SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11158 const SDNodeFlags Flags) {
11159 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11160}
11161
11162SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11164 SDNodeFlags Flags;
11165 if (Inserter)
11166 Flags = Inserter->getFlags();
11167 return getNode(Opcode, DL, VTList, Ops, Flags);
11168}
11169
11170SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11171 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11172 if (VTList.NumVTs == 1)
11173 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11174
11175#ifndef NDEBUG
11176 for (const auto &Op : Ops)
11177 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11178 "Operand is DELETED_NODE!");
11179#endif
11180
11181 switch (Opcode) {
11182 case ISD::SADDO:
11183 case ISD::UADDO:
11184 case ISD::SSUBO:
11185 case ISD::USUBO: {
11186 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11187 "Invalid add/sub overflow op!");
11188 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11189 Ops[0].getValueType() == Ops[1].getValueType() &&
11190 Ops[0].getValueType() == VTList.VTs[0] &&
11191 "Binary operator types must match!");
11192 SDValue N1 = Ops[0], N2 = Ops[1];
11193 canonicalizeCommutativeBinop(Opcode, N1, N2);
11194
11195 // (X +- 0) -> X with zero-overflow.
11196 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11197 /*AllowTruncation*/ true);
11198 if (N2CV && N2CV->isZero()) {
11199 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11200 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11201 }
11202
11203 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11204 VTList.VTs[1].getScalarType() == MVT::i1) {
11205 SDValue F1 = getFreeze(N1);
11206 SDValue F2 = getFreeze(N2);
11207 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11208 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11209 return getNode(ISD::MERGE_VALUES, DL, VTList,
11210 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11211 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11212 Flags);
11213 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11214 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11215 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11216 return getNode(ISD::MERGE_VALUES, DL, VTList,
11217 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11218 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11219 Flags);
11220 }
11221 }
11222 break;
11223 }
11224 case ISD::SADDO_CARRY:
11225 case ISD::UADDO_CARRY:
11226 case ISD::SSUBO_CARRY:
11227 case ISD::USUBO_CARRY:
11228 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11229 "Invalid add/sub overflow op!");
11230 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11231 Ops[0].getValueType() == Ops[1].getValueType() &&
11232 Ops[0].getValueType() == VTList.VTs[0] &&
11233 Ops[2].getValueType() == VTList.VTs[1] &&
11234 "Binary operator types must match!");
11235 break;
11236 case ISD::SMUL_LOHI:
11237 case ISD::UMUL_LOHI: {
11238 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11239 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11240 VTList.VTs[0] == Ops[0].getValueType() &&
11241 VTList.VTs[0] == Ops[1].getValueType() &&
11242 "Binary operator types must match!");
11243 // Constant fold.
11246 if (LHS && RHS) {
11247 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11248 unsigned OutWidth = Width * 2;
11249 APInt Val = LHS->getAPIntValue();
11250 APInt Mul = RHS->getAPIntValue();
11251 if (Opcode == ISD::SMUL_LOHI) {
11252 Val = Val.sext(OutWidth);
11253 Mul = Mul.sext(OutWidth);
11254 } else {
11255 Val = Val.zext(OutWidth);
11256 Mul = Mul.zext(OutWidth);
11257 }
11258 Val *= Mul;
11259
11260 SDValue Hi =
11261 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11262 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11263 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11264 }
11265 break;
11266 }
11267 case ISD::FFREXP: {
11268 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11269 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11270 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11271
11273 int FrexpExp;
11274 APFloat FrexpMant =
11275 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11276 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11277 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11278 DL, VTList.VTs[1]);
11279 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11280 }
11281
11282 break;
11283 }
11285 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11286 "Invalid STRICT_FP_EXTEND!");
11287 assert(VTList.VTs[0].isFloatingPoint() &&
11288 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11289 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11290 "STRICT_FP_EXTEND result type should be vector iff the operand "
11291 "type is vector!");
11292 assert((!VTList.VTs[0].isVector() ||
11293 VTList.VTs[0].getVectorElementCount() ==
11294 Ops[1].getValueType().getVectorElementCount()) &&
11295 "Vector element count mismatch!");
11296 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11297 "Invalid fpext node, dst <= src!");
11298 break;
11300 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11301 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11302 "STRICT_FP_ROUND result type should be vector iff the operand "
11303 "type is vector!");
11304 assert((!VTList.VTs[0].isVector() ||
11305 VTList.VTs[0].getVectorElementCount() ==
11306 Ops[1].getValueType().getVectorElementCount()) &&
11307 "Vector element count mismatch!");
11308 assert(VTList.VTs[0].isFloatingPoint() &&
11309 Ops[1].getValueType().isFloatingPoint() &&
11310 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11311 Ops[2].getOpcode() == ISD::TargetConstant &&
11312 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11313 "Invalid STRICT_FP_ROUND!");
11314 break;
11315 }
11316
11317 // Memoize the node unless it returns a glue result.
11318 SDNode *N;
11319 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11321 AddNodeIDNode(ID, Opcode, VTList, Ops);
11322 void *IP = nullptr;
11323 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11324 E->intersectFlagsWith(Flags);
11325 return SDValue(E, 0);
11326 }
11327
11328 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11329 createOperands(N, Ops);
11330 CSEMap.InsertNode(N, IP);
11331 } else {
11332 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11333 createOperands(N, Ops);
11334 }
11335
11336 N->setFlags(Flags);
11337 InsertNode(N);
11338 SDValue V(N, 0);
11339 NewSDValueDbgMsg(V, "Creating new node: ", this);
11340 return V;
11341}
11342
11343SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11344 SDVTList VTList) {
11345 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11346}
11347
11348SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11349 SDValue N1) {
11350 SDValue Ops[] = { N1 };
11351 return getNode(Opcode, DL, VTList, Ops);
11352}
11353
11354SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11355 SDValue N1, SDValue N2) {
11356 SDValue Ops[] = { N1, N2 };
11357 return getNode(Opcode, DL, VTList, Ops);
11358}
11359
11360SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11361 SDValue N1, SDValue N2, SDValue N3) {
11362 SDValue Ops[] = { N1, N2, N3 };
11363 return getNode(Opcode, DL, VTList, Ops);
11364}
11365
11366SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11367 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11368 SDValue Ops[] = { N1, N2, N3, N4 };
11369 return getNode(Opcode, DL, VTList, Ops);
11370}
11371
11372SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11373 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11374 SDValue N5) {
11375 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11376 return getNode(Opcode, DL, VTList, Ops);
11377}
11378
11380 if (!VT.isExtended())
11381 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11382
11383 return makeVTList(&(*EVTs.insert(VT).first), 1);
11384}
11385
11388 ID.AddInteger(2U);
11389 ID.AddInteger(VT1.getRawBits());
11390 ID.AddInteger(VT2.getRawBits());
11391
11392 void *IP = nullptr;
11393 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11394 if (!Result) {
11395 EVT *Array = Allocator.Allocate<EVT>(2);
11396 Array[0] = VT1;
11397 Array[1] = VT2;
11398 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11399 VTListMap.InsertNode(Result, IP);
11400 }
11401 return Result->getSDVTList();
11402}
11403
11406 ID.AddInteger(3U);
11407 ID.AddInteger(VT1.getRawBits());
11408 ID.AddInteger(VT2.getRawBits());
11409 ID.AddInteger(VT3.getRawBits());
11410
11411 void *IP = nullptr;
11412 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11413 if (!Result) {
11414 EVT *Array = Allocator.Allocate<EVT>(3);
11415 Array[0] = VT1;
11416 Array[1] = VT2;
11417 Array[2] = VT3;
11418 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11419 VTListMap.InsertNode(Result, IP);
11420 }
11421 return Result->getSDVTList();
11422}
11423
11426 ID.AddInteger(4U);
11427 ID.AddInteger(VT1.getRawBits());
11428 ID.AddInteger(VT2.getRawBits());
11429 ID.AddInteger(VT3.getRawBits());
11430 ID.AddInteger(VT4.getRawBits());
11431
11432 void *IP = nullptr;
11433 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11434 if (!Result) {
11435 EVT *Array = Allocator.Allocate<EVT>(4);
11436 Array[0] = VT1;
11437 Array[1] = VT2;
11438 Array[2] = VT3;
11439 Array[3] = VT4;
11440 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11441 VTListMap.InsertNode(Result, IP);
11442 }
11443 return Result->getSDVTList();
11444}
11445
11447 unsigned NumVTs = VTs.size();
11449 ID.AddInteger(NumVTs);
11450 for (unsigned index = 0; index < NumVTs; index++) {
11451 ID.AddInteger(VTs[index].getRawBits());
11452 }
11453
11454 void *IP = nullptr;
11455 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11456 if (!Result) {
11457 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11458 llvm::copy(VTs, Array);
11459 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11460 VTListMap.InsertNode(Result, IP);
11461 }
11462 return Result->getSDVTList();
11463}
11464
11465
11466/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11467/// specified operands. If the resultant node already exists in the DAG,
11468/// this does not modify the specified node, instead it returns the node that
11469/// already exists. If the resultant node does not exist in the DAG, the
11470/// input node is returned. As a degenerate case, if you specify the same
11471/// input operands as the node already has, the input node is returned.
11473 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11474
11475 // Check to see if there is no change.
11476 if (Op == N->getOperand(0)) return N;
11477
11478 // See if the modified node already exists.
11479 void *InsertPos = nullptr;
11480 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11481 return Existing;
11482
11483 // Nope it doesn't. Remove the node from its current place in the maps.
11484 if (InsertPos)
11485 if (!RemoveNodeFromCSEMaps(N))
11486 InsertPos = nullptr;
11487
11488 // Now we update the operands.
11489 N->OperandList[0].set(Op);
11490
11492 // If this gets put into a CSE map, add it.
11493 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11494 return N;
11495}
11496
11498 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11499
11500 // Check to see if there is no change.
11501 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11502 return N; // No operands changed, just return the input node.
11503
11504 // See if the modified node already exists.
11505 void *InsertPos = nullptr;
11506 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11507 return Existing;
11508
11509 // Nope it doesn't. Remove the node from its current place in the maps.
11510 if (InsertPos)
11511 if (!RemoveNodeFromCSEMaps(N))
11512 InsertPos = nullptr;
11513
11514 // Now we update the operands.
11515 if (N->OperandList[0] != Op1)
11516 N->OperandList[0].set(Op1);
11517 if (N->OperandList[1] != Op2)
11518 N->OperandList[1].set(Op2);
11519
11521 // If this gets put into a CSE map, add it.
11522 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11523 return N;
11524}
11525
11528 SDValue Ops[] = { Op1, Op2, Op3 };
11529 return UpdateNodeOperands(N, Ops);
11530}
11531
11534 SDValue Op3, SDValue Op4) {
11535 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11536 return UpdateNodeOperands(N, Ops);
11537}
11538
11541 SDValue Op3, SDValue Op4, SDValue Op5) {
11542 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11543 return UpdateNodeOperands(N, Ops);
11544}
11545
11548 unsigned NumOps = Ops.size();
11549 assert(N->getNumOperands() == NumOps &&
11550 "Update with wrong number of operands");
11551
11552 // If no operands changed just return the input node.
11553 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11554 return N;
11555
11556 // See if the modified node already exists.
11557 void *InsertPos = nullptr;
11558 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11559 return Existing;
11560
11561 // Nope it doesn't. Remove the node from its current place in the maps.
11562 if (InsertPos)
11563 if (!RemoveNodeFromCSEMaps(N))
11564 InsertPos = nullptr;
11565
11566 // Now we update the operands.
11567 for (unsigned i = 0; i != NumOps; ++i)
11568 if (N->OperandList[i] != Ops[i])
11569 N->OperandList[i].set(Ops[i]);
11570
11572 // If this gets put into a CSE map, add it.
11573 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11574 return N;
11575}
11576
11577/// DropOperands - Release the operands and set this node to have
11578/// zero operands.
11580 // Unlike the code in MorphNodeTo that does this, we don't need to
11581 // watch for dead nodes here.
11582 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11583 SDUse &Use = *I++;
11584 Use.set(SDValue());
11585 }
11586}
11587
11589 ArrayRef<MachineMemOperand *> NewMemRefs) {
11590 if (NewMemRefs.empty()) {
11591 N->clearMemRefs();
11592 return;
11593 }
11594
11595 // Check if we can avoid allocating by storing a single reference directly.
11596 if (NewMemRefs.size() == 1) {
11597 N->MemRefs = NewMemRefs[0];
11598 N->NumMemRefs = 1;
11599 return;
11600 }
11601
11602 MachineMemOperand **MemRefsBuffer =
11603 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11604 llvm::copy(NewMemRefs, MemRefsBuffer);
11605 N->MemRefs = MemRefsBuffer;
11606 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11607}
11608
11609/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11610/// machine opcode.
11611///
11613 EVT VT) {
11614 SDVTList VTs = getVTList(VT);
11615 return SelectNodeTo(N, MachineOpc, VTs, {});
11616}
11617
11619 EVT VT, SDValue Op1) {
11620 SDVTList VTs = getVTList(VT);
11621 SDValue Ops[] = { Op1 };
11622 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11623}
11624
11626 EVT VT, SDValue Op1,
11627 SDValue Op2) {
11628 SDVTList VTs = getVTList(VT);
11629 SDValue Ops[] = { Op1, Op2 };
11630 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11631}
11632
11634 EVT VT, SDValue Op1,
11635 SDValue Op2, SDValue Op3) {
11636 SDVTList VTs = getVTList(VT);
11637 SDValue Ops[] = { Op1, Op2, Op3 };
11638 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11639}
11640
11643 SDVTList VTs = getVTList(VT);
11644 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11645}
11646
11648 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11649 SDVTList VTs = getVTList(VT1, VT2);
11650 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11651}
11652
11654 EVT VT1, EVT VT2) {
11655 SDVTList VTs = getVTList(VT1, VT2);
11656 return SelectNodeTo(N, MachineOpc, VTs, {});
11657}
11658
11660 EVT VT1, EVT VT2, EVT VT3,
11662 SDVTList VTs = getVTList(VT1, VT2, VT3);
11663 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11664}
11665
11667 EVT VT1, EVT VT2,
11668 SDValue Op1, SDValue Op2) {
11669 SDVTList VTs = getVTList(VT1, VT2);
11670 SDValue Ops[] = { Op1, Op2 };
11671 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11672}
11673
11676 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11677 // Reset the NodeID to -1.
11678 New->setNodeId(-1);
11679 if (New != N) {
11680 ReplaceAllUsesWith(N, New);
11682 }
11683 return New;
11684}
11685
11686/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11687/// the line number information on the merged node since it is not possible to
11688/// preserve the information that operation is associated with multiple lines.
11689/// This will make the debugger working better at -O0, were there is a higher
11690/// probability having other instructions associated with that line.
11691///
11692/// For IROrder, we keep the smaller of the two
11693SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11694 DebugLoc NLoc = N->getDebugLoc();
11695 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11696 N->setDebugLoc(DebugLoc());
11697 }
11698 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11699 N->setIROrder(Order);
11700 return N;
11701}
11702
11703/// MorphNodeTo - This *mutates* the specified node to have the specified
11704/// return type, opcode, and operands.
11705///
11706/// Note that MorphNodeTo returns the resultant node. If there is already a
11707/// node of the specified opcode and operands, it returns that node instead of
11708/// the current one. Note that the SDLoc need not be the same.
11709///
11710/// Using MorphNodeTo is faster than creating a new node and swapping it in
11711/// with ReplaceAllUsesWith both because it often avoids allocating a new
11712/// node, and because it doesn't require CSE recalculation for any of
11713/// the node's users.
11714///
11715/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11716/// As a consequence it isn't appropriate to use from within the DAG combiner or
11717/// the legalizer which maintain worklists that would need to be updated when
11718/// deleting things.
11721 // If an identical node already exists, use it.
11722 void *IP = nullptr;
11723 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11725 AddNodeIDNode(ID, Opc, VTs, Ops);
11726 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11727 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11728 }
11729
11730 if (!RemoveNodeFromCSEMaps(N))
11731 IP = nullptr;
11732
11733 // Start the morphing.
11734 N->NodeType = Opc;
11735 N->ValueList = VTs.VTs;
11736 N->NumValues = VTs.NumVTs;
11737
11738 // Clear the operands list, updating used nodes to remove this from their
11739 // use list. Keep track of any operands that become dead as a result.
11740 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11741 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11742 SDUse &Use = *I++;
11743 SDNode *Used = Use.getNode();
11744 Use.set(SDValue());
11745 if (Used->use_empty())
11746 DeadNodeSet.insert(Used);
11747 }
11748
11749 // For MachineNode, initialize the memory references information.
11751 MN->clearMemRefs();
11752
11753 // Swap for an appropriately sized array from the recycler.
11754 removeOperands(N);
11755 createOperands(N, Ops);
11756
11757 // Delete any nodes that are still dead after adding the uses for the
11758 // new operands.
11759 if (!DeadNodeSet.empty()) {
11760 SmallVector<SDNode *, 16> DeadNodes;
11761 for (SDNode *N : DeadNodeSet)
11762 if (N->use_empty())
11763 DeadNodes.push_back(N);
11764 RemoveDeadNodes(DeadNodes);
11765 }
11766
11767 if (IP)
11768 CSEMap.InsertNode(N, IP); // Memoize the new node.
11769 return N;
11770}
11771
11773 unsigned OrigOpc = Node->getOpcode();
11774 unsigned NewOpc;
11775 switch (OrigOpc) {
11776 default:
11777 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11778#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11779 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11780#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11781 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11782#include "llvm/IR/ConstrainedOps.def"
11783 }
11784
11785 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11786
11787 // We're taking this node out of the chain, so we need to re-link things.
11788 SDValue InputChain = Node->getOperand(0);
11789 SDValue OutputChain = SDValue(Node, 1);
11790 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11791
11793 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11794 Ops.push_back(Node->getOperand(i));
11795
11796 SDVTList VTs = getVTList(Node->getValueType(0));
11797 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11798
11799 // MorphNodeTo can operate in two ways: if an existing node with the
11800 // specified operands exists, it can just return it. Otherwise, it
11801 // updates the node in place to have the requested operands.
11802 if (Res == Node) {
11803 // If we updated the node in place, reset the node ID. To the isel,
11804 // this should be just like a newly allocated machine node.
11805 Res->setNodeId(-1);
11806 } else {
11809 }
11810
11811 return Res;
11812}
11813
11814/// getMachineNode - These are used for target selectors to create a new node
11815/// with specified return type(s), MachineInstr opcode, and operands.
11816///
11817/// Note that getMachineNode returns the resultant node. If there is already a
11818/// node of the specified opcode and operands, it returns that node instead of
11819/// the current one.
11821 EVT VT) {
11822 SDVTList VTs = getVTList(VT);
11823 return getMachineNode(Opcode, dl, VTs, {});
11824}
11825
11827 EVT VT, SDValue Op1) {
11828 SDVTList VTs = getVTList(VT);
11829 SDValue Ops[] = { Op1 };
11830 return getMachineNode(Opcode, dl, VTs, Ops);
11831}
11832
11834 EVT VT, SDValue Op1, SDValue Op2) {
11835 SDVTList VTs = getVTList(VT);
11836 SDValue Ops[] = { Op1, Op2 };
11837 return getMachineNode(Opcode, dl, VTs, Ops);
11838}
11839
11841 EVT VT, SDValue Op1, SDValue Op2,
11842 SDValue Op3) {
11843 SDVTList VTs = getVTList(VT);
11844 SDValue Ops[] = { Op1, Op2, Op3 };
11845 return getMachineNode(Opcode, dl, VTs, Ops);
11846}
11847
11850 SDVTList VTs = getVTList(VT);
11851 return getMachineNode(Opcode, dl, VTs, Ops);
11852}
11853
11855 EVT VT1, EVT VT2, SDValue Op1,
11856 SDValue Op2) {
11857 SDVTList VTs = getVTList(VT1, VT2);
11858 SDValue Ops[] = { Op1, Op2 };
11859 return getMachineNode(Opcode, dl, VTs, Ops);
11860}
11861
11863 EVT VT1, EVT VT2, SDValue Op1,
11864 SDValue Op2, SDValue Op3) {
11865 SDVTList VTs = getVTList(VT1, VT2);
11866 SDValue Ops[] = { Op1, Op2, Op3 };
11867 return getMachineNode(Opcode, dl, VTs, Ops);
11868}
11869
11871 EVT VT1, EVT VT2,
11873 SDVTList VTs = getVTList(VT1, VT2);
11874 return getMachineNode(Opcode, dl, VTs, Ops);
11875}
11876
11878 EVT VT1, EVT VT2, EVT VT3,
11879 SDValue Op1, SDValue Op2) {
11880 SDVTList VTs = getVTList(VT1, VT2, VT3);
11881 SDValue Ops[] = { Op1, Op2 };
11882 return getMachineNode(Opcode, dl, VTs, Ops);
11883}
11884
11886 EVT VT1, EVT VT2, EVT VT3,
11887 SDValue Op1, SDValue Op2,
11888 SDValue Op3) {
11889 SDVTList VTs = getVTList(VT1, VT2, VT3);
11890 SDValue Ops[] = { Op1, Op2, Op3 };
11891 return getMachineNode(Opcode, dl, VTs, Ops);
11892}
11893
11895 EVT VT1, EVT VT2, EVT VT3,
11897 SDVTList VTs = getVTList(VT1, VT2, VT3);
11898 return getMachineNode(Opcode, dl, VTs, Ops);
11899}
11900
11902 ArrayRef<EVT> ResultTys,
11904 SDVTList VTs = getVTList(ResultTys);
11905 return getMachineNode(Opcode, dl, VTs, Ops);
11906}
11907
11909 SDVTList VTs,
11911 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11913 void *IP = nullptr;
11914
11915 if (DoCSE) {
11917 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11918 IP = nullptr;
11919 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11920 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11921 }
11922 }
11923
11924 // Allocate a new MachineSDNode.
11925 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11926 createOperands(N, Ops);
11927
11928 if (DoCSE)
11929 CSEMap.InsertNode(N, IP);
11930
11931 InsertNode(N);
11932 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11933 return N;
11934}
11935
11936/// getTargetExtractSubreg - A convenience function for creating
11937/// TargetOpcode::EXTRACT_SUBREG nodes.
11939 SDValue Operand) {
11940 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11941 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11942 VT, Operand, SRIdxVal);
11943 return SDValue(Subreg, 0);
11944}
11945
11946/// getTargetInsertSubreg - A convenience function for creating
11947/// TargetOpcode::INSERT_SUBREG nodes.
11949 SDValue Operand, SDValue Subreg) {
11950 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11951 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11952 VT, Operand, Subreg, SRIdxVal);
11953 return SDValue(Result, 0);
11954}
11955
11956/// getNodeIfExists - Get the specified node if it's already available, or
11957/// else return NULL.
11960 bool AllowCommute) {
11961 SDNodeFlags Flags;
11962 if (Inserter)
11963 Flags = Inserter->getFlags();
11964 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
11965}
11966
11969 const SDNodeFlags Flags,
11970 bool AllowCommute) {
11971 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
11972 return nullptr;
11973
11974 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
11976 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
11977 void *IP = nullptr;
11978 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
11979 E->intersectFlagsWith(Flags);
11980 return E;
11981 }
11982 return nullptr;
11983 };
11984
11985 if (SDNode *Existing = Lookup(Ops))
11986 return Existing;
11987
11988 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
11989 return Lookup({Ops[1], Ops[0]});
11990
11991 return nullptr;
11992}
11993
11994/// doesNodeExist - Check if a node exists without modifying its flags.
11995bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11997 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11999 AddNodeIDNode(ID, Opcode, VTList, Ops);
12000 void *IP = nullptr;
12001 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
12002 return true;
12003 }
12004 return false;
12005}
12006
12007/// getDbgValue - Creates a SDDbgValue node.
12008///
12009/// SDNode
12011 SDNode *N, unsigned R, bool IsIndirect,
12012 const DebugLoc &DL, unsigned O) {
12013 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12014 "Expected inlined-at fields to agree");
12015 return new (DbgInfo->getAlloc())
12016 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
12017 {}, IsIndirect, DL, O,
12018 /*IsVariadic=*/false);
12019}
12020
12021/// Constant
12023 DIExpression *Expr,
12024 const Value *C,
12025 const DebugLoc &DL, unsigned O) {
12026 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12027 "Expected inlined-at fields to agree");
12028 return new (DbgInfo->getAlloc())
12029 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
12030 /*IsIndirect=*/false, DL, O,
12031 /*IsVariadic=*/false);
12032}
12033
12034/// FrameIndex
12036 DIExpression *Expr, unsigned FI,
12037 bool IsIndirect,
12038 const DebugLoc &DL,
12039 unsigned O) {
12040 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12041 "Expected inlined-at fields to agree");
12042 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
12043}
12044
12045/// FrameIndex with dependencies
12047 DIExpression *Expr, unsigned FI,
12048 ArrayRef<SDNode *> Dependencies,
12049 bool IsIndirect,
12050 const DebugLoc &DL,
12051 unsigned O) {
12052 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12053 "Expected inlined-at fields to agree");
12054 return new (DbgInfo->getAlloc())
12055 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
12056 Dependencies, IsIndirect, DL, O,
12057 /*IsVariadic=*/false);
12058}
12059
12060/// VReg
12062 Register VReg, bool IsIndirect,
12063 const DebugLoc &DL, unsigned O) {
12064 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12065 "Expected inlined-at fields to agree");
12066 return new (DbgInfo->getAlloc())
12067 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
12068 {}, IsIndirect, DL, O,
12069 /*IsVariadic=*/false);
12070}
12071
12074 ArrayRef<SDNode *> Dependencies,
12075 bool IsIndirect, const DebugLoc &DL,
12076 unsigned O, bool IsVariadic) {
12077 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12078 "Expected inlined-at fields to agree");
12079 return new (DbgInfo->getAlloc())
12080 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
12081 DL, O, IsVariadic);
12082}
12083
12085 unsigned OffsetInBits, unsigned SizeInBits,
12086 bool InvalidateDbg) {
12087 SDNode *FromNode = From.getNode();
12088 SDNode *ToNode = To.getNode();
12089 assert(FromNode && ToNode && "Can't modify dbg values");
12090
12091 // PR35338
12092 // TODO: assert(From != To && "Redundant dbg value transfer");
12093 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12094 if (From == To || FromNode == ToNode)
12095 return;
12096
12097 if (!FromNode->getHasDebugValue())
12098 return;
12099
12100 SDDbgOperand FromLocOp =
12101 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12103
12105 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12106 if (Dbg->isInvalidated())
12107 continue;
12108
12109 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12110
12111 // Create a new location ops vector that is equal to the old vector, but
12112 // with each instance of FromLocOp replaced with ToLocOp.
12113 bool Changed = false;
12114 auto NewLocOps = Dbg->copyLocationOps();
12115 std::replace_if(
12116 NewLocOps.begin(), NewLocOps.end(),
12117 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12118 bool Match = Op == FromLocOp;
12119 Changed |= Match;
12120 return Match;
12121 },
12122 ToLocOp);
12123 // Ignore this SDDbgValue if we didn't find a matching location.
12124 if (!Changed)
12125 continue;
12126
12127 DIVariable *Var = Dbg->getVariable();
12128 auto *Expr = Dbg->getExpression();
12129 // If a fragment is requested, update the expression.
12130 if (SizeInBits) {
12131 // When splitting a larger (e.g., sign-extended) value whose
12132 // lower bits are described with an SDDbgValue, do not attempt
12133 // to transfer the SDDbgValue to the upper bits.
12134 if (auto FI = Expr->getFragmentInfo())
12135 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12136 continue;
12137 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12138 SizeInBits);
12139 if (!Fragment)
12140 continue;
12141 Expr = *Fragment;
12142 }
12143
12144 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12145 // Clone the SDDbgValue and move it to To.
12146 SDDbgValue *Clone = getDbgValueList(
12147 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12148 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12149 Dbg->isVariadic());
12150 ClonedDVs.push_back(Clone);
12151
12152 if (InvalidateDbg) {
12153 // Invalidate value and indicate the SDDbgValue should not be emitted.
12154 Dbg->setIsInvalidated();
12155 Dbg->setIsEmitted();
12156 }
12157 }
12158
12159 for (SDDbgValue *Dbg : ClonedDVs) {
12160 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12161 "Transferred DbgValues should depend on the new SDNode");
12162 AddDbgValue(Dbg, false);
12163 }
12164}
12165
12167 if (!N.getHasDebugValue())
12168 return;
12169
12170 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12171 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12172 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12173 return SDDbgOperand::fromNode(Node, ResNo);
12174 };
12175
12177 for (auto *DV : GetDbgValues(&N)) {
12178 if (DV->isInvalidated())
12179 continue;
12180 switch (N.getOpcode()) {
12181 default:
12182 break;
12183 case ISD::ADD: {
12184 SDValue N0 = N.getOperand(0);
12185 SDValue N1 = N.getOperand(1);
12186 if (!isa<ConstantSDNode>(N0)) {
12187 bool RHSConstant = isa<ConstantSDNode>(N1);
12189 if (RHSConstant)
12190 Offset = N.getConstantOperandVal(1);
12191 // We are not allowed to turn indirect debug values variadic, so
12192 // don't salvage those.
12193 if (!RHSConstant && DV->isIndirect())
12194 continue;
12195
12196 // Rewrite an ADD constant node into a DIExpression. Since we are
12197 // performing arithmetic to compute the variable's *value* in the
12198 // DIExpression, we need to mark the expression with a
12199 // DW_OP_stack_value.
12200 auto *DIExpr = DV->getExpression();
12201 auto NewLocOps = DV->copyLocationOps();
12202 bool Changed = false;
12203 size_t OrigLocOpsSize = NewLocOps.size();
12204 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12205 // We're not given a ResNo to compare against because the whole
12206 // node is going away. We know that any ISD::ADD only has one
12207 // result, so we can assume any node match is using the result.
12208 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12209 NewLocOps[i].getSDNode() != &N)
12210 continue;
12211 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12212 if (RHSConstant) {
12215 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12216 } else {
12217 // Convert to a variadic expression (if not already).
12218 // convertToVariadicExpression() returns a const pointer, so we use
12219 // a temporary const variable here.
12220 const auto *TmpDIExpr =
12224 ExprOps.push_back(NewLocOps.size());
12225 ExprOps.push_back(dwarf::DW_OP_plus);
12226 SDDbgOperand RHS =
12228 NewLocOps.push_back(RHS);
12229 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12230 }
12231 Changed = true;
12232 }
12233 (void)Changed;
12234 assert(Changed && "Salvage target doesn't use N");
12235
12236 bool IsVariadic =
12237 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12238
12239 auto AdditionalDependencies = DV->getAdditionalDependencies();
12240 SDDbgValue *Clone = getDbgValueList(
12241 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12242 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12243 ClonedDVs.push_back(Clone);
12244 DV->setIsInvalidated();
12245 DV->setIsEmitted();
12246 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12247 N0.getNode()->dumprFull(this);
12248 dbgs() << " into " << *DIExpr << '\n');
12249 }
12250 break;
12251 }
12252 case ISD::TRUNCATE: {
12253 SDValue N0 = N.getOperand(0);
12254 TypeSize FromSize = N0.getValueSizeInBits();
12255 TypeSize ToSize = N.getValueSizeInBits(0);
12256
12257 DIExpression *DbgExpression = DV->getExpression();
12258 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12259 auto NewLocOps = DV->copyLocationOps();
12260 bool Changed = false;
12261 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12262 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12263 NewLocOps[i].getSDNode() != &N)
12264 continue;
12265
12266 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12267 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12268 Changed = true;
12269 }
12270 assert(Changed && "Salvage target doesn't use N");
12271 (void)Changed;
12272
12273 SDDbgValue *Clone =
12274 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12275 DV->getAdditionalDependencies(), DV->isIndirect(),
12276 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12277
12278 ClonedDVs.push_back(Clone);
12279 DV->setIsInvalidated();
12280 DV->setIsEmitted();
12281 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12282 dbgs() << " into " << *DbgExpression << '\n');
12283 break;
12284 }
12285 }
12286 }
12287
12288 for (SDDbgValue *Dbg : ClonedDVs) {
12289 assert((!Dbg->getSDNodes().empty() ||
12290 llvm::any_of(Dbg->getLocationOps(),
12291 [&](const SDDbgOperand &Op) {
12292 return Op.getKind() == SDDbgOperand::FRAMEIX;
12293 })) &&
12294 "Salvaged DbgValue should depend on a new SDNode");
12295 AddDbgValue(Dbg, false);
12296 }
12297}
12298
12299/// Creates a SDDbgLabel node.
12301 const DebugLoc &DL, unsigned O) {
12302 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12303 "Expected inlined-at fields to agree");
12304 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12305}
12306
12307namespace {
12308
12309/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12310/// pointed to by a use iterator is deleted, increment the use iterator
12311/// so that it doesn't dangle.
12312///
12313class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12316
12317 void NodeDeleted(SDNode *N, SDNode *E) override {
12318 // Increment the iterator as needed.
12319 while (UI != UE && N == UI->getUser())
12320 ++UI;
12321 }
12322
12323public:
12324 RAUWUpdateListener(SelectionDAG &d,
12327 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12328};
12329
12330} // end anonymous namespace
12331
12332/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12333/// This can cause recursive merging of nodes in the DAG.
12334///
12335/// This version assumes From has a single result value.
12336///
12338 SDNode *From = FromN.getNode();
12339 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12340 "Cannot replace with this method!");
12341 assert(From != To.getNode() && "Cannot replace uses of with self");
12342
12343 // Preserve Debug Values
12344 transferDbgValues(FromN, To);
12345 // Preserve extra info.
12346 copyExtraInfo(From, To.getNode());
12347
12348 // Iterate over all the existing uses of From. New uses will be added
12349 // to the beginning of the use list, which we avoid visiting.
12350 // This specifically avoids visiting uses of From that arise while the
12351 // replacement is happening, because any such uses would be the result
12352 // of CSE: If an existing node looks like From after one of its operands
12353 // is replaced by To, we don't want to replace of all its users with To
12354 // too. See PR3018 for more info.
12355 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12356 RAUWUpdateListener Listener(*this, UI, UE);
12357 while (UI != UE) {
12358 SDNode *User = UI->getUser();
12359
12360 // This node is about to morph, remove its old self from the CSE maps.
12361 RemoveNodeFromCSEMaps(User);
12362
12363 // A user can appear in a use list multiple times, and when this
12364 // happens the uses are usually next to each other in the list.
12365 // To help reduce the number of CSE recomputations, process all
12366 // the uses of this user that we can find this way.
12367 do {
12368 SDUse &Use = *UI;
12369 ++UI;
12370 Use.set(To);
12371 if (To->isDivergent() != From->isDivergent())
12373 } while (UI != UE && UI->getUser() == User);
12374 // Now that we have modified User, add it back to the CSE maps. If it
12375 // already exists there, recursively merge the results together.
12376 AddModifiedNodeToCSEMaps(User);
12377 }
12378
12379 // If we just RAUW'd the root, take note.
12380 if (FromN == getRoot())
12381 setRoot(To);
12382}
12383
12384/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12385/// This can cause recursive merging of nodes in the DAG.
12386///
12387/// This version assumes that for each value of From, there is a
12388/// corresponding value in To in the same position with the same type.
12389///
12391#ifndef NDEBUG
12392 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12393 assert((!From->hasAnyUseOfValue(i) ||
12394 From->getValueType(i) == To->getValueType(i)) &&
12395 "Cannot use this version of ReplaceAllUsesWith!");
12396#endif
12397
12398 // Handle the trivial case.
12399 if (From == To)
12400 return;
12401
12402 // Preserve Debug Info. Only do this if there's a use.
12403 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12404 if (From->hasAnyUseOfValue(i)) {
12405 assert((i < To->getNumValues()) && "Invalid To location");
12406 transferDbgValues(SDValue(From, i), SDValue(To, i));
12407 }
12408 // Preserve extra info.
12409 copyExtraInfo(From, To);
12410
12411 // Iterate over just the existing users of From. See the comments in
12412 // the ReplaceAllUsesWith above.
12413 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12414 RAUWUpdateListener Listener(*this, UI, UE);
12415 while (UI != UE) {
12416 SDNode *User = UI->getUser();
12417
12418 // This node is about to morph, remove its old self from the CSE maps.
12419 RemoveNodeFromCSEMaps(User);
12420
12421 // A user can appear in a use list multiple times, and when this
12422 // happens the uses are usually next to each other in the list.
12423 // To help reduce the number of CSE recomputations, process all
12424 // the uses of this user that we can find this way.
12425 do {
12426 SDUse &Use = *UI;
12427 ++UI;
12428 Use.setNode(To);
12429 if (To->isDivergent() != From->isDivergent())
12431 } while (UI != UE && UI->getUser() == User);
12432
12433 // Now that we have modified User, add it back to the CSE maps. If it
12434 // already exists there, recursively merge the results together.
12435 AddModifiedNodeToCSEMaps(User);
12436 }
12437
12438 // If we just RAUW'd the root, take note.
12439 if (From == getRoot().getNode())
12440 setRoot(SDValue(To, getRoot().getResNo()));
12441}
12442
12443/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12444/// This can cause recursive merging of nodes in the DAG.
12445///
12446/// This version can replace From with any result values. To must match the
12447/// number and types of values returned by From.
12449 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12450 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12451
12452 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12453 // Preserve Debug Info.
12454 transferDbgValues(SDValue(From, i), To[i]);
12455 // Preserve extra info.
12456 copyExtraInfo(From, To[i].getNode());
12457 }
12458
12459 // Iterate over just the existing users of From. See the comments in
12460 // the ReplaceAllUsesWith above.
12461 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12462 RAUWUpdateListener Listener(*this, UI, UE);
12463 while (UI != UE) {
12464 SDNode *User = UI->getUser();
12465
12466 // This node is about to morph, remove its old self from the CSE maps.
12467 RemoveNodeFromCSEMaps(User);
12468
12469 // A user can appear in a use list multiple times, and when this happens the
12470 // uses are usually next to each other in the list. To help reduce the
12471 // number of CSE and divergence recomputations, process all the uses of this
12472 // user that we can find this way.
12473 bool To_IsDivergent = false;
12474 do {
12475 SDUse &Use = *UI;
12476 const SDValue &ToOp = To[Use.getResNo()];
12477 ++UI;
12478 Use.set(ToOp);
12479 if (ToOp.getValueType() != MVT::Other)
12480 To_IsDivergent |= ToOp->isDivergent();
12481 } while (UI != UE && UI->getUser() == User);
12482
12483 if (To_IsDivergent != From->isDivergent())
12485
12486 // Now that we have modified User, add it back to the CSE maps. If it
12487 // already exists there, recursively merge the results together.
12488 AddModifiedNodeToCSEMaps(User);
12489 }
12490
12491 // If we just RAUW'd the root, take note.
12492 if (From == getRoot().getNode())
12493 setRoot(SDValue(To[getRoot().getResNo()]));
12494}
12495
12496/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12497/// uses of other values produced by From.getNode() alone. The Deleted
12498/// vector is handled the same way as for ReplaceAllUsesWith.
12500 // Handle the really simple, really trivial case efficiently.
12501 if (From == To) return;
12502
12503 // Handle the simple, trivial, case efficiently.
12504 if (From.getNode()->getNumValues() == 1) {
12505 ReplaceAllUsesWith(From, To);
12506 return;
12507 }
12508
12509 // Preserve Debug Info.
12510 transferDbgValues(From, To);
12511 copyExtraInfo(From.getNode(), To.getNode());
12512
12513 // Iterate over just the existing users of From. See the comments in
12514 // the ReplaceAllUsesWith above.
12515 SDNode::use_iterator UI = From.getNode()->use_begin(),
12516 UE = From.getNode()->use_end();
12517 RAUWUpdateListener Listener(*this, UI, UE);
12518 while (UI != UE) {
12519 SDNode *User = UI->getUser();
12520 bool UserRemovedFromCSEMaps = false;
12521
12522 // A user can appear in a use list multiple times, and when this
12523 // happens the uses are usually next to each other in the list.
12524 // To help reduce the number of CSE recomputations, process all
12525 // the uses of this user that we can find this way.
12526 do {
12527 SDUse &Use = *UI;
12528
12529 // Skip uses of different values from the same node.
12530 if (Use.getResNo() != From.getResNo()) {
12531 ++UI;
12532 continue;
12533 }
12534
12535 // If this node hasn't been modified yet, it's still in the CSE maps,
12536 // so remove its old self from the CSE maps.
12537 if (!UserRemovedFromCSEMaps) {
12538 RemoveNodeFromCSEMaps(User);
12539 UserRemovedFromCSEMaps = true;
12540 }
12541
12542 ++UI;
12543 Use.set(To);
12544 if (To->isDivergent() != From->isDivergent())
12546 } while (UI != UE && UI->getUser() == User);
12547 // We are iterating over all uses of the From node, so if a use
12548 // doesn't use the specific value, no changes are made.
12549 if (!UserRemovedFromCSEMaps)
12550 continue;
12551
12552 // Now that we have modified User, add it back to the CSE maps. If it
12553 // already exists there, recursively merge the results together.
12554 AddModifiedNodeToCSEMaps(User);
12555 }
12556
12557 // If we just RAUW'd the root, take note.
12558 if (From == getRoot())
12559 setRoot(To);
12560}
12561
12562namespace {
12563
12564/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12565/// to record information about a use.
12566struct UseMemo {
12567 SDNode *User;
12568 unsigned Index;
12569 SDUse *Use;
12570};
12571
12572/// operator< - Sort Memos by User.
12573bool operator<(const UseMemo &L, const UseMemo &R) {
12574 return (intptr_t)L.User < (intptr_t)R.User;
12575}
12576
12577/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12578/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12579/// the node already has been taken care of recursively.
12580class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12581 SmallVectorImpl<UseMemo> &Uses;
12582
12583 void NodeDeleted(SDNode *N, SDNode *E) override {
12584 for (UseMemo &Memo : Uses)
12585 if (Memo.User == N)
12586 Memo.User = nullptr;
12587 }
12588
12589public:
12590 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12591 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12592};
12593
12594} // end anonymous namespace
12595
12596/// Return true if a glue output should propagate divergence information.
12598 switch (Node->getOpcode()) {
12599 case ISD::CopyFromReg:
12600 case ISD::CopyToReg:
12601 return false;
12602 default:
12603 return true;
12604 }
12605
12606 llvm_unreachable("covered opcode switch");
12607}
12608
12610 if (TLI->isSDNodeAlwaysUniform(N)) {
12611 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12612 "Conflicting divergence information!");
12613 return false;
12614 }
12615 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12616 return true;
12617 for (const auto &Op : N->ops()) {
12618 EVT VT = Op.getValueType();
12619
12620 // Skip Chain. It does not carry divergence.
12621 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12622 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12623 return true;
12624 }
12625 return false;
12626}
12627
12629 SmallVector<SDNode *, 16> Worklist(1, N);
12630 do {
12631 N = Worklist.pop_back_val();
12632 bool IsDivergent = calculateDivergence(N);
12633 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12634 N->SDNodeBits.IsDivergent = IsDivergent;
12635 llvm::append_range(Worklist, N->users());
12636 }
12637 } while (!Worklist.empty());
12638}
12639
12640void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12642 Order.reserve(AllNodes.size());
12643 for (auto &N : allnodes()) {
12644 unsigned NOps = N.getNumOperands();
12645 Degree[&N] = NOps;
12646 if (0 == NOps)
12647 Order.push_back(&N);
12648 }
12649 for (size_t I = 0; I != Order.size(); ++I) {
12650 SDNode *N = Order[I];
12651 for (auto *U : N->users()) {
12652 unsigned &UnsortedOps = Degree[U];
12653 if (0 == --UnsortedOps)
12654 Order.push_back(U);
12655 }
12656 }
12657}
12658
12659#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12660void SelectionDAG::VerifyDAGDivergence() {
12661 std::vector<SDNode *> TopoOrder;
12662 CreateTopologicalOrder(TopoOrder);
12663 for (auto *N : TopoOrder) {
12664 assert(calculateDivergence(N) == N->isDivergent() &&
12665 "Divergence bit inconsistency detected");
12666 }
12667}
12668#endif
12669
12670/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12671/// uses of other values produced by From.getNode() alone. The same value
12672/// may appear in both the From and To list. The Deleted vector is
12673/// handled the same way as for ReplaceAllUsesWith.
12675 const SDValue *To,
12676 unsigned Num){
12677 // Handle the simple, trivial case efficiently.
12678 if (Num == 1)
12679 return ReplaceAllUsesOfValueWith(*From, *To);
12680
12681 transferDbgValues(*From, *To);
12682 copyExtraInfo(From->getNode(), To->getNode());
12683
12684 // Read up all the uses and make records of them. This helps
12685 // processing new uses that are introduced during the
12686 // replacement process.
12688 for (unsigned i = 0; i != Num; ++i) {
12689 unsigned FromResNo = From[i].getResNo();
12690 SDNode *FromNode = From[i].getNode();
12691 for (SDUse &Use : FromNode->uses()) {
12692 if (Use.getResNo() == FromResNo) {
12693 UseMemo Memo = {Use.getUser(), i, &Use};
12694 Uses.push_back(Memo);
12695 }
12696 }
12697 }
12698
12699 // Sort the uses, so that all the uses from a given User are together.
12701 RAUOVWUpdateListener Listener(*this, Uses);
12702
12703 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12704 UseIndex != UseIndexEnd; ) {
12705 // We know that this user uses some value of From. If it is the right
12706 // value, update it.
12707 SDNode *User = Uses[UseIndex].User;
12708 // If the node has been deleted by recursive CSE updates when updating
12709 // another node, then just skip this entry.
12710 if (User == nullptr) {
12711 ++UseIndex;
12712 continue;
12713 }
12714
12715 // This node is about to morph, remove its old self from the CSE maps.
12716 RemoveNodeFromCSEMaps(User);
12717
12718 // The Uses array is sorted, so all the uses for a given User
12719 // are next to each other in the list.
12720 // To help reduce the number of CSE recomputations, process all
12721 // the uses of this user that we can find this way.
12722 do {
12723 unsigned i = Uses[UseIndex].Index;
12724 SDUse &Use = *Uses[UseIndex].Use;
12725 ++UseIndex;
12726
12727 Use.set(To[i]);
12728 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12729
12730 // Now that we have modified User, add it back to the CSE maps. If it
12731 // already exists there, recursively merge the results together.
12732 AddModifiedNodeToCSEMaps(User);
12733 }
12734}
12735
12736/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12737/// based on their topological order. It returns the maximum id and a vector
12738/// of the SDNodes* in assigned order by reference.
12740 unsigned DAGSize = 0;
12741
12742 // SortedPos tracks the progress of the algorithm. Nodes before it are
12743 // sorted, nodes after it are unsorted. When the algorithm completes
12744 // it is at the end of the list.
12745 allnodes_iterator SortedPos = allnodes_begin();
12746
12747 // Visit all the nodes. Move nodes with no operands to the front of
12748 // the list immediately. Annotate nodes that do have operands with their
12749 // operand count. Before we do this, the Node Id fields of the nodes
12750 // may contain arbitrary values. After, the Node Id fields for nodes
12751 // before SortedPos will contain the topological sort index, and the
12752 // Node Id fields for nodes At SortedPos and after will contain the
12753 // count of outstanding operands.
12755 checkForCycles(&N, this);
12756 unsigned Degree = N.getNumOperands();
12757 if (Degree == 0) {
12758 // A node with no uses, add it to the result array immediately.
12759 N.setNodeId(DAGSize++);
12760 allnodes_iterator Q(&N);
12761 if (Q != SortedPos)
12762 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12763 assert(SortedPos != AllNodes.end() && "Overran node list");
12764 ++SortedPos;
12765 } else {
12766 // Temporarily use the Node Id as scratch space for the degree count.
12767 N.setNodeId(Degree);
12768 }
12769 }
12770
12771 // Visit all the nodes. As we iterate, move nodes into sorted order,
12772 // such that by the time the end is reached all nodes will be sorted.
12773 for (SDNode &Node : allnodes()) {
12774 SDNode *N = &Node;
12775 checkForCycles(N, this);
12776 // N is in sorted position, so all its uses have one less operand
12777 // that needs to be sorted.
12778 for (SDNode *P : N->users()) {
12779 unsigned Degree = P->getNodeId();
12780 assert(Degree != 0 && "Invalid node degree");
12781 --Degree;
12782 if (Degree == 0) {
12783 // All of P's operands are sorted, so P may sorted now.
12784 P->setNodeId(DAGSize++);
12785 if (P->getIterator() != SortedPos)
12786 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12787 assert(SortedPos != AllNodes.end() && "Overran node list");
12788 ++SortedPos;
12789 } else {
12790 // Update P's outstanding operand count.
12791 P->setNodeId(Degree);
12792 }
12793 }
12794 if (Node.getIterator() == SortedPos) {
12795#ifndef NDEBUG
12797 SDNode *S = &*++I;
12798 dbgs() << "Overran sorted position:\n";
12799 S->dumprFull(this); dbgs() << "\n";
12800 dbgs() << "Checking if this is due to cycles\n";
12801 checkForCycles(this, true);
12802#endif
12803 llvm_unreachable(nullptr);
12804 }
12805 }
12806
12807 assert(SortedPos == AllNodes.end() &&
12808 "Topological sort incomplete!");
12809 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12810 "First node in topological sort is not the entry token!");
12811 assert(AllNodes.front().getNodeId() == 0 &&
12812 "First node in topological sort has non-zero id!");
12813 assert(AllNodes.front().getNumOperands() == 0 &&
12814 "First node in topological sort has operands!");
12815 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12816 "Last node in topologic sort has unexpected id!");
12817 assert(AllNodes.back().use_empty() &&
12818 "Last node in topologic sort has users!");
12819 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12820 return DAGSize;
12821}
12822
12824 SmallVectorImpl<const SDNode *> &SortedNodes) const {
12825 SortedNodes.clear();
12826 // Node -> remaining number of outstanding operands.
12827 DenseMap<const SDNode *, unsigned> RemainingOperands;
12828
12829 // Put nodes without any operands into SortedNodes first.
12830 for (const SDNode &N : allnodes()) {
12831 checkForCycles(&N, this);
12832 unsigned NumOperands = N.getNumOperands();
12833 if (NumOperands == 0)
12834 SortedNodes.push_back(&N);
12835 else
12836 // Record their total number of outstanding operands.
12837 RemainingOperands[&N] = NumOperands;
12838 }
12839
12840 // A node is pushed into SortedNodes when all of its operands (predecessors in
12841 // the graph) are also in SortedNodes.
12842 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
12843 const SDNode *N = SortedNodes[i];
12844 for (const SDNode *U : N->users()) {
12845 // HandleSDNode is never part of a DAG and therefore has no entry in
12846 // RemainingOperands.
12847 if (U->getOpcode() == ISD::HANDLENODE)
12848 continue;
12849 unsigned &NumRemOperands = RemainingOperands[U];
12850 assert(NumRemOperands && "Invalid number of remaining operands");
12851 --NumRemOperands;
12852 if (!NumRemOperands)
12853 SortedNodes.push_back(U);
12854 }
12855 }
12856
12857 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
12858 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
12859 "First node in topological sort is not the entry token");
12860 assert(SortedNodes.front()->getNumOperands() == 0 &&
12861 "First node in topological sort has operands");
12862}
12863
12864/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12865/// value is produced by SD.
12866void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12867 for (SDNode *SD : DB->getSDNodes()) {
12868 if (!SD)
12869 continue;
12870 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12871 SD->setHasDebugValue(true);
12872 }
12873 DbgInfo->add(DB, isParameter);
12874}
12875
12876void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12877
12879 SDValue NewMemOpChain) {
12880 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12881 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12882 // The new memory operation must have the same position as the old load in
12883 // terms of memory dependency. Create a TokenFactor for the old load and new
12884 // memory operation and update uses of the old load's output chain to use that
12885 // TokenFactor.
12886 if (OldChain == NewMemOpChain || OldChain.use_empty())
12887 return NewMemOpChain;
12888
12889 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12890 OldChain, NewMemOpChain);
12891 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12892 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12893 return TokenFactor;
12894}
12895
12897 SDValue NewMemOp) {
12898 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12899 SDValue OldChain = SDValue(OldLoad, 1);
12900 SDValue NewMemOpChain = NewMemOp.getValue(1);
12901 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12902}
12903
12905 Function **OutFunction) {
12906 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12907
12908 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12909 auto *Module = MF->getFunction().getParent();
12910 auto *Function = Module->getFunction(Symbol);
12911
12912 if (OutFunction != nullptr)
12913 *OutFunction = Function;
12914
12915 if (Function != nullptr) {
12916 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12917 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12918 }
12919
12920 std::string ErrorStr;
12921 raw_string_ostream ErrorFormatter(ErrorStr);
12922 ErrorFormatter << "Undefined external symbol ";
12923 ErrorFormatter << '"' << Symbol << '"';
12924 report_fatal_error(Twine(ErrorStr));
12925}
12926
12927//===----------------------------------------------------------------------===//
12928// SDNode Class
12929//===----------------------------------------------------------------------===//
12930
12933 return Const != nullptr && Const->isZero();
12934}
12935
12937 return V.isUndef() || isNullConstant(V);
12938}
12939
12942 return Const != nullptr && Const->isZero() && !Const->isNegative();
12943}
12944
12947 return Const != nullptr && Const->isAllOnes();
12948}
12949
12952 return Const != nullptr && Const->isOne();
12953}
12954
12957 return Const != nullptr && Const->isMinSignedValue();
12958}
12959
12960bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12961 unsigned OperandNo) {
12962 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12963 // TODO: Target-specific opcodes could be added.
12964 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12965 /*AllowTruncation*/ true)) {
12966 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12967 switch (Opcode) {
12968 case ISD::ADD:
12969 case ISD::OR:
12970 case ISD::XOR:
12971 case ISD::UMAX:
12972 return Const.isZero();
12973 case ISD::MUL:
12974 return Const.isOne();
12975 case ISD::AND:
12976 case ISD::UMIN:
12977 return Const.isAllOnes();
12978 case ISD::SMAX:
12979 return Const.isMinSignedValue();
12980 case ISD::SMIN:
12981 return Const.isMaxSignedValue();
12982 case ISD::SUB:
12983 case ISD::SHL:
12984 case ISD::SRA:
12985 case ISD::SRL:
12986 return OperandNo == 1 && Const.isZero();
12987 case ISD::UDIV:
12988 case ISD::SDIV:
12989 return OperandNo == 1 && Const.isOne();
12990 }
12991 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
12992 switch (Opcode) {
12993 case ISD::FADD:
12994 return ConstFP->isZero() &&
12995 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
12996 case ISD::FSUB:
12997 return OperandNo == 1 && ConstFP->isZero() &&
12998 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
12999 case ISD::FMUL:
13000 return ConstFP->isExactlyValue(1.0);
13001 case ISD::FDIV:
13002 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
13003 case ISD::FMINNUM:
13004 case ISD::FMAXNUM: {
13005 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13006 EVT VT = V.getValueType();
13007 const fltSemantics &Semantics = VT.getFltSemantics();
13008 APFloat NeutralAF = !Flags.hasNoNaNs()
13009 ? APFloat::getQNaN(Semantics)
13010 : !Flags.hasNoInfs()
13011 ? APFloat::getInf(Semantics)
13012 : APFloat::getLargest(Semantics);
13013 if (Opcode == ISD::FMAXNUM)
13014 NeutralAF.changeSign();
13015
13016 return ConstFP->isExactlyValue(NeutralAF);
13017 }
13018 }
13019 }
13020 return false;
13021}
13022
13024 while (V.getOpcode() == ISD::BITCAST)
13025 V = V.getOperand(0);
13026 return V;
13027}
13028
13030 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
13031 V = V.getOperand(0);
13032 return V;
13033}
13034
13036 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
13037 V = V.getOperand(0);
13038 return V;
13039}
13040
13042 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13043 SDValue InVec = V.getOperand(0);
13044 SDValue EltNo = V.getOperand(2);
13045 EVT VT = InVec.getValueType();
13046 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
13047 if (IndexC && VT.isFixedLengthVector() &&
13048 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
13049 !DemandedElts[IndexC->getZExtValue()]) {
13050 V = InVec;
13051 continue;
13052 }
13053 break;
13054 }
13055 return V;
13056}
13057
13059 while (V.getOpcode() == ISD::TRUNCATE)
13060 V = V.getOperand(0);
13061 return V;
13062}
13063
13064bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
13065 if (V.getOpcode() != ISD::XOR)
13066 return false;
13067 V = peekThroughBitcasts(V.getOperand(1));
13068 unsigned NumBits = V.getScalarValueSizeInBits();
13069 ConstantSDNode *C =
13070 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
13071 return C && (C->getAPIntValue().countr_one() >= NumBits);
13072}
13073
13075 bool AllowTruncation) {
13076 EVT VT = N.getValueType();
13077 APInt DemandedElts = VT.isFixedLengthVector()
13079 : APInt(1, 1);
13080 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
13081}
13082
13084 bool AllowUndefs,
13085 bool AllowTruncation) {
13087 return CN;
13088
13089 // SplatVectors can truncate their operands. Ignore that case here unless
13090 // AllowTruncation is set.
13091 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
13092 EVT VecEltVT = N->getValueType(0).getVectorElementType();
13093 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
13094 EVT CVT = CN->getValueType(0);
13095 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13096 if (AllowTruncation || CVT == VecEltVT)
13097 return CN;
13098 }
13099 }
13100
13102 BitVector UndefElements;
13103 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13104
13105 // BuildVectors can truncate their operands. Ignore that case here unless
13106 // AllowTruncation is set.
13107 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13108 if (CN && (UndefElements.none() || AllowUndefs)) {
13109 EVT CVT = CN->getValueType(0);
13110 EVT NSVT = N.getValueType().getScalarType();
13111 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13112 if (AllowTruncation || (CVT == NSVT))
13113 return CN;
13114 }
13115 }
13116
13117 return nullptr;
13118}
13119
13121 EVT VT = N.getValueType();
13122 APInt DemandedElts = VT.isFixedLengthVector()
13124 : APInt(1, 1);
13125 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13126}
13127
13129 const APInt &DemandedElts,
13130 bool AllowUndefs) {
13132 return CN;
13133
13135 BitVector UndefElements;
13136 ConstantFPSDNode *CN =
13137 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13138 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13139 if (CN && (UndefElements.none() || AllowUndefs))
13140 return CN;
13141 }
13142
13143 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13144 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13145 return CN;
13146
13147 return nullptr;
13148}
13149
13150bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13151 // TODO: may want to use peekThroughBitcast() here.
13152 ConstantSDNode *C =
13153 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13154 return C && C->isZero();
13155}
13156
13157bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13158 ConstantSDNode *C =
13159 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13160 return C && C->isOne();
13161}
13162
13163bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13164 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13165 return C && C->isExactlyValue(1.0);
13166}
13167
13168bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13170 unsigned BitWidth = N.getScalarValueSizeInBits();
13171 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13172 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13173}
13174
13175bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13176 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13177 return C && APInt::isSameValue(C->getAPIntValue(),
13178 APInt(C->getAPIntValue().getBitWidth(), 1));
13179}
13180
13181bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13183 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13184 return C && C->isZero();
13185}
13186
13187bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13188 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13189 return C && C->isZero();
13190}
13191
13195
13196MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
13197 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
13198 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
13199 MemSDNodeBits.IsVolatile = MMO->isVolatile();
13200 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
13201 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
13202 MemSDNodeBits.IsInvariant = MMO->isInvariant();
13203
13204 // We check here that the size of the memory operand fits within the size of
13205 // the MMO. This is because the MMO might indicate only a possible address
13206 // range instead of specifying the affected memory addresses precisely.
13207 assert(
13208 (!MMO->getType().isValid() ||
13209 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
13210 "Size mismatch!");
13211}
13212
13213/// Profile - Gather unique data for the node.
13214///
13216 AddNodeIDNode(ID, this);
13217}
13218
13219namespace {
13220
13221 struct EVTArray {
13222 std::vector<EVT> VTs;
13223
13224 EVTArray() {
13225 VTs.reserve(MVT::VALUETYPE_SIZE);
13226 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13227 VTs.push_back(MVT((MVT::SimpleValueType)i));
13228 }
13229 };
13230
13231} // end anonymous namespace
13232
13233/// getValueTypeList - Return a pointer to the specified value type.
13234///
13235const EVT *SDNode::getValueTypeList(MVT VT) {
13236 static EVTArray SimpleVTArray;
13237
13238 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13239 return &SimpleVTArray.VTs[VT.SimpleTy];
13240}
13241
13242/// hasAnyUseOfValue - Return true if there are any use of the indicated
13243/// value. This method ignores uses of other values defined by this operation.
13244bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13245 assert(Value < getNumValues() && "Bad value!");
13246
13247 for (SDUse &U : uses())
13248 if (U.getResNo() == Value)
13249 return true;
13250
13251 return false;
13252}
13253
13254/// isOnlyUserOf - Return true if this node is the only use of N.
13255bool SDNode::isOnlyUserOf(const SDNode *N) const {
13256 bool Seen = false;
13257 for (const SDNode *User : N->users()) {
13258 if (User == this)
13259 Seen = true;
13260 else
13261 return false;
13262 }
13263
13264 return Seen;
13265}
13266
13267/// Return true if the only users of N are contained in Nodes.
13269 bool Seen = false;
13270 for (const SDNode *User : N->users()) {
13271 if (llvm::is_contained(Nodes, User))
13272 Seen = true;
13273 else
13274 return false;
13275 }
13276
13277 return Seen;
13278}
13279
13280/// Return true if the referenced return value is an operand of N.
13281bool SDValue::isOperandOf(const SDNode *N) const {
13282 return is_contained(N->op_values(), *this);
13283}
13284
13285bool SDNode::isOperandOf(const SDNode *N) const {
13286 return any_of(N->op_values(),
13287 [this](SDValue Op) { return this == Op.getNode(); });
13288}
13289
13290/// reachesChainWithoutSideEffects - Return true if this operand (which must
13291/// be a chain) reaches the specified operand without crossing any
13292/// side-effecting instructions on any chain path. In practice, this looks
13293/// through token factors and non-volatile loads. In order to remain efficient,
13294/// this only looks a couple of nodes in, it does not do an exhaustive search.
13295///
13296/// Note that we only need to examine chains when we're searching for
13297/// side-effects; SelectionDAG requires that all side-effects are represented
13298/// by chains, even if another operand would force a specific ordering. This
13299/// constraint is necessary to allow transformations like splitting loads.
13301 unsigned Depth) const {
13302 if (*this == Dest) return true;
13303
13304 // Don't search too deeply, we just want to be able to see through
13305 // TokenFactor's etc.
13306 if (Depth == 0) return false;
13307
13308 // If this is a token factor, all inputs to the TF happen in parallel.
13309 if (getOpcode() == ISD::TokenFactor) {
13310 // First, try a shallow search.
13311 if (is_contained((*this)->ops(), Dest)) {
13312 // We found the chain we want as an operand of this TokenFactor.
13313 // Essentially, we reach the chain without side-effects if we could
13314 // serialize the TokenFactor into a simple chain of operations with
13315 // Dest as the last operation. This is automatically true if the
13316 // chain has one use: there are no other ordering constraints.
13317 // If the chain has more than one use, we give up: some other
13318 // use of Dest might force a side-effect between Dest and the current
13319 // node.
13320 if (Dest.hasOneUse())
13321 return true;
13322 }
13323 // Next, try a deep search: check whether every operand of the TokenFactor
13324 // reaches Dest.
13325 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13326 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13327 });
13328 }
13329
13330 // Loads don't have side effects, look through them.
13331 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13332 if (Ld->isUnordered())
13333 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13334 }
13335 return false;
13336}
13337
13338bool SDNode::hasPredecessor(const SDNode *N) const {
13341 Worklist.push_back(this);
13342 return hasPredecessorHelper(N, Visited, Worklist);
13343}
13344
13346 this->Flags &= Flags;
13347}
13348
13349SDValue
13351 ArrayRef<ISD::NodeType> CandidateBinOps,
13352 bool AllowPartials) {
13353 // The pattern must end in an extract from index 0.
13354 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13355 !isNullConstant(Extract->getOperand(1)))
13356 return SDValue();
13357
13358 // Match against one of the candidate binary ops.
13359 SDValue Op = Extract->getOperand(0);
13360 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13361 return Op.getOpcode() == unsigned(BinOp);
13362 }))
13363 return SDValue();
13364
13365 // Floating-point reductions may require relaxed constraints on the final step
13366 // of the reduction because they may reorder intermediate operations.
13367 unsigned CandidateBinOp = Op.getOpcode();
13368 if (Op.getValueType().isFloatingPoint()) {
13369 SDNodeFlags Flags = Op->getFlags();
13370 switch (CandidateBinOp) {
13371 case ISD::FADD:
13372 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13373 return SDValue();
13374 break;
13375 default:
13376 llvm_unreachable("Unhandled FP opcode for binop reduction");
13377 }
13378 }
13379
13380 // Matching failed - attempt to see if we did enough stages that a partial
13381 // reduction from a subvector is possible.
13382 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13383 if (!AllowPartials || !Op)
13384 return SDValue();
13385 EVT OpVT = Op.getValueType();
13386 EVT OpSVT = OpVT.getScalarType();
13387 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13388 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13389 return SDValue();
13390 BinOp = (ISD::NodeType)CandidateBinOp;
13391 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13392 };
13393
13394 // At each stage, we're looking for something that looks like:
13395 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13396 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13397 // i32 undef, i32 undef, i32 undef, i32 undef>
13398 // %a = binop <8 x i32> %op, %s
13399 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13400 // we expect something like:
13401 // <4,5,6,7,u,u,u,u>
13402 // <2,3,u,u,u,u,u,u>
13403 // <1,u,u,u,u,u,u,u>
13404 // While a partial reduction match would be:
13405 // <2,3,u,u,u,u,u,u>
13406 // <1,u,u,u,u,u,u,u>
13407 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13408 SDValue PrevOp;
13409 for (unsigned i = 0; i < Stages; ++i) {
13410 unsigned MaskEnd = (1 << i);
13411
13412 if (Op.getOpcode() != CandidateBinOp)
13413 return PartialReduction(PrevOp, MaskEnd);
13414
13415 SDValue Op0 = Op.getOperand(0);
13416 SDValue Op1 = Op.getOperand(1);
13417
13419 if (Shuffle) {
13420 Op = Op1;
13421 } else {
13422 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13423 Op = Op0;
13424 }
13425
13426 // The first operand of the shuffle should be the same as the other operand
13427 // of the binop.
13428 if (!Shuffle || Shuffle->getOperand(0) != Op)
13429 return PartialReduction(PrevOp, MaskEnd);
13430
13431 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13432 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13433 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13434 return PartialReduction(PrevOp, MaskEnd);
13435
13436 PrevOp = Op;
13437 }
13438
13439 // Handle subvector reductions, which tend to appear after the shuffle
13440 // reduction stages.
13441 while (Op.getOpcode() == CandidateBinOp) {
13442 unsigned NumElts = Op.getValueType().getVectorNumElements();
13443 SDValue Op0 = Op.getOperand(0);
13444 SDValue Op1 = Op.getOperand(1);
13445 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13447 Op0.getOperand(0) != Op1.getOperand(0))
13448 break;
13449 SDValue Src = Op0.getOperand(0);
13450 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13451 if (NumSrcElts != (2 * NumElts))
13452 break;
13453 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13454 Op1.getConstantOperandAPInt(1) == NumElts) &&
13455 !(Op1.getConstantOperandAPInt(1) == 0 &&
13456 Op0.getConstantOperandAPInt(1) == NumElts))
13457 break;
13458 Op = Src;
13459 }
13460
13461 BinOp = (ISD::NodeType)CandidateBinOp;
13462 return Op;
13463}
13464
13466 EVT VT = N->getValueType(0);
13467 EVT EltVT = VT.getVectorElementType();
13468 unsigned NE = VT.getVectorNumElements();
13469
13470 SDLoc dl(N);
13471
13472 // If ResNE is 0, fully unroll the vector op.
13473 if (ResNE == 0)
13474 ResNE = NE;
13475 else if (NE > ResNE)
13476 NE = ResNE;
13477
13478 if (N->getNumValues() == 2) {
13479 SmallVector<SDValue, 8> Scalars0, Scalars1;
13480 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13481 EVT VT1 = N->getValueType(1);
13482 EVT EltVT1 = VT1.getVectorElementType();
13483
13484 unsigned i;
13485 for (i = 0; i != NE; ++i) {
13486 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13487 SDValue Operand = N->getOperand(j);
13488 EVT OperandVT = Operand.getValueType();
13489
13490 // A vector operand; extract a single element.
13491 EVT OperandEltVT = OperandVT.getVectorElementType();
13492 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13493 }
13494
13495 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13496 Scalars0.push_back(EltOp);
13497 Scalars1.push_back(EltOp.getValue(1));
13498 }
13499
13500 for (; i < ResNE; ++i) {
13501 Scalars0.push_back(getUNDEF(EltVT));
13502 Scalars1.push_back(getUNDEF(EltVT1));
13503 }
13504
13505 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13506 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13507 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13508 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13509 return getMergeValues({Vec0, Vec1}, dl);
13510 }
13511
13512 assert(N->getNumValues() == 1 &&
13513 "Can't unroll a vector with multiple results!");
13514
13516 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13517
13518 unsigned i;
13519 for (i= 0; i != NE; ++i) {
13520 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13521 SDValue Operand = N->getOperand(j);
13522 EVT OperandVT = Operand.getValueType();
13523 if (OperandVT.isVector()) {
13524 // A vector operand; extract a single element.
13525 EVT OperandEltVT = OperandVT.getVectorElementType();
13526 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13527 } else {
13528 // A scalar operand; just use it as is.
13529 Operands[j] = Operand;
13530 }
13531 }
13532
13533 switch (N->getOpcode()) {
13534 default: {
13535 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13536 N->getFlags()));
13537 break;
13538 }
13539 case ISD::VSELECT:
13540 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13541 break;
13542 case ISD::SHL:
13543 case ISD::SRA:
13544 case ISD::SRL:
13545 case ISD::ROTL:
13546 case ISD::ROTR:
13547 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13548 getShiftAmountOperand(Operands[0].getValueType(),
13549 Operands[1])));
13550 break;
13552 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13553 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13554 Operands[0],
13555 getValueType(ExtVT)));
13556 break;
13557 }
13558 case ISD::ADDRSPACECAST: {
13559 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13560 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13561 ASC->getSrcAddressSpace(),
13562 ASC->getDestAddressSpace()));
13563 break;
13564 }
13565 }
13566 }
13567
13568 for (; i < ResNE; ++i)
13569 Scalars.push_back(getUNDEF(EltVT));
13570
13571 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13572 return getBuildVector(VecVT, dl, Scalars);
13573}
13574
13575std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13576 SDNode *N, unsigned ResNE) {
13577 unsigned Opcode = N->getOpcode();
13578 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13579 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13580 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13581 "Expected an overflow opcode");
13582
13583 EVT ResVT = N->getValueType(0);
13584 EVT OvVT = N->getValueType(1);
13585 EVT ResEltVT = ResVT.getVectorElementType();
13586 EVT OvEltVT = OvVT.getVectorElementType();
13587 SDLoc dl(N);
13588
13589 // If ResNE is 0, fully unroll the vector op.
13590 unsigned NE = ResVT.getVectorNumElements();
13591 if (ResNE == 0)
13592 ResNE = NE;
13593 else if (NE > ResNE)
13594 NE = ResNE;
13595
13596 SmallVector<SDValue, 8> LHSScalars;
13597 SmallVector<SDValue, 8> RHSScalars;
13598 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13599 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13600
13601 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13602 SDVTList VTs = getVTList(ResEltVT, SVT);
13603 SmallVector<SDValue, 8> ResScalars;
13604 SmallVector<SDValue, 8> OvScalars;
13605 for (unsigned i = 0; i < NE; ++i) {
13606 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13607 SDValue Ov =
13608 getSelect(dl, OvEltVT, Res.getValue(1),
13609 getBoolConstant(true, dl, OvEltVT, ResVT),
13610 getConstant(0, dl, OvEltVT));
13611
13612 ResScalars.push_back(Res);
13613 OvScalars.push_back(Ov);
13614 }
13615
13616 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13617 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13618
13619 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13620 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13621 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13622 getBuildVector(NewOvVT, dl, OvScalars));
13623}
13624
13627 unsigned Bytes,
13628 int Dist) const {
13629 if (LD->isVolatile() || Base->isVolatile())
13630 return false;
13631 // TODO: probably too restrictive for atomics, revisit
13632 if (!LD->isSimple())
13633 return false;
13634 if (LD->isIndexed() || Base->isIndexed())
13635 return false;
13636 if (LD->getChain() != Base->getChain())
13637 return false;
13638 EVT VT = LD->getMemoryVT();
13639 if (VT.getSizeInBits() / 8 != Bytes)
13640 return false;
13641
13642 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13643 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13644
13645 int64_t Offset = 0;
13646 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13647 return (Dist * (int64_t)Bytes == Offset);
13648 return false;
13649}
13650
13651/// InferPtrAlignment - Infer alignment of a load / store address. Return
13652/// std::nullopt if it cannot be inferred.
13654 // If this is a GlobalAddress + cst, return the alignment.
13655 const GlobalValue *GV = nullptr;
13656 int64_t GVOffset = 0;
13657 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13658 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13659 KnownBits Known(PtrWidth);
13661 unsigned AlignBits = Known.countMinTrailingZeros();
13662 if (AlignBits)
13663 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13664 }
13665
13666 // If this is a direct reference to a stack slot, use information about the
13667 // stack slot's alignment.
13668 int FrameIdx = INT_MIN;
13669 int64_t FrameOffset = 0;
13671 FrameIdx = FI->getIndex();
13672 } else if (isBaseWithConstantOffset(Ptr) &&
13674 // Handle FI+Cst
13675 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13676 FrameOffset = Ptr.getConstantOperandVal(1);
13677 }
13678
13679 if (FrameIdx != INT_MIN) {
13681 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13682 }
13683
13684 return std::nullopt;
13685}
13686
13687/// Split the scalar node with EXTRACT_ELEMENT using the provided
13688/// VTs and return the low/high part.
13689std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13690 const SDLoc &DL,
13691 const EVT &LoVT,
13692 const EVT &HiVT) {
13693 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13694 "Split node must be a scalar type");
13695 SDValue Lo =
13697 SDValue Hi =
13699 return std::make_pair(Lo, Hi);
13700}
13701
13702/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13703/// which is split (or expanded) into two not necessarily identical pieces.
13704std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13705 // Currently all types are split in half.
13706 EVT LoVT, HiVT;
13707 if (!VT.isVector())
13708 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13709 else
13710 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13711
13712 return std::make_pair(LoVT, HiVT);
13713}
13714
13715/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13716/// type, dependent on an enveloping VT that has been split into two identical
13717/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13718std::pair<EVT, EVT>
13720 bool *HiIsEmpty) const {
13721 EVT EltTp = VT.getVectorElementType();
13722 // Examples:
13723 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13724 // custom VL=9 with enveloping VL=8/8 yields 8/1
13725 // custom VL=10 with enveloping VL=8/8 yields 8/2
13726 // etc.
13727 ElementCount VTNumElts = VT.getVectorElementCount();
13728 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13729 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13730 "Mixing fixed width and scalable vectors when enveloping a type");
13731 EVT LoVT, HiVT;
13732 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13733 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13734 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13735 *HiIsEmpty = false;
13736 } else {
13737 // Flag that hi type has zero storage size, but return split envelop type
13738 // (this would be easier if vector types with zero elements were allowed).
13739 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13740 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13741 *HiIsEmpty = true;
13742 }
13743 return std::make_pair(LoVT, HiVT);
13744}
13745
13746/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13747/// low/high part.
13748std::pair<SDValue, SDValue>
13749SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13750 const EVT &HiVT) {
13751 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13752 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13753 "Splitting vector with an invalid mixture of fixed and scalable "
13754 "vector types");
13756 N.getValueType().getVectorMinNumElements() &&
13757 "More vector elements requested than available!");
13758 SDValue Lo, Hi;
13759 Lo = getExtractSubvector(DL, LoVT, N, 0);
13760 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13761 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13762 // IDX with the runtime scaling factor of the result vector type. For
13763 // fixed-width result vectors, that runtime scaling factor is 1.
13766 return std::make_pair(Lo, Hi);
13767}
13768
13769std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13770 const SDLoc &DL) {
13771 // Split the vector length parameter.
13772 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13773 EVT VT = N.getValueType();
13775 "Expecting the mask to be an evenly-sized vector");
13776 SDValue HalfNumElts = getElementCount(
13778 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13779 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13780 return std::make_pair(Lo, Hi);
13781}
13782
13783/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13785 EVT VT = N.getValueType();
13788 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13789}
13790
13793 unsigned Start, unsigned Count,
13794 EVT EltVT) {
13795 EVT VT = Op.getValueType();
13796 if (Count == 0)
13798 if (EltVT == EVT())
13799 EltVT = VT.getVectorElementType();
13800 SDLoc SL(Op);
13801 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13802 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13803 }
13804}
13805
13806// getAddressSpace - Return the address space this GlobalAddress belongs to.
13808 return getGlobal()->getType()->getAddressSpace();
13809}
13810
13813 return Val.MachineCPVal->getType();
13814 return Val.ConstVal->getType();
13815}
13816
13817bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13818 unsigned &SplatBitSize,
13819 bool &HasAnyUndefs,
13820 unsigned MinSplatBits,
13821 bool IsBigEndian) const {
13822 EVT VT = getValueType(0);
13823 assert(VT.isVector() && "Expected a vector type");
13824 unsigned VecWidth = VT.getSizeInBits();
13825 if (MinSplatBits > VecWidth)
13826 return false;
13827
13828 // FIXME: The widths are based on this node's type, but build vectors can
13829 // truncate their operands.
13830 SplatValue = APInt(VecWidth, 0);
13831 SplatUndef = APInt(VecWidth, 0);
13832
13833 // Get the bits. Bits with undefined values (when the corresponding element
13834 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13835 // in SplatValue. If any of the values are not constant, give up and return
13836 // false.
13837 unsigned int NumOps = getNumOperands();
13838 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13839 unsigned EltWidth = VT.getScalarSizeInBits();
13840
13841 for (unsigned j = 0; j < NumOps; ++j) {
13842 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13843 SDValue OpVal = getOperand(i);
13844 unsigned BitPos = j * EltWidth;
13845
13846 if (OpVal.isUndef())
13847 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13848 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13849 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13850 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13851 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13852 else
13853 return false;
13854 }
13855
13856 // The build_vector is all constants or undefs. Find the smallest element
13857 // size that splats the vector.
13858 HasAnyUndefs = (SplatUndef != 0);
13859
13860 // FIXME: This does not work for vectors with elements less than 8 bits.
13861 while (VecWidth > 8) {
13862 // If we can't split in half, stop here.
13863 if (VecWidth & 1)
13864 break;
13865
13866 unsigned HalfSize = VecWidth / 2;
13867 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13868 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13869 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13870 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13871
13872 // If the two halves do not match (ignoring undef bits), stop here.
13873 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13874 MinSplatBits > HalfSize)
13875 break;
13876
13877 SplatValue = HighValue | LowValue;
13878 SplatUndef = HighUndef & LowUndef;
13879
13880 VecWidth = HalfSize;
13881 }
13882
13883 // FIXME: The loop above only tries to split in halves. But if the input
13884 // vector for example is <3 x i16> it wouldn't be able to detect a
13885 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13886 // optimizations. I guess that back in the days when this helper was created
13887 // vectors normally was power-of-2 sized.
13888
13889 SplatBitSize = VecWidth;
13890 return true;
13891}
13892
13894 BitVector *UndefElements) const {
13895 unsigned NumOps = getNumOperands();
13896 if (UndefElements) {
13897 UndefElements->clear();
13898 UndefElements->resize(NumOps);
13899 }
13900 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13901 if (!DemandedElts)
13902 return SDValue();
13903 SDValue Splatted;
13904 for (unsigned i = 0; i != NumOps; ++i) {
13905 if (!DemandedElts[i])
13906 continue;
13907 SDValue Op = getOperand(i);
13908 if (Op.isUndef()) {
13909 if (UndefElements)
13910 (*UndefElements)[i] = true;
13911 } else if (!Splatted) {
13912 Splatted = Op;
13913 } else if (Splatted != Op) {
13914 return SDValue();
13915 }
13916 }
13917
13918 if (!Splatted) {
13919 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13920 assert(getOperand(FirstDemandedIdx).isUndef() &&
13921 "Can only have a splat without a constant for all undefs.");
13922 return getOperand(FirstDemandedIdx);
13923 }
13924
13925 return Splatted;
13926}
13927
13929 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13930 return getSplatValue(DemandedElts, UndefElements);
13931}
13932
13934 SmallVectorImpl<SDValue> &Sequence,
13935 BitVector *UndefElements) const {
13936 unsigned NumOps = getNumOperands();
13937 Sequence.clear();
13938 if (UndefElements) {
13939 UndefElements->clear();
13940 UndefElements->resize(NumOps);
13941 }
13942 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13943 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13944 return false;
13945
13946 // Set the undefs even if we don't find a sequence (like getSplatValue).
13947 if (UndefElements)
13948 for (unsigned I = 0; I != NumOps; ++I)
13949 if (DemandedElts[I] && getOperand(I).isUndef())
13950 (*UndefElements)[I] = true;
13951
13952 // Iteratively widen the sequence length looking for repetitions.
13953 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13954 Sequence.append(SeqLen, SDValue());
13955 for (unsigned I = 0; I != NumOps; ++I) {
13956 if (!DemandedElts[I])
13957 continue;
13958 SDValue &SeqOp = Sequence[I % SeqLen];
13960 if (Op.isUndef()) {
13961 if (!SeqOp)
13962 SeqOp = Op;
13963 continue;
13964 }
13965 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13966 Sequence.clear();
13967 break;
13968 }
13969 SeqOp = Op;
13970 }
13971 if (!Sequence.empty())
13972 return true;
13973 }
13974
13975 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13976 return false;
13977}
13978
13980 BitVector *UndefElements) const {
13981 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13982 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
13983}
13984
13987 BitVector *UndefElements) const {
13989 getSplatValue(DemandedElts, UndefElements));
13990}
13991
13994 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
13995}
13996
13999 BitVector *UndefElements) const {
14001 getSplatValue(DemandedElts, UndefElements));
14002}
14003
14008
14009int32_t
14011 uint32_t BitWidth) const {
14012 if (ConstantFPSDNode *CN =
14014 bool IsExact;
14015 APSInt IntVal(BitWidth);
14016 const APFloat &APF = CN->getValueAPF();
14017 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
14018 APFloat::opOK ||
14019 !IsExact)
14020 return -1;
14021
14022 return IntVal.exactLogBase2();
14023 }
14024 return -1;
14025}
14026
14028 bool IsLittleEndian, unsigned DstEltSizeInBits,
14029 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
14030 // Early-out if this contains anything but Undef/Constant/ConstantFP.
14031 if (!isConstant())
14032 return false;
14033
14034 unsigned NumSrcOps = getNumOperands();
14035 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
14036 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14037 "Invalid bitcast scale");
14038
14039 // Extract raw src bits.
14040 SmallVector<APInt> SrcBitElements(NumSrcOps,
14041 APInt::getZero(SrcEltSizeInBits));
14042 BitVector SrcUndeElements(NumSrcOps, false);
14043
14044 for (unsigned I = 0; I != NumSrcOps; ++I) {
14046 if (Op.isUndef()) {
14047 SrcUndeElements.set(I);
14048 continue;
14049 }
14050 auto *CInt = dyn_cast<ConstantSDNode>(Op);
14051 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
14052 assert((CInt || CFP) && "Unknown constant");
14053 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
14054 : CFP->getValueAPF().bitcastToAPInt();
14055 }
14056
14057 // Recast to dst width.
14058 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
14059 SrcBitElements, UndefElements, SrcUndeElements);
14060 return true;
14061}
14062
14063void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
14064 unsigned DstEltSizeInBits,
14065 SmallVectorImpl<APInt> &DstBitElements,
14066 ArrayRef<APInt> SrcBitElements,
14067 BitVector &DstUndefElements,
14068 const BitVector &SrcUndefElements) {
14069 unsigned NumSrcOps = SrcBitElements.size();
14070 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
14071 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14072 "Invalid bitcast scale");
14073 assert(NumSrcOps == SrcUndefElements.size() &&
14074 "Vector size mismatch");
14075
14076 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
14077 DstUndefElements.clear();
14078 DstUndefElements.resize(NumDstOps, false);
14079 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
14080
14081 // Concatenate src elements constant bits together into dst element.
14082 if (SrcEltSizeInBits <= DstEltSizeInBits) {
14083 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
14084 for (unsigned I = 0; I != NumDstOps; ++I) {
14085 DstUndefElements.set(I);
14086 APInt &DstBits = DstBitElements[I];
14087 for (unsigned J = 0; J != Scale; ++J) {
14088 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14089 if (SrcUndefElements[Idx])
14090 continue;
14091 DstUndefElements.reset(I);
14092 const APInt &SrcBits = SrcBitElements[Idx];
14093 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
14094 "Illegal constant bitwidths");
14095 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
14096 }
14097 }
14098 return;
14099 }
14100
14101 // Split src element constant bits into dst elements.
14102 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14103 for (unsigned I = 0; I != NumSrcOps; ++I) {
14104 if (SrcUndefElements[I]) {
14105 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14106 continue;
14107 }
14108 const APInt &SrcBits = SrcBitElements[I];
14109 for (unsigned J = 0; J != Scale; ++J) {
14110 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14111 APInt &DstBits = DstBitElements[Idx];
14112 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14113 }
14114 }
14115}
14116
14118 for (const SDValue &Op : op_values()) {
14119 unsigned Opc = Op.getOpcode();
14120 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14121 return false;
14122 }
14123 return true;
14124}
14125
14126std::optional<std::pair<APInt, APInt>>
14128 unsigned NumOps = getNumOperands();
14129 if (NumOps < 2)
14130 return std::nullopt;
14131
14134 return std::nullopt;
14135
14136 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14137 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
14138 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
14139
14140 if (Stride.isZero())
14141 return std::nullopt;
14142
14143 for (unsigned i = 2; i < NumOps; ++i) {
14145 return std::nullopt;
14146
14147 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
14148 if (Val != (Start + (Stride * i)))
14149 return std::nullopt;
14150 }
14151
14152 return std::make_pair(Start, Stride);
14153}
14154
14156 // Find the first non-undef value in the shuffle mask.
14157 unsigned i, e;
14158 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14159 /* search */;
14160
14161 // If all elements are undefined, this shuffle can be considered a splat
14162 // (although it should eventually get simplified away completely).
14163 if (i == e)
14164 return true;
14165
14166 // Make sure all remaining elements are either undef or the same as the first
14167 // non-undef value.
14168 for (int Idx = Mask[i]; i != e; ++i)
14169 if (Mask[i] >= 0 && Mask[i] != Idx)
14170 return false;
14171 return true;
14172}
14173
14174// Returns true if it is a constant integer BuildVector or constant integer,
14175// possibly hidden by a bitcast.
14177 SDValue N, bool AllowOpaques) const {
14179
14180 if (auto *C = dyn_cast<ConstantSDNode>(N))
14181 return AllowOpaques || !C->isOpaque();
14182
14184 return true;
14185
14186 // Treat a GlobalAddress supporting constant offset folding as a
14187 // constant integer.
14188 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14189 if (GA->getOpcode() == ISD::GlobalAddress &&
14190 TLI->isOffsetFoldingLegal(GA))
14191 return true;
14192
14193 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14194 isa<ConstantSDNode>(N.getOperand(0)))
14195 return true;
14196 return false;
14197}
14198
14199// Returns true if it is a constant float BuildVector or constant float.
14202 return true;
14203
14205 return true;
14206
14207 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14208 isa<ConstantFPSDNode>(N.getOperand(0)))
14209 return true;
14210
14211 return false;
14212}
14213
14214std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14215 ConstantSDNode *Const =
14216 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14217 if (!Const)
14218 return std::nullopt;
14219
14220 EVT VT = N->getValueType(0);
14221 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14222 switch (TLI->getBooleanContents(N.getValueType())) {
14224 if (CVal.isOne())
14225 return true;
14226 if (CVal.isZero())
14227 return false;
14228 return std::nullopt;
14230 if (CVal.isAllOnes())
14231 return true;
14232 if (CVal.isZero())
14233 return false;
14234 return std::nullopt;
14236 return CVal[0];
14237 }
14238 llvm_unreachable("Unknown BooleanContent enum");
14239}
14240
14241void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14242 assert(!Node->OperandList && "Node already has operands");
14244 "too many operands to fit into SDNode");
14245 SDUse *Ops = OperandRecycler.allocate(
14246 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14247
14248 bool IsDivergent = false;
14249 for (unsigned I = 0; I != Vals.size(); ++I) {
14250 Ops[I].setUser(Node);
14251 Ops[I].setInitial(Vals[I]);
14252 EVT VT = Ops[I].getValueType();
14253
14254 // Skip Chain. It does not carry divergence.
14255 if (VT != MVT::Other &&
14256 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14257 Ops[I].getNode()->isDivergent()) {
14258 IsDivergent = true;
14259 }
14260 }
14261 Node->NumOperands = Vals.size();
14262 Node->OperandList = Ops;
14263 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14264 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14265 Node->SDNodeBits.IsDivergent = IsDivergent;
14266 }
14267 checkForCycles(Node);
14268}
14269
14272 size_t Limit = SDNode::getMaxNumOperands();
14273 while (Vals.size() > Limit) {
14274 unsigned SliceIdx = Vals.size() - Limit;
14275 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14276 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14277 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14278 Vals.emplace_back(NewTF);
14279 }
14280 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14281}
14282
14284 EVT VT, SDNodeFlags Flags) {
14285 switch (Opcode) {
14286 default:
14287 return SDValue();
14288 case ISD::ADD:
14289 case ISD::OR:
14290 case ISD::XOR:
14291 case ISD::UMAX:
14292 return getConstant(0, DL, VT);
14293 case ISD::MUL:
14294 return getConstant(1, DL, VT);
14295 case ISD::AND:
14296 case ISD::UMIN:
14297 return getAllOnesConstant(DL, VT);
14298 case ISD::SMAX:
14300 case ISD::SMIN:
14302 case ISD::FADD:
14303 // If flags allow, prefer positive zero since it's generally cheaper
14304 // to materialize on most targets.
14305 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14306 case ISD::FMUL:
14307 return getConstantFP(1.0, DL, VT);
14308 case ISD::FMINNUM:
14309 case ISD::FMAXNUM: {
14310 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14311 const fltSemantics &Semantics = VT.getFltSemantics();
14312 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14313 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14314 APFloat::getLargest(Semantics);
14315 if (Opcode == ISD::FMAXNUM)
14316 NeutralAF.changeSign();
14317
14318 return getConstantFP(NeutralAF, DL, VT);
14319 }
14320 case ISD::FMINIMUM:
14321 case ISD::FMAXIMUM: {
14322 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14323 const fltSemantics &Semantics = VT.getFltSemantics();
14324 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14325 : APFloat::getLargest(Semantics);
14326 if (Opcode == ISD::FMAXIMUM)
14327 NeutralAF.changeSign();
14328
14329 return getConstantFP(NeutralAF, DL, VT);
14330 }
14331
14332 }
14333}
14334
14335/// Helper used to make a call to a library function that has one argument of
14336/// pointer type.
14337///
14338/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14339/// used to get or set floating-point state. They have one argument of pointer
14340/// type, which points to the memory region containing bits of the
14341/// floating-point state. The value returned by such function is ignored in the
14342/// created call.
14343///
14344/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14345/// \param Ptr Pointer used to save/load state.
14346/// \param InChain Ingoing token chain.
14347/// \returns Outgoing chain token.
14349 SDValue InChain,
14350 const SDLoc &DLoc) {
14351 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14353 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14354 RTLIB::LibcallImpl LibcallImpl =
14355 TLI->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
14356 if (LibcallImpl == RTLIB::Unsupported)
14357 reportFatalUsageError("emitting call to unsupported libcall");
14358
14359 SDValue Callee =
14360 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
14362 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14363 TLI->getLibcallImplCallingConv(LibcallImpl),
14364 Type::getVoidTy(*getContext()), Callee, std::move(Args));
14365 return TLI->LowerCallTo(CLI).second;
14366}
14367
14369 assert(From && To && "Invalid SDNode; empty source SDValue?");
14370 auto I = SDEI.find(From);
14371 if (I == SDEI.end())
14372 return;
14373
14374 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14375 // the iterator, hence the need to make a copy to prevent a use-after-free.
14376 NodeExtraInfo NEI = I->second;
14377 if (LLVM_LIKELY(!NEI.PCSections)) {
14378 // No deep copy required for the types of extra info set.
14379 //
14380 // FIXME: Investigate if other types of extra info also need deep copy. This
14381 // depends on the types of nodes they can be attached to: if some extra info
14382 // is only ever attached to nodes where a replacement To node is always the
14383 // node where later use and propagation of the extra info has the intended
14384 // semantics, no deep copy is required.
14385 SDEI[To] = std::move(NEI);
14386 return;
14387 }
14388
14389 const SDNode *EntrySDN = getEntryNode().getNode();
14390
14391 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14392 // through the replacement of From with To. Otherwise, replacements of a node
14393 // (From) with more complex nodes (To and its operands) may result in lost
14394 // extra info where the root node (To) is insignificant in further propagating
14395 // and using extra info when further lowering to MIR.
14396 //
14397 // In the first step pre-populate the visited set with the nodes reachable
14398 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14399 // DAG that is not new and should be left untouched.
14400 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14401 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14402 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14403 if (MaxDepth == 0) {
14404 // Remember this node in case we need to increase MaxDepth and continue
14405 // populating FromReach from this node.
14406 Leafs.emplace_back(N);
14407 return;
14408 }
14409 if (!FromReach.insert(N).second)
14410 return;
14411 for (const SDValue &Op : N->op_values())
14412 Self(Self, Op.getNode(), MaxDepth - 1);
14413 };
14414
14415 // Copy extra info to To and all its transitive operands (that are new).
14417 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14418 if (FromReach.contains(N))
14419 return true;
14420 if (!Visited.insert(N).second)
14421 return true;
14422 if (EntrySDN == N)
14423 return false;
14424 for (const SDValue &Op : N->op_values()) {
14425 if (N == To && Op.getNode() == EntrySDN) {
14426 // Special case: New node's operand is the entry node; just need to
14427 // copy extra info to new node.
14428 break;
14429 }
14430 if (!Self(Self, Op.getNode()))
14431 return false;
14432 }
14433 // Copy only if entry node was not reached.
14434 SDEI[N] = NEI;
14435 return true;
14436 };
14437
14438 // We first try with a lower MaxDepth, assuming that the path to common
14439 // operands between From and To is relatively short. This significantly
14440 // improves performance in the common case. The initial MaxDepth is big
14441 // enough to avoid retry in the common case; the last MaxDepth is large
14442 // enough to avoid having to use the fallback below (and protects from
14443 // potential stack exhaustion from recursion).
14444 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14445 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14446 // StartFrom is the previous (or initial) set of leafs reachable at the
14447 // previous maximum depth.
14449 std::swap(StartFrom, Leafs);
14450 for (const SDNode *N : StartFrom)
14451 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14452 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14453 return;
14454 // This should happen very rarely (reached the entry node).
14455 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14456 assert(!Leafs.empty());
14457 }
14458
14459 // This should not happen - but if it did, that means the subgraph reachable
14460 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14461 // could not visit all reachable common operands. Consequently, we were able
14462 // to reach the entry node.
14463 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14464 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14465 // Best-effort fallback if assertions disabled.
14466 SDEI[To] = std::move(NEI);
14467}
14468
14469#ifndef NDEBUG
14470static void checkForCyclesHelper(const SDNode *N,
14473 const llvm::SelectionDAG *DAG) {
14474 // If this node has already been checked, don't check it again.
14475 if (Checked.count(N))
14476 return;
14477
14478 // If a node has already been visited on this depth-first walk, reject it as
14479 // a cycle.
14480 if (!Visited.insert(N).second) {
14481 errs() << "Detected cycle in SelectionDAG\n";
14482 dbgs() << "Offending node:\n";
14483 N->dumprFull(DAG); dbgs() << "\n";
14484 abort();
14485 }
14486
14487 for (const SDValue &Op : N->op_values())
14488 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14489
14490 Checked.insert(N);
14491 Visited.erase(N);
14492}
14493#endif
14494
14496 const llvm::SelectionDAG *DAG,
14497 bool force) {
14498#ifndef NDEBUG
14499 bool check = force;
14500#ifdef EXPENSIVE_CHECKS
14501 check = true;
14502#endif // EXPENSIVE_CHECKS
14503 if (check) {
14504 assert(N && "Checking nonexistent SDNode");
14507 checkForCyclesHelper(N, visited, checked, DAG);
14508 }
14509#endif // !NDEBUG
14510}
14511
14512void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14513 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14514}
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isConstant(const MachineInstr &MI)
constexpr LLT S1
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
static std::optional< bool > isBigEndian(const SmallDenseMap< int64_t, int64_t, 8 > &MemOffset2Idx, int64_t LowestIdx)
Given a map from byte offsets in memory to indices in a load/store, determine if that map corresponds...
#define __asan_unpoison_memory_region(p, size)
Definition Compiler.h:592
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:335
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
This file defines a hash set that can be used to remove duplication of nodes in a graph.
iv users
Definition IVUsers.cpp:48
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static LVOptions Options
Definition LVOptions.cpp:25
static Register getMemsetValue(Register Val, LLT Ty, MachineIRBuilder &MIB)
static bool shouldLowerMemFuncForSize(const MachineFunction &MF)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
static Align getPrefTypeAlign(EVT VT, SelectionDAG &DAG)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register const TargetRegisterInfo * TRI
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
#define T
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
PowerPC Reduce CR logical Operation
const SmallVectorImpl< MachineOperand > & Cond
Remove Loads Into Fake Uses
Contains matchers for matching SelectionDAG nodes and values.
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, BatchAAResults *BatchAA)
static SDValue getFixedOrScalableQuantity(SelectionDAG &DAG, const SDLoc &DL, EVT VT, Ty Quantity)
static std::pair< SDValue, SDValue > getRuntimeCallSDValueHelper(SDValue Chain, const SDLoc &dl, TargetLowering::ArgListTy &&Args, const CallInst *CI, RTLIB::Libcall Call, SelectionDAG *DAG, const TargetLowering *TLI)
static SDValue getMemsetStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo)
Lower the call to 'memset' intrinsic function into a series of store operations.
static std::optional< APInt > FoldValueWithUndef(unsigned Opcode, const APInt &C1, bool IsUndef1, const APInt &C2, bool IsUndef2)
static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step, SelectionDAG &DAG)
static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC, SDVTList VTList, ArrayRef< SDValue > OpList)
static SDValue getMemsetStringVal(EVT VT, const SDLoc &dl, SelectionDAG &DAG, const TargetLowering &TLI, const ConstantDataArraySlice &Slice)
getMemsetStringVal - Similar to getMemsetValue.
static cl::opt< bool > EnableMemCpyDAGOpt("enable-memcpy-dag-opt", cl::Hidden, cl::init(true), cl::desc("Gang up loads and stores generated by inlining of memcpy"))
static bool haveNoCommonBitsSetCommutative(SDValue A, SDValue B)
static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList)
AddNodeIDValueTypes - Value type lists are intern'd so we can represent them solely with their pointe...
static void commuteShuffle(SDValue &N1, SDValue &N2, MutableArrayRef< int > M)
Swaps the values of N1 and N2.
static bool isMemSrcFromConstant(SDValue Src, ConstantDataArraySlice &Slice)
Returns true if memcpy source is constant data.
static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo)
static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC)
AddNodeIDOpcode - Add the node opcode to the NodeID data.
static ISD::CondCode getSetCCInverseImpl(ISD::CondCode Op, bool isIntegerLike)
static bool doNotCSE(SDNode *N)
doNotCSE - Return true if CSE should not be performed for this node.
static cl::opt< int > MaxLdStGlue("ldstmemcpy-glue-max", cl::desc("Number limit for gluing ld/st of memcpy."), cl::Hidden, cl::init(0))
static void AddNodeIDOperands(FoldingSetNodeID &ID, ArrayRef< SDValue > Ops)
AddNodeIDOperands - Various routines for adding operands to the NodeID data.
static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
Try to simplify vector concatenation to an input value, undef, or build vector.
static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info, SelectionDAG &DAG, SDValue Ptr, int64_t Offset=0)
InferPointerInfo - If the specified ptr/offset is a frame index, infer a MachinePointerInfo record fr...
static bool isInTailCallPositionWrapper(const CallInst *CI, const SelectionDAG *SelDAG, bool AllowReturnsFirstArg)
static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N)
If this is an SDNode with special info, add this info to the NodeID data.
static bool gluePropagatesDivergence(const SDNode *Node)
Return true if a glue output should propagate divergence information.
static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G)
static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs)
makeVTList - Return an instance of the SDVTList struct initialized with the specified members.
static void checkForCyclesHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallPtrSetImpl< const SDNode * > &Checked, const llvm::SelectionDAG *DAG)
static void chainLoadsAndStoresForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SmallVector< SDValue, 32 > &OutChains, unsigned From, unsigned To, SmallVector< SDValue, 16 > &OutLoadChains, SmallVector< SDValue, 16 > &OutStoreChains)
static int isSignedOp(ISD::CondCode Opcode)
For an integer comparison, return 1 if the comparison is a signed operation and 2 if the result is an...
static std::optional< APInt > FoldValue(unsigned Opcode, const APInt &C1, const APInt &C2)
static SDValue FoldBUILD_VECTOR(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI, unsigned AS)
static cl::opt< unsigned > MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192), cl::desc("DAG combiner limit number of steps when searching DAG " "for predecessor nodes"))
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
This file describes how to lower LLVM code to machine code.
static void removeOperands(MachineInstr &MI, unsigned i)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition APFloat.h:334
static constexpr roundingMode rmTowardZero
Definition APFloat.h:348
static const fltSemantics & BFloat()
Definition APFloat.h:295
static const fltSemantics & IEEEquad()
Definition APFloat.h:298
static const fltSemantics & IEEEdouble()
Definition APFloat.h:297
static constexpr roundingMode rmTowardNegative
Definition APFloat.h:347
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
static constexpr roundingMode rmTowardPositive
Definition APFloat.h:346
static const fltSemantics & IEEEhalf()
Definition APFloat.h:294
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:360
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition APFloat.h:1183
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1271
void copySign(const APFloat &RHS)
Definition APFloat.h:1365
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:5975
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1253
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
Definition APFloat.h:1495
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1244
bool isFinite() const
Definition APFloat.h:1517
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1410
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1262
bool isSignaling() const
Definition APFloat.h:1514
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition APFloat.h:1298
bool isZero() const
Definition APFloat.h:1508
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1201
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1395
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1161
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1289
bool isPosZero() const
Definition APFloat.h:1523
bool isNegZero() const
Definition APFloat.h:1524
void changeSign()
Definition APFloat.h:1360
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1172
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1982
LLVM_ABI APInt usub_sat(const APInt &RHS) const
Definition APInt.cpp:2066
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1584
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1415
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1023
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1549
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1400
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1679
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1394
LLVM_ABI APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition APInt.cpp:639
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1044
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1521
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:936
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1339
APInt abs() const
Get the absolute value.
Definition APInt.h:1804
LLVM_ABI APInt sadd_sat(const APInt &RHS) const
Definition APInt.cpp:2037
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1183
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:259
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1677
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1497
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1112
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1655
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1405
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition APInt.cpp:1165
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition APInt.h:835
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1167
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1648
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1637
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1607
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition APInt.cpp:651
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sshl_sat(const APInt &RHS) const
Definition APInt.cpp:2097
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
Definition APInt.cpp:2111
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1052
LLVM_ABI APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition APInt.cpp:1152
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition APInt.cpp:397
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition APInt.h:1444
unsigned logBase2() const
Definition APInt.h:1770
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition APInt.cpp:2047
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:828
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1747
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1151
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:996
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1376
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:874
LLVM_ABI APInt byteSwap() const
Definition APInt.cpp:746
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1258
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
static bool isSameValue(const APInt &I1, const APInt &I2)
Determine if two APInts have the same value, after zero-extending one of them (if needed!...
Definition APInt.h:554
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition APInt.h:1426
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1397
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:482
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1238
bool isOne() const
Determine if this is a value of 1.
Definition APInt.h:390
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:852
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1222
LLVM_ABI APInt ssub_sat(const APInt &RHS) const
Definition APInt.cpp:2056
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
unsigned getSrcAddressSpace() const
unsigned getDestAddressSpace() const
static Capacity get(size_t N)
Get the capacity of an array that can hold at least N elements.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
This is an SDNode representing atomic operations.
static LLVM_ABI BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
BitVector & reset()
Definition BitVector.h:411
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition BitVector.h:360
void clear()
clear - Removes all bits from the bitvector.
Definition BitVector.h:354
BitVector & set()
Definition BitVector.h:370
bool none() const
none - Returns true if none of the bits are set.
Definition BitVector.h:207
size_type size() const
size - Returns the number of bits in this bitvector.
Definition BitVector.h:178
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition Constants.h:904
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
LLVM_ABI bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &RawBitElements, BitVector &UndefElements) const
Extract the raw bit data from a build vector of Undef, Constant or ConstantFP node elements.
static LLVM_ABI void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &DstBitElements, ArrayRef< APInt > SrcBitElements, BitVector &DstUndefElements, const BitVector &SrcUndefElements)
Recast bit data SrcBitElements to DstEltSizeInBits wide elements.
LLVM_ABI bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
LLVM_ABI ConstantFPSDNode * getConstantFPSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant FP or null if this is not a constant FP splat.
LLVM_ABI std::optional< std::pair< APInt, APInt > > isConstantSequence() const
If this BuildVector is constant and represents the numerical series "<a, a+n, a+2n,...
LLVM_ABI SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
LLVM_ABI bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, unsigned &SplatBitSize, bool &HasAnyUndefs, unsigned MinSplatBits=0, bool isBigEndian=false) const
Check if this is a constant splat, and if so, find the smallest element size that splats the vector.
LLVM_ABI ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
LLVM_ABI int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements, uint32_t BitWidth) const
If this is a constant FP splat and the splatted constant FP is an exact power or 2,...
LLVM_ABI bool isConstant() const
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
static LLVM_ABI bool isValueValidForType(EVT VT, const APFloat &Val)
const APFloat & getValueAPF() const
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:282
const APFloat & getValue() const
Definition Constants.h:326
This is the shared class of boolean and integer constants.
Definition Constants.h:87
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:162
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
MachineConstantPoolValue * getMachineCPVal() const
const Constant * getConstVal() const
LLVM_ABI Type * getType() const
This class represents a range of values.
LLVM_ABI ConstantRange multiply(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
uint64_t getZExtValue() const
const APInt & getAPIntValue() const
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
DWARF expression.
static LLVM_ABI ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
Base class for variables.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:214
LLVM_ABI IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
LLVM_ABI Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
A debug info location.
Definition DebugLoc.h:123
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:209
Data structure describing the variable locations in a function.
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:703
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:352
LLVM_ABI unsigned getAddressSpace() const
const GlobalValue * getGlobal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
unsigned getAddressSpace() const
Module * getParent()
Get the module that this global value is contained inside of...
PointerType * getType() const
Global values are always pointers.
This class is used to form a handle around another node that is persistent and is updated across invo...
const SDValue & getValue() const
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Tracks which library functions to use for a particular subtarget.
LLVM_ABI 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:1078
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
Abstract base class for all machine specific constantpool value subclasses.
virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID)=0
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
const MDNode * getRanges() const
Return the range tag for the memory reference.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
This class contains meta information specific to a module.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
LLVM_ABI MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt, MachineMemOperand *MMO)
MachineMemOperand * MMO
Memory reference information.
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
unsigned getRawSubclassData() const
Return the SubclassData value, without HasDebugValue.
EVT getMemoryVT() const
Return the type of the in-memory value.
Representation for a specific memory location.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition Module.cpp:235
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:298
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
Class to represent pointers.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
unsigned getAddressSpace() const
Return the address space of the Pointer type.
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Analysis providing profile information.
void Deallocate(SubClass *E)
Deallocate - Release storage for the pointed-to object.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Keeps track of dbg_value information through SDISel.
LLVM_ABI void add(SDDbgValue *V, bool isParameter)
LLVM_ABI void erase(const SDNode *Node)
Invalidate all DbgValues attached to the node and remove it from the Node-to-DbgValues map.
Holds the information from a dbg_label node through SDISel.
Holds the information for a single machine location through SDISel; either an SDNode,...
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(Register VReg)
static SDDbgOperand fromConst(const Value *Const)
@ SDNODE
Value is the result of an expression.
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
const DebugLoc & getDebugLoc() const
unsigned getIROrder() const
This class provides iterator support for SDUse operands that use a specific SDNode.
Represents one node in the SelectionDAG.
ArrayRef< SDUse > ops() const
const APInt & getAsAPIntVal() const
Helper method returns the APInt value of a ConstantSDNode.
LLVM_ABI void dumprFull(const SelectionDAG *G=nullptr) const
printrFull to dbgs().
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
LLVM_ABI bool isOnlyUserOf(const SDNode *N) const
Return true if this node is the only use of N.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
static constexpr size_t getMaxNumOperands()
Return the maximum number of operands that a SDNode can hold.
iterator_range< use_iterator > uses()
MemSDNodeBitfields MemSDNodeBits
LLVM_ABI void Profile(FoldingSetNodeID &ID) const
Gather unique data for the node.
bool getHasDebugValue() const
SDNodeFlags getFlags() const
void setNodeId(int Id)
Set unique node id.
LLVM_ABI void intersectFlagsWith(const SDNodeFlags Flags)
Clear any flags in this node that aren't also set in Flags.
static bool hasPredecessorHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallVectorImpl< const SDNode * > &Worklist, unsigned int MaxSteps=0, bool TopologicalPrune=false)
Returns true if N is a predecessor of any node in Worklist.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
bool use_empty() const
Return true if there are no uses of this node.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
static LLVM_ABI bool areOnlyUsersOf(ArrayRef< const SDNode * > Nodes, const SDNode *N)
Return true if all the users of N are contained in Nodes.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
const APInt & getConstantOperandAPInt(unsigned Num) const
Helper method returns the APInt of a ConstantSDNode operand.
std::optional< APInt > bitcastToAPInt() const
LLVM_ABI bool hasPredecessor(const SDNode *N) const
Return true if N is a predecessor of this node.
LLVM_ABI bool hasAnyUseOfValue(unsigned Value) const
Return true if there are any use of the indicated value.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
bool isUndef() const
Returns true if the node type is UNDEF or POISON.
op_iterator op_end() const
op_iterator op_begin() const
static use_iterator use_end()
LLVM_ABI void DropOperands()
Release the operands and set this node to have zero operands.
SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
Create an SDNode.
Represents a use of a SDNode.
SDNode * getUser()
This returns the SDNode that contains this Use.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if the referenced return value is an operand of N.
SDValue()=default
LLVM_ABI bool reachesChainWithoutSideEffects(SDValue Dest, unsigned Depth=2) const
Return true if this operand (which must be a chain) reaches the specified operand without crossing an...
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
bool use_empty() const
Return true if there are no nodes using value ResNo of Node.
const APInt & getConstantOperandAPInt(unsigned i) const
uint64_t getScalarValueSizeInBits() const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
uint64_t getConstantOperandVal(unsigned i) const
unsigned getOpcode() const
virtual void verifyTargetNode(const SelectionDAG &DAG, const SDNode *N) const
Checks that the given target-specific node is valid. Aborts if it is not.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC)
LLVM_ABI Align getReducedAlign(EVT VT, bool UseABI)
In most cases this function returns the ABI alignment for a given type, except for illegal vector typ...
LLVM_ABI SDValue getVPZeroExtendInReg(SDValue Op, SDValue Mask, SDValue EVL, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
LLVM_ABI SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsExpanding=false)
SDValue getExtractVectorElt(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Extract element at Idx from Vec.
LLVM_ABI SDValue getSplatSourceVector(SDValue V, int &SplatIndex)
If V is a splatted value, return the source vector and its splat index.
LLVM_ABI SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI OverflowKind computeOverflowForUnsignedSub(SDValue N0, SDValue N1) const
Determine if the result of the unsigned sub of 2 nodes can overflow.
LLVM_ABI unsigned ComputeMaxSignificantBits(SDValue Op, unsigned Depth=0) const
Get the upper bound on bit size for this Value Op as a signed integer.
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
LLVM_ABI std::pair< SDValue, SDValue > getStrlen(SDValue Chain, const SDLoc &dl, SDValue Src, const CallInst *CI)
Lower a strlen operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
LLVM_ABI SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
bool isKnownNeverSNaN(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
LLVM_ABI std::optional< bool > isBoolConstant(SDValue N) const
Check if a value \op N is a constant using the target's BooleanContent for its type.
LLVM_ABI SDValue getStackArgumentTokenFactor(SDValue Chain)
Compute a TokenFactor to force all the incoming stack arguments to be loaded from the stack.
const TargetSubtargetInfo & getSubtarget() const
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI void updateDivergence(SDNode *N)
LLVM_ABI SDValue getSplatValue(SDValue V, bool LegalTypes=false)
If V is a splat vector, return its scalar source operand by extracting that element from the source v...
LLVM_ABI SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl)
Constant fold a setcc to true or false.
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) neutral element for the given opcode, if it exists.
LLVM_ABI SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
LLVM_ABI SDValue getAtomicLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT MemVT, EVT VT, SDValue Chain, SDValue Ptr, MachineMemOperand *MMO)
LLVM_ABI SDNode * getNodeIfExists(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops, const SDNodeFlags Flags, bool AllowCommute=false)
Get the specified node if it's already available, or else return NULL.
LLVM_ABI SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid, uint64_t Index, uint32_t Attr)
Creates a PseudoProbeSDNode with function GUID Guid and the index of the block Index it is probing,...
LLVM_ABI SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
LLVM_ABI SDNode * SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT)
These are used for target selectors to mutate the specified node to have the specified return type,...
LLVM_ABI void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, const LibcallLoweringInfo *LibcallsInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, MachineModuleInfo &MMI, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
LLVM_ABI SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
LLVM_ABI SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBitcastedSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
Gets a node for an atomic cmpxchg op.
LLVM_ABI SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain)
If an existing load has uses of its chain, create a token factor node with that chain and the new mem...
LLVM_ABI bool isConstantIntBuildVectorOrConstantInt(SDValue N, bool AllowOpaques=true) const
Test whether the given value is a constant int or similar node.
LLVM_ABI void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
LLVM_ABI SDValue getJumpTableDebugInfo(int JTI, SDValue Chain, const SDLoc &DL)
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false)
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
LLVM_ABI std::optional< unsigned > getValidMaximumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
LLVM_ABI SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
OverflowKind
Used to represent the possible overflow behavior of an operation.
static LLVM_ABI unsigned getHasPredecessorMaxSteps()
LLVM_ABI bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
SDValue getExtractSubvector(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Return the VT typed sub-vector of Vec at Idx.
LLVM_ABI bool cannotBeOrderedNegativeFP(SDValue Op) const
Test whether the given float value is known to be positive.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI bool calculateDivergence(SDNode *N)
LLVM_ABI SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
LLVM_ABI SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI bool canIgnoreSignBitOfZero(const SDUse &Use) const
Check if a use of a float value is insensitive to signed zeros.
LLVM_ABI bool SignBitIsZeroFP(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero, for a floating-point value.
LLVM_ABI SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=LocationSize::precise(0), const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
SDValue getInsertSubvector(const SDLoc &DL, SDValue Vec, SDValue SubVec, unsigned Idx)
Insert SubVec at the Idx element of Vec.
LLVM_ABI SDValue getBitcastedZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
LLVM_ABI SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
LLVM_ABI SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
LLVM_ABI SDValue getVPZExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be an integer vector, to the vector-type VT,...
const TargetLowering & getTargetLoweringInfo() const
LLVM_ABI bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
LLVM_ABI SDValue getStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
bool isGuaranteedNotToBePoison(SDValue Op, unsigned Depth=0) const
Return true if this function can prove that Op is never poison.
LLVM_ABI SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
LLVM_ABI SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI void dump(bool Sorted=false) const
Dump the textual format of this DAG.
LLVM_ABI APInt computeVectorKnownZeroElements(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
For each demanded element of a vector, see if it is known to be zero.
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
bool NewNodesMustHaveLegalTypes
When true, additional steps are taken to ensure that getConstant() and similar functions return DAG n...
LLVM_ABI std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
LLVM_ABI void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
LLVM_ABI SDNode * MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, ArrayRef< SDValue > Ops)
This mutates the specified node to have the specified return type, opcode, and operands.
LLVM_ABI std::pair< SDValue, SDValue > UnrollVectorOverflowOp(SDNode *N, unsigned ResNE=0)
Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
allnodes_const_iterator allnodes_begin() const
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
LLVM_ABI SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcastedAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts, unsigned Depth=0) const
Test whether V has a splatted value for all the demanded elements.
LLVM_ABI void DeleteNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
LLVM_ABI std::pair< SDValue, SDValue > getStrcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, const CallInst *CI)
Lower a strcpy operation into a target library call and return the resulting chain and call result as...
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
LLVM_ABI SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
LLVM_ABI std::optional< unsigned > getValidShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has a uniform shift amount that is less than the element bit-width of the shi...
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
LLVM_ABI SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
LLVM_ABI SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI bool isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
const DataLayout & getDataLayout() const
LLVM_ABI SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
LLVM_ABI SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
LLVM_ABI bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops)
Check if a node exists without modifying its flags.
const SelectionDAGTargetInfo & getSelectionDAGInfo() const
LLVM_ABI bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, unsigned Bytes, int Dist) const
Return true if loads are next to each other and can be merged.
LLVM_ABI SDValue getMaskedHistogram(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
LLVM_ABI SDValue getStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI OverflowKind computeOverflowForUnsignedMul(SDValue N0, SDValue N1) const
Determine if the result of the unsigned mul of 2 nodes can overflow.
LLVM_ABI void copyExtraInfo(SDNode *From, SDNode *To)
Copy extra info associated with one node to another.
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue SV, unsigned Align)
VAArg produces a result and token chain, and takes a pointer and a source value as input.
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getLoadFFVP(EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachineMemOperand *MMO)
LLVM_ABI SDValue getTypeSize(const SDLoc &DL, EVT VT, TypeSize TS)
LLVM_ABI SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
LLVM_ABI void clear()
Clear state and free memory necessary to make this SelectionDAG ready to process a new block.
LLVM_ABI std::pair< SDValue, SDValue > getMemcmp(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, const CallInst *CI)
Lower a memcmp operation into a target library call and return the resulting chain and call result as...
LLVM_ABI void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
LLVM_ABI SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV)
Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to the shuffle node in input but with swa...
LLVM_ABI std::pair< SDValue, SDValue > SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the vector with EXTRACT_SUBVECTOR using the provided VTs and return the low/high part.
LLVM_ABI SDValue makeStateFunctionCall(unsigned LibFunc, SDValue Ptr, SDValue InChain, const SDLoc &DLoc)
Helper used to make a call to a library function that has one argument of pointer type.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, bool PoisonOnly=false, unsigned Depth=0) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
LLVM_ABI SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
LLVM_ABI SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI OverflowKind computeOverflowForSignedMul(SDValue N0, SDValue N1) const
Determine if the result of the signed mul of 2 nodes can overflow.
LLVM_ABI MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
LLVM_ABI bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if '(Op & Mask) == Mask'.
LLVM_ABI bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
LLVM_ABI void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
LLVM_ABI void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
bool isConstantValueOfAnyType(SDValue N) const
LLVM_ABI SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
LLVM_ABI SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, Register VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
LLVM_ABI bool isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
LLVM_ABI SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI bool isKnownNeverZero(SDValue Op, unsigned Depth=0) const
Test whether the given SDValue is known to contain non-zero value(s).
LLVM_ABI SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SDNodeFlags Flags=SDNodeFlags())
LLVM_ABI std::optional< unsigned > getValidMinimumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT)
Convert Op, which must be of integer type, to the integer type VT, by using an extension appropriate ...
LLVM_ABI SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
LLVM_ABI std::pair< SDValue, SDValue > getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT)
Convert Op, which must be a STRICT operation of float type, to the float type VT, by either extending...
LLVM_ABI std::pair< SDValue, SDValue > SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL)
Split the explicit vector length parameter of a VP operation.
LLVM_ABI SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
LLVM_ABI SDValue getVPLogicalNOT(const SDLoc &DL, SDValue Val, SDValue Mask, SDValue EVL, EVT VT)
Create a vector-predicated logical NOT operation as (VP_XOR Val, BooleanOne, Mask,...
LLVM_ABI SDValue getMaskFromElementCount(const SDLoc &DL, EVT VT, ElementCount Len)
Return a vector with the first 'Len' lanes set to true and remaining lanes set to false.
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
iterator_range< allnodes_iterator > allnodes()
LLVM_ABI SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI SDValue WidenVector(const SDValue &N, const SDLoc &DL)
Widen the vector up to the next power of two using INSERT_SUBVECTOR.
LLVM_ABI bool isKnownNeverZeroFloat(SDValue Op) const
Test whether the given floating point SDValue is known to never be positive or negative zero.
const LibcallLoweringInfo & getLibcalls() const
LLVM_ABI SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, const MDNode *Ranges=nullptr, bool IsExpanding=false)
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
LLVM_ABI SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the Fra...
ArrayRef< SDDbgValue * > GetDbgValues(const SDNode *SD) const
Get the debug values which reference the given SDNode.
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI OverflowKind computeOverflowForSignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the signed addition of 2 nodes can overflow.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
LLVM_ABI unsigned AssignTopologicalOrder()
Topological-sort the AllNodes list and a assign a unique node id for each node in the DAG based on th...
ilist< SDNode >::size_type allnodes_size() const
LLVM_ABI bool isKnownNeverNaN(SDValue Op, const APInt &DemandedElts, bool SNaN=false, unsigned Depth=0) const
Test whether the given SDValue (or all elements of it, if it is a vector) is known to never be NaN in...
LLVM_ABI SDValue FoldConstantBuildVector(BuildVectorSDNode *BV, const SDLoc &DL, EVT DstEltVT)
Fold BUILD_VECTOR of constants/undefs to the destination type BUILD_VECTOR of constants/undefs elemen...
LLVM_ABI SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsCompressing=false)
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits.
LLVM_ABI bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Return true if 'Op' is known to be zero in DemandedElts.
LLVM_ABI SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT)
Create a true or false constant of type VT using the target's BooleanContent for type OpVT.
LLVM_ABI SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
LLVM_ABI SDValue getExtStridedLoadVP(ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
LLVM_ABI SDValue getVPPtrExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be of integer type, to the vector-type integer type VT,...
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI void getTopologicallyOrderedNodes(SmallVectorImpl< const SDNode * > &SortedNodes) const
Get all the nodes in their topological order without modifying any states.
LLVM_ABI void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
LLVM_ABI std::pair< SDValue, SDValue > getStrstr(SDValue Chain, const SDLoc &dl, SDValue S0, SDValue S1, const CallInst *CI)
Lower a strstr operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to extend the Op as a pointer value assuming it was the smaller SrcTy ...
LLVM_ABI bool canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts, bool PoisonOnly=false, bool ConsiderFlags=true, unsigned Depth=0) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
LLVM_ABI OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT SVT, MachineMemOperand *MMO, bool IsCompressing=false)
LLVM_ABI void canonicalizeCommutativeBinop(unsigned Opcode, SDValue &N1, SDValue &N2) const
Swap N1 and N2 if Opcode is a commutative binary opcode and the canonical form expects the opposite o...
LLVM_ABI KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
LLVM_ABI SDValue getRegisterMask(const uint32_t *RegMask)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVM_ABI SDValue getCondCode(ISD::CondCode Cond)
LLVM_ABI bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
LLVM_ABI bool isKnownToBeAPowerOfTwoFP(SDValue Val, unsigned Depth=0) const
Test if the given fp value is known to be an integer power-of-2, either positive or negative.
LLVM_ABI OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset)
Create an add instruction with appropriate flags when used for addressing some offset of an object.
LLVMContext * getContext() const
LLVM_ABI SDValue simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y, SDNodeFlags Flags)
Try to simplify a floating-point binary operation into 1 of its operands or a constant.
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
LLVM_ABI SDValue getDeactivationSymbol(const GlobalValue *GV)
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
LLVM_ABI SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
LLVM_ABI bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
LLVM_ABI SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
LLVM_ABI SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
LLVM_ABI std::pair< EVT, EVT > GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT, bool *HiIsEmpty) const
Compute the VTs needed for the low/hi parts of a type, dependent on an enveloping VT that has been sp...
LLVM_ABI SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
LLVM_ABI std::optional< ConstantRange > getValidShiftAmountRange(SDValue V, const APInt &DemandedElts, unsigned Depth) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
LLVM_ABI SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
LLVM_ABI SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
LLVM_ABI SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, ISD::LoadExtType, bool IsExpanding=false)
SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op)
Returns a node representing a splat of one value into all lanes of the provided vector type.
LLVM_ABI std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
LLVM_ABI SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp, ArrayRef< ISD::NodeType > CandidateBinOps, bool AllowPartials=false)
Match a binop + shuffle pyramid that represents a horizontal reduction over the elements of a vector ...
LLVM_ABI bool isADDLike(SDValue Op, bool NoWrap=false) const
Return true if the specified operand is an ISD::OR or ISD::XOR node that can be treated as an ISD::AD...
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
LLVM_ABI SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
LLVM_ABI void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits=0, unsigned SizeInBits=0, bool InvalidateDbg=true)
Transfer debug values from one node to another, while optionally generating fragment expressions for ...
LLVM_ABI SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
LLVM_ABI SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, bool IsTruncating=false)
ilist< SDNode >::iterator allnodes_iterator
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
int getMaskElt(unsigned Idx) const
ArrayRef< int > getMask() const
static void commuteMask(MutableArrayRef< int > Mask)
Change values in a shuffle permute mask assuming the two vector operands have swapped position.
static LLVM_ABI bool isSplatMask(ArrayRef< int > Mask)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Completely target-dependent object reference.
unsigned getTargetFlags() const
Provides information about what library functions are available for the current target.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
const TargetMachine & getTargetMachine() const
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
unsigned getMaxStoresPerMemcpy(bool OptSize) const
Get maximum # of store operations permitted for llvm.memcpy.
CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall implementation.
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
virtual bool shallExtractConstSplatVectorElementToStore(Type *VectorTy, unsigned ElemSizeInBits, unsigned &Index) const
Return true if the target shall perform extract vector element and store given that the vector is kno...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
BooleanContent
Enum that describes how the target represents true/false values.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
RTLIB::LibcallImpl getLibcallImpl(RTLIB::Libcall Call) const
Get the libcall impl routine name for the specified libcall.
std::vector< ArgListEntry > ArgListTy
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool findOptimalMemOpLowering(LLVMContext &Context, std::vector< EVT > &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS, unsigned SrcAS, const AttributeList &FuncAttributes) const
Determines the optimal series of memory ops to replace the memset / memcpy.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
Primary interface to the complete machine description for the target machine.
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition Triple.h:632
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:296
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:280
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:294
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:230
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:35
LLVM_ABI void set(Value *Val)
Definition Value.h:905
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
Value * getOperand(unsigned i) const
Definition User.h:207
This class is used to represent an VP_GATHER node.
This class is used to represent a VP_LOAD node.
This class is used to represent an VP_SCATTER node.
This class is used to represent a VP_STORE node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS.multiplyCoefficientBy(X) will result in a value whos...
Definition TypeSize.h:269
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr bool isKnownEven() const
A return value of true indicates we know at compile time that the number of elements (vscale * Min) i...
Definition TypeSize.h:176
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:252
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:237
A raw_ostream that writes to an std::string.
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt clmulr(const APInt &LHS, const APInt &RHS)
Perform a reversed carry-less multiply.
Definition APInt.cpp:3212
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition APInt.cpp:3142
LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition APInt.cpp:3129
LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition APInt.cpp:3119
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
Definition APInt.cpp:3193
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition APInt.cpp:3134
LLVM_ABI APInt clmul(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, also known as XOR multiplication, and return low-bits.
Definition APInt.cpp:3202
APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition APInt.h:2277
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
Definition APInt.cpp:3184
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3020
LLVM_ABI APInt clmulh(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, and return high-bits.
Definition APInt.cpp:3217
APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition APInt.h:2282
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition APInt.cpp:3114
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition APInt.cpp:3124
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
ISD namespace - This namespace contains an enum which represents all of the SelectionDAG node types a...
Definition ISDOpcodes.h:24
LLVM_ABI CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical AND between different comparisons of identical values: ((X op1 Y) & (X...
LLVM_ABI bool isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are ~0 ...
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:818
@ 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:787
@ 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:778
@ 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:852
@ 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:879
@ 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:746
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:909
@ 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:992
@ 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:773
@ 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:843
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:714
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:664
@ 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:786
@ 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.
@ 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:826
@ 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:690
@ 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:795
@ 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:671
@ 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:791
@ 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:703
@ ATOMIC_LOAD_FMAXIMUM
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:764
@ 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:849
@ 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:810
@ 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, IMM) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by IMM elements and retu...
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:898
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:887
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:726
@ 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:977
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:804
@ 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:925
@ 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:738
@ 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:734
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:709
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, IMM) - Shifts CONCAT_VECTORS(VEC1, VEC2) right by IMM elements and re...
Definition ISDOpcodes.h:656
@ 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:680
@ 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:958
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:698
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:920
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition ISDOpcodes.h:996
@ 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:944
@ VECREDUCE_FMINIMUM
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:855
@ 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:832
@ 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...
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:721
@ 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:667
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
GenericUniformityInfo< SSAContext > UniformityInfo
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:532
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:362
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:237
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1757
LLVM_ABI SDValue peekThroughExtractSubvectors(SDValue V)
Return the non-extracted vector source operand of V if it exists.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1737
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
LLVM_ABI bool isAllOnesOrAllOnesSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant -1 integer or a splatted vector of a constant -1 integer (with...
Definition Utils.cpp:1613
LLVM_ABI SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2544
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:293
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
LLVM_ABI bool isOneOrOneSplatFP(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant floating-point value, or a splatted vector of a constant float...
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:303
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1706
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2198
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
auto cast_or_null(const Y &Val)
Definition Casting.h:714
LLVM_ABI bool isNullOrNullSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
Definition Utils.cpp:1595
LLVM_ABI bool isMinSignedConstant(SDValue V)
Returns true if V is a constant min signed integer value.
LLVM_ABI ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition APFloat.h:1618
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1744
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1661
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
LLVM_ABI SDValue peekThroughInsertVectorElt(SDValue V, const APInt &DemandedElts)
Recursively peek through INSERT_VECTOR_ELT nodes, returning the source vector operand of V,...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1634
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1692
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI SDValue peekThroughTruncates(SDValue V)
Return the non-truncated source operand of V if it exists.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1751
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI SDValue peekThroughOneUseBitcasts(SDValue V)
Return the non-bitcasted and one-use source operand of V if it exists.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:82
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool isOneOrOneSplat(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
Definition ModRef.h:68
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1642
@ Mul
Product of integers.
@ Sub
Subtraction of integers.
LLVM_ABI bool isNullConstantOrUndef(SDValue V)
Returns true if V is a constant integer zero or an UNDEF node.
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition Analysis.cpp:539
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI ConstantSDNode * isConstOrConstSplat(SDValue N, bool AllowUndefs=false, bool AllowTruncation=false)
Returns the SDNode if it is a constant splat BuildVector or constant int.
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1883
constexpr unsigned BitWidth
bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition Analysis.cpp:719
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isZeroOrZeroSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
LLVM_ABI bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1945
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI bool isNullFPConstant(SDValue V)
Returns true if V is an FP constant with a value of positive zero.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant (+/-)0.0 floating-point value or a splatted vector thereof (wi...
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1679
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1719
LLVM_ABI bool isOnesOrOnesSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo)
Returns true if V is a neutral element of Opc with Flags.
LLVM_ABI bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition MathExtras.h:373
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:761
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition Metadata.h:781
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:778
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
void move(uint64_t Delta)
Moves the Offset and adjusts Length accordingly.
const ConstantDataArray * Array
ConstantDataArray pointer.
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:395
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:137
intptr_t getRawBits() const
Definition ValueTypes.h:512
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:74
EVT changeTypeToInteger() const
Return the type converted to an equivalently sized integer or vector with integer element type.
Definition ValueTypes.h:121
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:284
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:300
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:147
ElementCount getVectorElementCount() const
Definition ValueTypes.h:350
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:373
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:359
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:385
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:316
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:65
bool isFixedLengthVector() const
Definition ValueTypes.h:181
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:323
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:292
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:256
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:328
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition ValueTypes.h:142
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:336
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition ValueTypes.h:308
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:453
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:152
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:304
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:258
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:245
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:277
static LLVM_ABI std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
void makeNegative()
Make this value negative.
Definition KnownBits.h:122
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:164
KnownBits byteSwap() const
Definition KnownBits.h:522
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:292
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:526
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:236
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:175
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:324
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:228
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:314
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:183
static LLVM_ABI KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:199
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:148
static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:60
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition KnownBits.h:114
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:329
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
static LLVM_ABI KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)
Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
Definition KnownBits.cpp:53
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:283
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:222
static LLVM_ABI KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:170
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
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).
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
This class contains a discriminated union of information about pointers in memory operands,...
LLVM_ABI bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
LLVM_ABI unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
MachinePointerInfo getWithOffset(int64_t O) const
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
static MemOp Set(uint64_t Size, bool DstAlignCanChange, Align DstAlign, bool IsZeroMemset, bool IsVolatile)
static MemOp Copy(uint64_t Size, bool DstAlignCanChange, Align DstAlign, Align SrcAlign, bool IsVolatile, bool MemcpyStrSrc=false)
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.
These are IR-level optimization flags that may be propagated to SDNodes.
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
unsigned int NumVTs
Clients of various APIs that cause global effects on the DAG can optionally implement this interface.
virtual void NodeDeleted(SDNode *N, SDNode *E)
The node N that was deleted and, if E is not null, an equivalent node E that replaced it.
virtual void NodeInserted(SDNode *N)
The node N that was inserted.
virtual void NodeUpdated(SDNode *N)
The node N that was updated.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setLibCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList)
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setChain(SDValue InChain)