How to create ConstantInt in LLVM?

I am not sure how to create ConstantInt in LLVM. I know the number that I would like to create, but I'm not sure how I can make ConstantInt representing this number; I can't seem to find the constructor that I need in the documentation.

I think it should be line by line

ConstantInt consVal = new ConstantInt(something here).

I know that I want it to be an int type, and I know my value ... I just want to create a number!

+5
source share
4 answers

Most things in LLVM are created by calling a static method instead of using the constructor directly. One reason is that an existing object may be returned instead of creating a new instance.

ConstantInt . , , get (Type *Ty, uint64_t V, bool isSigned=false) , , IntegerType::get (LLVMContext &C, unsigned NumBits).

+7

32- :

llvm::ConstantInt::get(context, llvm::APInt(/*nbits*/32, value, /*bool*/is_signed));
+3
ConstantInt* const_int32  = ConstantInt::get( Context , APInt(32, StringRef("10"), 10));

where, APInt (32, StringRef ("10"), 10); gets int value from string "10" with base 10.

+1
source

To create an 32-bitinteger constant :

llvm::Type *i32_type = llvm::IntegerType::getInt32Ty(llvm_context);
llvm::Constant *i32_val = llvm::ConstantInt::get(i32_type, -1/*value*/, true);
0
source

All Articles