layout: page |
title: Constant (llvm.core) |
Constant-s represents constants that appear within the code. The values of such objects are known at creation time. Constants can be created from Python constants. A constant expression is also a constant – given a Constant object, an operation (like addition, subtraction etc) can be specified, to yield a new Constant object. Let’s see some examples:
#!/usr/bin/env python
ti = Type.int() # a 32-bit int type
k1 = Constant.int(ti, 42) # "int k1 = 42;" k2 = k1.add( Constant.int(
ti, 10 ) ) # "int k2 = k1 + 10;"
tr = Type.float()
r1 = Constant.real(tr, "3.141592") # create from a string
r2 = Constant.real(tr, 1.61803399) # create from a Python float
# llvm.core.Constant - This will become a table of contents (this text will be scraped). {:toc}
A null value (all zeros) of type ty
All 1’s value of type ty
An undefined value of type ty
Integer of type ty, with value value (a Python int or long)
Integer of signed type ty (use for signed types)
Floating point value of type ty, with value value (a Python float)
A null-terminated string. value is a Python string
As string(ty), but not null terminated
Array of type ty, initialized with consts (an iterable yielding Constant objects of the appropriate type)
Struct (unpacked) of type ty, initialized with consts (an iterable yielding Constant objects of the appropriate type)
As struct(ty, consts) but packed
Vector, initialized with consts (an iterable yielding Constant objects of the appropriate type)
Constant value representing the sizeof the type ty
The following operations on constants are supported. For more details on any operation, consult the Constant Expressions section of the LLVM Language Reference.
negation, same as 0 - k
1’s complement of k. Note trailing underscore.
k + k2, where k and k2 are integers.
k + k2, where k and k2 are floating-point.
k - k2, where k and k2 are integers.
k - k2, where k and k2 are floating-point.
k * k2, where k and k2 are integers.
k * k2, where k and k2 are floating-point.
Quotient of unsigned division of k with k2
Quotient of signed division of k with k2
Quotient of floating point division of k with k2
Reminder of unsigned division of k with k2
Reminder of signed division of k with k2
Reminder of floating point division of k with k2
Bitwise and of k and k2. Note trailing underscore.
Bitwise or of k and k2. Note trailing underscore.
Bitwise exclusive-or of k and k2.
Compare k with k2 using the predicate icmp. See here for list of predicates for integer operands.
Compare k with k2 using the predicate fcmp. See here for list of predicates for real operands.
Shift k left by k2 bits.
Shift k logically right by k2 bits (new bits are 0s).
Shift k arithmetically right by k2 bits (new bits are same as previous sign bit).
Truncate k to a type ty of lower bitwidth.
Sign extend k to a type ty of higher bitwidth, while extending the sign bit.
Sign extend k to a type ty of higher bitwidth, all new bits are 0s.
Truncate floating point constant k to floating point type ty of lower size than k’s.
Extend floating point constant k to floating point type ty of higher size than k’s.
Convert an unsigned integer constant k to floating point constant of type ty.
Convert a signed integer constant k to floating point constant of type ty.
Convert a floating point constant k to an unsigned integer constant of type ty.
Convert a floating point constant k to a signed integer constant of type ty.
Convert a pointer constant k to an integer constant of type ty.
Convert an integer constant k to a pointer constant of type ty.
Convert k to a (equal-width) constant of type ty.
Replace value with k2 if the 1-bit integer constant cond is 1, else with k3.
Extract value at idx (integer constant) from a vector constant k.
Insert value k2 (scalar constant) at index idx (integer constant) of vector constant k.
Shuffle vector constant k based on vector constants k2 and mask.
# Other Constant Classes The following subclasses of Constant do not provide additional methods, they serve only to provide richer type information.
Subclass | LLVM C++ Class | Remarks | ———|—————-|———| ConstantExpr | llvmConstantExpr | A constant expression | ConstantAggregateZero| llvmConstantAggregateZero| All-zero constant | ConstantInt| llvmConstantInt | An integer constant | ConstantFP| llvmConstantFP| A floating-point constant | ConstantArray| llvmConstantArray | An array constant | ConstantStruct| llvmConstantStruct| A structure constant | ConstantVector| llvmConstantVector | A vector constant | ConstantPointerNull| llvmConstantPointerNull| All-zero pointer constant | UndefValue| llvmUndefValue| corresponds to undef of LLVM IR |
These types are helpful in isinstance checks, like so:
{% highlight python %} ti = Type.int(32) k1 = Constant.int(ti, 42) # int32_t k1 = 42; k2 = Constant.array(ti, [k1, k1]) # int32_t k2[] = { k1, k1 };
assert isinstance(k1, ConstantInt) assert isinstance(k2, ConstantArray)