From tonic at nondot.org Mon Jun 9 01:02:09 2008 From: tonic at nondot.org (Tanya Lattner) Date: Mon, 09 Jun 2008 06:02:09 -0000 Subject: [llvm-commits] [llvm] r52121 - /llvm/trunk/docs/GettingStarted.html Message-ID: <200806090602.m59629RD020126@zion.cs.uiuc.edu> Author: tbrethou Date: Mon Jun 9 01:02:09 2008 New Revision: 52121 URL: http://llvm.org/viewvc/llvm-project?rev=52121&view=rev Log: Update with new release. Modified: llvm/trunk/docs/GettingStarted.html Modified: llvm/trunk/docs/GettingStarted.html URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/GettingStarted.html?rev=52121&r1=52120&r2=52121&view=diff ============================================================================== --- llvm/trunk/docs/GettingStarted.html (original) +++ llvm/trunk/docs/GettingStarted.html Mon Jun 9 01:02:09 2008 @@ -716,6 +716,7 @@ subdirectories of the 'tags' directory:
The main features of llvmc2 are: +
The main features of llvmc2 are:
LLVM 2.3 includes a completely rewritten interface for Link Time Optimization. This interface @@ -285,7 +285,7 @@
Written by the LLVM Team
-
This is the fourteenth public release of the LLVM Compiler Infrastructure. -It includes many features and refinements from LLVM 2.2.
+It includes a large number of features and refinements from LLVM 2.2.LLVM 2.2 was the last LLVM release to support llvm-gcc 4.0 and llvm-upgrade. -llvm-gcc 4.0 has been replaced with llvm-gcc 4.2. llvm-upgrade was useful for -upgrading llvm 1.9 files to llvm 2.x syntax, but you can always use a previous -llvm release to do this.
+LLVM 2.3 no longer supports llvm-gcc 4.0, it has been replaced with + llvm-gcc 4.2.
+LLVM 2.3 no longer includes the llvm-upgrade tool. It was useful + for upgrading LLVM 1.9 files to LLVM 2.x syntax, but you can always use a + previous LLVM release to do this. One nice impact of this is that the LLVM + regression test suite no longer depends on llvm-upgrade, which makes it run + faster.
+ +The llvm2cpp tool has been folded into llc, use + llc -march=cpp instead of llvm2cpp.
+ +LLVM API Changes:
+ ++The core LLVM 2.3 distribution currently consists of code from the core LLVM +repository (which roughly contains the LLVM optimizer, code generators and +supporting tools) and the llvm-gcc repository. In addition to this code, the +LLVM Project includes other sub-projects that are in development. The two which +are the most actively developed are the new vmkit Project +and the Clang Project. +
++The "vmkit" project is a new addition to the LLVM family. It is an +implementation of a JVM and a CLI Virtual Machines (Microsoft .NET is an +implementation of the CLI) using the Just-In-Time compiler of LLVM.
+ +The JVM, called JnJVM, executes real-world applications such as Apache +projects (e.g. Felix and Tomcat) and the SpecJVM98 benchmark. It uses the GNU +Classpath project for the base classes. The CLI implementation, called N3, is +its in early stages but can execute simple applications and the "pnetmark" +benchmark. It uses the pnetlib project as its core library.
+ +The 'vmkit' VMs compare in performance with industrial and top open-source +VMs on scientific applications. Besides the JIT, the VMs use many features of +the LLVM framework, including the standard set of optimizations, atomic +operations, custom function provider and memory manager for JITed methods, and +specific virtual machine optimizations. vmkit is not an official part of LLVM +2.3 release. It is publicly available under the LLVM license and can be +downloaded from: +
+ ++svn co http://llvm.org/svn/llvm-project/vmkit/trunk vmkit +
-LLVM 2.3 fully supports llvm-gcc 4.2 front-end.
+The Clang project is an effort to build +a set of new 'LLVM native' front-end technologies for the LLVM optimizer +and code generator. Clang is continuing to make major strides forward in all +areas. Its C and Objective-C parsing support is very solid, and the code +generation support is far enough along to build many C applications. While not +yet production quality, it is progressing very nicely. In addition, C++ +front-end work has started to make significant progress.
+ +At this point, Clang is most useful if you are interested in source-to-source +transformations (such as refactoring) and other source-level tools for C and +Objective-C. Clang now also includes tools for turning C code into pretty HTML, +and includes a new static +analysis tool in development. This tool focuses on automatically finding +bugs in C and Objective-C code.
+ +The clang project is an effort to build -a set of new 'llvm native' front-end technologies for the LLVM optimizer -and code generator. Currently, its C and Objective-C support is maturing -nicely, and it has advanced source-to-source analysis and transformation -capabilities. If you are interested in building source-level tools for C and -Objective-C (and eventually C++), you should take a look. However, note that -clang is not an official part of the LLVM 2.3 release. If you are interested in -this project, please see its web site.
+LLVM 2.3 includes a huge number of bug fixes, performance tweaks and minor +improvements. Some of the major improvements and new features are listed in +this section. +
LLVM 2.3 includes several major new capabilities:
The biggest change in LLVM 2.3 is Multiple Return Value (MRV) support. + MRVs allow LLVM IR to directly represent functions that return multiple + values without having to pass them "by reference" in the LLVM IR. This + allows a front-end to generate more efficient code, as MRVs are generally + returned in registers if a target supports them. See the LLVM IR Reference for more details.
+ +MRVs are fully supported in the LLVM IR, but are not yet fully supported in + on all targets. However, it is generally safe to return up to 2 values from + a function: most targets should be able to handle at least that. MRV + support is a critical requirement for X86-64 ABI support, as X86-64 requires + the ability to return multiple registers from functions, and we use MRVs to + accomplish this in a direct way.
LLVM 2.3 includes a complete reimplementation of the "llvmc" + tool. It is designed to overcome several problems with the original + llvmc and to provide a superset of the features of the + 'gcc' driver.
+ +The main features of llvmc2 are: +
LLVM 2.3 includes a completely rewritten interface for Link Time Optimization. This interface + is written in C, which allows for easier integration with C code bases, and + incorporates improvements we learned about from the first incarnation of the + interface.
The Kaleidoscope tutorial now + includes a "port" of the tutorial that uses the Ocaml bindings to implement + the Kaleidoscope language.
LLVM 2.3 fully supports the llvm-gcc 4.2 front-end, and includes support +for the C, C++, Objective-C, Ada, and Fortran front-ends.
+ ++
In addition to a huge array of bug fixes and minor performance tweaks, the +LLVM 2.3 optimizers support a few major enhancements:
+ +Loop index set splitting on by default. +This transformation hoists conditions from loop bodies and reduces a loop's +iteration space to improve performance. For example,
+ ++for (i = LB; i < UB; ++i) + if (i <= NV) + LOOP_BODY ++ +
is transformed into:
+ ++NUB = min(NV+1, UB) +for (i = LB; i < NUB; ++i) + LOOP_BODY ++
In addition to a huge array of bug fixes and minor performance tweaks, the -LLVM 2.3 optimizers support a few major enhancements:
+New target-specific features include: +
LLVM is known to work on the following platforms:
The core LLVM infrastructure uses -GNU autoconf to adapt itself +
The core LLVM infrastructure uses GNU autoconf to adapt itself to the machine and operating system on which it is built. However, minor porting may be required to get LLVM to work on new platforms. We welcome your portability patches and reports of successful builds or error messages.
@@ -263,9 +593,8 @@llvm-gcc does not currently support Link-Time Optimization on most platforms "out-of-the-box". Please inquire on the llvmdev mailing list if you are interested.
-The only major language feature of GCC not supported by llvm-gcc is + the __builtin_apply family of builtins. However, some extensions + are only supported on some targets. For example, trampolines are only + supported on some targets (these are used when you take the address of a + nested function).
-llvm-gcc does not support __builtin_apply yet. - See Constructing Calls: Dispatching a call to another function.
-llvm-gcc partially supports these GCC extensions:
-If you run into GCC extensions which have not been included in any of these -lists, please let us know (also including whether or not they work).
+If you run into GCC extensions which are not supported, please let us know. +
Tail call optimization, callee reusing the stack of the caller, is currently supported on x86/x86-64 and PowerPC. It is performed if: +
x86/x86-64 constraints: +
PowerPC constraints: +
Example:
+Call as llc -tailcallopt test.ll. +
+declare fastcc i32 @tailcallee(i32 inreg %a1, i32 inreg %a2, i32 %a3, i32 %a4)
+
+define fastcc i32 @tailcaller(i32 %in1, i32 %in2) {
+ %l1 = add i32 %in1, %in2
+ %tmp = tail call fastcc i32 @tailcallee(i32 %in1 inreg, i32 %in2 inreg, i32 %in1, i32 %l1)
+ ret i32 %tmp
+}
+ Implications of -tailcallopt:
+To support tail call optimization in situations where the callee has more arguments than the caller a 'callee pops arguments' convention is used. This currently causes each fastcc call that is not tail call optimized (because one or more of above constraints are not met) to be followed by a readjustment of the stack. So performance might be worse in such cases.
+On x86 and x86-64 one register is reserved for indirect tail calls (e.g via a function pointer). So there is one less register for integer argument passing. For x86 this means 2 registers (if inreg parameter attribute is used) and for x86-64 this means 5 register are used.
+NOTE: This document is a work in progress!
-This document describes the requirements, design, and configuration of the - LLVM compiler driver, llvmc. The compiler driver knows about LLVM's - tool set and can be configured to know about a variety of compilers for - source languages. It uses this knowledge to execute the tools necessary - to accomplish general compilation, optimization, and linking tasks. The main - purpose of llvmc is to provide a simple and consistent interface to - all compilation tasks. This reduces the burden on the end user who can just - learn to use llvmc instead of the entire LLVM tool set and all the - source language compilers compatible with LLVM.
-The llvmc tool is a configurable compiler - driver. As such, it isn't a compiler, optimizer, - or a linker itself but it drives (invokes) other software that perform those - tasks. If you are familiar with the GNU Compiler Collection's gcc - tool, llvmc is very similar.
-The following introductory sections will help you understand why this tool - is necessary and what it does.
-llvmc was invented to make compilation of user programs with - LLVM-based tools easier. To accomplish this, llvmc strives to:
-Additionally, llvmc makes it easier to write a compiler for use - with LLVM, because it:
-At a high level, llvmc operation is very simple. The basic action - taken by llvmc is to simply invoke some tool or set of tools to fill - the user's request for compilation. Every execution of llvmctakes the - following sequence of steps:
-llvmc's operation must be simple, regular and predictable. - Developers need to be able to rely on it to take a consistent approach to - compilation. For example, the invocation:
-
- llvmc -O2 x.c y.c z.c -o xyz
- must produce exactly the same results as:
-- llvmc -O2 x.c -o x.o - llvmc -O2 y.c -o y.o - llvmc -O2 z.c -o z.o - llvmc -O2 x.o y.o z.o -o xyz-
To accomplish this, llvmc uses a very simple goal oriented - procedure to do its work. The overall goal is to produce a functioning - executable. To accomplish this, llvmc always attempts to execute a - series of compilation phases in the same sequence. - However, the user's options to llvmc can cause the sequence of phases - to start in the middle or finish early.
-llvmc breaks every compilation task into the following five - distinct phases:
-The following table shows the inputs, outputs, and command line options - applicable to each phase.
-| Phase | -Inputs | -Outputs | -Options | -
|---|---|---|---|
| Preprocessing | -
|
-
|
-
|
-
| Translation | -
|
-
|
-
|
-
| Optimization | -
|
-
|
-
|
-
| Linking | -
|
-
|
-
|
-
An action, with regard to llvmc is a basic operation that it takes - in order to fulfill the user's request. Each phase of compilation will invoke - zero or more actions in order to accomplish that phase.
-Actions come in two forms:
-This section of the document describes the configuration files used by - llvmc. Configuration information is relatively static for a - given release of LLVM and a compiler tool. However, the details may - change from release to release of either. Users are encouraged to simply use - the various options of the llvmc command and ignore the configuration - of the tool. These configuration files are for compiler writers and LLVM - developers. Those wishing to simply use llvmc don't need to understand - this section but it may be instructive on how the tool works.
-llvmc is highly configurable both on the command line and in -configuration files. The options it understands are generic, consistent and -simple by design. Furthermore, the llvmc options apply to the -compilation of any LLVM enabled programming language. To be enabled as a -supported source language compiler, a compiler writer must provide a -configuration file that tells llvmc how to invoke the compiler -and what its capabilities are. The purpose of the configuration files then -is to allow compiler writers to specify to llvmc how the compiler -should be invoked. Users may but are not advised to alter the compiler's -llvmc configuration.
- -Because llvmc just invokes other programs, it must deal with the -available command line options for those programs regardless of whether they -were written for LLVM or not. Furthermore, not all compiler tools will -have the same capabilities. Some compiler tools will simply generate LLVM assembly -code, others will be able to generate fully optimized bitcode. In general, -llvmc doesn't make any assumptions about the capabilities or command -line options of a sub-tool. It simply uses the details found in the -configuration files and leaves it to the compiler writer to specify the -configuration correctly.
- -This approach means that new compiler tools can be up and working very -quickly. As a first cut, a tool can simply compile its source to raw -(unoptimized) bitcode or LLVM assembly and llvmc can be configured -to pick up the slack (translate LLVM assembly to bitcode, optimize the -bitcode, generate native assembly, link, etc.). In fact, the compiler tools -need not use any LLVM libraries, and it could be written in any language -(instead of C++). The configuration data will allow the full range of -optimization, assembly, and linking capabilities that LLVM provides to be added -to these kinds of tools. Enabling the rapid development of front-ends is one -of the primary goals of llvmc.
- -As a compiler tool matures, it may utilize the LLVM libraries and tools -to more efficiently produce optimized bitcode directly in a single compilation -and optimization program. In these cases, multiple tools would not be needed -and the configuration data for the compiler would change.
- -Configuring llvmc to the needs and capabilities of a source language -compiler is relatively straight-forward. A compiler writer must provide a -definition of what to do for each of the five compilation phases for each of -the optimization levels. The specification consists simply of prototypical -command lines into which llvmc can substitute command line -arguments and file names. Note that any given phase can be completely blank if -the source language's compiler combines multiple phases into a single program. -For example, quite often pre-processing, translation, and optimization are -combined into a single program. The specification for such a compiler would have -blank entries for pre-processing and translation but a full command line for -optimization.
-Each configuration file provides the details for a single source language - that is to be compiled. This configuration information tells llvmc - how to invoke the language's pre-processor, translator, optimizer, assembler - and linker. Note that a given source language needn't provide all these tools - as many of them exist in llvm currently.
-llvmc always looks for files of a specific name. It uses the
- first file with the name its looking for by searching directories in the
- following order:
-
The first file found in this search will be used. Other files with the - same name will be ignored even if they exist in one of the subsequent search - locations.
-In the directories searched, each configuration file is given a specific - name to foster faster lookup (so llvmc doesn't have to do directory searches). - The name of a given language specific configuration file is simply the same - as the suffix used to identify files containing source in that language. - For example, a configuration file for C++ source might be named - cpp, C, or cxx. For languages that support multiple - file suffixes, multiple (probably identical) files (or symbolic links) will - need to be provided.
-Which configuration files are read depends on the command line options and - the suffixes of the file names provided on llvmc's command line. Note - that the -x LANGUAGE option alters the language that llvmc - uses for the subsequent files on the command line. Only the configuration - files actually needed to complete llvmc's task are read. Other - language specific files will be ignored.
-The syntax of the configuration files is very simple and somewhat - compatible with Java's property files. Here are the syntax rules:
-The table below provides definitions of the allowed configuration items - that may appear in a configuration file. Every item has a default value and - does not need to appear in the configuration file. Missing items will have the - default value. Each identifier may appear as all lower case, first letter - capitalized or all upper case.
-| Name | -Value Type | -Description | -Default | -
|---|---|---|---|
LLVMC ITEMS | |||
| version | -string | -Provides the version string for the contents of this - configuration file. What is accepted as a legal configuration file - will change over time and this item tells llvmc which version - should be expected. | -b | -
LANG ITEMS | |||
| lang.name | -string | -Provides the common name for a language definition. - For example "C++", "Pascal", "FORTRAN", etc. | -blank | -
| lang.opt1 | -string | -Specifies the parameters to give the optimizer when - -O1 is specified on the llvmc command line. | --simplifycfg -instcombine -mem2reg | -
| lang.opt2 | -string | -Specifies the parameters to give the optimizer when - -O2 is specified on the llvmc command line. | -TBD | -
| lang.opt3 | -string | -Specifies the parameters to give the optimizer when - -O3 is specified on the llvmc command line. | -TBD | -
| lang.opt4 | -string | -Specifies the parameters to give the optimizer when - -O4 is specified on the llvmc command line. | -TBD | -
| lang.opt5 | -string | -Specifies the parameters to give the optimizer when - -O5 is specified on the llvmc command line. | -TBD | -
PREPROCESSOR ITEMS | |||
| preprocessor.command | -command | -This provides the command prototype that will be used - to run the preprocessor. This is generally only used with the - -E option. | -<blank> | -
| preprocessor.required | -boolean | -This item specifies whether the pre-processing phase - is required by the language. If the value is true, then the - preprocessor.command value must not be blank. With this option, - llvmc will always run the preprocessor as it assumes that the - translation and optimization phases don't know how to pre-process their - input. | -false | -
TRANSLATOR ITEMS | |||
| translator.command | -command | -This provides the command prototype that will be used - to run the translator. Valid substitutions are %in% for the - input file and %out% for the output file. | -<blank> | -
| translator.output | -bitcode or assembly | -This item specifies the kind of output the language's - translator generates. | -bitcode | -
| translator.preprocesses | -boolean | -Indicates that the translator also preprocesses. If - this is true, then llvmc will skip the pre-processing phase - whenever the final phase is not pre-processing. | -false | -
OPTIMIZER ITEMS | |||
| optimizer.command | -command | -This provides the command prototype that will be used - to run the optimizer. Valid substitutions are %in% for the - input file and %out% for the output file. | -<blank> | -
| optimizer.output | -bitcode or assembly | -This item specifies the kind of output the language's - optimizer generates. Valid values are "assembly" and "bitcode" | -bitcode | -
| optimizer.preprocesses | -boolean | -Indicates that the optimizer also preprocesses. If - this is true, then llvmc will skip the pre-processing phase - whenever the final phase is optimization or later. | -false | -
| optimizer.translates | -boolean | -Indicates that the optimizer also translates. If - this is true, then llvmc will skip the translation phase - whenever the final phase is optimization or later. | -false | -
ASSEMBLER ITEMS | |||
| assembler.command | -command | -This provides the command prototype that will be used - to run the assembler. Valid substitutions are %in% for the - input file and %out% for the output file. | -<blank> | -
On any configuration item that ends in command, you must - specify substitution tokens. Substitution tokens begin and end with a percent - sign (%) and are replaced by the corresponding text. Any substitution - token may be given on any command line but some are more useful than - others. In particular each command should have both an %in% - and an %out% substitution. The table below provides definitions of - each of the allowed substitution tokens.
-| Substitution Token | -Replacement Description | -
|---|---|
| %args% | -Replaced with all the tool-specific arguments given - to llvmc via the -T set of options. This just allows - you to place these arguments in the correct place on the command line. - If the %args% option does not appear on your command line, - then you are explicitly disallowing the -T option for your - tool. - | -
| %force% | -Replaced with the -f option if it was - specified on the llvmc command line. This is intended to tell - the compiler tool to force the overwrite of output files. - | -
| %in% | -Replaced with the full path of the input file. You - needn't worry about the cascading of file names. llvmc will - create temporary files and ensure that the output of one phase is the - input to the next phase. | -
| %opt% | -Replaced with the optimization options for the - tool. If the tool understands the -O options then that will - be passed. Otherwise, the lang.optN series of configuration - items will specify which arguments are to be given. | -
| %out% | -Replaced with the full path of the output file. - Note that this is not necessarily the output file specified with the - -o option on llvmc's command line. It might be a - temporary file that will be passed to a subsequent phase's input. - | -
| %stats% | -If your command accepts the -stats option, - use this substitution token. If the user requested -stats - from the llvmc command line then this token will be replaced - with -stats, otherwise it will be ignored. - | -
| %target% | -Replaced with the name of the target "machine" for - which code should be generated. The value used here is taken from the - llvmc option -march. - | -
| %time% | -If your command accepts the -time-passes - option, use this substitution token. If the user requested - -time-passes from the llvmc command line then this - token will be replaced with -time-passes, otherwise it will - be ignored. - | -
Since an example is always instructive, here's how the Stacker language - configuration file looks.
--# Stacker Configuration File For llvmc - -########################################################## -# Language definitions -########################################################## - lang.name=Stacker - lang.opt1=-simplifycfg -instcombine -mem2reg - lang.opt2=-simplifycfg -instcombine -mem2reg -load-vn \ - -gcse -dse -scalarrepl -sccp - lang.opt3=-simplifycfg -instcombine -mem2reg -load-vn \ - -gcse -dse -scalarrepl -sccp -branch-combine -adce \ - -globaldce -inline -licm - lang.opt4=-simplifycfg -instcombine -mem2reg -load-vn \ - -gcse -dse -scalarrepl -sccp -ipconstprop \ - -branch-combine -adce -globaldce -inline -licm - lang.opt5=-simplifycfg -instcombine -mem2reg --load-vn \ - -gcse -dse scalarrepl -sccp -ipconstprop \ - -branch-combine -adce -globaldce -inline -licm \ - -block-placement - -########################################################## -# Pre-processor definitions -########################################################## - - # Stacker doesn't have a preprocessor but the following - # allows the -E option to be supported - preprocessor.command=cp %in% %out% - preprocessor.required=false - -########################################################## -# Translator definitions -########################################################## - - # To compile stacker source, we just run the stacker - # compiler with a default stack size of 2048 entries. - translator.command=stkrc -s 2048 %in% -o %out% %time% \ - %stats% %force% %args% - - # stkrc doesn't preprocess but we set this to true so - # that we don't run the cp command by default. - translator.preprocesses=true - - # The translator is required to run. - translator.required=true - - # stkrc doesn't handle the -On options - translator.output=bitcode - -########################################################## -# Optimizer definitions -########################################################## - - # For optimization, we use the LLVM "opt" program - optimizer.command=opt %in% -o %out% %opt% %time% %stats% \ - %force% %args% - - optimizer.required = true - - # opt doesn't translate - optimizer.translates = no - - # opt doesn't preprocess - optimizer.preprocesses=no - - # opt produces bitcode - optimizer.output = bc - -########################################################## -# Assembler definitions -########################################################## - assembler.command=llc %in% -o %out% %target% %time% %stats% --
This document uses precise terms in reference to the various artifacts and - concepts related to compilation. The terms used throughout this document are - defined below.
-Note: This document is a work-in-progress. Additions and clarifications + are welcome.
+LLVMC is a generic compiler driver, designed to be customizable and +extensible. It plays the same role for LLVM as the gcc program +does for GCC - LLVMC's job is essentially to transform a set of input +files into a set of targets depending on configuration rules and user +options. What makes LLVMC different is that these transformation rules +are completely customizable - in fact, LLVMC knows nothing about the +specifics of transformation (even the command-line options are mostly +not hard-coded) and regards the transformation structure as an +abstract graph. This makes it possible to adapt LLVMC for other +purposes - for example, as a build tool for game resources.
+Because LLVMC employs TableGen [1] as its configuration language, you +need to be familiar with it to customize LLVMC.
+LLVMC tries hard to be as compatible with gcc as possible, +although there are some small differences. Most of the time, however, +you shouldn't be able to notice them:
++$ # This works as expected: +$ llvmc2 -O3 -Wall hello.cpp +$ ./a.out +hello ++
One nice feature of LLVMC is that one doesn't have to distinguish +between different compilers for different languages (think g++ and +gcc) - the right toolchain is chosen automatically based on input +language names (which are, in turn, determined from file +extensions). If you want to force files ending with ".c" to compile as +C++, use the -x option, just like you would do it with gcc:
++$ llvmc2 -x c hello.cpp +$ # hello.cpp is really a C file +$ ./a.out +hello ++
On the other hand, when using LLVMC as a linker to combine several C++ +object files you should provide the --linker option since it's +impossible for LLVMC to choose the right linker in that case:
++$ llvmc2 -c hello.cpp +$ llvmc2 hello.o +[A lot of link-time errors skipped] +$ llvmc2 --linker=c++ hello.o +$ ./a.out +hello ++
LLVMC has some built-in options that can't be overridden in the +configuration files:
+At the time of writing LLVMC does not support on-the-fly reloading of +configuration, so to customize LLVMC you'll have to recompile the +source code (which lives under $LLVM_DIR/tools/llvmc2). The +default configuration files are Common.td (contains common +definitions, don't forget to include it in your configuration +files), Tools.td (tool descriptions) and Graph.td (compilation +graph definition).
+To compile LLVMC with your own configuration file (say,``MyGraph.td``), +run make like this:
++$ cd $LLVM_DIR/tools/llvmc2 +$ make GRAPH=MyGraph.td TOOLNAME=my_llvmc ++
This will build an executable named my_llvmc. There are also +several sample configuration files in the llvmc2/examples +subdirectory that should help to get you started.
+Internally, LLVMC stores information about possible source +transformations in form of a graph. Nodes in this graph represent +tools, and edges between two nodes represent a transformation path. A +special "root" node is used to mark entry points for the +transformations. LLVMC also assigns a weight to each edge (more on +this later) to choose between several alternative edges.
+The definition of the compilation graph (see file Graph.td) is +just a list of edges:
++def CompilationGraph : CompilationGraph<[ + Edge<root, llvm_gcc_c>, + Edge<root, llvm_gcc_assembler>, + ... + + Edge<llvm_gcc_c, llc>, + Edge<llvm_gcc_cpp, llc>, + ... + + OptionalEdge<llvm_gcc_c, opt, [(switch_on "opt")]>, + OptionalEdge<llvm_gcc_cpp, opt, [(switch_on "opt")]>, + ... + + OptionalEdge<llvm_gcc_assembler, llvm_gcc_cpp_linker, + (case (input_languages_contain "c++"), (inc_weight), + (or (parameter_equals "linker", "g++"), + (parameter_equals "linker", "c++")), (inc_weight))>, + ... + + ]>; ++
As you can see, the edges can be either default or optional, where +optional edges are differentiated by sporting a case expression +used to calculate the edge's weight.
+The default edges are assigned a weight of 1, and optional edges get a +weight of 0 + 2*N where N is the number of tests that evaluated to +true in the case expression. It is also possible to provide an +integer parameter to inc_weight and dec_weight - in this case, +the weight is increased (or decreased) by the provided value instead +of the default 2.
+When passing an input file through the graph, LLVMC picks the edge +with the maximum weight. To avoid ambiguity, there should be only one +default edge between two nodes (with the exception of the root node, +which gets a special treatment - there you are allowed to specify one +default edge per language).
+To get a visual representation of the compilation graph (useful for +debugging), run llvmc2 --view-graph. You will need dot and +gsview installed for this to work properly.
+As was said earlier, nodes in the compilation graph represent tools, +which are described separately. A tool definition looks like this +(taken from the Tools.td file):
++def llvm_gcc_cpp : Tool<[ + (in_language "c++"), + (out_language "llvm-assembler"), + (output_suffix "bc"), + (cmd_line "llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm"), + (sink) + ]>; ++
This defines a new tool called llvm_gcc_cpp, which is an alias for +llvm-g++. As you can see, a tool definition is just a list of +properties; most of them should be self-explanatory. The sink +property means that this tool should be passed all command-line +options that lack explicit descriptions.
+The complete list of the currently implemented tool properties follows:
+The next tool definition is slightly more complex:
++def llvm_gcc_linker : Tool<[ + (in_language "object-code"), + (out_language "executable"), + (output_suffix "out"), + (cmd_line "llvm-gcc $INFILE -o $OUTFILE"), + (join), + (prefix_list_option "L", (forward), + (help "add a directory to link path")), + (prefix_list_option "l", (forward), + (help "search a library when linking")), + (prefix_list_option "Wl", (unpack_values), + (help "pass options to linker")) + ]>; ++
This tool has a "join" property, which means that it behaves like a +linker. This tool also defines several command-line options: -l, +-L and -Wl which have their usual meaning. An option has two +attributes: a name and a (possibly empty) list of properties. All +currently implemented option types and properties are described below:
+Possible option types:
++++
+- switch_option - a simple boolean switch, for example -time.
+- parameter_option - option that takes an argument, for example +-std=c99;
+- parameter_list_option - same as the above, but more than one +occurence of the option is allowed.
+- prefix_option - same as the parameter_option, but the option name +and parameter value are not separated.
+- prefix_list_option - same as the above, but more than one +occurence of the option is allowed; example: -lm -lpthread.
+- alias_option - a special option type for creating +aliases. Unlike other option types, aliases are not allowed to +have any properties besides the aliased option name. Usage +example: (alias_option "preprocess", "E")
+
Possible option properties:
++++
+- append_cmd - append a string to the tool invocation command.
+- forward - forward this option unchanged.
+- output_suffix - modify the output suffix of this +tool. Example : (switch "E", (output_suffix "i").
+- stop_compilation - stop compilation after this phase.
+- unpack_values - used for for splitting and forwarding +comma-separated lists of options, e.g. -Wa,-foo=bar,-baz is +converted to -foo=bar -baz and appended to the tool invocation +command.
+- help - help string associated with this option. Used for +--help output.
+- required - this option is obligatory.
+
It can be handy to have all information about options gathered in a +single place to provide an overview. This can be achieved by using a +so-called OptionList:
++def Options : OptionList<[ +(switch_option "E", (help "Help string")), +(alias_option "quiet", "q") +... +]>; ++
OptionList is also a good place to specify option aliases.
+Tool-specific option properties like append_cmd have (obviously) +no meaning in the context of OptionList, so the only properties +allowed there are help and required.
+Option lists are used at the file scope. See file +examples/Clang.td for an example of OptionList usage.
+Normally, LLVMC executes programs from the system PATH. Sometimes, +this is not sufficient: for example, we may want to specify tool names +in the configuration file. This can be achieved via the mechanism of +hooks - to compile LLVMC with your hooks, just drop a .cpp file into +tools/llvmc2 directory. Hooks should live in the hooks +namespace and have the signature std::string hooks::MyHookName +(void). They can be used from the cmd_line tool property:
++(cmd_line "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)") ++
It is also possible to use environment variables in the same manner:
++(cmd_line "$ENV(VAR1)/path/to/file -o $ENV(VAR2)") ++
To change the command line string based on user-provided options use +the case expression (documented below):
++(cmd_line + (case + (switch_on "E"), + "llvm-g++ -E -x c $INFILE -o $OUTFILE", + (default), + "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm")) ++
The 'case' construct can be used to calculate weights of the optional +edges and to choose between several alternative command line strings +in the cmd_line tool property. It is designed after the +similarly-named construct in functional languages and takes the form +(case (test_1), statement_1, (test_2), statement_2, ... (test_N), +statement_N). The statements are evaluated only if the corresponding +tests evaluate to true.
+Examples:
++// Increases edge weight by 5 if "-A" is provided on the +// command-line, and by 5 more if "-B" is also provided. +(case + (switch_on "A"), (inc_weight 5), + (switch_on "B"), (inc_weight 5)) + +// Evaluates to "cmdline1" if option "-A" is provided on the +// command line, otherwise to "cmdline2" +(case + (switch_on "A"), "cmdline1", + (switch_on "B"), "cmdline2", + (default), "cmdline3") ++
Note the slight difference in 'case' expression handling in contexts +of edge weights and command line specification - in the second example +the value of the "B" switch is never checked when switch "A" is +enabled, and the whole expression always evaluates to "cmdline1" in +that case.
+Case expressions can also be nested, i.e. the following is legal:
++(case (switch_on "E"), (case (switch_on "o"), ..., (default), ...) + (default), ...) ++
You should, however, try to avoid doing that because it hurts +readability. It is usually better to split tool descriptions and/or +use TableGen inheritance instead.
+One last thing that you will need to modify when adding support for a +new language to LLVMC is the language map, which defines mappings from +file extensions to language names. It is used to choose the proper +toolchain(s) for a given input file set. Language map definition is +located in the file Tools.td and looks like this:
++def LanguageMap : LanguageMap< + [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>, + LangToSuffixes<"c", ["c"]>, + ... + ]>; ++
| [1] | TableGen Fundamentals +http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html |
The main features of llvmc2 are: +
The main features of llvmc2 are:
LLVM 2.3 includes a completely rewritten interface for Link Time Optimization. This interface @@ -285,7 +285,7 @@
-svn co http://llvm.org/svn/llvm-project/vmkit/trunk vmkit -
+svn co http://llvm.org/svn/llvm-project/vmkit/trunk vmkit+
for (i = LB; i < UB; ++i)
if (i <= NV)
LOOP_BODY
+is transformed into:
+NUB = min(NV+1, UB) for (i = LB; i < NUB; ++i) LOOP_BODY+
Alias Analysis (aka Pointer Analysis) is a class of techniques which attempt + to determine whether or not two pointers ever can point to the same object in + memory. There are many different algorithms for alias analysis and many + different ways of classifying them: flow-sensitive vs flow-insensitive, + context-sensitive vs context-insensitive, field-sensitive vs field-insensitive, + unification-based vs subset-based, etc. Traditionally, alias analyses respond + to a query with a Must, May, or No alias response, + indicating that two pointers always point to the same object, might point to the + same object, or are known to never point to the same object.
+ +The LLVM AliasAnalysis + class is the primary interface used by clients and implementations of alias + analyses in the LLVM system. This class is the common interface between clients + of alias analysis information and the implementations providing it, and is + designed to support a wide range of implementations and clients (but currently + all clients are assumed to be flow-insensitive). In addition to simple alias + analysis information, this class exposes Mod/Ref information from those + implementations which can provide it, allowing for powerful analyses and + transformations to work well together.
+ +This document contains information necessary to successfully implement this + interface, use it, and to test both sides. It also explains some of the finer + points about what exactly results mean. If you feel that something is unclear + or should be added, please let me + know.
+ +The AliasAnalysis + class defines the interface that the various alias analysis implementations + should support. This class exports two important enums: AliasResult + and ModRefResult which represent the result of an alias query or a + mod/ref query, respectively.
+ +The AliasAnalysis interface exposes information about memory, + represented in several different ways. In particular, memory objects are + represented as a starting address and size, and function calls are represented + as the actual call or invoke instructions that performs the + call. The AliasAnalysis interface also exposes some helper methods + which allow you to get mod/ref information for arbitrary instructions.
+ +Most importantly, the AliasAnalysis class provides several methods + which are used to query whether or not two memory objects alias, whether + function calls can modify or read a memory object, etc. For all of these + queries, memory objects are represented as a pair of their starting address (a + symbolic LLVM Value*) and a static size.
+ +Representing memory objects as a starting address and a size is critically + important for correct Alias Analyses. For example, consider this (silly, but + possible) C code:
+ +
+ int i;
+ char C[2];
+ char A[10];
+ /* ... */
+ for (i = 0; i != 10; ++i) {
+ C[0] = A[i]; /* One byte store */
+ C[1] = A[9-i]; /* One byte store */
+ }
+
+ In this case, the basicaa pass will disambiguate the stores to + C[0] and C[1] because they are accesses to two distinct + locations one byte apart, and the accesses are each one byte. In this case, the + LICM pass can use store motion to remove the stores from the loop. In + constrast, the following code:
+ +
+ int i;
+ char C[2];
+ char A[10];
+ /* ... */
+ for (i = 0; i != 10; ++i) {
+ ((short*)C)[0] = A[i]; /* Two byte store! */
+ C[1] = A[9-i]; /* One byte store */
+ }
+
+ In this case, the two stores to C do alias each other, because the access to + the &C[0] element is a two byte access. If size information wasn't + available in the query, even the first case would have to conservatively assume + that the accesses alias.
+ +An Alias Analysis implementation can return one of three responses: + MustAlias, MayAlias, and NoAlias. The No and May alias results are obvious: if + the two pointers can never equal each other, return NoAlias, if they might, + return MayAlias.
+ +The MustAlias response is trickier though. In LLVM, the Must Alias response + may only be returned if the two memory objects are guaranteed to always start at + exactly the same location. If two memory objects overlap, but do not start at + the same location, return MayAlias.
+ +The getModRefInfo methods return information about whether the + execution of an instruction can read or modify a memory location. Mod/Ref + information is always conservative: if an instruction might read or write + a location, ModRef is returned.
+ +The AliasAnalysis class also provides a getModRefInfo + method for testing dependencies between function calls. This method takes two + call sites (CS1 & CS2), returns NoModRef if the two calls refer to disjoint + memory locations, Ref if CS1 reads memory written by CS2, Mod if CS1 writes to + memory read or written by CS2, or ModRef if CS1 might read or write memory + accessed by CS2. Note that this relation is not commutative. Clients that use + this method should be predicated on the hasNoModRefInfoForCalls() + method, which indicates whether or not an analysis can provide mod/ref + information for function call pairs (most can not). If this predicate is false, + the client shouldn't waste analysis time querying the getModRefInfo + method many times.
+ ++ Several other tidbits of information are often collected by various alias + analysis implementations and can be put to good use by various clients. +
+ +The getMustAliases method returns all values that are known to + always must alias a pointer. This information can be provided in some cases for + important objects like the null pointer and global values. Knowing that a + pointer always points to a particular function allows indirect calls to be + turned into direct calls, for example.
+ +The pointsToConstantMemory method returns true if and only if the + analysis can prove that the pointer only points to unchanging memory locations + (functions, constant global variables, and the null pointer). This information + can be used to refine mod/ref information: it is impossible for an unchanging + memory location to be modified.
+ +These methods are used to provide very simple mod/ref information for + function calls. The doesNotAccessMemory method returns true for a + function if the analysis can prove that the function never reads or writes to + memory, or if the function only reads from constant memory. Functions with this + property are side-effect free and only depend on their input arguments, allowing + them to be eliminated if they form common subexpressions or be hoisted out of + loops. Many common functions behave this way (e.g., sin and + cos) but many others do not (e.g., acos, which modifies the + errno variable).
+ +The onlyReadsMemory method returns true for a function if analysis + can prove that (at most) the function only reads from non-volatile memory. + Functions with this property are side-effect free, only depending on their input + arguments and the state of memory when they are called. This property allows + calls to these functions to be eliminated and moved around, as long as there is + no store instruction that changes the contents of memory. Note that all + functions that satisfy the doesNotAccessMemory method also satisfies + onlyReadsMemory.
+ +Writing a new alias analysis implementation for LLVM is quite + straight-forward. There are already several implementations that you can use + for examples, and the following information should help fill in any details. + For a examples, take a look at the various alias analysis + implementations included with LLVM.
+ +The first step to determining what type of LLVM pass you need to use for your Alias + Analysis. As is the case with most other analyses and transformations, the + answer should be fairly obvious from what type of problem you are trying to + solve:
+ +In addition to the pass that you subclass, you should also inherit from the + AliasAnalysis interface, of course, and use the + RegisterAnalysisGroup template to register as an implementation of + AliasAnalysis.
+ +Your subclass of AliasAnalysis is required to invoke two methods on + the AliasAnalysis base class: getAnalysisUsage and + InitializeAliasAnalysis. In particular, your implementation of + getAnalysisUsage should explicitly call into the + AliasAnalysis::getAnalysisUsage method in addition to doing any + declaring any pass dependencies your pass has. Thus you should have something + like this:
+ +
+ void getAnalysisUsage(AnalysisUsage &AU) const {
+ AliasAnalysis::getAnalysisUsage(AU);
+ // declare your dependencies here.
+ }
+
+ Additionally, your must invoke the InitializeAliasAnalysis method + from your analysis run method (run for a Pass, + runOnFunction for a FunctionPass, or InitializePass + for an ImmutablePass). For example (as part of a Pass):
+ +
+ bool run(Module &M) {
+ InitializeAliasAnalysis(this);
+ // Perform analysis here...
+ return false;
+ }
+
+ All of the AliasAnalysis + virtual methods default to providing chaining to another + alias analysis implementation, which ends up returning conservatively correct + information (returning "May" Alias and "Mod/Ref" for alias and mod/ref queries + respectively). Depending on the capabilities of the analysis you are + implementing, you just override the interfaces you can improve.
+ +With only two special exceptions (the basicaa and no-aa + passes) every alias analysis pass chains to another alias analysis + implementation (for example, the user can specify "-basicaa -ds-aa + -anders-aa -licm" to get the maximum benefit from the three alias + analyses). The alias analysis class automatically takes care of most of this + for methods that you don't override. For methods that you do override, in code + paths that return a conservative MayAlias or Mod/Ref result, simply return + whatever the superclass computes. For example:
+ +
+ AliasAnalysis::AliasResult alias(const Value *V1, unsigned V1Size,
+ const Value *V2, unsigned V2Size) {
+ if (...)
+ return NoAlias;
+ ...
+
+ // Couldn't determine a must or no-alias result.
+ return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
+ }
+
+ In addition to analysis queries, you must make sure to unconditionally pass + LLVM update notification methods to the superclass as + well if you override them, which allows all alias analyses in a change to be + updated.
+ ++ Alias analysis information is initially computed for a static snapshot of the + program, but clients will use this information to make transformations to the + code. All but the most trivial forms of alias analysis will need to have their + analysis results updated to reflect the changes made by these transformations. +
+ ++ The AliasAnalysis interface exposes two methods which are used to + communicate program changes from the clients to the analysis implementations. + Various alias analysis implementations should use these methods to ensure that + their internal data structures are kept up-to-date as the program changes (for + example, when an instruction is deleted), and clients of alias analysis must be + sure to call these interfaces appropriately. +
+From the LLVM perspective, the only thing you need to do to provide an + efficient alias analysis is to make sure that alias analysis queries are + serviced quickly. The actual calculation of the alias analysis results (the + "run" method) is only performed once, but many (perhaps duplicate) queries may + be performed. Because of this, try to move as much computation to the run + method as possible (within reason).
+ +There are several different ways to use alias analysis results. In order of + preference, these are...
+ +The load-vn pass uses alias analysis to provide value numbering + information for load instructions and pointer values. If your analysis + or transformation can be modeled in a form that uses value numbering + information, you don't have to do anything special to handle load instructions: + just use the load-vn pass, which uses alias analysis.
+ +Many transformations need information about alias sets that are active + in some scope, rather than information about pairwise aliasing. The AliasSetTracker class + is used to efficiently build these Alias Sets from the pairwise alias analysis + information provided by the AliasAnalysis interface.
+ +First you initialize the AliasSetTracker by using the "add" methods + to add information about various potentially aliasing instructions in the scope + you are interested in. Once all of the alias sets are completed, your pass + should simply iterate through the constructed alias sets, using the + AliasSetTracker begin()/end() methods.
+ +The AliasSets formed by the AliasSetTracker are guaranteed + to be disjoint, calculate mod/ref information and volatility for the set, and + keep track of whether or not all of the pointers in the set are Must aliases. + The AliasSetTracker also makes sure that sets are properly folded due to call + instructions, and can provide a list of pointers in each set.
+ +As an example user of this, the Loop + Invariant Code Motion pass uses AliasSetTrackers to calculate alias + sets for each loop nest. If an AliasSet in a loop is not modified, + then all load instructions from that set may be hoisted out of the loop. If any + alias sets are stored to and are must alias sets, then the stores may be + sunk to outside of the loop, promoting the memory location to a register for the + duration of the loop nest. Both of these transformations only apply if the + pointer argument is loop-invariant.
+ +The AliasSetTracker class is implemented to be as efficient as possible. It + uses the union-find algorithm to efficiently merge AliasSets when a pointer is + inserted into the AliasSetTracker that aliases multiple sets. The primary data + structure is a hash table mapping pointers to the AliasSet they are in.
+ +The AliasSetTracker class must maintain a list of all of the LLVM Value*'s + that are in each AliasSet. Since the hash table already has entries for each + LLVM Value* of interest, the AliasesSets thread the linked list through these + hash-table nodes to avoid having to allocate memory unnecessarily, and to make + merging alias sets extremely efficient (the linked list merge is constant time). +
+ +You shouldn't need to understand these details if you are just a client of + the AliasSetTracker, but if you look at the code, hopefully this brief + description will help make sense of why things are designed the way they + are.
+ +If neither of these utility class are what your pass needs, you should use + the interfaces exposed by the AliasAnalysis class directly. Try to use + the higher-level methods when possible (e.g., use mod/ref information instead of + the alias method directly if possible) to get the + best precision and efficiency.
+ +If you're going to be working with the LLVM alias analysis infrastructure, + you should know what clients and implementations of alias analysis are + available. In particular, if you are implementing an alias analysis, you should + be aware of the the clients that are useful + for monitoring and evaluating different implementations.
+ +This section lists the various implementations of the AliasAnalysis + interface. With the exception of the -no-aa and + -basicaa implementations, all of these chain to other alias analysis implementations.
+ +The -no-aa pass is just like what it sounds: an alias analysis that + never returns any useful information. This pass can be useful if you think that + alias analysis is doing something wrong and are trying to narrow down a + problem.
+ +The -basicaa pass is the default LLVM alias analysis. It is an + aggressive local analysis that "knows" many important facts:
+ +This pass implements a simple context-sensitive mod/ref and alias analysis + for internal global variables that don't "have their address taken". If a + global does not have its address taken, the pass knows that no pointers alias + the global. This pass also keeps track of functions that it knows never access + memory or never read memory. This allows certain optimizations (e.g. GCSE) to + eliminate call instructions entirely. +
+ +The real power of this pass is that it provides context-sensitive mod/ref + information for call instructions. This allows the optimizer to know that + calls to a function do not clobber or read the value of the global, allowing + loads and stores to be eliminated.
+ +Note that this pass is somewhat limited in its scope (only support + non-address taken globals), but is very quick analysis.
+The -anders-aa pass implements the well-known "Andersen's algorithm" + for interprocedural alias analysis. This algorithm is a subset-based, + flow-insensitive, context-insensitive, and field-insensitive alias analysis that + is widely believed to be fairly precise. Unfortunately, this algorithm is also + O(N3). The LLVM implementation currently does not implement any of + the refinements (such as "online cycle elimination" or "offline variable + substitution") to improve its efficiency, so it can be quite slow in common + cases. +
+ +The -steens-aa pass implements a variation on the well-known + "Steensgaard's algorithm" for interprocedural alias analysis. Steensgaard's + algorithm is a unification-based, flow-insensitive, context-insensitive, and + field-insensitive alias analysis that is also very scalable (effectively linear + time).
+ +The LLVM -steens-aa pass implements a "speculatively + field-sensitive" version of Steensgaard's algorithm using the Data + Structure Analysis framework. This gives it substantially more precision than + the standard algorithm while maintaining excellent analysis scalability.
+ +Note that -steens-aa is available in the optional "poolalloc" + module, it is not part of the LLVM core.
+ +The -ds-aa pass implements the full Data Structure Analysis + algorithm. Data Structure Analysis is a modular unification-based, + flow-insensitive, context-sensitive, and speculatively + field-sensitive alias analysis that is also quite scalable, usually at + O(n*log(n)).
+ +This algorithm is capable of responding to a full variety of alias analysis + queries, and can provide context-sensitive mod/ref information as well. The + only major facility not implemented so far is support for must-alias + information.
+ +Note that -ds-aa is available in the optional "poolalloc" + module, it is not part of the LLVM core.
+ +The -adce pass, which implements Aggressive Dead Code Elimination + uses the AliasAnalysis interface to delete calls to functions that do + not have side-effects and are not used.
+ +The -licm pass implements various Loop Invariant Code Motion related + transformations. It uses the AliasAnalysis interface for several + different transformations:
+ ++ The -argpromotion pass promotes by-reference arguments to be passed in + by-value instead. In particular, if pointer arguments are only loaded from it + passes in the value loaded instead of the address to the function. This pass + uses alias information to make sure that the value loaded from the argument + pointer is not modified between the entry of the function and any load of the + pointer.
+The -load-vn pass uses alias analysis to "value + number" loads and pointers values, which is used by the GCSE pass to + eliminate instructions. The -load-vn pass relies on alias information + and must-alias information. This combination of passes can make the following + transformations:
+ +These passes are useful for evaluating the various alias analysis + implementations. You can use them with commands like 'opt -anders-aa -ds-aa + -aa-eval foo.bc -disable-output -stats'.
+ +The -print-alias-sets pass is exposed as part of the + opt tool to print out the Alias Sets formed by the AliasSetTracker class. This is useful if you're using + the AliasSetTracker class. To use it, use something like:
+ ++ % opt -ds-aa -print-alias-sets -disable-output ++
The -count-aa pass is useful to see how many queries a particular + pass is making and what responses are returned by the alias analysis. As an + example,
+ ++ % opt -basicaa -count-aa -ds-aa -count-aa -licm ++
will print out how many queries (and what responses are returned) by the + -licm pass (of the -ds-aa pass) and how many queries are made + of the -basicaa pass by the -ds-aa pass. This can be useful + when debugging a transformation or an alias analysis implementation.
+ +The -aa-eval pass simply iterates through all pairs of pointers in a + function and asks an alias analysis whether or not the pointers alias. This + gives an indication of the precision of the alias analysis. Statistics are + printed indicating the percent of no/may/must aliases found (a more precise + algorithm will have a lower number of may aliases).
+ +If you're just looking to be a client of alias analysis information, consider + using the Memory Dependence Analysis interface instead. MemDep is a lazy, + caching layer on top of alias analysis that is able to answer the question of + what preceding memory operations a given instruction depends on, either at an + intra- or inter-block level. Because of its laziness and caching + policy, using MemDep can be a significant performance win over accessing alias + analysis directly.
+ +This document describes the LLVM bitstream file format and the encoding of + the LLVM IR into it.
+ ++ What is commonly known as the LLVM bitcode file format (also, sometimes + anachronistically known as bytecode) is actually two things: a bitstream container format + and an encoding of LLVM IR into the container format.
+ ++ The bitstream format is an abstract encoding of structured data, very + similar to XML in some ways. Like XML, bitstream files contain tags, and nested + structures, and you can parse the file without having to understand the tags. + Unlike XML, the bitstream format is a binary encoding, and unlike XML it + provides a mechanism for the file to self-describe "abbreviations", which are + effectively size optimizations for the content.
+ +This document first describes the LLVM bitstream format, then describes the + record structure used by LLVM IR files. +
+ ++ The bitstream format is literally a stream of bits, with a very simple + structure. This structure consists of the following concepts: +
+ +Note that the llvm-bcanalyzer tool can be + used to dump and inspect arbitrary bitstreams, which is very useful for + understanding the encoding.
+ +The first two bytes of a bitcode file are 'BC' (0x42, 0x43). + The second two bytes are an application-specific magic number. Generic + bitcode tools can look at only the first two bytes to verify the file is + bitcode, while application-specific programs will want to look at all four.
+ ++ A bitstream literally consists of a stream of bits, which are read in order + starting with the least significant bit of each byte. The stream is made up of a + number of primitive values that encode a stream of unsigned integer values. + These + integers are are encoded in two ways: either as Fixed + Width Integers or as Variable Width + Integers. +
+ +Fixed-width integer values have their low bits emitted directly to the file. + For example, a 3-bit integer value encodes 1 as 001. Fixed width integers + are used when there are a well-known number of options for a field. For + example, boolean values are usually encoded with a 1-bit wide integer. +
+ +Variable-width integer (VBR) values encode values of arbitrary size, + optimizing for the case where the values are small. Given a 4-bit VBR field, + any 3-bit value (0 through 7) is encoded directly, with the high bit set to + zero. Values larger than N-1 bits emit their bits in a series of N-1 bit + chunks, where all but the last set the high bit.
+ +For example, the value 27 (0x1B) is encoded as 1011 0011 when emitted as a + vbr4 value. The first set of four bits indicates the value 3 (011) with a + continuation piece (indicated by a high bit of 1). The next word indicates a + value of 24 (011 << 3) with no continuation. The sum (3+24) yields the value + 27. +
+ +6-bit characters encode common characters into a fixed 6-bit field. They + represent the following characters with the following 6-bit values:
+ +This encoding is only suitable for encoding characters and strings that + consist only of the above characters. It is completely incapable of encoding + characters not in the set.
+ +Occasionally, it is useful to emit zero bits until the bitstream is a + multiple of 32 bits. This ensures that the bit position in the stream can be + represented as a multiple of 32-bit words.
+ ++ A bitstream is a sequential series of Blocks and + Data Records. Both of these start with an + abbreviation ID encoded as a fixed-bitwidth field. The width is specified by + the current block, as described below. The value of the abbreviation ID + specifies either a builtin ID (which have special meanings, defined below) or + one of the abbreviation IDs defined by the stream itself. +
+ ++ The set of builtin abbrev IDs is: +
+ +Abbreviation IDs 4 and above are defined by the stream itself, and specify + an abbreviated record encoding.
+ ++ Blocks in a bitstream denote nested regions of the stream, and are identified by + a content-specific id number (for example, LLVM IR uses an ID of 12 to represent + function bodies). Block IDs 0-7 are reserved for standard blocks + whose meaning is defined by Bitcode; block IDs 8 and greater are + application specific. Nested blocks capture the hierachical structure of the data + encoded in it, and various properties are associated with blocks as the file is + parsed. Block definitions allow the reader to efficiently skip blocks + in constant time if the reader wants a summary of blocks, or if it wants to + efficiently skip data they do not understand. The LLVM IR reader uses this + mechanism to skip function bodies, lazily reading them on demand. +
+ ++ When reading and encoding the stream, several properties are maintained for the + block. In particular, each block maintains: +
+ +As sub blocks are entered, these properties are saved and the new sub-block + has its own set of abbreviations, and its own abbrev id width. When a sub-block + is popped, the saved values are restored.
+ +[ENTER_SUBBLOCK, blockidvbr8, newabbrevlenvbr4, + <align32bits>, blocklen32]
+ ++ The ENTER_SUBBLOCK abbreviation ID specifies the start of a new block record. + The blockid value is encoded as a 8-bit VBR identifier, and indicates + the type of block being entered (which can be a standard + block or an application-specific block). The + newabbrevlen value is a 4-bit VBR which specifies the + abbrev id width for the sub-block. The blocklen is a 32-bit aligned + value that specifies the size of the subblock, in 32-bit words. This value + allows the reader to skip over the entire block in one jump. +
+ +[END_BLOCK, <align32bits>]
+ ++ The END_BLOCK abbreviation ID specifies the end of the current block record. + Its end is aligned to 32-bits to ensure that the size of the block is an even + multiple of 32-bits.
+ ++ Data records consist of a record code and a number of (up to) 64-bit integer + values. The interpretation of the code and values is application specific and + there are multiple different ways to encode a record (with an unabbrev record + or with an abbreviation). In the LLVM IR format, for example, there is a record + which encodes the target triple of a module. The code is MODULE_CODE_TRIPLE, + and the values of the record are the ascii codes for the characters in the + string.
+ +[UNABBREV_RECORD, codevbr6, numopsvbr6, + op0vbr6, op1vbr6, ...]
+ +An UNABBREV_RECORD provides a default fallback encoding, which is both + completely general and also extremely inefficient. It can describe an arbitrary + record, by emitting the code and operands as vbrs.
+ +For example, emitting an LLVM IR target triple as an unabbreviated record + requires emitting the UNABBREV_RECORD abbrevid, a vbr6 for the + MODULE_CODE_TRIPLE code, a vbr6 for the length of the string (which is equal to + the number of operands), and a vbr6 for each character. Since there are no + letters with value less than 32, each letter would need to be emitted as at + least a two-part VBR, which means that each letter would require at least 12 + bits. This is not an efficient encoding, but it is fully general.
+ +[<abbrevid>, fields...]
+ +An abbreviated record is a abbreviation id followed by a set of fields that + are encoded according to the abbreviation + definition. This allows records to be encoded significantly more densely + than records encoded with the UNABBREV_RECORD + type, and allows the abbreviation types to be specified in the stream itself, + which allows the files to be completely self describing. The actual encoding + of abbreviations is defined below. +
+ ++ Abbreviations are an important form of compression for bitstreams. The idea is + to specify a dense encoding for a class of records once, then use that encoding + to emit many records. It takes space to emit the encoding into the file, but + the space is recouped (hopefully plus some) when the records that use it are + emitted. +
+ ++ Abbreviations can be determined dynamically per client, per file. Since the + abbreviations are stored in the bitstream itself, different streams of the same + format can contain different sets of abbreviations if the specific stream does + not need it. As a concrete example, LLVM IR files usually emit an abbreviation + for binary operators. If a specific LLVM module contained no or few binary + operators, the abbreviation does not need to be emitted. +
+[DEFINE_ABBREV, numabbrevopsvbr5, abbrevop0, abbrevop1, + ...]
+ +A DEFINE_ABBREV record adds an abbreviation to the list of currently + defined abbreviations in the scope of this block. This definition only + exists inside this immediate block -- it is not visible in subblocks or + enclosing blocks. + Abbreviations are implicitly assigned IDs + sequentially starting from 4 (the first application-defined abbreviation ID). + Any abbreviations defined in a BLOCKINFO record receive IDs first, in order, + followed by any abbreviations defined within the block itself. + Abbreviated data records reference this ID to indicate what abbreviation + they are invoking.
+ +An abbreviation definition consists of the DEFINE_ABBREV abbrevid followed + by a VBR that specifies the number of abbrev operands, then the abbrev + operands themselves. Abbreviation operands come in three forms. They all start + with a single bit that indicates whether the abbrev operand is a literal operand + (when the bit is 1) or an encoding operand (when the bit is 0).
+ +The possible operand encodings are:
+ +For example, target triples in LLVM modules are encoded as a record of the + form [TRIPLE, 'a', 'b', 'c', 'd']. Consider if the bitstream emitted + the following abbrev entry:
+ +When emitting a record with this abbreviation, the above entry would be + emitted as:
+ +[4abbrevwidth, 24, 4vbr6, + 06, 16, 26, 36]
+ +These values are:
+ +With this abbreviation, the triple is emitted with only 37 bits (assuming a + abbrev id width of 3). Without the abbreviation, significantly more space would + be required to emit the target triple. Also, since the TRIPLE value is not + emitted as a literal in the abbreviation, the abbreviation can also be used for + any other string value. +
+ ++ In addition to the basic block structure and record encodings, the bitstream + also defines specific builtin block types. These block types specify how the + stream is to be decoded or other metadata. In the future, new standard blocks + may be added. Block IDs 0-7 are reserved for standard blocks. +
+ +The BLOCKINFO block allows the description of metadata for other blocks. The + currently specified records are:
+ ++ The SETBID record indicates which block ID is being described. SETBID + records can occur multiple times throughout the block to change which + block ID is being described. There must be a SETBID record prior to + any other records. +
+ ++ Standard DEFINE_ABBREV records can occur inside BLOCKINFO blocks, but unlike + their occurrence in normal blocks, the abbreviation is defined for blocks + matching the block ID we are describing, not the BLOCKINFO block itself. + The abbreviations defined in BLOCKINFO blocks receive abbreviation ids + as described in DEFINE_ABBREV. +
+ ++ Note that although the data in BLOCKINFO blocks is described as "metadata," the + abbreviations they contain are essential for parsing records from the + corresponding blocks. It is not safe to skip them. +
+ +LLVM IR is encoded into a bitstream by defining blocks and records. It uses + blocks for things like constant pools, functions, symbol tables, etc. It uses + records for things like instructions, global variable descriptors, type + descriptions, etc. This document does not describe the set of abbreviations + that the writer uses, as these are fully self-described in the file, and the + reader is not allowed to build in any knowledge of this.
+ ++ The magic number for LLVM IR files is: +
+ +[0x04, 0xC4, 0xE4, 0xD4]
+ +When combined with the bitcode magic number and viewed as bytes, this is "BC 0xC0DE".
+ ++ Variable Width Integers are an efficient way to + encode arbitrary sized unsigned values, but is an extremely inefficient way to + encode signed values (as signed values are otherwise treated as maximally large + unsigned values).
+ +As such, signed vbr values of a specific width are emitted as follows:
+ +With this encoding, small positive and small negative values can both be + emitted efficiently.
+ ++ LLVM IR is defined with the following blocks: +
+ ++
+ +bugpoint narrows down the source of problems in LLVM tools and + passes. It can be used to debug three types of failures: optimizer crashes, + miscompilations by optimizers, or bad native code generation (including problems + in the static and JIT compilers). It aims to reduce large test cases to small, + useful ones. For example, if opt crashes while optimizing a + file, it will identify the optimization (or combination of optimizations) that + causes the crash, and reduce the file down to a small example which triggers the + crash.
+ +For detailed case scenarios, such as debugging opt, + llvm-ld, or one of the LLVM code generators, see How To Submit a Bug Report document.
+ +bugpoint is designed to be a useful tool without requiring any + hooks into the LLVM infrastructure at all. It works with any and all LLVM + passes and code generators, and does not need to "know" how they work. Because + of this, it may appear to do stupid things or miss obvious + simplifications. bugpoint is also designed to trade off programmer + time for computer time in the compiler-debugging process; consequently, it may + take a long period of (unattended) time to reduce a test case, but we feel it + is still worth it. Note that bugpoint is generally very quick unless + debugging a miscompilation where each test of the program (which requires + executing it) takes a long time.
+ +bugpoint reads each .bc or .ll file specified on + the command line and links them together into a single module, called the test + program. If any LLVM passes are specified on the command line, it runs these + passes on the test program. If any of the passes crash, or if they produce + malformed output (which causes the verifier to abort), bugpoint starts + the crash debugger.
+ +Otherwise, if the -output option was not specified, + bugpoint runs the test program with the C backend (which is assumed to + generate good code) to generate a reference output. Once bugpoint has + a reference output for the test program, it tries executing it with the + selected code generator. If the selected code generator crashes, + bugpoint starts the crash debugger on the + code generator. Otherwise, if the resulting output differs from the reference + output, it assumes the difference resulted from a code generator failure, and + starts the code generator debugger.
+ +Finally, if the output of the selected code generator matches the reference + output, bugpoint runs the test program after all of the LLVM passes + have been applied to it. If its output differs from the reference output, it + assumes the difference resulted from a failure in one of the LLVM passes, and + enters the miscompilation debugger. + Otherwise, there is no problem bugpoint can debug.
+ +If an optimizer or code generator crashes, bugpoint will try as hard + as it can to reduce the list of passes (for optimizer crashes) and the size of + the test program. First, bugpoint figures out which combination of + optimizer passes triggers the bug. This is useful when debugging a problem + exposed by opt, for example, because it runs over 38 passes.
+ +Next, bugpoint tries removing functions from the test program, to + reduce its size. Usually it is able to reduce a test program to a single + function, when debugging intraprocedural optimizations. Once the number of + functions has been reduced, it attempts to delete various edges in the control + flow graph, to reduce the size of the function as much as possible. Finally, + bugpoint deletes any individual LLVM instructions whose absence does + not eliminate the failure. At the end, bugpoint should tell you what + passes crash, give you a bitcode file, and give you instructions on how to + reproduce the failure with opt or llc.
+ +The code generator debugger attempts to narrow down the amount of code that + is being miscompiled by the selected code generator. To do this, it takes the + test program and partitions it into two pieces: one piece which it compiles + with the C backend (into a shared object), and one piece which it runs with + either the JIT or the static LLC compiler. It uses several techniques to + reduce the amount of code pushed through the LLVM code generator, to reduce the + potential scope of the problem. After it is finished, it emits two bitcode + files (called "test" [to be compiled with the code generator] and "safe" [to be + compiled with the C backend], respectively), and instructions for reproducing + the problem. The code generator debugger assumes that the C backend produces + good code.
+ +The miscompilation debugger works similarly to the code generator debugger. + It works by splitting the test program into two pieces, running the + optimizations specified on one piece, linking the two pieces back together, and + then executing the result. It attempts to narrow down the list of passes to + the one (or few) which are causing the miscompilation, then reduce the portion + of the test program which is being miscompiled. The miscompilation debugger + assumes that the selected code generator is working properly.
+ ++ +
bugpoint can generate a lot of output and run for a long period + of time. It is often useful to capture the output of the program to file. + For example, in the C shell, you can run:
+ +bugpoint ... |& tee bugpoint.log
+to get a copy of bugpoint's output in the file + bugpoint.log, as well as on your terminal.
+ +Warning: This is a work in progress.
+The LLVM target-independent code generator is a framework that provides a + suite of reusable components for translating the LLVM internal representation to + the machine code for a specified target—either in assembly form (suitable + for a static compiler) or in binary machine code format (usable for a JIT + compiler). The LLVM target-independent code generator consists of five main + components:
+ ++ Depending on which part of the code generator you are interested in working on, + different pieces of this will be useful to you. In any case, you should be + familiar with the target description and machine code representation classes. If you want to add + a backend for a new target, you will need to implement the + target description classes for your new target and understand the LLVM code representation. If you are interested in + implementing a new code generation algorithm, it + should only depend on the target-description and machine code representation + classes, ensuring that it is portable. +
+ +The two pieces of the LLVM code generator are the high-level interface to the + code generator and the set of reusable components that can be used to build + target-specific backends. The two most important interfaces (TargetMachine and TargetData) are the only ones that are + required to be defined for a backend to fit into the LLVM system, but the others + must be defined if the reusable code generator components are going to be + used.
+ +This design has two important implications. The first is that LLVM can + support completely non-traditional code generation targets. For example, the C + backend does not require register allocation, instruction selection, or any of + the other standard components provided by the system. As such, it only + implements these two interfaces, and does its own thing. Another example of a + code generator like this is a (purely hypothetical) backend that converts LLVM + to the GCC RTL form and uses GCC to emit machine code for a target.
+ +This design also implies that it is possible to design and + implement radically different code generators in the LLVM system that do not + make use of any of the built-in components. Doing so is not recommended at all, + but could be required for radically different targets that do not fit into the + LLVM machine description model: FPGAs for example.
+ +The LLVM target-independent code generator is designed to support efficient and + quality code generation for standard register-based microprocessors. Code + generation in this model is divided into the following stages:
+ +The code generator is based on the assumption that the instruction selector + will use an optimal pattern matching selector to create high-quality sequences of + native instructions. Alternative code generator designs based on pattern + expansion and aggressive iterative peephole optimization are much slower. This + design permits efficient compilation (important for JIT environments) and + aggressive optimization (used when generating code offline) by allowing + components of varying levels of sophistication to be used for any step of + compilation.
+ +In addition to these stages, target implementations can insert arbitrary + target-specific passes into the flow. For example, the X86 target uses a + special pass to handle the 80x87 floating point stack architecture. Other + targets with unusual requirements can be supported with custom passes as + needed.
+ +The target description classes require a detailed description of the target + architecture. These target descriptions often have a large amount of common + information (e.g., an add instruction is almost identical to a + sub instruction). + In order to allow the maximum amount of commonality to be factored out, the LLVM + code generator uses the TableGen tool to + describe big chunks of the target machine, which allows the use of + domain-specific and target-specific abstractions to reduce the amount of + repetition.
+ +As LLVM continues to be developed and refined, we plan to move more and more + of the target description to the .td form. Doing so gives us a + number of advantages. The most important is that it makes it easier to port + LLVM because it reduces the amount of C++ code that has to be written, and the + surface area of the code generator that needs to be understood before someone + can get something working. Second, it makes it easier to change things. In + particular, if tables and other things are all emitted by tblgen, we + only need a change in one place (tblgen) to update all of the targets + to a new interface.
+ +The LLVM target description classes (located in the + include/llvm/Target directory) provide an abstract description of the + target machine independent of any particular client. These classes are + designed to capture the abstract properties of the target (such as the + instructions and registers it has), and do not incorporate any particular pieces + of code generation algorithms.
+ +All of the target description classes (except the TargetData class) are designed to be subclassed by + the concrete target implementation, and have virtual methods implemented. To + get to these implementations, the TargetMachine class provides accessors that + should be implemented by the target.
+ +The TargetMachine class provides virtual methods that are used to + access the target-specific implementations of the various target description + classes via the get*Info methods (getInstrInfo, + getRegisterInfo, getFrameInfo, etc.). This class is + designed to be specialized by + a concrete target implementation (e.g., X86TargetMachine) which + implements the various virtual methods. The only required target description + class is the TargetData class, but if the + code generator components are to be used, the other interfaces should be + implemented as well.
+ +The TargetData class is the only required target description class, + and it is the only class that is not extensible (you cannot derived a new + class from it). TargetData specifies information about how the target + lays out memory for structures, the alignment requirements for various data + types, the size of pointers in the target, and whether the target is + little-endian or big-endian.
+ +The TargetLowering class is used by SelectionDAG based instruction + selectors primarily to describe how LLVM code should be lowered to SelectionDAG + operations. Among other things, this class indicates:
+ +The TargetRegisterInfo class is used to describe the register + file of the target and any interactions between the registers.
+ +Registers in the code generator are represented in the code generator by + unsigned integers. Physical registers (those that actually exist in the target + description) are unique small numbers, and virtual registers are generally + large. Note that register #0 is reserved as a flag value.
+ +Each register in the processor description has an associated + TargetRegisterDesc entry, which provides a textual name for the + register (used for assembly output and debugging dumps) and a set of aliases + (used to indicate whether one register overlaps with another). +
+ +In addition to the per-register description, the TargetRegisterInfo + class exposes a set of processor specific register classes (instances of the + TargetRegisterClass class). Each register class contains sets of + registers that have the same properties (for example, they are all 32-bit + integer registers). Each SSA virtual register created by the instruction + selector has an associated register class. When the register allocator runs, it + replaces virtual registers with a physical register in the set.
+ ++ The target-specific implementations of these classes is auto-generated from a TableGen description of the register file. +
+ +The TargetInstrInfo class is used to describe the machine + instructions supported by the target. It is essentially an array of + TargetInstrDescriptor objects, each of which describes one + instruction the target supports. Descriptors define things like the mnemonic + for the opcode, the number of operands, the list of implicit register uses + and defs, whether the instruction has certain target-independent properties + (accesses memory, is commutable, etc), and holds any target-specific + flags.
+The TargetFrameInfo class is used to provide information about the + stack frame layout of the target. It holds the direction of stack growth, + the known stack alignment on entry to each function, and the offset to the + local area. The offset to the local area is the offset from the stack + pointer on function entry to the first location where function data (local + variables, spill locations) can be stored.
+The TargetSubtarget class is used to provide information about the + specific chip set being targeted. A sub-target informs code generation of + which instructions are supported, instruction latencies and instruction + execution itinerary; i.e., which processing units are used, in what order, and + for how long.
+The TargetJITInfo class exposes an abstract interface used by the + Just-In-Time code generator to perform target-specific activities, such as + emitting stubs. If a TargetMachine supports JIT code generation, it + should provide one of these objects through the getJITInfo + method.
+At the high-level, LLVM code is translated to a machine specific + representation formed out of + MachineFunction, + MachineBasicBlock, and MachineInstr instances + (defined in include/llvm/CodeGen). This representation is completely + target agnostic, representing instructions in their most abstract form: an + opcode and a series of operands. This representation is designed to support + both an SSA representation for machine code, as well as a register allocated, + non-SSA form.
+ +Target machine instructions are represented as instances of the + MachineInstr class. This class is an extremely abstract way of + representing machine instructions. In particular, it only keeps track of + an opcode number and a set of operands.
+ +The opcode number is a simple unsigned integer that only has meaning to a + specific backend. All of the instructions for a target should be defined in + the *InstrInfo.td file for the target. The opcode enum values + are auto-generated from this description. The MachineInstr class does + not have any information about how to interpret the instruction (i.e., what the + semantics of the instruction are); for that you must refer to the + TargetInstrInfo class.
+ +The operands of a machine instruction can be of several different types: + a register reference, a constant integer, a basic block reference, etc. In + addition, a machine operand should be marked as a def or a use of the value + (though only registers are allowed to be defs).
+ +By convention, the LLVM code generator orders instruction operands so that + all register definitions come before the register uses, even on architectures + that are normally printed in other orders. For example, the SPARC add + instruction: "add %i1, %i2, %i3" adds the "%i1", and "%i2" registers + and stores the result into the "%i3" register. In the LLVM code generator, + the operands should be stored as "%i3, %i1, %i2": with the destination + first.
+ +Keeping destination (definition) operands at the beginning of the operand + list has several advantages. In particular, the debugging printer will print + the instruction like this:
+ ++ %r3 = add %i1, %i2 ++
Also if the first operand is a def, it is easier to create instructions whose only def is the first + operand.
+ +Machine instructions are created by using the BuildMI functions, + located in the include/llvm/CodeGen/MachineInstrBuilder.h file. The + BuildMI functions make it easy to build arbitrary machine + instructions. Usage of the BuildMI functions look like this:
+ ++ // Create a 'DestReg = mov 42' (rendered in X86 assembly as 'mov DestReg, 42') + // instruction. The '1' specifies how many operands will be added. + MachineInstr *MI = BuildMI(X86::MOV32ri, 1, DestReg).addImm(42); + + // Create the same instr, but insert it at the end of a basic block. + MachineBasicBlock &MBB = ... + BuildMI(MBB, X86::MOV32ri, 1, DestReg).addImm(42); + + // Create the same instr, but insert it before a specified iterator point. + MachineBasicBlock::iterator MBBI = ... + BuildMI(MBB, MBBI, X86::MOV32ri, 1, DestReg).addImm(42); + + // Create a 'cmp Reg, 0' instruction, no destination reg. + MI = BuildMI(X86::CMP32ri, 2).addReg(Reg).addImm(0); + // Create an 'sahf' instruction which takes no operands and stores nothing. + MI = BuildMI(X86::SAHF, 0); + + // Create a self looping branch instruction. + BuildMI(MBB, X86::JNE, 1).addMBB(&MBB); ++
The key thing to remember with the BuildMI functions is that you + have to specify the number of operands that the machine instruction will take. + This allows for efficient memory allocation. You also need to specify if + operands default to be uses of values, not definitions. If you need to add a + definition operand (other than the optional destination register), you must + explicitly mark it as such:
+ ++ MI.addReg(Reg, MachineOperand::Def); ++
One important issue that the code generator needs to be aware of is the + presence of fixed registers. In particular, there are often places in the + instruction stream where the register allocator must arrange for a + particular value to be in a particular register. This can occur due to + limitations of the instruction set (e.g., the X86 can only do a 32-bit divide + with the EAX/EDX registers), or external factors like calling + conventions. In any case, the instruction selector should emit code that + copies a virtual register into or out of a physical register when needed.
+ +For example, consider this simple LLVM example:
+ +
+ int %test(int %X, int %Y) {
+ %Z = div int %X, %Y
+ ret int %Z
+ }
+
+ The X86 instruction selector produces this machine code for the div + and ret (use + "llc X.bc -march=x86 -print-machineinstrs" to get this):
+ ++ ;; Start of div + %EAX = mov %reg1024 ;; Copy X (in reg1024) into EAX + %reg1027 = sar %reg1024, 31 + %EDX = mov %reg1027 ;; Sign extend X into EDX + idiv %reg1025 ;; Divide by Y (in reg1025) + %reg1026 = mov %EAX ;; Read the result (Z) out of EAX + + ;; Start of ret + %EAX = mov %reg1026 ;; 32-bit return value goes in EAX + ret ++
By the end of code generation, the register allocator has coalesced + the registers and deleted the resultant identity moves producing the + following code:
+ ++ ;; X is in EAX, Y is in ECX + mov %EAX, %EDX + sar %EDX, 31 + idiv %ECX + ret ++
This approach is extremely general (if it can handle the X86 architecture, + it can handle anything!) and allows all of the target specific + knowledge about the instruction stream to be isolated in the instruction + selector. Note that physical registers should have a short lifetime for good + code generation, and all physical registers are assumed dead on entry to and + exit from basic blocks (before register allocation). Thus, if you need a value + to be live across basic block boundaries, it must live in a virtual + register.
+ +MachineInstr's are initially selected in SSA-form, and + are maintained in SSA-form until register allocation happens. For the most + part, this is trivially simple since LLVM is already in SSA form; LLVM PHI nodes + become machine code PHI nodes, and virtual registers are only allowed to have a + single definition.
+ +After register allocation, machine code is no longer in SSA-form because there + are no virtual registers left in the code.
+ +The MachineBasicBlock class contains a list of machine instructions + (MachineInstr instances). It roughly + corresponds to the LLVM code input to the instruction selector, but there can be + a one-to-many mapping (i.e. one LLVM basic block can map to multiple machine + basic blocks). The MachineBasicBlock class has a + "getBasicBlock" method, which returns the LLVM basic block that it + comes from.
+ +The MachineFunction class contains a list of machine basic blocks + (MachineBasicBlock instances). It + corresponds one-to-one with the LLVM function input to the instruction selector. + In addition to a list of basic blocks, the MachineFunction contains a + a MachineConstantPool, a MachineFrameInfo, a + MachineFunctionInfo, and a MachineRegisterInfo. See + include/llvm/CodeGen/MachineFunction.h for more information.
+ +This section documents the phases described in the high-level design of the code generator. It + explains how they work and some of the rationale behind their design.
+ ++ Instruction Selection is the process of translating LLVM code presented to the + code generator into target-specific machine instructions. There are several + well-known ways to do this in the literature. LLVM uses a SelectionDAG based + instruction selector. +
+ +Portions of the DAG instruction selector are generated from the target + description (*.td) files. Our goal is for the entire instruction + selector to be generated from these .td files, though currently + there are still things that require custom C++ code.
+The SelectionDAG provides an abstraction for code representation in a way + that is amenable to instruction selection using automatic techniques + (e.g. dynamic-programming based optimal pattern matching selectors). It is also + well-suited to other phases of code generation; in particular, + instruction scheduling (SelectionDAG's are very close to scheduling DAGs + post-selection). Additionally, the SelectionDAG provides a host representation + where a large variety of very-low-level (but target-independent) + optimizations may be + performed; ones which require extensive information about the instructions + efficiently supported by the target.
+ +The SelectionDAG is a Directed-Acyclic-Graph whose nodes are instances of the + SDNode class. The primary payload of the SDNode is its + operation code (Opcode) that indicates what operation the node performs and + the operands to the operation. + The various operation node types are described at the top of the + include/llvm/CodeGen/SelectionDAGNodes.h file.
+ +Although most operations define a single value, each node in the graph may + define multiple values. For example, a combined div/rem operation will define + both the dividend and the remainder. Many other situations require multiple + values as well. Each node also has some number of operands, which are edges + to the node defining the used value. Because nodes may define multiple values, + edges are represented by instances of the SDOperand class, which is + a <SDNode, unsigned> pair, indicating the node and result + value being used, respectively. Each value produced by an SDNode has + an associated MVT::ValueType indicating what type the value is.
+ +SelectionDAGs contain two different kinds of values: those that represent + data flow and those that represent control flow dependencies. Data values are + simple edges with an integer or floating point value type. Control edges are + represented as "chain" edges which are of type MVT::Other. These edges + provide an ordering between nodes that have side effects (such as + loads, stores, calls, returns, etc). All nodes that have side effects should + take a token chain as input and produce a new one as output. By convention, + token chain inputs are always operand #0, and chain results are always the last + value produced by an operation.
+ +A SelectionDAG has designated "Entry" and "Root" nodes. The Entry node is + always a marker node with an Opcode of ISD::EntryToken. The Root node + is the final side-effecting node in the token chain. For example, in a single + basic block function it would be the return node.
+ +One important concept for SelectionDAGs is the notion of a "legal" vs. + "illegal" DAG. A legal DAG for a target is one that only uses supported + operations and supported types. On a 32-bit PowerPC, for example, a DAG with + a value of type i1, i8, i16, or i64 would be illegal, as would a DAG that uses a + SREM or UREM operation. The + legalize phase is responsible for turning + an illegal DAG into a legal DAG.
+ +SelectionDAG-based instruction selection consists of the following steps:
+ +After all of these steps are complete, the SelectionDAG is destroyed and the + rest of the code generation passes are run.
+ +One great way to visualize what is going on here is to take advantage of a + few LLC command line options. In particular, the -view-isel-dags + option pops up a window with the SelectionDAG input to the Select phase for all + of the code compiled (if you only get errors printed to the console while using + this, you probably need to configure + your system to add support for it). The -view-sched-dags option + views the SelectionDAG output from the Select phase and input to the Scheduler + phase. The -view-sunit-dags option views the ScheduleDAG, which is + based on the final SelectionDAG, with nodes that must be scheduled as a unit + bundled together into a single node, and with immediate operands and other + nodes that aren't relevent for scheduling omitted. +
+ +The initial SelectionDAG is naïvely peephole expanded from the LLVM + input by the SelectionDAGLowering class in the + lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp file. The intent of this + pass is to expose as much low-level, target-specific details to the SelectionDAG + as possible. This pass is mostly hard-coded (e.g. an LLVM add turns + into an SDNode add while a geteelementptr is expanded into the + obvious arithmetic). This pass requires target-specific hooks to lower calls, + returns, varargs, etc. For these features, the + TargetLowering interface is used.
+ +The Legalize phase is in charge of converting a DAG to only use the types and + operations that are natively supported by the target. This involves two major + tasks:
+ +Convert values of unsupported types to values of supported types.
+There are two main ways of doing this: converting small types to + larger types ("promoting"), and breaking up large integer types + into smaller ones ("expanding"). For example, a target might require + that all f32 values are promoted to f64 and that all i1/i8/i16 values + are promoted to i32. The same target might require that all i64 values + be expanded into i32 values. These changes can insert sign and zero + extensions as needed to make sure that the final code has the same + behavior as the input.
+A target implementation tells the legalizer which types are supported + (and which register class to use for them) by calling the + addRegisterClass method in its TargetLowering constructor.
+Eliminate operations that are not supported by the target.
+Targets often have weird constraints, such as not supporting every + operation on every supported datatype (e.g. X86 does not support byte + conditional moves and PowerPC does not support sign-extending loads from + a 16-bit memory location). Legalize takes care of this by open-coding + another sequence of operations to emulate the operation ("expansion"), by + promoting one type to a larger type that supports the operation + ("promotion"), or by using a target-specific hook to implement the + legalization ("custom").
+A target implementation tells the legalizer which operations are not + supported (and which of the above three actions to take) by calling the + setOperationAction method in its TargetLowering + constructor.
+Prior to the existance of the Legalize pass, we required that every target + selector supported and handled every + operator and type even if they are not natively supported. The introduction of + the Legalize phase allows all of the cannonicalization patterns to be shared + across targets, and makes it very easy to optimize the cannonicalized code + because it is still in the form of a DAG.
+ +The SelectionDAG optimization phase is run twice for code generation: once + immediately after the DAG is built and once after legalization. The first run + of the pass allows the initial code to be cleaned up (e.g. performing + optimizations that depend on knowing that the operators have restricted type + inputs). The second run of the pass cleans up the messy code generated by the + Legalize pass, which allows Legalize to be very simple (it can focus on making + code legal instead of focusing on generating good and legal code).
+ +One important class of optimizations performed is optimizing inserted sign + and zero extension instructions. We currently use ad-hoc techniques, but could + move to more rigorous techniques in the future. Here are some good papers on + the subject:
+ +
+ "Widening
+ integer arithmetic"
+ Kevin Redwine and Norman Ramsey
+ International Conference on Compiler Construction (CC) 2004
+
+ "Effective
+ sign extension elimination"
+ Motohiro Kawahito, Hideaki Komatsu, and Toshio Nakatani
+ Proceedings of the ACM SIGPLAN 2002 Conference on Programming Language Design
+ and Implementation.
+
The Select phase is the bulk of the target-specific code for instruction + selection. This phase takes a legal SelectionDAG as input, pattern matches the + instructions supported by the target to this DAG, and produces a new DAG of + target code. For example, consider the following LLVM fragment:
+ ++ %t1 = add float %W, %X + %t2 = mul float %t1, %Y + %t3 = add float %t2, %Z ++
This LLVM code corresponds to a SelectionDAG that looks basically like + this:
+ ++ (fadd:f32 (fmul:f32 (fadd:f32 W, X), Y), Z) ++
If a target supports floating point multiply-and-add (FMA) operations, one + of the adds can be merged with the multiply. On the PowerPC, for example, the + output of the instruction selector might look like this DAG:
+ ++ (FMADDS (FADDS W, X), Y, Z) ++
The FMADDS instruction is a ternary instruction that multiplies its + first two operands and adds the third (as single-precision floating-point + numbers). The FADDS instruction is a simple binary single-precision + add instruction. To perform this pattern match, the PowerPC backend includes + the following instruction definitions:
+ ++ def FMADDS : AForm_1<59, 29, + (ops F4RC:$FRT, F4RC:$FRA, F4RC:$FRC, F4RC:$FRB), + "fmadds $FRT, $FRA, $FRC, $FRB", + [(set F4RC:$FRT, (fadd (fmul F4RC:$FRA, F4RC:$FRC), + F4RC:$FRB))]>; + def FADDS : AForm_2<59, 21, + (ops F4RC:$FRT, F4RC:$FRA, F4RC:$FRB), + "fadds $FRT, $FRA, $FRB", + [(set F4RC:$FRT, (fadd F4RC:$FRA, F4RC:$FRB))]>; ++
The portion of the instruction definition in bold indicates the pattern used + to match the instruction. The DAG operators (like fmul/fadd) + are defined in the lib/Target/TargetSelectionDAG.td file. + "F4RC" is the register class of the input and result values.
+ +
The TableGen DAG instruction selector generator reads the instruction + patterns in the .td file and automatically builds parts of the pattern + matching code for your target. It has the following strengths:
+ ++ // Arbitrary immediate support. Implement in terms of LIS/ORI. + def : Pat<(i32 imm:$imm), + (ORI (LIS (HI16 imm:$imm)), (LO16 imm:$imm))>; ++
While it has many strengths, the system currently has some limitations, + primarily because it is a work in progress and is not yet finished:
+ +Despite these limitations, the instruction selector generator is still quite + useful for most of the binary and logical operations in typical instruction + sets. If you run into any problems or can't figure out how to do something, + please let Chris know!
+ +The scheduling phase takes the DAG of target instructions from the selection + phase and assigns an order. The scheduler can pick an order depending on + various constraints of the machines (i.e. order for minimal register pressure or + try to cover instruction latencies). Once an order is established, the DAG is + converted to a list of MachineInstrs and + the SelectionDAG is destroyed.
+ +Note that this phase is logically separate from the instruction selection + phase, but is tied to it closely in the code because it operates on + SelectionDAGs.
+ +To Be Written
Live Intervals are the ranges (intervals) where a variable is live. + They are used by some register allocator passes to + determine if two or more virtual registers which require the same physical + register are live at the same point in the program (i.e., they conflict). When + this situation occurs, one virtual register must be spilled.
+ +The first step in determining the live intervals of variables is to + calculate the set of registers that are immediately dead after the + instruction (i.e., the instruction calculates the value, but it is + never used) and the set of registers that are used by the instruction, + but are never used after the instruction (i.e., they are killed). Live + variable information is computed for each virtual register and + register allocatable physical register in the function. This + is done in a very efficient manner because it uses SSA to sparsely + compute lifetime information for virtual registers (which are in SSA + form) and only has to track physical registers within a block. Before + register allocation, LLVM can assume that physical registers are only + live within a single basic block. This allows it to do a single, + local analysis to resolve physical register lifetimes within each + basic block. If a physical register is not register allocatable (e.g., + a stack pointer or condition codes), it is not tracked.
+ +Physical registers may be live in to or out of a function. Live in values + are typically arguments in registers. Live out values are typically return + values in registers. Live in values are marked as such, and are given a dummy + "defining" instruction during live intervals analysis. If the last basic block + of a function is a return, then it's marked as using all live out + values in the function.
+ +PHI nodes need to be handled specially, because the calculation + of the live variable information from a depth first traversal of the CFG of + the function won't guarantee that a virtual register used by the PHI + node is defined before it's used. When a PHI node is encounted, only + the definition is handled, because the uses will be handled in other basic + blocks.
+ +For each PHI node of the current basic block, we simulate an + assignment at the end of the current basic block and traverse the successor + basic blocks. If a successor basic block has a PHI node and one of + the PHI node's operands is coming from the current basic block, + then the variable is marked as alive within the current basic block + and all of its predecessor basic blocks, until the basic block with the + defining instruction is encountered.
+ +We now have the information available to perform the live intervals analysis + and build the live intervals themselves. We start off by numbering the basic + blocks and machine instructions. We then handle the "live-in" values. These + are in physical registers, so the physical register is assumed to be killed by + the end of the basic block. Live intervals for virtual registers are computed + for some ordering of the machine instructions [1, N]. A live interval + is an interval [i, j), where 1 <= i <= j < N, for which a + variable is live.
+ +More to come...
+ +The Register Allocation problem consists in mapping a program + Pv, that can use an unbounded number of virtual + registers, to a program Pp that contains a finite + (possibly small) number of physical registers. Each target architecture has + a different number of physical registers. If the number of physical + registers is not enough to accommodate all the virtual registers, some of + them will have to be mapped into memory. These virtuals are called + spilled virtuals.
+ +In LLVM, physical registers are denoted by integer numbers that + normally range from 1 to 1023. To see how this numbering is defined + for a particular architecture, you can read the + GenRegisterNames.inc file for that architecture. For + instance, by inspecting + lib/Target/X86/X86GenRegisterNames.inc we see that the 32-bit + register EAX is denoted by 15, and the MMX register + MM0 is mapped to 48.
+ +Some architectures contain registers that share the same physical + location. A notable example is the X86 platform. For instance, in the + X86 architecture, the registers EAX, AX and + AL share the first eight bits. These physical registers are + marked as aliased in LLVM. Given a particular architecture, you + can check which registers are aliased by inspecting its + RegisterInfo.td file. Moreover, the method + TargetRegisterInfo::getAliasSet(p_reg) returns an array containing + all the physical registers aliased to the register p_reg.
+ +Physical registers, in LLVM, are grouped in Register Classes. + Elements in the same register class are functionally equivalent, and can + be interchangeably used. Each virtual register can only be mapped to + physical registers of a particular class. For instance, in the X86 + architecture, some virtuals can only be allocated to 8 bit registers. + A register class is described by TargetRegisterClass objects. + To discover if a virtual register is compatible with a given physical, + this code can be used: +
+ +
+ bool RegMapping_Fer::compatible_class(MachineFunction &mf,
+ unsigned v_reg,
+ unsigned p_reg) {
+ assert(TargetRegisterInfo::isPhysicalRegister(p_reg) &&
+ "Target register must be physical");
+ const TargetRegisterClass *trc = mf.getRegInfo().getRegClass(v_reg);
+ return trc->contains(p_reg);
+ }
+
+ Sometimes, mostly for debugging purposes, it is useful to change + the number of physical registers available in the target + architecture. This must be done statically, inside the + TargetRegsterInfo.td file. Just grep for + RegisterClass, the last parameter of which is a list of + registers. Just commenting some out is one simple way to avoid them + being used. A more polite way is to explicitly exclude some registers + from the allocation order. See the definition of the + GR register class in + lib/Target/IA64/IA64RegisterInfo.td for an example of this + (e.g., numReservedRegs registers are hidden.)
+ +Virtual registers are also denoted by integer numbers. Contrary to + physical registers, different virtual registers never share the same + number. The smallest virtual register is normally assigned the number + 1024. This may change, so, in order to know which is the first virtual + register, you should access + TargetRegisterInfo::FirstVirtualRegister. Any register whose + number is greater than or equal to + TargetRegisterInfo::FirstVirtualRegister is considered a virtual + register. Whereas physical registers are statically defined in a + TargetRegisterInfo.td file and cannot be created by the + application developer, that is not the case with virtual registers. + In order to create new virtual registers, use the method + MachineRegisterInfo::createVirtualRegister(). This method will return a + virtual register with the highest code. +
+ +Before register allocation, the operands of an instruction are + mostly virtual registers, although physical registers may also be + used. In order to check if a given machine operand is a register, use + the boolean function MachineOperand::isRegister(). To obtain + the integer code of a register, use + MachineOperand::getReg(). An instruction may define or use a + register. For instance, ADD reg:1026 := reg:1025 reg:1024 + defines the registers 1024, and uses registers 1025 and 1026. Given a + register operand, the method MachineOperand::isUse() informs + if that register is being used by the instruction. The method + MachineOperand::isDef() informs if that registers is being + defined.
+ +We will call physical registers present in the LLVM bitcode before + register allocation pre-colored registers. Pre-colored + registers are used in many different situations, for instance, to pass + parameters of functions calls, and to store results of particular + instructions. There are two types of pre-colored registers: the ones + implicitly defined, and those explicitly + defined. Explicitly defined registers are normal operands, and can be + accessed with MachineInstr::getOperand(int)::getReg(). In + order to check which registers are implicitly defined by an + instruction, use the + TargetInstrInfo::get(opcode)::ImplicitDefs, where + opcode is the opcode of the target instruction. One important + difference between explicit and implicit physical registers is that + the latter are defined statically for each instruction, whereas the + former may vary depending on the program being compiled. For example, + an instruction that represents a function call will always implicitly + define or use the same set of physical registers. To read the + registers implicitly used by an instruction, use + TargetInstrInfo::get(opcode)::ImplicitUses. Pre-colored + registers impose constraints on any register allocation algorithm. The + register allocator must make sure that none of them is been + overwritten by the values of virtual registers while still alive.
+ +There are two ways to map virtual registers to physical registers (or to + memory slots). The first way, that we will call direct mapping, + is based on the use of methods of the classes TargetRegisterInfo, + and MachineOperand. The second way, that we will call + indirect mapping, relies on the VirtRegMap class in + order to insert loads and stores sending and getting values to and from + memory.
+ +The direct mapping provides more flexibility to the developer of + the register allocator; however, it is more error prone, and demands + more implementation work. Basically, the programmer will have to + specify where load and store instructions should be inserted in the + target function being compiled in order to get and store values in + memory. To assign a physical register to a virtual register present in + a given operand, use MachineOperand::setReg(p_reg). To insert + a store instruction, use + TargetRegisterInfo::storeRegToStackSlot(...), and to insert a load + instruction, use TargetRegisterInfo::loadRegFromStackSlot.
+ +The indirect mapping shields the application developer from the + complexities of inserting load and store instructions. In order to map + a virtual register to a physical one, use + VirtRegMap::assignVirt2Phys(vreg, preg). In order to map a + certain virtual register to memory, use + VirtRegMap::assignVirt2StackSlot(vreg). This method will + return the stack slot where vreg's value will be located. If + it is necessary to map another virtual register to the same stack + slot, use VirtRegMap::assignVirt2StackSlot(vreg, + stack_location). One important point to consider when using the + indirect mapping, is that even if a virtual register is mapped to + memory, it still needs to be mapped to a physical register. This + physical register is the location where the virtual register is + supposed to be found before being stored or after being reloaded.
+ +If the indirect strategy is used, after all the virtual registers + have been mapped to physical registers or stack slots, it is necessary + to use a spiller object to place load and store instructions in the + code. Every virtual that has been mapped to a stack slot will be + stored to memory after been defined and will be loaded before being + used. The implementation of the spiller tries to recycle load/store + instructions, avoiding unnecessary instructions. For an example of how + to invoke the spiller, see + RegAllocLinearScan::runOnMachineFunction in + lib/CodeGen/RegAllocLinearScan.cpp.
+ +With very rare exceptions (e.g., function calls), the LLVM machine + code instructions are three address instructions. That is, each + instruction is expected to define at most one register, and to use at + most two registers. However, some architectures use two address + instructions. In this case, the defined register is also one of the + used register. For instance, an instruction such as ADD %EAX, + %EBX, in X86 is actually equivalent to %EAX = %EAX + + %EBX.
+ +In order to produce correct code, LLVM must convert three address + instructions that represent two address instructions into true two + address instructions. LLVM provides the pass + TwoAddressInstructionPass for this specific purpose. It must + be run before register allocation takes place. After its execution, + the resulting code may no longer be in SSA form. This happens, for + instance, in situations where an instruction such as %a = ADD %b + %c is converted to two instructions such as:
+ ++ %a = MOVE %b + %a = ADD %a %b ++
Notice that, internally, the second instruction is represented as + ADD %a[def/use] %b. I.e., the register operand %a is + both used and defined by the instruction.
+ +An important transformation that happens during register allocation is called + the SSA Deconstruction Phase. The SSA form simplifies many + analyses that are performed on the control flow graph of + programs. However, traditional instruction sets do not implement + PHI instructions. Thus, in order to generate executable code, compilers + must replace PHI instructions with other instructions that preserve their + semantics.
+ +There are many ways in which PHI instructions can safely be removed + from the target code. The most traditional PHI deconstruction + algorithm replaces PHI instructions with copy instructions. That is + the strategy adopted by LLVM. The SSA deconstruction algorithm is + implemented in nlib/CodeGen/>PHIElimination.cpp. In order to + invoke this pass, the identifier PHIEliminationID must be + marked as required in the code of the register allocator.
+ +Instruction folding is an optimization performed during + register allocation that removes unnecessary copy instructions. For + instance, a sequence of instructions such as:
+ ++ %EBX = LOAD %mem_address + %EAX = COPY %EBX ++
can be safely substituted by the single instruction: + +
+ %EAX = LOAD %mem_address ++
Instructions can be folded with the + TargetRegisterInfo::foldMemoryOperand(...) method. Care must be + taken when folding instructions; a folded instruction can be quite + different from the original instruction. See + LiveIntervals::addIntervalsForSpills in + lib/CodeGen/LiveIntervalAnalysis.cpp for an example of its use.
+ +The LLVM infrastructure provides the application developer with + three different register allocators:
+ +The type of register allocator used in llc can be chosen with the + command line option -regalloc=...:
+ ++ $ llc -f -regalloc=simple file.bc -o sp.s; + $ llc -f -regalloc=local file.bc -o lc.s; + $ llc -f -regalloc=linearscan file.bc -o ln.s; ++
To Be Written
To Be Written
To Be Written
To Be Written
For the JIT or .o file writer
+This section of the document explains features or design decisions that + are specific to the code generator for a particular target.
+ +Tail call optimization, callee reusing the stack of the caller, is currently supported on x86/x86-64 and PowerPC. It is performed if: +
x86/x86-64 constraints: +
PowerPC constraints: +
Example:
+Call as llc -tailcallopt test.ll. +
+ declare fastcc i32 @tailcallee(i32 inreg %a1, i32 inreg %a2, i32 %a3, i32 %a4)
+
+ define fastcc i32 @tailcaller(i32 %in1, i32 %in2) {
+ %l1 = add i32 %in1, %in2
+ %tmp = tail call fastcc i32 @tailcallee(i32 %in1 inreg, i32 %in2 inreg, i32 %in1, i32 %l1)
+ ret i32 %tmp
+ }
+ Implications of -tailcallopt:
+To support tail call optimization in situations where the callee has more arguments than the caller a 'callee pops arguments' convention is used. This currently causes each fastcc call that is not tail call optimized (because one or more of above constraints are not met) to be followed by a readjustment of the stack. So performance might be worse in such cases.
+On x86 and x86-64 one register is reserved for indirect tail calls (e.g via a function pointer). So there is one less register for integer argument passing. For x86 this means 2 registers (if inreg parameter attribute is used) and for x86-64 this means 5 register are used.
+The X86 code generator lives in the lib/Target/X86 directory. This + code generator is capable of targeting a variety of x86-32 and x86-64 + processors, and includes support for ISA extensions such as MMX and SSE. +
+ +The following are the known target triples that are supported by the X86 + backend. This is not an exhaustive list, and it would be useful to add those + that people test.
+ +The folowing target-specific calling conventions are known to backend:
+ +The x86 has a very flexible way of accessing memory. It is capable of + forming memory addresses of the following expression directly in integer + instructions (which use ModR/M addressing):
+ ++ Base + [1,2,4,8] * IndexReg + Disp32 ++
In order to represent this, LLVM tracks no less than 4 operands for each + memory operand of this form. This means that the "load" form of 'mov' + has the following MachineOperands in this order:
+ ++ Index: 0 | 1 2 3 4 + Meaning: DestReg, | BaseReg, Scale, IndexReg, Displacement + OperandTy: VirtReg, | VirtReg, UnsImm, VirtReg, SignExtImm ++ +
Stores, and all other instructions, treat the four memory operands in the + same way and in the same order.
+ +An instruction name consists of the base name, a default operand size, and a + a character per operand with an optional special size. For example:
+ +
+ ADD8rr -> add, 8-bit register, 8-bit register
+ IMUL16rmi -> imul, 16-bit register, 16-bit memory, 16-bit immediate
+ IMUL16rmi8 -> imul, 16-bit register, 16-bit memory, 8-bit immediate
+ MOVSX32rm16 -> movsx, 32-bit register, 16-bit memory
+
The PowerPC code generator lives in the lib/Target/PowerPC directory. The + code generation is retargetable to several variations or subtargets of + the PowerPC ISA; including ppc32, ppc64 and altivec. +
+LLVM follows the AIX PowerPC ABI, with two deviations. LLVM uses a PC + relative (PIC) or static addressing for accessing global values, so no TOC (r2) + is used. Second, r31 is used as a frame pointer to allow dynamic growth of a + stack frame. LLVM takes advantage of having no TOC to provide space to save + the frame pointer in the PowerPC linkage area of the caller frame. Other + details of PowerPC ABI can be found at PowerPC ABI. Note: This link describes the 32 bit ABI. The + 64 bit ABI is similar except space for GPRs are 8 bytes wide (not 4) and r13 is + reserved for system use.
+The size of a PowerPC frame is usually fixed for the duration of a + function’s invocation. Since the frame is fixed size, all references into + the frame can be accessed via fixed offsets from the stack pointer. The + exception to this is when dynamic alloca or variable sized arrays are present, + then a base pointer (r31) is used as a proxy for the stack pointer and stack + pointer is free to grow or shrink. A base pointer is also used if llvm-gcc is + not passed the -fomit-frame-pointer flag. The stack pointer is always aligned to + 16 bytes, so that space allocated for altivec vectors will be properly + aligned.
+An invocation frame is layed out as follows (low memory at top);
+| Linkage |
+
| Parameter area |
+
| Dynamic area |
+
| Locals area |
+
| Saved registers area |
+
| Previous Frame |
+
The linkage area is used by a callee to save special registers prior + to allocating its own frame. Only three entries are relevant to LLVM. The + first entry is the previous stack pointer (sp), aka link. This allows probing + tools like gdb or exception handlers to quickly scan the frames in the stack. A + function epilog can also use the link to pop the frame from the stack. The + third entry in the linkage area is used to save the return address from the lr + register. Finally, as mentioned above, the last entry is used to save the + previous frame pointer (r31.) The entries in the linkage area are the size of a + GPR, thus the linkage area is 24 bytes long in 32 bit mode and 48 bytes in 64 + bit mode.
+32 bit linkage area
+| 0 | +Saved SP (r1) | +
| 4 | +Saved CR | +
| 8 | +Saved LR | +
| 12 | +Reserved | +
| 16 | +Reserved | +
| 20 | +Saved FP (r31) | +
64 bit linkage area
+| 0 | +Saved SP (r1) | +
| 8 | +Saved CR | +
| 16 | +Saved LR | +
| 24 | +Reserved | +
| 32 | +Reserved | +
| 40 | +Saved FP (r31) | +
The parameter area is used to store arguments being passed to a callee + function. Following the PowerPC ABI, the first few arguments are actually + passed in registers, with the space in the parameter area unused. However, if + there are not enough registers or the callee is a thunk or vararg function, + these register arguments can be spilled into the parameter area. Thus, the + parameter area must be large enough to store all the parameters for the largest + call sequence made by the caller. The size must also be mimimally large enough + to spill registers r3-r10. This allows callees blind to the call signature, + such as thunks and vararg functions, enough space to cache the argument + registers. Therefore, the parameter area is minimally 32 bytes (64 bytes in 64 + bit mode.) Also note that since the parameter area is a fixed offset from the + top of the frame, that a callee can access its spilt arguments using fixed + offsets from the stack pointer (or base pointer.)
+Combining the information about the linkage, parameter areas and alignment. A + stack frame is minimally 64 bytes in 32 bit mode and 128 bytes in 64 bit + mode.
+The dynamic area starts out as size zero. If a function uses dynamic + alloca then space is added to the stack, the linkage and parameter areas are + shifted to top of stack, and the new space is available immediately below the + linkage and parameter areas. The cost of shifting the linkage and parameter + areas is minor since only the link value needs to be copied. The link value can + be easily fetched by adding the original frame size to the base pointer. Note + that allocations in the dynamic space need to observe 16 byte aligment.
+The locals area is where the llvm compiler reserves space for local + variables.
+The saved registers area is where the llvm compiler spills callee saved + registers on entry to the callee.
+The llvm prolog and epilog are the same as described in the PowerPC ABI, with + the following exceptions. Callee saved registers are spilled after the frame is + created. This allows the llvm epilog/prolog support to be common with other + targets. The base pointer callee saved register r31 is saved in the TOC slot of + linkage area. This simplifies allocation of space for the base pointer and + makes it convenient to locate programatically and during debugging.
+TODO - More to come.
+This document attempts to describe a few coding standards that are being used + in the LLVM source tree. Although no coding standards should be regarded as + absolute requirements to be followed in all instances, coding standards can be + useful.
+ +This document intentionally does not prescribe fixed standards for religious + issues such as brace placement and space usage. For issues like this, follow + the golden rule:
+ ++ + + ++ +
The ultimate goal of these guidelines is the increase readability and + maintainability of our common source base. If you have suggestions for topics to + be included, please mail them to Chris.
+ +Comments are one critical part of readability and maintainability. Everyone + knows they should comment, so should you. Although we all should probably + comment our code more than we do, there are a few very critical places that + documentation is very useful:
+ + File Headers + +Every source file should have a header on it that describes the basic + purpose of the file. If a file does not have a header, it should not be + checked into Subversion. Most source trees will probably have a standard + file header format. The standard format for the LLVM source tree looks like + this:
+ ++ //===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===// + // + // The LLVM Compiler Infrastructure + // + // This file is distributed under the University of Illinois Open Source + // License. See LICENSE.TXT for details. + // + //===----------------------------------------------------------------------===// + // + // This file contains the declaration of the Instruction class, which is the + // base class for all of the VM instructions. + // + //===----------------------------------------------------------------------===// ++
A few things to note about this particular format: The "-*- C++ + -*-" string on the first line is there to tell Emacs that the source file + is a C++ file, not a C file (Emacs assumes .h files are C files by default). + Note that this tag is not necessary in .cpp files. The name of the file is also + on the first line, along with a very short description of the purpose of the + file. This is important when printing out code and flipping though lots of + pages.
+ +The next section in the file is a concise note that defines the license + that the file is released under. This makes it perfectly clear what terms the + source code can be distributed under and should not be modified in any way.
+ +The main body of the description does not have to be very long in most cases. + Here it's only two lines. If an algorithm is being implemented or something + tricky is going on, a reference to the paper where it is published should be + included, as well as any notes or "gotchas" in the code to watch out for.
+ + Class overviews + +Classes are one fundamental part of a good object oriented design. As such, + a class definition should have a comment block that explains what the class is + used for... if it's not obvious. If it's so completely obvious your grandma + could figure it out, it's probably safe to leave it out. Naming classes + something sane goes a long ways towards avoiding writing documentation.
+ + + Method information + +Methods defined in a class (as well as any global functions) should also be + documented properly. A quick note about what it does any a description of the + borderline behaviour is all that is necessary here (unless something + particularly tricky or insideous is going on). The hope is that people can + figure out how to use your interfaces without reading the code itself... that is + the goal metric.
+ +Good things to talk about here are what happens when something unexpected + happens: does the method return null? Abort? Format your hard disk?
+ +In general, prefer C++ style (//) comments. They take less space, + require less typing, don't have nesting problems, etc. There are a few cases + when it is useful to use C style (/* */) comments however:
+ +To comment out a large block of code, use #if 0 and #endif. + These nest properly and are better behaved in general than C style comments.
+ +Immediately after the header file comment (and + include guards if working on a header file), the minimal list of #includes required by the + file should be listed. We prefer these #includes to be listed in this + order:
+ +... and each catagory should be sorted by name.
+ +The "Main Module Header" file applies to .cpp file + which implement an interface defined by a .h file. This #include + should always be included first regardless of where it lives on the file + system. By including a header file first in the .cpp files that implement the + interfaces, we ensure that the header does not have any hidden dependencies + which are not explicitly #included in the header, but should be. It is also a + form of documentation in the .cpp file to indicate where the interfaces it + implements are defined.
+ +Write your code to fit within 80 columns of text. This helps those of us who + like to print out code and look at your code in an xterm without resizing + it.
+ +In all cases, prefer spaces to tabs in source files. People have different + prefered indentation levels, and different styles of indentation that they + like... this is fine. What isn't is that different editors/viewers expand tabs + out to different tab stops. This can cause your code to look completely + unreadable, and it is not worth dealing with.
+ +As always, follow the Golden Rule above: follow the + style of existing code if your are modifying and extending it. If you like four + spaces of indentation, DO NOT do that in the middle of a chunk of code + with two spaces of indentation. Also, do not reindent a whole source file: it + makes for incredible diffs that are absolutely worthless.
+ +Okay, your first year of programming you were told that indentation is + important. If you didn't believe and internalize this then, now is the time. + Just do it.
+ +If your code has compiler warnings in it, something is wrong: you aren't + casting values correctly, your have "questionable" constructs in your code, or + you are doing something legitimately wrong. Compiler warnings can cover up + legitimate errors in output and make dealing with a translation unit + difficult.
+ +It is not possible to prevent all warnings from all compilers, nor is it + desirable. Instead, pick a standard compiler (like gcc) that provides + a good thorough set of warnings, and stick to them. At least in the case of + gcc, it is possible to work around any spurious errors by changing the + syntax of the code slightly. For example, an warning that annoys me occurs when + I write code like this:
+ +
+ if (V = getValue()) {
+ ...
+ }
+
+ gcc will warn me that I probably want to use the == + operator, and that I probably mistyped it. In most cases, I haven't, and I + really don't want the spurious errors. To fix this particular problem, I + rewrite the code like this:
+ +
+ if ((V = getValue())) {
+ ...
+ }
+
+ ...which shuts gcc up. Any gcc warning that annoys you can + be fixed by massaging the code appropriately.
+ +These are the gcc warnings that I prefer to enable: -Wall + -Winline -W -Wwrite-strings -Wno-unused
+ +In almost all cases, it is possible and within reason to write completely + portable code. If there are cases where it isn't possible to write portable + code, isolate it behind a well defined (and well documented) interface.
+ +In practice, this means that you shouldn't assume much about the host + compiler, including its support for "high tech" features like partial + specialization of templates. In fact, Visual C++ 6 could be an important target + for our work in the future, and we don't want to have to rewrite all of our code + to support it.
+ +In C++, the class and struct keywords can be used almost + interchangeably. The only difference is when they are used to declare a class: + class makes all members private by default while struct makes + all members public by default.
+ +Unfortunately, not all compilers follow the rules and some will generate + different symbols based on whether class or struct was used to + declare the symbol. This can lead to problems at link time.
+ +So, the rule for LLVM is to always use the class keyword, unless + all members are public, in which case struct is allowed.
+ +C++ doesn't do too well in the modularity department. There is no real + encapsulation or data hiding (unless you use expensive protocol classes), but it + is what we have to work with. When you write a public header file (in the LLVM + source tree, they live in the top level "include" directory), you are defining a + module of functionality.
+ +Ideally, modules should be completely independent of each other, and their + header files should only include the absolute minimum number of headers + possible. A module is not just a class, a function, or a namespace: it's a collection + of these that defines an interface. This interface may be several + functions, classes or data structures, but the important issue is how they work + together.
+ +In general, a module should be implemented with one or more .cpp + files. Each of these .cpp files should include the header that defines + their interface first. This ensure that all of the dependences of the module + header have been properly added to the module header itself, and are not + implicit. System headers should be included after user headers for a + translation unit.
+ +#include hurts compile time performance. Don't do it unless you + have to, especially in header files.
+ +But wait, sometimes you need to have the definition of a class to use it, or + to inherit from it. In these cases go ahead and #include that header + file. Be aware however that there are many cases where you don't need to have + the full definition of a class. If you are using a pointer or reference to a + class, you don't need the header file. If you are simply returning a class + instance from a prototyped function or method, you don't need it. In fact, for + most cases, you simply don't need the definition of a class... and not + #include'ing speeds up compilation.
+ +It is easy to try to go too overboard on this recommendation, however. You + must include all of the header files that you are using -- you can + include them either directly + or indirectly (through another header file). To make sure that you don't + accidently forget to include a header file in your module header, make sure to + include your module header first in the implementation file (as mentioned + above). This way there won't be any hidden dependencies that you'll find out + about later...
+ +Many modules have a complex implementation that causes them to use more than + one implementation (.cpp) file. It is often tempting to put the + internal communication interface (helper classes, extra functions, etc) in the + public module header file. Don't do this.
+ +If you really need to do something like this, put a private header file in + the same directory as the source files, and include it locally. This ensures + that your private interface remains private and undisturbed by outsiders.
+ +Note however, that it's okay to put extra implementation methods a public + class itself... just make them private (or protected), and all is well.
+ +The use of #include <iostream> in library files is + hereby forbidden. The primary reason for doing this is to + support clients using LLVM libraries as part of larger systems. In particular, + we statically link LLVM into some dynamic libraries. Even if LLVM isn't used, + the static c'tors are run whenever an application start up that uses the dynamic + library. There are two problems with this:
+ +| Old Way | +New Way | +
|---|---|
#include <iostream> |
+ #include "llvm/Support/Streams.h" |
+
DEBUG(std::cerr << ...); + DEBUG(dump(std::cerr)); |
+ DOUT << ...; + DEBUG(dump(DOUT)); |
+
std::cerr << "Hello world\n"; |
+ llvm::cerr << "Hello world\n"; |
+
std::cout << "Hello world\n"; |
+ llvm::cout << "Hello world\n"; |
+
std::cin >> Var; |
+ llvm::cin >> Var; |
+
std::ostream |
+ llvm::OStream |
+
std::istream |
+ llvm::IStream |
+
std::stringstream |
+ llvm::StringStream |
+
void print(std::ostream &Out); + // ... + print(std::cerr); |
+ void print(llvm::OStream Out);1 + // ... + print(llvm::cerr);+ + |
1llvm::OStream is a light-weight class so it should never + be passed by reference. This is important because in some configurations, + DOUT is an rvalue.
+Use the "assert" function to its fullest. Check all of your + preconditions and assumptions, you never know when a bug (not neccesarily even + yours) might be caught early by an assertion, which reduces debugging time + dramatically. The "<cassert>" header file is probably already + included by the header files you are using, so it doesn't cost anything to use + it.
+ +To further assist with debugging, make sure to put some kind of error message + in the assertion statement (which is printed if the assertion is tripped). This + helps the poor debugging make sense of why an assertion is being made and + enforced, and hopefully what to do about it. Here is one complete example:
+ +
+ inline Value *getOperand(unsigned i) {
+ assert(i < Operands.size() && "getOperand() out of range!");
+ return Operands[i];
+ }
+
+ Here are some examples:
+ ++ assert(Ty->isPointerType() && "Can't allocate a non pointer type!"); + + assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!"); + + assert(idx < getNumSuccessors() && "Successor # out of range!"); + + assert(V1.getType() == V2.getType() && "Constant types must be identical!"); + + assert(isa<PHINode>(Succ->front()) && "Only works on PHId BBs!"); ++
You get the idea...
+ +In LLVM, we prefer to explicitly prefix all identifiers from the standard + namespace with an "std::" prefix, rather than rely on + "using namespace std;".
+ +In header files, adding a 'using namespace XXX' directive pollutes + the namespace of any source file that includes the header. This is clearly a + bad thing.
+ +In implementation files (e.g. .cpp files), the rule is more of a stylistic + rule, but is still important. Basically, using explicit namespace prefixes + makes the code clearer, because it is immediately obvious what facilities + are being used and where they are coming from, and more portable, because + namespace clashes cannot occur between LLVM code and other namespaces. The + portability rule is important because different standard library implementations + expose different symbols (potentially ones they shouldn't), and future revisions + to the C++ standard will add more symbols to the std namespace. As + such, we never use 'using namespace std;' in LLVM.
+ +The exception to the general rule (i.e. it's not an exception for + the std namespace) is for implementation files. For example, all of + the code in the LLVM project implements code that lives in the 'llvm' namespace. + As such, it is ok, and actually clearer, for the .cpp files to have a 'using + namespace llvm' directive at their top, after the #includes. The + general form of this rule is that any .cpp file that implements code in any + namespace may use that namespace (and its parents'), but should not use any + others.
+ +If a class is defined in a header file and has a v-table (either it has + virtual methods or it derives from classes with virtual methods), it must + always have at least one out-of-line virtual method in the class. Without + this, the compiler will copy the vtable and RTTI into every .o file that + #includes the header, bloating .o file sizes and increasing link times. +
+ +Hard fast rule: Preincrement (++X) may be no slower than + postincrement (X++) and could very well be a lot faster than it. Use + preincrementation whenever possible.
+ +The semantics of postincrement include making a copy of the value being + incremented, returning it, and then preincrementing the "work value". For + primitive types, this isn't a big deal... but for iterators, it can be a huge + issue (for example, some iterators contains stack and set objects in them... + copying an iterator could invoke the copy ctor's of these as well). In general, + get in the habit of always using preincrement, and you won't have a problem.
+ +The std::endl modifier, when used with iostreams outputs a newline + to the output stream specified. In addition to doing this, however, it also + flushes the output stream. In other words, these are equivalent:
+ ++ std::cout << std::endl; + std::cout << '\n' << std::flush; ++
Most of the time, you probably have no reason to flush the output stream, so + it's better to use a literal '\n'.
+ +A lot of these comments and recommendations have been culled for other + sources. Two particularly important books for our work are:
+ +If you get some free time, and you haven't read them: do so, you might learn + something.
+ +This document describes the CommandLine argument processing library. It will + show you how to use it, and what it can do. The CommandLine library uses a + declarative approach to specifying the command line options that your program + takes. By default, these options declarations implicitly hold the value parsed + for the option declared (of course this can be + changed).
+ +Although there are a lot of command line argument parsing libraries + out there in many different languages, none of them fit well with what I needed. + By looking at the features and problems of other libraries, I designed the + CommandLine library to have the following features:
+ +This document will hopefully let you jump in and start using CommandLine in + your utility quickly and painlessly. Additionally it should be a simple + reference manual to figure out how stuff works. If it is failing in some area + (or you want an extension to the library), nag the author, Chris Lattner.
+ +This section of the manual runs through a simple CommandLine'ification of a + basic compiler tool. This is intended to show you how to jump into using the + CommandLine library in your own program, and show you some of the cool things it + can do.
+ +To start out, you need to include the CommandLine header file into your + program:
+ ++ #include "llvm/Support/CommandLine.h" +
Additionally, you need to add this as the first line of your main + program:
+ +
+ int main(int argc, char **argv) {
+ cl::ParseCommandLineOptions(argc, argv);
+ ...
+ }
+ ... which actually parses the arguments and fills in the variable + declarations.
+ +Now that you are ready to support command line arguments, we need to tell the + system which ones we want, and what type of arguments they are. The CommandLine + library uses a declarative syntax to model command line arguments with the + global variable declarations that capture the parsed values. This means that + for every command line option that you would like to support, there should be a + global variable declaration to capture the result. For example, in a compiler, + we would like to support the Unix-standard '-o <filename>' option + to specify where to put the output. With the CommandLine library, this is + represented like this:
+ + ++ cl::opt<string> OutputFilename("o", cl::desc("Specify output filename"), cl::value_desc("filename")); +
This declares a global variable "OutputFilename" that is used to + capture the result of the "o" argument (first parameter). We specify + that this is a simple scalar option by using the "cl::opt" template (as opposed to the "cl::list template), and tell the CommandLine library + that the data type that we are parsing is a string.
+ +The second and third parameters (which are optional) are used to specify what + to output for the "--help" option. In this case, we get a line that + looks like this:
+ ++ USAGE: compiler [options] + + OPTIONS: + -help - display available options (--help-hidden for more) + -o <filename> - Specify output filename +
Because we specified that the command line option should parse using the + string data type, the variable declared is automatically usable as a + real string in all contexts that a normal C++ string object may be used. For + example:
+ ++ ... + std::ofstream Output(OutputFilename.c_str()); + if (Output.good()) ... + ... +
There are many different options that you can use to customize the command + line option handling library, but the above example shows the general interface + to these options. The options can be specified in any order, and are specified + with helper functions like cl::desc(...), so + there are no positional dependencies to remember. The available options are + discussed in detail in the Reference Guide.
+ +Continuing the example, we would like to have our compiler take an input + filename as well as an output filename, but we do not want the input filename to + be specified with a hyphen (ie, not -filename.c). To support this + style of argument, the CommandLine library allows for positional arguments to be specified for the program. + These positional arguments are filled with command line parameters that are not + in option form. We use this feature like this:
+ ++ cl::opt<string> InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); +
This declaration indicates that the first positional argument should be + treated as the input filename. Here we use the cl::init option to specify an initial value for the + command line option, which is used if the option is not specified (if you do not + specify a cl::init modifier for an option, then + the default constructor for the data type is used to initialize the value). + Command line options default to being optional, so if we would like to require + that the user always specify an input filename, we would add the cl::Required flag, and we could eliminate the + cl::init modifier, like this:
+ ++ cl::opt<string> InputFilename(cl::Positional, cl::desc("<input file>"), cl::Required); +
Again, the CommandLine library does not require the options to be specified + in any particular order, so the above declaration is equivalent to:
+ ++ cl::opt<string> InputFilename(cl::Positional, cl::Required, cl::desc("<input file>")); +
By simply adding the cl::Required flag, + the CommandLine library will automatically issue an error if the argument is not + specified, which shifts all of the command line option verification code out of + your application into the library. This is just one example of how using flags + can alter the default behaviour of the library, on a per-option basis. By + adding one of the declarations above, the --help option synopsis is now + extended to:
+ ++ USAGE: compiler [options] <input file> + + OPTIONS: + -help - display available options (--help-hidden for more) + -o <filename> - Specify output filename +
... indicating that an input filename is expected.
+ +In addition to input and output filenames, we would like the compiler example + to support three boolean flags: "-f" to force overwriting of the output + file, "--quiet" to enable quiet mode, and "-q" for backwards + compatibility with some of our users. We can support these by declaring options + of boolean type like this:
+ ++ cl::opt<bool> Force ("f", cl::desc("Overwrite output files")); + cl::opt<bool> Quiet ("quiet", cl::desc("Don't print informational messages")); + cl::opt<bool> Quiet2("q", cl::desc("Don't print informational messages"), cl::Hidden); +
This does what you would expect: it declares three boolean variables + ("Force", "Quiet", and "Quiet2") to recognize these + options. Note that the "-q" option is specified with the "cl::Hidden" flag. This modifier prevents it + from being shown by the standard "--help" output (note that it is still + shown in the "--help-hidden" output).
+ +The CommandLine library uses a different parser + for different data types. For example, in the string case, the argument passed + to the option is copied literally into the content of the string variable... we + obviously cannot do that in the boolean case, however, so we must use a smarter + parser. In the case of the boolean parser, it allows no options (in which case + it assigns the value of true to the variable), or it allows the values + "true" or "false" to be specified, allowing any of the + following inputs:
+ ++ compiler -f # No value, 'Force' == true + compiler -f=true # Value specified, 'Force' == true + compiler -f=TRUE # Value specified, 'Force' == true + compiler -f=FALSE # Value specified, 'Force' == false +
... you get the idea. The bool parser just turns + the string values into boolean values, and rejects things like 'compiler + -f=foo'. Similarly, the float, double, and int parsers work + like you would expect, using the 'strtol' and 'strtod' C + library calls to parse the string value into the specified data type.
+ +With the declarations above, "compiler --help" emits this:
+ ++ USAGE: compiler [options] <input file> + + OPTIONS: + -f - Overwrite output files + -o - Override output filename + -quiet - Don't print informational messages + -help - display available options (--help-hidden for more) +
and "compiler --help-hidden" prints this:
+ ++ USAGE: compiler [options] <input file> + + OPTIONS: + -f - Overwrite output files + -o - Override output filename + -q - Don't print informational messages + -quiet - Don't print informational messages + -help - display available options (--help-hidden for more) +
This brief example has shown you how to use the 'cl::opt' class to parse simple scalar command line + arguments. In addition to simple scalar arguments, the CommandLine library also + provides primitives to support CommandLine option aliases, + and lists of options.
+ +So far, the example works well, except for the fact that we need to check the + quiet condition like this now:
+ ++ ... + if (!Quiet && !Quiet2) printInformationalMessage(...); + ... +
... which is a real pain! Instead of defining two values for the same + condition, we can use the "cl::alias" class to make the "-q" + option an alias for the "-quiet" option, instead of providing + a value itself:
+ ++ cl::opt<bool> Force ("f", cl::desc("Overwrite output files")); + cl::opt<bool> Quiet ("quiet", cl::desc("Don't print informational messages")); + cl::alias QuietA("q", cl::desc("Alias for -quiet"), cl::aliasopt(Quiet)); +
The third line (which is the only one we modified from above) defines a + "-q" alias that updates the "Quiet" variable (as specified by + the cl::aliasopt modifier) whenever it is + specified. Because aliases do not hold state, the only thing the program has to + query is the Quiet variable now. Another nice feature of aliases is + that they automatically hide themselves from the -help output + (although, again, they are still visible in the --help-hidden + output).
+ +Now the application code can simply use:
+ ++ ... + if (!Quiet) printInformationalMessage(...); + ... +
... which is much nicer! The "cl::alias" + can be used to specify an alternative name for any variable type, and has many + uses.
+ +So far we have seen how the CommandLine library handles builtin types like + std::string, bool and int, but how does it handle + things it doesn't know about, like enums or 'int*'s?
+ +The answer is that it uses a table-driven generic parser (unless you specify + your own parser, as described in the Extension + Guide). This parser maps literal strings to whatever type is required, and + requires you to tell it what this mapping should be.
+ +Let's say that we would like to add four optimization levels to our + optimizer, using the standard flags "-g", "-O0", + "-O1", and "-O2". We could easily implement this with boolean + options like above, but there are several problems with this strategy:
+ +To cope with these problems, we can use an enum value, and have the + CommandLine library fill it in with the appropriate level directly, which is + used like this:
+ +
+ enum OptLevel {
+ g, O1, O2, O3
+ };
+
+ cl::opt<OptLevel> OptimizationLevel(cl::desc("Choose optimization level:"),
+ cl::values(
+ clEnumVal(g , "No optimizations, enable debugging"),
+ clEnumVal(O1, "Enable trivial optimizations"),
+ clEnumVal(O2, "Enable default optimizations"),
+ clEnumVal(O3, "Enable expensive optimizations"),
+ clEnumValEnd));
+
+ ...
+ if (OptimizationLevel >= O2) doPartialRedundancyElimination(...);
+ ...
+ This declaration defines a variable "OptimizationLevel" of the + "OptLevel" enum type. This variable can be assigned any of the values + that are listed in the declaration (Note that the declaration list must be + terminated with the "clEnumValEnd" argument!). The CommandLine + library enforces + that the user can only specify one of the options, and it ensure that only valid + enum values can be specified. The "clEnumVal" macros ensure that the + command line arguments matched the enum values. With this option added, our + help output now is:
+ ++ USAGE: compiler [options] <input file> + + OPTIONS: + Choose optimization level: + -g - No optimizations, enable debugging + -O1 - Enable trivial optimizations + -O2 - Enable default optimizations + -O3 - Enable expensive optimizations + -f - Overwrite output files + -help - display available options (--help-hidden for more) + -o <filename> - Specify output filename + -quiet - Don't print informational messages +
In this case, it is sort of awkward that flag names correspond directly to + enum names, because we probably don't want a enum definition named "g" + in our program. Because of this, we can alternatively write this example like + this:
+ +
+ enum OptLevel {
+ Debug, O1, O2, O3
+ };
+
+ cl::opt<OptLevel> OptimizationLevel(cl::desc("Choose optimization level:"),
+ cl::values(
+ clEnumValN(Debug, "g", "No optimizations, enable debugging"),
+ clEnumVal(O1 , "Enable trivial optimizations"),
+ clEnumVal(O2 , "Enable default optimizations"),
+ clEnumVal(O3 , "Enable expensive optimizations"),
+ clEnumValEnd));
+
+ ...
+ if (OptimizationLevel == Debug) outputDebugInfo(...);
+ ...
+ By using the "clEnumValN" macro instead of "clEnumVal", we + can directly specify the name that the flag should get. In general a direct + mapping is nice, but sometimes you can't or don't want to preserve the mapping, + which is when you would use it.
+ +Another useful argument form is a named alternative style. We shall use this + style in our compiler to specify different debug levels that can be used. + Instead of each debug level being its own switch, we want to support the + following options, of which only one can be specified at a time: + "--debug-level=none", "--debug-level=quick", + "--debug-level=detailed". To do this, we use the exact same format as + our optimization level flags, but we also specify an option name. For this + case, the code looks like this:
+ +
+ enum DebugLev {
+ nodebuginfo, quick, detailed
+ };
+
+ // Enable Debug Options to be specified on the command line
+ cl::opt<DebugLev> DebugLevel("debug_level", cl::desc("Set the debugging level:"),
+ cl::values(
+ clEnumValN(nodebuginfo, "none", "disable debug information"),
+ clEnumVal(quick, "enable quick debug information"),
+ clEnumVal(detailed, "enable detailed debug information"),
+ clEnumValEnd));
+ This definition defines an enumerated command line variable of type "enum + DebugLev", which works exactly the same way as before. The difference here + is just the interface exposed to the user of your program and the help output by + the "--help" option:
+ ++ USAGE: compiler [options] <input file> + + OPTIONS: + Choose optimization level: + -g - No optimizations, enable debugging + -O1 - Enable trivial optimizations + -O2 - Enable default optimizations + -O3 - Enable expensive optimizations + -debug_level - Set the debugging level: + =none - disable debug information + =quick - enable quick debug information + =detailed - enable detailed debug information + -f - Overwrite output files + -help - display available options (--help-hidden for more) + -o <filename> - Specify output filename + -quiet - Don't print informational messages +
Again, the only structural difference between the debug level declaration and + the optimization level declaration is that the debug level declaration includes + an option name ("debug_level"), which automatically changes how the + library processes the argument. The CommandLine library supports both forms so + that you can choose the form most appropriate for your application.
+ +Now that we have the standard run-of-the-mill argument types out of the way, + lets get a little wild and crazy. Lets say that we want our optimizer to accept + a list of optimizations to perform, allowing duplicates. For example, we + might want to run: "compiler -dce -constprop -inline -dce -strip". In + this case, the order of the arguments and the number of appearances is very + important. This is what the "cl::list" + template is for. First, start by defining an enum of the optimizations that you + would like to perform:
+ +
+ enum Opts {
+ // 'inline' is a C++ keyword, so name it 'inlining'
+ dce, constprop, inlining, strip
+ };
+ Then define your "cl::list" variable:
+ ++ cl::list<Opts> OptimizationList(cl::desc("Available Optimizations:"), + cl::values( + clEnumVal(dce , "Dead Code Elimination"), + clEnumVal(constprop , "Constant Propagation"), + clEnumValN(inlining, "inline", "Procedure Integration"), + clEnumVal(strip , "Strip Symbols"), + clEnumValEnd)); +
This defines a variable that is conceptually of the type + "std::vector<enum Opts>". Thus, you can access it with standard + vector methods:
+ ++ for (unsigned i = 0; i != OptimizationList.size(); ++i) + switch (OptimizationList[i]) + ... +
... to iterate through the list of options specified.
+ +Note that the "cl::list" template is + completely general and may be used with any data types or other arguments that + you can use with the "cl::opt" template. One + especially useful way to use a list is to capture all of the positional + arguments together if there may be more than one specified. In the case of a + linker, for example, the linker takes several '.o' files, and needs to + capture them into a list. This is naturally specified as:
+ ++ ... + cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<Input files>"), cl::OneOrMore); + ... +
This variable works just like a "vector<string>" object. As + such, accessing the list is simple, just like above. In this example, we used + the cl::OneOrMore modifier to inform the + CommandLine library that it is an error if the user does not specify any + .o files on our command line. Again, this just reduces the amount of + checking we have to do.
+ +Instead of collecting sets of options in a list, it is also possible to + gather information for enum values in a bit vector. The represention used by + the cl::bits class is an unsigned + integer. An enum value is represented by a 0/1 in the enum's ordinal value bit + position. 1 indicating that the enum was specified, 0 otherwise. As each + specified value is parsed, the resulting enum's bit is set in the option's bit + vector:
+ ++ bits |= 1 << (unsigned)enum; +
Options that are specified multiple times are redundant. Any instances after + the first are discarded.
+ +Reworking the above list example, we could replace + cl::list with cl::bits:
+ ++ cl::bits<Opts> OptimizationBits(cl::desc("Available Optimizations:"), + cl::values( + clEnumVal(dce , "Dead Code Elimination"), + clEnumVal(constprop , "Constant Propagation"), + clEnumValN(inlining, "inline", "Procedure Integration"), + clEnumVal(strip , "Strip Symbols"), + clEnumValEnd)); +
To test to see if constprop was specified, we can use the + cl:bits::isSet function:
+ +
+ if (OptimizationBits.isSet(constprop)) {
+ ...
+ }
+ It's also possible to get the raw bit vector using the + cl::bits::getBits function:
+ ++ unsigned bits = OptimizationBits.getBits(); +
Finally, if external storage is used, then the location specified must be of + type unsigned. In all other ways a cl::bits option is equivalent to a cl::list option.
+ +As our program grows and becomes more mature, we may decide to put summary + information about what it does into the help output. The help output is styled + to look similar to a Unix man page, providing concise information about + a program. Unix man pages, however often have a description about what + the program does. To add this to your CommandLine program, simply pass a third + argument to the cl::ParseCommandLineOptions + call in main. This additional argument is then printed as the overview + information for your program, allowing you to include any additional information + that you want. For example:
+ +
+ int main(int argc, char **argv) {
+ cl::ParseCommandLineOptions(argc, argv, " CommandLine compiler example\n\n"
+ " This program blah blah blah...\n");
+ ...
+ }
+ would yield the help output:
+ ++ OVERVIEW: CommandLine compiler example + + This program blah blah blah... + + USAGE: compiler [options] <input file> + + OPTIONS: + ... + -help - display available options (--help-hidden for more) + -o <filename> - Specify output filename +
Now that you know the basics of how to use the CommandLine library, this + section will give you the detailed information you need to tune how command line + options work, as well as information on more "advanced" command line option + processing capabilities.
+ +Positional arguments are those arguments that are not named, and are not + specified with a hyphen. Positional arguments should be used when an option is + specified by its position alone. For example, the standard Unix grep + tool takes a regular expression argument, and an optional filename to search + through (which defaults to standard input if a filename is not specified). + Using the CommandLine library, this would be specified as:
+ ++ cl::opt<string> Regex (cl::Positional, cl::desc("<regular expression>"), cl::Required); + cl::opt<string> Filename(cl::Positional, cl::desc("<input file>"), cl::init("-")); +
Given these two option declarations, the --help output for our grep + replacement would look like this:
+ ++ USAGE: spiffygrep [options] <regular expression> <input file> + + OPTIONS: + -help - display available options (--help-hidden for more) +
... and the resultant program could be used just like the standard + grep tool.
+ +Positional arguments are sorted by their order of construction. This means + that command line options will be ordered according to how they are listed in a + .cpp file, but will not have an ordering defined if the positional arguments + are defined in multiple .cpp files. The fix for this problem is simply to + define all of your positional arguments in one .cpp file.
+ +Sometimes you may want to specify a value to your positional argument that + starts with a hyphen (for example, searching for '-foo' in a file). At + first, you will have trouble doing this, because it will try to find an argument + named '-foo', and will fail (and single quotes will not save you). + Note that the system grep has the same problem:
+ ++ $ spiffygrep '-foo' test.txt + Unknown command line argument '-foo'. Try: spiffygrep --help' + + $ grep '-foo' test.txt + grep: illegal option -- f + grep: illegal option -- o + grep: illegal option -- o + Usage: grep -hblcnsviw pattern file . . . +
The solution for this problem is the same for both your tool and the system + version: use the '--' marker. When the user specifies '--' on + the command line, it is telling the program that all options after the + '--' should be treated as positional arguments, not options. Thus, we + can use it like this:
+ ++ $ spiffygrep -- -foo test.txt + ...output... +
Sometimes an option can affect or modify the meaning of another option. For + example, consider gcc's -x LANG option. This tells + gcc to ignore the suffix of subsequent positional arguments and force + the file to be interpreted as if it contained source code in language + LANG. In order to handle this properly, you need to know the + absolute position of each argument, especially those in lists, so their + interaction(s) can be applied correctly. This is also useful for options like + -llibname which is actually a positional argument that starts with + a dash.
+So, generally, the problem is that you have two cl::list variables + that interact in some way. To ensure the correct interaction, you can use the + cl::list::getPosition(optnum) method. This method returns the + absolute position (as found on the command line) of the optnum + item in the cl::list.
+The idiom for usage is like this:
+ +
+ static cl::list<std::string> Files(cl::Positional, cl::OneOrMore);
+ static cl::list<std::string> Libraries("l", cl::ZeroOrMore);
+
+ int main(int argc, char**argv) {
+ // ...
+ std::vector<std::string>::iterator fileIt = Files.begin();
+ std::vector<std::string>::iterator libIt = Libraries.begin();
+ unsigned libPos = 0, filePos = 0;
+ while ( 1 ) {
+ if ( libIt != Libraries.end() )
+ libPos = Libraries.getPosition( libIt - Libraries.begin() );
+ else
+ libPos = 0;
+ if ( fileIt != Files.end() )
+ filePos = Files.getPosition( fileIt - Files.begin() );
+ else
+ filePos = 0;
+
+ if ( filePos != 0 && (libPos == 0 || filePos < libPos) ) {
+ // Source File Is next
+ ++fileIt;
+ }
+ else if ( libPos != 0 && (filePos == 0 || libPos < filePos) ) {
+ // Library is next
+ ++libIt;
+ }
+ else
+ break; // we're done with the list
+ }
+ }Note that, for compatibility reasons, the cl::opt also supports an + unsigned getPosition() option that will provide the absolute position + of that option. You can apply the same approach as above with a + cl::opt and a cl::list option as you can with two lists.
+The cl::ConsumeAfter formatting option is + used to construct programs that use "interpreter style" option processing. With + this style of option processing, all arguments specified after the last + positional argument are treated as special interpreter arguments that are not + interpreted by the command line argument.
+ +As a concrete example, lets say we are developing a replacement for the + standard Unix Bourne shell (/bin/sh). To run /bin/sh, first + you specify options to the shell itself (like -x which turns on trace + output), then you specify the name of the script to run, then you specify + arguments to the script. These arguments to the script are parsed by the Bourne + shell command line option processor, but are not interpreted as options to the + shell itself. Using the CommandLine library, we would specify this as:
+ ++ cl::opt<string> Script(cl::Positional, cl::desc("<input script>"), cl::init("-")); + cl::list<string> Argv(cl::ConsumeAfter, cl::desc("<program arguments>...")); + cl::opt<bool> Trace("x", cl::desc("Enable trace output")); +
which automatically provides the help output:
+ ++ USAGE: spiffysh [options] <input script> <program arguments>... + + OPTIONS: + -help - display available options (--help-hidden for more) + -x - Enable trace output +
At runtime, if we run our new shell replacement as `spiffysh -x test.sh + -a -x -y bar', the Trace variable will be set to true, the + Script variable will be set to "test.sh", and the + Argv list will contain ["-a", "-x", "-y", "bar"], because they + were specified after the last positional argument (which is the script + name).
+ +There are several limitations to when cl::ConsumeAfter options can + be specified. For example, only one cl::ConsumeAfter can be specified + per program, there must be at least one positional + argument specified, there must not be any cl::list + positional arguments, and the cl::ConsumeAfter option should be a cl::list option.
+ +By default, all command line options automatically hold the value that they + parse from the command line. This is very convenient in the common case, + especially when combined with the ability to define command line options in the + files that use them. This is called the internal storage model.
+ +Sometimes, however, it is nice to separate the command line option processing + code from the storage of the value parsed. For example, lets say that we have a + '-debug' option that we would like to use to enable debug information + across the entire body of our program. In this case, the boolean value + controlling the debug code should be globally accessable (in a header file, for + example) yet the command line option processing code should not be exposed to + all of these clients (requiring lots of .cpp files to #include + CommandLine.h).
+ +To do this, set up your .h file with your option, like this for example:
+ ++ // DebugFlag.h - Get access to the '-debug' command line option + // + + // DebugFlag - This boolean is set to true if the '-debug' command line option + // is specified. This should probably not be referenced directly, instead, use + // the DEBUG macro below. + // + extern bool DebugFlag; + + // DEBUG macro - This macro should be used by code to emit debug information. + // In the '-debug' option is specified on the command line, and if this is a + // debug build, then the code specified as the option to the macro will be + // executed. Otherwise it will not be. + #ifdef NDEBUG + #define DEBUG(X) + #else + #define DEBUG(X) do { if (DebugFlag) { X; } } while (0) + #endif ++
This allows clients to blissfully use the DEBUG() macro, or the + DebugFlag explicitly if they want to. Now we just need to be able to + set the DebugFlag boolean when the option is set. To do this, we pass + an additional argument to our command line argument processor, and we specify + where to fill in with the cl::location + attribute:
+ ++ bool DebugFlag; // the actual value + static cl::opt<bool, true> // The parser + Debug("debug", cl::desc("Enable debug output"), cl::Hidden, cl::location(DebugFlag)); ++
In the above example, we specify "true" as the second argument to + the cl::opt template, indicating that the + template should not maintain a copy of the value itself. In addition to this, + we specify the cl::location attribute, so + that DebugFlag is automatically set.
+ +This section describes the basic attributes that you can specify on + options.
+ ++ cl::opt<bool> Quiet("quiet"); ++ +
Option modifiers are the flags and expressions that you pass into the + constructors for cl::opt and cl::list. These modifiers give you the ability to + tweak how options are parsed and how --help output is generated to fit + your application well.
+ +These options fall into five main catagories:
+ +It is not possible to specify two options from the same catagory (you'll get + a runtime error) to a single option, except for options in the miscellaneous + catagory. The CommandLine library specifies defaults for all of these settings + that are the most useful in practice and the most common, which mean that you + usually shouldn't have to worry about these.
+ +The cl::NotHidden, cl::Hidden, and + cl::ReallyHidden modifiers are used to control whether or not an option + appears in the --help and --help-hidden output for the + compiled program:
+ +This group of options is used to control how many time an option is allowed + (or required) to be specified on the command line of your program. Specifying a + value for this setting allows the CommandLine library to do error checking for + you.
+ +The allowed values for this option group are:
+ +If an option is not specified, then the value of the option is equal to the + value specified by the cl::init attribute. If + the cl::init attribute is not specified, the + option value is initialized with the default constructor for the data type.
+ +If an option is specified multiple times for an option of the cl::opt class, only the last value will be + retained.
+ +This group of options is used to control whether or not the option allows a + value to be present. In the case of the CommandLine library, a value is either + specified with an equal sign (e.g. '-index-depth=17') or as a trailing + string (e.g. '-o a.out').
+ +The allowed values for this option group are:
+ +In general, the default values for this option group work just like you would + want them to. As mentioned above, you can specify the cl::ValueDisallowed modifier to a boolean + argument to restrict your command line parser. These options are mostly useful + when extending the library.
+ +The formatting option group is used to specify that the command line option + has special abilities and is otherwise different from other command line + arguments. As usual, you can only specify one of these arguments at most.
+ +The CommandLine library does not restrict how you use the cl::Prefix or cl::Grouping modifiers, but it is possible to + specify ambiguous argument settings. Thus, it is possible to have multiple + letter options that are prefix or grouping options, and they will still work as + designed.
+ +To do this, the CommandLine library uses a greedy algorithm to parse the + input option into (potentially multiple) prefix and grouping options. The + strategy basically looks like this:
+ +}
+The miscellaneous option modifiers are the only flags where you can specify + more than one flag from the set: they are not mutually exclusive. These flags + specify boolean properties that modify the option.
+ +So far, these are the only three miscellaneous option modifiers.
+ +Some systems, such as certain variants of Microsoft Windows and + some older Unices have a relatively low limit on command-line + length. It is therefore customary to use the so-called 'response + files' to circumvent this restriction. These files are mentioned on + the command-line (using the "@file") syntax. The program reads these + files and inserts the contents into argv, thereby working around the + command-line length limits. Response files are enabled by an optional + fourth argument to + cl::ParseEnvironmentOptions + and + cl::ParseCommandLineOptions. +
+ +Despite all of the built-in flexibility, the CommandLine option library + really only consists of one function (cl::ParseCommandLineOptions) + and three main classes: cl::opt, cl::list, and cl::alias. This section describes these three + classes in detail.
+ +The cl::ParseCommandLineOptions function is designed to be called + directly from main, and is used to fill in the values of all of the + command line option variables once argc and argv are + available.
+ +The cl::ParseCommandLineOptions function requires two parameters + (argc and argv), but may also take an optional third parameter + which holds additional extra text to emit when the + --help option is invoked, and a fourth boolean parameter that enables + response files.
+ +The cl::ParseEnvironmentOptions function has mostly the same effects + as cl::ParseCommandLineOptions, + except that it is designed to take values for options from an environment + variable, for those cases in which reading the command line is not convenient or + desired. It fills in the values of all the command line option variables just + like cl::ParseCommandLineOptions + does.
+ +It takes four parameters: the name of the program (since argv may + not be available, it can't just look in argv[0]), the name of the + environment variable to examine, the optional + additional extra text to emit when the + --help option is invoked, and the boolean + switch that controls whether reponse files + should be read.
+ +cl::ParseEnvironmentOptions will break the environment + variable's value up into words and then process them using + cl::ParseCommandLineOptions. + Note: Currently cl::ParseEnvironmentOptions does not support + quoting, so an environment variable containing -option "foo bar" will + be parsed as three words, -option, "foo, and bar", + which is different from what you would get from the shell with the same + input.
+ +The cl::SetVersionPrinter function is designed to be called + directly from main and before + cl::ParseCommandLineOptions. Its use is optional. It simply arranges + for a function to be called in response to the --version option instead + of having the CommandLine library print out the usual version string + for LLVM. This is useful for programs that are not part of LLVM but wish to use + the CommandLine facilities. Such programs should just define a small + function that takes no arguments and returns void and that prints out + whatever version information is appropriate for the program. Pass the address + of that function to cl::SetVersionPrinter to arrange for it to be + called when the --version option is given by the user.
+ +The cl::opt class is the class used to represent scalar command line + options, and is the one used most of the time. It is a templated class which + can take up to three arguments (all except for the first have default values + though):
+ +
+ namespace cl {
+ template <class DataType, bool ExternalStorage = false,
+ class ParserClass = parser<DataType> >
+ class opt;
+ }
+ The first template argument specifies what underlying data type the command + line argument is, and is used to select a default parser implementation. The + second template argument is used to specify whether the option should contain + the storage for the option (the default) or whether external storage should be + used to contain the value parsed for the option (see Internal + vs External Storage for more information).
+ +The third template argument specifies which parser to use. The default value + selects an instantiation of the parser class based on the underlying + data type of the option. In general, this default works well for most + applications, so this option is only used when using a custom parser.
+ +The cl::list class is the class used to represent a list of command + line options. It too is a templated class which can take up to three + arguments:
+ +
+ namespace cl {
+ template <class DataType, class Storage = bool,
+ class ParserClass = parser<DataType> >
+ class list;
+ }
+ This class works the exact same as the cl::opt class, except that the second argument is + the type of the external storage, not a boolean value. For this class, + the marker type 'bool' is used to indicate that internal storage should + be used.
+ +The cl::bits class is the class used to represent a list of command + line options in the form of a bit vector. It is also a templated class which + can take up to three arguments:
+ +
+ namespace cl {
+ template <class DataType, class Storage = bool,
+ class ParserClass = parser<DataType> >
+ class bits;
+ }
+ This class works the exact same as the cl::lists class, except that the second argument + must be of type unsigned if external storage is used.
+ +The cl::alias class is a nontemplated class that is used to form + aliases for other arguments.
+ +
+ namespace cl {
+ class alias;
+ }
+ The cl::aliasopt attribute should be + used to specify which option this is an alias for. Alias arguments default to + being Hidden, and use the aliased options parser to do + the conversion from string to data.
+ +The cl::extrahelp class is a nontemplated class that allows extra + help text to be printed out for the --help option.
+ +
+ namespace cl {
+ struct extrahelp;
+ }
+ To use the extrahelp, simply construct one with a const char* + parameter to the constructor. The text passed to the constructor will be printed + at the bottom of the help message, verbatim. Note that multiple + cl::extrahelp can be used, but this practice is discouraged. If + your tool needs to print additional help information, put all that help into a + single cl::extrahelp instance.
+For example:
+
+ cl::extrahelp("\nADDITIONAL HELP:\n\n This is the extra help\n");
+ Parsers control how the string value taken from the command line is + translated into a typed value, suitable for use in a C++ program. By default, + the CommandLine library uses an instance of parser<type> if the + command line option specifies that it uses values of type 'type'. + Because of this, custom option processing is specified with specializations of + the 'parser' class.
+ +The CommandLine library provides the following builtin parser + specializations, which are sufficient for most applications. It can, however, + also be extended to work with new data types and new ways of interpreting the + same data. See the Writing a Custom Parser for more + details on this type of library extension.
+ +Although the CommandLine library has a lot of functionality built into it + already (as discussed previously), one of its true strengths lie in its + extensibility. This section discusses how the CommandLine library works under + the covers and illustrates how to do some simple, common, extensions.
+ +One of the simplest and most common extensions is the use of a custom parser. + As discussed previously, parsers are the portion + of the CommandLine library that turns string input from the user into a + particular parsed data type, validating the input in the process.
+ +There are two ways to use a new parser:
+ +Specialize the cl::parser template for + your custom data type.
+ +
This approach has the advantage that users of your custom data type will + automatically use your custom parser whenever they define an option with a value + type of your data type. The disadvantage of this approach is that it doesn't + work if your fundamental data type is something that is already supported.
+ +Write an independent class, using it explicitly from options that need + it.
+ +This approach works well in situations where you would line to parse an + option using special syntax for a not-very-special data-type. The drawback of + this approach is that users of your parser have to be aware that they are using + your parser instead of the builtin ones.
+ +To guide the discussion, we will discuss a custom parser that accepts file + sizes, specified with an optional unit after the numeric size. For example, we + would like to parse "102kb", "41M", "1G" into the appropriate integer value. In + this case, the underlying data type we want to parse into is + 'unsigned'. We choose approach #2 above because we don't want to make + this the default for all unsigned options.
+ +To start out, we declare our new FileSizeParser class:
+ +
+ struct FileSizeParser : public cl::basic_parser<unsigned> {
+ // parse - Return true on error.
+ bool parse(cl::Option &O, const char *ArgName, const std::string &ArgValue,
+ unsigned &Val);
+ };
+ Our new class inherits from the cl::basic_parser template class to + fill in the default, boiler plate code for us. We give it the data type that + we parse into, the last argument to the parse method, so that clients of + our custom parser know what object type to pass in to the parse method. (Here we + declare that we parse into 'unsigned' variables.)
+ +For most purposes, the only method that must be implemented in a custom + parser is the parse method. The parse method is called + whenever the option is invoked, passing in the option itself, the option name, + the string to parse, and a reference to a return value. If the string to parse + is not well-formed, the parser should output an error message and return true. + Otherwise it should return false and set 'Val' to the parsed value. In + our example, we implement parse as:
+ +
+ bool FileSizeParser::parse(cl::Option &O, const char *ArgName,
+ const std::string &Arg, unsigned &Val) {
+ const char *ArgStart = Arg.c_str();
+ char *End;
+
+ // Parse integer part, leaving 'End' pointing to the first non-integer char
+ Val = (unsigned)strtol(ArgStart, &End, 0);
+
+ while (1) {
+ switch (*End++) {
+ case 0: return false; // No error
+ case 'i': // Ignore the 'i' in KiB if people use that
+ case 'b': case 'B': // Ignore B suffix
+ break;
+
+ case 'g': case 'G': Val *= 1024*1024*1024; break;
+ case 'm': case 'M': Val *= 1024*1024; break;
+ case 'k': case 'K': Val *= 1024; break;
+
+ default:
+ // Print an error message if unrecognized character!
+ return O.error(": '" + Arg + "' value invalid for file size argument!");
+ }
+ }
+ }
+ This function implements a very simple parser for the kinds of strings we are + interested in. Although it has some holes (it allows "123KKK" for + example), it is good enough for this example. Note that we use the option + itself to print out the error message (the error method always returns + true) in order to get a nice error message (shown below). Now that we have our + parser class, we can use it like this:
+ ++ static cl::opt<unsigned, false, FileSizeParser> + MFS("max-file-size", cl::desc("Maximum file size to accept"), + cl::value_desc("size")); +
Which adds this to the output of our program:
+ ++ OPTIONS: + -help - display available options (--help-hidden for more) + ... + -max-file-size=<size> - Maximum file size to accept +
And we can test that our parse works correctly now (the test program just + prints out the max-file-size argument value):
+ ++ $ ./test + MFS: 0 + $ ./test -max-file-size=123MB + MFS: 128974848 + $ ./test -max-file-size=3G + MFS: 3221225472 + $ ./test -max-file-size=dog + -max-file-size option: 'dog' value invalid for file size argument! +
It looks like it works. The error message that we get is nice and helpful, + and we seem to accept reasonable file sizes. This wraps up the "custom parser" + tutorial.
+ +Several of the LLVM libraries define static cl::opt instances that + will automatically be included in any program that links with that library. + This is a feature. However, sometimes it is necessary to know the value of the + command line option outside of the library. In these cases the library does or + should provide an external storage location that is accessible to users of the + library. Examples of this include the llvm::DebugFlag exported by the + lib/Support/Debug.cpp file and the llvm::TimePassesIsEnabled + flag exported by the lib/VMCore/Pass.cpp file.
+ +TODO: complete this section
+ +TODO: fill in this section
+ +Note: This document is a work-in-progress. Additions and clarifications + are welcome.
+LLVMC is a generic compiler driver, designed to be customizable and + extensible. It plays the same role for LLVM as the gcc program + does for GCC - LLVMC's job is essentially to transform a set of input + files into a set of targets depending on configuration rules and user + options. What makes LLVMC different is that these transformation rules + are completely customizable - in fact, LLVMC knows nothing about the + specifics of transformation (even the command-line options are mostly + not hard-coded) and regards the transformation structure as an + abstract graph. This makes it possible to adapt LLVMC for other + purposes - for example, as a build tool for game resources.
+Because LLVMC employs TableGen [1] as its configuration language, you + need to be familiar with it to customize LLVMC.
+LLVMC tries hard to be as compatible with gcc as possible, + although there are some small differences. Most of the time, however, + you shouldn't be able to notice them:
++ $ # This works as expected: + $ llvmc2 -O3 -Wall hello.cpp + $ ./a.out + hello ++
One nice feature of LLVMC is that one doesn't have to distinguish + between different compilers for different languages (think g++ and + gcc) - the right toolchain is chosen automatically based on input + language names (which are, in turn, determined from file + extensions). If you want to force files ending with ".c" to compile as + C++, use the -x option, just like you would do it with gcc:
++ $ llvmc2 -x c hello.cpp + $ # hello.cpp is really a C file + $ ./a.out + hello ++
On the other hand, when using LLVMC as a linker to combine several C++ + object files you should provide the --linker option since it's + impossible for LLVMC to choose the right linker in that case:
++ $ llvmc2 -c hello.cpp + $ llvmc2 hello.o + [A lot of link-time errors skipped] + $ llvmc2 --linker=c++ hello.o + $ ./a.out + hello ++
LLVMC has some built-in options that can't be overridden in the + configuration files:
+At the time of writing LLVMC does not support on-the-fly reloading of + configuration, so to customize LLVMC you'll have to recompile the + source code (which lives under $LLVM_DIR/tools/llvmc2). The + default configuration files are Common.td (contains common + definitions, don't forget to include it in your configuration + files), Tools.td (tool descriptions) and Graph.td (compilation + graph definition).
+To compile LLVMC with your own configuration file (say,``MyGraph.td``), + run make like this:
++ $ cd $LLVM_DIR/tools/llvmc2 + $ make GRAPH=MyGraph.td TOOLNAME=my_llvmc ++
This will build an executable named my_llvmc. There are also + several sample configuration files in the llvmc2/examples + subdirectory that should help to get you started.
+Internally, LLVMC stores information about possible source + transformations in form of a graph. Nodes in this graph represent + tools, and edges between two nodes represent a transformation path. A + special "root" node is used to mark entry points for the + transformations. LLVMC also assigns a weight to each edge (more on + this later) to choose between several alternative edges.
+The definition of the compilation graph (see file Graph.td) is + just a list of edges:
++ def CompilationGraph : CompilationGraph<[ + Edge<root, llvm_gcc_c>, + Edge<root, llvm_gcc_assembler>, + ... + + Edge<llvm_gcc_c, llc>, + Edge<llvm_gcc_cpp, llc>, + ... + + OptionalEdge<llvm_gcc_c, opt, [(switch_on "opt")]>, + OptionalEdge<llvm_gcc_cpp, opt, [(switch_on "opt")]>, + ... + + OptionalEdge<llvm_gcc_assembler, llvm_gcc_cpp_linker, + (case (input_languages_contain "c++"), (inc_weight), + (or (parameter_equals "linker", "g++"), + (parameter_equals "linker", "c++")), (inc_weight))>, + ... + + ]>; ++
As you can see, the edges can be either default or optional, where + optional edges are differentiated by sporting a case expression + used to calculate the edge's weight.
+The default edges are assigned a weight of 1, and optional edges get a + weight of 0 + 2*N where N is the number of tests that evaluated to + true in the case expression. It is also possible to provide an + integer parameter to inc_weight and dec_weight - in this case, + the weight is increased (or decreased) by the provided value instead + of the default 2.
+When passing an input file through the graph, LLVMC picks the edge + with the maximum weight. To avoid ambiguity, there should be only one + default edge between two nodes (with the exception of the root node, + which gets a special treatment - there you are allowed to specify one + default edge per language).
+To get a visual representation of the compilation graph (useful for + debugging), run llvmc2 --view-graph. You will need dot and + gsview installed for this to work properly.
+As was said earlier, nodes in the compilation graph represent tools, + which are described separately. A tool definition looks like this + (taken from the Tools.td file):
++ def llvm_gcc_cpp : Tool<[ + (in_language "c++"), + (out_language "llvm-assembler"), + (output_suffix "bc"), + (cmd_line "llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm"), + (sink) + ]>; ++
This defines a new tool called llvm_gcc_cpp, which is an alias for + llvm-g++. As you can see, a tool definition is just a list of + properties; most of them should be self-explanatory. The sink + property means that this tool should be passed all command-line + options that lack explicit descriptions.
+The complete list of the currently implemented tool properties follows:
+The next tool definition is slightly more complex:
++ def llvm_gcc_linker : Tool<[ + (in_language "object-code"), + (out_language "executable"), + (output_suffix "out"), + (cmd_line "llvm-gcc $INFILE -o $OUTFILE"), + (join), + (prefix_list_option "L", (forward), + (help "add a directory to link path")), + (prefix_list_option "l", (forward), + (help "search a library when linking")), + (prefix_list_option "Wl", (unpack_values), + (help "pass options to linker")) + ]>; ++
This tool has a "join" property, which means that it behaves like a + linker. This tool also defines several command-line options: -l, + -L and -Wl which have their usual meaning. An option has two + attributes: a name and a (possibly empty) list of properties. All + currently implemented option types and properties are described below:
+Possible option types:
++++
+- switch_option - a simple boolean switch, for example -time.
+- parameter_option - option that takes an argument, for example + -std=c99;
+- parameter_list_option - same as the above, but more than one + occurence of the option is allowed.
+- prefix_option - same as the parameter_option, but the option name + and parameter value are not separated.
+- prefix_list_option - same as the above, but more than one + occurence of the option is allowed; example: -lm -lpthread.
+- alias_option - a special option type for creating + aliases. Unlike other option types, aliases are not allowed to + have any properties besides the aliased option name. Usage + example: (alias_option "preprocess", "E")
+
Possible option properties:
++++
+- append_cmd - append a string to the tool invocation command.
+- forward - forward this option unchanged.
+- output_suffix - modify the output suffix of this + tool. Example : (switch "E", (output_suffix "i").
+- stop_compilation - stop compilation after this phase.
+- unpack_values - used for for splitting and forwarding + comma-separated lists of options, e.g. -Wa,-foo=bar,-baz is + converted to -foo=bar -baz and appended to the tool invocation + command.
+- help - help string associated with this option. Used for + --help output.
+- required - this option is obligatory.
+
It can be handy to have all information about options gathered in a + single place to provide an overview. This can be achieved by using a + so-called OptionList:
++ def Options : OptionList<[ + (switch_option "E", (help "Help string")), + (alias_option "quiet", "q") + ... + ]>; ++
OptionList is also a good place to specify option aliases.
+Tool-specific option properties like append_cmd have (obviously) + no meaning in the context of OptionList, so the only properties + allowed there are help and required.
+Option lists are used at the file scope. See file + examples/Clang.td for an example of OptionList usage.
+Normally, LLVMC executes programs from the system PATH. Sometimes, + this is not sufficient: for example, we may want to specify tool names + in the configuration file. This can be achieved via the mechanism of + hooks - to compile LLVMC with your hooks, just drop a .cpp file into + tools/llvmc2 directory. Hooks should live in the hooks + namespace and have the signature std::string hooks::MyHookName + (void). They can be used from the cmd_line tool property:
++ (cmd_line "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)") ++
It is also possible to use environment variables in the same manner:
++ (cmd_line "$ENV(VAR1)/path/to/file -o $ENV(VAR2)") ++
To change the command line string based on user-provided options use + the case expression (documented below):
++ (cmd_line + (case + (switch_on "E"), + "llvm-g++ -E -x c $INFILE -o $OUTFILE", + (default), + "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm")) ++
The 'case' construct can be used to calculate weights of the optional + edges and to choose between several alternative command line strings + in the cmd_line tool property. It is designed after the + similarly-named construct in functional languages and takes the form + (case (test_1), statement_1, (test_2), statement_2, ... (test_N), + statement_N). The statements are evaluated only if the corresponding + tests evaluate to true.
+Examples:
++ // Increases edge weight by 5 if "-A" is provided on the + // command-line, and by 5 more if "-B" is also provided. + (case + (switch_on "A"), (inc_weight 5), + (switch_on "B"), (inc_weight 5)) + + // Evaluates to "cmdline1" if option "-A" is provided on the + // command line, otherwise to "cmdline2" + (case + (switch_on "A"), "cmdline1", + (switch_on "B"), "cmdline2", + (default), "cmdline3") ++
Note the slight difference in 'case' expression handling in contexts + of edge weights and command line specification - in the second example + the value of the "B" switch is never checked when switch "A" is + enabled, and the whole expression always evaluates to "cmdline1" in + that case.
+Case expressions can also be nested, i.e. the following is legal:
++ (case (switch_on "E"), (case (switch_on "o"), ..., (default), ...) + (default), ...) ++
You should, however, try to avoid doing that because it hurts + readability. It is usually better to split tool descriptions and/or + use TableGen inheritance instead.
+One last thing that you will need to modify when adding support for a + new language to LLVMC is the language map, which defines mappings from + file extensions to language names. It is used to choose the proper + toolchain(s) for a given input file set. Language map definition is + located in the file Tools.td and looks like this:
++ def LanguageMap : LanguageMap< + [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>, + LangToSuffixes<"c", ["c"]>, + ... + ]>; ++
| [1] | TableGen Fundamentals + http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html |
Note: This document is a work-in-progress. Additions and clarifications + are welcome.
+This document contains the LLVM Developer Policy which defines the + project's policy towards developers and their contributions. The intent of + this policy is to eliminate mis-communication, rework, and confusion that + might arise from the distributed nature of LLVM's development. By stating + the policy in clear terms, we hope each developer can know ahead of time + what to expect when making LLVM contributions.
+This policy is also designed to accomplish the following objectives:
+This policy is aimed at frequent contributors to LLVM. People interested in + contributing one-off patches can do so in an informal way by sending them to + the + llvm-commits mailing list and engaging another developer to see it through + the process.
+ +This section contains policies that pertain to frequent LLVM + developers. We always welcome one-off patches from + people who do not routinely contribute to LLVM, but we expect more from + frequent contributors to keep the system as efficient as possible for + everyone. + Frequent LLVM contributors are expected to meet the following requirements in + order for LLVM to maintain a high standard of quality.
+
Developers should stay informed by reading at least the + llvmdev + email list. If you are doing anything more than just casual work on LLVM, + it is suggested that you also subscribe to the + llvm-commits + list and pay attention to changes being made by others.
+We recommend that active developers register an email account with + LLVM Bugzilla and preferably subscribe to + the llvm-bugs + email list to keep track of bugs and enhancements occurring in LLVM.
+When making a patch for review, the goal is to make it as easy for the + reviewer to read it as possible. As such, we recommend that you:
+svn diff -x -u+ or with the utility utils/mkpatch, which makes it easy to read the + diff.
When sending a patch to a mailing list, it is a good idea to send it as an + attachment to the message, not embedded into the text of the + message. This ensures that your mailer will not mangle the patch when it + sends it (e.g. by making whitespace changes or by wrapping lines).
+LLVM has a code review policy. Code review is one way to increase the + quality of software. We generally follow these policies:
+Developers should participate in code reviews as both reviewers and + reviewees. If someone is kind enough to review your code, you should + return the favor for someone else. Note that anyone is welcome to review + and give feedback on a patch, but only people with Subversion write access + can approve it.
+ +The LLVM Project relies on two features of its process to maintain rapid + development in addition to the high quality of its source base: the + combination of code review plus post-commit review for trusted maintainers. + Having both is a great way for the project to take advantage of the fact + that most people do the right thing most of the time, and only commit + patches without pre-commit review when they are confident they are + right.
+ +The trick to this is that the project has to guarantee that all patches + that are committed are reviewed after they go in: you don't want everyone + to assume someone else will review it, allowing the patch to go unreviewed. + To solve this problem, we have a notion of an 'owner' for a piece of the + code. The sole responsibility of a code owner is to ensure that a commit + to their area of the code is appropriately reviewed, either by themself or + by someone else. The current code owners are:
+ +Note that code ownership is completely different than reviewers: anyone can + review a piece of code, and we welcome code review from anyone who is + interested. Code owners are the "last line of defense" to guarantee that + all patches that are committed are actually reviewed.
+ +Being a code owner is a somewhat unglamorous position, but it is incredibly + important for the ongoing success of the project. Because people get busy, + interests change, and unexpected things happen, code ownership is purely + opt-in, and anyone can choose to resign their "title" at any time. For now, + we do not have an official policy on how one gets elected to be a code + owner. +
+ +Developers are required to create test cases for any bugs fixed and any new + features added. Some tips for getting your testcase approved:
+Note that llvm/test is designed for regression and small feature tests + only. More extensive test cases (e.g., entire applications, benchmarks, + etc) should be added to the llvm-test test suite. The llvm-test + suite is for coverage (correctness, performance, etc) testing, not feature + or regression testing.
+The minimum quality standards that any change must satisfy before being + committed to the main development branch are:
+Additionally, the committer is responsible for addressing any problems + found in the future that the change is responsible for. For example:
+We prefer for this to be handled before submission but understand that it + isn't possible to test all of this for every submission. Our nightly + testing + infrastructure normally finds these problems. A good rule of thumb is to + check the nightly testers for regressions the day after your change.
+ +Commits that violate these quality standards (e.g. are very broken) may + be reverted. This is necessary when the change blocks other developers from + making progress. The developer is welcome to re-commit the change after + the problem has been fixed.
++ We grant commit access to contributors with a track record of submitting high + quality patches. If you would like commit access, please send an email to + Chris with the following information:
+ +Once you've been granted commit access, you should be able to check out an + LLVM tree with an SVN URL of "https://username at llvm.org/..." instead of the + normal anonymous URL of "http://llvm.org/...". The first time you commit + you'll have to type in your password. Note that you may get a warning from + SVN about an untrusted key, you can ignore this. To verify that your commit + access works, please do a test commit (e.g. change a comment or add a blank + line). Your first commit to a repository may require the autogenerated email + to be approved by a mailing list. This is normal, and will be done when + the mailing list owner has time.
+ +If you have recently been granted commit access, these policies apply:
+ +In any case, your changes are still subject to code + review (either before or after they are committed, depending on the nature + of the change). You are encouraged to review other peoples' patches as well, + but you aren't required to.
+ +When a developer begins a major new project with the aim of contributing + it back to LLVM, s/he should inform the community with an email to + the llvmdev + email list, to the extent possible. The reason for this is to: +
The design of LLVM is carefully controlled to ensure that all the pieces + fit together well and are as consistent as possible. If you plan to make a + major change to the way LLVM works or want to add a major new extension, it + is a good idea to get consensus with the development + community before you start working on it.
+ +Once the design of the new feature is finalized, the work itself should be + done as a series of incremental changes, not as + a long-term development branch.
+ +In the LLVM project, we do all significant changes as a series of + incremental patches. We have a strong dislike for huge changes or + long-term development branches. Long-term development branches have a + number of drawbacks:
+ ++ To address these problems, LLVM uses an incremental development style and we + require contributors to follow this practice when making a large/invasive + change. Some tips:
+ +If you are interested in making a large change, and this scares you, please + make sure to first discuss the change/gather + consensus then ask about the best way to go about making + the change.
+We believe in correct attribution of contributions to + their contributors. However, we do not want the source code to be littered + with random attributions "this code written by J Random Guy" (this is noisy + and distracting. In practice, the revision control system keeps a perfect + history of who change what, and the CREDITS.txt file describes higher-level + contributions.
+ +Overall, please do not add contributor names to the source base.
+This section addresses the issues of copyright, license and patents for + the LLVM project. + Currently, the University of Illinois is the LLVM copyright holder and the + terms of its license to LLVM users and developers is the + University of + Illinois/NCSA Open Source License.
+ +NOTE: This section deals with legal matters but does not provide + legal advice. We are not lawyers, please seek legal counsel from an + attorney.
++
For consistency and ease of management, the project requires the + copyright for all LLVM software to be held by a single copyright holder: + the University of Illinois (UIUC).
+ ++ Although UIUC may eventually reassign the copyright of the software to another + entity (e.g. a dedicated non-profit "LLVM Organization", or something) + the intent for the project is to always have a single entity hold the + copyrights to LLVM at any given time.
+ +We believe that having a single copyright + holder is in the best interests of all developers and users as it greatly + reduces the managerial burden for any kind of administrative or technical + decisions about LLVM. The goal of the LLVM project is to always keep the code + open and licensed under a very liberal license.
+We intend to keep LLVM perpetually open source + and to use a liberal open source license. The current license is the + + University of Illinois/NCSA Open Source License, which boils + down to this:
+We believe this fosters the widest adoption of LLVM because it allows + commercial products to be derived from LLVM with few restrictions and + without a requirement for making any derived works also open source (i.e. + LLVM's license is not a "copyleft" license like the GPL). We suggest that you + read the License + if further clarification is needed.
+ +Note that the LLVM Project does distribute llvm-gcc, which is GPL. + This means that anything "linked" into llvm-gcc must itself be compatible + with the GPL, and must be releasable under the terms of the GPL. This implies + that any code linked into llvm-gcc and distributed to others may be subject + to the viral aspects of the GPL (for example, a proprietary code generator + linked into llvm-gcc must be made available under the GPL). This is not a + problem for code already distributed under a more liberal license (like the + UIUC license), and does not affect code generated by llvm-gcc. It may be a + problem if you intend to base commercial development on llvm-gcc without + redistributing your source code.
+ +We have no plans to change the license of LLVM. If you have questions + or comments about the license, please contact the LLVM Oversight Group.
+ +To the best of our knowledge, LLVM does not infringe on any patents (we have + actually removed code from LLVM in the past that was found to infringe). + Having code in LLVM that infringes on patents would violate an important + goal of the project by making it hard or impossible to reuse the code for + arbitrary purposes (including commercial use).
+ +When contributing code, we expect contributors to notify us of any potential + for patent-related trouble with their changes. If you own the rights to a + patent and would like to contribute code to LLVM that relies on it, we + require that you sign an agreement that allows any other user of LLVM to + freely use your patent. Please contact the oversight group for more + details.
+With regards to the LLVM copyright and licensing, developers agree to + assign their copyrights to UIUC for any contribution made so that + the entire software base can be managed by a single copyright holder. This + implies that any contributions can be licensed under the license that the + project uses.
+
+
|
+
This document is the central repository for all information pertaining to + exception handling in LLVM. It describes the format that LLVM exception + handling information takes, which is useful for those interested in creating + front-ends or dealing directly with the information. Further, this document + provides specific examples of what exception handling information is used for + C/C++.
+ +Exception handling for most programming languages is designed to recover from + conditions that rarely occur during general use of an application. To that end, + exception handling should not interfere with the main flow of an + application's algorithm by performing checkpointing tasks such as saving + the current pc or register state.
+ +The Itanium ABI Exception Handling Specification defines a methodology for + providing outlying data in the form of exception tables without inlining + speculative exception handling code in the flow of an application's main + algorithm. Thus, the specification is said to add "zero-cost" to the normal + execution of an application.
+ +A more complete description of the Itanium ABI exception handling runtime + support of can be found at Itanium C++ ABI: + Exception Handling. A description of the exception frame format can be found + at Exception Frames, with details of the Dwarf + specification at Dwarf 3 + Standard. A description for the C++ exception table formats can be found at + Exception Handling + Tables.
+ +When an exception is thrown in llvm code, the runtime does a best effort to + find a handler suited to process the circumstance.
+ +The runtime first attempts to find an exception frame corresponding to + the function where the exception was thrown. If the programming language (ex. + C++) supports exception handling, the exception frame contains a reference to an + exception table describing how to process the exception. If the language (ex. + C) does not support exception handling or if the exception needs to be forwarded + to a prior activation, the exception frame contains information about how to + unwind the current activation and restore the state of the prior activation. + This process is repeated until the exception is handled. If the exception is + not handled and no activations remain, then the application is terminated with + an appropriate error message.
+ +Since different programming languages have different behaviors when handling + exceptions, the exception handling ABI provides a mechanism for supplying + personalities. An exception handling personality is defined by way of a + personality function (ex. for C++ __gxx_personality_v0) which + receives the context of the exception, an exception structure containing + the exception object type and value, and a reference to the exception table for + the current function. The personality function for the current compile unit is + specified in a common exception frame.
+ +The organization of an exception table is language dependent. For C++, an + exception table is organized as a series of code ranges defining what to do if + an exception occurs in that range. Typically, the information associated with a + range defines which types of exception objects (using C++ type info) that + are handled in that range, and an associated action that should take place. + Actions typically pass control to a landing pad.
+ +A landing pad corresponds to the code found in the catch portion of a + try/catch sequence. When execution resumes at a landing pad, it receives the + exception structure and a selector corresponding to the type of exception + thrown. The selector is then used to determine which catch should actually + process the exception.
+ +At the time of this writing, only C++ exception handling support is available + in LLVM. So the remainder of this document will be somewhat C++-centric.
+ +From the C++ developers perspective, exceptions are defined in terms of the + throw and try/catch statements. In this section we will + describe the implementation of llvm exception handling in terms of C++ + examples.
+ +Languages that support exception handling typically provide a throw + operation to initiate the exception process. Internally, a throw operation + breaks down into two steps. First, a request is made to allocate exception + space for an exception structure. This structure needs to survive beyond the + current activation. This structure will contain the type and value of the + object being thrown. Second, a call is made to the runtime to raise the + exception, passing the exception structure as an argument.
+ +In C++, the allocation of the exception structure is done by the + __cxa_allocate_exception runtime function. The exception raising is + handled by __cxa_throw. The type of the exception is represented using + a C++ RTTI type info structure.
+ +A call within the scope of a try statement can potentially raise an exception. + In those circumstances, the LLVM C++ front-end replaces the call with an + invoke instruction. Unlike a call, the invoke has two potential + continuation points; where to continue when the call succeeds as per normal, and + where to continue if the call raises an exception, either by a throw or the + unwinding of a throw.
+ +The term used to define a the place where an invoke continues after an + exception is called a landing pad. LLVM landing pads are conceptually + alternative function entry points where a exception structure reference and a type + info index are passed in as arguments. The landing pad saves the exception + structure reference and then proceeds to select the catch block that corresponds + to the type info of the exception object.
+ +Two llvm intrinsic functions are used convey information about the landing + pad to the back end.
+ +llvm.eh.exception takes no + arguments and returns the exception structure reference. The backend replaces + this intrinsic with the code that accesses the first argument of a call. The + LLVM C++ front end generates code to save this value in an alloca location for + further use in the landing pad and catch code.
+ +llvm.eh.selector takes a minimum of + three arguments. The first argument is the reference to the exception + structure. The second argument is a reference to the personality function to be + used for this try catch sequence. Each of the remaining arguments is either a + reference to the type info for a catch statement, + a filter expression, + or the number zero representing a cleanup. + The exception is tested against the arguments sequentially from first to last. + The result of the llvm.eh.selector is a + positive number if the exception matched a type info, a negative number if it matched + a filter, and zero if it matched a cleanup. If nothing is matched, the behaviour of + the program is undefined. + The LLVM C++ front end generates code to save the selector value in an alloca + location for further use in the landing pad and catch code. + If a type info matched then the selector value is the index of the type info in + the exception table, which can be obtained using the + llvm.eh.typeid.for intrinsic.
+ +Once the landing pad has the type info selector, the code branches to the + code for the first catch. The catch then checks the value of the type info + selector against the index of type info for that catch. Since the type info + index is not known until all the type info have been gathered in the backend, + the catch code will call the llvm.eh.typeid.for intrinsic to + determine the index for a given type info. If the catch fails to match the + selector then control is passed on to the next catch. Note: Since the landing + pad will not be used if there is no match in the list of type info on the call + to llvm.eh.selector, then neither the + last catch nor catch all need to perform the the check against the + selector.
+ +Finally, the entry and exit of catch code is bracketed with calls to + __cxa_begin_catch and __cxa_end_catch. + __cxa_begin_catch takes a exception structure reference as an argument + and returns the value of the exception object. __cxa_end_catch + takes a exception structure reference as an argument. This function clears the + exception from the exception space. Note: a rethrow from within the catch may + replace this call with a __cxa_rethrow.
+ +To handle destructors and cleanups in try code, control may not run directly + from a landing pad to the first catch. Control may actually flow from the + landing pad to clean up code and then to the first catch. Since the required + clean up for each invoke in a try may be different (ex., intervening + constructor), there may be several landing pads for a given try. If cleanups + need to be run, the number zero should be passed as the last + llvm.eh.selector argument. + However for C++ a null i8* must be passed + instead. +
+ +C++ allows the specification of which exception types that can be thrown from + a function. To represent this a top level landing pad may exist to filter out + invalid types. To express this in LLVM code the landing pad will call llvm.eh.selector. The arguments are the + length of the filter expression (the number of type infos plus one), followed by + the type infos themselves. + llvm.eh.selector will return a negative + value if the exception does not match any of the type infos. If no match is + found then a call to __cxa_call_unexpected should be made, otherwise + _Unwind_Resume. Each of these functions require a reference to the + exception structure.
+ +The semantics of the invoke instruction require that any exception that + unwinds through an invoke call should result in a branch to the invoke's unwind + label. However such a branch will only happen if the + llvm.eh.selector matches. + Thus in order to ensure correct operation, the front-end must only generate + llvm.eh.selector calls that are + guaranteed to always match whatever exception unwinds through the invoke. + For most languages it is enough to pass zero, indicating the presence of + a cleanup, as the last + llvm.eh.selector argument. + However for C++ this is not sufficient, because the C++ personality function + will terminate the program if it detects that unwinding the exception only + results in matches with cleanups. For C++ a null i8* should + be passed as the last + llvm.eh.selector argument instead. + This is interpreted as a catch-all by the C++ personality function, and will + always match. +
+ +LLVM uses several intrinsic functions (name prefixed with "llvm.eh") to + provide exception handling information at various points in generated code.
+ ++ i8* %llvm.eh.exception( ) ++ +
This intrinsic indicates that the exception structure is available at this + point in the code. The backend will replace this intrinsic with code to fetch + the first argument of a call. The effect is that the intrinsic result is the + exception structure reference.
+ ++ i32 %llvm.eh.selector.i32(i8*, i8*, i8*, ...) + i64 %llvm.eh.selector.i64(i8*, i8*, i8*, ...) ++ +
This intrinsic indicates that the exception selector is available at this + point in the code. The backend will replace this intrinsic with code to fetch + the second argument of a call. The effect is that the intrinsic result is the + exception selector.
+ +llvm.eh.selector takes a minimum of + three arguments. The first argument is the reference to the exception + structure. The second argument is a reference to the personality function to be + used for this try catch sequence. Each of the remaining arguments is either a + reference to the type info for a catch statement, + a filter expression, + or the number zero representing a cleanup. + The exception is tested against the arguments sequentially from first to last. + The result of the llvm.eh.selector is a + positive number if the exception matched a type info, a negative number if it matched + a filter, and zero if it matched a cleanup. If nothing is matched, the behaviour of + the program is undefined. + If a type info matched then the selector value is the index of the type info in + the exception table, which can be obtained using the + llvm.eh.typeid.for intrinsic.
+ ++ i32 %llvm.eh.typeid.for.i32(i8*) + i64 %llvm.eh.typeid.for.i64(i8*) ++ +
This intrinsic returns the type info index in the exception table of the + current function. This value can be used to compare against the result of llvm.eh.selector. The single argument is + a reference to a type info.
+ +There are two tables that are used by the exception handling runtime to + determine which actions should take place when an exception is thrown.
+ +An exception handling frame eh_frame is very similar to the unwind + frame used by dwarf debug info. The frame contains all the information + necessary to tear down the current frame and restore the state of the prior + frame. There is an exception handling frame for each function in a compile + unit, plus a common exception handling frame that defines information common to + all functions in the unit.
+ +Todo - Table details here.
+ +An exception table contains information about what actions to take when an + exception is thrown in a particular part of a function's code. There is + one exception table per function except leaf routines and functions that have + only calls to non-throwing functions will not need an exception table.
+ +Todo - Table details here.
+ +Testing/Testing/Testing.
During the course of using LLVM, you may wish to customize it for your + research project or for experimentation. At this point, you may realize that + you need to add something to LLVM, whether it be a new fundamental type, a new + intrinsic function, or a whole new instruction.
+ +When you come to this realization, stop and think. Do you really need to + extend LLVM? Is it a new fundamental capability that LLVM does not support at + its current incarnation or can it be synthesized from already pre-existing LLVM + elements? If you are not sure, ask on the LLVM-dev list. The + reason is that extending LLVM will get involved as you need to update all the + different passes that you intend to use with your extension, and there are + many LLVM analyses and transformations, so it may be quite a bit of + work.
+ +Adding an intrinsic function is far easier than + adding an instruction, and is transparent to optimization passes. If your added + functionality can be expressed as a + function call, an intrinsic function is the method of choice for LLVM + extension.
+ +Before you invest a significant amount of effort into a non-trivial + extension, ask on the list if what you are + looking to do can be done with already-existing infrastructure, or if maybe + someone else is already working on it. You will save yourself a lot of time and + effort by doing so.
+ +Adding a new intrinsic function to LLVM is much easier than adding a new + instruction. Almost all extensions to LLVM should start as an intrinsic + function and then be turned into an instruction if warranted.
+ +Once the intrinsic has been added to the system, you must add code generator + support for it. Generally you must do the following steps:
+ +As with intrinsics, adding a new SelectionDAG node to LLVM is much easier + than adding a new instruction. New nodes are often added to help represent + instructions common to many targets. These nodes often map to an LLVM + instruction (add, sub) or intrinsic (byteswap, population count). In other + cases, new nodes have been added to allow many targets to perform a common task + (converting between floating point and integer representation) or capture more + complicated behavior in a single node (rotate).
+ +WARNING: adding instructions changes the bitcode + format, and it will take some effort to maintain compatibility with + the previous version. Only add an instruction if it is absolutely + necessary.
+ +Also, you need to implement (or modify) any analyses or passes that you want + to understand this new instruction.
+ +WARNING: adding new types changes the bitcode + format, and will break compatibility with currently-existing LLVM + installations. Only add new types if it is absolutely necessary.
+ ++ std::string getTypeDescription(const Type &Ty, + std::vector<const Type*> &TypeStack) + bool TypesEqual(const Type *Ty, const Type *Ty2, + std::map<const Type*, const Type*> & EqTypes) ++
+ void calcTypeName(const Type *Ty, + std::vector<const Type*> &TypeStack, + std::map<const Type*,std::string> &TypeNames, + std::string & Result) ++
Why are the LLVM source code and the front-end distributed under different + licenses?
+The C/C++ front-ends are based on GCC and must be distributed under the GPL. + Our aim is to distribute LLVM source code under a much less restrictive + license, in particular one that does not compel users who distribute tools based + on modifying the source to redistribute the modified source code as well.
+Does the University of Illinois Open Source License really qualify as an + "open source" license?
+Yes, the license is certified by the Open + Source Initiative (OSI).
+Can I modify LLVM source code and redistribute the modified source?
+Yes. The modified source distribution must retain the copyright notice and + follow the three bulletted conditions listed in the LLVM license.
+Can I modify LLVM source code and redistribute binaries or other tools based + on it, without redistributing the source?
+Yes, this is why we distribute LLVM under a less restrictive license than + GPL, as explained in the first question above.
+In what language is LLVM written?
+All of the LLVM tools and libraries are written in C++ with extensive use of + the STL.
+How portable is the LLVM source code?
+The LLVM source code should be portable to most modern UNIX-like operating + systems. Most of the code is written in standard C++ with operating system + services abstracted to a support library. The tools required to build and test + LLVM have been ported to a plethora of platforms.
+ +Some porting problems may exist in the following areas:
+ +When I run configure, it finds the wrong C compiler.
+The configure script attempts to locate first gcc and then + cc, unless it finds compiler paths set in CC and CXX + for the C and C++ compiler, respectively.
+ +If configure finds the wrong compiler, either adjust your + PATH environment variable or set CC and CXX + explicitly.
+ +The configure script finds the right C compiler, but it uses the + LLVM linker from a previous build. What do I do?
+The configure script uses the PATH to find executables, so + if it's grabbing the wrong linker/assembler/etc, there are two ways to fix + it:
+ +Adjust your PATH environment variable so that the correct + program appears first in the PATH. This may work, but may not be + convenient when you want them first in your path for other + work.
Run configure with an alternative PATH that is + correct. In a Borne compatible shell, the syntax would be:
+ ++ % PATH=[the path without the bad program] ./configure ... ++
This is still somewhat inconvenient, but it allows configure + to do its work without having to adjust your PATH + permanently.
When creating a dynamic library, I get a strange GLIBC error.
+Under some operating systems (i.e. Linux), libtool does not work correctly if + GCC was compiled with the --disable-shared option. To work around this, install + your own version of GCC that has shared libraries enabled by default.
+I've updated my source tree from Subversion, and now my build is trying to + use a file/directory that doesn't exist.
+You need to re-run configure in your object directory. When new Makefiles + are added to the source tree, they have to be copied over to the object tree in + order to be used by the build.
+I've modified a Makefile in my source tree, but my build tree keeps using the + old version. What do I do?
+If the Makefile already exists in your object tree, you + can just run the following command in the top level directory of your object + tree:
+ +% ./config.status <relative path to Makefile>+
If the Makefile is new, you will have to modify the configure script to copy + it over.
+ +I've upgraded to a new version of LLVM, and I get strange build errors.
+Sometimes, changes to the LLVM source code alters how the build system works. + Changes in libtool, autoconf, or header file dependencies are especially prone + to this sort of problem.
+ +The best thing to try is to remove the old files and re-build. In most + cases, this takes care of the problem. To do this, just type make + clean and then make in the directory that fails to build.
+ +I've built LLVM and am testing it, but the tests freeze.
+This is most likely occurring because you built a profile or release + (optimized) build of LLVM and have not specified the same information on the + gmake command line.
+ +For example, if you built LLVM with the command:
+ +% gmake ENABLE_PROFILING=1+
...then you must run the tests with the following commands:
+ ++ % cd llvm/test + % gmake ENABLE_PROFILING=1 ++
Why do test results differ when I perform different types of builds?
+The LLVM test suite is dependent upon several features of the LLVM tools and + libraries.
+ +First, the debugging assertions in code are not enabled in optimized or + profiling builds. Hence, tests that used to fail may pass.
+ +Second, some tests may rely upon debugging options or behavior that is only + available in the debug build. These tests will fail in an optimized or profile + build.
+ +Compiling LLVM with GCC 3.3.2 fails, what should I do?
+This is a bug in GCC, and + affects projects other than LLVM. Try upgrading or downgrading your GCC.
+After Subversion update, rebuilding gives the error "No rule to make + target".
+If the error is of the form:
+ ++ gmake[2]: *** No rule to make target `/path/to/somefile', needed by + `/path/to/another/file.d'.+
+ Stop. +
This may occur anytime files are moved within the Subversion repository or + removed entirely. In this case, the best solution is to erase all + .d files, which list dependencies for source files, and rebuild:
+ ++ % cd $LLVM_OBJ_DIR + % rm -f `find . -name \*\.d` + % gmake ++
In other cases, it may be necessary to run make clean before + rebuilding.
+llvmc is experimental and isn't really supported. We suggest + using llvm-gcc instead.
+LLVM currently has full support for C and C++ source languages. These are + available through a special version of GCC that LLVM calls the + C Front End
+There is an incomplete version of a Java front end available in the + java module. There is no documentation on this yet so + you'll need to download the code, compile it, and try it.
+In the stacker module is a compiler and runtime + library for the Stacker language, a "toy" language loosely based on Forth.
+The PyPy developers are working on integrating LLVM into the PyPy backend + so that PyPy language can translate to LLVM.
+Your compiler front-end will communicate with LLVM by creating a module in + the LLVM intermediate representation (IR) format. Assuming you want to + write your language's compiler in the language itself (rather than C++), + there are 3 major ways to tackle generating LLVM IR from a front-end:
+If you go with the first option, the C bindings in include/llvm-c should + help a lot, since most languages have strong support for interfacing with + C. The most common hurdle with calling C from managed code is interfacing + with the garbage collector. The C interface was designed to require very + little memory management, and so is straightforward in this regard.
+Currently, there isn't much. LLVM supports an intermediate representation + which is useful for code representation but will not support the high level + (abstract syntax tree) representation needed by most compilers. There are no + facilities for lexical nor semantic analysis. There is, however, a mostly + implemented configuration-driven + compiler driver which simplifies the task + of running optimizations, linking, and executable generation.
++ When I compile software that uses a configure script, the configure script + thinks my system has all of the header files and libraries it is testing for. + How do I get configure to work correctly? +
++ The configure script is getting things wrong because the LLVM linker allows + symbols to be undefined at link time (so that they can be resolved during JIT + or translation to the C back end). That is why configure thinks your system + "has everything." +
++ To work around this, perform the following steps: +
++ This will allow the llvm-ld linker to create a native code executable + instead of shell script that runs the JIT. Creating native code requires + standard linkage, which in turn will allow the configure script to find out if + code is not linking on your system because the feature isn't available on your + system.
++ When I compile code using the LLVM GCC front end, it complains that it cannot + find libcrtend.a. +
++ The only way this can happen is if you haven't installed the runtime library. To + correct this, do:
+ ++ % cd llvm/runtime + % make clean ; make install-bytecode ++
+ How can I disable all optimizations when compiling code using the LLVM GCC front end? +
++ Passing "-Wa,-disable-opt -Wl,-disable-opt" will disable *all* cleanup and + optimizations done at the llvm level, leaving you with the truly horrible + code that you desire. +
+Yes, you can use LLVM to convert code from any language LLVM supports to C. + Note that the generated C code will be very low level (all loops are lowered + to gotos, etc) and not very pretty (comments are stripped, original source + formatting is totally lost, variables are renamed, expressions are regrouped), + so this may not be what you're looking for. However, this is a good way to add + C++ support for a processor that does not otherwise have a C++ compiler. +
+ +Use commands like this:
+ +Compile your program as normal with llvm-g++:
+ ++ % llvm-g++ x.cpp -o program ++
or:
+ ++ % llvm-g++ a.cpp -c + % llvm-g++ b.cpp -c + % llvm-g++ a.o b.o -o program ++
With llvm-gcc3, this will generate program and program.bc. The .bc + file is the LLVM version of the program all linked together.
Convert the LLVM code to C code, using the LLC tool with the C + backend:
+ ++ % llc -march=c program.bc -o program.c ++
Finally, compile the C file:
+ ++ % cc x.c ++
Note that, by default, the C backend does not support exception handling. If + you want/need it for a certain program, you can enable it by passing + "-enable-correct-eh-support" to the llc program. The resultant code will use + setjmp/longjmp to implement exception support that is correct but relatively + slow.
+ +Also note: this specific sequence of commands won't work if you use a + function defined in the C++ runtime library (or any other C++ library). To + access an external C++ library, you must manually compile libstdc++ to LLVM + bitcode, statically link it into your program, then use the commands above to + convert the whole result into C code. Alternatively, you can compile the + libraries and your application into two different chunks of C code and link + them.
+ +What is this llvm.global_ctors and + _GLOBAL__I__tmp_webcompile... stuff that happens when I #include + <iostream>?
+If you #include the <iostream> header into a C++ translation unit, the + file will probably use the std::cin/std::cout/... global + objects. However, C++ does not guarantee an order of initialization between + static objects in different translation units, so if a static ctor/dtor in your + .cpp file used std::cout, for example, the object would not necessarily + be automatically initialized before your use.
+ +To make std::cout and friends work correctly in these scenarios, the + STL that we use declares a static object that gets created in every translation + unit that includes <iostream>. This object has a static + constructor and destructor that initializes and destroys the global iostream + objects before they could possibly be used in the file. The code that you see + in the .ll file corresponds to the constructor and destructor registration code. +
+ +If you would like to make it easier to understand the LLVM code + generated by the compiler in the demo page, consider using printf() + instead of iostreams to print values.
+ ++ If you are using the LLVM demo page, you may often wonder what happened to all + of the code that you typed in. Remember that the demo script is running the + code through the LLVM optimizers, so if your code doesn't actually do anything + useful, it might all be deleted. +
+ ++ To prevent this, make sure that the code is actually needed. For example, if + you are computing some expression, return the value from the function instead of + leaving it in a local variable. If you really want to constrain the optimizer, + you can read from and assign to volatile global variables. +
++ undef is the LLVM way of representing + a value that is not defined. You can get these if you do not initialize a + variable before you use it. For example, the C function:
+ +
+ int X() { int i; return i; }
+
+ Is compiled to "ret i32 undef" because "i" never has + a value specified for it.
+This section describes how to acquire and build llvm-gcc 4.0 and 4.2, which are + based on the GCC 4.0.1/4.2.1 front-ends respectively. Both front-ends support C, + C++, Objective-C and Objective-C++. The 4.2 front-end also supports Ada and + Fortran to some extent. Note that the instructions for building these front-ends + are completely different (and much easier!) than those for building llvm-gcc3 in + the past.
+ +Retrieve the appropriate llvm-gcc4.x-y.z.source.tar.gz archive from the + llvm web site.
+ +It is also possible to download the sources of the llvm-gcc front end + from a read-only mirror using subversion. To check out the 4.0 code + for first time use:
+ ++ svn co http://llvm.org/svn/llvm-project/llvm-gcc-4.0/trunk dst-directory ++
To check out the 4.2 code use:
+ ++ svn co http://llvm.org/svn/llvm-project/llvm-gcc-4.2/trunk dst-directory ++
After that, the code can be be updated in the destination directory + using:
+ +svn update+
The mirror is brought up to date every evening.
Building with support for Ada amounts to following the directions in the + top-level README.LLVM file, adding ",ada" to EXTRALANGS, for example: + EXTRALANGS=,ada
+ +There are some complications however:
+ +The only platform for which the Ada front-end is known to build is + 32 bit intel x86 running linux. It is unlikely to build for other + systems without some work.
The build requires having a compiler that supports Ada, C and C++. + The Ada front-end is written in Ada so an Ada compiler is needed to + build it. Compilers known to work with the + LLVM 2.2 release + are gcc-4.2 and the + 2005 GNAT GPL Edition. + LLVM from subversion + also works with the + 2006 and 2007 GNAT GPL Editions. + The LLVM parts of llvm-gcc are written in C++ so a C++ compiler is + needed to build them. The rest of gcc is written in C. + Some linux distributions provide a version of gcc that supports all + three languages (the Ada part often comes as an add-on package to + the rest of gcc). Otherwise it is possible to combine two versions + of gcc, one that supports Ada and C (such as the + 2005 GNAT GPL Edition) + and another which supports C++, see below.
Because the Ada front-end is experimental, it is wise to build the + compiler with checking enabled. This causes it to run much slower, but + helps catch mistakes in the compiler (please report any problems using + LLVM bugzilla).
Supposing appropriate compilers are available, llvm-gcc with Ada support can + be built on an x86-32 linux box using the following recipe:
+ +Download the LLVM source + and unpack it:
+ +wget http://llvm.org/releases/2.2/llvm-2.2.tar.gz + tar xzf llvm-2.2.tar.gz + mv llvm-2.2 llvm+
or check out the + latest version from subversion:
+ +svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm+
Download the + llvm-gcc-4.2 source + and unpack it:
+ +wget http://llvm.org/releases/2.2/llvm-gcc4.2-2.2.source.tar.gz + tar xzf llvm-gcc4.2-2.2.source.tar.gz + mv llvm-gcc4.2-2.2.source llvm-gcc-4.2+
or check out the + latest version from subversion:
+ +svn co http://llvm.org/svn/llvm-project/llvm-gcc-4.2/trunk llvm-gcc-4.2+
Make a build directory llvm-objects for llvm and make it the + current directory:
+ +mkdir llvm-objects + cd llvm-objects+
Configure LLVM (here it is configured to install into /usr/local):
+ +../llvm/configure --prefix=/usr/local+
If you have a multi-compiler setup and the C++ compiler is not the + default, then you can configure like this:
+ +CXX=PATH_TO_C++_COMPILER ../llvm/configure --prefix=/usr/local+
Build LLVM with checking enabled (use ENABLE_OPTIMIZED=1 to + build without checking):
+ +make ENABLE_OPTIMIZED=0+
Install LLVM (optional):
+ +make install+
Make a build directory llvm-gcc-4.2-objects for llvm-gcc and make it the + current directory:
+ ++ cd .. + mkdir llvm-gcc-4.2-objects + cd llvm-gcc-4.2-objects+
Configure llvm-gcc (here it is configured to install into /usr/local). + The --enable-checking flag turns on sanity checks inside the compiler. + If you omit it then LLVM must be built with make ENABLE_OPTIMIZED=1. + Additional languages can be appended to the --enable-languages switch, + for example --enable-languages=ada,c,c++.
+ +../llvm-gcc-4.2/configure --prefix=/usr/local --enable-languages=ada,c --enable-checking --enable-llvm=$PWD/../llvm-objects --disable-shared --disable-bootstrap --disable-multilib+
If you have a multi-compiler setup, then you can configure like this:
++ export CC=PATH_TO_C_AND_ADA_COMPILER + export CXX=PATH_TO_C++_COMPILER + ../llvm-gcc-4.2/configure --prefix=/usr/local --enable-languages=ada,c --enable-checking --enable-llvm=$PWD/../llvm-objects --disable-shared --disable-bootstrap --disable-multilib+
Build and install the compiler:
+ +make + make install+
+ To build with support for Fortran, follow the directions in the top-level + README.LLVM file, adding ",fortran" to EXTRALANGS, for example:
+ ++ EXTRALANGS=,fortran ++
+ The LLVM GCC frontend is licensed to you under the GNU General Public License + and the GNU Lesser General Public License. Please see the files COPYING and + COPYING.LIB for more details. +
+ ++ More information is available in the FAQ. +
+Garbage collection is a widely used technique that frees the programmer from + having to know the lifetimes of heap objects, making software easier to produce + and maintain. Many programming languages rely on garbage collection for + automatic memory management. There are two primary forms of garbage collection: + conservative and accurate.
+ +Conservative garbage collection often does not require any special support + from either the language or the compiler: it can handle non-type-safe + programming languages (such as C/C++) and does not require any special + information from the compiler. The + Boehm collector is + an example of a state-of-the-art conservative collector.
+ +Accurate garbage collection requires the ability to identify all pointers in + the program at run-time (which requires that the source-language be type-safe in + most cases). Identifying pointers at run-time requires compiler support to + locate all places that hold live pointer variables at run-time, including the + processor stack and registers.
+ +Conservative garbage collection is attractive because it does not require any + special compiler support, but it does have problems. In particular, because the + conservative garbage collector cannot know that a particular word in the + machine is a pointer, it cannot move live objects in the heap (preventing the + use of compacting and generational GC algorithms) and it can occasionally suffer + from memory leaks due to integer values that happen to point to objects in the + program. In addition, some aggressive compiler transformations can break + conservative garbage collectors (though these seem rare in practice).
+ +Accurate garbage collectors do not suffer from any of these problems, but + they can suffer from degraded scalar optimization of the program. In particular, + because the runtime must be able to identify and update all pointers active in + the program, some optimizations are less effective. In practice, however, the + locality and performance benefits of using aggressive garbage allocation + techniques dominates any low-level losses.
+ +This document describes the mechanisms and interfaces provided by LLVM to + support accurate garbage collection.
+ +LLVM's intermediate representation provides garbage + collection intrinsics that offer support for a broad class of + collector models. For instance, the intrinsics permit:
+ +We hope that the primitive support built into the LLVM IR is sufficient to + support a broad class of garbage collected languages including Scheme, ML, Java, + C#, Perl, Python, Lua, Ruby, other scripting languages, and more.
+ +However, LLVM does not itself implement a garbage collector. This is because + collectors are tightly coupled to object models, and LLVM is agnostic to object + models. Since LLVM is agnostic to object models, it would be inappropriate for + LLVM to dictate any particular collector. Instead, LLVM provides a framework for + garbage collector implementations in two manners:
+ +In general, using a collector implies:
+ +This table summarizes the available runtimes.
+ +| Collector | +gc attribute | +Linkage | +gcroot | +gcread | +gcwrite | +
|---|---|---|---|---|---|
| SemiSpace | +gc "shadow-stack" | +TODO FIXME | +required | +optional | +optional | +
| Ocaml | +gc "ocaml" | +provided by ocamlopt | +required | +optional | +optional | +
The sections for Collection intrinsics and + Recommended runtime interface detail the interfaces that + collectors may require user programs to utilize.
+ +The ShadowStack backend is invoked with the gc "shadow-stack" + function attribute. + Unlike many collectors which rely on a cooperative code generator to generate + stack maps, this algorithm carefully maintains a linked list of stack root + descriptors [Henderson2002]. This so-called "shadow + stack" mirrors the machine stack. Maintaining this data structure is slower + than using stack maps, but has a significant portability advantage because it + requires no special support from the target code generator.
+ +The ShadowStack collector does not use read or write barriers, so the user + program may use load and store instead of llvm.gcread + and llvm.gcwrite.
+ +ShadowStack is a code generator plugin only. It must be paired with a + compatible runtime.
+ +The SemiSpace runtime implements the suggested + runtime interface and is compatible with the ShadowStack backend.
+ +SemiSpace is a very simple copying collector. When it starts up, it + allocates two blocks of memory for the heap. It uses a simple bump-pointer + allocator to allocate memory from the first block until it runs out of space. + When it runs out of space, it traces through all of the roots of the program, + copying blocks to the other half of the memory space.
+ +This runtime is highly experimental and has not been used in a real project. + Enhancements would be welcomed.
+ +The ocaml backend is invoked with the gc "ocaml" function attribute. + It supports the + Objective Caml language runtime by emitting + a type-accurate stack map in the form of an ocaml 3.10.0-compatible frametable. + The linkage requirements are satisfied automatically by the ocamlopt + compiler when linking an executable.
+ +The ocaml collector does not use read or write barriers, so the user program + may use load and store instead of llvm.gcread and + llvm.gcwrite.
+ +This section describes the garbage collection facilities provided by the + LLVM intermediate representation.
+ +These facilities are limited to those strictly necessary for compilation. + They are not intended to be a complete interface to any garbage collector. + Notably, heap allocation is not among the supplied primitives. A user program + will also need to interface with the runtime, using either the + suggested runtime interface or another interface + specified by the runtime.
+ +The gc function attribute is used to specify the desired collector + algorithm to the compiler. It is equivalent to specifying the collector name + programmatically using the setCollector method of + Function.
+ +Specifying the collector on a per-function basis allows LLVM to link together + programs that use different garbage collection algorithms.
+ +The llvm.gcroot intrinsic is used to inform LLVM of a pointer + variable on the stack. The first argument must be a value referring to an alloca instruction + or a bitcast of an alloca. The second contains a pointer to metadata that + should be associated with the pointer, and must be a constant or global + value address. If your target collector uses tags, use a null pointer for + metadata.
+ +Consider the following fragment of Java code:
+ +
+ {
+ Object X; // A null-initialized reference to an object
+ ...
+ }
+
+
+ This block (which may be located in the middle of a function or in a loop + nest), could be compiled to this LLVM code:
+ ++ Entry: + ;; In the entry block for the function, allocate the + ;; stack space for X, which is an LLVM pointer. + %X = alloca %Object* + + ;; Tell LLVM that the stack space is a stack root. + ;; Java has type-tags on objects, so we pass null as metadata. + %tmp = bitcast %Object** %X to i8** + call void @llvm.gcroot(i8** %X, i8* null) + ... + + ;; "CodeBlock" is the block corresponding to the start + ;; of the scope above. + CodeBlock: + ;; Java null-initializes pointers. + store %Object* null, %Object** %X + + ... + + ;; As the pointer goes out of scope, store a null value into + ;; it, to indicate that the value is no longer live. + store %Object* null, %Object** %X + ... ++ +
Some collectors need to be informed when the mutator (the program that needs + garbage collection) either reads a pointer from or writes a pointer to a field + of a heap object. The code fragments inserted at these points are called + read barriers and write barriers, respectively. The amount of + code that needs to be executed is usually quite small and not on the critical + path of any computation, so the overall performance impact of the barrier is + tolerable.
+ +Barriers often require access to the object pointer rather than the + derived pointer (which is a pointer to the field within the + object). Accordingly, these intrinsics take both pointers as separate arguments + for completeness. In this snippet, %object is the object pointer, and + %derived is the derived pointer:
+ +
+ ;; An array type.
+ %class.Array = type { %class.Object, i32, [0 x %class.Object*] }
+ ...
+
+ ;; Load the object pointer from a gcroot.
+ %object = load %class.Array** %object_addr
+
+ ;; Compute the derived pointer.
+ %derived = getelementptr %object, i32 0, i32 2, i32 %n
+
+ For write barriers, LLVM provides the llvm.gcwrite intrinsic + function. It has exactly the same semantics as a non-volatile store to + the derived pointer (the third argument).
+ +Many important algorithms require write barriers, including generational + and concurrent collectors. Additionally, write barriers could be used to + implement reference counting.
+ +The use of this intrinsic is optional if the target collector does use + write barriers. If so, the collector will replace it with the corresponding + store.
+ +For read barriers, LLVM provides the llvm.gcread intrinsic function. + It has exactly the same semantics as a non-volatile load from the + derived pointer (the second argument).
+ +Read barriers are needed by fewer algorithms than write barriers, and may + have a greater performance impact since pointer reads are more frequent than + writes.
+ +As with llvm.gcwrite, a target collector might not require the use + of this intrinsic.
+ +LLVM specifies the following recommended runtime interface to the garbage + collection at runtime. A program should use these interfaces to accomplish the + tasks not supported by the intrinsics.
+ +Unlike the intrinsics, which are integral to LLVM's code generator, there is + nothing unique about these interfaces; a front-end compiler and runtime are free + to agree to a different specification.
+ +Note: This interface is a work in progress.
+ ++ The llvm_gc_initialize function should be called once before any other + garbage collection functions are called. This gives the garbage collector the + chance to initialize itself and allocate the heap. The initial heap size to + allocate should be specified as an argument. +
+ +The llvm_gc_allocate function is a global function defined by the + garbage collector implementation to allocate memory. It returns a + zeroed-out block of memory of the specified size, sufficiently aligned to store + any object.
+ ++ The llvm_gc_collect function is exported by the garbage collector + implementations to provide a full collection, even when the heap is not + exhausted. This can be used by end-user code as a hint, and may be ignored by + the garbage collector. +
+ ++ The llvm_cg_walk_gcroots function is a function provided by the code + generator that iterates through all of the GC roots on the stack, calling the + specified function pointer with each record. For each GC root, the address of + the pointer and the meta-data (from the llvm.gcroot intrinsic) are provided. +
+User code specifies which collector plugin to use with the gc + function attribute or, equivalently, with the setCollector method of + Function.
+ +To implement a collector plugin, it is necessary to subclass + llvm::Collector, which can be accomplished in a few lines of + boilerplate code. LLVM's infrastructure provides access to several important + algorithms. For an uncontroversial collector, all that remains may be to emit + the assembly code for the collector's unique stack map data structure, which + might be accomplished in as few as 100 LOC.
+ +To subclass llvm::Collector and register a collector:
+ +// lib/MyGC/MyGC.cpp - Example LLVM collector plugin
+
+ #include "llvm/CodeGen/Collector.h"
+ #include "llvm/CodeGen/Collectors.h"
+ #include "llvm/CodeGen/CollectorMetadata.h"
+ #include "llvm/Support/Compiler.h"
+
+ using namespace llvm;
+
+ namespace {
+ class VISIBILITY_HIDDEN MyCollector : public Collector {
+ public:
+ MyCollector() {}
+ };
+
+ CollectorRegistry::Add<MyCollector>
+ X("mygc", "My bespoke garbage collector.");
+ }
+
+ Using the LLVM makefiles (like the sample + project), this can be built into a plugin using a simple makefile:
+ ++ +# lib/MyGC/Makefile + + LEVEL := ../.. + LIBRARYNAME = MyGC + LOADABLE_MODULE = 1 + + include $(LEVEL)/Makefile.common
Once the plugin is compiled, code using it may be compiled using llc + -load=MyGC.so (though MyGC.so may have some other + platform-specific extension):
+ +$ cat sample.ll
+ define void @f() gc "mygc" {
+ entry:
+ ret void
+ }
+ $ llvm-as < sample.ll | llc -load=MyGC.so
+
+ It is also possible to statically link the collector plugin into tools, such + as a language-specific compiler front-end.
+ +The boilerplate collector above does nothing. More specifically:
+ +Collector provides a range of features through which a plugin + collector may do useful work. This matrix summarizes the supported (and planned) + features and correlates them with the collection techniques which typically + require them.
+ +| Algorithm | +Done | +shadow stack | +refcount | +mark-sweep | +copying | +incremental | +threaded | +concurrent | +|
|---|---|---|---|---|---|---|---|---|---|
| stack map | +✔ | ++ | + | ✘ | +✘ | +✘ | +✘ | +✘ | +|
| initialize roots | +✔ | +✘ | +✘ | +✘ | +✘ | +✘ | +✘ | +✘ | +|
| derived pointers | +NO | ++ | + | + | + | + | ✘* | +✘* | +|
| custom lowering | +✔ | ++ | + | + | + | + | + | + | |
| gcroot | +✔ | +✘ | +✘ | ++ | + | + | + | + | |
| gcwrite | +✔ | ++ | ✘ | ++ | + | ✘ | ++ | ✘ | +|
| gcread | +✔ | ++ | + | + | + | + | + | ✘ | +|
| safe points | ++ | + | + | + | + | + | + | + | |
| in calls | +✔ | ++ | + | ✘ | +✘ | +✘ | +✘ | +✘ | +|
| before calls | +✔ | ++ | + | + | + | + | ✘ | +✘ | +|
| for loops | +NO | ++ | + | + | + | + | ✘ | +✘ | +|
| before escape | +✔ | ++ | + | + | + | + | ✘ | +✘ | +|
| emit code at safe points | +NO | ++ | + | + | + | + | ✘ | +✘ | +|
| output | ++ | + | + | + | + | + | + | + | |
| assembly | +✔ | ++ | + | ✘ | +✘ | +✘ | +✘ | +✘ | +|
| JIT | +NO | ++ | + | ✘ | +✘ | +✘ | +✘ | +✘ | +|
| obj | +NO | ++ | + | ✘ | +✘ | +✘ | +✘ | +✘ | +|
| live analysis | +NO | ++ | + | ✘ | +✘ | +✘ | +✘ | +✘ | +|
| register map | +NO | ++ | + | ✘ | +✘ | +✘ | +✘ | +✘ | +|
|
+ * Derived pointers only pose a
+ hazard to copying collectors.
+ ✘ in gray denotes a feature which
+ could be utilized if available.
+ |
+ |||||||||
To be clear, the collection techniques above are defined as:
+ +As the matrix indicates, LLVM's garbage collection infrastructure is already + suitable for a wide variety of collectors, but does not currently extend to + multithreaded programs. This will be added in the future as there is + interest.
+ +for (iterator I = begin(), E = end(); I != E; ++I) {
+ CollectorMetadata *MD = *I;
+ unsigned FrameSize = MD->getFrameSize();
+ size_t RootCount = MD->roots_size();
+
+ for (CollectorMetadata::roots_iterator RI = MD->roots_begin(),
+ RE = MD->roots_end();
+ RI != RE; ++RI) {
+ int RootNum = RI->Num;
+ int RootStackOffset = RI->StackOffset;
+ Constant *RootMetadata = RI->Metadata;
+ }
+ }
+
+ LLVM automatically computes a stack map. All a Collector needs to do + is access it using CollectorMetadata::roots_begin() and + -end(). If the llvm.gcroot intrinsic is eliminated before code + generation by a custom lowering pass, LLVM's stack map will be empty.
+ +MyCollector::MyCollector() {
+ InitRoots = true;
+ }
+
+ When set, LLVM will automatically initialize each root to null upon + entry to the function. This prevents the reachability analysis from finding + uninitialized values in stack roots at runtime, which will almost certainly + cause it to segfault. This initialization occurs before custom lowering, so the + two may be used together.
+ +Since LLVM does not yet compute liveness information, this feature should be + used by all collectors which do not custom lower llvm.gcroot, and even + some that do.
+ +For collectors with barriers or unusual treatment of stack roots, these + flags allow the collector to perform any required transformation on the LLVM + IR:
+ +class MyCollector : public Collector {
+ public:
+ MyCollector() {
+ CustomRoots = true;
+ CustomReadBarriers = true;
+ CustomWriteBarriers = true;
+ }
+
+ virtual bool initializeCustomLowering(Module &M);
+ virtual bool performCustomLowering(Function &F);
+ };
+
+ If any of these flags are set, then LLVM suppresses its default lowering for + the corresponding intrinsics and instead passes them on to a custom lowering + pass specified by the collector.
+ +LLVM's default action for each intrinsic is as follows:
+ +If CustomReadBarriers or CustomWriteBarriers are specified, + then performCustomLowering must eliminate the + corresponding barriers.
+ +performCustomLowering, must comply with the same restrictions as runOnFunction, and + that initializeCustomLowering has the same semantics as doInitialization(Module + &).
+ +The following can be used as a template:
+ +#include "llvm/Module.h"
+ #include "llvm/IntrinsicInst.h"
+
+ bool MyCollector::initializeCustomLowering(Module &M) {
+ return false;
+ }
+
+ bool MyCollector::performCustomLowering(Function &F) {
+ bool MadeChange = false;
+
+ for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
+ for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; )
+ if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++))
+ if (Function *F = CI->getCalledFunction())
+ switch (F->getIntrinsicID()) {
+ case Intrinsic::gcwrite:
+ // Handle llvm.gcwrite.
+ CI->eraseFromParent();
+ MadeChange = true;
+ break;
+ case Intrinsic::gcread:
+ // Handle llvm.gcread.
+ CI->eraseFromParent();
+ MadeChange = true;
+ break;
+ case Intrinsic::gcroot:
+ // Handle llvm.gcroot.
+ CI->eraseFromParent();
+ MadeChange = true;
+ break;
+ }
+
+ return MadeChange;
+ }
+
+ LLVM can compute four kinds of safe points:
+ +namespace GC {
+ /// PointKind - The type of a collector-safe point.
+ ///
+ enum PointKind {
+ Loop, //< Instr is a loop (backwards branch).
+ Return, //< Instr is a return instruction.
+ PreCall, //< Instr is a call instruction.
+ PostCall //< Instr is the return address of a call.
+ };
+ }
+
+ A collector can request any combination of the four by setting the + NeededSafePoints mask:
+ +MyCollector::MyCollector() {
+ NeededSafePoints = 1 << GC::Loop
+ | 1 << GC::Return
+ | 1 << GC::PreCall
+ | 1 << GC::PostCall;
+ }
+
+ It can then use the following routines to access safe points.
+ +for (iterator I = begin(), E = end(); I != E; ++I) {
+ CollectorMetadata *MD = *I;
+ size_t PointCount = MD->size();
+
+ for (CollectorMetadata::iterator PI = MD->begin(),
+ PE = MD->end(); PI != PE; ++PI) {
+ GC::PointKind PointKind = PI->Kind;
+ unsigned PointNum = PI->Num;
+ }
+ }
+
+
+ Almost every collector requires PostCall safe points, since these + correspond to the moments when the function is suspended during a call to a + subroutine.
+ +Threaded programs generally require Loop safe points to guarantee + that the application will reach a safe point within a bounded amount of time, + even if it is executing a long-running loop which contains no function + calls.
+ +Threaded collectors may also require Return and PreCall + safe points to implement "stop the world" techniques using self-modifying code, + where it is important that the program not exit the function without reaching a + safe point (because only the topmost function has been patched).
+ +LLVM allows a collector to print arbitrary assembly code before and after + the rest of a module's assembly code. From the latter callback, the collector + can print stack maps built by the code generator.
+ +Note that LLVM does not currently have analogous APIs to support code + generation in the JIT, nor using the object writers.
+ +class MyCollector : public Collector {
+ public:
+ virtual void beginAssembly(std::ostream &OS, AsmPrinter &AP,
+ const TargetAsmInfo &TAI);
+
+ virtual void finishAssembly(std::ostream &OS, AsmPrinter &AP,
+ const TargetAsmInfo &TAI);
+ }
+
+ The collector should use AsmPrinter and TargetAsmInfo to + print portable assembly code to the std::ostream. The collector itself + contains the stack map for the entire module, and may access the + CollectorMetadata using its own begin() and end() + methods. Here's a realistic example:
+ +#include "llvm/CodeGen/AsmPrinter.h"
+ #include "llvm/Function.h"
+ #include "llvm/Target/TargetMachine.h"
+ #include "llvm/Target/TargetData.h"
+ #include "llvm/Target/TargetAsmInfo.h"
+
+ void MyCollector::beginAssembly(std::ostream &OS, AsmPrinter &AP,
+ const TargetAsmInfo &TAI) {
+ // Nothing to do.
+ }
+
+ void MyCollector::finishAssembly(std::ostream &OS, AsmPrinter &AP,
+ const TargetAsmInfo &TAI) {
+ // Set up for emitting addresses.
+ const char *AddressDirective;
+ int AddressAlignLog;
+ if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
+ AddressDirective = TAI.getData32bitsDirective();
+ AddressAlignLog = 2;
+ } else {
+ AddressDirective = TAI.getData64bitsDirective();
+ AddressAlignLog = 3;
+ }
+
+ // Put this in the data section.
+ AP.SwitchToDataSection(TAI.getDataSection());
+
+ // For each function...
+ for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
+ CollectorMetadata &MD = **FI;
+
+ // Emit this data structure:
+ //
+ // struct {
+ // int32_t PointCount;
+ // struct {
+ // void *SafePointAddress;
+ // int32_t LiveCount;
+ // int32_t LiveOffsets[LiveCount];
+ // } Points[PointCount];
+ // } __gcmap_<FUNCTIONNAME>;
+
+ // Align to address width.
+ AP.EmitAlignment(AddressAlignLog);
+
+ // Emit the symbol by which the stack map can be found.
+ std::string Symbol;
+ Symbol += TAI.getGlobalPrefix();
+ Symbol += "__gcmap_";
+ Symbol += MD.getFunction().getName();
+ if (const char *GlobalDirective = TAI.getGlobalDirective())
+ OS << GlobalDirective << Symbol << "\n";
+ OS << TAI.getGlobalPrefix() << Symbol << ":\n";
+
+ // Emit PointCount.
+ AP.EmitInt32(MD.size());
+ AP.EOL("safe point count");
+
+ // And each safe point...
+ for (CollectorMetadata::iterator PI = MD.begin(),
+ PE = MD.end(); PI != PE; ++PI) {
+ // Align to address width.
+ AP.EmitAlignment(AddressAlignLog);
+
+ // Emit the address of the safe point.
+ OS << AddressDirective
+ << TAI.getPrivateGlobalPrefix() << "label" << PI->Num;
+ AP.EOL("safe point address");
+
+ // Emit the stack frame size.
+ AP.EmitInt32(MD.getFrameSize());
+ AP.EOL("stack frame size");
+
+ // Emit the number of live roots in the function.
+ AP.EmitInt32(MD.live_size(PI));
+ AP.EOL("live root count");
+
+ // And for each live root...
+ for (CollectorMetadata::live_iterator LI = MD.live_begin(PI),
+ LE = MD.live_end(PI);
+ LI != LE; ++LI) {
+ // Print its offset within the stack frame.
+ AP.EmitInt32(LI->StackOffset);
+ AP.EOL("stack offset");
+ }
+ }
+ }
+ }
+
+
+ Implementing a garbage collector for LLVM is fairly straightforward. The + LLVM garbage collectors are provided in a form that makes them easy to link into + the language-specific runtime that a language front-end would use. They require + functionality from the language-specific runtime to get information about where pointers are located in heap objects.
+ +The implementation must include the + llvm_gc_allocate and + llvm_gc_collect functions. To do this, it will + probably have to trace through the roots + from the stack and understand the GC descriptors + for heap objects. Luckily, there are some example + implementations available. +
++ The three most common ways to keep track of where pointers live in heap objects + are (listed in order of space overhead required):
+ +The LLVM garbage collectors are capable of supporting all of these styles of + language, including ones that mix various implementations. To do this, it + allows the source-language to associate meta-data with the stack roots, and the heap tracing routines can propagate the + information. In addition, LLVM allows the front-end to extract GC information + in any form from a specific object pointer (this supports situations #1 and #3). +
+ +[Appel89] Runtime Tags Aren't Necessary. Andrew + W. Appel. Lisp and Symbolic Computation 19(7):703-705, July 1989.
+ +[Goldberg91] Tag-free garbage collection for + strongly typed programming languages. Benjamin Goldberg. ACM SIGPLAN + PLDI'91.
+ +[Tolmach94] Tag-free garbage collection using + explicit type parameters. Andrew Tolmach. Proceedings of the 1994 ACM + conference on LISP and functional programming.
+ +[Henderson2002] + Accurate Garbage Collection in an Uncooperative Environment. + Fergus Henderson. International Symposium on Memory Management 2002.
+ +This document seeks to dispel the mystery and confusion surrounding LLVM's + GetElementPtr (GEP) instruction. Questions about the wiley GEP instruction are + probably the most frequently occuring questions once a developer gets down to + coding with LLVM. Here we lay out the sources of confusion and show that the + GEP instruction is really quite simple. +
+When people are first confronted with the GEP instruction, they tend to + relate it to known concepts from other programming paradigms, most notably C + array indexing and field selection. However, GEP is a little different and + this leads to the following questions; all of which are answered in the + following sections.
+ +Quick answer: The index stepping through the first operand.
+The confusion with the first index usually arises from thinking about + the GetElementPtr instruction as if it was a C index operator. They aren't the + same. For example, when we write, in "C":
+ ++ AType *Foo; + ... + X = &Foo->F; ++
it is natural to think that there is only one index, the selection of the + field F. However, in this example, Foo is a pointer. That + pointer must be indexed explicitly in LLVM. C, on the other hand, indexs + through it transparently. To arrive at the same address location as the C + code, you would provide the GEP instruction with two index operands. The + first operand indexes through the pointer; the second operand indexes the + field F of the structure, just as if you wrote:
+ ++ X = &Foo[0].F; ++
Sometimes this question gets rephrased as:
++Why is it okay to index through the first pointer, but + subsequent pointers won't be dereferenced?
The answer is simply because memory does not have to be accessed to + perform the computation. The first operand to the GEP instruction must be a + value of a pointer type. The value of the pointer is provided directly to + the GEP instruction as an operand without any need for accessing memory. It + must, therefore be indexed and requires an index operand. Consider this + example:
+ +
+ struct munger_struct {
+ int f1;
+ int f2;
+ };
+ void munge(struct munger_struct *P) {
+ P[0].f1 = P[1].f1 + P[2].f2;
+ }
+ ...
+ munger_struct Array[3];
+ ...
+ munge(Array);
+
+ In this "C" example, the front end compiler (llvm-gcc) will generate three + GEP instructions for the three indices through "P" in the assignment + statement. The function argument P will be the first operand of each + of these GEP instructions. The second operand indexes through that pointer. + The third operand will be the field offset into the + struct munger_struct type, for either the f1 or + f2 field. So, in LLVM assembly the munge function looks + like:
+ +
+ void %munge(%struct.munger_struct* %P) {
+ entry:
+ %tmp = getelementptr %struct.munger_struct* %P, i32 1, i32 0
+ %tmp = load i32* %tmp
+ %tmp6 = getelementptr %struct.munger_struct* %P, i32 2, i32 1
+ %tmp7 = load i32* %tmp6
+ %tmp8 = add i32 %tmp7, %tmp
+ %tmp9 = getelementptr %struct.munger_struct* %P, i32 0, i32 0
+ store i32 %tmp8, i32* %tmp9
+ ret void
+ }
+
+ In each case the first operand is the pointer through which the GEP + instruction starts. The same is true whether the first operand is an + argument, allocated memory, or a global variable.
+To make this clear, let's consider a more obtuse example:
+ ++ %MyVar = unintialized global i32 + ... + %idx1 = getelementptr i32* %MyVar, i64 0 + %idx2 = getelementptr i32* %MyVar, i64 1 + %idx3 = getelementptr i32* %MyVar, i64 2 ++
These GEP instructions are simply making address computations from the + base address of MyVar. They compute, as follows (using C syntax): +
+ ++ idx1 = (char*) &MyVar + 0 + idx2 = (char*) &MyVar + 4 + idx3 = (char*) &MyVar + 8 ++
Since the type i32 is known to be four bytes long, the indices + 0, 1 and 2 translate into memory offsets of 0, 4, and 8, respectively. No + memory is accessed to make these computations because the address of + %MyVar is passed directly to the GEP instructions.
+The obtuse part of this example is in the cases of %idx2 and + %idx3. They result in the computation of addresses that point to + memory past the end of the %MyVar global, which is only one + i32 long, not three i32s long. While this is legal in LLVM, + it is inadvisable because any load or store with the pointer that results + from these GEP instructions would produce undefined results.
+Quick answer: there are no superfluous indices.
+This question arises most often when the GEP instruction is applied to a + global variable which is always a pointer type. For example, consider + this:
+ +
+ %MyStruct = uninitialized global { float*, i32 }
+ ...
+ %idx = getelementptr { float*, i32 }* %MyStruct, i64 0, i32 1
+
+ The GEP above yields an i32* by indexing the i32 typed + field of the structure %MyStruct. When people first look at it, they + wonder why the i64 0 index is needed. However, a closer inspection + of how globals and GEPs work reveals the need. Becoming aware of the following + facts will dispell the confusion:
+Quick answer: nothing.
+The GetElementPtr instruction dereferences nothing. That is, it doesn't + access memory in any way. That's what the Load and Store instructions are for. + GEP is only involved in the computation of addresses. For example, consider + this:
+ +
+ %MyVar = uninitialized global { [40 x i32 ]* }
+ ...
+ %idx = getelementptr { [40 x i32]* }* %MyVar, i64 0, i32 0, i64 0, i64 17
+
+ In this example, we have a global variable, %MyVar that is a + pointer to a structure containing a pointer to an array of 40 ints. The + GEP instruction seems to be accessing the 18th integer of the structure's + array of ints. However, this is actually an illegal GEP instruction. It + won't compile. The reason is that the pointer in the structure must + be dereferenced in order to index into the array of 40 ints. Since the + GEP instruction never accesses memory, it is illegal.
+In order to access the 18th integer in the array, you would need to do the + following:
+ +
+ %idx = getelementptr { [40 x i32]* }* %, i64 0, i32 0
+ %arr = load [40 x i32]** %idx
+ %idx = getelementptr [40 x i32]* %arr, i64 0, i64 17
+
+ In this case, we have to load the pointer in the structure with a load + instruction before we can index into the array. If the example was changed + to:
+ +
+ %MyVar = uninitialized global { [40 x i32 ] }
+ ...
+ %idx = getelementptr { [40 x i32] }*, i64 0, i32 0, i64 17
+
+ then everything works fine. In this case, the structure does not contain a + pointer and the GEP instruction can index through the global variable, + into the first field of the structure and access the 18th i32 in the + array there.
+Quick Answer: They compute different address locations.
+If you look at the first indices in these GEP + instructions you find that they are different (0 and 1), therefore the address + computation diverges with that index. Consider this example:
+ +
+ %MyVar = global { [10 x i32 ] }
+ %idx1 = getlementptr { [10 x i32 ] }* %MyVar, i64 0, i32 0, i64 1
+ %idx2 = getlementptr { [10 x i32 ] }* %MyVar, i64 1
+
+ In this example, idx1 computes the address of the second integer + in the array that is in the structure in %MyVar, that is MyVar+4. The + type of idx1 is i32*. However, idx2 computes the + address of the next structure after %MyVar. The type of + idx2 is { [10 x i32] }* and its value is equivalent + to MyVar + 40 because it indexes past the ten 4-byte integers + in MyVar. Obviously, in such a situation, the pointers don't + alias.
+Quick Answer: They compute the same address location.
+These two GEP instructions will compute the same address because indexing + through the 0th element does not change the address. However, it does change + the type. Consider this example:
+ +
+ %MyVar = global { [10 x i32 ] }
+ %idx1 = getlementptr { [10 x i32 ] }* %MyVar, i64 1, i32 0, i64 0
+ %idx2 = getlementptr { [10 x i32 ] }* %MyVar, i64 1
+
+ In this example, the value of %idx1 is %MyVar+40 and + its type is i32*. The value of %idx2 is also + MyVar+40 but its type is { [10 x i32] }*.
+In summary, here's some things to always remember about the GetElementPtr + instruction:
+Welcome to LLVM! In order to get started, you first need to know some + basic information.
+ +First, LLVM comes in two pieces. The first piece is the LLVM suite. This + contains all of the tools, libraries, and header files needed to use the low + level virtual machine. It contains an assembler, disassembler, bitcode + analyzer and bitcode optimizer. It also contains a test suite that can be + used to test the LLVM tools and the GCC front end.
+ +The second piece is the GCC front end. This component provides a version of + GCC that compiles C and C++ code into LLVM bitcode. Currently, the GCC front + end uses the GCC parser to convert code to LLVM. Once + compiled into LLVM bitcode, a program can be manipulated with the LLVM tools + from the LLVM suite.
+ ++ There is a third, optional piece called llvm-test. It is a suite of programs + with a testing harness that can be used to further test LLVM's functionality + and performance. +
+ +Here's the short story for getting up and running quickly with LLVM:
+ +Specify for directory the full pathname of where you + want the LLVM tools and libraries to be installed (default + /usr/local).
Optionally, specify for directory the full pathname of the + C/C++ front end installation to use with this LLVM configuration. If + not specified, the PATH will be searched.
Enable the SPEC2000 benchmarks for testing. The SPEC2000 + benchmarks should be available in + directory.
Consult the Getting Started with LLVM section for + detailed information on configuring and compiling LLVM. See Setting Up Your Environment for tips that simplify + working with the GCC front end and LLVM tools. Go to Program + Layout to learn about the layout of the source code tree.
+ +Before you begin to use the LLVM system, review the requirements given below. + This may save you some trouble by knowing ahead of time what hardware and + software you will need.
+ +LLVM is known to work on the following platforms:
+ +| OS | +Arch | +Compilers | +
|---|---|---|
| Linux | +x861 | +GCC | +
| Solaris | +V9 (Ultrasparc) | +GCC | +
| FreeBSD | +x861 | +GCC | +
| MacOS X2 | +PowerPC | +GCC | +
| MacOS X2,9 | +x86 | +GCC | + +
| Cygwin/Win32 | +x861,8 | +GCC 3.4.X, binutils 2.15 | +
| MinGW/Win32 | +x861,6,8 | +GCC 3.4.X, binutils 2.15 | +
| Linux | +amd643 | +GCC | +
LLVM has partial support for the following platforms:
+ +| OS | +Arch | +Compilers | +
|---|---|---|
| Windows | +x861 | +Visual Studio .NET4,5 | +
| AIX3,4 | +PowerPC | +GCC | +
| Linux3,5 | +PowerPC | +GCC | +
| Linux7 | +Alpha | +GCC | +
| Linux7 | +Itanium (IA-64) | +GCC | +
| HP-UX7 | +Itanium (IA-64) | +HP aCC | +
Notes:
+ +Note that you will need about 1-3 GB of space for a full LLVM build in Debug + mode, depending on the system (it is so large because of all the debugging + information and the fact that the libraries are statically linked into multiple + tools). If you do not need many of the tools and you are space-conscious, + you can disable them individually in llvm/tools/Makefile. The Release + build requires considerably less space.
+ +The LLVM suite may compile on other platforms, but it is not + guaranteed to do so. If compilation is successful, the LLVM utilities should be + able to assemble, disassemble, analyze, and optimize LLVM bitcode. Code + generation should work as well, although the generated native code may not work + on your platform.
+ +The GCC front end is not very portable at the moment. If you want to get it + to work on another platform, you can download a copy of the source and try to compile it on your platform.
+ +Compiling LLVM requires that you have several software packages + installed. The table below lists those required packages. The Package column + is the usual name for the software package that LLVM depends on. The Version + column provides "known to work" versions of the package. The Notes column + describes how LLVM uses the package and provides other details.
+| Package | Version | Notes |
|---|---|---|
| GNU Make | +3.79, 3.79.1 | +Makefile/build processor | +
| GCC | +3.4.2 | +C/C++ compiler1 | +
| TeXinfo | +4.5 | +For building the CFE | +
| Flex | +2.5.4 | +LEX compiler | +
| Bison | +1.28, 1.35, 1.75, 1.875d, 2.0, or 2.1 (not 1.85 or 1.875) |
+ YACC compiler | +
| SVN | +≥1.3 | +Subversion access to LLVM2 | +
| DejaGnu | +1.4.2 | +Automated test suite3 | +
| tcl | +8.3, 8.4 | +Automated test suite3 | +
| expect | +5.38.0 | +Automated test suite3 | +
| perl | +≥5.6.0 | +Nightly tester, utilities | +
| GNU M4 + | 1.4 | +Macro processor for configuration4 | +
| GNU Autoconf | +2.59 | +Configuration script builder4 | +
| GNU Automake | +1.9.2 | +aclocal macro generator4 | +
| libtool | +1.5.10 | +Shared library manager4 | +
Notes:
+Additionally, your compilation host is expected to have the usual + plethora of Unix utilities. Specifically:
+LLVM is very demanding of the host C++ compiler, and as such tends to expose + bugs in the compiler. In particular, several versions of GCC crash when trying + to compile LLVM. We routinely use GCC 3.3.3, 3.4.0, and Apple 4.0.1 + successfully with them (however, see important notes below). Other versions + of GCC will probably work as well. GCC versions listed + here are known to not work. If you are using one of these versions, please try + to upgrade your GCC to something more recent. If you run into a problem with a + version of GCC not listed here, please let + us know. Please use the "gcc -v" command to find out which version + of GCC you are using. +
+ +GCC versions prior to 3.0: GCC 2.96.x and before had several + problems in the STL that effectively prevent it from compiling LLVM. +
+ +GCC 3.2.2 and 3.2.3: These versions of GCC fails to compile LLVM with + a bogus template error. This was fixed in later GCCs.
+ +GCC 3.3.2: This version of GCC suffered from a serious bug which causes it to crash in + the "convert_from_eh_region_ranges_1" GCC function.
+ +Cygwin GCC 3.3.3: The version of GCC 3.3.3 commonly shipped with + Cygwin does not work. Please upgrade + to a newer version if possible.
+SuSE GCC 3.3.3: The version of GCC 3.3.3 shipped with SuSE 9.1 (and + possibly others) does not compile LLVM correctly (it appears that exception + handling is broken in some cases). Please download the FSF 3.3.3 or upgrade + to a newer version of GCC.
+GCC 3.4.0 on linux/x86 (32-bit): GCC miscompiles portions of the + code generator, causing an infinite loop in the llvm-gcc build when built + with optimizations enabled (i.e. a release build).
+GCC 3.4.2 on linux/x86 (32-bit): GCC miscompiles portions of the + code generator at -O3, as with 3.4.0. However gcc 3.4.2 (unlike 3.4.0) + correctly compiles LLVM at -O2. A work around is to build release LLVM + builds with "make ENABLE_OPTIMIZED=1 OPTIMIZE_OPTION=-O2 ..."
+GCC 3.4.x on X86-64/amd64: GCC + miscompiles portions of LLVM.
+GCC 3.4.4 (CodeSourcery ARM 2005q3-2): this compiler miscompiles LLVM + when building with optimizations enabled. It appears to work with + "make ENABLE_OPTIMIZED=1 OPTIMIZE_OPTION=-O1" or build a debug + build.
+IA-64 GCC 4.0.0: The IA-64 version of GCC 4.0.0 is known to + miscompile LLVM.
+Apple Xcode 2.3: GCC crashes when compiling LLVM at -O3 (which is the + default with ENABLE_OPTIMIZED=1. To work around this, build with + "ENABLE_OPTIMIZED=1 OPTIMIZE_OPTION=-O2".
+GCC 4.1.1: GCC fails to build LLVM with template concept check errors + compiling some files. At the time of this writing, GCC mainline (4.2) + did not share the problem.
+GCC 4.1.1 on X86-64/amd64: GCC + miscompiles portions of LLVM when compiling llvm itself into 64-bit + code. LLVM will appear to mostly work but will be buggy, e.g. failing + portions of its testsuite.
+GCC 4.1.2 on OpenSUSE: Seg faults during libstdc++ build and on x86_64 + platforms compiling md5.c gets a mangled constant.
+GNU ld 2.16.X. Some 2.16.X versions of the ld linker will produce very + long warning messages complaining that some ".gnu.linkonce.t.*" symbol was + defined in a discarded section. You can safely ignore these messages as they are + erroneous and the linkage is correct. These messages disappear using ld + 2.17.
+ +GNU binutils 2.17: Binutils 2.17 contains a bug which + causes huge link times (minutes instead of seconds) when building LLVM. We + recommend upgrading to a newer version (2.17.50.0.4 or later).
+ +The remainder of this guide is meant to get you up and running with + LLVM and to give you some basic information about the LLVM environment.
+ +The later sections of this guide describe the general layout of the the LLVM source tree, a simple example using the LLVM tool chain, and links to find more information about LLVM or to get + help via e-mail.
+Throughout this manual, the following names are used to denote paths + specific to the local system and working environment. These are not + environment variables you need to set but just strings used in the rest + of this document below. In any of the examples below, simply replace + each of these names with the appropriate pathname on your local system. + All these paths are absolute:
+ ++ For the pre-built GCC front end binaries, the LLVMGCCDIR is + llvm-gcc/platform/llvm-gcc. +
+ In order to compile and use LLVM, you may need to set some environment + variables. + +
+ If you have the LLVM distribution, you will need to unpack it before you + can begin to compile it. LLVM is distributed as a set of two files: the LLVM + suite and the LLVM GCC front end compiled for your platform. There is an + additional test suite that is optional. Each file is a TAR archive that is + compressed with the gzip program. +
+ +The files are as follows, with x.y marking the version number: +
It is also possible to download the sources of the llvm-gcc4 front end from a + read-only subversion mirror at + svn://anonsvn.opensource.apple.com/svn/llvm/trunk.
+ +If you have access to our Subversion repository, you can get a fresh copy of + the entire source code. All you need to do is check it out from Subvresion as + follows:
+ +This will create an 'llvm' directory in the current + directory and fully populate it with the LLVM source code, Makefiles, + test directories, and local copies of documentation files.
+ +If you want to get a specific release (as opposed to the most recent + revision), you can checkout it from the 'tags' directory (instead of + 'trunk'). The following releases are located in the following + subdirectories of the 'tags' directory:
+ +If you would like to get the LLVM test suite (a separate package as of 1.4), + you get it from the Subversion repository:
+ ++ % cd llvm/projects + % svn co http://llvm.org/svn/llvm-project/test-suite/trunk llvm-test ++
By placing it in the llvm/projects, it will be automatically + configured by the LLVM configure script as well as automatically updated when + you run svn update.
+ +If you would like to get the GCC front end source code, you can also get it + and build it yourself. Please follow these + instructions to successfully get and build the LLVM GCC front-end.
+ +Before configuring and compiling the LLVM suite, you can optionally extract the + LLVM GCC front end from the binary distribution. It is used for running the + llvm-test testsuite and for compiling C/C++ programs. Note that you can optionally + build llvm-gcc yourself after building the + main LLVM repository.
+ +To install the GCC front end, do the following:
+ +Once the binary is uncompressed, you should add a symlink for llvm-gcc and + llvm-g++ to some directory in your path. When you configure LLVM, it will + automatically detect llvm-gcc's presence (if it is in your path) enabling its + use in llvm-test. Note that you can always build or install llvm-gcc at any + pointer after building the main LLVM repository: just reconfigure llvm and + llvm-test will pick it up. +
+ +The binary versions of the GCC front end may not suit all of your needs. For + example, the binary distribution may include an old version of a system header + file, not "fix" a header file that needs to be fixed for GCC, or it may be + linked with libraries not available on your system.
+ +In cases like these, you may want to try building the GCC front end from source. This is + much easier now than it was in the past.
+ +Once checked out from the Subversion repository, the LLVM suite source + code must be + configured via the configure script. This script sets variables in the + various *.in files, most notably llvm/Makefile.config and + llvm/include/Config/config.h. It also populates OBJ_ROOT with + the Makefiles needed to begin building LLVM.
+ +The following environment variables are used by the configure + script to configure the build system:
+ +| Variable | Purpose |
|---|---|
| CC | +Tells configure which C compiler to use. By default, + configure will look for the first GCC C compiler in + PATH. Use this variable to override + configure's default behavior. | +
| CXX | +Tells configure which C++ compiler to use. By default, + configure will look for the first GCC C++ compiler in + PATH. Use this variable to override + configure's default behavior. | +
The following options can be used to set or enable LLVM specific options:
+ +To configure LLVM, follow these steps:
+ +Change directory into the object root directory:
+ +% cd OBJ_ROOT
Run the configure script located in the LLVM source + tree:
+ +% SRC_ROOT/configure --prefix=/install/path [other options]+
Once you have configured LLVM, you can build it. There are three types of + builds:
+ +Once you have LLVM configured, you can build it by entering the + OBJ_ROOT directory and issuing the following command:
+ +% gmake
If the build fails, please check here to see if you + are using a version of GCC that is known not to compile LLVM.
+ ++ If you have multiple processors in your machine, you may wish to use some of + the parallel build options provided by GNU Make. For example, you could use the + command:
+ +% gmake -j2
There are several special targets which are useful when working with the LLVM + source code:
+ +Please see the Makefile Guide for further + details on these make targets and descriptions of other targets + available.
+ +It is also possible to override default values from configure by + declaring variables on the command line. The following are some examples:
+ +Every directory in the LLVM object tree includes a Makefile to build + it and any subdirectories that it contains. Entering any directory inside the + LLVM object tree and typing gmake should rebuild anything in or below + that directory that is out of date.
+ +It is possible to cross-compile LLVM. That is, you can create LLVM + executables and libraries for a platform different than the one one which you + are compiling. To do this, a few additional steps are + required. 1 To cross-compile LLVM, use + these instructions:
+The result of such a build will produce executables that are not executable + on your build host (--build option) but can be executed on your compile host + (--host option).
+Notes:
+The LLVM build system is capable of sharing a single LLVM source tree among + several LLVM builds. Hence, it is possible to build LLVM for several different + platforms or configurations using the same source tree.
+ +This is accomplished in the typical autoconf manner:
+ +Change directory to where the LLVM object files should live:
+ +% cd OBJ_ROOT
Run the configure script found in the LLVM source + directory:
+ +% SRC_ROOT/configure
The LLVM build will place files underneath OBJ_ROOT in directories + named after the build type:
+ ++ If you're running on a Linux system that supports the "binfmt_misc" + module, and you have root access on the system, you can set your system up to + execute LLVM bitcode files directly. To do this, use commands like this (the + first command may not be required if you are already using the module):
+ ++ $ mount -t binfmt_misc none /proc/sys/fs/binfmt_misc + $ echo ':llvm:M::llvm::/path/to/lli:' > /proc/sys/fs/binfmt_misc/register + $ chmod u+x hello.bc (if needed) + $ ./hello.bc ++
+ This allows you to execute LLVM bitcode files directly. Thanks to Jack + Cummings for pointing this out! +
+ +One useful source of information about the LLVM source base is the LLVM doxygen documentation available at http://llvm.org/doxygen/. + The following is a brief introduction to code layout:
+ +This directory contains some simple examples of how to use the LLVM IR and + JIT.
+This directory contains public header files exported from the LLVM + library. The three main subdirectories of this directory are:
+ +This directory contains most of the source files of the LLVM system. In LLVM, + almost all code exists in libraries, making it very easy to share code among the + different tools.
+ +This directory contains projects that are not strictly part of LLVM but are + shipped with LLVM. This is also the directory where you should create your own + LLVM-based projects. See llvm/projects/sample for an example of how + to set up your own project. See llvm/projects/Stacker for a fully + functional example of a compiler front end.
+This directory contains libraries which are compiled into LLVM bitcode and + used when linking programs with the GCC front end. Most of these libraries are + skeleton versions of real libraries; for example, libc is a stripped down + version of glibc.
+ +Unlike the rest of the LLVM suite, this directory needs the LLVM GCC front + end to compile.
+ +This directory contains feature and regression tests and other basic sanity + checks on the LLVM infrastructure. These are intended to run quickly and cover + a lot of territory without being exhaustive.
+This is not a directory in the normal llvm module; it is a separate + Subversion + module that must be checked out (usually to projects/test-suite). + This + module contains a comprehensive correctness, performance, and benchmarking + test + suite for LLVM. It is a separate Subversion module because not every LLVM + user is + interested in downloading or building such a comprehensive test suite. For + further details on this test suite, please see the + Testing Guide document.
+The tools directory contains the executables built out of the + libraries above, which form the main part of the user interface. You can + always get help for a tool by typing tool_name --help. The + following is a brief introduction to the most important tools. More detailed + information is in the Command Guide.
+ +This directory contains utilities for working with LLVM source code, and some + of the utilities are actually required as part of the build process because they + are code generators for parts of LLVM infrastructure.
+ +This directory contains build scripts and project files for use with + Visual C++. This allows developers on Windows to build LLVM without the need + for Cygwin. The contents of this directory should be considered experimental + at this time. +
+This section gives an example of using LLVM. llvm-gcc3 is now obsolete, + so we only include instructiosn for llvm-gcc4. +
+ +Note: The gcc4 frontend's invocation is considerably different + from the previous gcc3 frontend. In particular, the gcc4 frontend does not + create bitcode by default: gcc4 produces native code. As the example below illustrates, + the '--emit-llvm' flag is needed to produce LLVM bitcode output. For makefiles and + configure scripts, the CFLAGS variable needs '--emit-llvm' to produce bitcode + output.
+First, create a simple C file, name it 'hello.c':
+ +
+ #include <stdio.h>
+
+ int main() {
+ printf("hello world\n");
+ return 0;
+ }
+ Next, compile the C file into a native executable:
+ +% llvm-gcc hello.c -o hello
Note that llvm-gcc works just like GCC by default. The standard -S and + -c arguments work as usual (producing a native .s or .o file, + respectively).
Next, compile the C file into a LLVM bitcode file:
+ +% llvm-gcc -O3 -emit-llvm hello.c -c -o hello.bc
The -emit-llvm option can be used with the -S or -c options to emit an + LLVM ".ll" or ".bc" file (respectively) for the code. This allows you + to use the standard LLVM tools on + the bitcode file.
+ +Unlike llvm-gcc3, llvm-gcc4 correctly responds to -O[0123] arguments. +
Run the program in both forms. To run the program, use:
+ +% ./hello
and
+ +% lli hello.bc
The second examples shows how to invoke the LLVM JIT, lli.
Use the llvm-dis utility to take a look at the LLVM assembly + code:
+ +llvm-dis < hello.bc | less+
Compile the program to native assembly using the LLC code + generator:
+ +% llc hello.bc -o hello.s
Assemble the native assembly language file into a program:
+ ++ Solaris: % /opt/SUNWspro/bin/cc -xarch=v9 hello.s -o hello.native + + Others: % gcc hello.s -o hello.native ++
Execute the native code program:
+ +% ./hello.native
Note that using llvm-gcc to compile directly to native code (i.e. when + the -emit-llvm option is not present) does steps 6/7/8 for you.
+If you are having problems building or using LLVM, or if you have any other + general questions about LLVM, please consult the Frequently + Asked Questions page.
+ +This document is just an introduction to how to use LLVM to do + some simple things... there are many more interesting and complicated things + that you can do that aren't documented here (but we'll gladly accept a patch + if you want to write something up!). For more information about LLVM, check + out:
+ + + +The Visual Studio port at this time is experimental. It is suitable for + use only if you are writing your own compiler front end or otherwise have a + need to dynamically generate machine code. The JIT and interpreter are + functional, but it is currently not possible to generate assembly code which + is then assembled into an executable. You can indirectly create executables + by using the C back end.
+ +To emphasize, there is no C/C++ front end currently available. + llvm-gcc is based on GCC, which cannot be bootstrapped using VC++. + Eventually there should be a llvm-gcc based on Cygwin or MinGW that + is usable. There is also the option of generating bitcode files on Unix and + copying them over to Windows. But be aware the odds of linking C++ code + compiled with llvm-gcc with code compiled with VC++ is essentially + zero.
+ +The LLVM test suite cannot be run on the Visual Studio port at this + time.
+ +Most of the tools build and work. llvm-db does not build at this + time. bugpoint does build, but does not work. + +
Additional information about the LLVM directory structure and tool chain + can be found on the main Getting Started + page.
+ +Here's the short story for getting up and running quickly with LLVM:
+ +It is strongly encouraged that you get the latest version from Subversion as + changes are continually making the VS support better.
+ +Before you begin to use the LLVM system, review the requirements given + below. This may save you some trouble by knowing ahead of time what hardware + and software you will need.
+ +Any system that can adequately run Visual Studio .NET 2003 is fine. The + LLVM source tree and object files, libraries and executables will consume + approximately 3GB.
+ +You will need Visual Studio .NET 2003. Earlier versions cannot open the + solution/project files. The VS 2005 beta can, but will migrate these files + to its own format in the process. While it should work with the VS 2005 + beta, there are no guarantees and there is no support for it at this time. + It has been reported that VC++ Express also works.
+ +If you plan to modify any .y or .l files, you will need to have bison + and/or flex installed where Visual Studio can find them. Otherwise, you do + not need them and the pre-generated files that come with the source tree + will be used.
+ ++ Do not install the LLVM directory tree into a path containing spaces (e.g. + C:\Documents and Settings\...) as the configure step will fail.
+ +The remainder of this guide is meant to get you up and running with + LLVM using Visual Studio and to give you some basic information about the LLVM + environment.
+ +Throughout this manual, the following names are used to denote paths + specific to the local system and working environment. These are not + environment variables you need to set but just strings used in the rest + of this document below. In any of the examples below, simply replace + each of these names with the appropriate pathname on your local system. + All these paths are absolute:
+ +This is the top level directory of the LLVM source tree.
This is the top level directory of the LLVM object tree (i.e. the + tree where object files and compiled programs will be placed. It is + fixed at SRC_ROOT/win32).
The object files are placed under OBJ_ROOT/Debug for debug builds + and OBJ_ROOT/Release for release (optimized) builds. These include + both executables and libararies that your application can link against.
+ +The files that configure would create when building on Unix are + created by the Configure project and placed in + OBJ_ROOT/llvm. You application must have OBJ_ROOT in its include + search path just before SRC_ROOT/include.
+ +First, create a simple C file, name it 'hello.c':
+ +
+ #include <stdio.h>
+ int main() {
+ printf("hello world\n");
+ return 0;
+ }
+ Next, compile the C file into a LLVM bitcode file:
+ ++ % llvm-gcc -c hello.c -emit-llvm -o hello.bc ++
This will create the result file hello.bc which is the LLVM + bitcode that corresponds the the compiled program and the library + facilities that it required. You can execute this file directly using + lli tool, compile it to native assembly with the llc, + optimize or analyze it further with the opt tool, etc.
+ +Note: while you cannot do this step on Windows, you can do it on a + Unix system and transfer hello.bc to Windows. Important: + transfer as a binary file!
Run the program using the just-in-time compiler:
+ ++ % lli hello.bc ++
Note: this will only work for trivial C programs. Non-trivial programs + (and any C++ program) will have dependencies on the GCC runtime that + won't be satisfied by the Microsoft runtime libraries.
Use the llvm-dis utility to take a look at the LLVM assembly + code:
+ ++ % llvm-dis < hello.bc | more ++
Compile the program to C using the LLC code generator:
+ ++ % llc -march=c hello.bc ++
Compile to binary using Microsoft C:
+ ++ % cl hello.cbe.c ++
Note: this will only work for trivial C programs. Non-trivial programs + (and any C++ program) will have dependencies on the GCC runtime that + won't be satisfied by the Microsoft runtime libraries.
Execute the native code program:
+ ++ % hello.cbe.exe ++
If you are having problems building or using LLVM, or if you have any other + general questions about LLVM, please consult the Frequently + Asked Questions page.
+ +This document is just an introduction to how to use LLVM to do + some simple things... there are many more interesting and complicated things + that you can do that aren't documented here (but we'll gladly accept a patch + if you want to write something up!). For more information about LLVM, check + out:
+ + + ++ This document collects information about successfully releasing LLVM to the + public. It is the release manager's guide to ensuring that a high quality + build of LLVM is released. +
+ ++ The following is the basic criteria for releasing LLVM: +
+ +Branch the Subversion HEAD using the following procedure:
+Verify that the current Subversion HEAD is in decent shape by examining nightly + tester results.
Request all developers to refrain from committing. Offenders get commit + rights taken away (temporarily).
Create the release branch for llvm, llvm-gcc4.0, + llvm-gcc4.2, and the test-suite. The + branch name will be release_XX, where XX is the major and + minor release numbers. These branches can be created without checking out + anything from subversion. +
+ ++ svn copy https://llvm.org/svn/llvm-project/llvm/trunk \ + https://llvm.org/svn/llvm-project/llvm/branches/release_XX + svn copy https://llvm.org/svn/llvm-project/llvm-gcc-4.0/trunk \ + https://llvm.org/svn/llvm-project/llvm-gcc-4.0/branches/release_XX + svn copy https://llvm.org/svn/llvm-project/llvm-gcc-4.2/trunk \ + https://llvm.org/svn/llvm-project/llvm-gcc-4.2/branches/release_XX + svn copy https://llvm.org/svn/llvm-project/test-suite/trunk \ + https://llvm.org/svn/llvm-project/test-suite/branches/release_XX ++
Advise developers they can work on Subversion HEAD again.
The Release Manager should switch to the release branch (as all changes + to the release will now be done in the branch). The easiest way to do this + is to grab another working copy using the following commands:
+ ++ svn co https://llvm.org/svn/llvm-project/llvm/branches/release_XX + svn co https://llvm.org/svn/llvm-project/llvm-gcc-4.0/branches/release_XX + svn co https://llvm.org/svn/llvm-project/llvm-gcc-4.2/branches/release_XX + svn co https://llvm.org/svn/llvm-project/test-suite/branches/release_XX ++
+ After creating the LLVM release branch, update the release branches' + autoconf/configure.ac version from X.Xsvn to just X.X. Update it on mainline + as well to be the next version (X.X+1svn). Regenerated the configure script + for both. This must be done for both llvm and the test-suite. +
+In addition, the version number of all the Bugzilla components must be + updated for the next release. +
++ Create source distributions for LLVM, LLVM GCC, and the LLVM Test Suite by + exporting the source from Subversion and archiving it. This can be done with + the following commands: +
+ ++ svn export https://llvm.org/svn/llvm-project/llvm/branches/release_XX llvm-X.X + svn export https://llvm.org/svn/llvm-project/llvm-gcc-4.0/branches/release_XX llvm-gcc4.0-X.X.source + svn export https://llvm.org/svn/llvm-project/llvm-gcc-4.2/branches/release_XX llvm-gcc4.2-X.X.source + svn export https://llvm.org/svn/llvm-project/test-suite/branches/release_XX llvm-test-X.X + tar -cvf - llvm-X.X | gzip > llvm-X.X.tar.gz + tar -cvf - llvm-test-X.X | gzip > llvm-test-X.X.tar.gz + tar -cvf - llvm-gcc4.0-X.X.source | gzip > llvm-gcc-4.0-X.X.source.tar.gz + tar -cvf - llvm-gcc4.2-X.X.source | gzip > llvm-gcc-4.2-X.X.source.tar.gz ++
+ Build both debug and release (optimized) versions of LLVM on all + platforms. Ensure the build is warning and error free on each platform. + Note that when building the LLVM GCC Binary, use a release build of LLVM. +
++ Creating the LLVM GCC binary distribution (release/optimized) requires + performing the following steps for each supported platform: +
+ ++ Using the newly built llvm-gcc and llvm, reconfigure llvm to locate llvm-gcc. + Run make check and ensure there are no unexpected failures. If there + are, resolve the failures or file a bug. If there is a fix commited to mainline, + merge back into the release branch, and restart testing by + re-building LLVM and llvm-gcc. If no + fix will be made, XFAIL the test and commit back to the release branch. +
+ ++ Ensure that 'make check' passes on all platforms for all targets. The + test suite must complete with "0 unexpected failures" before sending out the + pre-releases for testing. +
++ Run the llvm-test suite and ensure there are no unacceptable + failures. Unacceptable failures are regression from the previous release + and (optionally) major performance regressions from the previous release. + If a regression is found a bug is filled, but the pre-releases may still go + out.
++ You can, optionally, create source and binary RPM packages for LLVM. These may + make it easier to get LLVM into a distribution. This can be done with the + following commands: +
+ ++ make dist # Build the distribution source tarball + make dist-check # Check that the source tarball can build itself. + cp llvm-M.m.tar.gz /usr/src/redhat/SOURCES # Required by rpmbuild + make srpm # for source rpm + make rpm # for binary rpm ++
+ First, use make dist to simply build the distribution. Any failures + need to be corrected (on the branch). Once make dist can be + successful, do make dist-check. This target will do the same thing as + the 'dist' target but also test that distribution to make sure it can build + itself and runs make check as well. This ensures that needed files + are not missing and that the src tarball can be successfully unpacked, built, + installed, and cleaned. Once you have a reliable tarball, you need to copy it + to the /usr/src/redhat/SOURCES directory which is a requirement of + the rpmbuild tool. The last two make invocations just run rpmbuild to + build either a source (srpm) or binary (rpm) RPM package. +
++ Once all testing has been completed and appropriate bugs filed, the pre-release + tar balls may be put on the website and the LLVM community is notified. Ask that + all LLVM developers test the release in 2 ways:
+Ask LLVM developers to submit the report and make check results to the list. + Verify that there are no regressions from the previous release. For + unsupported targets, verify that make check at least is clean.
+ +The first round of pre-release testing will be the longest. During this time, + all regressions must be fixed before the second pre-release is created (repeat + steps 4-8).
+ +If this is the second round of testing, this is only to ensure the bug fixes + previously merged in have not created new major problems. This is not the time + to solve additional and unrelated bugs. If no patches are merged in, the release + is determined to be ready and the release manager may move onto the next step.
+Tag the release branch using the following procedure:
++ svn copy https://llvm.org/svn/llvm-project/llvm/branches/release_XX \ + https://llvm.org/svn/llvm-project/llvm/tags/RELEASE_XX + svn copy https://llvm.org/svn/llvm-project/llvm-gcc-4.0/branches/release_XX \ + https://llvm.org/svn/llvm-project/llvm-gcc-4.0/tags/RELEASE_XX + svn copy https://llvm.org/svn/llvm-project/llvm-gcc-4.2/branches/release_XX \ + https://llvm.org/svn/llvm-project/llvm-gcc-4.2/tags/RELEASE_XX + svn copy https://llvm.org/svn/llvm-project/test-suite/branches/release_XX \ + https://llvm.org/svn/llvm-project/test-suite/tags/RELEASE_XX ++
+ Review the documentation and ensure that it is up to date. The Release Notes + must be updated to reflect bug fixes, new known issues, and changes in the + list of supported platforms. The Getting Started Guide should be updated to + reflect the new release version number tag avaiable from Subversion and + changes in basic system requirements. Merge both changes from mainline into + the release branch. +
++ The LLVM demo page must be updated to use the new release. This consists of + using the llvm-gcc binary and building LLVM. Update the website demo page + configuration to use the new release.
++ The website must be updated before the release announcement is sent out. Here is + what to do:
+Have Chris send out the release announcement when everything is finished.
++ The first thing you need to understand is that there are multiple make targets + to support this feature. Here's an overview, we'll delve into the details + later. +
+ ++ Okay, that's the basic functionality. When making a release, we want to ensure + that the tree you build the distribution from passes + dist-check. Beyond fixing the usual bugs, there is generally one + impediment to making the release in this fashion: missing files. The + dist-check process guards against that possibility. It will either + fail and that failure will indicate what's missing, or it will succeed meaning + that it has proved that the tarballs can actually succeed in building LLVM + correctly and that it passes make check. +
++ This target builds the distribution directory which is the directory from + which the tarballs are generated. The distribution directory has the same + name as the release, e.g. LLVM-1.7). This target goes through the following + process: +
+ ++ To control the process of making the distribution directory correctly, each + Makefile can utilize two features: +
+ ++ You will see various messages if things go wrong: +
+ ++ This target does exactly what distdir target does, but also includes + assembling the tarballs. There are actually four related targets here: +
+ ++ This target checks the distribution. The basic idea is that it unpacks the + distribution tarball and ensures that it can build. It takes the following + actions: +
+ ++ If it can pass all that, the distribution will be deemed distribution worth y + and you will see: +
+ +===== LLVM-1.7.tar.gz Ready For Distribution =====+ +
+ This means the tarball should then be tested on other platforms and have the + nightly test run against it. If those all pass, THEN it is ready for + distribution. +
+ ++ A note about disk space: using dist-check will easily triple the + amount of disk space your build tree is using. You might want to check + available space before you begin. +
++ In addition to doing a normal clean, this target will clean up the + files and directories created by the distribution targets. In particular the + distribution directory (LLVM-X.X), check directory + (_distcheckdir), and the various tarballs will be removed. You do + this after the release has shipped and you no longer need this stuff in your + build tree. +
+| + + + | +
+
+ |
+
If you're working with LLVM and run into a bug, we definitely want to know + about it. This document describes what you can do to increase the odds of + getting it fixed quickly.
+ +Basically you have to do two things at a minimum. First, decide whether the + bug crashes the compiler (or an LLVM pass), or if the + compiler is miscompiling the program (i.e., the + compiler successfully produces an executable, but it doesn't run right). Based + on + what type of bug it is, follow the instructions in the linked section to narrow + down the bug so that the person who fixes it will be able to find the problem + more easily.
+ +Once you have a reduced test-case, go to the LLVM Bug Tracking + System and fill out the form with the necessary details (note that you don't + need to pick a catagory, just use the "new-bugs" catagory if you're not sure). + The bug description should contain the following + information:
+ +Thanks for helping us make LLVM better!
+ +More often than not, bugs in the compiler cause it to crash—often due + to an assertion failure of some sort. The most important + piece of the puzzle is to figure out if it is crashing in the GCC front-end + or if it is one of the LLVM libraries (e.g. the optimizer or code generator) + that has problems.
+ +To figure out which component is crashing (the front-end, + optimizer or code generator), run the + llvm-gcc command line as you were when the crash occurred, but + with the following extra command line options:
+ +If the problem is in the front-end, you should re-run the same + llvm-gcc command that resulted in the crash, but add the + -save-temps option. The compiler will crash again, but it will leave + behind a foo.i file (containing preprocessed C source code) and + possibly foo.s for each + compiled foo.c file. Send us the foo.i file, + along with the options you passed to llvm-gcc, and a brief description of the + error it caused.
+ +The delta tool helps to reduce the + preprocessed file down to the smallest amount of code that still replicates the + problem. You're encouraged to use delta to reduce the code to make the + developers' lives easier. This website + has instructions on the best way to use delta.
+ +If you find that a bug crashes in the optimizer, compile your test-case to a + .bc file by passing "-emit-llvm -O0 -c -o foo.bc". + Then run:
+ +opt -std-compile-opts -debug-pass=Arguments foo.bc + -disable-output
+This command should do two things: it should print out a list of passes, and + then it should crash in the same was as llvm-gcc. If it doesn't crash, please + follow the instructions for a front-end bug.
+ +If this does crash, then you should be able to debug this with the following + bugpoint command:
+ +bugpoint foo.bc <list of passes printed by + opt>
+Please run this, then file a bug with the instructions and reduced .bc files + that bugpoint emits. If something goes wrong with bugpoint, please submit the + "foo.bc" file and the list of passes printed by opt.
+ +If you find a bug that crashes llvm-gcc in the code generator, compile your + source file to a .bc file by passing "-emit-llvm -c -o foo.bc" + to llvm-gcc (in addition to the options you already pass). Once your have + foo.bc, one of the following commands should fail:
+ +If none of these crash, please follow the instructions for a + front-end bug. If one of these do crash, you should + be able to reduce this with one of the following bugpoint command lines (use + the one corresponding to the command above that failed):
+ +Please run this, then file a bug with the instructions and reduced .bc file + that bugpoint emits. If something goes wrong with bugpoint, please submit the + "foo.bc" file and the option that llc crashes with.
+ +If llvm-gcc successfully produces an executable, but that executable doesn't + run right, this is either a bug in the code or a bug in the + compiler. The first thing to check is to make sure it is not using undefined + behavior (e.g. reading a variable before it is defined). In particular, check + to see if the program valgrinds clean, + passes purify, or some other memory checker tool. Many of the "LLVM bugs" that + we have chased down ended up being bugs in the program being compiled, not + LLVM.
+ +Once you determine that the program itself is not buggy, you should choose + which code generator you wish to compile the program with (e.g. C backend, the + JIT, or LLC) and optionally a series of LLVM passes to run. For example:
+ ++ bugpoint -run-cbe [... optzn passes ...] file-to-test.bc --args -- [program arguments]
+bugpoint will try to narrow down your list of passes to the one pass + that causes an error, and simplify the bitcode file as much as it can to assist + you. It will print a message letting you know how to reproduce the resulting + error.
+ +Similarly to debugging incorrect compilation by mis-behaving passes, you can + debug incorrect code generation by either LLC or the JIT, using + bugpoint. The process bugpoint follows in this case is to try + to narrow the code down to a function that is miscompiled by one or the other + method, but since for correctness, the entire program must be run, + bugpoint will compile the code it deems to not be affected with the C + Backend, and then link in the shared object it generates.
+ +To debug the JIT:
+ ++ bugpoint -run-jit -output=[correct output file] [bitcode file] \ + --tool-args -- [arguments to pass to lli] \ + --args -- [program arguments] ++
Similarly, to debug the LLC, one would run:
+ ++ bugpoint -run-llc -output=[correct output file] [bitcode file] \ + --tool-args -- [arguments to pass to llc] \ + --args -- [program arguments] ++
Special note: if you are debugging MultiSource or SPEC tests that + already exist in the llvm/test hierarchy, there is an easier way to + debug the JIT, LLC, and CBE, using the pre-written Makefile targets, which + will pass the program options specified in the Makefiles:
+ +
+ cd llvm/test/../../program
+ make bugpoint-jit
+
At the end of a successful bugpoint run, you will be presented + with two bitcode files: a safe file which can be compiled with the C + backend and the test file which either LLC or the JIT + mis-codegenerates, and thus causes the error.
+ +To reproduce the error that bugpoint found, it is sufficient to do + the following:
+ +Regenerate the shared object from the safe bitcode file:
+ +
+ llc -march=c safe.bc -o safe.c
+ gcc -shared safe.c -o safe.so
+
If debugging LLC, compile test bitcode native and link with the shared + object:
+ +
+ llc test.bc -o test.s -f
+ gcc test.s safe.so -o test.llc
+ ./test.llc [program options]
+
If debugging the JIT, load the shared object and supply the test + bitcode:
+ +lli -load=safe.so test.bc [program options]
+This document is a reference manual for the LLVM assembly language. + LLVM is an SSA based representation that provides type safety, + low-level operations, flexibility, and the capability of representing + 'all' high-level languages cleanly. It is the common code + representation used throughout all phases of the LLVM compilation + strategy.
+The LLVM code representation is designed to be used in three + different forms: as an in-memory compiler IR, as an on-disk bitcode + representation (suitable for fast loading by a Just-In-Time compiler), + and as a human readable assembly language representation. This allows + LLVM to provide a powerful intermediate representation for efficient + compiler transformations and analysis, while providing a natural means + to debug and visualize the transformations. The three different forms + of LLVM are all equivalent. This document describes the human readable + representation and notation.
+ +The LLVM representation aims to be light-weight and low-level + while being expressive, typed, and extensible at the same time. It + aims to be a "universal IR" of sorts, by being at a low enough level + that high-level ideas may be cleanly mapped to it (similar to how + microprocessors are "universal IR's", allowing many source languages to + be mapped to them). By providing type information, LLVM can be used as + the target of optimizations: for example, through pointer analysis, it + can be proven that a C automatic variable is never accessed outside of + the current function... allowing it to be promoted to a simple SSA + value instead of a memory location.
+ +It is important to note that this document describes 'well formed' + LLVM assembly language. There is a difference between what the parser + accepts and what is considered 'well formed'. For example, the + following instruction is syntactically okay, but not well formed:
+ ++ %x = add i32 1, %x ++
...because the definition of %x does not dominate all of + its uses. The LLVM infrastructure provides a verification pass that may + be used to verify that an LLVM module is well formed. This pass is + automatically run by the parser after parsing input assembly and by + the optimizer before it outputs bitcode. The violations pointed out + by the verifier pass indicate bugs in transformation passes or input to + the parser.
+LLVM identifiers come in two basic types: global and local. Global + identifiers (functions, global variables) begin with the @ character. Local + identifiers (register names, types) begin with the % character. Additionally, + there are three different formats for identifiers, for different purposes: + +
LLVM requires that values start with a prefix for two reasons: Compilers + don't need to worry about name clashes with reserved words, and the set of + reserved words may be expanded in the future without penalty. Additionally, + unnamed identifiers allow a compiler to quickly come up with a temporary + variable without having to avoid symbol table conflicts.
+ +Reserved words in LLVM are very similar to reserved words in other + languages. There are keywords for different opcodes + ('add', + 'bitcast', + 'ret', etc...), for primitive type names ('void', 'i32', etc...), + and others. These reserved words cannot conflict with variable names, because + none of them start with a prefix character ('%' or '@').
+ +Here is an example of LLVM code to multiply the integer variable + '%X' by 8:
+ +The easy way:
+ ++ %result = mul i32 %X, 8 ++
After strength reduction:
+ ++ %result = shl i32 %X, i8 3 ++
And the hard way:
+ ++ 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:
+ +...and it also shows a convention that we follow in this document. When + demonstrating instructions, we will follow an instruction with a comment that + defines the type and name of value produced. Comments are shown in italic + text.
+ +LLVM programs are composed of "Module"s, each of which is a + translation unit of the input programs. Each module consists of + functions, global variables, and symbol table entries. Modules may be + 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]* + + ; External declaration of the puts function + declare i32 @puts(i8 *) ; i32(i8 *)* + + ; Definition of main function + define i32 @main() { ; i32()* + ; Convert [13x i8 ]* to i8 *... + %cast210 = getelementptr [13 x i8 ]* @.LC0, i64 0, i64 0 ; i8 * + + ; Call puts function to write out the string to stdout... + call i32 @puts(i8 * %cast210) ; i32 + ret i32 0+
}
+
This example is made up of a global variable + named ".LC0", an external declaration of the "puts" + function, and a function definition + for "main".
+ +In general, a module is made up of a list of global values, + where both functions and global variables are global values. Global values are + represented by a pointer to a memory location (in this case, a pointer to an + array of char, and a pointer to a function), and have one of the following linkage types.
+ ++ All Global Variables and Functions have one of the following types of linkage: +
+ ++ The next two types of linkage are targeted for Microsoft Windows platform + only. They are designed to support importing (exporting) symbols from (to) + DLLs. +
+ +_imp__ and the function or variable name.
+ _imp__ and the function or variable
+ name.
+ For example, since the ".LC0" + variable is defined to be internal, if another module defined a ".LC0" + variable and was linked with this one, one of the two would be renamed, + preventing a collision. Since "main" and "puts" are + external (i.e., lacking any linkage declarations), they are accessible + outside of the current module.
+It is illegal for a function declaration + to have any linkage type other than "externally visible", dllimport, + or extern_weak.
+Aliases can have only external, internal and weak + linkages. +
LLVM functions, calls + and invokes can all have an optional calling convention + specified for the call. The calling convention of any pair of dynamic + caller/callee must match, or the behavior of the program is undefined. The + following calling conventions are supported by LLVM, and more may be added in + the future:
+ +More calling conventions can be added/defined on an as-needed basis, to + support pascal conventions or any other well-known target-independent + convention.
+ ++ All Global Variables and Functions have one of the following visibility styles: +
+ +Global variables define regions of memory allocated at compilation time + instead of run-time. Global variables may optionally be initialized, may have + an explicit section to be placed in, and may have an optional explicit alignment + specified. A variable may be defined as "thread_local", which means that it + will not be shared by threads (each thread will have a separated copy of the + variable). A variable may be defined as a global "constant," which indicates + that the contents of the variable will never be modified (enabling better + optimization, allowing the global data to be placed in the read-only section of + an executable, etc). Note that variables that need runtime initialization + cannot be marked "constant" as there is a store to the variable.
+ ++ LLVM explicitly allows declarations of global variables to be marked + constant, even if the final definition of the global is not. This capability + can be used to enable slightly better optimization of the program, but requires + the language definition to guarantee that optimizations based on the + 'constantness' are valid for the translation units that do not include the + definition. +
+ +As SSA values, global variables define pointer values that are in + scope (i.e. they dominate) all basic blocks in the program. Global + variables always define a pointer to their "content" type because they + describe a region of memory, and all memory objects in LLVM are + accessed through pointers.
+ +A global variable may be declared to reside in a target-specifc numbered + address space. For targets that support them, address spaces may affect how + optimizations are performed and/or what target instructions are used to access + the variable. The default address space is zero. The address space qualifier + must precede any other attributes.
+ +LLVM allows an explicit section to be specified for globals. If the target + supports it, it will emit globals to the section specified.
+ +An explicit alignment may be specified for a global. If not present, or if + the alignment is set to zero, the alignment of the global is set by the target + to whatever it feels convenient. If an explicit alignment is specified, the + global is forced to have at least that much alignment. All alignments must be + a power of 2.
+ +For example, the following defines a global in a numbered address space with + an initializer, section, and alignment:
+ ++ @G = constant float 1.0 addrspace(5), section "foo", align 4 ++
LLVM function definitions consist of the "define" keyord, + an optional linkage type, an optional + visibility style, an optional + calling convention, a return type, an optional + parameter attribute for the return type, a function + name, a (possibly empty) argument list (each with optional + parameter attributes), an optional section, an + optional alignment, an optional garbage collector name, an + opening curly brace, a list of basic blocks, and a closing curly brace. + + LLVM function declarations consist of the "declare" keyword, an + optional linkage type, an optional + visibility style, an optional + calling convention, a return type, an optional + parameter attribute for the return type, a function + name, a possibly empty list of arguments, an optional alignment, and an optional + garbage collector name.
+ +A function definition contains a list of basic blocks, forming the CFG for + the function. Each basic block may optionally start with a label (giving the + basic block a symbol table entry), contains a list of instructions, and ends + with a terminator instruction (such as a branch or + function return).
+ +The first basic block in a function is special in two ways: it is immediately + executed on entrance to the function, and it is not allowed to have predecessor + basic blocks (i.e. there can not be any branches to the entry block of a + function). Because the block can have no predecessors, it also cannot have any + PHI nodes.
+ +LLVM allows an explicit section to be specified for functions. If the target + supports it, it will emit functions to the section specified.
+ +An explicit alignment may be specified for a function. If not present, or if + the alignment is set to zero, the alignment of the function is set by the target + to whatever it feels convenient. If an explicit alignment is specified, the + function is forced to have at least that much alignment. All alignments must be + a power of 2.
+ +Aliases act as "second name" for the aliasee value (which can be either + function, global variable, another alias or bitcast of global value). Aliases + may have an optional linkage type, and an + optional visibility style.
+ ++ @<Name> = [Linkage] [Visibility] alias <AliaseeTy> @<Aliasee> ++
The return type and each parameter of a function type may have a set of + parameter attributes associated with them. Parameter attributes are + used to communicate additional information about the result or parameters of + a function. Parameter attributes are considered to be part of the function, + not of the function type, so functions with different parameter attributes + can have the same function type.
+ +Parameter attributes are simple keywords that follow the type specified. If + multiple parameter attributes are needed, they are space separated. For + example:
+ ++ declare i32 @printf(i8* noalias , ...) nounwind + declare i32 @atoi(i8*) nounwind readonly ++
Note that any attributes for the function result (nounwind, + readonly) come immediately after the argument list.
+ +Currently, only the following parameter attributes are defined:
+Each function may specify a garbage collector name, which is simply a + string.
+ +define void @f() gc "name" { ...The compiler declares the supported values of name. Specifying a + collector which will cause the compiler to alter its output in order to support + the named garbage collection algorithm.
++ Modules may contain "module-level inline asm" blocks, which corresponds to the + GCC "file scope inline asm" blocks. These blocks are internally concatenated by + 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" ++
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 + for the number. +
+ ++ The inline asm code is simply printed to the machine code .s file when + assembly code is generated. +
+A module may specify a target specific data layout string that specifies how + data is to be laid out in memory. The syntax for the data layout is simply:
+target datalayout = "layout specification"+
The layout specification consists of a list of specifications + separated by the minus sign character ('-'). Each specification starts with a + letter and may include other information after the letter to define some + aspect of the data layout. The specifications accepted are as follows:
+When constructing the data layout for a given target, LLVM starts with a + default set of specifications which are then (possibly) overriden by the + specifications in the datalayout keyword. The default specifications + are given in this list:
+When llvm is determining the alignment for a given type, it uses the + following rules: +
The LLVM type system is one of the most important features of the + intermediate representation. Being typed enables a number of + optimizations to be performed on the IR directly, without having to do + extra analyses on the side before the transformation. A strong type + system makes it easier to read the generated code and enables novel + analyses and transformations that are not feasible to perform on normal + three address code representations.
+ +The types fall into a few useful + classifications:
+ +| Classification | Types |
|---|---|
| integer | +i1, i2, i3, ... i8, ... i16, ... i32, ... i64, ... | +
| floating point | +float, double, x86_fp80, fp128, ppc_fp128 | +
| first class | +integer, + floating point, + pointer, + vector + | +
| primitive | +label, + void, + integer, + floating point. | +
| derived | +integer, + array, + function, + pointer, + structure, + packed structure, + vector, + opaque. + |
The first class types are perhaps the + most important. Values of these types are the only ones which can be + produced by instructions, passed as arguments, or used as operands to + instructions. This means that all structures and arrays must be + manipulated either by pointer or by component.
+The primitive types are the fundamental building blocks of the LLVM + system.
+ +| Type | Description |
|---|---|
| float | 32-bit floating point value |
| double | 64-bit floating point value |
| fp128 | 128-bit floating point value (112-bit mantissa) |
| x86_fp80 | 80-bit floating point value (X87) |
| ppc_fp128 | 128-bit floating point value (two 64-bits) |
The void type does not represent any value and has no size.
+ ++ void ++
The label type represents code labels.
+ ++ label ++
The real power in LLVM comes from the derived types in the system. + This is what allows a programmer to represent arrays, functions, + pointers, and other useful types. Note that these derived types may be + recursive: For example, it is possible to have a two dimensional array.
+ +The integer type is a very simple derived type that simply specifies an + arbitrary bit width for the integer type desired. Any bit width from 1 bit to + 2^23-1 (about 8 million) can be specified.
+ ++ iN ++ +
The number of bits the integer will occupy is specified by the N + value.
+ +| i1 | +a single-bit integer. | +
| i32 | +a 32-bit integer. | +
| i1942652 | +a really big integer of over 1 million bits. | +
The array type is a very simple derived type that arranges elements + sequentially in memory. The array type requires a size (number of + elements) and an underlying data type.
+ ++ [<# elements> x <elementtype>] ++ +
The number of elements is a constant integer value; elementtype may + be any type with a size.
+ +| [40 x i32] | +Array of 40 32-bit integer values. | +
| [41 x i32] | +Array of 41 32-bit integer values. | +
| [4 x i8] | +Array of 4 8-bit integer values. | +
Here are some examples of multidimensional arrays:
+| [3 x [4 x i32]] | +3x4 array of 32-bit integer values. | +
| [12 x [10 x float]] | +12x10 array of single precision floating point values. | +
| [2 x [3 x [4 x i16]]] | +2x3x4 array of 16-bit integer values. | +
Note that 'variable sized arrays' can be implemented in LLVM with a zero + length array. Normally, accesses past the end of an array are undefined in + LLVM (e.g. it is illegal to access the 5th element of a 3 element array). + As a special case, however, zero length arrays are recognized to be variable + length. This allows implementation of 'pascal style arrays' with the LLVM + type "{ i32, [0 x float]}", for example.
+ +The function type can be thought of as a function signature. It + consists of a return type and a list of formal parameter types. The + return type of a function type is a scalar type, a void type, or a struct type. + If the return type is a struct type then all struct elements must be of first + class types, and the struct must have at least one element.
+ ++ <returntype list> (<parameter list>) ++ +
...where '<parameter list>' is a comma-separated list of type + specifiers. Optionally, the parameter list may include a type ..., + which indicates that the function takes a variable number of arguments. + Variable argument functions can access their arguments with the variable argument handling intrinsic functions. + '<returntype list>' is a comma-separated list of + first class type specifiers.
+ +| i32 (i32) | +function taking an i32, returning an i32 + | +
| float (i16 signext, i32 *) * + | +Pointer to a function that takes + an i16 that should be sign extended and a + pointer to i32, returning + float. + | +
| i32 (i8*, ...) | +A vararg function that takes at least one + pointer to i8 (char in C), + which returns an integer. This is the signature for printf in + LLVM. + | +
| {i32, i32} (i32) | +A function taking an i32>, returning two + i32 values as an aggregate of type { i32, i32 } + | +
The structure type is used to represent a collection of data members + together in memory. The packing of the field types is defined to match + the ABI of the underlying processor. The elements of a structure may + be any type that has a size.
+Structures are accessed using 'load + and 'store' by getting a pointer to a + field with the 'getelementptr' + instruction.
+ { <type list> }
+ | { i32, i32, i32 } | +A triple of three i32 values | +
| { float, i32 (i32) * } | +A pair, where the first element is a float and the + second element is a pointer to a + function that takes an i32, returning + an i32. | +
The packed structure type is used to represent a collection of data members + together in memory. There is no padding between fields. Further, the alignment + of a packed structure is 1 byte. The elements of a packed structure may + be any type that has a size.
+Structures are accessed using 'load + and 'store' by getting a pointer to a + field with the 'getelementptr' + instruction.
+ < { <type list> } >
+ | < { i32, i32, i32 } > | +A triple of three i32 values | +
| < { float, i32 (i32)* } > | +A pair, where the first element is a float and the + second element is a pointer to a + function that takes an i32, returning + an i32. | +
As in many languages, the pointer type represents a pointer or + reference to another object, which must live in memory. Pointer types may have + an optional address space attribute defining the target-specific numbered + address space where the pointed-to object resides. The default address space is + zero.
+<type> *+
| [4x i32]* | +A pointer to array of four i32 values. | +
| i32 (i32 *) * | +A pointer to a function that takes an i32*, returning an + i32. | +
| i32 addrspace(5)* | +A pointer to an i32 value + that resides in address space #5. | +
A vector type is a simple derived type that represents a vector + of elements. Vector types are used when multiple primitive data + are operated in parallel using a single instruction (SIMD). + A vector type requires a size (number of + elements) and an underlying primitive data type. Vectors must have a power + of two length (1, 2, 4, 8, 16 ...). Vector types are + considered first class.
+ ++ < <# elements> x <elementtype> > ++ +
The number of elements is a constant integer value; elementtype may + be any integer or floating point type.
+ +| <4 x i32> | +Vector of 4 32-bit integer values. | +
| <8 x float> | +Vector of 8 32-bit floating-point values. | +
| <2 x i64> | +Vector of 2 64-bit integer values. | +
Opaque types are used to represent unknown types in the system. This + corresponds (for example) to the C notion of a forward declared structure type. + In LLVM, opaque types can eventually be resolved to any type (not just a + structure type).
+ ++ opaque ++ +
| opaque | +An opaque type. | +
LLVM has several different basic types of constants. This section describes + them all and their syntax.
+ +The one non-intuitive notation for constants is the optional hexadecimal form + of floating point constants. For example, the form 'double + 0x432ff973cafa8000' is equivalent to (but harder to read than) 'double + 4.5e+15'. The only time hexadecimal floating point constants are required + (and the only time that they are generated by the disassembler) is when a + floating point constant must be emitted but it cannot be represented as a + decimal floating point number. For example, NaN's, infinities, and other + special values are represented in their IEEE hexadecimal format so that + assembly and disassembly do not cause any bits to change in the constants.
+ +Aggregate constants arise from aggregation of simple constants + and smaller aggregate constants.
+ +The addresses of global variables and functions are always implicitly valid (link-time) + constants. These constants are explicitly referenced when the 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 ] ++
The string 'undef' is recognized as a type-less constant that has + no specific value. Undefined values may be of any type and be used anywhere + a constant is permitted.
+ +Undefined values indicate to the compiler that the program is well defined + no matter what value is used, giving the compiler more freedom to optimize. +
+Constant expressions are used to allow expressions involving other constants + to be used as constants. Constant expressions may be of any first class type and may involve any LLVM operation + that does not have side effects (e.g. load and call are not supported). The + following is the syntax for constant expressions:
+ ++ LLVM supports inline assembler expressions (as opposed to + Module-Level Inline Assembly) through the use of a special value. This + value represents the inline assembler as a string (containing the instructions + to emit), a list of operand constraints (stored as a string), and a flag that + indicates whether or not the inline asm expression has side effects. An example + inline assembler expression is: +
+ ++ 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: +
+ ++ %X = call i32 asm "bswap $0", "=r,r"(i32 %Y) ++
+ Inline asms with side effects not visible in the constraint list must be marked + as having side effects. This is done through the use of the + 'sideeffect' keyword, like so: +
+ ++ 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 + need to be documented). +
+ +The LLVM instruction set consists of several different + classifications of instructions: terminator + instructions, binary instructions, + bitwise binary instructions, memory instructions, and other + instructions.
+ +As mentioned previously, every + basic block in a program ends with a "Terminator" instruction, which + indicates which block should be executed after the current block is + finished. These terminator instructions typically yield a 'void' + value: they produce control flow, not values (the one exception being + the 'invoke' instruction).
+There are six different terminator instructions: the 'ret' instruction, the 'br' + instruction, the 'switch' instruction, + the 'invoke' instruction, the 'unwind' instruction, and the 'unreachable' instruction.
+ +ret <type> <value> ; Return a value from a non-void function + ret void ; Return from void function + ret <type> <value>, <type> <value> ; Return two values from a non-void function ++ +
The 'ret' instruction is used to return control flow (and a + value) from a function back to the caller.
+There are two forms of the 'ret' instruction: one that + returns value(s) and then causes control flow, and one that just causes + control flow to occur.
+ +The 'ret' instruction may return zero, one or multiple values. + The type of each return value must be a 'first + class' type. Note that a function is not well + formed if there exists a 'ret' instruction inside of the + function that returns values that do not match the return type of the + function.
+ +When the 'ret' instruction is executed, control flow + returns back to the calling function's context. If the caller is a "call" instruction, execution continues at + the instruction after the call. If the caller was an "invoke" instruction, execution continues + at the beginning of the "normal" destination block. If the instruction + returns a value, that value shall set the call or invoke instruction's + return value. If the instruction returns multiple values then these + values can only be accessed through a 'getresult + ' instruction.
+ ++ ret i32 5 ; Return an integer value of 5 + ret void ; Return from a void function + ret i32 4, i8 2 ; Return two values 4 and 2 ++
br i1 <cond>, label <iftrue>, label <iffalse>+
br label <dest> ; Unconditional branch +
The 'br' instruction is used to cause control flow to + transfer to a different basic block in the current function. There are + two forms of this instruction, corresponding to a conditional branch + and an unconditional branch.
+The conditional branch form of the 'br' instruction takes a + single 'i1' value and two 'label' values. The + unconditional form of the 'br' instruction takes a single + 'label' value as a target.
+Upon execution of a conditional 'br' instruction, the 'i1' + argument is evaluated. If the value is true, control flows + to the 'iftrue' label argument. If "cond" is false, + control flows to the 'iffalse' label argument.
+Test:+
%cond = icmp eq, i32 %a, %b
br i1 %cond, label %IfEqual, label %IfUnequal
IfEqual:
ret i32 1
IfUnequal:
ret i32 0
+ switch <intty> <value>, label <defaultdest> [ <intty> <val>, label <dest> ... ] ++ +
The 'switch' instruction is used to transfer control flow to one of + several different places. It is a generalization of the 'br' + instruction, allowing a branch to occur to one of many possible + destinations.
+ + +The 'switch' instruction uses three parameters: an integer + comparison value 'value', a default 'label' destination, and + an array of pairs of comparison value constants and 'label's. The + table is not allowed to contain duplicate constant entries.
+ +The switch instruction specifies a table of values and + destinations. When the 'switch' instruction is executed, this + table is searched for the given value. If the value is found, control flow is + transfered to the corresponding destination; otherwise, control flow is + transfered to the default destination.
+ +Depending on properties of the target machine and the particular + switch instruction, this instruction may be code generated in different + ways. For example, it could be generated as a series of chained conditional + branches or with a lookup table.
+ ++ ; Emulate a conditional br instruction + %Val = zext i1 %value to i32 + switch i32 %Val, label %truedest [i32 0, label %falsedest ] + + ; Emulate an unconditional br instruction + switch i32 0, label %dest [ ] + + ; Implement a jump table: + switch i32 %val, label %otherwise [ i32 0, label %onzero + i32 1, label %onone + i32 2, label %ontwo ] ++
+ <result> = invoke [cconv] <ptr to function ty> <function ptr val>(<function args>) + to label <normal label> unwind label <exception label> ++ +
The 'invoke' instruction causes control to transfer to a specified + function, with the possibility of control flow transfer to either the + 'normal' label or the + 'exception' label. If the callee function returns with the + "ret" instruction, control flow will return to the + "normal" label. If the callee (or any indirect callees) returns with the "unwind" instruction, control is interrupted and + continued at the dynamically nearest "exception" label. If the callee function + returns multiple values then individual return values are only accessible through + a 'getresult' instruction.
+ +This instruction requires several arguments:
+ +This instruction is designed to operate as a standard 'call' instruction in most regards. The primary + difference is that it establishes an association with a label, which is used by + the runtime library to unwind the stack.
+ +This instruction is used in languages with destructors to ensure that proper + cleanup is performed in the case of either a longjmp or a thrown + exception. Additionally, this is important for implementation of + 'catch' clauses in high-level languages that support them.
+ +
+ %retval = invoke i32 @Test(i32 15) to label %Continue
+ unwind label %TestCleanup ; {i32}:retval set
+ %retval = invoke coldcc i32 %Testfnptr(i32 15) to label %Continue
+ unwind label %TestCleanup ; {i32}:retval set
+
+ + unwind ++ +
The 'unwind' instruction unwinds the stack, continuing control flow + at the first callee in the dynamic call stack which used an invoke instruction to perform the call. This is + primarily used to implement exception handling.
+ +The 'unwind' instruction causes execution of the current function to + immediately halt. The dynamic call stack is then searched for the first invoke instruction on the call stack. Once found, + execution continues at the "exceptional" destination block specified by the + invoke instruction. If there is no invoke instruction in the + dynamic call chain, undefined behavior results.
++ unreachable ++ +
The 'unreachable' instruction has no defined semantics. This + instruction is used to inform the optimizer that a particular portion of the + code is not reachable. This can be used to indicate that the code after a + no-return function cannot be reached, and other facts.
+ +The 'unreachable' instruction has no defined semantics.
+Binary operators are used to do most of the computation in a + program. They require two operands of the same type, execute an operation on them, and + produce a single value. The operands might represent + multiple data, as is the case with the vector data type. + The result value has the same type as its operands.
+There are several different binary operators:
+ <result> = add <ty> <var1>, <var2> ; yields {ty}:result
+
+ The 'add' instruction returns the sum of its two operands.
+The two arguments to the 'add' instruction must be either integer or floating point values. + This instruction can also take vector versions of the values. + Both arguments must have identical types.
+The value produced is the integer or floating point sum of the two + operands.
+If an integer sum has unsigned overflow, the result returned is the + mathematical result modulo 2n, where n is the bit width of + the result.
+Because LLVM integers use a two's complement representation, this + instruction is appropriate for both signed and unsigned integers.
+ <result> = add i32 4, %var ; yields {i32}:result = 4 + %var
+
+ <result> = sub <ty> <var1>, <var2> ; yields {ty}:result
+
+ The 'sub' instruction returns the difference of its two + operands.
+Note that the 'sub' instruction is used to represent the 'neg' + instruction present in most other intermediate representations.
+The two arguments to the 'sub' instruction must be either integer or floating point + values. + This instruction can also take vector versions of the values. + Both arguments must have identical types.
+The value produced is the integer or floating point difference of + the two operands.
+If an integer difference has unsigned overflow, the result returned is the + mathematical result modulo 2n, where n is the bit width of + the result.
+Because LLVM integers use a two's complement representation, this + instruction is appropriate for both signed and unsigned integers.
+
+ <result> = sub i32 4, %var ; yields {i32}:result = 4 - %var
+ <result> = sub i32 0, %val ; yields {i32}:result = -%var
+
+ <result> = mul <ty> <var1>, <var2> ; yields {ty}:result
+
+ The 'mul' instruction returns the product of its two + operands.
+The two arguments to the 'mul' instruction must be either integer or floating point + values. + This instruction can also take vector versions of the values. + Both arguments must have identical types.
+The value produced is the integer or floating point product of the + two operands.
+If the result of an integer multiplication has unsigned overflow, + the result returned is the mathematical result modulo + 2n, where n is the bit width of the result.
+Because LLVM integers use a two's complement representation, and the + result is the same width as the operands, this instruction returns the + correct result for both signed and unsigned integers. If a full product + (e.g. i32xi32->i64) is needed, the operands + should be sign-extended or zero-extended as appropriate to the + width of the full product.
+ <result> = mul i32 4, %var ; yields {i32}:result = 4 * %var
+
+ <result> = udiv <ty> <var1>, <var2> ; yields {ty}:result
+
+ The 'udiv' instruction returns the quotient of its two + operands.
+The two arguments to the 'udiv' instruction must be + integer values. Both arguments must have identical + types. This instruction can also take vector versions + of the values in which case the elements must be integers.
+The value produced is the unsigned integer quotient of the two operands.
+Note that unsigned integer division and signed integer division are distinct + operations; for signed integer division, use 'sdiv'.
+Division by zero leads to undefined behavior.
+ <result> = udiv i32 4, %var ; yields {i32}:result = 4 / %var
+
+ <result> = sdiv <ty> <var1>, <var2> ; yields {ty}:result
+
+ The 'sdiv' instruction returns the quotient of its two + operands.
+The two arguments to the 'sdiv' instruction must be + integer values. Both arguments must have identical + types. This instruction can also take vector versions + of the values in which case the elements must be integers.
+The value produced is the signed integer quotient of the two operands rounded towards zero.
+Note that signed integer division and unsigned integer division are distinct + operations; for unsigned integer division, use 'udiv'.
+Division by zero leads to undefined behavior. Overflow also leads to + undefined behavior; this is a rare case, but can occur, for example, + by doing a 32-bit division of -2147483648 by -1.
+ <result> = sdiv i32 4, %var ; yields {i32}:result = 4 / %var
+
+ <result> = fdiv <ty> <var1>, <var2> ; yields {ty}:result
+
+ The 'fdiv' instruction returns the quotient of its two + operands.
+The two arguments to the 'fdiv' instruction must be + floating point values. Both arguments must have + identical types. This instruction can also take vector + versions of floating point values.
+The value produced is the floating point quotient of the two operands.
+ <result> = fdiv float 4.0, %var ; yields {float}:result = 4.0 / %var
+
+ <result> = urem <ty> <var1>, <var2> ; yields {ty}:result
+
+ The 'urem' instruction returns the remainder from the + unsigned division of its two arguments.
+The two arguments to the 'urem' instruction must be + integer values. Both arguments must have identical + types. This instruction can also take vector versions + of the values in which case the elements must be integers.
+This instruction returns the unsigned integer remainder of a division. + This instruction always performs an unsigned division to get the remainder.
+Note that unsigned integer remainder and signed integer remainder are + distinct operations; for signed integer remainder, use 'srem'.
+Taking the remainder of a division by zero leads to undefined behavior.
+ <result> = urem i32 4, %var ; yields {i32}:result = 4 % %var
+
+
+ <result> = srem <ty> <var1>, <var2> ; yields {ty}:result
+
+ The 'srem' instruction returns the remainder from the + signed division of its two operands. This instruction can also take + vector versions of the values in which case + the elements must be integers.
+ +The two arguments to the 'srem' instruction must be + integer values. Both arguments must have identical + types.
+This instruction returns the remainder of a division (where the result + has the same sign as the dividend, var1), not the modulo + operator (where the result has the same sign as the divisor, var2) of + a value. For more information about the difference, see The + Math Forum. For a table of how this is implemented in various languages, + please see + Wikipedia: modulo operation.
+Note that signed integer remainder and unsigned integer remainder are + distinct operations; for unsigned integer remainder, use 'urem'.
+Taking the remainder of a division by zero leads to undefined behavior. + Overflow also leads to undefined behavior; this is a rare case, but can occur, + for example, by taking the remainder of a 32-bit division of -2147483648 by -1. + (The remainder doesn't actually overflow, but this rule lets srem be + implemented using instructions that return both the result of the division + and the remainder.)
+ <result> = srem i32 4, %var ; yields {i32}:result = 4 % %var
+
+
+ <result> = frem <ty> <var1>, <var2> ; yields {ty}:result
+
+ The 'frem' instruction returns the remainder from the + division of its two operands.
+The two arguments to the 'frem' instruction must be + floating point values. Both arguments must have + identical types. This instruction can also take vector + versions of floating point values.
+This instruction returns the remainder of a division. + The remainder has the same sign as the dividend.
+ <result> = frem float 4.0, %var ; yields {float}:result = 4.0 % %var
+
+ Bitwise binary operators are used to do various forms of + bit-twiddling in a program. They are generally very efficient + instructions and can commonly be strength reduced from other + instructions. They require two operands of the same type, execute an operation on them, + and produce a single value. The resulting value is the same type as its operands.
+ <result> = shl <ty> <var1>, <var2> ; yields {ty}:result
+
+
+ The 'shl' instruction returns the first operand shifted to + the left a specified number of bits.
+ +Both arguments to the 'shl' instruction must be the same integer type. 'var2' is treated as an + unsigned value.
+ +The value produced is var1 * 2var2 mod 2n, + where n is the width of the result. If var2 is (statically or dynamically) negative or + equal to or larger than the number of bits in var1, the result is undefined.
+ +
+ <result> = shl i32 4, %var ; yields {i32}: 4 << %var
+ <result> = shl i32 4, 2 ; yields {i32}: 16
+ <result> = shl i32 1, 10 ; yields {i32}: 1024
+ <result> = shl i32 1, 32 ; undefined
+
+ <result> = lshr <ty> <var1>, <var2> ; yields {ty}:result
+
+
+ The 'lshr' instruction (logical shift right) returns the first + operand shifted to the right a specified number of bits with zero fill.
+ +Both arguments to the 'lshr' instruction must be the same + integer type. 'var2' is treated as an + unsigned value.
+ +This instruction always performs a logical shift right operation. The most + significant bits of the result will be filled with zero bits after the + shift. If var2 is (statically or dynamically) equal to or larger than + the number of bits in var1, the result is undefined.
+ +
+ <result> = lshr i32 4, 1 ; yields {i32}:result = 2
+ <result> = lshr i32 4, 2 ; yields {i32}:result = 1
+ <result> = lshr i8 4, 3 ; yields {i8}:result = 0
+ <result> = lshr i8 -2, 1 ; yields {i8}:result = 0x7FFFFFFF
+ <result> = lshr i32 1, 32 ; undefined
+
+ <result> = ashr <ty> <var1>, <var2> ; yields {ty}:result
+
+
+ The 'ashr' instruction (arithmetic shift right) returns the first + operand shifted to the right a specified number of bits with sign extension.
+ +Both arguments to the 'ashr' instruction must be the same + integer type. 'var2' is treated as an + unsigned value.
+ +This instruction always performs an arithmetic shift right operation, + The most significant bits of the result will be filled with the sign bit + of var1. If var2 is (statically or dynamically) equal to or + larger than the number of bits in var1, the result is undefined. +
+ +
+ <result> = ashr i32 4, 1 ; yields {i32}:result = 2
+ <result> = ashr i32 4, 2 ; yields {i32}:result = 1
+ <result> = ashr i8 4, 3 ; yields {i8}:result = 0
+ <result> = ashr i8 -2, 1 ; yields {i8}:result = -1
+ <result> = ashr i32 1, 32 ; undefined
+
+ <result> = and <ty> <var1>, <var2> ; yields {ty}:result
+
+ The 'and' instruction returns the bitwise logical and of + its two operands.
+The two arguments to the 'and' instruction must be integer values. Both arguments must have + identical types.
+The truth table used for the 'and' instruction is:
++
| In0 | +In1 | +Out | +
| 0 | +0 | +0 | +
| 0 | +1 | +0 | +
| 1 | +0 | +0 | +
| 1 | +1 | +1 | +
<result> = and i32 4, %var ; yields {i32}:result = 4 & %var
+ <result> = and i32 15, 40 ; yields {i32}:result = 8
+ <result> = and i32 4, 8 ; yields {i32}:result = 0
+
+ <result> = or <ty> <var1>, <var2> ; yields {ty}:result
+
+ The 'or' instruction returns the bitwise logical inclusive + or of its two operands.
+The two arguments to the 'or' instruction must be integer values. Both arguments must have + identical types.
+The truth table used for the 'or' instruction is:
++
| In0 | +In1 | +Out | +
| 0 | +0 | +0 | +
| 0 | +1 | +1 | +
| 1 | +0 | +1 | +
| 1 | +1 | +1 | +
<result> = or i32 4, %var ; yields {i32}:result = 4 | %var
+ <result> = or i32 15, 40 ; yields {i32}:result = 47
+ <result> = or i32 4, 8 ; yields {i32}:result = 12
+
+ <result> = xor <ty> <var1>, <var2> ; yields {ty}:result
+
+ The 'xor' instruction returns the bitwise logical exclusive + or of its two operands. The xor is used to implement the + "one's complement" operation, which is the "~" operator in C.
+The two arguments to the 'xor' instruction must be integer values. Both arguments must have + identical types.
+The truth table used for the 'xor' instruction is:
++
| In0 | +In1 | +Out | +
| 0 | +0 | +0 | +
| 0 | +1 | +1 | +
| 1 | +0 | +1 | +
| 1 | +1 | +0 | +
+
<result> = xor i32 4, %var ; yields {i32}:result = 4 ^ %var
+ <result> = xor i32 15, 40 ; yields {i32}:result = 39
+ <result> = xor i32 4, 8 ; yields {i32}:result = 12
+ <result> = xor i32 %V, -1 ; yields {i32}:result = ~%V
+
+ LLVM supports several instructions to represent vector operations in a + target-independent manner. These instructions cover the element-access and + vector-specific operations needed to process vectors effectively. While LLVM + does directly support these vector operations, many sophisticated algorithms + will want to use target-specific intrinsics to take full advantage of a specific + target.
+ ++ <result> = extractelement <n x <ty>> <val>, i32 <idx> ; yields <ty> ++ +
+ The 'extractelement' instruction extracts a single scalar + element from a vector at a specified index. +
+ + ++ The first operand of an 'extractelement' instruction is a + value of vector type. The second operand is + an index indicating the position from which to extract the element. + The index may be a variable.
+ ++ The result is a scalar of the same type as the element type of + val. Its value is the value at position idx of + val. If idx exceeds the length of val, the + results are undefined. +
+ ++ %result = extractelement <4 x i32> %vec, i32 0 ; yields i32 ++
+ <result> = insertelement <n x <ty>> <val>, <ty> <elt>, i32 <idx> ; yields <n x <ty>> ++ +
+ The 'insertelement' instruction inserts a scalar + element into a vector at a specified index. +
+ + ++ The first operand of an 'insertelement' instruction is a + value of vector type. The second operand is a + scalar value whose type must equal the element type of the first + operand. The third operand is an index indicating the position at + which to insert the value. The index may be a variable.
+ ++ The result is a vector of the same type as val. Its + element values are those of val except at position + idx, where it gets the value elt. If idx + exceeds the length of val, the results are undefined. +
+ ++ %result = insertelement <4 x i32> %vec, i32 1, i32 0 ; yields <4 x i32> ++
+ <result> = shufflevector <n x <ty>> <v1>, <n x <ty>> <v2>, <n x i32> <mask> ; yields <n x <ty>> ++ +
+ The 'shufflevector' instruction constructs a permutation of elements + from two input vectors, returning a vector of the same type. +
+ ++ The first two operands of a 'shufflevector' instruction are vectors + with types that match each other and types that match the result of the + instruction. The third argument is a shuffle mask, which has the same number + of elements as the other vector type, but whose element type is always 'i32'. +
+ ++ The shuffle mask operand is required to be a constant vector with either + constant integer or undef values. +
+ ++ The elements of the two input vectors are numbered from left to right across + both of the vectors. The shuffle mask operand specifies, for each element of + the result vector, which element of the two input registers the result element + gets. The element selector may be undef (meaning "don't care") and the second + operand may be undef if performing a shuffle from only one vector. +
+ ++ %result = shufflevector <4 x i32> %v1, <4 x i32> %v2, + <4 x i32> <i32 0, i32 4, i32 1, i32 5> ; yields <4 x i32> + %result = shufflevector <4 x i32> %v1, <4 x i32> undef, + <4 x i32> <i32 0, i32 1, i32 2, i32 3> ; yields <4 x i32> - Identity shuffle. ++
A key design point of an SSA-based representation is how it + represents memory. In LLVM, no memory locations are in SSA form, which + makes things very simple. This section describes how to read, write, + allocate, and free memory in LLVM.
+ +
+ <result> = malloc <type>[, i32 <NumElements>][, align <alignment>] ; yields {type*}:result
+
+
+ The 'malloc' instruction allocates memory from the system + heap and returns a pointer to it. The object is always allocated in the generic + address space (address space zero).
+ +The 'malloc' instruction allocates + sizeof(<type>)*NumElements + bytes of memory from the operating system and returns a pointer of the + appropriate type to the program. If "NumElements" is specified, it is the + number of elements allocated, otherwise "NumElements" is defaulted to be one. + If a constant alignment is specified, the value result of the allocation is guaranteed to + be aligned to at least that boundary. If not specified, or if zero, the target can + choose to align the allocation on any convenient boundary.
+ +'type' must be a sized type.
+ +Memory is allocated using the system "malloc" function, and + a pointer is returned. The result of a zero byte allocattion is undefined. The + result is null if there is insufficient memory available.
+ +
+ %array = malloc [4 x i8 ] ; yields {[%4 x i8]*}:array
+
+ %size = add i32 2, 2 ; yields {i32}:size = i32 4
+ %array1 = malloc i8, i32 4 ; yields {i8*}:array1
+ %array2 = malloc [12 x i8], i32 %size ; yields {[12 x i8]*}:array2
+ %array3 = malloc i32, i32 4, align 1024 ; yields {i32*}:array3
+ %array4 = malloc i32, align 1024 ; yields {i32*}:array4
+
+
+ free <type> <value> ; yields {void}
+
+
+ The 'free' instruction returns memory back to the unused + memory heap to be reallocated in the future.
+ +'value' shall be a pointer value that points to a value + that was allocated with the 'malloc' + instruction.
+ +Access to the memory pointed to by the pointer is no longer defined + after this instruction executes. If the pointer is null, the operation + is a noop.
+ ++ %array = malloc [4 x i8] ; yields {[4 x i8]*}:array + free [4 x i8]* %array ++
+ <result> = alloca <type>[, i32 <NumElements>][, align <alignment>] ; yields {type*}:result
+
+
+ The 'alloca' instruction allocates memory on the stack frame of the + currently executing function, to be automatically released when this function + returns to its caller. The object is always allocated in the generic address + space (address space zero).
+ +The 'alloca' instruction allocates sizeof(<type>)*NumElements + bytes of memory on the runtime stack, returning a pointer of the + appropriate type to the program. If "NumElements" is specified, it is the + number of elements allocated, otherwise "NumElements" is defaulted to be one. + If a constant alignment is specified, the value result of the allocation is guaranteed + to be aligned to at least that boundary. If not specified, or if zero, the target + can choose to align the allocation on any convenient boundary.
+ +'type' may be any sized type.
+ +Memory is allocated; a pointer is returned. The operation is undefiend if + there is insufficient stack space for the allocation. 'alloca'd + memory is automatically released when the function returns. The 'alloca' + instruction is commonly used to represent automatic variables that must + have an address available. When the function returns (either with the ret or unwind + instructions), the memory is reclaimed. Allocating zero bytes + is legal, but the result is undefined.
+ +
+ %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, align 1024 ; yields {i32*}:ptr
+
+ <result> = load <ty>* <pointer>[, align <alignment>]+
<result> = volatile load <ty>* <pointer>[, align <alignment>]
The 'load' instruction is used to read from memory.
+The argument to the 'load' instruction specifies the memory + address from which to load. The pointer must point to a first class type. If the load is + marked as volatile, then the optimizer is not allowed to modify + the number or order of execution of this load with other + volatile load and store + instructions.
++ The optional constant "align" argument specifies the alignment of the operation + (that is, the alignment of the memory address). A value of 0 or an + omitted "align" argument means that the operation has the preferential + alignment for the target. It is the responsibility of the code emitter + to ensure that the alignment information is correct. Overestimating + the alignment results in an undefined behavior. Underestimating the + alignment may produce less efficient code. An alignment of 1 is always + safe. +
+The location of memory pointed to is loaded.
+%ptr = alloca i32 ; yields {i32*}:ptr + store i32 3, i32* %ptr ; yields {void} + %val = load i32* %ptr ; yields {i32}:val = i32 3 ++
store <ty> <value>, <ty>* <pointer>[, align <alignment>] ; yields {void}
+ volatile store <ty> <value>, <ty>* <pointer>[, align <alignment>] ; yields {void}
+
+ The 'store' instruction is used to write to memory.
+There are two arguments to the 'store' instruction: a value + to store and an address at which to store it. The type of the '<pointer>' + operand must be a pointer to the first class type + of the '<value>' + operand. If the store is marked as volatile, then the + optimizer is not allowed to modify the number or order of execution of + this store with other volatile load and store instructions.
++ The optional constant "align" argument specifies the alignment of the operation + (that is, the alignment of the memory address). A value of 0 or an + omitted "align" argument means that the operation has the preferential + alignment for the target. It is the responsibility of the code emitter + to ensure that the alignment information is correct. Overestimating + the alignment results in an undefined behavior. Underestimating the + alignment may produce less efficient code. An alignment of 1 is always + safe. +
+The contents of memory are updated to contain '<value>' + at the location specified by the '<pointer>' operand.
+%ptr = alloca i32 ; yields {i32*}:ptr + store i32 3, i32* %ptr ; yields {void} + %val = load i32* %ptr ; yields {i32}:val = i32 3 ++
+ <result> = getelementptr <ty>* <ptrval>{, <ty> <idx>}*
+
+
+ + The 'getelementptr' instruction is used to get the address of a + subelement of an aggregate data structure.
+ +This instruction takes a list of integer operands that indicate what + elements of the aggregate object to index to. The actual types of the arguments + provided depend on the type of the first pointer argument. The + 'getelementptr' instruction is used to index down through the type + levels of a structure or to a specific index in an array. When indexing into a + structure, only i32 integer constants are allowed. When indexing + into an array or pointer, only integers of 32 or 64 bits are allowed; 32-bit + values will be sign extended to 64-bits if required.
+ +For example, let's consider a C code fragment and how it gets + compiled to LLVM:
+ +
+ struct RT {
+ char A;
+ int B[10][20];
+ char C;
+ };
+ struct ST {
+ int X;
+ double Y;
+ struct RT Z;
+ };
+
+ int *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 }
+
+ define i32* %foo(%ST* %s) {
+ entry:
+ %reg = getelementptr %ST* %s, i32 1, i32 2, i32 1, i32 5, i32 13
+ ret i32* %reg
+ }
+
+ The index types specified for the 'getelementptr' instruction depend + on the pointer type that is being indexed into. Pointer + and array types can use a 32-bit or 64-bit + integer type but the value will always be sign extended + to 64-bits. Structure and packed + structure types require i32 constants.
+ +In the example above, the first index is indexing into the '%ST*' + type, which is a pointer, yielding a '%ST' = '{ i32, double, %RT + }' type, a structure. The second index indexes into the third element of + the structure, yielding a '%RT' = '{ i8 , [10 x [20 x i32]], + i8 }' type, another structure. The third index indexes into the second + element of the structure, yielding a '[10 x [20 x i32]]' type, an + array. The two dimensions of the array are subscripted into, yielding an + 'i32' type. The 'getelementptr' instruction returns a pointer + to this element, thus computing a value of 'i32*' type.
+ +Note that it is perfectly legal to index partially through a + structure, returning a pointer to an inner element. Because of this, + the LLVM code for the given testcase is equivalent to:
+ +
+ define i32* %foo(%ST* %s) {
+ %t1 = getelementptr %ST* %s, i32 1 ; yields %ST*:%t1
+ %t2 = getelementptr %ST* %t1, i32 0, i32 2 ; yields %RT*:%t2
+ %t3 = getelementptr %RT* %t2, i32 0, i32 1 ; yields [10 x [20 x i32]]*:%t3
+ %t4 = getelementptr [10 x [20 x i32]]* %t3, i32 0, i32 5 ; yields [20 x i32]*:%t4
+ %t5 = getelementptr [20 x i32]* %t4, i32 0, i32 13 ; yields i32*:%t5
+ ret i32* %t5
+ }
+
+
+ Note that it is undefined to access an array out of bounds: array and + pointer indexes must always be within the defined bounds of the array type. + The one exception for this rule is zero length arrays. These arrays are + defined to be accessible as variable length arrays, which requires access + beyond the zero'th element.
+ +The getelementptr instruction is often confusing. For some more insight + into how it works, see the getelementptr + FAQ.
+ +
+ ; yields [12 x i8]*:aptr
+ %aptr = getelementptr {i32, [12 x i8]}* %sptr, i64 0, i32 1
+
+ The instructions in this category are the conversion instructions (casting) + which all take a single operand and a type. They perform various bit conversions + on the operand.
++ <result> = trunc <ty> <value> to <ty2> ; yields ty2 ++ +
+ The 'trunc' instruction truncates its operand to the type ty2. +
+ ++ The 'trunc' instruction takes a value to trunc, which must + be an integer type, and a type that specifies the size + and type of the result, which must be an integer + type. The bit size of value must be larger than the bit size of + ty2. Equal sized types are not allowed.
+ ++ The 'trunc' instruction truncates the high order bits in value + and converts the remaining bits to ty2. Since the source size must be + larger than the destination size, trunc cannot be a no-op cast. + It will always truncate bits.
+ ++ %X = trunc i32 257 to i8 ; yields i8:1 + %Y = trunc i32 123 to i1 ; yields i1:true + %Y = trunc i32 122 to i1 ; yields i1:false ++
+ <result> = zext <ty> <value> to <ty2> ; yields ty2 ++ +
The 'zext' instruction zero extends its operand to type + ty2.
+ + +The 'zext' instruction takes a value to cast, which must be of + integer type, and a type to cast it to, which must + also be of integer type. The bit size of the + value must be smaller than the bit size of the destination type, + ty2.
+ +The zext fills the high order bits of the value with zero + bits until it reaches the size of the destination type, ty2.
+ +When zero extending from i1, the result will always be either 0 or 1.
+ ++ %X = zext i32 257 to i64 ; yields i64:257 + %Y = zext i1 true to i32 ; yields i32:1 ++
+ <result> = sext <ty> <value> to <ty2> ; yields ty2 ++ +
The 'sext' sign extends value to the type ty2.
+ ++ The 'sext' instruction takes a value to cast, which must be of + integer type, and a type to cast it to, which must + also be of integer type. The bit size of the + value must be smaller than the bit size of the destination type, + ty2.
+ ++ The 'sext' instruction performs a sign extension by copying the sign + bit (highest order bit) of the value until it reaches the bit size of + the type ty2.
+ +When sign extending from i1, the extension always results in -1 or 0.
+ ++ %X = sext i8 -1 to i16 ; yields i16 :65535 + %Y = sext i1 true to i32 ; yields i32:-1 ++
+ <result> = fptrunc <ty> <value> to <ty2> ; yields ty2 ++ +
The 'fptrunc' instruction truncates value to type + ty2.
+ + +The 'fptrunc' instruction takes a floating + point value to cast and a floating point type to + cast it to. The size of value must be larger than the size of + ty2. This implies that fptrunc cannot be used to make a + no-op cast.
+ +The 'fptrunc' instruction truncates a value from a larger + floating point type to a smaller + floating point type. If the value cannot fit within + the destination type, ty2, then the results are undefined.
+ ++ %X = fptrunc double 123.0 to float ; yields float:123.0 + %Y = fptrunc double 1.0E+300 to float ; yields undefined ++
+ <result> = fpext <ty> <value> to <ty2> ; yields ty2 ++ +
The 'fpext' extends a floating point value to a larger + floating point value.
+ +The 'fpext' instruction takes a + floating point value to cast, + and a floating point type to cast it to. The source + type must be smaller than the destination type.
+ +The 'fpext' instruction extends the value from a smaller + floating point type to a larger + floating point type. The fpext cannot be + used to make a no-op cast because it always changes bits. Use + bitcast to make a no-op cast for a floating point cast.
+ ++ %X = fpext float 3.1415 to double ; yields double:3.1415 + %Y = fpext float 1.0 to float ; yields float:1.0 (no-op) ++
+ <result> = fptoui <ty> <value> to <ty2> ; yields ty2 ++ +
The 'fptoui' converts a floating point value to its + unsigned integer equivalent of type ty2. +
+ +The 'fptoui' instruction takes a value to cast, which must be a + scalar or vector floating point value, and a type + to cast it to ty2, which must be an integer + type. If ty is a vector floating point type, ty2 must be a + vector integer type with the same number of elements as ty
+ +The 'fptoui' instruction converts its + floating point operand into the nearest (rounding + towards zero) unsigned integer value. If the value cannot fit in ty2, + the results are undefined.
+ ++ %X = fptoui double 123.0 to i32 ; yields i32:123 + %Y = fptoui float 1.0E+300 to i1 ; yields undefined:1 + %X = fptoui float 1.04E+17 to i8 ; yields undefined:1 ++
+ <result> = fptosi <ty> <value> to <ty2> ; yields ty2 ++ +
The 'fptosi' instruction converts + floating point value to type ty2. +
+ +The 'fptosi' instruction takes a value to cast, which must be a + scalar or vector floating point value, and a type + to cast it to ty2, which must be an integer + type. If ty is a vector floating point type, ty2 must be a + vector integer type with the same number of elements as ty
+ +The 'fptosi' instruction converts its + floating point operand into the nearest (rounding + towards zero) signed integer value. If the value cannot fit in ty2, + the results are undefined.
+ ++ %X = fptosi double -123.0 to i32 ; yields i32:-123 + %Y = fptosi float 1.0E-247 to i1 ; yields undefined:1 + %X = fptosi float 1.04E+17 to i8 ; yields undefined:1 ++
+ <result> = uitofp <ty> <value> to <ty2> ; yields ty2 ++ +
The 'uitofp' instruction regards value as an unsigned + integer and converts that value to the ty2 type.
+ +The 'uitofp' instruction takes a value to cast, which must be a + scalar or vector integer value, and a type to cast it + to ty2, which must be an floating point + type. If ty is a vector integer type, ty2 must be a vector + floating point type with the same number of elements as ty
+ +The 'uitofp' instruction interprets its operand as an unsigned + integer quantity and converts it to the corresponding floating point value. If + the value cannot fit in the floating point value, the results are undefined.
+ ++ %X = uitofp i32 257 to float ; yields float:257.0 + %Y = uitofp i8 -1 to double ; yields double:255.0 ++
+ <result> = sitofp <ty> <value> to <ty2> ; yields ty2 ++ +
The 'sitofp' instruction regards value as a signed + integer and converts that value to the ty2 type.
+ +The 'sitofp' instruction takes a value to cast, which must be a + scalar or vector integer value, and a type to cast it + to ty2, which must be an floating point + type. If ty is a vector integer type, ty2 must be a vector + floating point type with the same number of elements as ty
+ +The 'sitofp' instruction interprets its operand as a signed + integer quantity and converts it to the corresponding floating point value. If + the value cannot fit in the floating point value, the results are undefined.
+ ++ %X = sitofp i32 257 to float ; yields float:257.0 + %Y = sitofp i8 -1 to double ; yields double:-1.0 ++
+ <result> = ptrtoint <ty> <value> to <ty2> ; yields ty2 ++ +
The 'ptrtoint' instruction converts the pointer value to + the integer type ty2.
+ +The 'ptrtoint' instruction takes a value to cast, which + must be a pointer value, and a type to cast it to + ty2, which must be an integer type. + +
The 'ptrtoint' instruction converts value to integer type + ty2 by interpreting the pointer value as an integer and either + truncating or zero extending that value to the size of the integer type. If + value is smaller than ty2 then a zero extension is done. If + value is larger than ty2 then a truncation is done. If they + are the same size, then nothing is done (no-op cast) other than a type + change.
+ ++ %X = ptrtoint i32* %X to i8 ; yields truncation on 32-bit architecture + %Y = ptrtoint i32* %x to i64 ; yields zero extension on 32-bit architecture ++
+ <result> = inttoptr <ty> <value> to <ty2> ; yields ty2 ++ +
The 'inttoptr' instruction converts an integer value to + a pointer type, ty2.
+ +The 'inttoptr' instruction takes an integer + value to cast, and a type to cast it to, which must be a + pointer type. + +
The 'inttoptr' instruction converts value to type + ty2 by applying either a zero extension or a truncation depending on + the size of the integer value. If value is larger than the + size of a pointer then a truncation is done. If value is smaller than + the size of a pointer then a zero extension is done. If they are the same size, + nothing is done (no-op cast).
+ ++ %X = inttoptr i32 255 to i32* ; yields zero extension on 64-bit architecture + %X = inttoptr i32 255 to i32* ; yields no-op on 32-bit architecture + %Y = inttoptr i64 0 to i32* ; yields truncation on 32-bit architecture ++
+ <result> = bitcast <ty> <value> to <ty2> ; yields ty2 ++ +
The 'bitcast' instruction converts value to type + ty2 without changing any bits.
+ +The 'bitcast' instruction takes a value to cast, which must be + a first class value, and a type to cast it to, which must also be a first class type. The bit sizes of value + and the destination type, ty2, must be identical. If the source + type is a pointer, the destination type must also be a pointer.
+ +The 'bitcast' instruction converts value to type + ty2. It is always a no-op cast because no bits change with + this conversion. The conversion is done as if the value had been + stored to memory and read back as type ty2. Pointer types may only be + converted to other pointer types with this instruction. To convert pointers to + other types, use the inttoptr or + ptrtoint instructions first.
+ ++ %X = bitcast i8 255 to i8 ; yields i8 :-1 + %Y = bitcast i32* %x to sint* ; yields sint*:%x + %Z = bitcast <2xint> %V to i64; ; yields i64: %V ++
The instructions in this category are the "miscellaneous" + instructions, which defy better classification.
+ <result> = icmp <cond> <ty> <var1>, <var2> ; yields {i1}:result
+
+ The 'icmp' instruction returns a boolean value based on comparison + of its two integer or pointer operands.
+The 'icmp' instruction takes three operands. The first operand is + the condition code indicating the kind of comparison to perform. It is not + a value, just a keyword. The possible condition code are: +
The remaining two arguments must be integer or + pointer typed. They must also be identical types.
+The 'icmp' compares var1 and var2 according to + the condition code given as cond. The comparison performed always + yields a i1 result, as follows: +
If the operands are pointer typed, the pointer + values are compared as if they were integers.
+ +<result> = icmp eq i32 4, 5 ; yields: result=false + <result> = icmp ne float* %X, %X ; yields: result=false + <result> = icmp ult i16 4, 5 ; yields: result=true + <result> = icmp sgt i16 4, 5 ; yields: result=false + <result> = icmp ule i16 -4, 5 ; yields: result=false + <result> = icmp sge i16 4, 5 ; yields: result=false ++
<result> = fcmp <cond> <ty> <var1>, <var2> ; yields {i1}:result
+
+ The 'fcmp' instruction returns a boolean value based on comparison + of its floating point operands.
+The 'fcmp' instruction takes three operands. The first operand is + the condition code indicating the kind of comparison to perform. It is not + a value, just a keyword. The possible condition code are: +
Ordered means that neither operand is a QNAN while + unordered means that either operand may be a QNAN.
+The val1 and val2 arguments must be + floating point typed. They must have identical + types.
+The 'fcmp' compares var1 and var2 according to + the condition code given as cond. The comparison performed always + yields a i1 result, as follows: +
<result> = fcmp oeq float 4.0, 5.0 ; yields: result=false + <result> = icmp one float 4.0, 5.0 ; yields: result=true + <result> = icmp olt float 4.0, 5.0 ; yields: result=true + <result> = icmp ueq double 1.0, 2.0 ; yields: result=false ++
<result> = phi <ty> [ <val0>, <label0>], ...+
The 'phi' instruction is used to implement the φ node in + the SSA graph representing the function.
+The type of the incoming values is specified with the first type + field. After this, the 'phi' instruction takes a list of pairs + as arguments, with one pair for each predecessor basic block of the + current block. Only values of first class + type may be used as the value arguments to the PHI node. Only labels + may be used as the label arguments.
+There must be no non-phi instructions between the start of a basic + block and the PHI instructions: i.e. PHI instructions must be first in + a basic block.
+At runtime, the 'phi' instruction logically takes on the value + specified by the pair corresponding to the predecessor basic block that executed + just prior to the current block.
+Loop: ; Infinite loop that counts from 0 on up...+
%indvar = phi i32 [ 0, %LoopHeader ], [ %nextindvar, %Loop ]
%nextindvar = add i32 %indvar, 1
br label %Loop
+ <result> = select i1 <cond>, <ty> <val1>, <ty> <val2> ; yields ty ++ +
+ The 'select' instruction is used to choose one value based on a + condition, without branching. +
+ + ++ The 'select' instruction requires a boolean value indicating the condition, and two values of the same first class type. +
+ ++ If the boolean condition evaluates to true, the instruction returns the first + value argument; otherwise, it returns the second value argument. +
+ ++ %X = select i1 true, i8 17, i8 42 ; yields i8:17 ++
+ <result> = [tail] call [cconv] <ty> [<fnty>*] <fnptrval>(<param list>) ++ +
The 'call' instruction represents a simple function call.
+ +This instruction requires several arguments:
+ +The optional "tail" marker indicates whether the callee function accesses + any allocas or varargs in the caller. If the "tail" marker is present, the + function call is eligible for tail call optimization. Note that calls may + be marked "tail" even if they do not occur before a ret instruction. +
The optional "cconv" marker indicates which calling + convention the call should use. If none is specified, the call defaults + to using C calling conventions. +
'ty': the type of the call instruction itself which is also + the type of the return value. Functions that return no value are marked + void.
+'fnty': shall be the signature of the pointer to function + value being invoked. The argument types must match the types implied by + this signature. This type can be omitted if the function is not varargs + and if the function type does not return a pointer to a function.
+'fnptrval': An LLVM value containing a pointer to a function to + be invoked. In most cases, this is a direct function invocation, but + indirect calls are just as possible, calling an arbitrary pointer + to function value.
+'function args': argument list whose types match the + function signature argument types. All arguments must be of + first class type. If the function signature + indicates the function accepts a variable number of arguments, the extra + arguments can be specified.
+The 'call' instruction is used to cause control flow to + transfer to a specified function, with its incoming arguments bound to + the specified values. Upon a 'ret' + instruction in the called function, control flow continues with the + instruction after the function call, and the return value of the + function is bound to the result argument. If the callee returns multiple + values then the return values of the function are only accessible through + the 'getresult' instruction.
+ ++ %retval = call i32 @test(i32 %argc) + call i32 (i8 *, ...)* @printf(i8 * %msg, i32 12, i8 42) ; yields i32 + %X = tail call i32 @foo() ; yields i32 + %Y = tail call fastcc i32 @foo() ; yields i32 + call void %foo(i8 97 signext) + + %struct.A = type { i32, i8 } + %r = call %struct.A @foo() ; yields { 32, i8 } + %gr = getresult %struct.A %r, 0 ; yields i32 + %gr1 = getresult %struct.A %r, 1 ; yields i8 ++ +
+ <resultval> = va_arg <va_list*> <arglist>, <argty> ++ +
The 'va_arg' instruction is used to access arguments passed through + the "variable argument" area of a function call. It is used to implement the + va_arg macro in C.
+ +This instruction takes a va_list* value and the type of + the argument. It returns a value of the specified argument type and + increments the va_list to point to the next argument. The + actual type of va_list is target specific.
+ +The 'va_arg' instruction loads an argument of the specified + type from the specified va_list and causes the + va_list to point to the next argument. For more information, + see the variable argument handling Intrinsic + Functions.
+ +It is legal for this instruction to be called in a function which does not + take a variable number of arguments, for example, the vfprintf + function.
+ +va_arg is an LLVM instruction instead of an intrinsic function because it takes a type as an + argument.
+ +See the variable argument processing section.
+ ++ <resultval> = getresult <type> <retval>, <index> ++ +
The 'getresult' instruction is used to extract individual values + from a 'call' + or 'invoke' instruction that returns multiple + results.
+ +The 'getresult' instruction takes a call or invoke value as its + first argument, or an undef value. The value must have structure type. The second argument is a constant + unsigned index value which must be in range for the number of values returned + by the call.
+ +The 'getresult' instruction extracts the element identified by + 'index' from the aggregate value.
+ +
+ %struct.A = type { i32, i8 }
+
+ %r = call %struct.A @foo()
+ %gr = getresult %struct.A %r, 0 ; yields i32:%gr
+ %gr1 = getresult %struct.A %r, 1 ; yields i8:%gr1
+ add i32 %gr, 42
+ add i8 %gr1, 41
+
+
+ LLVM supports the notion of an "intrinsic function". These functions have + well known names and semantics and are required to follow certain restrictions. + Overall, these intrinsics represent an extension mechanism for the LLVM + language that does not require changing all of the transformations in LLVM when + adding to the language (or the bitcode reader/writer, the parser, etc...).
+ +Intrinsic function names must all start with an "llvm." prefix. This + prefix is reserved in LLVM for intrinsic names; thus, function names may not + begin with this prefix. Intrinsic functions must always be external functions: + you cannot define the body of intrinsic functions. Intrinsic functions may + only be used in call or invoke instructions: it is illegal to take the address + of an intrinsic function. Additionally, because intrinsic functions are part + of the LLVM language, it is required if any are added that they be documented + here.
+ +Some intrinsic functions can be overloaded, i.e., the intrinsic represents + a family of functions that perform the same operation but on different data + types. Because LLVM can represent over 8 million different integer types, + overloading is used commonly to allow an intrinsic function to operate on any + integer type. One or more of the argument types or the result type can be + overloaded to accept any integer type. Argument types may also be defined as + exactly matching a previous argument's type or the result type. This allows an + intrinsic function which accepts multiple arguments, but needs all of them to + be of the same type, to only be overloaded with respect to a single argument or + the result.
+ +Overloaded intrinsics will have the names of its overloaded argument types + encoded into its function name, each preceded by a period. Only those types + which are overloaded result in a name suffix. Arguments whose type is matched + against another type do not. For example, the llvm.ctpop function can + take an integer of any width and returns an integer of exactly the same integer + width. This leads to a family of functions such as + i8 @llvm.ctpop.i8(i8 %val) and i29 @llvm.ctpop.i29(i29 %val). + Only one type, the return type, is overloaded, and only one type suffix is + required. Because the argument's type is matched against the return type, it + does not require its own name suffix.
+ +To learn how to add an intrinsic function, please see the + Extending LLVM Guide. +
+ +Variable argument support is defined in LLVM with the va_arg instruction and these three + intrinsic functions. These functions are related to the similarly + named macros defined in the <stdarg.h> header file.
+ +All of these functions operate on arguments that use a + target-specific value type "va_list". The LLVM assembly + language reference manual does not define what this type is, so all + transformations should be prepared to handle these functions regardless of + the type used.
+ +This example shows how the va_arg + instruction and the variable argument handling intrinsic functions are + used.
+ +
+ define i32 @test(i32 %X, ...) {
+ ; Initialize variable argument processing
+ %ap = alloca i8*
+ %ap2 = bitcast i8** %ap to i8*
+ call void @llvm.va_start(i8* %ap2)
+
+ ; Read a single integer argument
+ %tmp = va_arg i8** %ap, i32
+
+ ; Demonstrate usage of llvm.va_copy and llvm.va_end
+ %aq = alloca i8*
+ %aq2 = bitcast i8** %aq to i8*
+ call void @llvm.va_copy(i8* %aq2, i8* %ap2)
+ call void @llvm.va_end(i8* %aq2)
+
+ ; Stop processing of arguments.
+ call void @llvm.va_end(i8* %ap2)
+ ret i32 %tmp
+ }
+
+ declare void @llvm.va_start(i8*)
+ declare void @llvm.va_copy(i8*, i8*)
+ declare void @llvm.va_end(i8*)
+
+ declare void %llvm.va_start(i8* <arglist>)+
The 'llvm.va_start' intrinsic initializes + *<arglist> for subsequent use by va_arg.
+ +The argument is a pointer to a va_list element to initialize.
+ +The 'llvm.va_start' intrinsic works just like the va_start + macro available in C. In a target-dependent way, it initializes the + va_list element to which the argument points, so that the next call to + va_arg will produce the first variable argument passed to the function. + Unlike the C va_start macro, this intrinsic does not need to know the + last argument of the function as the compiler can figure that out.
+ +declare void @llvm.va_end(i8* <arglist>)+
The 'llvm.va_end' intrinsic destroys *<arglist>, + which has been initialized previously with llvm.va_start + or llvm.va_copy.
+ +The argument is a pointer to a va_list to destroy.
+ +The 'llvm.va_end' intrinsic works just like the va_end + macro available in C. In a target-dependent way, it destroys the + va_list element to which the argument points. Calls to llvm.va_start and + llvm.va_copy must be matched exactly with calls to + llvm.va_end.
+ ++ declare void @llvm.va_copy(i8* <destarglist>, i8* <srcarglist>) ++ +
The 'llvm.va_copy' intrinsic copies the current argument position + from the source argument list to the destination argument list.
+ +The first argument is a pointer to a va_list element to initialize. + The second argument is a pointer to a va_list element to copy from.
+ + +The 'llvm.va_copy' intrinsic works just like the va_copy + macro available in C. In a target-dependent way, it copies the source + va_list element into the destination va_list element. This + intrinsic is necessary because the + llvm.va_start intrinsic may be arbitrarily complex and require, for + example, memory allocation.
+ ++ LLVM support for Accurate Garbage + Collection requires the implementation and generation of these intrinsics. + These intrinsics allow identification of GC roots on the + stack, as well as garbage collector implementations that require read and write barriers. + Front-ends for type-safe garbage collected languages should generate these + intrinsics to make use of the LLVM garbage collectors. For more details, see Accurate Garbage Collection with LLVM. +
+ +The garbage collection intrinsics only operate on objects in the generic + address space (address space zero).
+ ++ declare void @llvm.gcroot(i8** %ptrloc, i8* %metadata) ++ +
The 'llvm.gcroot' intrinsic declares the existence of a GC root to + the code generator, and allows some metadata to be associated with it.
+ +The first argument specifies the address of a stack object that contains the + root pointer. The second pointer (which must be either a constant or a global + value address) contains the meta-data to be associated with the root.
+ +At runtime, a call to this intrinsic stores a null pointer into the "ptrloc" + location. At compile-time, the code generator generates information to allow + the runtime to find the pointer at GC safe points. The 'llvm.gcroot' + intrinsic may only be used in a function which specifies a GC + algorithm.
+ ++ declare i8* @llvm.gcread(i8* %ObjPtr, i8** %Ptr) ++ +
The 'llvm.gcread' intrinsic identifies reads of references from heap + locations, allowing garbage collector implementations that require read + barriers.
+ +The second argument is the address to read from, which should be an address + allocated from the garbage collector. The first object is a pointer to the + start of the referenced object, if needed by the language runtime (otherwise + null).
+ +The 'llvm.gcread' intrinsic has the same semantics as a load + instruction, but may be replaced with substantially more complex code by the + garbage collector runtime, as needed. The 'llvm.gcread' intrinsic + may only be used in a function which specifies a GC + algorithm.
+ ++ declare void @llvm.gcwrite(i8* %P1, i8* %Obj, i8** %P2) ++ +
The 'llvm.gcwrite' intrinsic identifies writes of references to heap + locations, allowing garbage collector implementations that require write + barriers (such as generational or reference counting collectors).
+ +The first argument is the reference to store, the second is the start of the + object to store it to, and the third is the address of the field of Obj to + store to. If the runtime does not require a pointer to the object, Obj may be + null.
+ +The 'llvm.gcwrite' intrinsic has the same semantics as a store + instruction, but may be replaced with substantially more complex code by the + garbage collector runtime, as needed. The 'llvm.gcwrite' intrinsic + may only be used in a function which specifies a GC + algorithm.
+ ++ These intrinsics are provided by LLVM to expose special features that may only + be implemented with code generator support. +
+ ++ declare i8 *@llvm.returnaddress(i32 <level>) ++ +
+ The 'llvm.returnaddress' intrinsic attempts to compute a + target-specific value indicating the return address of the current function + or one of its callers. +
+ ++ The argument to this intrinsic indicates which function to return the address + for. Zero indicates the calling function, one indicates its caller, etc. The + argument is required to be a constant integer value. +
+ ++ The 'llvm.returnaddress' intrinsic either returns a pointer indicating + the return address of the specified call frame, or zero if it cannot be + identified. The value returned by this intrinsic is likely to be incorrect or 0 + for arguments other than zero, so it should only be used for debugging purposes. +
+ ++ Note that calling this intrinsic does not prevent function inlining or other + aggressive transformations, so the value returned may not be that of the obvious + source-language caller. +
++ declare i8 *@llvm.frameaddress(i32 <level>) ++ +
+ The 'llvm.frameaddress' intrinsic attempts to return the + target-specific frame pointer value for the specified stack frame. +
+ ++ The argument to this intrinsic indicates which function to return the frame + pointer for. Zero indicates the calling function, one indicates its caller, + etc. The argument is required to be a constant integer value. +
+ ++ The 'llvm.frameaddress' intrinsic either returns a pointer indicating + the frame address of the specified call frame, or zero if it cannot be + identified. The value returned by this intrinsic is likely to be incorrect or 0 + for arguments other than zero, so it should only be used for debugging purposes. +
+ ++ Note that calling this intrinsic does not prevent function inlining or other + aggressive transformations, so the value returned may not be that of the obvious + source-language caller. +
++ declare i8 *@llvm.stacksave() ++ +
+ The 'llvm.stacksave' intrinsic is used to remember the current state of + the function stack, for use with + llvm.stackrestore. This is useful for implementing language + features like scoped automatic variable sized arrays in C99. +
+ ++ This intrinsic returns a opaque pointer value that can be passed to llvm.stackrestore. When an + llvm.stackrestore intrinsic is executed with a value saved from + llvm.stacksave, it effectively restores the state of the stack to the + state it was in when the llvm.stacksave intrinsic executed. In + practice, this pops any alloca blocks from the stack + that were allocated after the llvm.stacksave was executed. +
+ ++ declare void @llvm.stackrestore(i8 * %ptr) ++ +
+ The 'llvm.stackrestore' intrinsic is used to restore the state of + the function stack to the state it was in when the corresponding llvm.stacksave intrinsic executed. This is + useful for implementing language features like scoped automatic variable sized + arrays in C99. +
+ ++ See the description for llvm.stacksave. +
+ ++ declare void @llvm.prefetch(i8* <address>, i32 <rw>, i32 <locality>) ++ +
+ The 'llvm.prefetch' intrinsic is a hint to the code generator to insert + a prefetch instruction if supported; otherwise, it is a noop. Prefetches have + no + effect on the behavior of the program but can change its performance + characteristics. +
+ ++ address is the address to be prefetched, rw is the specifier + determining if the fetch should be for a read (0) or write (1), and + locality is a temporal locality specifier ranging from (0) - no + locality, to (3) - extremely local keep in cache. The rw and + locality arguments must be constant integers. +
+ ++ This intrinsic does not modify the behavior of the program. In particular, + prefetches cannot trap and do not produce a value. On targets that support this + intrinsic, the prefetch can provide hints to the processor cache for better + performance. +
+ ++ declare void @llvm.pcmarker(i32 <id>) ++ +
+ The 'llvm.pcmarker' intrinsic is a method to export a Program Counter + (PC) in a region of + code to simulators and other tools. The method is target specific, but it is + expected that the marker will use exported symbols to transmit the PC of the marker. + The marker makes no guarantees that it will remain with any specific instruction + after optimizations. It is possible that the presence of a marker will inhibit + optimizations. The intended use is to be inserted after optimizations to allow + correlations of simulation runs. +
+ ++ id is a numerical id identifying the marker. +
+ ++ This intrinsic does not modify the behavior of the program. Backends that do not + support this intrinisic may ignore it. +
+ ++ declare i64 @llvm.readcyclecounter( ) ++ +
+ The 'llvm.readcyclecounter' intrinsic provides access to the cycle + counter register (or similar low latency, high accuracy clocks) on those targets + that support it. On X86, it should map to RDTSC. On Alpha, it should map to RPCC. + As the backing counters overflow quickly (on the order of 9 seconds on alpha), this + should only be used for small timings. +
+ ++ When directly supported, reading the cycle counter should not modify any memory. + Implementations are allowed to either return a application specific value or a + system wide value. On backends without support, this is lowered to a constant 0. +
+ ++ LLVM provides intrinsics for a few important standard C library functions. + These intrinsics allow source-language front-ends to pass information about the + alignment of the pointer arguments to the code generator, providing opportunity + for more efficient code generation. +
+ ++ declare void @llvm.memcpy.i32(i8 * <dest>, i8 * <src>, + i32 <len>, i32 <align>) + declare void @llvm.memcpy.i64(i8 * <dest>, i8 * <src>, + i64 <len>, i32 <align>) ++ +
+ The 'llvm.memcpy.*' intrinsics copy a block of memory from the source + location to the destination location. +
+ ++ Note that, unlike the standard libc function, the llvm.memcpy.* + intrinsics do not return a value, and takes an extra alignment argument. +
+ ++ The first argument is a pointer to the destination, the second is a pointer to + the source. The third argument is an integer argument + specifying the number of bytes to copy, and the fourth argument is the alignment + of the source and destination locations. +
+ ++ If the call to this intrinisic has an alignment value that is not 0 or 1, then + the caller guarantees that both the source and destination pointers are aligned + to that boundary. +
+ ++ The 'llvm.memcpy.*' intrinsics copy a block of memory from the source + location to the destination location, which are not allowed to overlap. It + copies "len" bytes of memory over. If the argument is known to be aligned to + some boundary, this can be specified as the fourth argument, otherwise it should + be set to 0 or 1. +
++ declare void @llvm.memmove.i32(i8 * <dest>, i8 * <src>, + i32 <len>, i32 <align>) + declare void @llvm.memmove.i64(i8 * <dest>, i8 * <src>, + i64 <len>, i32 <align>) ++ +
+ The 'llvm.memmove.*' intrinsics move a block of memory from the source + location to the destination location. It is similar to the + 'llvm.memcpy' intrinsic but allows the two memory locations to overlap. +
+ ++ Note that, unlike the standard libc function, the llvm.memmove.* + intrinsics do not return a value, and takes an extra alignment argument. +
+ ++ The first argument is a pointer to the destination, the second is a pointer to + the source. The third argument is an integer argument + specifying the number of bytes to copy, and the fourth argument is the alignment + of the source and destination locations. +
+ ++ If the call to this intrinisic has an alignment value that is not 0 or 1, then + the caller guarantees that the source and destination pointers are aligned to + that boundary. +
+ ++ The 'llvm.memmove.*' intrinsics copy a block of memory from the source + location to the destination location, which may overlap. It + copies "len" bytes of memory over. If the argument is known to be aligned to + some boundary, this can be specified as the fourth argument, otherwise it should + be set to 0 or 1. +
++ declare void @llvm.memset.i32(i8 * <dest>, i8 <val>, + i32 <len>, i32 <align>) + declare void @llvm.memset.i64(i8 * <dest>, i8 <val>, + i64 <len>, i32 <align>) ++ +
+ The 'llvm.memset.*' intrinsics fill a block of memory with a particular + byte value. +
+ ++ Note that, unlike the standard libc function, the llvm.memset intrinsic + does not return a value, and takes an extra alignment argument. +
+ ++ The first argument is a pointer to the destination to fill, the second is the + byte value to fill it with, the third argument is an integer + argument specifying the number of bytes to fill, and the fourth argument is the + known alignment of destination location. +
+ ++ If the call to this intrinisic has an alignment value that is not 0 or 1, then + the caller guarantees that the destination pointer is aligned to that boundary. +
+ ++ The 'llvm.memset.*' intrinsics fill "len" bytes of memory starting at + the + destination location. If the argument is known to be aligned to some boundary, + this can be specified as the fourth argument, otherwise it should be set to 0 or + 1. +
+This is an overloaded intrinsic. You can use llvm.sqrt on any + floating point or vector of floating point type. Not all targets support all + types however. +
+ declare float @llvm.sqrt.f32(float %Val) + declare double @llvm.sqrt.f64(double %Val) + declare x86_fp80 @llvm.sqrt.f80(x86_fp80 %Val) + declare fp128 @llvm.sqrt.f128(fp128 %Val) + declare ppc_fp128 @llvm.sqrt.ppcf128(ppc_fp128 %Val) ++ +
+ The 'llvm.sqrt' intrinsics return the sqrt of the specified operand, + returning the same value as the libm 'sqrt' functions would. Unlike + sqrt in libm, however, llvm.sqrt has undefined behavior for + negative numbers other than -0.0 (which allows for better optimization, because + there is no need to worry about errno being set). llvm.sqrt(-0.0) is + defined to return -0.0 like IEEE sqrt. +
+ ++ The argument and return value are floating point numbers of the same type. +
+ ++ This function returns the sqrt of the specified operand if it is a nonnegative + floating point number. +
+This is an overloaded intrinsic. You can use llvm.powi on any + floating point or vector of floating point type. Not all targets support all + types however. +
+ declare float @llvm.powi.f32(float %Val, i32 %power) + declare double @llvm.powi.f64(double %Val, i32 %power) + declare x86_fp80 @llvm.powi.f80(x86_fp80 %Val, i32 %power) + declare fp128 @llvm.powi.f128(fp128 %Val, i32 %power) + declare ppc_fp128 @llvm.powi.ppcf128(ppc_fp128 %Val, i32 %power) ++ +
+ The 'llvm.powi.*' intrinsics return the first operand raised to the + specified (positive or negative) power. The order of evaluation of + multiplications is not defined. When a vector of floating point type is + used, the second argument remains a scalar integer value. +
+ ++ The second argument is an integer power, and the first is a value to raise to + that power. +
+ ++ This function returns the first value raised to the second power with an + unspecified sequence of rounding operations.
+This is an overloaded intrinsic. You can use llvm.sin on any + floating point or vector of floating point type. Not all targets support all + types however. +
+ declare float @llvm.sin.f32(float %Val) + declare double @llvm.sin.f64(double %Val) + declare x86_fp80 @llvm.sin.f80(x86_fp80 %Val) + declare fp128 @llvm.sin.f128(fp128 %Val) + declare ppc_fp128 @llvm.sin.ppcf128(ppc_fp128 %Val) ++ +
+ The 'llvm.sin.*' intrinsics return the sine of the operand. +
+ ++ The argument and return value are floating point numbers of the same type. +
+ ++ This function returns the sine of the specified operand, returning the + same values as the libm sin functions would, and handles error + conditions in the same way.
+This is an overloaded intrinsic. You can use llvm.cos on any + floating point or vector of floating point type. Not all targets support all + types however. +
+ declare float @llvm.cos.f32(float %Val) + declare double @llvm.cos.f64(double %Val) + declare x86_fp80 @llvm.cos.f80(x86_fp80 %Val) + declare fp128 @llvm.cos.f128(fp128 %Val) + declare ppc_fp128 @llvm.cos.ppcf128(ppc_fp128 %Val) ++ +
+ The 'llvm.cos.*' intrinsics return the cosine of the operand. +
+ ++ The argument and return value are floating point numbers of the same type. +
+ ++ This function returns the cosine of the specified operand, returning the + same values as the libm cos functions would, and handles error + conditions in the same way.
+This is an overloaded intrinsic. You can use llvm.pow on any + floating point or vector of floating point type. Not all targets support all + types however. +
+ declare float @llvm.pow.f32(float %Val, float %Power) + declare double @llvm.pow.f64(double %Val, double %Power) + declare x86_fp80 @llvm.pow.f80(x86_fp80 %Val, x86_fp80 %Power) + declare fp128 @llvm.pow.f128(fp128 %Val, fp128 %Power) + declare ppc_fp128 @llvm.pow.ppcf128(ppc_fp128 %Val, ppc_fp128 Power) ++ +
+ The 'llvm.pow.*' intrinsics return the first operand raised to the + specified (positive or negative) power. +
+ ++ The second argument is a floating point power, and the first is a value to + raise to that power. +
+ ++ This function returns the first value raised to the second power, + returning the + same values as the libm pow functions would, and handles error + conditions in the same way.
++ LLVM provides intrinsics for a few important bit manipulation operations. + These allow efficient code generation for some algorithms. +
+ +This is an overloaded intrinsic function. You can use bswap on any integer + type that is an even number of bytes (i.e. BitWidth % 16 == 0). +
+ declare i16 @llvm.bswap.i16(i16 <id>) + declare i32 @llvm.bswap.i32(i32 <id>) + declare i64 @llvm.bswap.i64(i64 <id>) ++ +
+ The 'llvm.bswap' family of intrinsics is used to byte swap integer + values with an even number of bytes (positive multiple of 16 bits). These are + useful for performing operations on data that is not in the target's native + byte order. +
+ ++ The llvm.bswap.i16 intrinsic returns an i16 value that has the high + and low byte of the input i16 swapped. Similarly, the llvm.bswap.i32 + intrinsic returns an i32 value that has the four bytes of the input i32 + swapped, so that if the input bytes are numbered 0, 1, 2, 3 then the returned + i32 will have its bytes in 3, 2, 1, 0 order. The llvm.bswap.i48, + llvm.bswap.i64 and other intrinsics extend this concept to + additional even-byte lengths (6 bytes, 8 bytes and more, respectively). +
+ +This is an overloaded intrinsic. You can use llvm.ctpop on any integer bit + width. Not all targets support all bit widths however. +
+ declare i8 @llvm.ctpop.i8 (i8 <src>) + declare i16 @llvm.ctpop.i16(i16 <src>) + declare i32 @llvm.ctpop.i32(i32 <src>) + declare i64 @llvm.ctpop.i64(i64 <src>) + declare i256 @llvm.ctpop.i256(i256 <src>) ++ +
+ The 'llvm.ctpop' family of intrinsics counts the number of bits set in a + value. +
+ ++ The only argument is the value to be counted. The argument may be of any + integer type. The return type must match the argument type. +
+ ++ The 'llvm.ctpop' intrinsic counts the 1's in a variable. +
+This is an overloaded intrinsic. You can use llvm.ctlz on any + integer bit width. Not all targets support all bit widths however. +
+ declare i8 @llvm.ctlz.i8 (i8 <src>) + declare i16 @llvm.ctlz.i16(i16 <src>) + declare i32 @llvm.ctlz.i32(i32 <src>) + declare i64 @llvm.ctlz.i64(i64 <src>) + declare i256 @llvm.ctlz.i256(i256 <src>) ++ +
+ The 'llvm.ctlz' family of intrinsic functions counts the number of + leading zeros in a variable. +
+ ++ The only argument is the value to be counted. The argument may be of any + integer type. The return type must match the argument type. +
+ ++ The 'llvm.ctlz' intrinsic counts the leading (most significant) zeros + in a variable. If the src == 0 then the result is the size in bits of the type + of src. For example, llvm.ctlz(i32 2) = 30. +
+This is an overloaded intrinsic. You can use llvm.cttz on any + integer bit width. Not all targets support all bit widths however. +
+ declare i8 @llvm.cttz.i8 (i8 <src>) + declare i16 @llvm.cttz.i16(i16 <src>) + declare i32 @llvm.cttz.i32(i32 <src>) + declare i64 @llvm.cttz.i64(i64 <src>) + declare i256 @llvm.cttz.i256(i256 <src>) ++ +
+ The 'llvm.cttz' family of intrinsic functions counts the number of + trailing zeros. +
+ ++ The only argument is the value to be counted. The argument may be of any + integer type. The return type must match the argument type. +
+ ++ The 'llvm.cttz' intrinsic counts the trailing (least significant) zeros + in a variable. If the src == 0 then the result is the size in bits of the type + of src. For example, llvm.cttz(2) = 1. +
+This is an overloaded intrinsic. You can use llvm.part.select + on any integer bit width. +
+ declare i17 @llvm.part.select.i17 (i17 %val, i32 %loBit, i32 %hiBit) + declare i29 @llvm.part.select.i29 (i29 %val, i32 %loBit, i32 %hiBit) ++ +
The 'llvm.part.select' family of intrinsic functions selects a + range of bits from an integer value and returns them in the same bit width as + the original value.
+ +The first argument, %val and the result may be integer types of + any bit width but they must have the same bit width. The second and third + arguments must be i32 type since they specify only a bit index.
+ +The operation of the 'llvm.part.select' intrinsic has two modes + of operation: forwards and reverse. If %loBit is greater than + %hiBits then the intrinsic operates in reverse mode. Otherwise it + operates in forward mode.
+In forward mode, this intrinsic is the equivalent of shifting %val + right by %loBit bits and then ANDing it with a mask with + only the %hiBit - %loBit bits set, as follows:
+In reverse mode, a similar computation is made except that the bits are + returned in the reverse order. So, for example, if X has the value + i16 0x0ACF (101011001111) and we apply + part.select(i16 X, 8, 3) to it, we get back the value + i16 0x0026 (000000100110).
+This is an overloaded intrinsic. You can use llvm.part.set + on any integer bit width. +
+ declare i17 @llvm.part.set.i17.i9 (i17 %val, i9 %repl, i32 %lo, i32 %hi) + declare i29 @llvm.part.set.i29.i9 (i29 %val, i9 %repl, i32 %lo, i32 %hi) ++ +
The 'llvm.part.set' family of intrinsic functions replaces a range + of bits in an integer value with another integer value. It returns the integer + with the replaced bits.
+ +The first argument, %val and the result may be integer types of + any bit width but they must have the same bit width. %val is the value + whose bits will be replaced. The second argument, %repl may be an + integer of any bit width. The third and fourth arguments must be i32 + type since they specify only a bit index.
+ +The operation of the 'llvm.part.set' intrinsic has two modes + of operation: forwards and reverse. If %lo is greater than + %hi then the intrinsic operates in reverse mode. Otherwise it + operates in forward mode.
+For both modes, the %repl value is prepared for use by either + truncating it down to the size of the replacement area or zero extending it + up to that size.
+In forward mode, the bits between %lo and %hi (inclusive) + are replaced with corresponding bits from %repl. That is the 0th bit + in %repl replaces the %loth bit in %val and etc. up + to the %hith bit. +
In reverse mode, a similar computation is made except that the bits are + reversed. That is, the 0th bit in %repl replaces the + %hi bit in %val and etc. down to the %loth bit. +
+ llvm.part.set(0xFFFF, 0, 4, 7) -> 0xFF0F + llvm.part.set(0xFFFF, 0, 7, 4) -> 0xFF0F + llvm.part.set(0xFFFF, 1, 7, 4) -> 0xFF8F + llvm.part.set(0xFFFF, F, 8, 3) -> 0xFFE7 + llvm.part.set(0xFFFF, 0, 3, 8) -> 0xFE07 ++
+ The LLVM debugger intrinsics (which all start with llvm.dbg. prefix), + are described in the LLVM Source Level + Debugging document. +
+The LLVM exception handling intrinsics (which all start with + llvm.eh. prefix), are described in the LLVM Exception + Handling document.
++ This intrinsic makes it possible to excise one parameter, marked with + the nest attribute, from a function. The result is a callable + function pointer lacking the nest parameter - the caller does not need + to provide a value for it. Instead, the value to use is stored in + advance in a "trampoline", a block of memory usually allocated + on the stack, which also contains code to splice the nest value into the + argument list. This is used to implement the GCC nested function address + extension. +
++ For example, if the function is + i32 f(i8* nest %c, i32 %x, i32 %y) then the resulting function + pointer has signature i32 (i32, i32)*. It can be created as follows:
++ %tramp = alloca [10 x i8], align 4 ; size and alignment only correct for X86 + %tramp1 = getelementptr [10 x i8]* %tramp, i32 0, i32 0 + %p = call i8* @llvm.init.trampoline( i8* %tramp1, i8* bitcast (i32 (i8* nest , i32, i32)* @f to i8*), i8* %nval ) + %fp = bitcast i8* %p to i32 (i32, i32)* ++
The call %val = call i32 %fp( i32 %x, i32 %y ) is then equivalent + to %val = call i32 %f( i8* %nval, i32 %x, i32 %y ).
++ declare i8* @llvm.init.trampoline(i8* <tramp>, i8* <func>, i8* <nval>) ++
+ This fills the memory pointed to by tramp with code + and returns a function pointer suitable for executing it. +
++ The llvm.init.trampoline intrinsic takes three arguments, all + pointers. The tramp argument must point to a sufficiently large + and sufficiently aligned block of memory; this memory is written to by the + intrinsic. Note that the size and the alignment are target-specific - LLVM + currently provides no portable way of determining them, so a front-end that + generates this intrinsic needs to have some target-specific knowledge. + The func argument must hold a function bitcast to an i8*. +
++ The block of memory pointed to by tramp is filled with target + dependent code, turning it into a function. A pointer to this function is + returned, but needs to be bitcast to an + appropriate function pointer type + before being called. The new function's signature is the same as that of + func with any arguments marked with the nest attribute + removed. At most one such nest argument is allowed, and it must be + of pointer type. Calling the new function is equivalent to calling + func with the same argument list, but with nval used for the + missing nest argument. If, after calling + llvm.init.trampoline, the memory pointed to by tramp is + modified, then the effect of any later call to the returned function pointer is + undefined. +
++ These intrinsic functions expand the "universal IR" of LLVM to represent + hardware constructs for atomic operations and memory synchronization. This + provides an interface to the hardware, not an interface to the programmer. It + is aimed at a low enough level to allow any programming models or APIs which + need atomic behaviors to map cleanly onto it. It is also modeled primarily on + hardware behavior. Just as hardware provides a "universal IR" for source + languages, it also provides a starting point for developing a "universal" + atomic operation and synchronization IR. +
++ These do not form an API such as high-level threading libraries, + software transaction memory systems, atomic primitives, and intrinsic + functions as found in BSD, GNU libc, atomic_ops, APR, and other system and + application libraries. The hardware interface provided by LLVM should allow + a clean implementation of all of these APIs and parallel programming models. + No one model or paradigm should be selected above others unless the hardware + itself ubiquitously does so. + +
++ declare void @llvm.memory.barrier( i1 <ll>, i1 <ls>, i1 <sl>, i1 <ss>, + i1 <device> ) + ++
+ The llvm.memory.barrier intrinsic guarantees ordering between + specific pairs of memory access types. +
++ The llvm.memory.barrier intrinsic requires five boolean arguments. + The first four arguments enables a specific barrier as listed below. The fith + argument specifies that the barrier applies to io or device or uncached memory. + +
++ This intrinsic causes the system to enforce some ordering constraints upon + the loads and stores of the program. This barrier does not indicate + when any events will occur, it only enforces an order in + which they occur. For any of the specified pairs of load and store operations + (f.ex. load-load, or store-load), all of the first operations preceding the + barrier will complete before any of the second operations succeeding the + barrier begin. Specifically the semantics for each pairing is as follows: +
++ These semantics are applied with a logical "and" behavior when more than one + is enabled in a single memory barrier intrinsic. +
++ Backends may implement stronger barriers than those requested when they do not + support as fine grained a barrier as requested. Some architectures do not + need all types of barriers and on such architectures, these become noops. +
+
+ %ptr = malloc i32
+ store i32 4, %ptr
+
+ %result1 = load i32* %ptr ; yields {i32}:result1 = 4
+ call void @llvm.memory.barrier( i1 false, i1 true, i1 false, i1 false )
+ ; guarantee the above finishes
+ store i32 8, %ptr ; before this begins
+
+ + This is an overloaded intrinsic. You can use llvm.atomic.lcs on any + integer bit width. Not all targets support all bit widths however.
+ ++ declare i8 @llvm.atomic.lcs.i8( i8* <ptr>, i8 <cmp>, i8 <val> ) + declare i16 @llvm.atomic.lcs.i16( i16* <ptr>, i16 <cmp>, i16 <val> ) + declare i32 @llvm.atomic.lcs.i32( i32* <ptr>, i32 <cmp>, i32 <val> ) + declare i64 @llvm.atomic.lcs.i64( i64* <ptr>, i64 <cmp>, i64 <val> ) + ++
+ This loads a value in memory and compares it to a given value. If they are + equal, it stores a new value into the memory. +
++ The llvm.atomic.lcs intrinsic takes three arguments. The result as + well as both cmp and val must be integer values with the + same bit width. The ptr argument must be a pointer to a value of + this integer type. While any bit width integer may be used, targets may only + lower representations they support in hardware. + +
++ This entire intrinsic must be executed atomically. It first loads the value + in memory pointed to by ptr and compares it with the value + cmp. If they are equal, val is stored into the memory. The + loaded value is yielded in all cases. This provides the equivalent of an + atomic compare-and-swap operation within the SSA framework. +
+
+ %ptr = malloc i32
+ store i32 4, %ptr
+
+ %val1 = add i32 4, 4
+ %result1 = call i32 @llvm.atomic.lcs.i32( i32* %ptr, i32 4, %val1 )
+ ; yields {i32}:result1 = 4
+ %stored1 = icmp eq i32 %result1, 4 ; yields {i1}:stored1 = true
+ %memval1 = load i32* %ptr ; yields {i32}:memval1 = 8
+
+ %val2 = add i32 1, 1
+ %result2 = call i32 @llvm.atomic.lcs.i32( i32* %ptr, i32 5, %val2 )
+ ; yields {i32}:result2 = 8
+ %stored2 = icmp eq i32 %result2, 5 ; yields {i1}:stored2 = false
+
+ %memval2 = load i32* %ptr ; yields {i32}:memval2 = 8
+
+ + This is an overloaded intrinsic. You can use llvm.atomic.swap on any + integer bit width. Not all targets support all bit widths however.
++ declare i8 @llvm.atomic.swap.i8( i8* <ptr>, i8 <val> ) + declare i16 @llvm.atomic.swap.i16( i16* <ptr>, i16 <val> ) + declare i32 @llvm.atomic.swap.i32( i32* <ptr>, i32 <val> ) + declare i64 @llvm.atomic.swap.i64( i64* <ptr>, i64 <val> ) + ++
+ This intrinsic loads the value stored in memory at ptr and yields + the value from memory. It then stores the value in val in the memory + at ptr. +
++ The llvm.atomic.ls intrinsic takes two arguments. Both the + val argument and the result must be integers of the same bit width. + The first argument, ptr, must be a pointer to a value of this + integer type. The targets may only lower integer representations they + support. +
++ This intrinsic loads the value pointed to by ptr, yields it, and + stores val back into ptr atomically. This provides the + equivalent of an atomic swap operation within the SSA framework. + +
+
+ %ptr = malloc i32
+ store i32 4, %ptr
+
+ %val1 = add i32 4, 4
+ %result1 = call i32 @llvm.atomic.swap.i32( i32* %ptr, i32 %val1 )
+ ; yields {i32}:result1 = 4
+ %stored1 = icmp eq i32 %result1, 4 ; yields {i1}:stored1 = true
+ %memval1 = load i32* %ptr ; yields {i32}:memval1 = 8
+
+ %val2 = add i32 1, 1
+ %result2 = call i32 @llvm.atomic.swap.i32( i32* %ptr, i32 %val2 )
+ ; yields {i32}:result2 = 8
+
+ %stored2 = icmp eq i32 %result2, 8 ; yields {i1}:stored2 = true
+ %memval2 = load i32* %ptr ; yields {i32}:memval2 = 2
+
+ + This is an overloaded intrinsic. You can use llvm.atomic.las on any + integer bit width. Not all targets support all bit widths however.
++ declare i8 @llvm.atomic.las.i8.( i8* <ptr>, i8 <delta> ) + declare i16 @llvm.atomic.las.i16.( i16* <ptr>, i16 <delta> ) + declare i32 @llvm.atomic.las.i32.( i32* <ptr>, i32 <delta> ) + declare i64 @llvm.atomic.las.i64.( i64* <ptr>, i64 <delta> ) + ++
+ This intrinsic adds delta to the value stored in memory at + ptr. It yields the original value at ptr. +
++ + The intrinsic takes two arguments, the first a pointer to an integer value + and the second an integer value. The result is also an integer value. These + integer types can have any bit width, but they must all have the same bit + width. The targets may only lower integer representations they support. +
++ This intrinsic does a series of operations atomically. It first loads the + value stored at ptr. It then adds delta, stores the result + to ptr. It yields the original value stored at ptr. +
+ +
+ %ptr = malloc i32
+ store i32 4, %ptr
+ %result1 = call i32 @llvm.atomic.las.i32( i32* %ptr, i32 4 )
+ ; yields {i32}:result1 = 4
+ %result2 = call i32 @llvm.atomic.las.i32( i32* %ptr, i32 2 )
+ ; yields {i32}:result2 = 8
+ %result3 = call i32 @llvm.atomic.las.i32( i32* %ptr, i32 5 )
+ ; yields {i32}:result3 = 10
+ %memval = load i32* %ptr ; yields {i32}:memval1 = 15
+
+ This class of intrinsics is designed to be generic and has + no specific purpose.
++ declare void @llvm.var.annotation(i8* <val>, i8* <str>, i8* <str>, i32 <int> ) ++ +
+ The 'llvm.var.annotation' intrinsic +
+ ++ The first argument is a pointer to a value, the second is a pointer to a + global string, the third is a pointer to a global string which is the source + file name, and the last argument is the line number. +
+ ++ This intrinsic allows annotation of local variables with arbitrary strings. + This can be useful for special purpose optimizations that want to look for these + annotations. These have no other defined use, they are ignored by code + generation and optimization. +
+This is an overloaded intrinsic. You can use 'llvm.annotation' on + any integer bit width. +
++ declare i8 @llvm.annotation.i8(i8 <val>, i8* <str>, i8* <str>, i32 <int> ) + declare i16 @llvm.annotation.i16(i16 <val>, i8* <str>, i8* <str>, i32 <int> ) + declare i32 @llvm.annotation.i32(i32 <val>, i8* <str>, i8* <str>, i32 <int> ) + declare i64 @llvm.annotation.i64(i64 <val>, i8* <str>, i8* <str>, i32 <int> ) + declare i256 @llvm.annotation.i256(i256 <val>, i8* <str>, i8* <str>, i32 <int> ) ++ +
+ The 'llvm.annotation' intrinsic. +
+ ++ The first argument is an integer value (result of some expression), + the second is a pointer to a global string, the third is a pointer to a global + string which is the source file name, and the last argument is the line number. + It returns the value of the first argument. +
+ ++ This intrinsic allows annotations to be put on arbitrary expressions + with arbitrary strings. This can be useful for special purpose optimizations + that want to look for these annotations. These have no other defined use, they + are ignored by code generation and optimization. +
+ declare void @llvm.trap() ++ +
+ The 'llvm.trap' intrinsic +
+ ++ None +
+ ++ This intrinsics is lowered to the target dependent trap instruction. If the + target does not have a trap instruction, this intrinsic will be lowered to the + call of the abort() function. +
+NOTE: This document is a work in progress!
+ +| - A - | |||||||
|---|---|---|---|---|---|---|---|
| ADCE | +|||||||
| - B - | |||||||
| BURS | +|||||||
| - C - | |||||||
| CSE | +|||||||
| - D - | |||||||
| DAG | +Derived Pointer | +DSA | +DSE | +||||
| - G - | GC | + +||||||
| - I - | |||||||
| IPA | +IPO | +ISel | +|||||
| - L - | |||||||
| LCSSA | +LICM | +Load-VN | +|||||
| - O - | |||||||
| Object Pointer | +|||||||
| - P - | |||||||
| PRE | +|||||||
| - R - | |||||||
| Reassociation | +Root | +||||||
| - S - | |||||||
| Safe Point | +SCC | +SCCP | +SDISel | +SRoA | +Stack Map | +||
+ LLVM features powerful intermodular optimizations which can be used at link + time. Link Time Optimization (LTO) is another name for intermodular optimization + when performed during the link stage. This document describes the interface + and design between the LTO optimizer and the linker.
++ The LLVM Link Time Optimizer provides complete transparency, while doing + intermodular optimization, in the compiler tool chain. Its main goal is to let + the developer take advantage of intermodular optimizations without making any + significant changes to the developer's makefiles or build system. This is + achieved through tight integration with the linker. In this model, the linker + treates LLVM bitcode files like native object files and allows mixing and + matching among them. The linker uses libLTO, a shared + object, to handle LLVM bitcode files. This tight integration between + the linker and LLVM optimizer helps to do optimizations that are not possible + in other models. The linker input allows the optimizer to avoid relying on + conservative escape analysis. +
+The following example illustrates the advantages of LTO's integrated + approach and clean interface. This example requires a system linker which + supports LTO through the interface described in this document. Here, + llvm-gcc transparently invokes system linker.
+
+ --- a.h ---
+ extern int foo1(void);
+ extern void foo2(void);
+ extern void foo4(void);
+ --- a.c ---
+ #include "a.h"
+
+ static signed int i = 0;
+
+ void foo2(void) {
+ i = -1;
+ }
+
+ static int foo3() {
+ foo4();
+ return 10;
+ }
+
+ int foo1(void) {
+ int data = 0;
+
+ if (i < 0) { data = foo3(); }
+
+ data = data + 42;
+ return data;
+ }
+
+ --- main.c ---
+ #include <stdio.h>
+ #include "a.h"
+
+ void foo4(void) {
+ printf ("Hi\n");
+ }
+
+ int main() {
+ return foo1();
+ }
+
+ --- command lines ---
+ $ llvm-gcc --emit-llvm -c a.c -o a.o # <-- a.o is LLVM bitcode file
+ $ llvm-gcc -c main.c -o main.o # <-- main.o is native object file
+ $ llvm-gcc a.o main.o -o main # <-- standard link command without any modifications
+ In this example, the linker recognizes that foo2() is an + externally visible symbol defined in LLVM bitcode file. The linker completes + its usual symbol resolution + pass and finds that foo2() is not used anywhere. This information + is used by the LLVM optimizer and it removes foo2(). As soon as + foo2() is removed, the optimizer recognizes that condition + i < 0 is always false, which means foo3() is never + used. Hence, the optimizer removes foo3(), also. And this in turn, + enables linker to remove foo4(). This example illustrates the + advantage of tight integration with the linker. Here, the optimizer can not + remove foo3() without the linker's input. +
+The linker collects information about symbol defininitions and uses in + various link objects which is more accurate than any information collected + by other tools during typical build cycles. The linker collects this + information by looking at the definitions and uses of symbols in native .o + files and using symbol visibility information. The linker also uses + user-supplied information, such as a list of exported symbols. LLVM + optimizer collects control flow information, data flow information and knows + much more about program structure from the optimizer's point of view. + Our goal is to take advantage of tight intergration between the linker and + the optimizer by sharing this information during various linking phases. +
+The linker first reads all object files in natural order and collects + symbol information. This includes native object files as well as LLVM bitcode + files. To minimize the cost to the linker in the case that all .o files + are native object files, the linker only calls lto_module_create() + when a supplied object file is found to not be a native object file. If + lto_module_create() returns that the file is an LLVM bitcode file, + the linker + then iterates over the module using lto_module_get_symbol_name() and + lto_module_get_symbol_attribute() to get all symbols defined and + referenced. + This information is added to the linker's global symbol table. +
+The lto* functions are all implemented in a shared object libLTO. This + allows the LLVM LTO code to be updated independently of the linker tool. + On platforms that support it, the shared object is lazily loaded. +
+In this stage, the linker resolves symbols using global symbol table. + It may report undefined symbol errors, read archive members, replace + weak symbols, etc. The linker is able to do this seamlessly even though it + does not know the exact content of input LLVM bitcode files. If dead code + stripping is enabled then the linker collects the list of live symbols. +
+After symbol resolution, the linker tells the LTO shared object which + symbols are needed by native object files. In the example above, the linker + reports that only foo1() is used by native object files using + lto_codegen_add_must_preserve_symbol(). Next the linker invokes + the LLVM optimizer and code generators using lto_codegen_compile() + which returns a native object file creating by merging the LLVM bitcode files + and applying various optimization passes. +
+In this phase, the linker reads optimized a native object file and + updates the internal global symbol table to reflect any changes. The linker + also collects information about any changes in use of external symbols by + LLVM bitcode files. In the examle above, the linker notes that + foo4() is not used any more. If dead code stripping is enabled then + the linker refreshes the live symbol information appropriately and performs + dead code stripping.
+After this phase, the linker continues linking as if it never saw LLVM + bitcode files.
+libLTO is a shared object that is part of the LLVM tools, and + is intended for use by a linker. libLTO provides an abstract C + interface to use the LLVM interprocedural optimizer without exposing details + of LLVM's internals. The intention is to keep the interface as stable as + possible even when the LLVM optimizer continues to evolve. It should even + be possible for a completely different compilation technology to provide + a different libLTO that works with their object files and the standard + linker tool.
+A non-native object file is handled via an lto_module_t. + The following functions allow the linker to check if a file (on disk + or in a memory buffer) is a file which libLTO can process:
+ lto_module_is_object_file(const char*) + lto_module_is_object_file_for_target(const char*, const char*) + lto_module_is_object_file_in_memory(const void*, size_t) + lto_module_is_object_file_in_memory_for_target(const void*, size_t, const char*)+ If the object file can be processed by libLTO, the linker creates a + lto_module_t by using one of
+ lto_module_create(const char*) + lto_module_create_from_memory(const void*, size_t)+ and when done, the handle is released via
+ lto_module_dispose(lto_module_t)+ The linker can introspect the non-native object file by getting the number + of symbols and getting the name and attributes of each symbol via:
+ lto_module_get_num_symbols(lto_module_t) + lto_module_get_symbol_name(lto_module_t, unsigned int) + lto_module_get_symbol_attribute(lto_module_t, unsigned int)+ The attributes of a symbol include the alignment, visibility, and kind. + +
Once the linker has loaded each non-native object files into an + lto_module_t, it can request libLTO to process them all and + generate a native object file. This is done in a couple of steps. + First a code generator is created with:
+ lto_codegen_create()+ then each non-native object file is added to the code generator with:
+ lto_codegen_add_module(lto_code_gen_t, lto_module_t)+ The linker then has the option of setting some codegen options. Whether + or not to generate DWARF debug info is set with:
+ lto_codegen_set_debug_model(lto_code_gen_t)+ Which kind of position independence is set with:
+ lto_codegen_set_pic_model(lto_code_gen_t)+ And each symbol that is referenced by a native object file or otherwise + must not be optimized away is set with:
+ lto_codegen_add_must_preserve_symbol(lto_code_gen_t, const char*)+ After all these settings are done, the linker requests that a native + object file be created from the modules with the settings using: + lto_codegen_compile(lto_code_gen_t, size*) + which returns a pointer to a buffer containing the generated native + object file. The linker then parses that and links it with the rest + of the native object files. +