Bug Summary

File:llvm/lib/Analysis/ConstantFolding.cpp
Warning:line 2574, column 35
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ConstantFolding.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 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/build-llvm/lib/Analysis -resource-dir /usr/lib/llvm-13/lib/clang/13.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/build-llvm/lib/Analysis -I /build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis -I /build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/build-llvm/include -I /build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-13/lib/clang/13.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/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-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/build-llvm/lib/Analysis -fdebug-prefix-map=/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-05-21-192209-21630-1 -x c++ /build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp

/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp

1//===-- ConstantFolding.cpp - Fold instructions into constants ------------===//
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 file defines routines for folding instructions into constants.
10//
11// Also, to supplement the basic IR ConstantExpr simplifications,
12// this file defines some additional folding routines that can make use of
13// DataLayout information. These functions cannot go in IR due to library
14// dependency issues.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm/Analysis/ConstantFolding.h"
19#include "llvm/ADT/APFloat.h"
20#include "llvm/ADT/APInt.h"
21#include "llvm/ADT/APSInt.h"
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/Analysis/TargetFolder.h"
28#include "llvm/Analysis/TargetLibraryInfo.h"
29#include "llvm/Analysis/ValueTracking.h"
30#include "llvm/Analysis/VectorUtils.h"
31#include "llvm/Config/config.h"
32#include "llvm/IR/Constant.h"
33#include "llvm/IR/Constants.h"
34#include "llvm/IR/DataLayout.h"
35#include "llvm/IR/DerivedTypes.h"
36#include "llvm/IR/Function.h"
37#include "llvm/IR/GlobalValue.h"
38#include "llvm/IR/GlobalVariable.h"
39#include "llvm/IR/InstrTypes.h"
40#include "llvm/IR/Instruction.h"
41#include "llvm/IR/Instructions.h"
42#include "llvm/IR/IntrinsicInst.h"
43#include "llvm/IR/Intrinsics.h"
44#include "llvm/IR/IntrinsicsAArch64.h"
45#include "llvm/IR/IntrinsicsAMDGPU.h"
46#include "llvm/IR/IntrinsicsARM.h"
47#include "llvm/IR/IntrinsicsWebAssembly.h"
48#include "llvm/IR/IntrinsicsX86.h"
49#include "llvm/IR/Operator.h"
50#include "llvm/IR/Type.h"
51#include "llvm/IR/Value.h"
52#include "llvm/Support/Casting.h"
53#include "llvm/Support/ErrorHandling.h"
54#include "llvm/Support/KnownBits.h"
55#include "llvm/Support/MathExtras.h"
56#include <cassert>
57#include <cerrno>
58#include <cfenv>
59#include <cmath>
60#include <cstddef>
61#include <cstdint>
62
63using namespace llvm;
64
65namespace {
66
67//===----------------------------------------------------------------------===//
68// Constant Folding internal helper functions
69//===----------------------------------------------------------------------===//
70
71static Constant *foldConstVectorToAPInt(APInt &Result, Type *DestTy,
72 Constant *C, Type *SrcEltTy,
73 unsigned NumSrcElts,
74 const DataLayout &DL) {
75 // Now that we know that the input value is a vector of integers, just shift
76 // and insert them into our result.
77 unsigned BitShift = DL.getTypeSizeInBits(SrcEltTy);
78 for (unsigned i = 0; i != NumSrcElts; ++i) {
79 Constant *Element;
80 if (DL.isLittleEndian())
81 Element = C->getAggregateElement(NumSrcElts - i - 1);
82 else
83 Element = C->getAggregateElement(i);
84
85 if (Element && isa<UndefValue>(Element)) {
86 Result <<= BitShift;
87 continue;
88 }
89
90 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
91 if (!ElementCI)
92 return ConstantExpr::getBitCast(C, DestTy);
93
94 Result <<= BitShift;
95 Result |= ElementCI->getValue().zextOrSelf(Result.getBitWidth());
96 }
97
98 return nullptr;
99}
100
101/// Constant fold bitcast, symbolically evaluating it with DataLayout.
102/// This always returns a non-null constant, but it may be a
103/// ConstantExpr if unfoldable.
104Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
105 assert(CastInst::castIsValid(Instruction::BitCast, C, DestTy) &&(static_cast <bool> (CastInst::castIsValid(Instruction::
BitCast, C, DestTy) && "Invalid constantexpr bitcast!"
) ? void (0) : __assert_fail ("CastInst::castIsValid(Instruction::BitCast, C, DestTy) && \"Invalid constantexpr bitcast!\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 106, __extension__ __PRETTY_FUNCTION__))
106 "Invalid constantexpr bitcast!")(static_cast <bool> (CastInst::castIsValid(Instruction::
BitCast, C, DestTy) && "Invalid constantexpr bitcast!"
) ? void (0) : __assert_fail ("CastInst::castIsValid(Instruction::BitCast, C, DestTy) && \"Invalid constantexpr bitcast!\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 106, __extension__ __PRETTY_FUNCTION__))
;
107
108 // Catch the obvious splat cases.
109 if (C->isNullValue() && !DestTy->isX86_MMXTy() && !DestTy->isX86_AMXTy())
110 return Constant::getNullValue(DestTy);
111 if (C->isAllOnesValue() && !DestTy->isX86_MMXTy() && !DestTy->isX86_AMXTy() &&
112 !DestTy->isPtrOrPtrVectorTy()) // Don't get ones for ptr types!
113 return Constant::getAllOnesValue(DestTy);
114
115 if (auto *VTy = dyn_cast<VectorType>(C->getType())) {
116 // Handle a vector->scalar integer/fp cast.
117 if (isa<IntegerType>(DestTy) || DestTy->isFloatingPointTy()) {
118 unsigned NumSrcElts = cast<FixedVectorType>(VTy)->getNumElements();
119 Type *SrcEltTy = VTy->getElementType();
120
121 // If the vector is a vector of floating point, convert it to vector of int
122 // to simplify things.
123 if (SrcEltTy->isFloatingPointTy()) {
124 unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
125 auto *SrcIVTy = FixedVectorType::get(
126 IntegerType::get(C->getContext(), FPWidth), NumSrcElts);
127 // Ask IR to do the conversion now that #elts line up.
128 C = ConstantExpr::getBitCast(C, SrcIVTy);
129 }
130
131 APInt Result(DL.getTypeSizeInBits(DestTy), 0);
132 if (Constant *CE = foldConstVectorToAPInt(Result, DestTy, C,
133 SrcEltTy, NumSrcElts, DL))
134 return CE;
135
136 if (isa<IntegerType>(DestTy))
137 return ConstantInt::get(DestTy, Result);
138
139 APFloat FP(DestTy->getFltSemantics(), Result);
140 return ConstantFP::get(DestTy->getContext(), FP);
141 }
142 }
143
144 // The code below only handles casts to vectors currently.
145 auto *DestVTy = dyn_cast<VectorType>(DestTy);
146 if (!DestVTy)
147 return ConstantExpr::getBitCast(C, DestTy);
148
149 // If this is a scalar -> vector cast, convert the input into a <1 x scalar>
150 // vector so the code below can handle it uniformly.
151 if (isa<ConstantFP>(C) || isa<ConstantInt>(C)) {
152 Constant *Ops = C; // don't take the address of C!
153 return FoldBitCast(ConstantVector::get(Ops), DestTy, DL);
154 }
155
156 // If this is a bitcast from constant vector -> vector, fold it.
157 if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C))
158 return ConstantExpr::getBitCast(C, DestTy);
159
160 // If the element types match, IR can fold it.
161 unsigned NumDstElt = cast<FixedVectorType>(DestVTy)->getNumElements();
162 unsigned NumSrcElt = cast<FixedVectorType>(C->getType())->getNumElements();
163 if (NumDstElt == NumSrcElt)
164 return ConstantExpr::getBitCast(C, DestTy);
165
166 Type *SrcEltTy = cast<VectorType>(C->getType())->getElementType();
167 Type *DstEltTy = DestVTy->getElementType();
168
169 // Otherwise, we're changing the number of elements in a vector, which
170 // requires endianness information to do the right thing. For example,
171 // bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
172 // folds to (little endian):
173 // <4 x i32> <i32 0, i32 0, i32 1, i32 0>
174 // and to (big endian):
175 // <4 x i32> <i32 0, i32 0, i32 0, i32 1>
176
177 // First thing is first. We only want to think about integer here, so if
178 // we have something in FP form, recast it as integer.
179 if (DstEltTy->isFloatingPointTy()) {
180 // Fold to an vector of integers with same size as our FP type.
181 unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits();
182 auto *DestIVTy = FixedVectorType::get(
183 IntegerType::get(C->getContext(), FPWidth), NumDstElt);
184 // Recursively handle this integer conversion, if possible.
185 C = FoldBitCast(C, DestIVTy, DL);
186
187 // Finally, IR can handle this now that #elts line up.
188 return ConstantExpr::getBitCast(C, DestTy);
189 }
190
191 // Okay, we know the destination is integer, if the input is FP, convert
192 // it to integer first.
193 if (SrcEltTy->isFloatingPointTy()) {
194 unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
195 auto *SrcIVTy = FixedVectorType::get(
196 IntegerType::get(C->getContext(), FPWidth), NumSrcElt);
197 // Ask IR to do the conversion now that #elts line up.
198 C = ConstantExpr::getBitCast(C, SrcIVTy);
199 // If IR wasn't able to fold it, bail out.
200 if (!isa<ConstantVector>(C) && // FIXME: Remove ConstantVector.
201 !isa<ConstantDataVector>(C))
202 return C;
203 }
204
205 // Now we know that the input and output vectors are both integer vectors
206 // of the same size, and that their #elements is not the same. Do the
207 // conversion here, which depends on whether the input or output has
208 // more elements.
209 bool isLittleEndian = DL.isLittleEndian();
210
211 SmallVector<Constant*, 32> Result;
212 if (NumDstElt < NumSrcElt) {
213 // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>)
214 Constant *Zero = Constant::getNullValue(DstEltTy);
215 unsigned Ratio = NumSrcElt/NumDstElt;
216 unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
217 unsigned SrcElt = 0;
218 for (unsigned i = 0; i != NumDstElt; ++i) {
219 // Build each element of the result.
220 Constant *Elt = Zero;
221 unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
222 for (unsigned j = 0; j != Ratio; ++j) {
223 Constant *Src = C->getAggregateElement(SrcElt++);
224 if (Src && isa<UndefValue>(Src))
225 Src = Constant::getNullValue(
226 cast<VectorType>(C->getType())->getElementType());
227 else
228 Src = dyn_cast_or_null<ConstantInt>(Src);
229 if (!Src) // Reject constantexpr elements.
230 return ConstantExpr::getBitCast(C, DestTy);
231
232 // Zero extend the element to the right size.
233 Src = ConstantExpr::getZExt(Src, Elt->getType());
234
235 // Shift it to the right place, depending on endianness.
236 Src = ConstantExpr::getShl(Src,
237 ConstantInt::get(Src->getType(), ShiftAmt));
238 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
239
240 // Mix it in.
241 Elt = ConstantExpr::getOr(Elt, Src);
242 }
243 Result.push_back(Elt);
244 }
245 return ConstantVector::get(Result);
246 }
247
248 // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
249 unsigned Ratio = NumDstElt/NumSrcElt;
250 unsigned DstBitSize = DL.getTypeSizeInBits(DstEltTy);
251
252 // Loop over each source value, expanding into multiple results.
253 for (unsigned i = 0; i != NumSrcElt; ++i) {
254 auto *Element = C->getAggregateElement(i);
255
256 if (!Element) // Reject constantexpr elements.
257 return ConstantExpr::getBitCast(C, DestTy);
258
259 if (isa<UndefValue>(Element)) {
260 // Correctly Propagate undef values.
261 Result.append(Ratio, UndefValue::get(DstEltTy));
262 continue;
263 }
264
265 auto *Src = dyn_cast<ConstantInt>(Element);
266 if (!Src)
267 return ConstantExpr::getBitCast(C, DestTy);
268
269 unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
270 for (unsigned j = 0; j != Ratio; ++j) {
271 // Shift the piece of the value into the right place, depending on
272 // endianness.
273 Constant *Elt = ConstantExpr::getLShr(Src,
274 ConstantInt::get(Src->getType(), ShiftAmt));
275 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
276
277 // Truncate the element to an integer with the same pointer size and
278 // convert the element back to a pointer using a inttoptr.
279 if (DstEltTy->isPointerTy()) {
280 IntegerType *DstIntTy = Type::getIntNTy(C->getContext(), DstBitSize);
281 Constant *CE = ConstantExpr::getTrunc(Elt, DstIntTy);
282 Result.push_back(ConstantExpr::getIntToPtr(CE, DstEltTy));
283 continue;
284 }
285
286 // Truncate and remember this piece.
287 Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy));
288 }
289 }
290
291 return ConstantVector::get(Result);
292}
293
294} // end anonymous namespace
295
296/// If this constant is a constant offset from a global, return the global and
297/// the constant. Because of constantexprs, this function is recursive.
298bool llvm::IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
299 APInt &Offset, const DataLayout &DL,
300 DSOLocalEquivalent **DSOEquiv) {
301 if (DSOEquiv)
302 *DSOEquiv = nullptr;
303
304 // Trivial case, constant is the global.
305 if ((GV = dyn_cast<GlobalValue>(C))) {
306 unsigned BitWidth = DL.getIndexTypeSizeInBits(GV->getType());
307 Offset = APInt(BitWidth, 0);
308 return true;
309 }
310
311 if (auto *FoundDSOEquiv = dyn_cast<DSOLocalEquivalent>(C)) {
312 if (DSOEquiv)
313 *DSOEquiv = FoundDSOEquiv;
314 GV = FoundDSOEquiv->getGlobalValue();
315 unsigned BitWidth = DL.getIndexTypeSizeInBits(GV->getType());
316 Offset = APInt(BitWidth, 0);
317 return true;
318 }
319
320 // Otherwise, if this isn't a constant expr, bail out.
321 auto *CE = dyn_cast<ConstantExpr>(C);
322 if (!CE) return false;
323
324 // Look through ptr->int and ptr->ptr casts.
325 if (CE->getOpcode() == Instruction::PtrToInt ||
326 CE->getOpcode() == Instruction::BitCast)
327 return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, DL,
328 DSOEquiv);
329
330 // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
331 auto *GEP = dyn_cast<GEPOperator>(CE);
332 if (!GEP)
333 return false;
334
335 unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType());
336 APInt TmpOffset(BitWidth, 0);
337
338 // If the base isn't a global+constant, we aren't either.
339 if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, TmpOffset, DL,
340 DSOEquiv))
341 return false;
342
343 // Otherwise, add any offset that our operands provide.
344 if (!GEP->accumulateConstantOffset(DL, TmpOffset))
345 return false;
346
347 Offset = TmpOffset;
348 return true;
349}
350
351Constant *llvm::ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy,
352 const DataLayout &DL) {
353 do {
354 Type *SrcTy = C->getType();
355 uint64_t DestSize = DL.getTypeSizeInBits(DestTy);
356 uint64_t SrcSize = DL.getTypeSizeInBits(SrcTy);
357 if (SrcSize < DestSize)
358 return nullptr;
359
360 // Catch the obvious splat cases (since all-zeros can coerce non-integral
361 // pointers legally).
362 if (C->isNullValue() && !DestTy->isX86_MMXTy() && !DestTy->isX86_AMXTy())
363 return Constant::getNullValue(DestTy);
364 if (C->isAllOnesValue() &&
365 (DestTy->isIntegerTy() || DestTy->isFloatingPointTy() ||
366 DestTy->isVectorTy()) &&
367 !DestTy->isX86_AMXTy() && !DestTy->isX86_MMXTy() &&
368 !DestTy->isPtrOrPtrVectorTy())
369 // Get ones when the input is trivial, but
370 // only for supported types inside getAllOnesValue.
371 return Constant::getAllOnesValue(DestTy);
372
373 // If the type sizes are the same and a cast is legal, just directly
374 // cast the constant.
375 // But be careful not to coerce non-integral pointers illegally.
376 if (SrcSize == DestSize &&
377 DL.isNonIntegralPointerType(SrcTy->getScalarType()) ==
378 DL.isNonIntegralPointerType(DestTy->getScalarType())) {
379 Instruction::CastOps Cast = Instruction::BitCast;
380 // If we are going from a pointer to int or vice versa, we spell the cast
381 // differently.
382 if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
383 Cast = Instruction::IntToPtr;
384 else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())
385 Cast = Instruction::PtrToInt;
386
387 if (CastInst::castIsValid(Cast, C, DestTy))
388 return ConstantExpr::getCast(Cast, C, DestTy);
389 }
390
391 // If this isn't an aggregate type, there is nothing we can do to drill down
392 // and find a bitcastable constant.
393 if (!SrcTy->isAggregateType() && !SrcTy->isVectorTy())
394 return nullptr;
395
396 // We're simulating a load through a pointer that was bitcast to point to
397 // a different type, so we can try to walk down through the initial
398 // elements of an aggregate to see if some part of the aggregate is
399 // castable to implement the "load" semantic model.
400 if (SrcTy->isStructTy()) {
401 // Struct types might have leading zero-length elements like [0 x i32],
402 // which are certainly not what we are looking for, so skip them.
403 unsigned Elem = 0;
404 Constant *ElemC;
405 do {
406 ElemC = C->getAggregateElement(Elem++);
407 } while (ElemC && DL.getTypeSizeInBits(ElemC->getType()).isZero());
408 C = ElemC;
409 } else {
410 C = C->getAggregateElement(0u);
411 }
412 } while (C);
413
414 return nullptr;
415}
416
417namespace {
418
419/// Recursive helper to read bits out of global. C is the constant being copied
420/// out of. ByteOffset is an offset into C. CurPtr is the pointer to copy
421/// results into and BytesLeft is the number of bytes left in
422/// the CurPtr buffer. DL is the DataLayout.
423bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr,
424 unsigned BytesLeft, const DataLayout &DL) {
425 assert(ByteOffset <= DL.getTypeAllocSize(C->getType()) &&(static_cast <bool> (ByteOffset <= DL.getTypeAllocSize
(C->getType()) && "Out of range access") ? void (0
) : __assert_fail ("ByteOffset <= DL.getTypeAllocSize(C->getType()) && \"Out of range access\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 426, __extension__ __PRETTY_FUNCTION__))
426 "Out of range access")(static_cast <bool> (ByteOffset <= DL.getTypeAllocSize
(C->getType()) && "Out of range access") ? void (0
) : __assert_fail ("ByteOffset <= DL.getTypeAllocSize(C->getType()) && \"Out of range access\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 426, __extension__ __PRETTY_FUNCTION__))
;
427
428 // If this element is zero or undefined, we can just return since *CurPtr is
429 // zero initialized.
430 if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
431 return true;
432
433 if (auto *CI = dyn_cast<ConstantInt>(C)) {
434 if (CI->getBitWidth() > 64 ||
435 (CI->getBitWidth() & 7) != 0)
436 return false;
437
438 uint64_t Val = CI->getZExtValue();
439 unsigned IntBytes = unsigned(CI->getBitWidth()/8);
440
441 for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
442 int n = ByteOffset;
443 if (!DL.isLittleEndian())
444 n = IntBytes - n - 1;
445 CurPtr[i] = (unsigned char)(Val >> (n * 8));
446 ++ByteOffset;
447 }
448 return true;
449 }
450
451 if (auto *CFP = dyn_cast<ConstantFP>(C)) {
452 if (CFP->getType()->isDoubleTy()) {
453 C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), DL);
454 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
455 }
456 if (CFP->getType()->isFloatTy()){
457 C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), DL);
458 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
459 }
460 if (CFP->getType()->isHalfTy()){
461 C = FoldBitCast(C, Type::getInt16Ty(C->getContext()), DL);
462 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
463 }
464 return false;
465 }
466
467 if (auto *CS = dyn_cast<ConstantStruct>(C)) {
468 const StructLayout *SL = DL.getStructLayout(CS->getType());
469 unsigned Index = SL->getElementContainingOffset(ByteOffset);
470 uint64_t CurEltOffset = SL->getElementOffset(Index);
471 ByteOffset -= CurEltOffset;
472
473 while (true) {
474 // If the element access is to the element itself and not to tail padding,
475 // read the bytes from the element.
476 uint64_t EltSize = DL.getTypeAllocSize(CS->getOperand(Index)->getType());
477
478 if (ByteOffset < EltSize &&
479 !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
480 BytesLeft, DL))
481 return false;
482
483 ++Index;
484
485 // Check to see if we read from the last struct element, if so we're done.
486 if (Index == CS->getType()->getNumElements())
487 return true;
488
489 // If we read all of the bytes we needed from this element we're done.
490 uint64_t NextEltOffset = SL->getElementOffset(Index);
491
492 if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset)
493 return true;
494
495 // Move to the next element of the struct.
496 CurPtr += NextEltOffset - CurEltOffset - ByteOffset;
497 BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset;
498 ByteOffset = 0;
499 CurEltOffset = NextEltOffset;
500 }
501 // not reached.
502 }
503
504 if (isa<ConstantArray>(C) || isa<ConstantVector>(C) ||
505 isa<ConstantDataSequential>(C)) {
506 uint64_t NumElts;
507 Type *EltTy;
508 if (auto *AT = dyn_cast<ArrayType>(C->getType())) {
509 NumElts = AT->getNumElements();
510 EltTy = AT->getElementType();
511 } else {
512 NumElts = cast<FixedVectorType>(C->getType())->getNumElements();
513 EltTy = cast<FixedVectorType>(C->getType())->getElementType();
514 }
515 uint64_t EltSize = DL.getTypeAllocSize(EltTy);
516 uint64_t Index = ByteOffset / EltSize;
517 uint64_t Offset = ByteOffset - Index * EltSize;
518
519 for (; Index != NumElts; ++Index) {
520 if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr,
521 BytesLeft, DL))
522 return false;
523
524 uint64_t BytesWritten = EltSize - Offset;
525 assert(BytesWritten <= EltSize && "Not indexing into this element?")(static_cast <bool> (BytesWritten <= EltSize &&
"Not indexing into this element?") ? void (0) : __assert_fail
("BytesWritten <= EltSize && \"Not indexing into this element?\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 525, __extension__ __PRETTY_FUNCTION__))
;
526 if (BytesWritten >= BytesLeft)
527 return true;
528
529 Offset = 0;
530 BytesLeft -= BytesWritten;
531 CurPtr += BytesWritten;
532 }
533 return true;
534 }
535
536 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
537 if (CE->getOpcode() == Instruction::IntToPtr &&
538 CE->getOperand(0)->getType() == DL.getIntPtrType(CE->getType())) {
539 return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
540 BytesLeft, DL);
541 }
542 }
543
544 // Otherwise, unknown initializer type.
545 return false;
546}
547
548Constant *FoldReinterpretLoadFromConstPtr(Constant *C, Type *LoadTy,
549 const DataLayout &DL) {
550 // Bail out early. Not expect to load from scalable global variable.
551 if (isa<ScalableVectorType>(LoadTy))
552 return nullptr;
553
554 auto *PTy = cast<PointerType>(C->getType());
555 auto *IntType = dyn_cast<IntegerType>(LoadTy);
556
557 // If this isn't an integer load we can't fold it directly.
558 if (!IntType) {
559 unsigned AS = PTy->getAddressSpace();
560
561 // If this is a float/double load, we can try folding it as an int32/64 load
562 // and then bitcast the result. This can be useful for union cases. Note
563 // that address spaces don't matter here since we're not going to result in
564 // an actual new load.
565 Type *MapTy;
566 if (LoadTy->isHalfTy())
567 MapTy = Type::getInt16Ty(C->getContext());
568 else if (LoadTy->isFloatTy())
569 MapTy = Type::getInt32Ty(C->getContext());
570 else if (LoadTy->isDoubleTy())
571 MapTy = Type::getInt64Ty(C->getContext());
572 else if (LoadTy->isVectorTy()) {
573 MapTy = PointerType::getIntNTy(
574 C->getContext(), DL.getTypeSizeInBits(LoadTy).getFixedSize());
575 } else
576 return nullptr;
577
578 C = FoldBitCast(C, MapTy->getPointerTo(AS), DL);
579 if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, MapTy, DL)) {
580 if (Res->isNullValue() && !LoadTy->isX86_MMXTy() &&
581 !LoadTy->isX86_AMXTy())
582 // Materializing a zero can be done trivially without a bitcast
583 return Constant::getNullValue(LoadTy);
584 Type *CastTy = LoadTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(LoadTy) : LoadTy;
585 Res = FoldBitCast(Res, CastTy, DL);
586 if (LoadTy->isPtrOrPtrVectorTy()) {
587 // For vector of pointer, we needed to first convert to a vector of integer, then do vector inttoptr
588 if (Res->isNullValue() && !LoadTy->isX86_MMXTy() &&
589 !LoadTy->isX86_AMXTy())
590 return Constant::getNullValue(LoadTy);
591 if (DL.isNonIntegralPointerType(LoadTy->getScalarType()))
592 // Be careful not to replace a load of an addrspace value with an inttoptr here
593 return nullptr;
594 Res = ConstantExpr::getCast(Instruction::IntToPtr, Res, LoadTy);
595 }
596 return Res;
597 }
598 return nullptr;
599 }
600
601 unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
602 if (BytesLoaded > 32 || BytesLoaded == 0)
603 return nullptr;
604
605 GlobalValue *GVal;
606 APInt OffsetAI;
607 if (!IsConstantOffsetFromGlobal(C, GVal, OffsetAI, DL))
608 return nullptr;
609
610 auto *GV = dyn_cast<GlobalVariable>(GVal);
611 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
612 !GV->getInitializer()->getType()->isSized())
613 return nullptr;
614
615 int64_t Offset = OffsetAI.getSExtValue();
616 int64_t InitializerSize =
617 DL.getTypeAllocSize(GV->getInitializer()->getType()).getFixedSize();
618
619 // If we're not accessing anything in this constant, the result is undefined.
620 if (Offset <= -1 * static_cast<int64_t>(BytesLoaded))
621 return UndefValue::get(IntType);
622
623 // If we're not accessing anything in this constant, the result is undefined.
624 if (Offset >= InitializerSize)
625 return UndefValue::get(IntType);
626
627 unsigned char RawBytes[32] = {0};
628 unsigned char *CurPtr = RawBytes;
629 unsigned BytesLeft = BytesLoaded;
630
631 // If we're loading off the beginning of the global, some bytes may be valid.
632 if (Offset < 0) {
633 CurPtr += -Offset;
634 BytesLeft += Offset;
635 Offset = 0;
636 }
637
638 if (!ReadDataFromGlobal(GV->getInitializer(), Offset, CurPtr, BytesLeft, DL))
639 return nullptr;
640
641 APInt ResultVal = APInt(IntType->getBitWidth(), 0);
642 if (DL.isLittleEndian()) {
643 ResultVal = RawBytes[BytesLoaded - 1];
644 for (unsigned i = 1; i != BytesLoaded; ++i) {
645 ResultVal <<= 8;
646 ResultVal |= RawBytes[BytesLoaded - 1 - i];
647 }
648 } else {
649 ResultVal = RawBytes[0];
650 for (unsigned i = 1; i != BytesLoaded; ++i) {
651 ResultVal <<= 8;
652 ResultVal |= RawBytes[i];
653 }
654 }
655
656 return ConstantInt::get(IntType->getContext(), ResultVal);
657}
658
659Constant *ConstantFoldLoadThroughBitcastExpr(ConstantExpr *CE, Type *DestTy,
660 const DataLayout &DL) {
661 auto *SrcPtr = CE->getOperand(0);
662 if (!SrcPtr->getType()->isPointerTy())
663 return nullptr;
664
665 return ConstantFoldLoadFromConstPtr(SrcPtr, DestTy, DL);
666}
667
668} // end anonymous namespace
669
670Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
671 const DataLayout &DL) {
672 // First, try the easy cases:
673 if (auto *GV = dyn_cast<GlobalVariable>(C))
674 if (GV->isConstant() && GV->hasDefinitiveInitializer())
675 return ConstantFoldLoadThroughBitcast(GV->getInitializer(), Ty, DL);
676
677 if (auto *GA = dyn_cast<GlobalAlias>(C))
678 if (GA->getAliasee() && !GA->isInterposable())
679 return ConstantFoldLoadFromConstPtr(GA->getAliasee(), Ty, DL);
680
681 // If the loaded value isn't a constant expr, we can't handle it.
682 auto *CE = dyn_cast<ConstantExpr>(C);
683 if (!CE)
684 return nullptr;
685
686 if (CE->getOpcode() == Instruction::GetElementPtr) {
687 if (auto *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) {
688 if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
689 if (Constant *V = ConstantFoldLoadThroughGEPConstantExpr(
690 GV->getInitializer(), CE, Ty, DL))
691 return V;
692 }
693 }
694 }
695
696 if (CE->getOpcode() == Instruction::BitCast)
697 if (Constant *LoadedC = ConstantFoldLoadThroughBitcastExpr(CE, Ty, DL))
698 return LoadedC;
699
700 // Instead of loading constant c string, use corresponding integer value
701 // directly if string length is small enough.
702 StringRef Str;
703 if (getConstantStringInfo(CE, Str) && !Str.empty()) {
704 size_t StrLen = Str.size();
705 unsigned NumBits = Ty->getPrimitiveSizeInBits();
706 // Replace load with immediate integer if the result is an integer or fp
707 // value.
708 if ((NumBits >> 3) == StrLen + 1 && (NumBits & 7) == 0 &&
709 (isa<IntegerType>(Ty) || Ty->isFloatingPointTy())) {
710 APInt StrVal(NumBits, 0);
711 APInt SingleChar(NumBits, 0);
712 if (DL.isLittleEndian()) {
713 for (unsigned char C : reverse(Str.bytes())) {
714 SingleChar = static_cast<uint64_t>(C);
715 StrVal = (StrVal << 8) | SingleChar;
716 }
717 } else {
718 for (unsigned char C : Str.bytes()) {
719 SingleChar = static_cast<uint64_t>(C);
720 StrVal = (StrVal << 8) | SingleChar;
721 }
722 // Append NULL at the end.
723 SingleChar = 0;
724 StrVal = (StrVal << 8) | SingleChar;
725 }
726
727 Constant *Res = ConstantInt::get(CE->getContext(), StrVal);
728 if (Ty->isFloatingPointTy())
729 Res = ConstantExpr::getBitCast(Res, Ty);
730 return Res;
731 }
732 }
733
734 // If this load comes from anywhere in a constant global, and if the global
735 // is all undef or zero, we know what it loads.
736 if (auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(CE))) {
737 if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
738 if (GV->getInitializer()->isNullValue())
739 return Constant::getNullValue(Ty);
740 if (isa<UndefValue>(GV->getInitializer()))
741 return UndefValue::get(Ty);
742 }
743 }
744
745 // Try hard to fold loads from bitcasted strange and non-type-safe things.
746 return FoldReinterpretLoadFromConstPtr(CE, Ty, DL);
747}
748
749namespace {
750
751/// One of Op0/Op1 is a constant expression.
752/// Attempt to symbolically evaluate the result of a binary operator merging
753/// these together. If target data info is available, it is provided as DL,
754/// otherwise DL is null.
755Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1,
756 const DataLayout &DL) {
757 // SROA
758
759 // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
760 // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
761 // bits.
762
763 if (Opc == Instruction::And) {
764 KnownBits Known0 = computeKnownBits(Op0, DL);
765 KnownBits Known1 = computeKnownBits(Op1, DL);
766 if ((Known1.One | Known0.Zero).isAllOnesValue()) {
767 // All the bits of Op0 that the 'and' could be masking are already zero.
768 return Op0;
769 }
770 if ((Known0.One | Known1.Zero).isAllOnesValue()) {
771 // All the bits of Op1 that the 'and' could be masking are already zero.
772 return Op1;
773 }
774
775 Known0 &= Known1;
776 if (Known0.isConstant())
777 return ConstantInt::get(Op0->getType(), Known0.getConstant());
778 }
779
780 // If the constant expr is something like &A[123] - &A[4].f, fold this into a
781 // constant. This happens frequently when iterating over a global array.
782 if (Opc == Instruction::Sub) {
783 GlobalValue *GV1, *GV2;
784 APInt Offs1, Offs2;
785
786 if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, DL))
787 if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, DL) && GV1 == GV2) {
788 unsigned OpSize = DL.getTypeSizeInBits(Op0->getType());
789
790 // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
791 // PtrToInt may change the bitwidth so we have convert to the right size
792 // first.
793 return ConstantInt::get(Op0->getType(), Offs1.zextOrTrunc(OpSize) -
794 Offs2.zextOrTrunc(OpSize));
795 }
796 }
797
798 return nullptr;
799}
800
801/// If array indices are not pointer-sized integers, explicitly cast them so
802/// that they aren't implicitly casted by the getelementptr.
803Constant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops,
804 Type *ResultTy, Optional<unsigned> InRangeIndex,
805 const DataLayout &DL, const TargetLibraryInfo *TLI) {
806 Type *IntIdxTy = DL.getIndexType(ResultTy);
807 Type *IntIdxScalarTy = IntIdxTy->getScalarType();
808
809 bool Any = false;
810 SmallVector<Constant*, 32> NewIdxs;
811 for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
812 if ((i == 1 ||
813 !isa<StructType>(GetElementPtrInst::getIndexedType(
814 SrcElemTy, Ops.slice(1, i - 1)))) &&
815 Ops[i]->getType()->getScalarType() != IntIdxScalarTy) {
816 Any = true;
817 Type *NewType = Ops[i]->getType()->isVectorTy()
818 ? IntIdxTy
819 : IntIdxScalarTy;
820 NewIdxs.push_back(ConstantExpr::getCast(CastInst::getCastOpcode(Ops[i],
821 true,
822 NewType,
823 true),
824 Ops[i], NewType));
825 } else
826 NewIdxs.push_back(Ops[i]);
827 }
828
829 if (!Any)
830 return nullptr;
831
832 Constant *C = ConstantExpr::getGetElementPtr(
833 SrcElemTy, Ops[0], NewIdxs, /*InBounds=*/false, InRangeIndex);
834 return ConstantFoldConstant(C, DL, TLI);
835}
836
837/// Strip the pointer casts, but preserve the address space information.
838Constant *StripPtrCastKeepAS(Constant *Ptr, Type *&ElemTy) {
839 assert(Ptr->getType()->isPointerTy() && "Not a pointer type")(static_cast <bool> (Ptr->getType()->isPointerTy(
) && "Not a pointer type") ? void (0) : __assert_fail
("Ptr->getType()->isPointerTy() && \"Not a pointer type\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 839, __extension__ __PRETTY_FUNCTION__))
;
840 auto *OldPtrTy = cast<PointerType>(Ptr->getType());
841 Ptr = cast<Constant>(Ptr->stripPointerCasts());
842 auto *NewPtrTy = cast<PointerType>(Ptr->getType());
843
844 ElemTy = NewPtrTy->getPointerElementType();
845
846 // Preserve the address space number of the pointer.
847 if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) {
848 NewPtrTy = ElemTy->getPointerTo(OldPtrTy->getAddressSpace());
849 Ptr = ConstantExpr::getPointerCast(Ptr, NewPtrTy);
850 }
851 return Ptr;
852}
853
854/// If we can symbolically evaluate the GEP constant expression, do so.
855Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
856 ArrayRef<Constant *> Ops,
857 const DataLayout &DL,
858 const TargetLibraryInfo *TLI) {
859 const GEPOperator *InnermostGEP = GEP;
860 bool InBounds = GEP->isInBounds();
861
862 Type *SrcElemTy = GEP->getSourceElementType();
863 Type *ResElemTy = GEP->getResultElementType();
864 Type *ResTy = GEP->getType();
865 if (!SrcElemTy->isSized() || isa<ScalableVectorType>(SrcElemTy))
866 return nullptr;
867
868 if (Constant *C = CastGEPIndices(SrcElemTy, Ops, ResTy,
869 GEP->getInRangeIndex(), DL, TLI))
870 return C;
871
872 Constant *Ptr = Ops[0];
873 if (!Ptr->getType()->isPointerTy())
874 return nullptr;
875
876 Type *IntIdxTy = DL.getIndexType(Ptr->getType());
877
878 // If this is a constant expr gep that is effectively computing an
879 // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
880 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
881 if (!isa<ConstantInt>(Ops[i])) {
882
883 // If this is "gep i8* Ptr, (sub 0, V)", fold this as:
884 // "inttoptr (sub (ptrtoint Ptr), V)"
885 if (Ops.size() == 2 && ResElemTy->isIntegerTy(8)) {
886 auto *CE = dyn_cast<ConstantExpr>(Ops[1]);
887 assert((!CE || CE->getType() == IntIdxTy) &&(static_cast <bool> ((!CE || CE->getType() == IntIdxTy
) && "CastGEPIndices didn't canonicalize index types!"
) ? void (0) : __assert_fail ("(!CE || CE->getType() == IntIdxTy) && \"CastGEPIndices didn't canonicalize index types!\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 888, __extension__ __PRETTY_FUNCTION__))
888 "CastGEPIndices didn't canonicalize index types!")(static_cast <bool> ((!CE || CE->getType() == IntIdxTy
) && "CastGEPIndices didn't canonicalize index types!"
) ? void (0) : __assert_fail ("(!CE || CE->getType() == IntIdxTy) && \"CastGEPIndices didn't canonicalize index types!\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 888, __extension__ __PRETTY_FUNCTION__))
;
889 if (CE && CE->getOpcode() == Instruction::Sub &&
890 CE->getOperand(0)->isNullValue()) {
891 Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType());
892 Res = ConstantExpr::getSub(Res, CE->getOperand(1));
893 Res = ConstantExpr::getIntToPtr(Res, ResTy);
894 return ConstantFoldConstant(Res, DL, TLI);
895 }
896 }
897 return nullptr;
898 }
899
900 unsigned BitWidth = DL.getTypeSizeInBits(IntIdxTy);
901 APInt Offset =
902 APInt(BitWidth,
903 DL.getIndexedOffsetInType(
904 SrcElemTy,
905 makeArrayRef((Value * const *)Ops.data() + 1, Ops.size() - 1)));
906 Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy);
907
908 // If this is a GEP of a GEP, fold it all into a single GEP.
909 while (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
910 InnermostGEP = GEP;
911 InBounds &= GEP->isInBounds();
912
913 SmallVector<Value *, 4> NestedOps(GEP->op_begin() + 1, GEP->op_end());
914
915 // Do not try the incorporate the sub-GEP if some index is not a number.
916 bool AllConstantInt = true;
917 for (Value *NestedOp : NestedOps)
918 if (!isa<ConstantInt>(NestedOp)) {
919 AllConstantInt = false;
920 break;
921 }
922 if (!AllConstantInt)
923 break;
924
925 Ptr = cast<Constant>(GEP->getOperand(0));
926 SrcElemTy = GEP->getSourceElementType();
927 Offset += APInt(BitWidth, DL.getIndexedOffsetInType(SrcElemTy, NestedOps));
928 Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy);
929 }
930
931 // If the base value for this address is a literal integer value, fold the
932 // getelementptr to the resulting integer value casted to the pointer type.
933 APInt BasePtr(BitWidth, 0);
934 if (auto *CE = dyn_cast<ConstantExpr>(Ptr)) {
935 if (CE->getOpcode() == Instruction::IntToPtr) {
936 if (auto *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
937 BasePtr = Base->getValue().zextOrTrunc(BitWidth);
938 }
939 }
940
941 auto *PTy = cast<PointerType>(Ptr->getType());
942 if ((Ptr->isNullValue() || BasePtr != 0) &&
943 !DL.isNonIntegralPointerType(PTy)) {
944 Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr);
945 return ConstantExpr::getIntToPtr(C, ResTy);
946 }
947
948 // Otherwise form a regular getelementptr. Recompute the indices so that
949 // we eliminate over-indexing of the notional static type array bounds.
950 // This makes it easy to determine if the getelementptr is "inbounds".
951 // Also, this helps GlobalOpt do SROA on GlobalVariables.
952 Type *Ty = PTy;
953 SmallVector<Constant *, 32> NewIdxs;
954
955 do {
956 if (!Ty->isStructTy()) {
957 if (Ty->isPointerTy()) {
958 // The only pointer indexing we'll do is on the first index of the GEP.
959 if (!NewIdxs.empty())
960 break;
961
962 Ty = SrcElemTy;
963
964 // Only handle pointers to sized types, not pointers to functions.
965 if (!Ty->isSized())
966 return nullptr;
967 } else {
968 Type *NextTy = GetElementPtrInst::getTypeAtIndex(Ty, (uint64_t)0);
969 if (!NextTy)
970 break;
971 Ty = NextTy;
972 }
973
974 // Determine which element of the array the offset points into.
975 APInt ElemSize(BitWidth, DL.getTypeAllocSize(Ty));
976 if (ElemSize == 0) {
977 // The element size is 0. This may be [0 x Ty]*, so just use a zero
978 // index for this level and proceed to the next level to see if it can
979 // accommodate the offset.
980 NewIdxs.push_back(ConstantInt::get(IntIdxTy, 0));
981 } else {
982 // The element size is non-zero divide the offset by the element
983 // size (rounding down), to compute the index at this level.
984 bool Overflow;
985 APInt NewIdx = Offset.sdiv_ov(ElemSize, Overflow);
986 if (Overflow)
987 break;
988 Offset -= NewIdx * ElemSize;
989 NewIdxs.push_back(ConstantInt::get(IntIdxTy, NewIdx));
990 }
991 } else {
992 auto *STy = cast<StructType>(Ty);
993 // If we end up with an offset that isn't valid for this struct type, we
994 // can't re-form this GEP in a regular form, so bail out. The pointer
995 // operand likely went through casts that are necessary to make the GEP
996 // sensible.
997 const StructLayout &SL = *DL.getStructLayout(STy);
998 if (Offset.isNegative() || Offset.uge(SL.getSizeInBytes()))
999 break;
1000
1001 // Determine which field of the struct the offset points into. The
1002 // getZExtValue is fine as we've already ensured that the offset is
1003 // within the range representable by the StructLayout API.
1004 unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue());
1005 NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
1006 ElIdx));
1007 Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx));
1008 Ty = STy->getTypeAtIndex(ElIdx);
1009 }
1010 } while (Ty != ResElemTy);
1011
1012 // If we haven't used up the entire offset by descending the static
1013 // type, then the offset is pointing into the middle of an indivisible
1014 // member, so we can't simplify it.
1015 if (Offset != 0)
1016 return nullptr;
1017
1018 // Preserve the inrange index from the innermost GEP if possible. We must
1019 // have calculated the same indices up to and including the inrange index.
1020 Optional<unsigned> InRangeIndex;
1021 if (Optional<unsigned> LastIRIndex = InnermostGEP->getInRangeIndex())
1022 if (SrcElemTy == InnermostGEP->getSourceElementType() &&
1023 NewIdxs.size() > *LastIRIndex) {
1024 InRangeIndex = LastIRIndex;
1025 for (unsigned I = 0; I <= *LastIRIndex; ++I)
1026 if (NewIdxs[I] != InnermostGEP->getOperand(I + 1))
1027 return nullptr;
1028 }
1029
1030 // Create a GEP.
1031 Constant *C = ConstantExpr::getGetElementPtr(SrcElemTy, Ptr, NewIdxs,
1032 InBounds, InRangeIndex);
1033 assert(C->getType()->getPointerElementType() == Ty &&(static_cast <bool> (C->getType()->getPointerElementType
() == Ty && "Computed GetElementPtr has unexpected type!"
) ? void (0) : __assert_fail ("C->getType()->getPointerElementType() == Ty && \"Computed GetElementPtr has unexpected type!\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 1034, __extension__ __PRETTY_FUNCTION__))
1034 "Computed GetElementPtr has unexpected type!")(static_cast <bool> (C->getType()->getPointerElementType
() == Ty && "Computed GetElementPtr has unexpected type!"
) ? void (0) : __assert_fail ("C->getType()->getPointerElementType() == Ty && \"Computed GetElementPtr has unexpected type!\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 1034, __extension__ __PRETTY_FUNCTION__))
;
1035
1036 // If we ended up indexing a member with a type that doesn't match
1037 // the type of what the original indices indexed, add a cast.
1038 if (Ty != ResElemTy)
1039 C = FoldBitCast(C, ResTy, DL);
1040
1041 return C;
1042}
1043
1044/// Attempt to constant fold an instruction with the
1045/// specified opcode and operands. If successful, the constant result is
1046/// returned, if not, null is returned. Note that this function can fail when
1047/// attempting to fold instructions like loads and stores, which have no
1048/// constant expression form.
1049Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode,
1050 ArrayRef<Constant *> Ops,
1051 const DataLayout &DL,
1052 const TargetLibraryInfo *TLI) {
1053 Type *DestTy = InstOrCE->getType();
1054
1055 if (Instruction::isUnaryOp(Opcode))
1056 return ConstantFoldUnaryOpOperand(Opcode, Ops[0], DL);
1057
1058 if (Instruction::isBinaryOp(Opcode))
1059 return ConstantFoldBinaryOpOperands(Opcode, Ops[0], Ops[1], DL);
1060
1061 if (Instruction::isCast(Opcode))
1062 return ConstantFoldCastOperand(Opcode, Ops[0], DestTy, DL);
1063
1064 if (auto *GEP = dyn_cast<GEPOperator>(InstOrCE)) {
1065 if (Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI))
1066 return C;
1067
1068 return ConstantExpr::getGetElementPtr(GEP->getSourceElementType(), Ops[0],
1069 Ops.slice(1), GEP->isInBounds(),
1070 GEP->getInRangeIndex());
1071 }
1072
1073 if (auto *CE = dyn_cast<ConstantExpr>(InstOrCE))
1074 return CE->getWithOperands(Ops);
1075
1076 switch (Opcode) {
1077 default: return nullptr;
1078 case Instruction::ICmp:
1079 case Instruction::FCmp: llvm_unreachable("Invalid for compares")::llvm::llvm_unreachable_internal("Invalid for compares", "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 1079)
;
1080 case Instruction::Freeze:
1081 return isGuaranteedNotToBeUndefOrPoison(Ops[0]) ? Ops[0] : nullptr;
1082 case Instruction::Call:
1083 if (auto *F = dyn_cast<Function>(Ops.back())) {
1084 const auto *Call = cast<CallBase>(InstOrCE);
1085 if (canConstantFoldCallTo(Call, F))
1086 return ConstantFoldCall(Call, F, Ops.slice(0, Ops.size() - 1), TLI);
1087 }
1088 return nullptr;
1089 case Instruction::Select:
1090 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
1091 case Instruction::ExtractElement:
1092 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
1093 case Instruction::ExtractValue:
1094 return ConstantExpr::getExtractValue(
1095 Ops[0], cast<ExtractValueInst>(InstOrCE)->getIndices());
1096 case Instruction::InsertElement:
1097 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
1098 case Instruction::ShuffleVector:
1099 return ConstantExpr::getShuffleVector(
1100 Ops[0], Ops[1], cast<ShuffleVectorInst>(InstOrCE)->getShuffleMask());
1101 }
1102}
1103
1104} // end anonymous namespace
1105
1106//===----------------------------------------------------------------------===//
1107// Constant Folding public APIs
1108//===----------------------------------------------------------------------===//
1109
1110namespace {
1111
1112Constant *
1113ConstantFoldConstantImpl(const Constant *C, const DataLayout &DL,
1114 const TargetLibraryInfo *TLI,
1115 SmallDenseMap<Constant *, Constant *> &FoldedOps) {
1116 if (!isa<ConstantVector>(C) && !isa<ConstantExpr>(C))
1117 return const_cast<Constant *>(C);
1118
1119 SmallVector<Constant *, 8> Ops;
1120 for (const Use &OldU : C->operands()) {
1121 Constant *OldC = cast<Constant>(&OldU);
1122 Constant *NewC = OldC;
1123 // Recursively fold the ConstantExpr's operands. If we have already folded
1124 // a ConstantExpr, we don't have to process it again.
1125 if (isa<ConstantVector>(OldC) || isa<ConstantExpr>(OldC)) {
1126 auto It = FoldedOps.find(OldC);
1127 if (It == FoldedOps.end()) {
1128 NewC = ConstantFoldConstantImpl(OldC, DL, TLI, FoldedOps);
1129 FoldedOps.insert({OldC, NewC});
1130 } else {
1131 NewC = It->second;
1132 }
1133 }
1134 Ops.push_back(NewC);
1135 }
1136
1137 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1138 if (CE->isCompare())
1139 return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1],
1140 DL, TLI);
1141
1142 return ConstantFoldInstOperandsImpl(CE, CE->getOpcode(), Ops, DL, TLI);
1143 }
1144
1145 assert(isa<ConstantVector>(C))(static_cast <bool> (isa<ConstantVector>(C)) ? void
(0) : __assert_fail ("isa<ConstantVector>(C)", "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 1145, __extension__ __PRETTY_FUNCTION__))
;
1146 return ConstantVector::get(Ops);
1147}
1148
1149} // end anonymous namespace
1150
1151Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
1152 const TargetLibraryInfo *TLI) {
1153 // Handle PHI nodes quickly here...
1154 if (auto *PN = dyn_cast<PHINode>(I)) {
1155 Constant *CommonValue = nullptr;
1156
1157 SmallDenseMap<Constant *, Constant *> FoldedOps;
1158 for (Value *Incoming : PN->incoming_values()) {
1159 // If the incoming value is undef then skip it. Note that while we could
1160 // skip the value if it is equal to the phi node itself we choose not to
1161 // because that would break the rule that constant folding only applies if
1162 // all operands are constants.
1163 if (isa<UndefValue>(Incoming))
1164 continue;
1165 // If the incoming value is not a constant, then give up.
1166 auto *C = dyn_cast<Constant>(Incoming);
1167 if (!C)
1168 return nullptr;
1169 // Fold the PHI's operands.
1170 C = ConstantFoldConstantImpl(C, DL, TLI, FoldedOps);
1171 // If the incoming value is a different constant to
1172 // the one we saw previously, then give up.
1173 if (CommonValue && C != CommonValue)
1174 return nullptr;
1175 CommonValue = C;
1176 }
1177
1178 // If we reach here, all incoming values are the same constant or undef.
1179 return CommonValue ? CommonValue : UndefValue::get(PN->getType());
1180 }
1181
1182 // Scan the operand list, checking to see if they are all constants, if so,
1183 // hand off to ConstantFoldInstOperandsImpl.
1184 if (!all_of(I->operands(), [](Use &U) { return isa<Constant>(U); }))
1185 return nullptr;
1186
1187 SmallDenseMap<Constant *, Constant *> FoldedOps;
1188 SmallVector<Constant *, 8> Ops;
1189 for (const Use &OpU : I->operands()) {
1190 auto *Op = cast<Constant>(&OpU);
1191 // Fold the Instruction's operands.
1192 Op = ConstantFoldConstantImpl(Op, DL, TLI, FoldedOps);
1193 Ops.push_back(Op);
1194 }
1195
1196 if (const auto *CI = dyn_cast<CmpInst>(I))
1197 return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
1198 DL, TLI);
1199
1200 if (const auto *LI = dyn_cast<LoadInst>(I)) {
1201 if (LI->isVolatile())
1202 return nullptr;
1203 return ConstantFoldLoadFromConstPtr(Ops[0], LI->getType(), DL);
1204 }
1205
1206 if (auto *IVI = dyn_cast<InsertValueInst>(I))
1207 return ConstantExpr::getInsertValue(Ops[0], Ops[1], IVI->getIndices());
1208
1209 if (auto *EVI = dyn_cast<ExtractValueInst>(I))
1210 return ConstantExpr::getExtractValue(Ops[0], EVI->getIndices());
1211
1212 return ConstantFoldInstOperands(I, Ops, DL, TLI);
1213}
1214
1215Constant *llvm::ConstantFoldConstant(const Constant *C, const DataLayout &DL,
1216 const TargetLibraryInfo *TLI) {
1217 SmallDenseMap<Constant *, Constant *> FoldedOps;
1218 return ConstantFoldConstantImpl(C, DL, TLI, FoldedOps);
1219}
1220
1221Constant *llvm::ConstantFoldInstOperands(Instruction *I,
1222 ArrayRef<Constant *> Ops,
1223 const DataLayout &DL,
1224 const TargetLibraryInfo *TLI) {
1225 return ConstantFoldInstOperandsImpl(I, I->getOpcode(), Ops, DL, TLI);
1226}
1227
1228Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
1229 Constant *Ops0, Constant *Ops1,
1230 const DataLayout &DL,
1231 const TargetLibraryInfo *TLI) {
1232 // fold: icmp (inttoptr x), null -> icmp x, 0
1233 // fold: icmp null, (inttoptr x) -> icmp 0, x
1234 // fold: icmp (ptrtoint x), 0 -> icmp x, null
1235 // fold: icmp 0, (ptrtoint x) -> icmp null, x
1236 // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y
1237 // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y
1238 //
1239 // FIXME: The following comment is out of data and the DataLayout is here now.
1240 // ConstantExpr::getCompare cannot do this, because it doesn't have DL
1241 // around to know if bit truncation is happening.
1242 if (auto *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
1243 if (Ops1->isNullValue()) {
1244 if (CE0->getOpcode() == Instruction::IntToPtr) {
1245 Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
1246 // Convert the integer value to the right size to ensure we get the
1247 // proper extension or truncation.
1248 Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
1249 IntPtrTy, false);
1250 Constant *Null = Constant::getNullValue(C->getType());
1251 return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
1252 }
1253
1254 // Only do this transformation if the int is intptrty in size, otherwise
1255 // there is a truncation or extension that we aren't modeling.
1256 if (CE0->getOpcode() == Instruction::PtrToInt) {
1257 Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
1258 if (CE0->getType() == IntPtrTy) {
1259 Constant *C = CE0->getOperand(0);
1260 Constant *Null = Constant::getNullValue(C->getType());
1261 return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
1262 }
1263 }
1264 }
1265
1266 if (auto *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
1267 if (CE0->getOpcode() == CE1->getOpcode()) {
1268 if (CE0->getOpcode() == Instruction::IntToPtr) {
1269 Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
1270
1271 // Convert the integer value to the right size to ensure we get the
1272 // proper extension or truncation.
1273 Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0),
1274 IntPtrTy, false);
1275 Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
1276 IntPtrTy, false);
1277 return ConstantFoldCompareInstOperands(Predicate, C0, C1, DL, TLI);
1278 }
1279
1280 // Only do this transformation if the int is intptrty in size, otherwise
1281 // there is a truncation or extension that we aren't modeling.
1282 if (CE0->getOpcode() == Instruction::PtrToInt) {
1283 Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
1284 if (CE0->getType() == IntPtrTy &&
1285 CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) {
1286 return ConstantFoldCompareInstOperands(
1287 Predicate, CE0->getOperand(0), CE1->getOperand(0), DL, TLI);
1288 }
1289 }
1290 }
1291 }
1292
1293 // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0)
1294 // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0)
1295 if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) &&
1296 CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) {
1297 Constant *LHS = ConstantFoldCompareInstOperands(
1298 Predicate, CE0->getOperand(0), Ops1, DL, TLI);
1299 Constant *RHS = ConstantFoldCompareInstOperands(
1300 Predicate, CE0->getOperand(1), Ops1, DL, TLI);
1301 unsigned OpC =
1302 Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
1303 return ConstantFoldBinaryOpOperands(OpC, LHS, RHS, DL);
1304 }
1305 } else if (isa<ConstantExpr>(Ops1)) {
1306 // If RHS is a constant expression, but the left side isn't, swap the
1307 // operands and try again.
1308 Predicate = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)Predicate);
1309 return ConstantFoldCompareInstOperands(Predicate, Ops1, Ops0, DL, TLI);
1310 }
1311
1312 return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
1313}
1314
1315Constant *llvm::ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op,
1316 const DataLayout &DL) {
1317 assert(Instruction::isUnaryOp(Opcode))(static_cast <bool> (Instruction::isUnaryOp(Opcode)) ? void
(0) : __assert_fail ("Instruction::isUnaryOp(Opcode)", "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 1317, __extension__ __PRETTY_FUNCTION__))
;
1318
1319 return ConstantExpr::get(Opcode, Op);
1320}
1321
1322Constant *llvm::ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
1323 Constant *RHS,
1324 const DataLayout &DL) {
1325 assert(Instruction::isBinaryOp(Opcode))(static_cast <bool> (Instruction::isBinaryOp(Opcode)) ?
void (0) : __assert_fail ("Instruction::isBinaryOp(Opcode)",
"/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 1325, __extension__ __PRETTY_FUNCTION__))
;
1326 if (isa<ConstantExpr>(LHS) || isa<ConstantExpr>(RHS))
1327 if (Constant *C = SymbolicallyEvaluateBinop(Opcode, LHS, RHS, DL))
1328 return C;
1329
1330 return ConstantExpr::get(Opcode, LHS, RHS);
1331}
1332
1333Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
1334 Type *DestTy, const DataLayout &DL) {
1335 assert(Instruction::isCast(Opcode))(static_cast <bool> (Instruction::isCast(Opcode)) ? void
(0) : __assert_fail ("Instruction::isCast(Opcode)", "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 1335, __extension__ __PRETTY_FUNCTION__))
;
1336 switch (Opcode) {
1337 default:
1338 llvm_unreachable("Missing case")::llvm::llvm_unreachable_internal("Missing case", "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 1338)
;
1339 case Instruction::PtrToInt:
1340 // If the input is a inttoptr, eliminate the pair. This requires knowing
1341 // the width of a pointer, so it can't be done in ConstantExpr::getCast.
1342 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1343 if (CE->getOpcode() == Instruction::IntToPtr) {
1344 Constant *Input = CE->getOperand(0);
1345 unsigned InWidth = Input->getType()->getScalarSizeInBits();
1346 unsigned PtrWidth = DL.getPointerTypeSizeInBits(CE->getType());
1347 if (PtrWidth < InWidth) {
1348 Constant *Mask =
1349 ConstantInt::get(CE->getContext(),
1350 APInt::getLowBitsSet(InWidth, PtrWidth));
1351 Input = ConstantExpr::getAnd(Input, Mask);
1352 }
1353 // Do a zext or trunc to get to the dest size.
1354 return ConstantExpr::getIntegerCast(Input, DestTy, false);
1355 }
1356 }
1357 return ConstantExpr::getCast(Opcode, C, DestTy);
1358 case Instruction::IntToPtr:
1359 // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
1360 // the int size is >= the ptr size and the address spaces are the same.
1361 // This requires knowing the width of a pointer, so it can't be done in
1362 // ConstantExpr::getCast.
1363 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1364 if (CE->getOpcode() == Instruction::PtrToInt) {
1365 Constant *SrcPtr = CE->getOperand(0);
1366 unsigned SrcPtrSize = DL.getPointerTypeSizeInBits(SrcPtr->getType());
1367 unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
1368
1369 if (MidIntSize >= SrcPtrSize) {
1370 unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace();
1371 if (SrcAS == DestTy->getPointerAddressSpace())
1372 return FoldBitCast(CE->getOperand(0), DestTy, DL);
1373 }
1374 }
1375 }
1376
1377 return ConstantExpr::getCast(Opcode, C, DestTy);
1378 case Instruction::Trunc:
1379 case Instruction::ZExt:
1380 case Instruction::SExt:
1381 case Instruction::FPTrunc:
1382 case Instruction::FPExt:
1383 case Instruction::UIToFP:
1384 case Instruction::SIToFP:
1385 case Instruction::FPToUI:
1386 case Instruction::FPToSI:
1387 case Instruction::AddrSpaceCast:
1388 return ConstantExpr::getCast(Opcode, C, DestTy);
1389 case Instruction::BitCast:
1390 return FoldBitCast(C, DestTy, DL);
1391 }
1392}
1393
1394Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
1395 ConstantExpr *CE,
1396 Type *Ty,
1397 const DataLayout &DL) {
1398 if (!CE->getOperand(1)->isNullValue())
1399 return nullptr; // Do not allow stepping over the value!
1400
1401 // Loop over all of the operands, tracking down which value we are
1402 // addressing.
1403 for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) {
1404 C = C->getAggregateElement(CE->getOperand(i));
1405 if (!C)
1406 return nullptr;
1407 }
1408 return ConstantFoldLoadThroughBitcast(C, Ty, DL);
1409}
1410
1411Constant *
1412llvm::ConstantFoldLoadThroughGEPIndices(Constant *C,
1413 ArrayRef<Constant *> Indices) {
1414 // Loop over all of the operands, tracking down which value we are
1415 // addressing.
1416 for (Constant *Index : Indices) {
1417 C = C->getAggregateElement(Index);
1418 if (!C)
1419 return nullptr;
1420 }
1421 return C;
1422}
1423
1424//===----------------------------------------------------------------------===//
1425// Constant Folding for Calls
1426//
1427
1428bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
1429 if (Call->isNoBuiltin())
1430 return false;
1431 switch (F->getIntrinsicID()) {
1432 // Operations that do not operate floating-point numbers and do not depend on
1433 // FP environment can be folded even in strictfp functions.
1434 case Intrinsic::bswap:
1435 case Intrinsic::ctpop:
1436 case Intrinsic::ctlz:
1437 case Intrinsic::cttz:
1438 case Intrinsic::fshl:
1439 case Intrinsic::fshr:
1440 case Intrinsic::launder_invariant_group:
1441 case Intrinsic::strip_invariant_group:
1442 case Intrinsic::masked_load:
1443 case Intrinsic::get_active_lane_mask:
1444 case Intrinsic::abs:
1445 case Intrinsic::smax:
1446 case Intrinsic::smin:
1447 case Intrinsic::umax:
1448 case Intrinsic::umin:
1449 case Intrinsic::sadd_with_overflow:
1450 case Intrinsic::uadd_with_overflow:
1451 case Intrinsic::ssub_with_overflow:
1452 case Intrinsic::usub_with_overflow:
1453 case Intrinsic::smul_with_overflow:
1454 case Intrinsic::umul_with_overflow:
1455 case Intrinsic::sadd_sat:
1456 case Intrinsic::uadd_sat:
1457 case Intrinsic::ssub_sat:
1458 case Intrinsic::usub_sat:
1459 case Intrinsic::smul_fix:
1460 case Intrinsic::smul_fix_sat:
1461 case Intrinsic::bitreverse:
1462 case Intrinsic::is_constant:
1463 case Intrinsic::vector_reduce_add:
1464 case Intrinsic::vector_reduce_mul:
1465 case Intrinsic::vector_reduce_and:
1466 case Intrinsic::vector_reduce_or:
1467 case Intrinsic::vector_reduce_xor:
1468 case Intrinsic::vector_reduce_smin:
1469 case Intrinsic::vector_reduce_smax:
1470 case Intrinsic::vector_reduce_umin:
1471 case Intrinsic::vector_reduce_umax:
1472 // Target intrinsics
1473 case Intrinsic::amdgcn_perm:
1474 case Intrinsic::arm_mve_vctp8:
1475 case Intrinsic::arm_mve_vctp16:
1476 case Intrinsic::arm_mve_vctp32:
1477 case Intrinsic::arm_mve_vctp64:
1478 case Intrinsic::aarch64_sve_convert_from_svbool:
1479 // WebAssembly float semantics are always known
1480 case Intrinsic::wasm_trunc_signed:
1481 case Intrinsic::wasm_trunc_unsigned:
1482 return true;
1483
1484 // Floating point operations cannot be folded in strictfp functions in
1485 // general case. They can be folded if FP environment is known to compiler.
1486 case Intrinsic::minnum:
1487 case Intrinsic::maxnum:
1488 case Intrinsic::minimum:
1489 case Intrinsic::maximum:
1490 case Intrinsic::log:
1491 case Intrinsic::log2:
1492 case Intrinsic::log10:
1493 case Intrinsic::exp:
1494 case Intrinsic::exp2:
1495 case Intrinsic::sqrt:
1496 case Intrinsic::sin:
1497 case Intrinsic::cos:
1498 case Intrinsic::pow:
1499 case Intrinsic::powi:
1500 case Intrinsic::fma:
1501 case Intrinsic::fmuladd:
1502 case Intrinsic::fptoui_sat:
1503 case Intrinsic::fptosi_sat:
1504 case Intrinsic::convert_from_fp16:
1505 case Intrinsic::convert_to_fp16:
1506 case Intrinsic::amdgcn_cos:
1507 case Intrinsic::amdgcn_cubeid:
1508 case Intrinsic::amdgcn_cubema:
1509 case Intrinsic::amdgcn_cubesc:
1510 case Intrinsic::amdgcn_cubetc:
1511 case Intrinsic::amdgcn_fmul_legacy:
1512 case Intrinsic::amdgcn_fma_legacy:
1513 case Intrinsic::amdgcn_fract:
1514 case Intrinsic::amdgcn_ldexp:
1515 case Intrinsic::amdgcn_sin:
1516 // The intrinsics below depend on rounding mode in MXCSR.
1517 case Intrinsic::x86_sse_cvtss2si:
1518 case Intrinsic::x86_sse_cvtss2si64:
1519 case Intrinsic::x86_sse_cvttss2si:
1520 case Intrinsic::x86_sse_cvttss2si64:
1521 case Intrinsic::x86_sse2_cvtsd2si:
1522 case Intrinsic::x86_sse2_cvtsd2si64:
1523 case Intrinsic::x86_sse2_cvttsd2si:
1524 case Intrinsic::x86_sse2_cvttsd2si64:
1525 case Intrinsic::x86_avx512_vcvtss2si32:
1526 case Intrinsic::x86_avx512_vcvtss2si64:
1527 case Intrinsic::x86_avx512_cvttss2si:
1528 case Intrinsic::x86_avx512_cvttss2si64:
1529 case Intrinsic::x86_avx512_vcvtsd2si32:
1530 case Intrinsic::x86_avx512_vcvtsd2si64:
1531 case Intrinsic::x86_avx512_cvttsd2si:
1532 case Intrinsic::x86_avx512_cvttsd2si64:
1533 case Intrinsic::x86_avx512_vcvtss2usi32:
1534 case Intrinsic::x86_avx512_vcvtss2usi64:
1535 case Intrinsic::x86_avx512_cvttss2usi:
1536 case Intrinsic::x86_avx512_cvttss2usi64:
1537 case Intrinsic::x86_avx512_vcvtsd2usi32:
1538 case Intrinsic::x86_avx512_vcvtsd2usi64:
1539 case Intrinsic::x86_avx512_cvttsd2usi:
1540 case Intrinsic::x86_avx512_cvttsd2usi64:
1541 return !Call->isStrictFP();
1542
1543 // Sign operations are actually bitwise operations, they do not raise
1544 // exceptions even for SNANs.
1545 case Intrinsic::fabs:
1546 case Intrinsic::copysign:
1547 // Non-constrained variants of rounding operations means default FP
1548 // environment, they can be folded in any case.
1549 case Intrinsic::ceil:
1550 case Intrinsic::floor:
1551 case Intrinsic::round:
1552 case Intrinsic::roundeven:
1553 case Intrinsic::trunc:
1554 case Intrinsic::nearbyint:
1555 case Intrinsic::rint:
1556 // Constrained intrinsics can be folded if FP environment is known
1557 // to compiler.
1558 case Intrinsic::experimental_constrained_ceil:
1559 case Intrinsic::experimental_constrained_floor:
1560 case Intrinsic::experimental_constrained_round:
1561 case Intrinsic::experimental_constrained_roundeven:
1562 case Intrinsic::experimental_constrained_trunc:
1563 case Intrinsic::experimental_constrained_nearbyint:
1564 case Intrinsic::experimental_constrained_rint:
1565 return true;
1566 default:
1567 return false;
1568 case Intrinsic::not_intrinsic: break;
1569 }
1570
1571 if (!F->hasName() || Call->isStrictFP())
1572 return false;
1573
1574 // In these cases, the check of the length is required. We don't want to
1575 // return true for a name like "cos\0blah" which strcmp would return equal to
1576 // "cos", but has length 8.
1577 StringRef Name = F->getName();
1578 switch (Name[0]) {
1579 default:
1580 return false;
1581 case 'a':
1582 return Name == "acos" || Name == "acosf" ||
1583 Name == "asin" || Name == "asinf" ||
1584 Name == "atan" || Name == "atanf" ||
1585 Name == "atan2" || Name == "atan2f";
1586 case 'c':
1587 return Name == "ceil" || Name == "ceilf" ||
1588 Name == "cos" || Name == "cosf" ||
1589 Name == "cosh" || Name == "coshf";
1590 case 'e':
1591 return Name == "exp" || Name == "expf" ||
1592 Name == "exp2" || Name == "exp2f";
1593 case 'f':
1594 return Name == "fabs" || Name == "fabsf" ||
1595 Name == "floor" || Name == "floorf" ||
1596 Name == "fmod" || Name == "fmodf";
1597 case 'l':
1598 return Name == "log" || Name == "logf" ||
1599 Name == "log2" || Name == "log2f" ||
1600 Name == "log10" || Name == "log10f";
1601 case 'n':
1602 return Name == "nearbyint" || Name == "nearbyintf";
1603 case 'p':
1604 return Name == "pow" || Name == "powf";
1605 case 'r':
1606 return Name == "remainder" || Name == "remainderf" ||
1607 Name == "rint" || Name == "rintf" ||
1608 Name == "round" || Name == "roundf";
1609 case 's':
1610 return Name == "sin" || Name == "sinf" ||
1611 Name == "sinh" || Name == "sinhf" ||
1612 Name == "sqrt" || Name == "sqrtf";
1613 case 't':
1614 return Name == "tan" || Name == "tanf" ||
1615 Name == "tanh" || Name == "tanhf" ||
1616 Name == "trunc" || Name == "truncf";
1617 case '_':
1618 // Check for various function names that get used for the math functions
1619 // when the header files are preprocessed with the macro
1620 // __FINITE_MATH_ONLY__ enabled.
1621 // The '12' here is the length of the shortest name that can match.
1622 // We need to check the size before looking at Name[1] and Name[2]
1623 // so we may as well check a limit that will eliminate mismatches.
1624 if (Name.size() < 12 || Name[1] != '_')
1625 return false;
1626 switch (Name[2]) {
1627 default:
1628 return false;
1629 case 'a':
1630 return Name == "__acos_finite" || Name == "__acosf_finite" ||
1631 Name == "__asin_finite" || Name == "__asinf_finite" ||
1632 Name == "__atan2_finite" || Name == "__atan2f_finite";
1633 case 'c':
1634 return Name == "__cosh_finite" || Name == "__coshf_finite";
1635 case 'e':
1636 return Name == "__exp_finite" || Name == "__expf_finite" ||
1637 Name == "__exp2_finite" || Name == "__exp2f_finite";
1638 case 'l':
1639 return Name == "__log_finite" || Name == "__logf_finite" ||
1640 Name == "__log10_finite" || Name == "__log10f_finite";
1641 case 'p':
1642 return Name == "__pow_finite" || Name == "__powf_finite";
1643 case 's':
1644 return Name == "__sinh_finite" || Name == "__sinhf_finite";
1645 }
1646 }
1647}
1648
1649namespace {
1650
1651Constant *GetConstantFoldFPValue(double V, Type *Ty) {
1652 if (Ty->isHalfTy() || Ty->isFloatTy()) {
1653 APFloat APF(V);
1654 bool unused;
1655 APF.convert(Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &unused);
1656 return ConstantFP::get(Ty->getContext(), APF);
1657 }
1658 if (Ty->isDoubleTy())
1659 return ConstantFP::get(Ty->getContext(), APFloat(V));
1660 llvm_unreachable("Can only constant fold half/float/double")::llvm::llvm_unreachable_internal("Can only constant fold half/float/double"
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 1660)
;
1661}
1662
1663/// Clear the floating-point exception state.
1664inline void llvm_fenv_clearexcept() {
1665#if defined(HAVE_FENV_H1) && HAVE_DECL_FE_ALL_EXCEPT1
1666 feclearexcept(FE_ALL_EXCEPT(0x20 | 0x04 | 0x10 | 0x08 | 0x01));
1667#endif
1668 errno(*__errno_location ()) = 0;
1669}
1670
1671/// Test if a floating-point exception was raised.
1672inline bool llvm_fenv_testexcept() {
1673 int errno_val = errno(*__errno_location ());
1674 if (errno_val == ERANGE34 || errno_val == EDOM33)
1675 return true;
1676#if defined(HAVE_FENV_H1) && HAVE_DECL_FE_ALL_EXCEPT1 && HAVE_DECL_FE_INEXACT1
1677 if (fetestexcept(FE_ALL_EXCEPT(0x20 | 0x04 | 0x10 | 0x08 | 0x01) & ~FE_INEXACT0x20))
1678 return true;
1679#endif
1680 return false;
1681}
1682
1683Constant *ConstantFoldFP(double (*NativeFP)(double), double V, Type *Ty) {
1684 llvm_fenv_clearexcept();
1685 V = NativeFP(V);
1686 if (llvm_fenv_testexcept()) {
1687 llvm_fenv_clearexcept();
1688 return nullptr;
1689 }
1690
1691 return GetConstantFoldFPValue(V, Ty);
1692}
1693
1694Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), double V,
1695 double W, Type *Ty) {
1696 llvm_fenv_clearexcept();
1697 V = NativeFP(V, W);
1698 if (llvm_fenv_testexcept()) {
1699 llvm_fenv_clearexcept();
1700 return nullptr;
1701 }
1702
1703 return GetConstantFoldFPValue(V, Ty);
1704}
1705
1706Constant *constantFoldVectorReduce(Intrinsic::ID IID, Constant *Op) {
1707 FixedVectorType *VT = dyn_cast<FixedVectorType>(Op->getType());
1708 if (!VT)
1709 return nullptr;
1710
1711 // This isn't strictly necessary, but handle the special/common case of zero:
1712 // all integer reductions of a zero input produce zero.
1713 if (isa<ConstantAggregateZero>(Op))
1714 return ConstantInt::get(VT->getElementType(), 0);
1715
1716 // This is the same as the underlying binops - poison propagates.
1717 if (isa<PoisonValue>(Op))
1718 return PoisonValue::get(VT->getElementType());
1719
1720 // TODO: Handle undef.
1721 if (!isa<ConstantVector>(Op) && !isa<ConstantDataVector>(Op))
1722 return nullptr;
1723
1724 auto *EltC = dyn_cast<ConstantInt>(Op->getAggregateElement(0U));
1725 if (!EltC)
1726 return nullptr;
1727
1728 APInt Acc = EltC->getValue();
1729 for (unsigned I = 1, E = VT->getNumElements(); I != E; I++) {
1730 if (!(EltC = dyn_cast<ConstantInt>(Op->getAggregateElement(I))))
1731 return nullptr;
1732 const APInt &X = EltC->getValue();
1733 switch (IID) {
1734 case Intrinsic::vector_reduce_add:
1735 Acc = Acc + X;
1736 break;
1737 case Intrinsic::vector_reduce_mul:
1738 Acc = Acc * X;
1739 break;
1740 case Intrinsic::vector_reduce_and:
1741 Acc = Acc & X;
1742 break;
1743 case Intrinsic::vector_reduce_or:
1744 Acc = Acc | X;
1745 break;
1746 case Intrinsic::vector_reduce_xor:
1747 Acc = Acc ^ X;
1748 break;
1749 case Intrinsic::vector_reduce_smin:
1750 Acc = APIntOps::smin(Acc, X);
1751 break;
1752 case Intrinsic::vector_reduce_smax:
1753 Acc = APIntOps::smax(Acc, X);
1754 break;
1755 case Intrinsic::vector_reduce_umin:
1756 Acc = APIntOps::umin(Acc, X);
1757 break;
1758 case Intrinsic::vector_reduce_umax:
1759 Acc = APIntOps::umax(Acc, X);
1760 break;
1761 }
1762 }
1763
1764 return ConstantInt::get(Op->getContext(), Acc);
1765}
1766
1767/// Attempt to fold an SSE floating point to integer conversion of a constant
1768/// floating point. If roundTowardZero is false, the default IEEE rounding is
1769/// used (toward nearest, ties to even). This matches the behavior of the
1770/// non-truncating SSE instructions in the default rounding mode. The desired
1771/// integer type Ty is used to select how many bits are available for the
1772/// result. Returns null if the conversion cannot be performed, otherwise
1773/// returns the Constant value resulting from the conversion.
1774Constant *ConstantFoldSSEConvertToInt(const APFloat &Val, bool roundTowardZero,
1775 Type *Ty, bool IsSigned) {
1776 // All of these conversion intrinsics form an integer of at most 64bits.
1777 unsigned ResultWidth = Ty->getIntegerBitWidth();
1778 assert(ResultWidth <= 64 &&(static_cast <bool> (ResultWidth <= 64 && "Can only constant fold conversions to 64 and 32 bit ints"
) ? void (0) : __assert_fail ("ResultWidth <= 64 && \"Can only constant fold conversions to 64 and 32 bit ints\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 1779, __extension__ __PRETTY_FUNCTION__))
1779 "Can only constant fold conversions to 64 and 32 bit ints")(static_cast <bool> (ResultWidth <= 64 && "Can only constant fold conversions to 64 and 32 bit ints"
) ? void (0) : __assert_fail ("ResultWidth <= 64 && \"Can only constant fold conversions to 64 and 32 bit ints\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 1779, __extension__ __PRETTY_FUNCTION__))
;
1780
1781 uint64_t UIntVal;
1782 bool isExact = false;
1783 APFloat::roundingMode mode = roundTowardZero? APFloat::rmTowardZero
1784 : APFloat::rmNearestTiesToEven;
1785 APFloat::opStatus status =
1786 Val.convertToInteger(makeMutableArrayRef(UIntVal), ResultWidth,
1787 IsSigned, mode, &isExact);
1788 if (status != APFloat::opOK &&
1789 (!roundTowardZero || status != APFloat::opInexact))
1790 return nullptr;
1791 return ConstantInt::get(Ty, UIntVal, IsSigned);
1792}
1793
1794double getValueAsDouble(ConstantFP *Op) {
1795 Type *Ty = Op->getType();
1796
1797 if (Ty->isBFloatTy() || Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy())
1798 return Op->getValueAPF().convertToDouble();
1799
1800 bool unused;
1801 APFloat APF = Op->getValueAPF();
1802 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &unused);
1803 return APF.convertToDouble();
1804}
1805
1806static bool getConstIntOrUndef(Value *Op, const APInt *&C) {
1807 if (auto *CI
26.1
'CI' is null
34.1
'CI' is non-null
26.1
'CI' is null
34.1
'CI' is non-null
26.1
'CI' is null
34.1
'CI' is non-null
= dyn_cast<ConstantInt>(Op)) {
26
Assuming 'Op' is not a 'ConstantInt'
27
Taking false branch
34
Assuming 'Op' is a 'ConstantInt'
35
Taking true branch
1808 C = &CI->getValue();
1809 return true;
36
Returning the value 1, which participates in a condition later
1810 }
1811 if (isa<UndefValue>(Op)) {
28
Assuming 'Op' is a 'UndefValue'
29
Taking true branch
1812 C = nullptr;
30
Null pointer value stored to 'C0'
1813 return true;
31
Returning the value 1, which participates in a condition later
1814 }
1815 return false;
1816}
1817
1818static Constant *ConstantFoldScalarCall1(StringRef Name,
1819 Intrinsic::ID IntrinsicID,
1820 Type *Ty,
1821 ArrayRef<Constant *> Operands,
1822 const TargetLibraryInfo *TLI,
1823 const CallBase *Call) {
1824 assert(Operands.size() == 1 && "Wrong number of operands.")(static_cast <bool> (Operands.size() == 1 && "Wrong number of operands."
) ? void (0) : __assert_fail ("Operands.size() == 1 && \"Wrong number of operands.\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 1824, __extension__ __PRETTY_FUNCTION__))
;
1825
1826 if (IntrinsicID == Intrinsic::is_constant) {
1827 // We know we have a "Constant" argument. But we want to only
1828 // return true for manifest constants, not those that depend on
1829 // constants with unknowable values, e.g. GlobalValue or BlockAddress.
1830 if (Operands[0]->isManifestConstant())
1831 return ConstantInt::getTrue(Ty->getContext());
1832 return nullptr;
1833 }
1834 if (isa<UndefValue>(Operands[0])) {
1835 // cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN.
1836 // ctpop() is between 0 and bitwidth, pick 0 for undef.
1837 // fptoui.sat and fptosi.sat can always fold to zero (for a zero input).
1838 if (IntrinsicID == Intrinsic::cos ||
1839 IntrinsicID == Intrinsic::ctpop ||
1840 IntrinsicID == Intrinsic::fptoui_sat ||
1841 IntrinsicID == Intrinsic::fptosi_sat)
1842 return Constant::getNullValue(Ty);
1843 if (IntrinsicID == Intrinsic::bswap ||
1844 IntrinsicID == Intrinsic::bitreverse ||
1845 IntrinsicID == Intrinsic::launder_invariant_group ||
1846 IntrinsicID == Intrinsic::strip_invariant_group)
1847 return Operands[0];
1848 }
1849
1850 if (isa<ConstantPointerNull>(Operands[0])) {
1851 // launder(null) == null == strip(null) iff in addrspace 0
1852 if (IntrinsicID == Intrinsic::launder_invariant_group ||
1853 IntrinsicID == Intrinsic::strip_invariant_group) {
1854 // If instruction is not yet put in a basic block (e.g. when cloning
1855 // a function during inlining), Call's caller may not be available.
1856 // So check Call's BB first before querying Call->getCaller.
1857 const Function *Caller =
1858 Call->getParent() ? Call->getCaller() : nullptr;
1859 if (Caller &&
1860 !NullPointerIsDefined(
1861 Caller, Operands[0]->getType()->getPointerAddressSpace())) {
1862 return Operands[0];
1863 }
1864 return nullptr;
1865 }
1866 }
1867
1868 if (auto *Op = dyn_cast<ConstantFP>(Operands[0])) {
1869 if (IntrinsicID == Intrinsic::convert_to_fp16) {
1870 APFloat Val(Op->getValueAPF());
1871
1872 bool lost = false;
1873 Val.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &lost);
1874
1875 return ConstantInt::get(Ty->getContext(), Val.bitcastToAPInt());
1876 }
1877
1878 APFloat U = Op->getValueAPF();
1879
1880 if (IntrinsicID == Intrinsic::wasm_trunc_signed ||
1881 IntrinsicID == Intrinsic::wasm_trunc_unsigned) {
1882 bool Signed = IntrinsicID == Intrinsic::wasm_trunc_signed;
1883
1884 if (U.isNaN())
1885 return nullptr;
1886
1887 unsigned Width = Ty->getIntegerBitWidth();
1888 APSInt Int(Width, !Signed);
1889 bool IsExact = false;
1890 APFloat::opStatus Status =
1891 U.convertToInteger(Int, APFloat::rmTowardZero, &IsExact);
1892
1893 if (Status == APFloat::opOK || Status == APFloat::opInexact)
1894 return ConstantInt::get(Ty, Int);
1895
1896 return nullptr;
1897 }
1898
1899 if (IntrinsicID == Intrinsic::fptoui_sat ||
1900 IntrinsicID == Intrinsic::fptosi_sat) {
1901 // convertToInteger() already has the desired saturation semantics.
1902 APSInt Int(Ty->getIntegerBitWidth(),
1903 IntrinsicID == Intrinsic::fptoui_sat);
1904 bool IsExact;
1905 U.convertToInteger(Int, APFloat::rmTowardZero, &IsExact);
1906 return ConstantInt::get(Ty, Int);
1907 }
1908
1909 if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
1910 return nullptr;
1911
1912 // Use internal versions of these intrinsics.
1913
1914 if (IntrinsicID == Intrinsic::nearbyint || IntrinsicID == Intrinsic::rint) {
1915 U.roundToIntegral(APFloat::rmNearestTiesToEven);
1916 return ConstantFP::get(Ty->getContext(), U);
1917 }
1918
1919 if (IntrinsicID == Intrinsic::round) {
1920 U.roundToIntegral(APFloat::rmNearestTiesToAway);
1921 return ConstantFP::get(Ty->getContext(), U);
1922 }
1923
1924 if (IntrinsicID == Intrinsic::roundeven) {
1925 U.roundToIntegral(APFloat::rmNearestTiesToEven);
1926 return ConstantFP::get(Ty->getContext(), U);
1927 }
1928
1929 if (IntrinsicID == Intrinsic::ceil) {
1930 U.roundToIntegral(APFloat::rmTowardPositive);
1931 return ConstantFP::get(Ty->getContext(), U);
1932 }
1933
1934 if (IntrinsicID == Intrinsic::floor) {
1935 U.roundToIntegral(APFloat::rmTowardNegative);
1936 return ConstantFP::get(Ty->getContext(), U);
1937 }
1938
1939 if (IntrinsicID == Intrinsic::trunc) {
1940 U.roundToIntegral(APFloat::rmTowardZero);
1941 return ConstantFP::get(Ty->getContext(), U);
1942 }
1943
1944 if (IntrinsicID == Intrinsic::fabs) {
1945 U.clearSign();
1946 return ConstantFP::get(Ty->getContext(), U);
1947 }
1948
1949 if (IntrinsicID == Intrinsic::amdgcn_fract) {
1950 // The v_fract instruction behaves like the OpenCL spec, which defines
1951 // fract(x) as fmin(x - floor(x), 0x1.fffffep-1f): "The min() operator is
1952 // there to prevent fract(-small) from returning 1.0. It returns the
1953 // largest positive floating-point number less than 1.0."
1954 APFloat FloorU(U);
1955 FloorU.roundToIntegral(APFloat::rmTowardNegative);
1956 APFloat FractU(U - FloorU);
1957 APFloat AlmostOne(U.getSemantics(), 1);
1958 AlmostOne.next(/*nextDown*/ true);
1959 return ConstantFP::get(Ty->getContext(), minimum(FractU, AlmostOne));
1960 }
1961
1962 // Rounding operations (floor, trunc, ceil, round and nearbyint) do not
1963 // raise FP exceptions, unless the argument is signaling NaN.
1964
1965 Optional<APFloat::roundingMode> RM;
1966 switch (IntrinsicID) {
1967 default:
1968 break;
1969 case Intrinsic::experimental_constrained_nearbyint:
1970 case Intrinsic::experimental_constrained_rint: {
1971 auto CI = cast<ConstrainedFPIntrinsic>(Call);
1972 RM = CI->getRoundingMode();
1973 if (!RM || RM.getValue() == RoundingMode::Dynamic)
1974 return nullptr;
1975 break;
1976 }
1977 case Intrinsic::experimental_constrained_round:
1978 RM = APFloat::rmNearestTiesToAway;
1979 break;
1980 case Intrinsic::experimental_constrained_ceil:
1981 RM = APFloat::rmTowardPositive;
1982 break;
1983 case Intrinsic::experimental_constrained_floor:
1984 RM = APFloat::rmTowardNegative;
1985 break;
1986 case Intrinsic::experimental_constrained_trunc:
1987 RM = APFloat::rmTowardZero;
1988 break;
1989 }
1990 if (RM) {
1991 auto CI = cast<ConstrainedFPIntrinsic>(Call);
1992 if (U.isFinite()) {
1993 APFloat::opStatus St = U.roundToIntegral(*RM);
1994 if (IntrinsicID == Intrinsic::experimental_constrained_rint &&
1995 St == APFloat::opInexact) {
1996 Optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
1997 if (EB && *EB == fp::ebStrict)
1998 return nullptr;
1999 }
2000 } else if (U.isSignaling()) {
2001 Optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
2002 if (EB && *EB != fp::ebIgnore)
2003 return nullptr;
2004 U = APFloat::getQNaN(U.getSemantics());
2005 }
2006 return ConstantFP::get(Ty->getContext(), U);
2007 }
2008
2009 /// We only fold functions with finite arguments. Folding NaN and inf is
2010 /// likely to be aborted with an exception anyway, and some host libms
2011 /// have known errors raising exceptions.
2012 if (!U.isFinite())
2013 return nullptr;
2014
2015 /// Currently APFloat versions of these functions do not exist, so we use
2016 /// the host native double versions. Float versions are not called
2017 /// directly but for all these it is true (float)(f((double)arg)) ==
2018 /// f(arg). Long double not supported yet.
2019 double V = getValueAsDouble(Op);
2020
2021 switch (IntrinsicID) {
2022 default: break;
2023 case Intrinsic::log:
2024 return ConstantFoldFP(log, V, Ty);
2025 case Intrinsic::log2:
2026 // TODO: What about hosts that lack a C99 library?
2027 return ConstantFoldFP(Log2, V, Ty);
2028 case Intrinsic::log10:
2029 // TODO: What about hosts that lack a C99 library?
2030 return ConstantFoldFP(log10, V, Ty);
2031 case Intrinsic::exp:
2032 return ConstantFoldFP(exp, V, Ty);
2033 case Intrinsic::exp2:
2034 // Fold exp2(x) as pow(2, x), in case the host lacks a C99 library.
2035 return ConstantFoldBinaryFP(pow, 2.0, V, Ty);
2036 case Intrinsic::sin:
2037 return ConstantFoldFP(sin, V, Ty);
2038 case Intrinsic::cos:
2039 return ConstantFoldFP(cos, V, Ty);
2040 case Intrinsic::sqrt:
2041 return ConstantFoldFP(sqrt, V, Ty);
2042 case Intrinsic::amdgcn_cos:
2043 case Intrinsic::amdgcn_sin:
2044 if (V < -256.0 || V > 256.0)
2045 // The gfx8 and gfx9 architectures handle arguments outside the range
2046 // [-256, 256] differently. This should be a rare case so bail out
2047 // rather than trying to handle the difference.
2048 return nullptr;
2049 bool IsCos = IntrinsicID == Intrinsic::amdgcn_cos;
2050 double V4 = V * 4.0;
2051 if (V4 == floor(V4)) {
2052 // Force exact results for quarter-integer inputs.
2053 const double SinVals[4] = { 0.0, 1.0, 0.0, -1.0 };
2054 V = SinVals[((int)V4 + (IsCos ? 1 : 0)) & 3];
2055 } else {
2056 if (IsCos)
2057 V = cos(V * 2.0 * numbers::pi);
2058 else
2059 V = sin(V * 2.0 * numbers::pi);
2060 }
2061 return GetConstantFoldFPValue(V, Ty);
2062 }
2063
2064 if (!TLI)
2065 return nullptr;
2066
2067 LibFunc Func = NotLibFunc;
2068 TLI->getLibFunc(Name, Func);
2069 switch (Func) {
2070 default:
2071 break;
2072 case LibFunc_acos:
2073 case LibFunc_acosf:
2074 case LibFunc_acos_finite:
2075 case LibFunc_acosf_finite:
2076 if (TLI->has(Func))
2077 return ConstantFoldFP(acos, V, Ty);
2078 break;
2079 case LibFunc_asin:
2080 case LibFunc_asinf:
2081 case LibFunc_asin_finite:
2082 case LibFunc_asinf_finite:
2083 if (TLI->has(Func))
2084 return ConstantFoldFP(asin, V, Ty);
2085 break;
2086 case LibFunc_atan:
2087 case LibFunc_atanf:
2088 if (TLI->has(Func))
2089 return ConstantFoldFP(atan, V, Ty);
2090 break;
2091 case LibFunc_ceil:
2092 case LibFunc_ceilf:
2093 if (TLI->has(Func)) {
2094 U.roundToIntegral(APFloat::rmTowardPositive);
2095 return ConstantFP::get(Ty->getContext(), U);
2096 }
2097 break;
2098 case LibFunc_cos:
2099 case LibFunc_cosf:
2100 if (TLI->has(Func))
2101 return ConstantFoldFP(cos, V, Ty);
2102 break;
2103 case LibFunc_cosh:
2104 case LibFunc_coshf:
2105 case LibFunc_cosh_finite:
2106 case LibFunc_coshf_finite:
2107 if (TLI->has(Func))
2108 return ConstantFoldFP(cosh, V, Ty);
2109 break;
2110 case LibFunc_exp:
2111 case LibFunc_expf:
2112 case LibFunc_exp_finite:
2113 case LibFunc_expf_finite:
2114 if (TLI->has(Func))
2115 return ConstantFoldFP(exp, V, Ty);
2116 break;
2117 case LibFunc_exp2:
2118 case LibFunc_exp2f:
2119 case LibFunc_exp2_finite:
2120 case LibFunc_exp2f_finite:
2121 if (TLI->has(Func))
2122 // Fold exp2(x) as pow(2, x), in case the host lacks a C99 library.
2123 return ConstantFoldBinaryFP(pow, 2.0, V, Ty);
2124 break;
2125 case LibFunc_fabs:
2126 case LibFunc_fabsf:
2127 if (TLI->has(Func)) {
2128 U.clearSign();
2129 return ConstantFP::get(Ty->getContext(), U);
2130 }
2131 break;
2132 case LibFunc_floor:
2133 case LibFunc_floorf:
2134 if (TLI->has(Func)) {
2135 U.roundToIntegral(APFloat::rmTowardNegative);
2136 return ConstantFP::get(Ty->getContext(), U);
2137 }
2138 break;
2139 case LibFunc_log:
2140 case LibFunc_logf:
2141 case LibFunc_log_finite:
2142 case LibFunc_logf_finite:
2143 if (V > 0.0 && TLI->has(Func))
2144 return ConstantFoldFP(log, V, Ty);
2145 break;
2146 case LibFunc_log2:
2147 case LibFunc_log2f:
2148 case LibFunc_log2_finite:
2149 case LibFunc_log2f_finite:
2150 if (V > 0.0 && TLI->has(Func))
2151 // TODO: What about hosts that lack a C99 library?
2152 return ConstantFoldFP(Log2, V, Ty);
2153 break;
2154 case LibFunc_log10:
2155 case LibFunc_log10f:
2156 case LibFunc_log10_finite:
2157 case LibFunc_log10f_finite:
2158 if (V > 0.0 && TLI->has(Func))
2159 // TODO: What about hosts that lack a C99 library?
2160 return ConstantFoldFP(log10, V, Ty);
2161 break;
2162 case LibFunc_nearbyint:
2163 case LibFunc_nearbyintf:
2164 case LibFunc_rint:
2165 case LibFunc_rintf:
2166 if (TLI->has(Func)) {
2167 U.roundToIntegral(APFloat::rmNearestTiesToEven);
2168 return ConstantFP::get(Ty->getContext(), U);
2169 }
2170 break;
2171 case LibFunc_round:
2172 case LibFunc_roundf:
2173 if (TLI->has(Func)) {
2174 U.roundToIntegral(APFloat::rmNearestTiesToAway);
2175 return ConstantFP::get(Ty->getContext(), U);
2176 }
2177 break;
2178 case LibFunc_sin:
2179 case LibFunc_sinf:
2180 if (TLI->has(Func))
2181 return ConstantFoldFP(sin, V, Ty);
2182 break;
2183 case LibFunc_sinh:
2184 case LibFunc_sinhf:
2185 case LibFunc_sinh_finite:
2186 case LibFunc_sinhf_finite:
2187 if (TLI->has(Func))
2188 return ConstantFoldFP(sinh, V, Ty);
2189 break;
2190 case LibFunc_sqrt:
2191 case LibFunc_sqrtf:
2192 if (V >= 0.0 && TLI->has(Func))
2193 return ConstantFoldFP(sqrt, V, Ty);
2194 break;
2195 case LibFunc_tan:
2196 case LibFunc_tanf:
2197 if (TLI->has(Func))
2198 return ConstantFoldFP(tan, V, Ty);
2199 break;
2200 case LibFunc_tanh:
2201 case LibFunc_tanhf:
2202 if (TLI->has(Func))
2203 return ConstantFoldFP(tanh, V, Ty);
2204 break;
2205 case LibFunc_trunc:
2206 case LibFunc_truncf:
2207 if (TLI->has(Func)) {
2208 U.roundToIntegral(APFloat::rmTowardZero);
2209 return ConstantFP::get(Ty->getContext(), U);
2210 }
2211 break;
2212 }
2213 return nullptr;
2214 }
2215
2216 if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) {
2217 switch (IntrinsicID) {
2218 case Intrinsic::bswap:
2219 return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap());
2220 case Intrinsic::ctpop:
2221 return ConstantInt::get(Ty, Op->getValue().countPopulation());
2222 case Intrinsic::bitreverse:
2223 return ConstantInt::get(Ty->getContext(), Op->getValue().reverseBits());
2224 case Intrinsic::convert_from_fp16: {
2225 APFloat Val(APFloat::IEEEhalf(), Op->getValue());
2226
2227 bool lost = false;
2228 APFloat::opStatus status = Val.convert(
2229 Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &lost);
2230
2231 // Conversion is always precise.
2232 (void)status;
2233 assert(status == APFloat::opOK && !lost &&(static_cast <bool> (status == APFloat::opOK &&
!lost && "Precision lost during fp16 constfolding") ?
void (0) : __assert_fail ("status == APFloat::opOK && !lost && \"Precision lost during fp16 constfolding\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 2234, __extension__ __PRETTY_FUNCTION__))
2234 "Precision lost during fp16 constfolding")(static_cast <bool> (status == APFloat::opOK &&
!lost && "Precision lost during fp16 constfolding") ?
void (0) : __assert_fail ("status == APFloat::opOK && !lost && \"Precision lost during fp16 constfolding\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 2234, __extension__ __PRETTY_FUNCTION__))
;
2235
2236 return ConstantFP::get(Ty->getContext(), Val);
2237 }
2238 default:
2239 return nullptr;
2240 }
2241 }
2242
2243 switch (IntrinsicID) {
2244 default: break;
2245 case Intrinsic::vector_reduce_add:
2246 case Intrinsic::vector_reduce_mul:
2247 case Intrinsic::vector_reduce_and:
2248 case Intrinsic::vector_reduce_or:
2249 case Intrinsic::vector_reduce_xor:
2250 case Intrinsic::vector_reduce_smin:
2251 case Intrinsic::vector_reduce_smax:
2252 case Intrinsic::vector_reduce_umin:
2253 case Intrinsic::vector_reduce_umax:
2254 if (Constant *C = constantFoldVectorReduce(IntrinsicID, Operands[0]))
2255 return C;
2256 break;
2257 }
2258
2259 // Support ConstantVector in case we have an Undef in the top.
2260 if (isa<ConstantVector>(Operands[0]) ||
2261 isa<ConstantDataVector>(Operands[0])) {
2262 auto *Op = cast<Constant>(Operands[0]);
2263 switch (IntrinsicID) {
2264 default: break;
2265 case Intrinsic::x86_sse_cvtss2si:
2266 case Intrinsic::x86_sse_cvtss2si64:
2267 case Intrinsic::x86_sse2_cvtsd2si:
2268 case Intrinsic::x86_sse2_cvtsd2si64:
2269 if (ConstantFP *FPOp =
2270 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2271 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2272 /*roundTowardZero=*/false, Ty,
2273 /*IsSigned*/true);
2274 break;
2275 case Intrinsic::x86_sse_cvttss2si:
2276 case Intrinsic::x86_sse_cvttss2si64:
2277 case Intrinsic::x86_sse2_cvttsd2si:
2278 case Intrinsic::x86_sse2_cvttsd2si64:
2279 if (ConstantFP *FPOp =
2280 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2281 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2282 /*roundTowardZero=*/true, Ty,
2283 /*IsSigned*/true);
2284 break;
2285 }
2286 }
2287
2288 return nullptr;
2289}
2290
2291static Constant *ConstantFoldScalarCall2(StringRef Name,
2292 Intrinsic::ID IntrinsicID,
2293 Type *Ty,
2294 ArrayRef<Constant *> Operands,
2295 const TargetLibraryInfo *TLI,
2296 const CallBase *Call) {
2297 assert(Operands.size() == 2 && "Wrong number of operands.")(static_cast <bool> (Operands.size() == 2 && "Wrong number of operands."
) ? void (0) : __assert_fail ("Operands.size() == 2 && \"Wrong number of operands.\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 2297, __extension__ __PRETTY_FUNCTION__))
;
1
Assuming the condition is true
2
'?' condition is true
2298
2299 if (Ty->isFloatingPointTy()) {
3
Calling 'Type::isFloatingPointTy'
12
Returning from 'Type::isFloatingPointTy'
13
Taking false branch
2300 // TODO: We should have undef handling for all of the FP intrinsics that
2301 // are attempted to be folded in this function.
2302 bool IsOp0Undef = isa<UndefValue>(Operands[0]);
2303 bool IsOp1Undef = isa<UndefValue>(Operands[1]);
2304 switch (IntrinsicID) {
2305 case Intrinsic::maxnum:
2306 case Intrinsic::minnum:
2307 case Intrinsic::maximum:
2308 case Intrinsic::minimum:
2309 // If one argument is undef, return the other argument.
2310 if (IsOp0Undef)
2311 return Operands[1];
2312 if (IsOp1Undef)
2313 return Operands[0];
2314 break;
2315 }
2316 }
2317
2318 if (auto *Op1
14.1
'Op1' is null
14.1
'Op1' is null
14.1
'Op1' is null
= dyn_cast<ConstantFP>(Operands[0])) {
14
Assuming the object is not a 'ConstantFP'
15
Taking false branch
2319 if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
2320 return nullptr;
2321 double Op1V = getValueAsDouble(Op1);
2322
2323 if (auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
2324 if (Op2->getType() != Op1->getType())
2325 return nullptr;
2326
2327 double Op2V = getValueAsDouble(Op2);
2328 if (IntrinsicID == Intrinsic::pow) {
2329 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
2330 }
2331 if (IntrinsicID == Intrinsic::copysign) {
2332 APFloat V1 = Op1->getValueAPF();
2333 const APFloat &V2 = Op2->getValueAPF();
2334 V1.copySign(V2);
2335 return ConstantFP::get(Ty->getContext(), V1);
2336 }
2337
2338 if (IntrinsicID == Intrinsic::minnum) {
2339 const APFloat &C1 = Op1->getValueAPF();
2340 const APFloat &C2 = Op2->getValueAPF();
2341 return ConstantFP::get(Ty->getContext(), minnum(C1, C2));
2342 }
2343
2344 if (IntrinsicID == Intrinsic::maxnum) {
2345 const APFloat &C1 = Op1->getValueAPF();
2346 const APFloat &C2 = Op2->getValueAPF();
2347 return ConstantFP::get(Ty->getContext(), maxnum(C1, C2));
2348 }
2349
2350 if (IntrinsicID == Intrinsic::minimum) {
2351 const APFloat &C1 = Op1->getValueAPF();
2352 const APFloat &C2 = Op2->getValueAPF();
2353 return ConstantFP::get(Ty->getContext(), minimum(C1, C2));
2354 }
2355
2356 if (IntrinsicID == Intrinsic::maximum) {
2357 const APFloat &C1 = Op1->getValueAPF();
2358 const APFloat &C2 = Op2->getValueAPF();
2359 return ConstantFP::get(Ty->getContext(), maximum(C1, C2));
2360 }
2361
2362 if (IntrinsicID == Intrinsic::amdgcn_fmul_legacy) {
2363 const APFloat &C1 = Op1->getValueAPF();
2364 const APFloat &C2 = Op2->getValueAPF();
2365 // The legacy behaviour is that multiplying +/- 0.0 by anything, even
2366 // NaN or infinity, gives +0.0.
2367 if (C1.isZero() || C2.isZero())
2368 return ConstantFP::getNullValue(Ty);
2369 return ConstantFP::get(Ty->getContext(), C1 * C2);
2370 }
2371
2372 if (!TLI)
2373 return nullptr;
2374
2375 LibFunc Func = NotLibFunc;
2376 TLI->getLibFunc(Name, Func);
2377 switch (Func) {
2378 default:
2379 break;
2380 case LibFunc_pow:
2381 case LibFunc_powf:
2382 case LibFunc_pow_finite:
2383 case LibFunc_powf_finite:
2384 if (TLI->has(Func))
2385 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
2386 break;
2387 case LibFunc_fmod:
2388 case LibFunc_fmodf:
2389 if (TLI->has(Func)) {
2390 APFloat V = Op1->getValueAPF();
2391 if (APFloat::opStatus::opOK == V.mod(Op2->getValueAPF()))
2392 return ConstantFP::get(Ty->getContext(), V);
2393 }
2394 break;
2395 case LibFunc_remainder:
2396 case LibFunc_remainderf:
2397 if (TLI->has(Func)) {
2398 APFloat V = Op1->getValueAPF();
2399 if (APFloat::opStatus::opOK == V.remainder(Op2->getValueAPF()))
2400 return ConstantFP::get(Ty->getContext(), V);
2401 }
2402 break;
2403 case LibFunc_atan2:
2404 case LibFunc_atan2f:
2405 case LibFunc_atan2_finite:
2406 case LibFunc_atan2f_finite:
2407 if (TLI->has(Func))
2408 return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
2409 break;
2410 }
2411 } else if (auto *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
2412 if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy())
2413 return ConstantFP::get(Ty->getContext(),
2414 APFloat((float)std::pow((float)Op1V,
2415 (int)Op2C->getZExtValue())));
2416 if (IntrinsicID == Intrinsic::powi && Ty->isFloatTy())
2417 return ConstantFP::get(Ty->getContext(),
2418 APFloat((float)std::pow((float)Op1V,
2419 (int)Op2C->getZExtValue())));
2420 if (IntrinsicID == Intrinsic::powi && Ty->isDoubleTy())
2421 return ConstantFP::get(Ty->getContext(),
2422 APFloat((double)std::pow((double)Op1V,
2423 (int)Op2C->getZExtValue())));
2424
2425 if (IntrinsicID == Intrinsic::amdgcn_ldexp) {
2426 // FIXME: Should flush denorms depending on FP mode, but that's ignored
2427 // everywhere else.
2428
2429 // scalbn is equivalent to ldexp with float radix 2
2430 APFloat Result = scalbn(Op1->getValueAPF(), Op2C->getSExtValue(),
2431 APFloat::rmNearestTiesToEven);
2432 return ConstantFP::get(Ty->getContext(), Result);
2433 }
2434 }
2435 return nullptr;
2436 }
2437
2438 if (Operands[0]->getType()->isIntegerTy() &&
16
Calling 'Type::isIntegerTy'
19
Returning from 'Type::isIntegerTy'
24
Taking true branch
2439 Operands[1]->getType()->isIntegerTy()) {
20
Calling 'Type::isIntegerTy'
23
Returning from 'Type::isIntegerTy'
2440 const APInt *C0, *C1;
2441 if (!getConstIntOrUndef(Operands[0], C0) ||
25
Calling 'getConstIntOrUndef'
32
Returning from 'getConstIntOrUndef'
38
Taking false branch
2442 !getConstIntOrUndef(Operands[1], C1))
33
Calling 'getConstIntOrUndef'
37
Returning from 'getConstIntOrUndef'
2443 return nullptr;
2444
2445 unsigned BitWidth = Ty->getScalarSizeInBits();
2446 switch (IntrinsicID) {
39
Control jumps to 'case abs:' at line 2564
2447 default: break;
2448 case Intrinsic::smax:
2449 if (!C0 && !C1)
2450 return UndefValue::get(Ty);
2451 if (!C0 || !C1)
2452 return ConstantInt::get(Ty, APInt::getSignedMaxValue(BitWidth));
2453 return ConstantInt::get(Ty, C0->sgt(*C1) ? *C0 : *C1);
2454
2455 case Intrinsic::smin:
2456 if (!C0 && !C1)
2457 return UndefValue::get(Ty);
2458 if (!C0 || !C1)
2459 return ConstantInt::get(Ty, APInt::getSignedMinValue(BitWidth));
2460 return ConstantInt::get(Ty, C0->slt(*C1) ? *C0 : *C1);
2461
2462 case Intrinsic::umax:
2463 if (!C0 && !C1)
2464 return UndefValue::get(Ty);
2465 if (!C0 || !C1)
2466 return ConstantInt::get(Ty, APInt::getMaxValue(BitWidth));
2467 return ConstantInt::get(Ty, C0->ugt(*C1) ? *C0 : *C1);
2468
2469 case Intrinsic::umin:
2470 if (!C0 && !C1)
2471 return UndefValue::get(Ty);
2472 if (!C0 || !C1)
2473 return ConstantInt::get(Ty, APInt::getMinValue(BitWidth));
2474 return ConstantInt::get(Ty, C0->ult(*C1) ? *C0 : *C1);
2475
2476 case Intrinsic::usub_with_overflow:
2477 case Intrinsic::ssub_with_overflow:
2478 // X - undef -> { 0, false }
2479 // undef - X -> { 0, false }
2480 if (!C0 || !C1)
2481 return Constant::getNullValue(Ty);
2482 LLVM_FALLTHROUGH[[gnu::fallthrough]];
2483 case Intrinsic::uadd_with_overflow:
2484 case Intrinsic::sadd_with_overflow:
2485 // X + undef -> { -1, false }
2486 // undef + x -> { -1, false }
2487 if (!C0 || !C1) {
2488 return ConstantStruct::get(
2489 cast<StructType>(Ty),
2490 {Constant::getAllOnesValue(Ty->getStructElementType(0)),
2491 Constant::getNullValue(Ty->getStructElementType(1))});
2492 }
2493 LLVM_FALLTHROUGH[[gnu::fallthrough]];
2494 case Intrinsic::smul_with_overflow:
2495 case Intrinsic::umul_with_overflow: {
2496 // undef * X -> { 0, false }
2497 // X * undef -> { 0, false }
2498 if (!C0 || !C1)
2499 return Constant::getNullValue(Ty);
2500
2501 APInt Res;
2502 bool Overflow;
2503 switch (IntrinsicID) {
2504 default: llvm_unreachable("Invalid case")::llvm::llvm_unreachable_internal("Invalid case", "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 2504)
;
2505 case Intrinsic::sadd_with_overflow:
2506 Res = C0->sadd_ov(*C1, Overflow);
2507 break;
2508 case Intrinsic::uadd_with_overflow:
2509 Res = C0->uadd_ov(*C1, Overflow);
2510 break;
2511 case Intrinsic::ssub_with_overflow:
2512 Res = C0->ssub_ov(*C1, Overflow);
2513 break;
2514 case Intrinsic::usub_with_overflow:
2515 Res = C0->usub_ov(*C1, Overflow);
2516 break;
2517 case Intrinsic::smul_with_overflow:
2518 Res = C0->smul_ov(*C1, Overflow);
2519 break;
2520 case Intrinsic::umul_with_overflow:
2521 Res = C0->umul_ov(*C1, Overflow);
2522 break;
2523 }
2524 Constant *Ops[] = {
2525 ConstantInt::get(Ty->getContext(), Res),
2526 ConstantInt::get(Type::getInt1Ty(Ty->getContext()), Overflow)
2527 };
2528 return ConstantStruct::get(cast<StructType>(Ty), Ops);
2529 }
2530 case Intrinsic::uadd_sat:
2531 case Intrinsic::sadd_sat:
2532 if (!C0 && !C1)
2533 return UndefValue::get(Ty);
2534 if (!C0 || !C1)
2535 return Constant::getAllOnesValue(Ty);
2536 if (IntrinsicID == Intrinsic::uadd_sat)
2537 return ConstantInt::get(Ty, C0->uadd_sat(*C1));
2538 else
2539 return ConstantInt::get(Ty, C0->sadd_sat(*C1));
2540 case Intrinsic::usub_sat:
2541 case Intrinsic::ssub_sat:
2542 if (!C0 && !C1)
2543 return UndefValue::get(Ty);
2544 if (!C0 || !C1)
2545 return Constant::getNullValue(Ty);
2546 if (IntrinsicID == Intrinsic::usub_sat)
2547 return ConstantInt::get(Ty, C0->usub_sat(*C1));
2548 else
2549 return ConstantInt::get(Ty, C0->ssub_sat(*C1));
2550 case Intrinsic::cttz:
2551 case Intrinsic::ctlz:
2552 assert(C1 && "Must be constant int")(static_cast <bool> (C1 && "Must be constant int"
) ? void (0) : __assert_fail ("C1 && \"Must be constant int\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 2552, __extension__ __PRETTY_FUNCTION__))
;
2553
2554 // cttz(0, 1) and ctlz(0, 1) are undef.
2555 if (C1->isOneValue() && (!C0 || C0->isNullValue()))
2556 return UndefValue::get(Ty);
2557 if (!C0)
2558 return Constant::getNullValue(Ty);
2559 if (IntrinsicID == Intrinsic::cttz)
2560 return ConstantInt::get(Ty, C0->countTrailingZeros());
2561 else
2562 return ConstantInt::get(Ty, C0->countLeadingZeros());
2563
2564 case Intrinsic::abs:
2565 // Undef or minimum val operand with poison min --> undef
2566 assert(C1 && "Must be constant int")(static_cast <bool> (C1 && "Must be constant int"
) ? void (0) : __assert_fail ("C1 && \"Must be constant int\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 2566, __extension__ __PRETTY_FUNCTION__))
;
40
'?' condition is true
2567 if (C1->isOneValue() && (!C0 || C0->isMinSignedValue()))
41
Calling 'APInt::isOneValue'
49
Returning from 'APInt::isOneValue'
2568 return UndefValue::get(Ty);
2569
2570 // Undef operand with no poison min --> 0 (sign bit must be clear)
2571 if (C1->isNullValue() && !C0)
50
Calling 'APInt::isNullValue'
60
Returning from 'APInt::isNullValue'
2572 return Constant::getNullValue(Ty);
2573
2574 return ConstantInt::get(Ty, C0->abs());
61
Called C++ object pointer is null
2575 }
2576
2577 return nullptr;
2578 }
2579
2580 // Support ConstantVector in case we have an Undef in the top.
2581 if ((isa<ConstantVector>(Operands[0]) ||
2582 isa<ConstantDataVector>(Operands[0])) &&
2583 // Check for default rounding mode.
2584 // FIXME: Support other rounding modes?
2585 isa<ConstantInt>(Operands[1]) &&
2586 cast<ConstantInt>(Operands[1])->getValue() == 4) {
2587 auto *Op = cast<Constant>(Operands[0]);
2588 switch (IntrinsicID) {
2589 default: break;
2590 case Intrinsic::x86_avx512_vcvtss2si32:
2591 case Intrinsic::x86_avx512_vcvtss2si64:
2592 case Intrinsic::x86_avx512_vcvtsd2si32:
2593 case Intrinsic::x86_avx512_vcvtsd2si64:
2594 if (ConstantFP *FPOp =
2595 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2596 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2597 /*roundTowardZero=*/false, Ty,
2598 /*IsSigned*/true);
2599 break;
2600 case Intrinsic::x86_avx512_vcvtss2usi32:
2601 case Intrinsic::x86_avx512_vcvtss2usi64:
2602 case Intrinsic::x86_avx512_vcvtsd2usi32:
2603 case Intrinsic::x86_avx512_vcvtsd2usi64:
2604 if (ConstantFP *FPOp =
2605 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2606 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2607 /*roundTowardZero=*/false, Ty,
2608 /*IsSigned*/false);
2609 break;
2610 case Intrinsic::x86_avx512_cvttss2si:
2611 case Intrinsic::x86_avx512_cvttss2si64:
2612 case Intrinsic::x86_avx512_cvttsd2si:
2613 case Intrinsic::x86_avx512_cvttsd2si64:
2614 if (ConstantFP *FPOp =
2615 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2616 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2617 /*roundTowardZero=*/true, Ty,
2618 /*IsSigned*/true);
2619 break;
2620 case Intrinsic::x86_avx512_cvttss2usi:
2621 case Intrinsic::x86_avx512_cvttss2usi64:
2622 case Intrinsic::x86_avx512_cvttsd2usi:
2623 case Intrinsic::x86_avx512_cvttsd2usi64:
2624 if (ConstantFP *FPOp =
2625 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2626 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2627 /*roundTowardZero=*/true, Ty,
2628 /*IsSigned*/false);
2629 break;
2630 }
2631 }
2632 return nullptr;
2633}
2634
2635static APFloat ConstantFoldAMDGCNCubeIntrinsic(Intrinsic::ID IntrinsicID,
2636 const APFloat &S0,
2637 const APFloat &S1,
2638 const APFloat &S2) {
2639 unsigned ID;
2640 const fltSemantics &Sem = S0.getSemantics();
2641 APFloat MA(Sem), SC(Sem), TC(Sem);
2642 if (abs(S2) >= abs(S0) && abs(S2) >= abs(S1)) {
2643 if (S2.isNegative() && S2.isNonZero() && !S2.isNaN()) {
2644 // S2 < 0
2645 ID = 5;
2646 SC = -S0;
2647 } else {
2648 ID = 4;
2649 SC = S0;
2650 }
2651 MA = S2;
2652 TC = -S1;
2653 } else if (abs(S1) >= abs(S0)) {
2654 if (S1.isNegative() && S1.isNonZero() && !S1.isNaN()) {
2655 // S1 < 0
2656 ID = 3;
2657 TC = -S2;
2658 } else {
2659 ID = 2;
2660 TC = S2;
2661 }
2662 MA = S1;
2663 SC = S0;
2664 } else {
2665 if (S0.isNegative() && S0.isNonZero() && !S0.isNaN()) {
2666 // S0 < 0
2667 ID = 1;
2668 SC = S2;
2669 } else {
2670 ID = 0;
2671 SC = -S2;
2672 }
2673 MA = S0;
2674 TC = -S1;
2675 }
2676 switch (IntrinsicID) {
2677 default:
2678 llvm_unreachable("unhandled amdgcn cube intrinsic")::llvm::llvm_unreachable_internal("unhandled amdgcn cube intrinsic"
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 2678)
;
2679 case Intrinsic::amdgcn_cubeid:
2680 return APFloat(Sem, ID);
2681 case Intrinsic::amdgcn_cubema:
2682 return MA + MA;
2683 case Intrinsic::amdgcn_cubesc:
2684 return SC;
2685 case Intrinsic::amdgcn_cubetc:
2686 return TC;
2687 }
2688}
2689
2690static Constant *ConstantFoldAMDGCNPermIntrinsic(ArrayRef<Constant *> Operands,
2691 Type *Ty) {
2692 const APInt *C0, *C1, *C2;
2693 if (!getConstIntOrUndef(Operands[0], C0) ||
2694 !getConstIntOrUndef(Operands[1], C1) ||
2695 !getConstIntOrUndef(Operands[2], C2))
2696 return nullptr;
2697
2698 if (!C2)
2699 return UndefValue::get(Ty);
2700
2701 APInt Val(32, 0);
2702 unsigned NumUndefBytes = 0;
2703 for (unsigned I = 0; I < 32; I += 8) {
2704 unsigned Sel = C2->extractBitsAsZExtValue(8, I);
2705 unsigned B = 0;
2706
2707 if (Sel >= 13)
2708 B = 0xff;
2709 else if (Sel == 12)
2710 B = 0x00;
2711 else {
2712 const APInt *Src = ((Sel & 10) == 10 || (Sel & 12) == 4) ? C0 : C1;
2713 if (!Src)
2714 ++NumUndefBytes;
2715 else if (Sel < 8)
2716 B = Src->extractBitsAsZExtValue(8, (Sel & 3) * 8);
2717 else
2718 B = Src->extractBitsAsZExtValue(1, (Sel & 1) ? 31 : 15) * 0xff;
2719 }
2720
2721 Val.insertBits(B, I, 8);
2722 }
2723
2724 if (NumUndefBytes == 4)
2725 return UndefValue::get(Ty);
2726
2727 return ConstantInt::get(Ty, Val);
2728}
2729
2730static Constant *ConstantFoldScalarCall3(StringRef Name,
2731 Intrinsic::ID IntrinsicID,
2732 Type *Ty,
2733 ArrayRef<Constant *> Operands,
2734 const TargetLibraryInfo *TLI,
2735 const CallBase *Call) {
2736 assert(Operands.size() == 3 && "Wrong number of operands.")(static_cast <bool> (Operands.size() == 3 && "Wrong number of operands."
) ? void (0) : __assert_fail ("Operands.size() == 3 && \"Wrong number of operands.\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 2736, __extension__ __PRETTY_FUNCTION__))
;
2737
2738 if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
2739 if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
2740 if (const auto *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
2741 switch (IntrinsicID) {
2742 default: break;
2743 case Intrinsic::amdgcn_fma_legacy: {
2744 const APFloat &C1 = Op1->getValueAPF();
2745 const APFloat &C2 = Op2->getValueAPF();
2746 // The legacy behaviour is that multiplying +/- 0.0 by anything, even
2747 // NaN or infinity, gives +0.0.
2748 if (C1.isZero() || C2.isZero()) {
2749 const APFloat &C3 = Op3->getValueAPF();
2750 // It's tempting to just return C3 here, but that would give the
2751 // wrong result if C3 was -0.0.
2752 return ConstantFP::get(Ty->getContext(), APFloat(0.0f) + C3);
2753 }
2754 LLVM_FALLTHROUGH[[gnu::fallthrough]];
2755 }
2756 case Intrinsic::fma:
2757 case Intrinsic::fmuladd: {
2758 APFloat V = Op1->getValueAPF();
2759 V.fusedMultiplyAdd(Op2->getValueAPF(), Op3->getValueAPF(),
2760 APFloat::rmNearestTiesToEven);
2761 return ConstantFP::get(Ty->getContext(), V);
2762 }
2763 case Intrinsic::amdgcn_cubeid:
2764 case Intrinsic::amdgcn_cubema:
2765 case Intrinsic::amdgcn_cubesc:
2766 case Intrinsic::amdgcn_cubetc: {
2767 APFloat V = ConstantFoldAMDGCNCubeIntrinsic(
2768 IntrinsicID, Op1->getValueAPF(), Op2->getValueAPF(),
2769 Op3->getValueAPF());
2770 return ConstantFP::get(Ty->getContext(), V);
2771 }
2772 }
2773 }
2774 }
2775 }
2776
2777 if (IntrinsicID == Intrinsic::smul_fix ||
2778 IntrinsicID == Intrinsic::smul_fix_sat) {
2779 // poison * C -> poison
2780 // C * poison -> poison
2781 if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
2782 return PoisonValue::get(Ty);
2783
2784 const APInt *C0, *C1;
2785 if (!getConstIntOrUndef(Operands[0], C0) ||
2786 !getConstIntOrUndef(Operands[1], C1))
2787 return nullptr;
2788
2789 // undef * C -> 0
2790 // C * undef -> 0
2791 if (!C0 || !C1)
2792 return Constant::getNullValue(Ty);
2793
2794 // This code performs rounding towards negative infinity in case the result
2795 // cannot be represented exactly for the given scale. Targets that do care
2796 // about rounding should use a target hook for specifying how rounding
2797 // should be done, and provide their own folding to be consistent with
2798 // rounding. This is the same approach as used by
2799 // DAGTypeLegalizer::ExpandIntRes_MULFIX.
2800 unsigned Scale = cast<ConstantInt>(Operands[2])->getZExtValue();
2801 unsigned Width = C0->getBitWidth();
2802 assert(Scale < Width && "Illegal scale.")(static_cast <bool> (Scale < Width && "Illegal scale."
) ? void (0) : __assert_fail ("Scale < Width && \"Illegal scale.\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/lib/Analysis/ConstantFolding.cpp"
, 2802, __extension__ __PRETTY_FUNCTION__))
;
2803 unsigned ExtendedWidth = Width * 2;
2804 APInt Product = (C0->sextOrSelf(ExtendedWidth) *
2805 C1->sextOrSelf(ExtendedWidth)).ashr(Scale);
2806 if (IntrinsicID == Intrinsic::smul_fix_sat) {
2807 APInt Max = APInt::getSignedMaxValue(Width).sextOrSelf(ExtendedWidth);
2808 APInt Min = APInt::getSignedMinValue(Width).sextOrSelf(ExtendedWidth);
2809 Product = APIntOps::smin(Product, Max);
2810 Product = APIntOps::smax(Product, Min);
2811 }
2812 return ConstantInt::get(Ty->getContext(), Product.sextOrTrunc(Width));
2813 }
2814
2815 if (IntrinsicID == Intrinsic::fshl || IntrinsicID == Intrinsic::fshr) {
2816 const APInt *C0, *C1, *C2;
2817 if (!getConstIntOrUndef(Operands[0], C0) ||
2818 !getConstIntOrUndef(Operands[1], C1) ||
2819 !getConstIntOrUndef(Operands[2], C2))
2820 return nullptr;
2821
2822 bool IsRight = IntrinsicID == Intrinsic::fshr;
2823 if (!C2)
2824 return Operands[IsRight ? 1 : 0];
2825 if (!C0 && !C1)
2826 return UndefValue::get(Ty);
2827
2828 // The shift amount is interpreted as modulo the bitwidth. If the shift
2829 // amount is effectively 0, avoid UB due to oversized inverse shift below.
2830 unsigned BitWidth = C2->getBitWidth();
2831 unsigned ShAmt = C2->urem(BitWidth);
2832 if (!ShAmt)
2833 return Operands[IsRight ? 1 : 0];
2834
2835 // (C0 << ShlAmt) | (C1 >> LshrAmt)
2836 unsigned LshrAmt = IsRight ? ShAmt : BitWidth - ShAmt;
2837 unsigned ShlAmt = !IsRight ? ShAmt : BitWidth - ShAmt;
2838 if (!C0)
2839 return ConstantInt::get(Ty, C1->lshr(LshrAmt));
2840 if (!C1)
2841 return ConstantInt::get(Ty, C0->shl(ShlAmt));
2842 return ConstantInt::get(Ty, C0->shl(ShlAmt) | C1->lshr(LshrAmt));
2843 }
2844
2845 if (IntrinsicID == Intrinsic::amdgcn_perm)
2846 return ConstantFoldAMDGCNPermIntrinsic(Operands, Ty);
2847
2848 return nullptr;
2849}
2850
2851static Constant *ConstantFoldScalarCall(StringRef Name,
2852 Intrinsic::ID IntrinsicID,
2853 Type *Ty,
2854 ArrayRef<Constant *> Operands,
2855 const TargetLibraryInfo *TLI,
2856 const CallBase *Call) {
2857 if (Operands.size() == 1)
2858 return ConstantFoldScalarCall1(Name, IntrinsicID, Ty, Operands, TLI, Call);
2859
2860 if (Operands.size() == 2)
2861 return ConstantFoldScalarCall2(Name, IntrinsicID, Ty, Operands, TLI, Call);
2862
2863 if (Operands.size() == 3)
2864 return ConstantFoldScalarCall3(Name, IntrinsicID, Ty, Operands, TLI, Call);
2865
2866 return nullptr;
2867}
2868
2869static Constant *ConstantFoldFixedVectorCall(
2870 StringRef Name, Intrinsic::ID IntrinsicID, FixedVectorType *FVTy,
2871 ArrayRef<Constant *> Operands, const DataLayout &DL,
2872 const TargetLibraryInfo *TLI, const CallBase *Call) {
2873 SmallVector<Constant *, 4> Result(FVTy->getNumElements());
2874 SmallVector<Constant *, 4> Lane(Operands.size());
2875 Type *Ty = FVTy->getElementType();
2876
2877 switch (IntrinsicID) {
2878 case Intrinsic::masked_load: {
2879 auto *SrcPtr = Operands[0];
2880 auto *Mask = Operands[2];
2881 auto *Passthru = Operands[3];
2882
2883 Constant *VecData = ConstantFoldLoadFromConstPtr(SrcPtr, FVTy, DL);
2884
2885 SmallVector<Constant *, 32> NewElements;
2886 for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) {
2887 auto *MaskElt = Mask->getAggregateElement(I);
2888 if (!MaskElt)
2889 break;
2890 auto *PassthruElt = Passthru->getAggregateElement(I);
2891 auto *VecElt = VecData ? VecData->getAggregateElement(I) : nullptr;
2892 if (isa<UndefValue>(MaskElt)) {
2893 if (PassthruElt)
2894 NewElements.push_back(PassthruElt);
2895 else if (VecElt)
2896 NewElements.push_back(VecElt);
2897 else
2898 return nullptr;
2899 }
2900 if (MaskElt->isNullValue()) {
2901 if (!PassthruElt)
2902 return nullptr;
2903 NewElements.push_back(PassthruElt);
2904 } else if (MaskElt->isOneValue()) {
2905 if (!VecElt)
2906 return nullptr;
2907 NewElements.push_back(VecElt);
2908 } else {
2909 return nullptr;
2910 }
2911 }
2912 if (NewElements.size() != FVTy->getNumElements())
2913 return nullptr;
2914 return ConstantVector::get(NewElements);
2915 }
2916 case Intrinsic::arm_mve_vctp8:
2917 case Intrinsic::arm_mve_vctp16:
2918 case Intrinsic::arm_mve_vctp32:
2919 case Intrinsic::arm_mve_vctp64: {
2920 if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) {
2921 unsigned Lanes = FVTy->getNumElements();
2922 uint64_t Limit = Op->getZExtValue();
2923 // vctp64 are currently modelled as returning a v4i1, not a v2i1. Make
2924 // sure we get the limit right in that case and set all relevant lanes.
2925 if (IntrinsicID == Intrinsic::arm_mve_vctp64)
2926 Limit *= 2;
2927
2928 SmallVector<Constant *, 16> NCs;
2929 for (unsigned i = 0; i < Lanes; i++) {
2930 if (i < Limit)
2931 NCs.push_back(ConstantInt::getTrue(Ty));
2932 else
2933 NCs.push_back(ConstantInt::getFalse(Ty));
2934 }
2935 return ConstantVector::get(NCs);
2936 }
2937 break;
2938 }
2939 case Intrinsic::get_active_lane_mask: {
2940 auto *Op0 = dyn_cast<ConstantInt>(Operands[0]);
2941 auto *Op1 = dyn_cast<ConstantInt>(Operands[1]);
2942 if (Op0 && Op1) {
2943 unsigned Lanes = FVTy->getNumElements();
2944 uint64_t Base = Op0->getZExtValue();
2945 uint64_t Limit = Op1->getZExtValue();
2946
2947 SmallVector<Constant *, 16> NCs;
2948 for (unsigned i = 0; i < Lanes; i++) {
2949 if (Base + i < Limit)
2950 NCs.push_back(ConstantInt::getTrue(Ty));
2951 else
2952 NCs.push_back(ConstantInt::getFalse(Ty));
2953 }
2954 return ConstantVector::get(NCs);
2955 }
2956 break;
2957 }
2958 default:
2959 break;
2960 }
2961
2962 for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) {
2963 // Gather a column of constants.
2964 for (unsigned J = 0, JE = Operands.size(); J != JE; ++J) {
2965 // Some intrinsics use a scalar type for certain arguments.
2966 if (hasVectorInstrinsicScalarOpd(IntrinsicID, J)) {
2967 Lane[J] = Operands[J];
2968 continue;
2969 }
2970
2971 Constant *Agg = Operands[J]->getAggregateElement(I);
2972 if (!Agg)
2973 return nullptr;
2974
2975 Lane[J] = Agg;
2976 }
2977
2978 // Use the regular scalar folding to simplify this column.
2979 Constant *Folded =
2980 ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI, Call);
2981 if (!Folded)
2982 return nullptr;
2983 Result[I] = Folded;
2984 }
2985
2986 return ConstantVector::get(Result);
2987}
2988
2989static Constant *ConstantFoldScalableVectorCall(
2990 StringRef Name, Intrinsic::ID IntrinsicID, ScalableVectorType *SVTy,
2991 ArrayRef<Constant *> Operands, const DataLayout &DL,
2992 const TargetLibraryInfo *TLI, const CallBase *Call) {
2993 switch (IntrinsicID) {
2994 case Intrinsic::aarch64_sve_convert_from_svbool: {
2995 auto *Src = dyn_cast<Constant>(Operands[0]);
2996 if (!Src || !Src->isNullValue())
2997 break;
2998
2999 return ConstantInt::getFalse(SVTy);
3000 }
3001 default:
3002 break;
3003 }
3004 return nullptr;
3005}
3006
3007} // end anonymous namespace
3008
3009Constant *llvm::ConstantFoldCall(const CallBase *Call, Function *F,
3010 ArrayRef<Constant *> Operands,
3011 const TargetLibraryInfo *TLI) {
3012 if (Call->isNoBuiltin())
3013 return nullptr;
3014 if (!F->hasName())
3015 return nullptr;
3016 StringRef Name = F->getName();
3017
3018 Type *Ty = F->getReturnType();
3019
3020 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty))
3021 return ConstantFoldFixedVectorCall(
3022 Name, F->getIntrinsicID(), FVTy, Operands,
3023 F->getParent()->getDataLayout(), TLI, Call);
3024
3025 if (auto *SVTy = dyn_cast<ScalableVectorType>(Ty))
3026 return ConstantFoldScalableVectorCall(
3027 Name, F->getIntrinsicID(), SVTy, Operands,
3028 F->getParent()->getDataLayout(), TLI, Call);
3029
3030 return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI,
3031 Call);
3032}
3033
3034bool llvm::isMathLibCallNoop(const CallBase *Call,
3035 const TargetLibraryInfo *TLI) {
3036 // FIXME: Refactor this code; this duplicates logic in LibCallsShrinkWrap
3037 // (and to some extent ConstantFoldScalarCall).
3038 if (Call->isNoBuiltin() || Call->isStrictFP())
3039 return false;
3040 Function *F = Call->getCalledFunction();
3041 if (!F)
3042 return false;
3043
3044 LibFunc Func;
3045 if (!TLI || !TLI->getLibFunc(*F, Func))
3046 return false;
3047
3048 if (Call->getNumArgOperands() == 1) {
3049 if (ConstantFP *OpC = dyn_cast<ConstantFP>(Call->getArgOperand(0))) {
3050 const APFloat &Op = OpC->getValueAPF();
3051 switch (Func) {
3052 case LibFunc_logl:
3053 case LibFunc_log:
3054 case LibFunc_logf:
3055 case LibFunc_log2l:
3056 case LibFunc_log2:
3057 case LibFunc_log2f:
3058 case LibFunc_log10l:
3059 case LibFunc_log10:
3060 case LibFunc_log10f:
3061 return Op.isNaN() || (!Op.isZero() && !Op.isNegative());
3062
3063 case LibFunc_expl:
3064 case LibFunc_exp:
3065 case LibFunc_expf:
3066 // FIXME: These boundaries are slightly conservative.
3067 if (OpC->getType()->isDoubleTy())
3068 return !(Op < APFloat(-745.0) || Op > APFloat(709.0));
3069 if (OpC->getType()->isFloatTy())
3070 return !(Op < APFloat(-103.0f) || Op > APFloat(88.0f));
3071 break;
3072
3073 case LibFunc_exp2l:
3074 case LibFunc_exp2:
3075 case LibFunc_exp2f:
3076 // FIXME: These boundaries are slightly conservative.
3077 if (OpC->getType()->isDoubleTy())
3078 return !(Op < APFloat(-1074.0) || Op > APFloat(1023.0));
3079 if (OpC->getType()->isFloatTy())
3080 return !(Op < APFloat(-149.0f) || Op > APFloat(127.0f));
3081 break;
3082
3083 case LibFunc_sinl:
3084 case LibFunc_sin:
3085 case LibFunc_sinf:
3086 case LibFunc_cosl:
3087 case LibFunc_cos:
3088 case LibFunc_cosf:
3089 return !Op.isInfinity();
3090
3091 case LibFunc_tanl:
3092 case LibFunc_tan:
3093 case LibFunc_tanf: {
3094 // FIXME: Stop using the host math library.
3095 // FIXME: The computation isn't done in the right precision.
3096 Type *Ty = OpC->getType();
3097 if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy()) {
3098 double OpV = getValueAsDouble(OpC);
3099 return ConstantFoldFP(tan, OpV, Ty) != nullptr;
3100 }
3101 break;
3102 }
3103
3104 case LibFunc_asinl:
3105 case LibFunc_asin:
3106 case LibFunc_asinf:
3107 case LibFunc_acosl:
3108 case LibFunc_acos:
3109 case LibFunc_acosf:
3110 return !(Op < APFloat(Op.getSemantics(), "-1") ||
3111 Op > APFloat(Op.getSemantics(), "1"));
3112
3113 case LibFunc_sinh:
3114 case LibFunc_cosh:
3115 case LibFunc_sinhf:
3116 case LibFunc_coshf:
3117 case LibFunc_sinhl:
3118 case LibFunc_coshl:
3119 // FIXME: These boundaries are slightly conservative.
3120 if (OpC->getType()->isDoubleTy())
3121 return !(Op < APFloat(-710.0) || Op > APFloat(710.0));
3122 if (OpC->getType()->isFloatTy())
3123 return !(Op < APFloat(-89.0f) || Op > APFloat(89.0f));
3124 break;
3125
3126 case LibFunc_sqrtl:
3127 case LibFunc_sqrt:
3128 case LibFunc_sqrtf:
3129 return Op.isNaN() || Op.isZero() || !Op.isNegative();
3130
3131 // FIXME: Add more functions: sqrt_finite, atanh, expm1, log1p,
3132 // maybe others?
3133 default:
3134 break;
3135 }
3136 }
3137 }
3138
3139 if (Call->getNumArgOperands() == 2) {
3140 ConstantFP *Op0C = dyn_cast<ConstantFP>(Call->getArgOperand(0));
3141 ConstantFP *Op1C = dyn_cast<ConstantFP>(Call->getArgOperand(1));
3142 if (Op0C && Op1C) {
3143 const APFloat &Op0 = Op0C->getValueAPF();
3144 const APFloat &Op1 = Op1C->getValueAPF();
3145
3146 switch (Func) {
3147 case LibFunc_powl:
3148 case LibFunc_pow:
3149 case LibFunc_powf: {
3150 // FIXME: Stop using the host math library.
3151 // FIXME: The computation isn't done in the right precision.
3152 Type *Ty = Op0C->getType();
3153 if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy()) {
3154 if (Ty == Op1C->getType()) {
3155 double Op0V = getValueAsDouble(Op0C);
3156 double Op1V = getValueAsDouble(Op1C);
3157 return ConstantFoldBinaryFP(pow, Op0V, Op1V, Ty) != nullptr;
3158 }
3159 }
3160 break;
3161 }
3162
3163 case LibFunc_fmodl:
3164 case LibFunc_fmod:
3165 case LibFunc_fmodf:
3166 case LibFunc_remainderl:
3167 case LibFunc_remainder:
3168 case LibFunc_remainderf:
3169 return Op0.isNaN() || Op1.isNaN() ||
3170 (!Op0.isInfinity() && !Op1.isZero());
3171
3172 default:
3173 break;
3174 }
3175 }
3176 }
3177
3178 return false;
3179}
3180
3181void TargetFolder::anchor() {}

/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/IR/Type.h

1//===- llvm/Type.h - Classes for handling data types ------------*- C++ -*-===//
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 file contains the declaration of the Type class. For more "Type"
10// stuff, look in DerivedTypes.h.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_TYPE_H
15#define LLVM_IR_TYPE_H
16
17#include "llvm/ADT/APFloat.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/Support/CBindingWrapping.h"
21#include "llvm/Support/Casting.h"
22#include "llvm/Support/Compiler.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/TypeSize.h"
25#include <cassert>
26#include <cstdint>
27#include <iterator>
28
29namespace llvm {
30
31template<class GraphType> struct GraphTraits;
32class IntegerType;
33class LLVMContext;
34class PointerType;
35class raw_ostream;
36class StringRef;
37
38/// The instances of the Type class are immutable: once they are created,
39/// they are never changed. Also note that only one instance of a particular
40/// type is ever created. Thus seeing if two types are equal is a matter of
41/// doing a trivial pointer comparison. To enforce that no two equal instances
42/// are created, Type instances can only be created via static factory methods
43/// in class Type and in derived classes. Once allocated, Types are never
44/// free'd.
45///
46class Type {
47public:
48 //===--------------------------------------------------------------------===//
49 /// Definitions of all of the base types for the Type system. Based on this
50 /// value, you can cast to a class defined in DerivedTypes.h.
51 /// Note: If you add an element to this, you need to add an element to the
52 /// Type::getPrimitiveType function, or else things will break!
53 /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding.
54 ///
55 enum TypeID {
56 // PrimitiveTypes
57 HalfTyID = 0, ///< 16-bit floating point type
58 BFloatTyID, ///< 16-bit floating point type (7-bit significand)
59 FloatTyID, ///< 32-bit floating point type
60 DoubleTyID, ///< 64-bit floating point type
61 X86_FP80TyID, ///< 80-bit floating point type (X87)
62 FP128TyID, ///< 128-bit floating point type (112-bit significand)
63 PPC_FP128TyID, ///< 128-bit floating point type (two 64-bits, PowerPC)
64 VoidTyID, ///< type with no size
65 LabelTyID, ///< Labels
66 MetadataTyID, ///< Metadata
67 X86_MMXTyID, ///< MMX vectors (64 bits, X86 specific)
68 X86_AMXTyID, ///< AMX vectors (8192 bits, X86 specific)
69 TokenTyID, ///< Tokens
70
71 // Derived types... see DerivedTypes.h file.
72 IntegerTyID, ///< Arbitrary bit width integers
73 FunctionTyID, ///< Functions
74 PointerTyID, ///< Pointers
75 StructTyID, ///< Structures
76 ArrayTyID, ///< Arrays
77 FixedVectorTyID, ///< Fixed width SIMD vector type
78 ScalableVectorTyID ///< Scalable SIMD vector type
79 };
80
81private:
82 /// This refers to the LLVMContext in which this type was uniqued.
83 LLVMContext &Context;
84
85 TypeID ID : 8; // The current base type of this type.
86 unsigned SubclassData : 24; // Space for subclasses to store data.
87 // Note that this should be synchronized with
88 // MAX_INT_BITS value in IntegerType class.
89
90protected:
91 friend class LLVMContextImpl;
92
93 explicit Type(LLVMContext &C, TypeID tid)
94 : Context(C), ID(tid), SubclassData(0) {}
95 ~Type() = default;
96
97 unsigned getSubclassData() const { return SubclassData; }
98
99 void setSubclassData(unsigned val) {
100 SubclassData = val;
101 // Ensure we don't have any accidental truncation.
102 assert(getSubclassData() == val && "Subclass data too large for field")(static_cast <bool> (getSubclassData() == val &&
"Subclass data too large for field") ? void (0) : __assert_fail
("getSubclassData() == val && \"Subclass data too large for field\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/IR/Type.h"
, 102, __extension__ __PRETTY_FUNCTION__))
;
103 }
104
105 /// Keeps track of how many Type*'s there are in the ContainedTys list.
106 unsigned NumContainedTys = 0;
107
108 /// A pointer to the array of Types contained by this Type. For example, this
109 /// includes the arguments of a function type, the elements of a structure,
110 /// the pointee of a pointer, the element type of an array, etc. This pointer
111 /// may be 0 for types that don't contain other types (Integer, Double,
112 /// Float).
113 Type * const *ContainedTys = nullptr;
114
115public:
116 /// Print the current type.
117 /// Omit the type details if \p NoDetails == true.
118 /// E.g., let %st = type { i32, i16 }
119 /// When \p NoDetails is true, we only print %st.
120 /// Put differently, \p NoDetails prints the type as if
121 /// inlined with the operands when printing an instruction.
122 void print(raw_ostream &O, bool IsForDebug = false,
123 bool NoDetails = false) const;
124
125 void dump() const;
126
127 /// Return the LLVMContext in which this type was uniqued.
128 LLVMContext &getContext() const { return Context; }
129
130 //===--------------------------------------------------------------------===//
131 // Accessors for working with types.
132 //
133
134 /// Return the type id for the type. This will return one of the TypeID enum
135 /// elements defined above.
136 TypeID getTypeID() const { return ID; }
137
138 /// Return true if this is 'void'.
139 bool isVoidTy() const { return getTypeID() == VoidTyID; }
140
141 /// Return true if this is 'half', a 16-bit IEEE fp type.
142 bool isHalfTy() const { return getTypeID() == HalfTyID; }
143
144 /// Return true if this is 'bfloat', a 16-bit bfloat type.
145 bool isBFloatTy() const { return getTypeID() == BFloatTyID; }
146
147 /// Return true if this is 'float', a 32-bit IEEE fp type.
148 bool isFloatTy() const { return getTypeID() == FloatTyID; }
149
150 /// Return true if this is 'double', a 64-bit IEEE fp type.
151 bool isDoubleTy() const { return getTypeID() == DoubleTyID; }
152
153 /// Return true if this is x86 long double.
154 bool isX86_FP80Ty() const { return getTypeID() == X86_FP80TyID; }
155
156 /// Return true if this is 'fp128'.
157 bool isFP128Ty() const { return getTypeID() == FP128TyID; }
158
159 /// Return true if this is powerpc long double.
160 bool isPPC_FP128Ty() const { return getTypeID() == PPC_FP128TyID; }
161
162 /// Return true if this is one of the six floating-point types
163 bool isFloatingPointTy() const {
164 return getTypeID() == HalfTyID || getTypeID() == BFloatTyID ||
4
Assuming the condition is false
5
Assuming the condition is false
11
Returning zero, which participates in a condition later
165 getTypeID() == FloatTyID || getTypeID() == DoubleTyID ||
6
Assuming the condition is false
7
Assuming the condition is false
166 getTypeID() == X86_FP80TyID || getTypeID() == FP128TyID ||
8
Assuming the condition is false
9
Assuming the condition is false
167 getTypeID() == PPC_FP128TyID;
10
Assuming the condition is false
168 }
169
170 const fltSemantics &getFltSemantics() const {
171 switch (getTypeID()) {
172 case HalfTyID: return APFloat::IEEEhalf();
173 case BFloatTyID: return APFloat::BFloat();
174 case FloatTyID: return APFloat::IEEEsingle();
175 case DoubleTyID: return APFloat::IEEEdouble();
176 case X86_FP80TyID: return APFloat::x87DoubleExtended();
177 case FP128TyID: return APFloat::IEEEquad();
178 case PPC_FP128TyID: return APFloat::PPCDoubleDouble();
179 default: llvm_unreachable("Invalid floating type")::llvm::llvm_unreachable_internal("Invalid floating type", "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/IR/Type.h"
, 179)
;
180 }
181 }
182
183 /// Return true if this is X86 MMX.
184 bool isX86_MMXTy() const { return getTypeID() == X86_MMXTyID; }
185
186 /// Return true if this is X86 AMX.
187 bool isX86_AMXTy() const { return getTypeID() == X86_AMXTyID; }
188
189 /// Return true if this is a FP type or a vector of FP.
190 bool isFPOrFPVectorTy() const { return getScalarType()->isFloatingPointTy(); }
191
192 /// Return true if this is 'label'.
193 bool isLabelTy() const { return getTypeID() == LabelTyID; }
194
195 /// Return true if this is 'metadata'.
196 bool isMetadataTy() const { return getTypeID() == MetadataTyID; }
197
198 /// Return true if this is 'token'.
199 bool isTokenTy() const { return getTypeID() == TokenTyID; }
200
201 /// True if this is an instance of IntegerType.
202 bool isIntegerTy() const { return getTypeID() == IntegerTyID; }
17
Assuming the condition is true
18
Returning the value 1, which participates in a condition later
21
Assuming the condition is true
22
Returning the value 1, which participates in a condition later
203
204 /// Return true if this is an IntegerType of the given width.
205 bool isIntegerTy(unsigned Bitwidth) const;
206
207 /// Return true if this is an integer type or a vector of integer types.
208 bool isIntOrIntVectorTy() const { return getScalarType()->isIntegerTy(); }
209
210 /// Return true if this is an integer type or a vector of integer types of
211 /// the given width.
212 bool isIntOrIntVectorTy(unsigned BitWidth) const {
213 return getScalarType()->isIntegerTy(BitWidth);
214 }
215
216 /// Return true if this is an integer type or a pointer type.
217 bool isIntOrPtrTy() const { return isIntegerTy() || isPointerTy(); }
218
219 /// True if this is an instance of FunctionType.
220 bool isFunctionTy() const { return getTypeID() == FunctionTyID; }
221
222 /// True if this is an instance of StructType.
223 bool isStructTy() const { return getTypeID() == StructTyID; }
224
225 /// True if this is an instance of ArrayType.
226 bool isArrayTy() const { return getTypeID() == ArrayTyID; }
227
228 /// True if this is an instance of PointerType.
229 bool isPointerTy() const { return getTypeID() == PointerTyID; }
230
231 /// Return true if this is a pointer type or a vector of pointer types.
232 bool isPtrOrPtrVectorTy() const { return getScalarType()->isPointerTy(); }
233
234 /// True if this is an instance of VectorType.
235 inline bool isVectorTy() const {
236 return getTypeID() == ScalableVectorTyID || getTypeID() == FixedVectorTyID;
237 }
238
239 /// Return true if this type could be converted with a lossless BitCast to
240 /// type 'Ty'. For example, i8* to i32*. BitCasts are valid for types of the
241 /// same size only where no re-interpretation of the bits is done.
242 /// Determine if this type could be losslessly bitcast to Ty
243 bool canLosslesslyBitCastTo(Type *Ty) const;
244
245 /// Return true if this type is empty, that is, it has no elements or all of
246 /// its elements are empty.
247 bool isEmptyTy() const;
248
249 /// Return true if the type is "first class", meaning it is a valid type for a
250 /// Value.
251 bool isFirstClassType() const {
252 return getTypeID() != FunctionTyID && getTypeID() != VoidTyID;
253 }
254
255 /// Return true if the type is a valid type for a register in codegen. This
256 /// includes all first-class types except struct and array types.
257 bool isSingleValueType() const {
258 return isFloatingPointTy() || isX86_MMXTy() || isIntegerTy() ||
259 isPointerTy() || isVectorTy() || isX86_AMXTy();
260 }
261
262 /// Return true if the type is an aggregate type. This means it is valid as
263 /// the first operand of an insertvalue or extractvalue instruction. This
264 /// includes struct and array types, but does not include vector types.
265 bool isAggregateType() const {
266 return getTypeID() == StructTyID || getTypeID() == ArrayTyID;
267 }
268
269 /// Return true if it makes sense to take the size of this type. To get the
270 /// actual size for a particular target, it is reasonable to use the
271 /// DataLayout subsystem to do this.
272 bool isSized(SmallPtrSetImpl<Type*> *Visited = nullptr) const {
273 // If it's a primitive, it is always sized.
274 if (getTypeID() == IntegerTyID || isFloatingPointTy() ||
275 getTypeID() == PointerTyID || getTypeID() == X86_MMXTyID ||
276 getTypeID() == X86_AMXTyID)
277 return true;
278 // If it is not something that can have a size (e.g. a function or label),
279 // it doesn't have a size.
280 if (getTypeID() != StructTyID && getTypeID() != ArrayTyID && !isVectorTy())
281 return false;
282 // Otherwise we have to try harder to decide.
283 return isSizedDerivedType(Visited);
284 }
285
286 /// Return the basic size of this type if it is a primitive type. These are
287 /// fixed by LLVM and are not target-dependent.
288 /// This will return zero if the type does not have a size or is not a
289 /// primitive type.
290 ///
291 /// If this is a scalable vector type, the scalable property will be set and
292 /// the runtime size will be a positive integer multiple of the base size.
293 ///
294 /// Note that this may not reflect the size of memory allocated for an
295 /// instance of the type or the number of bytes that are written when an
296 /// instance of the type is stored to memory. The DataLayout class provides
297 /// additional query functions to provide this information.
298 ///
299 TypeSize getPrimitiveSizeInBits() const LLVM_READONLY__attribute__((__pure__));
300
301 /// If this is a vector type, return the getPrimitiveSizeInBits value for the
302 /// element type. Otherwise return the getPrimitiveSizeInBits value for this
303 /// type.
304 unsigned getScalarSizeInBits() const LLVM_READONLY__attribute__((__pure__));
305
306 /// Return the width of the mantissa of this type. This is only valid on
307 /// floating-point types. If the FP type does not have a stable mantissa (e.g.
308 /// ppc long double), this method returns -1.
309 int getFPMantissaWidth() const;
310
311 /// Return whether the type is IEEE compatible, as defined by the eponymous
312 /// method in APFloat.
313 bool isIEEE() const { return APFloat::getZero(getFltSemantics()).isIEEE(); }
314
315 /// If this is a vector type, return the element type, otherwise return
316 /// 'this'.
317 inline Type *getScalarType() const {
318 if (isVectorTy())
319 return getContainedType(0);
320 return const_cast<Type *>(this);
321 }
322
323 //===--------------------------------------------------------------------===//
324 // Type Iteration support.
325 //
326 using subtype_iterator = Type * const *;
327
328 subtype_iterator subtype_begin() const { return ContainedTys; }
329 subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];}
330 ArrayRef<Type*> subtypes() const {
331 return makeArrayRef(subtype_begin(), subtype_end());
332 }
333
334 using subtype_reverse_iterator = std::reverse_iterator<subtype_iterator>;
335
336 subtype_reverse_iterator subtype_rbegin() const {
337 return subtype_reverse_iterator(subtype_end());
338 }
339 subtype_reverse_iterator subtype_rend() const {
340 return subtype_reverse_iterator(subtype_begin());
341 }
342
343 /// This method is used to implement the type iterator (defined at the end of
344 /// the file). For derived types, this returns the types 'contained' in the
345 /// derived type.
346 Type *getContainedType(unsigned i) const {
347 assert(i < NumContainedTys && "Index out of range!")(static_cast <bool> (i < NumContainedTys && "Index out of range!"
) ? void (0) : __assert_fail ("i < NumContainedTys && \"Index out of range!\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/IR/Type.h"
, 347, __extension__ __PRETTY_FUNCTION__))
;
348 return ContainedTys[i];
349 }
350
351 /// Return the number of types in the derived type.
352 unsigned getNumContainedTypes() const { return NumContainedTys; }
353
354 //===--------------------------------------------------------------------===//
355 // Helper methods corresponding to subclass methods. This forces a cast to
356 // the specified subclass and calls its accessor. "getArrayNumElements" (for
357 // example) is shorthand for cast<ArrayType>(Ty)->getNumElements(). This is
358 // only intended to cover the core methods that are frequently used, helper
359 // methods should not be added here.
360
361 inline unsigned getIntegerBitWidth() const;
362
363 inline Type *getFunctionParamType(unsigned i) const;
364 inline unsigned getFunctionNumParams() const;
365 inline bool isFunctionVarArg() const;
366
367 inline StringRef getStructName() const;
368 inline unsigned getStructNumElements() const;
369 inline Type *getStructElementType(unsigned N) const;
370
371 inline uint64_t getArrayNumElements() const;
372
373 Type *getArrayElementType() const {
374 assert(getTypeID() == ArrayTyID)(static_cast <bool> (getTypeID() == ArrayTyID) ? void (
0) : __assert_fail ("getTypeID() == ArrayTyID", "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/IR/Type.h"
, 374, __extension__ __PRETTY_FUNCTION__))
;
375 return ContainedTys[0];
376 }
377
378 Type *getPointerElementType() const {
379 assert(getTypeID() == PointerTyID)(static_cast <bool> (getTypeID() == PointerTyID) ? void
(0) : __assert_fail ("getTypeID() == PointerTyID", "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/IR/Type.h"
, 379, __extension__ __PRETTY_FUNCTION__))
;
380 return ContainedTys[0];
381 }
382
383 /// Given vector type, change the element type,
384 /// whilst keeping the old number of elements.
385 /// For non-vectors simply returns \p EltTy.
386 inline Type *getWithNewType(Type *EltTy) const;
387
388 /// Given an integer or vector type, change the lane bitwidth to NewBitwidth,
389 /// whilst keeping the old number of lanes.
390 inline Type *getWithNewBitWidth(unsigned NewBitWidth) const;
391
392 /// Given scalar/vector integer type, returns a type with elements twice as
393 /// wide as in the original type. For vectors, preserves element count.
394 inline Type *getExtendedType() const;
395
396 /// Get the address space of this pointer or pointer vector type.
397 inline unsigned getPointerAddressSpace() const;
398
399 //===--------------------------------------------------------------------===//
400 // Static members exported by the Type class itself. Useful for getting
401 // instances of Type.
402 //
403
404 /// Return a type based on an identifier.
405 static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber);
406
407 //===--------------------------------------------------------------------===//
408 // These are the builtin types that are always available.
409 //
410 static Type *getVoidTy(LLVMContext &C);
411 static Type *getLabelTy(LLVMContext &C);
412 static Type *getHalfTy(LLVMContext &C);
413 static Type *getBFloatTy(LLVMContext &C);
414 static Type *getFloatTy(LLVMContext &C);
415 static Type *getDoubleTy(LLVMContext &C);
416 static Type *getMetadataTy(LLVMContext &C);
417 static Type *getX86_FP80Ty(LLVMContext &C);
418 static Type *getFP128Ty(LLVMContext &C);
419 static Type *getPPC_FP128Ty(LLVMContext &C);
420 static Type *getX86_MMXTy(LLVMContext &C);
421 static Type *getX86_AMXTy(LLVMContext &C);
422 static Type *getTokenTy(LLVMContext &C);
423 static IntegerType *getIntNTy(LLVMContext &C, unsigned N);
424 static IntegerType *getInt1Ty(LLVMContext &C);
425 static IntegerType *getInt8Ty(LLVMContext &C);
426 static IntegerType *getInt16Ty(LLVMContext &C);
427 static IntegerType *getInt32Ty(LLVMContext &C);
428 static IntegerType *getInt64Ty(LLVMContext &C);
429 static IntegerType *getInt128Ty(LLVMContext &C);
430 template <typename ScalarTy> static Type *getScalarTy(LLVMContext &C) {
431 int noOfBits = sizeof(ScalarTy) * CHAR_BIT8;
432 if (std::is_integral<ScalarTy>::value) {
433 return (Type*) Type::getIntNTy(C, noOfBits);
434 } else if (std::is_floating_point<ScalarTy>::value) {
435 switch (noOfBits) {
436 case 32:
437 return Type::getFloatTy(C);
438 case 64:
439 return Type::getDoubleTy(C);
440 }
441 }
442 llvm_unreachable("Unsupported type in Type::getScalarTy")::llvm::llvm_unreachable_internal("Unsupported type in Type::getScalarTy"
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/IR/Type.h"
, 442)
;
443 }
444 static Type *getFloatingPointTy(LLVMContext &C, const fltSemantics &S) {
445 Type *Ty;
446 if (&S == &APFloat::IEEEhalf())
447 Ty = Type::getHalfTy(C);
448 else if (&S == &APFloat::BFloat())
449 Ty = Type::getBFloatTy(C);
450 else if (&S == &APFloat::IEEEsingle())
451 Ty = Type::getFloatTy(C);
452 else if (&S == &APFloat::IEEEdouble())
453 Ty = Type::getDoubleTy(C);
454 else if (&S == &APFloat::x87DoubleExtended())
455 Ty = Type::getX86_FP80Ty(C);
456 else if (&S == &APFloat::IEEEquad())
457 Ty = Type::getFP128Ty(C);
458 else {
459 assert(&S == &APFloat::PPCDoubleDouble() && "Unknown FP format")(static_cast <bool> (&S == &APFloat::PPCDoubleDouble
() && "Unknown FP format") ? void (0) : __assert_fail
("&S == &APFloat::PPCDoubleDouble() && \"Unknown FP format\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/IR/Type.h"
, 459, __extension__ __PRETTY_FUNCTION__))
;
460 Ty = Type::getPPC_FP128Ty(C);
461 }
462 return Ty;
463 }
464
465 //===--------------------------------------------------------------------===//
466 // Convenience methods for getting pointer types with one of the above builtin
467 // types as pointee.
468 //
469 static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0);
470 static PointerType *getBFloatPtrTy(LLVMContext &C, unsigned AS = 0);
471 static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0);
472 static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0);
473 static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0);
474 static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0);
475 static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0);
476 static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0);
477 static PointerType *getX86_AMXPtrTy(LLVMContext &C, unsigned AS = 0);
478 static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0);
479 static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0);
480 static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0);
481 static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0);
482 static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0);
483 static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0);
484
485 /// Return a pointer to the current type. This is equivalent to
486 /// PointerType::get(Foo, AddrSpace).
487 PointerType *getPointerTo(unsigned AddrSpace = 0) const;
488
489private:
490 /// Derived types like structures and arrays are sized iff all of the members
491 /// of the type are sized as well. Since asking for their size is relatively
492 /// uncommon, move this operation out-of-line.
493 bool isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited = nullptr) const;
494};
495
496// Printing of types.
497inline raw_ostream &operator<<(raw_ostream &OS, const Type &T) {
498 T.print(OS);
499 return OS;
500}
501
502// allow isa<PointerType>(x) to work without DerivedTypes.h included.
503template <> struct isa_impl<PointerType, Type> {
504 static inline bool doit(const Type &Ty) {
505 return Ty.getTypeID() == Type::PointerTyID;
506 }
507};
508
509// Create wrappers for C Binding types (see CBindingWrapping.h).
510DEFINE_ISA_CONVERSION_FUNCTIONS(Type, LLVMTypeRef)inline Type *unwrap(LLVMTypeRef P) { return reinterpret_cast<
Type*>(P); } inline LLVMTypeRef wrap(const Type *P) { return
reinterpret_cast<LLVMTypeRef>(const_cast<Type*>(
P)); } template<typename T> inline T *unwrap(LLVMTypeRef
P) { return cast<T>(unwrap(P)); }
511
512/* Specialized opaque type conversions.
513 */
514inline Type **unwrap(LLVMTypeRef* Tys) {
515 return reinterpret_cast<Type**>(Tys);
516}
517
518inline LLVMTypeRef *wrap(Type **Tys) {
519 return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
520}
521
522} // end namespace llvm
523
524#endif // LLVM_IR_TYPE_H

/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h

1//===-- llvm/ADT/APInt.h - For Arbitrary Precision Integer -----*- C++ -*--===//
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/// \file
10/// This file implements a class to represent arbitrary precision
11/// integral constant values and operations on them.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ADT_APINT_H
16#define LLVM_ADT_APINT_H
17
18#include "llvm/Support/Compiler.h"
19#include "llvm/Support/MathExtras.h"
20#include <cassert>
21#include <climits>
22#include <cstring>
23#include <string>
24
25namespace llvm {
26class FoldingSetNodeID;
27class StringRef;
28class hash_code;
29class raw_ostream;
30
31template <typename T> class SmallVectorImpl;
32template <typename T> class ArrayRef;
33template <typename T> class Optional;
34template <typename T> struct DenseMapInfo;
35
36class APInt;
37
38inline APInt operator-(APInt);
39
40//===----------------------------------------------------------------------===//
41// APInt Class
42//===----------------------------------------------------------------------===//
43
44/// Class for arbitrary precision integers.
45///
46/// APInt is a functional replacement for common case unsigned integer type like
47/// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width
48/// integer sizes and large integer value types such as 3-bits, 15-bits, or more
49/// than 64-bits of precision. APInt provides a variety of arithmetic operators
50/// and methods to manipulate integer values of any bit-width. It supports both
51/// the typical integer arithmetic and comparison operations as well as bitwise
52/// manipulation.
53///
54/// The class has several invariants worth noting:
55/// * All bit, byte, and word positions are zero-based.
56/// * Once the bit width is set, it doesn't change except by the Truncate,
57/// SignExtend, or ZeroExtend operations.
58/// * All binary operators must be on APInt instances of the same bit width.
59/// Attempting to use these operators on instances with different bit
60/// widths will yield an assertion.
61/// * The value is stored canonically as an unsigned value. For operations
62/// where it makes a difference, there are both signed and unsigned variants
63/// of the operation. For example, sdiv and udiv. However, because the bit
64/// widths must be the same, operations such as Mul and Add produce the same
65/// results regardless of whether the values are interpreted as signed or
66/// not.
67/// * In general, the class tries to follow the style of computation that LLVM
68/// uses in its IR. This simplifies its use for LLVM.
69///
70class LLVM_NODISCARD[[clang::warn_unused_result]] APInt {
71public:
72 typedef uint64_t WordType;
73
74 /// This enum is used to hold the constants we needed for APInt.
75 enum : unsigned {
76 /// Byte size of a word.
77 APINT_WORD_SIZE = sizeof(WordType),
78 /// Bits in a word.
79 APINT_BITS_PER_WORD = APINT_WORD_SIZE * CHAR_BIT8
80 };
81
82 enum class Rounding {
83 DOWN,
84 TOWARD_ZERO,
85 UP,
86 };
87
88 static constexpr WordType WORDTYPE_MAX = ~WordType(0);
89
90private:
91 /// This union is used to store the integer value. When the
92 /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
93 union {
94 uint64_t VAL; ///< Used to store the <= 64 bits integer value.
95 uint64_t *pVal; ///< Used to store the >64 bits integer value.
96 } U;
97
98 unsigned BitWidth; ///< The number of bits in this APInt.
99
100 friend struct DenseMapInfo<APInt>;
101
102 friend class APSInt;
103
104 /// Fast internal constructor
105 ///
106 /// This constructor is used only internally for speed of construction of
107 /// temporaries. It is unsafe for general use so it is not public.
108 APInt(uint64_t *val, unsigned bits) : BitWidth(bits) {
109 U.pVal = val;
110 }
111
112 /// Determine if this APInt just has one word to store value.
113 ///
114 /// \returns true if the number of bits <= 64, false otherwise.
115 bool isSingleWord() const { return BitWidth
52.1
Field 'BitWidth' is > APINT_BITS_PER_WORD
52.1
Field 'BitWidth' is > APINT_BITS_PER_WORD
52.1
Field 'BitWidth' is > APINT_BITS_PER_WORD
<= APINT_BITS_PER_WORD
; }
43
Assuming field 'BitWidth' is > APINT_BITS_PER_WORD
44
Returning zero, which participates in a condition later
53
Returning zero, which participates in a condition later
116
117 /// Determine which word a bit is in.
118 ///
119 /// \returns the word position for the specified bit position.
120 static unsigned whichWord(unsigned bitPosition) {
121 return bitPosition / APINT_BITS_PER_WORD;
122 }
123
124 /// Determine which bit in a word a bit is in.
125 ///
126 /// \returns the bit position in a word for the specified bit position
127 /// in the APInt.
128 static unsigned whichBit(unsigned bitPosition) {
129 return bitPosition % APINT_BITS_PER_WORD;
130 }
131
132 /// Get a single bit mask.
133 ///
134 /// \returns a uint64_t with only bit at "whichBit(bitPosition)" set
135 /// This method generates and returns a uint64_t (word) mask for a single
136 /// bit at a specific bit position. This is used to mask the bit in the
137 /// corresponding word.
138 static uint64_t maskBit(unsigned bitPosition) {
139 return 1ULL << whichBit(bitPosition);
140 }
141
142 /// Clear unused high order bits
143 ///
144 /// This method is used internally to clear the top "N" bits in the high order
145 /// word that are not used by the APInt. This is needed after the most
146 /// significant word is assigned a value to ensure that those bits are
147 /// zero'd out.
148 APInt &clearUnusedBits() {
149 // Compute how many bits are used in the final word
150 unsigned WordBits = ((BitWidth-1) % APINT_BITS_PER_WORD) + 1;
151
152 // Mask out the high bits.
153 uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - WordBits);
154 if (isSingleWord())
155 U.VAL &= mask;
156 else
157 U.pVal[getNumWords() - 1] &= mask;
158 return *this;
159 }
160
161 /// Get the word corresponding to a bit position
162 /// \returns the corresponding word for the specified bit position.
163 uint64_t getWord(unsigned bitPosition) const {
164 return isSingleWord() ? U.VAL : U.pVal[whichWord(bitPosition)];
165 }
166
167 /// Utility method to change the bit width of this APInt to new bit width,
168 /// allocating and/or deallocating as necessary. There is no guarantee on the
169 /// value of any bits upon return. Caller should populate the bits after.
170 void reallocate(unsigned NewBitWidth);
171
172 /// Convert a char array into an APInt
173 ///
174 /// \param radix 2, 8, 10, 16, or 36
175 /// Converts a string into a number. The string must be non-empty
176 /// and well-formed as a number of the given base. The bit-width
177 /// must be sufficient to hold the result.
178 ///
179 /// This is used by the constructors that take string arguments.
180 ///
181 /// StringRef::getAsInteger is superficially similar but (1) does
182 /// not assume that the string is well-formed and (2) grows the
183 /// result to hold the input.
184 void fromString(unsigned numBits, StringRef str, uint8_t radix);
185
186 /// An internal division function for dividing APInts.
187 ///
188 /// This is used by the toString method to divide by the radix. It simply
189 /// provides a more convenient form of divide for internal use since KnuthDiv
190 /// has specific constraints on its inputs. If those constraints are not met
191 /// then it provides a simpler form of divide.
192 static void divide(const WordType *LHS, unsigned lhsWords,
193 const WordType *RHS, unsigned rhsWords, WordType *Quotient,
194 WordType *Remainder);
195
196 /// out-of-line slow case for inline constructor
197 void initSlowCase(uint64_t val, bool isSigned);
198
199 /// shared code between two array constructors
200 void initFromArray(ArrayRef<uint64_t> array);
201
202 /// out-of-line slow case for inline copy constructor
203 void initSlowCase(const APInt &that);
204
205 /// out-of-line slow case for shl
206 void shlSlowCase(unsigned ShiftAmt);
207
208 /// out-of-line slow case for lshr.
209 void lshrSlowCase(unsigned ShiftAmt);
210
211 /// out-of-line slow case for ashr.
212 void ashrSlowCase(unsigned ShiftAmt);
213
214 /// out-of-line slow case for operator=
215 void AssignSlowCase(const APInt &RHS);
216
217 /// out-of-line slow case for operator==
218 bool EqualSlowCase(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__));
219
220 /// out-of-line slow case for countLeadingZeros
221 unsigned countLeadingZerosSlowCase() const LLVM_READONLY__attribute__((__pure__));
222
223 /// out-of-line slow case for countLeadingOnes.
224 unsigned countLeadingOnesSlowCase() const LLVM_READONLY__attribute__((__pure__));
225
226 /// out-of-line slow case for countTrailingZeros.
227 unsigned countTrailingZerosSlowCase() const LLVM_READONLY__attribute__((__pure__));
228
229 /// out-of-line slow case for countTrailingOnes
230 unsigned countTrailingOnesSlowCase() const LLVM_READONLY__attribute__((__pure__));
231
232 /// out-of-line slow case for countPopulation
233 unsigned countPopulationSlowCase() const LLVM_READONLY__attribute__((__pure__));
234
235 /// out-of-line slow case for intersects.
236 bool intersectsSlowCase(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__));
237
238 /// out-of-line slow case for isSubsetOf.
239 bool isSubsetOfSlowCase(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__));
240
241 /// out-of-line slow case for setBits.
242 void setBitsSlowCase(unsigned loBit, unsigned hiBit);
243
244 /// out-of-line slow case for flipAllBits.
245 void flipAllBitsSlowCase();
246
247 /// out-of-line slow case for operator&=.
248 void AndAssignSlowCase(const APInt& RHS);
249
250 /// out-of-line slow case for operator|=.
251 void OrAssignSlowCase(const APInt& RHS);
252
253 /// out-of-line slow case for operator^=.
254 void XorAssignSlowCase(const APInt& RHS);
255
256 /// Unsigned comparison. Returns -1, 0, or 1 if this APInt is less than, equal
257 /// to, or greater than RHS.
258 int compare(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__));
259
260 /// Signed comparison. Returns -1, 0, or 1 if this APInt is less than, equal
261 /// to, or greater than RHS.
262 int compareSigned(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__));
263
264public:
265 /// \name Constructors
266 /// @{
267
268 /// Create a new APInt of numBits width, initialized as val.
269 ///
270 /// If isSigned is true then val is treated as if it were a signed value
271 /// (i.e. as an int64_t) and the appropriate sign extension to the bit width
272 /// will be done. Otherwise, no sign extension occurs (high order bits beyond
273 /// the range of val are zero filled).
274 ///
275 /// \param numBits the bit width of the constructed APInt
276 /// \param val the initial value of the APInt
277 /// \param isSigned how to treat signedness of val
278 APInt(unsigned numBits, uint64_t val, bool isSigned = false)
279 : BitWidth(numBits) {
280 assert(BitWidth && "bitwidth too small")(static_cast <bool> (BitWidth && "bitwidth too small"
) ? void (0) : __assert_fail ("BitWidth && \"bitwidth too small\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 280, __extension__ __PRETTY_FUNCTION__))
;
281 if (isSingleWord()) {
282 U.VAL = val;
283 clearUnusedBits();
284 } else {
285 initSlowCase(val, isSigned);
286 }
287 }
288
289 /// Construct an APInt of numBits width, initialized as bigVal[].
290 ///
291 /// Note that bigVal.size() can be smaller or larger than the corresponding
292 /// bit width but any extraneous bits will be dropped.
293 ///
294 /// \param numBits the bit width of the constructed APInt
295 /// \param bigVal a sequence of words to form the initial value of the APInt
296 APInt(unsigned numBits, ArrayRef<uint64_t> bigVal);
297
298 /// Equivalent to APInt(numBits, ArrayRef<uint64_t>(bigVal, numWords)), but
299 /// deprecated because this constructor is prone to ambiguity with the
300 /// APInt(unsigned, uint64_t, bool) constructor.
301 ///
302 /// If this overload is ever deleted, care should be taken to prevent calls
303 /// from being incorrectly captured by the APInt(unsigned, uint64_t, bool)
304 /// constructor.
305 APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]);
306
307 /// Construct an APInt from a string representation.
308 ///
309 /// This constructor interprets the string \p str in the given radix. The
310 /// interpretation stops when the first character that is not suitable for the
311 /// radix is encountered, or the end of the string. Acceptable radix values
312 /// are 2, 8, 10, 16, and 36. It is an error for the value implied by the
313 /// string to require more bits than numBits.
314 ///
315 /// \param numBits the bit width of the constructed APInt
316 /// \param str the string to be interpreted
317 /// \param radix the radix to use for the conversion
318 APInt(unsigned numBits, StringRef str, uint8_t radix);
319
320 /// Simply makes *this a copy of that.
321 /// Copy Constructor.
322 APInt(const APInt &that) : BitWidth(that.BitWidth) {
323 if (isSingleWord())
324 U.VAL = that.U.VAL;
325 else
326 initSlowCase(that);
327 }
328
329 /// Move Constructor.
330 APInt(APInt &&that) : BitWidth(that.BitWidth) {
331 memcpy(&U, &that.U, sizeof(U));
332 that.BitWidth = 0;
333 }
334
335 /// Destructor.
336 ~APInt() {
337 if (needsCleanup())
338 delete[] U.pVal;
339 }
340
341 /// Default constructor that creates an uninteresting APInt
342 /// representing a 1-bit zero value.
343 ///
344 /// This is useful for object deserialization (pair this with the static
345 /// method Read).
346 explicit APInt() : BitWidth(1) { U.VAL = 0; }
347
348 /// Returns whether this instance allocated memory.
349 bool needsCleanup() const { return !isSingleWord(); }
350
351 /// Used to insert APInt objects, or objects that contain APInt objects, into
352 /// FoldingSets.
353 void Profile(FoldingSetNodeID &id) const;
354
355 /// @}
356 /// \name Value Tests
357 /// @{
358
359 /// Determine sign of this APInt.
360 ///
361 /// This tests the high bit of this APInt to determine if it is set.
362 ///
363 /// \returns true if this APInt is negative, false otherwise
364 bool isNegative() const { return (*this)[BitWidth - 1]; }
365
366 /// Determine if this APInt Value is non-negative (>= 0)
367 ///
368 /// This tests the high bit of the APInt to determine if it is unset.
369 bool isNonNegative() const { return !isNegative(); }
370
371 /// Determine if sign bit of this APInt is set.
372 ///
373 /// This tests the high bit of this APInt to determine if it is set.
374 ///
375 /// \returns true if this APInt has its sign bit set, false otherwise.
376 bool isSignBitSet() const { return (*this)[BitWidth-1]; }
377
378 /// Determine if sign bit of this APInt is clear.
379 ///
380 /// This tests the high bit of this APInt to determine if it is clear.
381 ///
382 /// \returns true if this APInt has its sign bit clear, false otherwise.
383 bool isSignBitClear() const { return !isSignBitSet(); }
384
385 /// Determine if this APInt Value is positive.
386 ///
387 /// This tests if the value of this APInt is positive (> 0). Note
388 /// that 0 is not a positive value.
389 ///
390 /// \returns true if this APInt is positive.
391 bool isStrictlyPositive() const { return isNonNegative() && !isNullValue(); }
392
393 /// Determine if this APInt Value is non-positive (<= 0).
394 ///
395 /// \returns true if this APInt is non-positive.
396 bool isNonPositive() const { return !isStrictlyPositive(); }
397
398 /// Determine if all bits are set
399 ///
400 /// This checks to see if the value has all bits of the APInt are set or not.
401 bool isAllOnesValue() const {
402 if (isSingleWord())
403 return U.VAL == WORDTYPE_MAX >> (APINT_BITS_PER_WORD - BitWidth);
404 return countTrailingOnesSlowCase() == BitWidth;
405 }
406
407 /// Determine if all bits are clear
408 ///
409 /// This checks to see if the value has all bits of the APInt are clear or
410 /// not.
411 bool isNullValue() const { return !*this; }
51
Calling 'APInt::operator!'
58
Returning from 'APInt::operator!'
59
Returning zero, which participates in a condition later
412
413 /// Determine if this is a value of 1.
414 ///
415 /// This checks to see if the value of this APInt is one.
416 bool isOneValue() const {
417 if (isSingleWord())
42
Calling 'APInt::isSingleWord'
45
Returning from 'APInt::isSingleWord'
46
Taking false branch
418 return U.VAL == 1;
419 return countLeadingZerosSlowCase() == BitWidth - 1;
47
Assuming the condition is false
48
Returning zero, which participates in a condition later
420 }
421
422 /// Determine if this is the largest unsigned value.
423 ///
424 /// This checks to see if the value of this APInt is the maximum unsigned
425 /// value for the APInt's bit width.
426 bool isMaxValue() const { return isAllOnesValue(); }
427
428 /// Determine if this is the largest signed value.
429 ///
430 /// This checks to see if the value of this APInt is the maximum signed
431 /// value for the APInt's bit width.
432 bool isMaxSignedValue() const {
433 if (isSingleWord())
434 return U.VAL == ((WordType(1) << (BitWidth - 1)) - 1);
435 return !isNegative() && countTrailingOnesSlowCase() == BitWidth - 1;
436 }
437
438 /// Determine if this is the smallest unsigned value.
439 ///
440 /// This checks to see if the value of this APInt is the minimum unsigned
441 /// value for the APInt's bit width.
442 bool isMinValue() const { return isNullValue(); }
443
444 /// Determine if this is the smallest signed value.
445 ///
446 /// This checks to see if the value of this APInt is the minimum signed
447 /// value for the APInt's bit width.
448 bool isMinSignedValue() const {
449 if (isSingleWord())
450 return U.VAL == (WordType(1) << (BitWidth - 1));
451 return isNegative() && countTrailingZerosSlowCase() == BitWidth - 1;
452 }
453
454 /// Check if this APInt has an N-bits unsigned integer value.
455 bool isIntN(unsigned N) const {
456 assert(N && "N == 0 ???")(static_cast <bool> (N && "N == 0 ???") ? void (
0) : __assert_fail ("N && \"N == 0 ???\"", "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 456, __extension__ __PRETTY_FUNCTION__))
;
457 return getActiveBits() <= N;
458 }
459
460 /// Check if this APInt has an N-bits signed integer value.
461 bool isSignedIntN(unsigned N) const {
462 assert(N && "N == 0 ???")(static_cast <bool> (N && "N == 0 ???") ? void (
0) : __assert_fail ("N && \"N == 0 ???\"", "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 462, __extension__ __PRETTY_FUNCTION__))
;
463 return getMinSignedBits() <= N;
464 }
465
466 /// Check if this APInt's value is a power of two greater than zero.
467 ///
468 /// \returns true if the argument APInt value is a power of two > 0.
469 bool isPowerOf2() const {
470 if (isSingleWord())
471 return isPowerOf2_64(U.VAL);
472 return countPopulationSlowCase() == 1;
473 }
474
475 /// Check if the APInt's value is returned by getSignMask.
476 ///
477 /// \returns true if this is the value returned by getSignMask.
478 bool isSignMask() const { return isMinSignedValue(); }
479
480 /// Convert APInt to a boolean value.
481 ///
482 /// This converts the APInt to a boolean value as a test against zero.
483 bool getBoolValue() const { return !!*this; }
484
485 /// If this value is smaller than the specified limit, return it, otherwise
486 /// return the limit value. This causes the value to saturate to the limit.
487 uint64_t getLimitedValue(uint64_t Limit = UINT64_MAX(18446744073709551615UL)) const {
488 return ugt(Limit) ? Limit : getZExtValue();
489 }
490
491 /// Check if the APInt consists of a repeated bit pattern.
492 ///
493 /// e.g. 0x01010101 satisfies isSplat(8).
494 /// \param SplatSizeInBits The size of the pattern in bits. Must divide bit
495 /// width without remainder.
496 bool isSplat(unsigned SplatSizeInBits) const;
497
498 /// \returns true if this APInt value is a sequence of \param numBits ones
499 /// starting at the least significant bit with the remainder zero.
500 bool isMask(unsigned numBits) const {
501 assert(numBits != 0 && "numBits must be non-zero")(static_cast <bool> (numBits != 0 && "numBits must be non-zero"
) ? void (0) : __assert_fail ("numBits != 0 && \"numBits must be non-zero\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 501, __extension__ __PRETTY_FUNCTION__))
;
502 assert(numBits <= BitWidth && "numBits out of range")(static_cast <bool> (numBits <= BitWidth && "numBits out of range"
) ? void (0) : __assert_fail ("numBits <= BitWidth && \"numBits out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 502, __extension__ __PRETTY_FUNCTION__))
;
503 if (isSingleWord())
504 return U.VAL == (WORDTYPE_MAX >> (APINT_BITS_PER_WORD - numBits));
505 unsigned Ones = countTrailingOnesSlowCase();
506 return (numBits == Ones) &&
507 ((Ones + countLeadingZerosSlowCase()) == BitWidth);
508 }
509
510 /// \returns true if this APInt is a non-empty sequence of ones starting at
511 /// the least significant bit with the remainder zero.
512 /// Ex. isMask(0x0000FFFFU) == true.
513 bool isMask() const {
514 if (isSingleWord())
515 return isMask_64(U.VAL);
516 unsigned Ones = countTrailingOnesSlowCase();
517 return (Ones > 0) && ((Ones + countLeadingZerosSlowCase()) == BitWidth);
518 }
519
520 /// Return true if this APInt value contains a sequence of ones with
521 /// the remainder zero.
522 bool isShiftedMask() const {
523 if (isSingleWord())
524 return isShiftedMask_64(U.VAL);
525 unsigned Ones = countPopulationSlowCase();
526 unsigned LeadZ = countLeadingZerosSlowCase();
527 return (Ones + LeadZ + countTrailingZeros()) == BitWidth;
528 }
529
530 /// @}
531 /// \name Value Generators
532 /// @{
533
534 /// Gets maximum unsigned value of APInt for specific bit width.
535 static APInt getMaxValue(unsigned numBits) {
536 return getAllOnesValue(numBits);
537 }
538
539 /// Gets maximum signed value of APInt for a specific bit width.
540 static APInt getSignedMaxValue(unsigned numBits) {
541 APInt API = getAllOnesValue(numBits);
542 API.clearBit(numBits - 1);
543 return API;
544 }
545
546 /// Gets minimum unsigned value of APInt for a specific bit width.
547 static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); }
548
549 /// Gets minimum signed value of APInt for a specific bit width.
550 static APInt getSignedMinValue(unsigned numBits) {
551 APInt API(numBits, 0);
552 API.setBit(numBits - 1);
553 return API;
554 }
555
556 /// Get the SignMask for a specific bit width.
557 ///
558 /// This is just a wrapper function of getSignedMinValue(), and it helps code
559 /// readability when we want to get a SignMask.
560 static APInt getSignMask(unsigned BitWidth) {
561 return getSignedMinValue(BitWidth);
562 }
563
564 /// Get the all-ones value.
565 ///
566 /// \returns the all-ones value for an APInt of the specified bit-width.
567 static APInt getAllOnesValue(unsigned numBits) {
568 return APInt(numBits, WORDTYPE_MAX, true);
569 }
570
571 /// Get the '0' value.
572 ///
573 /// \returns the '0' value for an APInt of the specified bit-width.
574 static APInt getNullValue(unsigned numBits) { return APInt(numBits, 0); }
575
576 /// Compute an APInt containing numBits highbits from this APInt.
577 ///
578 /// Get an APInt with the same BitWidth as this APInt, just zero mask
579 /// the low bits and right shift to the least significant bit.
580 ///
581 /// \returns the high "numBits" bits of this APInt.
582 APInt getHiBits(unsigned numBits) const;
583
584 /// Compute an APInt containing numBits lowbits from this APInt.
585 ///
586 /// Get an APInt with the same BitWidth as this APInt, just zero mask
587 /// the high bits.
588 ///
589 /// \returns the low "numBits" bits of this APInt.
590 APInt getLoBits(unsigned numBits) const;
591
592 /// Return an APInt with exactly one bit set in the result.
593 static APInt getOneBitSet(unsigned numBits, unsigned BitNo) {
594 APInt Res(numBits, 0);
595 Res.setBit(BitNo);
596 return Res;
597 }
598
599 /// Get a value with a block of bits set.
600 ///
601 /// Constructs an APInt value that has a contiguous range of bits set. The
602 /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
603 /// bits will be zero. For example, with parameters(32, 0, 16) you would get
604 /// 0x0000FFFF. Please call getBitsSetWithWrap if \p loBit may be greater than
605 /// \p hiBit.
606 ///
607 /// \param numBits the intended bit width of the result
608 /// \param loBit the index of the lowest bit set.
609 /// \param hiBit the index of the highest bit set.
610 ///
611 /// \returns An APInt value with the requested bits set.
612 static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) {
613 assert(loBit <= hiBit && "loBit greater than hiBit")(static_cast <bool> (loBit <= hiBit && "loBit greater than hiBit"
) ? void (0) : __assert_fail ("loBit <= hiBit && \"loBit greater than hiBit\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 613, __extension__ __PRETTY_FUNCTION__))
;
614 APInt Res(numBits, 0);
615 Res.setBits(loBit, hiBit);
616 return Res;
617 }
618
619 /// Wrap version of getBitsSet.
620 /// If \p hiBit is bigger than \p loBit, this is same with getBitsSet.
621 /// If \p hiBit is not bigger than \p loBit, the set bits "wrap". For example,
622 /// with parameters (32, 28, 4), you would get 0xF000000F.
623 /// If \p hiBit is equal to \p loBit, you would get a result with all bits
624 /// set.
625 static APInt getBitsSetWithWrap(unsigned numBits, unsigned loBit,
626 unsigned hiBit) {
627 APInt Res(numBits, 0);
628 Res.setBitsWithWrap(loBit, hiBit);
629 return Res;
630 }
631
632 /// Get a value with upper bits starting at loBit set.
633 ///
634 /// Constructs an APInt value that has a contiguous range of bits set. The
635 /// bits from loBit (inclusive) to numBits (exclusive) will be set. All other
636 /// bits will be zero. For example, with parameters(32, 12) you would get
637 /// 0xFFFFF000.
638 ///
639 /// \param numBits the intended bit width of the result
640 /// \param loBit the index of the lowest bit to set.
641 ///
642 /// \returns An APInt value with the requested bits set.
643 static APInt getBitsSetFrom(unsigned numBits, unsigned loBit) {
644 APInt Res(numBits, 0);
645 Res.setBitsFrom(loBit);
646 return Res;
647 }
648
649 /// Get a value with high bits set
650 ///
651 /// Constructs an APInt value that has the top hiBitsSet bits set.
652 ///
653 /// \param numBits the bitwidth of the result
654 /// \param hiBitsSet the number of high-order bits set in the result.
655 static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) {
656 APInt Res(numBits, 0);
657 Res.setHighBits(hiBitsSet);
658 return Res;
659 }
660
661 /// Get a value with low bits set
662 ///
663 /// Constructs an APInt value that has the bottom loBitsSet bits set.
664 ///
665 /// \param numBits the bitwidth of the result
666 /// \param loBitsSet the number of low-order bits set in the result.
667 static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) {
668 APInt Res(numBits, 0);
669 Res.setLowBits(loBitsSet);
670 return Res;
671 }
672
673 /// Return a value containing V broadcasted over NewLen bits.
674 static APInt getSplat(unsigned NewLen, const APInt &V);
675
676 /// Determine if two APInts have the same value, after zero-extending
677 /// one of them (if needed!) to ensure that the bit-widths match.
678 static bool isSameValue(const APInt &I1, const APInt &I2) {
679 if (I1.getBitWidth() == I2.getBitWidth())
680 return I1 == I2;
681
682 if (I1.getBitWidth() > I2.getBitWidth())
683 return I1 == I2.zext(I1.getBitWidth());
684
685 return I1.zext(I2.getBitWidth()) == I2;
686 }
687
688 /// Overload to compute a hash_code for an APInt value.
689 friend hash_code hash_value(const APInt &Arg);
690
691 /// This function returns a pointer to the internal storage of the APInt.
692 /// This is useful for writing out the APInt in binary form without any
693 /// conversions.
694 const uint64_t *getRawData() const {
695 if (isSingleWord())
696 return &U.VAL;
697 return &U.pVal[0];
698 }
699
700 /// @}
701 /// \name Unary Operators
702 /// @{
703
704 /// Postfix increment operator.
705 ///
706 /// Increments *this by 1.
707 ///
708 /// \returns a new APInt value representing the original value of *this.
709 const APInt operator++(int) {
710 APInt API(*this);
711 ++(*this);
712 return API;
713 }
714
715 /// Prefix increment operator.
716 ///
717 /// \returns *this incremented by one
718 APInt &operator++();
719
720 /// Postfix decrement operator.
721 ///
722 /// Decrements *this by 1.
723 ///
724 /// \returns a new APInt value representing the original value of *this.
725 const APInt operator--(int) {
726 APInt API(*this);
727 --(*this);
728 return API;
729 }
730
731 /// Prefix decrement operator.
732 ///
733 /// \returns *this decremented by one.
734 APInt &operator--();
735
736 /// Logical negation operator.
737 ///
738 /// Performs logical negation operation on this APInt.
739 ///
740 /// \returns true if *this is zero, false otherwise.
741 bool operator!() const {
742 if (isSingleWord())
52
Calling 'APInt::isSingleWord'
54
Returning from 'APInt::isSingleWord'
55
Taking false branch
743 return U.VAL == 0;
744 return countLeadingZerosSlowCase() == BitWidth;
56
Assuming the condition is false
57
Returning zero, which participates in a condition later
745 }
746
747 /// @}
748 /// \name Assignment Operators
749 /// @{
750
751 /// Copy assignment operator.
752 ///
753 /// \returns *this after assignment of RHS.
754 APInt &operator=(const APInt &RHS) {
755 // If the bitwidths are the same, we can avoid mucking with memory
756 if (isSingleWord() && RHS.isSingleWord()) {
757 U.VAL = RHS.U.VAL;
758 BitWidth = RHS.BitWidth;
759 return clearUnusedBits();
760 }
761
762 AssignSlowCase(RHS);
763 return *this;
764 }
765
766 /// Move assignment operator.
767 APInt &operator=(APInt &&that) {
768#ifdef EXPENSIVE_CHECKS
769 // Some std::shuffle implementations still do self-assignment.
770 if (this == &that)
771 return *this;
772#endif
773 assert(this != &that && "Self-move not supported")(static_cast <bool> (this != &that && "Self-move not supported"
) ? void (0) : __assert_fail ("this != &that && \"Self-move not supported\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 773, __extension__ __PRETTY_FUNCTION__))
;
774 if (!isSingleWord())
775 delete[] U.pVal;
776
777 // Use memcpy so that type based alias analysis sees both VAL and pVal
778 // as modified.
779 memcpy(&U, &that.U, sizeof(U));
780
781 BitWidth = that.BitWidth;
782 that.BitWidth = 0;
783
784 return *this;
785 }
786
787 /// Assignment operator.
788 ///
789 /// The RHS value is assigned to *this. If the significant bits in RHS exceed
790 /// the bit width, the excess bits are truncated. If the bit width is larger
791 /// than 64, the value is zero filled in the unspecified high order bits.
792 ///
793 /// \returns *this after assignment of RHS value.
794 APInt &operator=(uint64_t RHS) {
795 if (isSingleWord()) {
796 U.VAL = RHS;
797 return clearUnusedBits();
798 }
799 U.pVal[0] = RHS;
800 memset(U.pVal + 1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
801 return *this;
802 }
803
804 /// Bitwise AND assignment operator.
805 ///
806 /// Performs a bitwise AND operation on this APInt and RHS. The result is
807 /// assigned to *this.
808 ///
809 /// \returns *this after ANDing with RHS.
810 APInt &operator&=(const APInt &RHS) {
811 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast <bool> (BitWidth == RHS.BitWidth &&
"Bit widths must be the same") ? void (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 811, __extension__ __PRETTY_FUNCTION__))
;
812 if (isSingleWord())
813 U.VAL &= RHS.U.VAL;
814 else
815 AndAssignSlowCase(RHS);
816 return *this;
817 }
818
819 /// Bitwise AND assignment operator.
820 ///
821 /// Performs a bitwise AND operation on this APInt and RHS. RHS is
822 /// logically zero-extended or truncated to match the bit-width of
823 /// the LHS.
824 APInt &operator&=(uint64_t RHS) {
825 if (isSingleWord()) {
826 U.VAL &= RHS;
827 return *this;
828 }
829 U.pVal[0] &= RHS;
830 memset(U.pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
831 return *this;
832 }
833
834 /// Bitwise OR assignment operator.
835 ///
836 /// Performs a bitwise OR operation on this APInt and RHS. The result is
837 /// assigned *this;
838 ///
839 /// \returns *this after ORing with RHS.
840 APInt &operator|=(const APInt &RHS) {
841 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast <bool> (BitWidth == RHS.BitWidth &&
"Bit widths must be the same") ? void (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 841, __extension__ __PRETTY_FUNCTION__))
;
842 if (isSingleWord())
843 U.VAL |= RHS.U.VAL;
844 else
845 OrAssignSlowCase(RHS);
846 return *this;
847 }
848
849 /// Bitwise OR assignment operator.
850 ///
851 /// Performs a bitwise OR operation on this APInt and RHS. RHS is
852 /// logically zero-extended or truncated to match the bit-width of
853 /// the LHS.
854 APInt &operator|=(uint64_t RHS) {
855 if (isSingleWord()) {
856 U.VAL |= RHS;
857 return clearUnusedBits();
858 }
859 U.pVal[0] |= RHS;
860 return *this;
861 }
862
863 /// Bitwise XOR assignment operator.
864 ///
865 /// Performs a bitwise XOR operation on this APInt and RHS. The result is
866 /// assigned to *this.
867 ///
868 /// \returns *this after XORing with RHS.
869 APInt &operator^=(const APInt &RHS) {
870 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast <bool> (BitWidth == RHS.BitWidth &&
"Bit widths must be the same") ? void (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 870, __extension__ __PRETTY_FUNCTION__))
;
871 if (isSingleWord())
872 U.VAL ^= RHS.U.VAL;
873 else
874 XorAssignSlowCase(RHS);
875 return *this;
876 }
877
878 /// Bitwise XOR assignment operator.
879 ///
880 /// Performs a bitwise XOR operation on this APInt and RHS. RHS is
881 /// logically zero-extended or truncated to match the bit-width of
882 /// the LHS.
883 APInt &operator^=(uint64_t RHS) {
884 if (isSingleWord()) {
885 U.VAL ^= RHS;
886 return clearUnusedBits();
887 }
888 U.pVal[0] ^= RHS;
889 return *this;
890 }
891
892 /// Multiplication assignment operator.
893 ///
894 /// Multiplies this APInt by RHS and assigns the result to *this.
895 ///
896 /// \returns *this
897 APInt &operator*=(const APInt &RHS);
898 APInt &operator*=(uint64_t RHS);
899
900 /// Addition assignment operator.
901 ///
902 /// Adds RHS to *this and assigns the result to *this.
903 ///
904 /// \returns *this
905 APInt &operator+=(const APInt &RHS);
906 APInt &operator+=(uint64_t RHS);
907
908 /// Subtraction assignment operator.
909 ///
910 /// Subtracts RHS from *this and assigns the result to *this.
911 ///
912 /// \returns *this
913 APInt &operator-=(const APInt &RHS);
914 APInt &operator-=(uint64_t RHS);
915
916 /// Left-shift assignment function.
917 ///
918 /// Shifts *this left by shiftAmt and assigns the result to *this.
919 ///
920 /// \returns *this after shifting left by ShiftAmt
921 APInt &operator<<=(unsigned ShiftAmt) {
922 assert(ShiftAmt <= BitWidth && "Invalid shift amount")(static_cast <bool> (ShiftAmt <= BitWidth &&
"Invalid shift amount") ? void (0) : __assert_fail ("ShiftAmt <= BitWidth && \"Invalid shift amount\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 922, __extension__ __PRETTY_FUNCTION__))
;
923 if (isSingleWord()) {
924 if (ShiftAmt == BitWidth)
925 U.VAL = 0;
926 else
927 U.VAL <<= ShiftAmt;
928 return clearUnusedBits();
929 }
930 shlSlowCase(ShiftAmt);
931 return *this;
932 }
933
934 /// Left-shift assignment function.
935 ///
936 /// Shifts *this left by shiftAmt and assigns the result to *this.
937 ///
938 /// \returns *this after shifting left by ShiftAmt
939 APInt &operator<<=(const APInt &ShiftAmt);
940
941 /// @}
942 /// \name Binary Operators
943 /// @{
944
945 /// Multiplication operator.
946 ///
947 /// Multiplies this APInt by RHS and returns the result.
948 APInt operator*(const APInt &RHS) const;
949
950 /// Left logical shift operator.
951 ///
952 /// Shifts this APInt left by \p Bits and returns the result.
953 APInt operator<<(unsigned Bits) const { return shl(Bits); }
954
955 /// Left logical shift operator.
956 ///
957 /// Shifts this APInt left by \p Bits and returns the result.
958 APInt operator<<(const APInt &Bits) const { return shl(Bits); }
959
960 /// Arithmetic right-shift function.
961 ///
962 /// Arithmetic right-shift this APInt by shiftAmt.
963 APInt ashr(unsigned ShiftAmt) const {
964 APInt R(*this);
965 R.ashrInPlace(ShiftAmt);
966 return R;
967 }
968
969 /// Arithmetic right-shift this APInt by ShiftAmt in place.
970 void ashrInPlace(unsigned ShiftAmt) {
971 assert(ShiftAmt <= BitWidth && "Invalid shift amount")(static_cast <bool> (ShiftAmt <= BitWidth &&
"Invalid shift amount") ? void (0) : __assert_fail ("ShiftAmt <= BitWidth && \"Invalid shift amount\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 971, __extension__ __PRETTY_FUNCTION__))
;
972 if (isSingleWord()) {
973 int64_t SExtVAL = SignExtend64(U.VAL, BitWidth);
974 if (ShiftAmt == BitWidth)
975 U.VAL = SExtVAL >> (APINT_BITS_PER_WORD - 1); // Fill with sign bit.
976 else
977 U.VAL = SExtVAL >> ShiftAmt;
978 clearUnusedBits();
979 return;
980 }
981 ashrSlowCase(ShiftAmt);
982 }
983
984 /// Logical right-shift function.
985 ///
986 /// Logical right-shift this APInt by shiftAmt.
987 APInt lshr(unsigned shiftAmt) const {
988 APInt R(*this);
989 R.lshrInPlace(shiftAmt);
990 return R;
991 }
992
993 /// Logical right-shift this APInt by ShiftAmt in place.
994 void lshrInPlace(unsigned ShiftAmt) {
995 assert(ShiftAmt <= BitWidth && "Invalid shift amount")(static_cast <bool> (ShiftAmt <= BitWidth &&
"Invalid shift amount") ? void (0) : __assert_fail ("ShiftAmt <= BitWidth && \"Invalid shift amount\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 995, __extension__ __PRETTY_FUNCTION__))
;
996 if (isSingleWord()) {
997 if (ShiftAmt == BitWidth)
998 U.VAL = 0;
999 else
1000 U.VAL >>= ShiftAmt;
1001 return;
1002 }
1003 lshrSlowCase(ShiftAmt);
1004 }
1005
1006 /// Left-shift function.
1007 ///
1008 /// Left-shift this APInt by shiftAmt.
1009 APInt shl(unsigned shiftAmt) const {
1010 APInt R(*this);
1011 R <<= shiftAmt;
1012 return R;
1013 }
1014
1015 /// Rotate left by rotateAmt.
1016 APInt rotl(unsigned rotateAmt) const;
1017
1018 /// Rotate right by rotateAmt.
1019 APInt rotr(unsigned rotateAmt) const;
1020
1021 /// Arithmetic right-shift function.
1022 ///
1023 /// Arithmetic right-shift this APInt by shiftAmt.
1024 APInt ashr(const APInt &ShiftAmt) const {
1025 APInt R(*this);
1026 R.ashrInPlace(ShiftAmt);
1027 return R;
1028 }
1029
1030 /// Arithmetic right-shift this APInt by shiftAmt in place.
1031 void ashrInPlace(const APInt &shiftAmt);
1032
1033 /// Logical right-shift function.
1034 ///
1035 /// Logical right-shift this APInt by shiftAmt.
1036 APInt lshr(const APInt &ShiftAmt) const {
1037 APInt R(*this);
1038 R.lshrInPlace(ShiftAmt);
1039 return R;
1040 }
1041
1042 /// Logical right-shift this APInt by ShiftAmt in place.
1043 void lshrInPlace(const APInt &ShiftAmt);
1044
1045 /// Left-shift function.
1046 ///
1047 /// Left-shift this APInt by shiftAmt.
1048 APInt shl(const APInt &ShiftAmt) const {
1049 APInt R(*this);
1050 R <<= ShiftAmt;
1051 return R;
1052 }
1053
1054 /// Rotate left by rotateAmt.
1055 APInt rotl(const APInt &rotateAmt) const;
1056
1057 /// Rotate right by rotateAmt.
1058 APInt rotr(const APInt &rotateAmt) const;
1059
1060 /// Unsigned division operation.
1061 ///
1062 /// Perform an unsigned divide operation on this APInt by RHS. Both this and
1063 /// RHS are treated as unsigned quantities for purposes of this division.
1064 ///
1065 /// \returns a new APInt value containing the division result, rounded towards
1066 /// zero.
1067 APInt udiv(const APInt &RHS) const;
1068 APInt udiv(uint64_t RHS) const;
1069
1070 /// Signed division function for APInt.
1071 ///
1072 /// Signed divide this APInt by APInt RHS.
1073 ///
1074 /// The result is rounded towards zero.
1075 APInt sdiv(const APInt &RHS) const;
1076 APInt sdiv(int64_t RHS) const;
1077
1078 /// Unsigned remainder operation.
1079 ///
1080 /// Perform an unsigned remainder operation on this APInt with RHS being the
1081 /// divisor. Both this and RHS are treated as unsigned quantities for purposes
1082 /// of this operation. Note that this is a true remainder operation and not a
1083 /// modulo operation because the sign follows the sign of the dividend which
1084 /// is *this.
1085 ///
1086 /// \returns a new APInt value containing the remainder result
1087 APInt urem(const APInt &RHS) const;
1088 uint64_t urem(uint64_t RHS) const;
1089
1090 /// Function for signed remainder operation.
1091 ///
1092 /// Signed remainder operation on APInt.
1093 APInt srem(const APInt &RHS) const;
1094 int64_t srem(int64_t RHS) const;
1095
1096 /// Dual division/remainder interface.
1097 ///
1098 /// Sometimes it is convenient to divide two APInt values and obtain both the
1099 /// quotient and remainder. This function does both operations in the same
1100 /// computation making it a little more efficient. The pair of input arguments
1101 /// may overlap with the pair of output arguments. It is safe to call
1102 /// udivrem(X, Y, X, Y), for example.
1103 static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
1104 APInt &Remainder);
1105 static void udivrem(const APInt &LHS, uint64_t RHS, APInt &Quotient,
1106 uint64_t &Remainder);
1107
1108 static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
1109 APInt &Remainder);
1110 static void sdivrem(const APInt &LHS, int64_t RHS, APInt &Quotient,
1111 int64_t &Remainder);
1112
1113 // Operations that return overflow indicators.
1114 APInt sadd_ov(const APInt &RHS, bool &Overflow) const;
1115 APInt uadd_ov(const APInt &RHS, bool &Overflow) const;
1116 APInt ssub_ov(const APInt &RHS, bool &Overflow) const;
1117 APInt usub_ov(const APInt &RHS, bool &Overflow) const;
1118 APInt sdiv_ov(const APInt &RHS, bool &Overflow) const;
1119 APInt smul_ov(const APInt &RHS, bool &Overflow) const;
1120 APInt umul_ov(const APInt &RHS, bool &Overflow) const;
1121 APInt sshl_ov(const APInt &Amt, bool &Overflow) const;
1122 APInt ushl_ov(const APInt &Amt, bool &Overflow) const;
1123
1124 // Operations that saturate
1125 APInt sadd_sat(const APInt &RHS) const;
1126 APInt uadd_sat(const APInt &RHS) const;
1127 APInt ssub_sat(const APInt &RHS) const;
1128 APInt usub_sat(const APInt &RHS) const;
1129 APInt smul_sat(const APInt &RHS) const;
1130 APInt umul_sat(const APInt &RHS) const;
1131 APInt sshl_sat(const APInt &RHS) const;
1132 APInt ushl_sat(const APInt &RHS) const;
1133
1134 /// Array-indexing support.
1135 ///
1136 /// \returns the bit value at bitPosition
1137 bool operator[](unsigned bitPosition) const {
1138 assert(bitPosition < getBitWidth() && "Bit position out of bounds!")(static_cast <bool> (bitPosition < getBitWidth() &&
"Bit position out of bounds!") ? void (0) : __assert_fail ("bitPosition < getBitWidth() && \"Bit position out of bounds!\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 1138, __extension__ __PRETTY_FUNCTION__))
;
1139 return (maskBit(bitPosition) & getWord(bitPosition)) != 0;
1140 }
1141
1142 /// @}
1143 /// \name Comparison Operators
1144 /// @{
1145
1146 /// Equality operator.
1147 ///
1148 /// Compares this APInt with RHS for the validity of the equality
1149 /// relationship.
1150 bool operator==(const APInt &RHS) const {
1151 assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths")(static_cast <bool> (BitWidth == RHS.BitWidth &&
"Comparison requires equal bit widths") ? void (0) : __assert_fail
("BitWidth == RHS.BitWidth && \"Comparison requires equal bit widths\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 1151, __extension__ __PRETTY_FUNCTION__))
;
1152 if (isSingleWord())
1153 return U.VAL == RHS.U.VAL;
1154 return EqualSlowCase(RHS);
1155 }
1156
1157 /// Equality operator.
1158 ///
1159 /// Compares this APInt with a uint64_t for the validity of the equality
1160 /// relationship.
1161 ///
1162 /// \returns true if *this == Val
1163 bool operator==(uint64_t Val) const {
1164 return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() == Val;
1165 }
1166
1167 /// Equality comparison.
1168 ///
1169 /// Compares this APInt with RHS for the validity of the equality
1170 /// relationship.
1171 ///
1172 /// \returns true if *this == Val
1173 bool eq(const APInt &RHS) const { return (*this) == RHS; }
1174
1175 /// Inequality operator.
1176 ///
1177 /// Compares this APInt with RHS for the validity of the inequality
1178 /// relationship.
1179 ///
1180 /// \returns true if *this != Val
1181 bool operator!=(const APInt &RHS) const { return !((*this) == RHS); }
1182
1183 /// Inequality operator.
1184 ///
1185 /// Compares this APInt with a uint64_t for the validity of the inequality
1186 /// relationship.
1187 ///
1188 /// \returns true if *this != Val
1189 bool operator!=(uint64_t Val) const { return !((*this) == Val); }
1190
1191 /// Inequality comparison
1192 ///
1193 /// Compares this APInt with RHS for the validity of the inequality
1194 /// relationship.
1195 ///
1196 /// \returns true if *this != Val
1197 bool ne(const APInt &RHS) const { return !((*this) == RHS); }
1198
1199 /// Unsigned less than comparison
1200 ///
1201 /// Regards both *this and RHS as unsigned quantities and compares them for
1202 /// the validity of the less-than relationship.
1203 ///
1204 /// \returns true if *this < RHS when both are considered unsigned.
1205 bool ult(const APInt &RHS) const { return compare(RHS) < 0; }
1206
1207 /// Unsigned less than comparison
1208 ///
1209 /// Regards both *this as an unsigned quantity and compares it with RHS for
1210 /// the validity of the less-than relationship.
1211 ///
1212 /// \returns true if *this < RHS when considered unsigned.
1213 bool ult(uint64_t RHS) const {
1214 // Only need to check active bits if not a single word.
1215 return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() < RHS;
1216 }
1217
1218 /// Signed less than comparison
1219 ///
1220 /// Regards both *this and RHS as signed quantities and compares them for
1221 /// validity of the less-than relationship.
1222 ///
1223 /// \returns true if *this < RHS when both are considered signed.
1224 bool slt(const APInt &RHS) const { return compareSigned(RHS) < 0; }
1225
1226 /// Signed less than comparison
1227 ///
1228 /// Regards both *this as a signed quantity and compares it with RHS for
1229 /// the validity of the less-than relationship.
1230 ///
1231 /// \returns true if *this < RHS when considered signed.
1232 bool slt(int64_t RHS) const {
1233 return (!isSingleWord() && getMinSignedBits() > 64) ? isNegative()
1234 : getSExtValue() < RHS;
1235 }
1236
1237 /// Unsigned less or equal comparison
1238 ///
1239 /// Regards both *this and RHS as unsigned quantities and compares them for
1240 /// validity of the less-or-equal relationship.
1241 ///
1242 /// \returns true if *this <= RHS when both are considered unsigned.
1243 bool ule(const APInt &RHS) const { return compare(RHS) <= 0; }
1244
1245 /// Unsigned less or equal comparison
1246 ///
1247 /// Regards both *this as an unsigned quantity and compares it with RHS for
1248 /// the validity of the less-or-equal relationship.
1249 ///
1250 /// \returns true if *this <= RHS when considered unsigned.
1251 bool ule(uint64_t RHS) const { return !ugt(RHS); }
1252
1253 /// Signed less or equal comparison
1254 ///
1255 /// Regards both *this and RHS as signed quantities and compares them for
1256 /// validity of the less-or-equal relationship.
1257 ///
1258 /// \returns true if *this <= RHS when both are considered signed.
1259 bool sle(const APInt &RHS) const { return compareSigned(RHS) <= 0; }
1260
1261 /// Signed less or equal comparison
1262 ///
1263 /// Regards both *this as a signed quantity and compares it with RHS for the
1264 /// validity of the less-or-equal relationship.
1265 ///
1266 /// \returns true if *this <= RHS when considered signed.
1267 bool sle(uint64_t RHS) const { return !sgt(RHS); }
1268
1269 /// Unsigned greater than comparison
1270 ///
1271 /// Regards both *this and RHS as unsigned quantities and compares them for
1272 /// the validity of the greater-than relationship.
1273 ///
1274 /// \returns true if *this > RHS when both are considered unsigned.
1275 bool ugt(const APInt &RHS) const { return !ule(RHS); }
1276
1277 /// Unsigned greater than comparison
1278 ///
1279 /// Regards both *this as an unsigned quantity and compares it with RHS for
1280 /// the validity of the greater-than relationship.
1281 ///
1282 /// \returns true if *this > RHS when considered unsigned.
1283 bool ugt(uint64_t RHS) const {
1284 // Only need to check active bits if not a single word.
1285 return (!isSingleWord() && getActiveBits() > 64) || getZExtValue() > RHS;
1286 }
1287
1288 /// Signed greater than comparison
1289 ///
1290 /// Regards both *this and RHS as signed quantities and compares them for the
1291 /// validity of the greater-than relationship.
1292 ///
1293 /// \returns true if *this > RHS when both are considered signed.
1294 bool sgt(const APInt &RHS) const { return !sle(RHS); }
1295
1296 /// Signed greater than comparison
1297 ///
1298 /// Regards both *this as a signed quantity and compares it with RHS for
1299 /// the validity of the greater-than relationship.
1300 ///
1301 /// \returns true if *this > RHS when considered signed.
1302 bool sgt(int64_t RHS) const {
1303 return (!isSingleWord() && getMinSignedBits() > 64) ? !isNegative()
1304 : getSExtValue() > RHS;
1305 }
1306
1307 /// Unsigned greater or equal comparison
1308 ///
1309 /// Regards both *this and RHS as unsigned quantities and compares them for
1310 /// validity of the greater-or-equal relationship.
1311 ///
1312 /// \returns true if *this >= RHS when both are considered unsigned.
1313 bool uge(const APInt &RHS) const { return !ult(RHS); }
1314
1315 /// Unsigned greater or equal comparison
1316 ///
1317 /// Regards both *this as an unsigned quantity and compares it with RHS for
1318 /// the validity of the greater-or-equal relationship.
1319 ///
1320 /// \returns true if *this >= RHS when considered unsigned.
1321 bool uge(uint64_t RHS) const { return !ult(RHS); }
1322
1323 /// Signed greater or equal comparison
1324 ///
1325 /// Regards both *this and RHS as signed quantities and compares them for
1326 /// validity of the greater-or-equal relationship.
1327 ///
1328 /// \returns true if *this >= RHS when both are considered signed.
1329 bool sge(const APInt &RHS) const { return !slt(RHS); }
1330
1331 /// Signed greater or equal comparison
1332 ///
1333 /// Regards both *this as a signed quantity and compares it with RHS for
1334 /// the validity of the greater-or-equal relationship.
1335 ///
1336 /// \returns true if *this >= RHS when considered signed.
1337 bool sge(int64_t RHS) const { return !slt(RHS); }
1338
1339 /// This operation tests if there are any pairs of corresponding bits
1340 /// between this APInt and RHS that are both set.
1341 bool intersects(const APInt &RHS) const {
1342 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast <bool> (BitWidth == RHS.BitWidth &&
"Bit widths must be the same") ? void (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 1342, __extension__ __PRETTY_FUNCTION__))
;
1343 if (isSingleWord())
1344 return (U.VAL & RHS.U.VAL) != 0;
1345 return intersectsSlowCase(RHS);
1346 }
1347
1348 /// This operation checks that all bits set in this APInt are also set in RHS.
1349 bool isSubsetOf(const APInt &RHS) const {
1350 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast <bool> (BitWidth == RHS.BitWidth &&
"Bit widths must be the same") ? void (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 1350, __extension__ __PRETTY_FUNCTION__))
;
1351 if (isSingleWord())
1352 return (U.VAL & ~RHS.U.VAL) == 0;
1353 return isSubsetOfSlowCase(RHS);
1354 }
1355
1356 /// @}
1357 /// \name Resizing Operators
1358 /// @{
1359
1360 /// Truncate to new width.
1361 ///
1362 /// Truncate the APInt to a specified width. It is an error to specify a width
1363 /// that is greater than or equal to the current width.
1364 APInt trunc(unsigned width) const;
1365
1366 /// Truncate to new width with unsigned saturation.
1367 ///
1368 /// If the APInt, treated as unsigned integer, can be losslessly truncated to
1369 /// the new bitwidth, then return truncated APInt. Else, return max value.
1370 APInt truncUSat(unsigned width) const;
1371
1372 /// Truncate to new width with signed saturation.
1373 ///
1374 /// If this APInt, treated as signed integer, can be losslessly truncated to
1375 /// the new bitwidth, then return truncated APInt. Else, return either
1376 /// signed min value if the APInt was negative, or signed max value.
1377 APInt truncSSat(unsigned width) const;
1378
1379 /// Sign extend to a new width.
1380 ///
1381 /// This operation sign extends the APInt to a new width. If the high order
1382 /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
1383 /// It is an error to specify a width that is less than or equal to the
1384 /// current width.
1385 APInt sext(unsigned width) const;
1386
1387 /// Zero extend to a new width.
1388 ///
1389 /// This operation zero extends the APInt to a new width. The high order bits
1390 /// are filled with 0 bits. It is an error to specify a width that is less
1391 /// than or equal to the current width.
1392 APInt zext(unsigned width) const;
1393
1394 /// Sign extend or truncate to width
1395 ///
1396 /// Make this APInt have the bit width given by \p width. The value is sign
1397 /// extended, truncated, or left alone to make it that width.
1398 APInt sextOrTrunc(unsigned width) const;
1399
1400 /// Zero extend or truncate to width
1401 ///
1402 /// Make this APInt have the bit width given by \p width. The value is zero
1403 /// extended, truncated, or left alone to make it that width.
1404 APInt zextOrTrunc(unsigned width) const;
1405
1406 /// Truncate to width
1407 ///
1408 /// Make this APInt have the bit width given by \p width. The value is
1409 /// truncated or left alone to make it that width.
1410 APInt truncOrSelf(unsigned width) const;
1411
1412 /// Sign extend or truncate to width
1413 ///
1414 /// Make this APInt have the bit width given by \p width. The value is sign
1415 /// extended, or left alone to make it that width.
1416 APInt sextOrSelf(unsigned width) const;
1417
1418 /// Zero extend or truncate to width
1419 ///
1420 /// Make this APInt have the bit width given by \p width. The value is zero
1421 /// extended, or left alone to make it that width.
1422 APInt zextOrSelf(unsigned width) const;
1423
1424 /// @}
1425 /// \name Bit Manipulation Operators
1426 /// @{
1427
1428 /// Set every bit to 1.
1429 void setAllBits() {
1430 if (isSingleWord())
1431 U.VAL = WORDTYPE_MAX;
1432 else
1433 // Set all the bits in all the words.
1434 memset(U.pVal, -1, getNumWords() * APINT_WORD_SIZE);
1435 // Clear the unused ones
1436 clearUnusedBits();
1437 }
1438
1439 /// Set a given bit to 1.
1440 ///
1441 /// Set the given bit to 1 whose position is given as "bitPosition".
1442 void setBit(unsigned BitPosition) {
1443 assert(BitPosition < BitWidth && "BitPosition out of range")(static_cast <bool> (BitPosition < BitWidth &&
"BitPosition out of range") ? void (0) : __assert_fail ("BitPosition < BitWidth && \"BitPosition out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 1443, __extension__ __PRETTY_FUNCTION__))
;
1444 WordType Mask = maskBit(BitPosition);
1445 if (isSingleWord())
1446 U.VAL |= Mask;
1447 else
1448 U.pVal[whichWord(BitPosition)] |= Mask;
1449 }
1450
1451 /// Set the sign bit to 1.
1452 void setSignBit() {
1453 setBit(BitWidth - 1);
1454 }
1455
1456 /// Set a given bit to a given value.
1457 void setBitVal(unsigned BitPosition, bool BitValue) {
1458 if (BitValue)
1459 setBit(BitPosition);
1460 else
1461 clearBit(BitPosition);
1462 }
1463
1464 /// Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
1465 /// This function handles "wrap" case when \p loBit >= \p hiBit, and calls
1466 /// setBits when \p loBit < \p hiBit.
1467 /// For \p loBit == \p hiBit wrap case, set every bit to 1.
1468 void setBitsWithWrap(unsigned loBit, unsigned hiBit) {
1469 assert(hiBit <= BitWidth && "hiBit out of range")(static_cast <bool> (hiBit <= BitWidth && "hiBit out of range"
) ? void (0) : __assert_fail ("hiBit <= BitWidth && \"hiBit out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 1469, __extension__ __PRETTY_FUNCTION__))
;
1470 assert(loBit <= BitWidth && "loBit out of range")(static_cast <bool> (loBit <= BitWidth && "loBit out of range"
) ? void (0) : __assert_fail ("loBit <= BitWidth && \"loBit out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 1470, __extension__ __PRETTY_FUNCTION__))
;
1471 if (loBit < hiBit) {
1472 setBits(loBit, hiBit);
1473 return;
1474 }
1475 setLowBits(hiBit);
1476 setHighBits(BitWidth - loBit);
1477 }
1478
1479 /// Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
1480 /// This function handles case when \p loBit <= \p hiBit.
1481 void setBits(unsigned loBit, unsigned hiBit) {
1482 assert(hiBit <= BitWidth && "hiBit out of range")(static_cast <bool> (hiBit <= BitWidth && "hiBit out of range"
) ? void (0) : __assert_fail ("hiBit <= BitWidth && \"hiBit out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 1482, __extension__ __PRETTY_FUNCTION__))
;
1483 assert(loBit <= BitWidth && "loBit out of range")(static_cast <bool> (loBit <= BitWidth && "loBit out of range"
) ? void (0) : __assert_fail ("loBit <= BitWidth && \"loBit out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 1483, __extension__ __PRETTY_FUNCTION__))
;
1484 assert(loBit <= hiBit && "loBit greater than hiBit")(static_cast <bool> (loBit <= hiBit && "loBit greater than hiBit"
) ? void (0) : __assert_fail ("loBit <= hiBit && \"loBit greater than hiBit\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 1484, __extension__ __PRETTY_FUNCTION__))
;
1485 if (loBit == hiBit)
1486 return;
1487 if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) {
1488 uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit));
1489 mask <<= loBit;
1490 if (isSingleWord())
1491 U.VAL |= mask;
1492 else
1493 U.pVal[0] |= mask;
1494 } else {
1495 setBitsSlowCase(loBit, hiBit);
1496 }
1497 }
1498
1499 /// Set the top bits starting from loBit.
1500 void setBitsFrom(unsigned loBit) {
1501 return setBits(loBit, BitWidth);
1502 }
1503
1504 /// Set the bottom loBits bits.
1505 void setLowBits(unsigned loBits) {
1506 return setBits(0, loBits);
1507 }
1508
1509 /// Set the top hiBits bits.
1510 void setHighBits(unsigned hiBits) {
1511 return setBits(BitWidth - hiBits, BitWidth);
1512 }
1513
1514 /// Set every bit to 0.
1515 void clearAllBits() {
1516 if (isSingleWord())
1517 U.VAL = 0;
1518 else
1519 memset(U.pVal, 0, getNumWords() * APINT_WORD_SIZE);
1520 }
1521
1522 /// Set a given bit to 0.
1523 ///
1524 /// Set the given bit to 0 whose position is given as "bitPosition".
1525 void clearBit(unsigned BitPosition) {
1526 assert(BitPosition < BitWidth && "BitPosition out of range")(static_cast <bool> (BitPosition < BitWidth &&
"BitPosition out of range") ? void (0) : __assert_fail ("BitPosition < BitWidth && \"BitPosition out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 1526, __extension__ __PRETTY_FUNCTION__))
;
1527 WordType Mask = ~maskBit(BitPosition);
1528 if (isSingleWord())
1529 U.VAL &= Mask;
1530 else
1531 U.pVal[whichWord(BitPosition)] &= Mask;
1532 }
1533
1534 /// Set bottom loBits bits to 0.
1535 void clearLowBits(unsigned loBits) {
1536 assert(loBits <= BitWidth && "More bits than bitwidth")(static_cast <bool> (loBits <= BitWidth && "More bits than bitwidth"
) ? void (0) : __assert_fail ("loBits <= BitWidth && \"More bits than bitwidth\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 1536, __extension__ __PRETTY_FUNCTION__))
;
1537 APInt Keep = getHighBitsSet(BitWidth, BitWidth - loBits);
1538 *this &= Keep;
1539 }
1540
1541 /// Set the sign bit to 0.
1542 void clearSignBit() {
1543 clearBit(BitWidth - 1);
1544 }
1545
1546 /// Toggle every bit to its opposite value.
1547 void flipAllBits() {
1548 if (isSingleWord()) {
1549 U.VAL ^= WORDTYPE_MAX;
1550 clearUnusedBits();
1551 } else {
1552 flipAllBitsSlowCase();
1553 }
1554 }
1555
1556 /// Toggles a given bit to its opposite value.
1557 ///
1558 /// Toggle a given bit to its opposite value whose position is given
1559 /// as "bitPosition".
1560 void flipBit(unsigned bitPosition);
1561
1562 /// Negate this APInt in place.
1563 void negate() {
1564 flipAllBits();
1565 ++(*this);
1566 }
1567
1568 /// Insert the bits from a smaller APInt starting at bitPosition.
1569 void insertBits(const APInt &SubBits, unsigned bitPosition);
1570 void insertBits(uint64_t SubBits, unsigned bitPosition, unsigned numBits);
1571
1572 /// Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
1573 APInt extractBits(unsigned numBits, unsigned bitPosition) const;
1574 uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const;
1575
1576 /// @}
1577 /// \name Value Characterization Functions
1578 /// @{
1579
1580 /// Return the number of bits in the APInt.
1581 unsigned getBitWidth() const { return BitWidth; }
1582
1583 /// Get the number of words.
1584 ///
1585 /// Here one word's bitwidth equals to that of uint64_t.
1586 ///
1587 /// \returns the number of words to hold the integer value of this APInt.
1588 unsigned getNumWords() const { return getNumWords(BitWidth); }
1589
1590 /// Get the number of words.
1591 ///
1592 /// *NOTE* Here one word's bitwidth equals to that of uint64_t.
1593 ///
1594 /// \returns the number of words to hold the integer value with a given bit
1595 /// width.
1596 static unsigned getNumWords(unsigned BitWidth) {
1597 return ((uint64_t)BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
1598 }
1599
1600 /// Compute the number of active bits in the value
1601 ///
1602 /// This function returns the number of active bits which is defined as the
1603 /// bit width minus the number of leading zeros. This is used in several
1604 /// computations to see how "wide" the value is.
1605 unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); }
1606
1607 /// Compute the number of active words in the value of this APInt.
1608 ///
1609 /// This is used in conjunction with getActiveData to extract the raw value of
1610 /// the APInt.
1611 unsigned getActiveWords() const {
1612 unsigned numActiveBits = getActiveBits();
1613 return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1;
1614 }
1615
1616 /// Get the minimum bit size for this signed APInt
1617 ///
1618 /// Computes the minimum bit width for this APInt while considering it to be a
1619 /// signed (and probably negative) value. If the value is not negative, this
1620 /// function returns the same value as getActiveBits()+1. Otherwise, it
1621 /// returns the smallest bit width that will retain the negative value. For
1622 /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
1623 /// for -1, this function will always return 1.
1624 unsigned getMinSignedBits() const { return BitWidth - getNumSignBits() + 1; }
1625
1626 /// Get zero extended value
1627 ///
1628 /// This method attempts to return the value of this APInt as a zero extended
1629 /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
1630 /// uint64_t. Otherwise an assertion will result.
1631 uint64_t getZExtValue() const {
1632 if (isSingleWord())
1633 return U.VAL;
1634 assert(getActiveBits() <= 64 && "Too many bits for uint64_t")(static_cast <bool> (getActiveBits() <= 64 &&
"Too many bits for uint64_t") ? void (0) : __assert_fail ("getActiveBits() <= 64 && \"Too many bits for uint64_t\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 1634, __extension__ __PRETTY_FUNCTION__))
;
1635 return U.pVal[0];
1636 }
1637
1638 /// Get sign extended value
1639 ///
1640 /// This method attempts to return the value of this APInt as a sign extended
1641 /// int64_t. The bit width must be <= 64 or the value must fit within an
1642 /// int64_t. Otherwise an assertion will result.
1643 int64_t getSExtValue() const {
1644 if (isSingleWord())
1645 return SignExtend64(U.VAL, BitWidth);
1646 assert(getMinSignedBits() <= 64 && "Too many bits for int64_t")(static_cast <bool> (getMinSignedBits() <= 64 &&
"Too many bits for int64_t") ? void (0) : __assert_fail ("getMinSignedBits() <= 64 && \"Too many bits for int64_t\""
, "/build/llvm-toolchain-snapshot-13~++20210521111114+b9076d119a5b/llvm/include/llvm/ADT/APInt.h"
, 1646, __extension__ __PRETTY_FUNCTION__))
;
1647 return int64_t(U.pVal[0]);
1648 }
1649
1650 /// Get bits required for string value.
1651 ///
1652 /// This method determines how many bits are required to hold the APInt
1653 /// equivalent of the string given by \p str.
1654 static unsigned getBitsNeeded(StringRef str, uint8_t radix);
1655
1656 /// The APInt version of the countLeadingZeros functions in
1657 /// MathExtras.h.
1658 ///
1659 /// It counts the number of zeros from the most significant bit to the first
1660 /// one bit.
1661 ///
1662 /// \returns BitWidth if the value is zero, otherwise returns the number of
1663 /// zeros from the most significant bit to the first one bits.
1664 unsigned countLeadingZeros() const {
1665 if (isSingleWord()) {
1666 unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth;
1667 return llvm::countLeadingZeros(U.VAL) - unusedBits;
1668 }
1669 return countLeadingZerosSlowCase();
1670 }
1671
1672 /// Count the number of leading one bits.
1673 ///
1674 /// This function is an APInt version of the countLeadingOnes
1675 /// functions in MathExtras.h. It counts the number of ones from the most
1676 /// significant bit to the first zero bit.
1677 ///
1678 /// \returns 0 if the high order bit is not set, otherwise returns the number
1679 /// of 1 bits from the most significant to the least
1680 unsigned countLeadingOnes() const {
1681 if (isSingleWord())
1682 return llvm::countLeadingOnes(U.VAL << (APINT_BITS_PER_WORD - BitWidth));
1683 return countLeadingOnesSlowCase();
1684 }
1685
1686 /// Computes the number of leading bits of this APInt that are equal to its
1687 /// sign bit.
1688 unsigned getNumSignBits() const {
1689 return isNegative() ? countLeadingOnes() : countLeadingZeros();
1690 }
1691
1692 /// Count the number of trailing zero bits.
1693 ///
1694 /// This function is an APInt version of the countTrailingZeros
1695 /// functions in MathExtras.h. It counts the number of zeros from the least
1696 /// significant bit to the first set bit.
1697 ///
1698 /// \returns BitWidth if the value is zero, otherwise returns the number of
1699 /// zeros from the least significant bit to the first one bit.
1700 unsigned countTrailingZeros() const {
1701 if (isSingleWord()) {
1702 unsigned TrailingZeros = llvm::countTrailingZeros(U.VAL);
1703 return (TrailingZeros > BitWidth ? BitWidth : TrailingZeros);
1704 }
1705 return countTrailingZerosSlowCase();
1706 }
1707
1708 /// Count the number of trailing one bits.
1709 ///
1710 /// This function is an APInt version of the countTrailingOnes
1711 /// functions in MathExtras.h. It counts the number of ones from the least
1712 /// significant bit to the first zero bit.
1713 ///
1714 /// \returns BitWidth if the value is all ones, otherwise returns the number
1715 /// of ones from the least significant bit to the first zero bit.
1716 unsigned countTrailingOnes() const {
1717 if (isSingleWord())
1718 return llvm::countTrailingOnes(U.VAL);
1719 return countTrailingOnesSlowCase();
1720 }
1721
1722 /// Count the number of bits set.
1723 ///
1724 /// This function is an APInt version of the countPopulation functions
1725 /// in MathExtras.h. It counts the number of 1 bits in the APInt value.
1726 ///
1727 /// \returns 0 if the value is zero, otherwise returns the number of set bits.
1728 unsigned countPopulation() const {
1729 if (isSingleWord())
1730 return llvm::countPopulation(U.VAL);
1731 return countPopulationSlowCase();
1732 }
1733
1734 /// @}
1735 /// \name Conversion Functions
1736 /// @{
1737 void print(raw_ostream &OS, bool isSigned) const;
1738
1739 /// Converts an APInt to a string and append it to Str. Str is commonly a
1740 /// SmallString.
1741 void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
1742 bool formatAsCLiteral = false) const;
1743
1744 /// Considers the APInt to be unsigned and converts it into a string in the
1745 /// radix given. The radix can be 2, 8, 10 16, or 36.
1746 void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
1747 toString(Str, Radix, false, false);
1748 }
1749
1750 /// Considers the APInt to be signed and converts it into a string in the
1751 /// radix given. The radix can be 2, 8, 10, 16, or 36.
1752 void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
1753 toString(Str, Radix, true, false);
1754 }
1755
1756 /// Return the APInt as a std::string.
1757 ///
1758 /// Note that this is an inefficient method. It is better to pass in a
1759 /// SmallVector/SmallString to the methods above to avoid thrashing the heap
1760 /// for the string.
1761 std::string toString(unsigned Radix, bool Signed) const;
1762
1763 /// \returns a byte-swapped representation of this APInt Value.
1764 APInt byteSwap() const;
1765
1766 /// \returns the value with the bit representation reversed of this APInt
1767 /// Value.
1768 APInt reverseBits() const;
1769
1770 /// Converts this APInt to a double value.
1771 double roundToDouble(bool isSigned) const;
1772
1773 /// Converts this unsigned APInt to a double value.
1774 double roundToDouble() const { return roundToDouble(false); }
1775
1776 /// Converts this signed APInt to a double value.
1777 double signedRoundToDouble() const { return roundToDouble(true); }
1778
1779 /// Converts APInt bits to a double
1780 ///
1781 /// The conversion does not do a translation from integer to double, it just
1782 /// re-interprets the bits as a double. Note that it is valid to do this on
1783 /// any bit width. Exactly 64 bits will be translated.
1784 double bitsToDouble() const {
1785 return BitsToDouble(getWord(0));
1786 }
1787
1788 /// Converts APInt bits to a float
1789 ///
1790 /// The conversion does not do a translation from integer to float, it just
1791 /// re-interprets the bits as a float. Note that it is valid to do this on
1792 /// any bit width. Exactly 32 bits will be translated.
1793 float bitsToFloat() const {
1794 return BitsToFloat(static_cast<uint32_t>(getWord(0)));
1795 }
1796
1797 /// Converts a double to APInt bits.
1798 ///
1799 /// The conversion does not do a translation from double to integer, it just
1800 /// re-interprets the bits of the double.
1801 static APInt doubleToBits(double V) {
1802 return APInt(sizeof(double) * CHAR_BIT8, DoubleToBits(V));
1803 }
1804
1805 /// Converts a float to APInt bits.
1806 ///
1807 /// The conversion does not do a translation from float to integer, it just
1808 /// re-interprets the bits of the float.
1809 static APInt floatToBits(float V) {
1810 return APInt(sizeof(float) * CHAR_BIT8, FloatToBits(V));
1811 }
1812
1813 /// @}
1814 /// \name Mathematics Operations
1815 /// @{
1816
1817 /// \returns the floor log base 2 of this APInt.
1818 unsigned logBase2() const { return getActiveBits() - 1; }
1819
1820 /// \returns the ceil log base 2 of this APInt.
1821 unsigned ceilLogBase2() const {
1822 APInt temp(*this);
1823 --temp;
1824 return temp.getActiveBits();
1825 }
1826
1827 /// \returns the nearest log base 2 of this APInt. Ties round up.
1828 ///
1829 /// NOTE: When we have a BitWidth of 1, we define:
1830 ///
1831 /// log2(0) = UINT32_MAX
1832 /// log2(1) = 0
1833 ///
1834 /// to get around any mathematical concerns resulting from
1835 /// referencing 2 in a space where 2 does no exist.
1836 unsigned nearestLogBase2() const {
1837 // Special case when we have a bitwidth of 1. If VAL is 1, then we
1838 // get 0. If VAL is 0, we get WORDTYPE_MAX which gets truncated to
1839 // UINT32_MAX.
1840 if (BitWidth == 1)
1841 return U.VAL - 1;
1842
1843 // Handle the zero case.
1844 if (isNullValue())
1845 return UINT32_MAX(4294967295U);
1846
1847 // The non-zero case is handled by computing:
1848 //
1849 // nearestLogBase2(x) = logBase2(x) + x[logBase2(x)-1].
1850 //
1851 // where x[i] is referring to the value of the ith bit of x.
1852 unsigned lg = logBase2();
1853 return lg + unsigned((*this)[lg - 1]);
1854 }
1855
1856 /// \returns the log base 2 of this APInt if its an exact power of two, -1
1857 /// otherwise
1858 int32_t exactLogBase2() const {
1859 if (!isPowerOf2())
1860 return -1;
1861 return logBase2();
1862 }
1863
1864 /// Compute the square root
1865 APInt sqrt() const;
1866
1867 /// Get the absolute value;
1868 ///
1869 /// If *this is < 0 then return -(*this), otherwise *this;
1870 APInt abs() const {
1871 if (isNegative())
1872 return -(*this);
1873 return *this;
1874 }
1875
1876 /// \returns the multiplicative inverse for a given modulo.
1877 APInt multiplicativeInverse(const APInt &modulo) const;
1878
1879 /// @}
1880 /// \name Support for division by constant
1881 /// @{
1882
1883 /// Calculate the magic number for signed division by a constant.
1884 struct ms;
1885 ms magic() const;
1886
1887 /// Calculate the magic number for unsigned division by a constant.
1888 struct mu;
1889 mu magicu(unsigned LeadingZeros = 0) const;
1890
1891 /// @}
1892 /// \name Building-block Operations for APInt and APFloat
1893 /// @{
1894
1895 // These building block operations operate on a representation of arbitrary
1896 // precision, two's-complement, bignum integer values. They should be
1897 // sufficient to implement APInt and APFloat bignum requirements. Inputs are
1898 // generally a pointer to the base of an array of integer parts, representing
1899 // an unsigned bignum, and a count of how many parts there are.
1900
1901 /// Sets the least significant part of a bignum to the input value, and zeroes
1902 /// out higher parts.
1903 static void tcSet(WordType *, WordType, unsigned);
1904
1905 /// Assign one bignum to another.
1906 static void tcAssign(WordType *, const WordType *, unsigned);
1907
1908 /// Returns true if a bignum is zero, false otherwise.
1909 static bool tcIsZero(const WordType *, unsigned);
1910
1911 /// Extract the given bit of a bignum; returns 0 or 1. Zero-based.
1912 static int tcExtractBit(const WordType *, unsigned bit);
1913
1914 /// Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to
1915 /// DST, of dstCOUNT parts, such that the bit srcLSB becomes the least
1916 /// significant bit of DST. All high bits above srcBITS in DST are
1917 /// zero-filled.
1918 static void tcExtract(WordType *, unsigned dstCount,
1919 const WordType *, unsigned srcBits,
1920 unsigned srcLSB);
1921
1922 /// Set the given bit of a bignum. Zero-based.
1923 static void tcSetBit(WordType *, unsigned bit);
1924
1925 /// Clear the given bit of a bignum. Zero-based.
1926 static void tcClearBit(WordType *, unsigned bit);
1927
1928 /// Returns the bit number of the least or most significant set bit of a
1929 /// number. If the input number has no bits set -1U is returned.
1930 static unsigned tcLSB(const WordType *, unsigned n);
1931 static unsigned tcMSB(const WordType *parts, unsigned n);
1932
1933 /// Negate a bignum in-place.
1934 static void tcNegate(WordType *, unsigned);
1935
1936 /// DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag.
1937 static WordType tcAdd(WordType *, const WordType *,
1938 WordType carry, unsigned);
1939 /// DST += RHS. Returns the carry flag.
1940 static WordType tcAddPart(WordType *, WordType, unsigned);
1941
1942 /// DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
1943 static WordType tcSubtract(WordType *, const WordType *,
1944 WordType carry, unsigned);
1945 /// DST -= RHS. Returns the carry flag.
1946 static WordType tcSubtractPart(WordType *, WordType, unsigned);
1947
1948 /// DST += SRC * MULTIPLIER + PART if add is true
1949 /// DST = SRC * MULTIPLIER + PART if add is false
1950 ///
1951 /// Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC they must
1952 /// start at the same point, i.e. DST == SRC.
1953 ///
1954 /// If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is returned.
1955 /// Otherwise DST is filled with the least significant DSTPARTS parts of the
1956 /// result, and if all of the omitted higher parts were zero return zero,
1957 /// otherwise overflow occurred and return one.
1958 static int tcMultiplyPart(WordType *dst, const WordType *src,
1959 WordType multiplier, WordType carry,
1960 unsigned srcParts, unsigned dstParts,
1961 bool add);
1962
1963 /// DST = LHS * RHS, where DST has the same width as the operands and is
1964 /// filled with the least significant parts of the result. Returns one if
1965 /// overflow occurred, otherwise zero. DST must be disjoint from both
1966 /// operands.
1967 static int tcMultiply(WordType *, const WordType *, const WordType *,
1968 unsigned);
1969
1970 /// DST = LHS * RHS, where DST has width the sum of the widths of the
1971 /// operands. No overflow occurs. DST must be disjoint from both operands.
1972 static void tcFullMultiply(WordType *, const WordType *,
1973 const WordType *, unsigned, unsigned);
1974
1975 /// If RHS is zero LHS and REMAINDER are left unchanged, return one.
1976 /// Otherwise set LHS to LHS / RHS with the fractional part discarded, set
1977 /// REMAINDER to the remainder, return zero. i.e.
1978 ///
1979 /// OLD_LHS = RHS * LHS + REMAINDER
1980 ///
1981 /// SCRATCH is a bignum of the same size as the operands and result for use by
1982 /// the routine; its contents need not be initialized and are destroyed. LHS,
1983 /// REMAINDER and SCRATCH must be distinct.
1984 static int tcDivide(WordType *lhs, const WordType *rhs,
1985 WordType *remainder, WordType *scratch,
1986 unsigned parts);
1987
1988 /// Shift a bignum left Count bits. Shifted in bits are zero. There are no
1989 /// restrictions on Count.
1990 static void tcShiftLeft(WordType *, unsigned Words, unsigned Count);
1991
1992 /// Shift a bignum right Count bits. Shifted in bits are zero. There are no
1993 /// restrictions on Count.
1994 static void tcShiftRight(WordType *, unsigned Words, unsigned Count);
1995
1996 /// The obvious AND, OR and XOR and complement operations.
1997 static void tcAnd(WordType *, const WordType *, unsigned);
1998 static void tcOr(WordType *, const WordType *, unsigned);
1999 static void tcXor(WordType *, const WordType *, unsigned);
2000 static void tcComplement(WordType *, unsigned);
2001
2002 /// Comparison (unsigned) of two bignums.
2003 static int tcCompare(const WordType *, const WordType *, unsigned);
2004
2005 /// Increment a bignum in-place. Return the carry flag.
2006 static WordType tcIncrement(WordType *dst, unsigned parts) {
2007 return tcAddPart(dst, 1, parts);
2008 }
2009
2010 /// Decrement a bignum in-place. Return the borrow flag.
2011 static WordType tcDecrement(WordType *dst, unsigned parts) {
2012 return tcSubtractPart(dst, 1, parts);
2013 }
2014
2015 /// Set the least significant BITS and clear the rest.
2016 static void tcSetLeastSignificantBits(WordType *, unsigned, unsigned bits);
2017
2018 /// debug method
2019 void dump() const;
2020
2021 /// @}
2022};
2023
2024/// Magic data for optimising signed division by a constant.
2025struct APInt::ms {
2026 APInt m; ///< magic number
2027 unsigned s; ///< shift amount
2028};
2029
2030/// Magic data for optimising unsigned division by a constant.
2031struct APInt::mu {
2032 APInt m; ///< magic number
2033 bool a; ///< add indicator
2034 unsigned s; ///< shift amount
2035};
2036
2037inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; }
2038
2039inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; }
2040
2041/// Unary bitwise complement operator.
2042///
2043/// \returns an APInt that is the bitwise complement of \p v.
2044inline APInt operator~(APInt v) {
2045 v.flipAllBits();
2046 return v;
2047}
2048
2049inline APInt operator&(APInt a, const APInt &b) {
2050 a &= b;
2051 return a;
2052}
2053
2054inline APInt operator&(const APInt &a, APInt &&b) {
2055 b &= a;
2056 return std::move(b);
2057}
2058
2059inline APInt operator&(APInt a, uint64_t RHS) {
2060 a &= RHS;
2061 return a;
2062}
2063
2064inline APInt operator&(uint64_t LHS, APInt b) {
2065 b &= LHS;
2066 return b;
2067}
2068
2069inline APInt operator|(APInt a, const APInt &b) {
2070 a |= b;
2071 return a;
2072}
2073
2074inline APInt operator|(const APInt &a, APInt &&b) {
2075 b |= a;
2076 return std::move(b);
2077}
2078
2079inline APInt operator|(APInt a, uint64_t RHS) {
2080 a |= RHS;
2081 return a;
2082}
2083
2084inline APInt operator|(uint64_t LHS, APInt b) {
2085 b |= LHS;
2086 return b;
2087}
2088
2089inline APInt operator^(APInt a, const APInt &b) {
2090 a ^= b;
2091 return a;
2092}
2093
2094inline APInt operator^(const APInt &a, APInt &&b) {
2095 b ^= a;
2096 return std::move(b);
2097}
2098
2099inline APInt operator^(APInt a, uint64_t RHS) {
2100 a ^= RHS;
2101 return a;
2102}
2103
2104inline APInt operator^(uint64_t LHS, APInt b) {
2105 b ^= LHS;
2106 return b;
2107}
2108
2109inline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) {
2110 I.print(OS, true);
2111 return OS;
2112}
2113
2114inline APInt operator-(APInt v) {
2115 v.negate();
2116 return v;
2117}
2118
2119inline APInt operator+(APInt a, const APInt &b) {
2120 a += b;
2121 return a;
2122}
2123
2124inline APInt operator+(const APInt &a, APInt &&b) {
2125 b += a;
2126 return std::move(b);
2127}
2128
2129inline APInt operator+(APInt a, uint64_t RHS) {
2130 a += RHS;
2131 return a;
2132}
2133
2134inline APInt operator+(uint64_t LHS, APInt b) {
2135 b += LHS;
2136 return b;
2137}
2138
2139inline APInt operator-(APInt a, const APInt &b) {
2140 a -= b;
2141 return a;
2142}
2143
2144inline APInt operator-(const APInt &a, APInt &&b) {
2145 b.negate();
2146 b += a;
2147 return std::move(b);
2148}
2149
2150inline APInt operator-(APInt a, uint64_t RHS) {
2151 a -= RHS;
2152 return a;
2153}
2154
2155inline APInt operator-(uint64_t LHS, APInt b) {
2156 b.negate();
2157 b += LHS;
2158 return b;
2159}
2160
2161inline APInt operator*(APInt a, uint64_t RHS) {
2162 a *= RHS;
2163 return a;
2164}
2165
2166inline APInt operator*(uint64_t LHS, APInt b) {
2167 b *= LHS;
2168 return b;
2169}
2170
2171
2172namespace APIntOps {
2173
2174/// Determine the smaller of two APInts considered to be signed.
2175inline const APInt &smin(const APInt &A, const APInt &B) {
2176 return A.slt(B) ? A : B;
2177}
2178
2179/// Determine the larger of two APInts considered to be signed.
2180inline const APInt &smax(const APInt &A, const APInt &B) {
2181 return A.sgt(B) ? A : B;
2182}
2183
2184/// Determine the smaller of two APInts considered to be unsigned.
2185inline const APInt &umin(const APInt &A, const APInt &B) {
2186 return A.ult(B) ? A : B;
2187}
2188
2189/// Determine the larger of two APInts considered to be unsigned.
2190inline const APInt &umax(const APInt &A, const APInt &B) {
2191 return A.ugt(B) ? A : B;
2192}
2193
2194/// Compute GCD of two unsigned APInt values.
2195///
2196/// This function returns the greatest common divisor of the two APInt values
2197/// using Stein's algorithm.
2198///
2199/// \returns the greatest common divisor of A and B.
2200APInt GreatestCommonDivisor(APInt A, APInt B);
2201
2202/// Converts the given APInt to a double value.
2203///
2204/// Treats the APInt as an unsigned value for conversion purposes.
2205inline double RoundAPIntToDouble(const APInt &APIVal) {
2206 return APIVal.roundToDouble();
2207}
2208
2209/// Converts the given APInt to a double value.
2210///
2211/// Treats the APInt as a signed value for conversion purposes.
2212inline double RoundSignedAPIntToDouble(const APInt &APIVal) {
2213 return APIVal.signedRoundToDouble();
2214}
2215
2216/// Converts the given APInt to a float value.
2217inline float RoundAPIntToFloat(const APInt &APIVal) {
2218 return float(RoundAPIntToDouble(APIVal));
2219}
2220
2221/// Converts the given APInt to a float value.
2222///
2223/// Treats the APInt as a signed value for conversion purposes.
2224inline float RoundSignedAPIntToFloat(const APInt &APIVal) {
2225 return float(APIVal.signedRoundToDouble());
2226}
2227
2228/// Converts the given double value into a APInt.
2229///
2230/// This function convert a double value to an APInt value.
2231APInt RoundDoubleToAPInt(double Double, unsigned width);
2232
2233/// Converts a float value into a APInt.
2234///
2235/// Converts a float value into an APInt value.
2236inline APInt RoundFloatToAPInt(float Float, unsigned width) {
2237 return RoundDoubleToAPInt(double(Float), width);
2238}
2239
2240/// Return A unsign-divided by B, rounded by the given rounding mode.
2241APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM);
2242
2243/// Return A sign-divided by B, rounded by the given rounding mode.
2244APInt RoundingSDiv(const APInt &A, const APInt &B, APInt::Rounding RM);
2245
2246/// Let q(n) = An^2 + Bn + C, and BW = bit width of the value range
2247/// (e.g. 32 for i32).
2248/// This function finds the smallest number n, such that
2249/// (a) n >= 0 and q(n) = 0, or
2250/// (b) n >= 1 and q(n-1) and q(n), when evaluated in the set of all
2251/// integers, belong to two different intervals [Rk, Rk+R),
2252/// where R = 2^BW, and k is an integer.
2253/// The idea here is to find when q(n) "overflows" 2^BW, while at the
2254/// same time "allowing" subtraction. In unsigned modulo arithmetic a
2255/// subtraction (treated as addition of negated numbers) would always
2256/// count as an overflow, but here we want to allow values to decrease
2257/// and increase as long as they are within the same interval.
2258/// Specifically, adding of two negative numbers should not cause an
2259/// overflow (as long as the magnitude does not exceed the bit width).
2260/// On the other hand, given a positive number, adding a negative
2261/// number to it can give a negative result, which would cause the
2262/// value to go from [-2^BW, 0) to [0, 2^BW). In that sense, zero is
2263/// treated as a special case of an overflow.
2264///
2265/// This function returns None if after finding k that minimizes the
2266/// positive solution to q(n) = kR, both solutions are contained between
2267/// two consecutive integers.
2268///
2269/// There are cases where q(n) > T, and q(n+1) < T (assuming evaluation
2270/// in arithmetic modulo 2^BW, and treating the values as signed) by the
2271/// virtue of *signed* overflow. This function will *not* find such an n,
2272/// however it may find a value of n satisfying the inequalities due to
2273/// an *unsigned* overflow (if the values are treated as unsigned).
2274/// To find a solution for a signed overflow, treat it as a problem of
2275/// finding an unsigned overflow with a range with of BW-1.
2276///
2277/// The returned value may have a different bit width from the input
2278/// coefficients.
2279Optional<APInt> SolveQuadraticEquationWrap(APInt A, APInt B, APInt C,
2280 unsigned RangeWidth);
2281
2282/// Compare two values, and if they are different, return the position of the
2283/// most significant bit that is different in the values.
2284Optional<unsigned> GetMostSignificantDifferentBit(const APInt &A,
2285 const APInt &B);
2286
2287} // End of APIntOps namespace
2288
2289// See friend declaration above. This additional declaration is required in
2290// order to compile LLVM with IBM xlC compiler.
2291hash_code hash_value(const APInt &Arg);
2292
2293/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
2294/// with the integer held in IntVal.
2295void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, unsigned StoreBytes);
2296
2297/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
2298/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
2299void LoadIntFromMemory(APInt &IntVal, const uint8_t *Src, unsigned LoadBytes);
2300
2301} // namespace llvm
2302
2303#endif