When I was reading LLVM IR code (transformed from C), I saw an instruction like this:
%div = sdiv i32 %add, %32
The C code of this instruction may be like this:
a = c / b;
We can see that there is a type i32
in this instruction. This is because of the type of variables a, b, c
are int
. And my computer is x86.
Now, if I have a machine where the integer is about 8 bits and I write the C code same as above and transform C to LLVM IR on this machine, the LLVM IR instruction might be %div = sdiv i8 %add, %32
.
Is this right? If yes, how can we say that the LLVM IR is a machine independent language?
I thought that if a piece of code is in a machine independent language, then this code can run on any machine in this world, and when the code is running, the virtual machine or other things will handle the differences between architectures.
Yes, LLVM IR is a machine independent language.
But the IR code could not be run directly on real hardware. In order to run the IR code, a retarget process is needed. During 'retarget', the machine independent IR code is translated to machine dependent code for the target(x86, MIPS, aarch, 8bit chip and so on).