LLVM get constant integer back from Value*

前端 未结 2 1554
失恋的感觉
失恋的感觉 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:09

    Eli's answer is great, but it's missing the final part, which is getting the integer back. The full picture should look like this:

    if (ConstantInt* CI = dyn_cast(Val)) {
      if (CI->getBitWidth() <= 32) {
        constIntValue = CI->getSExtValue();
      }
    }
    

    Of course, you can also change it to <= 64 if constIntValue is a 64-bit integer, etc.

    And as Eli wrote, if you are confident the value is indeed of type ConstInt, you can use cast instead of dyn_cast.

提交回复
热议问题