<div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Hi,</div><div>thanks for your answer<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno ven 25 gen 2019 alle ore 06:23 Doerfert, Johannes Rudolf <<a href="mailto:jdoerfert@anl.gov">jdoerfert@anl.gov</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 01/20, Alberto Barbaro via llvm-dev wrote:<br>
> Hi all,<br>
> I have the following C code:<br>
> <br>
> #include<stdio.h><br>
> <br>
> int main(int argc, char *argv[]) {<br>
> <br>
> printf("%s\n", argv[0]);<br>
> return argc;<br>
> }<br>
> <br>
> that generates the following IR for the main function:<br>
> <br>
> ; Function Attrs: noinline nounwind optnone uwtable<br>
> define i32 @main(i32, i8**) #0 {<br>
> %3 = alloca i32, align 4<br>
> %4 = alloca i32, align 4<br>
> %5 = alloca i8**, align 8<br>
> store i32 0, i32* %3, align 4<br>
> store i32 %0, i32* %4, align 4<br>
> store i8** %1, i8*** %5, align 8<br>
> %6 = load i8**, i8*** %5, align 8<br>
> %7 = getelementptr inbounds i8*, i8** %6, i64 0<br>
> %8 = load i8*, i8** %7, align 8<br>
> %9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4<br>
> x i8]* @.str, i32 0, i32 0), i8* %8)<br>
> %10 = load i32, i32* %4, align 4<br>
> ret i32 %10<br>
> }<br>
<br>
You should really run -mem2reg (without the optnone though). Also<br>
-instcombine is afterwards often helpful to clean up clang generated<br>
code, or maybe just run it with -O1.<br>
<br></blockquote><div><br></div><div>thanks for the suggestion, I understand that the code is not clean but i'd like to understand how it works in general for now :)<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> I think %8 will contain the address of argv[0], so %7 was storing a pointer<br>
> to pointer to argv? I'm not sure of that.<br>
<br>
Let's see: argc is %0 and argv == &argv[0] is %1. The latter is stored<br>
in %5, which is loaded as %6. The GEP (%7) is a no-op, thus %6 == %7.<br>
Finally, %8 is the value argv[0] of type char/i8.<br>
<br></blockquote><div><br></div><div>why do you consider the GET to be a no-op? %6 is a i8** instead %i7 is an i8*. Thanks<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> Considering that I can access the GenericValue set by<br>
> visitGetElementPtrInst. How can I obtain the value of argv[0] at runtime<br>
> using the Interpreter class?<br>
<br>
Sorry can't help with that. I don't know the Interpreter class and I<br>
guess "GenericValue" and "visitGetElementPtrInst" are somehow related to<br>
it.<br>
<br></blockquote><div><br></div><div>No problem, visitGetElementPtrInst is jus that the function that is called when a GEP instruction is found. I tried to get the value calling getOperandValue and basically reproducing the usual LLVM behavior but i always have 0 as value. Maybe a stupid question but when I see the PointerVal of a pointer (e.g. 0x2281a30 ), if I read the bytes as that specific address would I find the value or LLVM does not really store any value in there and the address is just some sort of index within a "emulated" memory?<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> Thanks<br>
<br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br>
<br>
-- <br>
<br>
Johannes Doerfert<br>
Researcher<br>
<br>
Argonne National Laboratory<br>
Lemont, IL 60439, USA<br>
<br>
<a href="mailto:jdoerfert@anl.gov" target="_blank">jdoerfert@anl.gov</a></blockquote><div> </div><div>Thanks<br></div></div></div></div></div>