llvm.org GIT mirror llvm / 88816ec
Fix up the formating and change llvm-gcc to clang. Note that this example doesn't work anymore! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@139999 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 8 years ago
1 changed file(s) with 35 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
7878

The following example illustrates the advantages of LTO's integrated

7979 approach and clean interface. This example requires a system linker which
8080 supports LTO through the interface described in this document. Here,
81 llvm-gcc transparently invokes system linker.

81 clang transparently invokes system linker.

8282
8383
  • Input source file a.c is compiled into LLVM bitcode form.
  • 8484
  • Input source file main.c is compiled into native object code.
  • 8888 extern int foo1(void);
    8989 extern void foo2(void);
    9090 extern void foo4(void);
    91
    9192 --- a.c ---
    9293 #include "a.h"
    9394
    9495 static signed int i = 0;
    9596
    9697 void foo2(void) {
    97 i = -1;
    98 i = -1;
    9899 }
    99100
    100101 static int foo3() {
    101 foo4();
    102 return 10;
    102 foo4();
    103 return 10;
    103104 }
    104105
    105106 int foo1(void) {
    106 int data = 0;
    107
    108 if (i < 0) { data = foo3(); }
    109
    110 data = data + 42;
    111 return data;
    107 int data = 0;
    108
    109 if (i < 0)
    110 data = foo3();
    111
    112 data = data + 42;
    113 return data;
    112114 }
    113115
    114116 --- main.c ---
    116118 #include "a.h"
    117119
    118120 void foo4(void) {
    119 printf ("Hi\n");
    121 printf("Hi\n");
    120122 }
    121123
    122124 int main() {
    123 return foo1();
    125 return foo1();
    124126 }
    125127
    126128 --- command lines ---
    127 $ llvm-gcc --emit-llvm -c a.c -o a.o # <-- a.o is LLVM bitcode file
    128 $ llvm-gcc -c main.c -o main.o # <-- main.o is native object file
    129 $ llvm-gcc a.o main.o -o main # <-- standard link command without any modifications
    130
    131

    In this example, the linker recognizes that foo2() is an

    132 externally visible symbol defined in LLVM bitcode file. The linker completes
    133 its usual symbol resolution
    134 pass and finds that foo2() is not used anywhere. This information
    135 is used by the LLVM optimizer and it removes foo2(). As soon as
    136 foo2() is removed, the optimizer recognizes that condition
    137 i < 0 is always false, which means foo3() is never
    138 used. Hence, the optimizer removes foo3(), also. And this in turn,
    139 enables linker to remove foo4(). This example illustrates the
    140 advantage of tight integration with the linker. Here, the optimizer can not
    141 remove foo3() without the linker's input.
    142

    129 $ clang -emit-llvm -c a.c -o a.o # <-- a.o is LLVM bitcode file
    130 $ clang -c main.c -o main.o # <-- main.o is native object file
    131 $ clang a.o main.o -o main # <-- standard link command without any modifications
    132
    133
    134
    135
  • In this example, the linker recognizes that foo2() is an
  • 136 externally visible symbol defined in LLVM bitcode file. The linker
    137 completes its usual symbol resolution pass and finds that foo2()
    138 is not used anywhere. This information is used by the LLVM optimizer and
    139 it removes foo2().
    140
  • As soon as foo2() is removed, the optimizer recognizes that condition
  • 141 i < 0 is always false, which means foo3() is never
    142 used. Hence, the optimizer also removes foo3().
    143
  • And this in turn, enables linker to remove foo4().
  • 144
    145
    146

    This example illustrates the advantage of tight integration with the

    147 linker. Here, the optimizer can not remove foo3() without the
    148 linker's input.

    149
    143150
    144151
    145152