kamkow1
kamkow1

Reputation: 501

LLVM: simple way to cast types?

I'm looking for a simple way to cast types using the LLVM C++ api. What I'm trying to avoid is endless nested if-else checks what will pick the right IR cast instruction. Here's an example code that I'd like to get working:

int x = 0;
int64_t y = (int64_t) x;

Is there a way to cast types without checking what type it is? Obviously a type conversion from a struct to a float is stupid and should fail but how can I simply pick the right instruction?

Also I'm using this as a reference: https://mapping-high-level-constructs-to-llvm-ir.readthedocs.io/en/latest/basic-constructs/casts.html because the official LLVM docs aren't very helpful.

Upvotes: 0

Views: 721

Answers (1)

Nick Lewycky
Nick Lewycky

Reputation: 1342

The different instructions do different things, as in, produce different bits in the output. There are utility functions like "CastInst::CreateIntegerCast" and "CastInst::CreateTruncOrBitCast" which might be the sort of thing you want? See llvm.org/doxygen/classllvm_1_1CastInst.html .

Upvotes: 1

Related Questions