How to create a ConstantInt in LLVM?

青春壹個敷衍的年華 提交于 2019-12-03 02:58:26

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).

To make an a 32 bit integer:

llvm::ConstantInt::get(context, llvm::APInt(/*nbits*/32, value, /*bool*/is_signed));

To create a 32-bit integer constant:

llvm::Type *i32_type = llvm::IntegerType::getInt32Ty(llvm_context);
llvm::Constant *i32_val = llvm::ConstantInt::get(i32_type, -1/*value*/, true);
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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!