<div dir="ltr"><div>Dear LLVM mailing list,</div><div><br></div><div>I am currently having difficulty with figuring out how to get a function parameter Value* when I am compiling with -O2. It seems -O2 performs an optimization that optimizes out the local variable formed for the function parameter.<br></div><div>I have a pass where I am inserting new function calls, and need to pass the function parameters variable address. With -O0 this issue does not come up as each function parameter when using -O0 gets its own AllocaInst and I can leverage that Value* to pass to my new function call. I have tried to leverage llvm.dbg information but it seems LLVM is not inserting llvm.dbg.addr for me to use.<br></div><div><br></div><div>Below is the code example I am working with:</div><div>/*  example.c */<br></div><div></div><div>int fd_in;</div><div>char* ut_addr;</div><div><br></div><div>__attribute__((noinline)) void bar (int b){</div><div>       my_pass_function( &b );  // NEW (what I want to be done by my pass)<br></div><div>       ut_addr = (char*) mmap( 0, b, my_prot, MAP_PRIVATE, fd_in, 0);</div><div>}</div><div><br></div><div>__attribute__((noinline)) void foo (int f){<br>        bar( f );<br>}</div><div><br>int main(int argc, char** argv){<br>        int m;<br>        struct stat statbuf;<br>        char *input;</div><div><br>        if( (fd_in = open ( input, O_RDONLY)) < 0){        /* open the input file */<br>                return 0;<br>        }<br>        if( fstat( fd_in , &statbuf ) < 0 ){         /* find size of input file */<br>                return 0;<br>        }<br><br>        m = statbuf.st_size;<br>        foo( m );<br>        return 0;<br>}<br></div><div>-----------------------------------------------------------------</div><div>The original IR I get for bar() is below, as you can see the function parameter is the "raw" value "i32 %b" passed into the function and not a variable that I can leverage, get its Value* . Hence when I run my newly instrumented code, I get a segmentation fault.<br></div><div><br></div><div>define void @bar(i32 %b) local_unnamed_addr #0 !dbg !35 {<br>entry:<br>  call void @llvm.dbg.value(metadata i32 %b, metadata !39, metadata !DIExpression()), !dbg !44<br>  call void @llvm.dbg.value(metadata i64 1000, metadata !40, metadata !DIExpression()), !dbg !44<br>  call void @llvm.dbg.value(metadata i32 1, metadata !43, metadata !DIExpression()), !dbg !44<br>  %conv = sext i32 %b to i64, !dbg !45<br>  %0 = load i32, i32* @fd_in, align 4, !dbg !46, !tbaa !47<br>  %call = tail call i8* @mmap(i8* null, i64 %conv, i32 1, i32 2, i32 %0, i64 0) #7, !dbg !51<br>  store i8* %call, i8** @ut_addr, align 8, !dbg !52, !tbaa !53<br>  ret void, !dbg !55<br>}<br></div></div>