Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a ConstantInt in LLVM?

Tags:

llvm

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

I'm thinking it has to be along the lines of

ConstantInt consVal = new ConstantInt(something here).

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

like image 606
winepretzel Avatar asked Apr 27 '13 00:04

winepretzel


2 Answers

Most things in LLVM are created through a static method call instead of directly using a constructor. One reason is that an existing object can be returned instead of creating a new instance.

The static members of ConstantInt have a number of creation methods. You're probably most interested in get (Type *Ty, uint64_t V, bool isSigned=false) and, if you don't already have an integer type, IntegerType::get (LLVMContext &C, unsigned NumBits).

like image 165
Geoff Reedy Avatar answered Oct 24 '22 14:10

Geoff Reedy


To make an a 32 bit integer:

llvm::ConstantInt::get(context, llvm::APInt(/*nbits*/32, value, /*bool*/is_signed));
like image 42
cktan Avatar answered Oct 24 '22 15:10

cktan