Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <html><head><title>Writing an LLVM Pass</title></head> |
| 3 | |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 4 | <body bgcolor=white> |
| 5 | |
| 6 | <table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0> |
| 7 | <tr><td> <font size=+3 color="#EEEEFF" face="Georgia,Palatino,Times,Roman"><b>Writing an LLVM Pass</b></font></td> |
| 8 | </tr></table> |
| 9 | |
| 10 | |
| 11 | <ol> |
| 12 | <li><a href="#introduction">Introduction - What is a pass?</a> |
| 13 | <li><a href="#quickstart">Quick Start - Writing hello world</a> |
| 14 | <ul> |
| 15 | <li><a href="#makefile">Setting up the build environment</a> |
| 16 | <li><a href="#basiccode">Basic code required</a> |
| 17 | <li><a href="#running">Running a pass with <tt>opt</tt> |
| 18 | or <tt>analyze</tt></a> |
| 19 | </ul> |
| 20 | <li><a href="#passtype">Pass classes and requirements</a> |
| 21 | <ul> |
Chris Lattner | f004f9a | 2002-09-25 22:31:38 +0000 | [diff] [blame] | 22 | <li><a href="#ImmutablePass">The <tt>ImmutablePass</tt> class</a> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 23 | <li><a href="#Pass">The <tt>Pass</tt> class</a> |
| 24 | <ul> |
| 25 | <li><a href="#run">The <tt>run</tt> method</a> |
| 26 | </ul> |
| 27 | <li><a href="#FunctionPass">The <tt>FunctionPass</tt> class</a> |
| 28 | <ul> |
Chris Lattner | d0713f9 | 2002-09-12 17:06:43 +0000 | [diff] [blame] | 29 | <li><a href="#doInitialization_mod">The <tt>doInitialization(Module |
| 30 | &)</tt> method</a> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 31 | <li><a href="#runOnFunction">The <tt>runOnFunction</tt> method</a> |
Chris Lattner | d0713f9 | 2002-09-12 17:06:43 +0000 | [diff] [blame] | 32 | <li><a href="#doFinalization_mod">The <tt>doFinalization(Module |
| 33 | &)</tt> method</a> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 34 | </ul> |
| 35 | <li><a href="#BasicBlockPass">The <tt>BasicBlockPass</tt> class</a> |
| 36 | <ul> |
Chris Lattner | d0713f9 | 2002-09-12 17:06:43 +0000 | [diff] [blame] | 37 | <li><a href="#doInitialization_fn">The <tt>doInitialization(Function |
| 38 | &)</tt> method</a> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 39 | <li><a href="#runOnBasicBlock">The <tt>runOnBasicBlock</tt> method</a> |
Chris Lattner | d0713f9 | 2002-09-12 17:06:43 +0000 | [diff] [blame] | 40 | <li><a href="#doFinalization_fn">The <tt>doFinalization(Function |
| 41 | &)</tt> method</a> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 42 | </ul> |
Brian Gaeke | cab8b6f | 2003-07-17 18:53:20 +0000 | [diff] [blame^] | 43 | <li><a href="#MachineFunctionPass">The <tt>MachineFunctionPass</tt> |
| 44 | class</a> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 45 | </ul> |
| 46 | <li><a href="#registration">Pass Registration</a> |
| 47 | <ul> |
| 48 | <li><a href="#print">The <tt>print</tt> method</a> |
| 49 | </ul> |
| 50 | <li><a href="#interaction">Specifying interactions between passes</a> |
| 51 | <ul> |
| 52 | <li><a href="#getAnalysisUsage">The <tt>getAnalysisUsage</tt> method</a> |
| 53 | <li><a href="#getAnalysis">The <tt>getAnalysis</tt> method</a> |
| 54 | </ul> |
Chris Lattner | 7991070 | 2002-08-22 19:21:04 +0000 | [diff] [blame] | 55 | <li><a href="#analysisgroup">Implementing Analysis Groups</a> |
| 56 | <ul> |
| 57 | <li><a href="#agconcepts">Analysis Group Concepts</a> |
| 58 | <li><a href="#registerag">Using <tt>RegisterAnalysisGroup</tt></a> |
| 59 | </ul> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 60 | <li><a href="#passmanager">What PassManager does</a> |
| 61 | <ul> |
| 62 | <li><a href="#releaseMemory">The <tt>releaseMemory</tt> method</a> |
| 63 | </ul> |
Chris Lattner | 480e2ef | 2002-09-06 02:02:58 +0000 | [diff] [blame] | 64 | <li><a href="#debughints">Using GDB with dynamically loaded passes</a> |
| 65 | <ul> |
| 66 | <li><a href="#breakpoint">Setting a breakpoint in your pass |
| 67 | <li><a href="#debugmisc">Miscellaneous Problems |
| 68 | </ul> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 69 | <li><a href="#future">Future extensions planned</a> |
| 70 | <ul> |
| 71 | <li><a href="#SMP">Multithreaded LLVM</a> |
| 72 | <li><a href="#ModuleSource">A new <tt>ModuleSource</tt> interface</a> |
| 73 | <li><a href="#PassFunctionPass"><tt>Pass</tt>'s requiring <tt>FunctionPass</tt>'s</a> |
| 74 | </ul> |
Chris Lattner | 38c633d | 2002-08-08 20:23:41 +0000 | [diff] [blame] | 75 | |
| 76 | <p><b>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></b><p> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 77 | </ol><p> |
| 78 | |
| 79 | |
| 80 | |
| 81 | <!-- *********************************************************************** --> |
| 82 | <table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0> |
| 83 | <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b> |
| 84 | <a name="introduction">Introduction - What is a pass? |
| 85 | </b></font></td></tr></table><ul> |
| 86 | <!-- *********************************************************************** --> |
| 87 | |
| 88 | The LLVM Pass Framework is an important part of the LLVM system, because LLVM |
| 89 | passes are where the interesting parts of the compiler exist. Passes perform |
| 90 | the transformations and optimizations that make up the compiler, they build |
| 91 | the analysis results that are used by these transformations, and they are, above |
| 92 | all, a structuring technique for compiler code.<p> |
| 93 | |
| 94 | All LLVM passes are subclasses of the <tt><a |
| 95 | href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">Pass</a></tt> class, which |
| 96 | implement functionality by overriding virtual methods inherited from |
| 97 | <tt>Pass</tt>. Depending on how your pass works, you may be able to inherit |
| 98 | from the <tt><a |
| 99 | href="http://llvm.cs.uiuc.edu/doxygen/structFunctionPass.html">FunctionPass</a></tt> |
| 100 | or <tt><a |
| 101 | href="http://llvm.cs.uiuc.edu/doxygen/structBasicBlockPass.html">BasicBlockPass</a></tt>, |
| 102 | which gives the system more information about what your pass does, and how it |
| 103 | can be combined with other passes. One of the main features of the LLVM Pass |
| 104 | Framework is that it schedules passes to run in an efficient way based on the |
| 105 | constraints that your pass has.<p> |
| 106 | |
| 107 | We start by showing you how to construct a pass, everything from setting up the |
| 108 | code, to compiling, loading, and executing it. After the basics are down, more |
| 109 | advanced features are discussed.<p> |
| 110 | |
| 111 | |
| 112 | <!-- *********************************************************************** --> |
| 113 | </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0> |
| 114 | <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b> |
| 115 | <a name="quickstart">Quick Start - Writing hello world |
| 116 | </b></font></td></tr></table><ul> |
| 117 | <!-- *********************************************************************** --> |
| 118 | |
| 119 | Here we describe how to write the "hello world" of passes. The "Hello" pass is |
| 120 | designed to simply print out the name of non-external functions that exist in |
| 121 | the program being compiled. It does not modify the program at all, just |
| 122 | inspects it. The source code and files for this pass are available in the LLVM |
| 123 | source tree in the <tt>lib/Transforms/Hello</tt> directory.<p> |
| 124 | |
| 125 | |
| 126 | <!-- ======================================================================= --> |
| 127 | </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0> |
| 128 | <tr><td> </td><td width="100%"> |
| 129 | <font color="#EEEEFF" face="Georgia,Palatino"><b> |
| 130 | <a name="makefile">Setting up the build environment |
| 131 | </b></font></td></tr></table><ul> |
| 132 | |
| 133 | First thing you need to do is create a new directory somewhere in the LLVM |
| 134 | source base. For this example, we'll assume that you made |
| 135 | "<tt>lib/Transforms/Hello</tt>". The first thing you must do is set up a build |
| 136 | script (Makefile) that will compile the source code for the new pass. To do |
Chris Lattner | fd21476 | 2002-09-16 22:37:56 +0000 | [diff] [blame] | 137 | this, copy this into "<tt>Makefile</tt>":<p> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 138 | |
| 139 | </ul><hr><ul><pre> |
| 140 | # Makefile for hello pass |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 17a4c3e | 2002-08-14 20:06:13 +0000 | [diff] [blame] | 142 | # Path to top level of LLVM heirarchy |
Chris Lattner | 7ce83e5 | 2002-08-14 20:07:01 +0000 | [diff] [blame] | 143 | LEVEL = ../../.. |
Chris Lattner | 17a4c3e | 2002-08-14 20:06:13 +0000 | [diff] [blame] | 144 | |
| 145 | # Name of the library to build |
Chris Lattner | 7ce83e5 | 2002-08-14 20:07:01 +0000 | [diff] [blame] | 146 | LIBRARYNAME = hello |
Chris Lattner | 17a4c3e | 2002-08-14 20:06:13 +0000 | [diff] [blame] | 147 | |
| 148 | # Build a dynamically loadable shared object |
| 149 | SHARED_LIBRARY = 1 |
| 150 | |
| 151 | # Include the makefile implementation stuff |
| 152 | include $(LEVEL)/Makefile.common |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 153 | </pre></ul><hr><ul><p> |
| 154 | |
| 155 | This makefile specifies that all of the <tt>.cpp</tt> files in the current |
| 156 | directory are to be compiled and linked together into a |
| 157 | <tt>lib/Debug/libhello.so</tt> shared object that can be dynamically loaded by |
| 158 | the <tt>opt</tt> or <tt>analyze</tt> tools.<p> |
| 159 | |
| 160 | Now that we have the build scripts set up, we just need to write the code for |
| 161 | the pass itself.<p> |
| 162 | |
| 163 | |
| 164 | <!-- ======================================================================= --> |
| 165 | </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0> |
| 166 | <tr><td> </td><td width="100%"> |
| 167 | <font color="#EEEEFF" face="Georgia,Palatino"><b> |
| 168 | <a name="basiccode">Basic code required |
| 169 | </b></font></td></tr></table><ul> |
| 170 | |
| 171 | Now that we have a way to compile our new pass, we just have to write it. Start |
| 172 | out with:<p> |
| 173 | |
| 174 | <pre> |
| 175 | <b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Pass_8h-source.html">llvm/Pass.h</a>" |
| 176 | <b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Function_8h-source.html">llvm/Function.h</a>" |
| 177 | </pre> |
| 178 | |
| 179 | Which are needed because we are writing a <tt><a |
| 180 | href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">Pass</a></tt>, and we are |
| 181 | operating on <tt><a |
| 182 | href="http://llvm.cs.uiuc.edu/doxygen/classFunction.html">Function</a></tt>'s.<p> |
| 183 | |
| 184 | Next we have:<p> |
| 185 | |
| 186 | <pre> |
| 187 | <b>namespace</b> { |
| 188 | </pre><p> |
| 189 | |
| 190 | ... which starts out an anonymous namespace. Anonymous namespaces are to C++ |
| 191 | what the "<tt>static</tt>" keyword is to C (at global scope). It makes the |
| 192 | things declared inside of the anonymous namespace only visible to the current |
| 193 | file. If you're not familiar with them, consult a decent C++ book for more |
| 194 | information.<p> |
| 195 | |
| 196 | Next, we declare our pass itself:<p> |
| 197 | |
| 198 | <pre> |
| 199 | <b>struct</b> Hello : <b>public</b> <a href="#FunctionPass">FunctionPass</a> { |
| 200 | </pre><p> |
| 201 | |
| 202 | This declares a "<tt>Hello</tt>" class that is a subclass of <tt><a |
| 203 | href="http://llvm.cs.uiuc.edu/doxygen/structFunctionPass.html">FunctionPass</a></tt>. |
Chris Lattner | d6ea926 | 2002-09-09 03:48:46 +0000 | [diff] [blame] | 204 | The different builtin pass subclasses are described in detail <a |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 205 | href="#passtype">later</a>, but for now, know that <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s |
| 206 | operate a function at a time.<p> |
| 207 | |
| 208 | <pre> |
| 209 | <b>virtual bool</b> <a href="#runOnFunction">runOnFunction</a>(Function &F) { |
| 210 | std::cerr << "<i>Hello: </i>" << F.getName() << "\n"; |
| 211 | <b>return false</b>; |
| 212 | } |
| 213 | }; <i>// end of struct Hello</i> |
| 214 | </pre> |
| 215 | |
| 216 | We declare a "<a href="#runOnFunction"><tt>runOnFunction</tt></a>" method, which |
| 217 | overloads an abstract virtual method inherited from <a |
| 218 | href="#FunctionPass"><tt>FunctionPass</tt></a>. This is where we are supposed |
| 219 | to do our thing, so we just print out our message with the name of each |
| 220 | function.<p> |
| 221 | |
| 222 | <pre> |
| 223 | RegisterOpt<Hello> X("<i>hello</i>", "<i>Hello World Pass</i>"); |
| 224 | } <i>// end of anonymous namespace</i> |
| 225 | </pre><p> |
| 226 | |
| 227 | Lastly, we register our class <tt>Hello</tt>, giving it a command line argument |
| 228 | "<tt>hello</tt>", and a name "<tt>Hello World Pass</tt>". There are several |
| 229 | different ways of <a href="#registration">registering your pass</a>, depending |
| 230 | on what it is to be used for. For "optimizations" we use the |
| 231 | <tt>RegisterOpt</tt> template.<p> |
| 232 | |
| 233 | As a whole, the <tt>.cpp</tt> file looks like:<p> |
| 234 | |
| 235 | <pre> |
| 236 | <b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Pass_8h-source.html">llvm/Pass.h</a>" |
| 237 | <b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Function_8h-source.html">llvm/Function.h</a>" |
| 238 | |
| 239 | <b>namespace</b> { |
| 240 | <b>struct Hello</b> : <b>public</b> <a href="#FunctionPass">FunctionPass</a> { |
| 241 | <b>virtual bool</b> <a href="#runOnFunction">runOnFunction</a>(Function &F) { |
| 242 | std::cerr << "<i>Hello: </i>" << F.getName() << "\n"; |
| 243 | <b>return false</b>; |
| 244 | } |
| 245 | }; |
| 246 | |
| 247 | RegisterOpt<Hello> X("<i>hello</i>", "<i>Hello World Pass</i>"); |
| 248 | } |
| 249 | </pre><p> |
| 250 | |
| 251 | Now that it's all together, compile the file with a simple "<tt>gmake</tt>" |
| 252 | command in the local directory and you should get a new |
| 253 | "<tt>lib/Debug/libhello.so</tt> file. Note that everything in this file is |
| 254 | contained in an anonymous namespace: this reflects the fact that passes are self |
| 255 | contained units that do not need external interfaces (although they can have |
| 256 | them) to be useful.<p> |
| 257 | |
| 258 | |
| 259 | <!-- ======================================================================= --> |
| 260 | </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0> |
| 261 | <tr><td> </td><td width="100%"> |
| 262 | <font color="#EEEEFF" face="Georgia,Palatino"><b> |
| 263 | <a name="running">Running a pass with <tt>opt</tt> or <tt>analyze</tt> |
| 264 | </b></font></td></tr></table><ul> |
| 265 | |
| 266 | Now that you have a brand new shiny <tt>.so</tt> file, we can use the |
| 267 | <tt>opt</tt> command to run an LLVM program through your pass. Because you |
| 268 | registered your pass with the <tt>RegisterOpt</tt> template, you will be able to |
| 269 | use the <tt>opt</tt> tool to access it, once loaded.<p> |
| 270 | |
| 271 | To test it, follow the example at the end of the <a |
| 272 | href="GettingStarted.html">Getting Started Guide</a> to compile "Hello World" to |
| 273 | LLVM. We can now run the bytecode file (<tt>hello.bc</tt>) for the program |
| 274 | through our transformation like this (or course, any bytecode file will |
| 275 | work):<p> |
| 276 | |
| 277 | <pre> |
| 278 | $ opt -load ../../../lib/Debug/libhello.so -hello < hello.bc > /dev/null |
| 279 | Hello: __main |
| 280 | Hello: puts |
| 281 | Hello: main |
| 282 | </pre><p> |
| 283 | |
| 284 | The '<tt>-load</tt>' option specifies that '<tt>opt</tt>' should load your pass |
| 285 | as a shared object, which makes '<tt>-hello</tt>' a valid command line argument |
| 286 | (which is one reason you need to <a href="#registration">register your |
| 287 | pass</a>). Because the hello pass does not modify the program in any |
| 288 | interesting way, we just throw away the result of <tt>opt</tt> (sending it to |
| 289 | <tt>/dev/null</tt>).<p> |
| 290 | |
| 291 | To see what happened to the other string you registered, try running |
| 292 | <tt>opt</tt> with the <tt>--help</tt> option:<p> |
| 293 | |
| 294 | <pre> |
| 295 | $ opt -load ../../../lib/Debug/libhello.so --help |
| 296 | OVERVIEW: llvm .bc -> .bc modular optimizer |
| 297 | |
| 298 | USAGE: opt [options] <input bytecode> |
| 299 | |
| 300 | OPTIONS: |
| 301 | Optimizations available: |
| 302 | ... |
| 303 | -funcresolve - Resolve Functions |
| 304 | -gcse - Global Common Subexpression Elimination |
| 305 | -globaldce - Dead Global Elimination |
| 306 | <b>-hello - Hello World Pass</b> |
| 307 | -indvars - Cannonicalize Induction Variables |
| 308 | -inline - Function Integration/Inlining |
| 309 | -instcombine - Combine redundant instructions |
| 310 | ... |
| 311 | </pre><p> |
| 312 | |
| 313 | The pass name get added as the information string for your pass, giving some |
| 314 | documentation to users of <tt>opt</tt>. Now that you have a working pass, you |
| 315 | would go ahead and make it do the cool transformations you want. Once you get |
| 316 | it all working and tested, it may become useful to find out how fast your pass |
| 317 | is. The <a href="#passManager"><tt>PassManager</tt></a> provides a nice command |
| 318 | line option (<tt>--time-passes</tt>) that allows you to get information about |
| 319 | the execution time of your pass along with the other passes you queue up. For |
| 320 | example:<p> |
| 321 | |
| 322 | <pre> |
| 323 | $ opt -load ../../../lib/Debug/libhello.so -hello -time-passes < hello.bc > /dev/null |
| 324 | Hello: __main |
| 325 | Hello: puts |
| 326 | Hello: main |
| 327 | =============================================================================== |
| 328 | ... Pass execution timing report ... |
| 329 | =============================================================================== |
| 330 | Total Execution Time: 0.02 seconds (0.0479059 wall clock) |
| 331 | |
| 332 | ---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Pass Name --- |
| 333 | 0.0100 (100.0%) 0.0000 ( 0.0%) 0.0100 ( 50.0%) 0.0402 ( 84.0%) Bytecode Writer |
| 334 | 0.0000 ( 0.0%) 0.0100 (100.0%) 0.0100 ( 50.0%) 0.0031 ( 6.4%) Dominator Set Construction |
| 335 | 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0013 ( 2.7%) Module Verifier |
| 336 | <b> 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0033 ( 6.9%) Hello World Pass</b> |
| 337 | 0.0100 (100.0%) 0.0100 (100.0%) 0.0200 (100.0%) 0.0479 (100.0%) TOTAL |
| 338 | </pre><p> |
| 339 | |
| 340 | As you can see, our implementation above is pretty fast :). The additional |
| 341 | passes listed are automatically inserted by the '<tt>opt</tt>' tool to verify |
| 342 | that the LLVM emitted by your pass is still valid and well formed LLVM, which |
| 343 | hasn't been broken somehow. |
| 344 | |
| 345 | Now that you have seen the basics of the mechanics behind passes, we can talk |
| 346 | about some more details of how they work and how to use them.<p> |
| 347 | |
| 348 | |
| 349 | |
| 350 | <!-- *********************************************************************** --> |
| 351 | </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0> |
| 352 | <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b> |
| 353 | <a name="passtype">Pass classes and requirements |
| 354 | </b></font></td></tr></table><ul> |
| 355 | <!-- *********************************************************************** --> |
| 356 | |
| 357 | One of the first things that you should do when designing a new pass is to |
| 358 | decide what class you should subclass for your pass. The <a |
| 359 | href="#basiccode">Hello World</a> example uses the <tt><a |
| 360 | href="#FunctionPass">FunctionPass</a></tt> class for its implementation, but we |
| 361 | did not discuss why or when this should occur. Here we talk about the classes |
| 362 | available, from the most general to the most specific.<p> |
| 363 | |
Chris Lattner | 7991070 | 2002-08-22 19:21:04 +0000 | [diff] [blame] | 364 | When choosing a superclass for your Pass, you should choose the <b>most |
| 365 | specific</b> class possible, while still being able to meet the requirements |
| 366 | listed. This gives the LLVM Pass Infrastructure information neccesary to |
| 367 | optimize how passes are run, so that the resultant compiler isn't unneccesarily |
| 368 | slow.<p> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 369 | |
| 370 | |
Chris Lattner | f004f9a | 2002-09-25 22:31:38 +0000 | [diff] [blame] | 371 | <!-- ======================================================================= --> |
| 372 | </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0> |
| 373 | <tr><td> </td><td width="100%"> |
| 374 | <font color="#EEEEFF" face="Georgia,Palatino"><b> |
| 375 | <a name="ImmutablePass">The <tt>ImmutablePass</tt> class |
| 376 | </b></font></td></tr></table><ul> |
| 377 | |
| 378 | The most plain and boring type of pass is the "<tt><a |
| 379 | href="http://llvm.cs.uiuc.edu/doxygen/structImmutablePass.html">ImmutablePass</a></tt>" |
| 380 | class. This pass type is used for passes that do not have to be run, do not |
| 381 | change state, and never need to be updated. This is not a normal type of |
| 382 | transformation or analysis, but can provide information about the current |
| 383 | compiler configuration.<p> |
| 384 | |
| 385 | Although this pass class is very infrequently used, it is important for |
| 386 | providing information about the current target machine being compiled for, and |
| 387 | other static information that can affect the various transformations.<p> |
| 388 | |
| 389 | <tt>ImmutablePass</tt>'s never invalidate other transformations, are never |
| 390 | invalidated, and are never "run".<p> |
| 391 | |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 392 | |
| 393 | <!-- ======================================================================= --> |
| 394 | </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0> |
| 395 | <tr><td> </td><td width="100%"> |
| 396 | <font color="#EEEEFF" face="Georgia,Palatino"><b> |
| 397 | <a name="Pass">The <tt>Pass</tt> class |
| 398 | </b></font></td></tr></table><ul> |
| 399 | |
| 400 | The "<tt><a href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">Pass</a></tt>" |
| 401 | class is the most general of all superclasses that you can use. Deriving from |
| 402 | <tt>Pass</tt> indicates that your pass uses the entire program as a unit, |
| 403 | refering to function bodies in no predictable order, or adding and removing |
| 404 | functions. Because nothing is known about the behavior of direct <tt>Pass</tt> |
| 405 | subclasses, no optimization can be done for their execution.<p> |
| 406 | |
| 407 | To write a correct <tt>Pass</tt> subclass, derive from <tt>Pass</tt> and |
| 408 | overload the <tt>run</tt> method with the following signature:<p> |
| 409 | |
| 410 | <!-- _______________________________________________________________________ --> |
| 411 | </ul><h4><a name="run"><hr size=0>The <tt>run</tt> method</h4><ul> |
| 412 | |
| 413 | |
| 414 | <pre> |
| 415 | <b>virtual bool</b> run(Module &M) = 0; |
| 416 | </pre><p> |
| 417 | |
| 418 | The <tt>run</tt> method performs the interesting work of the pass, and should |
| 419 | return true if the module was modified by the transformation, false |
| 420 | otherwise.<p> |
| 421 | |
| 422 | |
| 423 | |
| 424 | <!-- ======================================================================= --> |
| 425 | </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0> |
| 426 | <tr><td> </td><td width="100%"> |
| 427 | <font color="#EEEEFF" face="Georgia,Palatino"><b> |
| 428 | <a name="FunctionPass">The <tt>FunctionPass</tt> class |
| 429 | </b></font></td></tr></table><ul> |
| 430 | |
| 431 | In contrast to direct <tt>Pass</tt> subclasses, direct <tt><a |
| 432 | href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">FunctionPass</a></tt> |
| 433 | subclasses do have a predictable, local behavior that can be expected by the |
| 434 | system. All <tt>FunctionPass</tt> execute on each function in the program |
| 435 | independant of all of the other functions in the program. |
| 436 | <tt>FunctionPass</tt>'s do not require that they are executed in a particular |
| 437 | order, and <tt>FunctionPass</tt>'s do not modify external functions.<p> |
| 438 | |
| 439 | To be explicit, <tt>FunctionPass</tt> subclasses are not allowed to:<p> |
| 440 | |
| 441 | <ol> |
| 442 | <li>Modify a Function other than the one currently being processed. |
| 443 | <li>Add or remove Function's from the current Module. |
| 444 | <li>Add or remove global variables from the current Module. |
| 445 | <li>Maintain state across invocations of |
| 446 | <a href="#runOnFunction"><tt>runOnFunction</tt></a> (including global data) |
| 447 | </ol><p> |
| 448 | |
| 449 | Implementing a <tt>FunctionPass</tt> is usually straightforward (See the <a |
| 450 | href="#basiccode">Hello World</a> pass for example). <tt>FunctionPass</tt>'s |
| 451 | may overload three virtual methods to do their work. All of these methods |
| 452 | should return true if they modified the program, or false if they didn't.<p> |
| 453 | |
| 454 | <!-- _______________________________________________________________________ --> |
Chris Lattner | d0713f9 | 2002-09-12 17:06:43 +0000 | [diff] [blame] | 455 | </ul><h4><a name="doInitialization_mod"><hr size=0>The |
| 456 | <tt>doInitialization(Module &)</tt> method</h4><ul> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 457 | |
| 458 | <pre> |
| 459 | <b>virtual bool</b> doInitialization(Module &M); |
| 460 | </pre><p> |
| 461 | |
| 462 | The <tt>doIninitialize</tt> method is allowed to do most of the things that |
| 463 | <tt>FunctionPass</tt>'s are not allowed to do. They can add and remove |
Chris Lattner | d0713f9 | 2002-09-12 17:06:43 +0000 | [diff] [blame] | 464 | functions, get pointers to functions, etc. The <tt>doInitialization</tt> method |
| 465 | is designed to do simple initialization type of stuff that does not depend on |
| 466 | the functions being processed. The <tt>doInitialization</tt> method call is not |
| 467 | scheduled to overlap with any other pass executions (thus it should be very |
| 468 | fast).<p> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 469 | |
| 470 | A good example of how this method should be used is the <a |
| 471 | href="http://llvm.cs.uiuc.edu/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations</a> |
| 472 | pass. This pass converts <tt>malloc</tt> and <tt>free</tt> instructions into |
| 473 | platform dependant <tt>malloc()</tt> and <tt>free()</tt> function calls. It |
| 474 | uses the <tt>doInitialization</tt> method to get a reference to the malloc and |
| 475 | free functions that it needs, adding prototypes to the module if neccesary.<p> |
| 476 | |
| 477 | <!-- _______________________________________________________________________ --> |
| 478 | </ul><h4><a name="runOnFunction"><hr size=0>The <tt>runOnFunction</tt> method</h4><ul> |
| 479 | |
| 480 | <pre> |
| 481 | <b>virtual bool</b> runOnFunction(Function &F) = 0; |
| 482 | </pre><p> |
| 483 | |
| 484 | The <tt>runOnFunction</tt> method must be implemented by your subclass to do the |
| 485 | transformation or analysis work of your pass. As usual, a true value should be |
| 486 | returned if the function is modified.<p> |
| 487 | |
| 488 | <!-- _______________________________________________________________________ --> |
Chris Lattner | d0713f9 | 2002-09-12 17:06:43 +0000 | [diff] [blame] | 489 | </ul><h4><a name="doFinalization_mod"><hr size=0>The <tt>doFinalization(Module &)</tt> method</h4><ul> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 490 | |
| 491 | <pre> |
| 492 | <b>virtual bool</b> doFinalization(Module &M); |
Misha Brukman | e3443a6 | 2003-07-14 17:05:20 +0000 | [diff] [blame] | 493 | </pre></p> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 494 | |
| 495 | The <tt>doFinalization</tt> method is an infrequently used method that is called |
| 496 | when the pass framework has finished calling <a |
| 497 | href="#runOnFunction"><tt>runOnFunction</tt></a> for every function in the |
| 498 | program being compiled.<p> |
| 499 | |
| 500 | |
| 501 | |
| 502 | <!-- ======================================================================= --> |
| 503 | </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0> |
| 504 | <tr><td> </td><td width="100%"> |
| 505 | <font color="#EEEEFF" face="Georgia,Palatino"><b> |
| 506 | <a name="BasicBlockPass">The <tt>BasicBlockPass</tt> class</a> |
| 507 | </b></font></td></tr></table><ul> |
| 508 | |
| 509 | <tt>BasicBlockPass</tt>'s are just like <a |
| 510 | href="#FunctionPass"><tt>FunctionPass</tt></a>'s, except that they must limit |
| 511 | their scope of inspection and modification to a single basic block at a time. |
| 512 | As such, they are <b>not</b> allowed to do any of the following:<p> |
| 513 | |
| 514 | <ol> |
| 515 | <li>Modify or inspect any basic blocks outside of the current one |
| 516 | <li>Maintain state across invocations of |
| 517 | <a href="#runOnBasicBlock"><tt>runOnBasicBlock</tt></a> |
| 518 | <li>Modify the constrol flow graph (by altering terminator instructions) |
| 519 | <li>Any of the things verboten for |
| 520 | <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s. |
| 521 | </ol><p> |
| 522 | |
| 523 | <tt>BasicBlockPass</tt>'s are useful for traditional local and "peephole" |
| 524 | optimizations. They may override the same <a |
Chris Lattner | d0713f9 | 2002-09-12 17:06:43 +0000 | [diff] [blame] | 525 | href="#doInitialization_mod"><tt>doInitialization(Module &)</tt></a> and <a |
| 526 | href="#doFinalization_mod"><tt>doFinalization(Module &)</tt></a> methods that <a |
| 527 | href="#FunctionPass"><tt>FunctionPass</tt></a>'s have, but also have the following virtual methods that may also be implemented:<p> |
| 528 | |
| 529 | <!-- _______________________________________________________________________ --> |
| 530 | </ul><h4><a name="doInitialization_fn"><hr size=0>The |
| 531 | <tt>doInitialization(Function &)</tt> method</h4><ul> |
| 532 | |
| 533 | <pre> |
| 534 | <b>virtual bool</b> doInitialization(Function &F); |
| 535 | </pre><p> |
| 536 | |
| 537 | The <tt>doIninitialize</tt> method is allowed to do most of the things that |
| 538 | <tt>BasicBlockPass</tt>'s are not allowed to do, but that |
| 539 | <tt>FunctionPass</tt>'s can. The <tt>doInitialization</tt> method is designed |
| 540 | to do simple initialization type of stuff that does not depend on the |
| 541 | BasicBlocks being processed. The <tt>doInitialization</tt> method call is not |
| 542 | scheduled to overlap with any other pass executions (thus it should be very |
| 543 | fast).<p> |
| 544 | |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 545 | |
| 546 | <!-- _______________________________________________________________________ --> |
| 547 | </ul><h4><a name="runOnBasicBlock"><hr size=0>The <tt>runOnBasicBlock</tt> method</h4><ul> |
| 548 | |
| 549 | <pre> |
| 550 | <b>virtual bool</b> runOnBasicBlock(BasicBlock &BB) = 0; |
| 551 | </pre><p> |
| 552 | |
| 553 | Override this function to do the work of the <tt>BasicBlockPass</tt>. This |
| 554 | function is not allowed to inspect or modify basic blocks other than the |
| 555 | parameter, and are not allowed to modify the CFG. A true value must be returned |
| 556 | if the basic block is modified.<p> |
| 557 | |
| 558 | |
Chris Lattner | d0713f9 | 2002-09-12 17:06:43 +0000 | [diff] [blame] | 559 | <!-- _______________________________________________________________________ --> |
| 560 | </ul><h4><a name="doFinalization_fn"><hr size=0>The <tt>doFinalization(Function |
| 561 | &)</tt> method</h4><ul> |
| 562 | |
| 563 | <pre> |
| 564 | <b>virtual bool</b> doFinalization(Function &F); |
Misha Brukman | e3443a6 | 2003-07-14 17:05:20 +0000 | [diff] [blame] | 565 | </pre></p> |
Chris Lattner | d0713f9 | 2002-09-12 17:06:43 +0000 | [diff] [blame] | 566 | |
| 567 | The <tt>doFinalization</tt> method is an infrequently used method that is called |
| 568 | when the pass framework has finished calling <a |
| 569 | href="#runOnBasicBlock"><tt>runOnBasicBlock</tt></a> for every BasicBlock in the |
| 570 | program being compiled. This can be used to perform per-function |
| 571 | finalization.<p> |
| 572 | |
| 573 | |
Brian Gaeke | cab8b6f | 2003-07-17 18:53:20 +0000 | [diff] [blame^] | 574 | <!-- ======================================================================= --> |
| 575 | </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0> |
| 576 | <tr><td> </td><td width="100%"> |
| 577 | <font color="#EEEEFF" face="Georgia,Palatino"><b> |
| 578 | <a name="MachineFunctionPass">The <tt>MachineFunctionPass</tt> class |
| 579 | </b></font></td></tr></table><ul> |
| 580 | |
| 581 | A <tt>MachineFunctionPass</tt> executes on the machine-dependent |
| 582 | representation of each LLVM function in the program, |
| 583 | independent of all of the other functions in the program. |
| 584 | A <tt>MachineFunctionPass</tt> is also a <tt>FunctionPass</tt>, so all |
| 585 | the restrictions that apply to a <tt>FunctionPass</tt> also apply to it. |
| 586 | <tt>MachineFunctionPass</tt>es also have additional restrictions. In |
| 587 | particular, <tt>MachineFunctionPass</tt>es are not allowed to do any of |
| 588 | the following: |
| 589 | |
| 590 | <ol> |
| 591 | <li>Modify any LLVM Instructions, BasicBlocks or Functions. |
| 592 | <li>Modify a MachineFunction other than the one currently being processed. |
| 593 | <li>Add or remove MachineFunctions from the current Module. |
| 594 | <li>Add or remove global variables from the current Module. |
| 595 | <li>Maintain state across invocations of |
| 596 | <a href="#runOnMachineFunction"><tt>runOnMachineFunction</tt></a> (including global data) |
| 597 | </ol><p> |
| 598 | |
Chris Lattner | d0713f9 | 2002-09-12 17:06:43 +0000 | [diff] [blame] | 599 | |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 600 | <!-- *********************************************************************** --> |
| 601 | </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0> |
| 602 | <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b> |
| 603 | <a name="registration">Pass registration |
| 604 | </b></font></td></tr></table><ul> |
| 605 | <!-- *********************************************************************** --> |
| 606 | |
| 607 | In the <a href="#basiccode">Hello World</a> example pass we illustrated how pass |
| 608 | registration works, and discussed some of the reasons that it is used and what |
| 609 | it does. Here we discuss how and why passes are registered.<p> |
| 610 | |
| 611 | Passes can be registered in several different ways. Depending on the general |
| 612 | classification of the pass, you should use one of the following templates to |
| 613 | register the pass:<p> |
| 614 | |
| 615 | <ul> |
| 616 | <li><b><tt>RegisterOpt</tt></b> - This template should be used when you are |
| 617 | registering a pass that logically should be available for use in the |
| 618 | '<tt>opt</tt>' utility.<p> |
| 619 | |
| 620 | <li><b><tt>RegisterAnalysis</tt></b> - This template should be used when you are |
| 621 | registering a pass that logically should be available for use in the |
| 622 | '<tt>analysis</tt>' utility.<p> |
| 623 | |
| 624 | <li><b><tt>RegisterLLC</tt></b> - This template should be used when you are |
| 625 | registering a pass that logically should be available for use in the |
| 626 | '<tt>llc</tt>' utility.<p> |
| 627 | |
| 628 | <li><b><tt>RegisterPass</tt></b> - This is the generic form of the |
| 629 | <tt>Register*</tt> templates that should be used if you want your pass listed by |
| 630 | multiple or no utilities. This template takes an extra third argument that |
| 631 | specifies which tools it should be listed in. See the <a |
| 632 | href="http://llvm.cs.uiuc.edu/doxygen/PassSupport_8h-source.html">PassSupport.h</a> |
| 633 | file for more information.<p> |
| 634 | </ul><p> |
| 635 | |
| 636 | Regardless of how you register your pass, you must specify at least two |
| 637 | parameters. The first parameter is the name of the pass that is to be used on |
| 638 | the command line to specify that the pass should be added to a program (for |
| 639 | example <tt>opt</tt> or <tt>analyze</tt>). The second argument is the name of |
| 640 | the pass, which is to be used for the <tt>--help</tt> output of programs, as |
| 641 | well as for debug output generated by the <tt>--debug-pass</tt> option.<p> |
| 642 | |
| 643 | If you pass is constructed by its default constructor, you only ever have to |
| 644 | pass these two arguments. If, on the other hand, you require other information |
| 645 | (like target specific information), you must pass an additional argument. This |
| 646 | argument is a pointer to a function used to create the pass. For an example of |
| 647 | how this works, look at the <a |
| 648 | href="http://llvm.cs.uiuc.edu/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations.cpp</a> |
| 649 | file.<p> |
| 650 | |
| 651 | If a pass is registered to be used by the <tt>analyze</tt> utility, you should |
| 652 | implement the virtual <tt>print</tt> method:<p> |
| 653 | |
| 654 | <!-- _______________________________________________________________________ --> |
| 655 | </ul><h4><a name="print"><hr size=0>The <tt>print</tt> method</h4><ul> |
| 656 | |
| 657 | <pre> |
| 658 | <b>virtual void</b> print(std::ostream &O, <b>const</b> Module *M) <b>const</b>; |
| 659 | </pre><p> |
| 660 | |
| 661 | The <tt>print</tt> method must be implemented by "analyses" in order to print a |
| 662 | human readable version of the analysis results. This is useful for debugging an |
| 663 | analysis itself, as well as for other people to figure out how an analysis |
| 664 | works. The <tt>analyze</tt> tool uses this method to generate its output.<p> |
| 665 | |
| 666 | The <tt>ostream</tt> parameter specifies the stream to write the results on, and |
| 667 | the <tt>Module</tt> parameter gives a pointer to the top level module of the |
| 668 | program that has been analyzed. Note however that this pointer may be null in |
| 669 | certain circumstances (such as calling the <tt>Pass::dump()</tt> from a |
| 670 | debugger), so it should only be used to enhance debug output, it should not be |
| 671 | depended on.<p> |
| 672 | |
| 673 | |
| 674 | <!-- *********************************************************************** --> |
| 675 | </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0> |
| 676 | <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b> |
| 677 | <a name="interaction">Specifying interactions between passes |
| 678 | </b></font></td></tr></table><ul> |
| 679 | <!-- *********************************************************************** --> |
| 680 | |
| 681 | One of the main responsibilities of the <tt>PassManager</tt> is the make sure |
| 682 | that passes interact with each other correctly. Because <tt>PassManager</tt> |
| 683 | tries to <a href="#passmanager">optimize the execution of passes</a> it must |
| 684 | know how the passes interact with each other and what dependencies exist between |
| 685 | the various passes. To track this, each pass can declare the set of passes that |
| 686 | are required to be executed before the current pass, and the passes which are |
| 687 | invalidated by the current pass.<p> |
| 688 | |
| 689 | Typically this functionality is used to require that analysis results are |
| 690 | computed before your pass is run. Running arbitrary transformation passes can |
| 691 | invalidate the computed analysis results, which is what the invalidation set |
| 692 | specifies. If a pass does not implement the <tt><a |
| 693 | href="#getAnalysisUsage">getAnalysisUsage</a></tt> method, it defaults to not |
| 694 | having any prerequisite passes, and invalidating <b>all</b> other passes.<p> |
| 695 | |
| 696 | |
| 697 | <!-- _______________________________________________________________________ --> |
| 698 | </ul><h4><a name="getAnalysisUsage"><hr size=0>The <tt>getAnalysisUsage</tt> method</h4><ul> |
| 699 | |
| 700 | <pre> |
| 701 | <b>virtual void</b> getAnalysisUsage(AnalysisUsage &Info) <b>const</b>; |
| 702 | </pre><p> |
| 703 | |
| 704 | By implementing the <tt>getAnalysisUsage</tt> method, the required and |
| 705 | invalidated sets may be specified for your transformation. The implementation |
| 706 | should fill in the <tt><a |
| 707 | href="http://llvm.cs.uiuc.edu/doxygen/classAnalysisUsage.html">AnalysisUsage</a></tt> |
| 708 | object with information about which passes are required and not invalidated. To do this, the following set methods are provided by the <tt><a |
| 709 | href="http://llvm.cs.uiuc.edu/doxygen/classAnalysisUsage.html">AnalysisUsage</a></tt> class:<p> |
| 710 | |
| 711 | <pre> |
| 712 | <i>// addRequires - Add the specified pass to the required set for your pass.</i> |
| 713 | <b>template</b><<b>class</b> PassClass> |
| 714 | AnalysisUsage &AnalysisUsage::addRequired(); |
| 715 | |
| 716 | <i>// addPreserved - Add the specified pass to the set of analyses preserved by |
| 717 | // this pass</i> |
| 718 | <b>template</b><<b>class</b> PassClass> |
| 719 | AnalysisUsage &AnalysisUsage::addPreserved(); |
| 720 | |
| 721 | <i>// setPreservesAll - Call this if the pass does not modify its input at all</i> |
| 722 | <b>void</b> AnalysisUsage::setPreservesAll(); |
| 723 | |
Chris Lattner | 8291e04 | 2002-10-21 19:57:59 +0000 | [diff] [blame] | 724 | <i>// setPreservesCFG - This function should be called by the pass, iff they do not: |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 725 | // |
| 726 | // 1. Add or remove basic blocks from the function |
| 727 | // 2. Modify terminator instructions in any way. |
| 728 | // |
| 729 | // This is automatically implied for <a href="#BasicBlockPass">BasicBlockPass</a>'s |
| 730 | //</i> |
Chris Lattner | 8291e04 | 2002-10-21 19:57:59 +0000 | [diff] [blame] | 731 | <b>void</b> AnalysisUsage::setPreservesCFG(); |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 732 | </pre><p> |
| 733 | |
| 734 | Some examples of how to use these methods are:<p> |
| 735 | |
| 736 | <pre> |
| 737 | <i>// This is an example implementation from an analysis, which does not modify |
| 738 | // the program at all, yet has a prerequisite.</i> |
| 739 | <b>void</b> <a href="http://llvm.cs.uiuc.edu/doxygen/structPostDominanceFrontier.html">PostDominanceFrontier</a>::getAnalysisUsage(AnalysisUsage &AU) <b>const</b> { |
| 740 | AU.setPreservesAll(); |
| 741 | AU.addRequired<<a href="http://llvm.cs.uiuc.edu/doxygen/structPostDominatorTree.html">PostDominatorTree</a>>(); |
| 742 | } |
| 743 | </pre><p> |
| 744 | |
| 745 | and:<p> |
| 746 | |
| 747 | <pre> |
| 748 | <i>// This example modifies the program, but does not modify the CFG</i> |
| 749 | <b>void</b> <a href="http://llvm.cs.uiuc.edu/doxygen/structLICM.html">LICM</a>::getAnalysisUsage(AnalysisUsage &AU) <b>const</b> { |
Chris Lattner | 8291e04 | 2002-10-21 19:57:59 +0000 | [diff] [blame] | 750 | AU.setPreservesCFG(); |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 751 | AU.addRequired<<a href="http://llvm.cs.uiuc.edu/doxygen/classLoopInfo.html">LoopInfo</a>>(); |
| 752 | } |
| 753 | </pre><p> |
| 754 | |
| 755 | <!-- _______________________________________________________________________ --> |
| 756 | </ul><h4><a name="getAnalysis"><hr size=0>The <tt>getAnalysis<></tt> method</h4><ul> |
| 757 | |
| 758 | The <tt>Pass::getAnalysis<></tt> method is inherited by your class, |
| 759 | providing you with access to the passes that you declared that you required with |
| 760 | the <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method. It takes |
| 761 | a single template argument that specifies which pass class you want, and returns |
| 762 | a reference to that pass.<p> |
| 763 | |
| 764 | <pre> |
| 765 | <b>template</b><<b>typename</b> PassClass> |
| 766 | AnalysisType &getAnalysis(); |
| 767 | </pre><p> |
| 768 | |
| 769 | This method call returns a reference to the pass desired. You may get a runtime |
| 770 | assertion failure if you attempt to get an analysis that you did not declare as |
| 771 | required in your <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> |
| 772 | implementation. This method can be called by your <tt>run*</tt> method |
| 773 | implementation, or by any other local method invoked by your <tt>run*</tt> |
| 774 | method.<p> |
| 775 | |
Chris Lattner | 7991070 | 2002-08-22 19:21:04 +0000 | [diff] [blame] | 776 | <!-- *********************************************************************** --> |
| 777 | </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0> |
| 778 | <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b> |
| 779 | <a name="analysisgroup">Implementing Analysis Groups |
| 780 | </b></font></td></tr></table><ul> |
| 781 | <!-- *********************************************************************** --> |
| 782 | |
| 783 | Now that we understand the basics of how passes are defined, how the are used, |
| 784 | and how they are required from other passes, it's time to get a little bit |
| 785 | fancier. All of the pass relationships that we have seen so far are very |
| 786 | simple: one pass depends on one other specific pass to be run before it can run. |
| 787 | For many applications, this is great, for others, more flexibility is |
| 788 | required.<p> |
| 789 | |
| 790 | In particular, some analyses are defined such that there is a single simple |
| 791 | interface to the analysis results, but multiple ways of calculating them. |
| 792 | Consider alias analysis for example. The most trivial alias analysis returns |
| 793 | "may alias" for any alias query. The most sophisticated analysis a |
| 794 | flow-sensitive, context-sensitive interprocedural analysis that can take a |
| 795 | significant amount of time to execute (and obviously, there is a lot of room |
| 796 | between these two extremes for other implementations). To cleanly support |
| 797 | situations like this, the LLVM Pass Infrastructure supports the notion of |
| 798 | Analysis Groups.<p> |
| 799 | |
| 800 | <!-- _______________________________________________________________________ --> |
| 801 | </ul><h4><a name="agconcepts"><hr size=0>Analysis Group Concepts</h4><ul> |
| 802 | |
| 803 | An Analysis Group is a single simple interface that may be implemented by |
| 804 | multiple different passes. Analysis Groups can be given human readable names |
| 805 | just like passes, but unlike passes, they need not derive from the <tt>Pass</tt> |
| 806 | class. An analysis group may have one or more implementations, one of which is |
| 807 | the "default" implementation.<p> |
| 808 | |
| 809 | Analysis groups are used by client passes just like other passes are: the |
| 810 | <tt>AnalysisUsage::addRequired()</tt> and <tt>Pass::getAnalysis()</tt> methods. |
| 811 | In order to resolve this requirement, the <a href="#passmanager">PassManager</a> |
| 812 | scans the available passes to see if any implementations of the analysis group |
| 813 | are available. If none is available, the default implementation is created for |
| 814 | the pass to use. All standard rules for <A href="#interaction">interaction |
| 815 | between passes</a> still apply.<p> |
| 816 | |
| 817 | Although <a href="#registration">Pass Registration</a> is optional for normal |
| 818 | passes, all analysis group implementations must be registered, and must use the |
| 819 | <A href="#registerag"><tt>RegisterAnalysisGroup</tt></a> template to join the |
| 820 | implementation pool. Also, a default implementation of the interface |
| 821 | <b>must</b> be registered with <A |
| 822 | href="#registerag"><tt>RegisterAnalysisGroup</tt></a>.<p> |
| 823 | |
| 824 | As a concrete example of an Analysis Group in action, consider the <a |
| 825 | href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a> |
| 826 | analysis group. The default implementation of the alias analysis interface (the |
| 827 | <tt><a |
| 828 | href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">basicaa</a></tt> |
| 829 | pass) just does a few simple checks that don't require significant analysis to |
| 830 | compute (such as: two different globals can never alias each other, etc). |
| 831 | Passes that use the <tt><a |
| 832 | href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a></tt> |
| 833 | interface (for example the <tt><a |
| 834 | href="http://llvm.cs.uiuc.edu/doxygen/classGCSE.html">gcse</a></tt> pass), do not care which implementation |
| 835 | of alias analysis is actually provided, they just use the designated |
| 836 | interface.<p> |
| 837 | |
| 838 | From the user's perspective, commands work just like normal. Issuing the |
| 839 | command '<tt>opt -gcse ...</tt>' will cause the <tt>basicaa</tt> class to be |
| 840 | instantiated and added to the pass sequence. Issuing the command '<tt>opt |
| 841 | -somefancyaa -gcse ...</tt>' will cause the <tt>gcse</tt> pass to use the |
| 842 | <tt>somefancyaa</tt> alias analysis (which doesn't actually exist, it's just a |
| 843 | hypothetical example) instead.<p> |
| 844 | |
| 845 | |
| 846 | <!-- _______________________________________________________________________ --> |
| 847 | </ul><h4><a name="registerag"><hr size=0>Using <tt>RegisterAnalysisGroup</tt></h4><ul> |
| 848 | |
| 849 | The <tt>RegisterAnalysisGroup</tt> template is used to register the analysis |
| 850 | group itself as well as add pass implementations to the analysis group. First, |
| 851 | an analysis should be registered, with a human readable name provided for it. |
| 852 | Unlike registration of passes, there is no command line argument to be specified |
| 853 | for the Analysis Group Interface itself, because it is "abstract":<p> |
| 854 | |
| 855 | <pre> |
| 856 | <b>static</b> RegisterAnalysisGroup<<a href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a>> A("<i>Alias Analysis</i>"); |
| 857 | </pre><p> |
| 858 | |
| 859 | Once the analysis is registered, passes can declare that they are valid |
| 860 | implementations of the interface by using the following code:<p> |
| 861 | |
| 862 | <pre> |
| 863 | <b>namespace</b> { |
| 864 | //<i> Analysis Group implementations <b>must</b> be registered normally...</i> |
| 865 | RegisterOpt<FancyAA> |
| 866 | B("<i>somefancyaa</i>", "<i>A more complex alias analysis implementation</i>"); |
| 867 | |
| 868 | //<i> Declare that we implement the AliasAnalysis interface</i> |
| 869 | RegisterAnalysisGroup<<a href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a>, FancyAA> C; |
| 870 | } |
| 871 | </pre><p> |
| 872 | |
| 873 | This just shows a class <tt>FancyAA</tt> that is registered normally, then uses |
| 874 | the <tt>RegisterAnalysisGroup</tt> template to "join" the <tt><a |
| 875 | href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a></tt> |
| 876 | analysis group. Every implementation of an analysis group should join using |
| 877 | this template. A single pass may join multiple different analysis groups with |
| 878 | no problem.<p> |
| 879 | |
| 880 | <pre> |
| 881 | <b>namespace</b> { |
| 882 | //<i> Analysis Group implementations <b>must</b> be registered normally...</i> |
| 883 | RegisterOpt<<a href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a>> |
| 884 | D("<i>basicaa</i>", "<i>Basic Alias Analysis (default AA impl)</i>"); |
| 885 | |
| 886 | //<i> Declare that we implement the AliasAnalysis interface</i> |
| 887 | RegisterAnalysisGroup<<a href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a>, <a href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a>, <b>true</b>> E; |
| 888 | } |
| 889 | </pre><p> |
| 890 | |
| 891 | Here we show how the default implementation is specified (using the extra |
| 892 | argument to the <tt>RegisterAnalysisGroup</tt> template). There must be exactly |
| 893 | one default implementation available at all times for an Analysis Group to be |
| 894 | used. Here we declare that the <tt><a |
| 895 | href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a></tt> |
| 896 | pass is the default implementation for the interface.<p> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 897 | |
| 898 | |
| 899 | <!-- *********************************************************************** --> |
| 900 | </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0> |
| 901 | <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b> |
| 902 | <a name="passmanager">What PassManager does |
| 903 | </b></font></td></tr></table><ul> |
| 904 | <!-- *********************************************************************** --> |
| 905 | |
| 906 | The <a |
| 907 | href="http://llvm.cs.uiuc.edu/doxygen/PassManager_8h-source.html"><tt>PassManager</tt></a> |
| 908 | <a href="http://llvm.cs.uiuc.edu/doxygen/classPassManager.html">class</a> takes |
| 909 | a list of passes, ensures their <a href="#interaction">prerequisites</a> are set |
| 910 | up correctly, and then schedules passes to run efficiently. All of the LLVM |
| 911 | tools that run passes use the <tt>PassManager</tt> for execution of these |
| 912 | passes.<p> |
| 913 | |
| 914 | The <tt>PassManager</tt> does two main things to try to reduce the execution |
| 915 | time of a series of passes:<p> |
| 916 | |
| 917 | <ol> |
| 918 | <li><b>Share analysis results</b> - The PassManager attempts to avoid |
| 919 | recomputing analysis results as much as possible. This means keeping track of |
| 920 | which analyses are available already, which analyses get invalidated, and which |
| 921 | analyses are needed to be run for a pass. An important part of work is that the |
| 922 | <tt>PassManager</tt> tracks the exact lifetime of all analysis results, allowing |
| 923 | it to <a href="#releaseMemory">free memory</a> allocated to holding analysis |
| 924 | results as soon as they are no longer needed.<p> |
| 925 | |
| 926 | <li><b>Pipeline the execution of passes on the program</b> - The |
| 927 | <tt>PassManager</tt> attempts to get better cache and memory usage behavior out |
| 928 | of a series of passes by pipelining the passes together. This means that, given |
| 929 | a series of consequtive <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s, it |
| 930 | will execute all of the <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s on |
| 931 | the first function, then all of the <a |
| 932 | href="#FunctionPass"><tt>FunctionPass</tt></a>'s on the second function, |
| 933 | etc... until the entire program has been run through the passes.<p> |
| 934 | |
| 935 | This improves the cache behavior of the compiler, because it is only touching |
| 936 | the LLVM program representation for a single function at a time, instead of |
| 937 | traversing the entire program. It reduces the memory consumption of compiler, |
| 938 | because, for example, only one <a |
| 939 | href="http://llvm.cs.uiuc.edu/doxygen/structDominatorSet.html"><tt>DominatorSet</tt></a> |
| 940 | needs to be calculated at a time. This also makes it possible some <a |
| 941 | href="#SMP">interesting enhancements</a> in the future.<p> |
| 942 | |
| 943 | </ol><p> |
| 944 | |
| 945 | The effectiveness of the <tt>PassManager</tt> is influenced directly by how much |
| 946 | information it has about the behaviors of the passes it is scheduling. For |
| 947 | example, the "preserved" set is intentionally conservative in the face of an |
| 948 | unimplemented <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method. |
| 949 | Not implementing when it should be implemented will have the effect of not |
| 950 | allowing any analysis results to live across the execution of your pass.<p> |
| 951 | |
| 952 | The <tt>PassManager</tt> class exposes a <tt>--debug-pass</tt> command line |
| 953 | options that is useful for debugging pass execution, seeing how things work, and |
| 954 | diagnosing when you should be preserving more analyses than you currently are |
| 955 | (To get information about all of the variants of the <tt>--debug-pass</tt> |
| 956 | option, just type '<tt>opt --help-hidden</tt>').<p> |
| 957 | |
| 958 | By using the <tt>--debug-pass=Structure</tt> option, for example, we can see how |
| 959 | our <a href="#basiccode">Hello World</a> pass interacts with other passes. Lets |
| 960 | try it out with the <tt>gcse</tt> and <tt>licm</tt> passes:<p> |
| 961 | |
| 962 | <pre> |
| 963 | $ opt -load ../../../lib/Debug/libhello.so -gcse -licm --debug-pass=Structure < hello.bc > /dev/null |
| 964 | Module Pass Manager |
| 965 | Function Pass Manager |
| 966 | Dominator Set Construction |
| 967 | Immediate Dominators Construction |
| 968 | Global Common Subexpression Elimination |
| 969 | -- Immediate Dominators Construction |
| 970 | -- Global Common Subexpression Elimination |
| 971 | Natural Loop Construction |
| 972 | Loop Invariant Code Motion |
| 973 | -- Natural Loop Construction |
| 974 | -- Loop Invariant Code Motion |
| 975 | Module Verifier |
| 976 | -- Dominator Set Construction |
| 977 | -- Module Verifier |
| 978 | Bytecode Writer |
| 979 | --Bytecode Writer |
| 980 | </pre><p> |
| 981 | |
| 982 | This output shows us when passes are constructed and when the analysis results |
| 983 | are known to be dead (prefixed with '<tt>--</tt>'). Here we see that GCSE uses |
| 984 | dominator and immediate dominator information to do its job. The LICM pass uses |
| 985 | natural loop information, which uses dominator sets, but not immediate |
| 986 | dominators. Because immediate dominators are no longer useful after the GCSE |
| 987 | pass, it is immediately destroyed. The dominator sets are then reused to |
| 988 | compute natural loop information, which is then used by the LICM pass.<p> |
| 989 | |
| 990 | After the LICM pass, the module verifier runs (which is automatically added by |
| 991 | the '<tt>opt</tt>' tool), which uses the dominator set to check that the |
| 992 | resultant LLVM code is well formed. After it finishes, the dominator set |
| 993 | information is destroyed, after being computed once, and shared by three |
| 994 | passes.<p> |
| 995 | |
| 996 | Lets see how this changes when we run the <a href="#basiccode">Hello World</a> |
| 997 | pass in between the two passes:<p> |
| 998 | |
| 999 | <pre> |
| 1000 | $ opt -load ../../../lib/Debug/libhello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null |
| 1001 | Module Pass Manager |
| 1002 | Function Pass Manager |
| 1003 | Dominator Set Construction |
| 1004 | Immediate Dominators Construction |
| 1005 | Global Common Subexpression Elimination |
| 1006 | <b>-- Dominator Set Construction</b> |
| 1007 | -- Immediate Dominators Construction |
| 1008 | -- Global Common Subexpression Elimination |
| 1009 | <b> Hello World Pass |
| 1010 | -- Hello World Pass |
| 1011 | Dominator Set Construction</b> |
| 1012 | Natural Loop Construction |
| 1013 | Loop Invariant Code Motion |
| 1014 | -- Natural Loop Construction |
| 1015 | -- Loop Invariant Code Motion |
| 1016 | Module Verifier |
| 1017 | -- Dominator Set Construction |
| 1018 | -- Module Verifier |
| 1019 | Bytecode Writer |
| 1020 | --Bytecode Writer |
| 1021 | Hello: __main |
| 1022 | Hello: puts |
| 1023 | Hello: main |
| 1024 | </pre><p> |
| 1025 | |
| 1026 | Here we see that the <a href="#basiccode">Hello World</a> pass has killed the |
| 1027 | Dominator Set pass, even though it doesn't modify the code at all! To fix this, |
| 1028 | we need to add the following <a |
| 1029 | href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method to our pass:<p> |
| 1030 | |
| 1031 | <pre> |
| 1032 | <i>// We don't modify the program, so we preserve all analyses</i> |
| 1033 | <b>virtual void</b> getAnalysisUsage(AnalysisUsage &AU) <b>const</b> { |
| 1034 | AU.setPreservesAll(); |
| 1035 | } |
| 1036 | </pre><p> |
| 1037 | |
| 1038 | Now when we run our pass, we get this output:<p> |
| 1039 | |
| 1040 | <pre> |
| 1041 | $ opt -load ../../../lib/Debug/libhello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null |
| 1042 | Pass Arguments: -gcse -hello -licm |
| 1043 | Module Pass Manager |
| 1044 | Function Pass Manager |
| 1045 | Dominator Set Construction |
| 1046 | Immediate Dominators Construction |
| 1047 | Global Common Subexpression Elimination |
| 1048 | -- Immediate Dominators Construction |
| 1049 | -- Global Common Subexpression Elimination |
| 1050 | Hello World Pass |
| 1051 | -- Hello World Pass |
| 1052 | Natural Loop Construction |
| 1053 | Loop Invariant Code Motion |
| 1054 | -- Loop Invariant Code Motion |
| 1055 | -- Natural Loop Construction |
| 1056 | Module Verifier |
| 1057 | -- Dominator Set Construction |
| 1058 | -- Module Verifier |
| 1059 | Bytecode Writer |
| 1060 | --Bytecode Writer |
| 1061 | Hello: __main |
| 1062 | Hello: puts |
| 1063 | Hello: main |
| 1064 | </pre><p> |
| 1065 | |
| 1066 | Which shows that we don't accidentally invalidate dominator information |
| 1067 | anymore, and therefore do not have to compute it twice.<p> |
| 1068 | |
| 1069 | |
| 1070 | <!-- _______________________________________________________________________ --> |
| 1071 | </ul><h4><a name="releaseMemory"><hr size=0>The <tt>releaseMemory</tt> method</h4><ul> |
| 1072 | |
| 1073 | <pre> |
| 1074 | <b>virtual void</b> releaseMemory(); |
| 1075 | </pre><p> |
| 1076 | |
| 1077 | The <tt>PassManager</tt> automatically determines when to compute analysis |
| 1078 | results, and how long to keep them around for. Because the lifetime of the pass |
| 1079 | object itself is effectively the entire duration of the compilation process, we |
| 1080 | need some way to free analysis results when they are no longer useful. The |
| 1081 | <tt>releaseMemory</tt> virtual method is the way to do this.<p> |
| 1082 | |
| 1083 | If you are writing an analysis or any other pass that retains a significant |
| 1084 | amount of state (for use by another pass which "requires" your pass and uses the |
| 1085 | <a href="#getAnalysis">getAnalysis</a> method) you should implement |
| 1086 | <tt>releaseMEmory</tt> to, well, release the memory allocated to maintain this |
| 1087 | internal state. This method is called after the <tt>run*</tt> method for the |
| 1088 | class, before the next call of <tt>run*</tt> in your pass.<p> |
| 1089 | |
| 1090 | |
| 1091 | <!-- *********************************************************************** --> |
| 1092 | </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0> |
| 1093 | <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b> |
Chris Lattner | 480e2ef | 2002-09-06 02:02:58 +0000 | [diff] [blame] | 1094 | <a name="debughints">Using GDB with dynamically loaded passes |
| 1095 | </b></font></td></tr></table><ul> |
| 1096 | <!-- *********************************************************************** --> |
| 1097 | |
| 1098 | Unfortunately, using GDB with dynamically loaded passes is not as easy as it |
| 1099 | should be. First of all, you can't set a breakpoint in a shared object that has |
| 1100 | not been loaded yet, and second of all there are problems with inlined functions |
| 1101 | in shared objects. Here are some suggestions to debugging your pass with |
| 1102 | GDB.<p> |
| 1103 | |
| 1104 | For sake of discussion, I'm going to assume that you are debugging a |
| 1105 | transformation invoked by <tt>opt</tt>, although nothing described here depends |
| 1106 | on that.<p> |
| 1107 | |
| 1108 | <!-- _______________________________________________________________________ --> |
| 1109 | </ul><h4><a name="breakpoint"><hr size=0>Setting a breakpoint in your pass</h4><ul> |
| 1110 | |
| 1111 | First thing you do is start <tt>gdb</tt> on the <tt>opt</tt> process:<p> |
| 1112 | |
| 1113 | <pre> |
| 1114 | $ <b>gdb opt</b> |
| 1115 | GNU gdb 5.0 |
| 1116 | Copyright 2000 Free Software Foundation, Inc. |
| 1117 | GDB is free software, covered by the GNU General Public License, and you are |
| 1118 | welcome to change it and/or distribute copies of it under certain conditions. |
| 1119 | Type "show copying" to see the conditions. |
| 1120 | There is absolutely no warranty for GDB. Type "show warranty" for details. |
| 1121 | This GDB was configured as "sparc-sun-solaris2.6"... |
| 1122 | (gdb) |
| 1123 | </pre><p> |
| 1124 | |
| 1125 | Note that <tt>opt</tt> has a lot of debugging information in it, so it takes |
| 1126 | time to load. Be patient. Since we cannot set a breakpoint in our pass yet |
| 1127 | (the shared object isn't loaded until runtime), we must execute the process, and |
| 1128 | have it stop before it invokes our pass, but after it has loaded the shared |
| 1129 | object. The most foolproof way of doing this is to set a breakpoint in |
| 1130 | <tt>PassManager::run</tt> and then run the process with the arguments you |
| 1131 | want:<p> |
| 1132 | |
| 1133 | <pre> |
| 1134 | (gdb) <b>break PassManager::run</b> |
| 1135 | Breakpoint 1 at 0x2413bc: file Pass.cpp, line 70. |
| 1136 | (gdb) <b>run test.bc -load /shared/lattner/cvs/llvm/lib/Debug/[libname].so -[passoption]</b> |
| 1137 | Starting program: /shared/lattner/cvs/llvm/tools/Debug/opt test.bc |
| 1138 | -load /shared/lattner/cvs/llvm/lib/Debug/[libname].so -[passoption] |
| 1139 | Breakpoint 1, PassManager::run (this=0xffbef174, M=@0x70b298) at Pass.cpp:70 |
| 1140 | 70 bool PassManager::run(Module &M) { return PM->run(M); } |
| 1141 | (gdb) |
| 1142 | </pre></p> |
| 1143 | |
| 1144 | Once the <tt>opt</tt> stops in the <tt>PassManager::run</tt> method you are now |
| 1145 | free to set breakpoints in your pass so that you can trace through execution or |
| 1146 | do other standard debugging stuff.<p> |
| 1147 | |
| 1148 | |
| 1149 | <!-- _______________________________________________________________________ --> |
| 1150 | </ul><h4><a name="debugmisc"><hr size=0>Miscellaneous Problems</h4><ul> |
| 1151 | |
| 1152 | Once you have the basics down, there are a couple of problems that GDB has, some |
| 1153 | with solutions, some without.<p> |
| 1154 | |
| 1155 | <ul> |
| 1156 | <li>Inline functions have bogus stack information. In general, GDB does a |
| 1157 | pretty good job getting stack traces and stepping through inline functions. |
| 1158 | When a pass is dynamically loaded however, it somehow completely loses this |
| 1159 | capability. The only solution I know of is to de-inline a function (move it |
| 1160 | from the body of a class to a .cpp file).<p> |
| 1161 | |
| 1162 | <li>Restarting the program breaks breakpoints. After following the information |
| 1163 | above, you have succeeded in getting some breakpoints planted in your pass. Nex |
| 1164 | thing you know, you restart the program (i.e., you type '<tt>run</tt>' again), |
| 1165 | and you start getting errors about breakpoints being unsettable. The only way I |
| 1166 | have found to "fix" this problem is to <tt>delete</tt> the breakpoints that are |
| 1167 | already set in your pass, run the program, and re-set the breakpoints once |
| 1168 | execution stops in <tt>PassManager::run</tt>.<p> |
| 1169 | |
| 1170 | </ul> |
| 1171 | |
| 1172 | Hopefully these tips will help with common case debugging situations. If you'd |
| 1173 | like to contribute some tips of your own, just contact <a |
| 1174 | href="mailto:sabre@nondot.org">Chris</a>.<p> |
| 1175 | |
| 1176 | |
| 1177 | <!-- *********************************************************************** --> |
| 1178 | </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0> |
| 1179 | <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 1180 | <a name="future">Future extensions planned |
| 1181 | </b></font></td></tr></table><ul> |
| 1182 | <!-- *********************************************************************** --> |
| 1183 | |
| 1184 | Although the LLVM Pass Infrastructure is very capable as it stands, and does |
| 1185 | some nifty stuff, there are things we'd like to add in the future. Here is |
| 1186 | where we are going:<p> |
| 1187 | |
| 1188 | <!-- _______________________________________________________________________ --> |
| 1189 | </ul><h4><a name="SMP"><hr size=0>Multithreaded LLVM</h4><ul> |
| 1190 | |
Chris Lattner | ed2e7a9 | 2002-09-17 16:47:06 +0000 | [diff] [blame] | 1191 | Multiple CPU machines are becoming more common and compilation can never be |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 1192 | fast enough: obviously we should allow for a multithreaded compiler. Because of |
| 1193 | the semantics defined for passes above (specifically they cannot maintain state |
| 1194 | across invocations of their <tt>run*</tt> methods), a nice clean way to |
| 1195 | implement a multithreaded compiler would be for the <tt>PassManager</tt> class |
Misha Brukman | bc0e998 | 2003-07-14 17:20:40 +0000 | [diff] [blame] | 1196 | to create multiple instances of each pass object, and allow the separate |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 1197 | instances to be hacking on different parts of the program at the same time.<p> |
| 1198 | |
| 1199 | This implementation would prevent each of the passes from having to implement |
| 1200 | multithreaded constructs, requiring only the LLVM core to have locking in a few |
| 1201 | places (for global resources). Although this is a simple extension, we simply |
| 1202 | haven't had time (or multiprocessor machines, thus a reason) to implement this. |
| 1203 | Despite that, we have kept the LLVM passes SMP ready, and you should too.<p> |
| 1204 | |
| 1205 | |
| 1206 | <!-- _______________________________________________________________________ --> |
| 1207 | </ul><h4><a name="ModuleSource"><hr size=0>A new <tt>ModuleSource</tt> interface</h4><ul> |
| 1208 | |
| 1209 | Currently, the <tt>PassManager</tt>'s <tt>run</tt> method takes a <tt><a |
| 1210 | href="http://llvm.cs.uiuc.edu/doxygen/classModule.html">Module</a></tt> as |
| 1211 | input, and runs all of the passes on this module. The problem with this |
| 1212 | approach is that none of the <tt>PassManager</tt> features can be used for |
| 1213 | timing and debugging the actual <b>loading</b> of the module from disk or |
| 1214 | standard input.<p> |
| 1215 | |
| 1216 | To solve this problem, eventually the <tt>PassManger</tt> class will accept a |
| 1217 | <tt>ModuleSource</tt> object instead of a Module itself. When complete, this |
| 1218 | will also allow for streaming of functions out of the bytecode representation, |
| 1219 | allowing us to avoid holding the entire program in memory at once if we only are |
| 1220 | dealing with <a href="#FunctionPass">FunctionPass</a>'s.<p> |
| 1221 | |
| 1222 | As part of a different issue, eventually the bytecode loader will be extended to |
| 1223 | allow on-demand loading of functions from the bytecode representation, in order |
| 1224 | to better support the runtime reoptimizer. The bytecode format is already |
| 1225 | capable of this, the loader just needs to be reworked a bit.<p> |
| 1226 | |
| 1227 | |
| 1228 | <!-- _______________________________________________________________________ --> |
| 1229 | </ul><h4><a name="PassFunctionPass"><hr size=0><tt>Pass</tt>'s requiring <tt>FunctionPass</tt>'s</h4><ul> |
| 1230 | |
| 1231 | Currently it is illegal for a <a href="#Pass"><tt>Pass</tt></a> to require a <a |
| 1232 | href="#FunctionPass"><tt>FunctionPass</tt></a>. This is because there is only |
| 1233 | one instance of the <a href="#FunctionPass"><tt>FunctionPass</tt></a> object |
| 1234 | ever created, thus nowhere to store information for all of the functions in the |
| 1235 | program at the same time. Although this has come up a couple of times before, |
| 1236 | this has always been worked around by factoring one big complicated pass into a |
| 1237 | global and an interprocedural part, both of which are distinct. In the future, |
| 1238 | it would be nice to have this though.<p> |
| 1239 | |
| 1240 | Note that it is no problem for a <a |
| 1241 | href="#FunctionPass"><tt>FunctionPass</tt></a> to require the results of a <a |
| 1242 | href="#Pass"><tt>Pass</tt></a>, only the other way around.<p> |
| 1243 | |
| 1244 | |
| 1245 | <!-- *********************************************************************** --> |
| 1246 | </ul> |
| 1247 | <!-- *********************************************************************** --> |
| 1248 | |
| 1249 | <hr><font size-1> |
Chris Lattner | 480e2ef | 2002-09-06 02:02:58 +0000 | [diff] [blame] | 1250 | <address><a href="mailto:sabre@nondot.org">Chris Lattner</a></address> |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 1251 | <!-- Created: Tue Aug 6 15:00:33 CDT 2002 --> |
| 1252 | <!-- hhmts start --> |
Misha Brukman | bc0e998 | 2003-07-14 17:20:40 +0000 | [diff] [blame] | 1253 | Last modified: Mon Jul 14 12:12:53 CDT 2003 |
Chris Lattner | c6bb824 | 2002-08-08 20:11:18 +0000 | [diff] [blame] | 1254 | <!-- hhmts end --> |
| 1255 | </font></body></html> |