LLVM get constant integer back from Value*

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<ConstantInt>(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<ConstantInt> instead of dyn_cast.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)