From 3716c5df291464a994b57dcbafeda5650e9c72f8 Mon Sep 17 00:00:00 2001
From: Bill Wendling ...because the definition of %x does not dominate all of
its uses. The LLVM infrastructure provides a verification pass that may
@@ -263,6 +265,7 @@ automatically run by the parser after parsing input assembly and by
the optimizer before it outputs bytecode. The violations pointed out
by the verifier pass indicate bugs in transformation passes or input to
the parser.
The easy way:
+After strength reduction:
+And the hard way:
+- add i32 %X, %X ; yields {i32}:%0 - add i32 %0, %0 ; yields {i32}:%1 - %result = add i32 %1, %1 +add i32 %X, %X ; yields {i32}:%0 +add i32 %0, %0 ; yields {i32}:%1 +%result = add i32 %1, %1+
This last way of multiplying %X by 8 illustrates several important lexical features of LLVM:
@@ -367,6 +376,7 @@ combined together with the LLVM linker, which merges function (and global variable) definitions, resolves forward declarations, and merges symbol table entries. Here is an example of the "hello world" module: +; Declare the string constant as a global constant... %.LC0 = internal constant [13 x i8 ] c"hello world\0A\00" ; [13 x i8 ]* @@ -384,7 +394,9 @@ define i32 %main() { ; i32()* call i32 %puts(i8 * %cast210) ; i32 ret i32 0+ href="#i_ret">ret i32 0
}
This example is made up of a global variable named ".LC0", an external declaration of the "puts" @@ -647,9 +659,11 @@ a power of 2.
For example, the following defines a global with an initializer, section, and alignment:
+- %G = constant float 1.0, section "foo", align 4 +%G = constant float 1.0, section "foo", align 4+
- @<Name> = [Linkage] [Visibility] alias <AliaseeTy> @<Aliasee> +@<Name> = [Linkage] [Visibility] alias <AliaseeTy> @<Aliasee>+
Parameter attributes are simple keywords that follow the type specified. If multiple parameter attributes are needed, they are space separated. For - example:
- %someFunc = i16 (i8 sext %someParam) zext - %someFunc = i16 (i8 zext %someParam) zext+ example: + +
+%someFunc = i16 (i8 sext %someParam) zext +%someFunc = i16 (i8 zext %someParam) zext ++
Note that the two function types above are unique because the parameter has a different attribute (sext in the first one, zext in the second). Also note that the attribute for the function result (zext) comes immediately after the @@ -787,10 +809,12 @@ LLVM and treated as a single unit, but may be separated in the .ll file if desired. The syntax is very simple:
-- module asm "inline asm code goes here" - module asm "more can go here" -
+module asm "inline asm code goes here" +module asm "more can go here" ++
The strings can contain any character by escaping non-printable characters. The escape sequence used is simply "\xx" where "xx" is the two digit hex code @@ -1015,6 +1039,7 @@ value.
+ @@ -1398,11 +1423,13 @@ href="#identifiers">identifier for the global is used and always have pointer type. For example, the following is a legal LLVM file: +- %X = global i32 17 - %Y = global i32 42 - %Z = global [2 x i32*] [ i32* %X, i32* %Y ] +%X = global i32 17 +%Y = global i32 42 +%Z = global [2 x i32*] [ i32* %X, i32* %Y ]+
- i32 (i32) asm "bswap $0", "=r,r" +i32 (i32) asm "bswap $0", "=r,r"+
Inline assembler expressions may only be used as the callee operand of a call instruction. Thus, typically we have:
+Inline asms with side effects not visible in the constraint list must be marked @@ -1575,9 +1606,11 @@ as having side effects. This is done through the use of the 'sideeffect' keyword, like so:
+- call void asm sideeffect "eieio", ""() +call void asm sideeffect "eieio", ""()+
TODO: The format of the asm and constraints string still need to be documented here. Constraints on what can be done (e.g. duplication, moving, etc @@ -2663,8 +2696,8 @@ instructions), the memory is reclaimed.
%ptr = alloca i32 ; yields {i32*}:ptr - %ptr = alloca i32, i32 4 ; yields {i32*}:ptr - %ptr = alloca i32, i32 4, align 1024 ; yields {i32*}:ptr + %ptr = alloca i32, i32 4 ; yields {i32*}:ptr + %ptr = alloca i32, i32 4, align 1024 ; yields {i32*}:ptr %ptr = alloca i32, align 1024 ; yields {i32*}:ptr@@ -2754,35 +2787,39 @@ be sign extended to 64-bit values.
For example, let's consider a C code fragment and how it gets compiled to LLVM:
+- struct RT { - char A; - i32 B[10][20]; - char C; - }; - struct ST { - i32 X; - double Y; - struct RT Z; - }; +struct RT { + char A; + i32 B[10][20]; + char C; +}; +struct ST { + i32 X; + double Y; + struct RT Z; +}; - define i32 *foo(struct ST *s) { - return &s[1].Z.B[5][13]; - } +i32 *foo(struct ST *s) { + return &s[1].Z.B[5][13]; +}+
The LLVM code generated by the GCC frontend is:
+- %RT = type { i8 , [10 x [20 x i32]], i8 } - %ST = type { i32, double, %RT } +%RT = type { i8 , [10 x [20 x i32]], i8 } +%ST = type { i32, double, %RT } - define i32* %foo(%ST* %s) { - entry: - %reg = getelementptr %ST* %s, i32 1, i32 2, i32 1, i32 5, i32 13 - ret i32* %reg - } +define i32* %foo(%ST* %s) { +entry: + %reg = getelementptr %ST* %s, i32 1, i32 2, i32 1, i32 5, i32 13 + ret i32* %reg +}+
define i32 @test(i32 %X, ...) { ; Initialize variable argument processing @@ -3721,6 +3759,8 @@ declare void @llvm.va_end(i8*)