LLVM 19.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"
50#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"
68#include "llvm/Support/Mutex.h"
74#include <algorithm>
75#include <cassert>
76#include <cstdint>
77#include <cstdlib>
78#include <limits>
79#include <set>
80#include <string>
81#include <utility>
82#include <vector>
83
84using namespace llvm;
85using namespace llvm::SDPatternMatch;
86
87/// makeVTList - Return an instance of the SDVTList struct initialized with the
88/// specified members.
89static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
90 SDVTList Res = {VTs, NumVTs};
91 return Res;
92}
93
94// Default null implementations of the callbacks.
98
99void SelectionDAG::DAGNodeDeletedListener::anchor() {}
100void SelectionDAG::DAGNodeInsertedListener::anchor() {}
101
102#define DEBUG_TYPE "selectiondag"
103
104static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
105 cl::Hidden, cl::init(true),
106 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
107
108static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
109 cl::desc("Number limit for gluing ld/st of memcpy."),
110 cl::Hidden, cl::init(0));
111
113 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
114}
115
116//===----------------------------------------------------------------------===//
117// ConstantFPSDNode Class
118//===----------------------------------------------------------------------===//
119
120/// isExactlyValue - We don't rely on operator== working on double values, as
121/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
122/// As such, this method can be used to do an exact bit-for-bit comparison of
123/// two floating point values.
125 return getValueAPF().bitwiseIsEqual(V);
126}
127
129 const APFloat& Val) {
130 assert(VT.isFloatingPoint() && "Can only convert between FP types");
131
132 // convert modifies in place, so make a copy.
133 APFloat Val2 = APFloat(Val);
134 bool losesInfo;
137 &losesInfo);
138 return !losesInfo;
139}
140
141//===----------------------------------------------------------------------===//
142// ISD Namespace
143//===----------------------------------------------------------------------===//
144
145bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
146 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
147 unsigned EltSize =
148 N->getValueType(0).getVectorElementType().getSizeInBits();
149 if (auto *Op0 = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
150 SplatVal = Op0->getAPIntValue().trunc(EltSize);
151 return true;
152 }
153 if (auto *Op0 = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
154 SplatVal = Op0->getValueAPF().bitcastToAPInt().trunc(EltSize);
155 return true;
156 }
157 }
158
159 auto *BV = dyn_cast<BuildVectorSDNode>(N);
160 if (!BV)
161 return false;
162
163 APInt SplatUndef;
164 unsigned SplatBitSize;
165 bool HasUndefs;
166 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
167 // Endianness does not matter here. We are checking for a splat given the
168 // element size of the vector, and if we find such a splat for little endian
169 // layout, then that should be valid also for big endian (as the full vector
170 // size is known to be a multiple of the element size).
171 const bool IsBigEndian = false;
172 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
173 EltSize, IsBigEndian) &&
174 EltSize == SplatBitSize;
175}
176
177// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
178// specializations of the more general isConstantSplatVector()?
179
180bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
181 // Look through a bit convert.
182 while (N->getOpcode() == ISD::BITCAST)
183 N = N->getOperand(0).getNode();
184
185 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
186 APInt SplatVal;
187 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
188 }
189
190 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
191
192 unsigned i = 0, e = N->getNumOperands();
193
194 // Skip over all of the undef values.
195 while (i != e && N->getOperand(i).isUndef())
196 ++i;
197
198 // Do not accept an all-undef vector.
199 if (i == e) return false;
200
201 // Do not accept build_vectors that aren't all constants or which have non-~0
202 // elements. We have to be a bit careful here, as the type of the constant
203 // may not be the same as the type of the vector elements due to type
204 // legalization (the elements are promoted to a legal type for the target and
205 // a vector of a type may be legal when the base element type is not).
206 // We only want to check enough bits to cover the vector elements, because
207 // we care if the resultant vector is all ones, not whether the individual
208 // constants are.
209 SDValue NotZero = N->getOperand(i);
210 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
211 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
212 if (CN->getAPIntValue().countr_one() < EltSize)
213 return false;
214 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
215 if (CFPN->getValueAPF().bitcastToAPInt().countr_one() < EltSize)
216 return false;
217 } else
218 return false;
219
220 // Okay, we have at least one ~0 value, check to see if the rest match or are
221 // undefs. Even with the above element type twiddling, this should be OK, as
222 // the same type legalization should have applied to all the elements.
223 for (++i; i != e; ++i)
224 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
225 return false;
226 return true;
227}
228
229bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
230 // Look through a bit convert.
231 while (N->getOpcode() == ISD::BITCAST)
232 N = N->getOperand(0).getNode();
233
234 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
235 APInt SplatVal;
236 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
237 }
238
239 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
240
241 bool IsAllUndef = true;
242 for (const SDValue &Op : N->op_values()) {
243 if (Op.isUndef())
244 continue;
245 IsAllUndef = false;
246 // Do not accept build_vectors that aren't all constants or which have non-0
247 // elements. We have to be a bit careful here, as the type of the constant
248 // may not be the same as the type of the vector elements due to type
249 // legalization (the elements are promoted to a legal type for the target
250 // and a vector of a type may be legal when the base element type is not).
251 // We only want to check enough bits to cover the vector elements, because
252 // we care if the resultant vector is all zeros, not whether the individual
253 // constants are.
254 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
255 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
256 if (CN->getAPIntValue().countr_zero() < EltSize)
257 return false;
258 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
259 if (CFPN->getValueAPF().bitcastToAPInt().countr_zero() < EltSize)
260 return false;
261 } else
262 return false;
263 }
264
265 // Do not accept an all-undef vector.
266 if (IsAllUndef)
267 return false;
268 return true;
269}
270
272 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
273}
274
276 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
277}
278
280 if (N->getOpcode() != ISD::BUILD_VECTOR)
281 return false;
282
283 for (const SDValue &Op : N->op_values()) {
284 if (Op.isUndef())
285 continue;
286 if (!isa<ConstantSDNode>(Op))
287 return false;
288 }
289 return true;
290}
291
293 if (N->getOpcode() != ISD::BUILD_VECTOR)
294 return false;
295
296 for (const SDValue &Op : N->op_values()) {
297 if (Op.isUndef())
298 continue;
299 if (!isa<ConstantFPSDNode>(Op))
300 return false;
301 }
302 return true;
303}
304
305bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
306 bool Signed) {
307 assert(N->getValueType(0).isVector() && "Expected a vector!");
308
309 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
310 if (EltSize <= NewEltSize)
311 return false;
312
313 if (N->getOpcode() == ISD::ZERO_EXTEND) {
314 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
315 NewEltSize) &&
316 !Signed;
317 }
318 if (N->getOpcode() == ISD::SIGN_EXTEND) {
319 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
320 NewEltSize) &&
321 Signed;
322 }
323 if (N->getOpcode() != ISD::BUILD_VECTOR)
324 return false;
325
326 for (const SDValue &Op : N->op_values()) {
327 if (Op.isUndef())
328 continue;
329 if (!isa<ConstantSDNode>(Op))
330 return false;
331
332 APInt C = Op->getAsAPIntVal().trunc(EltSize);
333 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
334 return false;
335 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
336 return false;
337 }
338
339 return true;
340}
341
343 // Return false if the node has no operands.
344 // This is "logically inconsistent" with the definition of "all" but
345 // is probably the desired behavior.
346 if (N->getNumOperands() == 0)
347 return false;
348 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
349}
350
352 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
353}
354
355template <typename ConstNodeType>
357 std::function<bool(ConstNodeType *)> Match,
358 bool AllowUndefs) {
359 // FIXME: Add support for scalar UNDEF cases?
360 if (auto *C = dyn_cast<ConstNodeType>(Op))
361 return Match(C);
362
363 // FIXME: Add support for vector UNDEF cases?
364 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
365 ISD::SPLAT_VECTOR != Op.getOpcode())
366 return false;
367
368 EVT SVT = Op.getValueType().getScalarType();
369 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
370 if (AllowUndefs && Op.getOperand(i).isUndef()) {
371 if (!Match(nullptr))
372 return false;
373 continue;
374 }
375
376 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
377 if (!Cst || Cst->getValueType(0) != SVT || !Match(Cst))
378 return false;
379 }
380 return true;
381}
382// Build used template types.
383template bool ISD::matchUnaryPredicateImpl<ConstantSDNode>(
384 SDValue, std::function<bool(ConstantSDNode *)>, bool);
385template bool ISD::matchUnaryPredicateImpl<ConstantFPSDNode>(
386 SDValue, std::function<bool(ConstantFPSDNode *)>, bool);
387
389 SDValue LHS, SDValue RHS,
390 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
391 bool AllowUndefs, bool AllowTypeMismatch) {
392 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
393 return false;
394
395 // TODO: Add support for scalar UNDEF cases?
396 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
397 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
398 return Match(LHSCst, RHSCst);
399
400 // TODO: Add support for vector UNDEF cases?
401 if (LHS.getOpcode() != RHS.getOpcode() ||
402 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
403 LHS.getOpcode() != ISD::SPLAT_VECTOR))
404 return false;
405
406 EVT SVT = LHS.getValueType().getScalarType();
407 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
408 SDValue LHSOp = LHS.getOperand(i);
409 SDValue RHSOp = RHS.getOperand(i);
410 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
411 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
412 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
413 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
414 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
415 return false;
416 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
417 LHSOp.getValueType() != RHSOp.getValueType()))
418 return false;
419 if (!Match(LHSCst, RHSCst))
420 return false;
421 }
422 return true;
423}
424
426 switch (VecReduceOpcode) {
427 default:
428 llvm_unreachable("Expected VECREDUCE opcode");
431 case ISD::VP_REDUCE_FADD:
432 case ISD::VP_REDUCE_SEQ_FADD:
433 return ISD::FADD;
436 case ISD::VP_REDUCE_FMUL:
437 case ISD::VP_REDUCE_SEQ_FMUL:
438 return ISD::FMUL;
440 case ISD::VP_REDUCE_ADD:
441 return ISD::ADD;
443 case ISD::VP_REDUCE_MUL:
444 return ISD::MUL;
446 case ISD::VP_REDUCE_AND:
447 return ISD::AND;
449 case ISD::VP_REDUCE_OR:
450 return ISD::OR;
452 case ISD::VP_REDUCE_XOR:
453 return ISD::XOR;
455 case ISD::VP_REDUCE_SMAX:
456 return ISD::SMAX;
458 case ISD::VP_REDUCE_SMIN:
459 return ISD::SMIN;
461 case ISD::VP_REDUCE_UMAX:
462 return ISD::UMAX;
464 case ISD::VP_REDUCE_UMIN:
465 return ISD::UMIN;
467 case ISD::VP_REDUCE_FMAX:
468 return ISD::FMAXNUM;
470 case ISD::VP_REDUCE_FMIN:
471 return ISD::FMINNUM;
473 case ISD::VP_REDUCE_FMAXIMUM:
474 return ISD::FMAXIMUM;
476 case ISD::VP_REDUCE_FMINIMUM:
477 return ISD::FMINIMUM;
478 }
479}
480
481bool ISD::isVPOpcode(unsigned Opcode) {
482 switch (Opcode) {
483 default:
484 return false;
485#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
486 case ISD::VPSD: \
487 return true;
488#include "llvm/IR/VPIntrinsics.def"
489 }
490}
491
492bool ISD::isVPBinaryOp(unsigned Opcode) {
493 switch (Opcode) {
494 default:
495 break;
496#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
497#define VP_PROPERTY_BINARYOP return true;
498#define END_REGISTER_VP_SDNODE(VPSD) break;
499#include "llvm/IR/VPIntrinsics.def"
500 }
501 return false;
502}
503
504bool ISD::isVPReduction(unsigned Opcode) {
505 switch (Opcode) {
506 default:
507 break;
508#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
509#define VP_PROPERTY_REDUCTION(STARTPOS, ...) return true;
510#define END_REGISTER_VP_SDNODE(VPSD) break;
511#include "llvm/IR/VPIntrinsics.def"
512 }
513 return false;
514}
515
516/// The operand position of the vector mask.
517std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
518 switch (Opcode) {
519 default:
520 return std::nullopt;
521#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
522 case ISD::VPSD: \
523 return MASKPOS;
524#include "llvm/IR/VPIntrinsics.def"
525 }
526}
527
528/// The operand position of the explicit vector length parameter.
529std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
530 switch (Opcode) {
531 default:
532 return std::nullopt;
533#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
534 case ISD::VPSD: \
535 return EVLPOS;
536#include "llvm/IR/VPIntrinsics.def"
537 }
538}
539
540std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
541 bool hasFPExcept) {
542 // FIXME: Return strict opcodes in case of fp exceptions.
543 switch (VPOpcode) {
544 default:
545 return std::nullopt;
546#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
547#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
548#define END_REGISTER_VP_SDNODE(VPOPC) break;
549#include "llvm/IR/VPIntrinsics.def"
550 }
551 return std::nullopt;
552}
553
554unsigned ISD::getVPForBaseOpcode(unsigned Opcode) {
555 switch (Opcode) {
556 default:
557 llvm_unreachable("can not translate this Opcode to VP.");
558#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
559#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
560#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
561#include "llvm/IR/VPIntrinsics.def"
562 }
563}
564
566 switch (ExtType) {
567 case ISD::EXTLOAD:
568 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
569 case ISD::SEXTLOAD:
570 return ISD::SIGN_EXTEND;
571 case ISD::ZEXTLOAD:
572 return ISD::ZERO_EXTEND;
573 default:
574 break;
575 }
576
577 llvm_unreachable("Invalid LoadExtType");
578}
579
581 // To perform this operation, we just need to swap the L and G bits of the
582 // operation.
583 unsigned OldL = (Operation >> 2) & 1;
584 unsigned OldG = (Operation >> 1) & 1;
585 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
586 (OldL << 1) | // New G bit
587 (OldG << 2)); // New L bit.
588}
589
591 unsigned Operation = Op;
592 if (isIntegerLike)
593 Operation ^= 7; // Flip L, G, E bits, but not U.
594 else
595 Operation ^= 15; // Flip all of the condition bits.
596
598 Operation &= ~8; // Don't let N and U bits get set.
599
600 return ISD::CondCode(Operation);
601}
602
604 return getSetCCInverseImpl(Op, Type.isInteger());
605}
606
608 bool isIntegerLike) {
609 return getSetCCInverseImpl(Op, isIntegerLike);
610}
611
612/// For an integer comparison, return 1 if the comparison is a signed operation
613/// and 2 if the result is an unsigned comparison. Return zero if the operation
614/// does not depend on the sign of the input (setne and seteq).
615static int isSignedOp(ISD::CondCode Opcode) {
616 switch (Opcode) {
617 default: llvm_unreachable("Illegal integer setcc operation!");
618 case ISD::SETEQ:
619 case ISD::SETNE: return 0;
620 case ISD::SETLT:
621 case ISD::SETLE:
622 case ISD::SETGT:
623 case ISD::SETGE: return 1;
624 case ISD::SETULT:
625 case ISD::SETULE:
626 case ISD::SETUGT:
627 case ISD::SETUGE: return 2;
628 }
629}
630
632 EVT Type) {
633 bool IsInteger = Type.isInteger();
634 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
635 // Cannot fold a signed integer setcc with an unsigned integer setcc.
636 return ISD::SETCC_INVALID;
637
638 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
639
640 // If the N and U bits get set, then the resultant comparison DOES suddenly
641 // care about orderedness, and it is true when ordered.
642 if (Op > ISD::SETTRUE2)
643 Op &= ~16; // Clear the U bit if the N bit is set.
644
645 // Canonicalize illegal integer setcc's.
646 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
647 Op = ISD::SETNE;
648
649 return ISD::CondCode(Op);
650}
651
653 EVT Type) {
654 bool IsInteger = Type.isInteger();
655 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
656 // Cannot fold a signed setcc with an unsigned setcc.
657 return ISD::SETCC_INVALID;
658
659 // Combine all of the condition bits.
660 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
661
662 // Canonicalize illegal integer setcc's.
663 if (IsInteger) {
664 switch (Result) {
665 default: break;
666 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
667 case ISD::SETOEQ: // SETEQ & SETU[LG]E
668 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
669 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
670 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
671 }
672 }
673
674 return Result;
675}
676
677//===----------------------------------------------------------------------===//
678// SDNode Profile Support
679//===----------------------------------------------------------------------===//
680
681/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
682static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
683 ID.AddInteger(OpC);
684}
685
686/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
687/// solely with their pointer.
689 ID.AddPointer(VTList.VTs);
690}
691
692/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
694 ArrayRef<SDValue> Ops) {
695 for (const auto &Op : Ops) {
696 ID.AddPointer(Op.getNode());
697 ID.AddInteger(Op.getResNo());
698 }
699}
700
701/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
703 ArrayRef<SDUse> Ops) {
704 for (const auto &Op : Ops) {
705 ID.AddPointer(Op.getNode());
706 ID.AddInteger(Op.getResNo());
707 }
708}
709
710static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
711 SDVTList VTList, ArrayRef<SDValue> OpList) {
712 AddNodeIDOpcode(ID, OpC);
713 AddNodeIDValueTypes(ID, VTList);
714 AddNodeIDOperands(ID, OpList);
715}
716
717/// If this is an SDNode with special info, add this info to the NodeID data.
719 switch (N->getOpcode()) {
722 case ISD::MCSymbol:
723 llvm_unreachable("Should only be used on nodes with operands");
724 default: break; // Normal nodes don't need extra info.
726 case ISD::Constant: {
727 const ConstantSDNode *C = cast<ConstantSDNode>(N);
728 ID.AddPointer(C->getConstantIntValue());
729 ID.AddBoolean(C->isOpaque());
730 break;
731 }
733 case ISD::ConstantFP:
734 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
735 break;
740 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
741 ID.AddPointer(GA->getGlobal());
742 ID.AddInteger(GA->getOffset());
743 ID.AddInteger(GA->getTargetFlags());
744 break;
745 }
746 case ISD::BasicBlock:
747 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
748 break;
749 case ISD::Register:
750 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
751 break;
753 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
754 break;
755 case ISD::SRCVALUE:
756 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
757 break;
758 case ISD::FrameIndex:
760 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
761 break;
764 if (cast<LifetimeSDNode>(N)->hasOffset()) {
765 ID.AddInteger(cast<LifetimeSDNode>(N)->getSize());
766 ID.AddInteger(cast<LifetimeSDNode>(N)->getOffset());
767 }
768 break;
770 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
771 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
772 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
773 break;
774 case ISD::JumpTable:
776 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
777 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
778 break;
781 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
782 ID.AddInteger(CP->getAlign().value());
783 ID.AddInteger(CP->getOffset());
784 if (CP->isMachineConstantPoolEntry())
785 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
786 else
787 ID.AddPointer(CP->getConstVal());
788 ID.AddInteger(CP->getTargetFlags());
789 break;
790 }
791 case ISD::TargetIndex: {
792 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
793 ID.AddInteger(TI->getIndex());
794 ID.AddInteger(TI->getOffset());
795 ID.AddInteger(TI->getTargetFlags());
796 break;
797 }
798 case ISD::LOAD: {
799 const LoadSDNode *LD = cast<LoadSDNode>(N);
800 ID.AddInteger(LD->getMemoryVT().getRawBits());
801 ID.AddInteger(LD->getRawSubclassData());
802 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
803 ID.AddInteger(LD->getMemOperand()->getFlags());
804 break;
805 }
806 case ISD::STORE: {
807 const StoreSDNode *ST = cast<StoreSDNode>(N);
808 ID.AddInteger(ST->getMemoryVT().getRawBits());
809 ID.AddInteger(ST->getRawSubclassData());
810 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
811 ID.AddInteger(ST->getMemOperand()->getFlags());
812 break;
813 }
814 case ISD::VP_LOAD: {
815 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
816 ID.AddInteger(ELD->getMemoryVT().getRawBits());
817 ID.AddInteger(ELD->getRawSubclassData());
818 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
819 ID.AddInteger(ELD->getMemOperand()->getFlags());
820 break;
821 }
822 case ISD::VP_STORE: {
823 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
824 ID.AddInteger(EST->getMemoryVT().getRawBits());
825 ID.AddInteger(EST->getRawSubclassData());
826 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
827 ID.AddInteger(EST->getMemOperand()->getFlags());
828 break;
829 }
830 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
831 const VPStridedLoadSDNode *SLD = cast<VPStridedLoadSDNode>(N);
832 ID.AddInteger(SLD->getMemoryVT().getRawBits());
833 ID.AddInteger(SLD->getRawSubclassData());
834 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
835 break;
836 }
837 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
838 const VPStridedStoreSDNode *SST = cast<VPStridedStoreSDNode>(N);
839 ID.AddInteger(SST->getMemoryVT().getRawBits());
840 ID.AddInteger(SST->getRawSubclassData());
841 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
842 break;
843 }
844 case ISD::VP_GATHER: {
845 const VPGatherSDNode *EG = cast<VPGatherSDNode>(N);
846 ID.AddInteger(EG->getMemoryVT().getRawBits());
847 ID.AddInteger(EG->getRawSubclassData());
848 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
849 ID.AddInteger(EG->getMemOperand()->getFlags());
850 break;
851 }
852 case ISD::VP_SCATTER: {
853 const VPScatterSDNode *ES = cast<VPScatterSDNode>(N);
854 ID.AddInteger(ES->getMemoryVT().getRawBits());
855 ID.AddInteger(ES->getRawSubclassData());
856 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
857 ID.AddInteger(ES->getMemOperand()->getFlags());
858 break;
859 }
860 case ISD::MLOAD: {
861 const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
862 ID.AddInteger(MLD->getMemoryVT().getRawBits());
863 ID.AddInteger(MLD->getRawSubclassData());
864 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
865 ID.AddInteger(MLD->getMemOperand()->getFlags());
866 break;
867 }
868 case ISD::MSTORE: {
869 const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
870 ID.AddInteger(MST->getMemoryVT().getRawBits());
871 ID.AddInteger(MST->getRawSubclassData());
872 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
873 ID.AddInteger(MST->getMemOperand()->getFlags());
874 break;
875 }
876 case ISD::MGATHER: {
877 const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
878 ID.AddInteger(MG->getMemoryVT().getRawBits());
879 ID.AddInteger(MG->getRawSubclassData());
880 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
881 ID.AddInteger(MG->getMemOperand()->getFlags());
882 break;
883 }
884 case ISD::MSCATTER: {
885 const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
886 ID.AddInteger(MS->getMemoryVT().getRawBits());
887 ID.AddInteger(MS->getRawSubclassData());
888 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
889 ID.AddInteger(MS->getMemOperand()->getFlags());
890 break;
891 }
894 case ISD::ATOMIC_SWAP:
906 case ISD::ATOMIC_LOAD:
907 case ISD::ATOMIC_STORE: {
908 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
909 ID.AddInteger(AT->getMemoryVT().getRawBits());
910 ID.AddInteger(AT->getRawSubclassData());
911 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
912 ID.AddInteger(AT->getMemOperand()->getFlags());
913 break;
914 }
915 case ISD::VECTOR_SHUFFLE: {
916 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
917 for (int M : Mask)
918 ID.AddInteger(M);
919 break;
920 }
922 case ISD::BlockAddress: {
923 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
924 ID.AddPointer(BA->getBlockAddress());
925 ID.AddInteger(BA->getOffset());
926 ID.AddInteger(BA->getTargetFlags());
927 break;
928 }
929 case ISD::AssertAlign:
930 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
931 break;
932 case ISD::PREFETCH:
935 // Handled by MemIntrinsicSDNode check after the switch.
936 break;
937 } // end switch (N->getOpcode())
938
939 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
940 // to check.
941 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
942 ID.AddInteger(MN->getRawSubclassData());
943 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
944 ID.AddInteger(MN->getMemOperand()->getFlags());
945 ID.AddInteger(MN->getMemoryVT().getRawBits());
946 }
947}
948
949/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
950/// data.
951static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
952 AddNodeIDOpcode(ID, N->getOpcode());
953 // Add the return value info.
954 AddNodeIDValueTypes(ID, N->getVTList());
955 // Add the operand info.
956 AddNodeIDOperands(ID, N->ops());
957
958 // Handle SDNode leafs with special info.
960}
961
962//===----------------------------------------------------------------------===//
963// SelectionDAG Class
964//===----------------------------------------------------------------------===//
965
966/// doNotCSE - Return true if CSE should not be performed for this node.
967static bool doNotCSE(SDNode *N) {
968 if (N->getValueType(0) == MVT::Glue)
969 return true; // Never CSE anything that produces a glue result.
970
971 switch (N->getOpcode()) {
972 default: break;
973 case ISD::HANDLENODE:
974 case ISD::EH_LABEL:
975 return true; // Never CSE these nodes.
976 }
977
978 // Check that remaining values produced are not flags.
979 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
980 if (N->getValueType(i) == MVT::Glue)
981 return true; // Never CSE anything that produces a glue result.
982
983 return false;
984}
985
986/// RemoveDeadNodes - This method deletes all unreachable nodes in the
987/// SelectionDAG.
989 // Create a dummy node (which is not added to allnodes), that adds a reference
990 // to the root node, preventing it from being deleted.
991 HandleSDNode Dummy(getRoot());
992
994
995 // Add all obviously-dead nodes to the DeadNodes worklist.
996 for (SDNode &Node : allnodes())
997 if (Node.use_empty())
998 DeadNodes.push_back(&Node);
999
1000 RemoveDeadNodes(DeadNodes);
1001
1002 // If the root changed (e.g. it was a dead load, update the root).
1003 setRoot(Dummy.getValue());
1004}
1005
1006/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1007/// given list, and any nodes that become unreachable as a result.
1009
1010 // Process the worklist, deleting the nodes and adding their uses to the
1011 // worklist.
1012 while (!DeadNodes.empty()) {
1013 SDNode *N = DeadNodes.pop_back_val();
1014 // Skip to next node if we've already managed to delete the node. This could
1015 // happen if replacing a node causes a node previously added to the node to
1016 // be deleted.
1017 if (N->getOpcode() == ISD::DELETED_NODE)
1018 continue;
1019
1020 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1021 DUL->NodeDeleted(N, nullptr);
1022
1023 // Take the node out of the appropriate CSE map.
1024 RemoveNodeFromCSEMaps(N);
1025
1026 // Next, brutally remove the operand list. This is safe to do, as there are
1027 // no cycles in the graph.
1028 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1029 SDUse &Use = *I++;
1030 SDNode *Operand = Use.getNode();
1031 Use.set(SDValue());
1032
1033 // Now that we removed this operand, see if there are no uses of it left.
1034 if (Operand->use_empty())
1035 DeadNodes.push_back(Operand);
1036 }
1037
1038 DeallocateNode(N);
1039 }
1040}
1041
1043 SmallVector<SDNode*, 16> DeadNodes(1, N);
1044
1045 // Create a dummy node that adds a reference to the root node, preventing
1046 // it from being deleted. (This matters if the root is an operand of the
1047 // dead node.)
1048 HandleSDNode Dummy(getRoot());
1049
1050 RemoveDeadNodes(DeadNodes);
1051}
1052
1054 // First take this out of the appropriate CSE map.
1055 RemoveNodeFromCSEMaps(N);
1056
1057 // Finally, remove uses due to operands of this node, remove from the
1058 // AllNodes list, and delete the node.
1059 DeleteNodeNotInCSEMaps(N);
1060}
1061
1062void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1063 assert(N->getIterator() != AllNodes.begin() &&
1064 "Cannot delete the entry node!");
1065 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1066
1067 // Drop all of the operands and decrement used node's use counts.
1068 N->DropOperands();
1069
1070 DeallocateNode(N);
1071}
1072
1073void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1074 assert(!(V->isVariadic() && isParameter));
1075 if (isParameter)
1076 ByvalParmDbgValues.push_back(V);
1077 else
1078 DbgValues.push_back(V);
1079 for (const SDNode *Node : V->getSDNodes())
1080 if (Node)
1081 DbgValMap[Node].push_back(V);
1082}
1083
1084void SDDbgInfo::erase(const SDNode *Node) {
1085 DbgValMapType::iterator I = DbgValMap.find(Node);
1086 if (I == DbgValMap.end())
1087 return;
1088 for (auto &Val: I->second)
1089 Val->setIsInvalidated();
1090 DbgValMap.erase(I);
1091}
1092
1093void SelectionDAG::DeallocateNode(SDNode *N) {
1094 // If we have operands, deallocate them.
1095 removeOperands(N);
1096
1097 NodeAllocator.Deallocate(AllNodes.remove(N));
1098
1099 // Set the opcode to DELETED_NODE to help catch bugs when node
1100 // memory is reallocated.
1101 // FIXME: There are places in SDag that have grown a dependency on the opcode
1102 // value in the released node.
1103 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1104 N->NodeType = ISD::DELETED_NODE;
1105
1106 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1107 // them and forget about that node.
1108 DbgInfo->erase(N);
1109
1110 // Invalidate extra info.
1111 SDEI.erase(N);
1112}
1113
1114#ifndef NDEBUG
1115/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1116static void VerifySDNode(SDNode *N, const TargetLowering *TLI) {
1117 switch (N->getOpcode()) {
1118 default:
1119 if (N->getOpcode() > ISD::BUILTIN_OP_END)
1120 TLI->verifyTargetSDNode(N);
1121 break;
1122 case ISD::BUILD_PAIR: {
1123 EVT VT = N->getValueType(0);
1124 assert(N->getNumValues() == 1 && "Too many results!");
1125 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1126 "Wrong return type!");
1127 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1128 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1129 "Mismatched operand types!");
1130 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1131 "Wrong operand type!");
1132 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1133 "Wrong return type size");
1134 break;
1135 }
1136 case ISD::BUILD_VECTOR: {
1137 assert(N->getNumValues() == 1 && "Too many results!");
1138 assert(N->getValueType(0).isVector() && "Wrong return type!");
1139 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1140 "Wrong number of operands!");
1141 EVT EltVT = N->getValueType(0).getVectorElementType();
1142 for (const SDUse &Op : N->ops()) {
1143 assert((Op.getValueType() == EltVT ||
1144 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1145 EltVT.bitsLE(Op.getValueType()))) &&
1146 "Wrong operand type!");
1147 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1148 "Operands must all have the same type");
1149 }
1150 break;
1151 }
1152 }
1153}
1154#endif // NDEBUG
1155
1156/// Insert a newly allocated node into the DAG.
1157///
1158/// Handles insertion into the all nodes list and CSE map, as well as
1159/// verification and other common operations when a new node is allocated.
1160void SelectionDAG::InsertNode(SDNode *N) {
1161 AllNodes.push_back(N);
1162#ifndef NDEBUG
1163 N->PersistentId = NextPersistentId++;
1164 VerifySDNode(N, TLI);
1165#endif
1166 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1167 DUL->NodeInserted(N);
1168}
1169
1170/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1171/// correspond to it. This is useful when we're about to delete or repurpose
1172/// the node. We don't want future request for structurally identical nodes
1173/// to return N anymore.
1174bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1175 bool Erased = false;
1176 switch (N->getOpcode()) {
1177 case ISD::HANDLENODE: return false; // noop.
1178 case ISD::CONDCODE:
1179 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1180 "Cond code doesn't exist!");
1181 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1182 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1183 break;
1185 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1186 break;
1188 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1189 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1190 ESN->getSymbol(), ESN->getTargetFlags()));
1191 break;
1192 }
1193 case ISD::MCSymbol: {
1194 auto *MCSN = cast<MCSymbolSDNode>(N);
1195 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1196 break;
1197 }
1198 case ISD::VALUETYPE: {
1199 EVT VT = cast<VTSDNode>(N)->getVT();
1200 if (VT.isExtended()) {
1201 Erased = ExtendedValueTypeNodes.erase(VT);
1202 } else {
1203 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1204 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1205 }
1206 break;
1207 }
1208 default:
1209 // Remove it from the CSE Map.
1210 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1211 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1212 Erased = CSEMap.RemoveNode(N);
1213 break;
1214 }
1215#ifndef NDEBUG
1216 // Verify that the node was actually in one of the CSE maps, unless it has a
1217 // glue result (which cannot be CSE'd) or is one of the special cases that are
1218 // not subject to CSE.
1219 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1220 !N->isMachineOpcode() && !doNotCSE(N)) {
1221 N->dump(this);
1222 dbgs() << "\n";
1223 llvm_unreachable("Node is not in map!");
1224 }
1225#endif
1226 return Erased;
1227}
1228
1229/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1230/// maps and modified in place. Add it back to the CSE maps, unless an identical
1231/// node already exists, in which case transfer all its users to the existing
1232/// node. This transfer can potentially trigger recursive merging.
1233void
1234SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1235 // For node types that aren't CSE'd, just act as if no identical node
1236 // already exists.
1237 if (!doNotCSE(N)) {
1238 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1239 if (Existing != N) {
1240 // If there was already an existing matching node, use ReplaceAllUsesWith
1241 // to replace the dead one with the existing one. This can cause
1242 // recursive merging of other unrelated nodes down the line.
1243 ReplaceAllUsesWith(N, Existing);
1244
1245 // N is now dead. Inform the listeners and delete it.
1246 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1247 DUL->NodeDeleted(N, Existing);
1248 DeleteNodeNotInCSEMaps(N);
1249 return;
1250 }
1251 }
1252
1253 // If the node doesn't already exist, we updated it. Inform listeners.
1254 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1255 DUL->NodeUpdated(N);
1256}
1257
1258/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1259/// were replaced with those specified. If this node is never memoized,
1260/// return null, otherwise return a pointer to the slot it would take. If a
1261/// node already exists with these operands, the slot will be non-null.
1262SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1263 void *&InsertPos) {
1264 if (doNotCSE(N))
1265 return nullptr;
1266
1267 SDValue Ops[] = { Op };
1269 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1271 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1272 if (Node)
1273 Node->intersectFlagsWith(N->getFlags());
1274 return Node;
1275}
1276
1277/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1278/// were replaced with those specified. If this node is never memoized,
1279/// return null, otherwise return a pointer to the slot it would take. If a
1280/// node already exists with these operands, the slot will be non-null.
1281SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1282 SDValue Op1, SDValue Op2,
1283 void *&InsertPos) {
1284 if (doNotCSE(N))
1285 return nullptr;
1286
1287 SDValue Ops[] = { Op1, Op2 };
1289 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1291 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1292 if (Node)
1293 Node->intersectFlagsWith(N->getFlags());
1294 return Node;
1295}
1296
1297/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1298/// were replaced with those specified. If this node is never memoized,
1299/// return null, otherwise return a pointer to the slot it would take. If a
1300/// node already exists with these operands, the slot will be non-null.
1301SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1302 void *&InsertPos) {
1303 if (doNotCSE(N))
1304 return nullptr;
1305
1307 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1309 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1310 if (Node)
1311 Node->intersectFlagsWith(N->getFlags());
1312 return Node;
1313}
1314
1316 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1317 : VT.getTypeForEVT(*getContext());
1318
1319 return getDataLayout().getABITypeAlign(Ty);
1320}
1321
1322// EntryNode could meaningfully have debug info if we can find it...
1324 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1325 getVTList(MVT::Other, MVT::Glue)),
1326 Root(getEntryNode()) {
1327 InsertNode(&EntryNode);
1328 DbgInfo = new SDDbgInfo();
1329}
1330
1332 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1333 const TargetLibraryInfo *LibraryInfo,
1334 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1335 BlockFrequencyInfo *BFIin,
1336 FunctionVarLocs const *VarLocs) {
1337 MF = &NewMF;
1338 SDAGISelPass = PassPtr;
1339 ORE = &NewORE;
1342 LibInfo = LibraryInfo;
1343 Context = &MF->getFunction().getContext();
1344 UA = NewUA;
1345 PSI = PSIin;
1346 BFI = BFIin;
1347 FnVarLocs = VarLocs;
1348}
1349
1351 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1352 allnodes_clear();
1353 OperandRecycler.clear(OperandAllocator);
1354 delete DbgInfo;
1355}
1356
1358 return MF->getFunction().hasOptSize() ||
1360}
1361
1362void SelectionDAG::allnodes_clear() {
1363 assert(&*AllNodes.begin() == &EntryNode);
1364 AllNodes.remove(AllNodes.begin());
1365 while (!AllNodes.empty())
1366 DeallocateNode(&AllNodes.front());
1367#ifndef NDEBUG
1368 NextPersistentId = 0;
1369#endif
1370}
1371
1372SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1373 void *&InsertPos) {
1374 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1375 if (N) {
1376 switch (N->getOpcode()) {
1377 default: break;
1378 case ISD::Constant:
1379 case ISD::ConstantFP:
1380 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1381 "debug location. Use another overload.");
1382 }
1383 }
1384 return N;
1385}
1386
1387SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1388 const SDLoc &DL, void *&InsertPos) {
1389 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1390 if (N) {
1391 switch (N->getOpcode()) {
1392 case ISD::Constant:
1393 case ISD::ConstantFP:
1394 // Erase debug location from the node if the node is used at several
1395 // different places. Do not propagate one location to all uses as it
1396 // will cause a worse single stepping debugging experience.
1397 if (N->getDebugLoc() != DL.getDebugLoc())
1398 N->setDebugLoc(DebugLoc());
1399 break;
1400 default:
1401 // When the node's point of use is located earlier in the instruction
1402 // sequence than its prior point of use, update its debug info to the
1403 // earlier location.
1404 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1405 N->setDebugLoc(DL.getDebugLoc());
1406 break;
1407 }
1408 }
1409 return N;
1410}
1411
1413 allnodes_clear();
1414 OperandRecycler.clear(OperandAllocator);
1415 OperandAllocator.Reset();
1416 CSEMap.clear();
1417
1418 ExtendedValueTypeNodes.clear();
1419 ExternalSymbols.clear();
1420 TargetExternalSymbols.clear();
1421 MCSymbols.clear();
1422 SDEI.clear();
1423 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(), nullptr);
1424 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(), nullptr);
1425
1426 EntryNode.UseList = nullptr;
1427 InsertNode(&EntryNode);
1428 Root = getEntryNode();
1429 DbgInfo->clear();
1430}
1431
1433 return VT.bitsGT(Op.getValueType())
1434 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1435 : getNode(ISD::FP_ROUND, DL, VT, Op,
1436 getIntPtrConstant(0, DL, /*isTarget=*/true));
1437}
1438
1439std::pair<SDValue, SDValue>
1441 const SDLoc &DL, EVT VT) {
1442 assert(!VT.bitsEq(Op.getValueType()) &&
1443 "Strict no-op FP extend/round not allowed.");
1444 SDValue Res =
1445 VT.bitsGT(Op.getValueType())
1446 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1447 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1448 {Chain, Op, getIntPtrConstant(0, DL)});
1449
1450 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1451}
1452
1454 return VT.bitsGT(Op.getValueType()) ?
1455 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1456 getNode(ISD::TRUNCATE, DL, VT, Op);
1457}
1458
1460 return VT.bitsGT(Op.getValueType()) ?
1461 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1462 getNode(ISD::TRUNCATE, DL, VT, Op);
1463}
1464
1466 return VT.bitsGT(Op.getValueType()) ?
1467 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1468 getNode(ISD::TRUNCATE, DL, VT, Op);
1469}
1470
1472 EVT VT) {
1473 assert(!VT.isVector());
1474 auto Type = Op.getValueType();
1475 SDValue DestOp;
1476 if (Type == VT)
1477 return Op;
1478 auto Size = Op.getValueSizeInBits();
1479 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1480 if (DestOp.getValueType() == VT)
1481 return DestOp;
1482
1483 return getAnyExtOrTrunc(DestOp, DL, VT);
1484}
1485
1487 EVT VT) {
1488 assert(!VT.isVector());
1489 auto Type = Op.getValueType();
1490 SDValue DestOp;
1491 if (Type == VT)
1492 return Op;
1493 auto Size = Op.getValueSizeInBits();
1494 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1495 if (DestOp.getValueType() == VT)
1496 return DestOp;
1497
1498 return getSExtOrTrunc(DestOp, DL, VT);
1499}
1500
1502 EVT VT) {
1503 assert(!VT.isVector());
1504 auto Type = Op.getValueType();
1505 SDValue DestOp;
1506 if (Type == VT)
1507 return Op;
1508 auto Size = Op.getValueSizeInBits();
1509 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1510 if (DestOp.getValueType() == VT)
1511 return DestOp;
1512
1513 return getZExtOrTrunc(DestOp, DL, VT);
1514}
1515
1517 EVT OpVT) {
1518 if (VT.bitsLE(Op.getValueType()))
1519 return getNode(ISD::TRUNCATE, SL, VT, Op);
1520
1522 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1523}
1524
1526 EVT OpVT = Op.getValueType();
1527 assert(VT.isInteger() && OpVT.isInteger() &&
1528 "Cannot getZeroExtendInReg FP types");
1529 assert(VT.isVector() == OpVT.isVector() &&
1530 "getZeroExtendInReg type should be vector iff the operand "
1531 "type is vector!");
1532 assert((!VT.isVector() ||
1534 "Vector element counts must match in getZeroExtendInReg");
1535 assert(VT.bitsLE(OpVT) && "Not extending!");
1536 if (OpVT == VT)
1537 return Op;
1539 VT.getScalarSizeInBits());
1540 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1541}
1542
1544 // Only unsigned pointer semantics are supported right now. In the future this
1545 // might delegate to TLI to check pointer signedness.
1546 return getZExtOrTrunc(Op, DL, VT);
1547}
1548
1550 // Only unsigned pointer semantics are supported right now. In the future this
1551 // might delegate to TLI to check pointer signedness.
1552 return getZeroExtendInReg(Op, DL, VT);
1553}
1554
1556 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1557}
1558
1559/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1561 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1562}
1563
1565 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1566 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1567}
1568
1570 SDValue Mask, SDValue EVL, EVT VT) {
1571 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1572 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1573}
1574
1576 SDValue Mask, SDValue EVL) {
1577 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1578}
1579
1581 SDValue Mask, SDValue EVL) {
1582 if (VT.bitsGT(Op.getValueType()))
1583 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1584 if (VT.bitsLT(Op.getValueType()))
1585 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1586 return Op;
1587}
1588
1590 EVT OpVT) {
1591 if (!V)
1592 return getConstant(0, DL, VT);
1593
1594 switch (TLI->getBooleanContents(OpVT)) {
1597 return getConstant(1, DL, VT);
1599 return getAllOnesConstant(DL, VT);
1600 }
1601 llvm_unreachable("Unexpected boolean content enum!");
1602}
1603
1605 bool isT, bool isO) {
1606 EVT EltVT = VT.getScalarType();
1607 assert((EltVT.getSizeInBits() >= 64 ||
1608 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1609 "getConstant with a uint64_t value that doesn't fit in the type!");
1610 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
1611}
1612
1614 bool isT, bool isO) {
1615 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1616}
1617
1619 EVT VT, bool isT, bool isO) {
1620 assert(VT.isInteger() && "Cannot create FP integer constant!");
1621
1622 EVT EltVT = VT.getScalarType();
1623 const ConstantInt *Elt = &Val;
1624
1625 // In some cases the vector type is legal but the element type is illegal and
1626 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1627 // inserted value (the type does not need to match the vector element type).
1628 // Any extra bits introduced will be truncated away.
1629 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1631 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1632 APInt NewVal;
1633 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1634 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1635 else
1636 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1637 Elt = ConstantInt::get(*getContext(), NewVal);
1638 }
1639 // In other cases the element type is illegal and needs to be expanded, for
1640 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1641 // the value into n parts and use a vector type with n-times the elements.
1642 // Then bitcast to the type requested.
1643 // Legalizing constants too early makes the DAGCombiner's job harder so we
1644 // only legalize if the DAG tells us we must produce legal types.
1645 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1646 TLI->getTypeAction(*getContext(), EltVT) ==
1648 const APInt &NewVal = Elt->getValue();
1649 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1650 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1651
1652 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1653 if (VT.isScalableVector() ||
1655 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1656 "Can only handle an even split!");
1657 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1658
1659 SmallVector<SDValue, 2> ScalarParts;
1660 for (unsigned i = 0; i != Parts; ++i)
1661 ScalarParts.push_back(getConstant(
1662 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1663 ViaEltVT, isT, isO));
1664
1665 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1666 }
1667
1668 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1669 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1670
1671 // Check the temporary vector is the correct size. If this fails then
1672 // getTypeToTransformTo() probably returned a type whose size (in bits)
1673 // isn't a power-of-2 factor of the requested type size.
1674 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1675
1676 SmallVector<SDValue, 2> EltParts;
1677 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1678 EltParts.push_back(getConstant(
1679 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1680 ViaEltVT, isT, isO));
1681
1682 // EltParts is currently in little endian order. If we actually want
1683 // big-endian order then reverse it now.
1684 if (getDataLayout().isBigEndian())
1685 std::reverse(EltParts.begin(), EltParts.end());
1686
1687 // The elements must be reversed when the element order is different
1688 // to the endianness of the elements (because the BITCAST is itself a
1689 // vector shuffle in this situation). However, we do not need any code to
1690 // perform this reversal because getConstant() is producing a vector
1691 // splat.
1692 // This situation occurs in MIPS MSA.
1693
1695 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1696 llvm::append_range(Ops, EltParts);
1697
1698 SDValue V =
1699 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1700 return V;
1701 }
1702
1703 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1704 "APInt size does not match type size!");
1705 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1706 SDVTList VTs = getVTList(EltVT);
1708 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1709 ID.AddPointer(Elt);
1710 ID.AddBoolean(isO);
1711 void *IP = nullptr;
1712 SDNode *N = nullptr;
1713 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1714 if (!VT.isVector())
1715 return SDValue(N, 0);
1716
1717 if (!N) {
1718 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1719 CSEMap.InsertNode(N, IP);
1720 InsertNode(N);
1721 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1722 }
1723
1724 SDValue Result(N, 0);
1725 if (VT.isVector())
1726 Result = getSplat(VT, DL, Result);
1727 return Result;
1728}
1729
1731 bool isTarget) {
1732 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1733}
1734
1736 const SDLoc &DL, bool LegalTypes) {
1737 assert(VT.isInteger() && "Shift amount is not an integer type!");
1738 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout(), LegalTypes);
1739 return getConstant(Val, DL, ShiftVT);
1740}
1741
1743 const SDLoc &DL, bool LegalTypes) {
1744 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1745 return getShiftAmountConstant(Val.getZExtValue(), VT, DL, LegalTypes);
1746}
1747
1749 bool isTarget) {
1750 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1751}
1752
1754 bool isTarget) {
1755 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1756}
1757
1759 EVT VT, bool isTarget) {
1760 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1761
1762 EVT EltVT = VT.getScalarType();
1763
1764 // Do the map lookup using the actual bit pattern for the floating point
1765 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1766 // we don't have issues with SNANs.
1767 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1768 SDVTList VTs = getVTList(EltVT);
1770 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1771 ID.AddPointer(&V);
1772 void *IP = nullptr;
1773 SDNode *N = nullptr;
1774 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1775 if (!VT.isVector())
1776 return SDValue(N, 0);
1777
1778 if (!N) {
1779 N = newSDNode<ConstantFPSDNode>(isTarget, &V, VTs);
1780 CSEMap.InsertNode(N, IP);
1781 InsertNode(N);
1782 }
1783
1784 SDValue Result(N, 0);
1785 if (VT.isVector())
1786 Result = getSplat(VT, DL, Result);
1787 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1788 return Result;
1789}
1790
1792 bool isTarget) {
1793 EVT EltVT = VT.getScalarType();
1794 if (EltVT == MVT::f32)
1795 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1796 if (EltVT == MVT::f64)
1797 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1798 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1799 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1800 bool Ignored;
1801 APFloat APF = APFloat(Val);
1803 &Ignored);
1804 return getConstantFP(APF, DL, VT, isTarget);
1805 }
1806 llvm_unreachable("Unsupported type in getConstantFP");
1807}
1808
1810 EVT VT, int64_t Offset, bool isTargetGA,
1811 unsigned TargetFlags) {
1812 assert((TargetFlags == 0 || isTargetGA) &&
1813 "Cannot set target flags on target-independent globals");
1814
1815 // Truncate (with sign-extension) the offset value to the pointer size.
1817 if (BitWidth < 64)
1819
1820 unsigned Opc;
1821 if (GV->isThreadLocal())
1823 else
1824 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1825
1826 SDVTList VTs = getVTList(VT);
1828 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1829 ID.AddPointer(GV);
1830 ID.AddInteger(Offset);
1831 ID.AddInteger(TargetFlags);
1832 void *IP = nullptr;
1833 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1834 return SDValue(E, 0);
1835
1836 auto *N = newSDNode<GlobalAddressSDNode>(
1837 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1838 CSEMap.InsertNode(N, IP);
1839 InsertNode(N);
1840 return SDValue(N, 0);
1841}
1842
1843SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1844 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1845 SDVTList VTs = getVTList(VT);
1847 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1848 ID.AddInteger(FI);
1849 void *IP = nullptr;
1850 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1851 return SDValue(E, 0);
1852
1853 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1854 CSEMap.InsertNode(N, IP);
1855 InsertNode(N);
1856 return SDValue(N, 0);
1857}
1858
1859SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1860 unsigned TargetFlags) {
1861 assert((TargetFlags == 0 || isTarget) &&
1862 "Cannot set target flags on target-independent jump tables");
1863 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1864 SDVTList VTs = getVTList(VT);
1866 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1867 ID.AddInteger(JTI);
1868 ID.AddInteger(TargetFlags);
1869 void *IP = nullptr;
1870 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1871 return SDValue(E, 0);
1872
1873 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1874 CSEMap.InsertNode(N, IP);
1875 InsertNode(N);
1876 return SDValue(N, 0);
1877}
1878
1880 const SDLoc &DL) {
1882 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1883 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1884}
1885
1887 MaybeAlign Alignment, int Offset,
1888 bool isTarget, unsigned TargetFlags) {
1889 assert((TargetFlags == 0 || isTarget) &&
1890 "Cannot set target flags on target-independent globals");
1891 if (!Alignment)
1892 Alignment = shouldOptForSize()
1893 ? getDataLayout().getABITypeAlign(C->getType())
1894 : getDataLayout().getPrefTypeAlign(C->getType());
1895 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1896 SDVTList VTs = getVTList(VT);
1898 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1899 ID.AddInteger(Alignment->value());
1900 ID.AddInteger(Offset);
1901 ID.AddPointer(C);
1902 ID.AddInteger(TargetFlags);
1903 void *IP = nullptr;
1904 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1905 return SDValue(E, 0);
1906
1907 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1908 TargetFlags);
1909 CSEMap.InsertNode(N, IP);
1910 InsertNode(N);
1911 SDValue V = SDValue(N, 0);
1912 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
1913 return V;
1914}
1915
1917 MaybeAlign Alignment, int Offset,
1918 bool isTarget, unsigned TargetFlags) {
1919 assert((TargetFlags == 0 || isTarget) &&
1920 "Cannot set target flags on target-independent globals");
1921 if (!Alignment)
1922 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
1923 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1924 SDVTList VTs = getVTList(VT);
1926 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1927 ID.AddInteger(Alignment->value());
1928 ID.AddInteger(Offset);
1929 C->addSelectionDAGCSEId(ID);
1930 ID.AddInteger(TargetFlags);
1931 void *IP = nullptr;
1932 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1933 return SDValue(E, 0);
1934
1935 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1936 TargetFlags);
1937 CSEMap.InsertNode(N, IP);
1938 InsertNode(N);
1939 return SDValue(N, 0);
1940}
1941
1944 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), std::nullopt);
1945 ID.AddPointer(MBB);
1946 void *IP = nullptr;
1947 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1948 return SDValue(E, 0);
1949
1950 auto *N = newSDNode<BasicBlockSDNode>(MBB);
1951 CSEMap.InsertNode(N, IP);
1952 InsertNode(N);
1953 return SDValue(N, 0);
1954}
1955
1957 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1958 ValueTypeNodes.size())
1959 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
1960
1961 SDNode *&N = VT.isExtended() ?
1962 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
1963
1964 if (N) return SDValue(N, 0);
1965 N = newSDNode<VTSDNode>(VT);
1966 InsertNode(N);
1967 return SDValue(N, 0);
1968}
1969
1971 SDNode *&N = ExternalSymbols[Sym];
1972 if (N) return SDValue(N, 0);
1973 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
1974 InsertNode(N);
1975 return SDValue(N, 0);
1976}
1977
1979 SDNode *&N = MCSymbols[Sym];
1980 if (N)
1981 return SDValue(N, 0);
1982 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
1983 InsertNode(N);
1984 return SDValue(N, 0);
1985}
1986
1988 unsigned TargetFlags) {
1989 SDNode *&N =
1990 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
1991 if (N) return SDValue(N, 0);
1992 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
1993 InsertNode(N);
1994 return SDValue(N, 0);
1995}
1996
1998 if ((unsigned)Cond >= CondCodeNodes.size())
1999 CondCodeNodes.resize(Cond+1);
2000
2001 if (!CondCodeNodes[Cond]) {
2002 auto *N = newSDNode<CondCodeSDNode>(Cond);
2003 CondCodeNodes[Cond] = N;
2004 InsertNode(N);
2005 }
2006
2007 return SDValue(CondCodeNodes[Cond], 0);
2008}
2009
2011 bool ConstantFold) {
2012 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2013 "APInt size does not match type size!");
2014
2015 if (MulImm == 0)
2016 return getConstant(0, DL, VT);
2017
2018 if (ConstantFold) {
2019 const MachineFunction &MF = getMachineFunction();
2020 const Function &F = MF.getFunction();
2021 ConstantRange CR = getVScaleRange(&F, 64);
2022 if (const APInt *C = CR.getSingleElement())
2023 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2024 }
2025
2026 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2027}
2028
2030 bool ConstantFold) {
2031 if (EC.isScalable())
2032 return getVScale(DL, VT,
2033 APInt(VT.getSizeInBits(), EC.getKnownMinValue()));
2034
2035 return getConstant(EC.getKnownMinValue(), DL, VT);
2036}
2037
2039 APInt One(ResVT.getScalarSizeInBits(), 1);
2040 return getStepVector(DL, ResVT, One);
2041}
2042
2044 const APInt &StepVal) {
2045 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2046 if (ResVT.isScalableVector())
2047 return getNode(
2048 ISD::STEP_VECTOR, DL, ResVT,
2049 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2050
2051 SmallVector<SDValue, 16> OpsStepConstants;
2052 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2053 OpsStepConstants.push_back(
2054 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2055 return getBuildVector(ResVT, DL, OpsStepConstants);
2056}
2057
2058/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2059/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2061 std::swap(N1, N2);
2063}
2064
2066 SDValue N2, ArrayRef<int> Mask) {
2067 assert(VT.getVectorNumElements() == Mask.size() &&
2068 "Must have the same number of vector elements as mask elements!");
2069 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2070 "Invalid VECTOR_SHUFFLE");
2071
2072 // Canonicalize shuffle undef, undef -> undef
2073 if (N1.isUndef() && N2.isUndef())
2074 return getUNDEF(VT);
2075
2076 // Validate that all indices in Mask are within the range of the elements
2077 // input to the shuffle.
2078 int NElts = Mask.size();
2079 assert(llvm::all_of(Mask,
2080 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2081 "Index out of range");
2082
2083 // Copy the mask so we can do any needed cleanup.
2084 SmallVector<int, 8> MaskVec(Mask);
2085
2086 // Canonicalize shuffle v, v -> v, undef
2087 if (N1 == N2) {
2088 N2 = getUNDEF(VT);
2089 for (int i = 0; i != NElts; ++i)
2090 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2091 }
2092
2093 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2094 if (N1.isUndef())
2095 commuteShuffle(N1, N2, MaskVec);
2096
2097 if (TLI->hasVectorBlend()) {
2098 // If shuffling a splat, try to blend the splat instead. We do this here so
2099 // that even when this arises during lowering we don't have to re-handle it.
2100 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2101 BitVector UndefElements;
2102 SDValue Splat = BV->getSplatValue(&UndefElements);
2103 if (!Splat)
2104 return;
2105
2106 for (int i = 0; i < NElts; ++i) {
2107 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2108 continue;
2109
2110 // If this input comes from undef, mark it as such.
2111 if (UndefElements[MaskVec[i] - Offset]) {
2112 MaskVec[i] = -1;
2113 continue;
2114 }
2115
2116 // If we can blend a non-undef lane, use that instead.
2117 if (!UndefElements[i])
2118 MaskVec[i] = i + Offset;
2119 }
2120 };
2121 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2122 BlendSplat(N1BV, 0);
2123 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2124 BlendSplat(N2BV, NElts);
2125 }
2126
2127 // Canonicalize all index into lhs, -> shuffle lhs, undef
2128 // Canonicalize all index into rhs, -> shuffle rhs, undef
2129 bool AllLHS = true, AllRHS = true;
2130 bool N2Undef = N2.isUndef();
2131 for (int i = 0; i != NElts; ++i) {
2132 if (MaskVec[i] >= NElts) {
2133 if (N2Undef)
2134 MaskVec[i] = -1;
2135 else
2136 AllLHS = false;
2137 } else if (MaskVec[i] >= 0) {
2138 AllRHS = false;
2139 }
2140 }
2141 if (AllLHS && AllRHS)
2142 return getUNDEF(VT);
2143 if (AllLHS && !N2Undef)
2144 N2 = getUNDEF(VT);
2145 if (AllRHS) {
2146 N1 = getUNDEF(VT);
2147 commuteShuffle(N1, N2, MaskVec);
2148 }
2149 // Reset our undef status after accounting for the mask.
2150 N2Undef = N2.isUndef();
2151 // Re-check whether both sides ended up undef.
2152 if (N1.isUndef() && N2Undef)
2153 return getUNDEF(VT);
2154
2155 // If Identity shuffle return that node.
2156 bool Identity = true, AllSame = true;
2157 for (int i = 0; i != NElts; ++i) {
2158 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2159 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2160 }
2161 if (Identity && NElts)
2162 return N1;
2163
2164 // Shuffling a constant splat doesn't change the result.
2165 if (N2Undef) {
2166 SDValue V = N1;
2167
2168 // Look through any bitcasts. We check that these don't change the number
2169 // (and size) of elements and just changes their types.
2170 while (V.getOpcode() == ISD::BITCAST)
2171 V = V->getOperand(0);
2172
2173 // A splat should always show up as a build vector node.
2174 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2175 BitVector UndefElements;
2176 SDValue Splat = BV->getSplatValue(&UndefElements);
2177 // If this is a splat of an undef, shuffling it is also undef.
2178 if (Splat && Splat.isUndef())
2179 return getUNDEF(VT);
2180
2181 bool SameNumElts =
2182 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2183
2184 // We only have a splat which can skip shuffles if there is a splatted
2185 // value and no undef lanes rearranged by the shuffle.
2186 if (Splat && UndefElements.none()) {
2187 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2188 // number of elements match or the value splatted is a zero constant.
2189 if (SameNumElts || isNullConstant(Splat))
2190 return N1;
2191 }
2192
2193 // If the shuffle itself creates a splat, build the vector directly.
2194 if (AllSame && SameNumElts) {
2195 EVT BuildVT = BV->getValueType(0);
2196 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2197 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2198
2199 // We may have jumped through bitcasts, so the type of the
2200 // BUILD_VECTOR may not match the type of the shuffle.
2201 if (BuildVT != VT)
2202 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2203 return NewBV;
2204 }
2205 }
2206 }
2207
2208 SDVTList VTs = getVTList(VT);
2210 SDValue Ops[2] = { N1, N2 };
2212 for (int i = 0; i != NElts; ++i)
2213 ID.AddInteger(MaskVec[i]);
2214
2215 void* IP = nullptr;
2216 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2217 return SDValue(E, 0);
2218
2219 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2220 // SDNode doesn't have access to it. This memory will be "leaked" when
2221 // the node is deallocated, but recovered when the NodeAllocator is released.
2222 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2223 llvm::copy(MaskVec, MaskAlloc);
2224
2225 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2226 dl.getDebugLoc(), MaskAlloc);
2227 createOperands(N, Ops);
2228
2229 CSEMap.InsertNode(N, IP);
2230 InsertNode(N);
2231 SDValue V = SDValue(N, 0);
2232 NewSDValueDbgMsg(V, "Creating new node: ", this);
2233 return V;
2234}
2235
2237 EVT VT = SV.getValueType(0);
2238 SmallVector<int, 8> MaskVec(SV.getMask());
2240
2241 SDValue Op0 = SV.getOperand(0);
2242 SDValue Op1 = SV.getOperand(1);
2243 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2244}
2245
2247 SDVTList VTs = getVTList(VT);
2249 AddNodeIDNode(ID, ISD::Register, VTs, std::nullopt);
2250 ID.AddInteger(RegNo);
2251 void *IP = nullptr;
2252 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2253 return SDValue(E, 0);
2254
2255 auto *N = newSDNode<RegisterSDNode>(RegNo, VTs);
2256 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2257 CSEMap.InsertNode(N, IP);
2258 InsertNode(N);
2259 return SDValue(N, 0);
2260}
2261
2264 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), std::nullopt);
2265 ID.AddPointer(RegMask);
2266 void *IP = nullptr;
2267 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2268 return SDValue(E, 0);
2269
2270 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2271 CSEMap.InsertNode(N, IP);
2272 InsertNode(N);
2273 return SDValue(N, 0);
2274}
2275
2277 MCSymbol *Label) {
2278 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2279}
2280
2281SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2282 SDValue Root, MCSymbol *Label) {
2284 SDValue Ops[] = { Root };
2285 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2286 ID.AddPointer(Label);
2287 void *IP = nullptr;
2288 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2289 return SDValue(E, 0);
2290
2291 auto *N =
2292 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2293 createOperands(N, Ops);
2294
2295 CSEMap.InsertNode(N, IP);
2296 InsertNode(N);
2297 return SDValue(N, 0);
2298}
2299
2301 int64_t Offset, bool isTarget,
2302 unsigned TargetFlags) {
2303 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2304 SDVTList VTs = getVTList(VT);
2305
2307 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
2308 ID.AddPointer(BA);
2309 ID.AddInteger(Offset);
2310 ID.AddInteger(TargetFlags);
2311 void *IP = nullptr;
2312 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2313 return SDValue(E, 0);
2314
2315 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2316 CSEMap.InsertNode(N, IP);
2317 InsertNode(N);
2318 return SDValue(N, 0);
2319}
2320
2323 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), std::nullopt);
2324 ID.AddPointer(V);
2325
2326 void *IP = nullptr;
2327 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2328 return SDValue(E, 0);
2329
2330 auto *N = newSDNode<SrcValueSDNode>(V);
2331 CSEMap.InsertNode(N, IP);
2332 InsertNode(N);
2333 return SDValue(N, 0);
2334}
2335
2338 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), std::nullopt);
2339 ID.AddPointer(MD);
2340
2341 void *IP = nullptr;
2342 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2343 return SDValue(E, 0);
2344
2345 auto *N = newSDNode<MDNodeSDNode>(MD);
2346 CSEMap.InsertNode(N, IP);
2347 InsertNode(N);
2348 return SDValue(N, 0);
2349}
2350
2352 if (VT == V.getValueType())
2353 return V;
2354
2355 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2356}
2357
2359 unsigned SrcAS, unsigned DestAS) {
2360 SDVTList VTs = getVTList(VT);
2361 SDValue Ops[] = {Ptr};
2364 ID.AddInteger(SrcAS);
2365 ID.AddInteger(DestAS);
2366
2367 void *IP = nullptr;
2368 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2369 return SDValue(E, 0);
2370
2371 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2372 VTs, SrcAS, DestAS);
2373 createOperands(N, Ops);
2374
2375 CSEMap.InsertNode(N, IP);
2376 InsertNode(N);
2377 return SDValue(N, 0);
2378}
2379
2381 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2382}
2383
2384/// getShiftAmountOperand - Return the specified value casted to
2385/// the target's desired shift amount type.
2387 EVT OpTy = Op.getValueType();
2388 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2389 if (OpTy == ShTy || OpTy.isVector()) return Op;
2390
2391 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2392}
2393
2395 SDLoc dl(Node);
2397 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2398 EVT VT = Node->getValueType(0);
2399 SDValue Tmp1 = Node->getOperand(0);
2400 SDValue Tmp2 = Node->getOperand(1);
2401 const MaybeAlign MA(Node->getConstantOperandVal(3));
2402
2403 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2404 Tmp2, MachinePointerInfo(V));
2405 SDValue VAList = VAListLoad;
2406
2407 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2408 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2409 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2410
2411 VAList =
2412 getNode(ISD::AND, dl, VAList.getValueType(), VAList,
2413 getConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2414 }
2415
2416 // Increment the pointer, VAList, to the next vaarg
2417 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2418 getConstant(getDataLayout().getTypeAllocSize(
2419 VT.getTypeForEVT(*getContext())),
2420 dl, VAList.getValueType()));
2421 // Store the incremented VAList to the legalized pointer
2422 Tmp1 =
2423 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2424 // Load the actual argument out of the pointer VAList
2425 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2426}
2427
2429 SDLoc dl(Node);
2431 // This defaults to loading a pointer from the input and storing it to the
2432 // output, returning the chain.
2433 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2434 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2435 SDValue Tmp1 =
2436 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2437 Node->getOperand(2), MachinePointerInfo(VS));
2438 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2439 MachinePointerInfo(VD));
2440}
2441
2443 const DataLayout &DL = getDataLayout();
2444 Type *Ty = VT.getTypeForEVT(*getContext());
2445 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2446
2447 if (TLI->isTypeLegal(VT) || !VT.isVector())
2448 return RedAlign;
2449
2451 const Align StackAlign = TFI->getStackAlign();
2452
2453 // See if we can choose a smaller ABI alignment in cases where it's an
2454 // illegal vector type that will get broken down.
2455 if (RedAlign > StackAlign) {
2456 EVT IntermediateVT;
2457 MVT RegisterVT;
2458 unsigned NumIntermediates;
2459 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2460 NumIntermediates, RegisterVT);
2461 Ty = IntermediateVT.getTypeForEVT(*getContext());
2462 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2463 if (RedAlign2 < RedAlign)
2464 RedAlign = RedAlign2;
2465 }
2466
2467 return RedAlign;
2468}
2469
2471 MachineFrameInfo &MFI = MF->getFrameInfo();
2473 int StackID = 0;
2474 if (Bytes.isScalable())
2475 StackID = TFI->getStackIDForScalableVectors();
2476 // The stack id gives an indication of whether the object is scalable or
2477 // not, so it's safe to pass in the minimum size here.
2478 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2479 false, nullptr, StackID);
2480 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2481}
2482
2484 Type *Ty = VT.getTypeForEVT(*getContext());
2485 Align StackAlign =
2486 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2487 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2488}
2489
2491 TypeSize VT1Size = VT1.getStoreSize();
2492 TypeSize VT2Size = VT2.getStoreSize();
2493 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2494 "Don't know how to choose the maximum size when creating a stack "
2495 "temporary");
2496 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2497 ? VT1Size
2498 : VT2Size;
2499
2500 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2501 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2502 const DataLayout &DL = getDataLayout();
2503 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2504 return CreateStackTemporary(Bytes, Align);
2505}
2506
2508 ISD::CondCode Cond, const SDLoc &dl) {
2509 EVT OpVT = N1.getValueType();
2510
2511 auto GetUndefBooleanConstant = [&]() {
2512 if (VT.getScalarType() == MVT::i1 ||
2513 TLI->getBooleanContents(OpVT) ==
2515 return getUNDEF(VT);
2516 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2517 // so we cannot use getUNDEF(). Return zero instead.
2518 return getConstant(0, dl, VT);
2519 };
2520
2521 // These setcc operations always fold.
2522 switch (Cond) {
2523 default: break;
2524 case ISD::SETFALSE:
2525 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2526 case ISD::SETTRUE:
2527 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2528
2529 case ISD::SETOEQ:
2530 case ISD::SETOGT:
2531 case ISD::SETOGE:
2532 case ISD::SETOLT:
2533 case ISD::SETOLE:
2534 case ISD::SETONE:
2535 case ISD::SETO:
2536 case ISD::SETUO:
2537 case ISD::SETUEQ:
2538 case ISD::SETUNE:
2539 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2540 break;
2541 }
2542
2543 if (OpVT.isInteger()) {
2544 // For EQ and NE, we can always pick a value for the undef to make the
2545 // predicate pass or fail, so we can return undef.
2546 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2547 // icmp eq/ne X, undef -> undef.
2548 if ((N1.isUndef() || N2.isUndef()) &&
2549 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2550 return GetUndefBooleanConstant();
2551
2552 // If both operands are undef, we can return undef for int comparison.
2553 // icmp undef, undef -> undef.
2554 if (N1.isUndef() && N2.isUndef())
2555 return GetUndefBooleanConstant();
2556
2557 // icmp X, X -> true/false
2558 // icmp X, undef -> true/false because undef could be X.
2559 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2560 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2561 }
2562
2563 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
2564 const APInt &C2 = N2C->getAPIntValue();
2565 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
2566 const APInt &C1 = N1C->getAPIntValue();
2567
2569 dl, VT, OpVT);
2570 }
2571 }
2572
2573 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2574 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2575
2576 if (N1CFP && N2CFP) {
2577 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2578 switch (Cond) {
2579 default: break;
2580 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2581 return GetUndefBooleanConstant();
2582 [[fallthrough]];
2583 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2584 OpVT);
2585 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2586 return GetUndefBooleanConstant();
2587 [[fallthrough]];
2589 R==APFloat::cmpLessThan, dl, VT,
2590 OpVT);
2591 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2592 return GetUndefBooleanConstant();
2593 [[fallthrough]];
2594 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2595 OpVT);
2596 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2597 return GetUndefBooleanConstant();
2598 [[fallthrough]];
2600 VT, OpVT);
2601 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2602 return GetUndefBooleanConstant();
2603 [[fallthrough]];
2605 R==APFloat::cmpEqual, dl, VT,
2606 OpVT);
2607 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2608 return GetUndefBooleanConstant();
2609 [[fallthrough]];
2611 R==APFloat::cmpEqual, dl, VT, OpVT);
2612 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2613 OpVT);
2614 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2615 OpVT);
2617 R==APFloat::cmpEqual, dl, VT,
2618 OpVT);
2619 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2620 OpVT);
2622 R==APFloat::cmpLessThan, dl, VT,
2623 OpVT);
2625 R==APFloat::cmpUnordered, dl, VT,
2626 OpVT);
2628 VT, OpVT);
2629 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2630 OpVT);
2631 }
2632 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2633 // Ensure that the constant occurs on the RHS.
2635 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2636 return SDValue();
2637 return getSetCC(dl, VT, N2, N1, SwappedCond);
2638 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2639 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2640 // If an operand is known to be a nan (or undef that could be a nan), we can
2641 // fold it.
2642 // Choosing NaN for the undef will always make unordered comparison succeed
2643 // and ordered comparison fails.
2644 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2645 switch (ISD::getUnorderedFlavor(Cond)) {
2646 default:
2647 llvm_unreachable("Unknown flavor!");
2648 case 0: // Known false.
2649 return getBoolConstant(false, dl, VT, OpVT);
2650 case 1: // Known true.
2651 return getBoolConstant(true, dl, VT, OpVT);
2652 case 2: // Undefined.
2653 return GetUndefBooleanConstant();
2654 }
2655 }
2656
2657 // Could not fold it.
2658 return SDValue();
2659}
2660
2661/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2662/// use this predicate to simplify operations downstream.
2664 unsigned BitWidth = Op.getScalarValueSizeInBits();
2666}
2667
2668/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2669/// this predicate to simplify operations downstream. Mask is known to be zero
2670/// for bits that V cannot have.
2672 unsigned Depth) const {
2673 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2674}
2675
2676/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2677/// DemandedElts. We use this predicate to simplify operations downstream.
2678/// Mask is known to be zero for bits that V cannot have.
2680 const APInt &DemandedElts,
2681 unsigned Depth) const {
2682 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2683}
2684
2685/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2686/// DemandedElts. We use this predicate to simplify operations downstream.
2688 unsigned Depth /* = 0 */) const {
2689 return computeKnownBits(V, DemandedElts, Depth).isZero();
2690}
2691
2692/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2694 unsigned Depth) const {
2695 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2696}
2697
2699 const APInt &DemandedElts,
2700 unsigned Depth) const {
2701 EVT VT = Op.getValueType();
2702 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2703
2704 unsigned NumElts = VT.getVectorNumElements();
2705 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2706
2707 APInt KnownZeroElements = APInt::getZero(NumElts);
2708 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2709 if (!DemandedElts[EltIdx])
2710 continue; // Don't query elements that are not demanded.
2711 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2712 if (MaskedVectorIsZero(Op, Mask, Depth))
2713 KnownZeroElements.setBit(EltIdx);
2714 }
2715 return KnownZeroElements;
2716}
2717
2718/// isSplatValue - Return true if the vector V has the same value
2719/// across all DemandedElts. For scalable vectors, we don't know the
2720/// number of lanes at compile time. Instead, we use a 1 bit APInt
2721/// to represent a conservative value for all lanes; that is, that
2722/// one bit value is implicitly splatted across all lanes.
2723bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2724 APInt &UndefElts, unsigned Depth) const {
2725 unsigned Opcode = V.getOpcode();
2726 EVT VT = V.getValueType();
2727 assert(VT.isVector() && "Vector type expected");
2728 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2729 "scalable demanded bits are ignored");
2730
2731 if (!DemandedElts)
2732 return false; // No demanded elts, better to assume we don't know anything.
2733
2734 if (Depth >= MaxRecursionDepth)
2735 return false; // Limit search depth.
2736
2737 // Deal with some common cases here that work for both fixed and scalable
2738 // vector types.
2739 switch (Opcode) {
2740 case ISD::SPLAT_VECTOR:
2741 UndefElts = V.getOperand(0).isUndef()
2742 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2743 : APInt(DemandedElts.getBitWidth(), 0);
2744 return true;
2745 case ISD::ADD:
2746 case ISD::SUB:
2747 case ISD::AND:
2748 case ISD::XOR:
2749 case ISD::OR: {
2750 APInt UndefLHS, UndefRHS;
2751 SDValue LHS = V.getOperand(0);
2752 SDValue RHS = V.getOperand(1);
2753 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2754 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1)) {
2755 UndefElts = UndefLHS | UndefRHS;
2756 return true;
2757 }
2758 return false;
2759 }
2760 case ISD::ABS:
2761 case ISD::TRUNCATE:
2762 case ISD::SIGN_EXTEND:
2763 case ISD::ZERO_EXTEND:
2764 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2765 default:
2766 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2767 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2768 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2769 Depth);
2770 break;
2771}
2772
2773 // We don't support other cases than those above for scalable vectors at
2774 // the moment.
2775 if (VT.isScalableVector())
2776 return false;
2777
2778 unsigned NumElts = VT.getVectorNumElements();
2779 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2780 UndefElts = APInt::getZero(NumElts);
2781
2782 switch (Opcode) {
2783 case ISD::BUILD_VECTOR: {
2784 SDValue Scl;
2785 for (unsigned i = 0; i != NumElts; ++i) {
2786 SDValue Op = V.getOperand(i);
2787 if (Op.isUndef()) {
2788 UndefElts.setBit(i);
2789 continue;
2790 }
2791 if (!DemandedElts[i])
2792 continue;
2793 if (Scl && Scl != Op)
2794 return false;
2795 Scl = Op;
2796 }
2797 return true;
2798 }
2799 case ISD::VECTOR_SHUFFLE: {
2800 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2801 APInt DemandedLHS = APInt::getZero(NumElts);
2802 APInt DemandedRHS = APInt::getZero(NumElts);
2803 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2804 for (int i = 0; i != (int)NumElts; ++i) {
2805 int M = Mask[i];
2806 if (M < 0) {
2807 UndefElts.setBit(i);
2808 continue;
2809 }
2810 if (!DemandedElts[i])
2811 continue;
2812 if (M < (int)NumElts)
2813 DemandedLHS.setBit(M);
2814 else
2815 DemandedRHS.setBit(M - NumElts);
2816 }
2817
2818 // If we aren't demanding either op, assume there's no splat.
2819 // If we are demanding both ops, assume there's no splat.
2820 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
2821 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
2822 return false;
2823
2824 // See if the demanded elts of the source op is a splat or we only demand
2825 // one element, which should always be a splat.
2826 // TODO: Handle source ops splats with undefs.
2827 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
2828 APInt SrcUndefs;
2829 return (SrcElts.popcount() == 1) ||
2830 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
2831 (SrcElts & SrcUndefs).isZero());
2832 };
2833 if (!DemandedLHS.isZero())
2834 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
2835 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
2836 }
2838 // Offset the demanded elts by the subvector index.
2839 SDValue Src = V.getOperand(0);
2840 // We don't support scalable vectors at the moment.
2841 if (Src.getValueType().isScalableVector())
2842 return false;
2843 uint64_t Idx = V.getConstantOperandVal(1);
2844 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2845 APInt UndefSrcElts;
2846 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
2847 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2848 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
2849 return true;
2850 }
2851 break;
2852 }
2856 // Widen the demanded elts by the src element count.
2857 SDValue Src = V.getOperand(0);
2858 // We don't support scalable vectors at the moment.
2859 if (Src.getValueType().isScalableVector())
2860 return false;
2861 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2862 APInt UndefSrcElts;
2863 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
2864 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2865 UndefElts = UndefSrcElts.trunc(NumElts);
2866 return true;
2867 }
2868 break;
2869 }
2870 case ISD::BITCAST: {
2871 SDValue Src = V.getOperand(0);
2872 EVT SrcVT = Src.getValueType();
2873 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
2874 unsigned BitWidth = VT.getScalarSizeInBits();
2875
2876 // Ignore bitcasts from unsupported types.
2877 // TODO: Add fp support?
2878 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
2879 break;
2880
2881 // Bitcast 'small element' vector to 'large element' vector.
2882 if ((BitWidth % SrcBitWidth) == 0) {
2883 // See if each sub element is a splat.
2884 unsigned Scale = BitWidth / SrcBitWidth;
2885 unsigned NumSrcElts = SrcVT.getVectorNumElements();
2886 APInt ScaledDemandedElts =
2887 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
2888 for (unsigned I = 0; I != Scale; ++I) {
2889 APInt SubUndefElts;
2890 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
2891 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
2892 SubDemandedElts &= ScaledDemandedElts;
2893 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
2894 return false;
2895 // TODO: Add support for merging sub undef elements.
2896 if (!SubUndefElts.isZero())
2897 return false;
2898 }
2899 return true;
2900 }
2901 break;
2902 }
2903 }
2904
2905 return false;
2906}
2907
2908/// Helper wrapper to main isSplatValue function.
2909bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
2910 EVT VT = V.getValueType();
2911 assert(VT.isVector() && "Vector type expected");
2912
2913 APInt UndefElts;
2914 // Since the number of lanes in a scalable vector is unknown at compile time,
2915 // we track one bit which is implicitly broadcast to all lanes. This means
2916 // that all lanes in a scalable vector are considered demanded.
2917 APInt DemandedElts
2919 return isSplatValue(V, DemandedElts, UndefElts) &&
2920 (AllowUndefs || !UndefElts);
2921}
2922
2925
2926 EVT VT = V.getValueType();
2927 unsigned Opcode = V.getOpcode();
2928 switch (Opcode) {
2929 default: {
2930 APInt UndefElts;
2931 // Since the number of lanes in a scalable vector is unknown at compile time,
2932 // we track one bit which is implicitly broadcast to all lanes. This means
2933 // that all lanes in a scalable vector are considered demanded.
2934 APInt DemandedElts
2936
2937 if (isSplatValue(V, DemandedElts, UndefElts)) {
2938 if (VT.isScalableVector()) {
2939 // DemandedElts and UndefElts are ignored for scalable vectors, since
2940 // the only supported cases are SPLAT_VECTOR nodes.
2941 SplatIdx = 0;
2942 } else {
2943 // Handle case where all demanded elements are UNDEF.
2944 if (DemandedElts.isSubsetOf(UndefElts)) {
2945 SplatIdx = 0;
2946 return getUNDEF(VT);
2947 }
2948 SplatIdx = (UndefElts & DemandedElts).countr_one();
2949 }
2950 return V;
2951 }
2952 break;
2953 }
2954 case ISD::SPLAT_VECTOR:
2955 SplatIdx = 0;
2956 return V;
2957 case ISD::VECTOR_SHUFFLE: {
2958 assert(!VT.isScalableVector());
2959 // Check if this is a shuffle node doing a splat.
2960 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
2961 // getTargetVShiftNode currently struggles without the splat source.
2962 auto *SVN = cast<ShuffleVectorSDNode>(V);
2963 if (!SVN->isSplat())
2964 break;
2965 int Idx = SVN->getSplatIndex();
2966 int NumElts = V.getValueType().getVectorNumElements();
2967 SplatIdx = Idx % NumElts;
2968 return V.getOperand(Idx / NumElts);
2969 }
2970 }
2971
2972 return SDValue();
2973}
2974
2976 int SplatIdx;
2977 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
2978 EVT SVT = SrcVector.getValueType().getScalarType();
2979 EVT LegalSVT = SVT;
2980 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
2981 if (!SVT.isInteger())
2982 return SDValue();
2983 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
2984 if (LegalSVT.bitsLT(SVT))
2985 return SDValue();
2986 }
2987 return getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(V), LegalSVT, SrcVector,
2988 getVectorIdxConstant(SplatIdx, SDLoc(V)));
2989 }
2990 return SDValue();
2991}
2992
2993const APInt *
2995 const APInt &DemandedElts) const {
2996 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
2997 V.getOpcode() == ISD::SRA) &&
2998 "Unknown shift node");
2999 unsigned BitWidth = V.getScalarValueSizeInBits();
3000 if (ConstantSDNode *SA = isConstOrConstSplat(V.getOperand(1), DemandedElts)) {
3001 // Shifting more than the bitwidth is not valid.
3002 const APInt &ShAmt = SA->getAPIntValue();
3003 if (ShAmt.ult(BitWidth))
3004 return &ShAmt;
3005 }
3006 return nullptr;
3007}
3008
3010 EVT VT = V.getValueType();
3011 APInt DemandedElts = VT.isFixedLengthVector()
3013 : APInt(1, 1);
3014 return getValidShiftAmountConstant(V, DemandedElts);
3015}
3016
3018 SDValue V, const APInt &DemandedElts) const {
3019 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3020 V.getOpcode() == ISD::SRA) &&
3021 "Unknown shift node");
3022 if (const APInt *ValidAmt = getValidShiftAmountConstant(V, DemandedElts))
3023 return ValidAmt;
3024 unsigned BitWidth = V.getScalarValueSizeInBits();
3025 auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1));
3026 if (!BV)
3027 return nullptr;
3028 const APInt *MinShAmt = nullptr;
3029 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3030 if (!DemandedElts[i])
3031 continue;
3032 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3033 if (!SA)
3034 return nullptr;
3035 // Shifting more than the bitwidth is not valid.
3036 const APInt &ShAmt = SA->getAPIntValue();
3037 if (ShAmt.uge(BitWidth))
3038 return nullptr;
3039 if (MinShAmt && MinShAmt->ule(ShAmt))
3040 continue;
3041 MinShAmt = &ShAmt;
3042 }
3043 return MinShAmt;
3044}
3045
3047 EVT VT = V.getValueType();
3048 APInt DemandedElts = VT.isFixedLengthVector()
3050 : APInt(1, 1);
3051 return getValidMinimumShiftAmountConstant(V, DemandedElts);
3052}
3053
3055 SDValue V, const APInt &DemandedElts) const {
3056 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3057 V.getOpcode() == ISD::SRA) &&
3058 "Unknown shift node");
3059 if (const APInt *ValidAmt = getValidShiftAmountConstant(V, DemandedElts))
3060 return ValidAmt;
3061 unsigned BitWidth = V.getScalarValueSizeInBits();
3062 auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1));
3063 if (!BV)
3064 return nullptr;
3065 const APInt *MaxShAmt = nullptr;
3066 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3067 if (!DemandedElts[i])
3068 continue;
3069 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3070 if (!SA)
3071 return nullptr;
3072 // Shifting more than the bitwidth is not valid.
3073 const APInt &ShAmt = SA->getAPIntValue();
3074 if (ShAmt.uge(BitWidth))
3075 return nullptr;
3076 if (MaxShAmt && MaxShAmt->uge(ShAmt))
3077 continue;
3078 MaxShAmt = &ShAmt;
3079 }
3080 return MaxShAmt;
3081}
3082
3084 EVT VT = V.getValueType();
3085 APInt DemandedElts = VT.isFixedLengthVector()
3087 : APInt(1, 1);
3088 return getValidMaximumShiftAmountConstant(V, DemandedElts);
3089}
3090
3091/// Determine which bits of Op are known to be either zero or one and return
3092/// them in Known. For vectors, the known bits are those that are shared by
3093/// every vector element.
3095 EVT VT = Op.getValueType();
3096
3097 // Since the number of lanes in a scalable vector is unknown at compile time,
3098 // we track one bit which is implicitly broadcast to all lanes. This means
3099 // that all lanes in a scalable vector are considered demanded.
3100 APInt DemandedElts = VT.isFixedLengthVector()
3102 : APInt(1, 1);
3103 return computeKnownBits(Op, DemandedElts, Depth);
3104}
3105
3106/// Determine which bits of Op are known to be either zero or one and return
3107/// them in Known. The DemandedElts argument allows us to only collect the known
3108/// bits that are shared by the requested vector elements.
3110 unsigned Depth) const {
3111 unsigned BitWidth = Op.getScalarValueSizeInBits();
3112
3113 KnownBits Known(BitWidth); // Don't know anything.
3114
3115 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3116 // We know all of the bits for a constant!
3117 return KnownBits::makeConstant(C->getAPIntValue());
3118 }
3119 if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
3120 // We know all of the bits for a constant fp!
3121 return KnownBits::makeConstant(C->getValueAPF().bitcastToAPInt());
3122 }
3123
3124 if (Depth >= MaxRecursionDepth)
3125 return Known; // Limit search depth.
3126
3127 KnownBits Known2;
3128 unsigned NumElts = DemandedElts.getBitWidth();
3129 assert((!Op.getValueType().isFixedLengthVector() ||
3130 NumElts == Op.getValueType().getVectorNumElements()) &&
3131 "Unexpected vector size");
3132
3133 if (!DemandedElts)
3134 return Known; // No demanded elts, better to assume we don't know anything.
3135
3136 unsigned Opcode = Op.getOpcode();
3137 switch (Opcode) {
3138 case ISD::MERGE_VALUES:
3139 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3140 Depth + 1);
3141 case ISD::SPLAT_VECTOR: {
3142 SDValue SrcOp = Op.getOperand(0);
3143 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3144 "Expected SPLAT_VECTOR implicit truncation");
3145 // Implicitly truncate the bits to match the official semantics of
3146 // SPLAT_VECTOR.
3147 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3148 break;
3149 }
3151 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3152 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3153 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3154 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3155 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3156 }
3157 break;
3158 }
3159 case ISD::STEP_VECTOR: {
3160 const APInt &Step = Op.getConstantOperandAPInt(0);
3161
3162 if (Step.isPowerOf2())
3163 Known.Zero.setLowBits(Step.logBase2());
3164
3166
3167 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3168 break;
3169 const APInt MinNumElts =
3170 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3171
3172 bool Overflow;
3173 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3175 .umul_ov(MinNumElts, Overflow);
3176 if (Overflow)
3177 break;
3178
3179 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3180 if (Overflow)
3181 break;
3182
3183 Known.Zero.setHighBits(MaxValue.countl_zero());
3184 break;
3185 }
3186 case ISD::BUILD_VECTOR:
3187 assert(!Op.getValueType().isScalableVector());
3188 // Collect the known bits that are shared by every demanded vector element.
3189 Known.Zero.setAllBits(); Known.One.setAllBits();
3190 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3191 if (!DemandedElts[i])
3192 continue;
3193
3194 SDValue SrcOp = Op.getOperand(i);
3195 Known2 = computeKnownBits(SrcOp, Depth + 1);
3196
3197 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3198 if (SrcOp.getValueSizeInBits() != BitWidth) {
3199 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3200 "Expected BUILD_VECTOR implicit truncation");
3201 Known2 = Known2.trunc(BitWidth);
3202 }
3203
3204 // Known bits are the values that are shared by every demanded element.
3205 Known = Known.intersectWith(Known2);
3206
3207 // If we don't know any bits, early out.
3208 if (Known.isUnknown())
3209 break;
3210 }
3211 break;
3212 case ISD::VECTOR_SHUFFLE: {
3213 assert(!Op.getValueType().isScalableVector());
3214 // Collect the known bits that are shared by every vector element referenced
3215 // by the shuffle.
3216 APInt DemandedLHS, DemandedRHS;
3217 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
3218 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3219 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3220 DemandedLHS, DemandedRHS))
3221 break;
3222
3223 // Known bits are the values that are shared by every demanded element.
3224 Known.Zero.setAllBits(); Known.One.setAllBits();
3225 if (!!DemandedLHS) {
3226 SDValue LHS = Op.getOperand(0);
3227 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3228 Known = Known.intersectWith(Known2);
3229 }
3230 // If we don't know any bits, early out.
3231 if (Known.isUnknown())
3232 break;
3233 if (!!DemandedRHS) {
3234 SDValue RHS = Op.getOperand(1);
3235 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3236 Known = Known.intersectWith(Known2);
3237 }
3238 break;
3239 }
3240 case ISD::VSCALE: {
3242 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3243 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3244 break;
3245 }
3246 case ISD::CONCAT_VECTORS: {
3247 if (Op.getValueType().isScalableVector())
3248 break;
3249 // Split DemandedElts and test each of the demanded subvectors.
3250 Known.Zero.setAllBits(); Known.One.setAllBits();
3251 EVT SubVectorVT = Op.getOperand(0).getValueType();
3252 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3253 unsigned NumSubVectors = Op.getNumOperands();
3254 for (unsigned i = 0; i != NumSubVectors; ++i) {
3255 APInt DemandedSub =
3256 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3257 if (!!DemandedSub) {
3258 SDValue Sub = Op.getOperand(i);
3259 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3260 Known = Known.intersectWith(Known2);
3261 }
3262 // If we don't know any bits, early out.
3263 if (Known.isUnknown())
3264 break;
3265 }
3266 break;
3267 }
3268 case ISD::INSERT_SUBVECTOR: {
3269 if (Op.getValueType().isScalableVector())
3270 break;
3271 // Demand any elements from the subvector and the remainder from the src its
3272 // inserted into.
3273 SDValue Src = Op.getOperand(0);
3274 SDValue Sub = Op.getOperand(1);
3275 uint64_t Idx = Op.getConstantOperandVal(2);
3276 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3277 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3278 APInt DemandedSrcElts = DemandedElts;
3279 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
3280
3281 Known.One.setAllBits();
3282 Known.Zero.setAllBits();
3283 if (!!DemandedSubElts) {
3284 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3285 if (Known.isUnknown())
3286 break; // early-out.
3287 }
3288 if (!!DemandedSrcElts) {
3289 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3290 Known = Known.intersectWith(Known2);
3291 }
3292 break;
3293 }
3295 // Offset the demanded elts by the subvector index.
3296 SDValue Src = Op.getOperand(0);
3297 // Bail until we can represent demanded elements for scalable vectors.
3298 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3299 break;
3300 uint64_t Idx = Op.getConstantOperandVal(1);
3301 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3302 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3303 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3304 break;
3305 }
3306 case ISD::SCALAR_TO_VECTOR: {
3307 if (Op.getValueType().isScalableVector())
3308 break;
3309 // We know about scalar_to_vector as much as we know about it source,
3310 // which becomes the first element of otherwise unknown vector.
3311 if (DemandedElts != 1)
3312 break;
3313
3314 SDValue N0 = Op.getOperand(0);
3315 Known = computeKnownBits(N0, Depth + 1);
3316 if (N0.getValueSizeInBits() != BitWidth)
3317 Known = Known.trunc(BitWidth);
3318
3319 break;
3320 }
3321 case ISD::BITCAST: {
3322 if (Op.getValueType().isScalableVector())
3323 break;
3324
3325 SDValue N0 = Op.getOperand(0);
3326 EVT SubVT = N0.getValueType();
3327 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3328
3329 // Ignore bitcasts from unsupported types.
3330 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3331 break;
3332
3333 // Fast handling of 'identity' bitcasts.
3334 if (BitWidth == SubBitWidth) {
3335 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3336 break;
3337 }
3338
3339 bool IsLE = getDataLayout().isLittleEndian();
3340
3341 // Bitcast 'small element' vector to 'large element' scalar/vector.
3342 if ((BitWidth % SubBitWidth) == 0) {
3343 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3344
3345 // Collect known bits for the (larger) output by collecting the known
3346 // bits from each set of sub elements and shift these into place.
3347 // We need to separately call computeKnownBits for each set of
3348 // sub elements as the knownbits for each is likely to be different.
3349 unsigned SubScale = BitWidth / SubBitWidth;
3350 APInt SubDemandedElts(NumElts * SubScale, 0);
3351 for (unsigned i = 0; i != NumElts; ++i)
3352 if (DemandedElts[i])
3353 SubDemandedElts.setBit(i * SubScale);
3354
3355 for (unsigned i = 0; i != SubScale; ++i) {
3356 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3357 Depth + 1);
3358 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3359 Known.insertBits(Known2, SubBitWidth * Shifts);
3360 }
3361 }
3362
3363 // Bitcast 'large element' scalar/vector to 'small element' vector.
3364 if ((SubBitWidth % BitWidth) == 0) {
3365 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3366
3367 // Collect known bits for the (smaller) output by collecting the known
3368 // bits from the overlapping larger input elements and extracting the
3369 // sub sections we actually care about.
3370 unsigned SubScale = SubBitWidth / BitWidth;
3371 APInt SubDemandedElts =
3372 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3373 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3374
3375 Known.Zero.setAllBits(); Known.One.setAllBits();
3376 for (unsigned i = 0; i != NumElts; ++i)
3377 if (DemandedElts[i]) {
3378 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3379 unsigned Offset = (Shifts % SubScale) * BitWidth;
3380 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3381 // If we don't know any bits, early out.
3382 if (Known.isUnknown())
3383 break;
3384 }
3385 }
3386 break;
3387 }
3388 case ISD::AND:
3389 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3390 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3391
3392 Known &= Known2;
3393 break;
3394 case ISD::OR:
3395 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3396 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3397
3398 Known |= Known2;
3399 break;
3400 case ISD::XOR:
3401 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3402 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3403
3404 Known ^= Known2;
3405 break;
3406 case ISD::MUL: {
3407 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3408 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3409 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3410 // TODO: SelfMultiply can be poison, but not undef.
3411 if (SelfMultiply)
3412 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3413 Op.getOperand(0), DemandedElts, false, Depth + 1);
3414 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3415
3416 // If the multiplication is known not to overflow, the product of a number
3417 // with itself is non-negative. Only do this if we didn't already computed
3418 // the opposite value for the sign bit.
3419 if (Op->getFlags().hasNoSignedWrap() &&
3420 Op.getOperand(0) == Op.getOperand(1) &&
3421 !Known.isNegative())
3422 Known.makeNonNegative();
3423 break;
3424 }
3425 case ISD::MULHU: {
3426 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3427 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3428 Known = KnownBits::mulhu(Known, Known2);
3429 break;
3430 }
3431 case ISD::MULHS: {
3432 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3433 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3434 Known = KnownBits::mulhs(Known, Known2);
3435 break;
3436 }
3437 case ISD::ABDU: {
3438 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3439 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3440 Known = KnownBits::abdu(Known, Known2);
3441 break;
3442 }
3443 case ISD::ABDS: {
3444 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3445 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3446 Known = KnownBits::abds(Known, Known2);
3447 break;
3448 }
3449 case ISD::UMUL_LOHI: {
3450 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3451 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3452 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3453 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3454 if (Op.getResNo() == 0)
3455 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3456 else
3457 Known = KnownBits::mulhu(Known, Known2);
3458 break;
3459 }
3460 case ISD::SMUL_LOHI: {
3461 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3462 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3463 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3464 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3465 if (Op.getResNo() == 0)
3466 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3467 else
3468 Known = KnownBits::mulhs(Known, Known2);
3469 break;
3470 }
3471 case ISD::AVGFLOORU:
3472 case ISD::AVGCEILU:
3473 case ISD::AVGFLOORS:
3474 case ISD::AVGCEILS: {
3475 bool IsCeil = Opcode == ISD::AVGCEILU || Opcode == ISD::AVGCEILS;
3476 bool IsSigned = Opcode == ISD::AVGFLOORS || Opcode == ISD::AVGCEILS;
3477 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3478 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3479 Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
3480 Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
3481 KnownBits Carry = KnownBits::makeConstant(APInt(1, IsCeil ? 1 : 0));
3482 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
3483 Known = Known.extractBits(BitWidth, 1);
3484 break;
3485 }
3486 case ISD::SELECT:
3487 case ISD::VSELECT:
3488 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3489 // If we don't know any bits, early out.
3490 if (Known.isUnknown())
3491 break;
3492 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3493
3494 // Only known if known in both the LHS and RHS.
3495 Known = Known.intersectWith(Known2);
3496 break;
3497 case ISD::SELECT_CC:
3498 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3499 // If we don't know any bits, early out.
3500 if (Known.isUnknown())
3501 break;
3502 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3503
3504 // Only known if known in both the LHS and RHS.
3505 Known = Known.intersectWith(Known2);
3506 break;
3507 case ISD::SMULO:
3508 case ISD::UMULO:
3509 if (Op.getResNo() != 1)
3510 break;
3511 // The boolean result conforms to getBooleanContents.
3512 // If we know the result of a setcc has the top bits zero, use this info.
3513 // We know that we have an integer-based boolean since these operations
3514 // are only available for integer.
3515 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3517 BitWidth > 1)
3518 Known.Zero.setBitsFrom(1);
3519 break;
3520 case ISD::SETCC:
3521 case ISD::SETCCCARRY:
3522 case ISD::STRICT_FSETCC:
3523 case ISD::STRICT_FSETCCS: {
3524 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3525 // If we know the result of a setcc has the top bits zero, use this info.
3526 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3528 BitWidth > 1)
3529 Known.Zero.setBitsFrom(1);
3530 break;
3531 }
3532 case ISD::SHL: {
3533 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3534 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3535
3536 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3537 bool NSW = Op->getFlags().hasNoSignedWrap();
3538
3539 bool ShAmtNonZero = Known2.isNonZero();
3540
3541 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3542
3543 // Minimum shift low bits are known zero.
3544 if (const APInt *ShMinAmt =
3546 Known.Zero.setLowBits(ShMinAmt->getZExtValue());
3547 break;
3548 }
3549 case ISD::SRL:
3550 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3551 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3552 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3553 Op->getFlags().hasExact());
3554
3555 // Minimum shift high bits are known zero.
3556 if (const APInt *ShMinAmt =
3558 Known.Zero.setHighBits(ShMinAmt->getZExtValue());
3559 break;
3560 case ISD::SRA:
3561 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3562 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3563 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3564 Op->getFlags().hasExact());
3565 break;
3566 case ISD::FSHL:
3567 case ISD::FSHR:
3568 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3569 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3570
3571 // For fshl, 0-shift returns the 1st arg.
3572 // For fshr, 0-shift returns the 2nd arg.
3573 if (Amt == 0) {
3574 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3575 DemandedElts, Depth + 1);
3576 break;
3577 }
3578
3579 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3580 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3581 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3582 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3583 if (Opcode == ISD::FSHL) {
3584 Known.One <<= Amt;
3585 Known.Zero <<= Amt;
3586 Known2.One.lshrInPlace(BitWidth - Amt);
3587 Known2.Zero.lshrInPlace(BitWidth - Amt);
3588 } else {
3589 Known.One <<= BitWidth - Amt;
3590 Known.Zero <<= BitWidth - Amt;
3591 Known2.One.lshrInPlace(Amt);
3592 Known2.Zero.lshrInPlace(Amt);
3593 }
3594 Known = Known.unionWith(Known2);
3595 }
3596 break;
3597 case ISD::SHL_PARTS:
3598 case ISD::SRA_PARTS:
3599 case ISD::SRL_PARTS: {
3600 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3601
3602 // Collect lo/hi source values and concatenate.
3603 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3604 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3605 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3606 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3607 Known = Known2.concat(Known);
3608
3609 // Collect shift amount.
3610 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3611
3612 if (Opcode == ISD::SHL_PARTS)
3613 Known = KnownBits::shl(Known, Known2);
3614 else if (Opcode == ISD::SRA_PARTS)
3615 Known = KnownBits::ashr(Known, Known2);
3616 else // if (Opcode == ISD::SRL_PARTS)
3617 Known = KnownBits::lshr(Known, Known2);
3618
3619 // TODO: Minimum shift low/high bits are known zero.
3620
3621 if (Op.getResNo() == 0)
3622 Known = Known.extractBits(LoBits, 0);
3623 else
3624 Known = Known.extractBits(HiBits, LoBits);
3625 break;
3626 }
3628 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3629 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3630 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3631 break;
3632 }
3633 case ISD::CTTZ:
3634 case ISD::CTTZ_ZERO_UNDEF: {
3635 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3636 // If we have a known 1, its position is our upper bound.
3637 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3638 unsigned LowBits = llvm::bit_width(PossibleTZ);
3639 Known.Zero.setBitsFrom(LowBits);
3640 break;
3641 }
3642 case ISD::CTLZ:
3643 case ISD::CTLZ_ZERO_UNDEF: {
3644 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3645 // If we have a known 1, its position is our upper bound.
3646 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3647 unsigned LowBits = llvm::bit_width(PossibleLZ);
3648 Known.Zero.setBitsFrom(LowBits);
3649 break;
3650 }
3651 case ISD::CTPOP: {
3652 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3653 // If we know some of the bits are zero, they can't be one.
3654 unsigned PossibleOnes = Known2.countMaxPopulation();
3655 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3656 break;
3657 }
3658 case ISD::PARITY: {
3659 // Parity returns 0 everywhere but the LSB.
3660 Known.Zero.setBitsFrom(1);
3661 break;
3662 }
3663 case ISD::LOAD: {
3664 LoadSDNode *LD = cast<LoadSDNode>(Op);
3665 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3666 if (ISD::isNON_EXTLoad(LD) && Cst) {
3667 // Determine any common known bits from the loaded constant pool value.
3668 Type *CstTy = Cst->getType();
3669 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3670 !Op.getValueType().isScalableVector()) {
3671 // If its a vector splat, then we can (quickly) reuse the scalar path.
3672 // NOTE: We assume all elements match and none are UNDEF.
3673 if (CstTy->isVectorTy()) {
3674 if (const Constant *Splat = Cst->getSplatValue()) {
3675 Cst = Splat;
3676 CstTy = Cst->getType();
3677 }
3678 }
3679 // TODO - do we need to handle different bitwidths?
3680 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3681 // Iterate across all vector elements finding common known bits.
3682 Known.One.setAllBits();
3683 Known.Zero.setAllBits();
3684 for (unsigned i = 0; i != NumElts; ++i) {
3685 if (!DemandedElts[i])
3686 continue;
3687 if (Constant *Elt = Cst->getAggregateElement(i)) {
3688 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3689 const APInt &Value = CInt->getValue();
3690 Known.One &= Value;
3691 Known.Zero &= ~Value;
3692 continue;
3693 }
3694 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3695 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3696 Known.One &= Value;
3697 Known.Zero &= ~Value;
3698 continue;
3699 }
3700 }
3701 Known.One.clearAllBits();
3702 Known.Zero.clearAllBits();
3703 break;
3704 }
3705 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3706 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3707 Known = KnownBits::makeConstant(CInt->getValue());
3708 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3709 Known =
3710 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3711 }
3712 }
3713 }
3714 } else if (Op.getResNo() == 0) {
3715 KnownBits Known0(!LD->getMemoryVT().isScalableVT()
3716 ? LD->getMemoryVT().getFixedSizeInBits()
3717 : BitWidth);
3718 EVT VT = Op.getValueType();
3719 // Fill in any known bits from range information. There are 3 types being
3720 // used. The results VT (same vector elt size as BitWidth), the loaded
3721 // MemoryVT (which may or may not be vector) and the range VTs original
3722 // type. The range matadata needs the full range (i.e
3723 // MemoryVT().getSizeInBits()), which is truncated to the correct elt size
3724 // if it is know. These are then extended to the original VT sizes below.
3725 if (const MDNode *MD = LD->getRanges()) {
3727 if (VT.isVector()) {
3728 // Handle truncation to the first demanded element.
3729 // TODO: Figure out which demanded elements are covered
3730 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
3731 break;
3732 Known0 = Known0.trunc(BitWidth);
3733 }
3734 }
3735
3736 if (LD->getMemoryVT().isVector())
3737 Known0 = Known0.trunc(LD->getMemoryVT().getScalarSizeInBits());
3738
3739 // Extend the Known bits from memory to the size of the result.
3740 if (ISD::isZEXTLoad(Op.getNode()))
3741 Known = Known0.zext(BitWidth);
3742 else if (ISD::isSEXTLoad(Op.getNode()))
3743 Known = Known0.sext(BitWidth);
3744 else if (ISD::isEXTLoad(Op.getNode()))
3745 Known = Known0.anyext(BitWidth);
3746 else
3747 Known = Known0;
3748 assert(Known.getBitWidth() == BitWidth);
3749 return Known;
3750 }
3751 break;
3752 }
3754 if (Op.getValueType().isScalableVector())
3755 break;
3756 EVT InVT = Op.getOperand(0).getValueType();
3757 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3758 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3759 Known = Known.zext(BitWidth);
3760 break;
3761 }
3762 case ISD::ZERO_EXTEND: {
3763 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3764 Known = Known.zext(BitWidth);
3765 break;
3766 }
3768 if (Op.getValueType().isScalableVector())
3769 break;
3770 EVT InVT = Op.getOperand(0).getValueType();
3771 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3772 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3773 // If the sign bit is known to be zero or one, then sext will extend
3774 // it to the top bits, else it will just zext.
3775 Known = Known.sext(BitWidth);
3776 break;
3777 }
3778 case ISD::SIGN_EXTEND: {
3779 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3780 // If the sign bit is known to be zero or one, then sext will extend
3781 // it to the top bits, else it will just zext.
3782 Known = Known.sext(BitWidth);
3783 break;
3784 }
3786 if (Op.getValueType().isScalableVector())
3787 break;
3788 EVT InVT = Op.getOperand(0).getValueType();
3789 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3790 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3791 Known = Known.anyext(BitWidth);
3792 break;
3793 }
3794 case ISD::ANY_EXTEND: {
3795 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3796 Known = Known.anyext(BitWidth);
3797 break;
3798 }
3799 case ISD::TRUNCATE: {
3800 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3801 Known = Known.trunc(BitWidth);
3802 break;
3803 }
3804 case ISD::AssertZext: {
3805 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3807 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3808 Known.Zero |= (~InMask);
3809 Known.One &= (~Known.Zero);
3810 break;
3811 }
3812 case ISD::AssertAlign: {
3813 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
3814 assert(LogOfAlign != 0);
3815
3816 // TODO: Should use maximum with source
3817 // If a node is guaranteed to be aligned, set low zero bits accordingly as
3818 // well as clearing one bits.
3819 Known.Zero.setLowBits(LogOfAlign);
3820 Known.One.clearLowBits(LogOfAlign);
3821 break;
3822 }
3823 case ISD::FGETSIGN:
3824 // All bits are zero except the low bit.
3825 Known.Zero.setBitsFrom(1);
3826 break;
3827 case ISD::ADD:
3828 case ISD::SUB: {
3829 SDNodeFlags Flags = Op.getNode()->getFlags();
3830 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3831 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3833 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
3834 Flags.hasNoUnsignedWrap(), Known, Known2);
3835 break;
3836 }
3837 case ISD::USUBO:
3838 case ISD::SSUBO:
3839 case ISD::USUBO_CARRY:
3840 case ISD::SSUBO_CARRY:
3841 if (Op.getResNo() == 1) {
3842 // If we know the result of a setcc has the top bits zero, use this info.
3843 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3845 BitWidth > 1)
3846 Known.Zero.setBitsFrom(1);
3847 break;
3848 }
3849 [[fallthrough]];
3850 case ISD::SUBC: {
3851 assert(Op.getResNo() == 0 &&
3852 "We only compute knownbits for the difference here.");
3853
3854 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
3855 KnownBits Borrow(1);
3856 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
3857 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3858 // Borrow has bit width 1
3859 Borrow = Borrow.trunc(1);
3860 } else {
3861 Borrow.setAllZero();
3862 }
3863
3864 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3865 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3866 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
3867 break;
3868 }
3869 case ISD::UADDO:
3870 case ISD::SADDO:
3871 case ISD::UADDO_CARRY:
3872 case ISD::SADDO_CARRY:
3873 if (Op.getResNo() == 1) {
3874 // If we know the result of a setcc has the top bits zero, use this info.
3875 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3877 BitWidth > 1)
3878 Known.Zero.setBitsFrom(1);
3879 break;
3880 }
3881 [[fallthrough]];
3882 case ISD::ADDC:
3883 case ISD::ADDE: {
3884 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
3885
3886 // With ADDE and UADDO_CARRY, a carry bit may be added in.
3887 KnownBits Carry(1);
3888 if (Opcode == ISD::ADDE)
3889 // Can't track carry from glue, set carry to unknown.
3890 Carry.resetAll();
3891 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
3892 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3893 // Carry has bit width 1
3894 Carry = Carry.trunc(1);
3895 } else {
3896 Carry.setAllZero();
3897 }
3898
3899 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3900 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3901 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
3902 break;
3903 }
3904 case ISD::UDIV: {
3905 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3906 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3907 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
3908 break;
3909 }
3910 case ISD::SDIV: {
3911 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3912 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3913 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
3914 break;
3915 }
3916 case ISD::SREM: {
3917 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3918 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3919 Known = KnownBits::srem(Known, Known2);
3920 break;
3921 }
3922 case ISD::UREM: {
3923 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3924 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3925 Known = KnownBits::urem(Known, Known2);
3926 break;
3927 }
3928 case ISD::EXTRACT_ELEMENT: {
3929 Known = computeKnownBits(Op.getOperand(0), Depth+1);
3930 const unsigned Index = Op.getConstantOperandVal(1);
3931 const unsigned EltBitWidth = Op.getValueSizeInBits();
3932
3933 // Remove low part of known bits mask
3934 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3935 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3936
3937 // Remove high part of known bit mask
3938 Known = Known.trunc(EltBitWidth);
3939 break;
3940 }
3942 SDValue InVec = Op.getOperand(0);
3943 SDValue EltNo = Op.getOperand(1);
3944 EVT VecVT = InVec.getValueType();
3945 // computeKnownBits not yet implemented for scalable vectors.
3946 if (VecVT.isScalableVector())
3947 break;
3948 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
3949 const unsigned NumSrcElts = VecVT.getVectorNumElements();
3950
3951 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
3952 // anything about the extended bits.
3953 if (BitWidth > EltBitWidth)
3954 Known = Known.trunc(EltBitWidth);
3955
3956 // If we know the element index, just demand that vector element, else for
3957 // an unknown element index, ignore DemandedElts and demand them all.
3958 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
3959 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
3960 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
3961 DemandedSrcElts =
3962 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
3963
3964 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
3965 if (BitWidth > EltBitWidth)
3966 Known = Known.anyext(BitWidth);
3967 break;
3968 }
3970 if (Op.getValueType().isScalableVector())
3971 break;
3972
3973 // If we know the element index, split the demand between the
3974 // source vector and the inserted element, otherwise assume we need
3975 // the original demanded vector elements and the value.
3976 SDValue InVec = Op.getOperand(0);
3977 SDValue InVal = Op.getOperand(1);
3978 SDValue EltNo = Op.getOperand(2);
3979 bool DemandedVal = true;
3980 APInt DemandedVecElts = DemandedElts;
3981 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
3982 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
3983 unsigned EltIdx = CEltNo->getZExtValue();
3984 DemandedVal = !!DemandedElts[EltIdx];
3985 DemandedVecElts.clearBit(EltIdx);
3986 }
3987 Known.One.setAllBits();
3988 Known.Zero.setAllBits();
3989 if (DemandedVal) {
3990 Known2 = computeKnownBits(InVal, Depth + 1);
3991 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
3992 }
3993 if (!!DemandedVecElts) {
3994 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
3995 Known = Known.intersectWith(Known2);
3996 }
3997 break;
3998 }
3999 case ISD::BITREVERSE: {
4000 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4001 Known = Known2.reverseBits();
4002 break;
4003 }
4004 case ISD::BSWAP: {
4005 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4006 Known = Known2.byteSwap();
4007 break;
4008 }
4009 case ISD::ABS: {
4010 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4011 Known = Known2.abs();
4012 break;
4013 }
4014 case ISD::USUBSAT: {
4015 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4016 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4017 Known = KnownBits::usub_sat(Known, Known2);
4018 break;
4019 }
4020 case ISD::UMIN: {
4021 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4022 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4023 Known = KnownBits::umin(Known, Known2);
4024 break;
4025 }
4026 case ISD::UMAX: {
4027 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4028 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4029 Known = KnownBits::umax(Known, Known2);
4030 break;
4031 }
4032 case ISD::SMIN:
4033 case ISD::SMAX: {
4034 // If we have a clamp pattern, we know that the number of sign bits will be
4035 // the minimum of the clamp min/max range.
4036 bool IsMax = (Opcode == ISD::SMAX);
4037 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4038 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4039 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4040 CstHigh =
4041 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4042 if (CstLow && CstHigh) {
4043 if (!IsMax)
4044 std::swap(CstLow, CstHigh);
4045
4046 const APInt &ValueLow = CstLow->getAPIntValue();
4047 const APInt &ValueHigh = CstHigh->getAPIntValue();
4048 if (ValueLow.sle(ValueHigh)) {
4049 unsigned LowSignBits = ValueLow.getNumSignBits();
4050 unsigned HighSignBits = ValueHigh.getNumSignBits();
4051 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4052 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4053 Known.One.setHighBits(MinSignBits);
4054 break;
4055 }
4056 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4057 Known.Zero.setHighBits(MinSignBits);
4058 break;
4059 }
4060 }
4061 }
4062
4063 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4064 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4065 if (IsMax)
4066 Known = KnownBits::smax(Known, Known2);
4067 else
4068 Known = KnownBits::smin(Known, Known2);
4069
4070 // For SMAX, if CstLow is non-negative we know the result will be
4071 // non-negative and thus all sign bits are 0.
4072 // TODO: There's an equivalent of this for smin with negative constant for
4073 // known ones.
4074 if (IsMax && CstLow) {
4075 const APInt &ValueLow = CstLow->getAPIntValue();
4076 if (ValueLow.isNonNegative()) {
4077 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4078 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4079 }
4080 }
4081
4082 break;
4083 }
4084 case ISD::UINT_TO_FP: {
4085 Known.makeNonNegative();
4086 break;
4087 }
4088 case ISD::SINT_TO_FP: {
4089 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4090 if (Known2.isNonNegative())
4091 Known.makeNonNegative();
4092 else if (Known2.isNegative())
4093 Known.makeNegative();
4094 break;
4095 }
4096 case ISD::FP_TO_UINT_SAT: {
4097 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4098 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4100 break;
4101 }
4103 if (Op.getResNo() == 1) {
4104 // The boolean result conforms to getBooleanContents.
4105 // If we know the result of a setcc has the top bits zero, use this info.
4106 // We know that we have an integer-based boolean since these operations
4107 // are only available for integer.
4108 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4110 BitWidth > 1)
4111 Known.Zero.setBitsFrom(1);
4112 break;
4113 }
4114 [[fallthrough]];
4116 case ISD::ATOMIC_SWAP:
4128 case ISD::ATOMIC_LOAD: {
4129 unsigned MemBits =
4130 cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
4131 // If we are looking at the loaded value.
4132 if (Op.getResNo() == 0) {
4134 Known.Zero.setBitsFrom(MemBits);
4135 else if (Op->getOpcode() == ISD::ATOMIC_LOAD &&
4136 cast<AtomicSDNode>(Op)->getExtensionType() == ISD::ZEXTLOAD)
4137 Known.Zero.setBitsFrom(MemBits);
4138 }
4139 break;
4140 }
4141 case ISD::FrameIndex:
4143 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4144 Known, getMachineFunction());
4145 break;
4146
4147 default:
4148 if (Opcode < ISD::BUILTIN_OP_END)
4149 break;
4150 [[fallthrough]];
4154 // TODO: Probably okay to remove after audit; here to reduce change size
4155 // in initial enablement patch for scalable vectors
4156 if (Op.getValueType().isScalableVector())
4157 break;
4158
4159 // Allow the target to implement this method for its nodes.
4160 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4161 break;
4162 }
4163
4164 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
4165 return Known;
4166}
4167
4168/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4170 switch (OR) {
4178 }
4179 llvm_unreachable("Unknown OverflowResult");
4180}
4181
4184 // X + 0 never overflow
4185 if (isNullConstant(N1))
4186 return OFK_Never;
4187
4188 // If both operands each have at least two sign bits, the addition
4189 // cannot overflow.
4190 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4191 return OFK_Never;
4192
4193 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4194 return OFK_Sometime;
4195}
4196
4199 // X + 0 never overflow
4200 if (isNullConstant(N1))
4201 return OFK_Never;
4202
4203 // mulhi + 1 never overflow
4204 KnownBits N1Known = computeKnownBits(N1);
4205 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4206 N1Known.getMaxValue().ult(2))
4207 return OFK_Never;
4208
4209 KnownBits N0Known = computeKnownBits(N0);
4210 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4211 N0Known.getMaxValue().ult(2))
4212 return OFK_Never;
4213
4214 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4215 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4216 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4217 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4218}
4219
4222 // X - 0 never overflow
4223 if (isNullConstant(N1))
4224 return OFK_Never;
4225
4226 // If both operands each have at least two sign bits, the subtraction
4227 // cannot overflow.
4228 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4229 return OFK_Never;
4230
4231 KnownBits N0Known = computeKnownBits(N0);
4232 KnownBits N1Known = computeKnownBits(N1);
4233 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4234 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4235 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4236}
4237
4240 // X - 0 never overflow
4241 if (isNullConstant(N1))
4242 return OFK_Never;
4243
4244 KnownBits N0Known = computeKnownBits(N0);
4245 KnownBits N1Known = computeKnownBits(N1);
4246 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4247 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4248 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4249}
4250
4253 // X * 0 and X * 1 never overflow.
4254 if (isNullConstant(N1) || isOneConstant(N1))
4255 return OFK_Never;
4256
4257 KnownBits N0Known = computeKnownBits(N0);
4258 KnownBits N1Known = computeKnownBits(N1);
4259 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4260 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4261 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4262}
4263
4266 // X * 0 and X * 1 never overflow.
4267 if (isNullConstant(N1) || isOneConstant(N1))
4268 return OFK_Never;
4269
4270 // Get the size of the result.
4271 unsigned BitWidth = N0.getScalarValueSizeInBits();
4272
4273 // Sum of the sign bits.
4274 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4275
4276 // If we have enough sign bits, then there's no overflow.
4277 if (SignBits > BitWidth + 1)
4278 return OFK_Never;
4279
4280 if (SignBits == BitWidth + 1) {
4281 // The overflow occurs when the true multiplication of the
4282 // the operands is the minimum negative number.
4283 KnownBits N0Known = computeKnownBits(N0);
4284 KnownBits N1Known = computeKnownBits(N1);
4285 // If one of the operands is non-negative, then there's no
4286 // overflow.
4287 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4288 return OFK_Never;
4289 }
4290
4291 return OFK_Sometime;
4292}
4293
4295 if (Depth >= MaxRecursionDepth)
4296 return false; // Limit search depth.
4297
4298 EVT OpVT = Val.getValueType();
4299 unsigned BitWidth = OpVT.getScalarSizeInBits();
4300
4301 // Is the constant a known power of 2?
4303 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4304 }))
4305 return true;
4306
4307 // A left-shift of a constant one will have exactly one bit set because
4308 // shifting the bit off the end is undefined.
4309 if (Val.getOpcode() == ISD::SHL) {
4310 auto *C = isConstOrConstSplat(Val.getOperand(0));
4311 if (C && C->getAPIntValue() == 1)
4312 return true;
4313 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4314 isKnownNeverZero(Val, Depth);
4315 }
4316
4317 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4318 // one bit set.
4319 if (Val.getOpcode() == ISD::SRL) {
4320 auto *C = isConstOrConstSplat(Val.getOperand(0));
4321 if (C && C->getAPIntValue().isSignMask())
4322 return true;
4323 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4324 isKnownNeverZero(Val, Depth);
4325 }
4326
4327 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4328 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4329
4330 // Are all operands of a build vector constant powers of two?
4331 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4332 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4333 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4334 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4335 return false;
4336 }))
4337 return true;
4338
4339 // Is the operand of a splat vector a constant power of two?
4340 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4341 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4342 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4343 return true;
4344
4345 // vscale(power-of-two) is a power-of-two for some targets
4346 if (Val.getOpcode() == ISD::VSCALE &&
4347 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4349 return true;
4350
4351 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4352 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4353 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4355
4356 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4357 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4359
4360 // Looking for `x & -x` pattern:
4361 // If x == 0:
4362 // x & -x -> 0
4363 // If x != 0:
4364 // x & -x -> non-zero pow2
4365 // so if we find the pattern return whether we know `x` is non-zero.
4366 SDValue X;
4367 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4368 return isKnownNeverZero(X, Depth);
4369
4370 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4371 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4372
4373 // More could be done here, though the above checks are enough
4374 // to handle some common cases.
4375 return false;
4376}
4377
4379 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4380 return C1->getValueAPF().getExactLog2Abs() >= 0;
4381
4382 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4383 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4384
4385 return false;
4386}
4387
4389 EVT VT = Op.getValueType();
4390
4391 // Since the number of lanes in a scalable vector is unknown at compile time,
4392 // we track one bit which is implicitly broadcast to all lanes. This means
4393 // that all lanes in a scalable vector are considered demanded.
4394 APInt DemandedElts = VT.isFixedLengthVector()
4396 : APInt(1, 1);
4397 return ComputeNumSignBits(Op, DemandedElts, Depth);
4398}
4399
4400unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4401 unsigned Depth) const {
4402 EVT VT = Op.getValueType();
4403 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4404 unsigned VTBits = VT.getScalarSizeInBits();
4405 unsigned NumElts = DemandedElts.getBitWidth();
4406 unsigned Tmp, Tmp2;
4407 unsigned FirstAnswer = 1;
4408
4409 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4410 const APInt &Val = C->getAPIntValue();
4411 return Val.getNumSignBits();
4412 }
4413
4414 if (Depth >= MaxRecursionDepth)
4415 return 1; // Limit search depth.
4416
4417 if (!DemandedElts)
4418 return 1; // No demanded elts, better to assume we don't know anything.
4419
4420 unsigned Opcode = Op.getOpcode();
4421 switch (Opcode) {
4422 default: break;
4423 case ISD::AssertSext:
4424 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4425 return VTBits-Tmp+1;
4426 case ISD::AssertZext:
4427 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4428 return VTBits-Tmp;
4429 case ISD::MERGE_VALUES:
4430 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4431 Depth + 1);
4432 case ISD::SPLAT_VECTOR: {
4433 // Check if the sign bits of source go down as far as the truncated value.
4434 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4435 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4436 if (NumSrcSignBits > (NumSrcBits - VTBits))
4437 return NumSrcSignBits - (NumSrcBits - VTBits);
4438 break;
4439 }
4440 case ISD::BUILD_VECTOR:
4441 assert(!VT.isScalableVector());
4442 Tmp = VTBits;
4443 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4444 if (!DemandedElts[i])
4445 continue;
4446
4447 SDValue SrcOp = Op.getOperand(i);
4448 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4449 // for constant nodes to ensure we only look at the sign bits.
4450 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SrcOp)) {
4451 APInt T = C->getAPIntValue().trunc(VTBits);
4452 Tmp2 = T.getNumSignBits();
4453 } else {
4454 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4455
4456 if (SrcOp.getValueSizeInBits() != VTBits) {
4457 assert(SrcOp.getValueSizeInBits() > VTBits &&
4458 "Expected BUILD_VECTOR implicit truncation");
4459 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4460 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4461 }
4462 }
4463 Tmp = std::min(Tmp, Tmp2);
4464 }
4465 return Tmp;
4466
4467 case ISD::VECTOR_SHUFFLE: {
4468 // Collect the minimum number of sign bits that are shared by every vector
4469 // element referenced by the shuffle.
4470 APInt DemandedLHS, DemandedRHS;
4471 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
4472 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4473 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4474 DemandedLHS, DemandedRHS))
4475 return 1;
4476
4477 Tmp = std::numeric_limits<unsigned>::max();
4478 if (!!DemandedLHS)
4479 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4480 if (!!DemandedRHS) {
4481 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4482 Tmp = std::min(Tmp, Tmp2);
4483 }
4484 // If we don't know anything, early out and try computeKnownBits fall-back.
4485 if (Tmp == 1)
4486 break;
4487 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4488 return Tmp;
4489 }
4490
4491 case ISD::BITCAST: {
4492 if (VT.isScalableVector())
4493 break;
4494 SDValue N0 = Op.getOperand(0);
4495 EVT SrcVT = N0.getValueType();
4496 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4497
4498 // Ignore bitcasts from unsupported types..
4499 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4500 break;
4501
4502 // Fast handling of 'identity' bitcasts.
4503 if (VTBits == SrcBits)
4504 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4505
4506 bool IsLE = getDataLayout().isLittleEndian();
4507
4508 // Bitcast 'large element' scalar/vector to 'small element' vector.
4509 if ((SrcBits % VTBits) == 0) {
4510 assert(VT.isVector() && "Expected bitcast to vector");
4511
4512 unsigned Scale = SrcBits / VTBits;
4513 APInt SrcDemandedElts =
4514 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4515
4516 // Fast case - sign splat can be simply split across the small elements.
4517 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4518 if (Tmp == SrcBits)
4519 return VTBits;
4520
4521 // Slow case - determine how far the sign extends into each sub-element.
4522 Tmp2 = VTBits;
4523 for (unsigned i = 0; i != NumElts; ++i)
4524 if (DemandedElts[i]) {
4525 unsigned SubOffset = i % Scale;
4526 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4527 SubOffset = SubOffset * VTBits;
4528 if (Tmp <= SubOffset)
4529 return 1;
4530 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4531 }
4532 return Tmp2;
4533 }
4534 break;
4535 }
4536
4538 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4539 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4540 return VTBits - Tmp + 1;
4541 case ISD::SIGN_EXTEND:
4542 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4543 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4545 // Max of the input and what this extends.
4546 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4547 Tmp = VTBits-Tmp+1;
4548 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4549 return std::max(Tmp, Tmp2);
4551 if (VT.isScalableVector())
4552 break;
4553 SDValue Src = Op.getOperand(0);
4554 EVT SrcVT = Src.getValueType();
4555 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4556 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4557 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4558 }
4559 case ISD::SRA:
4560 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4561 // SRA X, C -> adds C sign bits.
4562 if (const APInt *ShAmt =
4564 Tmp = std::min<uint64_t>(Tmp + ShAmt->getZExtValue(), VTBits);
4565 return Tmp;
4566 case ISD::SHL:
4567 if (const APInt *ShAmt =
4568 getValidMaximumShiftAmountConstant(Op, DemandedElts)) {
4569 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4570 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4571 if (ShAmt->ult(Tmp))
4572 return Tmp - ShAmt->getZExtValue();
4573 }
4574 break;
4575 case ISD::AND:
4576 case ISD::OR:
4577 case ISD::XOR: // NOT is handled here.
4578 // Logical binary ops preserve the number of sign bits at the worst.
4579 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4580 if (Tmp != 1) {
4581 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4582 FirstAnswer = std::min(Tmp, Tmp2);
4583 // We computed what we know about the sign bits as our first
4584 // answer. Now proceed to the generic code that uses
4585 // computeKnownBits, and pick whichever answer is better.
4586 }
4587 break;
4588
4589 case ISD::SELECT:
4590 case ISD::VSELECT:
4591 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4592 if (Tmp == 1) return 1; // Early out.
4593 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4594 return std::min(Tmp, Tmp2);
4595 case ISD::SELECT_CC:
4596 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4597 if (Tmp == 1) return 1; // Early out.
4598 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4599 return std::min(Tmp, Tmp2);
4600
4601 case ISD::SMIN:
4602 case ISD::SMAX: {
4603 // If we have a clamp pattern, we know that the number of sign bits will be
4604 // the minimum of the clamp min/max range.
4605 bool IsMax = (Opcode == ISD::SMAX);
4606 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4607 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4608 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4609 CstHigh =
4610 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4611 if (CstLow && CstHigh) {
4612 if (!IsMax)
4613 std::swap(CstLow, CstHigh);
4614 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4615 Tmp = CstLow->getAPIntValue().getNumSignBits();
4616 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4617 return std::min(Tmp, Tmp2);
4618 }
4619 }
4620
4621 // Fallback - just get the minimum number of sign bits of the operands.
4622 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4623 if (Tmp == 1)
4624 return 1; // Early out.
4625 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4626 return std::min(Tmp, Tmp2);
4627 }
4628 case ISD::UMIN:
4629 case ISD::UMAX:
4630 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4631 if (Tmp == 1)
4632 return 1; // Early out.
4633 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4634 return std::min(Tmp, Tmp2);
4635 case ISD::SADDO:
4636 case ISD::UADDO:
4637 case ISD::SADDO_CARRY:
4638 case ISD::UADDO_CARRY:
4639 case ISD::SSUBO:
4640 case ISD::USUBO:
4641 case ISD::SSUBO_CARRY:
4642 case ISD::USUBO_CARRY:
4643 case ISD::SMULO:
4644 case ISD::UMULO:
4645 if (Op.getResNo() != 1)
4646 break;
4647 // The boolean result conforms to getBooleanContents. Fall through.
4648 // If setcc returns 0/-1, all bits are sign bits.
4649 // We know that we have an integer-based boolean since these operations
4650 // are only available for integer.
4651 if (TLI->getBooleanContents(VT.isVector(), false) ==
4653 return VTBits;
4654 break;
4655 case ISD::SETCC:
4656 case ISD::SETCCCARRY:
4657 case ISD::STRICT_FSETCC:
4658 case ISD::STRICT_FSETCCS: {
4659 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
4660 // If setcc returns 0/-1, all bits are sign bits.
4661 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
4663 return VTBits;
4664 break;
4665 }
4666 case ISD::ROTL:
4667 case ISD::ROTR:
4668 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4669
4670 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
4671 if (Tmp == VTBits)
4672 return VTBits;
4673
4674 if (ConstantSDNode *C =
4675 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
4676 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
4677
4678 // Handle rotate right by N like a rotate left by 32-N.
4679 if (Opcode == ISD::ROTR)
4680 RotAmt = (VTBits - RotAmt) % VTBits;
4681
4682 // If we aren't rotating out all of the known-in sign bits, return the
4683 // number that are left. This handles rotl(sext(x), 1) for example.
4684 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
4685 }
4686 break;
4687 case ISD::ADD:
4688 case ISD::ADDC:
4689 // Add can have at most one carry bit. Thus we know that the output
4690 // is, at worst, one more bit than the inputs.
4691 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4692 if (Tmp == 1) return 1; // Early out.
4693
4694 // Special case decrementing a value (ADD X, -1):
4695 if (ConstantSDNode *CRHS =
4696 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
4697 if (CRHS->isAllOnes()) {
4698 KnownBits Known =
4699 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4700
4701 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4702 // sign bits set.
4703 if ((Known.Zero | 1).isAllOnes())
4704 return VTBits;
4705
4706 // If we are subtracting one from a positive number, there is no carry
4707 // out of the result.
4708 if (Known.isNonNegative())
4709 return Tmp;
4710 }
4711
4712 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4713 if (Tmp2 == 1) return 1; // Early out.
4714 return std::min(Tmp, Tmp2) - 1;
4715 case ISD::SUB:
4716 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4717 if (Tmp2 == 1) return 1; // Early out.
4718
4719 // Handle NEG.
4720 if (ConstantSDNode *CLHS =
4721 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
4722 if (CLHS->isZero()) {
4723 KnownBits Known =
4724 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4725 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4726 // sign bits set.
4727 if ((Known.Zero | 1).isAllOnes())
4728 return VTBits;
4729
4730 // If the input is known to be positive (the sign bit is known clear),
4731 // the output of the NEG has the same number of sign bits as the input.
4732 if (Known.isNonNegative())
4733 return Tmp2;
4734
4735 // Otherwise, we treat this like a SUB.
4736 }
4737
4738 // Sub can have at most one carry bit. Thus we know that the output
4739 // is, at worst, one more bit than the inputs.
4740 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4741 if (Tmp == 1) return 1; // Early out.
4742 return std::min(Tmp, Tmp2) - 1;
4743 case ISD::MUL: {
4744 // The output of the Mul can be at most twice the valid bits in the inputs.
4745 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4746 if (SignBitsOp0 == 1)
4747 break;
4748 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
4749 if (SignBitsOp1 == 1)
4750 break;
4751 unsigned OutValidBits =
4752 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
4753 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
4754 }
4755 case ISD::SREM:
4756 // The sign bit is the LHS's sign bit, except when the result of the
4757 // remainder is zero. The magnitude of the result should be less than or
4758 // equal to the magnitude of the LHS. Therefore, the result should have
4759 // at least as many sign bits as the left hand side.
4760 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4761 case ISD::TRUNCATE: {
4762 // Check if the sign bits of source go down as far as the truncated value.
4763 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
4764 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4765 if (NumSrcSignBits > (NumSrcBits - VTBits))
4766 return NumSrcSignBits - (NumSrcBits - VTBits);
4767 break;
4768 }
4769 case ISD::EXTRACT_ELEMENT: {
4770 if (VT.isScalableVector())
4771 break;
4772 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
4773 const int BitWidth = Op.getValueSizeInBits();
4774 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
4775
4776 // Get reverse index (starting from 1), Op1 value indexes elements from
4777 // little end. Sign starts at big end.
4778 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
4779
4780 // If the sign portion ends in our element the subtraction gives correct
4781 // result. Otherwise it gives either negative or > bitwidth result
4782 return std::clamp(KnownSign - rIndex * BitWidth, 0, BitWidth);
4783 }
4785 if (VT.isScalableVector())
4786 break;
4787 // If we know the element index, split the demand between the
4788 // source vector and the inserted element, otherwise assume we need
4789 // the original demanded vector elements and the value.
4790 SDValue InVec = Op.getOperand(0);
4791 SDValue InVal = Op.getOperand(1);
4792 SDValue EltNo = Op.getOperand(2);
4793 bool DemandedVal = true;
4794 APInt DemandedVecElts = DemandedElts;
4795 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4796 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4797 unsigned EltIdx = CEltNo->getZExtValue();
4798 DemandedVal = !!DemandedElts[EltIdx];
4799 DemandedVecElts.clearBit(EltIdx);
4800 }
4801 Tmp = std::numeric_limits<unsigned>::max();
4802 if (DemandedVal) {
4803 // TODO - handle implicit truncation of inserted elements.
4804 if (InVal.getScalarValueSizeInBits() != VTBits)
4805 break;
4806 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
4807 Tmp = std::min(Tmp, Tmp2);
4808 }
4809 if (!!DemandedVecElts) {
4810 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
4811 Tmp = std::min(Tmp, Tmp2);
4812 }
4813 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4814 return Tmp;
4815 }
4817 assert(!VT.isScalableVector());
4818 SDValue InVec = Op.getOperand(0);
4819 SDValue EltNo = Op.getOperand(1);
4820 EVT VecVT = InVec.getValueType();
4821 // ComputeNumSignBits not yet implemented for scalable vectors.
4822 if (VecVT.isScalableVector())
4823 break;
4824 const unsigned BitWidth = Op.getValueSizeInBits();
4825 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
4826 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4827
4828 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
4829 // anything about sign bits. But if the sizes match we can derive knowledge
4830 // about sign bits from the vector operand.
4831 if (BitWidth != EltBitWidth)
4832 break;
4833
4834 // If we know the element index, just demand that vector element, else for
4835 // an unknown element index, ignore DemandedElts and demand them all.
4836 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4837 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4838 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4839 DemandedSrcElts =
4840 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4841
4842 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
4843 }
4845 // Offset the demanded elts by the subvector index.
4846 SDValue Src = Op.getOperand(0);
4847 // Bail until we can represent demanded elements for scalable vectors.
4848 if (Src.getValueType().isScalableVector())
4849 break;
4850 uint64_t Idx = Op.getConstantOperandVal(1);
4851 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
4852 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
4853 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4854 }
4855 case ISD::CONCAT_VECTORS: {
4856 if (VT.isScalableVector())
4857 break;
4858 // Determine the minimum number of sign bits across all demanded
4859 // elts of the input vectors. Early out if the result is already 1.
4860 Tmp = std::numeric_limits<unsigned>::max();
4861 EVT SubVectorVT = Op.getOperand(0).getValueType();
4862 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
4863 unsigned NumSubVectors = Op.getNumOperands();
4864 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
4865 APInt DemandedSub =
4866 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
4867 if (!DemandedSub)
4868 continue;
4869 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
4870 Tmp = std::min(Tmp, Tmp2);
4871 }
4872 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4873 return Tmp;
4874 }
4875 case ISD::INSERT_SUBVECTOR: {
4876 if (VT.isScalableVector())
4877 break;
4878 // Demand any elements from the subvector and the remainder from the src its
4879 // inserted into.
4880 SDValue Src = Op.getOperand(0);
4881 SDValue Sub = Op.getOperand(1);
4882 uint64_t Idx = Op.getConstantOperandVal(2);
4883 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
4884 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
4885 APInt DemandedSrcElts = DemandedElts;
4886 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
4887
4888 Tmp = std::numeric_limits<unsigned>::max();
4889 if (!!DemandedSubElts) {
4890 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
4891 if (Tmp == 1)
4892 return 1; // early-out
4893 }
4894 if (!!DemandedSrcElts) {
4895 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4896 Tmp = std::min(Tmp, Tmp2);
4897 }
4898 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4899 return Tmp;
4900 }
4901 case ISD::LOAD: {
4902 LoadSDNode *LD = cast<LoadSDNode>(Op);
4903 if (const MDNode *Ranges = LD->getRanges()) {
4904 if (DemandedElts != 1)
4905 break;
4906
4908 if (VTBits > CR.getBitWidth()) {
4909 switch (LD->getExtensionType()) {
4910 case ISD::SEXTLOAD:
4911 CR = CR.signExtend(VTBits);
4912 break;
4913 case ISD::ZEXTLOAD:
4914 CR = CR.zeroExtend(VTBits);
4915 break;
4916 default:
4917 break;
4918 }
4919 }
4920
4921 if (VTBits != CR.getBitWidth())
4922 break;
4923 return std::min(CR.getSignedMin().getNumSignBits(),
4925 }
4926
4927 break;
4928 }
4931 case ISD::ATOMIC_SWAP:
4943 case ISD::ATOMIC_LOAD: {
4944 Tmp = cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
4945 // If we are looking at the loaded value.
4946 if (Op.getResNo() == 0) {
4947 if (Tmp == VTBits)
4948 return 1; // early-out
4950 return VTBits - Tmp + 1;
4952 return VTBits - Tmp;
4953 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
4954 ISD::LoadExtType ETy = cast<AtomicSDNode>(Op)->getExtensionType();
4955 if (ETy == ISD::SEXTLOAD)
4956 return VTBits - Tmp + 1;
4957 if (ETy == ISD::ZEXTLOAD)
4958 return VTBits - Tmp;
4959 }
4960 }
4961 break;
4962 }
4963 }
4964
4965 // If we are looking at the loaded value of the SDNode.
4966 if (Op.getResNo() == 0) {
4967 // Handle LOADX separately here. EXTLOAD case will fallthrough.
4968 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
4969 unsigned ExtType = LD->getExtensionType();
4970 switch (ExtType) {
4971 default: break;
4972 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
4973 Tmp = LD->getMemoryVT().getScalarSizeInBits();
4974 return VTBits - Tmp + 1;
4975 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
4976 Tmp = LD->getMemoryVT().getScalarSizeInBits();
4977 return VTBits - Tmp;
4978 case ISD::NON_EXTLOAD:
4979 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
4980 // We only need to handle vectors - computeKnownBits should handle
4981 // scalar cases.
4982 Type *CstTy = Cst->getType();
4983 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
4984 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
4985 VTBits == CstTy->getScalarSizeInBits()) {
4986 Tmp = VTBits;
4987 for (unsigned i = 0; i != NumElts; ++i) {
4988 if (!DemandedElts[i])
4989 continue;
4990 if (Constant *Elt = Cst->getAggregateElement(i)) {
4991 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
4992 const APInt &Value = CInt->getValue();
4993 Tmp = std::min(Tmp, Value.getNumSignBits());
4994 continue;
4995 }
4996 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
4997 APInt Value = CFP->getValueAPF().bitcastToAPInt();
4998 Tmp = std::min(Tmp, Value.getNumSignBits());
4999 continue;
5000 }
5001 }
5002 // Unknown type. Conservatively assume no bits match sign bit.
5003 return 1;
5004 }
5005 return Tmp;
5006 }
5007 }
5008 break;
5009 }
5010 }
5011 }
5012
5013 // Allow the target to implement this method for its nodes.
5014 if (Opcode >= ISD::BUILTIN_OP_END ||
5015 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5016 Opcode == ISD::INTRINSIC_W_CHAIN ||
5017 Opcode == ISD::INTRINSIC_VOID) {
5018 // TODO: This can probably be removed once target code is audited. This
5019 // is here purely to reduce patch size and review complexity.
5020 if (!VT.isScalableVector()) {
5021 unsigned NumBits =
5022 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5023 if (NumBits > 1)
5024 FirstAnswer = std::max(FirstAnswer, NumBits);
5025 }
5026 }
5027
5028 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5029 // use this information.
5030 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5031 return std::max(FirstAnswer, Known.countMinSignBits());
5032}
5033
5035 unsigned Depth) const {
5036 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5037 return Op.getScalarValueSizeInBits() - SignBits + 1;
5038}
5039
5041 const APInt &DemandedElts,
5042 unsigned Depth) const {
5043 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5044 return Op.getScalarValueSizeInBits() - SignBits + 1;
5045}
5046
5048 unsigned Depth) const {
5049 // Early out for FREEZE.
5050 if (Op.getOpcode() == ISD::FREEZE)
5051 return true;
5052
5053 // TODO: Assume we don't know anything for now.
5054 EVT VT = Op.getValueType();
5055 if (VT.isScalableVector())
5056 return false;
5057
5058 APInt DemandedElts = VT.isVector()
5060 : APInt(1, 1);
5061 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5062}
5063
5065 const APInt &DemandedElts,
5066 bool PoisonOnly,
5067 unsigned Depth) const {
5068 unsigned Opcode = Op.getOpcode();
5069
5070 // Early out for FREEZE.
5071 if (Opcode == ISD::FREEZE)
5072 return true;
5073
5074 if (Depth >= MaxRecursionDepth)
5075 return false; // Limit search depth.
5076
5077 if (isIntOrFPConstant(Op))
5078 return true;
5079
5080 switch (Opcode) {
5081 case ISD::CONDCODE:
5082 case ISD::VALUETYPE:
5083 case ISD::FrameIndex:
5085 case ISD::CopyFromReg:
5086 return true;
5087
5088 case ISD::UNDEF:
5089 return PoisonOnly;
5090
5091 case ISD::BUILD_VECTOR:
5092 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5093 // this shouldn't affect the result.
5094 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5095 if (!DemandedElts[i])
5096 continue;
5098 Depth + 1))
5099 return false;
5100 }
5101 return true;
5102
5103 case ISD::VECTOR_SHUFFLE: {
5104 APInt DemandedLHS, DemandedRHS;
5105 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5106 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5107 DemandedElts, DemandedLHS, DemandedRHS,
5108 /*AllowUndefElts=*/false))
5109 return false;
5110 if (!DemandedLHS.isZero() &&
5111 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5112 PoisonOnly, Depth + 1))
5113 return false;
5114 if (!DemandedRHS.isZero() &&
5115 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5116 PoisonOnly, Depth + 1))
5117 return false;
5118 return true;
5119 }
5120
5121 // TODO: Search for noundef attributes from library functions.
5122
5123 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5124
5125 default:
5126 // Allow the target to implement this method for its nodes.
5127 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5128 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5130 Op, DemandedElts, *this, PoisonOnly, Depth);
5131 break;
5132 }
5133
5134 // If Op can't create undef/poison and none of its operands are undef/poison
5135 // then Op is never undef/poison.
5136 // NOTE: TargetNodes can handle this in themselves in
5137 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5138 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5139 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5140 Depth) &&
5141 all_of(Op->ops(), [&](SDValue V) {
5142 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5143 });
5144}
5145
5147 bool ConsiderFlags,
5148 unsigned Depth) const {
5149 // TODO: Assume we don't know anything for now.
5150 EVT VT = Op.getValueType();
5151 if (VT.isScalableVector())
5152 return true;
5153
5154 APInt DemandedElts = VT.isVector()
5156 : APInt(1, 1);
5157 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5158 Depth);
5159}
5160
5162 bool PoisonOnly, bool ConsiderFlags,
5163 unsigned Depth) const {
5164 // TODO: Assume we don't know anything for now.
5165 EVT VT = Op.getValueType();
5166 if (VT.isScalableVector())
5167 return true;
5168
5169 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5170 return true;
5171
5172 unsigned Opcode = Op.getOpcode();
5173 switch (Opcode) {
5174 case ISD::FREEZE:
5177 case ISD::SADDSAT:
5178 case ISD::UADDSAT:
5179 case ISD::SSUBSAT:
5180 case ISD::USUBSAT:
5181 case ISD::MULHU:
5182 case ISD::MULHS:
5183 case ISD::SMIN:
5184 case ISD::SMAX:
5185 case ISD::UMIN:
5186 case ISD::UMAX:
5187 case ISD::AND:
5188 case ISD::XOR:
5189 case ISD::ROTL:
5190 case ISD::ROTR:
5191 case ISD::FSHL:
5192 case ISD::FSHR:
5193 case ISD::BSWAP:
5194 case ISD::CTPOP:
5195 case ISD::BITREVERSE:
5196 case ISD::PARITY:
5197 case ISD::SIGN_EXTEND:
5198 case ISD::TRUNCATE:
5202 case ISD::BITCAST:
5203 case ISD::BUILD_VECTOR:
5204 case ISD::BUILD_PAIR:
5205 return false;
5206
5207 case ISD::SELECT_CC:
5208 case ISD::SETCC: {
5209 // Integer setcc cannot create undef or poison.
5210 if (Op.getOperand(0).getValueType().isInteger())
5211 return false;
5212
5213 // FP compares are more complicated. They can create poison for nan/infinity
5214 // based on options and flags. The options and flags also cause special
5215 // nonan condition codes to be used. Those condition codes may be preserved
5216 // even if the nonan flag is dropped somewhere.
5217 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5218 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5219 if (((unsigned)CCCode & 0x10U))
5220 return true;
5221
5223 return Options.NoNaNsFPMath || Options.NoInfsFPMath;
5224 }
5225
5226 case ISD::OR:
5227 case ISD::ZERO_EXTEND:
5228 case ISD::ADD:
5229 case ISD::SUB:
5230 case ISD::MUL:
5231 // No poison except from flags (which is handled above)
5232 return false;
5233
5234 case ISD::SHL:
5235 case ISD::SRL:
5236 case ISD::SRA:
5237 // If the max shift amount isn't in range, then the shift can create poison.
5238 return !getValidMaximumShiftAmountConstant(Op, DemandedElts);
5239
5241 // Check if we demand any upper (undef) elements.
5242 return !PoisonOnly && DemandedElts.ugt(1);
5243
5245 // Ensure that the element index is in bounds.
5246 EVT VecVT = Op.getOperand(0).getValueType();
5247 KnownBits KnownIdx = computeKnownBits(Op.getOperand(1), Depth + 1);
5248 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5249 }
5250
5252 // Ensure that the element index is in bounds.
5253 EVT VecVT = Op.getOperand(0).getValueType();
5254 KnownBits KnownIdx = computeKnownBits(Op.getOperand(2), Depth + 1);
5255 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5256 }
5257
5258 case ISD::VECTOR_SHUFFLE: {
5259 // Check for any demanded shuffle element that is undef.
5260 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5261 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5262 if (Elt < 0 && DemandedElts[Idx])
5263 return true;
5264 return false;
5265 }
5266
5267 default:
5268 // Allow the target to implement this method for its nodes.
5269 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5270 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5272 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5273 break;
5274 }
5275
5276 // Be conservative and return true.
5277 return true;
5278}
5279
5280bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5281 unsigned Opcode = Op.getOpcode();
5282 if (Opcode == ISD::OR)
5283 return Op->getFlags().hasDisjoint() ||
5284 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5285 if (Opcode == ISD::XOR)
5286 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5287 return false;
5288}
5289
5291 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5292 (Op.getOpcode() == ISD::ADD || isADDLike(Op));
5293}
5294
5295bool SelectionDAG::isKnownNeverNaN(SDValue Op, bool SNaN, unsigned Depth) const {
5296 // If we're told that NaNs won't happen, assume they won't.
5297 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5298 return true;
5299
5300 if (Depth >= MaxRecursionDepth)
5301 return false; // Limit search depth.
5302
5303 // If the value is a constant, we can obviously see if it is a NaN or not.
5304 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
5305 return !C->getValueAPF().isNaN() ||
5306 (SNaN && !C->getValueAPF().isSignaling());
5307 }
5308
5309 unsigned Opcode = Op.getOpcode();
5310 switch (Opcode) {
5311 case ISD::FADD:
5312 case ISD::FSUB:
5313 case ISD::FMUL:
5314 case ISD::FDIV:
5315 case ISD::FREM:
5316 case ISD::FSIN:
5317 case ISD::FCOS:
5318 case ISD::FMA:
5319 case ISD::FMAD: {
5320 if (SNaN)
5321 return true;
5322 // TODO: Need isKnownNeverInfinity
5323 return false;
5324 }
5325 case ISD::FCANONICALIZE:
5326 case ISD::FEXP:
5327 case ISD::FEXP2:
5328 case ISD::FEXP10:
5329 case ISD::FTRUNC:
5330 case ISD::FFLOOR:
5331 case ISD::FCEIL:
5332 case ISD::FROUND:
5333 case ISD::FROUNDEVEN:
5334 case ISD::FRINT:
5335 case ISD::LRINT:
5336 case ISD::LLRINT:
5337 case ISD::FNEARBYINT:
5338 case ISD::FLDEXP: {
5339 if (SNaN)
5340 return true;
5341 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5342 }
5343 case ISD::FABS:
5344 case ISD::FNEG:
5345 case ISD::FCOPYSIGN: {
5346 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5347 }
5348 case ISD::SELECT:
5349 return isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
5350 isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
5351 case ISD::FP_EXTEND:
5352 case ISD::FP_ROUND: {
5353 if (SNaN)
5354 return true;
5355 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5356 }
5357 case ISD::SINT_TO_FP:
5358 case ISD::UINT_TO_FP:
5359 return true;
5360 case ISD::FSQRT: // Need is known positive
5361 case ISD::FLOG:
5362 case ISD::FLOG2:
5363 case ISD::FLOG10:
5364 case ISD::FPOWI:
5365 case ISD::FPOW: {
5366 if (SNaN)
5367 return true;
5368 // TODO: Refine on operand
5369 return false;
5370 }
5371 case ISD::FMINNUM:
5372 case ISD::FMAXNUM: {
5373 // Only one needs to be known not-nan, since it will be returned if the
5374 // other ends up being one.
5375 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) ||
5376 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5377 }
5378 case ISD::FMINNUM_IEEE:
5379 case ISD::FMAXNUM_IEEE: {
5380 if (SNaN)
5381 return true;
5382 // This can return a NaN if either operand is an sNaN, or if both operands
5383 // are NaN.
5384 return (isKnownNeverNaN(Op.getOperand(0), false, Depth + 1) &&
5385 isKnownNeverSNaN(Op.getOperand(1), Depth + 1)) ||
5386 (isKnownNeverNaN(Op.getOperand(1), false, Depth + 1) &&
5387 isKnownNeverSNaN(Op.getOperand(0), Depth + 1));
5388 }
5389 case ISD::FMINIMUM:
5390 case ISD::FMAXIMUM: {
5391 // TODO: Does this quiet or return the origina NaN as-is?
5392 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
5393 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5394 }
5396 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5397 }
5398 case ISD::BUILD_VECTOR: {
5399 for (const SDValue &Opnd : Op->ops())
5400 if (!isKnownNeverNaN(Opnd, SNaN, Depth + 1))
5401 return false;
5402 return true;
5403 }
5404 default:
5405 if (Opcode >= ISD::BUILTIN_OP_END ||
5406 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5407 Opcode == ISD::INTRINSIC_W_CHAIN ||
5408 Opcode == ISD::INTRINSIC_VOID) {
5409 return TLI->isKnownNeverNaNForTargetNode(Op, *this, SNaN, Depth);
5410 }
5411
5412 return false;
5413 }
5414}
5415
5417 assert(Op.getValueType().isFloatingPoint() &&
5418 "Floating point type expected");
5419
5420 // If the value is a constant, we can obviously see if it is a zero or not.
5422 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
5423}
5424
5426 if (Depth >= MaxRecursionDepth)
5427 return false; // Limit search depth.
5428
5429 assert(!Op.getValueType().isFloatingPoint() &&
5430 "Floating point types unsupported - use isKnownNeverZeroFloat");
5431
5432 // If the value is a constant, we can obviously see if it is a zero or not.
5434 [](ConstantSDNode *C) { return !C->isZero(); }))
5435 return true;
5436
5437 // TODO: Recognize more cases here. Most of the cases are also incomplete to
5438 // some degree.
5439 switch (Op.getOpcode()) {
5440 default:
5441 break;
5442
5443 case ISD::OR:
5444 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5445 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5446
5447 case ISD::VSELECT:
5448 case ISD::SELECT:
5449 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5450 isKnownNeverZero(Op.getOperand(2), Depth + 1);
5451
5452 case ISD::SHL: {
5453 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5454 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5455 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5456 // 1 << X is never zero.
5457 if (ValKnown.One[0])
5458 return true;
5459 // If max shift cnt of known ones is non-zero, result is non-zero.
5460 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5461 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5462 !ValKnown.One.shl(MaxCnt).isZero())
5463 return true;
5464 break;
5465 }
5466 case ISD::UADDSAT:
5467 case ISD::UMAX:
5468 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5469 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5470
5471 // For smin/smax: If either operand is known negative/positive
5472 // respectively we don't need the other to be known at all.
5473 case ISD::SMAX: {
5474 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5475 if (Op1.isStrictlyPositive())
5476 return true;
5477
5478 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5479 if (Op0.isStrictlyPositive())
5480 return true;
5481
5482 if (Op1.isNonZero() && Op0.isNonZero())
5483 return true;
5484
5485 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5486 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5487 }
5488 case ISD::SMIN: {
5489 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5490 if (Op1.isNegative())
5491 return true;
5492
5493 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5494 if (Op0.isNegative())
5495 return true;
5496
5497 if (Op1.isNonZero() && Op0.isNonZero())
5498 return true;
5499
5500 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5501 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5502 }
5503 case ISD::UMIN:
5504 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5505 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5506
5507 case ISD::ROTL:
5508 case ISD::ROTR:
5509 case ISD::BITREVERSE:
5510 case ISD::BSWAP:
5511 case ISD::CTPOP:
5512 case ISD::ABS:
5513 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5514
5515 case ISD::SRA:
5516 case ISD::SRL: {
5517 if (Op->getFlags().hasExact())
5518 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5519 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5520 if (ValKnown.isNegative())
5521 return true;
5522 // If max shift cnt of known ones is non-zero, result is non-zero.
5523 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5524 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5525 !ValKnown.One.lshr(MaxCnt).isZero())
5526 return true;
5527 break;
5528 }
5529 case ISD::UDIV:
5530 case ISD::SDIV:
5531 // div exact can only produce a zero if the dividend is zero.
5532 // TODO: For udiv this is also true if Op1 u<= Op0
5533 if (Op->getFlags().hasExact())
5534 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5535 break;
5536
5537 case ISD::ADD:
5538 if (Op->getFlags().hasNoUnsignedWrap())
5539 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5540 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5541 return true;
5542 // TODO: There are a lot more cases we can prove for add.
5543 break;
5544
5545 case ISD::SUB: {
5546 if (isNullConstant(Op.getOperand(0)))
5547 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
5548
5549 std::optional<bool> ne =
5550 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
5551 computeKnownBits(Op.getOperand(1), Depth + 1));
5552 return ne && *ne;
5553 }
5554
5555 case ISD::MUL:
5556 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5557 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5558 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5559 return true;
5560 break;
5561
5562 case ISD::ZERO_EXTEND:
5563 case ISD::SIGN_EXTEND:
5564 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5565 }
5566
5568}
5569
5571 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
5572 return !C1->isNegative();
5573
5574 return Op.getOpcode() == ISD::FABS;
5575}
5576
5578 // Check the obvious case.
5579 if (A == B) return true;
5580
5581 // For negative and positive zero.
5582 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
5583 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
5584 if (CA->isZero() && CB->isZero()) return true;
5585
5586 // Otherwise they may not be equal.
5587 return false;
5588}
5589
5590// Only bits set in Mask must be negated, other bits may be arbitrary.
5592 if (isBitwiseNot(V, AllowUndefs))
5593 return V.getOperand(0);
5594
5595 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
5596 // bits in the non-extended part.
5597 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
5598 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
5599 return SDValue();
5600 SDValue ExtArg = V.getOperand(0);
5601 if (ExtArg.getScalarValueSizeInBits() >=
5602 MaskC->getAPIntValue().getActiveBits() &&
5603 isBitwiseNot(ExtArg, AllowUndefs) &&
5604 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
5605 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
5606 return ExtArg.getOperand(0).getOperand(0);
5607 return SDValue();
5608}
5609
5611 // Match masked merge pattern (X & ~M) op (Y & M)
5612 // Including degenerate case (X & ~M) op M
5613 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
5614 SDValue Other) {
5615 if (SDValue NotOperand =
5616 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
5617 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
5618 NotOperand->getOpcode() == ISD::TRUNCATE)
5619 NotOperand = NotOperand->getOperand(0);
5620
5621 if (Other == NotOperand)
5622 return true;
5623 if (Other->getOpcode() == ISD::AND)
5624 return NotOperand == Other->getOperand(0) ||
5625 NotOperand == Other->getOperand(1);
5626 }
5627 return false;
5628 };
5629
5630 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
5631 A = A->getOperand(0);
5632
5633 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
5634 B = B->getOperand(0);
5635
5636 if (A->getOpcode() == ISD::AND)
5637 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
5638 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
5639 return false;
5640}
5641
5642// FIXME: unify with llvm::haveNoCommonBitsSet.
5644 assert(A.getValueType() == B.getValueType() &&
5645 "Values must have the same type");
5648 return true;
5651}
5652
5653static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
5654 SelectionDAG &DAG) {
5655 if (cast<ConstantSDNode>(Step)->isZero())
5656 return DAG.getConstant(0, DL, VT);
5657
5658 return SDValue();
5659}
5660
5663 SelectionDAG &DAG) {
5664 int NumOps = Ops.size();
5665 assert(NumOps != 0 && "Can't build an empty vector!");
5666 assert(!VT.isScalableVector() &&
5667 "BUILD_VECTOR cannot be used with scalable types");
5668 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
5669 "Incorrect element count in BUILD_VECTOR!");
5670
5671 // BUILD_VECTOR of UNDEFs is UNDEF.
5672 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
5673 return DAG.getUNDEF(VT);
5674
5675 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
5676 SDValue IdentitySrc;
5677 bool IsIdentity = true;
5678 for (int i = 0; i != NumOps; ++i) {
5679 if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
5680 Ops[i].getOperand(0).getValueType() != VT ||
5681 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
5682 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
5683 Ops[i].getConstantOperandAPInt(1) != i) {
5684 IsIdentity = false;
5685 break;
5686 }
5687 IdentitySrc = Ops[i].getOperand(0);
5688 }
5689 if (IsIdentity)
5690 return IdentitySrc;
5691
5692 return SDValue();
5693}
5694
5695/// Try to simplify vector concatenation to an input value, undef, or build
5696/// vector.
5699 SelectionDAG &DAG) {
5700 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
5701 assert(llvm::all_of(Ops,
5702 [Ops](SDValue Op) {
5703 return Ops[0].getValueType() == Op.getValueType();
5704 }) &&
5705 "Concatenation of vectors with inconsistent value types!");
5706 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
5707 VT.getVectorElementCount() &&
5708 "Incorrect element count in vector concatenation!");
5709
5710 if (Ops.size() == 1)
5711 return Ops[0];
5712
5713 // Concat of UNDEFs is UNDEF.
5714 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
5715 return DAG.getUNDEF(VT);
5716
5717 // Scan the operands and look for extract operations from a single source
5718 // that correspond to insertion at the same location via this concatenation:
5719 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
5720 SDValue IdentitySrc;
5721 bool IsIdentity = true;
5722 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
5723 SDValue Op = Ops[i];
5724 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
5725 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
5726 Op.getOperand(0).getValueType() != VT ||
5727 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
5728 Op.getConstantOperandVal(1) != IdentityIndex) {
5729 IsIdentity = false;
5730 break;
5731 }
5732 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
5733 "Unexpected identity source vector for concat of extracts");
5734 IdentitySrc = Op.getOperand(0);
5735 }
5736 if (IsIdentity) {
5737 assert(IdentitySrc && "Failed to set source vector of extracts");
5738 return IdentitySrc;
5739 }
5740
5741 // The code below this point is only designed to work for fixed width
5742 // vectors, so we bail out for now.
5743 if (VT.isScalableVector())
5744 return SDValue();
5745
5746 // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
5747 // simplified to one big BUILD_VECTOR.
5748 // FIXME: Add support for SCALAR_TO_VECTOR as well.
5749 EVT SVT = VT.getScalarType();
5751 for (SDValue Op : Ops) {
5752 EVT OpVT = Op.getValueType();
5753 if (Op.isUndef())
5754 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
5755 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
5756 Elts.append(Op->op_begin(), Op->op_end());
5757 else
5758 return SDValue();
5759 }
5760
5761 // BUILD_VECTOR requires all inputs to be of the same type, find the
5762 // maximum type and extend them all.
5763 for (SDValue Op : Elts)
5764 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
5765
5766 if (SVT.bitsGT(VT.getScalarType())) {
5767 for (SDValue &Op : Elts) {
5768 if (Op.isUndef())
5769 Op = DAG.getUNDEF(SVT);
5770 else
5771 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
5772 ? DAG.getZExtOrTrunc(Op, DL, SVT)
5773 : DAG.getSExtOrTrunc(Op, DL, SVT);
5774 }
5775 }
5776
5777 SDValue V = DAG.getBuildVector(VT, DL, Elts);
5778 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
5779 return V;
5780}
5781
5782/// Gets or creates the specified node.
5783SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
5784 SDVTList VTs = getVTList(VT);
5786 AddNodeIDNode(ID, Opcode, VTs, std::nullopt);
5787 void *IP = nullptr;
5788 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
5789 return SDValue(E, 0);
5790
5791 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
5792 CSEMap.InsertNode(N, IP);
5793
5794 InsertNode(N);
5795 SDValue V = SDValue(N, 0);
5796 NewSDValueDbgMsg(V, "Creating new node: ", this);
5797 return V;
5798}
5799
5800SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5801 SDValue N1) {
5802 SDNodeFlags Flags;
5803 if (Inserter)
5804 Flags = Inserter->getFlags();
5805 return getNode(Opcode, DL, VT, N1, Flags);
5806}
5807
5808SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5809 SDValue N1, const SDNodeFlags Flags) {
5810 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
5811
5812 // Constant fold unary operations with a vector integer or float operand.
5813 switch (Opcode) {
5814 default:
5815 // FIXME: Entirely reasonable to perform folding of other unary
5816 // operations here as the need arises.
5817 break;
5818 case ISD::FNEG:
5819 case ISD::FABS:
5820 case ISD::FCEIL:
5821 case ISD::FTRUNC:
5822 case ISD::FFLOOR:
5823 case ISD::FP_EXTEND:
5824 case ISD::FP_TO_SINT:
5825 case ISD::FP_TO_UINT:
5826 case ISD::FP_TO_FP16:
5827 case ISD::FP_TO_BF16:
5828 case ISD::TRUNCATE:
5829 case ISD::ANY_EXTEND:
5830 case ISD::ZERO_EXTEND:
5831 case ISD::SIGN_EXTEND:
5832 case ISD::UINT_TO_FP:
5833 case ISD::SINT_TO_FP:
5834 case ISD::FP16_TO_FP:
5835 case ISD::BF16_TO_FP:
5836 case ISD::BITCAST:
5837 case ISD::ABS:
5838 case ISD::BITREVERSE:
5839 case ISD::BSWAP:
5840 case ISD::CTLZ:
5842 case ISD::CTTZ:
5844 case ISD::CTPOP:
5845 case ISD::STEP_VECTOR: {
5846 SDValue Ops = {N1};
5847 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
5848 return Fold;
5849 }
5850 }
5851
5852 unsigned OpOpcode = N1.getNode()->getOpcode();
5853 switch (Opcode) {
5854 case ISD::STEP_VECTOR:
5855 assert(VT.isScalableVector() &&
5856 "STEP_VECTOR can only be used with scalable types");
5857 assert(OpOpcode == ISD::TargetConstant &&
5858 VT.getVectorElementType() == N1.getValueType() &&
5859 "Unexpected step operand");
5860 break;
5861 case ISD::FREEZE:
5862 assert(VT == N1.getValueType() && "Unexpected VT!");
5863 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly*/ false,
5864 /*Depth*/ 1))
5865 return N1;
5866 break;
5867 case ISD::TokenFactor:
5868 case ISD::MERGE_VALUES:
5870 return N1; // Factor, merge or concat of one node? No need.
5871 case ISD::BUILD_VECTOR: {
5872 // Attempt to simplify BUILD_VECTOR.
5873 SDValue Ops[] = {N1};
5874 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
5875 return V;
5876 break;
5877 }
5878 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
5879 case ISD::FP_EXTEND:
5881 "Invalid FP cast!");
5882 if (N1.getValueType() == VT) return N1; // noop conversion.
5883 assert((!VT.isVector() || VT.getVectorElementCount() ==
5885 "Vector element count mismatch!");
5886 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
5887 if (N1.isUndef())
5888 return getUNDEF(VT);
5889 break;
5890 case ISD::FP_TO_SINT:
5891 case ISD::FP_TO_UINT:
5892 if (N1.isUndef())
5893 return getUNDEF(VT);
5894 break;
5895 case ISD::SINT_TO_FP:
5896 case ISD::UINT_TO_FP:
5897 // [us]itofp(undef) = 0, because the result value is bounded.
5898 if (N1.isUndef())
5899 return getConstantFP(0.0, DL, VT);
5900 break;
5901 case ISD::SIGN_EXTEND:
5902 assert(VT.isInteger() && N1.getValueType().isInteger() &&
5903 "Invalid SIGN_EXTEND!");
5904 assert(VT.isVector() == N1.getValueType().isVector() &&
5905 "SIGN_EXTEND result type type should be vector iff the operand "
5906 "type is vector!");
5907 if (N1.getValueType() == VT) return N1; // noop extension
5908 assert((!VT.isVector() || VT.getVectorElementCount() ==
5910 "Vector element count mismatch!");
5911 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
5912 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
5913 SDNodeFlags Flags;
5914 if (OpOpcode == ISD::ZERO_EXTEND)
5915 Flags.setNonNeg(N1->getFlags().hasNonNeg());
5916 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
5917 }
5918 if (OpOpcode == ISD::UNDEF)
5919 // sext(undef) = 0, because the top bits will all be the same.
5920 return getConstant(0, DL, VT);
5921 break;
5922 case ISD::ZERO_EXTEND:
5923 assert(VT.isInteger() && N1.getValueType().isInteger() &&
5924 "Invalid ZERO_EXTEND!");
5925 assert(VT.isVector() == N1.getValueType().isVector() &&
5926 "ZERO_EXTEND result type type should be vector iff the operand "
5927 "type is vector!");
5928 if (N1.getValueType() == VT) return N1; // noop extension
5929 assert((!VT.isVector() || VT.getVectorElementCount() ==
5931 "Vector element count mismatch!");
5932 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
5933 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
5934 SDNodeFlags Flags;
5935 Flags.setNonNeg(N1->getFlags().hasNonNeg());
5936 return getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
5937 }
5938 if (OpOpcode == ISD::UNDEF)
5939 // zext(undef) = 0, because the top bits will be zero.
5940 return getConstant(0, DL, VT);
5941
5942 // Skip unnecessary zext_inreg pattern:
5943 // (zext (trunc x)) -> x iff the upper bits are known zero.
5944 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
5945 // use to recognise zext_inreg patterns.
5946 if (OpOpcode == ISD::TRUNCATE) {
5947 SDValue OpOp = N1.getOperand(0);
5948 if (OpOp.getValueType() == VT) {
5949 if (OpOp.getOpcode() != ISD::AND) {
5952 if (MaskedValueIsZero(OpOp, HiBits)) {
5953 transferDbgValues(N1, OpOp);
5954 return OpOp;
5955 }
5956 }
5957 }
5958 }
5959 break;
5960 case ISD::ANY_EXTEND:
5961 assert(VT.isInteger() && N1.getValueType().isInteger() &&
5962 "Invalid ANY_EXTEND!");
5963 assert(VT.isVector() == N1.getValueType().isVector() &&
5964 "ANY_EXTEND result type type should be vector iff the operand "
5965 "type is vector!");
5966 if (N1.getValueType() == VT) return N1; // noop extension
5967 assert((!VT.isVector() || VT.getVectorElementCount() ==
5969 "Vector element count mismatch!");
5970 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
5971
5972 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
5973 OpOpcode == ISD::ANY_EXTEND) {
5974 SDNodeFlags Flags;
5975 if (OpOpcode == ISD::ZERO_EXTEND)
5976 Flags.setNonNeg(N1->getFlags().hasNonNeg());
5977 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
5978 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
5979 }
5980 if (OpOpcode == ISD::UNDEF)
5981 return getUNDEF(VT);
5982
5983 // (ext (trunc x)) -> x
5984 if (OpOpcode == ISD::TRUNCATE) {
5985 SDValue OpOp = N1.getOperand(0);
5986 if (OpOp.getValueType() == VT) {
5987 transferDbgValues(N1, OpOp);
5988 return OpOp;
5989 }
5990 }
5991 break;
5992 case ISD::TRUNCATE:
5993 assert(VT.isInteger() && N1.getValueType().isInteger() &&
5994 "Invalid TRUNCATE!");
5995 assert(VT.isVector() == N1.getValueType().isVector() &&
5996 "TRUNCATE result type type should be vector iff the operand "
5997 "type is vector!");
5998 if (N1.getValueType() == VT) return N1; // noop truncate
5999 assert((!VT.isVector() || VT.getVectorElementCount() ==
6001 "Vector element count mismatch!");
6002 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6003 if (OpOpcode == ISD::TRUNCATE)
6004 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6005 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6006 OpOpcode == ISD::ANY_EXTEND) {
6007 // If the source is smaller than the dest, we still need an extend.
6009 VT.getScalarType()))
6010 return getNode(OpOpcode, DL, VT, N1.getOperand(0));
6011 if (N1.getOperand(0).getValueType().bitsGT(VT))
6012 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6013 return N1.getOperand(0);
6014 }
6015 if (OpOpcode == ISD::UNDEF)
6016 return getUNDEF(VT);
6017 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6018 return getVScale(DL, VT,
6020 break;
6024 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6025 assert(N1.getValueType().bitsLE(VT) &&
6026 "The input must be the same size or smaller than the result.");
6029 "The destination vector type must have fewer lanes than the input.");
6030 break;
6031 case ISD::ABS:
6032 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6033 if (OpOpcode == ISD::UNDEF)
6034 return getConstant(0, DL, VT);
6035 break;
6036 case ISD::BSWAP:
6037 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6038 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6039 "BSWAP types must be a multiple of 16 bits!");
6040 if (OpOpcode == ISD::UNDEF)
6041 return getUNDEF(VT);
6042 // bswap(bswap(X)) -> X.
6043 if (OpOpcode == ISD::BSWAP)
6044 return N1.getOperand(0);
6045 break;
6046 case ISD::BITREVERSE:
6047 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6048 if (OpOpcode == ISD::UNDEF)
6049 return getUNDEF(VT);
6050 break;
6051 case ISD::BITCAST:
6053 "Cannot BITCAST between types of different sizes!");
6054 if (VT == N1.getValueType()) return N1; // noop conversion.
6055 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6056 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6057 if (OpOpcode == ISD::UNDEF)
6058 return getUNDEF(VT);
6059 break;
6061 assert(VT.isVector() && !N1.getValueType().isVector() &&
6062 (VT.getVectorElementType() == N1.getValueType() ||
6064 N1.getValueType().isInteger() &&
6066 "Illegal SCALAR_TO_VECTOR node!");
6067 if (OpOpcode == ISD::UNDEF)
6068 return getUNDEF(VT);
6069 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6070 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6071 isa<ConstantSDNode>(N1.getOperand(1)) &&
6072 N1.getConstantOperandVal(1) == 0 &&
6073 N1.getOperand(0).getValueType() == VT)
6074 return N1.getOperand(0);
6075 break;
6076 case ISD::FNEG:
6077 // Negation of an unknown bag of bits is still completely undefined.
6078 if (OpOpcode == ISD::UNDEF)
6079 return getUNDEF(VT);
6080
6081 if (OpOpcode == ISD::FNEG) // --X -> X
6082 return N1.getOperand(0);
6083 break;
6084 case ISD::FABS:
6085 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6086 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6087 break;
6088 case ISD::VSCALE:
6089 assert(VT == N1.getValueType() && "Unexpected VT!");
6090 break;
6091 case ISD::CTPOP:
6092 if (N1.getValueType().getScalarType() == MVT::i1)
6093 return N1;
6094 break;
6095 case ISD::CTLZ:
6096 case ISD::CTTZ:
6097 if (N1.getValueType().getScalarType() == MVT::i1)
6098 return getNOT(DL, N1, N1.getValueType());
6099 break;
6100 case ISD::VECREDUCE_ADD:
6101 if (N1.getValueType().getScalarType() == MVT::i1)
6102 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6103 break;
6106 if (N1.getValueType().getScalarType() == MVT::i1)
6107 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6108 break;
6111 if (N1.getValueType().getScalarType() == MVT::i1)
6112 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6113 break;
6114 case ISD::SPLAT_VECTOR:
6115 assert(VT.isVector() && "Wrong return type!");
6116 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6117 // that for now.
6119 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6121 N1.getValueType().isInteger() &&
6123 "Wrong operand type!");
6124 break;
6125 }
6126
6127 SDNode *N;
6128 SDVTList VTs = getVTList(VT);
6129 SDValue Ops[] = {N1};
6130 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6132 AddNodeIDNode(ID, Opcode, VTs, Ops);
6133 void *IP = nullptr;
6134 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6135 E->intersectFlagsWith(Flags);
6136 return SDValue(E, 0);
6137 }
6138
6139 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6140 N->setFlags(Flags);
6141 createOperands(N, Ops);
6142 CSEMap.InsertNode(N, IP);
6143 } else {
6144 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6145 createOperands(N, Ops);
6146 }
6147
6148 InsertNode(N);
6149 SDValue V = SDValue(N, 0);
6150 NewSDValueDbgMsg(V, "Creating new node: ", this);
6151 return V;
6152}
6153
6154static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6155 const APInt &C2) {
6156 switch (Opcode) {
6157 case ISD::ADD: return C1 + C2;
6158 case ISD::SUB: return C1 - C2;
6159 case ISD::MUL: return C1 * C2;
6160 case ISD::AND: return C1 & C2;
6161 case ISD::OR: return C1 | C2;
6162 case ISD::XOR: return C1 ^ C2;
6163 case ISD::SHL: return C1 << C2;
6164 case ISD::SRL: return C1.lshr(C2);
6165 case ISD::SRA: return C1.ashr(C2);
6166 case ISD::ROTL: return C1.rotl(C2);
6167 case ISD::ROTR: return C1.rotr(C2);
6168 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6169 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6170 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6171 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6172 case ISD::SADDSAT: return C1.sadd_sat(C2);
6173 case ISD::UADDSAT: return C1.uadd_sat(C2);
6174 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6175 case ISD::USUBSAT: return C1.usub_sat(C2);
6176 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6177 case ISD::USHLSAT: return C1.ushl_sat(C2);
6178 case ISD::UDIV:
6179 if (!C2.getBoolValue())
6180 break;
6181 return C1.udiv(C2);
6182 case ISD::UREM:
6183 if (!C2.getBoolValue())
6184 break;
6185 return C1.urem(C2);
6186 case ISD::SDIV:
6187 if (!C2.getBoolValue())
6188 break;
6189 return C1.sdiv(C2);
6190 case ISD::SREM:
6191 if (!C2.getBoolValue())
6192 break;
6193 return C1.srem(C2);
6194 case ISD::AVGFLOORS:
6195 return APIntOps::avgFloorS(C1, C2);
6196 case ISD::AVGFLOORU:
6197 return APIntOps::avgFloorU(C1, C2);
6198 case ISD::AVGCEILS:
6199 return APIntOps::avgCeilS(C1, C2);
6200 case ISD::AVGCEILU:
6201 return APIntOps::avgCeilU(C1, C2);
6202 case ISD::ABDS:
6203 return APIntOps::abds(C1, C2);
6204 case ISD::ABDU:
6205 return APIntOps::abdu(C1, C2);
6206 case ISD::MULHS:
6207 return APIntOps::mulhs(C1, C2);
6208 case ISD::MULHU:
6209 return APIntOps::mulhu(C1, C2);
6210 }
6211 return std::nullopt;
6212}
6213// Handle constant folding with UNDEF.
6214// TODO: Handle more cases.
6215static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6216 bool IsUndef1, const APInt &C2,
6217 bool IsUndef2) {
6218 if (!(IsUndef1 || IsUndef2))
6219 return FoldValue(Opcode, C1, C2);
6220
6221 // Fold and(x, undef) -> 0
6222 // Fold mul(x, undef) -> 0
6223 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6224 return APInt::getZero(C1.getBitWidth());
6225
6226 return std::nullopt;
6227}
6228
6230 const GlobalAddressSDNode *GA,
6231 const SDNode *N2) {
6232 if (GA->getOpcode() != ISD::GlobalAddress)
6233 return SDValue();
6234 if (!TLI->isOffsetFoldingLegal(GA))
6235 return SDValue();
6236 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6237 if (!C2)
6238 return SDValue();
6239 int64_t Offset = C2->getSExtValue();
6240 switch (Opcode) {
6241 case ISD::ADD: break;
6242 case ISD::SUB: Offset = -uint64_t(Offset); break;
6243 default: return SDValue();
6244 }
6245 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6246 GA->getOffset() + uint64_t(Offset));
6247}
6248
6249bool SelectionDAG::isUndef(unsigned Opcode, ArrayRef<SDValue> Ops) {
6250 switch (Opcode) {
6251 case ISD::SDIV:
6252 case ISD::UDIV:
6253 case ISD::SREM:
6254 case ISD::UREM: {
6255 // If a divisor is zero/undef or any element of a divisor vector is
6256 // zero/undef, the whole op is undef.
6257 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6258 SDValue Divisor = Ops[1];
6259 if (Divisor.isUndef() || isNullConstant(Divisor))
6260 return true;
6261
6262 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6263 llvm::any_of(Divisor->op_values(),
6264 [](SDValue V) { return V.isUndef() ||
6265 isNullConstant(V); });
6266 // TODO: Handle signed overflow.
6267 }
6268 // TODO: Handle oversized shifts.
6269 default:
6270 return false;
6271 }
6272}
6273
6275 EVT VT, ArrayRef<SDValue> Ops) {
6276 // If the opcode is a target-specific ISD node, there's nothing we can
6277 // do here and the operand rules may not line up with the below, so
6278 // bail early.
6279 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6280 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6281 // foldCONCAT_VECTORS in getNode before this is called.
6282 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6283 return SDValue();
6284
6285 unsigned NumOps = Ops.size();
6286 if (NumOps == 0)
6287 return SDValue();
6288
6289 if (isUndef(Opcode, Ops))
6290 return getUNDEF(VT);
6291
6292 // Handle unary special cases.
6293 if (NumOps == 1) {
6294 SDValue N1 = Ops[0];
6295
6296 // Constant fold unary operations with an integer constant operand. Even
6297 // opaque constant will be folded, because the folding of unary operations
6298 // doesn't create new constants with different values. Nevertheless, the
6299 // opaque flag is preserved during folding to prevent future folding with
6300 // other constants.
6301 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6302 const APInt &Val = C->getAPIntValue();
6303 switch (Opcode) {
6304 case ISD::SIGN_EXTEND:
6305 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6306 C->isTargetOpcode(), C->isOpaque());
6307 case ISD::TRUNCATE:
6308 if (C->isOpaque())
6309 break;
6310 [[fallthrough]];
6311 case ISD::ZERO_EXTEND:
6312 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6313 C->isTargetOpcode(), C->isOpaque());
6314 case ISD::ANY_EXTEND:
6315 // Some targets like RISCV prefer to sign extend some types.
6316 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6317 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6318 C->isTargetOpcode(), C->isOpaque());
6319 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6320 C->isTargetOpcode(), C->isOpaque());
6321 case ISD::ABS:
6322 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6323 C->isOpaque());
6324 case ISD::BITREVERSE:
6325 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6326 C->isOpaque());
6327 case ISD::BSWAP:
6328 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6329 C->isOpaque());
6330 case ISD::CTPOP:
6331 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6332 C->isOpaque());
6333 case ISD::CTLZ:
6335 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
6336 C->isOpaque());
6337 case ISD::CTTZ:
6339 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
6340 C->isOpaque());
6341 case ISD::UINT_TO_FP:
6342 case ISD::SINT_TO_FP: {
6345 (void)apf.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
6347 return getConstantFP(apf, DL, VT);
6348 }
6349 case ISD::FP16_TO_FP:
6350 case ISD::BF16_TO_FP: {
6351 bool Ignored;
6352 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
6353 : APFloat::BFloat(),
6354 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
6355
6356 // This can return overflow, underflow, or inexact; we don't care.
6357 // FIXME need to be more flexible about rounding mode.
6358 (void)FPV.convert(EVTToAPFloatSemantics(VT),
6360 return getConstantFP(FPV, DL, VT);
6361 }
6362 case ISD::STEP_VECTOR:
6363 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
6364 return V;
6365 break;
6366 case ISD::BITCAST:
6367 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
6368 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
6369 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
6370 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
6371 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
6372 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
6373 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
6374 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
6375 break;
6376 }
6377 }
6378
6379 // Constant fold unary operations with a floating point constant operand.
6380 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
6381 APFloat V = C->getValueAPF(); // make copy
6382 switch (Opcode) {
6383 case ISD::FNEG:
6384 V.changeSign();
6385 return getConstantFP(V, DL, VT);
6386 case ISD::FABS:
6387 V.clearSign();
6388 return getConstantFP(V, DL, VT);
6389 case ISD::FCEIL: {
6390 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
6391 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6392 return getConstantFP(V, DL, VT);
6393 return SDValue();
6394 }
6395 case ISD::FTRUNC: {
6396 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
6397 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6398 return getConstantFP(V, DL, VT);
6399 return SDValue();
6400 }
6401 case ISD::FFLOOR: {
6402 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
6403 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6404 return getConstantFP(V, DL, VT);
6405 return SDValue();
6406 }
6407 case ISD::FP_EXTEND: {
6408 bool ignored;
6409 // This can return overflow, underflow, or inexact; we don't care.
6410 // FIXME need to be more flexible about rounding mode.
6412 &ignored);
6413 return getConstantFP(V, DL, VT);
6414 }
6415 case ISD::FP_TO_SINT:
6416 case ISD::FP_TO_UINT: {
6417 bool ignored;
6418 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
6419 // FIXME need to be more flexible about rounding mode.
6421 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
6422 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
6423 break;
6424 return getConstant(IntVal, DL, VT);
6425 }
6426 case ISD::FP_TO_FP16:
6427 case ISD::FP_TO_BF16: {
6428 bool Ignored;
6429 // This can return overflow, underflow, or inexact; we don't care.
6430 // FIXME need to be more flexible about rounding mode.
6431 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
6432 : APFloat::BFloat(),
6434 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6435 }
6436 case ISD::BITCAST:
6437 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
6438 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6439 VT);
6440 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
6441 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6442 VT);
6443 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
6444 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
6445 VT);
6446 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
6447 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6448 break;
6449 }
6450 }
6451
6452 // Early-out if we failed to constant fold a bitcast.
6453 if (Opcode == ISD::BITCAST)
6454 return SDValue();
6455 }
6456
6457 // Handle binops special cases.
6458 if (NumOps == 2) {
6459 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
6460 return CFP;
6461
6462 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
6463 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
6464 if (C1->isOpaque() || C2->isOpaque())
6465 return SDValue();
6466
6467 std::optional<APInt> FoldAttempt =
6468 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
6469 if (!FoldAttempt)
6470 return SDValue();
6471
6472 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
6473 assert((!Folded || !VT.isVector()) &&
6474 "Can't fold vectors ops with scalar operands");
6475 return Folded;
6476 }
6477 }
6478
6479 // fold (add Sym, c) -> Sym+c
6480 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[0]))
6481 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
6482 if (TLI->isCommutativeBinOp(Opcode))
6483 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[1]))
6484 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
6485 }
6486
6487 // This is for vector folding only from here on.
6488 if (!VT.isVector())
6489 return SDValue();
6490
6491 ElementCount NumElts = VT.getVectorElementCount();
6492
6493 // See if we can fold through bitcasted integer ops.
6494 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
6495 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
6496 Ops[0].getOpcode() == ISD::BITCAST &&
6497 Ops[1].getOpcode() == ISD::BITCAST) {
6498 SDValue N1 = peekThroughBitcasts(Ops[0]);
6499 SDValue N2 = peekThroughBitcasts(Ops[1]);
6500 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
6501 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
6502 EVT BVVT = N1.getValueType();
6503 if (BV1 && BV2 && BVVT.isInteger() && BVVT == N2.getValueType()) {
6504 bool IsLE = getDataLayout().isLittleEndian();
6505 unsigned EltBits = VT.getScalarSizeInBits();
6506 SmallVector<APInt> RawBits1, RawBits2;
6507 BitVector UndefElts1, UndefElts2;
6508 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
6509 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
6510 SmallVector<APInt> RawBits;
6511 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
6512 std::optional<APInt> Fold = FoldValueWithUndef(
6513 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
6514 if (!Fold)
6515 break;
6516 RawBits.push_back(*Fold);
6517 }
6518 if (RawBits.size() == NumElts.getFixedValue()) {
6519 // We have constant folded, but we need to cast this again back to
6520 // the original (possibly legalized) type.
6521 SmallVector<APInt> DstBits;
6522 BitVector DstUndefs;
6524 DstBits, RawBits, DstUndefs,
6525 BitVector(RawBits.size(), false));
6526 EVT BVEltVT = BV1->getOperand(0).getValueType();
6527 unsigned BVEltBits = BVEltVT.getSizeInBits();
6528 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
6529 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
6530 if (DstUndefs[I])
6531 continue;
6532 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
6533 }
6534 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
6535 }
6536 }
6537 }
6538 }
6539
6540 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
6541 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
6542 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
6543 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
6544 APInt RHSVal;
6545 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
6546 APInt NewStep = Opcode == ISD::MUL
6547 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
6548 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
6549 return getStepVector(DL, VT, NewStep);
6550 }
6551 }
6552
6553 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
6554 return !Op.getValueType().isVector() ||
6555 Op.getValueType().getVectorElementCount() == NumElts;
6556 };
6557
6558 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
6559 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
6560 Op.getOpcode() == ISD::BUILD_VECTOR ||
6561 Op.getOpcode() == ISD::SPLAT_VECTOR;
6562 };
6563
6564 // All operands must be vector types with the same number of elements as
6565 // the result type and must be either UNDEF or a build/splat vector
6566 // or UNDEF scalars.
6567 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
6568 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
6569 return SDValue();
6570
6571 // If we are comparing vectors, then the result needs to be a i1 boolean that
6572 // is then extended back to the legal result type depending on how booleans
6573 // are represented.
6574 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
6575 ISD::NodeType ExtendCode =
6576 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
6579
6580 // Find legal integer scalar type for constant promotion and
6581 // ensure that its scalar size is at least as large as source.
6582 EVT LegalSVT = VT.getScalarType();
6583 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
6584 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
6585 if (LegalSVT.bitsLT(VT.getScalarType()))
6586 return SDValue();
6587 }
6588
6589 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
6590 // only have one operand to check. For fixed-length vector types we may have
6591 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
6592 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
6593
6594 // Constant fold each scalar lane separately.
6595 SmallVector<SDValue, 4> ScalarResults;
6596 for (unsigned I = 0; I != NumVectorElts; I++) {
6597 SmallVector<SDValue, 4> ScalarOps;
6598 for (SDValue Op : Ops) {
6599 EVT InSVT = Op.getValueType().getScalarType();
6600 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
6601 Op.getOpcode() != ISD::SPLAT_VECTOR) {
6602 if (Op.isUndef())
6603 ScalarOps.push_back(getUNDEF(InSVT));
6604 else
6605 ScalarOps.push_back(Op);
6606 continue;
6607 }
6608
6609 SDValue ScalarOp =
6610 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
6611 EVT ScalarVT = ScalarOp.getValueType();
6612
6613 // Build vector (integer) scalar operands may need implicit
6614 // truncation - do this before constant folding.
6615 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
6616 // Don't create illegally-typed nodes unless they're constants or undef
6617 // - if we fail to constant fold we can't guarantee the (dead) nodes
6618 // we're creating will be cleaned up before being visited for
6619 // legalization.
6620 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
6621 !isa<ConstantSDNode>(ScalarOp) &&
6622 TLI->getTypeAction(*getContext(), InSVT) !=
6624 return SDValue();
6625 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
6626 }
6627
6628 ScalarOps.push_back(ScalarOp);
6629 }
6630
6631 // Constant fold the scalar operands.
6632 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps);
6633
6634 // Legalize the (integer) scalar constant if necessary.
6635 if (LegalSVT != SVT)
6636 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
6637
6638 // Scalar folding only succeeded if the result is a constant or UNDEF.
6639 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
6640 ScalarResult.getOpcode() != ISD::ConstantFP)
6641 return SDValue();
6642 ScalarResults.push_back(ScalarResult);
6643 }
6644
6645 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
6646 : getBuildVector(VT, DL, ScalarResults);
6647 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
6648 return V;
6649}
6650
6652 EVT VT, ArrayRef<SDValue> Ops) {
6653 // TODO: Add support for unary/ternary fp opcodes.
6654 if (Ops.size() != 2)
6655 return SDValue();
6656
6657 // TODO: We don't do any constant folding for strict FP opcodes here, but we
6658 // should. That will require dealing with a potentially non-default
6659 // rounding mode, checking the "opStatus" return value from the APFloat
6660 // math calculations, and possibly other variations.
6661 SDValue N1 = Ops[0];
6662 SDValue N2 = Ops[1];
6663 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
6664 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
6665 if (N1CFP && N2CFP) {
6666 APFloat C1 = N1CFP->getValueAPF(); // make copy
6667 const APFloat &C2 = N2CFP->getValueAPF();
6668 switch (Opcode) {
6669 case ISD::FADD:
6671 return getConstantFP(C1, DL, VT);
6672 case ISD::FSUB:
6674 return getConstantFP(C1, DL, VT);
6675 case ISD::FMUL:
6677 return getConstantFP(C1, DL, VT);
6678 case ISD::FDIV:
6680 return getConstantFP(C1, DL, VT);
6681 case ISD::FREM:
6682 C1.mod(C2);
6683 return getConstantFP(C1, DL, VT);
6684 case ISD::FCOPYSIGN:
6685 C1.copySign(C2);
6686 return getConstantFP(C1, DL, VT);
6687 case ISD::FMINNUM:
6688 return getConstantFP(minnum(C1, C2), DL, VT);
6689 case ISD::FMAXNUM:
6690 return getConstantFP(maxnum(C1, C2), DL, VT);
6691 case ISD::FMINIMUM:
6692 return getConstantFP(minimum(C1, C2), DL, VT);
6693 case ISD::FMAXIMUM:
6694 return getConstantFP(maximum(C1, C2), DL, VT);
6695 default: break;
6696 }
6697 }
6698 if (N1CFP && Opcode == ISD::FP_ROUND) {
6699 APFloat C1 = N1CFP->getValueAPF(); // make copy
6700 bool Unused;
6701 // This can return overflow, underflow, or inexact; we don't care.
6702 // FIXME need to be more flexible about rounding mode.
6704 &Unused);
6705 return getConstantFP(C1, DL, VT);
6706 }
6707
6708 switch (Opcode) {
6709 case ISD::FSUB:
6710 // -0.0 - undef --> undef (consistent with "fneg undef")
6711 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
6712 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
6713 return getUNDEF(VT);
6714 [[fallthrough]];
6715
6716 case ISD::FADD:
6717 case ISD::FMUL:
6718 case ISD::FDIV:
6719 case ISD::FREM:
6720 // If both operands are undef, the result is undef. If 1 operand is undef,
6721 // the result is NaN. This should match the behavior of the IR optimizer.
6722 if (N1.isUndef() && N2.isUndef())
6723 return getUNDEF(VT);
6724 if (N1.isUndef() || N2.isUndef())
6726 }
6727 return SDValue();
6728}
6729
6731 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
6732
6733 // There's no need to assert on a byte-aligned pointer. All pointers are at
6734 // least byte aligned.
6735 if (A == Align(1))
6736 return Val;
6737
6738 SDVTList VTs = getVTList(Val.getValueType());
6740 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
6741 ID.AddInteger(A.value());
6742
6743 void *IP = nullptr;
6744 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6745 return SDValue(E, 0);
6746
6747 auto *N =
6748 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
6749 createOperands(N, {Val});
6750
6751 CSEMap.InsertNode(N, IP);
6752 InsertNode(N);
6753
6754 SDValue V(N, 0);
6755 NewSDValueDbgMsg(V, "Creating new node: ", this);
6756 return V;
6757}
6758
6759SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6760 SDValue N1, SDValue N2) {
6761 SDNodeFlags Flags;
6762 if (Inserter)
6763 Flags = Inserter->getFlags();
6764 return getNode(Opcode, DL, VT, N1, N2, Flags);
6765}
6766
6768 SDValue &N2) const {
6769 if (!TLI->isCommutativeBinOp(Opcode))
6770 return;
6771
6772 // Canonicalize:
6773 // binop(const, nonconst) -> binop(nonconst, const)
6778 if ((N1C && !N2C) || (N1CFP && !N2CFP))
6779 std::swap(N1, N2);
6780
6781 // Canonicalize:
6782 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
6783 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
6785 std::swap(N1, N2);
6786}
6787
6788SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6789 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
6791 N2.getOpcode() != ISD::DELETED_NODE &&
6792 "Operand is DELETED_NODE!");
6793
6794 canonicalizeCommutativeBinop(Opcode, N1, N2);
6795
6796 auto *N1C = dyn_cast<ConstantSDNode>(N1);
6797 auto *N2C = dyn_cast<ConstantSDNode>(N2);
6798
6799 // Don't allow undefs in vector splats - we might be returning N2 when folding
6800 // to zero etc.
6801 ConstantSDNode *N2CV =
6802 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
6803
6804 switch (Opcode) {
6805 default: break;
6806 case ISD::TokenFactor:
6807 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
6808 N2.getValueType() == MVT::Other && "Invalid token factor!");
6809 // Fold trivial token factors.
6810 if (N1.getOpcode() == ISD::EntryToken) return N2;
6811 if (N2.getOpcode() == ISD::EntryToken) return N1;
6812 if (N1 == N2) return N1;
6813 break;
6814 case ISD::BUILD_VECTOR: {
6815 // Attempt to simplify BUILD_VECTOR.
6816 SDValue Ops[] = {N1, N2};
6817 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6818 return V;
6819 break;
6820 }
6821 case ISD::CONCAT_VECTORS: {
6822 SDValue Ops[] = {N1, N2};
6823 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
6824 return V;
6825 break;
6826 }
6827 case ISD::AND:
6828 assert(VT.isInteger() && "This operator does not apply to FP types!");
6829 assert(N1.getValueType() == N2.getValueType() &&
6830 N1.getValueType() == VT && "Binary operator types must match!");
6831 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
6832 // worth handling here.
6833 if (N2CV && N2CV->isZero())
6834 return N2;
6835 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
6836 return N1;
6837 break;
6838 case ISD::OR:
6839 case ISD::XOR:
6840 case ISD::ADD:
6841 case ISD::SUB:
6842 assert(VT.isInteger() && "This operator does not apply to FP types!");
6843 assert(N1.getValueType() == N2.getValueType() &&
6844 N1.getValueType() == VT && "Binary operator types must match!");
6845 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
6846 // it's worth handling here.
6847 if (N2CV && N2CV->isZero())
6848 return N1;
6849 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) && VT.isVector() &&
6850 VT.getVectorElementType() == MVT::i1)
6851 return getNode(ISD::XOR, DL, VT, N1, N2);
6852 break;
6853 case ISD::MUL:
6854 assert(VT.isInteger() && "This operator does not apply to FP types!");
6855 assert(N1.getValueType() == N2.getValueType() &&
6856 N1.getValueType() == VT && "Binary operator types must match!");
6857 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6858 return getNode(ISD::AND, DL, VT, N1, N2);
6859 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
6860 const APInt &MulImm = N1->getConstantOperandAPInt(0);
6861 const APInt &N2CImm = N2C->getAPIntValue();
6862 return getVScale(DL, VT, MulImm * N2CImm);
6863 }
6864 break;
6865 case ISD::UDIV:
6866 case ISD::UREM:
6867 case ISD::MULHU:
6868 case ISD::MULHS:
6869 case ISD::SDIV:
6870 case ISD::SREM:
6871 case ISD::SADDSAT:
6872 case ISD::SSUBSAT:
6873 case ISD::UADDSAT:
6874 case ISD::USUBSAT:
6875 assert(VT.isInteger() && "This operator does not apply to FP types!");
6876 assert(N1.getValueType() == N2.getValueType() &&
6877 N1.getValueType() == VT && "Binary operator types must match!");
6878 if (VT.isVector() && VT.getVectorElementType() == MVT::i1) {
6879 // fold (add_sat x, y) -> (or x, y) for bool types.
6880 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
6881 return getNode(ISD::OR, DL, VT, N1, N2);
6882 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
6883 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
6884 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
6885 }
6886 break;
6887 case ISD::ABDS:
6888 case ISD::ABDU:
6889 assert(VT.isInteger() && "This operator does not apply to FP types!");
6890 assert(N1.getValueType() == N2.getValueType() &&
6891 N1.getValueType() == VT && "Binary operator types must match!");
6892 break;
6893 case ISD::SMIN:
6894 case ISD::UMAX:
6895 assert(VT.isInteger() && "This operator does not apply to FP types!");
6896 assert(N1.getValueType() == N2.getValueType() &&
6897 N1.getValueType() == VT && "Binary operator types must match!");
6898 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6899 return getNode(ISD::OR, DL, VT, N1, N2);
6900 break;
6901 case ISD::SMAX:
6902 case ISD::UMIN:
6903 assert(VT.isInteger() && "This operator does not apply to FP types!");
6904 assert(N1.getValueType() == N2.getValueType() &&
6905 N1.getValueType() == VT && "Binary operator types must match!");
6906 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6907 return getNode(ISD::AND, DL, VT, N1, N2);
6908 break;
6909 case ISD::FADD:
6910 case ISD::FSUB:
6911 case ISD::FMUL:
6912 case ISD::FDIV:
6913 case ISD::FREM:
6914 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
6915 assert(N1.getValueType() == N2.getValueType() &&
6916 N1.getValueType() == VT && "Binary operator types must match!");
6917 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
6918 return V;
6919 break;
6920 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
6921 assert(N1.getValueType() == VT &&
6924 "Invalid FCOPYSIGN!");
6925 break;
6926 case ISD::SHL:
6927 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
6928 const APInt &MulImm = N1->getConstantOperandAPInt(0);
6929 const APInt &ShiftImm = N2C->getAPIntValue();
6930 return getVScale(DL, VT, MulImm << ShiftImm);
6931 }
6932 [[fallthrough]];
6933 case ISD::SRA:
6934 case ISD::SRL:
6935 if (SDValue V = simplifyShift(N1, N2))
6936 return V;
6937 [[fallthrough]];
6938 case ISD::ROTL:
6939 case ISD::ROTR:
6940 assert(VT == N1.getValueType() &&
6941 "Shift operators return type must be the same as their first arg");
6942 assert(VT.isInteger() && N2.getValueType().isInteger() &&
6943 "Shifts only work on integers");
6944 assert((!VT.isVector() || VT == N2.getValueType()) &&
6945 "Vector shift amounts must be in the same as their first arg");
6946 // Verify that the shift amount VT is big enough to hold valid shift
6947 // amounts. This catches things like trying to shift an i1024 value by an
6948 // i8, which is easy to fall into in generic code that uses
6949 // TLI.getShiftAmount().
6952 "Invalid use of small shift amount with oversized value!");
6953
6954 // Always fold shifts of i1 values so the code generator doesn't need to
6955 // handle them. Since we know the size of the shift has to be less than the
6956 // size of the value, the shift/rotate count is guaranteed to be zero.
6957 if (VT == MVT::i1)
6958 return N1;
6959 if (N2CV && N2CV->isZero())
6960 return N1;
6961 break;
6962 case ISD::FP_ROUND:
6963 assert(VT.isFloatingPoint() &&
6965 VT.bitsLE(N1.getValueType()) &&
6966 N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
6967 "Invalid FP_ROUND!");
6968 if (N1.getValueType() == VT) return N1; // noop conversion.
6969 break;
6970 case ISD::AssertSext:
6971 case ISD::AssertZext: {
6972 EVT EVT = cast<VTSDNode>(N2)->getVT();
6973 assert(VT == N1.getValueType() && "Not an inreg extend!");
6974 assert(VT.isInteger() && EVT.isInteger() &&
6975 "Cannot *_EXTEND_INREG FP types");
6976 assert(!EVT.isVector() &&
6977 "AssertSExt/AssertZExt type should be the vector element type "
6978 "rather than the vector type!");
6979 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
6980 if (VT.getScalarType() == EVT) return N1; // noop assertion.
6981 break;
6982 }
6984 EVT EVT = cast<VTSDNode>(N2)->getVT();
6985 assert(VT == N1.getValueType() && "Not an inreg extend!");
6986 assert(VT.isInteger() && EVT.isInteger() &&
6987 "Cannot *_EXTEND_INREG FP types");
6988 assert(EVT.isVector() == VT.isVector() &&
6989 "SIGN_EXTEND_INREG type should be vector iff the operand "
6990 "type is vector!");
6991 assert((!EVT.isVector() ||
6993 "Vector element counts must match in SIGN_EXTEND_INREG");
6994 assert(EVT.bitsLE(VT) && "Not extending!");
6995 if (EVT == VT) return N1; // Not actually extending
6996
6997 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
6998 unsigned FromBits = EVT.getScalarSizeInBits();
6999 Val <<= Val.getBitWidth() - FromBits;
7000 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7001 return getConstant(Val, DL, ConstantVT);
7002 };
7003
7004 if (N1C) {
7005 const APInt &Val = N1C->getAPIntValue();
7006 return SignExtendInReg(Val, VT);
7007 }
7008
7011 llvm::EVT OpVT = N1.getOperand(0).getValueType();
7012 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
7013 SDValue Op = N1.getOperand(i);
7014 if (Op.isUndef()) {
7015 Ops.push_back(getUNDEF(OpVT));
7016 continue;
7017 }
7018 ConstantSDNode *C = cast<ConstantSDNode>(Op);
7019 APInt Val = C->getAPIntValue();
7020 Ops.push_back(SignExtendInReg(Val, OpVT));
7021 }
7022 return getBuildVector(VT, DL, Ops);
7023 }
7024
7025 if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7026 isa<ConstantSDNode>(N1.getOperand(0)))
7027 return getNode(
7028 ISD::SPLAT_VECTOR, DL, VT,
7029 SignExtendInReg(N1.getConstantOperandAPInt(0),
7030 N1.getOperand(0).getValueType()));
7031 break;
7032 }
7034 case ISD::FP_TO_UINT_SAT: {
7035 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7036 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7037 assert(N1.getValueType().isVector() == VT.isVector() &&
7038 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7039 "vector!");
7040 assert((!VT.isVector() || VT.getVectorElementCount() ==
7042 "Vector element counts must match in FP_TO_*INT_SAT");
7043 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7044 "Type to saturate to must be a scalar.");
7045 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7046 "Not extending!");
7047 break;
7048 }
7051 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7052 element type of the vector.");
7053
7054 // Extract from an undefined value or using an undefined index is undefined.
7055 if (N1.isUndef() || N2.isUndef())
7056 return getUNDEF(VT);
7057
7058 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7059 // vectors. For scalable vectors we will provide appropriate support for
7060 // dealing with arbitrary indices.
7061 if (N2C && N1.getValueType().isFixedLengthVector() &&
7062 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7063 return getUNDEF(VT);
7064
7065 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7066 // expanding copies of large vectors from registers. This only works for
7067 // fixed length vectors, since we need to know the exact number of
7068 // elements.
7069 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7071 unsigned Factor =
7074 N1.getOperand(N2C->getZExtValue() / Factor),
7075 getVectorIdxConstant(N2C->getZExtValue() % Factor, DL));
7076 }
7077
7078 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7079 // lowering is expanding large vector constants.
7080 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7081 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7084 "BUILD_VECTOR used for scalable vectors");
7085 unsigned Index =
7086 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7087 SDValue Elt = N1.getOperand(Index);
7088
7089 if (VT != Elt.getValueType())
7090 // If the vector element type is not legal, the BUILD_VECTOR operands
7091 // are promoted and implicitly truncated, and the result implicitly
7092 // extended. Make that explicit here.
7093 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7094
7095 return Elt;
7096 }
7097
7098 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7099 // operations are lowered to scalars.
7100 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7101 // If the indices are the same, return the inserted element else
7102 // if the indices are known different, extract the element from
7103 // the original vector.
7104 SDValue N1Op2 = N1.getOperand(2);
7105 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
7106
7107 if (N1Op2C && N2C) {
7108 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7109 if (VT == N1.getOperand(1).getValueType())
7110 return N1.getOperand(1);
7111 if (VT.isFloatingPoint()) {
7113 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7114 }
7115 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7116 }
7117 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7118 }
7119 }
7120
7121 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7122 // when vector types are scalarized and v1iX is legal.
7123 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7124 // Here we are completely ignoring the extract element index (N2),
7125 // which is fine for fixed width vectors, since any index other than 0
7126 // is undefined anyway. However, this cannot be ignored for scalable
7127 // vectors - in theory we could support this, but we don't want to do this
7128 // without a profitability check.
7129 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7131 N1.getValueType().getVectorNumElements() == 1) {
7132 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7133 N1.getOperand(1));
7134 }
7135 break;
7137 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7138 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7139 (N1.getValueType().isInteger() == VT.isInteger()) &&
7140 N1.getValueType() != VT &&
7141 "Wrong types for EXTRACT_ELEMENT!");
7142
7143 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7144 // 64-bit integers into 32-bit parts. Instead of building the extract of
7145 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7146 if (N1.getOpcode() == ISD::BUILD_PAIR)
7147 return N1.getOperand(N2C->getZExtValue());
7148
7149 // EXTRACT_ELEMENT of a constant int is also very common.
7150 if (N1C) {
7151 unsigned ElementSize = VT.getSizeInBits();
7152 unsigned Shift = ElementSize * N2C->getZExtValue();
7153 const APInt &Val = N1C->getAPIntValue();
7154 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7155 }
7156 break;
7158 EVT N1VT = N1.getValueType();
7159 assert(VT.isVector() && N1VT.isVector() &&
7160 "Extract subvector VTs must be vectors!");
7162 "Extract subvector VTs must have the same element type!");
7163 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
7164 "Cannot extract a scalable vector from a fixed length vector!");
7165 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7167 "Extract subvector must be from larger vector to smaller vector!");
7168 assert(N2C && "Extract subvector index must be a constant");
7169 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7170 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
7171 N1VT.getVectorMinNumElements()) &&
7172 "Extract subvector overflow!");
7173 assert(N2C->getAPIntValue().getBitWidth() ==
7174 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7175 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
7176
7177 // Trivial extraction.
7178 if (VT == N1VT)
7179 return N1;
7180
7181 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
7182 if (N1.isUndef())
7183 return getUNDEF(VT);
7184
7185 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
7186 // the concat have the same type as the extract.
7187 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
7188 VT == N1.getOperand(0).getValueType()) {
7189 unsigned Factor = VT.getVectorMinNumElements();
7190 return N1.getOperand(N2C->getZExtValue() / Factor);
7191 }
7192
7193 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
7194 // during shuffle legalization.
7195 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
7196 VT == N1.getOperand(1).getValueType())
7197 return N1.getOperand(1);
7198 break;
7199 }
7200 }
7201
7202 // Perform trivial constant folding.
7203 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}))
7204 return SV;
7205
7206 // Canonicalize an UNDEF to the RHS, even over a constant.
7207 if (N1.isUndef()) {
7208 if (TLI->isCommutativeBinOp(Opcode)) {
7209 std::swap(N1, N2);
7210 } else {
7211 switch (Opcode) {
7212 case ISD::SUB:
7213 return getUNDEF(VT); // fold op(undef, arg2) -> undef
7215 case ISD::UDIV:
7216 case ISD::SDIV:
7217 case ISD::UREM:
7218 case ISD::SREM:
7219 case ISD::SSUBSAT:
7220 case ISD::USUBSAT:
7221 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
7222 }
7223 }
7224 }
7225
7226 // Fold a bunch of operators when the RHS is undef.
7227 if (N2.isUndef()) {
7228 switch (Opcode) {
7229 case ISD::XOR:
7230 if (N1.isUndef())
7231 // Handle undef ^ undef -> 0 special case. This is a common
7232 // idiom (misuse).
7233 return getConstant(0, DL, VT);
7234 [[fallthrough]];
7235 case ISD::ADD:
7236 case ISD::SUB:
7237 case ISD::UDIV:
7238 case ISD::SDIV:
7239 case ISD::UREM:
7240 case ISD::SREM:
7241 return getUNDEF(VT); // fold op(arg1, undef) -> undef
7242 case ISD::MUL:
7243 case ISD::AND:
7244 case ISD::SSUBSAT:
7245 case ISD::USUBSAT:
7246 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
7247 case ISD::OR:
7248 case ISD::SADDSAT:
7249 case ISD::UADDSAT:
7250 return getAllOnesConstant(DL, VT);
7251 }
7252 }
7253
7254 // Memoize this node if possible.
7255 SDNode *N;
7256 SDVTList VTs = getVTList(VT);
7257 SDValue Ops[] = {N1, N2};
7258 if (VT != MVT::Glue) {
7260 AddNodeIDNode(ID, Opcode, VTs, Ops);
7261 void *IP = nullptr;
7262 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7263 E->intersectFlagsWith(Flags);
7264 return SDValue(E, 0);
7265 }
7266
7267 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7268 N->setFlags(Flags);
7269 createOperands(N, Ops);
7270 CSEMap.InsertNode(N, IP);
7271 } else {
7272 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7273 createOperands(N, Ops);
7274 }
7275
7276 InsertNode(N);
7277 SDValue V = SDValue(N, 0);
7278 NewSDValueDbgMsg(V, "Creating new node: ", this);
7279 return V;
7280}
7281
7282SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7283 SDValue N1, SDValue N2, SDValue N3) {
7284 SDNodeFlags Flags;
7285 if (Inserter)
7286 Flags = Inserter->getFlags();
7287 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
7288}
7289
7290SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7291 SDValue N1, SDValue N2, SDValue N3,
7292 const SDNodeFlags Flags) {
7294 N2.getOpcode() != ISD::DELETED_NODE &&
7295 N3.getOpcode() != ISD::DELETED_NODE &&
7296 "Operand is DELETED_NODE!");
7297 // Perform various simplifications.
7298 switch (Opcode) {
7299 case ISD::FMA:
7300 case ISD::FMAD: {
7301 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7302 assert(N1.getValueType() == VT && N2.getValueType() == VT &&
7303 N3.getValueType() == VT && "FMA types must match!");
7304 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
7305 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
7306 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
7307 if (N1CFP && N2CFP && N3CFP) {
7308 APFloat V1 = N1CFP->getValueAPF();
7309 const APFloat &V2 = N2CFP->getValueAPF();
7310 const APFloat &V3 = N3CFP->getValueAPF();
7311 if (Opcode == ISD::FMAD) {
7314 } else
7316 return getConstantFP(V1, DL, VT);
7317 }
7318 break;
7319 }
7320 case ISD::BUILD_VECTOR: {
7321 // Attempt to simplify BUILD_VECTOR.
7322 SDValue Ops[] = {N1, N2, N3};
7323 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7324 return V;
7325 break;
7326 }
7327 case ISD::CONCAT_VECTORS: {
7328 SDValue Ops[] = {N1, N2, N3};
7329 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7330 return V;
7331 break;
7332 }
7333 case ISD::SETCC: {
7334 assert(VT.isInteger() && "SETCC result type must be an integer!");
7335 assert(N1.getValueType() == N2.getValueType() &&
7336 "SETCC operands must have the same type!");
7337 assert(VT.isVector() == N1.getValueType().isVector() &&
7338 "SETCC type should be vector iff the operand type is vector!");
7339 assert((!VT.isVector() || VT.getVectorElementCount() ==
7341 "SETCC vector element counts must match!");
7342 // Use FoldSetCC to simplify SETCC's.
7343 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
7344 return V;
7345 // Vector constant folding.
7346 SDValue Ops[] = {N1, N2, N3};
7347 if (SDValue V = FoldConstantArithmetic(Opcode, DL, VT, Ops)) {
7348 NewSDValueDbgMsg(V, "New node vector constant folding: ", this);
7349 return V;
7350 }
7351 break;
7352 }
7353 case ISD::SELECT:
7354 case ISD::VSELECT:
7355 if (SDValue V = simplifySelect(N1, N2, N3))
7356 return V;
7357 break;
7359 llvm_unreachable("should use getVectorShuffle constructor!");
7360 case ISD::VECTOR_SPLICE: {
7361 if (cast<ConstantSDNode>(N3)->isZero())
7362 return N1;
7363 break;
7364 }
7366 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3);
7367 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
7368 // for scalable vectors where we will generate appropriate code to
7369 // deal with out-of-bounds cases correctly.
7370 if (N3C && N1.getValueType().isFixedLengthVector() &&
7372 return getUNDEF(VT);
7373
7374 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
7375 if (N3.isUndef())
7376 return getUNDEF(VT);
7377
7378 // If the inserted element is an UNDEF, just use the input vector.
7379 if (N2.isUndef())
7380 return N1;
7381
7382 break;
7383 }
7384 case ISD::INSERT_SUBVECTOR: {
7385 // Inserting undef into undef is still undef.
7386 if (N1.isUndef() && N2.isUndef())
7387 return getUNDEF(VT);
7388
7389 EVT N2VT = N2.getValueType();
7390 assert(VT == N1.getValueType() &&
7391 "Dest and insert subvector source types must match!");
7392 assert(VT.isVector() && N2VT.isVector() &&
7393 "Insert subvector VTs must be vectors!");
7395 "Insert subvector VTs must have the same element type!");
7396 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
7397 "Cannot insert a scalable vector into a fixed length vector!");
7398 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7400 "Insert subvector must be from smaller vector to larger vector!");
7401 assert(isa<ConstantSDNode>(N3) &&
7402 "Insert subvector index must be constant");
7403 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7404 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
7406 "Insert subvector overflow!");
7408 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7409 "Constant index for INSERT_SUBVECTOR has an invalid size");
7410
7411 // Trivial insertion.
7412 if (VT == N2VT)
7413 return N2;
7414
7415 // If this is an insert of an extracted vector into an undef vector, we
7416 // can just use the input to the extract.
7417 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7418 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
7419 return N2.getOperand(0);
7420 break;
7421 }
7422 case ISD::BITCAST:
7423 // Fold bit_convert nodes from a type to themselves.
7424 if (N1.getValueType() == VT)
7425 return N1;
7426 break;
7427 case ISD::VP_TRUNCATE:
7428 case ISD::VP_SIGN_EXTEND:
7429 case ISD::VP_ZERO_EXTEND:
7430 // Don't create noop casts.
7431 if (N1.getValueType() == VT)
7432 return N1;
7433 break;
7434 }
7435
7436 // Memoize node if it doesn't produce a glue result.
7437 SDNode *N;
7438 SDVTList VTs = getVTList(VT);
7439 SDValue Ops[] = {N1, N2, N3};
7440 if (VT != MVT::Glue) {
7442 AddNodeIDNode(ID, Opcode, VTs, Ops);
7443 void *IP = nullptr;
7444 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7445 E->intersectFlagsWith(Flags);
7446 return SDValue(E, 0);
7447 }
7448
7449 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7450 N->setFlags(Flags);
7451 createOperands(N, Ops);
7452 CSEMap.InsertNode(N, IP);
7453 } else {
7454 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7455 createOperands(N, Ops);
7456 }
7457
7458 InsertNode(N);
7459 SDValue V = SDValue(N, 0);
7460 NewSDValueDbgMsg(V, "Creating new node: ", this);
7461 return V;
7462}
7463
7464SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7465 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
7466 SDValue Ops[] = { N1, N2, N3, N4 };
7467 return getNode(Opcode, DL, VT, Ops);
7468}
7469
7470SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7471 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7472 SDValue N5) {
7473 SDValue Ops[] = { N1, N2, N3, N4, N5 };
7474 return getNode(Opcode, DL, VT, Ops);
7475}
7476
7477/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
7478/// the incoming stack arguments to be loaded from the stack.
7480 SmallVector<SDValue, 8> ArgChains;
7481
7482 // Include the original chain at the beginning of the list. When this is
7483 // used by target LowerCall hooks, this helps legalize find the
7484 // CALLSEQ_BEGIN node.
7485 ArgChains.push_back(Chain);
7486
7487 // Add a chain value for each stack argument.
7488 for (SDNode *U : getEntryNode().getNode()->uses())
7489 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
7490 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
7491 if (FI->getIndex() < 0)
7492 ArgChains.push_back(SDValue(L, 1));
7493
7494 // Build a tokenfactor for all the chains.
7495 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
7496}
7497
7498/// getMemsetValue - Vectorized representation of the memset value
7499/// operand.
7501 const SDLoc &dl) {
7502 assert(!Value.isUndef());
7503
7504 unsigned NumBits = VT.getScalarSizeInBits();
7505 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
7506 assert(C->getAPIntValue().getBitWidth() == 8);
7507 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
7508 if (VT.isInteger()) {
7509 bool IsOpaque = VT.getSizeInBits() > 64 ||
7510 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
7511 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
7512 }
7513 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
7514 VT);
7515 }
7516
7517 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
7518 EVT IntVT = VT.getScalarType();
7519 if (!IntVT.isInteger())
7520 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
7521
7522 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
7523 if (NumBits > 8) {
7524 // Use a multiplication with 0x010101... to extend the input to the
7525 // required length.
7526 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
7527 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
7528 DAG.getConstant(Magic, dl, IntVT));
7529 }
7530
7531 if (VT != Value.getValueType() && !VT.isInteger())
7532 Value = DAG.getBitcast(VT.getScalarType(), Value);
7533 if (VT != Value.getValueType())
7534 Value = DAG.getSplatBuildVector(VT, dl, Value);
7535
7536 return Value;
7537}
7538
7539/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
7540/// used when a memcpy is turned into a memset when the source is a constant
7541/// string ptr.
7543 const TargetLowering &TLI,
7544 const ConstantDataArraySlice &Slice) {
7545 // Handle vector with all elements zero.
7546 if (Slice.Array == nullptr) {
7547 if (VT.isInteger())
7548 return DAG.getConstant(0, dl, VT);
7549 if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
7550 return DAG.getConstantFP(0.0, dl, VT);
7551 if (VT.isVector()) {
7552 unsigned NumElts = VT.getVectorNumElements();
7553 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
7554 return DAG.getNode(ISD::BITCAST, dl, VT,
7555 DAG.getConstant(0, dl,
7557 EltVT, NumElts)));
7558 }
7559 llvm_unreachable("Expected type!");
7560 }
7561
7562 assert(!VT.isVector() && "Can't handle vector type here!");
7563 unsigned NumVTBits = VT.getSizeInBits();
7564 unsigned NumVTBytes = NumVTBits / 8;
7565 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
7566
7567 APInt Val(NumVTBits, 0);
7568 if (DAG.getDataLayout().isLittleEndian()) {
7569 for (unsigned i = 0; i != NumBytes; ++i)
7570 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
7571 } else {
7572 for (unsigned i = 0; i != NumBytes; ++i)
7573 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
7574 }
7575
7576 // If the "cost" of materializing the integer immediate is less than the cost
7577 // of a load, then it is cost effective to turn the load into the immediate.
7578 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
7579 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
7580 return DAG.getConstant(Val, dl, VT);
7581 return SDValue();
7582}
7583
7585 const SDLoc &DL,
7586 const SDNodeFlags Flags) {
7587 EVT VT = Base.getValueType();
7588 SDValue Index;
7589
7590 if (Offset.isScalable())
7591 Index = getVScale(DL, Base.getValueType(),
7592 APInt(Base.getValueSizeInBits().getFixedValue(),
7593 Offset.getKnownMinValue()));
7594 else
7595 Index = getConstant(Offset.getFixedValue(), DL, VT);
7596
7597 return getMemBasePlusOffset(Base, Index, DL, Flags);
7598}
7599
7601 const SDLoc &DL,
7602 const SDNodeFlags Flags) {
7603 assert(Offset.getValueType().isInteger());
7604 EVT BasePtrVT = Ptr.getValueType();
7605 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, Flags);
7606}
7607
7608/// Returns true if memcpy source is constant data.
7610 uint64_t SrcDelta = 0;
7611 GlobalAddressSDNode *G = nullptr;
7612 if (Src.getOpcode() == ISD::GlobalAddress)
7613 G = cast<GlobalAddressSDNode>(Src);
7614 else if (Src.getOpcode() == ISD::ADD &&
7615 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
7616 Src.getOperand(1).getOpcode() == ISD::Constant) {
7617 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
7618 SrcDelta = Src.getConstantOperandVal(1);
7619 }
7620 if (!G)
7621 return false;
7622
7623 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
7624 SrcDelta + G->getOffset());
7625}
7626
7628 SelectionDAG &DAG) {
7629 // On Darwin, -Os means optimize for size without hurting performance, so
7630 // only really optimize for size when -Oz (MinSize) is used.
7632 return MF.getFunction().hasMinSize();
7633 return DAG.shouldOptForSize();
7634}
7635
7637 SmallVector<SDValue, 32> &OutChains, unsigned From,
7638 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
7639 SmallVector<SDValue, 16> &OutStoreChains) {
7640 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
7641 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
7642 SmallVector<SDValue, 16> GluedLoadChains;
7643 for (unsigned i = From; i < To; ++i) {
7644 OutChains.push_back(OutLoadChains[i]);
7645 GluedLoadChains.push_back(OutLoadChains[i]);
7646 }
7647
7648 // Chain for all loads.
7649 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7650 GluedLoadChains);
7651
7652 for (unsigned i = From; i < To; ++i) {
7653 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
7654 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
7655 ST->getBasePtr(), ST->getMemoryVT(),
7656 ST->getMemOperand());
7657 OutChains.push_back(NewStore);
7658 }
7659}
7660
7662 SDValue Chain, SDValue Dst, SDValue Src,
7663 uint64_t Size, Align Alignment,
7664 bool isVol, bool AlwaysInline,
7665 MachinePointerInfo DstPtrInfo,
7666 MachinePointerInfo SrcPtrInfo,
7667 const AAMDNodes &AAInfo, AAResults *AA) {
7668 // Turn a memcpy of undef to nop.
7669 // FIXME: We need to honor volatile even is Src is undef.
7670 if (Src.isUndef())
7671 return Chain;
7672
7673 // Expand memcpy to a series of load and store ops if the size operand falls
7674 // below a certain threshold.
7675 // TODO: In the AlwaysInline case, if the size is big then generate a loop
7676 // rather than maybe a humongous number of loads and stores.
7677 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7678 const DataLayout &DL = DAG.getDataLayout();
7679 LLVMContext &C = *DAG.getContext();
7680 std::vector<EVT> MemOps;
7681 bool DstAlignCanChange = false;
7683 MachineFrameInfo &MFI = MF.getFrameInfo();
7684 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
7685 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
7686 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
7687 DstAlignCanChange = true;
7688 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
7689 if (!SrcAlign || Alignment > *SrcAlign)
7690 SrcAlign = Alignment;
7691 assert(SrcAlign && "SrcAlign must be set");
7693 // If marked as volatile, perform a copy even when marked as constant.
7694 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
7695 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
7696 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
7697 const MemOp Op = isZeroConstant
7698 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
7699 /*IsZeroMemset*/ true, isVol)
7700 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
7701 *SrcAlign, isVol, CopyFromConstant);
7702 if (!TLI.findOptimalMemOpLowering(
7703 MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
7704 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
7705 return SDValue();
7706
7707 if (DstAlignCanChange) {
7708 Type *Ty = MemOps[0].getTypeForEVT(C);
7709 Align NewAlign = DL.getABITypeAlign(Ty);
7710
7711 // Don't promote to an alignment that would require dynamic stack
7712 // realignment which may conflict with optimizations such as tail call
7713 // optimization.
7715 if (!TRI->hasStackRealignment(MF))
7716 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
7717 NewAlign = NewAlign.previous();
7718
7719 if (NewAlign > Alignment) {
7720 // Give the stack frame object a larger alignment if needed.
7721 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
7722 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
7723 Alignment = NewAlign;
7724 }
7725 }
7726
7727 // Prepare AAInfo for loads/stores after lowering this memcpy.
7728 AAMDNodes NewAAInfo = AAInfo;
7729 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
7730
7731 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
7732 bool isConstant =
7733 AA && SrcVal &&
7734 AA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
7735
7736 MachineMemOperand::Flags MMOFlags =
7738 SmallVector<SDValue, 16> OutLoadChains;
7739 SmallVector<SDValue, 16> OutStoreChains;
7740 SmallVector<SDValue, 32> OutChains;
7741 unsigned NumMemOps = MemOps.size();
7742 uint64_t SrcOff = 0, DstOff = 0;
7743 for (unsigned i = 0; i != NumMemOps; ++i) {
7744 EVT VT = MemOps[i];
7745 unsigned VTSize = VT.getSizeInBits() / 8;
7746 SDValue Value, Store;
7747
7748 if (VTSize > Size) {
7749 // Issuing an unaligned load / store pair that overlaps with the previous
7750 // pair. Adjust the offset accordingly.
7751 assert(i == NumMemOps-1 && i != 0);
7752 SrcOff -= VTSize - Size;
7753 DstOff -= VTSize - Size;
7754 }
7755
7756 if (CopyFromConstant &&
7757 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
7758 // It's unlikely a store of a vector immediate can be done in a single
7759 // instruction. It would require a load from a constantpool first.
7760 // We only handle zero vectors here.
7761 // FIXME: Handle other cases where store of vector immediate is done in
7762 // a single instruction.
7763 ConstantDataArraySlice SubSlice;
7764 if (SrcOff < Slice.Length) {
7765 SubSlice = Slice;
7766 SubSlice.move(SrcOff);
7767 } else {
7768 // This is an out-of-bounds access and hence UB. Pretend we read zero.
7769 SubSlice.Array = nullptr;
7770 SubSlice.Offset = 0;
7771 SubSlice.Length = VTSize;
7772 }
7773 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
7774 if (Value.getNode()) {
7775 Store = DAG.getStore(
7776 Chain, dl, Value,
7777 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7778 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
7779 OutChains.push_back(Store);
7780 }
7781 }
7782
7783 if (!Store.getNode()) {
7784 // The type might not be legal for the target. This should only happen
7785 // if the type is smaller than a legal type, as on PPC, so the right
7786 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
7787 // to Load/Store if NVT==VT.
7788 // FIXME does the case above also need this?
7789 EVT NVT = TLI.getTypeToTransformTo(C, VT);
7790 assert(NVT.bitsGE(VT));
7791
7792 bool isDereferenceable =
7793 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
7794 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
7795 if (isDereferenceable)
7797 if (isConstant)
7798 SrcMMOFlags |= MachineMemOperand::MOInvariant;
7799
7800 Value = DAG.getExtLoad(
7801 ISD::EXTLOAD, dl, NVT, Chain,
7802 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
7803 SrcPtrInfo.getWithOffset(SrcOff), VT,
7804 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
7805 OutLoadChains.push_back(Value.getValue(1));
7806
7807 Store = DAG.getTruncStore(
7808 Chain, dl, Value,
7809 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7810 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
7811 OutStoreChains.push_back(Store);
7812 }
7813 SrcOff += VTSize;
7814 DstOff += VTSize;
7815 Size -= VTSize;
7816 }
7817
7818 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
7820 unsigned NumLdStInMemcpy = OutStoreChains.size();
7821
7822 if (NumLdStInMemcpy) {
7823 // It may be that memcpy might be converted to memset if it's memcpy
7824 // of constants. In such a case, we won't have loads and stores, but
7825 // just stores. In the absence of loads, there is nothing to gang up.
7826 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
7827 // If target does not care, just leave as it.
7828 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
7829 OutChains.push_back(OutLoadChains[i]);
7830 OutChains.push_back(OutStoreChains[i]);
7831 }
7832 } else {
7833 // Ld/St less than/equal limit set by target.
7834 if (NumLdStInMemcpy <= GluedLdStLimit) {
7835 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
7836 NumLdStInMemcpy, OutLoadChains,
7837 OutStoreChains);
7838 } else {
7839 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
7840 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
7841 unsigned GlueIter = 0;
7842
7843 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
7844 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
7845 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
7846
7847 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
7848 OutLoadChains, OutStoreChains);
7849 GlueIter += GluedLdStLimit;
7850 }
7851
7852 // Residual ld/st.
7853 if (RemainingLdStInMemcpy) {
7854 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
7855 RemainingLdStInMemcpy, OutLoadChains,
7856 OutStoreChains);
7857 }
7858 }
7859 }
7860 }
7861 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
7862}
7863
7865 SDValue Chain, SDValue Dst, SDValue Src,
7866 uint64_t Size, Align Alignment,
7867 bool isVol, bool AlwaysInline,
7868 MachinePointerInfo DstPtrInfo,
7869 MachinePointerInfo SrcPtrInfo,
7870 const AAMDNodes &AAInfo) {
7871 // Turn a memmove of undef to nop.
7872 // FIXME: We need to honor volatile even is Src is undef.
7873 if (Src.isUndef())
7874 return Chain;
7875
7876 // Expand memmove to a series of load and store ops if the size operand falls
7877 // below a certain threshold.
7878 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7879 const DataLayout &DL = DAG.getDataLayout();
7880 LLVMContext &C = *DAG.getContext();
7881 std::vector<EVT> MemOps;
7882 bool DstAlignCanChange = false;
7884 MachineFrameInfo &MFI = MF.getFrameInfo();
7885 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
7886 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
7887 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
7888 DstAlignCanChange = true;
7889 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
7890 if (!SrcAlign || Alignment > *SrcAlign)
7891 SrcAlign = Alignment;
7892 assert(SrcAlign && "SrcAlign must be set");
7893 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
7894 if (!TLI.findOptimalMemOpLowering(
7895 MemOps, Limit,
7896 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
7897 /*IsVolatile*/ true),
7898 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
7899 MF.getFunction().getAttributes()))
7900 return SDValue();
7901
7902 if (DstAlignCanChange) {
7903 Type *Ty = MemOps[0].getTypeForEVT(C);
7904 Align NewAlign = DL.getABITypeAlign(Ty);
7905
7906 // Don't promote to an alignment that would require dynamic stack
7907 // realignment which may conflict with optimizations such as tail call
7908 // optimization.
7910 if (!TRI->hasStackRealignment(MF))
7911 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
7912 NewAlign = NewAlign.previous();
7913
7914 if (NewAlign > Alignment) {
7915 // Give the stack frame object a larger alignment if needed.
7916 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
7917 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
7918 Alignment = NewAlign;
7919 }
7920 }
7921
7922 // Prepare AAInfo for loads/stores after lowering this memmove.
7923 AAMDNodes NewAAInfo = AAInfo;
7924 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
7925
7926 MachineMemOperand::Flags MMOFlags =
7928 uint64_t SrcOff = 0, DstOff = 0;
7929 SmallVector<SDValue, 8> LoadValues;
7930 SmallVector<SDValue, 8> LoadChains;
7931 SmallVector<SDValue, 8> OutChains;
7932 unsigned NumMemOps = MemOps.size();
7933 for (unsigned i = 0; i < NumMemOps; i++) {
7934 EVT VT = MemOps[i];
7935 unsigned VTSize = VT.getSizeInBits() / 8;
7936 SDValue Value;
7937
7938 bool isDereferenceable =
7939 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
7940 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
7941 if (isDereferenceable)
7943
7944 Value = DAG.getLoad(
7945 VT, dl, Chain,
7946 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
7947 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
7948 LoadValues.push_back(Value);
7949 LoadChains.push_back(Value.getValue(1));
7950 SrcOff += VTSize;
7951 }
7952 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
7953 OutChains.clear();
7954 for (unsigned i = 0; i < NumMemOps; i++) {
7955 EVT VT = MemOps[i];
7956 unsigned VTSize = VT.getSizeInBits() / 8;
7957 SDValue Store;
7958
7959 Store = DAG.getStore(
7960 Chain, dl, LoadValues[i],
7961 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7962 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
7963 OutChains.push_back(Store);
7964 DstOff += VTSize;
7965 }
7966
7967 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
7968}
7969
7970/// Lower the call to 'memset' intrinsic function into a series of store
7971/// operations.
7972///
7973/// \param DAG Selection DAG where lowered code is placed.
7974/// \param dl Link to corresponding IR location.
7975/// \param Chain Control flow dependency.
7976/// \param Dst Pointer to destination memory location.
7977/// \param Src Value of byte to write into the memory.
7978/// \param Size Number of bytes to write.
7979/// \param Alignment Alignment of the destination in bytes.
7980/// \param isVol True if destination is volatile.
7981/// \param AlwaysInline Makes sure no function call is generated.
7982/// \param DstPtrInfo IR information on the memory pointer.
7983/// \returns New head in the control flow, if lowering was successful, empty
7984/// SDValue otherwise.
7985///
7986/// The function tries to replace 'llvm.memset' intrinsic with several store
7987/// operations and value calculation code. This is usually profitable for small
7988/// memory size or when the semantic requires inlining.
7990 SDValue Chain, SDValue Dst, SDValue Src,
7991 uint64_t Size, Align Alignment, bool isVol,
7992 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
7993 const AAMDNodes &AAInfo) {
7994 // Turn a memset of undef to nop.
7995 // FIXME: We need to honor volatile even is Src is undef.
7996 if (Src.isUndef())
7997 return Chain;
7998
7999 // Expand memset to a series of load/store ops if the size operand
8000 // falls below a certain threshold.
8001 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8002 std::vector<EVT> MemOps;
8003 bool DstAlignCanChange = false;
8005 MachineFrameInfo &MFI = MF.getFrameInfo();
8006 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8007 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8008 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8009 DstAlignCanChange = true;
8010 bool IsZeroVal = isNullConstant(Src);
8011 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
8012
8013 if (!TLI.findOptimalMemOpLowering(
8014 MemOps, Limit,
8015 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
8016 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
8017 return SDValue();
8018
8019 if (DstAlignCanChange) {
8020 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
8021 const DataLayout &DL = DAG.getDataLayout();
8022 Align NewAlign = DL.getABITypeAlign(Ty);
8023
8024 // Don't promote to an alignment that would require dynamic stack
8025 // realignment which may conflict with optimizations such as tail call
8026 // optimization.
8028 if (!TRI->hasStackRealignment(MF))
8029 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
8030 NewAlign = NewAlign.previous();
8031
8032 if (NewAlign > Alignment) {
8033 // Give the stack frame object a larger alignment if needed.
8034 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8035 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8036 Alignment = NewAlign;
8037 }
8038 }
8039
8040 SmallVector<SDValue, 8> OutChains;
8041 uint64_t DstOff = 0;
8042 unsigned NumMemOps = MemOps.size();
8043
8044 // Find the largest store and generate the bit pattern for it.
8045 EVT LargestVT = MemOps[0];
8046 for (unsigned i = 1; i < NumMemOps; i++)
8047 if (MemOps[i].bitsGT(LargestVT))
8048 LargestVT = MemOps[i];
8049 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
8050
8051 // Prepare AAInfo for loads/stores after lowering this memset.
8052 AAMDNodes NewAAInfo = AAInfo;
8053 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8054
8055 for (unsigned i = 0; i < NumMemOps; i++) {
8056 EVT VT = MemOps[i];
8057 unsigned VTSize = VT.getSizeInBits() / 8;
8058 if (VTSize > Size) {
8059 // Issuing an unaligned load / store pair that overlaps with the previous
8060 // pair. Adjust the offset accordingly.
8061 assert(i == NumMemOps-1 && i != 0);
8062 DstOff -= VTSize - Size;
8063 }
8064
8065 // If this store is smaller than the largest store see whether we can get
8066 // the smaller value for free with a truncate or extract vector element and
8067 // then store.
8068 SDValue Value = MemSetValue;
8069 if (VT.bitsLT(LargestVT)) {
8070 unsigned Index;
8071 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
8072 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
8073 if (!LargestVT.isVector() && !VT.isVector() &&
8074 TLI.isTruncateFree(LargestVT, VT))
8075 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
8076 else if (LargestVT.isVector() && !VT.isVector() &&
8078 LargestVT.getTypeForEVT(*DAG.getContext()),
8079 VT.getSizeInBits(), Index) &&
8080 TLI.isTypeLegal(SVT) &&
8081 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
8082 // Target which can combine store(extractelement VectorTy, Idx) can get
8083 // the smaller value for free.
8084 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
8085 Value = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, TailValue,
8086 DAG.getVectorIdxConstant(Index, dl));
8087 } else
8088 Value = getMemsetValue(Src, VT, DAG, dl);
8089 }
8090 assert(Value.getValueType() == VT && "Value with wrong type.");
8091 SDValue Store = DAG.getStore(
8092 Chain, dl, Value,
8093 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8094 DstPtrInfo.getWithOffset(DstOff), Alignment,
8096 NewAAInfo);
8097 OutChains.push_back(Store);
8098 DstOff += VT.getSizeInBits() / 8;
8099 Size -= VTSize;
8100 }
8101
8102 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8103}
8104
8106 unsigned AS) {
8107 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
8108 // pointer operands can be losslessly bitcasted to pointers of address space 0
8109 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
8110 report_fatal_error("cannot lower memory intrinsic in address space " +
8111 Twine(AS));
8112 }
8113}
8114
8116 SDValue Src, SDValue Size, Align Alignment,
8117 bool isVol, bool AlwaysInline, bool isTailCall,
8118 MachinePointerInfo DstPtrInfo,
8119 MachinePointerInfo SrcPtrInfo,
8120 const AAMDNodes &AAInfo, AAResults *AA) {
8121 // Check to see if we should lower the memcpy to loads and stores first.
8122 // For cases within the target-specified limits, this is the best choice.
8123 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8124 if (ConstantSize) {
8125 // Memcpy with size zero? Just return the original chain.
8126 if (ConstantSize->isZero())
8127 return Chain;
8128
8130 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8131 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8132 if (Result.getNode())
8133 return Result;
8134 }
8135
8136 // Then check to see if we should lower the memcpy with target-specific
8137 // code. If the target chooses to do this, this is the next best.
8138 if (TSI) {
8139 SDValue Result = TSI->EmitTargetCodeForMemcpy(
8140 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
8141 DstPtrInfo, SrcPtrInfo);
8142 if (Result.getNode())
8143 return Result;
8144 }
8145
8146 // If we really need inline code and the target declined to provide it,
8147 // use a (potentially long) sequence of loads and stores.
8148 if (AlwaysInline) {
8149 assert(ConstantSize && "AlwaysInline requires a constant size!");
8151 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8152 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8153 }
8154
8157
8158 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
8159 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
8160 // respect volatile, so they may do things like read or write memory
8161 // beyond the given memory regions. But fixing this isn't easy, and most
8162 // people don't care.
8163
8164 // Emit a library call.
8167 Entry.Ty = PointerType::getUnqual(*getContext());
8168 Entry.Node = Dst; Args.push_back(Entry);
8169 Entry.Node = Src; Args.push_back(Entry);
8170
8171 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8172 Entry.Node = Size; Args.push_back(Entry);
8173 // FIXME: pass in SDLoc
8175 CLI.setDebugLoc(dl)
8176 .setChain(Chain)
8177 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
8178 Dst.getValueType().getTypeForEVT(*getContext()),
8179 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
8180 TLI->getPointerTy(getDataLayout())),
8181 std::move(Args))
8183 .setTailCall(isTailCall);
8184
8185 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8186 return CallResult.second;
8187}
8188
8190 SDValue Dst, SDValue Src, SDValue Size,
8191 Type *SizeTy, unsigned ElemSz,
8192 bool isTailCall,
8193 MachinePointerInfo DstPtrInfo,
8194 MachinePointerInfo SrcPtrInfo) {
8195 // Emit a library call.
8198 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8199 Entry.Node = Dst;
8200 Args.push_back(Entry);
8201
8202 Entry.Node = Src;
8203 Args.push_back(Entry);
8204
8205 Entry.Ty = SizeTy;
8206 Entry.Node = Size;
8207 Args.push_back(Entry);
8208
8209 RTLIB::Libcall LibraryCall =
8211 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8212 report_fatal_error("Unsupported element size");
8213
8215 CLI.setDebugLoc(dl)
8216 .setChain(Chain)
8217 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8219 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8220 TLI->getPointerTy(getDataLayout())),
8221 std::move(Args))
8223 .setTailCall(isTailCall);
8224
8225 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8226 return CallResult.second;
8227}
8228
8230 SDValue Src, SDValue Size, Align Alignment,
8231 bool isVol, bool isTailCall,
8232 MachinePointerInfo DstPtrInfo,
8233 MachinePointerInfo SrcPtrInfo,
8234 const AAMDNodes &AAInfo, AAResults *AA) {
8235 // Check to see if we should lower the memmove to loads and stores first.
8236 // For cases within the target-specified limits, this is the best choice.
8237 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8238 if (ConstantSize) {
8239 // Memmove with size zero? Just return the original chain.
8240 if (ConstantSize->isZero())
8241 return Chain;
8242
8244 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8245 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
8246 if (Result.getNode())
8247 return Result;
8248 }
8249
8250 // Then check to see if we should lower the memmove with target-specific
8251 // code. If the target chooses to do this, this is the next best.
8252 if (TSI) {
8253 SDValue Result =
8254 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
8255 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
8256 if (Result.getNode())
8257 return Result;
8258 }
8259
8262
8263 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
8264 // not be safe. See memcpy above for more details.
8265
8266 // Emit a library call.
8269 Entry.Ty = PointerType::getUnqual(*getContext());
8270 Entry.Node = Dst; Args.push_back(Entry);
8271 Entry.Node = Src; Args.push_back(Entry);
8272
8273 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8274 Entry.Node = Size; Args.push_back(Entry);
8275 // FIXME: pass in SDLoc
8277 CLI.setDebugLoc(dl)
8278 .setChain(Chain)
8279 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
8280 Dst.getValueType().getTypeForEVT(*getContext()),
8281 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
8282 TLI->getPointerTy(getDataLayout())),
8283 std::move(Args))
8285 .setTailCall(isTailCall);
8286
8287 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8288 return CallResult.second;
8289}
8290
8292 SDValue Dst, SDValue Src, SDValue Size,
8293 Type *SizeTy, unsigned ElemSz,
8294 bool isTailCall,
8295 MachinePointerInfo DstPtrInfo,
8296 MachinePointerInfo SrcPtrInfo) {
8297 // Emit a library call.
8300 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8301 Entry.Node = Dst;
8302 Args.push_back(Entry);
8303
8304 Entry.Node = Src;
8305 Args.push_back(Entry);
8306
8307 Entry.Ty = SizeTy;
8308 Entry.Node = Size;
8309 Args.push_back(Entry);
8310
8311 RTLIB::Libcall LibraryCall =
8313 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8314 report_fatal_error("Unsupported element size");
8315
8317 CLI.setDebugLoc(dl)
8318 .setChain(Chain)
8319 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8321 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8322 TLI->getPointerTy(getDataLayout())),
8323 std::move(Args))
8325 .setTailCall(isTailCall);
8326
8327 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8328 return CallResult.second;
8329}
8330
8332 SDValue Src, SDValue Size, Align Alignment,
8333 bool isVol, bool AlwaysInline, bool isTailCall,
8334 MachinePointerInfo DstPtrInfo,
8335 const AAMDNodes &AAInfo) {
8336 // Check to see if we should lower the memset to stores first.
8337 // For cases within the target-specified limits, this is the best choice.
8338 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8339 if (ConstantSize) {
8340 // Memset with size zero? Just return the original chain.
8341 if (ConstantSize->isZero())
8342 return Chain;
8343
8344 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8345 ConstantSize->getZExtValue(), Alignment,
8346 isVol, false, DstPtrInfo, AAInfo);
8347
8348 if (Result.getNode())
8349 return Result;
8350 }
8351
8352 // Then check to see if we should lower the memset with target-specific
8353 // code. If the target chooses to do this, this is the next best.
8354 if (TSI) {
8355 SDValue Result = TSI->EmitTargetCodeForMemset(
8356 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
8357 if (Result.getNode())
8358 return Result;
8359 }
8360
8361 // If we really need inline code and the target declined to provide it,
8362 // use a (potentially long) sequence of loads and stores.
8363 if (AlwaysInline) {
8364 assert(ConstantSize && "AlwaysInline requires a constant size!");
8365 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8366 ConstantSize->getZExtValue(), Alignment,
8367 isVol, true, DstPtrInfo, AAInfo);
8368 assert(Result &&
8369 "getMemsetStores must return a valid sequence when AlwaysInline");
8370 return Result;
8371 }
8372
8374
8375 // Emit a library call.
8376 auto &Ctx = *getContext();
8377 const auto& DL = getDataLayout();
8378
8380 // FIXME: pass in SDLoc
8381 CLI.setDebugLoc(dl).setChain(Chain);
8382
8383 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
8384
8385 // Helper function to create an Entry from Node and Type.
8386 const auto CreateEntry = [](SDValue Node, Type *Ty) {
8388 Entry.Node = Node;
8389 Entry.Ty = Ty;
8390 return Entry;
8391 };
8392
8393 // If zeroing out and bzero is present, use it.
8394 if (isNullConstant(Src) && BzeroName) {
8396 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8397 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8398 CLI.setLibCallee(
8399 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
8400 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
8401 } else {
8403 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8404 Args.push_back(CreateEntry(Src, Src.getValueType().getTypeForEVT(Ctx)));
8405 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8406 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
8407 Dst.getValueType().getTypeForEVT(Ctx),
8408 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
8409 TLI->getPointerTy(DL)),
8410 std::move(Args));
8411 }
8412
8413 CLI.setDiscardResult().setTailCall(isTailCall);
8414
8415 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8416 return CallResult.second;
8417}
8418
8421 Type *SizeTy, unsigned ElemSz,
8422 bool isTailCall,
8423 MachinePointerInfo DstPtrInfo) {
8424 // Emit a library call.
8427 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8428 Entry.Node = Dst;
8429 Args.push_back(Entry);
8430
8431 Entry.Ty = Type::getInt8Ty(*getContext());
8432 Entry.Node = Value;
8433 Args.push_back(Entry);
8434
8435 Entry.Ty = SizeTy;
8436 Entry.Node = Size;
8437 Args.push_back(Entry);
8438
8439 RTLIB::Libcall LibraryCall =
8441 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8442 report_fatal_error("Unsupported element size");
8443
8445 CLI.setDebugLoc(dl)
8446 .setChain(Chain)
8447 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8449 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8450 TLI->getPointerTy(getDataLayout())),
8451 std::move(Args))
8453 .setTailCall(isTailCall);
8454
8455 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8456 return CallResult.second;
8457}
8458
8459SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8460 SDVTList VTList, ArrayRef<SDValue> Ops,
8461 MachineMemOperand *MMO) {
8463 ID.AddInteger(MemVT.getRawBits());
8464 AddNodeIDNode(ID, Opcode, VTList, Ops);
8465 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8466 ID.AddInteger(MMO->getFlags());
8467 void* IP = nullptr;
8468 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8469 cast<AtomicSDNode>(E)->refineAlignment(MMO);
8470 return SDValue(E, 0);
8471 }
8472
8473 auto *N = newSDNode<AtomicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8474 VTList, MemVT, MMO);
8475 createOperands(N, Ops);
8476
8477 CSEMap.InsertNode(N, IP);
8478 InsertNode(N);
8479 return SDValue(N, 0);
8480}
8481
8483 EVT MemVT, SDVTList VTs, SDValue Chain,
8484 SDValue Ptr, SDValue Cmp, SDValue Swp,
8485 MachineMemOperand *MMO) {
8486 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
8488 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
8489
8490 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
8491 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8492}
8493
8494SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8495 SDValue Chain, SDValue Ptr, SDValue Val,
8496 MachineMemOperand *MMO) {
8497 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
8498 Opcode == ISD::ATOMIC_LOAD_SUB ||
8499 Opcode == ISD::ATOMIC_LOAD_AND ||
8500 Opcode == ISD::ATOMIC_LOAD_CLR ||
8501 Opcode == ISD::ATOMIC_LOAD_OR ||
8502 Opcode == ISD::ATOMIC_LOAD_XOR ||
8503 Opcode == ISD::ATOMIC_LOAD_NAND ||
8504 Opcode == ISD::ATOMIC_LOAD_MIN ||
8505 Opcode == ISD::ATOMIC_LOAD_MAX ||
8506 Opcode == ISD::ATOMIC_LOAD_UMIN ||
8507 Opcode == ISD::ATOMIC_LOAD_UMAX ||
8508 Opcode == ISD::ATOMIC_LOAD_FADD ||
8509 Opcode == ISD::ATOMIC_LOAD_FSUB ||
8510 Opcode == ISD::ATOMIC_LOAD_FMAX ||
8511 Opcode == ISD::ATOMIC_LOAD_FMIN ||
8512 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
8513 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
8514 Opcode == ISD::ATOMIC_SWAP ||
8515 Opcode == ISD::ATOMIC_STORE) &&
8516 "Invalid Atomic Op");
8517
8518 EVT VT = Val.getValueType();
8519
8520 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
8521 getVTList(VT, MVT::Other);
8522 SDValue Ops[] = {Chain, Ptr, Val};
8523 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8524}
8525
8526SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8527 EVT VT, SDValue Chain, SDValue Ptr,
8528 MachineMemOperand *MMO) {
8529 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
8530
8531 SDVTList VTs = getVTList(VT, MVT::Other);
8532 SDValue Ops[] = {Chain, Ptr};
8533 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8534}
8535
8536/// getMergeValues - Create a MERGE_VALUES node from the given operands.
8538 if (Ops.size() == 1)
8539 return Ops[0];
8540
8542 VTs.reserve(Ops.size());
8543 for (const SDValue &Op : Ops)
8544 VTs.push_back(Op.getValueType());
8545 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
8546}
8547
8549 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
8550 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
8552 const AAMDNodes &AAInfo) {
8553 if (Size.hasValue() && !Size.getValue())
8555
8557 MachineMemOperand *MMO =
8558 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
8559
8560 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
8561}
8562
8564 SDVTList VTList,
8565 ArrayRef<SDValue> Ops, EVT MemVT,
8566 MachineMemOperand *MMO) {
8567 assert((Opcode == ISD::INTRINSIC_VOID ||
8568 Opcode == ISD::INTRINSIC_W_CHAIN ||
8569 Opcode == ISD::PREFETCH ||
8570 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
8571 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
8572 "Opcode is not a memory-accessing opcode!");
8573
8574 // Memoize the node unless it returns a glue result.
8576 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
8578 AddNodeIDNode(ID, Opcode, VTList, Ops);
8579 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
8580 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
8581 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8582 ID.AddInteger(MMO->getFlags());
8583 ID.AddInteger(MemVT.getRawBits());
8584 void *IP = nullptr;
8585 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8586 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
8587 return SDValue(E, 0);
8588 }
8589
8590 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8591 VTList, MemVT, MMO);
8592 createOperands(N, Ops);
8593
8594 CSEMap.InsertNode(N, IP);
8595 } else {
8596 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8597 VTList, MemVT, MMO);
8598 createOperands(N, Ops);
8599 }
8600 InsertNode(N);
8601 SDValue V(N, 0);
8602 NewSDValueDbgMsg(V, "Creating new node: ", this);
8603 return V;
8604}
8605
8607 SDValue Chain, int FrameIndex,
8608 int64_t Size, int64_t Offset) {
8609 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
8610 const auto VTs = getVTList(MVT::Other);
8611 SDValue Ops[2] = {
8612 Chain,
8613 getFrameIndex(FrameIndex,
8614 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
8615 true)};
8616
8618 AddNodeIDNode(ID, Opcode, VTs, Ops);
8619 ID.AddInteger(FrameIndex);
8620 ID.AddInteger(Size);
8621 ID.AddInteger(Offset);
8622 void *IP = nullptr;
8623 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
8624 return SDValue(E, 0);
8625
8626 LifetimeSDNode *N = newSDNode<LifetimeSDNode>(
8627 Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs, Size, Offset);
8628 createOperands(N, Ops);
8629 CSEMap.InsertNode(N, IP);
8630 InsertNode(N);
8631 SDValue V(N, 0);
8632 NewSDValueDbgMsg(V, "Creating new node: ", this);
8633 return V;
8634}
8635
8637 uint64_t Guid, uint64_t Index,
8638 uint32_t Attr) {
8639 const unsigned Opcode = ISD::PSEUDO_PROBE;
8640 const auto VTs = getVTList(MVT::Other);
8641 SDValue Ops[] = {Chain};
8643 AddNodeIDNode(ID, Opcode, VTs, Ops);
8644 ID.AddInteger(Guid);
8645 ID.AddInteger(Index);
8646 void *IP = nullptr;
8647 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
8648 return SDValue(E, 0);
8649
8650 auto *N = newSDNode<PseudoProbeSDNode>(
8651 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
8652 createOperands(N, Ops);
8653 CSEMap.InsertNode(N, IP);
8654 InsertNode(N);
8655 SDValue V(N, 0);
8656 NewSDValueDbgMsg(V, "Creating new node: ", this);
8657 return V;
8658}
8659
8660/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
8661/// MachinePointerInfo record from it. This is particularly useful because the
8662/// code generator has many cases where it doesn't bother passing in a
8663/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
8665 SelectionDAG &DAG, SDValue Ptr,
8666 int64_t Offset = 0) {
8667 // If this is FI+Offset, we can model it.
8668 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
8670 FI->getIndex(), Offset);
8671
8672 // If this is (FI+Offset1)+Offset2, we can model it.
8673 if (Ptr.getOpcode() != ISD::ADD ||
8674 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
8675 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
8676 return Info;
8677
8678 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
8680 DAG.getMachineFunction(), FI,
8681 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
8682}
8683
8684/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
8685/// MachinePointerInfo record from it. This is particularly useful because the
8686/// code generator has many cases where it doesn't bother passing in a
8687/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
8689 SelectionDAG &DAG, SDValue Ptr,
8690 SDValue OffsetOp) {
8691 // If the 'Offset' value isn't a constant, we can't handle this.
8692 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
8693 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
8694 if (OffsetOp.isUndef())
8695 return InferPointerInfo(Info, DAG, Ptr);
8696 return Info;
8697}
8698
8700 EVT VT, const SDLoc &dl, SDValue Chain,
8702 MachinePointerInfo PtrInfo, EVT MemVT,
8703 Align Alignment,
8704 MachineMemOperand::Flags MMOFlags,
8705 const AAMDNodes &AAInfo, const MDNode *Ranges) {
8706 assert(Chain.getValueType() == MVT::Other &&
8707 "Invalid chain type");
8708
8709 MMOFlags |= MachineMemOperand::MOLoad;
8710 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
8711 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
8712 // clients.
8713 if (PtrInfo.V.isNull())
8714 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
8715
8718 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
8719 Alignment, AAInfo, Ranges);
8720 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
8721}
8722
8724 EVT VT, const SDLoc &dl, SDValue Chain,
8725 SDValue Ptr, SDValue Offset, EVT MemVT,
8726 MachineMemOperand *MMO) {
8727 if (VT == MemVT) {
8728 ExtType = ISD::NON_EXTLOAD;
8729 } else if (ExtType == ISD::NON_EXTLOAD) {
8730 assert(VT == MemVT && "Non-extending load from different memory type!");
8731 } else {
8732 // Extending load.
8733 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
8734 "Should only be an extending load, not truncating!");
8735 assert(VT.isInteger() == MemVT.isInteger() &&
8736 "Cannot convert from FP to Int or Int -> FP!");
8737 assert(VT.isVector() == MemVT.isVector() &&
8738 "Cannot use an ext load to convert to or from a vector!");
8739 assert((!VT.isVector() ||
8741 "Cannot use an ext load to change the number of vector elements!");
8742 }
8743
8744 bool Indexed = AM != ISD::UNINDEXED;
8745 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
8746
8747 SDVTList VTs = Indexed ?
8748 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
8749 SDValue Ops[] = { Chain, Ptr, Offset };
8751 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
8752 ID.AddInteger(MemVT.getRawBits());
8753 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
8754 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
8755 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8756 ID.AddInteger(MMO->getFlags());
8757 void *IP = nullptr;
8758 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8759 cast<LoadSDNode>(E)->refineAlignment(MMO);
8760 return SDValue(E, 0);
8761 }
8762 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
8763 ExtType, MemVT, MMO);
8764 createOperands(N, Ops);
8765
8766 CSEMap.InsertNode(N, IP);
8767 InsertNode(N);
8768 SDValue V(N, 0);
8769 NewSDValueDbgMsg(V, "Creating new node: ", this);
8770 return V;
8771}
8772
8775 MaybeAlign Alignment,
8776 MachineMemOperand::Flags MMOFlags,
8777 const AAMDNodes &AAInfo, const MDNode *Ranges) {
8778 SDValue Undef = getUNDEF(Ptr.getValueType());
8779 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8780 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
8781}
8782
8785 SDValue Undef = getUNDEF(Ptr.getValueType());
8786 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8787 VT, MMO);
8788}
8789
8791 EVT VT, SDValue Chain, SDValue Ptr,
8792 MachinePointerInfo PtrInfo, EVT MemVT,
8793 MaybeAlign Alignment,
8794 MachineMemOperand::Flags MMOFlags,
8795 const AAMDNodes &AAInfo) {
8796 SDValue Undef = getUNDEF(Ptr.getValueType());
8797 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
8798 MemVT, Alignment, MMOFlags, AAInfo);
8799}
8800
8802 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
8803 MachineMemOperand *MMO) {
8804 SDValue Undef = getUNDEF(Ptr.getValueType());
8805 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
8806 MemVT, MMO);
8807}
8808
8812 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
8813 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
8814 // Don't propagate the invariant or dereferenceable flags.
8815 auto MMOFlags =
8816 LD->getMemOperand()->getFlags() &
8818 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
8819 LD->getChain(), Base, Offset, LD->getPointerInfo(),
8820 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
8821}
8822
8825 Align Alignment,
8826 MachineMemOperand::Flags MMOFlags,
8827 const AAMDNodes &AAInfo) {
8828 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
8829
8830 MMOFlags |= MachineMemOperand::MOStore;
8831 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
8832
8833 if (PtrInfo.V.isNull())
8834 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
8835
8838 MachineMemOperand *MMO =
8839 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
8840 return getStore(Chain, dl, Val, Ptr, MMO);
8841}
8842
8845 assert(Chain.getValueType() == MVT::Other &&
8846 "Invalid chain type");
8847 EVT VT = Val.getValueType();
8848 SDVTList VTs = getVTList(MVT::Other);
8849 SDValue Undef = getUNDEF(Ptr.getValueType());
8850 SDValue Ops[] = { Chain, Val, Ptr, Undef };
8852 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
8853 ID.AddInteger(VT.getRawBits());
8854 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
8855 dl.getIROrder(), VTs, ISD::UNINDEXED, false, VT, MMO));
8856 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8857 ID.AddInteger(MMO->getFlags());
8858 void *IP = nullptr;
8859 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8860 cast<StoreSDNode>(E)->refineAlignment(MMO);
8861 return SDValue(E, 0);
8862 }
8863 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
8864 ISD::UNINDEXED, false, VT, MMO);
8865 createOperands(N, Ops);
8866
8867 CSEMap.InsertNode(N, IP);
8868 InsertNode(N);
8869 SDValue V(N, 0);
8870 NewSDValueDbgMsg(V, "Creating new node: ", this);
8871 return V;
8872}
8873
8876 EVT SVT, Align Alignment,
8877 MachineMemOperand::Flags MMOFlags,
8878 const AAMDNodes &AAInfo) {
8879 assert(Chain.getValueType() == MVT::Other &&
8880 "Invalid chain type");
8881
8882 MMOFlags |= MachineMemOperand::MOStore;
8883 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
8884
8885 if (PtrInfo.V.isNull())
8886 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
8887
8890 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
8891 AAInfo);
8892 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
8893}
8894
8896 SDValue Ptr, EVT SVT,
8897 MachineMemOperand *MMO) {
8898 EVT VT = Val.getValueType();
8899
8900 assert(Chain.getValueType() == MVT::Other &&
8901 "Invalid chain type");
8902 if (VT == SVT)
8903 return getStore(Chain, dl, Val, Ptr, MMO);
8904
8906 "Should only be a truncating store, not extending!");
8907 assert(VT.isInteger() == SVT.isInteger() &&
8908 "Can't do FP-INT conversion!");
8909 assert(VT.isVector() == SVT.isVector() &&
8910 "Cannot use trunc store to convert to or from a vector!");
8911 assert((!VT.isVector() ||
8913 "Cannot use trunc store to change the number of vector elements!");
8914
8915 SDVTList VTs = getVTList(MVT::Other);
8916 SDValue Undef = getUNDEF(Ptr.getValueType());
8917 SDValue Ops[] = { Chain, Val, Ptr, Undef };
8919 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
8920 ID.AddInteger(SVT.getRawBits());
8921 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
8922 dl.getIROrder(), VTs, ISD::UNINDEXED, true, SVT, MMO));
8923 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8924 ID.AddInteger(MMO->getFlags());
8925 void *IP = nullptr;
8926 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8927 cast<StoreSDNode>(E)->refineAlignment(MMO);
8928 return SDValue(E, 0);
8929 }
8930 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
8931 ISD::UNINDEXED, true, SVT, MMO);
8932 createOperands(N, Ops);
8933
8934 CSEMap.InsertNode(N, IP);
8935 InsertNode(N);
8936 SDValue V(N, 0);
8937 NewSDValueDbgMsg(V, "Creating new node: ", this);
8938 return V;
8939}
8940
8944 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
8945 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
8946 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
8947 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
8949 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
8950 ID.AddInteger(ST->getMemoryVT().getRawBits());
8951 ID.AddInteger(ST->getRawSubclassData());
8952 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
8953 ID.AddInteger(ST->getMemOperand()->getFlags());
8954 void *IP = nullptr;
8955 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
8956 return SDValue(E, 0);
8957
8958 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
8959 ST->isTruncatingStore(), ST->getMemoryVT(),
8960 ST->getMemOperand());
8961 createOperands(N, Ops);
8962
8963 CSEMap.InsertNode(N, IP);
8964 InsertNode(N);
8965 SDValue V(N, 0);
8966 NewSDValueDbgMsg(V, "Creating new node: ", this);
8967 return V;
8968}
8969
8971 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
8972 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
8973 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
8974 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
8975 const MDNode *Ranges, bool IsExpanding) {
8976 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
8977
8978 MMOFlags |= MachineMemOperand::MOLoad;
8979 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
8980 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
8981 // clients.
8982 if (PtrInfo.V.isNull())
8983 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
8984
8987 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
8988 Alignment, AAInfo, Ranges);
8989 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
8990 MMO, IsExpanding);
8991}
8992
8994 ISD::LoadExtType ExtType, EVT VT,
8995 const SDLoc &dl, SDValue Chain, SDValue Ptr,
8996 SDValue Offset, SDValue Mask, SDValue EVL,
8997 EVT MemVT, MachineMemOperand *MMO,
8998 bool IsExpanding) {
8999 bool Indexed = AM != ISD::UNINDEXED;
9000 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9001
9002 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9003 : getVTList(VT, MVT::Other);
9004 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
9006 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
9007 ID.AddInteger(MemVT.getRawBits());
9008 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
9009 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9010 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9011 ID.AddInteger(MMO->getFlags());
9012 void *IP = nullptr;
9013 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9014 cast<VPLoadSDNode>(E)->refineAlignment(MMO);
9015 return SDValue(E, 0);
9016 }
9017 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9018 ExtType, IsExpanding, MemVT, MMO);
9019 createOperands(N, Ops);
9020
9021 CSEMap.InsertNode(N, IP);
9022 InsertNode(N);
9023 SDValue V(N, 0);
9024 NewSDValueDbgMsg(V, "Creating new node: ", this);
9025 return V;
9026}
9027
9029 SDValue Ptr, SDValue Mask, SDValue EVL,
9030 MachinePointerInfo PtrInfo,
9031 MaybeAlign Alignment,
9032 MachineMemOperand::Flags MMOFlags,
9033 const AAMDNodes &AAInfo, const MDNode *Ranges,
9034 bool IsExpanding) {
9035 SDValue Undef = getUNDEF(Ptr.getValueType());
9036 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9037 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
9038 IsExpanding);
9039}
9040
9042 SDValue Ptr, SDValue Mask, SDValue EVL,
9043 MachineMemOperand *MMO, bool IsExpanding) {
9044 SDValue Undef = getUNDEF(Ptr.getValueType());
9045 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9046 Mask, EVL, VT, MMO, IsExpanding);
9047}
9048
9050 EVT VT, SDValue Chain, SDValue Ptr,
9051 SDValue Mask, SDValue EVL,
9052 MachinePointerInfo PtrInfo, EVT MemVT,
9053 MaybeAlign Alignment,
9054 MachineMemOperand::Flags MMOFlags,
9055 const AAMDNodes &AAInfo, bool IsExpanding) {
9056 SDValue Undef = getUNDEF(Ptr.getValueType());
9057 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9058 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
9059 IsExpanding);
9060}
9061
9063 EVT VT, SDValue Chain, SDValue Ptr,
9064 SDValue Mask, SDValue EVL, EVT MemVT,
9065 MachineMemOperand *MMO, bool IsExpanding) {
9066 SDValue Undef = getUNDEF(Ptr.getValueType());
9067 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9068 EVL, MemVT, MMO, IsExpanding);
9069}
9070
9074 auto *LD = cast<VPLoadSDNode>(OrigLoad);
9075 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9076 // Don't propagate the invariant or dereferenceable flags.
9077 auto MMOFlags =
9078 LD->getMemOperand()->getFlags() &
9080 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9081 LD->getChain(), Base, Offset, LD->getMask(),
9082 LD->getVectorLength(), LD->getPointerInfo(),
9083 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
9084 nullptr, LD->isExpandingLoad());
9085}
9086
9089 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
9090 ISD::MemIndexedMode AM, bool IsTruncating,
9091 bool IsCompressing) {
9092 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9093 bool Indexed = AM != ISD::UNINDEXED;
9094 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9095 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9096 : getVTList(MVT::Other);
9097 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
9099 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9100 ID.AddInteger(MemVT.getRawBits());
9101 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9102 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9103 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9104 ID.AddInteger(MMO->getFlags());
9105 void *IP = nullptr;
9106 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9107 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9108 return SDValue(E, 0);
9109 }
9110 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9111 IsTruncating, IsCompressing, MemVT, MMO);
9112 createOperands(N, Ops);
9113
9114 CSEMap.InsertNode(N, IP);
9115 InsertNode(N);
9116 SDValue V(N, 0);
9117 NewSDValueDbgMsg(V, "Creating new node: ", this);
9118 return V;
9119}
9120
9122 SDValue Val, SDValue Ptr, SDValue Mask,
9123 SDValue EVL, MachinePointerInfo PtrInfo,
9124 EVT SVT, Align Alignment,
9125 MachineMemOperand::Flags MMOFlags,
9126 const AAMDNodes &AAInfo,
9127 bool IsCompressing) {
9128 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9129
9130 MMOFlags |= MachineMemOperand::MOStore;
9131 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9132
9133 if (PtrInfo.V.isNull())
9134 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9135
9138 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
9139 AAInfo);
9140 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
9141 IsCompressing);
9142}
9143
9145 SDValue Val, SDValue Ptr, SDValue Mask,
9146 SDValue EVL, EVT SVT,
9147 MachineMemOperand *MMO,
9148 bool IsCompressing) {
9149 EVT VT = Val.getValueType();
9150
9151 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9152 if (VT == SVT)
9153 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
9154 EVL, VT, MMO, ISD::UNINDEXED,
9155 /*IsTruncating*/ false, IsCompressing);
9156
9158 "Should only be a truncating store, not extending!");
9159 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9160 assert(VT.isVector() == SVT.isVector() &&
9161 "Cannot use trunc store to convert to or from a vector!");
9162 assert((!VT.isVector() ||
9164 "Cannot use trunc store to change the number of vector elements!");
9165
9166 SDVTList VTs = getVTList(MVT::Other);
9167 SDValue Undef = getUNDEF(Ptr.getValueType());
9168 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
9170 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9171 ID.AddInteger(SVT.getRawBits());
9172 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9173 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9174 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9175 ID.AddInteger(MMO->getFlags());
9176 void *IP = nullptr;
9177 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9178 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9179 return SDValue(E, 0);
9180 }
9181 auto *N =
9182 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9183 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
9184 createOperands(N, Ops);
9185
9186 CSEMap.InsertNode(N, IP);
9187 InsertNode(N);
9188 SDValue V(N, 0);
9189 NewSDValueDbgMsg(V, "Creating new node: ", this);
9190 return V;
9191}
9192
9196 auto *ST = cast<VPStoreSDNode>(OrigStore);
9197 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
9198 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9199 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
9200 Offset, ST->getMask(), ST->getVectorLength()};
9202 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9203 ID.AddInteger(ST->getMemoryVT().getRawBits());
9204 ID.AddInteger(ST->getRawSubclassData());
9205 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9206 ID.AddInteger(ST->getMemOperand()->getFlags());
9207 void *IP = nullptr;
9208 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9209 return SDValue(E, 0);
9210
9211 auto *N = newSDNode<VPStoreSDNode>(
9212 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
9213 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
9214 createOperands(N, Ops);
9215
9216 CSEMap.InsertNode(N, IP);
9217 InsertNode(N);
9218 SDValue V(N, 0);
9219 NewSDValueDbgMsg(V, "Creating new node: ", this);
9220 return V;
9221}
9222
9224 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
9225 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
9226 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
9227 bool Indexed = AM != ISD::UNINDEXED;
9228 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9229
9230 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
9231 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9232 : getVTList(VT, MVT::Other);
9234 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
9235 ID.AddInteger(VT.getRawBits());
9236 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
9237 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9238 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9239
9240 void *IP = nullptr;
9241 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9242 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
9243 return SDValue(E, 0);
9244 }
9245
9246 auto *N =
9247 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
9248 ExtType, IsExpanding, MemVT, MMO);
9249 createOperands(N, Ops);
9250 CSEMap.InsertNode(N, IP);
9251 InsertNode(N);
9252 SDValue V(N, 0);
9253 NewSDValueDbgMsg(V, "Creating new node: ", this);
9254 return V;
9255}
9256
9258 SDValue Ptr, SDValue Stride,
9259 SDValue Mask, SDValue EVL,
9260 MachineMemOperand *MMO,
9261 bool IsExpanding) {
9262 SDValue Undef = getUNDEF(Ptr.getValueType());
9264 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
9265}
9266
9268 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
9269 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
9270 MachineMemOperand *MMO, bool IsExpanding) {
9271 SDValue Undef = getUNDEF(Ptr.getValueType());
9272 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
9273 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
9274}
9275
9277 SDValue Val, SDValue Ptr,
9278 SDValue Offset, SDValue Stride,
9279 SDValue Mask, SDValue EVL, EVT MemVT,
9280 MachineMemOperand *MMO,
9282 bool IsTruncating, bool IsCompressing) {
9283 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9284 bool Indexed = AM != ISD::UNINDEXED;
9285 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9286 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9287 : getVTList(MVT::Other);
9288 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
9290 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9291 ID.AddInteger(MemVT.getRawBits());
9292 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9293 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9294 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9295 void *IP = nullptr;
9296 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9297 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9298 return SDValue(E, 0);
9299 }
9300 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9301 VTs, AM, IsTruncating,
9302 IsCompressing, MemVT, MMO);
9303 createOperands(N, Ops);
9304
9305 CSEMap.InsertNode(N, IP);
9306 InsertNode(N);
9307 SDValue V(N, 0);
9308 NewSDValueDbgMsg(V, "Creating new node: ", this);
9309 return V;
9310}
9311
9313 SDValue Val, SDValue Ptr,
9314 SDValue Stride, SDValue Mask,
9315 SDValue EVL, EVT SVT,
9316 MachineMemOperand *MMO,
9317 bool IsCompressing) {
9318 EVT VT = Val.getValueType();
9319
9320 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9321 if (VT == SVT)
9322 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
9323 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
9324 /*IsTruncating*/ false, IsCompressing);
9325
9327 "Should only be a truncating store, not extending!");
9328 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9329 assert(VT.isVector() == SVT.isVector() &&
9330 "Cannot use trunc store to convert to or from a vector!");
9331 assert((!VT.isVector() ||
9333 "Cannot use trunc store to change the number of vector elements!");
9334
9335 SDVTList VTs = getVTList(MVT::Other);
9336 SDValue Undef = getUNDEF(Ptr.getValueType());
9337 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
9339 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9340 ID.AddInteger(SVT.getRawBits());
9341 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9342 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9343 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9344 void *IP = nullptr;
9345 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9346 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9347 return SDValue(E, 0);
9348 }
9349 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9350 VTs, ISD::UNINDEXED, true,
9351 IsCompressing, SVT, MMO);
9352 createOperands(N, Ops);
9353
9354 CSEMap.InsertNode(N, IP);
9355 InsertNode(N);
9356 SDValue V(N, 0);
9357 NewSDValueDbgMsg(V, "Creating new node: ", this);
9358 return V;
9359}
9360
9363 ISD::MemIndexType IndexType) {
9364 assert(Ops.size() == 6 && "Incompatible number of operands");
9365
9367 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
9368 ID.AddInteger(VT.getRawBits());
9369 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
9370 dl.getIROrder(), VTs, VT, MMO, IndexType));
9371 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9372 ID.AddInteger(MMO->getFlags());
9373 void *IP = nullptr;
9374 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9375 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
9376 return SDValue(E, 0);
9377 }
9378
9379 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9380 VT, MMO, IndexType);
9381 createOperands(N, Ops);
9382
9383 assert(N->getMask().getValueType().getVectorElementCount() ==
9384 N->getValueType(0).getVectorElementCount() &&
9385 "Vector width mismatch between mask and data");
9386 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9387 N->getValueType(0).getVectorElementCount().isScalable() &&
9388 "Scalable flags of index and data do not match");
9390 N->getIndex().getValueType().getVectorElementCount(),
9391 N->getValueType(0).getVectorElementCount()) &&
9392 "Vector width mismatch between index and data");
9393 assert(isa<ConstantSDNode>(N->getScale()) &&
9394 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9395 "Scale should be a constant power of 2");
9396
9397 CSEMap.InsertNode(N, IP);
9398 InsertNode(N);
9399 SDValue V(N, 0);
9400 NewSDValueDbgMsg(V, "Creating new node: ", this);
9401 return V;
9402}
9403
9406 MachineMemOperand *MMO,
9407 ISD::MemIndexType IndexType) {
9408 assert(Ops.size() == 7 && "Incompatible number of operands");
9409
9411 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
9412 ID.AddInteger(VT.getRawBits());
9413 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
9414 dl.getIROrder(), VTs, VT, MMO, IndexType));
9415 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9416 ID.AddInteger(MMO->getFlags());
9417 void *IP = nullptr;
9418 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9419 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
9420 return SDValue(E, 0);
9421 }
9422 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9423 VT, MMO, IndexType);
9424 createOperands(N, Ops);
9425
9426 assert(N->getMask().getValueType().getVectorElementCount() ==
9427 N->getValue().getValueType().getVectorElementCount() &&
9428 "Vector width mismatch between mask and data");
9429 assert(
9430 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9431 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9432 "Scalable flags of index and data do not match");
9434 N->getIndex().getValueType().getVectorElementCount(),
9435 N->getValue().getValueType().getVectorElementCount()) &&
9436 "Vector width mismatch between index and data");
9437 assert(isa<ConstantSDNode>(N->getScale()) &&
9438 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9439 "Scale should be a constant power of 2");
9440
9441 CSEMap.InsertNode(N, IP);
9442 InsertNode(N);
9443 SDValue V(N, 0);
9444 NewSDValueDbgMsg(V, "Creating new node: ", this);
9445 return V;
9446}
9447
9450 SDValue PassThru, EVT MemVT,
9451 MachineMemOperand *MMO,
9453 ISD::LoadExtType ExtTy, bool isExpanding) {
9454 bool Indexed = AM != ISD::UNINDEXED;
9455 assert((Indexed || Offset.isUndef()) &&
9456 "Unindexed masked load with an offset!");
9457 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
9458 : getVTList(VT, MVT::Other);
9459 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
9461 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
9462 ID.AddInteger(MemVT.getRawBits());
9463 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
9464 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
9465 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9466 ID.AddInteger(MMO->getFlags());
9467 void *IP = nullptr;
9468 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9469 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
9470 return SDValue(E, 0);
9471 }
9472 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9473 AM, ExtTy, isExpanding, MemVT, MMO);
9474 createOperands(N, Ops);
9475
9476 CSEMap.InsertNode(N, IP);
9477 InsertNode(N);
9478 SDValue V(N, 0);
9479 NewSDValueDbgMsg(V, "Creating new node: ", this);
9480 return V;
9481}
9482
9486 MaskedLoadSDNode *LD = cast<MaskedLoadSDNode>(OrigLoad);
9487 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
9488 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
9489 Offset, LD->getMask(), LD->getPassThru(),
9490 LD->getMemoryVT(), LD->getMemOperand(), AM,
9491 LD->getExtensionType(), LD->isExpandingLoad());
9492}
9493
9496 SDValue Mask, EVT MemVT,
9497 MachineMemOperand *MMO,
9498 ISD::MemIndexedMode AM, bool IsTruncating,
9499 bool IsCompressing) {
9500 assert(Chain.getValueType() == MVT::Other &&
9501 "Invalid chain type");
9502 bool Indexed = AM != ISD::UNINDEXED;
9503 assert((Indexed || Offset.isUndef()) &&
9504 "Unindexed masked store with an offset!");
9505 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
9506 : getVTList(MVT::Other);
9507 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
9509 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
9510 ID.AddInteger(MemVT.getRawBits());
9511 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
9512 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9513 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9514 ID.AddInteger(MMO->getFlags());
9515 void *IP = nullptr;
9516 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9517 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
9518 return SDValue(E, 0);
9519 }
9520 auto *N =
9521 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9522 IsTruncating, IsCompressing, MemVT, MMO);
9523 createOperands(N, Ops);
9524
9525 CSEMap.InsertNode(N, IP);
9526 InsertNode(N);
9527 SDValue V(N, 0);
9528 NewSDValueDbgMsg(V, "Creating new node: ", this);
9529 return V;
9530}
9531
9535 MaskedStoreSDNode *ST = cast<MaskedStoreSDNode>(OrigStore);
9536 assert(ST->getOffset().isUndef() &&
9537 "Masked store is already a indexed store!");
9538 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9539 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
9540 AM, ST->isTruncatingStore(), ST->isCompressingStore());
9541}
9542
9545 MachineMemOperand *MMO,
9546 ISD::MemIndexType IndexType,
9547 ISD::LoadExtType ExtTy) {
9548 assert(Ops.size() == 6 && "Incompatible number of operands");
9549
9551 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
9552 ID.AddInteger(MemVT.getRawBits());
9553 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
9554 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
9555 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9556 ID.AddInteger(MMO->getFlags());
9557 void *IP = nullptr;
9558 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9559 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
9560 return SDValue(E, 0);
9561 }
9562
9563 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9564 VTs, MemVT, MMO, IndexType, ExtTy);
9565 createOperands(N, Ops);
9566
9567 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
9568 "Incompatible type of the PassThru value in MaskedGatherSDNode");
9569 assert(N->getMask().getValueType().getVectorElementCount() ==
9570 N->getValueType(0).getVectorElementCount() &&
9571 "Vector width mismatch between mask and data");
9572 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9573 N->getValueType(0).getVectorElementCount().isScalable() &&
9574 "Scalable flags of index and data do not match");
9576 N->getIndex().getValueType().getVectorElementCount(),
9577 N->getValueType(0).getVectorElementCount()) &&
9578 "Vector width mismatch between index and data");
9579 assert(isa<ConstantSDNode>(N->getScale()) &&
9580 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9581 "Scale should be a constant power of 2");
9582
9583 CSEMap.InsertNode(N, IP);
9584 InsertNode(N);
9585 SDValue V(N, 0);
9586 NewSDValueDbgMsg(V, "Creating new node: ", this);
9587 return V;
9588}
9589
9592 MachineMemOperand *MMO,
9593 ISD::MemIndexType IndexType,
9594 bool IsTrunc) {
9595 assert(Ops.size() == 6 && "Incompatible number of operands");
9596
9598 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
9599 ID.AddInteger(MemVT.getRawBits());
9600 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
9601 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
9602 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9603 ID.AddInteger(MMO->getFlags());
9604 void *IP = nullptr;
9605 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9606 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
9607 return SDValue(E, 0);
9608 }
9609
9610 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9611 VTs, MemVT, MMO, IndexType, IsTrunc);
9612 createOperands(N, Ops);
9613
9614 assert(N->getMask().getValueType().getVectorElementCount() ==
9615 N->getValue().getValueType().getVectorElementCount() &&
9616 "Vector width mismatch between mask and data");
9617 assert(
9618 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9619 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9620 "Scalable flags of index and data do not match");
9622 N->getIndex().getValueType().getVectorElementCount(),
9623 N->getValue().getValueType().getVectorElementCount()) &&
9624 "Vector width mismatch between index and data");
9625 assert(isa<ConstantSDNode>(N->getScale()) &&
9626 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9627 "Scale should be a constant power of 2");
9628
9629 CSEMap.InsertNode(N, IP);
9630 InsertNode(N);
9631 SDValue V(N, 0);
9632 NewSDValueDbgMsg(V, "Creating new node: ", this);
9633 return V;
9634}
9635
9637 EVT MemVT, MachineMemOperand *MMO) {
9638 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9639 SDVTList VTs = getVTList(MVT::Other);
9640 SDValue Ops[] = {Chain, Ptr};
9643 ID.AddInteger(MemVT.getRawBits());
9644 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
9645 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
9646 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9647 ID.AddInteger(MMO->getFlags());
9648 void *IP = nullptr;
9649 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9650 return SDValue(E, 0);
9651
9652 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
9653 dl.getDebugLoc(), VTs, MemVT, MMO);
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, MachineMemOperand *MMO) {
9665 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9666 SDVTList VTs = getVTList(MVT::Other);
9667 SDValue Ops[] = {Chain, Ptr};
9670 ID.AddInteger(MemVT.getRawBits());
9671 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
9672 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
9673 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9674 ID.AddInteger(MMO->getFlags());
9675 void *IP = nullptr;
9676 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9677 return SDValue(E, 0);
9678
9679 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
9680 dl.getDebugLoc(), VTs, MemVT, MMO);
9681 createOperands(N, Ops);
9682
9683 CSEMap.InsertNode(N, IP);
9684 InsertNode(N);
9685 SDValue V(N, 0);
9686 NewSDValueDbgMsg(V, "Creating new node: ", this);
9687 return V;
9688}
9689
9691 // select undef, T, F --> T (if T is a constant), otherwise F
9692 // select, ?, undef, F --> F
9693 // select, ?, T, undef --> T
9694 if (Cond.isUndef())
9695 return isConstantValueOfAnyType(T) ? T : F;
9696 if (T.isUndef())
9697 return F;
9698 if (F.isUndef())
9699 return T;
9700
9701 // select true, T, F --> T
9702 // select false, T, F --> F
9703 if (auto *CondC = dyn_cast<ConstantSDNode>(Cond))
9704 return CondC->isZero() ? F : T;
9705
9706 // TODO: This should simplify VSELECT with non-zero constant condition using
9707 // something like this (but check boolean contents to be complete?):
9708 if (ConstantSDNode *CondC = isConstOrConstSplat(Cond, /*AllowUndefs*/ false,
9709 /*AllowTruncation*/ true))
9710 if (CondC->isZero())
9711 return F;
9712
9713 // select ?, T, T --> T
9714 if (T == F)
9715 return T;
9716
9717 return SDValue();
9718}
9719
9721 // shift undef, Y --> 0 (can always assume that the undef value is 0)
9722 if (X.isUndef())
9723 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
9724 // shift X, undef --> undef (because it may shift by the bitwidth)
9725 if (Y.isUndef())
9726 return getUNDEF(X.getValueType());
9727
9728 // shift 0, Y --> 0
9729 // shift X, 0 --> X
9731 return X;
9732
9733 // shift X, C >= bitwidth(X) --> undef
9734 // All vector elements must be too big (or undef) to avoid partial undefs.
9735 auto isShiftTooBig = [X](ConstantSDNode *Val) {
9736 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
9737 };
9738 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
9739 return getUNDEF(X.getValueType());
9740
9741 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
9742 if (X.getValueType().getScalarType() == MVT::i1)
9743 return X;
9744
9745 return SDValue();
9746}
9747
9749 SDNodeFlags Flags) {
9750 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
9751 // (an undef operand can be chosen to be Nan/Inf), then the result of this
9752 // operation is poison. That result can be relaxed to undef.
9753 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
9754 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
9755 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
9756 (YC && YC->getValueAPF().isNaN());
9757 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
9758 (YC && YC->getValueAPF().isInfinity());
9759
9760 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
9761 return getUNDEF(X.getValueType());
9762
9763 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
9764 return getUNDEF(X.getValueType());
9765
9766 if (!YC)
9767 return SDValue();
9768
9769 // X + -0.0 --> X
9770 if (Opcode == ISD::FADD)
9771 if (YC->getValueAPF().isNegZero())
9772 return X;
9773
9774 // X - +0.0 --> X
9775 if (Opcode == ISD::FSUB)
9776 if (YC->getValueAPF().isPosZero())
9777 return X;
9778
9779 // X * 1.0 --> X
9780 // X / 1.0 --> X
9781 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
9782 if (YC->getValueAPF().isExactlyValue(1.0))
9783 return X;
9784
9785 // X * 0.0 --> 0.0
9786 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
9787 if (YC->getValueAPF().isZero())
9788 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
9789
9790 return SDValue();
9791}
9792
9794 SDValue Ptr, SDValue SV, unsigned Align) {
9795 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
9796 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
9797}
9798
9799SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9800 ArrayRef<SDUse> Ops) {
9801 switch (Ops.size()) {
9802 case 0: return getNode(Opcode, DL, VT);
9803 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
9804 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
9805 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
9806 default: break;
9807 }
9808
9809 // Copy from an SDUse array into an SDValue array for use with
9810 // the regular getNode logic.
9811 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
9812 return getNode(Opcode, DL, VT, NewOps);
9813}
9814
9815SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9816 ArrayRef<SDValue> Ops) {
9817 SDNodeFlags Flags;
9818 if (Inserter)
9819 Flags = Inserter->getFlags();
9820 return getNode(Opcode, DL, VT, Ops, Flags);
9821}
9822
9823SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9824 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
9825 unsigned NumOps = Ops.size();
9826 switch (NumOps) {
9827 case 0: return getNode(Opcode, DL, VT);
9828 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
9829 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
9830 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
9831 default: break;
9832 }
9833
9834#ifndef NDEBUG
9835 for (const auto &Op : Ops)
9836 assert(Op.getOpcode() != ISD::DELETED_NODE &&
9837 "Operand is DELETED_NODE!");
9838#endif
9839
9840 switch (Opcode) {
9841 default: break;
9842 case ISD::BUILD_VECTOR:
9843 // Attempt to simplify BUILD_VECTOR.
9844 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
9845 return V;
9846 break;
9848 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
9849 return V;
9850 break;
9851 case ISD::SELECT_CC:
9852 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
9853 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
9854 "LHS and RHS of condition must have same type!");
9855 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
9856 "True and False arms of SelectCC must have same type!");
9857 assert(Ops[2].getValueType() == VT &&
9858 "select_cc node must be of same type as true and false value!");
9859 assert((!Ops[0].getValueType().isVector() ||
9860 Ops[0].getValueType().getVectorElementCount() ==
9861 VT.getVectorElementCount()) &&
9862 "Expected select_cc with vector result to have the same sized "
9863 "comparison type!");
9864 break;
9865 case ISD::BR_CC:
9866 assert(NumOps == 5 && "BR_CC takes 5 operands!");
9867 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
9868 "LHS/RHS of comparison should match types!");
9869 break;
9870 case ISD::VP_ADD:
9871 case ISD::VP_SUB:
9872 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
9873 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
9874 Opcode = ISD::VP_XOR;
9875 break;
9876 case ISD::VP_MUL:
9877 // If it is VP_MUL mask operation then turn it to VP_AND
9878 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
9879 Opcode = ISD::VP_AND;
9880 break;
9881 case ISD::VP_REDUCE_MUL:
9882 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
9883 if (VT == MVT::i1)
9884 Opcode = ISD::VP_REDUCE_AND;
9885 break;
9886 case ISD::VP_REDUCE_ADD:
9887 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
9888 if (VT == MVT::i1)
9889 Opcode = ISD::VP_REDUCE_XOR;
9890 break;
9891 case ISD::VP_REDUCE_SMAX:
9892 case ISD::VP_REDUCE_UMIN:
9893 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
9894 // VP_REDUCE_AND.
9895 if (VT == MVT::i1)
9896 Opcode = ISD::VP_REDUCE_AND;
9897 break;
9898 case ISD::VP_REDUCE_SMIN:
9899 case ISD::VP_REDUCE_UMAX:
9900 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
9901 // VP_REDUCE_OR.
9902 if (VT == MVT::i1)
9903 Opcode = ISD::VP_REDUCE_OR;
9904 break;
9905 }
9906
9907 // Memoize nodes.
9908 SDNode *N;
9909 SDVTList VTs = getVTList(VT);
9910
9911 if (VT != MVT::Glue) {
9913 AddNodeIDNode(ID, Opcode, VTs, Ops);
9914 void *IP = nullptr;
9915
9916 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
9917 return SDValue(E, 0);
9918
9919 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
9920 createOperands(N, Ops);
9921
9922 CSEMap.InsertNode(N, IP);
9923 } else {
9924 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
9925 createOperands(N, Ops);
9926 }
9927
9928 N->setFlags(Flags);
9929 InsertNode(N);
9930 SDValue V(N, 0);
9931 NewSDValueDbgMsg(V, "Creating new node: ", this);
9932 return V;
9933}
9934
9935SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
9936 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
9937 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
9938}
9939
9940SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
9941 ArrayRef<SDValue> Ops) {
9942 SDNodeFlags Flags;
9943 if (Inserter)
9944 Flags = Inserter->getFlags();
9945 return getNode(Opcode, DL, VTList, Ops, Flags);
9946}
9947
9948SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
9949 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
9950 if (VTList.NumVTs == 1)
9951 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
9952
9953#ifndef NDEBUG
9954 for (const auto &Op : Ops)
9955 assert(Op.getOpcode() != ISD::DELETED_NODE &&
9956 "Operand is DELETED_NODE!");
9957#endif
9958
9959 switch (Opcode) {
9960 case ISD::SADDO:
9961 case ISD::UADDO:
9962 case ISD::SSUBO:
9963 case ISD::USUBO: {
9964 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
9965 "Invalid add/sub overflow op!");
9966 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
9967 Ops[0].getValueType() == Ops[1].getValueType() &&
9968 Ops[0].getValueType() == VTList.VTs[0] &&
9969 "Binary operator types must match!");
9970 SDValue N1 = Ops[0], N2 = Ops[1];
9971 canonicalizeCommutativeBinop(Opcode, N1, N2);
9972
9973 // (X +- 0) -> X with zero-overflow.
9974 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
9975 /*AllowTruncation*/ true);
9976 if (N2CV && N2CV->isZero()) {
9977 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
9978 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
9979 }
9980
9981 if (VTList.VTs[0].isVector() &&
9982 VTList.VTs[0].getVectorElementType() == MVT::i1 &&
9983 VTList.VTs[1].getVectorElementType() == MVT::i1) {
9984 SDValue F1 = getFreeze(N1);
9985 SDValue F2 = getFreeze(N2);
9986 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
9987 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
9988 return getNode(ISD::MERGE_VALUES, DL, VTList,
9989 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
9990 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
9991 Flags);
9992 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
9993 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
9994 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
9995 return getNode(ISD::MERGE_VALUES, DL, VTList,
9996 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
9997 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
9998 Flags);
9999 }
10000 }
10001 break;
10002 }
10003 case ISD::SADDO_CARRY:
10004 case ISD::UADDO_CARRY:
10005 case ISD::SSUBO_CARRY:
10006 case ISD::USUBO_CARRY:
10007 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
10008 "Invalid add/sub overflow op!");
10009 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10010 Ops[0].getValueType() == Ops[1].getValueType() &&
10011 Ops[0].getValueType() == VTList.VTs[0] &&
10012 Ops[2].getValueType() == VTList.VTs[1] &&
10013 "Binary operator types must match!");
10014 break;
10015 case ISD::SMUL_LOHI:
10016 case ISD::UMUL_LOHI: {
10017 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
10018 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
10019 VTList.VTs[0] == Ops[0].getValueType() &&
10020 VTList.VTs[0] == Ops[1].getValueType() &&
10021 "Binary operator types must match!");
10022 // Constant fold.
10023 ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(Ops[0]);
10024 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ops[1]);
10025 if (LHS && RHS) {
10026 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
10027 unsigned OutWidth = Width * 2;
10028 APInt Val = LHS->getAPIntValue();
10029 APInt Mul = RHS->getAPIntValue();
10030 if (Opcode == ISD::SMUL_LOHI) {
10031 Val = Val.sext(OutWidth);
10032 Mul = Mul.sext(OutWidth);
10033 } else {
10034 Val = Val.zext(OutWidth);
10035 Mul = Mul.zext(OutWidth);
10036 }
10037 Val *= Mul;
10038
10039 SDValue Hi =
10040 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
10041 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
10042 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
10043 }
10044 break;
10045 }
10046 case ISD::FFREXP: {
10047 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
10048 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
10049 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
10050
10051 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Ops[0])) {
10052 int FrexpExp;
10053 APFloat FrexpMant =
10054 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
10055 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
10056 SDValue Result1 =
10057 getConstant(FrexpMant.isFinite() ? FrexpExp : 0, DL, VTList.VTs[1]);
10058 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
10059 }
10060
10061 break;
10062 }
10064 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10065 "Invalid STRICT_FP_EXTEND!");
10066 assert(VTList.VTs[0].isFloatingPoint() &&
10067 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
10068 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10069 "STRICT_FP_EXTEND result type should be vector iff the operand "
10070 "type is vector!");
10071 assert((!VTList.VTs[0].isVector() ||
10072 VTList.VTs[0].getVectorElementCount() ==
10073 Ops[1].getValueType().getVectorElementCount()) &&
10074 "Vector element count mismatch!");
10075 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
10076 "Invalid fpext node, dst <= src!");
10077 break;
10079 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
10080 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10081 "STRICT_FP_ROUND result type should be vector iff the operand "
10082 "type is vector!");
10083 assert((!VTList.VTs[0].isVector() ||
10084 VTList.VTs[0].getVectorElementCount() ==
10085 Ops[1].getValueType().getVectorElementCount()) &&
10086 "Vector element count mismatch!");
10087 assert(VTList.VTs[0].isFloatingPoint() &&
10088 Ops[1].getValueType().isFloatingPoint() &&
10089 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
10090 isa<ConstantSDNode>(Ops[2]) &&
10091 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
10092 "Invalid STRICT_FP_ROUND!");
10093 break;
10094#if 0
10095 // FIXME: figure out how to safely handle things like
10096 // int foo(int x) { return 1 << (x & 255); }
10097 // int bar() { return foo(256); }
10098 case ISD::SRA_PARTS:
10099 case ISD::SRL_PARTS:
10100 case ISD::SHL_PARTS:
10101 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
10102 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
10103 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10104 else if (N3.getOpcode() == ISD::AND)
10105 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
10106 // If the and is only masking out bits that cannot effect the shift,
10107 // eliminate the and.
10108 unsigned NumBits = VT.getScalarSizeInBits()*2;
10109 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
10110 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10111 }
10112 break;
10113#endif
10114 }
10115
10116 // Memoize the node unless it returns a glue result.
10117 SDNode *N;
10118 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10120 AddNodeIDNode(ID, Opcode, VTList, Ops);
10121 void *IP = nullptr;
10122 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
10123 return SDValue(E, 0);
10124
10125 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10126 createOperands(N, Ops);
10127 CSEMap.InsertNode(N, IP);
10128 } else {
10129 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10130 createOperands(N, Ops);
10131 }
10132
10133 N->setFlags(Flags);
10134 InsertNode(N);
10135 SDValue V(N, 0);
10136 NewSDValueDbgMsg(V, "Creating new node: ", this);
10137 return V;
10138}
10139
10140SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10141 SDVTList VTList) {
10142 return getNode(Opcode, DL, VTList, std::nullopt);
10143}
10144
10145SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10146 SDValue N1) {
10147 SDValue Ops[] = { N1 };
10148 return getNode(Opcode, DL, VTList, Ops);
10149}
10150
10151SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10152 SDValue N1, SDValue N2) {
10153 SDValue Ops[] = { N1, N2 };
10154 return getNode(Opcode, DL, VTList, Ops);
10155}
10156
10157SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10158 SDValue N1, SDValue N2, SDValue N3) {
10159 SDValue Ops[] = { N1, N2, N3 };
10160 return getNode(Opcode, DL, VTList, Ops);
10161}
10162
10163SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10164 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
10165 SDValue Ops[] = { N1, N2, N3, N4 };
10166 return getNode(Opcode, DL, VTList, Ops);
10167}
10168
10169SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10170 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
10171 SDValue N5) {
10172 SDValue Ops[] = { N1, N2, N3, N4, N5 };
10173 return getNode(Opcode, DL, VTList, Ops);
10174}
10175
10177 return makeVTList(SDNode::getValueTypeList(VT), 1);
10178}
10179
10182 ID.AddInteger(2U);
10183 ID.AddInteger(VT1.getRawBits());
10184 ID.AddInteger(VT2.getRawBits());
10185
10186 void *IP = nullptr;
10187 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10188 if (!Result) {
10189 EVT *Array = Allocator.Allocate<EVT>(2);
10190 Array[0] = VT1;
10191 Array[1] = VT2;
10192 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
10193 VTListMap.InsertNode(Result, IP);
10194 }
10195 return Result->getSDVTList();
10196}
10197
10200 ID.AddInteger(3U);
10201 ID.AddInteger(VT1.getRawBits());
10202 ID.AddInteger(VT2.getRawBits());
10203 ID.AddInteger(VT3.getRawBits());
10204
10205 void *IP = nullptr;
10206 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10207 if (!Result) {
10208 EVT *Array = Allocator.Allocate<EVT>(3);
10209 Array[0] = VT1;
10210 Array[1] = VT2;
10211 Array[2] = VT3;
10212 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
10213 VTListMap.InsertNode(Result, IP);
10214 }
10215 return Result->getSDVTList();
10216}
10217
10220 ID.AddInteger(4U);
10221 ID.AddInteger(VT1.getRawBits());
10222 ID.AddInteger(VT2.getRawBits());
10223 ID.AddInteger(VT3.getRawBits());
10224 ID.AddInteger(VT4.getRawBits());
10225
10226 void *IP = nullptr;
10227 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10228 if (!Result) {
10229 EVT *Array = Allocator.Allocate<EVT>(4);
10230 Array[0] = VT1;
10231 Array[1] = VT2;
10232 Array[2] = VT3;
10233 Array[3] = VT4;
10234 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
10235 VTListMap.InsertNode(Result, IP);
10236 }
10237 return Result->getSDVTList();
10238}
10239
10241 unsigned NumVTs = VTs.size();
10243 ID.AddInteger(NumVTs);
10244 for (unsigned index = 0; index < NumVTs; index++) {
10245 ID.AddInteger(VTs[index].getRawBits());
10246 }
10247
10248 void *IP = nullptr;
10249 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10250 if (!Result) {
10251 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
10252 llvm::copy(VTs, Array);
10253 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
10254 VTListMap.InsertNode(Result, IP);
10255 }
10256 return Result->getSDVTList();
10257}
10258
10259
10260/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
10261/// specified operands. If the resultant node already exists in the DAG,
10262/// this does not modify the specified node, instead it returns the node that
10263/// already exists. If the resultant node does not exist in the DAG, the
10264/// input node is returned. As a degenerate case, if you specify the same
10265/// input operands as the node already has, the input node is returned.
10267 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
10268
10269 // Check to see if there is no change.
10270 if (Op == N->getOperand(0)) return N;
10271
10272 // See if the modified node already exists.
10273 void *InsertPos = nullptr;
10274 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
10275 return Existing;
10276
10277 // Nope it doesn't. Remove the node from its current place in the maps.
10278 if (InsertPos)
10279 if (!RemoveNodeFromCSEMaps(N))
10280 InsertPos = nullptr;
10281
10282 // Now we update the operands.
10283 N->OperandList[0].set(Op);
10284
10286 // If this gets put into a CSE map, add it.
10287 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10288 return N;
10289}
10290
10292 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
10293
10294 // Check to see if there is no change.
10295 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
10296 return N; // No operands changed, just return the input node.
10297
10298 // See if the modified node already exists.
10299 void *InsertPos = nullptr;
10300 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
10301 return Existing;
10302
10303 // Nope it doesn't. Remove the node from its current place in the maps.
10304 if (InsertPos)
10305 if (!RemoveNodeFromCSEMaps(N))
10306 InsertPos = nullptr;
10307
10308 // Now we update the operands.
10309 if (N->OperandList[0] != Op1)
10310 N->OperandList[0].set(Op1);
10311 if (N->OperandList[1] != Op2)
10312 N->OperandList[1].set(Op2);
10313
10315 // If this gets put into a CSE map, add it.
10316 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10317 return N;
10318}
10319
10322 SDValue Ops[] = { Op1, Op2, Op3 };
10323 return UpdateNodeOperands(N, Ops);
10324}
10325
10328 SDValue Op3, SDValue Op4) {
10329 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
10330 return UpdateNodeOperands(N, Ops);
10331}
10332
10335 SDValue Op3, SDValue Op4, SDValue Op5) {
10336 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
10337 return UpdateNodeOperands(N, Ops);
10338}
10339
10342 unsigned NumOps = Ops.size();
10343 assert(N->getNumOperands() == NumOps &&
10344 "Update with wrong number of operands");
10345
10346 // If no operands changed just return the input node.
10347 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
10348 return N;
10349
10350 // See if the modified node already exists.
10351 void *InsertPos = nullptr;
10352 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
10353 return Existing;
10354
10355 // Nope it doesn't. Remove the node from its current place in the maps.
10356 if (InsertPos)
10357 if (!RemoveNodeFromCSEMaps(N))
10358 InsertPos = nullptr;
10359
10360 // Now we update the operands.
10361 for (unsigned i = 0; i != NumOps; ++i)
10362 if (N->OperandList[i] != Ops[i])
10363 N->OperandList[i].set(Ops[i]);
10364
10366 // If this gets put into a CSE map, add it.
10367 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10368 return N;
10369}
10370
10371/// DropOperands - Release the operands and set this node to have
10372/// zero operands.
10374 // Unlike the code in MorphNodeTo that does this, we don't need to
10375 // watch for dead nodes here.
10376 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
10377 SDUse &Use = *I++;
10378 Use.set(SDValue());
10379 }
10380}
10381
10383 ArrayRef<MachineMemOperand *> NewMemRefs) {
10384 if (NewMemRefs.empty()) {
10385 N->clearMemRefs();
10386 return;
10387 }
10388
10389 // Check if we can avoid allocating by storing a single reference directly.
10390 if (NewMemRefs.size() == 1) {
10391 N->MemRefs = NewMemRefs[0];
10392 N->NumMemRefs = 1;
10393 return;
10394 }
10395
10396 MachineMemOperand **MemRefsBuffer =
10397 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
10398 llvm::copy(NewMemRefs, MemRefsBuffer);
10399 N->MemRefs = MemRefsBuffer;
10400 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
10401}
10402
10403/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
10404/// machine opcode.
10405///
10407 EVT VT) {
10408 SDVTList VTs = getVTList(VT);
10409 return SelectNodeTo(N, MachineOpc, VTs, std::nullopt);
10410}
10411
10413 EVT VT, SDValue Op1) {
10414 SDVTList VTs = getVTList(VT);
10415 SDValue Ops[] = { Op1 };
10416 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10417}
10418
10420 EVT VT, SDValue Op1,
10421 SDValue Op2) {
10422 SDVTList VTs = getVTList(VT);
10423 SDValue Ops[] = { Op1, Op2 };
10424 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10425}
10426
10428 EVT VT, SDValue Op1,
10429 SDValue Op2, SDValue Op3) {
10430 SDVTList VTs = getVTList(VT);
10431 SDValue Ops[] = { Op1, Op2, Op3 };
10432 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10433}
10434
10436 EVT VT, ArrayRef<SDValue> Ops) {
10437 SDVTList VTs = getVTList(VT);
10438 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10439}
10440
10442 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
10443 SDVTList VTs = getVTList(VT1, VT2);
10444 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10445}
10446
10448 EVT VT1, EVT VT2) {
10449 SDVTList VTs = getVTList(VT1, VT2);
10450 return SelectNodeTo(N, MachineOpc, VTs, std::nullopt);
10451}
10452
10454 EVT VT1, EVT VT2, EVT VT3,
10455 ArrayRef<SDValue> Ops) {
10456 SDVTList VTs = getVTList(VT1, VT2, VT3);
10457 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10458}
10459
10461 EVT VT1, EVT VT2,
10462 SDValue Op1, SDValue Op2) {
10463 SDVTList VTs = getVTList(VT1, VT2);
10464 SDValue Ops[] = { Op1, Op2 };
10465 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10466}
10467
10469 SDVTList VTs,ArrayRef<SDValue> Ops) {
10470 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
10471 // Reset the NodeID to -1.
10472 New->setNodeId(-1);
10473 if (New != N) {
10474 ReplaceAllUsesWith(N, New);
10476 }
10477 return New;
10478}
10479
10480/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
10481/// the line number information on the merged node since it is not possible to
10482/// preserve the information that operation is associated with multiple lines.
10483/// This will make the debugger working better at -O0, were there is a higher
10484/// probability having other instructions associated with that line.
10485///
10486/// For IROrder, we keep the smaller of the two
10487SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
10488 DebugLoc NLoc = N->getDebugLoc();
10489 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
10490 N->setDebugLoc(DebugLoc());
10491 }
10492 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
10493 N->setIROrder(Order);
10494 return N;
10495}
10496
10497/// MorphNodeTo - This *mutates* the specified node to have the specified
10498/// return type, opcode, and operands.
10499///
10500/// Note that MorphNodeTo returns the resultant node. If there is already a
10501/// node of the specified opcode and operands, it returns that node instead of
10502/// the current one. Note that the SDLoc need not be the same.
10503///
10504/// Using MorphNodeTo is faster than creating a new node and swapping it in
10505/// with ReplaceAllUsesWith both because it often avoids allocating a new
10506/// node, and because it doesn't require CSE recalculation for any of
10507/// the node's users.
10508///
10509/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
10510/// As a consequence it isn't appropriate to use from within the DAG combiner or
10511/// the legalizer which maintain worklists that would need to be updated when
10512/// deleting things.
10514 SDVTList VTs, ArrayRef<SDValue> Ops) {
10515 // If an identical node already exists, use it.
10516 void *IP = nullptr;
10517 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
10519 AddNodeIDNode(ID, Opc, VTs, Ops);
10520 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
10521 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
10522 }
10523
10524 if (!RemoveNodeFromCSEMaps(N))
10525 IP = nullptr;
10526
10527 // Start the morphing.
10528 N->NodeType = Opc;
10529 N->ValueList = VTs.VTs;
10530 N->NumValues = VTs.NumVTs;
10531
10532 // Clear the operands list, updating used nodes to remove this from their
10533 // use list. Keep track of any operands that become dead as a result.
10534 SmallPtrSet<SDNode*, 16> DeadNodeSet;
10535 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
10536 SDUse &Use = *I++;
10537 SDNode *Used = Use.getNode();
10538 Use.set(SDValue());
10539 if (Used->use_empty())
10540 DeadNodeSet.insert(Used);
10541 }
10542
10543 // For MachineNode, initialize the memory references information.
10544 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
10545 MN->clearMemRefs();
10546
10547 // Swap for an appropriately sized array from the recycler.
10548 removeOperands(N);
10549 createOperands(N, Ops);
10550
10551 // Delete any nodes that are still dead after adding the uses for the
10552 // new operands.
10553 if (!DeadNodeSet.empty()) {
10554 SmallVector<SDNode *, 16> DeadNodes;
10555 for (SDNode *N : DeadNodeSet)
10556 if (N->use_empty())
10557 DeadNodes.push_back(N);
10558 RemoveDeadNodes(DeadNodes);
10559 }
10560
10561 if (IP)
10562 CSEMap.InsertNode(N, IP); // Memoize the new node.
10563 return N;
10564}
10565
10567 unsigned OrigOpc = Node->getOpcode();
10568 unsigned NewOpc;
10569 switch (OrigOpc) {
10570 default:
10571 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
10572#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
10573 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
10574#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
10575 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
10576#include "llvm/IR/ConstrainedOps.def"
10577 }
10578
10579 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
10580
10581 // We're taking this node out of the chain, so we need to re-link things.
10582 SDValue InputChain = Node->getOperand(0);
10583 SDValue OutputChain = SDValue(Node, 1);
10584 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
10585
10587 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
10588 Ops.push_back(Node->getOperand(i));
10589
10590 SDVTList VTs = getVTList(Node->getValueType(0));
10591 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
10592
10593 // MorphNodeTo can operate in two ways: if an existing node with the
10594 // specified operands exists, it can just return it. Otherwise, it
10595 // updates the node in place to have the requested operands.
10596 if (Res == Node) {
10597 // If we updated the node in place, reset the node ID. To the isel,
10598 // this should be just like a newly allocated machine node.
10599 Res->setNodeId(-1);
10600 } else {
10601 ReplaceAllUsesWith(Node, Res);
10602 RemoveDeadNode(Node);
10603 }
10604
10605 return Res;
10606}
10607
10608/// getMachineNode - These are used for target selectors to create a new node
10609/// with specified return type(s), MachineInstr opcode, and operands.
10610///
10611/// Note that getMachineNode returns the resultant node. If there is already a
10612/// node of the specified opcode and operands, it returns that node instead of
10613/// the current one.
10615 EVT VT) {
10616 SDVTList VTs = getVTList(VT);
10617 return getMachineNode(Opcode, dl, VTs, std::nullopt);
10618}
10619
10621 EVT VT, SDValue Op1) {
10622 SDVTList VTs = getVTList(VT);
10623 SDValue Ops[] = { Op1 };
10624 return getMachineNode(Opcode, dl, VTs, Ops);
10625}
10626
10628 EVT VT, SDValue Op1, SDValue Op2) {
10629 SDVTList VTs = getVTList(VT);
10630 SDValue Ops[] = { Op1, Op2 };
10631 return getMachineNode(Opcode, dl, VTs, Ops);
10632}
10633
10635 EVT VT, SDValue Op1, SDValue Op2,
10636 SDValue Op3) {
10637 SDVTList VTs = getVTList(VT);
10638 SDValue Ops[] = { Op1, Op2, Op3 };
10639 return getMachineNode(Opcode, dl, VTs, Ops);
10640}
10641
10643 EVT VT, ArrayRef<SDValue> Ops) {
10644 SDVTList VTs = getVTList(VT);
10645 return getMachineNode(Opcode, dl, VTs, Ops);
10646}
10647
10649 EVT VT1, EVT VT2, SDValue Op1,
10650 SDValue Op2) {
10651 SDVTList VTs = getVTList(VT1, VT2);
10652 SDValue Ops[] = { Op1, Op2 };
10653 return getMachineNode(Opcode, dl, VTs, Ops);
10654}
10655
10657 EVT VT1, EVT VT2, SDValue Op1,
10658 SDValue Op2, SDValue Op3) {
10659 SDVTList VTs = getVTList(VT1, VT2);
10660 SDValue Ops[] = { Op1, Op2, Op3 };
10661 return getMachineNode(Opcode, dl, VTs, Ops);
10662}
10663
10665 EVT VT1, EVT VT2,
10666 ArrayRef<SDValue> Ops) {
10667 SDVTList VTs = getVTList(VT1, VT2);
10668 return getMachineNode(Opcode, dl, VTs, Ops);
10669}
10670
10672 EVT VT1, EVT VT2, EVT VT3,
10673 SDValue Op1, SDValue Op2) {
10674 SDVTList VTs = getVTList(VT1, VT2, VT3);
10675 SDValue Ops[] = { Op1, Op2 };
10676 return getMachineNode(Opcode, dl, VTs, Ops);
10677}
10678
10680 EVT VT1, EVT VT2, EVT VT3,
10681 SDValue Op1, SDValue Op2,
10682 SDValue Op3) {
10683 SDVTList VTs = getVTList(VT1, VT2, VT3);
10684 SDValue Ops[] = { Op1, Op2, Op3 };
10685 return getMachineNode(Opcode, dl, VTs, Ops);
10686}
10687
10689 EVT VT1, EVT VT2, EVT VT3,
10690 ArrayRef<SDValue> Ops) {
10691 SDVTList VTs = getVTList(VT1, VT2, VT3);
10692 return getMachineNode(Opcode, dl, VTs, Ops);
10693}
10694
10696 ArrayRef<EVT> ResultTys,
10697 ArrayRef<SDValue> Ops) {
10698 SDVTList VTs = getVTList(ResultTys);
10699 return getMachineNode(Opcode, dl, VTs, Ops);
10700}
10701
10703 SDVTList VTs,
10704 ArrayRef<SDValue> Ops) {
10705 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
10707 void *IP = nullptr;
10708
10709 if (DoCSE) {
10711 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
10712 IP = nullptr;
10713 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10714 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
10715 }
10716 }
10717
10718 // Allocate a new MachineSDNode.
10719 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10720 createOperands(N, Ops);
10721
10722 if (DoCSE)
10723 CSEMap.InsertNode(N, IP);
10724
10725 InsertNode(N);
10726 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
10727 return N;
10728}
10729
10730/// getTargetExtractSubreg - A convenience function for creating
10731/// TargetOpcode::EXTRACT_SUBREG nodes.
10733 SDValue Operand) {
10734 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
10735 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
10736 VT, Operand, SRIdxVal);
10737 return SDValue(Subreg, 0);
10738}
10739
10740/// getTargetInsertSubreg - A convenience function for creating
10741/// TargetOpcode::INSERT_SUBREG nodes.
10743 SDValue Operand, SDValue Subreg) {
10744 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
10745 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
10746 VT, Operand, Subreg, SRIdxVal);
10747 return SDValue(Result, 0);
10748}
10749
10750/// getNodeIfExists - Get the specified node if it's already available, or
10751/// else return NULL.
10753 ArrayRef<SDValue> Ops) {
10754 SDNodeFlags Flags;
10755 if (Inserter)
10756 Flags = Inserter->getFlags();
10757 return getNodeIfExists(Opcode, VTList, Ops, Flags);
10758}
10759
10762 const SDNodeFlags Flags) {
10763 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
10765 AddNodeIDNode(ID, Opcode, VTList, Ops);
10766 void *IP = nullptr;
10767 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
10768 E->intersectFlagsWith(Flags);
10769 return E;
10770 }
10771 }
10772 return nullptr;
10773}
10774
10775/// doesNodeExist - Check if a node exists without modifying its flags.
10776bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
10777 ArrayRef<SDValue> Ops) {
10778 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
10780 AddNodeIDNode(ID, Opcode, VTList, Ops);
10781 void *IP = nullptr;
10782 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
10783 return true;
10784 }
10785 return false;
10786}
10787
10788/// getDbgValue - Creates a SDDbgValue node.
10789///
10790/// SDNode
10792 SDNode *N, unsigned R, bool IsIndirect,
10793 const DebugLoc &DL, unsigned O) {
10794 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10795 "Expected inlined-at fields to agree");
10796 return new (DbgInfo->getAlloc())
10797 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
10798 {}, IsIndirect, DL, O,
10799 /*IsVariadic=*/false);
10800}
10801
10802/// Constant
10804 DIExpression *Expr,
10805 const Value *C,
10806 const DebugLoc &DL, unsigned O) {
10807 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10808 "Expected inlined-at fields to agree");
10809 return new (DbgInfo->getAlloc())
10810 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
10811 /*IsIndirect=*/false, DL, O,
10812 /*IsVariadic=*/false);
10813}
10814
10815/// FrameIndex
10817 DIExpression *Expr, unsigned FI,
10818 bool IsIndirect,
10819 const DebugLoc &DL,
10820 unsigned O) {
10821 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10822 "Expected inlined-at fields to agree");
10823 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
10824}
10825
10826/// FrameIndex with dependencies
10828 DIExpression *Expr, unsigned FI,
10829 ArrayRef<SDNode *> Dependencies,
10830 bool IsIndirect,
10831 const DebugLoc &DL,
10832 unsigned O) {
10833 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10834 "Expected inlined-at fields to agree");
10835 return new (DbgInfo->getAlloc())
10836 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
10837 Dependencies, IsIndirect, DL, O,
10838 /*IsVariadic=*/false);
10839}
10840
10841/// VReg
10843 unsigned VReg, bool IsIndirect,
10844 const DebugLoc &DL, unsigned O) {
10845 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10846 "Expected inlined-at fields to agree");
10847 return new (DbgInfo->getAlloc())
10848 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
10849 {}, IsIndirect, DL, O,
10850 /*IsVariadic=*/false);
10851}
10852
10855 ArrayRef<SDNode *> Dependencies,
10856 bool IsIndirect, const DebugLoc &DL,
10857 unsigned O, bool IsVariadic) {
10858 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10859 "Expected inlined-at fields to agree");
10860 return new (DbgInfo->getAlloc())
10861 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
10862 DL, O, IsVariadic);
10863}
10864
10866 unsigned OffsetInBits, unsigned SizeInBits,
10867 bool InvalidateDbg) {
10868 SDNode *FromNode = From.getNode();
10869 SDNode *ToNode = To.getNode();
10870 assert(FromNode && ToNode && "Can't modify dbg values");
10871
10872 // PR35338
10873 // TODO: assert(From != To && "Redundant dbg value transfer");
10874 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
10875 if (From == To || FromNode == ToNode)
10876 return;
10877
10878 if (!FromNode->getHasDebugValue())
10879 return;
10880
10881 SDDbgOperand FromLocOp =
10882 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
10884
10886 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
10887 if (Dbg->isInvalidated())
10888 continue;
10889
10890 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
10891
10892 // Create a new location ops vector that is equal to the old vector, but
10893 // with each instance of FromLocOp replaced with ToLocOp.
10894 bool Changed = false;
10895 auto NewLocOps = Dbg->copyLocationOps();
10896 std::replace_if(
10897 NewLocOps.begin(), NewLocOps.end(),
10898 [&Changed, FromLocOp](const SDDbgOperand &Op) {
10899 bool Match = Op == FromLocOp;
10900 Changed |= Match;
10901 return Match;
10902 },
10903 ToLocOp);
10904 // Ignore this SDDbgValue if we didn't find a matching location.
10905 if (!Changed)
10906 continue;
10907
10908 DIVariable *Var = Dbg->getVariable();
10909 auto *Expr = Dbg->getExpression();
10910 // If a fragment is requested, update the expression.
10911 if (SizeInBits) {
10912 // When splitting a larger (e.g., sign-extended) value whose
10913 // lower bits are described with an SDDbgValue, do not attempt
10914 // to transfer the SDDbgValue to the upper bits.
10915 if (auto FI = Expr->getFragmentInfo())
10916 if (OffsetInBits + SizeInBits > FI->SizeInBits)
10917 continue;
10918 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
10919 SizeInBits);
10920 if (!Fragment)
10921 continue;
10922 Expr = *Fragment;
10923 }
10924
10925 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
10926 // Clone the SDDbgValue and move it to To.
10927 SDDbgValue *Clone = getDbgValueList(
10928 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
10929 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
10930 Dbg->isVariadic());
10931 ClonedDVs.push_back(Clone);
10932
10933 if (InvalidateDbg) {
10934 // Invalidate value and indicate the SDDbgValue should not be emitted.
10935 Dbg->setIsInvalidated();
10936 Dbg->setIsEmitted();
10937 }
10938 }
10939
10940 for (SDDbgValue *Dbg : ClonedDVs) {
10941 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
10942 "Transferred DbgValues should depend on the new SDNode");
10943 AddDbgValue(Dbg, false);
10944 }
10945}
10946
10948 if (!N.getHasDebugValue())
10949 return;
10950
10952 for (auto *DV : GetDbgValues(&N)) {
10953 if (DV->isInvalidated())
10954 continue;
10955 switch (N.getOpcode()) {
10956 default:
10957 break;
10958 case ISD::ADD: {
10959 SDValue N0 = N.getOperand(0);
10960 SDValue N1 = N.getOperand(1);
10961 if (!isa<ConstantSDNode>(N0)) {
10962 bool RHSConstant = isa<ConstantSDNode>(N1);
10964 if (RHSConstant)
10965 Offset = N.getConstantOperandVal(1);
10966 // We are not allowed to turn indirect debug values variadic, so
10967 // don't salvage those.
10968 if (!RHSConstant && DV->isIndirect())
10969 continue;
10970
10971 // Rewrite an ADD constant node into a DIExpression. Since we are
10972 // performing arithmetic to compute the variable's *value* in the
10973 // DIExpression, we need to mark the expression with a
10974 // DW_OP_stack_value.
10975 auto *DIExpr = DV->getExpression();
10976 auto NewLocOps = DV->copyLocationOps();
10977 bool Changed = false;
10978 size_t OrigLocOpsSize = NewLocOps.size();
10979 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
10980 // We're not given a ResNo to compare against because the whole
10981 // node is going away. We know that any ISD::ADD only has one
10982 // result, so we can assume any node match is using the result.
10983 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
10984 NewLocOps[i].getSDNode() != &N)
10985 continue;
10986 NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
10987 if (RHSConstant) {
10990 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
10991 } else {
10992 // Convert to a variadic expression (if not already).
10993 // convertToVariadicExpression() returns a const pointer, so we use
10994 // a temporary const variable here.
10995 const auto *TmpDIExpr =
10999 ExprOps.push_back(NewLocOps.size());
11000 ExprOps.push_back(dwarf::DW_OP_plus);
11003 NewLocOps.push_back(RHS);
11004 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
11005 }
11006 Changed = true;
11007 }
11008 (void)Changed;
11009 assert(Changed && "Salvage target doesn't use N");
11010
11011 bool IsVariadic =
11012 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
11013
11014 auto AdditionalDependencies = DV->getAdditionalDependencies();
11015 SDDbgValue *Clone = getDbgValueList(
11016 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
11017 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
11018 ClonedDVs.push_back(Clone);
11019 DV->setIsInvalidated();
11020 DV->setIsEmitted();
11021 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
11022 N0.getNode()->dumprFull(this);
11023 dbgs() << " into " << *DIExpr << '\n');
11024 }
11025 break;
11026 }
11027 case ISD::TRUNCATE: {
11028 SDValue N0 = N.getOperand(0);
11029 TypeSize FromSize = N0.getValueSizeInBits();
11030 TypeSize ToSize = N.getValueSizeInBits(0);
11031
11032 DIExpression *DbgExpression = DV->getExpression();
11033 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
11034 auto NewLocOps = DV->copyLocationOps();
11035 bool Changed = false;
11036 for (size_t i = 0; i < NewLocOps.size(); ++i) {
11037 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11038 NewLocOps[i].getSDNode() != &N)
11039 continue;
11040
11041 NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
11042 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
11043 Changed = true;
11044 }
11045 assert(Changed && "Salvage target doesn't use N");
11046 (void)Changed;
11047
11048 SDDbgValue *Clone =
11049 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
11050 DV->getAdditionalDependencies(), DV->isIndirect(),
11051 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
11052
11053 ClonedDVs.push_back(Clone);
11054 DV->setIsInvalidated();
11055 DV->setIsEmitted();
11056 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
11057 dbgs() << " into " << *DbgExpression << '\n');
11058 break;
11059 }
11060 }
11061 }
11062
11063 for (SDDbgValue *Dbg : ClonedDVs) {
11064 assert(!Dbg->getSDNodes().empty() &&
11065 "Salvaged DbgValue should depend on a new SDNode");
11066 AddDbgValue(Dbg, false);
11067 }
11068}
11069
11070/// Creates a SDDbgLabel node.
11072 const DebugLoc &DL, unsigned O) {
11073 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
11074 "Expected inlined-at fields to agree");
11075 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
11076}
11077
11078namespace {
11079
11080/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
11081/// pointed to by a use iterator is deleted, increment the use iterator
11082/// so that it doesn't dangle.
11083///
11084class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
11087
11088 void NodeDeleted(SDNode *N, SDNode *E) override {
11089 // Increment the iterator as needed.
11090 while (UI != UE && N == *UI)
11091 ++UI;
11092 }
11093
11094public:
11095 RAUWUpdateListener(SelectionDAG &d,
11098 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
11099};
11100
11101} // end anonymous namespace
11102
11103/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11104/// This can cause recursive merging of nodes in the DAG.
11105///
11106/// This version assumes From has a single result value.
11107///
11109 SDNode *From = FromN.getNode();
11110 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
11111 "Cannot replace with this method!");
11112 assert(From != To.getNode() && "Cannot replace uses of with self");
11113
11114 // Preserve Debug Values
11115 transferDbgValues(FromN, To);
11116 // Preserve extra info.
11117 copyExtraInfo(From, To.getNode());
11118
11119 // Iterate over all the existing uses of From. New uses will be added
11120 // to the beginning of the use list, which we avoid visiting.
11121 // This specifically avoids visiting uses of From that arise while the
11122 // replacement is happening, because any such uses would be the result
11123 // of CSE: If an existing node looks like From after one of its operands
11124 // is replaced by To, we don't want to replace of all its users with To
11125 // too. See PR3018 for more info.
11126 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11127 RAUWUpdateListener Listener(*this, UI, UE);
11128 while (UI != UE) {
11129 SDNode *User = *UI;
11130
11131 // This node is about to morph, remove its old self from the CSE maps.
11132 RemoveNodeFromCSEMaps(User);
11133
11134 // A user can appear in a use list multiple times, and when this
11135 // happens the uses are usually next to each other in the list.
11136 // To help reduce the number of CSE recomputations, process all
11137 // the uses of this user that we can find this way.
11138 do {
11139 SDUse &Use = UI.getUse();
11140 ++UI;
11141 Use.set(To);
11142 if (To->isDivergent() != From->isDivergent())
11144 } while (UI != UE && *UI == User);
11145 // Now that we have modified User, add it back to the CSE maps. If it
11146 // already exists there, recursively merge the results together.
11147 AddModifiedNodeToCSEMaps(User);
11148 }
11149
11150 // If we just RAUW'd the root, take note.
11151 if (FromN == getRoot())
11152 setRoot(To);
11153}
11154
11155/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11156/// This can cause recursive merging of nodes in the DAG.
11157///
11158/// This version assumes that for each value of From, there is a
11159/// corresponding value in To in the same position with the same type.
11160///
11162#ifndef NDEBUG
11163 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11164 assert((!From->hasAnyUseOfValue(i) ||
11165 From->getValueType(i) == To->getValueType(i)) &&
11166 "Cannot use this version of ReplaceAllUsesWith!");
11167#endif
11168
11169 // Handle the trivial case.
11170 if (From == To)
11171 return;
11172
11173 // Preserve Debug Info. Only do this if there's a use.
11174 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11175 if (From->hasAnyUseOfValue(i)) {
11176 assert((i < To->getNumValues()) && "Invalid To location");
11178 }
11179 // Preserve extra info.
11180 copyExtraInfo(From, To);
11181
11182 // Iterate over just the existing users of From. See the comments in
11183 // the ReplaceAllUsesWith above.
11184 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11185 RAUWUpdateListener Listener(*this, UI, UE);
11186 while (UI != UE) {
11187 SDNode *User = *UI;
11188
11189 // This node is about to morph, remove its old self from the CSE maps.
11190 RemoveNodeFromCSEMaps(User);
11191
11192 // A user can appear in a use list multiple times, and when this
11193 // happens the uses are usually next to each other in the list.
11194 // To help reduce the number of CSE recomputations, process all
11195 // the uses of this user that we can find this way.
11196 do {
11197 SDUse &Use = UI.getUse();
11198 ++UI;
11199 Use.setNode(To);
11200 if (To->isDivergent() != From->isDivergent())
11202 } while (UI != UE && *UI == User);
11203
11204 // Now that we have modified User, add it back to the CSE maps. If it
11205 // already exists there, recursively merge the results together.
11206 AddModifiedNodeToCSEMaps(User);
11207 }
11208
11209 // If we just RAUW'd the root, take note.
11210 if (From == getRoot().getNode())
11211 setRoot(SDValue(To, getRoot().getResNo()));
11212}
11213
11214/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11215/// This can cause recursive merging of nodes in the DAG.
11216///
11217/// This version can replace From with any result values. To must match the
11218/// number and types of values returned by From.
11220 if (From->getNumValues() == 1) // Handle the simple case efficiently.
11221 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
11222
11223 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
11224 // Preserve Debug Info.
11225 transferDbgValues(SDValue(From, i), To[i]);
11226 // Preserve extra info.
11227 copyExtraInfo(From, To[i].getNode());
11228 }
11229
11230 // Iterate over just the existing users of From. See the comments in
11231 // the ReplaceAllUsesWith above.
11232 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11233 RAUWUpdateListener Listener(*this, UI, UE);
11234 while (UI != UE) {
11235 SDNode *User = *UI;
11236
11237 // This node is about to morph, remove its old self from the CSE maps.
11238 RemoveNodeFromCSEMaps(User);
11239
11240 // A user can appear in a use list multiple times, and when this happens the
11241 // uses are usually next to each other in the list. To help reduce the
11242 // number of CSE and divergence recomputations, process all the uses of this
11243 // user that we can find this way.
11244 bool To_IsDivergent = false;
11245 do {
11246 SDUse &Use = UI.getUse();
11247 const SDValue &ToOp = To[Use.getResNo()];
11248 ++UI;
11249 Use.set(ToOp);
11250 To_IsDivergent |= ToOp->isDivergent();
11251 } while (UI != UE && *UI == User);
11252
11253 if (To_IsDivergent != From->isDivergent())
11255
11256 // Now that we have modified User, add it back to the CSE maps. If it
11257 // already exists there, recursively merge the results together.
11258 AddModifiedNodeToCSEMaps(User);
11259 }
11260
11261 // If we just RAUW'd the root, take note.
11262 if (From == getRoot().getNode())
11263 setRoot(SDValue(To[getRoot().getResNo()]));
11264}
11265
11266/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
11267/// uses of other values produced by From.getNode() alone. The Deleted
11268/// vector is handled the same way as for ReplaceAllUsesWith.
11270 // Handle the really simple, really trivial case efficiently.
11271 if (From == To) return;
11272
11273 // Handle the simple, trivial, case efficiently.
11274 if (From.getNode()->getNumValues() == 1) {
11276 return;
11277 }
11278
11279 // Preserve Debug Info.
11281 copyExtraInfo(From.getNode(), To.getNode());
11282
11283 // Iterate over just the existing users of From. See the comments in
11284 // the ReplaceAllUsesWith above.
11285 SDNode::use_iterator UI = From.getNode()->use_begin(),
11286 UE = From.getNode()->use_end();
11287 RAUWUpdateListener Listener(*this, UI, UE);
11288 while (UI != UE) {
11289 SDNode *User = *UI;
11290 bool UserRemovedFromCSEMaps = false;
11291
11292 // A user can appear in a use list multiple times, and when this
11293 // happens the uses are usually next to each other in the list.
11294 // To help reduce the number of CSE recomputations, process all
11295 // the uses of this user that we can find this way.
11296 do {
11297 SDUse &Use = UI.getUse();
11298
11299 // Skip uses of different values from the same node.
11300 if (Use.getResNo() != From.getResNo()) {
11301 ++UI;
11302 continue;
11303 }
11304
11305 // If this node hasn't been modified yet, it's still in the CSE maps,
11306 // so remove its old self from the CSE maps.
11307 if (!UserRemovedFromCSEMaps) {
11308 RemoveNodeFromCSEMaps(User);
11309 UserRemovedFromCSEMaps = true;
11310 }
11311
11312 ++UI;
11313 Use.set(To);
11314 if (To->isDivergent() != From->isDivergent())
11316 } while (UI != UE && *UI == User);
11317 // We are iterating over all uses of the From node, so if a use
11318 // doesn't use the specific value, no changes are made.
11319 if (!UserRemovedFromCSEMaps)
11320 continue;
11321
11322 // Now that we have modified User, add it back to the CSE maps. If it
11323 // already exists there, recursively merge the results together.
11324 AddModifiedNodeToCSEMaps(User);
11325 }
11326
11327 // If we just RAUW'd the root, take note.
11328 if (From == getRoot())
11329 setRoot(To);
11330}
11331
11332namespace {
11333
11334/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
11335/// to record information about a use.
11336struct UseMemo {
11337 SDNode *User;
11338 unsigned Index;
11339 SDUse *Use;
11340};
11341
11342/// operator< - Sort Memos by User.
11343bool operator<(const UseMemo &L, const UseMemo &R) {
11344 return (intptr_t)L.User < (intptr_t)R.User;
11345}
11346
11347/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
11348/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
11349/// the node already has been taken care of recursively.
11350class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
11352
11353 void NodeDeleted(SDNode *N, SDNode *E) override {
11354 for (UseMemo &Memo : Uses)
11355 if (Memo.User == N)
11356 Memo.User = nullptr;
11357 }
11358
11359public:
11360 RAUOVWUpdateListener(SelectionDAG &d, SmallVector<UseMemo, 4> &uses)
11361 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
11362};
11363
11364} // end anonymous namespace
11365
11367 if (TLI->isSDNodeAlwaysUniform(N)) {
11368 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
11369 "Conflicting divergence information!");
11370 return false;
11371 }
11372 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
11373 return true;
11374 for (const auto &Op : N->ops()) {
11375 if (Op.Val.getValueType() != MVT::Other && Op.getNode()->isDivergent())
11376 return true;
11377 }
11378 return false;
11379}
11380
11382 SmallVector<SDNode *, 16> Worklist(1, N);
11383 do {
11384 N = Worklist.pop_back_val();
11385 bool IsDivergent = calculateDivergence(N);
11386 if (N->SDNodeBits.IsDivergent != IsDivergent) {
11387 N->SDNodeBits.IsDivergent = IsDivergent;
11388 llvm::append_range(Worklist, N->uses());
11389 }
11390 } while (!Worklist.empty());
11391}
11392
11393void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
11395 Order.reserve(AllNodes.size());
11396 for (auto &N : allnodes()) {
11397 unsigned NOps = N.getNumOperands();
11398 Degree[&N] = NOps;
11399 if (0 == NOps)
11400 Order.push_back(&N);
11401 }
11402 for (size_t I = 0; I != Order.size(); ++I) {
11403 SDNode *N = Order[I];
11404 for (auto *U : N->uses()) {
11405 unsigned &UnsortedOps = Degree[U];
11406 if (0 == --UnsortedOps)
11407 Order.push_back(U);
11408 }
11409 }
11410}
11411
11412#ifndef NDEBUG
11414 std::vector<SDNode *> TopoOrder;
11415 CreateTopologicalOrder(TopoOrder);
11416 for (auto *N : TopoOrder) {
11417 assert(calculateDivergence(N) == N->isDivergent() &&
11418 "Divergence bit inconsistency detected");
11419 }
11420}
11421#endif
11422
11423/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
11424/// uses of other values produced by From.getNode() alone. The same value
11425/// may appear in both the From and To list. The Deleted vector is
11426/// handled the same way as for ReplaceAllUsesWith.
11428 const SDValue *To,
11429 unsigned Num){
11430 // Handle the simple, trivial case efficiently.
11431 if (Num == 1)
11432 return ReplaceAllUsesOfValueWith(*From, *To);
11433
11434 transferDbgValues(*From, *To);
11435 copyExtraInfo(From->getNode(), To->getNode());
11436
11437 // Read up all the uses and make records of them. This helps
11438 // processing new uses that are introduced during the
11439 // replacement process.
11441 for (unsigned i = 0; i != Num; ++i) {
11442 unsigned FromResNo = From[i].getResNo();
11443 SDNode *FromNode = From[i].getNode();
11444 for (SDNode::use_iterator UI = FromNode->use_begin(),
11445 E = FromNode->use_end(); UI != E; ++UI) {
11446 SDUse &Use = UI.getUse();
11447 if (Use.getResNo() == FromResNo) {
11448 UseMemo Memo = { *UI, i, &Use };
11449 Uses.push_back(Memo);
11450 }
11451 }
11452 }
11453
11454 // Sort the uses, so that all the uses from a given User are together.
11456 RAUOVWUpdateListener Listener(*this, Uses);
11457
11458 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
11459 UseIndex != UseIndexEnd; ) {
11460 // We know that this user uses some value of From. If it is the right
11461 // value, update it.
11462 SDNode *User = Uses[UseIndex].User;
11463 // If the node has been deleted by recursive CSE updates when updating
11464 // another node, then just skip this entry.
11465 if (User == nullptr) {
11466 ++UseIndex;
11467 continue;
11468 }
11469
11470 // This node is about to morph, remove its old self from the CSE maps.
11471 RemoveNodeFromCSEMaps(User);
11472
11473 // The Uses array is sorted, so all the uses for a given User
11474 // are next to each other in the list.
11475 // To help reduce the number of CSE recomputations, process all
11476 // the uses of this user that we can find this way.
11477 do {
11478 unsigned i = Uses[UseIndex].Index;
11479 SDUse &Use = *Uses[UseIndex].Use;
11480 ++UseIndex;
11481
11482 Use.set(To[i]);
11483 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
11484
11485 // Now that we have modified User, add it back to the CSE maps. If it
11486 // already exists there, recursively merge the results together.
11487 AddModifiedNodeToCSEMaps(User);
11488 }
11489}
11490
11491/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
11492/// based on their topological order. It returns the maximum id and a vector
11493/// of the SDNodes* in assigned order by reference.
11495 unsigned DAGSize = 0;
11496
11497 // SortedPos tracks the progress of the algorithm. Nodes before it are
11498 // sorted, nodes after it are unsorted. When the algorithm completes
11499 // it is at the end of the list.
11500 allnodes_iterator SortedPos = allnodes_begin();
11501
11502 // Visit all the nodes. Move nodes with no operands to the front of
11503 // the list immediately. Annotate nodes that do have operands with their
11504 // operand count. Before we do this, the Node Id fields of the nodes
11505 // may contain arbitrary values. After, the Node Id fields for nodes
11506 // before SortedPos will contain the topological sort index, and the
11507 // Node Id fields for nodes At SortedPos and after will contain the
11508 // count of outstanding operands.
11510 checkForCycles(&N, this);
11511 unsigned Degree = N.getNumOperands();
11512 if (Degree == 0) {
11513 // A node with no uses, add it to the result array immediately.
11514 N.setNodeId(DAGSize++);
11515 allnodes_iterator Q(&N);
11516 if (Q != SortedPos)
11517 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
11518 assert(SortedPos != AllNodes.end() && "Overran node list");
11519 ++SortedPos;
11520 } else {
11521 // Temporarily use the Node Id as scratch space for the degree count.
11522 N.setNodeId(Degree);
11523 }
11524 }
11525
11526 // Visit all the nodes. As we iterate, move nodes into sorted order,
11527 // such that by the time the end is reached all nodes will be sorted.
11528 for (SDNode &Node : allnodes()) {
11529 SDNode *N = &Node;
11530 checkForCycles(N, this);
11531 // N is in sorted position, so all its uses have one less operand
11532 // that needs to be sorted.
11533 for (SDNode *P : N->uses()) {
11534 unsigned Degree = P->getNodeId();
11535 assert(Degree != 0 && "Invalid node degree");
11536 --Degree;
11537 if (Degree == 0) {
11538 // All of P's operands are sorted, so P may sorted now.
11539 P->setNodeId(DAGSize++);
11540 if (P->getIterator() != SortedPos)
11541 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
11542 assert(SortedPos != AllNodes.end() && "Overran node list");
11543 ++SortedPos;
11544 } else {
11545 // Update P's outstanding operand count.
11546 P->setNodeId(Degree);
11547 }
11548 }
11549 if (Node.getIterator() == SortedPos) {
11550#ifndef NDEBUG
11552 SDNode *S = &*++I;
11553 dbgs() << "Overran sorted position:\n";
11554 S->dumprFull(this); dbgs() << "\n";
11555 dbgs() << "Checking if this is due to cycles\n";
11556 checkForCycles(this, true);
11557#endif
11558 llvm_unreachable(nullptr);
11559 }
11560 }
11561
11562 assert(SortedPos == AllNodes.end() &&
11563 "Topological sort incomplete!");
11564 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
11565 "First node in topological sort is not the entry token!");
11566 assert(AllNodes.front().getNodeId() == 0 &&
11567 "First node in topological sort has non-zero id!");
11568 assert(AllNodes.front().getNumOperands() == 0 &&
11569 "First node in topological sort has operands!");
11570 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
11571 "Last node in topologic sort has unexpected id!");
11572 assert(AllNodes.back().use_empty() &&
11573 "Last node in topologic sort has users!");
11574 assert(DAGSize == allnodes_size() && "Node count mismatch!");
11575 return DAGSize;
11576}
11577
11578/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
11579/// value is produced by SD.
11580void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
11581 for (SDNode *SD : DB->getSDNodes()) {
11582 if (!SD)
11583 continue;
11584 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
11585 SD->setHasDebugValue(true);
11586 }
11587 DbgInfo->add(DB, isParameter);
11588}
11589
11590void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
11591
11593 SDValue NewMemOpChain) {
11594 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
11595 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
11596 // The new memory operation must have the same position as the old load in
11597 // terms of memory dependency. Create a TokenFactor for the old load and new
11598 // memory operation and update uses of the old load's output chain to use that
11599 // TokenFactor.
11600 if (OldChain == NewMemOpChain || OldChain.use_empty())
11601 return NewMemOpChain;
11602
11603 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
11604 OldChain, NewMemOpChain);
11605 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
11606 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
11607 return TokenFactor;
11608}
11609
11611 SDValue NewMemOp) {
11612 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
11613 SDValue OldChain = SDValue(OldLoad, 1);
11614 SDValue NewMemOpChain = NewMemOp.getValue(1);
11615 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
11616}
11617
11619 Function **OutFunction) {
11620 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
11621
11622 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
11623 auto *Module = MF->getFunction().getParent();
11624 auto *Function = Module->getFunction(Symbol);
11625
11626 if (OutFunction != nullptr)
11627 *OutFunction = Function;
11628
11629 if (Function != nullptr) {
11630 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
11631 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
11632 }
11633
11634 std::string ErrorStr;
11635 raw_string_ostream ErrorFormatter(ErrorStr);
11636 ErrorFormatter << "Undefined external symbol ";
11637 ErrorFormatter << '"' << Symbol << '"';
11638 report_fatal_error(Twine(ErrorFormatter.str()));
11639}
11640
11641//===----------------------------------------------------------------------===//
11642// SDNode Class
11643//===----------------------------------------------------------------------===//
11644
11646 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11647 return Const != nullptr && Const->isZero();
11648}
11649
11651 ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
11652 return Const != nullptr && Const->isZero() && !Const->isNegative();
11653}
11654
11656 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11657 return Const != nullptr && Const->isAllOnes();
11658}
11659
11661 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11662 return Const != nullptr && Const->isOne();
11663}
11664
11666 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11667 return Const != nullptr && Const->isMinSignedValue();
11668}
11669
11670bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
11671 unsigned OperandNo) {
11672 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
11673 // TODO: Target-specific opcodes could be added.
11674 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
11675 /*AllowTruncation*/ true)) {
11676 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
11677 switch (Opcode) {
11678 case ISD::ADD:
11679 case ISD::OR:
11680 case ISD::XOR:
11681 case ISD::UMAX:
11682 return Const.isZero();
11683 case ISD::MUL:
11684 return Const.isOne();
11685 case ISD::AND:
11686 case ISD::UMIN:
11687 return Const.isAllOnes();
11688 case ISD::SMAX:
11689 return Const.isMinSignedValue();
11690 case ISD::SMIN:
11691 return Const.isMaxSignedValue();
11692 case ISD::SUB:
11693 case ISD::SHL:
11694 case ISD::SRA:
11695 case ISD::SRL:
11696 return OperandNo == 1 && Const.isZero();
11697 case ISD::UDIV:
11698 case ISD::SDIV:
11699 return OperandNo == 1 && Const.isOne();
11700 }
11701 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
11702 switch (Opcode) {
11703 case ISD::FADD:
11704 return ConstFP->isZero() &&
11705 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
11706 case ISD::FSUB:
11707 return OperandNo == 1 && ConstFP->isZero() &&
11708 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
11709 case ISD::FMUL:
11710 return ConstFP->isExactlyValue(1.0);
11711 case ISD::FDIV:
11712 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
11713 case ISD::FMINNUM:
11714 case ISD::FMAXNUM: {
11715 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
11716 EVT VT = V.getValueType();
11718 APFloat NeutralAF = !Flags.hasNoNaNs()
11719 ? APFloat::getQNaN(Semantics)
11720 : !Flags.hasNoInfs()
11721 ? APFloat::getInf(Semantics)
11722 : APFloat::getLargest(Semantics);
11723 if (Opcode == ISD::FMAXNUM)
11724 NeutralAF.changeSign();
11725
11726 return ConstFP->isExactlyValue(NeutralAF);
11727 }
11728 }
11729 }
11730 return false;
11731}
11732
11734 while (V.getOpcode() == ISD::BITCAST)
11735 V = V.getOperand(0);
11736 return V;
11737}
11738
11740 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
11741 V = V.getOperand(0);
11742 return V;
11743}
11744
11746 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
11747 V = V.getOperand(0);
11748 return V;
11749}
11750
11752 while (V.getOpcode() == ISD::TRUNCATE)
11753 V = V.getOperand(0);
11754 return V;
11755}
11756
11757bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
11758 if (V.getOpcode() != ISD::XOR)
11759 return false;
11760 V = peekThroughBitcasts(V.getOperand(1));
11761 unsigned NumBits = V.getScalarValueSizeInBits();
11762 ConstantSDNode *C =
11763 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
11764 return C && (C->getAPIntValue().countr_one() >= NumBits);
11765}
11766
11768 bool AllowTruncation) {
11769 EVT VT = N.getValueType();
11770 APInt DemandedElts = VT.isFixedLengthVector()
11772 : APInt(1, 1);
11773 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
11774}
11775
11777 bool AllowUndefs,
11778 bool AllowTruncation) {
11779 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
11780 return CN;
11781
11782 // SplatVectors can truncate their operands. Ignore that case here unless
11783 // AllowTruncation is set.
11784 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
11785 EVT VecEltVT = N->getValueType(0).getVectorElementType();
11786 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11787 EVT CVT = CN->getValueType(0);
11788 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
11789 if (AllowTruncation || CVT == VecEltVT)
11790 return CN;
11791 }
11792 }
11793
11794 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
11795 BitVector UndefElements;
11796 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
11797
11798 // BuildVectors can truncate their operands. Ignore that case here unless
11799 // AllowTruncation is set.
11800 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
11801 if (CN && (UndefElements.none() || AllowUndefs)) {
11802 EVT CVT = CN->getValueType(0);
11803 EVT NSVT = N.getValueType().getScalarType();
11804 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
11805 if (AllowTruncation || (CVT == NSVT))
11806 return CN;
11807 }
11808 }
11809
11810 return nullptr;
11811}
11812
11814 EVT VT = N.getValueType();
11815 APInt DemandedElts = VT.isFixedLengthVector()
11817 : APInt(1, 1);
11818 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
11819}
11820
11822 const APInt &DemandedElts,
11823 bool AllowUndefs) {
11824 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
11825 return CN;
11826
11827 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
11828 BitVector UndefElements;
11829 ConstantFPSDNode *CN =
11830 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
11831 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
11832 if (CN && (UndefElements.none() || AllowUndefs))
11833 return CN;
11834 }
11835
11836 if (N.getOpcode() == ISD::SPLAT_VECTOR)
11837 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
11838 return CN;
11839
11840 return nullptr;
11841}
11842
11843bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
11844 // TODO: may want to use peekThroughBitcast() here.
11845 ConstantSDNode *C =
11846 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
11847 return C && C->isZero();
11848}
11849
11850bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
11851 ConstantSDNode *C =
11852 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
11853 return C && C->isOne();
11854}
11855
11856bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
11858 unsigned BitWidth = N.getScalarValueSizeInBits();
11859 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
11860 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
11861}
11862
11864 DropOperands();
11865}
11866
11867MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
11868 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
11869 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
11870 MemSDNodeBits.IsVolatile = MMO->isVolatile();
11871 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
11872 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
11873 MemSDNodeBits.IsInvariant = MMO->isInvariant();
11874
11875 // We check here that the size of the memory operand fits within the size of
11876 // the MMO. This is because the MMO might indicate only a possible address
11877 // range instead of specifying the affected memory addresses precisely.
11878 assert(
11879 (!MMO->getType().isValid() ||
11881 "Size mismatch!");
11882}
11883
11884/// Profile - Gather unique data for the node.
11885///
11887 AddNodeIDNode(ID, this);
11888}
11889
11890namespace {
11891
11892 struct EVTArray {
11893 std::vector<EVT> VTs;
11894
11895 EVTArray() {
11896 VTs.reserve(MVT::VALUETYPE_SIZE);
11897 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
11898 VTs.push_back(MVT((MVT::SimpleValueType)i));
11899 }
11900 };
11901
11902} // end anonymous namespace
11903
11904/// getValueTypeList - Return a pointer to the specified value type.
11905///
11906const EVT *SDNode::getValueTypeList(EVT VT) {
11907 static std::set<EVT, EVT::compareRawBits> EVTs;
11908 static EVTArray SimpleVTArray;
11909 static sys::SmartMutex<true> VTMutex;
11910
11911 if (VT.isExtended()) {
11912 sys::SmartScopedLock<true> Lock(VTMutex);
11913 return &(*EVTs.insert(VT).first);
11914 }
11915 assert(VT.getSimpleVT() < MVT::VALUETYPE_SIZE && "Value type out of range!");
11916 return &SimpleVTArray.VTs[VT.getSimpleVT().SimpleTy];
11917}
11918
11919/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
11920/// indicated value. This method ignores uses of other values defined by this
11921/// operation.
11922bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
11923 assert(Value < getNumValues() && "Bad value!");
11924
11925 // TODO: Only iterate over uses of a given value of the node
11926 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
11927 if (UI.getUse().getResNo() == Value) {
11928 if (NUses == 0)
11929 return false;
11930 --NUses;
11931 }
11932 }
11933
11934 // Found exactly the right number of uses?
11935 return NUses == 0;
11936}
11937
11938/// hasAnyUseOfValue - Return true if there are any use of the indicated
11939/// value. This method ignores uses of other values defined by this operation.
11940bool SDNode::hasAnyUseOfValue(unsigned Value) const {
11941 assert(Value < getNumValues() && "Bad value!");
11942
11943 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
11944 if (UI.getUse().getResNo() == Value)
11945 return true;
11946
11947 return false;
11948}
11949
11950/// isOnlyUserOf - Return true if this node is the only use of N.
11951bool SDNode::isOnlyUserOf(const SDNode *N) const {
11952 bool Seen = false;
11953 for (const SDNode *User : N->uses()) {
11954 if (User == this)
11955 Seen = true;
11956 else
11957 return false;
11958 }
11959
11960 return Seen;
11961}
11962
11963/// Return true if the only users of N are contained in Nodes.
11965 bool Seen = false;
11966 for (const SDNode *User : N->uses()) {
11967 if (llvm::is_contained(Nodes, User))
11968 Seen = true;
11969 else
11970 return false;
11971 }
11972
11973 return Seen;
11974}
11975
11976/// isOperand - Return true if this node is an operand of N.
11977bool SDValue::isOperandOf(const SDNode *N) const {
11978 return is_contained(N->op_values(), *this);
11979}
11980
11981bool SDNode::isOperandOf(const SDNode *N) const {
11982 return any_of(N->op_values(),
11983 [this](SDValue Op) { return this == Op.getNode(); });
11984}
11985
11986/// reachesChainWithoutSideEffects - Return true if this operand (which must
11987/// be a chain) reaches the specified operand without crossing any
11988/// side-effecting instructions on any chain path. In practice, this looks
11989/// through token factors and non-volatile loads. In order to remain efficient,
11990/// this only looks a couple of nodes in, it does not do an exhaustive search.
11991///
11992/// Note that we only need to examine chains when we're searching for
11993/// side-effects; SelectionDAG requires that all side-effects are represented
11994/// by chains, even if another operand would force a specific ordering. This
11995/// constraint is necessary to allow transformations like splitting loads.
11997 unsigned Depth) const {
11998 if (*this == Dest) return true;
11999
12000 // Don't search too deeply, we just want to be able to see through
12001 // TokenFactor's etc.
12002 if (Depth == 0) return false;
12003
12004 // If this is a token factor, all inputs to the TF happen in parallel.
12005 if (getOpcode() == ISD::TokenFactor) {
12006 // First, try a shallow search.
12007 if (is_contained((*this)->ops(), Dest)) {
12008 // We found the chain we want as an operand of this TokenFactor.
12009 // Essentially, we reach the chain without side-effects if we could
12010 // serialize the TokenFactor into a simple chain of operations with
12011 // Dest as the last operation. This is automatically true if the
12012 // chain has one use: there are no other ordering constraints.
12013 // If the chain has more than one use, we give up: some other
12014 // use of Dest might force a side-effect between Dest and the current
12015 // node.
12016 if (Dest.hasOneUse())
12017 return true;
12018 }
12019 // Next, try a deep search: check whether every operand of the TokenFactor
12020 // reaches Dest.
12021 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
12022 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
12023 });
12024 }
12025
12026 // Loads don't have side effects, look through them.
12027 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
12028 if (Ld->isUnordered())
12029 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
12030 }
12031 return false;
12032}
12033
12034bool SDNode::hasPredecessor(const SDNode *N) const {
12037 Worklist.push_back(this);
12038 return hasPredecessorHelper(N, Visited, Worklist);
12039}
12040
12042 this->Flags.intersectWith(Flags);
12043}
12044
12045SDValue
12047 ArrayRef<ISD::NodeType> CandidateBinOps,
12048 bool AllowPartials) {
12049 // The pattern must end in an extract from index 0.
12050 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
12051 !isNullConstant(Extract->getOperand(1)))
12052 return SDValue();
12053
12054 // Match against one of the candidate binary ops.
12055 SDValue Op = Extract->getOperand(0);
12056 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
12057 return Op.getOpcode() == unsigned(BinOp);
12058 }))
12059 return SDValue();
12060
12061 // Floating-point reductions may require relaxed constraints on the final step
12062 // of the reduction because they may reorder intermediate operations.
12063 unsigned CandidateBinOp = Op.getOpcode();
12064 if (Op.getValueType().isFloatingPoint()) {
12065 SDNodeFlags Flags = Op->getFlags();
12066 switch (CandidateBinOp) {
12067 case ISD::FADD:
12068 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
12069 return SDValue();
12070 break;
12071 default:
12072 llvm_unreachable("Unhandled FP opcode for binop reduction");
12073 }
12074 }
12075
12076 // Matching failed - attempt to see if we did enough stages that a partial
12077 // reduction from a subvector is possible.
12078 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
12079 if (!AllowPartials || !Op)
12080 return SDValue();
12081 EVT OpVT = Op.getValueType();
12082 EVT OpSVT = OpVT.getScalarType();
12083 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
12084 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
12085 return SDValue();
12086 BinOp = (ISD::NodeType)CandidateBinOp;
12087 return getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(Op), SubVT, Op,
12089 };
12090
12091 // At each stage, we're looking for something that looks like:
12092 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
12093 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
12094 // i32 undef, i32 undef, i32 undef, i32 undef>
12095 // %a = binop <8 x i32> %op, %s
12096 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
12097 // we expect something like:
12098 // <4,5,6,7,u,u,u,u>
12099 // <2,3,u,u,u,u,u,u>
12100 // <1,u,u,u,u,u,u,u>
12101 // While a partial reduction match would be:
12102 // <2,3,u,u,u,u,u,u>
12103 // <1,u,u,u,u,u,u,u>
12104 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
12105 SDValue PrevOp;
12106 for (unsigned i = 0; i < Stages; ++i) {
12107 unsigned MaskEnd = (1 << i);
12108
12109 if (Op.getOpcode() != CandidateBinOp)
12110 return PartialReduction(PrevOp, MaskEnd);
12111
12112 SDValue Op0 = Op.getOperand(0);
12113 SDValue Op1 = Op.getOperand(1);
12114
12115 ShuffleVectorSDNode *Shuffle = dyn_cast<ShuffleVectorSDNode>(Op0);
12116 if (Shuffle) {
12117 Op = Op1;
12118 } else {
12119 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
12120 Op = Op0;
12121 }
12122
12123 // The first operand of the shuffle should be the same as the other operand
12124 // of the binop.
12125 if (!Shuffle || Shuffle->getOperand(0) != Op)
12126 return PartialReduction(PrevOp, MaskEnd);
12127
12128 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
12129 for (int Index = 0; Index < (int)MaskEnd; ++Index)
12130 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
12131 return PartialReduction(PrevOp, MaskEnd);
12132
12133 PrevOp = Op;
12134 }
12135
12136 // Handle subvector reductions, which tend to appear after the shuffle
12137 // reduction stages.
12138 while (Op.getOpcode() == CandidateBinOp) {
12139 unsigned NumElts = Op.getValueType().getVectorNumElements();
12140 SDValue Op0 = Op.getOperand(0);
12141 SDValue Op1 = Op.getOperand(1);
12142 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
12144 Op0.getOperand(0) != Op1.getOperand(0))
12145 break;
12146 SDValue Src = Op0.getOperand(0);
12147 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
12148 if (NumSrcElts != (2 * NumElts))
12149 break;
12150 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
12151 Op1.getConstantOperandAPInt(1) == NumElts) &&
12152 !(Op1.getConstantOperandAPInt(1) == 0 &&
12153 Op0.getConstantOperandAPInt(1) == NumElts))
12154 break;
12155 Op = Src;
12156 }
12157
12158 BinOp = (ISD::NodeType)CandidateBinOp;
12159 return Op;
12160}
12161
12163 EVT VT = N->getValueType(0);
12164 EVT EltVT = VT.getVectorElementType();
12165 unsigned NE = VT.getVectorNumElements();
12166
12167 SDLoc dl(N);
12168
12169 // If ResNE is 0, fully unroll the vector op.
12170 if (ResNE == 0)
12171 ResNE = NE;
12172 else if (NE > ResNE)
12173 NE = ResNE;
12174
12175 if (N->getNumValues() == 2) {
12176 SmallVector<SDValue, 8> Scalars0, Scalars1;
12177 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12178 EVT VT1 = N->getValueType(1);
12179 EVT EltVT1 = VT1.getVectorElementType();
12180
12181 unsigned i;
12182 for (i = 0; i != NE; ++i) {
12183 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12184 SDValue Operand = N->getOperand(j);
12185 EVT OperandVT = Operand.getValueType();
12186
12187 // A vector operand; extract a single element.
12188 EVT OperandEltVT = OperandVT.getVectorElementType();
12189 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12190 Operand, getVectorIdxConstant(i, dl));
12191 }
12192
12193 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
12194 Scalars0.push_back(EltOp);
12195 Scalars1.push_back(EltOp.getValue(1));
12196 }
12197
12198 SDValue Vec0 = getBuildVector(VT, dl, Scalars0);
12199 SDValue Vec1 = getBuildVector(VT1, dl, Scalars1);
12200 return getMergeValues({Vec0, Vec1}, dl);
12201 }
12202
12203 assert(N->getNumValues() == 1 &&
12204 "Can't unroll a vector with multiple results!");
12205
12207 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12208
12209 unsigned i;
12210 for (i= 0; i != NE; ++i) {
12211 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12212 SDValue Operand = N->getOperand(j);
12213 EVT OperandVT = Operand.getValueType();
12214 if (OperandVT.isVector()) {
12215 // A vector operand; extract a single element.
12216 EVT OperandEltVT = OperandVT.getVectorElementType();
12217 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12218 Operand, getVectorIdxConstant(i, dl));
12219 } else {
12220 // A scalar operand; just use it as is.
12221 Operands[j] = Operand;
12222 }
12223 }
12224
12225 switch (N->getOpcode()) {
12226 default: {
12227 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
12228 N->getFlags()));
12229 break;
12230 }
12231 case ISD::VSELECT:
12232 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
12233 break;
12234 case ISD::SHL:
12235 case ISD::SRA:
12236 case ISD::SRL:
12237 case ISD::ROTL:
12238 case ISD::ROTR:
12239 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
12241 Operands[1])));
12242 break;
12244 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
12245 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
12246 Operands[0],
12247 getValueType(ExtVT)));
12248 }
12249 }
12250 }
12251
12252 for (; i < ResNE; ++i)
12253 Scalars.push_back(getUNDEF(EltVT));
12254
12255 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
12256 return getBuildVector(VecVT, dl, Scalars);
12257}
12258
12259std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
12260 SDNode *N, unsigned ResNE) {
12261 unsigned Opcode = N->getOpcode();
12262 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
12263 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
12264 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
12265 "Expected an overflow opcode");
12266
12267 EVT ResVT = N->getValueType(0);
12268 EVT OvVT = N->getValueType(1);
12269 EVT ResEltVT = ResVT.getVectorElementType();
12270 EVT OvEltVT = OvVT.getVectorElementType();
12271 SDLoc dl(N);
12272
12273 // If ResNE is 0, fully unroll the vector op.
12274 unsigned NE = ResVT.getVectorNumElements();
12275 if (ResNE == 0)
12276 ResNE = NE;
12277 else if (NE > ResNE)
12278 NE = ResNE;
12279
12280 SmallVector<SDValue, 8> LHSScalars;
12281 SmallVector<SDValue, 8> RHSScalars;
12282 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
12283 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
12284
12285 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
12286 SDVTList VTs = getVTList(ResEltVT, SVT);
12287 SmallVector<SDValue, 8> ResScalars;
12288 SmallVector<SDValue, 8> OvScalars;
12289 for (unsigned i = 0; i < NE; ++i) {
12290 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
12291 SDValue Ov =
12292 getSelect(dl, OvEltVT, Res.getValue(1),
12293 getBoolConstant(true, dl, OvEltVT, ResVT),
12294 getConstant(0, dl, OvEltVT));
12295
12296 ResScalars.push_back(Res);
12297 OvScalars.push_back(Ov);
12298 }
12299
12300 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
12301 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
12302
12303 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
12304 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
12305 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
12306 getBuildVector(NewOvVT, dl, OvScalars));
12307}
12308
12311 unsigned Bytes,
12312 int Dist) const {
12313 if (LD->isVolatile() || Base->isVolatile())
12314 return false;
12315 // TODO: probably too restrictive for atomics, revisit
12316 if (!LD->isSimple())
12317 return false;
12318 if (LD->isIndexed() || Base->isIndexed())
12319 return false;
12320 if (LD->getChain() != Base->getChain())
12321 return false;
12322 EVT VT = LD->getMemoryVT();
12323 if (VT.getSizeInBits() / 8 != Bytes)
12324 return false;
12325
12326 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
12327 auto LocDecomp = BaseIndexOffset::match(LD, *this);
12328
12329 int64_t Offset = 0;
12330 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
12331 return (Dist * (int64_t)Bytes == Offset);
12332 return false;
12333}
12334
12335/// InferPtrAlignment - Infer alignment of a load / store address. Return
12336/// std::nullopt if it cannot be inferred.
12338 // If this is a GlobalAddress + cst, return the alignment.
12339 const GlobalValue *GV = nullptr;
12340 int64_t GVOffset = 0;
12341 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
12342 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
12343 KnownBits Known(PtrWidth);
12345 unsigned AlignBits = Known.countMinTrailingZeros();
12346 if (AlignBits)
12347 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
12348 }
12349
12350 // If this is a direct reference to a stack slot, use information about the
12351 // stack slot's alignment.
12352 int FrameIdx = INT_MIN;
12353 int64_t FrameOffset = 0;
12354 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
12355 FrameIdx = FI->getIndex();
12356 } else if (isBaseWithConstantOffset(Ptr) &&
12357 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
12358 // Handle FI+Cst
12359 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
12360 FrameOffset = Ptr.getConstantOperandVal(1);
12361 }
12362
12363 if (FrameIdx != INT_MIN) {
12365 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
12366 }
12367
12368 return std::nullopt;
12369}
12370
12371/// Split the scalar node with EXTRACT_ELEMENT using the provided
12372/// VTs and return the low/high part.
12373std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
12374 const SDLoc &DL,
12375 const EVT &LoVT,
12376 const EVT &HiVT) {
12377 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
12378 "Split node must be a scalar type");
12379 SDValue Lo =
12381 SDValue Hi =
12383 return std::make_pair(Lo, Hi);
12384}
12385
12386/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
12387/// which is split (or expanded) into two not necessarily identical pieces.
12388std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
12389 // Currently all types are split in half.
12390 EVT LoVT, HiVT;
12391 if (!VT.isVector())
12392 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
12393 else
12394 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
12395
12396 return std::make_pair(LoVT, HiVT);
12397}
12398
12399/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
12400/// type, dependent on an enveloping VT that has been split into two identical
12401/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
12402std::pair<EVT, EVT>
12404 bool *HiIsEmpty) const {
12405 EVT EltTp = VT.getVectorElementType();
12406 // Examples:
12407 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
12408 // custom VL=9 with enveloping VL=8/8 yields 8/1
12409 // custom VL=10 with enveloping VL=8/8 yields 8/2
12410 // etc.
12411 ElementCount VTNumElts = VT.getVectorElementCount();
12412 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
12413 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
12414 "Mixing fixed width and scalable vectors when enveloping a type");
12415 EVT LoVT, HiVT;
12416 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
12417 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12418 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
12419 *HiIsEmpty = false;
12420 } else {
12421 // Flag that hi type has zero storage size, but return split envelop type
12422 // (this would be easier if vector types with zero elements were allowed).
12423 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
12424 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12425 *HiIsEmpty = true;
12426 }
12427 return std::make_pair(LoVT, HiVT);
12428}
12429
12430/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
12431/// low/high part.
12432std::pair<SDValue, SDValue>
12433SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
12434 const EVT &HiVT) {
12435 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
12436 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
12437 "Splitting vector with an invalid mixture of fixed and scalable "
12438 "vector types");
12440 N.getValueType().getVectorMinNumElements() &&
12441 "More vector elements requested than available!");
12442 SDValue Lo, Hi;
12443 Lo =
12445 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
12446 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
12447 // IDX with the runtime scaling factor of the result vector type. For
12448 // fixed-width result vectors, that runtime scaling factor is 1.
12451 return std::make_pair(Lo, Hi);
12452}
12453
12454std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
12455 const SDLoc &DL) {
12456 // Split the vector length parameter.
12457 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
12458 EVT VT = N.getValueType();
12460 "Expecting the mask to be an evenly-sized vector");
12461 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
12462 SDValue HalfNumElts =
12463 VecVT.isFixedLengthVector()
12464 ? getConstant(HalfMinNumElts, DL, VT)
12465 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
12466 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
12467 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
12468 return std::make_pair(Lo, Hi);
12469}
12470
12471/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
12473 EVT VT = N.getValueType();
12476 return getNode(ISD::INSERT_SUBVECTOR, DL, WideVT, getUNDEF(WideVT), N,
12478}
12479
12482 unsigned Start, unsigned Count,
12483 EVT EltVT) {
12484 EVT VT = Op.getValueType();
12485 if (Count == 0)
12486 Count = VT.getVectorNumElements();
12487 if (EltVT == EVT())
12488 EltVT = VT.getVectorElementType();
12489 SDLoc SL(Op);
12490 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
12491 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Op,
12492 getVectorIdxConstant(i, SL)));
12493 }
12494}
12495
12496// getAddressSpace - Return the address space this GlobalAddress belongs to.
12498 return getGlobal()->getType()->getAddressSpace();
12499}
12500
12503 return Val.MachineCPVal->getType();
12504 return Val.ConstVal->getType();
12505}
12506
12507bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
12508 unsigned &SplatBitSize,
12509 bool &HasAnyUndefs,
12510 unsigned MinSplatBits,
12511 bool IsBigEndian) const {
12512 EVT VT = getValueType(0);
12513 assert(VT.isVector() && "Expected a vector type");
12514 unsigned VecWidth = VT.getSizeInBits();
12515 if (MinSplatBits > VecWidth)
12516 return false;
12517
12518 // FIXME: The widths are based on this node's type, but build vectors can
12519 // truncate their operands.
12520 SplatValue = APInt(VecWidth, 0);
12521 SplatUndef = APInt(VecWidth, 0);
12522
12523 // Get the bits. Bits with undefined values (when the corresponding element
12524 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
12525 // in SplatValue. If any of the values are not constant, give up and return
12526 // false.
12527 unsigned int NumOps = getNumOperands();
12528 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
12529 unsigned EltWidth = VT.getScalarSizeInBits();
12530
12531 for (unsigned j = 0; j < NumOps; ++j) {
12532 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
12533 SDValue OpVal = getOperand(i);
12534 unsigned BitPos = j * EltWidth;
12535
12536 if (OpVal.isUndef())
12537 SplatUndef.setBits(BitPos, BitPos + EltWidth);
12538 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
12539 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
12540 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
12541 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
12542 else
12543 return false;
12544 }
12545
12546 // The build_vector is all constants or undefs. Find the smallest element
12547 // size that splats the vector.
12548 HasAnyUndefs = (SplatUndef != 0);
12549
12550 // FIXME: This does not work for vectors with elements less than 8 bits.
12551 while (VecWidth > 8) {
12552 // If we can't split in half, stop here.
12553 if (VecWidth & 1)
12554 break;
12555
12556 unsigned HalfSize = VecWidth / 2;
12557 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
12558 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
12559 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
12560 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
12561
12562 // If the two halves do not match (ignoring undef bits), stop here.
12563 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
12564 MinSplatBits > HalfSize)
12565 break;
12566
12567 SplatValue = HighValue | LowValue;
12568 SplatUndef = HighUndef & LowUndef;
12569
12570 VecWidth = HalfSize;
12571 }
12572
12573 // FIXME: The loop above only tries to split in halves. But if the input
12574 // vector for example is <3 x i16> it wouldn't be able to detect a
12575 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
12576 // optimizations. I guess that back in the days when this helper was created
12577 // vectors normally was power-of-2 sized.
12578
12579 SplatBitSize = VecWidth;
12580 return true;
12581}
12582
12584 BitVector *UndefElements) const {
12585 unsigned NumOps = getNumOperands();
12586 if (UndefElements) {
12587 UndefElements->clear();
12588 UndefElements->resize(NumOps);
12589 }
12590 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
12591 if (!DemandedElts)
12592 return SDValue();
12593 SDValue Splatted;
12594 for (unsigned i = 0; i != NumOps; ++i) {
12595 if (!DemandedElts[i])
12596 continue;
12597 SDValue Op = getOperand(i);
12598 if (Op.isUndef()) {
12599 if (UndefElements)
12600 (*UndefElements)[i] = true;
12601 } else if (!Splatted) {
12602 Splatted = Op;
12603 } else if (Splatted != Op) {
12604 return SDValue();
12605 }
12606 }
12607
12608 if (!Splatted) {
12609 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
12610 assert(getOperand(FirstDemandedIdx).isUndef() &&
12611 "Can only have a splat without a constant for all undefs.");
12612 return getOperand(FirstDemandedIdx);
12613 }
12614
12615 return Splatted;
12616}
12617
12619 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
12620 return getSplatValue(DemandedElts, UndefElements);
12621}
12622
12624 SmallVectorImpl<SDValue> &Sequence,
12625 BitVector *UndefElements) const {
12626 unsigned NumOps = getNumOperands();
12627 Sequence.clear();
12628 if (UndefElements) {
12629 UndefElements->clear();
12630 UndefElements->resize(NumOps);
12631 }
12632 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
12633 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
12634 return false;
12635
12636 // Set the undefs even if we don't find a sequence (like getSplatValue).
12637 if (UndefElements)
12638 for (unsigned I = 0; I != NumOps; ++I)
12639 if (DemandedElts[I] && getOperand(I).isUndef())
12640 (*UndefElements)[I] = true;
12641
12642 // Iteratively widen the sequence length looking for repetitions.
12643 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
12644 Sequence.append(SeqLen, SDValue());
12645 for (unsigned I = 0; I != NumOps; ++I) {
12646 if (!DemandedElts[I])
12647 continue;
12648 SDValue &SeqOp = Sequence[I % SeqLen];
12650 if (Op.isUndef()) {
12651 if (!SeqOp)
12652 SeqOp = Op;
12653 continue;
12654 }
12655 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
12656 Sequence.clear();
12657 break;
12658 }
12659 SeqOp = Op;
12660 }
12661 if (!Sequence.empty())
12662 return true;
12663 }
12664
12665 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
12666 return false;
12667}
12668
12670 BitVector *UndefElements) const {
12671 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
12672 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
12673}
12674
12677 BitVector *UndefElements) const {
12678 return dyn_cast_or_null<ConstantSDNode>(
12679 getSplatValue(DemandedElts, UndefElements));
12680}
12681
12684 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
12685}
12686
12689 BitVector *UndefElements) const {
12690 return dyn_cast_or_null<ConstantFPSDNode>(
12691 getSplatValue(DemandedElts, UndefElements));
12692}
12693
12696 return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
12697}
12698
12699int32_t
12701 uint32_t BitWidth) const {
12702 if (ConstantFPSDNode *CN =
12703 dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
12704 bool IsExact;
12705 APSInt IntVal(BitWidth);
12706 const APFloat &APF = CN->getValueAPF();
12707 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
12708 APFloat::opOK ||
12709 !IsExact)
12710 return -1;
12711
12712 return IntVal.exactLogBase2();
12713 }
12714 return -1;
12715}
12716
12718 bool IsLittleEndian, unsigned DstEltSizeInBits,
12719 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
12720 // Early-out if this contains anything but Undef/Constant/ConstantFP.
12721 if (!isConstant())
12722 return false;
12723
12724 unsigned NumSrcOps = getNumOperands();
12725 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
12726 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
12727 "Invalid bitcast scale");
12728
12729 // Extract raw src bits.
12730 SmallVector<APInt> SrcBitElements(NumSrcOps,
12731 APInt::getZero(SrcEltSizeInBits));
12732 BitVector SrcUndeElements(NumSrcOps, false);
12733
12734 for (unsigned I = 0; I != NumSrcOps; ++I) {
12736 if (Op.isUndef()) {
12737 SrcUndeElements.set(I);
12738 continue;
12739 }
12740 auto *CInt = dyn_cast<ConstantSDNode>(Op);
12741 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
12742 assert((CInt || CFP) && "Unknown constant");
12743 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
12744 : CFP->getValueAPF().bitcastToAPInt();
12745 }
12746
12747 // Recast to dst width.
12748 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
12749 SrcBitElements, UndefElements, SrcUndeElements);
12750 return true;
12751}
12752
12753void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
12754 unsigned DstEltSizeInBits,
12755 SmallVectorImpl<APInt> &DstBitElements,
12756 ArrayRef<APInt> SrcBitElements,
12757 BitVector &DstUndefElements,
12758 const BitVector &SrcUndefElements) {
12759 unsigned NumSrcOps = SrcBitElements.size();
12760 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
12761 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
12762 "Invalid bitcast scale");
12763 assert(NumSrcOps == SrcUndefElements.size() &&
12764 "Vector size mismatch");
12765
12766 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
12767 DstUndefElements.clear();
12768 DstUndefElements.resize(NumDstOps, false);
12769 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
12770
12771 // Concatenate src elements constant bits together into dst element.
12772 if (SrcEltSizeInBits <= DstEltSizeInBits) {
12773 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
12774 for (unsigned I = 0; I != NumDstOps; ++I) {
12775 DstUndefElements.set(I);
12776 APInt &DstBits = DstBitElements[I];
12777 for (unsigned J = 0; J != Scale; ++J) {
12778 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
12779 if (SrcUndefElements[Idx])
12780 continue;
12781 DstUndefElements.reset(I);
12782 const APInt &SrcBits = SrcBitElements[Idx];
12783 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
12784 "Illegal constant bitwidths");
12785 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
12786 }
12787 }
12788 return;
12789 }
12790
12791 // Split src element constant bits into dst elements.
12792 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
12793 for (unsigned I = 0; I != NumSrcOps; ++I) {
12794 if (SrcUndefElements[I]) {
12795 DstUndefElements.set(I * Scale, (I + 1) * Scale);
12796 continue;
12797 }
12798 const APInt &SrcBits = SrcBitElements[I];
12799 for (unsigned J = 0; J != Scale; ++J) {
12800 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
12801 APInt &DstBits = DstBitElements[Idx];
12802 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
12803 }
12804 }
12805}
12806
12808 for (const SDValue &Op : op_values()) {
12809 unsigned Opc = Op.getOpcode();
12810 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
12811 return false;
12812 }
12813 return true;
12814}
12815
12816std::optional<std::pair<APInt, APInt>>
12818 unsigned NumOps = getNumOperands();
12819 if (NumOps < 2)
12820 return std::nullopt;
12821
12822 if (!isa<ConstantSDNode>(getOperand(0)) ||
12823 !isa<ConstantSDNode>(getOperand(1)))
12824 return std::nullopt;
12825
12826 unsigned EltSize = getValueType(0).getScalarSizeInBits();
12827 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
12828 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
12829
12830 if (Stride.isZero())
12831 return std::nullopt;
12832
12833 for (unsigned i = 2; i < NumOps; ++i) {
12834 if (!isa<ConstantSDNode>(getOperand(i)))
12835 return std::nullopt;
12836
12837 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
12838 if (Val != (Start + (Stride * i)))
12839 return std::nullopt;
12840 }
12841
12842 return std::make_pair(Start, Stride);
12843}
12844
12845bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
12846 // Find the first non-undef value in the shuffle mask.
12847 unsigned i, e;
12848 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
12849 /* search */;
12850
12851 // If all elements are undefined, this shuffle can be considered a splat
12852 // (although it should eventually get simplified away completely).
12853 if (i == e)
12854 return true;
12855
12856 // Make sure all remaining elements are either undef or the same as the first
12857 // non-undef value.
12858 for (int Idx = Mask[i]; i != e; ++i)
12859 if (Mask[i] >= 0 && Mask[i] != Idx)
12860 return false;
12861 return true;
12862}
12863
12864// Returns the SDNode if it is a constant integer BuildVector
12865// or constant integer.
12867 if (isa<ConstantSDNode>(N))
12868 return N.getNode();
12870 return N.getNode();
12871 // Treat a GlobalAddress supporting constant offset folding as a
12872 // constant integer.
12873 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N))
12874 if (GA->getOpcode() == ISD::GlobalAddress &&
12875 TLI->isOffsetFoldingLegal(GA))
12876 return GA;
12877 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
12878 isa<ConstantSDNode>(N.getOperand(0)))
12879 return N.getNode();
12880 return nullptr;
12881}
12882
12883// Returns the SDNode if it is a constant float BuildVector
12884// or constant float.
12886 if (isa<ConstantFPSDNode>(N))
12887 return N.getNode();
12888
12890 return N.getNode();
12891
12892 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
12893 isa<ConstantFPSDNode>(N.getOperand(0)))
12894 return N.getNode();
12895
12896 return nullptr;
12897}
12898
12899void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
12900 assert(!Node->OperandList && "Node already has operands");
12902 "too many operands to fit into SDNode");
12903 SDUse *Ops = OperandRecycler.allocate(
12904 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
12905
12906 bool IsDivergent = false;
12907 for (unsigned I = 0; I != Vals.size(); ++I) {
12908 Ops[I].setUser(Node);
12909 Ops[I].setInitial(Vals[I]);
12910 if (Ops[I].Val.getValueType() != MVT::Other) // Skip Chain. It does not carry divergence.
12911 IsDivergent |= Ops[I].getNode()->isDivergent();
12912 }
12913 Node->NumOperands = Vals.size();
12914 Node->OperandList = Ops;
12915 if (!TLI->isSDNodeAlwaysUniform(Node)) {
12916 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
12917 Node->SDNodeBits.IsDivergent = IsDivergent;
12918 }
12920}
12921
12924 size_t Limit = SDNode::getMaxNumOperands();
12925 while (Vals.size() > Limit) {
12926 unsigned SliceIdx = Vals.size() - Limit;
12927 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
12928 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
12929 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
12930 Vals.emplace_back(NewTF);
12931 }
12932 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
12933}
12934
12936 EVT VT, SDNodeFlags Flags) {
12937 switch (Opcode) {
12938 default:
12939 return SDValue();
12940 case ISD::ADD:
12941 case ISD::OR:
12942 case ISD::XOR:
12943 case ISD::UMAX:
12944 return getConstant(0, DL, VT);
12945 case ISD::MUL:
12946 return getConstant(1, DL, VT);
12947 case ISD::AND:
12948 case ISD::UMIN:
12949 return getAllOnesConstant(DL, VT);
12950 case ISD::SMAX:
12952 case ISD::SMIN:
12954 case ISD::FADD:
12955 return getConstantFP(-0.0, DL, VT);
12956 case ISD::FMUL:
12957 return getConstantFP(1.0, DL, VT);
12958 case ISD::FMINNUM:
12959 case ISD::FMAXNUM: {
12960 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12961 const fltSemantics &Semantics = EVTToAPFloatSemantics(VT);
12962 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
12963 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
12964 APFloat::getLargest(Semantics);
12965 if (Opcode == ISD::FMAXNUM)
12966 NeutralAF.changeSign();
12967
12968 return getConstantFP(NeutralAF, DL, VT);
12969 }
12970 case ISD::FMINIMUM:
12971 case ISD::FMAXIMUM: {
12972 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
12973 const fltSemantics &Semantics = EVTToAPFloatSemantics(VT);
12974 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
12975 : APFloat::getLargest(Semantics);
12976 if (Opcode == ISD::FMAXIMUM)
12977 NeutralAF.changeSign();
12978
12979 return getConstantFP(NeutralAF, DL, VT);
12980 }
12981
12982 }
12983}
12984
12985/// Helper used to make a call to a library function that has one argument of
12986/// pointer type.
12987///
12988/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
12989/// used to get or set floating-point state. They have one argument of pointer
12990/// type, which points to the memory region containing bits of the
12991/// floating-point state. The value returned by such function is ignored in the
12992/// created call.
12993///
12994/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
12995/// \param Ptr Pointer used to save/load state.
12996/// \param InChain Ingoing token chain.
12997/// \returns Outgoing chain token.
12999 SDValue InChain,
13000 const SDLoc &DLoc) {
13001 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
13004 Entry.Node = Ptr;
13005 Entry.Ty = Ptr.getValueType().getTypeForEVT(*getContext());
13006 Args.push_back(Entry);
13007 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
13008 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
13009 TLI->getPointerTy(getDataLayout()));
13011 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
13012 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
13013 std::move(Args));
13014 return TLI->LowerCallTo(CLI).second;
13015}
13016
13018 assert(From && To && "Invalid SDNode; empty source SDValue?");
13019 auto I = SDEI.find(From);
13020 if (I == SDEI.end())
13021 return;
13022
13023 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
13024 // the iterator, hence the need to make a copy to prevent a use-after-free.
13025 NodeExtraInfo NEI = I->second;
13026 if (LLVM_LIKELY(!NEI.PCSections) && LLVM_LIKELY(!NEI.MMRA)) {
13027 // No deep copy required for the types of extra info set.
13028 //
13029 // FIXME: Investigate if other types of extra info also need deep copy. This
13030 // depends on the types of nodes they can be attached to: if some extra info
13031 // is only ever attached to nodes where a replacement To node is always the
13032 // node where later use and propagation of the extra info has the intended
13033 // semantics, no deep copy is required.
13034 SDEI[To] = std::move(NEI);
13035 return;
13036 }
13037
13038 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
13039 // through the replacement of From with To. Otherwise, replacements of a node
13040 // (From) with more complex nodes (To and its operands) may result in lost
13041 // extra info where the root node (To) is insignificant in further propagating
13042 // and using extra info when further lowering to MIR.
13043 //
13044 // In the first step pre-populate the visited set with the nodes reachable
13045 // from the old From node. This avoids copying NodeExtraInfo to parts of the
13046 // DAG that is not new and should be left untouched.
13047 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
13048 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
13049 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
13050 if (MaxDepth == 0) {
13051 // Remember this node in case we need to increase MaxDepth and continue
13052 // populating FromReach from this node.
13053 Leafs.emplace_back(N);
13054 return;
13055 }
13056 if (!FromReach.insert(N).second)
13057 return;
13058 for (const SDValue &Op : N->op_values())
13059 Self(Self, Op.getNode(), MaxDepth - 1);
13060 };
13061
13062 // Copy extra info to To and all its transitive operands (that are new).
13064 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
13065 if (FromReach.contains(N))
13066 return true;
13067 if (!Visited.insert(N).second)
13068 return true;
13069 if (getEntryNode().getNode() == N)
13070 return false;
13071 for (const SDValue &Op : N->op_values()) {
13072 if (!Self(Self, Op.getNode()))
13073 return false;
13074 }
13075 // Copy only if entry node was not reached.
13076 SDEI[N] = NEI;
13077 return true;
13078 };
13079
13080 // We first try with a lower MaxDepth, assuming that the path to common
13081 // operands between From and To is relatively short. This significantly
13082 // improves performance in the common case. The initial MaxDepth is big
13083 // enough to avoid retry in the common case; the last MaxDepth is large
13084 // enough to avoid having to use the fallback below (and protects from
13085 // potential stack exhaustion from recursion).
13086 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
13087 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
13088 // StartFrom is the previous (or initial) set of leafs reachable at the
13089 // previous maximum depth.
13091 std::swap(StartFrom, Leafs);
13092 for (const SDNode *N : StartFrom)
13093 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
13094 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
13095 return;
13096 // This should happen very rarely (reached the entry node).
13097 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
13098 assert(!Leafs.empty());
13099 }
13100
13101 // This should not happen - but if it did, that means the subgraph reachable
13102 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
13103 // could not visit all reachable common operands. Consequently, we were able
13104 // to reach the entry node.
13105 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
13106 assert(false && "From subgraph too complex - increase max. MaxDepth?");
13107 // Best-effort fallback if assertions disabled.
13108 SDEI[To] = std::move(NEI);
13109}
13110
13111#ifndef NDEBUG
13112static void checkForCyclesHelper(const SDNode *N,
13115 const llvm::SelectionDAG *DAG) {
13116 // If this node has already been checked, don't check it again.
13117 if (Checked.count(N))
13118 return;
13119
13120 // If a node has already been visited on this depth-first walk, reject it as
13121 // a cycle.
13122 if (!Visited.insert(N).second) {
13123 errs() << "Detected cycle in SelectionDAG\n";
13124 dbgs() << "Offending node:\n";
13125 N->dumprFull(DAG); dbgs() << "\n";
13126 abort();
13127 }
13128
13129 for (const SDValue &Op : N->op_values())
13130 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
13131
13132 Checked.insert(N);
13133 Visited.erase(N);
13134}
13135#endif
13136
13138 const llvm::SelectionDAG *DAG,
13139 bool force) {
13140#ifndef NDEBUG
13141 bool check = force;
13142#ifdef EXPENSIVE_CHECKS
13143 check = true;
13144#endif // EXPENSIVE_CHECKS
13145 if (check) {
13146 assert(N && "Checking nonexistent SDNode");
13149 checkForCyclesHelper(N, visited, checked, DAG);
13150 }
13151#endif // !NDEBUG
13152}
13153
13154void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
13155 checkForCycles(DAG->getRoot().getNode(), DAG, force);
13156}
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static bool isConstant(const MachineInstr &MI)
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...
This file implements the BitVector class.
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-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:468
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:240
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Looks at all the uses of the given value Returns the Liveness deduced from the uses of this value Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses If the result is MaybeLiveUses might be modified but its content should be ignored(since it might not be complete). DeadArgumentEliminationPass
Given that RA is a live propagate it s liveness to any other values it uses(according to Uses). void DeadArgumentEliminationPass
Given that RA is a live value
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This file defines a hash set that can be used to remove duplication of nodes in a graph.
Rewrite Partial Register Uses
#define check(cond)
static const unsigned MaxDepth
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:528
static Align getPrefTypeAlign(EVT VT, SelectionDAG &DAG)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
mir Rename Register Operands
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
unsigned const TargetRegisterInfo * TRI
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
PowerPC Reduce CR logical Operation
const SmallVectorImpl< MachineOperand > & Cond
Contains matchers for matching SelectionDAG nodes and values.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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 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 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, AAResults *AA)
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 void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N)
If this is an SDNode with special info, add this info to the NodeID data.
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 VerifySDNode(SDNode *N, const TargetLowering *TLI)
VerifySDNode - Check the given SDNode. Aborts if it is invalid.
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 Constant * ConstantFold(Instruction *I, const DataLayout &DL, const SmallDenseMap< Value *, Constant * > &ConstantPool)
Try to fold instruction I into a constant.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file describes how to lower LLVM code to machine code.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:191
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
Value * RHS
Value * LHS
static unsigned getSize(unsigned Kind)
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
Checks whether the given location points to constant memory, or if OrLocal is true whether it points ...
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition: APFloat.h:988
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1069
void copySign(const APFloat &RHS)
Definition: APFloat.h:1163
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5196
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1051
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:1278
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1042
bool isFinite() const
Definition: APFloat.h:1300
bool isNaN() const
Definition: APFloat.h:1293
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1193
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1060
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition: APFloat.h:1096
bool isZero() const
Definition: APFloat.h:1291
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1006
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1185
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:966
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1087
bool isPosZero() const
Definition: APFloat.h:1306
bool isNegZero() const
Definition: APFloat.h:1307
void changeSign()
Definition: APFloat.h:1158
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition: APFloat.h:977
bool isInfinity() const
Definition: APFloat.h:1292
Class for arbitrary precision integers.
Definition: APInt.h:76
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1941
APInt usub_sat(const APInt &RHS) const
Definition: APInt.cpp:2025
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1543
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:212
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1385
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:981
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:207
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1491
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1370
unsigned popcount() const
Count the number of bits set.
Definition: APInt.h:1620
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1364
APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition: APInt.cpp:608
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1002
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1463
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:906
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1308
APInt abs() const
Get the absolute value.
Definition: APInt.h:1737
APInt sadd_sat(const APInt &RHS) const
Definition: APInt.cpp:1996
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:349
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1160
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:358
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1636
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1439
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1089
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:187
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:307
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1614
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1375
APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition: APInt.cpp:1124
APInt reverseBits() const
Definition: APInt.cpp:737
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:812
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1144
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1589
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition: APInt.h:1578
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1548
static APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition: APInt.cpp:620
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:197
APInt sshl_sat(const APInt &RHS) const
Definition: APInt.cpp:2056
APInt ushl_sat(const APInt &RHS) const
Definition: APInt.cpp:2070
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1010
APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition: APInt.cpp:1111
void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition: APInt.cpp:368
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition: APInt.h:1395
unsigned logBase2() const
Definition: APInt.h:1703
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2006
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:805
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1297
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1706
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:312
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1128
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:954
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition: APInt.h:1345
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:851
APInt byteSwap() const
Definition: APInt.cpp:715
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition: APInt.h:1235
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:418
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:284
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:178
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1367
APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition: APInt.cpp:453
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1215
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:264
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:217
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:836
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:829
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1199
APInt ssub_sat(const APInt &RHS) const
Definition: APInt.cpp:2015
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
Recycle small arrays allocated from a BumpPtrAllocator.
Definition: ArrayRecycler.h:28
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:154
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition: ArrayRef.h:195
This is an SDNode representing atomic operations.
static BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
BitVector & reset()
Definition: BitVector.h:392
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:341
void clear()
clear - Removes all bits from the bitvector.
Definition: BitVector.h:335
BitVector & set()
Definition: BitVector.h:351
bool none() const
none - Returns true if none of the bits are set.
Definition: BitVector.h:188
size_type size() const
size - Returns the number of bits in this bitvector.
Definition: BitVector.h:159
unsigned getTargetFlags() const
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition: Constants.h:889
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
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 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.
bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
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.
std::optional< std::pair< APInt, APInt > > isConstantSequence() const
If this BuildVector is constant and represents the numerical series "<a, a+n, a+2n,...
SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
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.
ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
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_ATTRIBUTE_RETURNS_NONNULL void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:148
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it,...
Definition: Allocator.h:123
static 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:268
This is the shared class of boolean and integer constants.
Definition: Constants.h:80
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition: Constants.h:148
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:145
bool isMachineConstantPoolEntry() const
This class represents a range of values.
Definition: ConstantRange.h:47
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 ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
KnownBits toKnownBits() const
Return known bits for values in this range.
ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
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.
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:41
Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
Definition: Constants.cpp:1699
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:432
DWARF expression.
static ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static 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 const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static 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.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:238
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.
Definition: DataLayout.cpp:878
Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:865
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:763
Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Definition: DataLayout.cpp:874
A debug info location.
Definition: DebugLoc.h:33
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:103
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:319
MachineBasicBlock * MBB
MBB - The current block.
Data structure describing the variable locations in a function.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition: Function.h:685
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition: Function.h:682
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:340
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:358
unsigned getAddressSpace() const
const GlobalValue * getGlobal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
Definition: GlobalValue.h:263
unsigned getAddressSpace() const
Definition: GlobalValue.h:205
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:294
This class is used to form a handle around another node that is persistent and is updated across invo...
static bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
constexpr bool isValid() const
Definition: LowLevelType.h:145
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
This SDNode is used for LIFETIME_START/LIFETIME_END values, which indicate the offet and size that ar...
This class is used to represent ISD::LOAD nodes.
static LocationSize precise(uint64_t Value)
TypeSize getValue() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
Metadata node.
Definition: Metadata.h:1067
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
Abstract base class for all machine specific constantpool value subclasses.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
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.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
LocationSize getSize() const
Return the size in bytes of 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,.
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.
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:65
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:193
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:662
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:679
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Definition: PointerUnion.h:142
Analysis providing profile information.
void Deallocate(SubClass *E)
Deallocate - Release storage for the pointed-to object.
Keeps track of dbg_value information through SDISel.
Definition: SelectionDAG.h:159
BumpPtrAllocator & getAlloc()
Definition: SelectionDAG.h:188
void add(SDDbgValue *V, bool isParameter)
void erase(const SDNode *Node)
Invalidate all DbgValues attached to the node and remove it from the Node-to-DbgValues map.
ArrayRef< SDDbgValue * > getSDDbgValues(const SDNode *Node) const
Definition: SelectionDAG.h:194
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(unsigned 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.
void dumprFull(const SelectionDAG *G=nullptr) const
printrFull to dbgs().
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
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.
MemSDNodeBitfields MemSDNodeBits
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.
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 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.
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.
bool hasPredecessor(const SDNode *N) const
Return true if N is a predecessor of this node.
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
Return true if the type of the node type undefined.
bool hasNUsesOfValue(unsigned NUses, unsigned Value) const
Return true if there are exactly NUSES uses of the indicated value.
op_iterator op_end() const
op_iterator op_begin() const
static use_iterator use_end()
void DropOperands()
Release the operands and set this node to have zero operands.
Represents a use of a SDNode.
SDNode * getNode() const
Convenience function for get().getNode().
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.
bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
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 SDValue EmitTargetCodeForMemset(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, bool AlwaysInline, MachinePointerInfo DstPtrInfo) const
Emit target-specific code that performs a memset.
virtual SDValue EmitTargetCodeForMemmove(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memmove.
virtual SDValue EmitTargetCodeForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memcpy.
SDNodeFlags getFlags() const
Definition: SelectionDAG.h:379
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:225
Align getReducedAlign(EVT VT, bool UseABI)
In most cases this function returns the ABI alignment for a given type, except for illegal vector typ...
SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
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())
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 getSplatSourceVector(SDValue V, int &SplatIndex)
If V is a splatted value, return the source vector and its splat index.
SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
OverflowKind computeOverflowForUnsignedSub(SDValue N0, SDValue N1) const
Determine if the result of the unsigned sub of 2 nodes can overflow.
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.
Definition: SelectionDAG.h:551
SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), AAResults *AA=nullptr)
SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
bool isKnownNeverSNaN(SDValue Op, unsigned Depth=0) const
SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS)
Helper function to make it easier to build Select's if you just have operands and don't want to check...
SDValue getStackArgumentTokenFactor(SDValue Chain)
Compute a TokenFactor to force all the incoming stack arguments to be loaded from the stack.
const TargetSubtargetInfo & getSubtarget() const
Definition: SelectionDAG.h:474
const APInt * getValidMaximumShiftAmountConstant(SDValue V, const APInt &DemandedElts) const
If a SHL/SRA/SRL node V has constant shift amounts that are all less than the element bit-width of th...
SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
void updateDivergence(SDNode *N)
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...
SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl)
Constant fold a setcc to true or false.
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),...
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.
SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) neutral element for the given opcode, if it exists.
SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm, bool ConstantFold=true)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
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,...
SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
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,...
SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
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...
SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
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)
SDNode * isConstantIntBuildVectorOrConstantInt(SDValue N) const
Test whether the given value is a constant int or similar node.
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.
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...
SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, unsigned VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
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...
SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
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.
bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
bool cannotBeOrderedNegativeFP(SDValue Op) const
Test whether the given float value is known to be positive.
bool calculateDivergence(SDNode *N)
SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC, bool ConstantFold=true)
SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
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,...
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...
SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
SDValue 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.
Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
bool shouldOptForSize() const
SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
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
Definition: SelectionDAG.h:478
bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
Definition: SelectionDAG.h:448
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)
SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
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.
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...
Definition: SelectionDAG.h:387
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...
void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
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.
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
Definition: SelectionDAG.h:531
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
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.
Definition: SelectionDAG.h:828
SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), AAResults *AA=nullptr)
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...
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.
void DeleteNode(SDNode *N)
Remove the specified node from the system.
SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
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.
SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
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.
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:472
SDNode * isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops)
Check if a node exists without modifying its flags.
bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, unsigned Bytes, int Dist) const
Return true if loads are next to each other and can be merged.
SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
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)
OverflowKind computeOverflowForUnsignedMul(SDValue N0, SDValue N1) const
Determine if the result of the unsigned mul of 2 nodes can overflow.
void copyExtraInfo(SDNode *From, SDNode *To)
Copy extra info associated with one node to another.
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, bool isTailCall, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
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.
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())
SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
Definition: SelectionDAG.h:659
SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
void clear()
Clear state and free memory necessary to make this SelectionDAG ready to process a new block.
void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV)
Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to the shuffle node in input but with swa...
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.
const APInt * getValidShiftAmountConstant(SDValue V, const APInt &DemandedElts) const
If a SHL/SRA/SRL node V has a constant or splat constant shift amount that is less than the element b...
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.
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,...
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.
SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
Definition: SelectionDAG.h:862
SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
OverflowKind computeOverflowForSignedMul(SDValue N0, SDValue N1) const
Determine if the result of the signed mul of 2 nodes can overflow.
MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if '(Op & Mask) == Mask'.
bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
SDValue getRegister(unsigned Reg, EVT VT)
void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
bool isConstantValueOfAnyType(SDValue N) const
SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
SDValue getBasicBlock(MachineBasicBlock *MBB)
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...
bool isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
bool isKnownNeverZero(SDValue Op, unsigned Depth=0) const
Test whether the given SDValue is known to contain non-zero value(s).
SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
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 ...
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)
static const fltSemantics & EVTToAPFloatSemantics(EVT VT)
Returns an APFloat semantics tag appropriate for the given type.
SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
Definition: SelectionDAG.h:473
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...
std::pair< SDValue, SDValue > SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL)
Split the explicit vector length parameter of a VP operation.
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...
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,...
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()
Definition: SelectionDAG.h:543
SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
SDValue WidenVector(const SDValue &N, const SDLoc &DL)
Widen the vector up to the next power of two using INSERT_SUBVECTOR.
bool isKnownNeverZeroFloat(SDValue Op) const
Test whether the given floating point SDValue is known to never be positive or negative zero.
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)
SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getValueType(EVT)
ArrayRef< SDDbgValue * > GetDbgValues(const SDNode *SD) const
Get the debug values which reference the given SDNode.
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
OverflowKind computeOverflowForSignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the signed addition of 2 nodes can overflow.
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...
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
Definition: SelectionDAG.h:539
SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
bool isKnownNeverNaN(SDValue Op, 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.
SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
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)
Definition: SelectionDAG.h:676
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.
bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Return true if 'Op' is known to be zero in DemandedElts.
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.
SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
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)
SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
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,...
SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
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
Definition: SelectionDAG.h:469
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 ...
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.
OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
Definition: SelectionDAG.h:845
SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT SVT, MachineMemOperand *MMO, bool IsCompressing=false)
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...
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.
SDValue getRegisterMask(const uint32_t *RegMask)
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...
SDValue getCondCode(ISD::CondCode Cond)
SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex, int64_t Size, int64_t Offset=-1)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the por...
bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
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.
OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
LLVMContext * getContext() const
Definition: SelectionDAG.h:485
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.
Definition: SelectionDAG.h:560
const APInt * getValidMinimumShiftAmountConstant(SDValue V, const APInt &DemandedElts) const
If a SHL/SRA/SRL node V has constant shift amounts that are all less than the element bit-width of th...
SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL, bool LegalTypes=true)
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=0, const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
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...
SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
SDNode * getNodeIfExists(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops, const SDNodeFlags Flags)
Get the specified node if it's already available, or else return NULL.
SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
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.
Definition: SelectionDAG.h:554
SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
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.
Definition: SelectionDAG.h:878
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.
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 ...
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...
SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
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 ...
SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
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
Definition: SelectionDAG.h:534
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
static bool isSplatMask(const int *Mask, EVT VT)
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.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:321
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false.
Definition: SmallPtrSet.h:356
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:360
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:342
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:717
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
void reserve(size_type N)
Definition: SmallVector.h:676
iterator erase(const_iterator CI)
Definition: SmallVector.h:750
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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.
CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const
Get the CallingConv that should be used for the specified libcall.
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.
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
virtual MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
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.
virtual bool isCommutativeBinOp(unsigned Opcode) const
Returns true if the opcode is a commutative binary operation.
virtual ISD::NodeType getExtendForAtomicOps() const
Returns how the platform's atomic operations are extended (ZERO_EXTEND, SIGN_EXTEND,...
virtual bool isExtractSubvectorCheap(EVT ResVT, EVT SrcVT, unsigned Index) const
Return true if EXTRACT_SUBVECTOR is cheap for extracting this result type from this source type with ...
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 getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL, bool LegalTypes=true) const
Returns the type for the shift amount of a shift opcode.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
BooleanContent getBooleanContents(bool isVec, bool isFloat) const
For targets without i1 registers, this gives the nature of the high-bits of boolean values held in ty...
bool isCondCodeLegal(ISD::CondCode CC, MVT VT) const
Return true if the specified condition code is legal on this target.
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...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
BooleanContent
Enum that describes how the target represents true/false values.
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
Align getMinStackArgumentAlignment() const
Return the minimum stack alignment of an argument.
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
std::vector< ArgListEntry > ArgListTy
virtual bool hasVectorBlend() const
Return true if the target has a vector blend instruction.
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
MVT getFrameIndexTy(const DataLayout &DL) const
Return the type for frame index, which is determined by the alloca address space specified through th...
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
unsigned getVectorTypeBreakdown(LLVMContext &Context, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Vector types are broken down into some number of legal first class types.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual void computeKnownBitsForFrameIndex(int FIOp, KnownBits &Known, const MachineFunction &MF) const
Determine which of the bits of FrameIndex FIOp are known to be 0.
virtual unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
This method can be implemented by targets that want to expose additional information about sign bits ...
virtual void verifyTargetSDNode(const SDNode *N) const
Check the given SDNode. Aborts if it is invalid.
virtual bool findOptimalMemOpLowering(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.
virtual bool isKnownNeverNaNForTargetNode(SDValue Op, const SelectionDAG &DAG, bool SNaN=false, unsigned Depth=0) const
If SNaN is false,.
virtual void computeKnownBitsForTargetNode(const SDValue Op, KnownBits &Known, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
Determine which of the bits specified in Mask are known to be either zero or one and return them in t...
virtual bool isSDNodeSourceOfDivergence(const SDNode *N, FunctionLoweringInfo *FLI, UniformityInfo *UA) const
virtual bool isSDNodeAlwaysUniform(const SDNode *N) const
virtual bool isSplatValueForTargetNode(SDValue Op, const APInt &DemandedElts, APInt &UndefElts, const SelectionDAG &DAG, unsigned Depth=0) const
Return true if vector Op has the same value across all DemandedElts, indicating any elements which ma...
virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const
Return true if folding a constant offset with the given GlobalAddress is legal.
virtual const Constant * getTargetConstantFromLoad(LoadSDNode *LD) const
This method returns the constant pool value that will be loaded by LD.
virtual bool isGAPlusOffset(SDNode *N, const GlobalValue *&GA, int64_t &Offset) const
Returns true (and the GlobalValue and the offset) if the node is a GlobalAddress + offset.
virtual bool isGuaranteedNotToBeUndefOrPoisonForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool PoisonOnly, unsigned Depth) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
virtual bool canCreateUndefOrPoisonForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool PoisonOnly, bool ConsiderFlags, unsigned Depth) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:76
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetOptions Options
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, XROS, or DriverKit).
Definition: Triple.h:558
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition: TypeSize.h:342
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:265
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt8Ty(LLVMContext &C)
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
void set(Value *Val)
Definition: Value.h:882
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:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition: DenseSet.h:185
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:199
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:229
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
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:168
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:236
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:660
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:678
SmartMutex - A mutex with a compile time constant parameter that indicates whether this mutex should ...
Definition: Mutex.h:28
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition: APInt.cpp:3100
const APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition: APInt.h:2203
APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition: APInt.cpp:3087
APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition: APInt.cpp:3077
const APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition: APInt.h:2198
APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition: APInt.cpp:3092
APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition: APInt.cpp:2978
APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition: APInt.cpp:3072
APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition: APInt.cpp:3082
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
CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
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...
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:40
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition: ISDOpcodes.h:751
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition: ISDOpcodes.h:237
@ CTLZ_ZERO_UNDEF
Definition: ISDOpcodes.h:724
@ TargetConstantPool
Definition: ISDOpcodes.h:168
@ MDNODE_SDNODE
MDNODE_SDNODE - This is a node that holdes an MDNode*, which is used to reference metadata in the IR.
Definition: ISDOpcodes.h:1178
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition: ISDOpcodes.h:477
@ ATOMIC_LOAD_FMAX
Definition: ISDOpcodes.h:1283
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition: ISDOpcodes.h:44
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
Definition: ISDOpcodes.h:1346
@ VECREDUCE_SMIN
Definition: ISDOpcodes.h:1377
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition: ISDOpcodes.h:498
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition: ISDOpcodes.h:251
@ ATOMIC_LOAD_NAND
Definition: ISDOpcodes.h:1276
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition: ISDOpcodes.h:560
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
Definition: ISDOpcodes.h:1067
@ BSWAP
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:715
@ TargetBlockAddress
Definition: ISDOpcodes.h:170
@ ConstantFP
Definition: ISDOpcodes.h:77
@ ATOMIC_LOAD_MAX
Definition: ISDOpcodes.h:1278
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, ptr, val) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:1248
@ ATOMIC_LOAD_UMIN
Definition: ISDOpcodes.h:1279
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:270
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition: ISDOpcodes.h:488
@ FMAXNUM_IEEE
Definition: ISDOpcodes.h:986
@ ADD
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:240
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:1038
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:784
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:484
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition: ISDOpcodes.h:199
@ GlobalAddress
Definition: ISDOpcodes.h:78
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
Definition: ISDOpcodes.h:1261
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:791
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:544
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
Definition: ISDOpcodes.h:1362
@ FADD
Simple binary floating point operators.
Definition: ISDOpcodes.h:391
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
Definition: ISDOpcodes.h:1366
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition: ISDOpcodes.h:689
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition: ISDOpcodes.h:821
@ VECREDUCE_SMAX
Definition: ISDOpcodes.h:1376
@ STRICT_FSETCCS
Definition: ISDOpcodes.h:478
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition: ISDOpcodes.h:914
@ ATOMIC_LOAD_OR
Definition: ISDOpcodes.h:1274
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:904
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:230
@ ATOMIC_LOAD_XOR
Definition: ISDOpcodes.h:1275
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Definition: ISDOpcodes.h:940
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
Definition: ISDOpcodes.h:1407
@ ATOMIC_LOAD_FADD
Definition: ISDOpcodes.h:1281
@ GlobalTLSAddress
Definition: ISDOpcodes.h:79
@ SRCVALUE
SRCVALUE - This is a node type that holds a Value* that is used to make reference to a value in the L...
Definition: ISDOpcodes.h:1174
@ FrameIndex
Definition: ISDOpcodes.h:80
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
Definition: ISDOpcodes.h:1109
@ SIGN_EXTEND
Conversion operators.
Definition: ISDOpcodes.h:775
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition: ISDOpcodes.h:663
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition: ISDOpcodes.h:621
@ TargetExternalSymbol
Definition: ISDOpcodes.h:169
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
Definition: ISDOpcodes.h:1359
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition: ISDOpcodes.h:723
@ TargetJumpTable
Definition: ISDOpcodes.h:167
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition: ISDOpcodes.h:177
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
Definition: ISDOpcodes.h:1228
@ VECREDUCE_FMIN
Definition: ISDOpcodes.h:1363
@ 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:759
@ FNEG
Perform various unary floating-point operations inspired by libm.
Definition: ISDOpcodes.h:931
@ ATOMIC_LOAD_FSUB
Definition: ISDOpcodes.h:1282
@ BR_CC
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:1084
@ SSUBO
Same for subtraction.
Definition: ISDOpcodes.h:328
@ ATOMIC_LOAD_MIN
Definition: ISDOpcodes.h:1277
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition: ISDOpcodes.h:647
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:501
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition: ISDOpcodes.h:350
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition: ISDOpcodes.h:728
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
Definition: ISDOpcodes.h:1244
@ UNDEF
UNDEF - An undefined node.
Definition: ISDOpcodes.h:212
@ VECREDUCE_UMAX
Definition: ISDOpcodes.h:1378
@ RegisterMask
Definition: ISDOpcodes.h:75
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition: ISDOpcodes.h:223
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition: ISDOpcodes.h:628
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition: ISDOpcodes.h:68
@ ATOMIC_LOAD_FMIN
Definition: ISDOpcodes.h:1284
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition: ISDOpcodes.h:209
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition: ISDOpcodes.h:324
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition: ISDOpcodes.h:164
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
Definition: ISDOpcodes.h:1371
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:652
@ SHL
Shift and rotation operations.
Definition: ISDOpcodes.h:706
@ ATOMIC_LOAD_CLR
Definition: ISDOpcodes.h:1273
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition: ISDOpcodes.h:601
@ ATOMIC_LOAD_AND
Definition: ISDOpcodes.h:1272
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition: ISDOpcodes.h:574
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
Definition: ISDOpcodes.h:985
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition: ISDOpcodes.h:47
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition: ISDOpcodes.h:536
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:781
@ TargetConstantFP
Definition: ISDOpcodes.h:159
@ FP_TO_UINT_SAT
Definition: ISDOpcodes.h:857
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:743
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
Definition: ISDOpcodes.h:1336
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
Definition: ISDOpcodes.h:1255
@ ATOMIC_LOAD_UMAX
Definition: ISDOpcodes.h:1280
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:972
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition: ISDOpcodes.h:360
@ SMULO
Same for multiplication.
Definition: ISDOpcodes.h:332
@ TargetFrameIndex
Definition: ISDOpcodes.h:166
@ ConstantPool
Definition: ISDOpcodes.h:82
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition: ISDOpcodes.h:810
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:799
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition: ISDOpcodes.h:675
@ LIFETIME_START
This corresponds to the llvm.lifetime.
Definition: ISDOpcodes.h:1311
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:889
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition: ISDOpcodes.h:737
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:304
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
Definition: ISDOpcodes.h:1198
@ VECREDUCE_UMIN
Definition: ISDOpcodes.h:1379
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
Definition: ISDOpcodes.h:923
@ ATOMIC_LOAD_UDEC_WRAP
Definition: ISDOpcodes.h:1286
@ ATOMIC_LOAD_ADD
Definition: ISDOpcodes.h:1270
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition: ISDOpcodes.h:466
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
Definition: ISDOpcodes.h:991
@ ATOMIC_LOAD_SUB
Definition: ISDOpcodes.h:1271
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:837
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition: ISDOpcodes.h:158
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:471
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:681
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition: ISDOpcodes.h:184
@ GET_FPENV_MEM
Gets the current floating-point environment.
Definition: ISDOpcodes.h:1014
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
Definition: ISDOpcodes.h:1331
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition: ISDOpcodes.h:658
@ VECREDUCE_FMUL
Definition: ISDOpcodes.h:1360
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:280
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition: ISDOpcodes.h:637
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition: ISDOpcodes.h:525
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition: ISDOpcodes.h:52
@ VECTOR_SPLICE
VECTOR_SPLICE(VEC1, VEC2, IMM) - Returns a subvector of the same type as VEC1/VEC2 from CONCAT_VECTOR...
Definition: ISDOpcodes.h:613
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
Definition: ISDOpcodes.h:1269
@ ExternalSymbol
Definition: ISDOpcodes.h:83
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
Definition: ISDOpcodes.h:945
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:870
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition: ISDOpcodes.h:832
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition: ISDOpcodes.h:908
@ 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:856
@ VECREDUCE_FMINIMUM
Definition: ISDOpcodes.h:1367
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:787
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
Definition: ISDOpcodes.h:1153
@ BlockAddress
Definition: ISDOpcodes.h:84
@ VECREDUCE_SEQ_FMUL
Definition: ISDOpcodes.h:1347
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition: ISDOpcodes.h:764
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition: ISDOpcodes.h:61
@ ATOMIC_LOAD_UINC_WRAP
Definition: ISDOpcodes.h:1285
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition: ISDOpcodes.h:494
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition: ISDOpcodes.h:341
@ AssertZext
Definition: ISDOpcodes.h:62
@ SET_FPENV_MEM
Sets the current floating point environment.
Definition: ISDOpcodes.h:1019
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:314
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition: ISDOpcodes.h:192
@ TargetGlobalTLSAddress
Definition: ISDOpcodes.h:165
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition: ISDOpcodes.h:516
unsigned getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
bool isBuildVectorOfConstantSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantSDNode or undef.
NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantSDNode predicate.
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.
static const int FIRST_TARGET_MEMORY_OPCODE
FIRST_TARGET_MEMORY_OPCODE - Target-specific pre-isel operations which do not reference a specific me...
Definition: ISDOpcodes.h:1419
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...
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...
bool matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
bool isVPBinaryOp(unsigned Opcode)
Whether this is a vector-predicated binary operation opcode.
CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
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.
Definition: ISDOpcodes.h:1588
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...
Definition: ISDOpcodes.h:1593
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.
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...
bool isFreezeUndef(const SDNode *N)
Return true if the specified node is FREEZE(UNDEF).
CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
Definition: ISDOpcodes.h:1492
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 isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
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...
bool isVPReduction(unsigned Opcode)
Whether this is a vector-predicated reduction opcode.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
Definition: ISDOpcodes.h:1479
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,...
Definition: ISDOpcodes.h:1530
bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef.
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).
Definition: ISDOpcodes.h:1510
bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
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< 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()...
Definition: PatternMatch.h:893
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
Libcall
RTLIB::Libcall enum - This enum defines all of the runtime library calls the backend can emit.
Libcall getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMCPY_ELEMENT_UNORDERED_ATOMIC - Return MEMCPY_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMSET_ELEMENT_UNORDERED_ATOMIC - Return MEMSET_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
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)
Definition: CommandLine.h:450
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition: Dwarf.h:146
std::lock_guard< SmartMutex< mt_only > > SmartScopedLock
Definition: Mutex.h:69
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:337
@ Offset
Definition: DWP.cpp:456
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:361
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition: Analysis.cpp:233
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:1722
bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:239
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:1527
SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
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 are tuples (A,...
Definition: STLExtras.h:2406
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition: bit.h:307
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
bool getAlign(const Function &F, unsigned index, unsigned &align)
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.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition: bit.h:317
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition: APFloat.h:1436
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2073
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:656
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:1509
bool isMinSignedConstant(SDValue V)
Returns true if V is a constant min signed integer value.
ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
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:1373
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
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:1729
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 2019 maximumNumber semantics.
Definition: APFloat.h:1410
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:324
bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:275
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
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:1736
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
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...
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:54
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...
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 minimumNumber semantics.
Definition: APFloat.h:1396
@ Mul
Product of integers.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
DWARFExpression::Operation Op
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:1824
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
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:1879
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition: Alignment.h:212
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:465
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
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:1423
bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo)
Returns true if V is a neutral element of Opc with Flags.
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:360
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:760
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition: Metadata.h:780
MDNode * TBAA
The tag for type-based alias analysis.
Definition: Metadata.h:777
static const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:249
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition: APFloat.h:220
static constexpr roundingMode rmTowardNegative
Definition: APFloat.h:233
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:230
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:234
static const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:251
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:250
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:247
static constexpr roundingMode rmTowardPositive
Definition: APFloat.h:232
static const fltSemantics & BFloat() LLVM_READNONE
Definition: APFloat.cpp:248
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:246
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Align previous() const
Definition: Alignment.h:88
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:34
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition: ValueTypes.h:380
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition: ValueTypes.h:136
intptr_t getRawBits() const
Definition: ValueTypes.h:495
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:73
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition: ValueTypes.h:274
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:290
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition: ValueTypes.h:146
ElementCount getVectorElementCount() const
Definition: ValueTypes.h:340
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:358
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition: ValueTypes.h:349
uint64_t getScalarSizeInBits() const
Definition: ValueTypes.h:370
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:306
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition: ValueTypes.h:64
bool isFixedLengthVector() const
Definition: ValueTypes.h:177
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:167
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:313
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition: ValueTypes.h:282
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition: ValueTypes.h:246
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:202
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition: ValueTypes.h:173
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:318
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition: ValueTypes.h:141
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:326
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:298
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition: ValueTypes.h:438
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition: ValueTypes.h:151
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:297
KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
Definition: KnownBits.cpp:155
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:884
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:251
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:208
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:104
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:77
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:120
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:773
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:238
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
Definition: KnownBits.cpp:434
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1030
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:63
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:270
static std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
Definition: KnownBits.cpp:502
void makeNegative()
Make this value negative.
Definition: KnownBits.h:115
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:157
KnownBits byteSwap() const
Definition: KnownBits.h:457
bool hasConflict() const
Returns true if there is conflicting information.
Definition: KnownBits.h:47
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:285
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:89
KnownBits reverseBits() const
Definition: KnownBits.h:461
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition: KnownBits.h:229
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition: KnownBits.cpp:184
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition: KnownBits.h:168
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:71
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:317
static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
Definition: KnownBits.cpp:376
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition: KnownBits.h:107
static KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
Definition: KnownBits.cpp:234
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition: KnownBits.h:221
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:307
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:176
static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
Definition: KnownBits.cpp:143
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:192
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:141
static KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
Definition: KnownBits.cpp:253
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition: KnownBits.cpp:221
static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
Definition: KnownBits.cpp:875
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1049
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:988
static 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:57
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition: KnownBits.h:110
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:929
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:328
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:101
static 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:50
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:276
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:215
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:777
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:163
KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition: KnownBits.cpp:556
static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
Definition: KnownBits.cpp:291
static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
Definition: KnownBits.cpp:202
This class contains a discriminated union of information about pointers in memory operands,...
bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
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 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:117
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)
These are IR-level optimization flags that may be propagated to SDNodes.
void intersectWith(const SDNodeFlags Flags)
Clear any flags in this flag set that aren't also set in Flags.
bool hasNonNeg() const
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.
Definition: SelectionDAG.h:307
DAGUpdateListener *const Next
Definition: SelectionDAG.h:308
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)