Question

i am trying to extract constants from the LLVM IR for further analysis. So far i have been able to extract ints, floats and doubles by using the appropriate methods.

How can i differentiate between floats and doubles before trying to get the value from the methods in the APF class. Without an appropriate check i end up triggering an assert when i invoke convertToFloat() on a double or a convertToDouble() on a float. Is there some indirect mechanism in LLVM to distinguish between the datatypes before trying to get the value?

Was it helpful?

Solution

There are several ways, the simplest one I can think of is by using the getSemantics method:

bool IsFloat = MyFloat.getSemantics() == &APFloat::IEEEsingle;
bool IsDouble = MyFloat.getSemantics() == &APFloat::IEEEdouble;

By the way, it's more common to just check the type of the Value that the APFloat came from, if you have it:

bool IsFloat = MyValue.getType()->isFloatTy();
bool IsDouble = MyValue.getType()->isDoubleTy();

OTHER TIPS

If you know the size of each type then you can use a substitute function for sizeof() of the C Language as described in this link: http://nondot.org/sabre/LLVMNotes/SizeOf-OffsetOf-VariableSizedStructs.txt

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top