Bug Summary

File:llvm/include/llvm/CodeGen/SelectionDAGNodes.h
Warning:line 1149, column 10
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SelectionDAG.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-11/lib/clang/11.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/lib/CodeGen/SelectionDAG -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-11/lib/clang/11.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/lib/CodeGen/SelectionDAG -fdebug-prefix-map=/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-03-09-184146-41876-1 -x c++ /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

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
13#include "llvm/CodeGen/SelectionDAG.h"
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/FoldingSet.h"
21#include "llvm/ADT/None.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/SmallPtrSet.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/Triple.h"
26#include "llvm/ADT/Twine.h"
27#include "llvm/Analysis/BlockFrequencyInfo.h"
28#include "llvm/Analysis/MemoryLocation.h"
29#include "llvm/Analysis/ProfileSummaryInfo.h"
30#include "llvm/Analysis/ValueTracking.h"
31#include "llvm/CodeGen/ISDOpcodes.h"
32#include "llvm/CodeGen/MachineBasicBlock.h"
33#include "llvm/CodeGen/MachineConstantPool.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineFunction.h"
36#include "llvm/CodeGen/MachineMemOperand.h"
37#include "llvm/CodeGen/RuntimeLibcalls.h"
38#include "llvm/CodeGen/SelectionDAGAddressAnalysis.h"
39#include "llvm/CodeGen/SelectionDAGNodes.h"
40#include "llvm/CodeGen/SelectionDAGTargetInfo.h"
41#include "llvm/CodeGen/TargetLowering.h"
42#include "llvm/CodeGen/TargetRegisterInfo.h"
43#include "llvm/CodeGen/TargetSubtargetInfo.h"
44#include "llvm/CodeGen/ValueTypes.h"
45#include "llvm/IR/Constant.h"
46#include "llvm/IR/Constants.h"
47#include "llvm/IR/DataLayout.h"
48#include "llvm/IR/DebugInfoMetadata.h"
49#include "llvm/IR/DebugLoc.h"
50#include "llvm/IR/DerivedTypes.h"
51#include "llvm/IR/Function.h"
52#include "llvm/IR/GlobalValue.h"
53#include "llvm/IR/Metadata.h"
54#include "llvm/IR/Type.h"
55#include "llvm/IR/Value.h"
56#include "llvm/Support/Casting.h"
57#include "llvm/Support/CodeGen.h"
58#include "llvm/Support/Compiler.h"
59#include "llvm/Support/Debug.h"
60#include "llvm/Support/ErrorHandling.h"
61#include "llvm/Support/KnownBits.h"
62#include "llvm/Support/MachineValueType.h"
63#include "llvm/Support/ManagedStatic.h"
64#include "llvm/Support/MathExtras.h"
65#include "llvm/Support/Mutex.h"
66#include "llvm/Support/raw_ostream.h"
67#include "llvm/Target/TargetMachine.h"
68#include "llvm/Target/TargetOptions.h"
69#include "llvm/Transforms/Utils/SizeOpts.h"
70#include <algorithm>
71#include <cassert>
72#include <cstdint>
73#include <cstdlib>
74#include <limits>
75#include <set>
76#include <string>
77#include <utility>
78#include <vector>
79
80using namespace llvm;
81
82/// makeVTList - Return an instance of the SDVTList struct initialized with the
83/// specified members.
84static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
85 SDVTList Res = {VTs, NumVTs};
86 return Res;
87}
88
89// Default null implementations of the callbacks.
90void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
91void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
92void SelectionDAG::DAGUpdateListener::NodeInserted(SDNode *) {}
93
94void SelectionDAG::DAGNodeDeletedListener::anchor() {}
95
96#define DEBUG_TYPE"selectiondag" "selectiondag"
97
98static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
99 cl::Hidden, cl::init(true),
100 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
101
102static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
103 cl::desc("Number limit for gluing ld/st of memcpy."),
104 cl::Hidden, cl::init(0));
105
106static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G) {
107 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G);)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("selectiondag")) { dbgs() << Msg; V.getNode()->dump
(G);; } } while (false)
;
108}
109
110//===----------------------------------------------------------------------===//
111// ConstantFPSDNode Class
112//===----------------------------------------------------------------------===//
113
114/// isExactlyValue - We don't rely on operator== working on double values, as
115/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
116/// As such, this method can be used to do an exact bit-for-bit comparison of
117/// two floating point values.
118bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
119 return getValueAPF().bitwiseIsEqual(V);
120}
121
122bool ConstantFPSDNode::isValueValidForType(EVT VT,
123 const APFloat& Val) {
124 assert(VT.isFloatingPoint() && "Can only convert between FP types")((VT.isFloatingPoint() && "Can only convert between FP types"
) ? static_cast<void> (0) : __assert_fail ("VT.isFloatingPoint() && \"Can only convert between FP types\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 124, __PRETTY_FUNCTION__))
;
125
126 // convert modifies in place, so make a copy.
127 APFloat Val2 = APFloat(Val);
128 bool losesInfo;
129 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
130 APFloat::rmNearestTiesToEven,
131 &losesInfo);
132 return !losesInfo;
133}
134
135//===----------------------------------------------------------------------===//
136// ISD Namespace
137//===----------------------------------------------------------------------===//
138
139bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
140 auto *BV = dyn_cast<BuildVectorSDNode>(N);
141 if (!BV)
142 return false;
143
144 APInt SplatUndef;
145 unsigned SplatBitSize;
146 bool HasUndefs;
147 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
148 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
149 EltSize) &&
150 EltSize == SplatBitSize;
151}
152
153// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
154// specializations of the more general isConstantSplatVector()?
155
156bool ISD::isBuildVectorAllOnes(const SDNode *N) {
157 // Look through a bit convert.
158 while (N->getOpcode() == ISD::BITCAST)
159 N = N->getOperand(0).getNode();
160
161 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
162
163 unsigned i = 0, e = N->getNumOperands();
164
165 // Skip over all of the undef values.
166 while (i != e && N->getOperand(i).isUndef())
167 ++i;
168
169 // Do not accept an all-undef vector.
170 if (i == e) return false;
171
172 // Do not accept build_vectors that aren't all constants or which have non-~0
173 // elements. We have to be a bit careful here, as the type of the constant
174 // may not be the same as the type of the vector elements due to type
175 // legalization (the elements are promoted to a legal type for the target and
176 // a vector of a type may be legal when the base element type is not).
177 // We only want to check enough bits to cover the vector elements, because
178 // we care if the resultant vector is all ones, not whether the individual
179 // constants are.
180 SDValue NotZero = N->getOperand(i);
181 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
182 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
183 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
184 return false;
185 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
186 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
187 return false;
188 } else
189 return false;
190
191 // Okay, we have at least one ~0 value, check to see if the rest match or are
192 // undefs. Even with the above element type twiddling, this should be OK, as
193 // the same type legalization should have applied to all the elements.
194 for (++i; i != e; ++i)
195 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
196 return false;
197 return true;
198}
199
200bool ISD::isBuildVectorAllZeros(const SDNode *N) {
201 // Look through a bit convert.
202 while (N->getOpcode() == ISD::BITCAST)
203 N = N->getOperand(0).getNode();
204
205 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
206
207 bool IsAllUndef = true;
208 for (const SDValue &Op : N->op_values()) {
209 if (Op.isUndef())
210 continue;
211 IsAllUndef = false;
212 // Do not accept build_vectors that aren't all constants or which have non-0
213 // elements. We have to be a bit careful here, as the type of the constant
214 // may not be the same as the type of the vector elements due to type
215 // legalization (the elements are promoted to a legal type for the target
216 // and a vector of a type may be legal when the base element type is not).
217 // We only want to check enough bits to cover the vector elements, because
218 // we care if the resultant vector is all zeros, not whether the individual
219 // constants are.
220 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
221 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
222 if (CN->getAPIntValue().countTrailingZeros() < EltSize)
223 return false;
224 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
225 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
226 return false;
227 } else
228 return false;
229 }
230
231 // Do not accept an all-undef vector.
232 if (IsAllUndef)
233 return false;
234 return true;
235}
236
237bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
238 if (N->getOpcode() != ISD::BUILD_VECTOR)
239 return false;
240
241 for (const SDValue &Op : N->op_values()) {
242 if (Op.isUndef())
243 continue;
244 if (!isa<ConstantSDNode>(Op))
245 return false;
246 }
247 return true;
248}
249
250bool ISD::isBuildVectorOfConstantFPSDNodes(const SDNode *N) {
251 if (N->getOpcode() != ISD::BUILD_VECTOR)
252 return false;
253
254 for (const SDValue &Op : N->op_values()) {
255 if (Op.isUndef())
256 continue;
257 if (!isa<ConstantFPSDNode>(Op))
258 return false;
259 }
260 return true;
261}
262
263bool ISD::allOperandsUndef(const SDNode *N) {
264 // Return false if the node has no operands.
265 // This is "logically inconsistent" with the definition of "all" but
266 // is probably the desired behavior.
267 if (N->getNumOperands() == 0)
268 return false;
269 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
270}
271
272bool ISD::matchUnaryPredicate(SDValue Op,
273 std::function<bool(ConstantSDNode *)> Match,
274 bool AllowUndefs) {
275 // FIXME: Add support for scalar UNDEF cases?
276 if (auto *Cst = dyn_cast<ConstantSDNode>(Op))
277 return Match(Cst);
278
279 // FIXME: Add support for vector UNDEF cases?
280 if (ISD::BUILD_VECTOR != Op.getOpcode())
281 return false;
282
283 EVT SVT = Op.getValueType().getScalarType();
284 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
285 if (AllowUndefs && Op.getOperand(i).isUndef()) {
286 if (!Match(nullptr))
287 return false;
288 continue;
289 }
290
291 auto *Cst = dyn_cast<ConstantSDNode>(Op.getOperand(i));
292 if (!Cst || Cst->getValueType(0) != SVT || !Match(Cst))
293 return false;
294 }
295 return true;
296}
297
298bool ISD::matchBinaryPredicate(
299 SDValue LHS, SDValue RHS,
300 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
301 bool AllowUndefs, bool AllowTypeMismatch) {
302 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
1
Assuming 'AllowTypeMismatch' is true
2
Taking false branch
303 return false;
304
305 // TODO: Add support for scalar UNDEF cases?
306 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
3
Calling 'dyn_cast<llvm::ConstantSDNode, llvm::SDValue>'
18
Returning from 'dyn_cast<llvm::ConstantSDNode, llvm::SDValue>'
19
Assuming 'LHSCst' is null
20
Taking false branch
307 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
308 return Match(LHSCst, RHSCst);
309
310 // TODO: Add support for vector UNDEF cases?
311 if (ISD::BUILD_VECTOR != LHS.getOpcode() ||
21
Calling 'SDValue::getOpcode'
312 ISD::BUILD_VECTOR != RHS.getOpcode())
313 return false;
314
315 EVT SVT = LHS.getValueType().getScalarType();
316 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
317 SDValue LHSOp = LHS.getOperand(i);
318 SDValue RHSOp = RHS.getOperand(i);
319 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
320 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
321 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
322 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
323 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
324 return false;
325 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
326 LHSOp.getValueType() != RHSOp.getValueType()))
327 return false;
328 if (!Match(LHSCst, RHSCst))
329 return false;
330 }
331 return true;
332}
333
334ISD::NodeType ISD::getExtForLoadExtType(bool IsFP, ISD::LoadExtType ExtType) {
335 switch (ExtType) {
336 case ISD::EXTLOAD:
337 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
338 case ISD::SEXTLOAD:
339 return ISD::SIGN_EXTEND;
340 case ISD::ZEXTLOAD:
341 return ISD::ZERO_EXTEND;
342 default:
343 break;
344 }
345
346 llvm_unreachable("Invalid LoadExtType")::llvm::llvm_unreachable_internal("Invalid LoadExtType", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 346)
;
347}
348
349ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
350 // To perform this operation, we just need to swap the L and G bits of the
351 // operation.
352 unsigned OldL = (Operation >> 2) & 1;
353 unsigned OldG = (Operation >> 1) & 1;
354 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
355 (OldL << 1) | // New G bit
356 (OldG << 2)); // New L bit.
357}
358
359static ISD::CondCode getSetCCInverseImpl(ISD::CondCode Op, bool isIntegerLike) {
360 unsigned Operation = Op;
361 if (isIntegerLike)
362 Operation ^= 7; // Flip L, G, E bits, but not U.
363 else
364 Operation ^= 15; // Flip all of the condition bits.
365
366 if (Operation > ISD::SETTRUE2)
367 Operation &= ~8; // Don't let N and U bits get set.
368
369 return ISD::CondCode(Operation);
370}
371
372ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, EVT Type) {
373 return getSetCCInverseImpl(Op, Type.isInteger());
374}
375
376ISD::CondCode ISD::GlobalISel::getSetCCInverse(ISD::CondCode Op,
377 bool isIntegerLike) {
378 return getSetCCInverseImpl(Op, isIntegerLike);
379}
380
381/// For an integer comparison, return 1 if the comparison is a signed operation
382/// and 2 if the result is an unsigned comparison. Return zero if the operation
383/// does not depend on the sign of the input (setne and seteq).
384static int isSignedOp(ISD::CondCode Opcode) {
385 switch (Opcode) {
386 default: llvm_unreachable("Illegal integer setcc operation!")::llvm::llvm_unreachable_internal("Illegal integer setcc operation!"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 386)
;
387 case ISD::SETEQ:
388 case ISD::SETNE: return 0;
389 case ISD::SETLT:
390 case ISD::SETLE:
391 case ISD::SETGT:
392 case ISD::SETGE: return 1;
393 case ISD::SETULT:
394 case ISD::SETULE:
395 case ISD::SETUGT:
396 case ISD::SETUGE: return 2;
397 }
398}
399
400ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
401 EVT Type) {
402 bool IsInteger = Type.isInteger();
403 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
404 // Cannot fold a signed integer setcc with an unsigned integer setcc.
405 return ISD::SETCC_INVALID;
406
407 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
408
409 // If the N and U bits get set, then the resultant comparison DOES suddenly
410 // care about orderedness, and it is true when ordered.
411 if (Op > ISD::SETTRUE2)
412 Op &= ~16; // Clear the U bit if the N bit is set.
413
414 // Canonicalize illegal integer setcc's.
415 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
416 Op = ISD::SETNE;
417
418 return ISD::CondCode(Op);
419}
420
421ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
422 EVT Type) {
423 bool IsInteger = Type.isInteger();
424 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
425 // Cannot fold a signed setcc with an unsigned setcc.
426 return ISD::SETCC_INVALID;
427
428 // Combine all of the condition bits.
429 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
430
431 // Canonicalize illegal integer setcc's.
432 if (IsInteger) {
433 switch (Result) {
434 default: break;
435 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
436 case ISD::SETOEQ: // SETEQ & SETU[LG]E
437 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
438 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
439 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
440 }
441 }
442
443 return Result;
444}
445
446//===----------------------------------------------------------------------===//
447// SDNode Profile Support
448//===----------------------------------------------------------------------===//
449
450/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
451static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
452 ID.AddInteger(OpC);
453}
454
455/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
456/// solely with their pointer.
457static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
458 ID.AddPointer(VTList.VTs);
459}
460
461/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
462static void AddNodeIDOperands(FoldingSetNodeID &ID,
463 ArrayRef<SDValue> Ops) {
464 for (auto& Op : Ops) {
465 ID.AddPointer(Op.getNode());
466 ID.AddInteger(Op.getResNo());
467 }
468}
469
470/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
471static void AddNodeIDOperands(FoldingSetNodeID &ID,
472 ArrayRef<SDUse> Ops) {
473 for (auto& Op : Ops) {
474 ID.AddPointer(Op.getNode());
475 ID.AddInteger(Op.getResNo());
476 }
477}
478
479static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
480 SDVTList VTList, ArrayRef<SDValue> OpList) {
481 AddNodeIDOpcode(ID, OpC);
482 AddNodeIDValueTypes(ID, VTList);
483 AddNodeIDOperands(ID, OpList);
484}
485
486/// If this is an SDNode with special info, add this info to the NodeID data.
487static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
488 switch (N->getOpcode()) {
489 case ISD::TargetExternalSymbol:
490 case ISD::ExternalSymbol:
491 case ISD::MCSymbol:
492 llvm_unreachable("Should only be used on nodes with operands")::llvm::llvm_unreachable_internal("Should only be used on nodes with operands"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 492)
;
493 default: break; // Normal nodes don't need extra info.
494 case ISD::TargetConstant:
495 case ISD::Constant: {
496 const ConstantSDNode *C = cast<ConstantSDNode>(N);
497 ID.AddPointer(C->getConstantIntValue());
498 ID.AddBoolean(C->isOpaque());
499 break;
500 }
501 case ISD::TargetConstantFP:
502 case ISD::ConstantFP:
503 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
504 break;
505 case ISD::TargetGlobalAddress:
506 case ISD::GlobalAddress:
507 case ISD::TargetGlobalTLSAddress:
508 case ISD::GlobalTLSAddress: {
509 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
510 ID.AddPointer(GA->getGlobal());
511 ID.AddInteger(GA->getOffset());
512 ID.AddInteger(GA->getTargetFlags());
513 break;
514 }
515 case ISD::BasicBlock:
516 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
517 break;
518 case ISD::Register:
519 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
520 break;
521 case ISD::RegisterMask:
522 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
523 break;
524 case ISD::SRCVALUE:
525 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
526 break;
527 case ISD::FrameIndex:
528 case ISD::TargetFrameIndex:
529 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
530 break;
531 case ISD::LIFETIME_START:
532 case ISD::LIFETIME_END:
533 if (cast<LifetimeSDNode>(N)->hasOffset()) {
534 ID.AddInteger(cast<LifetimeSDNode>(N)->getSize());
535 ID.AddInteger(cast<LifetimeSDNode>(N)->getOffset());
536 }
537 break;
538 case ISD::JumpTable:
539 case ISD::TargetJumpTable:
540 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
541 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
542 break;
543 case ISD::ConstantPool:
544 case ISD::TargetConstantPool: {
545 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
546 ID.AddInteger(CP->getAlignment());
547 ID.AddInteger(CP->getOffset());
548 if (CP->isMachineConstantPoolEntry())
549 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
550 else
551 ID.AddPointer(CP->getConstVal());
552 ID.AddInteger(CP->getTargetFlags());
553 break;
554 }
555 case ISD::TargetIndex: {
556 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
557 ID.AddInteger(TI->getIndex());
558 ID.AddInteger(TI->getOffset());
559 ID.AddInteger(TI->getTargetFlags());
560 break;
561 }
562 case ISD::LOAD: {
563 const LoadSDNode *LD = cast<LoadSDNode>(N);
564 ID.AddInteger(LD->getMemoryVT().getRawBits());
565 ID.AddInteger(LD->getRawSubclassData());
566 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
567 break;
568 }
569 case ISD::STORE: {
570 const StoreSDNode *ST = cast<StoreSDNode>(N);
571 ID.AddInteger(ST->getMemoryVT().getRawBits());
572 ID.AddInteger(ST->getRawSubclassData());
573 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
574 break;
575 }
576 case ISD::MLOAD: {
577 const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
578 ID.AddInteger(MLD->getMemoryVT().getRawBits());
579 ID.AddInteger(MLD->getRawSubclassData());
580 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
581 break;
582 }
583 case ISD::MSTORE: {
584 const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
585 ID.AddInteger(MST->getMemoryVT().getRawBits());
586 ID.AddInteger(MST->getRawSubclassData());
587 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
588 break;
589 }
590 case ISD::MGATHER: {
591 const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
592 ID.AddInteger(MG->getMemoryVT().getRawBits());
593 ID.AddInteger(MG->getRawSubclassData());
594 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
595 break;
596 }
597 case ISD::MSCATTER: {
598 const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
599 ID.AddInteger(MS->getMemoryVT().getRawBits());
600 ID.AddInteger(MS->getRawSubclassData());
601 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
602 break;
603 }
604 case ISD::ATOMIC_CMP_SWAP:
605 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
606 case ISD::ATOMIC_SWAP:
607 case ISD::ATOMIC_LOAD_ADD:
608 case ISD::ATOMIC_LOAD_SUB:
609 case ISD::ATOMIC_LOAD_AND:
610 case ISD::ATOMIC_LOAD_CLR:
611 case ISD::ATOMIC_LOAD_OR:
612 case ISD::ATOMIC_LOAD_XOR:
613 case ISD::ATOMIC_LOAD_NAND:
614 case ISD::ATOMIC_LOAD_MIN:
615 case ISD::ATOMIC_LOAD_MAX:
616 case ISD::ATOMIC_LOAD_UMIN:
617 case ISD::ATOMIC_LOAD_UMAX:
618 case ISD::ATOMIC_LOAD:
619 case ISD::ATOMIC_STORE: {
620 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
621 ID.AddInteger(AT->getMemoryVT().getRawBits());
622 ID.AddInteger(AT->getRawSubclassData());
623 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
624 break;
625 }
626 case ISD::PREFETCH: {
627 const MemSDNode *PF = cast<MemSDNode>(N);
628 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
629 break;
630 }
631 case ISD::VECTOR_SHUFFLE: {
632 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
633 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
634 i != e; ++i)
635 ID.AddInteger(SVN->getMaskElt(i));
636 break;
637 }
638 case ISD::TargetBlockAddress:
639 case ISD::BlockAddress: {
640 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
641 ID.AddPointer(BA->getBlockAddress());
642 ID.AddInteger(BA->getOffset());
643 ID.AddInteger(BA->getTargetFlags());
644 break;
645 }
646 } // end switch (N->getOpcode())
647
648 // Target specific memory nodes could also have address spaces to check.
649 if (N->isTargetMemoryOpcode())
650 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
651}
652
653/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
654/// data.
655static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
656 AddNodeIDOpcode(ID, N->getOpcode());
657 // Add the return value info.
658 AddNodeIDValueTypes(ID, N->getVTList());
659 // Add the operand info.
660 AddNodeIDOperands(ID, N->ops());
661
662 // Handle SDNode leafs with special info.
663 AddNodeIDCustom(ID, N);
664}
665
666//===----------------------------------------------------------------------===//
667// SelectionDAG Class
668//===----------------------------------------------------------------------===//
669
670/// doNotCSE - Return true if CSE should not be performed for this node.
671static bool doNotCSE(SDNode *N) {
672 if (N->getValueType(0) == MVT::Glue)
673 return true; // Never CSE anything that produces a flag.
674
675 switch (N->getOpcode()) {
676 default: break;
677 case ISD::HANDLENODE:
678 case ISD::EH_LABEL:
679 return true; // Never CSE these nodes.
680 }
681
682 // Check that remaining values produced are not flags.
683 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
684 if (N->getValueType(i) == MVT::Glue)
685 return true; // Never CSE anything that produces a flag.
686
687 return false;
688}
689
690/// RemoveDeadNodes - This method deletes all unreachable nodes in the
691/// SelectionDAG.
692void SelectionDAG::RemoveDeadNodes() {
693 // Create a dummy node (which is not added to allnodes), that adds a reference
694 // to the root node, preventing it from being deleted.
695 HandleSDNode Dummy(getRoot());
696
697 SmallVector<SDNode*, 128> DeadNodes;
698
699 // Add all obviously-dead nodes to the DeadNodes worklist.
700 for (SDNode &Node : allnodes())
701 if (Node.use_empty())
702 DeadNodes.push_back(&Node);
703
704 RemoveDeadNodes(DeadNodes);
705
706 // If the root changed (e.g. it was a dead load, update the root).
707 setRoot(Dummy.getValue());
708}
709
710/// RemoveDeadNodes - This method deletes the unreachable nodes in the
711/// given list, and any nodes that become unreachable as a result.
712void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
713
714 // Process the worklist, deleting the nodes and adding their uses to the
715 // worklist.
716 while (!DeadNodes.empty()) {
717 SDNode *N = DeadNodes.pop_back_val();
718 // Skip to next node if we've already managed to delete the node. This could
719 // happen if replacing a node causes a node previously added to the node to
720 // be deleted.
721 if (N->getOpcode() == ISD::DELETED_NODE)
722 continue;
723
724 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
725 DUL->NodeDeleted(N, nullptr);
726
727 // Take the node out of the appropriate CSE map.
728 RemoveNodeFromCSEMaps(N);
729
730 // Next, brutally remove the operand list. This is safe to do, as there are
731 // no cycles in the graph.
732 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
733 SDUse &Use = *I++;
734 SDNode *Operand = Use.getNode();
735 Use.set(SDValue());
736
737 // Now that we removed this operand, see if there are no uses of it left.
738 if (Operand->use_empty())
739 DeadNodes.push_back(Operand);
740 }
741
742 DeallocateNode(N);
743 }
744}
745
746void SelectionDAG::RemoveDeadNode(SDNode *N){
747 SmallVector<SDNode*, 16> DeadNodes(1, N);
748
749 // Create a dummy node that adds a reference to the root node, preventing
750 // it from being deleted. (This matters if the root is an operand of the
751 // dead node.)
752 HandleSDNode Dummy(getRoot());
753
754 RemoveDeadNodes(DeadNodes);
755}
756
757void SelectionDAG::DeleteNode(SDNode *N) {
758 // First take this out of the appropriate CSE map.
759 RemoveNodeFromCSEMaps(N);
760
761 // Finally, remove uses due to operands of this node, remove from the
762 // AllNodes list, and delete the node.
763 DeleteNodeNotInCSEMaps(N);
764}
765
766void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
767 assert(N->getIterator() != AllNodes.begin() &&((N->getIterator() != AllNodes.begin() && "Cannot delete the entry node!"
) ? static_cast<void> (0) : __assert_fail ("N->getIterator() != AllNodes.begin() && \"Cannot delete the entry node!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 768, __PRETTY_FUNCTION__))
768 "Cannot delete the entry node!")((N->getIterator() != AllNodes.begin() && "Cannot delete the entry node!"
) ? static_cast<void> (0) : __assert_fail ("N->getIterator() != AllNodes.begin() && \"Cannot delete the entry node!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 768, __PRETTY_FUNCTION__))
;
769 assert(N->use_empty() && "Cannot delete a node that is not dead!")((N->use_empty() && "Cannot delete a node that is not dead!"
) ? static_cast<void> (0) : __assert_fail ("N->use_empty() && \"Cannot delete a node that is not dead!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 769, __PRETTY_FUNCTION__))
;
770
771 // Drop all of the operands and decrement used node's use counts.
772 N->DropOperands();
773
774 DeallocateNode(N);
775}
776
777void SDDbgInfo::erase(const SDNode *Node) {
778 DbgValMapType::iterator I = DbgValMap.find(Node);
779 if (I == DbgValMap.end())
780 return;
781 for (auto &Val: I->second)
782 Val->setIsInvalidated();
783 DbgValMap.erase(I);
784}
785
786void SelectionDAG::DeallocateNode(SDNode *N) {
787 // If we have operands, deallocate them.
788 removeOperands(N);
789
790 NodeAllocator.Deallocate(AllNodes.remove(N));
791
792 // Set the opcode to DELETED_NODE to help catch bugs when node
793 // memory is reallocated.
794 // FIXME: There are places in SDag that have grown a dependency on the opcode
795 // value in the released node.
796 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
797 N->NodeType = ISD::DELETED_NODE;
798
799 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
800 // them and forget about that node.
801 DbgInfo->erase(N);
802}
803
804#ifndef NDEBUG
805/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
806static void VerifySDNode(SDNode *N) {
807 switch (N->getOpcode()) {
808 default:
809 break;
810 case ISD::BUILD_PAIR: {
811 EVT VT = N->getValueType(0);
812 assert(N->getNumValues() == 1 && "Too many results!")((N->getNumValues() == 1 && "Too many results!") ?
static_cast<void> (0) : __assert_fail ("N->getNumValues() == 1 && \"Too many results!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 812, __PRETTY_FUNCTION__))
;
813 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&((!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint
()) && "Wrong return type!") ? static_cast<void>
(0) : __assert_fail ("!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) && \"Wrong return type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 814, __PRETTY_FUNCTION__))
814 "Wrong return type!")((!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint
()) && "Wrong return type!") ? static_cast<void>
(0) : __assert_fail ("!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) && \"Wrong return type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 814, __PRETTY_FUNCTION__))
;
815 assert(N->getNumOperands() == 2 && "Wrong number of operands!")((N->getNumOperands() == 2 && "Wrong number of operands!"
) ? static_cast<void> (0) : __assert_fail ("N->getNumOperands() == 2 && \"Wrong number of operands!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 815, __PRETTY_FUNCTION__))
;
816 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&((N->getOperand(0).getValueType() == N->getOperand(1).getValueType
() && "Mismatched operand types!") ? static_cast<void
> (0) : __assert_fail ("N->getOperand(0).getValueType() == N->getOperand(1).getValueType() && \"Mismatched operand types!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 817, __PRETTY_FUNCTION__))
817 "Mismatched operand types!")((N->getOperand(0).getValueType() == N->getOperand(1).getValueType
() && "Mismatched operand types!") ? static_cast<void
> (0) : __assert_fail ("N->getOperand(0).getValueType() == N->getOperand(1).getValueType() && \"Mismatched operand types!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 817, __PRETTY_FUNCTION__))
;
818 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&((N->getOperand(0).getValueType().isInteger() == VT.isInteger
() && "Wrong operand type!") ? static_cast<void>
(0) : __assert_fail ("N->getOperand(0).getValueType().isInteger() == VT.isInteger() && \"Wrong operand type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 819, __PRETTY_FUNCTION__))
819 "Wrong operand type!")((N->getOperand(0).getValueType().isInteger() == VT.isInteger
() && "Wrong operand type!") ? static_cast<void>
(0) : __assert_fail ("N->getOperand(0).getValueType().isInteger() == VT.isInteger() && \"Wrong operand type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 819, __PRETTY_FUNCTION__))
;
820 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&((VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits
() && "Wrong return type size") ? static_cast<void
> (0) : __assert_fail ("VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() && \"Wrong return type size\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 821, __PRETTY_FUNCTION__))
821 "Wrong return type size")((VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits
() && "Wrong return type size") ? static_cast<void
> (0) : __assert_fail ("VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() && \"Wrong return type size\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 821, __PRETTY_FUNCTION__))
;
822 break;
823 }
824 case ISD::BUILD_VECTOR: {
825 assert(N->getNumValues() == 1 && "Too many results!")((N->getNumValues() == 1 && "Too many results!") ?
static_cast<void> (0) : __assert_fail ("N->getNumValues() == 1 && \"Too many results!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 825, __PRETTY_FUNCTION__))
;
826 assert(N->getValueType(0).isVector() && "Wrong return type!")((N->getValueType(0).isVector() && "Wrong return type!"
) ? static_cast<void> (0) : __assert_fail ("N->getValueType(0).isVector() && \"Wrong return type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 826, __PRETTY_FUNCTION__))
;
827 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&((N->getNumOperands() == N->getValueType(0).getVectorNumElements
() && "Wrong number of operands!") ? static_cast<void
> (0) : __assert_fail ("N->getNumOperands() == N->getValueType(0).getVectorNumElements() && \"Wrong number of operands!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 828, __PRETTY_FUNCTION__))
828 "Wrong number of operands!")((N->getNumOperands() == N->getValueType(0).getVectorNumElements
() && "Wrong number of operands!") ? static_cast<void
> (0) : __assert_fail ("N->getNumOperands() == N->getValueType(0).getVectorNumElements() && \"Wrong number of operands!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 828, __PRETTY_FUNCTION__))
;
829 EVT EltVT = N->getValueType(0).getVectorElementType();
830 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
831 assert((I->getValueType() == EltVT ||(((I->getValueType() == EltVT || (EltVT.isInteger() &&
I->getValueType().isInteger() && EltVT.bitsLE(I->
getValueType()))) && "Wrong operand type!") ? static_cast
<void> (0) : __assert_fail ("(I->getValueType() == EltVT || (EltVT.isInteger() && I->getValueType().isInteger() && EltVT.bitsLE(I->getValueType()))) && \"Wrong operand type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 834, __PRETTY_FUNCTION__))
832 (EltVT.isInteger() && I->getValueType().isInteger() &&(((I->getValueType() == EltVT || (EltVT.isInteger() &&
I->getValueType().isInteger() && EltVT.bitsLE(I->
getValueType()))) && "Wrong operand type!") ? static_cast
<void> (0) : __assert_fail ("(I->getValueType() == EltVT || (EltVT.isInteger() && I->getValueType().isInteger() && EltVT.bitsLE(I->getValueType()))) && \"Wrong operand type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 834, __PRETTY_FUNCTION__))
833 EltVT.bitsLE(I->getValueType()))) &&(((I->getValueType() == EltVT || (EltVT.isInteger() &&
I->getValueType().isInteger() && EltVT.bitsLE(I->
getValueType()))) && "Wrong operand type!") ? static_cast
<void> (0) : __assert_fail ("(I->getValueType() == EltVT || (EltVT.isInteger() && I->getValueType().isInteger() && EltVT.bitsLE(I->getValueType()))) && \"Wrong operand type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 834, __PRETTY_FUNCTION__))
834 "Wrong operand type!")(((I->getValueType() == EltVT || (EltVT.isInteger() &&
I->getValueType().isInteger() && EltVT.bitsLE(I->
getValueType()))) && "Wrong operand type!") ? static_cast
<void> (0) : __assert_fail ("(I->getValueType() == EltVT || (EltVT.isInteger() && I->getValueType().isInteger() && EltVT.bitsLE(I->getValueType()))) && \"Wrong operand type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 834, __PRETTY_FUNCTION__))
;
835 assert(I->getValueType() == N->getOperand(0).getValueType() &&((I->getValueType() == N->getOperand(0).getValueType() &&
"Operands must all have the same type") ? static_cast<void
> (0) : __assert_fail ("I->getValueType() == N->getOperand(0).getValueType() && \"Operands must all have the same type\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 836, __PRETTY_FUNCTION__))
836 "Operands must all have the same type")((I->getValueType() == N->getOperand(0).getValueType() &&
"Operands must all have the same type") ? static_cast<void
> (0) : __assert_fail ("I->getValueType() == N->getOperand(0).getValueType() && \"Operands must all have the same type\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 836, __PRETTY_FUNCTION__))
;
837 }
838 break;
839 }
840 }
841}
842#endif // NDEBUG
843
844/// Insert a newly allocated node into the DAG.
845///
846/// Handles insertion into the all nodes list and CSE map, as well as
847/// verification and other common operations when a new node is allocated.
848void SelectionDAG::InsertNode(SDNode *N) {
849 AllNodes.push_back(N);
850#ifndef NDEBUG
851 N->PersistentId = NextPersistentId++;
852 VerifySDNode(N);
853#endif
854 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
855 DUL->NodeInserted(N);
856}
857
858/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
859/// correspond to it. This is useful when we're about to delete or repurpose
860/// the node. We don't want future request for structurally identical nodes
861/// to return N anymore.
862bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
863 bool Erased = false;
864 switch (N->getOpcode()) {
865 case ISD::HANDLENODE: return false; // noop.
866 case ISD::CONDCODE:
867 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&((CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
"Cond code doesn't exist!") ? static_cast<void> (0) : __assert_fail
("CondCodeNodes[cast<CondCodeSDNode>(N)->get()] && \"Cond code doesn't exist!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 868, __PRETTY_FUNCTION__))
868 "Cond code doesn't exist!")((CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
"Cond code doesn't exist!") ? static_cast<void> (0) : __assert_fail
("CondCodeNodes[cast<CondCodeSDNode>(N)->get()] && \"Cond code doesn't exist!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 868, __PRETTY_FUNCTION__))
;
869 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
870 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
871 break;
872 case ISD::ExternalSymbol:
873 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
874 break;
875 case ISD::TargetExternalSymbol: {
876 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
877 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
878 ESN->getSymbol(), ESN->getTargetFlags()));
879 break;
880 }
881 case ISD::MCSymbol: {
882 auto *MCSN = cast<MCSymbolSDNode>(N);
883 Erased = MCSymbols.erase(MCSN->getMCSymbol());
884 break;
885 }
886 case ISD::VALUETYPE: {
887 EVT VT = cast<VTSDNode>(N)->getVT();
888 if (VT.isExtended()) {
889 Erased = ExtendedValueTypeNodes.erase(VT);
890 } else {
891 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
892 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
893 }
894 break;
895 }
896 default:
897 // Remove it from the CSE Map.
898 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!")((N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!"
) ? static_cast<void> (0) : __assert_fail ("N->getOpcode() != ISD::DELETED_NODE && \"DELETED_NODE in CSEMap!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 898, __PRETTY_FUNCTION__))
;
899 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!")((N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!"
) ? static_cast<void> (0) : __assert_fail ("N->getOpcode() != ISD::EntryToken && \"EntryToken in CSEMap!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 899, __PRETTY_FUNCTION__))
;
900 Erased = CSEMap.RemoveNode(N);
901 break;
902 }
903#ifndef NDEBUG
904 // Verify that the node was actually in one of the CSE maps, unless it has a
905 // flag result (which cannot be CSE'd) or is one of the special cases that are
906 // not subject to CSE.
907 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
908 !N->isMachineOpcode() && !doNotCSE(N)) {
909 N->dump(this);
910 dbgs() << "\n";
911 llvm_unreachable("Node is not in map!")::llvm::llvm_unreachable_internal("Node is not in map!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 911)
;
912 }
913#endif
914 return Erased;
915}
916
917/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
918/// maps and modified in place. Add it back to the CSE maps, unless an identical
919/// node already exists, in which case transfer all its users to the existing
920/// node. This transfer can potentially trigger recursive merging.
921void
922SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
923 // For node types that aren't CSE'd, just act as if no identical node
924 // already exists.
925 if (!doNotCSE(N)) {
926 SDNode *Existing = CSEMap.GetOrInsertNode(N);
927 if (Existing != N) {
928 // If there was already an existing matching node, use ReplaceAllUsesWith
929 // to replace the dead one with the existing one. This can cause
930 // recursive merging of other unrelated nodes down the line.
931 ReplaceAllUsesWith(N, Existing);
932
933 // N is now dead. Inform the listeners and delete it.
934 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
935 DUL->NodeDeleted(N, Existing);
936 DeleteNodeNotInCSEMaps(N);
937 return;
938 }
939 }
940
941 // If the node doesn't already exist, we updated it. Inform listeners.
942 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
943 DUL->NodeUpdated(N);
944}
945
946/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
947/// were replaced with those specified. If this node is never memoized,
948/// return null, otherwise return a pointer to the slot it would take. If a
949/// node already exists with these operands, the slot will be non-null.
950SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
951 void *&InsertPos) {
952 if (doNotCSE(N))
953 return nullptr;
954
955 SDValue Ops[] = { Op };
956 FoldingSetNodeID ID;
957 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
958 AddNodeIDCustom(ID, N);
959 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
960 if (Node)
961 Node->intersectFlagsWith(N->getFlags());
962 return Node;
963}
964
965/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
966/// were replaced with those specified. If this node is never memoized,
967/// return null, otherwise return a pointer to the slot it would take. If a
968/// node already exists with these operands, the slot will be non-null.
969SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
970 SDValue Op1, SDValue Op2,
971 void *&InsertPos) {
972 if (doNotCSE(N))
973 return nullptr;
974
975 SDValue Ops[] = { Op1, Op2 };
976 FoldingSetNodeID ID;
977 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
978 AddNodeIDCustom(ID, N);
979 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
980 if (Node)
981 Node->intersectFlagsWith(N->getFlags());
982 return Node;
983}
984
985/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
986/// were replaced with those specified. If this node is never memoized,
987/// return null, otherwise return a pointer to the slot it would take. If a
988/// node already exists with these operands, the slot will be non-null.
989SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
990 void *&InsertPos) {
991 if (doNotCSE(N))
992 return nullptr;
993
994 FoldingSetNodeID ID;
995 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
996 AddNodeIDCustom(ID, N);
997 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
998 if (Node)
999 Node->intersectFlagsWith(N->getFlags());
1000 return Node;
1001}
1002
1003unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
1004 Type *Ty = VT == MVT::iPTR ?
1005 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
1006 VT.getTypeForEVT(*getContext());
1007
1008 return getDataLayout().getABITypeAlignment(Ty);
1009}
1010
1011// EntryNode could meaningfully have debug info if we can find it...
1012SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
1013 : TM(tm), OptLevel(OL),
1014 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
1015 Root(getEntryNode()) {
1016 InsertNode(&EntryNode);
1017 DbgInfo = new SDDbgInfo();
1018}
1019
1020void SelectionDAG::init(MachineFunction &NewMF,
1021 OptimizationRemarkEmitter &NewORE,
1022 Pass *PassPtr, const TargetLibraryInfo *LibraryInfo,
1023 LegacyDivergenceAnalysis * Divergence,
1024 ProfileSummaryInfo *PSIin,
1025 BlockFrequencyInfo *BFIin) {
1026 MF = &NewMF;
1027 SDAGISelPass = PassPtr;
1028 ORE = &NewORE;
1029 TLI = getSubtarget().getTargetLowering();
1030 TSI = getSubtarget().getSelectionDAGInfo();
1031 LibInfo = LibraryInfo;
1032 Context = &MF->getFunction().getContext();
1033 DA = Divergence;
1034 PSI = PSIin;
1035 BFI = BFIin;
1036}
1037
1038SelectionDAG::~SelectionDAG() {
1039 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners")((!UpdateListeners && "Dangling registered DAGUpdateListeners"
) ? static_cast<void> (0) : __assert_fail ("!UpdateListeners && \"Dangling registered DAGUpdateListeners\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1039, __PRETTY_FUNCTION__))
;
1040 allnodes_clear();
1041 OperandRecycler.clear(OperandAllocator);
1042 delete DbgInfo;
1043}
1044
1045bool SelectionDAG::shouldOptForSize() const {
1046 return MF->getFunction().hasOptSize() ||
1047 llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1048}
1049
1050void SelectionDAG::allnodes_clear() {
1051 assert(&*AllNodes.begin() == &EntryNode)((&*AllNodes.begin() == &EntryNode) ? static_cast<
void> (0) : __assert_fail ("&*AllNodes.begin() == &EntryNode"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1051, __PRETTY_FUNCTION__))
;
1052 AllNodes.remove(AllNodes.begin());
1053 while (!AllNodes.empty())
1054 DeallocateNode(&AllNodes.front());
1055#ifndef NDEBUG
1056 NextPersistentId = 0;
1057#endif
1058}
1059
1060SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1061 void *&InsertPos) {
1062 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1063 if (N) {
1064 switch (N->getOpcode()) {
1065 default: break;
1066 case ISD::Constant:
1067 case ISD::ConstantFP:
1068 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "::llvm::llvm_unreachable_internal("Querying for Constant and ConstantFP nodes requires "
"debug location. Use another overload.", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1069)
1069 "debug location. Use another overload.")::llvm::llvm_unreachable_internal("Querying for Constant and ConstantFP nodes requires "
"debug location. Use another overload.", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1069)
;
1070 }
1071 }
1072 return N;
1073}
1074
1075SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1076 const SDLoc &DL, void *&InsertPos) {
1077 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1078 if (N) {
1079 switch (N->getOpcode()) {
1080 case ISD::Constant:
1081 case ISD::ConstantFP:
1082 // Erase debug location from the node if the node is used at several
1083 // different places. Do not propagate one location to all uses as it
1084 // will cause a worse single stepping debugging experience.
1085 if (N->getDebugLoc() != DL.getDebugLoc())
1086 N->setDebugLoc(DebugLoc());
1087 break;
1088 default:
1089 // When the node's point of use is located earlier in the instruction
1090 // sequence than its prior point of use, update its debug info to the
1091 // earlier location.
1092 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1093 N->setDebugLoc(DL.getDebugLoc());
1094 break;
1095 }
1096 }
1097 return N;
1098}
1099
1100void SelectionDAG::clear() {
1101 allnodes_clear();
1102 OperandRecycler.clear(OperandAllocator);
1103 OperandAllocator.Reset();
1104 CSEMap.clear();
1105
1106 ExtendedValueTypeNodes.clear();
1107 ExternalSymbols.clear();
1108 TargetExternalSymbols.clear();
1109 MCSymbols.clear();
1110 SDCallSiteDbgInfo.clear();
1111 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
1112 static_cast<CondCodeSDNode*>(nullptr));
1113 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
1114 static_cast<SDNode*>(nullptr));
1115
1116 EntryNode.UseList = nullptr;
1117 InsertNode(&EntryNode);
1118 Root = getEntryNode();
1119 DbgInfo->clear();
1120}
1121
1122SDValue SelectionDAG::getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT) {
1123 return VT.bitsGT(Op.getValueType())
1124 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1125 : getNode(ISD::FP_ROUND, DL, VT, Op, getIntPtrConstant(0, DL));
1126}
1127
1128std::pair<SDValue, SDValue>
1129SelectionDAG::getStrictFPExtendOrRound(SDValue Op, SDValue Chain,
1130 const SDLoc &DL, EVT VT) {
1131 assert(!VT.bitsEq(Op.getValueType()) &&((!VT.bitsEq(Op.getValueType()) && "Strict no-op FP extend/round not allowed."
) ? static_cast<void> (0) : __assert_fail ("!VT.bitsEq(Op.getValueType()) && \"Strict no-op FP extend/round not allowed.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1132, __PRETTY_FUNCTION__))
1132 "Strict no-op FP extend/round not allowed.")((!VT.bitsEq(Op.getValueType()) && "Strict no-op FP extend/round not allowed."
) ? static_cast<void> (0) : __assert_fail ("!VT.bitsEq(Op.getValueType()) && \"Strict no-op FP extend/round not allowed.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1132, __PRETTY_FUNCTION__))
;
1133 SDValue Res =
1134 VT.bitsGT(Op.getValueType())
1135 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1136 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1137 {Chain, Op, getIntPtrConstant(0, DL)});
1138
1139 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1140}
1141
1142SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
1143 return VT.bitsGT(Op.getValueType()) ?
1144 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1145 getNode(ISD::TRUNCATE, DL, VT, Op);
1146}
1147
1148SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
1149 return VT.bitsGT(Op.getValueType()) ?
1150 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1151 getNode(ISD::TRUNCATE, DL, VT, Op);
1152}
1153
1154SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
1155 return VT.bitsGT(Op.getValueType()) ?
1156 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1157 getNode(ISD::TRUNCATE, DL, VT, Op);
1158}
1159
1160SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT,
1161 EVT OpVT) {
1162 if (VT.bitsLE(Op.getValueType()))
1163 return getNode(ISD::TRUNCATE, SL, VT, Op);
1164
1165 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1166 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1167}
1168
1169SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT) {
1170 assert(!VT.isVector() &&((!VT.isVector() && "getZeroExtendInReg should use the vector element type instead of "
"the vector type!") ? static_cast<void> (0) : __assert_fail
("!VT.isVector() && \"getZeroExtendInReg should use the vector element type instead of \" \"the vector type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1172, __PRETTY_FUNCTION__))
1171 "getZeroExtendInReg should use the vector element type instead of "((!VT.isVector() && "getZeroExtendInReg should use the vector element type instead of "
"the vector type!") ? static_cast<void> (0) : __assert_fail
("!VT.isVector() && \"getZeroExtendInReg should use the vector element type instead of \" \"the vector type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1172, __PRETTY_FUNCTION__))
1172 "the vector type!")((!VT.isVector() && "getZeroExtendInReg should use the vector element type instead of "
"the vector type!") ? static_cast<void> (0) : __assert_fail
("!VT.isVector() && \"getZeroExtendInReg should use the vector element type instead of \" \"the vector type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1172, __PRETTY_FUNCTION__))
;
1173 if (Op.getValueType().getScalarType() == VT) return Op;
1174 unsigned BitWidth = Op.getScalarValueSizeInBits();
1175 APInt Imm = APInt::getLowBitsSet(BitWidth,
1176 VT.getSizeInBits());
1177 return getNode(ISD::AND, DL, Op.getValueType(), Op,
1178 getConstant(Imm, DL, Op.getValueType()));
1179}
1180
1181SDValue SelectionDAG::getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
1182 // Only unsigned pointer semantics are supported right now. In the future this
1183 // might delegate to TLI to check pointer signedness.
1184 return getZExtOrTrunc(Op, DL, VT);
1185}
1186
1187SDValue SelectionDAG::getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT) {
1188 // Only unsigned pointer semantics are supported right now. In the future this
1189 // might delegate to TLI to check pointer signedness.
1190 return getZeroExtendInReg(Op, DL, VT);
1191}
1192
1193/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1194SDValue SelectionDAG::getNOT(const SDLoc &DL, SDValue Val, EVT VT) {
1195 EVT EltVT = VT.getScalarType();
1196 SDValue NegOne =
1197 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL, VT);
1198 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1199}
1200
1201SDValue SelectionDAG::getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT) {
1202 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1203 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1204}
1205
1206SDValue SelectionDAG::getBoolConstant(bool V, const SDLoc &DL, EVT VT,
1207 EVT OpVT) {
1208 if (!V)
1209 return getConstant(0, DL, VT);
1210
1211 switch (TLI->getBooleanContents(OpVT)) {
1212 case TargetLowering::ZeroOrOneBooleanContent:
1213 case TargetLowering::UndefinedBooleanContent:
1214 return getConstant(1, DL, VT);
1215 case TargetLowering::ZeroOrNegativeOneBooleanContent:
1216 return getAllOnesConstant(DL, VT);
1217 }
1218 llvm_unreachable("Unexpected boolean content enum!")::llvm::llvm_unreachable_internal("Unexpected boolean content enum!"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1218)
;
1219}
1220
1221SDValue SelectionDAG::getConstant(uint64_t Val, const SDLoc &DL, EVT VT,
1222 bool isT, bool isO) {
1223 EVT EltVT = VT.getScalarType();
1224 assert((EltVT.getSizeInBits() >= 64 ||(((EltVT.getSizeInBits() >= 64 || (uint64_t)((int64_t)Val >>
EltVT.getSizeInBits()) + 1 < 2) && "getConstant with a uint64_t value that doesn't fit in the type!"
) ? static_cast<void> (0) : __assert_fail ("(EltVT.getSizeInBits() >= 64 || (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) && \"getConstant with a uint64_t value that doesn't fit in the type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1226, __PRETTY_FUNCTION__))
1225 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&(((EltVT.getSizeInBits() >= 64 || (uint64_t)((int64_t)Val >>
EltVT.getSizeInBits()) + 1 < 2) && "getConstant with a uint64_t value that doesn't fit in the type!"
) ? static_cast<void> (0) : __assert_fail ("(EltVT.getSizeInBits() >= 64 || (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) && \"getConstant with a uint64_t value that doesn't fit in the type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1226, __PRETTY_FUNCTION__))
1226 "getConstant with a uint64_t value that doesn't fit in the type!")(((EltVT.getSizeInBits() >= 64 || (uint64_t)((int64_t)Val >>
EltVT.getSizeInBits()) + 1 < 2) && "getConstant with a uint64_t value that doesn't fit in the type!"
) ? static_cast<void> (0) : __assert_fail ("(EltVT.getSizeInBits() >= 64 || (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) && \"getConstant with a uint64_t value that doesn't fit in the type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1226, __PRETTY_FUNCTION__))
;
1227 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
1228}
1229
1230SDValue SelectionDAG::getConstant(const APInt &Val, const SDLoc &DL, EVT VT,
1231 bool isT, bool isO) {
1232 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1233}
1234
1235SDValue SelectionDAG::getConstant(const ConstantInt &Val, const SDLoc &DL,
1236 EVT VT, bool isT, bool isO) {
1237 assert(VT.isInteger() && "Cannot create FP integer constant!")((VT.isInteger() && "Cannot create FP integer constant!"
) ? static_cast<void> (0) : __assert_fail ("VT.isInteger() && \"Cannot create FP integer constant!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1237, __PRETTY_FUNCTION__))
;
1238
1239 EVT EltVT = VT.getScalarType();
1240 const ConstantInt *Elt = &Val;
1241
1242 // In some cases the vector type is legal but the element type is illegal and
1243 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1244 // inserted value (the type does not need to match the vector element type).
1245 // Any extra bits introduced will be truncated away.
1246 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1247 TargetLowering::TypePromoteInteger) {
1248 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1249 APInt NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1250 Elt = ConstantInt::get(*getContext(), NewVal);
1251 }
1252 // In other cases the element type is illegal and needs to be expanded, for
1253 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1254 // the value into n parts and use a vector type with n-times the elements.
1255 // Then bitcast to the type requested.
1256 // Legalizing constants too early makes the DAGCombiner's job harder so we
1257 // only legalize if the DAG tells us we must produce legal types.
1258 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1259 TLI->getTypeAction(*getContext(), EltVT) ==
1260 TargetLowering::TypeExpandInteger) {
1261 const APInt &NewVal = Elt->getValue();
1262 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1263 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1264 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1265 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1266
1267 // Check the temporary vector is the correct size. If this fails then
1268 // getTypeToTransformTo() probably returned a type whose size (in bits)
1269 // isn't a power-of-2 factor of the requested type size.
1270 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits())((ViaVecVT.getSizeInBits() == VT.getSizeInBits()) ? static_cast
<void> (0) : __assert_fail ("ViaVecVT.getSizeInBits() == VT.getSizeInBits()"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1270, __PRETTY_FUNCTION__))
;
1271
1272 SmallVector<SDValue, 2> EltParts;
1273 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1274 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1275 .zextOrTrunc(ViaEltSizeInBits), DL,
1276 ViaEltVT, isT, isO));
1277 }
1278
1279 // EltParts is currently in little endian order. If we actually want
1280 // big-endian order then reverse it now.
1281 if (getDataLayout().isBigEndian())
1282 std::reverse(EltParts.begin(), EltParts.end());
1283
1284 // The elements must be reversed when the element order is different
1285 // to the endianness of the elements (because the BITCAST is itself a
1286 // vector shuffle in this situation). However, we do not need any code to
1287 // perform this reversal because getConstant() is producing a vector
1288 // splat.
1289 // This situation occurs in MIPS MSA.
1290
1291 SmallVector<SDValue, 8> Ops;
1292 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1293 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1294
1295 SDValue V = getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1296 return V;
1297 }
1298
1299 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&((Elt->getBitWidth() == EltVT.getSizeInBits() && "APInt size does not match type size!"
) ? static_cast<void> (0) : __assert_fail ("Elt->getBitWidth() == EltVT.getSizeInBits() && \"APInt size does not match type size!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1300, __PRETTY_FUNCTION__))
1300 "APInt size does not match type size!")((Elt->getBitWidth() == EltVT.getSizeInBits() && "APInt size does not match type size!"
) ? static_cast<void> (0) : __assert_fail ("Elt->getBitWidth() == EltVT.getSizeInBits() && \"APInt size does not match type size!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1300, __PRETTY_FUNCTION__))
;
1301 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1302 FoldingSetNodeID ID;
1303 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
1304 ID.AddPointer(Elt);
1305 ID.AddBoolean(isO);
1306 void *IP = nullptr;
1307 SDNode *N = nullptr;
1308 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1309 if (!VT.isVector())
1310 return SDValue(N, 0);
1311
1312 if (!N) {
1313 N = newSDNode<ConstantSDNode>(isT, isO, Elt, EltVT);
1314 CSEMap.InsertNode(N, IP);
1315 InsertNode(N);
1316 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1317 }
1318
1319 SDValue Result(N, 0);
1320 if (VT.isScalableVector())
1321 Result = getSplatVector(VT, DL, Result);
1322 else if (VT.isVector())
1323 Result = getSplatBuildVector(VT, DL, Result);
1324
1325 return Result;
1326}
1327
1328SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, const SDLoc &DL,
1329 bool isTarget) {
1330 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1331}
1332
1333SDValue SelectionDAG::getShiftAmountConstant(uint64_t Val, EVT VT,
1334 const SDLoc &DL, bool LegalTypes) {
1335 assert(VT.isInteger() && "Shift amount is not an integer type!")((VT.isInteger() && "Shift amount is not an integer type!"
) ? static_cast<void> (0) : __assert_fail ("VT.isInteger() && \"Shift amount is not an integer type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1335, __PRETTY_FUNCTION__))
;
1336 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout(), LegalTypes);
1337 return getConstant(Val, DL, ShiftVT);
1338}
1339
1340SDValue SelectionDAG::getVectorIdxConstant(uint64_t Val, const SDLoc &DL,
1341 bool isTarget) {
1342 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1343}
1344
1345SDValue SelectionDAG::getConstantFP(const APFloat &V, const SDLoc &DL, EVT VT,
1346 bool isTarget) {
1347 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1348}
1349
1350SDValue SelectionDAG::getConstantFP(const ConstantFP &V, const SDLoc &DL,
1351 EVT VT, bool isTarget) {
1352 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!")((VT.isFloatingPoint() && "Cannot create integer FP constant!"
) ? static_cast<void> (0) : __assert_fail ("VT.isFloatingPoint() && \"Cannot create integer FP constant!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1352, __PRETTY_FUNCTION__))
;
1353
1354 EVT EltVT = VT.getScalarType();
1355
1356 // Do the map lookup using the actual bit pattern for the floating point
1357 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1358 // we don't have issues with SNANs.
1359 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1360 FoldingSetNodeID ID;
1361 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
1362 ID.AddPointer(&V);
1363 void *IP = nullptr;
1364 SDNode *N = nullptr;
1365 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1366 if (!VT.isVector())
1367 return SDValue(N, 0);
1368
1369 if (!N) {
1370 N = newSDNode<ConstantFPSDNode>(isTarget, &V, EltVT);
1371 CSEMap.InsertNode(N, IP);
1372 InsertNode(N);
1373 }
1374
1375 SDValue Result(N, 0);
1376 if (VT.isVector())
1377 Result = getSplatBuildVector(VT, DL, Result);
1378 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1379 return Result;
1380}
1381
1382SDValue SelectionDAG::getConstantFP(double Val, const SDLoc &DL, EVT VT,
1383 bool isTarget) {
1384 EVT EltVT = VT.getScalarType();
1385 if (EltVT == MVT::f32)
1386 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1387 else if (EltVT == MVT::f64)
1388 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1389 else if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1390 EltVT == MVT::f16) {
1391 bool Ignored;
1392 APFloat APF = APFloat(Val);
1393 APF.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
1394 &Ignored);
1395 return getConstantFP(APF, DL, VT, isTarget);
1396 } else
1397 llvm_unreachable("Unsupported type in getConstantFP")::llvm::llvm_unreachable_internal("Unsupported type in getConstantFP"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1397)
;
1398}
1399
1400SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, const SDLoc &DL,
1401 EVT VT, int64_t Offset, bool isTargetGA,
1402 unsigned TargetFlags) {
1403 assert((TargetFlags == 0 || isTargetGA) &&(((TargetFlags == 0 || isTargetGA) && "Cannot set target flags on target-independent globals"
) ? static_cast<void> (0) : __assert_fail ("(TargetFlags == 0 || isTargetGA) && \"Cannot set target flags on target-independent globals\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1404, __PRETTY_FUNCTION__))
1404 "Cannot set target flags on target-independent globals")(((TargetFlags == 0 || isTargetGA) && "Cannot set target flags on target-independent globals"
) ? static_cast<void> (0) : __assert_fail ("(TargetFlags == 0 || isTargetGA) && \"Cannot set target flags on target-independent globals\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1404, __PRETTY_FUNCTION__))
;
1405
1406 // Truncate (with sign-extension) the offset value to the pointer size.
1407 unsigned BitWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
1408 if (BitWidth < 64)
1409 Offset = SignExtend64(Offset, BitWidth);
1410
1411 unsigned Opc;
1412 if (GV->isThreadLocal())
1413 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1414 else
1415 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1416
1417 FoldingSetNodeID ID;
1418 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1419 ID.AddPointer(GV);
1420 ID.AddInteger(Offset);
1421 ID.AddInteger(TargetFlags);
1422 void *IP = nullptr;
1423 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1424 return SDValue(E, 0);
1425
1426 auto *N = newSDNode<GlobalAddressSDNode>(
1427 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VT, Offset, TargetFlags);
1428 CSEMap.InsertNode(N, IP);
1429 InsertNode(N);
1430 return SDValue(N, 0);
1431}
1432
1433SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1434 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1435 FoldingSetNodeID ID;
1436 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1437 ID.AddInteger(FI);
1438 void *IP = nullptr;
1439 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1440 return SDValue(E, 0);
1441
1442 auto *N = newSDNode<FrameIndexSDNode>(FI, VT, isTarget);
1443 CSEMap.InsertNode(N, IP);
1444 InsertNode(N);
1445 return SDValue(N, 0);
1446}
1447
1448SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1449 unsigned TargetFlags) {
1450 assert((TargetFlags == 0 || isTarget) &&(((TargetFlags == 0 || isTarget) && "Cannot set target flags on target-independent jump tables"
) ? static_cast<void> (0) : __assert_fail ("(TargetFlags == 0 || isTarget) && \"Cannot set target flags on target-independent jump tables\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1451, __PRETTY_FUNCTION__))
1451 "Cannot set target flags on target-independent jump tables")(((TargetFlags == 0 || isTarget) && "Cannot set target flags on target-independent jump tables"
) ? static_cast<void> (0) : __assert_fail ("(TargetFlags == 0 || isTarget) && \"Cannot set target flags on target-independent jump tables\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1451, __PRETTY_FUNCTION__))
;
1452 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1453 FoldingSetNodeID ID;
1454 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1455 ID.AddInteger(JTI);
1456 ID.AddInteger(TargetFlags);
1457 void *IP = nullptr;
1458 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1459 return SDValue(E, 0);
1460
1461 auto *N = newSDNode<JumpTableSDNode>(JTI, VT, isTarget, TargetFlags);
1462 CSEMap.InsertNode(N, IP);
1463 InsertNode(N);
1464 return SDValue(N, 0);
1465}
1466
1467SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
1468 unsigned Alignment, int Offset,
1469 bool isTarget,
1470 unsigned TargetFlags) {
1471 assert((TargetFlags == 0 || isTarget) &&(((TargetFlags == 0 || isTarget) && "Cannot set target flags on target-independent globals"
) ? static_cast<void> (0) : __assert_fail ("(TargetFlags == 0 || isTarget) && \"Cannot set target flags on target-independent globals\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1472, __PRETTY_FUNCTION__))
1472 "Cannot set target flags on target-independent globals")(((TargetFlags == 0 || isTarget) && "Cannot set target flags on target-independent globals"
) ? static_cast<void> (0) : __assert_fail ("(TargetFlags == 0 || isTarget) && \"Cannot set target flags on target-independent globals\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1472, __PRETTY_FUNCTION__))
;
1473 if (Alignment == 0)
1474 Alignment = shouldOptForSize()
1475 ? getDataLayout().getABITypeAlignment(C->getType())
1476 : getDataLayout().getPrefTypeAlignment(C->getType());
1477 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1478 FoldingSetNodeID ID;
1479 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1480 ID.AddInteger(Alignment);
1481 ID.AddInteger(Offset);
1482 ID.AddPointer(C);
1483 ID.AddInteger(TargetFlags);
1484 void *IP = nullptr;
1485 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1486 return SDValue(E, 0);
1487
1488 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VT, Offset, Alignment,
1489 TargetFlags);
1490 CSEMap.InsertNode(N, IP);
1491 InsertNode(N);
1492 return SDValue(N, 0);
1493}
1494
1495SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
1496 unsigned Alignment, int Offset,
1497 bool isTarget,
1498 unsigned TargetFlags) {
1499 assert((TargetFlags == 0 || isTarget) &&(((TargetFlags == 0 || isTarget) && "Cannot set target flags on target-independent globals"
) ? static_cast<void> (0) : __assert_fail ("(TargetFlags == 0 || isTarget) && \"Cannot set target flags on target-independent globals\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1500, __PRETTY_FUNCTION__))
1500 "Cannot set target flags on target-independent globals")(((TargetFlags == 0 || isTarget) && "Cannot set target flags on target-independent globals"
) ? static_cast<void> (0) : __assert_fail ("(TargetFlags == 0 || isTarget) && \"Cannot set target flags on target-independent globals\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1500, __PRETTY_FUNCTION__))
;
1501 if (Alignment == 0)
1502 Alignment = getDataLayout().getPrefTypeAlignment(C->getType());
1503 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1504 FoldingSetNodeID ID;
1505 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1506 ID.AddInteger(Alignment);
1507 ID.AddInteger(Offset);
1508 C->addSelectionDAGCSEId(ID);
1509 ID.AddInteger(TargetFlags);
1510 void *IP = nullptr;
1511 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1512 return SDValue(E, 0);
1513
1514 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VT, Offset, Alignment,
1515 TargetFlags);
1516 CSEMap.InsertNode(N, IP);
1517 InsertNode(N);
1518 return SDValue(N, 0);
1519}
1520
1521SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1522 unsigned TargetFlags) {
1523 FoldingSetNodeID ID;
1524 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
1525 ID.AddInteger(Index);
1526 ID.AddInteger(Offset);
1527 ID.AddInteger(TargetFlags);
1528 void *IP = nullptr;
1529 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1530 return SDValue(E, 0);
1531
1532 auto *N = newSDNode<TargetIndexSDNode>(Index, VT, Offset, TargetFlags);
1533 CSEMap.InsertNode(N, IP);
1534 InsertNode(N);
1535 return SDValue(N, 0);
1536}
1537
1538SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
1539 FoldingSetNodeID ID;
1540 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
1541 ID.AddPointer(MBB);
1542 void *IP = nullptr;
1543 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1544 return SDValue(E, 0);
1545
1546 auto *N = newSDNode<BasicBlockSDNode>(MBB);
1547 CSEMap.InsertNode(N, IP);
1548 InsertNode(N);
1549 return SDValue(N, 0);
1550}
1551
1552SDValue SelectionDAG::getValueType(EVT VT) {
1553 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1554 ValueTypeNodes.size())
1555 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
1556
1557 SDNode *&N = VT.isExtended() ?
1558 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
1559
1560 if (N) return SDValue(N, 0);
1561 N = newSDNode<VTSDNode>(VT);
1562 InsertNode(N);
1563 return SDValue(N, 0);
1564}
1565
1566SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
1567 SDNode *&N = ExternalSymbols[Sym];
1568 if (N) return SDValue(N, 0);
1569 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, VT);
1570 InsertNode(N);
1571 return SDValue(N, 0);
1572}
1573
1574SDValue SelectionDAG::getMCSymbol(MCSymbol *Sym, EVT VT) {
1575 SDNode *&N = MCSymbols[Sym];
1576 if (N)
1577 return SDValue(N, 0);
1578 N = newSDNode<MCSymbolSDNode>(Sym, VT);
1579 InsertNode(N);
1580 return SDValue(N, 0);
1581}
1582
1583SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
1584 unsigned TargetFlags) {
1585 SDNode *&N =
1586 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
1587 if (N) return SDValue(N, 0);
1588 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, VT);
1589 InsertNode(N);
1590 return SDValue(N, 0);
1591}
1592
1593SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
1594 if ((unsigned)Cond >= CondCodeNodes.size())
1595 CondCodeNodes.resize(Cond+1);
1596
1597 if (!CondCodeNodes[Cond]) {
1598 auto *N = newSDNode<CondCodeSDNode>(Cond);
1599 CondCodeNodes[Cond] = N;
1600 InsertNode(N);
1601 }
1602
1603 return SDValue(CondCodeNodes[Cond], 0);
1604}
1605
1606/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
1607/// point at N1 to point at N2 and indices that point at N2 to point at N1.
1608static void commuteShuffle(SDValue &N1, SDValue &N2, MutableArrayRef<int> M) {
1609 std::swap(N1, N2);
1610 ShuffleVectorSDNode::commuteMask(M);
1611}
1612
1613SDValue SelectionDAG::getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1,
1614 SDValue N2, ArrayRef<int> Mask) {
1615 assert(VT.getVectorNumElements() == Mask.size() &&((VT.getVectorNumElements() == Mask.size() && "Must have the same number of vector elements as mask elements!"
) ? static_cast<void> (0) : __assert_fail ("VT.getVectorNumElements() == Mask.size() && \"Must have the same number of vector elements as mask elements!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1616, __PRETTY_FUNCTION__))
1616 "Must have the same number of vector elements as mask elements!")((VT.getVectorNumElements() == Mask.size() && "Must have the same number of vector elements as mask elements!"
) ? static_cast<void> (0) : __assert_fail ("VT.getVectorNumElements() == Mask.size() && \"Must have the same number of vector elements as mask elements!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1616, __PRETTY_FUNCTION__))
;
1617 assert(VT == N1.getValueType() && VT == N2.getValueType() &&((VT == N1.getValueType() && VT == N2.getValueType() &&
"Invalid VECTOR_SHUFFLE") ? static_cast<void> (0) : __assert_fail
("VT == N1.getValueType() && VT == N2.getValueType() && \"Invalid VECTOR_SHUFFLE\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1618, __PRETTY_FUNCTION__))
1618 "Invalid VECTOR_SHUFFLE")((VT == N1.getValueType() && VT == N2.getValueType() &&
"Invalid VECTOR_SHUFFLE") ? static_cast<void> (0) : __assert_fail
("VT == N1.getValueType() && VT == N2.getValueType() && \"Invalid VECTOR_SHUFFLE\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1618, __PRETTY_FUNCTION__))
;
1619
1620 // Canonicalize shuffle undef, undef -> undef
1621 if (N1.isUndef() && N2.isUndef())
1622 return getUNDEF(VT);
1623
1624 // Validate that all indices in Mask are within the range of the elements
1625 // input to the shuffle.
1626 int NElts = Mask.size();
1627 assert(llvm::all_of(Mask,((llvm::all_of(Mask, [&](int M) { return M < (NElts * 2
) && M >= -1; }) && "Index out of range") ?
static_cast<void> (0) : __assert_fail ("llvm::all_of(Mask, [&](int M) { return M < (NElts * 2) && M >= -1; }) && \"Index out of range\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1629, __PRETTY_FUNCTION__))
1628 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&((llvm::all_of(Mask, [&](int M) { return M < (NElts * 2
) && M >= -1; }) && "Index out of range") ?
static_cast<void> (0) : __assert_fail ("llvm::all_of(Mask, [&](int M) { return M < (NElts * 2) && M >= -1; }) && \"Index out of range\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1629, __PRETTY_FUNCTION__))
1629 "Index out of range")((llvm::all_of(Mask, [&](int M) { return M < (NElts * 2
) && M >= -1; }) && "Index out of range") ?
static_cast<void> (0) : __assert_fail ("llvm::all_of(Mask, [&](int M) { return M < (NElts * 2) && M >= -1; }) && \"Index out of range\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1629, __PRETTY_FUNCTION__))
;
1630
1631 // Copy the mask so we can do any needed cleanup.
1632 SmallVector<int, 8> MaskVec(Mask.begin(), Mask.end());
1633
1634 // Canonicalize shuffle v, v -> v, undef
1635 if (N1 == N2) {
1636 N2 = getUNDEF(VT);
1637 for (int i = 0; i != NElts; ++i)
1638 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
1639 }
1640
1641 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1642 if (N1.isUndef())
1643 commuteShuffle(N1, N2, MaskVec);
1644
1645 if (TLI->hasVectorBlend()) {
1646 // If shuffling a splat, try to blend the splat instead. We do this here so
1647 // that even when this arises during lowering we don't have to re-handle it.
1648 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
1649 BitVector UndefElements;
1650 SDValue Splat = BV->getSplatValue(&UndefElements);
1651 if (!Splat)
1652 return;
1653
1654 for (int i = 0; i < NElts; ++i) {
1655 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
1656 continue;
1657
1658 // If this input comes from undef, mark it as such.
1659 if (UndefElements[MaskVec[i] - Offset]) {
1660 MaskVec[i] = -1;
1661 continue;
1662 }
1663
1664 // If we can blend a non-undef lane, use that instead.
1665 if (!UndefElements[i])
1666 MaskVec[i] = i + Offset;
1667 }
1668 };
1669 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
1670 BlendSplat(N1BV, 0);
1671 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
1672 BlendSplat(N2BV, NElts);
1673 }
1674
1675 // Canonicalize all index into lhs, -> shuffle lhs, undef
1676 // Canonicalize all index into rhs, -> shuffle rhs, undef
1677 bool AllLHS = true, AllRHS = true;
1678 bool N2Undef = N2.isUndef();
1679 for (int i = 0; i != NElts; ++i) {
1680 if (MaskVec[i] >= NElts) {
1681 if (N2Undef)
1682 MaskVec[i] = -1;
1683 else
1684 AllLHS = false;
1685 } else if (MaskVec[i] >= 0) {
1686 AllRHS = false;
1687 }
1688 }
1689 if (AllLHS && AllRHS)
1690 return getUNDEF(VT);
1691 if (AllLHS && !N2Undef)
1692 N2 = getUNDEF(VT);
1693 if (AllRHS) {
1694 N1 = getUNDEF(VT);
1695 commuteShuffle(N1, N2, MaskVec);
1696 }
1697 // Reset our undef status after accounting for the mask.
1698 N2Undef = N2.isUndef();
1699 // Re-check whether both sides ended up undef.
1700 if (N1.isUndef() && N2Undef)
1701 return getUNDEF(VT);
1702
1703 // If Identity shuffle return that node.
1704 bool Identity = true, AllSame = true;
1705 for (int i = 0; i != NElts; ++i) {
1706 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
1707 if (MaskVec[i] != MaskVec[0]) AllSame = false;
1708 }
1709 if (Identity && NElts)
1710 return N1;
1711
1712 // Shuffling a constant splat doesn't change the result.
1713 if (N2Undef) {
1714 SDValue V = N1;
1715
1716 // Look through any bitcasts. We check that these don't change the number
1717 // (and size) of elements and just changes their types.
1718 while (V.getOpcode() == ISD::BITCAST)
1719 V = V->getOperand(0);
1720
1721 // A splat should always show up as a build vector node.
1722 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
1723 BitVector UndefElements;
1724 SDValue Splat = BV->getSplatValue(&UndefElements);
1725 // If this is a splat of an undef, shuffling it is also undef.
1726 if (Splat && Splat.isUndef())
1727 return getUNDEF(VT);
1728
1729 bool SameNumElts =
1730 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
1731
1732 // We only have a splat which can skip shuffles if there is a splatted
1733 // value and no undef lanes rearranged by the shuffle.
1734 if (Splat && UndefElements.none()) {
1735 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1736 // number of elements match or the value splatted is a zero constant.
1737 if (SameNumElts)
1738 return N1;
1739 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1740 if (C->isNullValue())
1741 return N1;
1742 }
1743
1744 // If the shuffle itself creates a splat, build the vector directly.
1745 if (AllSame && SameNumElts) {
1746 EVT BuildVT = BV->getValueType(0);
1747 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
1748 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
1749
1750 // We may have jumped through bitcasts, so the type of the
1751 // BUILD_VECTOR may not match the type of the shuffle.
1752 if (BuildVT != VT)
1753 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
1754 return NewBV;
1755 }
1756 }
1757 }
1758
1759 FoldingSetNodeID ID;
1760 SDValue Ops[2] = { N1, N2 };
1761 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
1762 for (int i = 0; i != NElts; ++i)
1763 ID.AddInteger(MaskVec[i]);
1764
1765 void* IP = nullptr;
1766 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
1767 return SDValue(E, 0);
1768
1769 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1770 // SDNode doesn't have access to it. This memory will be "leaked" when
1771 // the node is deallocated, but recovered when the NodeAllocator is released.
1772 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1773 llvm::copy(MaskVec, MaskAlloc);
1774
1775 auto *N = newSDNode<ShuffleVectorSDNode>(VT, dl.getIROrder(),
1776 dl.getDebugLoc(), MaskAlloc);
1777 createOperands(N, Ops);
1778
1779 CSEMap.InsertNode(N, IP);
1780 InsertNode(N);
1781 SDValue V = SDValue(N, 0);
1782 NewSDValueDbgMsg(V, "Creating new node: ", this);
1783 return V;
1784}
1785
1786SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1787 EVT VT = SV.getValueType(0);
1788 SmallVector<int, 8> MaskVec(SV.getMask().begin(), SV.getMask().end());
1789 ShuffleVectorSDNode::commuteMask(MaskVec);
1790
1791 SDValue Op0 = SV.getOperand(0);
1792 SDValue Op1 = SV.getOperand(1);
1793 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
1794}
1795
1796SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
1797 FoldingSetNodeID ID;
1798 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
1799 ID.AddInteger(RegNo);
1800 void *IP = nullptr;
1801 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1802 return SDValue(E, 0);
1803
1804 auto *N = newSDNode<RegisterSDNode>(RegNo, VT);
1805 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, DA);
1806 CSEMap.InsertNode(N, IP);
1807 InsertNode(N);
1808 return SDValue(N, 0);
1809}
1810
1811SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1812 FoldingSetNodeID ID;
1813 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
1814 ID.AddPointer(RegMask);
1815 void *IP = nullptr;
1816 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1817 return SDValue(E, 0);
1818
1819 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
1820 CSEMap.InsertNode(N, IP);
1821 InsertNode(N);
1822 return SDValue(N, 0);
1823}
1824
1825SDValue SelectionDAG::getEHLabel(const SDLoc &dl, SDValue Root,
1826 MCSymbol *Label) {
1827 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
1828}
1829
1830SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
1831 SDValue Root, MCSymbol *Label) {
1832 FoldingSetNodeID ID;
1833 SDValue Ops[] = { Root };
1834 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
1835 ID.AddPointer(Label);
1836 void *IP = nullptr;
1837 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1838 return SDValue(E, 0);
1839
1840 auto *N =
1841 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
1842 createOperands(N, Ops);
1843
1844 CSEMap.InsertNode(N, IP);
1845 InsertNode(N);
1846 return SDValue(N, 0);
1847}
1848
1849SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
1850 int64_t Offset, bool isTarget,
1851 unsigned TargetFlags) {
1852 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1853
1854 FoldingSetNodeID ID;
1855 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1856 ID.AddPointer(BA);
1857 ID.AddInteger(Offset);
1858 ID.AddInteger(TargetFlags);
1859 void *IP = nullptr;
1860 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1861 return SDValue(E, 0);
1862
1863 auto *N = newSDNode<BlockAddressSDNode>(Opc, VT, BA, Offset, TargetFlags);
1864 CSEMap.InsertNode(N, IP);
1865 InsertNode(N);
1866 return SDValue(N, 0);
1867}
1868
1869SDValue SelectionDAG::getSrcValue(const Value *V) {
1870 assert((!V || V->getType()->isPointerTy()) &&(((!V || V->getType()->isPointerTy()) && "SrcValue is not a pointer?"
) ? static_cast<void> (0) : __assert_fail ("(!V || V->getType()->isPointerTy()) && \"SrcValue is not a pointer?\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1871, __PRETTY_FUNCTION__))
1871 "SrcValue is not a pointer?")(((!V || V->getType()->isPointerTy()) && "SrcValue is not a pointer?"
) ? static_cast<void> (0) : __assert_fail ("(!V || V->getType()->isPointerTy()) && \"SrcValue is not a pointer?\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 1871, __PRETTY_FUNCTION__))
;
1872
1873 FoldingSetNodeID ID;
1874 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
1875 ID.AddPointer(V);
1876
1877 void *IP = nullptr;
1878 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1879 return SDValue(E, 0);
1880
1881 auto *N = newSDNode<SrcValueSDNode>(V);
1882 CSEMap.InsertNode(N, IP);
1883 InsertNode(N);
1884 return SDValue(N, 0);
1885}
1886
1887SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1888 FoldingSetNodeID ID;
1889 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
1890 ID.AddPointer(MD);
1891
1892 void *IP = nullptr;
1893 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1894 return SDValue(E, 0);
1895
1896 auto *N = newSDNode<MDNodeSDNode>(MD);
1897 CSEMap.InsertNode(N, IP);
1898 InsertNode(N);
1899 return SDValue(N, 0);
1900}
1901
1902SDValue SelectionDAG::getBitcast(EVT VT, SDValue V) {
1903 if (VT == V.getValueType())
1904 return V;
1905
1906 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
1907}
1908
1909SDValue SelectionDAG::getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr,
1910 unsigned SrcAS, unsigned DestAS) {
1911 SDValue Ops[] = {Ptr};
1912 FoldingSetNodeID ID;
1913 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
1914 ID.AddInteger(SrcAS);
1915 ID.AddInteger(DestAS);
1916
1917 void *IP = nullptr;
1918 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
1919 return SDValue(E, 0);
1920
1921 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
1922 VT, SrcAS, DestAS);
1923 createOperands(N, Ops);
1924
1925 CSEMap.InsertNode(N, IP);
1926 InsertNode(N);
1927 return SDValue(N, 0);
1928}
1929
1930/// getShiftAmountOperand - Return the specified value casted to
1931/// the target's desired shift amount type.
1932SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
1933 EVT OpTy = Op.getValueType();
1934 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
1935 if (OpTy == ShTy || OpTy.isVector()) return Op;
1936
1937 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
1938}
1939
1940SDValue SelectionDAG::expandVAArg(SDNode *Node) {
1941 SDLoc dl(Node);
1942 const TargetLowering &TLI = getTargetLoweringInfo();
1943 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1944 EVT VT = Node->getValueType(0);
1945 SDValue Tmp1 = Node->getOperand(0);
1946 SDValue Tmp2 = Node->getOperand(1);
1947 const MaybeAlign MA(Node->getConstantOperandVal(3));
1948
1949 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
1950 Tmp2, MachinePointerInfo(V));
1951 SDValue VAList = VAListLoad;
1952
1953 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
1954 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
1955 getConstant(MA->value() - 1, dl, VAList.getValueType()));
1956
1957 VAList =
1958 getNode(ISD::AND, dl, VAList.getValueType(), VAList,
1959 getConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
1960 }
1961
1962 // Increment the pointer, VAList, to the next vaarg
1963 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
1964 getConstant(getDataLayout().getTypeAllocSize(
1965 VT.getTypeForEVT(*getContext())),
1966 dl, VAList.getValueType()));
1967 // Store the incremented VAList to the legalized pointer
1968 Tmp1 =
1969 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
1970 // Load the actual argument out of the pointer VAList
1971 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
1972}
1973
1974SDValue SelectionDAG::expandVACopy(SDNode *Node) {
1975 SDLoc dl(Node);
1976 const TargetLowering &TLI = getTargetLoweringInfo();
1977 // This defaults to loading a pointer from the input and storing it to the
1978 // output, returning the chain.
1979 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
1980 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
1981 SDValue Tmp1 =
1982 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
1983 Node->getOperand(2), MachinePointerInfo(VS));
1984 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
1985 MachinePointerInfo(VD));
1986}
1987
1988SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
1989 MachineFrameInfo &MFI = getMachineFunction().getFrameInfo();
1990 unsigned ByteSize = VT.getStoreSize();
1991 Type *Ty = VT.getTypeForEVT(*getContext());
1992 unsigned StackAlign =
1993 std::max((unsigned)getDataLayout().getPrefTypeAlignment(Ty), minAlign);
1994
1995 int FrameIdx = MFI.CreateStackObject(ByteSize, StackAlign, false);
1996 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
1997}
1998
1999SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
2000 unsigned Bytes = std::max(VT1.getStoreSize(), VT2.getStoreSize());
2001 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2002 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2003 const DataLayout &DL = getDataLayout();
2004 unsigned Align =
2005 std::max(DL.getPrefTypeAlignment(Ty1), DL.getPrefTypeAlignment(Ty2));
2006
2007 MachineFrameInfo &MFI = getMachineFunction().getFrameInfo();
2008 int FrameIdx = MFI.CreateStackObject(Bytes, Align, false);
2009 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2010}
2011
2012SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1, SDValue N2,
2013 ISD::CondCode Cond, const SDLoc &dl) {
2014 EVT OpVT = N1.getValueType();
2015
2016 // These setcc operations always fold.
2017 switch (Cond) {
2018 default: break;
2019 case ISD::SETFALSE:
2020 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2021 case ISD::SETTRUE:
2022 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2023
2024 case ISD::SETOEQ:
2025 case ISD::SETOGT:
2026 case ISD::SETOGE:
2027 case ISD::SETOLT:
2028 case ISD::SETOLE:
2029 case ISD::SETONE:
2030 case ISD::SETO:
2031 case ISD::SETUO:
2032 case ISD::SETUEQ:
2033 case ISD::SETUNE:
2034 assert(!OpVT.isInteger() && "Illegal setcc for integer!")((!OpVT.isInteger() && "Illegal setcc for integer!") ?
static_cast<void> (0) : __assert_fail ("!OpVT.isInteger() && \"Illegal setcc for integer!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2034, __PRETTY_FUNCTION__))
;
2035 break;
2036 }
2037
2038 if (OpVT.isInteger()) {
2039 // For EQ and NE, we can always pick a value for the undef to make the
2040 // predicate pass or fail, so we can return undef.
2041 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2042 // icmp eq/ne X, undef -> undef.
2043 if ((N1.isUndef() || N2.isUndef()) &&
2044 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2045 return getUNDEF(VT);
2046
2047 // If both operands are undef, we can return undef for int comparison.
2048 // icmp undef, undef -> undef.
2049 if (N1.isUndef() && N2.isUndef())
2050 return getUNDEF(VT);
2051
2052 // icmp X, X -> true/false
2053 // icmp X, undef -> true/false because undef could be X.
2054 if (N1 == N2)
2055 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2056 }
2057
2058 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
2059 const APInt &C2 = N2C->getAPIntValue();
2060 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
2061 const APInt &C1 = N1C->getAPIntValue();
2062
2063 switch (Cond) {
2064 default: llvm_unreachable("Unknown integer setcc!")::llvm::llvm_unreachable_internal("Unknown integer setcc!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2064)
;
2065 case ISD::SETEQ: return getBoolConstant(C1 == C2, dl, VT, OpVT);
2066 case ISD::SETNE: return getBoolConstant(C1 != C2, dl, VT, OpVT);
2067 case ISD::SETULT: return getBoolConstant(C1.ult(C2), dl, VT, OpVT);
2068 case ISD::SETUGT: return getBoolConstant(C1.ugt(C2), dl, VT, OpVT);
2069 case ISD::SETULE: return getBoolConstant(C1.ule(C2), dl, VT, OpVT);
2070 case ISD::SETUGE: return getBoolConstant(C1.uge(C2), dl, VT, OpVT);
2071 case ISD::SETLT: return getBoolConstant(C1.slt(C2), dl, VT, OpVT);
2072 case ISD::SETGT: return getBoolConstant(C1.sgt(C2), dl, VT, OpVT);
2073 case ISD::SETLE: return getBoolConstant(C1.sle(C2), dl, VT, OpVT);
2074 case ISD::SETGE: return getBoolConstant(C1.sge(C2), dl, VT, OpVT);
2075 }
2076 }
2077 }
2078
2079 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2080 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2081
2082 if (N1CFP && N2CFP) {
2083 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2084 switch (Cond) {
2085 default: break;
2086 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2087 return getUNDEF(VT);
2088 LLVM_FALLTHROUGH[[gnu::fallthrough]];
2089 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2090 OpVT);
2091 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2092 return getUNDEF(VT);
2093 LLVM_FALLTHROUGH[[gnu::fallthrough]];
2094 case ISD::SETONE: return getBoolConstant(R==APFloat::cmpGreaterThan ||
2095 R==APFloat::cmpLessThan, dl, VT,
2096 OpVT);
2097 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2098 return getUNDEF(VT);
2099 LLVM_FALLTHROUGH[[gnu::fallthrough]];
2100 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2101 OpVT);
2102 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2103 return getUNDEF(VT);
2104 LLVM_FALLTHROUGH[[gnu::fallthrough]];
2105 case ISD::SETOGT: return getBoolConstant(R==APFloat::cmpGreaterThan, dl,
2106 VT, OpVT);
2107 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2108 return getUNDEF(VT);
2109 LLVM_FALLTHROUGH[[gnu::fallthrough]];
2110 case ISD::SETOLE: return getBoolConstant(R==APFloat::cmpLessThan ||
2111 R==APFloat::cmpEqual, dl, VT,
2112 OpVT);
2113 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2114 return getUNDEF(VT);
2115 LLVM_FALLTHROUGH[[gnu::fallthrough]];
2116 case ISD::SETOGE: return getBoolConstant(R==APFloat::cmpGreaterThan ||
2117 R==APFloat::cmpEqual, dl, VT, OpVT);
2118 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2119 OpVT);
2120 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2121 OpVT);
2122 case ISD::SETUEQ: return getBoolConstant(R==APFloat::cmpUnordered ||
2123 R==APFloat::cmpEqual, dl, VT,
2124 OpVT);
2125 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2126 OpVT);
2127 case ISD::SETULT: return getBoolConstant(R==APFloat::cmpUnordered ||
2128 R==APFloat::cmpLessThan, dl, VT,
2129 OpVT);
2130 case ISD::SETUGT: return getBoolConstant(R==APFloat::cmpGreaterThan ||
2131 R==APFloat::cmpUnordered, dl, VT,
2132 OpVT);
2133 case ISD::SETULE: return getBoolConstant(R!=APFloat::cmpGreaterThan, dl,
2134 VT, OpVT);
2135 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2136 OpVT);
2137 }
2138 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2139 // Ensure that the constant occurs on the RHS.
2140 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
2141 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2142 return SDValue();
2143 return getSetCC(dl, VT, N2, N1, SwappedCond);
2144 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2145 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2146 // If an operand is known to be a nan (or undef that could be a nan), we can
2147 // fold it.
2148 // Choosing NaN for the undef will always make unordered comparison succeed
2149 // and ordered comparison fails.
2150 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2151 switch (ISD::getUnorderedFlavor(Cond)) {
2152 default:
2153 llvm_unreachable("Unknown flavor!")::llvm::llvm_unreachable_internal("Unknown flavor!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2153)
;
2154 case 0: // Known false.
2155 return getBoolConstant(false, dl, VT, OpVT);
2156 case 1: // Known true.
2157 return getBoolConstant(true, dl, VT, OpVT);
2158 case 2: // Undefined.
2159 return getUNDEF(VT);
2160 }
2161 }
2162
2163 // Could not fold it.
2164 return SDValue();
2165}
2166
2167/// See if the specified operand can be simplified with the knowledge that only
2168/// the bits specified by DemandedBits are used.
2169/// TODO: really we should be making this into the DAG equivalent of
2170/// SimplifyMultipleUseDemandedBits and not generate any new nodes.
2171SDValue SelectionDAG::GetDemandedBits(SDValue V, const APInt &DemandedBits) {
2172 EVT VT = V.getValueType();
2173 APInt DemandedElts = VT.isVector()
2174 ? APInt::getAllOnesValue(VT.getVectorNumElements())
2175 : APInt(1, 1);
2176 return GetDemandedBits(V, DemandedBits, DemandedElts);
2177}
2178
2179/// See if the specified operand can be simplified with the knowledge that only
2180/// the bits specified by DemandedBits are used in the elements specified by
2181/// DemandedElts.
2182/// TODO: really we should be making this into the DAG equivalent of
2183/// SimplifyMultipleUseDemandedBits and not generate any new nodes.
2184SDValue SelectionDAG::GetDemandedBits(SDValue V, const APInt &DemandedBits,
2185 const APInt &DemandedElts) {
2186 switch (V.getOpcode()) {
2187 default:
2188 return TLI->SimplifyMultipleUseDemandedBits(V, DemandedBits, DemandedElts,
2189 *this, 0);
2190 break;
2191 case ISD::Constant: {
2192 auto *CV = cast<ConstantSDNode>(V.getNode());
2193 assert(CV && "Const value should be ConstSDNode.")((CV && "Const value should be ConstSDNode.") ? static_cast
<void> (0) : __assert_fail ("CV && \"Const value should be ConstSDNode.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2193, __PRETTY_FUNCTION__))
;
2194 const APInt &CVal = CV->getAPIntValue();
2195 APInt NewVal = CVal & DemandedBits;
2196 if (NewVal != CVal)
2197 return getConstant(NewVal, SDLoc(V), V.getValueType());
2198 break;
2199 }
2200 case ISD::SRL:
2201 // Only look at single-use SRLs.
2202 if (!V.getNode()->hasOneUse())
2203 break;
2204 if (auto *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
2205 // See if we can recursively simplify the LHS.
2206 unsigned Amt = RHSC->getZExtValue();
2207
2208 // Watch out for shift count overflow though.
2209 if (Amt >= DemandedBits.getBitWidth())
2210 break;
2211 APInt SrcDemandedBits = DemandedBits << Amt;
2212 if (SDValue SimplifyLHS =
2213 GetDemandedBits(V.getOperand(0), SrcDemandedBits))
2214 return getNode(ISD::SRL, SDLoc(V), V.getValueType(), SimplifyLHS,
2215 V.getOperand(1));
2216 }
2217 break;
2218 case ISD::AND: {
2219 // X & -1 -> X (ignoring bits which aren't demanded).
2220 // Also handle the case where masked out bits in X are known to be zero.
2221 if (ConstantSDNode *RHSC = isConstOrConstSplat(V.getOperand(1))) {
2222 const APInt &AndVal = RHSC->getAPIntValue();
2223 if (DemandedBits.isSubsetOf(AndVal) ||
2224 DemandedBits.isSubsetOf(computeKnownBits(V.getOperand(0)).Zero |
2225 AndVal))
2226 return V.getOperand(0);
2227 }
2228 break;
2229 }
2230 }
2231 return SDValue();
2232}
2233
2234/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2235/// use this predicate to simplify operations downstream.
2236bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
2237 unsigned BitWidth = Op.getScalarValueSizeInBits();
2238 return MaskedValueIsZero(Op, APInt::getSignMask(BitWidth), Depth);
2239}
2240
2241/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2242/// this predicate to simplify operations downstream. Mask is known to be zero
2243/// for bits that V cannot have.
2244bool SelectionDAG::MaskedValueIsZero(SDValue V, const APInt &Mask,
2245 unsigned Depth) const {
2246 EVT VT = V.getValueType();
2247 APInt DemandedElts = VT.isVector()
2248 ? APInt::getAllOnesValue(VT.getVectorNumElements())
2249 : APInt(1, 1);
2250 return MaskedValueIsZero(V, Mask, DemandedElts, Depth);
2251}
2252
2253/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2254/// DemandedElts. We use this predicate to simplify operations downstream.
2255/// Mask is known to be zero for bits that V cannot have.
2256bool SelectionDAG::MaskedValueIsZero(SDValue V, const APInt &Mask,
2257 const APInt &DemandedElts,
2258 unsigned Depth) const {
2259 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2260}
2261
2262/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2263bool SelectionDAG::MaskedValueIsAllOnes(SDValue V, const APInt &Mask,
2264 unsigned Depth) const {
2265 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2266}
2267
2268/// isSplatValue - Return true if the vector V has the same value
2269/// across all DemandedElts.
2270bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2271 APInt &UndefElts) {
2272 if (!DemandedElts)
2273 return false; // No demanded elts, better to assume we don't know anything.
2274
2275 EVT VT = V.getValueType();
2276 assert(VT.isVector() && "Vector type expected")((VT.isVector() && "Vector type expected") ? static_cast
<void> (0) : __assert_fail ("VT.isVector() && \"Vector type expected\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2276, __PRETTY_FUNCTION__))
;
2277
2278 unsigned NumElts = VT.getVectorNumElements();
2279 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch")((NumElts == DemandedElts.getBitWidth() && "Vector size mismatch"
) ? static_cast<void> (0) : __assert_fail ("NumElts == DemandedElts.getBitWidth() && \"Vector size mismatch\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2279, __PRETTY_FUNCTION__))
;
2280 UndefElts = APInt::getNullValue(NumElts);
2281
2282 switch (V.getOpcode()) {
2283 case ISD::BUILD_VECTOR: {
2284 SDValue Scl;
2285 for (unsigned i = 0; i != NumElts; ++i) {
2286 SDValue Op = V.getOperand(i);
2287 if (Op.isUndef()) {
2288 UndefElts.setBit(i);
2289 continue;
2290 }
2291 if (!DemandedElts[i])
2292 continue;
2293 if (Scl && Scl != Op)
2294 return false;
2295 Scl = Op;
2296 }
2297 return true;
2298 }
2299 case ISD::VECTOR_SHUFFLE: {
2300 // Check if this is a shuffle node doing a splat.
2301 // TODO: Do we need to handle shuffle(splat, undef, mask)?
2302 int SplatIndex = -1;
2303 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2304 for (int i = 0; i != (int)NumElts; ++i) {
2305 int M = Mask[i];
2306 if (M < 0) {
2307 UndefElts.setBit(i);
2308 continue;
2309 }
2310 if (!DemandedElts[i])
2311 continue;
2312 if (0 <= SplatIndex && SplatIndex != M)
2313 return false;
2314 SplatIndex = M;
2315 }
2316 return true;
2317 }
2318 case ISD::EXTRACT_SUBVECTOR: {
2319 SDValue Src = V.getOperand(0);
2320 ConstantSDNode *SubIdx = dyn_cast<ConstantSDNode>(V.getOperand(1));
2321 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2322 if (SubIdx && SubIdx->getAPIntValue().ule(NumSrcElts - NumElts)) {
2323 // Offset the demanded elts by the subvector index.
2324 uint64_t Idx = SubIdx->getZExtValue();
2325 APInt UndefSrcElts;
2326 APInt DemandedSrc = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx);
2327 if (isSplatValue(Src, DemandedSrc, UndefSrcElts)) {
2328 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
2329 return true;
2330 }
2331 }
2332 break;
2333 }
2334 case ISD::ADD:
2335 case ISD::SUB:
2336 case ISD::AND: {
2337 APInt UndefLHS, UndefRHS;
2338 SDValue LHS = V.getOperand(0);
2339 SDValue RHS = V.getOperand(1);
2340 if (isSplatValue(LHS, DemandedElts, UndefLHS) &&
2341 isSplatValue(RHS, DemandedElts, UndefRHS)) {
2342 UndefElts = UndefLHS | UndefRHS;
2343 return true;
2344 }
2345 break;
2346 }
2347 }
2348
2349 return false;
2350}
2351
2352/// Helper wrapper to main isSplatValue function.
2353bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) {
2354 EVT VT = V.getValueType();
2355 assert(VT.isVector() && "Vector type expected")((VT.isVector() && "Vector type expected") ? static_cast
<void> (0) : __assert_fail ("VT.isVector() && \"Vector type expected\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2355, __PRETTY_FUNCTION__))
;
2356 unsigned NumElts = VT.getVectorNumElements();
2357
2358 APInt UndefElts;
2359 APInt DemandedElts = APInt::getAllOnesValue(NumElts);
2360 return isSplatValue(V, DemandedElts, UndefElts) &&
2361 (AllowUndefs || !UndefElts);
2362}
2363
2364SDValue SelectionDAG::getSplatSourceVector(SDValue V, int &SplatIdx) {
2365 V = peekThroughExtractSubvectors(V);
2366
2367 EVT VT = V.getValueType();
2368 unsigned Opcode = V.getOpcode();
2369 switch (Opcode) {
2370 default: {
2371 APInt UndefElts;
2372 APInt DemandedElts = APInt::getAllOnesValue(VT.getVectorNumElements());
2373 if (isSplatValue(V, DemandedElts, UndefElts)) {
2374 // Handle case where all demanded elements are UNDEF.
2375 if (DemandedElts.isSubsetOf(UndefElts)) {
2376 SplatIdx = 0;
2377 return getUNDEF(VT);
2378 }
2379 SplatIdx = (UndefElts & DemandedElts).countTrailingOnes();
2380 return V;
2381 }
2382 break;
2383 }
2384 case ISD::VECTOR_SHUFFLE: {
2385 // Check if this is a shuffle node doing a splat.
2386 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
2387 // getTargetVShiftNode currently struggles without the splat source.
2388 auto *SVN = cast<ShuffleVectorSDNode>(V);
2389 if (!SVN->isSplat())
2390 break;
2391 int Idx = SVN->getSplatIndex();
2392 int NumElts = V.getValueType().getVectorNumElements();
2393 SplatIdx = Idx % NumElts;
2394 return V.getOperand(Idx / NumElts);
2395 }
2396 }
2397
2398 return SDValue();
2399}
2400
2401SDValue SelectionDAG::getSplatValue(SDValue V) {
2402 int SplatIdx;
2403 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx))
2404 return getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(V),
2405 SrcVector.getValueType().getScalarType(), SrcVector,
2406 getVectorIdxConstant(SplatIdx, SDLoc(V)));
2407 return SDValue();
2408}
2409
2410const APInt *
2411SelectionDAG::getValidShiftAmountConstant(SDValue V,
2412 const APInt &DemandedElts) const {
2413 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||(((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V
.getOpcode() == ISD::SRA) && "Unknown shift node") ? static_cast
<void> (0) : __assert_fail ("(V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V.getOpcode() == ISD::SRA) && \"Unknown shift node\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2415, __PRETTY_FUNCTION__))
2414 V.getOpcode() == ISD::SRA) &&(((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V
.getOpcode() == ISD::SRA) && "Unknown shift node") ? static_cast
<void> (0) : __assert_fail ("(V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V.getOpcode() == ISD::SRA) && \"Unknown shift node\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2415, __PRETTY_FUNCTION__))
2415 "Unknown shift node")(((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V
.getOpcode() == ISD::SRA) && "Unknown shift node") ? static_cast
<void> (0) : __assert_fail ("(V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V.getOpcode() == ISD::SRA) && \"Unknown shift node\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2415, __PRETTY_FUNCTION__))
;
2416 unsigned BitWidth = V.getScalarValueSizeInBits();
2417 if (ConstantSDNode *SA = isConstOrConstSplat(V.getOperand(1), DemandedElts)) {
2418 // Shifting more than the bitwidth is not valid.
2419 const APInt &ShAmt = SA->getAPIntValue();
2420 if (ShAmt.ult(BitWidth))
2421 return &ShAmt;
2422 }
2423 return nullptr;
2424}
2425
2426const APInt *SelectionDAG::getValidMinimumShiftAmountConstant(
2427 SDValue V, const APInt &DemandedElts) const {
2428 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||(((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V
.getOpcode() == ISD::SRA) && "Unknown shift node") ? static_cast
<void> (0) : __assert_fail ("(V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V.getOpcode() == ISD::SRA) && \"Unknown shift node\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2430, __PRETTY_FUNCTION__))
2429 V.getOpcode() == ISD::SRA) &&(((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V
.getOpcode() == ISD::SRA) && "Unknown shift node") ? static_cast
<void> (0) : __assert_fail ("(V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V.getOpcode() == ISD::SRA) && \"Unknown shift node\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2430, __PRETTY_FUNCTION__))
2430 "Unknown shift node")(((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V
.getOpcode() == ISD::SRA) && "Unknown shift node") ? static_cast
<void> (0) : __assert_fail ("(V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V.getOpcode() == ISD::SRA) && \"Unknown shift node\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2430, __PRETTY_FUNCTION__))
;
2431 if (const APInt *ValidAmt = getValidShiftAmountConstant(V, DemandedElts))
2432 return ValidAmt;
2433 unsigned BitWidth = V.getScalarValueSizeInBits();
2434 auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1));
2435 if (!BV)
2436 return nullptr;
2437 const APInt *MinShAmt = nullptr;
2438 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
2439 if (!DemandedElts[i])
2440 continue;
2441 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
2442 if (!SA)
2443 return nullptr;
2444 // Shifting more than the bitwidth is not valid.
2445 const APInt &ShAmt = SA->getAPIntValue();
2446 if (ShAmt.uge(BitWidth))
2447 return nullptr;
2448 if (MinShAmt && MinShAmt->ule(ShAmt))
2449 continue;
2450 MinShAmt = &ShAmt;
2451 }
2452 return MinShAmt;
2453}
2454
2455const APInt *SelectionDAG::getValidMaximumShiftAmountConstant(
2456 SDValue V, const APInt &DemandedElts) const {
2457 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||(((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V
.getOpcode() == ISD::SRA) && "Unknown shift node") ? static_cast
<void> (0) : __assert_fail ("(V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V.getOpcode() == ISD::SRA) && \"Unknown shift node\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2459, __PRETTY_FUNCTION__))
2458 V.getOpcode() == ISD::SRA) &&(((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V
.getOpcode() == ISD::SRA) && "Unknown shift node") ? static_cast
<void> (0) : __assert_fail ("(V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V.getOpcode() == ISD::SRA) && \"Unknown shift node\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2459, __PRETTY_FUNCTION__))
2459 "Unknown shift node")(((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V
.getOpcode() == ISD::SRA) && "Unknown shift node") ? static_cast
<void> (0) : __assert_fail ("(V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL || V.getOpcode() == ISD::SRA) && \"Unknown shift node\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2459, __PRETTY_FUNCTION__))
;
2460 if (const APInt *ValidAmt = getValidShiftAmountConstant(V, DemandedElts))
2461 return ValidAmt;
2462 unsigned BitWidth = V.getScalarValueSizeInBits();
2463 auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1));
2464 if (!BV)
2465 return nullptr;
2466 const APInt *MaxShAmt = nullptr;
2467 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
2468 if (!DemandedElts[i])
2469 continue;
2470 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
2471 if (!SA)
2472 return nullptr;
2473 // Shifting more than the bitwidth is not valid.
2474 const APInt &ShAmt = SA->getAPIntValue();
2475 if (ShAmt.uge(BitWidth))
2476 return nullptr;
2477 if (MaxShAmt && MaxShAmt->uge(ShAmt))
2478 continue;
2479 MaxShAmt = &ShAmt;
2480 }
2481 return MaxShAmt;
2482}
2483
2484/// Determine which bits of Op are known to be either zero or one and return
2485/// them in Known. For vectors, the known bits are those that are shared by
2486/// every vector element.
2487KnownBits SelectionDAG::computeKnownBits(SDValue Op, unsigned Depth) const {
2488 EVT VT = Op.getValueType();
2489 APInt DemandedElts = VT.isVector()
2490 ? APInt::getAllOnesValue(VT.getVectorNumElements())
2491 : APInt(1, 1);
2492 return computeKnownBits(Op, DemandedElts, Depth);
2493}
2494
2495/// Determine which bits of Op are known to be either zero or one and return
2496/// them in Known. The DemandedElts argument allows us to only collect the known
2497/// bits that are shared by the requested vector elements.
2498KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
2499 unsigned Depth) const {
2500 unsigned BitWidth = Op.getScalarValueSizeInBits();
2501
2502 KnownBits Known(BitWidth); // Don't know anything.
2503
2504 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
2505 // We know all of the bits for a constant!
2506 Known.One = C->getAPIntValue();
2507 Known.Zero = ~Known.One;
2508 return Known;
2509 }
2510 if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
2511 // We know all of the bits for a constant fp!
2512 Known.One = C->getValueAPF().bitcastToAPInt();
2513 Known.Zero = ~Known.One;
2514 return Known;
2515 }
2516
2517 if (Depth >= MaxRecursionDepth)
2518 return Known; // Limit search depth.
2519
2520 KnownBits Known2;
2521 unsigned NumElts = DemandedElts.getBitWidth();
2522 assert((!Op.getValueType().isVector() ||(((!Op.getValueType().isVector() || NumElts == Op.getValueType
().getVectorNumElements()) && "Unexpected vector size"
) ? static_cast<void> (0) : __assert_fail ("(!Op.getValueType().isVector() || NumElts == Op.getValueType().getVectorNumElements()) && \"Unexpected vector size\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2524, __PRETTY_FUNCTION__))
2523 NumElts == Op.getValueType().getVectorNumElements()) &&(((!Op.getValueType().isVector() || NumElts == Op.getValueType
().getVectorNumElements()) && "Unexpected vector size"
) ? static_cast<void> (0) : __assert_fail ("(!Op.getValueType().isVector() || NumElts == Op.getValueType().getVectorNumElements()) && \"Unexpected vector size\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2524, __PRETTY_FUNCTION__))
2524 "Unexpected vector size")(((!Op.getValueType().isVector() || NumElts == Op.getValueType
().getVectorNumElements()) && "Unexpected vector size"
) ? static_cast<void> (0) : __assert_fail ("(!Op.getValueType().isVector() || NumElts == Op.getValueType().getVectorNumElements()) && \"Unexpected vector size\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2524, __PRETTY_FUNCTION__))
;
2525
2526 if (!DemandedElts)
2527 return Known; // No demanded elts, better to assume we don't know anything.
2528
2529 unsigned Opcode = Op.getOpcode();
2530 switch (Opcode) {
2531 case ISD::BUILD_VECTOR:
2532 // Collect the known bits that are shared by every demanded vector element.
2533 Known.Zero.setAllBits(); Known.One.setAllBits();
2534 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
2535 if (!DemandedElts[i])
2536 continue;
2537
2538 SDValue SrcOp = Op.getOperand(i);
2539 Known2 = computeKnownBits(SrcOp, Depth + 1);
2540
2541 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
2542 if (SrcOp.getValueSizeInBits() != BitWidth) {
2543 assert(SrcOp.getValueSizeInBits() > BitWidth &&((SrcOp.getValueSizeInBits() > BitWidth && "Expected BUILD_VECTOR implicit truncation"
) ? static_cast<void> (0) : __assert_fail ("SrcOp.getValueSizeInBits() > BitWidth && \"Expected BUILD_VECTOR implicit truncation\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2544, __PRETTY_FUNCTION__))
2544 "Expected BUILD_VECTOR implicit truncation")((SrcOp.getValueSizeInBits() > BitWidth && "Expected BUILD_VECTOR implicit truncation"
) ? static_cast<void> (0) : __assert_fail ("SrcOp.getValueSizeInBits() > BitWidth && \"Expected BUILD_VECTOR implicit truncation\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2544, __PRETTY_FUNCTION__))
;
2545 Known2 = Known2.trunc(BitWidth);
2546 }
2547
2548 // Known bits are the values that are shared by every demanded element.
2549 Known.One &= Known2.One;
2550 Known.Zero &= Known2.Zero;
2551
2552 // If we don't know any bits, early out.
2553 if (Known.isUnknown())
2554 break;
2555 }
2556 break;
2557 case ISD::VECTOR_SHUFFLE: {
2558 // Collect the known bits that are shared by every vector element referenced
2559 // by the shuffle.
2560 APInt DemandedLHS(NumElts, 0), DemandedRHS(NumElts, 0);
2561 Known.Zero.setAllBits(); Known.One.setAllBits();
2562 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2563 assert(NumElts == SVN->getMask().size() && "Unexpected vector size")((NumElts == SVN->getMask().size() && "Unexpected vector size"
) ? static_cast<void> (0) : __assert_fail ("NumElts == SVN->getMask().size() && \"Unexpected vector size\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2563, __PRETTY_FUNCTION__))
;
2564 for (unsigned i = 0; i != NumElts; ++i) {
2565 if (!DemandedElts[i])
2566 continue;
2567
2568 int M = SVN->getMaskElt(i);
2569 if (M < 0) {
2570 // For UNDEF elements, we don't know anything about the common state of
2571 // the shuffle result.
2572 Known.resetAll();
2573 DemandedLHS.clearAllBits();
2574 DemandedRHS.clearAllBits();
2575 break;
2576 }
2577
2578 if ((unsigned)M < NumElts)
2579 DemandedLHS.setBit((unsigned)M % NumElts);
2580 else
2581 DemandedRHS.setBit((unsigned)M % NumElts);
2582 }
2583 // Known bits are the values that are shared by every demanded element.
2584 if (!!DemandedLHS) {
2585 SDValue LHS = Op.getOperand(0);
2586 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
2587 Known.One &= Known2.One;
2588 Known.Zero &= Known2.Zero;
2589 }
2590 // If we don't know any bits, early out.
2591 if (Known.isUnknown())
2592 break;
2593 if (!!DemandedRHS) {
2594 SDValue RHS = Op.getOperand(1);
2595 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
2596 Known.One &= Known2.One;
2597 Known.Zero &= Known2.Zero;
2598 }
2599 break;
2600 }
2601 case ISD::CONCAT_VECTORS: {
2602 // Split DemandedElts and test each of the demanded subvectors.
2603 Known.Zero.setAllBits(); Known.One.setAllBits();
2604 EVT SubVectorVT = Op.getOperand(0).getValueType();
2605 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
2606 unsigned NumSubVectors = Op.getNumOperands();
2607 for (unsigned i = 0; i != NumSubVectors; ++i) {
2608 APInt DemandedSub = DemandedElts.lshr(i * NumSubVectorElts);
2609 DemandedSub = DemandedSub.trunc(NumSubVectorElts);
2610 if (!!DemandedSub) {
2611 SDValue Sub = Op.getOperand(i);
2612 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
2613 Known.One &= Known2.One;
2614 Known.Zero &= Known2.Zero;
2615 }
2616 // If we don't know any bits, early out.
2617 if (Known.isUnknown())
2618 break;
2619 }
2620 break;
2621 }
2622 case ISD::INSERT_SUBVECTOR: {
2623 // If we know the element index, demand any elements from the subvector and
2624 // the remainder from the src its inserted into, otherwise assume we need
2625 // the original demanded base elements and ALL the inserted subvector
2626 // elements.
2627 SDValue Src = Op.getOperand(0);
2628 SDValue Sub = Op.getOperand(1);
2629 auto *SubIdx = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2630 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
2631 APInt DemandedSubElts = APInt::getAllOnesValue(NumSubElts);
2632 APInt DemandedSrcElts = DemandedElts;
2633 if (SubIdx && SubIdx->getAPIntValue().ule(NumElts - NumSubElts)) {
2634 uint64_t Idx = SubIdx->getZExtValue();
2635 DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
2636 DemandedSrcElts.insertBits(APInt::getNullValue(NumSubElts), Idx);
2637 }
2638 Known.One.setAllBits();
2639 Known.Zero.setAllBits();
2640 if (!!DemandedSubElts) {
2641 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
2642 if (Known.isUnknown())
2643 break; // early-out.
2644 }
2645 if (!!DemandedSrcElts) {
2646 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
2647 Known.One &= Known2.One;
2648 Known.Zero &= Known2.Zero;
2649 }
2650 break;
2651 }
2652 case ISD::EXTRACT_SUBVECTOR: {
2653 // If we know the element index, just demand that subvector elements,
2654 // otherwise demand them all.
2655 SDValue Src = Op.getOperand(0);
2656 ConstantSDNode *SubIdx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2657 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2658 APInt DemandedSrc = APInt::getAllOnesValue(NumSrcElts);
2659 if (SubIdx && SubIdx->getAPIntValue().ule(NumSrcElts - NumElts)) {
2660 // Offset the demanded elts by the subvector index.
2661 uint64_t Idx = SubIdx->getZExtValue();
2662 DemandedSrc = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx);
2663 }
2664 Known = computeKnownBits(Src, DemandedSrc, Depth + 1);
2665 break;
2666 }
2667 case ISD::SCALAR_TO_VECTOR: {
2668 // We know about scalar_to_vector as much as we know about it source,
2669 // which becomes the first element of otherwise unknown vector.
2670 if (DemandedElts != 1)
2671 break;
2672
2673 SDValue N0 = Op.getOperand(0);
2674 Known = computeKnownBits(N0, Depth + 1);
2675 if (N0.getValueSizeInBits() != BitWidth)
2676 Known = Known.trunc(BitWidth);
2677
2678 break;
2679 }
2680 case ISD::BITCAST: {
2681 SDValue N0 = Op.getOperand(0);
2682 EVT SubVT = N0.getValueType();
2683 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
2684
2685 // Ignore bitcasts from unsupported types.
2686 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
2687 break;
2688
2689 // Fast handling of 'identity' bitcasts.
2690 if (BitWidth == SubBitWidth) {
2691 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
2692 break;
2693 }
2694
2695 bool IsLE = getDataLayout().isLittleEndian();
2696
2697 // Bitcast 'small element' vector to 'large element' scalar/vector.
2698 if ((BitWidth % SubBitWidth) == 0) {
2699 assert(N0.getValueType().isVector() && "Expected bitcast from vector")((N0.getValueType().isVector() && "Expected bitcast from vector"
) ? static_cast<void> (0) : __assert_fail ("N0.getValueType().isVector() && \"Expected bitcast from vector\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2699, __PRETTY_FUNCTION__))
;
2700
2701 // Collect known bits for the (larger) output by collecting the known
2702 // bits from each set of sub elements and shift these into place.
2703 // We need to separately call computeKnownBits for each set of
2704 // sub elements as the knownbits for each is likely to be different.
2705 unsigned SubScale = BitWidth / SubBitWidth;
2706 APInt SubDemandedElts(NumElts * SubScale, 0);
2707 for (unsigned i = 0; i != NumElts; ++i)
2708 if (DemandedElts[i])
2709 SubDemandedElts.setBit(i * SubScale);
2710
2711 for (unsigned i = 0; i != SubScale; ++i) {
2712 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
2713 Depth + 1);
2714 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
2715 Known.One |= Known2.One.zext(BitWidth).shl(SubBitWidth * Shifts);
2716 Known.Zero |= Known2.Zero.zext(BitWidth).shl(SubBitWidth * Shifts);
2717 }
2718 }
2719
2720 // Bitcast 'large element' scalar/vector to 'small element' vector.
2721 if ((SubBitWidth % BitWidth) == 0) {
2722 assert(Op.getValueType().isVector() && "Expected bitcast to vector")((Op.getValueType().isVector() && "Expected bitcast to vector"
) ? static_cast<void> (0) : __assert_fail ("Op.getValueType().isVector() && \"Expected bitcast to vector\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 2722, __PRETTY_FUNCTION__))
;
2723
2724 // Collect known bits for the (smaller) output by collecting the known
2725 // bits from the overlapping larger input elements and extracting the
2726 // sub sections we actually care about.
2727 unsigned SubScale = SubBitWidth / BitWidth;
2728 APInt SubDemandedElts(NumElts / SubScale, 0);
2729 for (unsigned i = 0; i != NumElts; ++i)
2730 if (DemandedElts[i])
2731 SubDemandedElts.setBit(i / SubScale);
2732
2733 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
2734
2735 Known.Zero.setAllBits(); Known.One.setAllBits();
2736 for (unsigned i = 0; i != NumElts; ++i)
2737 if (DemandedElts[i]) {
2738 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
2739 unsigned Offset = (Shifts % SubScale) * BitWidth;
2740 Known.One &= Known2.One.lshr(Offset).trunc(BitWidth);
2741 Known.Zero &= Known2.Zero.lshr(Offset).trunc(BitWidth);
2742 // If we don't know any bits, early out.
2743 if (Known.isUnknown())
2744 break;
2745 }
2746 }
2747 break;
2748 }
2749 case ISD::AND:
2750 // If either the LHS or the RHS are Zero, the result is zero.
2751 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
2752 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2753
2754 // Output known-1 bits are only known if set in both the LHS & RHS.
2755 Known.One &= Known2.One;
2756 // Output known-0 are known to be clear if zero in either the LHS | RHS.
2757 Known.Zero |= Known2.Zero;
2758 break;
2759 case ISD::OR:
2760 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
2761 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2762
2763 // Output known-0 bits are only known if clear in both the LHS & RHS.
2764 Known.Zero &= Known2.Zero;
2765 // Output known-1 are known to be set if set in either the LHS | RHS.
2766 Known.One |= Known2.One;
2767 break;
2768 case ISD::XOR: {
2769 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
2770 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2771
2772 // Output known-0 bits are known if clear or set in both the LHS & RHS.
2773 APInt KnownZeroOut = (Known.Zero & Known2.Zero) | (Known.One & Known2.One);
2774 // Output known-1 are known to be set if set in only one of the LHS, RHS.
2775 Known.One = (Known.Zero & Known2.One) | (Known.One & Known2.Zero);
2776 Known.Zero = KnownZeroOut;
2777 break;
2778 }
2779 case ISD::MUL: {
2780 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
2781 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2782
2783 // If low bits are zero in either operand, output low known-0 bits.
2784 // Also compute a conservative estimate for high known-0 bits.
2785 // More trickiness is possible, but this is sufficient for the
2786 // interesting case of alignment computation.
2787 unsigned TrailZ = Known.countMinTrailingZeros() +
2788 Known2.countMinTrailingZeros();
2789 unsigned LeadZ = std::max(Known.countMinLeadingZeros() +
2790 Known2.countMinLeadingZeros(),
2791 BitWidth) - BitWidth;
2792
2793 Known.resetAll();
2794 Known.Zero.setLowBits(std::min(TrailZ, BitWidth));
2795 Known.Zero.setHighBits(std::min(LeadZ, BitWidth));
2796 break;
2797 }
2798 case ISD::UDIV: {
2799 // For the purposes of computing leading zeros we can conservatively
2800 // treat a udiv as a logical right shift by the power of 2 known to
2801 // be less than the denominator.
2802 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2803 unsigned LeadZ = Known2.countMinLeadingZeros();
2804
2805 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
2806 unsigned RHSMaxLeadingZeros = Known2.countMaxLeadingZeros();
2807 if (RHSMaxLeadingZeros != BitWidth)
2808 LeadZ = std::min(BitWidth, LeadZ + BitWidth - RHSMaxLeadingZeros - 1);
2809
2810 Known.Zero.setHighBits(LeadZ);
2811 break;
2812 }
2813 case ISD::SELECT:
2814 case ISD::VSELECT:
2815 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
2816 // If we don't know any bits, early out.
2817 if (Known.isUnknown())
2818 break;
2819 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
2820
2821 // Only known if known in both the LHS and RHS.
2822 Known.One &= Known2.One;
2823 Known.Zero &= Known2.Zero;
2824 break;
2825 case ISD::SELECT_CC:
2826 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
2827 // If we don't know any bits, early out.
2828 if (Known.isUnknown())
2829 break;
2830 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
2831
2832 // Only known if known in both the LHS and RHS.
2833 Known.One &= Known2.One;
2834 Known.Zero &= Known2.Zero;
2835 break;
2836 case ISD::SMULO:
2837 case ISD::UMULO:
2838 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
2839 if (Op.getResNo() != 1)
2840 break;
2841 // The boolean result conforms to getBooleanContents.
2842 // If we know the result of a setcc has the top bits zero, use this info.
2843 // We know that we have an integer-based boolean since these operations
2844 // are only available for integer.
2845 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2846 TargetLowering::ZeroOrOneBooleanContent &&
2847 BitWidth > 1)
2848 Known.Zero.setBitsFrom(1);
2849 break;
2850 case ISD::SETCC:
2851 case ISD::STRICT_FSETCC:
2852 case ISD::STRICT_FSETCCS: {
2853 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
2854 // If we know the result of a setcc has the top bits zero, use this info.
2855 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
2856 TargetLowering::ZeroOrOneBooleanContent &&
2857 BitWidth > 1)
2858 Known.Zero.setBitsFrom(1);
2859 break;
2860 }
2861 case ISD::SHL:
2862 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2863
2864 if (const APInt *ShAmt = getValidShiftAmountConstant(Op, DemandedElts)) {
2865 unsigned Shift = ShAmt->getZExtValue();
2866 Known.Zero <<= Shift;
2867 Known.One <<= Shift;
2868 // Low bits are known zero.
2869 Known.Zero.setLowBits(Shift);
2870 break;
2871 }
2872
2873 // No matter the shift amount, the trailing zeros will stay zero.
2874 Known.Zero = APInt::getLowBitsSet(BitWidth, Known.countMinTrailingZeros());
2875 Known.One.clearAllBits();
2876
2877 // Minimum shift low bits are known zero.
2878 if (const APInt *ShMinAmt =
2879 getValidMinimumShiftAmountConstant(Op, DemandedElts))
2880 Known.Zero.setLowBits(ShMinAmt->getZExtValue());
2881 break;
2882 case ISD::SRL:
2883 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2884
2885 if (const APInt *ShAmt = getValidShiftAmountConstant(Op, DemandedElts)) {
2886 unsigned Shift = ShAmt->getZExtValue();
2887 Known.Zero.lshrInPlace(Shift);
2888 Known.One.lshrInPlace(Shift);
2889 // High bits are known zero.
2890 Known.Zero.setHighBits(Shift);
2891 break;
2892 }
2893
2894 // No matter the shift amount, the leading zeros will stay zero.
2895 Known.Zero = APInt::getHighBitsSet(BitWidth, Known.countMinLeadingZeros());
2896 Known.One.clearAllBits();
2897
2898 // Minimum shift high bits are known zero.
2899 if (const APInt *ShMinAmt =
2900 getValidMinimumShiftAmountConstant(Op, DemandedElts))
2901 Known.Zero.setHighBits(ShMinAmt->getZExtValue());
2902 break;
2903 case ISD::SRA:
2904 if (const APInt *ShAmt = getValidShiftAmountConstant(Op, DemandedElts)) {
2905 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2906 unsigned Shift = ShAmt->getZExtValue();
2907 // Sign extend known zero/one bit (else is unknown).
2908 Known.Zero.ashrInPlace(Shift);
2909 Known.One.ashrInPlace(Shift);
2910 }
2911 break;
2912 case ISD::FSHL:
2913 case ISD::FSHR:
2914 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
2915 unsigned Amt = C->getAPIntValue().urem(BitWidth);
2916
2917 // For fshl, 0-shift returns the 1st arg.
2918 // For fshr, 0-shift returns the 2nd arg.
2919 if (Amt == 0) {
2920 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
2921 DemandedElts, Depth + 1);
2922 break;
2923 }
2924
2925 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
2926 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
2927 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2928 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
2929 if (Opcode == ISD::FSHL) {
2930 Known.One <<= Amt;
2931 Known.Zero <<= Amt;
2932 Known2.One.lshrInPlace(BitWidth - Amt);
2933 Known2.Zero.lshrInPlace(BitWidth - Amt);
2934 } else {
2935 Known.One <<= BitWidth - Amt;
2936 Known.Zero <<= BitWidth - Amt;
2937 Known2.One.lshrInPlace(Amt);
2938 Known2.Zero.lshrInPlace(Amt);
2939 }
2940 Known.One |= Known2.One;
2941 Known.Zero |= Known2.Zero;
2942 }
2943 break;
2944 case ISD::SIGN_EXTEND_INREG: {
2945 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2946 unsigned EBits = EVT.getScalarSizeInBits();
2947
2948 // Sign extension. Compute the demanded bits in the result that are not
2949 // present in the input.
2950 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
2951
2952 APInt InSignMask = APInt::getSignMask(EBits);
2953 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
2954
2955 // If the sign extended bits are demanded, we know that the sign
2956 // bit is demanded.
2957 InSignMask = InSignMask.zext(BitWidth);
2958 if (NewBits.getBoolValue())
2959 InputDemandedBits |= InSignMask;
2960
2961 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2962 Known.One &= InputDemandedBits;
2963 Known.Zero &= InputDemandedBits;
2964
2965 // If the sign bit of the input is known set or clear, then we know the
2966 // top bits of the result.
2967 if (Known.Zero.intersects(InSignMask)) { // Input sign bit known clear
2968 Known.Zero |= NewBits;
2969 Known.One &= ~NewBits;
2970 } else if (Known.One.intersects(InSignMask)) { // Input sign bit known set
2971 Known.One |= NewBits;
2972 Known.Zero &= ~NewBits;
2973 } else { // Input sign bit unknown
2974 Known.Zero &= ~NewBits;
2975 Known.One &= ~NewBits;
2976 }
2977 break;
2978 }
2979 case ISD::CTTZ:
2980 case ISD::CTTZ_ZERO_UNDEF: {
2981 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2982 // If we have a known 1, its position is our upper bound.
2983 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
2984 unsigned LowBits = Log2_32(PossibleTZ) + 1;
2985 Known.Zero.setBitsFrom(LowBits);
2986 break;
2987 }
2988 case ISD::CTLZ:
2989 case ISD::CTLZ_ZERO_UNDEF: {
2990 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2991 // If we have a known 1, its position is our upper bound.
2992 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
2993 unsigned LowBits = Log2_32(PossibleLZ) + 1;
2994 Known.Zero.setBitsFrom(LowBits);
2995 break;
2996 }
2997 case ISD::CTPOP: {
2998 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2999 // If we know some of the bits are zero, they can't be one.
3000 unsigned PossibleOnes = Known2.countMaxPopulation();
3001 Known.Zero.setBitsFrom(Log2_32(PossibleOnes) + 1);
3002 break;
3003 }
3004 case ISD::LOAD: {
3005 LoadSDNode *LD = cast<LoadSDNode>(Op);
3006 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3007 if (ISD::isNON_EXTLoad(LD) && Cst) {
3008 // Determine any common known bits from the loaded constant pool value.
3009 Type *CstTy = Cst->getType();
3010 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits()) {
3011 // If its a vector splat, then we can (quickly) reuse the scalar path.
3012 // NOTE: We assume all elements match and none are UNDEF.
3013 if (CstTy->isVectorTy()) {
3014 if (const Constant *Splat = Cst->getSplatValue()) {
3015 Cst = Splat;
3016 CstTy = Cst->getType();
3017 }
3018 }
3019 // TODO - do we need to handle different bitwidths?
3020 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3021 // Iterate across all vector elements finding common known bits.
3022 Known.One.setAllBits();
3023 Known.Zero.setAllBits();
3024 for (unsigned i = 0; i != NumElts; ++i) {
3025 if (!DemandedElts[i])
3026 continue;
3027 if (Constant *Elt = Cst->getAggregateElement(i)) {
3028 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3029 const APInt &Value = CInt->getValue();
3030 Known.One &= Value;
3031 Known.Zero &= ~Value;
3032 continue;
3033 }
3034 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3035 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3036 Known.One &= Value;
3037 Known.Zero &= ~Value;
3038 continue;
3039 }
3040 }
3041 Known.One.clearAllBits();
3042 Known.Zero.clearAllBits();
3043 break;
3044 }
3045 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3046 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3047 const APInt &Value = CInt->getValue();
3048 Known.One = Value;
3049 Known.Zero = ~Value;
3050 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3051 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3052 Known.One = Value;
3053 Known.Zero = ~Value;
3054 }
3055 }
3056 }
3057 } else if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
3058 // If this is a ZEXTLoad and we are looking at the loaded value.
3059 EVT VT = LD->getMemoryVT();
3060 unsigned MemBits = VT.getScalarSizeInBits();
3061 Known.Zero.setBitsFrom(MemBits);
3062 } else if (const MDNode *Ranges = LD->getRanges()) {
3063 if (LD->getExtensionType() == ISD::NON_EXTLOAD)
3064 computeKnownBitsFromRangeMetadata(*Ranges, Known);
3065 }
3066 break;
3067 }
3068 case ISD::ZERO_EXTEND_VECTOR_INREG: {
3069 EVT InVT = Op.getOperand(0).getValueType();
3070 APInt InDemandedElts = DemandedElts.zextOrSelf(InVT.getVectorNumElements());
3071 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3072 Known = Known.zext(BitWidth);
3073 break;
3074 }
3075 case ISD::ZERO_EXTEND: {
3076 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3077 Known = Known.zext(BitWidth);
3078 break;
3079 }
3080 case ISD::SIGN_EXTEND_VECTOR_INREG: {
3081 EVT InVT = Op.getOperand(0).getValueType();
3082 APInt InDemandedElts = DemandedElts.zextOrSelf(InVT.getVectorNumElements());
3083 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3084 // If the sign bit is known to be zero or one, then sext will extend
3085 // it to the top bits, else it will just zext.
3086 Known = Known.sext(BitWidth);
3087 break;
3088 }
3089 case ISD::SIGN_EXTEND: {
3090 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3091 // If the sign bit is known to be zero or one, then sext will extend
3092 // it to the top bits, else it will just zext.
3093 Known = Known.sext(BitWidth);
3094 break;
3095 }
3096 case ISD::ANY_EXTEND_VECTOR_INREG: {
3097 EVT InVT = Op.getOperand(0).getValueType();
3098 APInt InDemandedElts = DemandedElts.zextOrSelf(InVT.getVectorNumElements());
3099 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3100 Known = Known.anyext(BitWidth);
3101 break;
3102 }
3103 case ISD::ANY_EXTEND: {
3104 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3105 Known = Known.anyext(BitWidth);
3106 break;
3107 }
3108 case ISD::TRUNCATE: {
3109 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3110 Known = Known.trunc(BitWidth);
3111 break;
3112 }
3113 case ISD::AssertZext: {
3114 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3115 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
3116 Known = computeKnownBits(Op.getOperand(0), Depth+1);
3117 Known.Zero |= (~InMask);
3118 Known.One &= (~Known.Zero);
3119 break;
3120 }
3121 case ISD::FGETSIGN:
3122 // All bits are zero except the low bit.
3123 Known.Zero.setBitsFrom(1);
3124 break;
3125 case ISD::USUBO:
3126 case ISD::SSUBO:
3127 if (Op.getResNo() == 1) {
3128 // If we know the result of a setcc has the top bits zero, use this info.
3129 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3130 TargetLowering::ZeroOrOneBooleanContent &&
3131 BitWidth > 1)
3132 Known.Zero.setBitsFrom(1);
3133 break;
3134 }
3135 LLVM_FALLTHROUGH[[gnu::fallthrough]];
3136 case ISD::SUB:
3137 case ISD::SUBC: {
3138 assert(Op.getResNo() == 0 &&((Op.getResNo() == 0 && "We only compute knownbits for the difference here."
) ? static_cast<void> (0) : __assert_fail ("Op.getResNo() == 0 && \"We only compute knownbits for the difference here.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 3139, __PRETTY_FUNCTION__))
3139 "We only compute knownbits for the difference here.")((Op.getResNo() == 0 && "We only compute knownbits for the difference here."
) ? static_cast<void> (0) : __assert_fail ("Op.getResNo() == 0 && \"We only compute knownbits for the difference here.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 3139, __PRETTY_FUNCTION__))
;
3140
3141 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3142 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3143 Known = KnownBits::computeForAddSub(/* Add */ false, /* NSW */ false,
3144 Known, Known2);
3145 break;
3146 }
3147 case ISD::UADDO:
3148 case ISD::SADDO:
3149 case ISD::ADDCARRY:
3150 if (Op.getResNo() == 1) {
3151 // If we know the result of a setcc has the top bits zero, use this info.
3152 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3153 TargetLowering::ZeroOrOneBooleanContent &&
3154 BitWidth > 1)
3155 Known.Zero.setBitsFrom(1);
3156 break;
3157 }
3158 LLVM_FALLTHROUGH[[gnu::fallthrough]];
3159 case ISD::ADD:
3160 case ISD::ADDC:
3161 case ISD::ADDE: {
3162 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.")((Op.getResNo() == 0 && "We only compute knownbits for the sum here."
) ? static_cast<void> (0) : __assert_fail ("Op.getResNo() == 0 && \"We only compute knownbits for the sum here.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 3162, __PRETTY_FUNCTION__))
;
3163
3164 // With ADDE and ADDCARRY, a carry bit may be added in.
3165 KnownBits Carry(1);
3166 if (Opcode == ISD::ADDE)
3167 // Can't track carry from glue, set carry to unknown.
3168 Carry.resetAll();
3169 else if (Opcode == ISD::ADDCARRY)
3170 // TODO: Compute known bits for the carry operand. Not sure if it is worth
3171 // the trouble (how often will we find a known carry bit). And I haven't
3172 // tested this very much yet, but something like this might work:
3173 // Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3174 // Carry = Carry.zextOrTrunc(1, false);
3175 Carry.resetAll();
3176 else
3177 Carry.setAllZero();
3178
3179 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3180 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3181 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
3182 break;
3183 }
3184 case ISD::SREM:
3185 if (ConstantSDNode *Rem = isConstOrConstSplat(Op.getOperand(1))) {
3186 const APInt &RA = Rem->getAPIntValue().abs();
3187 if (RA.isPowerOf2()) {
3188 APInt LowBits = RA - 1;
3189 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3190
3191 // The low bits of the first operand are unchanged by the srem.
3192 Known.Zero = Known2.Zero & LowBits;
3193 Known.One = Known2.One & LowBits;
3194
3195 // If the first operand is non-negative or has all low bits zero, then
3196 // the upper bits are all zero.
3197 if (Known2.isNonNegative() || LowBits.isSubsetOf(Known2.Zero))
3198 Known.Zero |= ~LowBits;
3199
3200 // If the first operand is negative and not all low bits are zero, then
3201 // the upper bits are all one.
3202 if (Known2.isNegative() && LowBits.intersects(Known2.One))
3203 Known.One |= ~LowBits;
3204 assert((Known.Zero & Known.One) == 0&&"Bits known to be one AND zero?")(((Known.Zero & Known.One) == 0&&"Bits known to be one AND zero?"
) ? static_cast<void> (0) : __assert_fail ("(Known.Zero & Known.One) == 0&&\"Bits known to be one AND zero?\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 3204, __PRETTY_FUNCTION__))
;
3205 }
3206 }
3207 break;
3208 case ISD::UREM: {
3209 if (ConstantSDNode *Rem = isConstOrConstSplat(Op.getOperand(1))) {
3210 const APInt &RA = Rem->getAPIntValue();
3211 if (RA.isPowerOf2()) {
3212 APInt LowBits = (RA - 1);
3213 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3214
3215 // The upper bits are all zero, the lower ones are unchanged.
3216 Known.Zero = Known2.Zero | ~LowBits;
3217 Known.One = Known2.One & LowBits;
3218 break;
3219 }
3220 }
3221
3222 // Since the result is less than or equal to either operand, any leading
3223 // zero bits in either operand must also exist in the result.
3224 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3225 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3226
3227 uint32_t Leaders =
3228 std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros());
3229 Known.resetAll();
3230 Known.Zero.setHighBits(Leaders);
3231 break;
3232 }
3233 case ISD::EXTRACT_ELEMENT: {
3234 Known = computeKnownBits(Op.getOperand(0), Depth+1);
3235 const unsigned Index = Op.getConstantOperandVal(1);
3236 const unsigned EltBitWidth = Op.getValueSizeInBits();
3237
3238 // Remove low part of known bits mask
3239 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3240 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3241
3242 // Remove high part of known bit mask
3243 Known = Known.trunc(EltBitWidth);
3244 break;
3245 }
3246 case ISD::EXTRACT_VECTOR_ELT: {
3247 SDValue InVec = Op.getOperand(0);
3248 SDValue EltNo = Op.getOperand(1);
3249 EVT VecVT = InVec.getValueType();
3250 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
3251 const unsigned NumSrcElts = VecVT.getVectorNumElements();
3252
3253 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
3254 // anything about the extended bits.
3255 if (BitWidth > EltBitWidth)
3256 Known = Known.trunc(EltBitWidth);
3257
3258 // If we know the element index, just demand that vector element, else for
3259 // an unknown element index, ignore DemandedElts and demand them all.
3260 APInt DemandedSrcElts = APInt::getAllOnesValue(NumSrcElts);
3261 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
3262 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
3263 DemandedSrcElts =
3264 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
3265
3266 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
3267 if (BitWidth > EltBitWidth)
3268 Known = Known.anyext(BitWidth);
3269 break;
3270 }
3271 case ISD::INSERT_VECTOR_ELT: {
3272 // If we know the element index, split the demand between the
3273 // source vector and the inserted element, otherwise assume we need
3274 // the original demanded vector elements and the value.
3275 SDValue InVec = Op.getOperand(0);
3276 SDValue InVal = Op.getOperand(1);
3277 SDValue EltNo = Op.getOperand(2);
3278 bool DemandedVal = true;
3279 APInt DemandedVecElts = DemandedElts;
3280 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
3281 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
3282 unsigned EltIdx = CEltNo->getZExtValue();
3283 DemandedVal = !!DemandedElts[EltIdx];
3284 DemandedVecElts.clearBit(EltIdx);
3285 }
3286 Known.One.setAllBits();
3287 Known.Zero.setAllBits();
3288 if (DemandedVal) {
3289 Known2 = computeKnownBits(InVal, Depth + 1);
3290 Known.One &= Known2.One.zextOrTrunc(BitWidth);
3291 Known.Zero &= Known2.Zero.zextOrTrunc(BitWidth);
3292 }
3293 if (!!DemandedVecElts) {
3294 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
3295 Known.One &= Known2.One;
3296 Known.Zero &= Known2.Zero;
3297 }
3298 break;
3299 }
3300 case ISD::BITREVERSE: {
3301 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3302 Known.Zero = Known2.Zero.reverseBits();
3303 Known.One = Known2.One.reverseBits();
3304 break;
3305 }
3306 case ISD::BSWAP: {
3307 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3308 Known.Zero = Known2.Zero.byteSwap();
3309 Known.One = Known2.One.byteSwap();
3310 break;
3311 }
3312 case ISD::ABS: {
3313 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3314
3315 // If the source's MSB is zero then we know the rest of the bits already.
3316 if (Known2.isNonNegative()) {
3317 Known.Zero = Known2.Zero;
3318 Known.One = Known2.One;
3319 break;
3320 }
3321
3322 // We only know that the absolute values's MSB will be zero iff there is
3323 // a set bit that isn't the sign bit (otherwise it could be INT_MIN).
3324 Known2.One.clearSignBit();
3325 if (Known2.One.getBoolValue()) {
3326 Known.Zero = APInt::getSignMask(BitWidth);
3327 break;
3328 }
3329 break;
3330 }
3331 case ISD::UMIN: {
3332 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3333 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3334
3335 // UMIN - we know that the result will have the maximum of the
3336 // known zero leading bits of the inputs.
3337 unsigned LeadZero = Known.countMinLeadingZeros();
3338 LeadZero = std::max(LeadZero, Known2.countMinLeadingZeros());
3339
3340 Known.Zero &= Known2.Zero;
3341 Known.One &= Known2.One;
3342 Known.Zero.setHighBits(LeadZero);
3343 break;
3344 }
3345 case ISD::UMAX: {
3346 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3347 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3348
3349 // UMAX - we know that the result will have the maximum of the
3350 // known one leading bits of the inputs.
3351 unsigned LeadOne = Known.countMinLeadingOnes();
3352 LeadOne = std::max(LeadOne, Known2.countMinLeadingOnes());
3353
3354 Known.Zero &= Known2.Zero;
3355 Known.One &= Known2.One;
3356 Known.One.setHighBits(LeadOne);
3357 break;
3358 }
3359 case ISD::SMIN:
3360 case ISD::SMAX: {
3361 // If we have a clamp pattern, we know that the number of sign bits will be
3362 // the minimum of the clamp min/max range.
3363 bool IsMax = (Opcode == ISD::SMAX);
3364 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
3365 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
3366 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
3367 CstHigh =
3368 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
3369 if (CstLow && CstHigh) {
3370 if (!IsMax)
3371 std::swap(CstLow, CstHigh);
3372
3373 const APInt &ValueLow = CstLow->getAPIntValue();
3374 const APInt &ValueHigh = CstHigh->getAPIntValue();
3375 if (ValueLow.sle(ValueHigh)) {
3376 unsigned LowSignBits = ValueLow.getNumSignBits();
3377 unsigned HighSignBits = ValueHigh.getNumSignBits();
3378 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
3379 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
3380 Known.One.setHighBits(MinSignBits);
3381 break;
3382 }
3383 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
3384 Known.Zero.setHighBits(MinSignBits);
3385 break;
3386 }
3387 }
3388 }
3389
3390 // Fallback - just get the shared known bits of the operands.
3391 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3392 if (Known.isUnknown()) break; // Early-out
3393 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3394 Known.Zero &= Known2.Zero;
3395 Known.One &= Known2.One;
3396 break;
3397 }
3398 case ISD::FrameIndex:
3399 case ISD::TargetFrameIndex:
3400 TLI->computeKnownBitsForFrameIndex(Op, Known, DemandedElts, *this, Depth);
3401 break;
3402
3403 default:
3404 if (Opcode < ISD::BUILTIN_OP_END)
3405 break;
3406 LLVM_FALLTHROUGH[[gnu::fallthrough]];
3407 case ISD::INTRINSIC_WO_CHAIN:
3408 case ISD::INTRINSIC_W_CHAIN:
3409 case ISD::INTRINSIC_VOID:
3410 // Allow the target to implement this method for its nodes.
3411 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
3412 break;
3413 }
3414
3415 assert(!Known.hasConflict() && "Bits known to be one AND zero?")((!Known.hasConflict() && "Bits known to be one AND zero?"
) ? static_cast<void> (0) : __assert_fail ("!Known.hasConflict() && \"Bits known to be one AND zero?\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 3415, __PRETTY_FUNCTION__))
;
3416 return Known;
3417}
3418
3419SelectionDAG::OverflowKind SelectionDAG::computeOverflowKind(SDValue N0,
3420 SDValue N1) const {
3421 // X + 0 never overflow
3422 if (isNullConstant(N1))
3423 return OFK_Never;
3424
3425 KnownBits N1Known = computeKnownBits(N1);
3426 if (N1Known.Zero.getBoolValue()) {
3427 KnownBits N0Known = computeKnownBits(N0);
3428
3429 bool overflow;
3430 (void)N0Known.getMaxValue().uadd_ov(N1Known.getMaxValue(), overflow);
3431 if (!overflow)
3432 return OFK_Never;
3433 }
3434
3435 // mulhi + 1 never overflow
3436 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
3437 (N1Known.getMaxValue() & 0x01) == N1Known.getMaxValue())
3438 return OFK_Never;
3439
3440 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1) {
3441 KnownBits N0Known = computeKnownBits(N0);
3442
3443 if ((N0Known.getMaxValue() & 0x01) == N0Known.getMaxValue())
3444 return OFK_Never;
3445 }
3446
3447 return OFK_Sometime;
3448}
3449
3450bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val) const {
3451 EVT OpVT = Val.getValueType();
3452 unsigned BitWidth = OpVT.getScalarSizeInBits();
3453
3454 // Is the constant a known power of 2?
3455 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(Val))
3456 return Const->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
3457
3458 // A left-shift of a constant one will have exactly one bit set because
3459 // shifting the bit off the end is undefined.
3460 if (Val.getOpcode() == ISD::SHL) {
3461 auto *C = isConstOrConstSplat(Val.getOperand(0));
3462 if (C && C->getAPIntValue() == 1)
3463 return true;
3464 }
3465
3466 // Similarly, a logical right-shift of a constant sign-bit will have exactly
3467 // one bit set.
3468 if (Val.getOpcode() == ISD::SRL) {
3469 auto *C = isConstOrConstSplat(Val.getOperand(0));
3470 if (C && C->getAPIntValue().isSignMask())
3471 return true;
3472 }
3473
3474 // Are all operands of a build vector constant powers of two?
3475 if (Val.getOpcode() == ISD::BUILD_VECTOR)
3476 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
3477 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
3478 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
3479 return false;
3480 }))
3481 return true;
3482
3483 // More could be done here, though the above checks are enough
3484 // to handle some common cases.
3485
3486 // Fall back to computeKnownBits to catch other known cases.
3487 KnownBits Known = computeKnownBits(Val);
3488 return (Known.countMaxPopulation() == 1) && (Known.countMinPopulation() == 1);
3489}
3490
3491unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const {
3492 EVT VT = Op.getValueType();
3493 APInt DemandedElts = VT.isVector()
3494 ? APInt::getAllOnesValue(VT.getVectorNumElements())
3495 : APInt(1, 1);
3496 return ComputeNumSignBits(Op, DemandedElts, Depth);
3497}
3498
3499unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
3500 unsigned Depth) const {
3501 EVT VT = Op.getValueType();
3502 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!")(((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!"
) ? static_cast<void> (0) : __assert_fail ("(VT.isInteger() || VT.isFloatingPoint()) && \"Invalid VT!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 3502, __PRETTY_FUNCTION__))
;
3503 unsigned VTBits = VT.getScalarSizeInBits();
3504 unsigned NumElts = DemandedElts.getBitWidth();
3505 unsigned Tmp, Tmp2;
3506 unsigned FirstAnswer = 1;
3507
3508 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3509 const APInt &Val = C->getAPIntValue();
3510 return Val.getNumSignBits();
3511 }
3512
3513 if (Depth >= MaxRecursionDepth)
3514 return 1; // Limit search depth.
3515
3516 if (!DemandedElts)
3517 return 1; // No demanded elts, better to assume we don't know anything.
3518
3519 unsigned Opcode = Op.getOpcode();
3520 switch (Opcode) {
3521 default: break;
3522 case ISD::AssertSext:
3523 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
3524 return VTBits-Tmp+1;
3525 case ISD::AssertZext:
3526 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
3527 return VTBits-Tmp;
3528
3529 case ISD::BUILD_VECTOR:
3530 Tmp = VTBits;
3531 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
3532 if (!DemandedElts[i])
3533 continue;
3534
3535 SDValue SrcOp = Op.getOperand(i);
3536 Tmp2 = ComputeNumSignBits(Op.getOperand(i), Depth + 1);
3537
3538 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3539 if (SrcOp.getValueSizeInBits() != VTBits) {
3540 assert(SrcOp.getValueSizeInBits() > VTBits &&((SrcOp.getValueSizeInBits() > VTBits && "Expected BUILD_VECTOR implicit truncation"
) ? static_cast<void> (0) : __assert_fail ("SrcOp.getValueSizeInBits() > VTBits && \"Expected BUILD_VECTOR implicit truncation\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 3541, __PRETTY_FUNCTION__))
3541 "Expected BUILD_VECTOR implicit truncation")((SrcOp.getValueSizeInBits() > VTBits && "Expected BUILD_VECTOR implicit truncation"
) ? static_cast<void> (0) : __assert_fail ("SrcOp.getValueSizeInBits() > VTBits && \"Expected BUILD_VECTOR implicit truncation\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 3541, __PRETTY_FUNCTION__))
;
3542 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
3543 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
3544 }
3545 Tmp = std::min(Tmp, Tmp2);
3546 }
3547 return Tmp;
3548
3549 case ISD::VECTOR_SHUFFLE: {
3550 // Collect the minimum number of sign bits that are shared by every vector
3551 // element referenced by the shuffle.
3552 APInt DemandedLHS(NumElts, 0), DemandedRHS(NumElts, 0);
3553 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
3554 assert(NumElts == SVN->getMask().size() && "Unexpected vector size")((NumElts == SVN->getMask().size() && "Unexpected vector size"
) ? static_cast<void> (0) : __assert_fail ("NumElts == SVN->getMask().size() && \"Unexpected vector size\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 3554, __PRETTY_FUNCTION__))
;
3555 for (unsigned i = 0; i != NumElts; ++i) {
3556 int M = SVN->getMaskElt(i);
3557 if (!DemandedElts[i])
3558 continue;
3559 // For UNDEF elements, we don't know anything about the common state of
3560 // the shuffle result.
3561 if (M < 0)
3562 return 1;
3563 if ((unsigned)M < NumElts)
3564 DemandedLHS.setBit((unsigned)M % NumElts);
3565 else
3566 DemandedRHS.setBit((unsigned)M % NumElts);
3567 }
3568 Tmp = std::numeric_limits<unsigned>::max();
3569 if (!!DemandedLHS)
3570 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
3571 if (!!DemandedRHS) {
3572 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
3573 Tmp = std::min(Tmp, Tmp2);
3574 }
3575 // If we don't know anything, early out and try computeKnownBits fall-back.
3576 if (Tmp == 1)
3577 break;
3578 assert(Tmp <= VTBits && "Failed to determine minimum sign bits")((Tmp <= VTBits && "Failed to determine minimum sign bits"
) ? static_cast<void> (0) : __assert_fail ("Tmp <= VTBits && \"Failed to determine minimum sign bits\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 3578, __PRETTY_FUNCTION__))
;
3579 return Tmp;
3580 }
3581
3582 case ISD::BITCAST: {
3583 SDValue N0 = Op.getOperand(0);
3584 EVT SrcVT = N0.getValueType();
3585 unsigned SrcBits = SrcVT.getScalarSizeInBits();
3586
3587 // Ignore bitcasts from unsupported types..
3588 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
3589 break;
3590
3591 // Fast handling of 'identity' bitcasts.
3592 if (VTBits == SrcBits)
3593 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
3594
3595 bool IsLE = getDataLayout().isLittleEndian();
3596
3597 // Bitcast 'large element' scalar/vector to 'small element' vector.
3598 if ((SrcBits % VTBits) == 0) {
3599 assert(VT.isVector() && "Expected bitcast to vector")((VT.isVector() && "Expected bitcast to vector") ? static_cast
<void> (0) : __assert_fail ("VT.isVector() && \"Expected bitcast to vector\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 3599, __PRETTY_FUNCTION__))
;
3600
3601 unsigned Scale = SrcBits / VTBits;
3602 APInt SrcDemandedElts(NumElts / Scale, 0);
3603 for (unsigned i = 0; i != NumElts; ++i)
3604 if (DemandedElts[i])
3605 SrcDemandedElts.setBit(i / Scale);
3606
3607 // Fast case - sign splat can be simply split across the small elements.
3608 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
3609 if (Tmp == SrcBits)
3610 return VTBits;
3611
3612 // Slow case - determine how far the sign extends into each sub-element.
3613 Tmp2 = VTBits;
3614 for (unsigned i = 0; i != NumElts; ++i)
3615 if (DemandedElts[i]) {
3616 unsigned SubOffset = i % Scale;
3617 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
3618 SubOffset = SubOffset * VTBits;
3619 if (Tmp <= SubOffset)
3620 return 1;
3621 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
3622 }
3623 return Tmp2;
3624 }
3625 break;
3626 }
3627
3628 case ISD::SIGN_EXTEND:
3629 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
3630 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
3631 case ISD::SIGN_EXTEND_INREG:
3632 // Max of the input and what this extends.
3633 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
3634 Tmp = VTBits-Tmp+1;
3635 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
3636 return std::max(Tmp, Tmp2);
3637 case ISD::SIGN_EXTEND_VECTOR_INREG: {
3638 SDValue Src = Op.getOperand(0);
3639 EVT SrcVT = Src.getValueType();
3640 APInt DemandedSrcElts = DemandedElts.zextOrSelf(SrcVT.getVectorNumElements());
3641 Tmp = VTBits - SrcVT.getScalarSizeInBits();
3642 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
3643 }
3644 case ISD::SRA:
3645 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3646 // SRA X, C -> adds C sign bits.
3647 if (const APInt *ShAmt = getValidShiftAmountConstant(Op, DemandedElts))
3648 Tmp = std::min<uint64_t>(Tmp + ShAmt->getZExtValue(), VTBits);
3649 else if (const APInt *ShAmt =
3650 getValidMinimumShiftAmountConstant(Op, DemandedElts))
3651 Tmp = std::min<uint64_t>(Tmp + ShAmt->getZExtValue(), VTBits);
3652 return Tmp;
3653 case ISD::SHL:
3654 if (const APInt *ShAmt = getValidShiftAmountConstant(Op, DemandedElts)) {
3655 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
3656 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3657 if (ShAmt->ult(Tmp))
3658 return Tmp - ShAmt->getZExtValue();
3659 } else if (const APInt *ShAmt =
3660 getValidMaximumShiftAmountConstant(Op, DemandedElts)) {
3661 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3662 if (ShAmt->ult(Tmp))
3663 return Tmp - ShAmt->getZExtValue();
3664 }
3665 break;
3666 case ISD::AND:
3667 case ISD::OR:
3668 case ISD::XOR: // NOT is handled here.
3669 // Logical binary ops preserve the number of sign bits at the worst.
3670 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
3671 if (Tmp != 1) {
3672 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
3673 FirstAnswer = std::min(Tmp, Tmp2);
3674 // We computed what we know about the sign bits as our first
3675 // answer. Now proceed to the generic code that uses
3676 // computeKnownBits, and pick whichever answer is better.
3677 }
3678 break;
3679
3680 case ISD::SELECT:
3681 case ISD::VSELECT:
3682 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
3683 if (Tmp == 1) return 1; // Early out.
3684 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
3685 return std::min(Tmp, Tmp2);
3686 case ISD::SELECT_CC:
3687 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
3688 if (Tmp == 1) return 1; // Early out.
3689 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
3690 return std::min(Tmp, Tmp2);
3691
3692 case ISD::SMIN:
3693 case ISD::SMAX: {
3694 // If we have a clamp pattern, we know that the number of sign bits will be
3695 // the minimum of the clamp min/max range.
3696 bool IsMax = (Opcode == ISD::SMAX);
3697 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
3698 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
3699 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
3700 CstHigh =
3701 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
3702 if (CstLow && CstHigh) {
3703 if (!IsMax)
3704 std::swap(CstLow, CstHigh);
3705 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
3706 Tmp = CstLow->getAPIntValue().getNumSignBits();
3707 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
3708 return std::min(Tmp, Tmp2);
3709 }
3710 }
3711
3712 // Fallback - just get the minimum number of sign bits of the operands.
3713 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3714 if (Tmp == 1)
3715 return 1; // Early out.
3716 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3717 return std::min(Tmp, Tmp2);
3718 }
3719 case ISD::UMIN:
3720 case ISD::UMAX:
3721 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3722 if (Tmp == 1)
3723 return 1; // Early out.
3724 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3725 return std::min(Tmp, Tmp2);
3726 case ISD::SADDO:
3727 case ISD::UADDO:
3728 case ISD::SSUBO:
3729 case ISD::USUBO:
3730 case ISD::SMULO:
3731 case ISD::UMULO:
3732 if (Op.getResNo() != 1)
3733 break;
3734 // The boolean result conforms to getBooleanContents. Fall through.
3735 // If setcc returns 0/-1, all bits are sign bits.
3736 // We know that we have an integer-based boolean since these operations
3737 // are only available for integer.
3738 if (TLI->getBooleanContents(VT.isVector(), false) ==
3739 TargetLowering::ZeroOrNegativeOneBooleanContent)
3740 return VTBits;
3741 break;
3742 case ISD::SETCC:
3743 case ISD::STRICT_FSETCC:
3744 case ISD::STRICT_FSETCCS: {
3745 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3746 // If setcc returns 0/-1, all bits are sign bits.
3747 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3748 TargetLowering::ZeroOrNegativeOneBooleanContent)
3749 return VTBits;
3750 break;
3751 }
3752 case ISD::ROTL:
3753 case ISD::ROTR:
3754 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3755
3756 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
3757 if (Tmp == VTBits)
3758 return VTBits;
3759
3760 if (ConstantSDNode *C =
3761 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3762 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
3763
3764 // Handle rotate right by N like a rotate left by 32-N.
3765 if (Opcode == ISD::ROTR)
3766 RotAmt = (VTBits - RotAmt) % VTBits;
3767
3768 // If we aren't rotating out all of the known-in sign bits, return the
3769 // number that are left. This handles rotl(sext(x), 1) for example.
3770 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
3771 }
3772 break;
3773 case ISD::ADD:
3774 case ISD::ADDC:
3775 // Add can have at most one carry bit. Thus we know that the output
3776 // is, at worst, one more bit than the inputs.
3777 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3778 if (Tmp == 1) return 1; // Early out.
3779
3780 // Special case decrementing a value (ADD X, -1):
3781 if (ConstantSDNode *CRHS =
3782 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
3783 if (CRHS->isAllOnesValue()) {
3784 KnownBits Known =
3785 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3786
3787 // If the input is known to be 0 or 1, the output is 0/-1, which is all
3788 // sign bits set.
3789 if ((Known.Zero | 1).isAllOnesValue())
3790 return VTBits;
3791
3792 // If we are subtracting one from a positive number, there is no carry
3793 // out of the result.
3794 if (Known.isNonNegative())
3795 return Tmp;
3796 }
3797
3798 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3799 if (Tmp2 == 1) return 1; // Early out.
3800 return std::min(Tmp, Tmp2) - 1;
3801 case ISD::SUB:
3802 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3803 if (Tmp2 == 1) return 1; // Early out.
3804
3805 // Handle NEG.
3806 if (ConstantSDNode *CLHS =
3807 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
3808 if (CLHS->isNullValue()) {
3809 KnownBits Known =
3810 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3811 // If the input is known to be 0 or 1, the output is 0/-1, which is all
3812 // sign bits set.
3813 if ((Known.Zero | 1).isAllOnesValue())
3814 return VTBits;
3815
3816 // If the input is known to be positive (the sign bit is known clear),
3817 // the output of the NEG has the same number of sign bits as the input.
3818 if (Known.isNonNegative())
3819 return Tmp2;
3820
3821 // Otherwise, we treat this like a SUB.
3822 }
3823
3824 // Sub can have at most one carry bit. Thus we know that the output
3825 // is, at worst, one more bit than the inputs.
3826 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3827 if (Tmp == 1) return 1; // Early out.
3828 return std::min(Tmp, Tmp2) - 1;
3829 case ISD::MUL: {
3830 // The output of the Mul can be at most twice the valid bits in the inputs.
3831 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
3832 if (SignBitsOp0 == 1)
3833 break;
3834 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
3835 if (SignBitsOp1 == 1)
3836 break;
3837 unsigned OutValidBits =
3838 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
3839 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
3840 }
3841 case ISD::TRUNCATE: {
3842 // Check if the sign bits of source go down as far as the truncated value.
3843 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
3844 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
3845 if (NumSrcSignBits > (NumSrcBits - VTBits))
3846 return NumSrcSignBits - (NumSrcBits - VTBits);
3847 break;
3848 }
3849 case ISD::EXTRACT_ELEMENT: {
3850 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
3851 const int BitWidth = Op.getValueSizeInBits();
3852 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
3853
3854 // Get reverse index (starting from 1), Op1 value indexes elements from
3855 // little end. Sign starts at big end.
3856 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
3857
3858 // If the sign portion ends in our element the subtraction gives correct
3859 // result. Otherwise it gives either negative or > bitwidth result
3860 return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
3861 }
3862 case ISD::INSERT_VECTOR_ELT: {
3863 // If we know the element index, split the demand between the
3864 // source vector and the inserted element, otherwise assume we need
3865 // the original demanded vector elements and the value.
3866 SDValue InVec = Op.getOperand(0);
3867 SDValue InVal = Op.getOperand(1);
3868 SDValue EltNo = Op.getOperand(2);
3869 bool DemandedVal = true;
3870 APInt DemandedVecElts = DemandedElts;
3871 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
3872 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
3873 unsigned EltIdx = CEltNo->getZExtValue();
3874 DemandedVal = !!DemandedElts[EltIdx];
3875 DemandedVecElts.clearBit(EltIdx);
3876 }
3877 Tmp = std::numeric_limits<unsigned>::max();
3878 if (DemandedVal) {
3879 // TODO - handle implicit truncation of inserted elements.
3880 if (InVal.getScalarValueSizeInBits() != VTBits)
3881 break;
3882 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
3883 Tmp = std::min(Tmp, Tmp2);
3884 }
3885 if (!!DemandedVecElts) {
3886 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
3887 Tmp = std::min(Tmp, Tmp2);
3888 }
3889 assert(Tmp <= VTBits && "Failed to determine minimum sign bits")((Tmp <= VTBits && "Failed to determine minimum sign bits"
) ? static_cast<void> (0) : __assert_fail ("Tmp <= VTBits && \"Failed to determine minimum sign bits\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 3889, __PRETTY_FUNCTION__))
;
3890 return Tmp;
3891 }
3892 case ISD::EXTRACT_VECTOR_ELT: {
3893 SDValue InVec = Op.getOperand(0);
3894 SDValue EltNo = Op.getOperand(1);
3895 EVT VecVT = InVec.getValueType();
3896 const unsigned BitWidth = Op.getValueSizeInBits();
3897 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
3898 const unsigned NumSrcElts = VecVT.getVectorNumElements();
3899
3900 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
3901 // anything about sign bits. But if the sizes match we can derive knowledge
3902 // about sign bits from the vector operand.
3903 if (BitWidth != EltBitWidth)
3904 break;
3905
3906 // If we know the element index, just demand that vector element, else for
3907 // an unknown element index, ignore DemandedElts and demand them all.
3908 APInt DemandedSrcElts = APInt::getAllOnesValue(NumSrcElts);
3909 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
3910 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
3911 DemandedSrcElts =
3912 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
3913
3914 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
3915 }
3916 case ISD::EXTRACT_SUBVECTOR: {
3917 // If we know the element index, just demand that subvector elements,
3918 // otherwise demand them all.
3919 SDValue Src = Op.getOperand(0);
3920 ConstantSDNode *SubIdx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
3921 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3922 APInt DemandedSrc = APInt::getAllOnesValue(NumSrcElts);
3923 if (SubIdx && SubIdx->getAPIntValue().ule(NumSrcElts - NumElts)) {
3924 // Offset the demanded elts by the subvector index.
3925 uint64_t Idx = SubIdx->getZExtValue();
3926 DemandedSrc = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx);
3927 }
3928 return ComputeNumSignBits(Src, DemandedSrc, Depth + 1);
3929 }
3930 case ISD::CONCAT_VECTORS: {
3931 // Determine the minimum number of sign bits across all demanded
3932 // elts of the input vectors. Early out if the result is already 1.
3933 Tmp = std::numeric_limits<unsigned>::max();
3934 EVT SubVectorVT = Op.getOperand(0).getValueType();
3935 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3936 unsigned NumSubVectors = Op.getNumOperands();
3937 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
3938 APInt DemandedSub = DemandedElts.lshr(i * NumSubVectorElts);
3939 DemandedSub = DemandedSub.trunc(NumSubVectorElts);
3940 if (!DemandedSub)
3941 continue;
3942 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
3943 Tmp = std::min(Tmp, Tmp2);
3944 }
3945 assert(Tmp <= VTBits && "Failed to determine minimum sign bits")((Tmp <= VTBits && "Failed to determine minimum sign bits"
) ? static_cast<void> (0) : __assert_fail ("Tmp <= VTBits && \"Failed to determine minimum sign bits\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 3945, __PRETTY_FUNCTION__))
;
3946 return Tmp;
3947 }
3948 case ISD::INSERT_SUBVECTOR: {
3949 // If we know the element index, demand any elements from the subvector and
3950 // the remainder from the src its inserted into, otherwise assume we need
3951 // the original demanded base elements and ALL the inserted subvector
3952 // elements.
3953 SDValue Src = Op.getOperand(0);
3954 SDValue Sub = Op.getOperand(1);
3955 auto *SubIdx = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3956 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3957 APInt DemandedSubElts = APInt::getAllOnesValue(NumSubElts);
3958 APInt DemandedSrcElts = DemandedElts;
3959 if (SubIdx && SubIdx->getAPIntValue().ule(NumElts - NumSubElts)) {
3960 uint64_t Idx = SubIdx->getZExtValue();
3961 DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3962 DemandedSrcElts.insertBits(APInt::getNullValue(NumSubElts), Idx);
3963 }
3964 Tmp = std::numeric_limits<unsigned>::max();
3965 if (!!DemandedSubElts) {
3966 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
3967 if (Tmp == 1)
3968 return 1; // early-out
3969 }
3970 if (!!DemandedSrcElts) {
3971 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
3972 Tmp = std::min(Tmp, Tmp2);
3973 }
3974 assert(Tmp <= VTBits && "Failed to determine minimum sign bits")((Tmp <= VTBits && "Failed to determine minimum sign bits"
) ? static_cast<void> (0) : __assert_fail ("Tmp <= VTBits && \"Failed to determine minimum sign bits\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 3974, __PRETTY_FUNCTION__))
;
3975 return Tmp;
3976 }
3977 }
3978
3979 // If we are looking at the loaded value of the SDNode.
3980 if (Op.getResNo() == 0) {
3981 // Handle LOADX separately here. EXTLOAD case will fallthrough.
3982 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
3983 unsigned ExtType = LD->getExtensionType();
3984 switch (ExtType) {
3985 default: break;
3986 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
3987 Tmp = LD->getMemoryVT().getScalarSizeInBits();
3988 return VTBits - Tmp + 1;
3989 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
3990 Tmp = LD->getMemoryVT().getScalarSizeInBits();
3991 return VTBits - Tmp;
3992 case ISD::NON_EXTLOAD:
3993 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
3994 // We only need to handle vectors - computeKnownBits should handle
3995 // scalar cases.
3996 Type *CstTy = Cst->getType();
3997 if (CstTy->isVectorTy() &&
3998 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits()) {
3999 Tmp = VTBits;
4000 for (unsigned i = 0; i != NumElts; ++i) {
4001 if (!DemandedElts[i])
4002 continue;
4003 if (Constant *Elt = Cst->getAggregateElement(i)) {
4004 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
4005 const APInt &Value = CInt->getValue();
4006 Tmp = std::min(Tmp, Value.getNumSignBits());
4007 continue;
4008 }
4009 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
4010 APInt Value = CFP->getValueAPF().bitcastToAPInt();
4011 Tmp = std::min(Tmp, Value.getNumSignBits());
4012 continue;
4013 }
4014 }
4015 // Unknown type. Conservatively assume no bits match sign bit.
4016 return 1;
4017 }
4018 return Tmp;
4019 }
4020 }
4021 break;
4022 }
4023 }
4024 }
4025
4026 // Allow the target to implement this method for its nodes.
4027 if (Opcode >= ISD::BUILTIN_OP_END ||
4028 Opcode == ISD::INTRINSIC_WO_CHAIN ||
4029 Opcode == ISD::INTRINSIC_W_CHAIN ||
4030 Opcode == ISD::INTRINSIC_VOID) {
4031 unsigned NumBits =
4032 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
4033 if (NumBits > 1)
4034 FirstAnswer = std::max(FirstAnswer, NumBits);
4035 }
4036
4037 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4038 // use this information.
4039 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
4040
4041 APInt Mask;
4042 if (Known.isNonNegative()) { // sign bit is 0
4043 Mask = Known.Zero;
4044 } else if (Known.isNegative()) { // sign bit is 1;
4045 Mask = Known.One;
4046 } else {
4047 // Nothing known.
4048 return FirstAnswer;
4049 }
4050
4051 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
4052 // the number of identical bits in the top of the input value.
4053 Mask = ~Mask;
4054 Mask <<= Mask.getBitWidth()-VTBits;
4055 // Return # leading zeros. We use 'min' here in case Val was zero before
4056 // shifting. We don't want to return '64' as for an i32 "0".
4057 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
4058}
4059
4060bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
4061 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
4062 !isa<ConstantSDNode>(Op.getOperand(1)))
4063 return false;
4064
4065 if (Op.getOpcode() == ISD::OR &&
4066 !MaskedValueIsZero(Op.getOperand(0), Op.getConstantOperandAPInt(1)))
4067 return false;
4068
4069 return true;
4070}
4071
4072bool SelectionDAG::isKnownNeverNaN(SDValue Op, bool SNaN, unsigned Depth) const {
4073 // If we're told that NaNs won't happen, assume they won't.
4074 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
4075 return true;
4076
4077 if (Depth >= MaxRecursionDepth)
4078 return false; // Limit search depth.
4079
4080 // TODO: Handle vectors.
4081 // If the value is a constant, we can obviously see if it is a NaN or not.
4082 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
4083 return !C->getValueAPF().isNaN() ||
4084 (SNaN && !C->getValueAPF().isSignaling());
4085 }
4086
4087 unsigned Opcode = Op.getOpcode();
4088 switch (Opcode) {
4089 case ISD::FADD:
4090 case ISD::FSUB:
4091 case ISD::FMUL:
4092 case ISD::FDIV:
4093 case ISD::FREM:
4094 case ISD::FSIN:
4095 case ISD::FCOS: {
4096 if (SNaN)
4097 return true;
4098 // TODO: Need isKnownNeverInfinity
4099 return false;
4100 }
4101 case ISD::FCANONICALIZE:
4102 case ISD::FEXP:
4103 case ISD::FEXP2:
4104 case ISD::FTRUNC:
4105 case ISD::FFLOOR:
4106 case ISD::FCEIL:
4107 case ISD::FROUND:
4108 case ISD::FRINT:
4109 case ISD::FNEARBYINT: {
4110 if (SNaN)
4111 return true;
4112 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
4113 }
4114 case ISD::FABS:
4115 case ISD::FNEG:
4116 case ISD::FCOPYSIGN: {
4117 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
4118 }
4119 case ISD::SELECT:
4120 return isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
4121 isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
4122 case ISD::FP_EXTEND:
4123 case ISD::FP_ROUND: {
4124 if (SNaN)
4125 return true;
4126 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
4127 }
4128 case ISD::SINT_TO_FP:
4129 case ISD::UINT_TO_FP:
4130 return true;
4131 case ISD::FMA:
4132 case ISD::FMAD: {
4133 if (SNaN)
4134 return true;
4135 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
4136 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
4137 isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
4138 }
4139 case ISD::FSQRT: // Need is known positive
4140 case ISD::FLOG:
4141 case ISD::FLOG2:
4142 case ISD::FLOG10:
4143 case ISD::FPOWI:
4144 case ISD::FPOW: {
4145 if (SNaN)
4146 return true;
4147 // TODO: Refine on operand
4148 return false;
4149 }
4150 case ISD::FMINNUM:
4151 case ISD::FMAXNUM: {
4152 // Only one needs to be known not-nan, since it will be returned if the
4153 // other ends up being one.
4154 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) ||
4155 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
4156 }
4157 case ISD::FMINNUM_IEEE:
4158 case ISD::FMAXNUM_IEEE: {
4159 if (SNaN)
4160 return true;
4161 // This can return a NaN if either operand is an sNaN, or if both operands
4162 // are NaN.
4163 return (isKnownNeverNaN(Op.getOperand(0), false, Depth + 1) &&
4164 isKnownNeverSNaN(Op.getOperand(1), Depth + 1)) ||
4165 (isKnownNeverNaN(Op.getOperand(1), false, Depth + 1) &&
4166 isKnownNeverSNaN(Op.getOperand(0), Depth + 1));
4167 }
4168 case ISD::FMINIMUM:
4169 case ISD::FMAXIMUM: {
4170 // TODO: Does this quiet or return the origina NaN as-is?
4171 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
4172 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
4173 }
4174 case ISD::EXTRACT_VECTOR_ELT: {
4175 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
4176 }
4177 default:
4178 if (Opcode >= ISD::BUILTIN_OP_END ||
4179 Opcode == ISD::INTRINSIC_WO_CHAIN ||
4180 Opcode == ISD::INTRINSIC_W_CHAIN ||
4181 Opcode == ISD::INTRINSIC_VOID) {
4182 return TLI->isKnownNeverNaNForTargetNode(Op, *this, SNaN, Depth);
4183 }
4184
4185 return false;
4186 }
4187}
4188
4189bool SelectionDAG::isKnownNeverZeroFloat(SDValue Op) const {
4190 assert(Op.getValueType().isFloatingPoint() &&((Op.getValueType().isFloatingPoint() && "Floating point type expected"
) ? static_cast<void> (0) : __assert_fail ("Op.getValueType().isFloatingPoint() && \"Floating point type expected\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4191, __PRETTY_FUNCTION__))
4191 "Floating point type expected")((Op.getValueType().isFloatingPoint() && "Floating point type expected"
) ? static_cast<void> (0) : __assert_fail ("Op.getValueType().isFloatingPoint() && \"Floating point type expected\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4191, __PRETTY_FUNCTION__))
;
4192
4193 // If the value is a constant, we can obviously see if it is a zero or not.
4194 // TODO: Add BuildVector support.
4195 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
4196 return !C->isZero();
4197 return false;
4198}
4199
4200bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
4201 assert(!Op.getValueType().isFloatingPoint() &&((!Op.getValueType().isFloatingPoint() && "Floating point types unsupported - use isKnownNeverZeroFloat"
) ? static_cast<void> (0) : __assert_fail ("!Op.getValueType().isFloatingPoint() && \"Floating point types unsupported - use isKnownNeverZeroFloat\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4202, __PRETTY_FUNCTION__))
4202 "Floating point types unsupported - use isKnownNeverZeroFloat")((!Op.getValueType().isFloatingPoint() && "Floating point types unsupported - use isKnownNeverZeroFloat"
) ? static_cast<void> (0) : __assert_fail ("!Op.getValueType().isFloatingPoint() && \"Floating point types unsupported - use isKnownNeverZeroFloat\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4202, __PRETTY_FUNCTION__))
;
4203
4204 // If the value is a constant, we can obviously see if it is a zero or not.
4205 if (ISD::matchUnaryPredicate(
4206 Op, [](ConstantSDNode *C) { return !C->isNullValue(); }))
4207 return true;
4208
4209 // TODO: Recognize more cases here.
4210 switch (Op.getOpcode()) {
4211 default: break;
4212 case ISD::OR:
4213 if (isKnownNeverZero(Op.getOperand(1)) ||
4214 isKnownNeverZero(Op.getOperand(0)))
4215 return true;
4216 break;
4217 }
4218
4219 return false;
4220}
4221
4222bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
4223 // Check the obvious case.
4224 if (A == B) return true;
4225
4226 // For for negative and positive zero.
4227 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
4228 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
4229 if (CA->isZero() && CB->isZero()) return true;
4230
4231 // Otherwise they may not be equal.
4232 return false;
4233}
4234
4235// FIXME: unify with llvm::haveNoCommonBitsSet.
4236// FIXME: could also handle masked merge pattern (X & ~M) op (Y & M)
4237bool SelectionDAG::haveNoCommonBitsSet(SDValue A, SDValue B) const {
4238 assert(A.getValueType() == B.getValueType() &&((A.getValueType() == B.getValueType() && "Values must have the same type"
) ? static_cast<void> (0) : __assert_fail ("A.getValueType() == B.getValueType() && \"Values must have the same type\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4239, __PRETTY_FUNCTION__))
4239 "Values must have the same type")((A.getValueType() == B.getValueType() && "Values must have the same type"
) ? static_cast<void> (0) : __assert_fail ("A.getValueType() == B.getValueType() && \"Values must have the same type\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4239, __PRETTY_FUNCTION__))
;
4240 return (computeKnownBits(A).Zero | computeKnownBits(B).Zero).isAllOnesValue();
4241}
4242
4243static SDValue FoldBUILD_VECTOR(const SDLoc &DL, EVT VT,
4244 ArrayRef<SDValue> Ops,
4245 SelectionDAG &DAG) {
4246 int NumOps = Ops.size();
4247 assert(NumOps != 0 && "Can't build an empty vector!")((NumOps != 0 && "Can't build an empty vector!") ? static_cast
<void> (0) : __assert_fail ("NumOps != 0 && \"Can't build an empty vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4247, __PRETTY_FUNCTION__))
;
4248 assert(VT.getVectorNumElements() == (unsigned)NumOps &&((VT.getVectorNumElements() == (unsigned)NumOps && "Incorrect element count in BUILD_VECTOR!"
) ? static_cast<void> (0) : __assert_fail ("VT.getVectorNumElements() == (unsigned)NumOps && \"Incorrect element count in BUILD_VECTOR!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4249, __PRETTY_FUNCTION__))
4249 "Incorrect element count in BUILD_VECTOR!")((VT.getVectorNumElements() == (unsigned)NumOps && "Incorrect element count in BUILD_VECTOR!"
) ? static_cast<void> (0) : __assert_fail ("VT.getVectorNumElements() == (unsigned)NumOps && \"Incorrect element count in BUILD_VECTOR!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4249, __PRETTY_FUNCTION__))
;
4250
4251 // BUILD_VECTOR of UNDEFs is UNDEF.
4252 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
4253 return DAG.getUNDEF(VT);
4254
4255 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
4256 SDValue IdentitySrc;
4257 bool IsIdentity = true;
4258 for (int i = 0; i != NumOps; ++i) {
4259 if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
4260 Ops[i].getOperand(0).getValueType() != VT ||
4261 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
4262 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
4263 cast<ConstantSDNode>(Ops[i].getOperand(1))->getAPIntValue() != i) {
4264 IsIdentity = false;
4265 break;
4266 }
4267 IdentitySrc = Ops[i].getOperand(0);
4268 }
4269 if (IsIdentity)
4270 return IdentitySrc;
4271
4272 return SDValue();
4273}
4274
4275/// Try to simplify vector concatenation to an input value, undef, or build
4276/// vector.
4277static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT,
4278 ArrayRef<SDValue> Ops,
4279 SelectionDAG &DAG) {
4280 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!")((!Ops.empty() && "Can't concatenate an empty list of vectors!"
) ? static_cast<void> (0) : __assert_fail ("!Ops.empty() && \"Can't concatenate an empty list of vectors!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4280, __PRETTY_FUNCTION__))
;
4281 assert(llvm::all_of(Ops,((llvm::all_of(Ops, [Ops](SDValue Op) { return Ops[0].getValueType
() == Op.getValueType(); }) && "Concatenation of vectors with inconsistent value types!"
) ? static_cast<void> (0) : __assert_fail ("llvm::all_of(Ops, [Ops](SDValue Op) { return Ops[0].getValueType() == Op.getValueType(); }) && \"Concatenation of vectors with inconsistent value types!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4285, __PRETTY_FUNCTION__))
4282 [Ops](SDValue Op) {((llvm::all_of(Ops, [Ops](SDValue Op) { return Ops[0].getValueType
() == Op.getValueType(); }) && "Concatenation of vectors with inconsistent value types!"
) ? static_cast<void> (0) : __assert_fail ("llvm::all_of(Ops, [Ops](SDValue Op) { return Ops[0].getValueType() == Op.getValueType(); }) && \"Concatenation of vectors with inconsistent value types!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4285, __PRETTY_FUNCTION__))
4283 return Ops[0].getValueType() == Op.getValueType();((llvm::all_of(Ops, [Ops](SDValue Op) { return Ops[0].getValueType
() == Op.getValueType(); }) && "Concatenation of vectors with inconsistent value types!"
) ? static_cast<void> (0) : __assert_fail ("llvm::all_of(Ops, [Ops](SDValue Op) { return Ops[0].getValueType() == Op.getValueType(); }) && \"Concatenation of vectors with inconsistent value types!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4285, __PRETTY_FUNCTION__))
4284 }) &&((llvm::all_of(Ops, [Ops](SDValue Op) { return Ops[0].getValueType
() == Op.getValueType(); }) && "Concatenation of vectors with inconsistent value types!"
) ? static_cast<void> (0) : __assert_fail ("llvm::all_of(Ops, [Ops](SDValue Op) { return Ops[0].getValueType() == Op.getValueType(); }) && \"Concatenation of vectors with inconsistent value types!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4285, __PRETTY_FUNCTION__))
4285 "Concatenation of vectors with inconsistent value types!")((llvm::all_of(Ops, [Ops](SDValue Op) { return Ops[0].getValueType
() == Op.getValueType(); }) && "Concatenation of vectors with inconsistent value types!"
) ? static_cast<void> (0) : __assert_fail ("llvm::all_of(Ops, [Ops](SDValue Op) { return Ops[0].getValueType() == Op.getValueType(); }) && \"Concatenation of vectors with inconsistent value types!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4285, __PRETTY_FUNCTION__))
;
4286 assert((Ops.size() * Ops[0].getValueType().getVectorNumElements()) ==(((Ops.size() * Ops[0].getValueType().getVectorNumElements())
== VT.getVectorNumElements() && "Incorrect element count in vector concatenation!"
) ? static_cast<void> (0) : __assert_fail ("(Ops.size() * Ops[0].getValueType().getVectorNumElements()) == VT.getVectorNumElements() && \"Incorrect element count in vector concatenation!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4288, __PRETTY_FUNCTION__))
4287 VT.getVectorNumElements() &&(((Ops.size() * Ops[0].getValueType().getVectorNumElements())
== VT.getVectorNumElements() && "Incorrect element count in vector concatenation!"
) ? static_cast<void> (0) : __assert_fail ("(Ops.size() * Ops[0].getValueType().getVectorNumElements()) == VT.getVectorNumElements() && \"Incorrect element count in vector concatenation!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4288, __PRETTY_FUNCTION__))
4288 "Incorrect element count in vector concatenation!")(((Ops.size() * Ops[0].getValueType().getVectorNumElements())
== VT.getVectorNumElements() && "Incorrect element count in vector concatenation!"
) ? static_cast<void> (0) : __assert_fail ("(Ops.size() * Ops[0].getValueType().getVectorNumElements()) == VT.getVectorNumElements() && \"Incorrect element count in vector concatenation!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4288, __PRETTY_FUNCTION__))
;
4289
4290 if (Ops.size() == 1)
4291 return Ops[0];
4292
4293 // Concat of UNDEFs is UNDEF.
4294 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
4295 return DAG.getUNDEF(VT);
4296
4297 // Scan the operands and look for extract operations from a single source
4298 // that correspond to insertion at the same location via this concatenation:
4299 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
4300 SDValue IdentitySrc;
4301 bool IsIdentity = true;
4302 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
4303 SDValue Op = Ops[i];
4304 unsigned IdentityIndex = i * Op.getValueType().getVectorNumElements();
4305 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
4306 Op.getOperand(0).getValueType() != VT ||
4307 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
4308 !isa<ConstantSDNode>(Op.getOperand(1)) ||
4309 Op.getConstantOperandVal(1) != IdentityIndex) {
4310 IsIdentity = false;
4311 break;
4312 }
4313 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&(((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
"Unexpected identity source vector for concat of extracts") ?
static_cast<void> (0) : __assert_fail ("(!IdentitySrc || IdentitySrc == Op.getOperand(0)) && \"Unexpected identity source vector for concat of extracts\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4314, __PRETTY_FUNCTION__))
4314 "Unexpected identity source vector for concat of extracts")(((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
"Unexpected identity source vector for concat of extracts") ?
static_cast<void> (0) : __assert_fail ("(!IdentitySrc || IdentitySrc == Op.getOperand(0)) && \"Unexpected identity source vector for concat of extracts\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4314, __PRETTY_FUNCTION__))
;
4315 IdentitySrc = Op.getOperand(0);
4316 }
4317 if (IsIdentity) {
4318 assert(IdentitySrc && "Failed to set source vector of extracts")((IdentitySrc && "Failed to set source vector of extracts"
) ? static_cast<void> (0) : __assert_fail ("IdentitySrc && \"Failed to set source vector of extracts\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4318, __PRETTY_FUNCTION__))
;
4319 return IdentitySrc;
4320 }
4321
4322 // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
4323 // simplified to one big BUILD_VECTOR.
4324 // FIXME: Add support for SCALAR_TO_VECTOR as well.
4325 EVT SVT = VT.getScalarType();
4326 SmallVector<SDValue, 16> Elts;
4327 for (SDValue Op : Ops) {
4328 EVT OpVT = Op.getValueType();
4329 if (Op.isUndef())
4330 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
4331 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
4332 Elts.append(Op->op_begin(), Op->op_end());
4333 else
4334 return SDValue();
4335 }
4336
4337 // BUILD_VECTOR requires all inputs to be of the same type, find the
4338 // maximum type and extend them all.
4339 for (SDValue Op : Elts)
4340 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
4341
4342 if (SVT.bitsGT(VT.getScalarType()))
4343 for (SDValue &Op : Elts)
4344 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
4345 ? DAG.getZExtOrTrunc(Op, DL, SVT)
4346 : DAG.getSExtOrTrunc(Op, DL, SVT);
4347
4348 SDValue V = DAG.getBuildVector(VT, DL, Elts);
4349 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
4350 return V;
4351}
4352
4353/// Gets or creates the specified node.
4354SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
4355 FoldingSetNodeID ID;
4356 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
4357 void *IP = nullptr;
4358 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
4359 return SDValue(E, 0);
4360
4361 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4362 getVTList(VT));
4363 CSEMap.InsertNode(N, IP);
4364
4365 InsertNode(N);
4366 SDValue V = SDValue(N, 0);
4367 NewSDValueDbgMsg(V, "Creating new node: ", this);
4368 return V;
4369}
4370
4371SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
4372 SDValue Operand, const SDNodeFlags Flags) {
4373 // Constant fold unary operations with an integer constant operand. Even
4374 // opaque constant will be folded, because the folding of unary operations
4375 // doesn't create new constants with different values. Nevertheless, the
4376 // opaque flag is preserved during folding to prevent future folding with
4377 // other constants.
4378 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand)) {
4379 const APInt &Val = C->getAPIntValue();
4380 switch (Opcode) {
4381 default: break;
4382 case ISD::SIGN_EXTEND:
4383 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
4384 C->isTargetOpcode(), C->isOpaque());
4385 case ISD::TRUNCATE:
4386 if (C->isOpaque())
4387 break;
4388 LLVM_FALLTHROUGH[[gnu::fallthrough]];
4389 case ISD::ANY_EXTEND:
4390 case ISD::ZERO_EXTEND:
4391 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
4392 C->isTargetOpcode(), C->isOpaque());
4393 case ISD::UINT_TO_FP:
4394 case ISD::SINT_TO_FP: {
4395 APFloat apf(EVTToAPFloatSemantics(VT),
4396 APInt::getNullValue(VT.getSizeInBits()));
4397 (void)apf.convertFromAPInt(Val,
4398 Opcode==ISD::SINT_TO_FP,
4399 APFloat::rmNearestTiesToEven);
4400 return getConstantFP(apf, DL, VT);
4401 }
4402 case ISD::BITCAST:
4403 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
4404 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
4405 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
4406 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
4407 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
4408 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
4409 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
4410 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
4411 break;
4412 case ISD::ABS:
4413 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
4414 C->isOpaque());
4415 case ISD::BITREVERSE:
4416 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
4417 C->isOpaque());
4418 case ISD::BSWAP:
4419 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
4420 C->isOpaque());
4421 case ISD::CTPOP:
4422 return getConstant(Val.countPopulation(), DL, VT, C->isTargetOpcode(),
4423 C->isOpaque());
4424 case ISD::CTLZ:
4425 case ISD::CTLZ_ZERO_UNDEF:
4426 return getConstant(Val.countLeadingZeros(), DL, VT, C->isTargetOpcode(),
4427 C->isOpaque());
4428 case ISD::CTTZ:
4429 case ISD::CTTZ_ZERO_UNDEF:
4430 return getConstant(Val.countTrailingZeros(), DL, VT, C->isTargetOpcode(),
4431 C->isOpaque());
4432 case ISD::FP16_TO_FP: {
4433 bool Ignored;
4434 APFloat FPV(APFloat::IEEEhalf(),
4435 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
4436
4437 // This can return overflow, underflow, or inexact; we don't care.
4438 // FIXME need to be more flexible about rounding mode.
4439 (void)FPV.convert(EVTToAPFloatSemantics(VT),
4440 APFloat::rmNearestTiesToEven, &Ignored);
4441 return getConstantFP(FPV, DL, VT);
4442 }
4443 }
4444 }
4445
4446 // Constant fold unary operations with a floating point constant operand.
4447 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand)) {
4448 APFloat V = C->getValueAPF(); // make copy
4449 switch (Opcode) {
4450 case ISD::FNEG:
4451 V.changeSign();
4452 return getConstantFP(V, DL, VT);
4453 case ISD::FABS:
4454 V.clearSign();
4455 return getConstantFP(V, DL, VT);
4456 case ISD::FCEIL: {
4457 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
4458 if (fs == APFloat::opOK || fs == APFloat::opInexact)
4459 return getConstantFP(V, DL, VT);
4460 break;
4461 }
4462 case ISD::FTRUNC: {
4463 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
4464 if (fs == APFloat::opOK || fs == APFloat::opInexact)
4465 return getConstantFP(V, DL, VT);
4466 break;
4467 }
4468 case ISD::FFLOOR: {
4469 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
4470 if (fs == APFloat::opOK || fs == APFloat::opInexact)
4471 return getConstantFP(V, DL, VT);
4472 break;
4473 }
4474 case ISD::FP_EXTEND: {
4475 bool ignored;
4476 // This can return overflow, underflow, or inexact; we don't care.
4477 // FIXME need to be more flexible about rounding mode.
4478 (void)V.convert(EVTToAPFloatSemantics(VT),
4479 APFloat::rmNearestTiesToEven, &ignored);
4480 return getConstantFP(V, DL, VT);
4481 }
4482 case ISD::FP_TO_SINT:
4483 case ISD::FP_TO_UINT: {
4484 bool ignored;
4485 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
4486 // FIXME need to be more flexible about rounding mode.
4487 APFloat::opStatus s =
4488 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
4489 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
4490 break;
4491 return getConstant(IntVal, DL, VT);
4492 }
4493 case ISD::BITCAST:
4494 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
4495 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
4496 else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
4497 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
4498 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
4499 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
4500 break;
4501 case ISD::FP_TO_FP16: {
4502 bool Ignored;
4503 // This can return overflow, underflow, or inexact; we don't care.
4504 // FIXME need to be more flexible about rounding mode.
4505 (void)V.convert(APFloat::IEEEhalf(),
4506 APFloat::rmNearestTiesToEven, &Ignored);
4507 return getConstant(V.bitcastToAPInt(), DL, VT);
4508 }
4509 }
4510 }
4511
4512 // Constant fold unary operations with a vector integer or float operand.
4513 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand)) {
4514 if (BV->isConstant()) {
4515 switch (Opcode) {
4516 default:
4517 // FIXME: Entirely reasonable to perform folding of other unary
4518 // operations here as the need arises.
4519 break;
4520 case ISD::FNEG:
4521 case ISD::FABS:
4522 case ISD::FCEIL:
4523 case ISD::FTRUNC:
4524 case ISD::FFLOOR:
4525 case ISD::FP_EXTEND:
4526 case ISD::FP_TO_SINT:
4527 case ISD::FP_TO_UINT:
4528 case ISD::TRUNCATE:
4529 case ISD::ANY_EXTEND:
4530 case ISD::ZERO_EXTEND:
4531 case ISD::SIGN_EXTEND:
4532 case ISD::UINT_TO_FP:
4533 case ISD::SINT_TO_FP:
4534 case ISD::ABS:
4535 case ISD::BITREVERSE:
4536 case ISD::BSWAP:
4537 case ISD::CTLZ:
4538 case ISD::CTLZ_ZERO_UNDEF:
4539 case ISD::CTTZ:
4540 case ISD::CTTZ_ZERO_UNDEF:
4541 case ISD::CTPOP: {
4542 SDValue Ops = { Operand };
4543 if (SDValue Fold = FoldConstantVectorArithmetic(Opcode, DL, VT, Ops))
4544 return Fold;
4545 }
4546 }
4547 }
4548 }
4549
4550 unsigned OpOpcode = Operand.getNode()->getOpcode();
4551 switch (Opcode) {
4552 case ISD::TokenFactor:
4553 case ISD::MERGE_VALUES:
4554 case ISD::CONCAT_VECTORS:
4555 return Operand; // Factor, merge or concat of one node? No need.
4556 case ISD::BUILD_VECTOR: {
4557 // Attempt to simplify BUILD_VECTOR.
4558 SDValue Ops[] = {Operand};
4559 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
4560 return V;
4561 break;
4562 }
4563 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node")::llvm::llvm_unreachable_internal("Invalid method to make FP_ROUND node"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4563)
;
4564 case ISD::FP_EXTEND:
4565 assert(VT.isFloatingPoint() &&((VT.isFloatingPoint() && Operand.getValueType().isFloatingPoint
() && "Invalid FP cast!") ? static_cast<void> (
0) : __assert_fail ("VT.isFloatingPoint() && Operand.getValueType().isFloatingPoint() && \"Invalid FP cast!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4566, __PRETTY_FUNCTION__))
4566 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!")((VT.isFloatingPoint() && Operand.getValueType().isFloatingPoint
() && "Invalid FP cast!") ? static_cast<void> (
0) : __assert_fail ("VT.isFloatingPoint() && Operand.getValueType().isFloatingPoint() && \"Invalid FP cast!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4566, __PRETTY_FUNCTION__))
;
4567 if (Operand.getValueType() == VT) return Operand; // noop conversion.
4568 assert((!VT.isVector() ||(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4571, __PRETTY_FUNCTION__))
4569 VT.getVectorNumElements() ==(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4571, __PRETTY_FUNCTION__))
4570 Operand.getValueType().getVectorNumElements()) &&(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4571, __PRETTY_FUNCTION__))
4571 "Vector element count mismatch!")(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4571, __PRETTY_FUNCTION__))
;
4572 assert(Operand.getValueType().bitsLT(VT) &&((Operand.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!"
) ? static_cast<void> (0) : __assert_fail ("Operand.getValueType().bitsLT(VT) && \"Invalid fpext node, dst < src!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4573, __PRETTY_FUNCTION__))
4573 "Invalid fpext node, dst < src!")((Operand.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!"
) ? static_cast<void> (0) : __assert_fail ("Operand.getValueType().bitsLT(VT) && \"Invalid fpext node, dst < src!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4573, __PRETTY_FUNCTION__))
;
4574 if (Operand.isUndef())
4575 return getUNDEF(VT);
4576 break;
4577 case ISD::FP_TO_SINT:
4578 case ISD::FP_TO_UINT:
4579 if (Operand.isUndef())
4580 return getUNDEF(VT);
4581 break;
4582 case ISD::SINT_TO_FP:
4583 case ISD::UINT_TO_FP:
4584 // [us]itofp(undef) = 0, because the result value is bounded.
4585 if (Operand.isUndef())
4586 return getConstantFP(0.0, DL, VT);
4587 break;
4588 case ISD::SIGN_EXTEND:
4589 assert(VT.isInteger() && Operand.getValueType().isInteger() &&((VT.isInteger() && Operand.getValueType().isInteger(
) && "Invalid SIGN_EXTEND!") ? static_cast<void>
(0) : __assert_fail ("VT.isInteger() && Operand.getValueType().isInteger() && \"Invalid SIGN_EXTEND!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4590, __PRETTY_FUNCTION__))
4590 "Invalid SIGN_EXTEND!")((VT.isInteger() && Operand.getValueType().isInteger(
) && "Invalid SIGN_EXTEND!") ? static_cast<void>
(0) : __assert_fail ("VT.isInteger() && Operand.getValueType().isInteger() && \"Invalid SIGN_EXTEND!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4590, __PRETTY_FUNCTION__))
;
4591 assert(VT.isVector() == Operand.getValueType().isVector() &&((VT.isVector() == Operand.getValueType().isVector() &&
"SIGN_EXTEND result type type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("VT.isVector() == Operand.getValueType().isVector() && \"SIGN_EXTEND result type type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4593, __PRETTY_FUNCTION__))
4592 "SIGN_EXTEND result type type should be vector iff the operand "((VT.isVector() == Operand.getValueType().isVector() &&
"SIGN_EXTEND result type type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("VT.isVector() == Operand.getValueType().isVector() && \"SIGN_EXTEND result type type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4593, __PRETTY_FUNCTION__))
4593 "type is vector!")((VT.isVector() == Operand.getValueType().isVector() &&
"SIGN_EXTEND result type type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("VT.isVector() == Operand.getValueType().isVector() && \"SIGN_EXTEND result type type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4593, __PRETTY_FUNCTION__))
;
4594 if (Operand.getValueType() == VT) return Operand; // noop extension
4595 assert((!VT.isVector() ||(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4598, __PRETTY_FUNCTION__))
4596 VT.getVectorNumElements() ==(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4598, __PRETTY_FUNCTION__))
4597 Operand.getValueType().getVectorNumElements()) &&(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4598, __PRETTY_FUNCTION__))
4598 "Vector element count mismatch!")(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4598, __PRETTY_FUNCTION__))
;
4599 assert(Operand.getValueType().bitsLT(VT) &&((Operand.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!"
) ? static_cast<void> (0) : __assert_fail ("Operand.getValueType().bitsLT(VT) && \"Invalid sext node, dst < src!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4600, __PRETTY_FUNCTION__))
4600 "Invalid sext node, dst < src!")((Operand.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!"
) ? static_cast<void> (0) : __assert_fail ("Operand.getValueType().bitsLT(VT) && \"Invalid sext node, dst < src!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4600, __PRETTY_FUNCTION__))
;
4601 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
4602 return getNode(OpOpcode, DL, VT, Operand.getOperand(0));
4603 else if (OpOpcode == ISD::UNDEF)
4604 // sext(undef) = 0, because the top bits will all be the same.
4605 return getConstant(0, DL, VT);
4606 break;
4607 case ISD::ZERO_EXTEND:
4608 assert(VT.isInteger() && Operand.getValueType().isInteger() &&((VT.isInteger() && Operand.getValueType().isInteger(
) && "Invalid ZERO_EXTEND!") ? static_cast<void>
(0) : __assert_fail ("VT.isInteger() && Operand.getValueType().isInteger() && \"Invalid ZERO_EXTEND!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4609, __PRETTY_FUNCTION__))
4609 "Invalid ZERO_EXTEND!")((VT.isInteger() && Operand.getValueType().isInteger(
) && "Invalid ZERO_EXTEND!") ? static_cast<void>
(0) : __assert_fail ("VT.isInteger() && Operand.getValueType().isInteger() && \"Invalid ZERO_EXTEND!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4609, __PRETTY_FUNCTION__))
;
4610 assert(VT.isVector() == Operand.getValueType().isVector() &&((VT.isVector() == Operand.getValueType().isVector() &&
"ZERO_EXTEND result type type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("VT.isVector() == Operand.getValueType().isVector() && \"ZERO_EXTEND result type type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4612, __PRETTY_FUNCTION__))
4611 "ZERO_EXTEND result type type should be vector iff the operand "((VT.isVector() == Operand.getValueType().isVector() &&
"ZERO_EXTEND result type type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("VT.isVector() == Operand.getValueType().isVector() && \"ZERO_EXTEND result type type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4612, __PRETTY_FUNCTION__))
4612 "type is vector!")((VT.isVector() == Operand.getValueType().isVector() &&
"ZERO_EXTEND result type type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("VT.isVector() == Operand.getValueType().isVector() && \"ZERO_EXTEND result type type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4612, __PRETTY_FUNCTION__))
;
4613 if (Operand.getValueType() == VT) return Operand; // noop extension
4614 assert((!VT.isVector() ||(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4617, __PRETTY_FUNCTION__))
4615 VT.getVectorNumElements() ==(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4617, __PRETTY_FUNCTION__))
4616 Operand.getValueType().getVectorNumElements()) &&(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4617, __PRETTY_FUNCTION__))
4617 "Vector element count mismatch!")(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4617, __PRETTY_FUNCTION__))
;
4618 assert(Operand.getValueType().bitsLT(VT) &&((Operand.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!"
) ? static_cast<void> (0) : __assert_fail ("Operand.getValueType().bitsLT(VT) && \"Invalid zext node, dst < src!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4619, __PRETTY_FUNCTION__))
4619 "Invalid zext node, dst < src!")((Operand.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!"
) ? static_cast<void> (0) : __assert_fail ("Operand.getValueType().bitsLT(VT) && \"Invalid zext node, dst < src!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4619, __PRETTY_FUNCTION__))
;
4620 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
4621 return getNode(ISD::ZERO_EXTEND, DL, VT, Operand.getOperand(0));
4622 else if (OpOpcode == ISD::UNDEF)
4623 // zext(undef) = 0, because the top bits will be zero.
4624 return getConstant(0, DL, VT);
4625 break;
4626 case ISD::ANY_EXTEND:
4627 assert(VT.isInteger() && Operand.getValueType().isInteger() &&((VT.isInteger() && Operand.getValueType().isInteger(
) && "Invalid ANY_EXTEND!") ? static_cast<void>
(0) : __assert_fail ("VT.isInteger() && Operand.getValueType().isInteger() && \"Invalid ANY_EXTEND!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4628, __PRETTY_FUNCTION__))
4628 "Invalid ANY_EXTEND!")((VT.isInteger() && Operand.getValueType().isInteger(
) && "Invalid ANY_EXTEND!") ? static_cast<void>
(0) : __assert_fail ("VT.isInteger() && Operand.getValueType().isInteger() && \"Invalid ANY_EXTEND!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4628, __PRETTY_FUNCTION__))
;
4629 assert(VT.isVector() == Operand.getValueType().isVector() &&((VT.isVector() == Operand.getValueType().isVector() &&
"ANY_EXTEND result type type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("VT.isVector() == Operand.getValueType().isVector() && \"ANY_EXTEND result type type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4631, __PRETTY_FUNCTION__))
4630 "ANY_EXTEND result type type should be vector iff the operand "((VT.isVector() == Operand.getValueType().isVector() &&
"ANY_EXTEND result type type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("VT.isVector() == Operand.getValueType().isVector() && \"ANY_EXTEND result type type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4631, __PRETTY_FUNCTION__))
4631 "type is vector!")((VT.isVector() == Operand.getValueType().isVector() &&
"ANY_EXTEND result type type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("VT.isVector() == Operand.getValueType().isVector() && \"ANY_EXTEND result type type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4631, __PRETTY_FUNCTION__))
;
4632 if (Operand.getValueType() == VT) return Operand; // noop extension
4633 assert((!VT.isVector() ||(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4636, __PRETTY_FUNCTION__))
4634 VT.getVectorNumElements() ==(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4636, __PRETTY_FUNCTION__))
4635 Operand.getValueType().getVectorNumElements()) &&(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4636, __PRETTY_FUNCTION__))
4636 "Vector element count mismatch!")(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4636, __PRETTY_FUNCTION__))
;
4637 assert(Operand.getValueType().bitsLT(VT) &&((Operand.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!"
) ? static_cast<void> (0) : __assert_fail ("Operand.getValueType().bitsLT(VT) && \"Invalid anyext node, dst < src!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4638, __PRETTY_FUNCTION__))
4638 "Invalid anyext node, dst < src!")((Operand.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!"
) ? static_cast<void> (0) : __assert_fail ("Operand.getValueType().bitsLT(VT) && \"Invalid anyext node, dst < src!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4638, __PRETTY_FUNCTION__))
;
4639
4640 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
4641 OpOpcode == ISD::ANY_EXTEND)
4642 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
4643 return getNode(OpOpcode, DL, VT, Operand.getOperand(0));
4644 else if (OpOpcode == ISD::UNDEF)
4645 return getUNDEF(VT);
4646
4647 // (ext (trunc x)) -> x
4648 if (OpOpcode == ISD::TRUNCATE) {
4649 SDValue OpOp = Operand.getOperand(0);
4650 if (OpOp.getValueType() == VT) {
4651 transferDbgValues(Operand, OpOp);
4652 return OpOp;
4653 }
4654 }
4655 break;
4656 case ISD::TRUNCATE:
4657 assert(VT.isInteger() && Operand.getValueType().isInteger() &&((VT.isInteger() && Operand.getValueType().isInteger(
) && "Invalid TRUNCATE!") ? static_cast<void> (
0) : __assert_fail ("VT.isInteger() && Operand.getValueType().isInteger() && \"Invalid TRUNCATE!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4658, __PRETTY_FUNCTION__))
4658 "Invalid TRUNCATE!")((VT.isInteger() && Operand.getValueType().isInteger(
) && "Invalid TRUNCATE!") ? static_cast<void> (
0) : __assert_fail ("VT.isInteger() && Operand.getValueType().isInteger() && \"Invalid TRUNCATE!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4658, __PRETTY_FUNCTION__))
;
4659 assert(VT.isVector() == Operand.getValueType().isVector() &&((VT.isVector() == Operand.getValueType().isVector() &&
"TRUNCATE result type type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("VT.isVector() == Operand.getValueType().isVector() && \"TRUNCATE result type type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4661, __PRETTY_FUNCTION__))
4660 "TRUNCATE result type type should be vector iff the operand "((VT.isVector() == Operand.getValueType().isVector() &&
"TRUNCATE result type type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("VT.isVector() == Operand.getValueType().isVector() && \"TRUNCATE result type type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4661, __PRETTY_FUNCTION__))
4661 "type is vector!")((VT.isVector() == Operand.getValueType().isVector() &&
"TRUNCATE result type type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("VT.isVector() == Operand.getValueType().isVector() && \"TRUNCATE result type type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4661, __PRETTY_FUNCTION__))
;
4662 if (Operand.getValueType() == VT) return Operand; // noop truncate
4663 assert((!VT.isVector() ||(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4666, __PRETTY_FUNCTION__))
4664 VT.getVectorNumElements() ==(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4666, __PRETTY_FUNCTION__))
4665 Operand.getValueType().getVectorNumElements()) &&(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4666, __PRETTY_FUNCTION__))
4666 "Vector element count mismatch!")(((!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType
().getVectorNumElements()) && "Vector element count mismatch!"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT.getVectorNumElements() == Operand.getValueType().getVectorNumElements()) && \"Vector element count mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4666, __PRETTY_FUNCTION__))
;
4667 assert(Operand.getValueType().bitsGT(VT) &&((Operand.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!"
) ? static_cast<void> (0) : __assert_fail ("Operand.getValueType().bitsGT(VT) && \"Invalid truncate node, src < dst!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4668, __PRETTY_FUNCTION__))
4668 "Invalid truncate node, src < dst!")((Operand.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!"
) ? static_cast<void> (0) : __assert_fail ("Operand.getValueType().bitsGT(VT) && \"Invalid truncate node, src < dst!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4668, __PRETTY_FUNCTION__))
;
4669 if (OpOpcode == ISD::TRUNCATE)
4670 return getNode(ISD::TRUNCATE, DL, VT, Operand.getOperand(0));
4671 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
4672 OpOpcode == ISD::ANY_EXTEND) {
4673 // If the source is smaller than the dest, we still need an extend.
4674 if (Operand.getOperand(0).getValueType().getScalarType()
4675 .bitsLT(VT.getScalarType()))
4676 return getNode(OpOpcode, DL, VT, Operand.getOperand(0));
4677 if (Operand.getOperand(0).getValueType().bitsGT(VT))
4678 return getNode(ISD::TRUNCATE, DL, VT, Operand.getOperand(0));
4679 return Operand.getOperand(0);
4680 }
4681 if (OpOpcode == ISD::UNDEF)
4682 return getUNDEF(VT);
4683 break;
4684 case ISD::ANY_EXTEND_VECTOR_INREG:
4685 case ISD::ZERO_EXTEND_VECTOR_INREG:
4686 case ISD::SIGN_EXTEND_VECTOR_INREG:
4687 assert(VT.isVector() && "This DAG node is restricted to vector types.")((VT.isVector() && "This DAG node is restricted to vector types."
) ? static_cast<void> (0) : __assert_fail ("VT.isVector() && \"This DAG node is restricted to vector types.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4687, __PRETTY_FUNCTION__))
;
4688 assert(Operand.getValueType().bitsLE(VT) &&((Operand.getValueType().bitsLE(VT) && "The input must be the same size or smaller than the result."
) ? static_cast<void> (0) : __assert_fail ("Operand.getValueType().bitsLE(VT) && \"The input must be the same size or smaller than the result.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4689, __PRETTY_FUNCTION__))
4689 "The input must be the same size or smaller than the result.")((Operand.getValueType().bitsLE(VT) && "The input must be the same size or smaller than the result."
) ? static_cast<void> (0) : __assert_fail ("Operand.getValueType().bitsLE(VT) && \"The input must be the same size or smaller than the result.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4689, __PRETTY_FUNCTION__))
;
4690 assert(VT.getVectorNumElements() <((VT.getVectorNumElements() < Operand.getValueType().getVectorNumElements
() && "The destination vector type must have fewer lanes than the input."
) ? static_cast<void> (0) : __assert_fail ("VT.getVectorNumElements() < Operand.getValueType().getVectorNumElements() && \"The destination vector type must have fewer lanes than the input.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4692, __PRETTY_FUNCTION__))
4691 Operand.getValueType().getVectorNumElements() &&((VT.getVectorNumElements() < Operand.getValueType().getVectorNumElements
() && "The destination vector type must have fewer lanes than the input."
) ? static_cast<void> (0) : __assert_fail ("VT.getVectorNumElements() < Operand.getValueType().getVectorNumElements() && \"The destination vector type must have fewer lanes than the input.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4692, __PRETTY_FUNCTION__))
4692 "The destination vector type must have fewer lanes than the input.")((VT.getVectorNumElements() < Operand.getValueType().getVectorNumElements
() && "The destination vector type must have fewer lanes than the input."
) ? static_cast<void> (0) : __assert_fail ("VT.getVectorNumElements() < Operand.getValueType().getVectorNumElements() && \"The destination vector type must have fewer lanes than the input.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4692, __PRETTY_FUNCTION__))
;
4693 break;
4694 case ISD::ABS:
4695 assert(VT.isInteger() && VT == Operand.getValueType() &&((VT.isInteger() && VT == Operand.getValueType() &&
"Invalid ABS!") ? static_cast<void> (0) : __assert_fail
("VT.isInteger() && VT == Operand.getValueType() && \"Invalid ABS!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4696, __PRETTY_FUNCTION__))
4696 "Invalid ABS!")((VT.isInteger() && VT == Operand.getValueType() &&
"Invalid ABS!") ? static_cast<void> (0) : __assert_fail
("VT.isInteger() && VT == Operand.getValueType() && \"Invalid ABS!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4696, __PRETTY_FUNCTION__))
;
4697 if (OpOpcode == ISD::UNDEF)
4698 return getUNDEF(VT);
4699 break;
4700 case ISD::BSWAP:
4701 assert(VT.isInteger() && VT == Operand.getValueType() &&((VT.isInteger() && VT == Operand.getValueType() &&
"Invalid BSWAP!") ? static_cast<void> (0) : __assert_fail
("VT.isInteger() && VT == Operand.getValueType() && \"Invalid BSWAP!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4702, __PRETTY_FUNCTION__))
4702 "Invalid BSWAP!")((VT.isInteger() && VT == Operand.getValueType() &&
"Invalid BSWAP!") ? static_cast<void> (0) : __assert_fail
("VT.isInteger() && VT == Operand.getValueType() && \"Invalid BSWAP!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4702, __PRETTY_FUNCTION__))
;
4703 assert((VT.getScalarSizeInBits() % 16 == 0) &&(((VT.getScalarSizeInBits() % 16 == 0) && "BSWAP types must be a multiple of 16 bits!"
) ? static_cast<void> (0) : __assert_fail ("(VT.getScalarSizeInBits() % 16 == 0) && \"BSWAP types must be a multiple of 16 bits!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4704, __PRETTY_FUNCTION__))
4704 "BSWAP types must be a multiple of 16 bits!")(((VT.getScalarSizeInBits() % 16 == 0) && "BSWAP types must be a multiple of 16 bits!"
) ? static_cast<void> (0) : __assert_fail ("(VT.getScalarSizeInBits() % 16 == 0) && \"BSWAP types must be a multiple of 16 bits!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4704, __PRETTY_FUNCTION__))
;
4705 if (OpOpcode == ISD::UNDEF)
4706 return getUNDEF(VT);
4707 break;
4708 case ISD::BITREVERSE:
4709 assert(VT.isInteger() && VT == Operand.getValueType() &&((VT.isInteger() && VT == Operand.getValueType() &&
"Invalid BITREVERSE!") ? static_cast<void> (0) : __assert_fail
("VT.isInteger() && VT == Operand.getValueType() && \"Invalid BITREVERSE!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4710, __PRETTY_FUNCTION__))
4710 "Invalid BITREVERSE!")((VT.isInteger() && VT == Operand.getValueType() &&
"Invalid BITREVERSE!") ? static_cast<void> (0) : __assert_fail
("VT.isInteger() && VT == Operand.getValueType() && \"Invalid BITREVERSE!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4710, __PRETTY_FUNCTION__))
;
4711 if (OpOpcode == ISD::UNDEF)
4712 return getUNDEF(VT);
4713 break;
4714 case ISD::BITCAST:
4715 // Basic sanity checking.
4716 assert(VT.getSizeInBits() == Operand.getValueSizeInBits() &&((VT.getSizeInBits() == Operand.getValueSizeInBits() &&
"Cannot BITCAST between types of different sizes!") ? static_cast
<void> (0) : __assert_fail ("VT.getSizeInBits() == Operand.getValueSizeInBits() && \"Cannot BITCAST between types of different sizes!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4717, __PRETTY_FUNCTION__))
4717 "Cannot BITCAST between types of different sizes!")((VT.getSizeInBits() == Operand.getValueSizeInBits() &&
"Cannot BITCAST between types of different sizes!") ? static_cast
<void> (0) : __assert_fail ("VT.getSizeInBits() == Operand.getValueSizeInBits() && \"Cannot BITCAST between types of different sizes!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4717, __PRETTY_FUNCTION__))
;
4718 if (VT == Operand.getValueType()) return Operand; // noop conversion.
4719 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
4720 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
4721 if (OpOpcode == ISD::UNDEF)
4722 return getUNDEF(VT);
4723 break;
4724 case ISD::SCALAR_TO_VECTOR:
4725 assert(VT.isVector() && !Operand.getValueType().isVector() &&((VT.isVector() && !Operand.getValueType().isVector()
&& (VT.getVectorElementType() == Operand.getValueType
() || (VT.getVectorElementType().isInteger() && Operand
.getValueType().isInteger() && VT.getVectorElementType
().bitsLE(Operand.getValueType()))) && "Illegal SCALAR_TO_VECTOR node!"
) ? static_cast<void> (0) : __assert_fail ("VT.isVector() && !Operand.getValueType().isVector() && (VT.getVectorElementType() == Operand.getValueType() || (VT.getVectorElementType().isInteger() && Operand.getValueType().isInteger() && VT.getVectorElementType().bitsLE(Operand.getValueType()))) && \"Illegal SCALAR_TO_VECTOR node!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4730, __PRETTY_FUNCTION__))
4726 (VT.getVectorElementType() == Operand.getValueType() ||((VT.isVector() && !Operand.getValueType().isVector()
&& (VT.getVectorElementType() == Operand.getValueType
() || (VT.getVectorElementType().isInteger() && Operand
.getValueType().isInteger() && VT.getVectorElementType
().bitsLE(Operand.getValueType()))) && "Illegal SCALAR_TO_VECTOR node!"
) ? static_cast<void> (0) : __assert_fail ("VT.isVector() && !Operand.getValueType().isVector() && (VT.getVectorElementType() == Operand.getValueType() || (VT.getVectorElementType().isInteger() && Operand.getValueType().isInteger() && VT.getVectorElementType().bitsLE(Operand.getValueType()))) && \"Illegal SCALAR_TO_VECTOR node!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4730, __PRETTY_FUNCTION__))
4727 (VT.getVectorElementType().isInteger() &&((VT.isVector() && !Operand.getValueType().isVector()
&& (VT.getVectorElementType() == Operand.getValueType
() || (VT.getVectorElementType().isInteger() && Operand
.getValueType().isInteger() && VT.getVectorElementType
().bitsLE(Operand.getValueType()))) && "Illegal SCALAR_TO_VECTOR node!"
) ? static_cast<void> (0) : __assert_fail ("VT.isVector() && !Operand.getValueType().isVector() && (VT.getVectorElementType() == Operand.getValueType() || (VT.getVectorElementType().isInteger() && Operand.getValueType().isInteger() && VT.getVectorElementType().bitsLE(Operand.getValueType()))) && \"Illegal SCALAR_TO_VECTOR node!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4730, __PRETTY_FUNCTION__))
4728 Operand.getValueType().isInteger() &&((VT.isVector() && !Operand.getValueType().isVector()
&& (VT.getVectorElementType() == Operand.getValueType
() || (VT.getVectorElementType().isInteger() && Operand
.getValueType().isInteger() && VT.getVectorElementType
().bitsLE(Operand.getValueType()))) && "Illegal SCALAR_TO_VECTOR node!"
) ? static_cast<void> (0) : __assert_fail ("VT.isVector() && !Operand.getValueType().isVector() && (VT.getVectorElementType() == Operand.getValueType() || (VT.getVectorElementType().isInteger() && Operand.getValueType().isInteger() && VT.getVectorElementType().bitsLE(Operand.getValueType()))) && \"Illegal SCALAR_TO_VECTOR node!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4730, __PRETTY_FUNCTION__))
4729 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&((VT.isVector() && !Operand.getValueType().isVector()
&& (VT.getVectorElementType() == Operand.getValueType
() || (VT.getVectorElementType().isInteger() && Operand
.getValueType().isInteger() && VT.getVectorElementType
().bitsLE(Operand.getValueType()))) && "Illegal SCALAR_TO_VECTOR node!"
) ? static_cast<void> (0) : __assert_fail ("VT.isVector() && !Operand.getValueType().isVector() && (VT.getVectorElementType() == Operand.getValueType() || (VT.getVectorElementType().isInteger() && Operand.getValueType().isInteger() && VT.getVectorElementType().bitsLE(Operand.getValueType()))) && \"Illegal SCALAR_TO_VECTOR node!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4730, __PRETTY_FUNCTION__))
4730 "Illegal SCALAR_TO_VECTOR node!")((VT.isVector() && !Operand.getValueType().isVector()
&& (VT.getVectorElementType() == Operand.getValueType
() || (VT.getVectorElementType().isInteger() && Operand
.getValueType().isInteger() && VT.getVectorElementType
().bitsLE(Operand.getValueType()))) && "Illegal SCALAR_TO_VECTOR node!"
) ? static_cast<void> (0) : __assert_fail ("VT.isVector() && !Operand.getValueType().isVector() && (VT.getVectorElementType() == Operand.getValueType() || (VT.getVectorElementType().isInteger() && Operand.getValueType().isInteger() && VT.getVectorElementType().bitsLE(Operand.getValueType()))) && \"Illegal SCALAR_TO_VECTOR node!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4730, __PRETTY_FUNCTION__))
;
4731 if (OpOpcode == ISD::UNDEF)
4732 return getUNDEF(VT);
4733 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
4734 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
4735 isa<ConstantSDNode>(Operand.getOperand(1)) &&
4736 Operand.getConstantOperandVal(1) == 0 &&
4737 Operand.getOperand(0).getValueType() == VT)
4738 return Operand.getOperand(0);
4739 break;
4740 case ISD::FNEG:
4741 // Negation of an unknown bag of bits is still completely undefined.
4742 if (OpOpcode == ISD::UNDEF)
4743 return getUNDEF(VT);
4744
4745 if (OpOpcode == ISD::FNEG) // --X -> X
4746 return Operand.getOperand(0);
4747 break;
4748 case ISD::FABS:
4749 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
4750 return getNode(ISD::FABS, DL, VT, Operand.getOperand(0));
4751 break;
4752 }
4753
4754 SDNode *N;
4755 SDVTList VTs = getVTList(VT);
4756 SDValue Ops[] = {Operand};
4757 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
4758 FoldingSetNodeID ID;
4759 AddNodeIDNode(ID, Opcode, VTs, Ops);
4760 void *IP = nullptr;
4761 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
4762 E->intersectFlagsWith(Flags);
4763 return SDValue(E, 0);
4764 }
4765
4766 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
4767 N->setFlags(Flags);
4768 createOperands(N, Ops);
4769 CSEMap.InsertNode(N, IP);
4770 } else {
4771 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
4772 createOperands(N, Ops);
4773 }
4774
4775 InsertNode(N);
4776 SDValue V = SDValue(N, 0);
4777 NewSDValueDbgMsg(V, "Creating new node: ", this);
4778 return V;
4779}
4780
4781static llvm::Optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
4782 const APInt &C2) {
4783 switch (Opcode) {
4784 case ISD::ADD: return C1 + C2;
4785 case ISD::SUB: return C1 - C2;
4786 case ISD::MUL: return C1 * C2;
4787 case ISD::AND: return C1 & C2;
4788 case ISD::OR: return C1 | C2;
4789 case ISD::XOR: return C1 ^ C2;
4790 case ISD::SHL: return C1 << C2;
4791 case ISD::SRL: return C1.lshr(C2);
4792 case ISD::SRA: return C1.ashr(C2);
4793 case ISD::ROTL: return C1.rotl(C2);
4794 case ISD::ROTR: return C1.rotr(C2);
4795 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
4796 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
4797 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
4798 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
4799 case ISD::SADDSAT: return C1.sadd_sat(C2);
4800 case ISD::UADDSAT: return C1.uadd_sat(C2);
4801 case ISD::SSUBSAT: return C1.ssub_sat(C2);
4802 case ISD::USUBSAT: return C1.usub_sat(C2);
4803 case ISD::UDIV:
4804 if (!C2.getBoolValue())
4805 break;
4806 return C1.udiv(C2);
4807 case ISD::UREM:
4808 if (!C2.getBoolValue())
4809 break;
4810 return C1.urem(C2);
4811 case ISD::SDIV:
4812 if (!C2.getBoolValue())
4813 break;
4814 return C1.sdiv(C2);
4815 case ISD::SREM:
4816 if (!C2.getBoolValue())
4817 break;
4818 return C1.srem(C2);
4819 }
4820 return llvm::None;
4821}
4822
4823SDValue SelectionDAG::FoldSymbolOffset(unsigned Opcode, EVT VT,
4824 const GlobalAddressSDNode *GA,
4825 const SDNode *N2) {
4826 if (GA->getOpcode() != ISD::GlobalAddress)
4827 return SDValue();
4828 if (!TLI->isOffsetFoldingLegal(GA))
4829 return SDValue();
4830 auto *C2 = dyn_cast<ConstantSDNode>(N2);
4831 if (!C2)
4832 return SDValue();
4833 int64_t Offset = C2->getSExtValue();
4834 switch (Opcode) {
4835 case ISD::ADD: break;
4836 case ISD::SUB: Offset = -uint64_t(Offset); break;
4837 default: return SDValue();
4838 }
4839 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
4840 GA->getOffset() + uint64_t(Offset));
4841}
4842
4843bool SelectionDAG::isUndef(unsigned Opcode, ArrayRef<SDValue> Ops) {
4844 switch (Opcode) {
4845 case ISD::SDIV:
4846 case ISD::UDIV:
4847 case ISD::SREM:
4848 case ISD::UREM: {
4849 // If a divisor is zero/undef or any element of a divisor vector is
4850 // zero/undef, the whole op is undef.
4851 assert(Ops.size() == 2 && "Div/rem should have 2 operands")((Ops.size() == 2 && "Div/rem should have 2 operands"
) ? static_cast<void> (0) : __assert_fail ("Ops.size() == 2 && \"Div/rem should have 2 operands\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4851, __PRETTY_FUNCTION__))
;
4852 SDValue Divisor = Ops[1];
4853 if (Divisor.isUndef() || isNullConstant(Divisor))
4854 return true;
4855
4856 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
4857 llvm::any_of(Divisor->op_values(),
4858 [](SDValue V) { return V.isUndef() ||
4859 isNullConstant(V); });
4860 // TODO: Handle signed overflow.
4861 }
4862 // TODO: Handle oversized shifts.
4863 default:
4864 return false;
4865 }
4866}
4867
4868SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL,
4869 EVT VT, ArrayRef<SDValue> Ops) {
4870 // If the opcode is a target-specific ISD node, there's nothing we can
4871 // do here and the operand rules may not line up with the below, so
4872 // bail early.
4873 if (Opcode >= ISD::BUILTIN_OP_END)
4874 return SDValue();
4875
4876 // For now, the array Ops should only contain two values.
4877 // This enforcement will be removed once this function is merged with
4878 // FoldConstantVectorArithmetic
4879 if (Ops.size() != 2)
4880 return SDValue();
4881
4882 if (isUndef(Opcode, Ops))
4883 return getUNDEF(VT);
4884
4885 SDNode *N1 = Ops[0].getNode();
4886 SDNode *N2 = Ops[1].getNode();
4887
4888 // Handle the case of two scalars.
4889 if (auto *C1 = dyn_cast<ConstantSDNode>(N1)) {
4890 if (auto *C2 = dyn_cast<ConstantSDNode>(N2)) {
4891 if (C1->isOpaque() || C2->isOpaque())
4892 return SDValue();
4893
4894 Optional<APInt> FoldAttempt =
4895 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
4896 if (!FoldAttempt)
4897 return SDValue();
4898
4899 SDValue Folded = getConstant(FoldAttempt.getValue(), DL, VT);
4900 assert((!Folded || !VT.isVector()) &&(((!Folded || !VT.isVector()) && "Can't fold vectors ops with scalar operands"
) ? static_cast<void> (0) : __assert_fail ("(!Folded || !VT.isVector()) && \"Can't fold vectors ops with scalar operands\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4901, __PRETTY_FUNCTION__))
4901 "Can't fold vectors ops with scalar operands")(((!Folded || !VT.isVector()) && "Can't fold vectors ops with scalar operands"
) ? static_cast<void> (0) : __assert_fail ("(!Folded || !VT.isVector()) && \"Can't fold vectors ops with scalar operands\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4901, __PRETTY_FUNCTION__))
;
4902 return Folded;
4903 }
4904 }
4905
4906 // fold (add Sym, c) -> Sym+c
4907 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N1))
4908 return FoldSymbolOffset(Opcode, VT, GA, N2);
4909 if (TLI->isCommutativeBinOp(Opcode))
4910 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N2))
4911 return FoldSymbolOffset(Opcode, VT, GA, N1);
4912
4913 // For vectors, extract each constant element and fold them individually.
4914 // Either input may be an undef value.
4915 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
4916 if (!BV1 && !N1->isUndef())
4917 return SDValue();
4918 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
4919 if (!BV2 && !N2->isUndef())
4920 return SDValue();
4921 // If both operands are undef, that's handled the same way as scalars.
4922 if (!BV1 && !BV2)
4923 return SDValue();
4924
4925 assert((!BV1 || !BV2 || BV1->getNumOperands() == BV2->getNumOperands()) &&(((!BV1 || !BV2 || BV1->getNumOperands() == BV2->getNumOperands
()) && "Vector binop with different number of elements in operands?"
) ? static_cast<void> (0) : __assert_fail ("(!BV1 || !BV2 || BV1->getNumOperands() == BV2->getNumOperands()) && \"Vector binop with different number of elements in operands?\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4926, __PRETTY_FUNCTION__))
4926 "Vector binop with different number of elements in operands?")(((!BV1 || !BV2 || BV1->getNumOperands() == BV2->getNumOperands
()) && "Vector binop with different number of elements in operands?"
) ? static_cast<void> (0) : __assert_fail ("(!BV1 || !BV2 || BV1->getNumOperands() == BV2->getNumOperands()) && \"Vector binop with different number of elements in operands?\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4926, __PRETTY_FUNCTION__))
;
4927
4928 EVT SVT = VT.getScalarType();
4929 EVT LegalSVT = SVT;
4930 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
4931 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
4932 if (LegalSVT.bitsLT(SVT))
4933 return SDValue();
4934 }
4935 SmallVector<SDValue, 4> Outputs;
4936 unsigned NumOps = BV1 ? BV1->getNumOperands() : BV2->getNumOperands();
4937 for (unsigned I = 0; I != NumOps; ++I) {
4938 SDValue V1 = BV1 ? BV1->getOperand(I) : getUNDEF(SVT);
4939 SDValue V2 = BV2 ? BV2->getOperand(I) : getUNDEF(SVT);
4940 if (SVT.isInteger()) {
4941 if (V1->getValueType(0).bitsGT(SVT))
4942 V1 = getNode(ISD::TRUNCATE, DL, SVT, V1);
4943 if (V2->getValueType(0).bitsGT(SVT))
4944 V2 = getNode(ISD::TRUNCATE, DL, SVT, V2);
4945 }
4946
4947 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
4948 return SDValue();
4949
4950 // Fold one vector element.
4951 SDValue ScalarResult = getNode(Opcode, DL, SVT, V1, V2);
4952 if (LegalSVT != SVT)
4953 ScalarResult = getNode(ISD::SIGN_EXTEND, DL, LegalSVT, ScalarResult);
4954
4955 // Scalar folding only succeeded if the result is a constant or UNDEF.
4956 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
4957 ScalarResult.getOpcode() != ISD::ConstantFP)
4958 return SDValue();
4959 Outputs.push_back(ScalarResult);
4960 }
4961
4962 assert(VT.getVectorNumElements() == Outputs.size() &&((VT.getVectorNumElements() == Outputs.size() && "Vector size mismatch!"
) ? static_cast<void> (0) : __assert_fail ("VT.getVectorNumElements() == Outputs.size() && \"Vector size mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4963, __PRETTY_FUNCTION__))
4963 "Vector size mismatch!")((VT.getVectorNumElements() == Outputs.size() && "Vector size mismatch!"
) ? static_cast<void> (0) : __assert_fail ("VT.getVectorNumElements() == Outputs.size() && \"Vector size mismatch!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 4963, __PRETTY_FUNCTION__))
;
4964
4965 // We may have a vector type but a scalar result. Create a splat.
4966 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
4967
4968 // Build a big vector out of the scalar elements we generated.
4969 return getBuildVector(VT, SDLoc(), Outputs);
4970}
4971
4972// TODO: Merge with FoldConstantArithmetic
4973SDValue SelectionDAG::FoldConstantVectorArithmetic(unsigned Opcode,
4974 const SDLoc &DL, EVT VT,
4975 ArrayRef<SDValue> Ops,
4976 const SDNodeFlags Flags) {
4977 // If the opcode is a target-specific ISD node, there's nothing we can
4978 // do here and the operand rules may not line up with the below, so
4979 // bail early.
4980 if (Opcode >= ISD::BUILTIN_OP_END)
4981 return SDValue();
4982
4983 if (isUndef(Opcode, Ops))
4984 return getUNDEF(VT);
4985
4986 // We can only fold vectors - maybe merge with FoldConstantArithmetic someday?
4987 if (!VT.isVector())
4988 return SDValue();
4989
4990 unsigned NumElts = VT.getVectorNumElements();
4991
4992 auto IsScalarOrSameVectorSize = [&](const SDValue &Op) {
4993 return !Op.getValueType().isVector() ||
4994 Op.getValueType().getVectorNumElements() == NumElts;
4995 };
4996
4997 auto IsConstantBuildVectorOrUndef = [&](const SDValue &Op) {
4998 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Op);
4999 return (Op.isUndef()) || (Op.getOpcode() == ISD::CONDCODE) ||
5000 (BV && BV->isConstant());
5001 };
5002
5003 // All operands must be vector types with the same number of elements as
5004 // the result type and must be either UNDEF or a build vector of constant
5005 // or UNDEF scalars.
5006 if (!llvm::all_of(Ops, IsConstantBuildVectorOrUndef) ||
5007 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
5008 return SDValue();
5009
5010 // If we are comparing vectors, then the result needs to be a i1 boolean
5011 // that is then sign-extended back to the legal result type.
5012 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
5013
5014 // Find legal integer scalar type for constant promotion and
5015 // ensure that its scalar size is at least as large as source.
5016 EVT LegalSVT = VT.getScalarType();
5017 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
5018 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
5019 if (LegalSVT.bitsLT(VT.getScalarType()))
5020 return SDValue();
5021 }
5022
5023 // Constant fold each scalar lane separately.
5024 SmallVector<SDValue, 4> ScalarResults;
5025 for (unsigned i = 0; i != NumElts; i++) {
5026 SmallVector<SDValue, 4> ScalarOps;
5027 for (SDValue Op : Ops) {
5028 EVT InSVT = Op.getValueType().getScalarType();
5029 BuildVectorSDNode *InBV = dyn_cast<BuildVectorSDNode>(Op);
5030 if (!InBV) {
5031 // We've checked that this is UNDEF or a constant of some kind.
5032 if (Op.isUndef())
5033 ScalarOps.push_back(getUNDEF(InSVT));
5034 else
5035 ScalarOps.push_back(Op);
5036 continue;
5037 }
5038
5039 SDValue ScalarOp = InBV->getOperand(i);
5040 EVT ScalarVT = ScalarOp.getValueType();
5041
5042 // Build vector (integer) scalar operands may need implicit
5043 // truncation - do this before constant folding.
5044 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT))
5045 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
5046
5047 ScalarOps.push_back(ScalarOp);
5048 }
5049
5050 // Constant fold the scalar operands.
5051 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
5052
5053 // Legalize the (integer) scalar constant if necessary.
5054 if (LegalSVT != SVT)
5055 ScalarResult = getNode(ISD::SIGN_EXTEND, DL, LegalSVT, ScalarResult);
5056
5057 // Scalar folding only succeeded if the result is a constant or UNDEF.
5058 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
5059 ScalarResult.getOpcode() != ISD::ConstantFP)
5060 return SDValue();
5061 ScalarResults.push_back(ScalarResult);
5062 }
5063
5064 SDValue V = getBuildVector(VT, DL, ScalarResults);
5065 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
5066 return V;
5067}
5068
5069SDValue SelectionDAG::foldConstantFPMath(unsigned Opcode, const SDLoc &DL,
5070 EVT VT, SDValue N1, SDValue N2) {
5071 // TODO: We don't do any constant folding for strict FP opcodes here, but we
5072 // should. That will require dealing with a potentially non-default
5073 // rounding mode, checking the "opStatus" return value from the APFloat
5074 // math calculations, and possibly other variations.
5075 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
5076 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
5077 if (N1CFP && N2CFP) {
5078 APFloat C1 = N1CFP->getValueAPF(), C2 = N2CFP->getValueAPF();
5079 switch (Opcode) {
5080 case ISD::FADD:
5081 C1.add(C2, APFloat::rmNearestTiesToEven);
5082 return getConstantFP(C1, DL, VT);
5083 case ISD::FSUB:
5084 C1.subtract(C2, APFloat::rmNearestTiesToEven);
5085 return getConstantFP(C1, DL, VT);
5086 case ISD::FMUL:
5087 C1.multiply(C2, APFloat::rmNearestTiesToEven);
5088 return getConstantFP(C1, DL, VT);
5089 case ISD::FDIV:
5090 C1.divide(C2, APFloat::rmNearestTiesToEven);
5091 return getConstantFP(C1, DL, VT);
5092 case ISD::FREM:
5093 C1.mod(C2);
5094 return getConstantFP(C1, DL, VT);
5095 case ISD::FCOPYSIGN:
5096 C1.copySign(C2);
5097 return getConstantFP(C1, DL, VT);
5098 default: break;
5099 }
5100 }
5101 if (N1CFP && Opcode == ISD::FP_ROUND) {
5102 APFloat C1 = N1CFP->getValueAPF(); // make copy
5103 bool Unused;
5104 // This can return overflow, underflow, or inexact; we don't care.
5105 // FIXME need to be more flexible about rounding mode.
5106 (void) C1.convert(EVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
5107 &Unused);
5108 return getConstantFP(C1, DL, VT);
5109 }
5110
5111 switch (Opcode) {
5112 case ISD::FSUB:
5113 // -0.0 - undef --> undef (consistent with "fneg undef")
5114 if (N1CFP && N1CFP->getValueAPF().isNegZero() && N2.isUndef())
5115 return getUNDEF(VT);
5116 LLVM_FALLTHROUGH[[gnu::fallthrough]];
5117
5118 case ISD::FADD:
5119 case ISD::FMUL:
5120 case ISD::FDIV:
5121 case ISD::FREM:
5122 // If both operands are undef, the result is undef. If 1 operand is undef,
5123 // the result is NaN. This should match the behavior of the IR optimizer.
5124 if (N1.isUndef() && N2.isUndef())
5125 return getUNDEF(VT);
5126 if (N1.isUndef() || N2.isUndef())
5127 return getConstantFP(APFloat::getNaN(EVTToAPFloatSemantics(VT)), DL, VT);
5128 }
5129 return SDValue();
5130}
5131
5132SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5133 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
5134 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5135 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
5136 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
5137 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
5138
5139 // Canonicalize constant to RHS if commutative.
5140 if (TLI->isCommutativeBinOp(Opcode)) {
5141 if (N1C && !N2C) {
5142 std::swap(N1C, N2C);
5143 std::swap(N1, N2);
5144 } else if (N1CFP && !N2CFP) {
5145 std::swap(N1CFP, N2CFP);
5146 std::swap(N1, N2);
5147 }
5148 }
5149
5150 switch (Opcode) {
5151 default: break;
5152 case ISD::TokenFactor:
5153 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&((VT == MVT::Other && N1.getValueType() == MVT::Other
&& N2.getValueType() == MVT::Other && "Invalid token factor!"
) ? static_cast<void> (0) : __assert_fail ("VT == MVT::Other && N1.getValueType() == MVT::Other && N2.getValueType() == MVT::Other && \"Invalid token factor!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5154, __PRETTY_FUNCTION__))
5154 N2.getValueType() == MVT::Other && "Invalid token factor!")((VT == MVT::Other && N1.getValueType() == MVT::Other
&& N2.getValueType() == MVT::Other && "Invalid token factor!"
) ? static_cast<void> (0) : __assert_fail ("VT == MVT::Other && N1.getValueType() == MVT::Other && N2.getValueType() == MVT::Other && \"Invalid token factor!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5154, __PRETTY_FUNCTION__))
;
5155 // Fold trivial token factors.
5156 if (N1.getOpcode() == ISD::EntryToken) return N2;
5157 if (N2.getOpcode() == ISD::EntryToken) return N1;
5158 if (N1 == N2) return N1;
5159 break;
5160 case ISD::BUILD_VECTOR: {
5161 // Attempt to simplify BUILD_VECTOR.
5162 SDValue Ops[] = {N1, N2};
5163 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
5164 return V;
5165 break;
5166 }
5167 case ISD::CONCAT_VECTORS: {
5168 SDValue Ops[] = {N1, N2};
5169 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
5170 return V;
5171 break;
5172 }
5173 case ISD::AND:
5174 assert(VT.isInteger() && "This operator does not apply to FP types!")((VT.isInteger() && "This operator does not apply to FP types!"
) ? static_cast<void> (0) : __assert_fail ("VT.isInteger() && \"This operator does not apply to FP types!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5174, __PRETTY_FUNCTION__))
;
5175 assert(N1.getValueType() == N2.getValueType() &&((N1.getValueType() == N2.getValueType() && N1.getValueType
() == VT && "Binary operator types must match!") ? static_cast
<void> (0) : __assert_fail ("N1.getValueType() == N2.getValueType() && N1.getValueType() == VT && \"Binary operator types must match!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5176, __PRETTY_FUNCTION__))
5176 N1.getValueType() == VT && "Binary operator types must match!")((N1.getValueType() == N2.getValueType() && N1.getValueType
() == VT && "Binary operator types must match!") ? static_cast
<void> (0) : __assert_fail ("N1.getValueType() == N2.getValueType() && N1.getValueType() == VT && \"Binary operator types must match!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5176, __PRETTY_FUNCTION__))
;
5177 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
5178 // worth handling here.
5179 if (N2C && N2C->isNullValue())
5180 return N2;
5181 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
5182 return N1;
5183 break;
5184 case ISD::OR:
5185 case ISD::XOR:
5186 case ISD::ADD:
5187 case ISD::SUB:
5188 assert(VT.isInteger() && "This operator does not apply to FP types!")((VT.isInteger() && "This operator does not apply to FP types!"
) ? static_cast<void> (0) : __assert_fail ("VT.isInteger() && \"This operator does not apply to FP types!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5188, __PRETTY_FUNCTION__))
;
5189 assert(N1.getValueType() == N2.getValueType() &&((N1.getValueType() == N2.getValueType() && N1.getValueType
() == VT && "Binary operator types must match!") ? static_cast
<void> (0) : __assert_fail ("N1.getValueType() == N2.getValueType() && N1.getValueType() == VT && \"Binary operator types must match!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5190, __PRETTY_FUNCTION__))
5190 N1.getValueType() == VT && "Binary operator types must match!")((N1.getValueType() == N2.getValueType() && N1.getValueType
() == VT && "Binary operator types must match!") ? static_cast
<void> (0) : __assert_fail ("N1.getValueType() == N2.getValueType() && N1.getValueType() == VT && \"Binary operator types must match!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5190, __PRETTY_FUNCTION__))
;
5191 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
5192 // it's worth handling here.
5193 if (N2C && N2C->isNullValue())
5194 return N1;
5195 break;
5196 case ISD::MUL:
5197 assert(VT.isInteger() && "This operator does not apply to FP types!")((VT.isInteger() && "This operator does not apply to FP types!"
) ? static_cast<void> (0) : __assert_fail ("VT.isInteger() && \"This operator does not apply to FP types!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5197, __PRETTY_FUNCTION__))
;
5198 assert(N1.getValueType() == N2.getValueType() &&((N1.getValueType() == N2.getValueType() && N1.getValueType
() == VT && "Binary operator types must match!") ? static_cast
<void> (0) : __assert_fail ("N1.getValueType() == N2.getValueType() && N1.getValueType() == VT && \"Binary operator types must match!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5199, __PRETTY_FUNCTION__))
5199 N1.getValueType() == VT && "Binary operator types must match!")((N1.getValueType() == N2.getValueType() && N1.getValueType
() == VT && "Binary operator types must match!") ? static_cast
<void> (0) : __assert_fail ("N1.getValueType() == N2.getValueType() && N1.getValueType() == VT && \"Binary operator types must match!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5199, __PRETTY_FUNCTION__))
;
5200 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
5201 APInt MulImm = cast<ConstantSDNode>(N1->getOperand(0))->getAPIntValue();
5202 APInt N2CImm = N2C->getAPIntValue();
5203 return getVScale(DL, VT, MulImm * N2CImm);
5204 }
5205 break;
5206 case ISD::UDIV:
5207 case ISD::UREM:
5208 case ISD::MULHU:
5209 case ISD::MULHS:
5210 case ISD::SDIV:
5211 case ISD::SREM:
5212 case ISD::SMIN:
5213 case ISD::SMAX:
5214 case ISD::UMIN:
5215 case ISD::UMAX:
5216 case ISD::SADDSAT:
5217 case ISD::SSUBSAT:
5218 case ISD::UADDSAT:
5219 case ISD::USUBSAT:
5220 assert(VT.isInteger() && "This operator does not apply to FP types!")((VT.isInteger() && "This operator does not apply to FP types!"
) ? static_cast<void> (0) : __assert_fail ("VT.isInteger() && \"This operator does not apply to FP types!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5220, __PRETTY_FUNCTION__))
;
5221 assert(N1.getValueType() == N2.getValueType() &&((N1.getValueType() == N2.getValueType() && N1.getValueType
() == VT && "Binary operator types must match!") ? static_cast
<void> (0) : __assert_fail ("N1.getValueType() == N2.getValueType() && N1.getValueType() == VT && \"Binary operator types must match!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5222, __PRETTY_FUNCTION__))
5222 N1.getValueType() == VT && "Binary operator types must match!")((N1.getValueType() == N2.getValueType() && N1.getValueType
() == VT && "Binary operator types must match!") ? static_cast
<void> (0) : __assert_fail ("N1.getValueType() == N2.getValueType() && N1.getValueType() == VT && \"Binary operator types must match!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5222, __PRETTY_FUNCTION__))
;
5223 break;
5224 case ISD::FADD:
5225 case ISD::FSUB:
5226 case ISD::FMUL:
5227 case ISD::FDIV:
5228 case ISD::FREM:
5229 assert(VT.isFloatingPoint() && "This operator only applies to FP types!")((VT.isFloatingPoint() && "This operator only applies to FP types!"
) ? static_cast<void> (0) : __assert_fail ("VT.isFloatingPoint() && \"This operator only applies to FP types!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5229, __PRETTY_FUNCTION__))
;
5230 assert(N1.getValueType() == N2.getValueType() &&((N1.getValueType() == N2.getValueType() && N1.getValueType
() == VT && "Binary operator types must match!") ? static_cast
<void> (0) : __assert_fail ("N1.getValueType() == N2.getValueType() && N1.getValueType() == VT && \"Binary operator types must match!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5231, __PRETTY_FUNCTION__))
5231 N1.getValueType() == VT && "Binary operator types must match!")((N1.getValueType() == N2.getValueType() && N1.getValueType
() == VT && "Binary operator types must match!") ? static_cast
<void> (0) : __assert_fail ("N1.getValueType() == N2.getValueType() && N1.getValueType() == VT && \"Binary operator types must match!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5231, __PRETTY_FUNCTION__))
;
5232 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
5233 return V;
5234 break;
5235 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
5236 assert(N1.getValueType() == VT &&((N1.getValueType() == VT && N1.getValueType().isFloatingPoint
() && N2.getValueType().isFloatingPoint() && "Invalid FCOPYSIGN!"
) ? static_cast<void> (0) : __assert_fail ("N1.getValueType() == VT && N1.getValueType().isFloatingPoint() && N2.getValueType().isFloatingPoint() && \"Invalid FCOPYSIGN!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5239, __PRETTY_FUNCTION__))
5237 N1.getValueType().isFloatingPoint() &&((N1.getValueType() == VT && N1.getValueType().isFloatingPoint
() && N2.getValueType().isFloatingPoint() && "Invalid FCOPYSIGN!"
) ? static_cast<void> (0) : __assert_fail ("N1.getValueType() == VT && N1.getValueType().isFloatingPoint() && N2.getValueType().isFloatingPoint() && \"Invalid FCOPYSIGN!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5239, __PRETTY_FUNCTION__))
5238 N2.getValueType().isFloatingPoint() &&((N1.getValueType() == VT && N1.getValueType().isFloatingPoint
() && N2.getValueType().isFloatingPoint() && "Invalid FCOPYSIGN!"
) ? static_cast<void> (0) : __assert_fail ("N1.getValueType() == VT && N1.getValueType().isFloatingPoint() && N2.getValueType().isFloatingPoint() && \"Invalid FCOPYSIGN!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5239, __PRETTY_FUNCTION__))
5239 "Invalid FCOPYSIGN!")((N1.getValueType() == VT && N1.getValueType().isFloatingPoint
() && N2.getValueType().isFloatingPoint() && "Invalid FCOPYSIGN!"
) ? static_cast<void> (0) : __assert_fail ("N1.getValueType() == VT && N1.getValueType().isFloatingPoint() && N2.getValueType().isFloatingPoint() && \"Invalid FCOPYSIGN!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5239, __PRETTY_FUNCTION__))
;
5240 break;
5241 case ISD::SHL:
5242 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
5243 APInt MulImm = cast<ConstantSDNode>(N1->getOperand(0))->getAPIntValue();
5244 APInt ShiftImm = N2C->getAPIntValue();
5245 return getVScale(DL, VT, MulImm << ShiftImm);
5246 }
5247 LLVM_FALLTHROUGH[[gnu::fallthrough]];
5248 case ISD::SRA:
5249 case ISD::SRL:
5250 if (SDValue V = simplifyShift(N1, N2))
5251 return V;
5252 LLVM_FALLTHROUGH[[gnu::fallthrough]];
5253 case ISD::ROTL:
5254 case ISD::ROTR:
5255 assert(VT == N1.getValueType() &&((VT == N1.getValueType() && "Shift operators return type must be the same as their first arg"
) ? static_cast<void> (0) : __assert_fail ("VT == N1.getValueType() && \"Shift operators return type must be the same as their first arg\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5256, __PRETTY_FUNCTION__))
5256 "Shift operators return type must be the same as their first arg")((VT == N1.getValueType() && "Shift operators return type must be the same as their first arg"
) ? static_cast<void> (0) : __assert_fail ("VT == N1.getValueType() && \"Shift operators return type must be the same as their first arg\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5256, __PRETTY_FUNCTION__))
;
5257 assert(VT.isInteger() && N2.getValueType().isInteger() &&((VT.isInteger() && N2.getValueType().isInteger() &&
"Shifts only work on integers") ? static_cast<void> (0
) : __assert_fail ("VT.isInteger() && N2.getValueType().isInteger() && \"Shifts only work on integers\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5258, __PRETTY_FUNCTION__))
5258 "Shifts only work on integers")((VT.isInteger() && N2.getValueType().isInteger() &&
"Shifts only work on integers") ? static_cast<void> (0
) : __assert_fail ("VT.isInteger() && N2.getValueType().isInteger() && \"Shifts only work on integers\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5258, __PRETTY_FUNCTION__))
;
5259 assert((!VT.isVector() || VT == N2.getValueType()) &&(((!VT.isVector() || VT == N2.getValueType()) && "Vector shift amounts must be in the same as their first arg"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT == N2.getValueType()) && \"Vector shift amounts must be in the same as their first arg\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5260, __PRETTY_FUNCTION__))
5260 "Vector shift amounts must be in the same as their first arg")(((!VT.isVector() || VT == N2.getValueType()) && "Vector shift amounts must be in the same as their first arg"
) ? static_cast<void> (0) : __assert_fail ("(!VT.isVector() || VT == N2.getValueType()) && \"Vector shift amounts must be in the same as their first arg\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5260, __PRETTY_FUNCTION__))
;
5261 // Verify that the shift amount VT is big enough to hold valid shift
5262 // amounts. This catches things like trying to shift an i1024 value by an
5263 // i8, which is easy to fall into in generic code that uses
5264 // TLI.getShiftAmount().
5265 assert(N2.getValueSizeInBits() >= Log2_32_Ceil(N1.getValueSizeInBits()) &&((N2.getValueSizeInBits() >= Log2_32_Ceil(N1.getValueSizeInBits
()) && "Invalid use of small shift amount with oversized value!"
) ? static_cast<void> (0) : __assert_fail ("N2.getValueSizeInBits() >= Log2_32_Ceil(N1.getValueSizeInBits()) && \"Invalid use of small shift amount with oversized value!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5266, __PRETTY_FUNCTION__))
5266 "Invalid use of small shift amount with oversized value!")((N2.getValueSizeInBits() >= Log2_32_Ceil(N1.getValueSizeInBits
()) && "Invalid use of small shift amount with oversized value!"
) ? static_cast<void> (0) : __assert_fail ("N2.getValueSizeInBits() >= Log2_32_Ceil(N1.getValueSizeInBits()) && \"Invalid use of small shift amount with oversized value!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5266, __PRETTY_FUNCTION__))
;
5267
5268 // Always fold shifts of i1 values so the code generator doesn't need to
5269 // handle them. Since we know the size of the shift has to be less than the
5270 // size of the value, the shift/rotate count is guaranteed to be zero.
5271 if (VT == MVT::i1)
5272 return N1;
5273 if (N2C && N2C->isNullValue())
5274 return N1;
5275 break;
5276 case ISD::FP_ROUND:
5277 assert(VT.isFloatingPoint() &&((VT.isFloatingPoint() && N1.getValueType().isFloatingPoint
() && VT.bitsLE(N1.getValueType()) && N2C &&
(N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1)
&& "Invalid FP_ROUND!") ? static_cast<void> (0
) : __assert_fail ("VT.isFloatingPoint() && N1.getValueType().isFloatingPoint() && VT.bitsLE(N1.getValueType()) && N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) && \"Invalid FP_ROUND!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5281, __PRETTY_FUNCTION__))
5278 N1.getValueType().isFloatingPoint() &&((VT.isFloatingPoint() && N1.getValueType().isFloatingPoint
() && VT.bitsLE(N1.getValueType()) && N2C &&
(N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1)
&& "Invalid FP_ROUND!") ? static_cast<void> (0
) : __assert_fail ("VT.isFloatingPoint() && N1.getValueType().isFloatingPoint() && VT.bitsLE(N1.getValueType()) && N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) && \"Invalid FP_ROUND!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5281, __PRETTY_FUNCTION__))
5279 VT.bitsLE(N1.getValueType()) &&((VT.isFloatingPoint() && N1.getValueType().isFloatingPoint
() && VT.bitsLE(N1.getValueType()) && N2C &&
(N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1)
&& "Invalid FP_ROUND!") ? static_cast<void> (0
) : __assert_fail ("VT.isFloatingPoint() && N1.getValueType().isFloatingPoint() && VT.bitsLE(N1.getValueType()) && N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) && \"Invalid FP_ROUND!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5281, __PRETTY_FUNCTION__))
5280 N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&((VT.isFloatingPoint() && N1.getValueType().isFloatingPoint
() && VT.bitsLE(N1.getValueType()) && N2C &&
(N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1)
&& "Invalid FP_ROUND!") ? static_cast<void> (0
) : __assert_fail ("VT.isFloatingPoint() && N1.getValueType().isFloatingPoint() && VT.bitsLE(N1.getValueType()) && N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) && \"Invalid FP_ROUND!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5281, __PRETTY_FUNCTION__))
5281 "Invalid FP_ROUND!")((VT.isFloatingPoint() && N1.getValueType().isFloatingPoint
() && VT.bitsLE(N1.getValueType()) && N2C &&
(N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1)
&& "Invalid FP_ROUND!") ? static_cast<void> (0
) : __assert_fail ("VT.isFloatingPoint() && N1.getValueType().isFloatingPoint() && VT.bitsLE(N1.getValueType()) && N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) && \"Invalid FP_ROUND!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5281, __PRETTY_FUNCTION__))
;
5282 if (N1.getValueType() == VT) return N1; // noop conversion.
5283 break;
5284 case ISD::AssertSext:
5285 case ISD::AssertZext: {
5286 EVT EVT = cast<VTSDNode>(N2)->getVT();
5287 assert(VT == N1.getValueType() && "Not an inreg extend!")((VT == N1.getValueType() && "Not an inreg extend!") ?
static_cast<void> (0) : __assert_fail ("VT == N1.getValueType() && \"Not an inreg extend!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5287, __PRETTY_FUNCTION__))
;
5288 assert(VT.isInteger() && EVT.isInteger() &&((VT.isInteger() && EVT.isInteger() && "Cannot *_EXTEND_INREG FP types"
) ? static_cast<void> (0) : __assert_fail ("VT.isInteger() && EVT.isInteger() && \"Cannot *_EXTEND_INREG FP types\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5289, __PRETTY_FUNCTION__))
5289 "Cannot *_EXTEND_INREG FP types")((VT.isInteger() && EVT.isInteger() && "Cannot *_EXTEND_INREG FP types"
) ? static_cast<void> (0) : __assert_fail ("VT.isInteger() && EVT.isInteger() && \"Cannot *_EXTEND_INREG FP types\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5289, __PRETTY_FUNCTION__))
;
5290 assert(!EVT.isVector() &&((!EVT.isVector() && "AssertSExt/AssertZExt type should be the vector element type "
"rather than the vector type!") ? static_cast<void> (0
) : __assert_fail ("!EVT.isVector() && \"AssertSExt/AssertZExt type should be the vector element type \" \"rather than the vector type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5292, __PRETTY_FUNCTION__))
5291 "AssertSExt/AssertZExt type should be the vector element type "((!EVT.isVector() && "AssertSExt/AssertZExt type should be the vector element type "
"rather than the vector type!") ? static_cast<void> (0
) : __assert_fail ("!EVT.isVector() && \"AssertSExt/AssertZExt type should be the vector element type \" \"rather than the vector type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5292, __PRETTY_FUNCTION__))
5292 "rather than the vector type!")((!EVT.isVector() && "AssertSExt/AssertZExt type should be the vector element type "
"rather than the vector type!") ? static_cast<void> (0
) : __assert_fail ("!EVT.isVector() && \"AssertSExt/AssertZExt type should be the vector element type \" \"rather than the vector type!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5292, __PRETTY_FUNCTION__))
;
5293 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!")((EVT.bitsLE(VT.getScalarType()) && "Not extending!")
? static_cast<void> (0) : __assert_fail ("EVT.bitsLE(VT.getScalarType()) && \"Not extending!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5293, __PRETTY_FUNCTION__))
;
5294 if (VT.getScalarType() == EVT) return N1; // noop assertion.
5295 break;
5296 }
5297 case ISD::SIGN_EXTEND_INREG: {
5298 EVT EVT = cast<VTSDNode>(N2)->getVT();
5299 assert(VT == N1.getValueType() && "Not an inreg extend!")((VT == N1.getValueType() && "Not an inreg extend!") ?
static_cast<void> (0) : __assert_fail ("VT == N1.getValueType() && \"Not an inreg extend!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5299, __PRETTY_FUNCTION__))
;
5300 assert(VT.isInteger() && EVT.isInteger() &&((VT.isInteger() && EVT.isInteger() && "Cannot *_EXTEND_INREG FP types"
) ? static_cast<void> (0) : __assert_fail ("VT.isInteger() && EVT.isInteger() && \"Cannot *_EXTEND_INREG FP types\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5301, __PRETTY_FUNCTION__))
5301 "Cannot *_EXTEND_INREG FP types")((VT.isInteger() && EVT.isInteger() && "Cannot *_EXTEND_INREG FP types"
) ? static_cast<void> (0) : __assert_fail ("VT.isInteger() && EVT.isInteger() && \"Cannot *_EXTEND_INREG FP types\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5301, __PRETTY_FUNCTION__))
;
5302 assert(EVT.isVector() == VT.isVector() &&((EVT.isVector() == VT.isVector() && "SIGN_EXTEND_INREG type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("EVT.isVector() == VT.isVector() && \"SIGN_EXTEND_INREG type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5304, __PRETTY_FUNCTION__))
5303 "SIGN_EXTEND_INREG type should be vector iff the operand "((EVT.isVector() == VT.isVector() && "SIGN_EXTEND_INREG type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("EVT.isVector() == VT.isVector() && \"SIGN_EXTEND_INREG type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5304, __PRETTY_FUNCTION__))
5304 "type is vector!")((EVT.isVector() == VT.isVector() && "SIGN_EXTEND_INREG type should be vector iff the operand "
"type is vector!") ? static_cast<void> (0) : __assert_fail
("EVT.isVector() == VT.isVector() && \"SIGN_EXTEND_INREG type should be vector iff the operand \" \"type is vector!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5304, __PRETTY_FUNCTION__))
;
5305 assert((!EVT.isVector() ||(((!EVT.isVector() || EVT.getVectorNumElements() == VT.getVectorNumElements
()) && "Vector element counts must match in SIGN_EXTEND_INREG"
) ? static_cast<void> (0) : __assert_fail ("(!EVT.isVector() || EVT.getVectorNumElements() == VT.getVectorNumElements()) && \"Vector element counts must match in SIGN_EXTEND_INREG\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5307, __PRETTY_FUNCTION__))
5306 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&(((!EVT.isVector() || EVT.getVectorNumElements() == VT.getVectorNumElements
()) && "Vector element counts must match in SIGN_EXTEND_INREG"
) ? static_cast<void> (0) : __assert_fail ("(!EVT.isVector() || EVT.getVectorNumElements() == VT.getVectorNumElements()) && \"Vector element counts must match in SIGN_EXTEND_INREG\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5307, __PRETTY_FUNCTION__))
5307 "Vector element counts must match in SIGN_EXTEND_INREG")(((!EVT.isVector() || EVT.getVectorNumElements() == VT.getVectorNumElements
()) && "Vector element counts must match in SIGN_EXTEND_INREG"
) ? static_cast<void> (0) : __assert_fail ("(!EVT.isVector() || EVT.getVectorNumElements() == VT.getVectorNumElements()) && \"Vector element counts must match in SIGN_EXTEND_INREG\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5307, __PRETTY_FUNCTION__))
;
5308 assert(EVT.bitsLE(VT) && "Not extending!")((EVT.bitsLE(VT) && "Not extending!") ? static_cast<
void> (0) : __assert_fail ("EVT.bitsLE(VT) && \"Not extending!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5308, __PRETTY_FUNCTION__))
;
5309 if (EVT == VT) return N1; // Not actually extending
5310
5311 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
5312 unsigned FromBits = EVT.getScalarSizeInBits();
5313 Val <<= Val.getBitWidth() - FromBits;
5314 Val.ashrInPlace(Val.getBitWidth() - FromBits);
5315 return getConstant(Val, DL, ConstantVT);
5316 };
5317
5318 if (N1C) {
5319 const APInt &Val = N1C->getAPIntValue();
5320 return SignExtendInReg(Val, VT);
5321 }
5322 if (ISD::isBuildVectorOfConstantSDNodes(N1.getNode())) {
5323 SmallVector<SDValue, 8> Ops;
5324 llvm::EVT OpVT = N1.getOperand(0).getValueType();
5325 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5326 SDValue Op = N1.getOperand(i);
5327 if (Op.isUndef()) {
5328 Ops.push_back(getUNDEF(OpVT));
5329 continue;
5330 }
5331 ConstantSDNode *C = cast<ConstantSDNode>(Op);
5332 APInt Val = C->getAPIntValue();
5333 Ops.push_back(SignExtendInReg(Val, OpVT));
5334 }
5335 return getBuildVector(VT, DL, Ops);
5336 }
5337 break;
5338 }
5339 case ISD::EXTRACT_VECTOR_ELT:
5340 assert(VT.getSizeInBits() >= N1.getValueType().getScalarSizeInBits() &&((VT.getSizeInBits() >= N1.getValueType().getScalarSizeInBits
() && "The result of EXTRACT_VECTOR_ELT must be at least as wide as the element type of the vector."
) ? static_cast<void> (0) : __assert_fail ("VT.getSizeInBits() >= N1.getValueType().getScalarSizeInBits() && \"The result of EXTRACT_VECTOR_ELT must be at least as wide as the element type of the vector.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5342, __PRETTY_FUNCTION__))
5341 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \((VT.getSizeInBits() >= N1.getValueType().getScalarSizeInBits
() && "The result of EXTRACT_VECTOR_ELT must be at least as wide as the element type of the vector."
) ? static_cast<void> (0) : __assert_fail ("VT.getSizeInBits() >= N1.getValueType().getScalarSizeInBits() && \"The result of EXTRACT_VECTOR_ELT must be at least as wide as the element type of the vector.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5342, __PRETTY_FUNCTION__))
5342 element type of the vector.")((VT.getSizeInBits() >= N1.getValueType().getScalarSizeInBits
() && "The result of EXTRACT_VECTOR_ELT must be at least as wide as the element type of the vector."
) ? static_cast<void> (0) : __assert_fail ("VT.getSizeInBits() >= N1.getValueType().getScalarSizeInBits() && \"The result of EXTRACT_VECTOR_ELT must be at least as wide as the element type of the vector.\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5342, __PRETTY_FUNCTION__))
;
5343
5344 // Extract from an undefined value or using an undefined index is undefined.
5345 if (N1.isUndef() || N2.isUndef())
5346 return getUNDEF(VT);
5347
5348 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF
5349 if (N2C && N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
5350 return getUNDEF(VT);
5351
5352 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
5353 // expanding copies of large vectors from registers.
5354 if (N2C &&
5355 N1.getOpcode() == ISD::CONCAT_VECTORS &&
5356 N1.getNumOperands() > 0) {
5357 unsigned Factor =
5358 N1.getOperand(0).getValueType().getVectorNumElements();
5359 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
5360 N1.getOperand(N2C->getZExtValue() / Factor),
5361 getVectorIdxConstant(N2C->getZExtValue() % Factor, DL));
5362 }
5363
5364 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
5365 // expanding large vector constants.
5366 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
5367 SDValue Elt = N1.getOperand(N2C->getZExtValue());
5368
5369 if (VT != Elt.getValueType())
5370 // If the vector element type is not legal, the BUILD_VECTOR operands
5371 // are promoted and implicitly truncated, and the result implicitly
5372 // extended. Make that explicit here.
5373 Elt = getAnyExtOrTrunc(Elt, DL, VT);
5374
5375 return Elt;
5376 }
5377
5378 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
5379 // operations are lowered to scalars.
5380 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
5381 // If the indices are the same, return the inserted element else
5382 // if the indices are known different, extract the element from
5383 // the original vector.
5384 SDValue N1Op2 = N1.getOperand(2);
5385 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
5386
5387 if (N1Op2C && N2C) {
5388 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
5389 if (VT == N1.getOperand(1).getValueType())
5390 return N1.getOperand(1);
5391 else
5392 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
5393 }
5394
5395 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
5396 }
5397 }
5398
5399 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
5400 // when vector types are scalarized and v1iX is legal.
5401 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx)
5402 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
5403 N1.getValueType().getVectorNumElements() == 1) {
5404 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
5405 N1.getOperand(1));
5406 }
5407 break;
5408 case ISD::EXTRACT_ELEMENT:
5409 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!")((N2C && (unsigned)N2C->getZExtValue() < 2 &&
"Bad EXTRACT_ELEMENT!") ? static_cast<void> (0) : __assert_fail
("N2C && (unsigned)N2C->getZExtValue() < 2 && \"Bad EXTRACT_ELEMENT!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5409, __PRETTY_FUNCTION__))
;
5410 assert(!N1.getValueType().isVector() && !VT.isVector() &&((!N1.getValueType().isVector() && !VT.isVector() &&
(N1.getValueType().isInteger() == VT.isInteger()) &&
N1.getValueType() != VT && "Wrong types for EXTRACT_ELEMENT!"
) ? static_cast<void> (0) : __assert_fail ("!N1.getValueType().isVector() && !VT.isVector() && (N1.getValueType().isInteger() == VT.isInteger()) && N1.getValueType() != VT && \"Wrong types for EXTRACT_ELEMENT!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5413, __PRETTY_FUNCTION__))
5411 (N1.getValueType().isInteger() == VT.isInteger()) &&((!N1.getValueType().isVector() && !VT.isVector() &&
(N1.getValueType().isInteger() == VT.isInteger()) &&
N1.getValueType() != VT && "Wrong types for EXTRACT_ELEMENT!"
) ? static_cast<void> (0) : __assert_fail ("!N1.getValueType().isVector() && !VT.isVector() && (N1.getValueType().isInteger() == VT.isInteger()) && N1.getValueType() != VT && \"Wrong types for EXTRACT_ELEMENT!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5413, __PRETTY_FUNCTION__))
5412 N1.getValueType() != VT &&((!N1.getValueType().isVector() && !VT.isVector() &&
(N1.getValueType().isInteger() == VT.isInteger()) &&
N1.getValueType() != VT && "Wrong types for EXTRACT_ELEMENT!"
) ? static_cast<void> (0) : __assert_fail ("!N1.getValueType().isVector() && !VT.isVector() && (N1.getValueType().isInteger() == VT.isInteger()) && N1.getValueType() != VT && \"Wrong types for EXTRACT_ELEMENT!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5413, __PRETTY_FUNCTION__))
5413 "Wrong types for EXTRACT_ELEMENT!")((!N1.getValueType().isVector() && !VT.isVector() &&
(N1.getValueType().isInteger() == VT.isInteger()) &&
N1.getValueType() != VT && "Wrong types for EXTRACT_ELEMENT!"
) ? static_cast<void> (0) : __assert_fail ("!N1.getValueType().isVector() && !VT.isVector() && (N1.getValueType().isInteger() == VT.isInteger()) && N1.getValueType() != VT && \"Wrong types for EXTRACT_ELEMENT!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp"
, 5413, __PRETTY_FUNCTION__))
;
5414
5415 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
5416 // 64-bit integers into 32-bit parts. Instead of building the extract of
5417 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
5418 if (N1.getOpcode() == ISD::BUILD_PAIR)
5419 return N1.getOperand(N2C->getZExtValue());
5420
5421 // EXTRACT_ELEMENT of a constant int is also very common.
5422 if (N1C) {
5423 unsigned ElementSize = VT.getSizeInBits();
5424 unsigned Shift = ElementSize * N2C->getZExtValue();
5425 APInt ShiftedVal = N1C->getAPIntValue().lshr(Shift);
5426 return getConstant(ShiftedVal.trunc(ElementSize), DL, VT);