How to generate code for in-register named local values in LLVM IR? -
i generating llvm ir (.ll files) source language. language doesn't have mutable local variables , don't use allocas yet, far in llvm registers. have immutable local values, though. currently, work fine unless initializer part constant or identifier. example:
def fun(a: int, b: int) = { val n = + b n + 2 } this compiles fine, because a + b compiles instruction add i32 %a, %b , instructions can optionally assigned local values, line becomes: %n = add i32 %a, %b.
on other hand, have trouble generating code following:
def fun() = { val n = 1 n } i generate %n = bitcast i32 1 i32 bitcast doesn't work types , not intended this. well, guess in llvm there nothing intended this, otherwise wouldn't have question.
but there solution without generating tons of different no-op instructions depending on type of value? bitcast not work tuples example:
error: invalid cast opcode cast '{ i32, i32 }' '{ i32, i32 }' %n = bitcast {i32, i32} {i32 1, i32 2} {i32, i32} then again, maybe because there no 'copy' instructions in ir, shouldn't trying , should replacing %n value everywhere used?
you have 2 possibilities:
- generate code using alloca's, load , stores (check e.g. clang's or llvm-gcc's output @ -o0) , use -mem2reg optimization pass raise stuff llvm registers
- use 1 instead of %n everywhere.
Comments
Post a Comment