Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/IR/Constants.h
Line
Count
Source (jump to first uncovered line)
1
//===-- llvm/Constants.h - Constant class subclass definitions --*- 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 contains the declarations for the subclasses of Constant,
11
/// which represent the different flavors of constant values that live in LLVM.
12
/// Note that Constants are immutable (once created they never change) and are
13
/// fully shared by structural equivalence.  This means that two structurally
14
/// equivalent constants will always have the same address.  Constants are
15
/// created on demand as needed and never deleted: thus clients don't have to
16
/// worry about the lifetime of the objects.
17
//
18
//===----------------------------------------------------------------------===//
19
20
#ifndef LLVM_IR_CONSTANTS_H
21
#define LLVM_IR_CONSTANTS_H
22
23
#include "llvm/ADT/APFloat.h"
24
#include "llvm/ADT/APInt.h"
25
#include "llvm/ADT/ArrayRef.h"
26
#include "llvm/ADT/None.h"
27
#include "llvm/ADT/Optional.h"
28
#include "llvm/ADT/STLExtras.h"
29
#include "llvm/ADT/StringRef.h"
30
#include "llvm/IR/Constant.h"
31
#include "llvm/IR/DerivedTypes.h"
32
#include "llvm/IR/OperandTraits.h"
33
#include "llvm/IR/User.h"
34
#include "llvm/IR/Value.h"
35
#include "llvm/Support/Casting.h"
36
#include "llvm/Support/Compiler.h"
37
#include "llvm/Support/ErrorHandling.h"
38
#include <cassert>
39
#include <cstddef>
40
#include <cstdint>
41
42
namespace llvm {
43
44
class ArrayType;
45
class IntegerType;
46
class PointerType;
47
class SequentialType;
48
class StructType;
49
class VectorType;
50
template <class ConstantClass> struct ConstantAggrKeyType;
51
52
/// Base class for constants with no operands.
53
///
54
/// These constants have no operands; they represent their data directly.
55
/// Since they can be in use by unrelated modules (and are never based on
56
/// GlobalValues), it never makes sense to RAUW them.
57
class ConstantData : public Constant {
58
  friend class Constant;
59
60
0
  Value *handleOperandChangeImpl(Value *From, Value *To) {
61
0
    llvm_unreachable("Constant data does not have operands!");
62
0
  }
63
64
protected:
65
4.09M
  explicit ConstantData(Type *Ty, ValueTy VT) : Constant(Ty, VT, nullptr, 0) {}
66
67
4.09M
  void *operator new(size_t s) { return User::operator new(s, 0); }
68
69
public:
70
  ConstantData(const ConstantData &) = delete;
71
72
  /// Methods to support type inquiry through isa, cast, and dyn_cast.
73
401k
  static bool classof(const Value *V) {
74
401k
    return V->getValueID() >= ConstantDataFirstVal &&
75
401k
           
V->getValueID() <= ConstantDataLastVal118
;
76
401k
  }
77
};
78
79
//===----------------------------------------------------------------------===//
80
/// This is the shared class of boolean and integer constants. This class
81
/// represents both boolean and integral constants.
82
/// Class for constant integers.
83
class ConstantInt final : public ConstantData {
84
  friend class Constant;
85
86
  APInt Val;
87
88
  ConstantInt(IntegerType *Ty, const APInt& V);
89
90
  void destroyConstantImpl();
91
92
public:
93
  ConstantInt(const ConstantInt &) = delete;
94
95
  static ConstantInt *getTrue(LLVMContext &Context);
96
  static ConstantInt *getFalse(LLVMContext &Context);
97
  static Constant *getTrue(Type *Ty);
98
  static Constant *getFalse(Type *Ty);
99
100
  /// If Ty is a vector type, return a Constant with a splat of the given
101
  /// value. Otherwise return a ConstantInt for the given value.
102
  static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
103
104
  /// Return a ConstantInt with the specified integer value for the specified
105
  /// type. If the type is wider than 64 bits, the value will be zero-extended
106
  /// to fit the type, unless isSigned is true, in which case the value will
107
  /// be interpreted as a 64-bit signed integer and sign-extended to fit
108
  /// the type.
109
  /// Get a ConstantInt for a specific value.
110
  static ConstantInt *get(IntegerType *Ty, uint64_t V,
111
                          bool isSigned = false);
112
113
  /// Return a ConstantInt with the specified value for the specified type. The
114
  /// value V will be canonicalized to a an unsigned APInt. Accessing it with
115
  /// either getSExtValue() or getZExtValue() will yield a correctly sized and
116
  /// signed value for the type Ty.
117
  /// Get a ConstantInt for a specific signed value.
118
  static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
119
  static Constant *getSigned(Type *Ty, int64_t V);
120
121
  /// Return a ConstantInt with the specified value and an implied Type. The
122
  /// type is the integer type that corresponds to the bit width of the value.
123
  static ConstantInt *get(LLVMContext &Context, const APInt &V);
124
125
  /// Return a ConstantInt constructed from the string strStart with the given
126
  /// radix.
127
  static ConstantInt *get(IntegerType *Ty, StringRef Str,
128
                          uint8_t radix);
129
130
  /// If Ty is a vector type, return a Constant with a splat of the given
131
  /// value. Otherwise return a ConstantInt for the given value.
132
  static Constant *get(Type* Ty, const APInt& V);
133
134
  /// Return the constant as an APInt value reference. This allows clients to
135
  /// obtain a full-precision copy of the value.
136
  /// Return the constant's value.
137
1.09G
  inline const APInt &getValue() const {
138
1.09G
    return Val;
139
1.09G
  }
140
141
  /// getBitWidth - Return the bitwidth of this constant.
142
17.4M
  unsigned getBitWidth() const { return Val.getBitWidth(); }
143
144
  /// Return the constant as a 64-bit unsigned integer value after it
145
  /// has been zero extended as appropriate for the type of this constant. Note
146
  /// that this method can assert if the value does not fit in 64 bits.
147
  /// Return the zero extended value.
148
362M
  inline uint64_t getZExtValue() const {
149
362M
    return Val.getZExtValue();
150
362M
  }
151
152
  /// Return the constant as a 64-bit integer value after it has been sign
153
  /// extended as appropriate for the type of this constant. Note that
154
  /// this method can assert if the value does not fit in 64 bits.
155
  /// Return the sign extended value.
156
98.0M
  inline int64_t getSExtValue() const {
157
98.0M
    return Val.getSExtValue();
158
98.0M
  }
159
160
  /// A helper method that can be used to determine if the constant contained
161
  /// within is equal to a constant.  This only works for very small values,
162
  /// because this is all that can be represented with all types.
163
  /// Determine if this constant's value is same as an unsigned char.
164
56
  bool equalsInt(uint64_t V) const {
165
56
    return Val == V;
166
56
  }
167
168
  /// getType - Specialize the getType() method to always return an IntegerType,
169
  /// which reduces the amount of casting needed in parts of the compiler.
170
  ///
171
126M
  inline IntegerType *getType() const {
172
126M
    return cast<IntegerType>(Value::getType());
173
126M
  }
174
175
  /// This static method returns true if the type Ty is big enough to
176
  /// represent the value V. This can be used to avoid having the get method
177
  /// assert when V is larger than Ty can represent. Note that there are two
178
  /// versions of this method, one for unsigned and one for signed integers.
179
  /// Although ConstantInt canonicalizes everything to an unsigned integer,
180
  /// the signed version avoids callers having to convert a signed quantity
181
  /// to the appropriate unsigned type before calling the method.
182
  /// @returns true if V is a valid value for type Ty
183
  /// Determine if the value is in range for the given type.
184
  static bool isValueValidForType(Type *Ty, uint64_t V);
185
  static bool isValueValidForType(Type *Ty, int64_t V);
186
187
398k
  bool isNegative() const { return Val.isNegative(); }
188
189
  /// This is just a convenience method to make client code smaller for a
190
  /// common code. It also correctly performs the comparison without the
191
  /// potential for an assertion from getZExtValue().
192
706M
  bool isZero() const {
193
706M
    return Val.isNullValue();
194
706M
  }
195
196
  /// This is just a convenience method to make client code smaller for a
197
  /// common case. It also correctly performs the comparison without the
198
  /// potential for an assertion from getZExtValue().
199
  /// Determine if the value is one.
200
39.0M
  bool isOne() const {
201
39.0M
    return Val.isOneValue();
202
39.0M
  }
203
204
  /// This function will return true iff every bit in this constant is set
205
  /// to true.
206
  /// @returns true iff this constant's bits are all set to true.
207
  /// Determine if the value is all ones.
208
23.0M
  bool isMinusOne() const {
209
23.0M
    return Val.isAllOnesValue();
210
23.0M
  }
211
212
  /// This function will return true iff this constant represents the largest
213
  /// value that may be represented by the constant's type.
214
  /// @returns true iff this is the largest value that may be represented
215
  /// by this type.
216
  /// Determine if the value is maximal.
217
118k
  bool isMaxValue(bool isSigned) const {
218
118k
    if (isSigned)
219
40.6k
      return Val.isMaxSignedValue();
220
78.3k
    else
221
78.3k
      return Val.isMaxValue();
222
118k
  }
223
224
  /// This function will return true iff this constant represents the smallest
225
  /// value that may be represented by this constant's type.
226
  /// @returns true if this is the smallest value that may be represented by
227
  /// this type.
228
  /// Determine if the value is minimal.
229
149k
  bool isMinValue(bool isSigned) const {
230
149k
    if (isSigned)
231
70.8k
      return Val.isMinSignedValue();
232
78.3k
    else
233
78.3k
      return Val.isMinValue();
234
149k
  }
235
236
  /// This function will return true iff this constant represents a value with
237
  /// active bits bigger than 64 bits or a value greater than the given uint64_t
238
  /// value.
239
  /// @returns true iff this constant is greater or equal to the given number.
240
  /// Determine if the value is greater or equal to the given number.
241
2.16M
  bool uge(uint64_t Num) const {
242
2.16M
    return Val.uge(Num);
243
2.16M
  }
244
245
  /// getLimitedValue - If the value is smaller than the specified limit,
246
  /// return it, otherwise return the limit value.  This causes the value
247
  /// to saturate to the limit.
248
  /// @returns the min of the value of the constant and the specified value
249
  /// Get the constant's value with a saturation limit
250
83.4M
  uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
251
83.4M
    return Val.getLimitedValue(Limit);
252
83.4M
  }
253
254
  /// Methods to support type inquiry through isa, cast, and dyn_cast.
255
2.74G
  static bool classof(const Value *V) {
256
2.74G
    return V->getValueID() == ConstantIntVal;
257
2.74G
  }
258
};
259
260
//===----------------------------------------------------------------------===//
261
/// ConstantFP - Floating Point Values [float, double]
262
///
263
class ConstantFP final : public ConstantData {
264
  friend class Constant;
265
266
  APFloat Val;
267
268
  ConstantFP(Type *Ty, const APFloat& V);
269
270
  void destroyConstantImpl();
271
272
public:
273
  ConstantFP(const ConstantFP &) = delete;
274
275
  /// Floating point negation must be implemented with f(x) = -0.0 - x. This
276
  /// method returns the negative zero constant for floating point or vector
277
  /// floating point types; for all other types, it returns the null value.
278
  static Constant *getZeroValueForNegation(Type *Ty);
279
280
  /// This returns a ConstantFP, or a vector containing a splat of a ConstantFP,
281
  /// for the specified value in the specified type. This should only be used
282
  /// for simple constant values like 2.0/1.0 etc, that are known-valid both as
283
  /// host double and as the target format.
284
  static Constant *get(Type* Ty, double V);
285
286
  /// If Ty is a vector type, return a Constant with a splat of the given
287
  /// value. Otherwise return a ConstantFP for the given value.
288
  static Constant *get(Type *Ty, const APFloat &V);
289
290
  static Constant *get(Type* Ty, StringRef Str);
291
  static ConstantFP *get(LLVMContext &Context, const APFloat &V);
292
  static Constant *getNaN(Type *Ty, bool Negative = false, uint64_t Payload = 0);
293
  static Constant *getQNaN(Type *Ty, bool Negative = false,
294
                           APInt *Payload = nullptr);
295
  static Constant *getSNaN(Type *Ty, bool Negative = false,
296
                           APInt *Payload = nullptr);
297
  static Constant *getNegativeZero(Type *Ty);
298
  static Constant *getInfinity(Type *Ty, bool Negative = false);
299
300
  /// Return true if Ty is big enough to represent V.
301
  static bool isValueValidForType(Type *Ty, const APFloat &V);
302
5.36M
  inline const APFloat &getValueAPF() const { return Val; }
303
304
  /// Return true if the value is positive or negative zero.
305
772k
  bool isZero() const { return Val.isZero(); }
306
307
  /// Return true if the sign bit is set.
308
156k
  bool isNegative() const { return Val.isNegative(); }
309
310
  /// Return true if the value is infinity
311
29
  bool isInfinity() const { return Val.isInfinity(); }
312
313
  /// Return true if the value is a NaN.
314
56.2k
  bool isNaN() const { return Val.isNaN(); }
315
316
  /// We don't rely on operator== working on double values, as it returns true
317
  /// for things that are clearly not equal, like -0.0 and 0.0.
318
  /// As such, this method can be used to do an exact bit-for-bit comparison of
319
  /// two floating point values.  The version with a double operand is retained
320
  /// because it's so convenient to write isExactlyValue(2.0), but please use
321
  /// it only for simple constants.
322
  bool isExactlyValue(const APFloat &V) const;
323
324
567k
  bool isExactlyValue(double V) const {
325
567k
    bool ignored;
326
567k
    APFloat FV(V);
327
567k
    FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
328
567k
    return isExactlyValue(FV);
329
567k
  }
330
331
  /// Methods for support type inquiry through isa, cast, and dyn_cast:
332
126M
  static bool classof(const Value *V) {
333
126M
    return V->getValueID() == ConstantFPVal;
334
126M
  }
335
};
336
337
//===----------------------------------------------------------------------===//
338
/// All zero aggregate value
339
///
340
class ConstantAggregateZero final : public ConstantData {
341
  friend class Constant;
342
343
  explicit ConstantAggregateZero(Type *Ty)
344
48.8k
      : ConstantData(Ty, ConstantAggregateZeroVal) {}
345
346
  void destroyConstantImpl();
347
348
public:
349
  ConstantAggregateZero(const ConstantAggregateZero &) = delete;
350
351
  static ConstantAggregateZero *get(Type *Ty);
352
353
  /// If this CAZ has array or vector type, return a zero with the right element
354
  /// type.
355
  Constant *getSequentialElement() const;
356
357
  /// If this CAZ has struct type, return a zero with the right element type for
358
  /// the specified element.
359
  Constant *getStructElement(unsigned Elt) const;
360
361
  /// Return a zero of the right value for the specified GEP index if we can,
362
  /// otherwise return null (e.g. if C is a ConstantExpr).
363
  Constant *getElementValue(Constant *C) const;
364
365
  /// Return a zero of the right value for the specified GEP index.
366
  Constant *getElementValue(unsigned Idx) const;
367
368
  /// Return the number of elements in the array, vector, or struct.
369
  unsigned getNumElements() const;
370
371
  /// Methods for support type inquiry through isa, cast, and dyn_cast:
372
  ///
373
688M
  static bool classof(const Value *V) {
374
688M
    return V->getValueID() == ConstantAggregateZeroVal;
375
688M
  }
376
};
377
378
/// Base class for aggregate constants (with operands).
379
///
380
/// These constants are aggregates of other constants, which are stored as
381
/// operands.
382
///
383
/// Subclasses are \a ConstantStruct, \a ConstantArray, and \a
384
/// ConstantVector.
385
///
386
/// \note Some subclasses of \a ConstantData are semantically aggregates --
387
/// such as \a ConstantDataArray -- but are not subclasses of this because they
388
/// use operands.
389
class ConstantAggregate : public Constant {
390
protected:
391
  ConstantAggregate(CompositeType *T, ValueTy VT, ArrayRef<Constant *> V);
392
393
public:
394
  /// Transparently provide more efficient getOperand methods.
395
  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
396
397
  /// Methods for support type inquiry through isa, cast, and dyn_cast:
398
18.9M
  static bool classof(const Value *V) {
399
18.9M
    return V->getValueID() >= ConstantAggregateFirstVal &&
400
18.9M
           
V->getValueID() <= ConstantAggregateLastVal18.8M
;
401
18.9M
  }
402
};
403
404
template <>
405
struct OperandTraits<ConstantAggregate>
406
    : public VariadicOperandTraits<ConstantAggregate> {};
407
408
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantAggregate, Constant)
409
410
//===----------------------------------------------------------------------===//
411
/// ConstantArray - Constant Array Declarations
412
///
413
class ConstantArray final : public ConstantAggregate {
414
  friend struct ConstantAggrKeyType<ConstantArray>;
415
  friend class Constant;
416
417
  ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
418
419
  void destroyConstantImpl();
420
  Value *handleOperandChangeImpl(Value *From, Value *To);
421
422
public:
423
  // ConstantArray accessors
424
  static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
425
426
private:
427
  static Constant *getImpl(ArrayType *T, ArrayRef<Constant *> V);
428
429
public:
430
  /// Specialize the getType() method to always return an ArrayType,
431
  /// which reduces the amount of casting needed in parts of the compiler.
432
78.9k
  inline ArrayType *getType() const {
433
78.9k
    return cast<ArrayType>(Value::getType());
434
78.9k
  }
435
436
  /// Methods for support type inquiry through isa, cast, and dyn_cast:
437
1.15M
  static bool classof(const Value *V) {
438
1.15M
    return V->getValueID() == ConstantArrayVal;
439
1.15M
  }
440
};
441
442
//===----------------------------------------------------------------------===//
443
// Constant Struct Declarations
444
//
445
class ConstantStruct final : public ConstantAggregate {
446
  friend struct ConstantAggrKeyType<ConstantStruct>;
447
  friend class Constant;
448
449
  ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
450
451
  void destroyConstantImpl();
452
  Value *handleOperandChangeImpl(Value *From, Value *To);
453
454
public:
455
  // ConstantStruct accessors
456
  static Constant *get(StructType *T, ArrayRef<Constant*> V);
457
458
  template <typename... Csts>
459
  static typename std::enable_if<are_base_of<Constant, Csts...>::value,
460
                                 Constant *>::type
461
526
  get(StructType *T, Csts *... Vs) {
462
526
    SmallVector<Constant *, 8> Values({Vs...});
463
526
    return get(T, Values);
464
526
  }
std::__1::enable_if<are_base_of<llvm::Constant, llvm::Constant, llvm::Constant, llvm::Constant>::value, llvm::Constant*>::type llvm::ConstantStruct::get<llvm::Constant, llvm::Constant, llvm::Constant>(llvm::StructType*, llvm::Constant*, llvm::Constant*, llvm::Constant*)
Line
Count
Source
461
4
  get(StructType *T, Csts *... Vs) {
462
4
    SmallVector<Constant *, 8> Values({Vs...});
463
4
    return get(T, Values);
464
4
  }
std::__1::enable_if<are_base_of<llvm::Constant, llvm::Constant, llvm::Constant>::value, llvm::Constant*>::type llvm::ConstantStruct::get<llvm::Constant, llvm::Constant>(llvm::StructType*, llvm::Constant*, llvm::Constant*)
Line
Count
Source
461
263
  get(StructType *T, Csts *... Vs) {
462
263
    SmallVector<Constant *, 8> Values({Vs...});
463
263
    return get(T, Values);
464
263
  }
std::__1::enable_if<are_base_of<llvm::Constant, llvm::Constant, llvm::Constant, llvm::Constant, llvm::Constant, llvm::Constant, llvm::Constant, llvm::Constant, llvm::Constant>::value, llvm::Constant*>::type llvm::ConstantStruct::get<llvm::Constant, llvm::Constant, llvm::Constant, llvm::Constant, llvm::Constant, llvm::Constant, llvm::Constant, llvm::Constant>(llvm::StructType*, llvm::Constant*, llvm::Constant*, llvm::Constant*, llvm::Constant*, llvm::Constant*, llvm::Constant*, llvm::Constant*, llvm::Constant*)
Line
Count
Source
461
254
  get(StructType *T, Csts *... Vs) {
462
254
    SmallVector<Constant *, 8> Values({Vs...});
463
254
    return get(T, Values);
464
254
  }
std::__1::enable_if<are_base_of<llvm::Constant, llvm::Constant>::value, llvm::Constant*>::type llvm::ConstantStruct::get<llvm::Constant>(llvm::StructType*, llvm::Constant*)
Line
Count
Source
461
2
  get(StructType *T, Csts *... Vs) {
462
2
    SmallVector<Constant *, 8> Values({Vs...});
463
2
    return get(T, Values);
464
2
  }
std::__1::enable_if<are_base_of<llvm::Constant, llvm::ConstantInt, llvm::Function, llvm::ConstantPointerNull>::value, llvm::Constant*>::type llvm::ConstantStruct::get<llvm::ConstantInt, llvm::Function, llvm::ConstantPointerNull>(llvm::StructType*, llvm::ConstantInt*, llvm::Function*, llvm::ConstantPointerNull*)
Line
Count
Source
461
3
  get(StructType *T, Csts *... Vs) {
462
3
    SmallVector<Constant *, 8> Values({Vs...});
463
3
    return get(T, Values);
464
3
  }
465
466
  /// Return an anonymous struct that has the specified elements.
467
  /// If the struct is possibly empty, then you must specify a context.
468
16.3k
  static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
469
16.3k
    return get(getTypeForElements(V, Packed), V);
470
16.3k
  }
471
  static Constant *getAnon(LLVMContext &Ctx,
472
40
                           ArrayRef<Constant*> V, bool Packed = false) {
473
40
    return get(getTypeForElements(Ctx, V, Packed), V);
474
40
  }
475
476
  /// Return an anonymous struct type to use for a constant with the specified
477
  /// set of elements. The list must not be empty.
478
  static StructType *getTypeForElements(ArrayRef<Constant*> V,
479
                                        bool Packed = false);
480
  /// This version of the method allows an empty list.
481
  static StructType *getTypeForElements(LLVMContext &Ctx,
482
                                        ArrayRef<Constant*> V,
483
                                        bool Packed = false);
484
485
  /// Specialization - reduce amount of casting.
486
463k
  inline StructType *getType() const {
487
463k
    return cast<StructType>(Value::getType());
488
463k
  }
489
490
  /// Methods for support type inquiry through isa, cast, and dyn_cast:
491
685k
  static bool classof(const Value *V) {
492
685k
    return V->getValueID() == ConstantStructVal;
493
685k
  }
494
};
495
496
//===----------------------------------------------------------------------===//
497
/// Constant Vector Declarations
498
///
499
class ConstantVector final : public ConstantAggregate {
500
  friend struct ConstantAggrKeyType<ConstantVector>;
501
  friend class Constant;
502
503
  ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
504
505
  void destroyConstantImpl();
506
  Value *handleOperandChangeImpl(Value *From, Value *To);
507
508
public:
509
  // ConstantVector accessors
510
  static Constant *get(ArrayRef<Constant*> V);
511
512
private:
513
  static Constant *getImpl(ArrayRef<Constant *> V);
514
515
public:
516
  /// Return a ConstantVector with the specified constant in each element.
517
  static Constant *getSplat(unsigned NumElts, Constant *Elt);
518
519
  /// Specialize the getType() method to always return a VectorType,
520
  /// which reduces the amount of casting needed in parts of the compiler.
521
242k
  inline VectorType *getType() const {
522
242k
    return cast<VectorType>(Value::getType());
523
242k
  }
524
525
  /// If this is a splat constant, meaning that all of the elements have the
526
  /// same value, return that value. Otherwise return NULL.
527
  Constant *getSplatValue() const;
528
529
  /// Methods for support type inquiry through isa, cast, and dyn_cast:
530
1.08G
  static bool classof(const Value *V) {
531
1.08G
    return V->getValueID() == ConstantVectorVal;
532
1.08G
  }
533
};
534
535
//===----------------------------------------------------------------------===//
536
/// A constant pointer value that points to null
537
///
538
class ConstantPointerNull final : public ConstantData {
539
  friend class Constant;
540
541
  explicit ConstantPointerNull(PointerType *T)
542
143k
      : ConstantData(T, Value::ConstantPointerNullVal) {}
543
544
  void destroyConstantImpl();
545
546
public:
547
  ConstantPointerNull(const ConstantPointerNull &) = delete;
548
549
  /// Static factory methods - Return objects of the specified value
550
  static ConstantPointerNull *get(PointerType *T);
551
552
  /// Specialize the getType() method to always return an PointerType,
553
  /// which reduces the amount of casting needed in parts of the compiler.
554
361k
  inline PointerType *getType() const {
555
361k
    return cast<PointerType>(Value::getType());
556
361k
  }
557
558
  /// Methods for support type inquiry through isa, cast, and dyn_cast:
559
974M
  static bool classof(const Value *V) {
560
974M
    return V->getValueID() == ConstantPointerNullVal;
561
974M
  }
562
};
563
564
//===----------------------------------------------------------------------===//
565
/// ConstantDataSequential - A vector or array constant whose element type is a
566
/// simple 1/2/4/8-byte integer or float/double, and whose elements are just
567
/// simple data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
568
/// operands because it stores all of the elements of the constant as densely
569
/// packed data, instead of as Value*'s.
570
///
571
/// This is the common base class of ConstantDataArray and ConstantDataVector.
572
///
573
class ConstantDataSequential : public ConstantData {
574
  friend class LLVMContextImpl;
575
  friend class Constant;
576
577
  /// A pointer to the bytes underlying this constant (which is owned by the
578
  /// uniquing StringMap).
579
  const char *DataElements;
580
581
  /// This forms a link list of ConstantDataSequential nodes that have
582
  /// the same value but different type.  For example, 0,0,0,1 could be a 4
583
  /// element array of i8, or a 1-element array of i32.  They'll both end up in
584
  /// the same StringMap bucket, linked up.
585
  ConstantDataSequential *Next;
586
587
  void destroyConstantImpl();
588
589
protected:
590
  explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
591
501k
      : ConstantData(ty, VT), DataElements(Data), Next(nullptr) {}
592
86.6k
  ~ConstantDataSequential() { delete Next; }
593
594
  static Constant *getImpl(StringRef Bytes, Type *Ty);
595
596
public:
597
  ConstantDataSequential(const ConstantDataSequential &) = delete;
598
599
  /// Return true if a ConstantDataSequential can be formed with a vector or
600
  /// array of the specified element type.
601
  /// ConstantDataArray only works with normal float and int types that are
602
  /// stored densely in memory, not with things like i42 or x86_f80.
603
  static bool isElementTypeCompatible(Type *Ty);
604
605
  /// If this is a sequential container of integers (of any size), return the
606
  /// specified element in the low bits of a uint64_t.
607
  uint64_t getElementAsInteger(unsigned i) const;
608
609
  /// If this is a sequential container of integers (of any size), return the
610
  /// specified element as an APInt.
611
  APInt getElementAsAPInt(unsigned i) const;
612
613
  /// If this is a sequential container of floating point type, return the
614
  /// specified element as an APFloat.
615
  APFloat getElementAsAPFloat(unsigned i) const;
616
617
  /// If this is an sequential container of floats, return the specified element
618
  /// as a float.
619
  float getElementAsFloat(unsigned i) const;
620
621
  /// If this is an sequential container of doubles, return the specified
622
  /// element as a double.
623
  double getElementAsDouble(unsigned i) const;
624
625
  /// Return a Constant for a specified index's element.
626
  /// Note that this has to compute a new constant to return, so it isn't as
627
  /// efficient as getElementAsInteger/Float/Double.
628
  Constant *getElementAsConstant(unsigned i) const;
629
630
  /// Specialize the getType() method to always return a SequentialType, which
631
  /// reduces the amount of casting needed in parts of the compiler.
632
264M
  inline SequentialType *getType() const {
633
264M
    return cast<SequentialType>(Value::getType());
634
264M
  }
635
636
  /// Return the element type of the array/vector.
637
  Type *getElementType() const;
638
639
  /// Return the number of elements in the array or vector.
640
  unsigned getNumElements() const;
641
642
  /// Return the size (in bytes) of each element in the array/vector.
643
  /// The size of the elements is known to be a multiple of one byte.
644
  uint64_t getElementByteSize() const;
645
646
  /// This method returns true if this is an array of \p CharSize integers.
647
  bool isString(unsigned CharSize = 8) const;
648
649
  /// This method returns true if the array "isString", ends with a null byte,
650
  /// and does not contains any other null bytes.
651
  bool isCString() const;
652
653
  /// If this array is isString(), then this method returns the array as a
654
  /// StringRef. Otherwise, it asserts out.
655
825k
  StringRef getAsString() const {
656
825k
    assert(isString() && "Not a string");
657
825k
    return getRawDataValues();
658
825k
  }
659
660
  /// If this array is isCString(), then this method returns the array (without
661
  /// the trailing null byte) as a StringRef. Otherwise, it asserts out.
662
4
  StringRef getAsCString() const {
663
4
    assert(isCString() && "Isn't a C string");
664
4
    StringRef Str = getAsString();
665
4
    return Str.substr(0, Str.size()-1);
666
4
  }
667
668
  /// Return the raw, underlying, bytes of this data. Note that this is an
669
  /// extremely tricky thing to work with, as it exposes the host endianness of
670
  /// the data elements.
671
  StringRef getRawDataValues() const;
672
673
  /// Methods for support type inquiry through isa, cast, and dyn_cast:
674
618M
  static bool classof(const Value *V) {
675
618M
    return V->getValueID() == ConstantDataArrayVal ||
676
618M
           
V->getValueID() == ConstantDataVectorVal617M
;
677
618M
  }
678
679
private:
680
  const char *getElementPointer(unsigned Elt) const;
681
};
682
683
//===----------------------------------------------------------------------===//
684
/// An array constant whose element type is a simple 1/2/4/8-byte integer or
685
/// float/double, and whose elements are just simple data values
686
/// (i.e. ConstantInt/ConstantFP). This Constant node has no operands because it
687
/// stores all of the elements of the constant as densely packed data, instead
688
/// of as Value*'s.
689
class ConstantDataArray final : public ConstantDataSequential {
690
  friend class ConstantDataSequential;
691
692
  explicit ConstantDataArray(Type *ty, const char *Data)
693
380k
      : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
694
695
public:
696
  ConstantDataArray(const ConstantDataArray &) = delete;
697
698
  /// get() constructor - Return a constant with array type with an element
699
  /// count and element type matching the ArrayRef passed in.  Note that this
700
  /// can return a ConstantAggregateZero object.
701
  template <typename ElementTy>
702
468k
  static Constant *get(LLVMContext &Context, ArrayRef<ElementTy> Elts) {
703
468k
    const char *Data = reinterpret_cast<const char *>(Elts.data());
704
468k
    return getRaw(StringRef(Data, Elts.size() * sizeof(ElementTy)), Elts.size(),
705
468k
                  Type::getScalarTy<ElementTy>(Context));
706
468k
  }
llvm::Constant* llvm::ConstantDataArray::get<unsigned char>(llvm::LLVMContext&, llvm::ArrayRef<unsigned char>)
Line
Count
Source
702
437k
  static Constant *get(LLVMContext &Context, ArrayRef<ElementTy> Elts) {
703
437k
    const char *Data = reinterpret_cast<const char *>(Elts.data());
704
437k
    return getRaw(StringRef(Data, Elts.size() * sizeof(ElementTy)), Elts.size(),
705
437k
                  Type::getScalarTy<ElementTy>(Context));
706
437k
  }
llvm::Constant* llvm::ConstantDataArray::get<unsigned short>(llvm::LLVMContext&, llvm::ArrayRef<unsigned short>)
Line
Count
Source
702
2.80k
  static Constant *get(LLVMContext &Context, ArrayRef<ElementTy> Elts) {
703
2.80k
    const char *Data = reinterpret_cast<const char *>(Elts.data());
704
2.80k
    return getRaw(StringRef(Data, Elts.size() * sizeof(ElementTy)), Elts.size(),
705
2.80k
                  Type::getScalarTy<ElementTy>(Context));
706
2.80k
  }
llvm::Constant* llvm::ConstantDataArray::get<unsigned int>(llvm::LLVMContext&, llvm::ArrayRef<unsigned int>)
Line
Count
Source
702
18.3k
  static Constant *get(LLVMContext &Context, ArrayRef<ElementTy> Elts) {
703
18.3k
    const char *Data = reinterpret_cast<const char *>(Elts.data());
704
18.3k
    return getRaw(StringRef(Data, Elts.size() * sizeof(ElementTy)), Elts.size(),
705
18.3k
                  Type::getScalarTy<ElementTy>(Context));
706
18.3k
  }
llvm::Constant* llvm::ConstantDataArray::get<unsigned long long>(llvm::LLVMContext&, llvm::ArrayRef<unsigned long long>)
Line
Count
Source
702
9.69k
  static Constant *get(LLVMContext &Context, ArrayRef<ElementTy> Elts) {
703
9.69k
    const char *Data = reinterpret_cast<const char *>(Elts.data());
704
9.69k
    return getRaw(StringRef(Data, Elts.size() * sizeof(ElementTy)), Elts.size(),
705
9.69k
                  Type::getScalarTy<ElementTy>(Context));
706
9.69k
  }
707
708
  /// get() constructor - ArrayTy needs to be compatible with
709
  /// ArrayRef<ElementTy>. Calls get(LLVMContext, ArrayRef<ElementTy>).
710
  template <typename ArrayTy>
711
300k
  static Constant *get(LLVMContext &Context, ArrayTy &Elts) {
712
300k
    return ConstantDataArray::get(Context, makeArrayRef(Elts));
713
300k
  }
llvm::Constant* llvm::ConstantDataArray::get<llvm::SmallVector<unsigned char, 16u> >(llvm::LLVMContext&, llvm::SmallVector<unsigned char, 16u>&)
Line
Count
Source
711
3.11k
  static Constant *get(LLVMContext &Context, ArrayTy &Elts) {
712
3.11k
    return ConstantDataArray::get(Context, makeArrayRef(Elts));
713
3.11k
  }
llvm::Constant* llvm::ConstantDataArray::get<llvm::SmallVector<unsigned short, 16u> >(llvm::LLVMContext&, llvm::SmallVector<unsigned short, 16u>&)
Line
Count
Source
711
2.54k
  static Constant *get(LLVMContext &Context, ArrayTy &Elts) {
712
2.54k
    return ConstantDataArray::get(Context, makeArrayRef(Elts));
713
2.54k
  }
llvm::Constant* llvm::ConstantDataArray::get<llvm::SmallVector<unsigned int, 16u> >(llvm::LLVMContext&, llvm::SmallVector<unsigned int, 16u>&)
Line
Count
Source
711
12.5k
  static Constant *get(LLVMContext &Context, ArrayTy &Elts) {
712
12.5k
    return ConstantDataArray::get(Context, makeArrayRef(Elts));
713
12.5k
  }
llvm::Constant* llvm::ConstantDataArray::get<llvm::SmallVector<unsigned long long, 16u> >(llvm::LLVMContext&, llvm::SmallVector<unsigned long long, 16u>&)
Line
Count
Source
711
5.87k
  static Constant *get(LLVMContext &Context, ArrayTy &Elts) {
712
5.87k
    return ConstantDataArray::get(Context, makeArrayRef(Elts));
713
5.87k
  }
llvm::Constant* llvm::ConstantDataArray::get<llvm::SmallVector<unsigned char, 64u> >(llvm::LLVMContext&, llvm::SmallVector<unsigned char, 64u>&)
Line
Count
Source
711
266k
  static Constant *get(LLVMContext &Context, ArrayTy &Elts) {
712
266k
    return ConstantDataArray::get(Context, makeArrayRef(Elts));
713
266k
  }
llvm::Constant* llvm::ConstantDataArray::get<llvm::SmallVector<unsigned int, 3u> >(llvm::LLVMContext&, llvm::SmallVector<unsigned int, 3u>&)
Line
Count
Source
711
5.44k
  static Constant *get(LLVMContext &Context, ArrayTy &Elts) {
712
5.44k
    return ConstantDataArray::get(Context, makeArrayRef(Elts));
713
5.44k
  }
llvm::Constant* llvm::ConstantDataArray::get<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >(llvm::LLVMContext&, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&)
Line
Count
Source
711
129
  static Constant *get(LLVMContext &Context, ArrayTy &Elts) {
712
129
    return ConstantDataArray::get(Context, makeArrayRef(Elts));
713
129
  }
llvm::Constant* llvm::ConstantDataArray::get<llvm::SmallVector<unsigned long long, 4u> >(llvm::LLVMContext&, llvm::SmallVector<unsigned long long, 4u>&)
Line
Count
Source
711
3.81k
  static Constant *get(LLVMContext &Context, ArrayTy &Elts) {
712
3.81k
    return ConstantDataArray::get(Context, makeArrayRef(Elts));
713
3.81k
  }
llvm::Constant* llvm::ConstantDataArray::get<llvm::SmallVector<unsigned short, 32u> >(llvm::LLVMContext&, llvm::SmallVector<unsigned short, 32u>&)
Line
Count
Source
711
239
  static Constant *get(LLVMContext &Context, ArrayTy &Elts) {
712
239
    return ConstantDataArray::get(Context, makeArrayRef(Elts));
713
239
  }
llvm::Constant* llvm::ConstantDataArray::get<llvm::SmallVector<unsigned int, 32u> >(llvm::LLVMContext&, llvm::SmallVector<unsigned int, 32u>&)
Line
Count
Source
711
402
  static Constant *get(LLVMContext &Context, ArrayTy &Elts) {
712
402
    return ConstantDataArray::get(Context, makeArrayRef(Elts));
713
402
  }
714
715
  /// get() constructor - Return a constant with array type with an element
716
  /// count and element type matching the NumElements and ElementTy parameters
717
  /// passed in. Note that this can return a ConstantAggregateZero object.
718
  /// ElementTy needs to be one of i8/i16/i32/i64/float/double. Data is the
719
  /// buffer containing the elements. Be careful to make sure Data uses the
720
  /// right endianness, the buffer will be used as-is.
721
468k
  static Constant *getRaw(StringRef Data, uint64_t NumElements, Type *ElementTy) {
722
468k
    Type *Ty = ArrayType::get(ElementTy, NumElements);
723
468k
    return getImpl(Data, Ty);
724
468k
  }
725
726
  /// getFP() constructors - Return a constant with array type with an element
727
  /// count and element type of float with precision matching the number of
728
  /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
729
  /// double for 64bits) Note that this can return a ConstantAggregateZero
730
  /// object.
731
  static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
732
  static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
733
  static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
734
735
  /// This method constructs a CDS and initializes it with a text string.
736
  /// The default behavior (AddNull==true) causes a null terminator to
737
  /// be placed at the end of the array (increasing the length of the string by
738
  /// one more than the StringRef would normally indicate.  Pass AddNull=false
739
  /// to disable this behavior.
740
  static Constant *getString(LLVMContext &Context, StringRef Initializer,
741
                             bool AddNull = true);
742
743
  /// Specialize the getType() method to always return an ArrayType,
744
  /// which reduces the amount of casting needed in parts of the compiler.
745
566k
  inline ArrayType *getType() const {
746
566k
    return cast<ArrayType>(Value::getType());
747
566k
  }
748
749
  /// Methods for support type inquiry through isa, cast, and dyn_cast:
750
893k
  static bool classof(const Value *V) {
751
893k
    return V->getValueID() == ConstantDataArrayVal;
752
893k
  }
753
};
754
755
//===----------------------------------------------------------------------===//
756
/// A vector constant whose element type is a simple 1/2/4/8-byte integer or
757
/// float/double, and whose elements are just simple data values
758
/// (i.e. ConstantInt/ConstantFP). This Constant node has no operands because it
759
/// stores all of the elements of the constant as densely packed data, instead
760
/// of as Value*'s.
761
class ConstantDataVector final : public ConstantDataSequential {
762
  friend class ConstantDataSequential;
763
764
  explicit ConstantDataVector(Type *ty, const char *Data)
765
121k
      : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
766
767
public:
768
  ConstantDataVector(const ConstantDataVector &) = delete;
769
770
  /// get() constructors - Return a constant with vector type with an element
771
  /// count and element type matching the ArrayRef passed in.  Note that this
772
  /// can return a ConstantAggregateZero object.
773
  static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
774
  static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
775
  static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
776
  static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
777
  static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
778
  static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
779
780
  /// getFP() constructors - Return a constant with vector type with an element
781
  /// count and element type of float with the precision matching the number of
782
  /// bits in the ArrayRef passed in.  (i.e. half for 16bits, float for 32bits,
783
  /// double for 64bits) Note that this can return a ConstantAggregateZero
784
  /// object.
785
  static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
786
  static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
787
  static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
788
789
  /// Return a ConstantVector with the specified constant in each element.
790
  /// The specified constant has to be a of a compatible type (i8/i16/
791
  /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
792
  static Constant *getSplat(unsigned NumElts, Constant *Elt);
793
794
  /// Returns true if this is a splat constant, meaning that all elements have
795
  /// the same value.
796
  bool isSplat() const;
797
798
  /// If this is a splat constant, meaning that all of the elements have the
799
  /// same value, return that value. Otherwise return NULL.
800
  Constant *getSplatValue() const;
801
802
  /// Specialize the getType() method to always return a VectorType,
803
  /// which reduces the amount of casting needed in parts of the compiler.
804
158
  inline VectorType *getType() const {
805
158
    return cast<VectorType>(Value::getType());
806
158
  }
807
808
  /// Methods for support type inquiry through isa, cast, and dyn_cast:
809
63.1M
  static bool classof(const Value *V) {
810
63.1M
    return V->getValueID() == ConstantDataVectorVal;
811
63.1M
  }
812
};
813
814
//===----------------------------------------------------------------------===//
815
/// A constant token which is empty
816
///
817
class ConstantTokenNone final : public ConstantData {
818
  friend class Constant;
819
820
  explicit ConstantTokenNone(LLVMContext &Context)
821
332
      : ConstantData(Type::getTokenTy(Context), ConstantTokenNoneVal) {}
822
823
  void destroyConstantImpl();
824
825
public:
826
  ConstantTokenNone(const ConstantTokenNone &) = delete;
827
828
  /// Return the ConstantTokenNone.
829
  static ConstantTokenNone *get(LLVMContext &Context);
830
831
  /// Methods to support type inquiry through isa, cast, and dyn_cast.
832
44.2M
  static bool classof(const Value *V) {
833
44.2M
    return V->getValueID() == ConstantTokenNoneVal;
834
44.2M
  }
835
};
836
837
/// The address of a basic block.
838
///
839
class BlockAddress final : public Constant {
840
  friend class Constant;
841
842
  BlockAddress(Function *F, BasicBlock *BB);
843
844
1.00k
  void *operator new(size_t s) { return User::operator new(s, 2); }
845
846
  void destroyConstantImpl();
847
  Value *handleOperandChangeImpl(Value *From, Value *To);
848
849
public:
850
  /// Return a BlockAddress for the specified function and basic block.
851
  static BlockAddress *get(Function *F, BasicBlock *BB);
852
853
  /// Return a BlockAddress for the specified basic block.  The basic
854
  /// block must be embedded into a function.
855
  static BlockAddress *get(BasicBlock *BB);
856
857
  /// Lookup an existing \c BlockAddress constant for the given BasicBlock.
858
  ///
859
  /// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress.
860
  static BlockAddress *lookup(const BasicBlock *BB);
861
862
  /// Transparently provide more efficient getOperand methods.
863
  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
864
865
3.00k
  Function *getFunction() const { return (Function*)Op<0>().get(); }
866
3.11k
  BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
867
868
  /// Methods for support type inquiry through isa, cast, and dyn_cast:
869
11.6M
  static bool classof(const Value *V) {
870
11.6M
    return V->getValueID() == BlockAddressVal;
871
11.6M
  }
872
};
873
874
template <>
875
struct OperandTraits<BlockAddress> :
876
  public FixedNumOperandTraits<BlockAddress, 2> {
877
};
878
879
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
880
881
//===----------------------------------------------------------------------===//
882
/// A constant value that is initialized with an expression using
883
/// other constant values.
884
///
885
/// This class uses the standard Instruction opcodes to define the various
886
/// constant expressions.  The Opcode field for the ConstantExpr class is
887
/// maintained in the Value::SubclassData field.
888
class ConstantExpr : public Constant {
889
  friend struct ConstantExprKeyType;
890
  friend class Constant;
891
892
  void destroyConstantImpl();
893
  Value *handleOperandChangeImpl(Value *From, Value *To);
894
895
protected:
896
  ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
897
808k
      : Constant(ty, ConstantExprVal, Ops, NumOps) {
898
808k
    // Operation type (an Instruction opcode) is stored as the SubclassData.
899
808k
    setValueSubclassData(Opcode);
900
808k
  }
901
902
public:
903
  // Static methods to construct a ConstantExpr of different kinds.  Note that
904
  // these methods may return a object that is not an instance of the
905
  // ConstantExpr class, because they will attempt to fold the constant
906
  // expression into something simpler if possible.
907
908
  /// getAlignOf constant expr - computes the alignment of a type in a target
909
  /// independent way (Note: the return type is an i64).
910
  static Constant *getAlignOf(Type *Ty);
911
912
  /// getSizeOf constant expr - computes the (alloc) size of a type (in
913
  /// address-units, not bits) in a target independent way (Note: the return
914
  /// type is an i64).
915
  ///
916
  static Constant *getSizeOf(Type *Ty);
917
918
  /// getOffsetOf constant expr - computes the offset of a struct field in a
919
  /// target independent way (Note: the return type is an i64).
920
  ///
921
  static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
922
923
  /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
924
  /// which supports any aggregate type, and any Constant index.
925
  ///
926
  static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
927
928
  static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
929
  static Constant *getFNeg(Constant *C);
930
  static Constant *getNot(Constant *C);
931
  static Constant *getAdd(Constant *C1, Constant *C2,
932
                          bool HasNUW = false, bool HasNSW = false);
933
  static Constant *getFAdd(Constant *C1, Constant *C2);
934
  static Constant *getSub(Constant *C1, Constant *C2,
935
                          bool HasNUW = false, bool HasNSW = false);
936
  static Constant *getFSub(Constant *C1, Constant *C2);
937
  static Constant *getMul(Constant *C1, Constant *C2,
938
                          bool HasNUW = false, bool HasNSW = false);
939
  static Constant *getFMul(Constant *C1, Constant *C2);
940
  static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
941
  static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
942
  static Constant *getFDiv(Constant *C1, Constant *C2);
943
  static Constant *getURem(Constant *C1, Constant *C2);
944
  static Constant *getSRem(Constant *C1, Constant *C2);
945
  static Constant *getFRem(Constant *C1, Constant *C2);
946
  static Constant *getAnd(Constant *C1, Constant *C2);
947
  static Constant *getOr(Constant *C1, Constant *C2);
948
  static Constant *getXor(Constant *C1, Constant *C2);
949
  static Constant *getShl(Constant *C1, Constant *C2,
950
                          bool HasNUW = false, bool HasNSW = false);
951
  static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
952
  static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
953
  static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false);
954
  static Constant *getSExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
955
  static Constant *getZExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
956
  static Constant *getFPTrunc(Constant *C, Type *Ty,
957
                              bool OnlyIfReduced = false);
958
  static Constant *getFPExtend(Constant *C, Type *Ty,
959
                               bool OnlyIfReduced = false);
960
  static Constant *getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
961
  static Constant *getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
962
  static Constant *getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
963
  static Constant *getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
964
  static Constant *getPtrToInt(Constant *C, Type *Ty,
965
                               bool OnlyIfReduced = false);
966
  static Constant *getIntToPtr(Constant *C, Type *Ty,
967
                               bool OnlyIfReduced = false);
968
  static Constant *getBitCast(Constant *C, Type *Ty,
969
                              bool OnlyIfReduced = false);
970
  static Constant *getAddrSpaceCast(Constant *C, Type *Ty,
971
                                    bool OnlyIfReduced = false);
972
973
0
  static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
974
0
  static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
975
976
8
  static Constant *getNSWAdd(Constant *C1, Constant *C2) {
977
8
    return getAdd(C1, C2, false, true);
978
8
  }
979
980
0
  static Constant *getNUWAdd(Constant *C1, Constant *C2) {
981
0
    return getAdd(C1, C2, true, false);
982
0
  }
983
984
0
  static Constant *getNSWSub(Constant *C1, Constant *C2) {
985
0
    return getSub(C1, C2, false, true);
986
0
  }
987
988
0
  static Constant *getNUWSub(Constant *C1, Constant *C2) {
989
0
    return getSub(C1, C2, true, false);
990
0
  }
991
992
0
  static Constant *getNSWMul(Constant *C1, Constant *C2) {
993
0
    return getMul(C1, C2, false, true);
994
0
  }
995
996
134
  static Constant *getNUWMul(Constant *C1, Constant *C2) {
997
134
    return getMul(C1, C2, true, false);
998
134
  }
999
1000
0
  static Constant *getNSWShl(Constant *C1, Constant *C2) {
1001
0
    return getShl(C1, C2, false, true);
1002
0
  }
1003
1004
1
  static Constant *getNUWShl(Constant *C1, Constant *C2) {
1005
1
    return getShl(C1, C2, true, false);
1006
1
  }
1007
1008
0
  static Constant *getExactSDiv(Constant *C1, Constant *C2) {
1009
0
    return getSDiv(C1, C2, true);
1010
0
  }
1011
1012
1
  static Constant *getExactUDiv(Constant *C1, Constant *C2) {
1013
1
    return getUDiv(C1, C2, true);
1014
1
  }
1015
1016
0
  static Constant *getExactAShr(Constant *C1, Constant *C2) {
1017
0
    return getAShr(C1, C2, true);
1018
0
  }
1019
1020
0
  static Constant *getExactLShr(Constant *C1, Constant *C2) {
1021
0
    return getLShr(C1, C2, true);
1022
0
  }
1023
1024
  /// Return the identity constant for a binary opcode.
1025
  /// The identity constant C is defined as X op C = X and C op X = X for every
1026
  /// X when the binary operation is commutative. If the binop is not
1027
  /// commutative, callers can acquire the operand 1 identity constant by
1028
  /// setting AllowRHSConstant to true. For example, any shift has a zero
1029
  /// identity constant for operand 1: X shift 0 = X.
1030
  /// Return nullptr if the operator does not have an identity constant.
1031
  static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty,
1032
                                    bool AllowRHSConstant = false);
1033
1034
  /// Return the absorbing element for the given binary
1035
  /// operation, i.e. a constant C such that X op C = C and C op X = C for
1036
  /// every X.  For example, this returns zero for integer multiplication.
1037
  /// It returns null if the operator doesn't have an absorbing element.
1038
  static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
1039
1040
  /// Transparently provide more efficient getOperand methods.
1041
  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
1042
1043
  /// Convenience function for getting a Cast operation.
1044
  ///
1045
  /// \param ops The opcode for the conversion
1046
  /// \param C  The constant to be converted
1047
  /// \param Ty The type to which the constant is converted
1048
  /// \param OnlyIfReduced see \a getWithOperands() docs.
1049
  static Constant *getCast(unsigned ops, Constant *C, Type *Ty,
1050
                           bool OnlyIfReduced = false);
1051
1052
  // Create a ZExt or BitCast cast constant expression
1053
  static Constant *getZExtOrBitCast(
1054
    Constant *C,   ///< The constant to zext or bitcast
1055
    Type *Ty ///< The type to zext or bitcast C to
1056
  );
1057
1058
  // Create a SExt or BitCast cast constant expression
1059
  static Constant *getSExtOrBitCast(
1060
    Constant *C,   ///< The constant to sext or bitcast
1061
    Type *Ty ///< The type to sext or bitcast C to
1062
  );
1063
1064
  // Create a Trunc or BitCast cast constant expression
1065
  static Constant *getTruncOrBitCast(
1066
    Constant *C,   ///< The constant to trunc or bitcast
1067
    Type *Ty ///< The type to trunc or bitcast C to
1068
  );
1069
1070
  /// Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant
1071
  /// expression.
1072
  static Constant *getPointerCast(
1073
    Constant *C,   ///< The pointer value to be casted (operand 0)
1074
    Type *Ty ///< The type to which cast should be made
1075
  );
1076
1077
  /// Create a BitCast or AddrSpaceCast for a pointer type depending on
1078
  /// the address space.
1079
  static Constant *getPointerBitCastOrAddrSpaceCast(
1080
    Constant *C,   ///< The constant to addrspacecast or bitcast
1081
    Type *Ty ///< The type to bitcast or addrspacecast C to
1082
  );
1083
1084
  /// Create a ZExt, Bitcast or Trunc for integer -> integer casts
1085
  static Constant *getIntegerCast(
1086
    Constant *C,    ///< The integer constant to be casted
1087
    Type *Ty, ///< The integer type to cast to
1088
    bool isSigned   ///< Whether C should be treated as signed or not
1089
  );
1090
1091
  /// Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
1092
  static Constant *getFPCast(
1093
    Constant *C,    ///< The integer constant to be casted
1094
    Type *Ty ///< The integer type to cast to
1095
  );
1096
1097
  /// Return true if this is a convert constant expression
1098
  bool isCast() const;
1099
1100
  /// Return true if this is a compare constant expression
1101
  bool isCompare() const;
1102
1103
  /// Return true if this is an insertvalue or extractvalue expression,
1104
  /// and the getIndices() method may be used.
1105
  bool hasIndices() const;
1106
1107
  /// Return true if this is a getelementptr expression and all
1108
  /// the index operands are compile-time known integers within the
1109
  /// corresponding notional static array extents. Note that this is
1110
  /// not equivalant to, a subset of, or a superset of the "inbounds"
1111
  /// property.
1112
  bool isGEPWithNoNotionalOverIndexing() const;
1113
1114
  /// Select constant expr
1115
  ///
1116
  /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1117
  static Constant *getSelect(Constant *C, Constant *V1, Constant *V2,
1118
                             Type *OnlyIfReducedTy = nullptr);
1119
1120
  /// get - Return a unary operator constant expression,
1121
  /// folding if possible.
1122
  ///
1123
  /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1124
  static Constant *get(unsigned Opcode, Constant *C1, unsigned Flags = 0, 
1125
                       Type *OnlyIfReducedTy = nullptr);
1126
1127
  /// get - Return a binary or shift operator constant expression,
1128
  /// folding if possible.
1129
  ///
1130
  /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1131
  static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
1132
                       unsigned Flags = 0, Type *OnlyIfReducedTy = nullptr);
1133
1134
  /// Return an ICmp or FCmp comparison operator constant expression.
1135
  ///
1136
  /// \param OnlyIfReduced see \a getWithOperands() docs.
1137
  static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2,
1138
                              bool OnlyIfReduced = false);
1139
1140
  /// get* - Return some common constants without having to
1141
  /// specify the full Instruction::OPCODE identifier.
1142
  ///
1143
  static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS,
1144
                           bool OnlyIfReduced = false);
1145
  static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS,
1146
                           bool OnlyIfReduced = false);
1147
1148
  /// Getelementptr form.  Value* is only accepted for convenience;
1149
  /// all elements must be Constants.
1150
  ///
1151
  /// \param InRangeIndex the inrange index if present or None.
1152
  /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1153
  static Constant *getGetElementPtr(Type *Ty, Constant *C,
1154
                                    ArrayRef<Constant *> IdxList,
1155
                                    bool InBounds = false,
1156
                                    Optional<unsigned> InRangeIndex = None,
1157
13.6M
                                    Type *OnlyIfReducedTy = nullptr) {
1158
13.6M
    return getGetElementPtr(
1159
13.6M
        Ty, C, makeArrayRef((Value * const *)IdxList.data(), IdxList.size()),
1160
13.6M
        InBounds, InRangeIndex, OnlyIfReducedTy);
1161
13.6M
  }
1162
  static Constant *getGetElementPtr(Type *Ty, Constant *C, Constant *Idx,
1163
                                    bool InBounds = false,
1164
                                    Optional<unsigned> InRangeIndex = None,
1165
20.5k
                                    Type *OnlyIfReducedTy = nullptr) {
1166
20.5k
    // This form of the function only exists to avoid ambiguous overload
1167
20.5k
    // warnings about whether to convert Idx to ArrayRef<Constant *> or
1168
20.5k
    // ArrayRef<Value *>.
1169
20.5k
    return getGetElementPtr(Ty, C, cast<Value>(Idx), InBounds, InRangeIndex,
1170
20.5k
                            OnlyIfReducedTy);
1171
20.5k
  }
1172
  static Constant *getGetElementPtr(Type *Ty, Constant *C,
1173
                                    ArrayRef<Value *> IdxList,
1174
                                    bool InBounds = false,
1175
                                    Optional<unsigned> InRangeIndex = None,
1176
                                    Type *OnlyIfReducedTy = nullptr);
1177
1178
  /// Create an "inbounds" getelementptr. See the documentation for the
1179
  /// "inbounds" flag in LangRef.html for details.
1180
  static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1181
6.79k
                                            ArrayRef<Constant *> IdxList) {
1182
6.79k
    return getGetElementPtr(Ty, C, IdxList, true);
1183
6.79k
  }
1184
  static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1185
3.87k
                                            Constant *Idx) {
1186
3.87k
    // This form of the function only exists to avoid ambiguous overload
1187
3.87k
    // warnings about whether to convert Idx to ArrayRef<Constant *> or
1188
3.87k
    // ArrayRef<Value *>.
1189
3.87k
    return getGetElementPtr(Ty, C, Idx, true);
1190
3.87k
  }
1191
  static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1192
231k
                                            ArrayRef<Value *> IdxList) {
1193
231k
    return getGetElementPtr(Ty, C, IdxList, true);
1194
231k
  }
1195
1196
  static Constant *getExtractElement(Constant *Vec, Constant *Idx,
1197
                                     Type *OnlyIfReducedTy = nullptr);
1198
  static Constant *getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx,
1199
                                    Type *OnlyIfReducedTy = nullptr);
1200
  static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask,
1201
                                    Type *OnlyIfReducedTy = nullptr);
1202
  static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
1203
                                   Type *OnlyIfReducedTy = nullptr);
1204
  static Constant *getInsertValue(Constant *Agg, Constant *Val,
1205
                                  ArrayRef<unsigned> Idxs,
1206
                                  Type *OnlyIfReducedTy = nullptr);
1207
1208
  /// Return the opcode at the root of this constant expression
1209
349M
  unsigned getOpcode() const { return getSubclassDataFromValue(); }
1210
1211
  /// Return the ICMP or FCMP predicate value. Assert if this is not an ICMP or
1212
  /// FCMP constant expression.
1213
  unsigned getPredicate() const;
1214
1215
  /// Assert that this is an insertvalue or exactvalue
1216
  /// expression and return the list of indices.
1217
  ArrayRef<unsigned> getIndices() const;
1218
1219
  /// Return a string representation for an opcode.
1220
  const char *getOpcodeName() const;
1221
1222
  /// Return a constant expression identical to this one, but with the specified
1223
  /// operand set to the specified value.
1224
  Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
1225
1226
  /// This returns the current constant expression with the operands replaced
1227
  /// with the specified values. The specified array must have the same number
1228
  /// of operands as our current one.
1229
3.28k
  Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
1230
3.28k
    return getWithOperands(Ops, getType());
1231
3.28k
  }
1232
1233
  /// Get the current expression with the operands replaced.
1234
  ///
1235
  /// Return the current constant expression with the operands replaced with \c
1236
  /// Ops and the type with \c Ty.  The new operands must have the same number
1237
  /// as the current ones.
1238
  ///
1239
  /// If \c OnlyIfReduced is \c true, nullptr will be returned unless something
1240
  /// gets constant-folded, the type changes, or the expression is otherwise
1241
  /// canonicalized.  This parameter should almost always be \c false.
1242
  Constant *getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
1243
                            bool OnlyIfReduced = false,
1244
                            Type *SrcTy = nullptr) const;
1245
1246
  /// Returns an Instruction which implements the same operation as this
1247
  /// ConstantExpr. The instruction is not linked to any basic block.
1248
  ///
1249
  /// A better approach to this could be to have a constructor for Instruction
1250
  /// which would take a ConstantExpr parameter, but that would have spread
1251
  /// implementation details of ConstantExpr outside of Constants.cpp, which
1252
  /// would make it harder to remove ConstantExprs altogether.
1253
  Instruction *getAsInstruction();
1254
1255
  /// Methods for support type inquiry through isa, cast, and dyn_cast:
1256
3.61G
  static bool classof(const Value *V) {
1257
3.61G
    return V->getValueID() == ConstantExprVal;
1258
3.61G
  }
1259
1260
private:
1261
  // Shadow Value::setValueSubclassData with a private forwarding method so that
1262
  // subclasses cannot accidentally use it.
1263
808k
  void setValueSubclassData(unsigned short D) {
1264
808k
    Value::setValueSubclassData(D);
1265
808k
  }
1266
};
1267
1268
template <>
1269
struct OperandTraits<ConstantExpr> :
1270
  public VariadicOperandTraits<ConstantExpr, 1> {
1271
};
1272
1273
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
1274
1275
//===----------------------------------------------------------------------===//
1276
/// 'undef' values are things that do not have specified contents.
1277
/// These are used for a variety of purposes, including global variable
1278
/// initializers and operands to instructions.  'undef' values can occur with
1279
/// any first-class type.
1280
///
1281
/// Undef values aren't exactly constants; if they have multiple uses, they
1282
/// can appear to have different bit patterns at each use. See
1283
/// LangRef.html#undefvalues for details.
1284
///
1285
class UndefValue final : public ConstantData {
1286
  friend class Constant;
1287
1288
215k
  explicit UndefValue(Type *T) : ConstantData(T, UndefValueVal) {}
1289
1290
  void destroyConstantImpl();
1291
1292
public:
1293
  UndefValue(const UndefValue &) = delete;
1294
1295
  /// Static factory methods - Return an 'undef' object of the specified type.
1296
  static UndefValue *get(Type *T);
1297
1298
  /// If this Undef has array or vector type, return a undef with the right
1299
  /// element type.
1300
  UndefValue *getSequentialElement() const;
1301
1302
  /// If this undef has struct type, return a undef with the right element type
1303
  /// for the specified element.
1304
  UndefValue *getStructElement(unsigned Elt) const;
1305
1306
  /// Return an undef of the right value for the specified GEP index if we can,
1307
  /// otherwise return null (e.g. if C is a ConstantExpr).
1308
  UndefValue *getElementValue(Constant *C) const;
1309
1310
  /// Return an undef of the right value for the specified GEP index.
1311
  UndefValue *getElementValue(unsigned Idx) const;
1312
1313
  /// Return the number of elements in the array, vector, or struct.
1314
  unsigned getNumElements() const;
1315
1316
  /// Methods for support type inquiry through isa, cast, and dyn_cast:
1317
1.41G
  static bool classof(const Value *V) {
1318
1.41G
    return V->getValueID() == UndefValueVal;
1319
1.41G
  }
1320
};
1321
1322
} // end namespace llvm
1323
1324
#endif // LLVM_IR_CONSTANTS_H