LLVM get constant integer back from Value*

前端 未结 2 1545
失恋的感觉
失恋的感觉 2020-12-09 08:45

I create a llvm::Value* from a integer constant like this:

llvm::Value* constValue = llvm::ConstantInt::get( llvmContext , llvm::APInt( node->someInt() ))         


        
2条回答
  •  感情败类
    2020-12-09 09:06

    Given llvm::Value* foo and you know that foo is actually a ConstantInt, I believe that the idiomatic LLVM code approach is to use dyn_cast as follows:

    if (llvm::ConstantInt* CI = dyn_cast(foo)) {
      // foo indeed is a ConstantInt, we can use CI here
    }
    else {
      // foo was not actually a ConstantInt
    }
    

    If you're absolutely sure that foo is a ConstantInt and are ready to be hit with an assertion failure if it isn't, you can use cast instead of dyn_cast.


    P.S. Do note that cast and dyn_cast are part of LLVM's own implementation of RTTI. dyn_cast acts somewhat similarly to the standard C++ dynamic_cast, though there are differences in implementation and performance (as can be read here).

提交回复
热议问题