@@ -50,34 +50,34 @@ Setting up the build environment
50
50
First, configure and build LLVM. Next, you need to create a new directory
51
51
somewhere in the LLVM source base. For this example, we'll assume that you
52
52
made ``lib/Transforms/Hello ``. Finally, you must set up a build script
53
- (`` Makefile ``) that will compile the source code for the new pass. To do this,
54
- copy the following into ``Makefile ``:
53
+ that will compile the source code for the new pass. To do this,
54
+ copy the following into ``CMakeLists.txt ``:
55
55
56
- .. code-block :: make
56
+ .. code-block :: cmake
57
57
58
- # Makefile for hello pass
58
+ add_llvm_loadable_module( LLVMHello
59
+ Hello.cpp
60
+
61
+ PLUGIN_TOOL
62
+ opt
63
+ )
59
64
60
- # Path to top level of LLVM hierarchy
61
- LEVEL = ../../..
65
+ and the following line into ``lib/Transforms/CMakeLists.txt ``:
62
66
63
- # Name of the library to build
64
- LIBRARYNAME = Hello
67
+ .. code-block :: cmake
65
68
66
- # Make the shared library become a loadable module so the tools can
67
- # dlopen/dlsym on the resulting library.
68
- LOADABLE_MODULE = 1
69
+ add_subdirectory(Hello)
69
70
70
- # Include the makefile implementation stuff
71
- include $(LEVEL)/Makefile.common
71
+ (Note that there is already a directory named ``Hello `` with a sample "Hello"
72
+ pass; you may play with it -- in which case you don't need to modify any
73
+ ``CMakeLists.txt `` files -- or, if you want to create everything from scratch,
74
+ use another name.)
72
75
73
- This makefile specifies that all of the ``.cpp `` files in the current directory
74
- are to be compiled and linked together into a shared object
75
- ``$(LEVEL)/Debug+Asserts/lib/Hello.so `` that can be dynamically loaded by the
76
- :program: `opt ` or :program: `bugpoint ` tools via their :option: `-load ` options.
77
- If your operating system uses a suffix other than ``.so `` (such as Windows or Mac
78
- OS X), the appropriate extension will be used.
79
-
80
- If you are used CMake to build LLVM, see :ref: `cmake-out-of-source-pass `.
76
+ This build script specifies that ``Hello.cpp `` file in the current directory
77
+ is to be compiled and linked into a shared object ``$(LEVEL)/lib/LLVMHello.so `` that
78
+ can be dynamically loaded by the :program: `opt ` tool via its :option: `-load `
79
+ option. If your operating system uses a suffix other than ``.so `` (such as
80
+ Windows or Mac OS X), the appropriate extension will be used.
81
81
82
82
Now that we have the build scripts set up, we just need to write the code for
83
83
the pass itself.
@@ -143,12 +143,12 @@ to avoid using expensive C++ runtime information.
143
143
144
144
.. code-block :: c++
145
145
146
- bool runOnFunction(Function &F) override {
147
- errs() << "Hello: ";
148
- errs().write_escaped(F.getName()) << " \n " ;
149
- return false;
150
- }
151
- }; // end of struct Hello
146
+ bool runOnFunction(Function &F) override {
147
+ errs() << "Hello: ";
148
+ errs().write_escaped(F.getName()) << ' \n ' ;
149
+ return false;
150
+ }
151
+ }; // end of struct Hello
152
152
} // end of anonymous namespace
153
153
154
154
We declare a :ref: `runOnFunction <writing-an-llvm-pass-runOnFunction >` method,
@@ -180,31 +180,33 @@ As a whole, the ``.cpp`` file looks like:
180
180
181
181
.. code-block :: c++
182
182
183
- #include "llvm/Pass.h"
184
- #include "llvm/IR/Function.h"
185
- #include "llvm/Support/raw_ostream.h"
186
-
187
- using namespace llvm;
188
-
189
- namespace {
190
- struct Hello : public FunctionPass {
191
- static char ID;
192
- Hello() : FunctionPass(ID) {}
193
-
194
- bool runOnFunction(Function &F) override {
195
- errs() << "Hello: ";
196
- errs().write_escaped(F.getName()) << '\n ';
197
- return false;
198
- }
199
- };
183
+ #include "llvm/Pass.h"
184
+ #include "llvm/IR/Function.h"
185
+ #include "llvm/Support/raw_ostream.h"
186
+
187
+ using namespace llvm;
188
+
189
+ namespace {
190
+ struct Hello : public FunctionPass {
191
+ static char ID;
192
+ Hello() : FunctionPass(ID) {}
193
+
194
+ bool runOnFunction(Function &F) override {
195
+ errs() << "Hello: ";
196
+ errs().write_escaped(F.getName()) << '\n ';
197
+ return false;
200
198
}
201
-
202
- char Hello::ID = 0;
203
- static RegisterPass<Hello> X("hello", "Hello World Pass", false, false);
199
+ }; // end of struct Hello
200
+ } // end of anonymous namespace
201
+
202
+ char Hello::ID = 0;
203
+ static RegisterPass<Hello> X("hello", "Hello World Pass",
204
+ false /* Only looks at CFG */,
205
+ false / * Analysis Pass */);
204
206
205
207
Now that it's all together, compile the file with a simple "``gmake ``" command
206
208
from the top level of your build directory and you should get a new file
207
- "``Debug+Asserts/ lib/Hello .so ``". Note that everything in this file is
209
+ "``lib/LLVMHello .so ``". Note that everything in this file is
208
210
contained in an anonymous namespace --- this reflects the fact that passes
209
211
are self contained units that do not need external interfaces (although they
210
212
can have them) to be useful.
@@ -224,7 +226,7 @@ will work):
224
226
225
227
.. code-block :: console
226
228
227
- $ opt -load ../../Debug+Asserts/ lib/Hello .so -hello < hello.bc > /dev/null
229
+ $ opt -load lib/LLVMHello .so -hello < hello.bc > /dev/null
228
230
Hello: __main
229
231
Hello: puts
230
232
Hello: main
@@ -241,20 +243,20 @@ To see what happened to the other string you registered, try running
241
243
242
244
.. code-block :: console
243
245
244
- $ opt -load ../../Debug+Asserts/ lib/Hello .so -help
245
- OVERVIEW: llvm .bc -> .bc modular optimizer
246
+ $ opt -load lib/LLVMHello .so -help
247
+ OVERVIEW: llvm .bc -> .bc modular optimizer and analysis printer
246
248
247
- USAGE: opt [options] <input bitcode>
249
+ USAGE: opt [subcommand] [ options] <input bitcode file >
248
250
249
251
OPTIONS:
250
252
Optimizations available:
251
253
...
252
- -globalopt - Global Variable Optimizer
253
- -globalsmodref-aa - Simple mod/ref analysis for globals
254
+ -guard-widening - Widen guards
254
255
-gvn - Global Value Numbering
256
+ -gvn-hoist - Early GVN Hoisting of Expressions
255
257
-hello - Hello World Pass
256
258
-indvars - Induction Variable Simplification
257
- -inline - Function Integration/Inlining
259
+ -inferattrs - Infer set function attributes
258
260
...
259
261
260
262
The pass name gets added as the information string for your pass, giving some
@@ -268,21 +270,20 @@ you queue up. For example:
268
270
269
271
.. code-block :: console
270
272
271
- $ opt -load ../../Debug+Asserts/ lib/Hello .so -hello -time-passes < hello.bc > /dev/null
273
+ $ opt -load lib/LLVMHello .so -hello -time-passes < hello.bc > /dev/null
272
274
Hello: __main
273
275
Hello: puts
274
276
Hello: main
275
- ============================================================================ ===
277
+ ===------------------------------------------------------------------------- ===
276
278
... Pass execution timing report ...
277
- ===============================================================================
278
- Total Execution Time: 0.02 seconds (0.0479059 wall clock)
279
-
280
- ---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Pass Name ---
281
- 0.0100 (100.0%) 0.0000 ( 0.0%) 0.0100 ( 50.0%) 0.0402 ( 84.0%) Bitcode Writer
282
- 0.0000 ( 0.0%) 0.0100 (100.0%) 0.0100 ( 50.0%) 0.0031 ( 6.4%) Dominator Set Construction
283
- 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0013 ( 2.7%) Module Verifier
284
- 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0033 ( 6.9%) Hello World Pass
285
- 0.0100 (100.0%) 0.0100 (100.0%) 0.0200 (100.0%) 0.0479 (100.0%) TOTAL
279
+ ===-------------------------------------------------------------------------===
280
+ Total Execution Time: 0.0007 seconds (0.0005 wall clock)
281
+
282
+ ---User Time--- --User+System-- ---Wall Time--- --- Name ---
283
+ 0.0004 ( 55.3%) 0.0004 ( 55.3%) 0.0004 ( 75.7%) Bitcode Writer
284
+ 0.0003 ( 44.7%) 0.0003 ( 44.7%) 0.0001 ( 13.6%) Hello World Pass
285
+ 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 10.7%) Module Verifier
286
+ 0.0007 (100.0%) 0.0007 (100.0%) 0.0005 (100.0%) Total
286
287
287
288
As you can see, our implementation above is pretty fast. The additional
288
289
passes listed are automatically inserted by the :program: `opt ` tool to verify
@@ -964,14 +965,14 @@ just does a few simple checks that don't require significant analysis to
964
965
compute (such as: two different globals can never alias each other, etc).
965
966
Passes that use the `AliasAnalysis
966
967
<http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html> `_ interface (for
967
- example the `gcse <http://llvm.org/doxygen/structGCSE .html >`_ pass), do not
968
+ example the `gvn <http://llvm.org/doxygen/classllvm_1_1GVN .html >`_ pass), do not
968
969
care which implementation of alias analysis is actually provided, they just use
969
970
the designated interface.
970
971
971
972
From the user's perspective, commands work just like normal. Issuing the
972
- command ``opt -gcse ... `` will cause the ``basicaa `` class to be instantiated
973
- and added to the pass sequence. Issuing the command ``opt -somefancyaa -gcse
974
- ... `` will cause the ``gcse `` pass to use the ``somefancyaa `` alias analysis
973
+ command ``opt -gvn ... `` will cause the ``basicaa `` class to be instantiated
974
+ and added to the pass sequence. Issuing the command ``opt -somefancyaa -gvn
975
+ ... `` will cause the ``gvn `` pass to use the ``somefancyaa `` alias analysis
975
976
(which doesn't actually exist, it's just a hypothetical example) instead.
976
977
977
978
.. _writing-an-llvm-pass-RegisterAnalysisGroup :
@@ -1092,74 +1093,69 @@ information about all of the variants of the ``--debug-pass`` option, just type
1092
1093
1093
1094
By using the --debug-pass=Structure option, for example, we can see how our
1094
1095
:ref: `Hello World <writing-an-llvm-pass-basiccode >` pass interacts with other
1095
- passes. Lets try it out with the gcse and licm passes:
1096
+ passes. Lets try it out with the gvn and licm passes:
1096
1097
1097
1098
.. code-block :: console
1098
1099
1099
- $ opt -load ../../Debug+Asserts/lib/Hello.so -gcse -licm --debug-pass=Structure < hello.bc > /dev/null
1100
- Module Pass Manager
1101
- Function Pass Manager
1102
- Dominator Set Construction
1103
- Immediate Dominators Construction
1104
- Global Common Subexpression Elimination
1105
- -- Immediate Dominators Construction
1106
- -- Global Common Subexpression Elimination
1107
- Natural Loop Construction
1108
- Loop Invariant Code Motion
1109
- -- Natural Loop Construction
1110
- -- Loop Invariant Code Motion
1100
+ $ opt -load lib/LLVMHello.so -gvn -licm --debug-pass=Structure < hello.bc > /dev/null
1101
+ ModulePass Manager
1102
+ FunctionPass Manager
1103
+ Dominator Tree Construction
1104
+ Basic Alias Analysis (stateless AA impl)
1105
+ Function Alias Analysis Results
1106
+ Memory Dependence Analysis
1107
+ Global Value Numbering
1108
+ Natural Loop Information
1109
+ Canonicalize natural loops
1110
+ Loop-Closed SSA Form Pass
1111
+ Basic Alias Analysis (stateless AA impl)
1112
+ Function Alias Analysis Results
1113
+ Scalar Evolution Analysis
1114
+ Loop Pass Manager
1115
+ Loop Invariant Code Motion
1111
1116
Module Verifier
1112
- -- Dominator Set Construction
1113
- -- Module Verifier
1114
1117
Bitcode Writer
1115
- --Bitcode Writer
1116
1118
1117
- This output shows us when passes are constructed and when the analysis results
1118
- are known to be dead (prefixed with "``-- ``"). Here we see that GCSE uses
1119
- dominator and immediate dominator information to do its job. The LICM pass
1120
- uses natural loop information, which uses dominator sets, but not immediate
1121
- dominators. Because immediate dominators are no longer useful after the GCSE
1122
- pass, it is immediately destroyed. The dominator sets are then reused to
1123
- compute natural loop information, which is then used by the LICM pass.
1119
+ This output shows us when passes are constructed.
1120
+ Here we see that GVN uses dominator tree information to do its job. The LICM pass
1121
+ uses natural loop information, which uses dominator tree as well.
1124
1122
1125
1123
After the LICM pass, the module verifier runs (which is automatically added by
1126
- the :program: `opt ` tool), which uses the dominator set to check that the
1127
- resultant LLVM code is well formed. After it finishes, the dominator set
1128
- information is destroyed, after being computed once, and shared by three
1129
- passes.
1124
+ the :program: `opt ` tool), which uses the dominator tree to check that the
1125
+ resultant LLVM code is well formed. Note that the dominator tree is computed
1126
+ once, and shared by three passes.
1130
1127
1131
1128
Lets see how this changes when we run the :ref: `Hello World
1132
1129
<writing-an-llvm-pass-basiccode>` pass in between the two passes:
1133
1130
1134
1131
.. code-block :: console
1135
1132
1136
- $ opt -load ../../Debug+Asserts/lib/Hello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null
1137
- Module Pass Manager
1138
- Function Pass Manager
1139
- Dominator Set Construction
1140
- Immediate Dominators Construction
1141
- Global Common Subexpression Elimination
1142
- -- Dominator Set Construction
1143
- -- Immediate Dominators Construction
1144
- -- Global Common Subexpression Elimination
1133
+ $ opt -load lib/LLVMHello.so -gvn -hello -licm --debug-pass=Structure < hello.bc > /dev/null
1134
+ ModulePass Manager
1135
+ FunctionPass Manager
1136
+ Dominator Tree Construction
1137
+ Basic Alias Analysis (stateless AA impl)
1138
+ Function Alias Analysis Results
1139
+ Memory Dependence Analysis
1140
+ Global Value Numbering
1145
1141
Hello World Pass
1146
- -- Hello World Pass
1147
- Dominator Set Construction
1148
- Natural Loop Construction
1149
- Loop Invariant Code Motion
1150
- -- Natural Loop Construction
1151
- -- Loop Invariant Code Motion
1142
+ Dominator Tree Construction
1143
+ Natural Loop Information
1144
+ Canonicalize natural loops
1145
+ Loop-Closed SSA Form Pass
1146
+ Basic Alias Analysis (stateless AA impl)
1147
+ Function Alias Analysis Results
1148
+ Scalar Evolution Analysis
1149
+ Loop Pass Manager
1150
+ Loop Invariant Code Motion
1152
1151
Module Verifier
1153
- -- Dominator Set Construction
1154
- -- Module Verifier
1155
1152
Bitcode Writer
1156
- --Bitcode Writer
1157
1153
Hello: __main
1158
1154
Hello: puts
1159
1155
Hello: main
1160
1156
1161
1157
Here we see that the :ref: `Hello World <writing-an-llvm-pass-basiccode >` pass
1162
- has killed the Dominator Set pass, even though it doesn't modify the code at
1158
+ has killed the Dominator Tree pass, even though it doesn't modify the code at
1163
1159
all! To fix this, we need to add the following :ref: `getAnalysisUsage
1164
1160
<writing-an-llvm-pass-getAnalysisUsage>` method to our pass:
1165
1161
@@ -1174,26 +1170,26 @@ Now when we run our pass, we get this output:
1174
1170
1175
1171
.. code-block :: console
1176
1172
1177
- $ opt -load ../../Debug+Asserts/ lib/Hello .so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null
1178
- Pass Arguments: -gcse -hello -licm
1179
- Module Pass Manager
1180
- Function Pass Manager
1181
- Dominator Set Construction
1182
- Immediate Dominators Construction
1183
- Global Common Subexpression Elimination
1184
- -- Immediate Dominators Construction
1185
- -- Global Common Subexpression Elimination
1173
+ $ opt -load lib/LLVMHello .so -gvn -hello -licm --debug-pass=Structure < hello.bc > /dev/null
1174
+ Pass Arguments: -gvn -hello -licm
1175
+ ModulePass Manager
1176
+ FunctionPass Manager
1177
+ Dominator Tree Construction
1178
+ Basic Alias Analysis (stateless AA impl)
1179
+ Function Alias Analysis Results
1180
+ Memory Dependence Analysis
1181
+ Global Value Numbering
1186
1182
Hello World Pass
1187
- -- Hello World Pass
1188
- Natural Loop Construction
1189
- Loop Invariant Code Motion
1190
- -- Loop Invariant Code Motion
1191
- -- Natural Loop Construction
1183
+ Natural Loop Information
1184
+ Canonicalize natural loops
1185
+ Loop-Closed SSA Form Pass
1186
+ Basic Alias Analysis (stateless AA impl)
1187
+ Function Alias Analysis Results
1188
+ Scalar Evolution Analysis
1189
+ Loop Pass Manager
1190
+ Loop Invariant Code Motion
1192
1191
Module Verifier
1193
- -- Dominator Set Construction
1194
- -- Module Verifier
1195
1192
Bitcode Writer
1196
- --Bitcode Writer
1197
1193
Hello: __main
1198
1194
Hello: puts
1199
1195
Hello: main
0 commit comments