[LLVMdev] Compiler opt is turned off ?

Justin Holewinski justin.holewinski at gmail.com
Fri Jan 4 05:23:36 PST 2013


Since a, b, and c are globals, how does the optimize *know* they are not
used elsewhere (e.g. another module)?


On Fri, Jan 4, 2013 at 7:49 AM, Somenath Chakraborty <some.chak at gmail.com>wrote:

> Hello,
>
> I was trying to run few testcases and see how llvm optmizes different
> scenarios. I have a small testcase like:
>
>
> #include <stdio.h>
>
>
>
> int a, b, c;
>
>
>
> int
>
> main()
>
> {
>
>     a = b + c;
>
>     c = a;
>
>
>
>     if (a == b)
>
>         b = c;
>
>     else
>
>         b = a;
>
>
>
>     printf( " a = %d \n ", a );
>
>     return 0;
>
> }
>
>
>
> The corresponding llvm IR is ( clang test.c -S -emit-llvm -o - -O3 ) :
>
>
>
>  define i32 @main() nounwind uwtable {
>
> entry:
>
>   %0 = load i32* @b, align 4, !tbaa !4
>
>   %1 = load i32* @c, align 4, !tbaa !4
>
>   %add = add nsw i32 %1, %0
>
>   store i32 %add, i32* @a, align 4, !tbaa !4
>
>   store i32 %add, i32* @c, align 4, !tbaa !4
>
>   store i32 %add, i32* @b, align 4, !tbaa !4
>
>   %call = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds
> ([11 x i8]* @.str, i64 0, i64 0), i32 %add) nounwind
>
>   ret i32 0
>
> }
>
> Here, I can see, although the *copy-propagation* is kicked in, but, *constant
> propagation* is not happening. I assume the reason behind this is that
> a, b and c are all global variables. Also, as no one is reading b and c
> after the statement "a = b + c", should they not get optimized out ( from
> "c = a" to end of function)?
>
>
>
> Now, if a, b and c are made locals, all the optmizations are kicked in:
>
>
>
> define i32 @main() nounwind uwtable {
>
> entry:
>
>   %call = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds
> ([11 x i8]* @.str, i64 0, i64 0), i32 0) nounwind
>
>   ret i32 0
>
> }
>
> Are we little conservative on global variables even though *they don't
> have readers* and debug mode is *OFF* ? Or, is it supported under some
> FLAG? Am I missing something .... ?
>
>
>
> Thanks.
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>


-- 

Thanks,

Justin Holewinski
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130104/2f248304/attachment.html>


More information about the llvm-dev mailing list