X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=docs%2FWritingAnLLVMPass.html;h=07e736da691e55ae8a2aa1325e72dc0c9e2205f1;hb=53ebe352ab265aa029d16fe27426e32f07fef609;hp=3b116074b716bcb6792022300bb2f58c68d39819;hpb=2e8f27d0bdb2b20ac6582b53e2ddf2bfc5c28411;p=oota-llvm.git diff --git a/docs/WritingAnLLVMPass.html b/docs/WritingAnLLVMPass.html index 3b116074b71..07e736da691 100644 --- a/docs/WritingAnLLVMPass.html +++ b/docs/WritingAnLLVMPass.html @@ -103,8 +103,6 @@
  • Future extensions planned
  • @@ -264,6 +262,14 @@ href="#passtype">later, but for now, know that FunctionPass's operate a function at a time.

    +
    +     static char ID;
    +     Hello() : FunctionPass((intptr_t)&ID) {}
    +

    + +

    This declares pass identifier used by LLVM to identify pass. This allows LLVM to +avoid using expensive C++ runtime information.

    +
         virtual bool runOnFunction(Function &F) {
           llvm::cerr << "Hello: " << F.getName() << "\n";
    @@ -279,13 +285,26 @@ to do our thing, so we just print out our message with the name of each
     function.

    -  RegisterPass<Hello> X("hello", "Hello World Pass");
    +  char Hello::ID = 0;
    +
    + +

    We initialize pass ID here. LLVM uses ID's address to identify pass so +initialization value is not important.

    + +
    +  RegisterPass<Hello> X("hello", "Hello World Pass",
    +                        false /* Only looks at CFG */,
    +                        false /* Analysis Pass */);
     }  // end of anonymous namespace
     

    Lastly, we register our class Hello, giving it a command line -argument "hello", and a name "Hello World Pass".

    +argument "hello", and a name "Hello World Pass". +Last two RegisterPass arguments are optional. Their default value is false. +If a pass walks CFG without modifying it then third argument is set to true. +If a pass is an analysis pass, for example dominator tree pass, then true +is supplied as fourth argument.

    As a whole, the .cpp file looks like:

    @@ -297,12 +316,17 @@ argument "hello", and a name "Hello World Pass".

    namespace { struct Hello : public FunctionPass { + + static char ID; + Hello() : FunctionPass((intptr_t)&ID) {} + virtual bool runOnFunction(Function &F) { llvm::cerr << "Hello: " << F.getName() << "\n"; return false; } }; + char Hello::ID = 0; RegisterPass<Hello> X("hello", "Hello World Pass"); }
    @@ -330,8 +354,8 @@ use the opt tool to access it, once loaded.

    To test it, follow the example at the end of the Getting Started Guide to compile "Hello World" to -LLVM. We can now run the bytecode file (hello.bc) for the program -through our transformation like this (or course, any bytecode file will +LLVM. We can now run the bitcode file (hello.bc) for the program +through our transformation like this (or course, any bitcode file will work):

    @@ -355,7 +379,7 @@ interesting way, we just throw away the result of opt (sending it to
     $ opt -load ../../../Debug/lib/Hello.so --help
     OVERVIEW: llvm .bc -> .bc modular optimizer
     
    -USAGE: opt [options] <input bytecode>
    +USAGE: opt [options] <input bitcode>
     
     OPTIONS:
       Optimizations available:
    @@ -390,7 +414,7 @@ Hello: main
       Total Execution Time: 0.02 seconds (0.0479059 wall clock)
     
        ---User Time---   --System Time--   --User+System--   ---Wall Time---  --- Pass Name ---
    -   0.0100 (100.0%)   0.0000 (  0.0%)   0.0100 ( 50.0%)   0.0402 ( 84.0%)  Bytecode Writer
    +   0.0100 (100.0%)   0.0000 (  0.0%)   0.0100 ( 50.0%)   0.0402 ( 84.0%)  Bitcode Writer
        0.0000 (  0.0%)   0.0100 (100.0%)   0.0100 ( 50.0%)   0.0031 (  6.4%)  Dominator Set Construction
        0.0000 (  0.0%)   0.0000 (  0.0%)   0.0000 (  0.0%)   0.0013 (  2.7%)  Module Verifier
        0.0000 (  0.0%)   0.0000 (  0.0%)   0.0000 (  0.0%)   0.0033 (  6.9%)  Hello World Pass
    @@ -466,7 +490,9 @@ class is the most general of all superclasses that you can use.  Deriving from
     ModulePass indicates that your pass uses the entire program as a unit,
     refering to function bodies in no predictable order, or adding and removing
     functions.  Because nothing is known about the behavior of ModulePass
    -subclasses, no optimization can be done for their execution.

    +subclasses, no optimization can be done for their execution. A module pass +can use function level passes (e.g. dominators) using getAnalysis interface + getAnalysis<DominatorTree>(Function).

    To write a correct ModulePass subclass, derive from ModulePass and overload the runOnModule method with the @@ -518,7 +544,7 @@ href="#BasicBlockPass">BasicBlockPass, you should derive from

  • ... not allowed to modify any Functions that are not in the current SCC.
  • -
  • ... allowed to inspect any Function's other than those in the +
  • ... not allowed to inspect any Function's other than those in the current SCC and the direct callees of the SCC.
  • ... required to preserve the current CallGraph object, updating it @@ -727,7 +753,7 @@ program, or false if they didn't.

    virtual bool doInitialization(Loop *, LPPassManager &LPM);
  • -The doInitialization method is designed to do simple initialization +

    The doInitialization method is designed to do simple initialization type of stuff that does not depend on the functions being processed. The doInitialization method call is not scheduled to overlap with any other pass executions (thus it should be very fast). LPPassManager @@ -976,7 +1002,7 @@ depended on.

    -

    One of the main responsibilities of the PassManager is the make sure +

    One of the main responsibilities of the PassManager is to make sure that passes interact with each other correctly. Because PassManager tries to optimize the execution of passes it must know how the passes interact with each other and what dependencies exist between @@ -1127,7 +1153,21 @@ runtime assertion failure if you attempt to get an analysis that you did not declare as required in your getAnalysisUsage implementation. This method can be called by your run* method implementation, or by any -other local method invoked by your run* method.

    +other local method invoked by your run* method. + +A module level pass can use function level analysis info using this interface. +For example:

    + +
    +   bool ModuleLevelPass::runOnModule(Module &M) {
    +     ...
    +     DominatorTree &DT = getAnalysis<DominatorTree>(Func);
    +     ...
    +   }
    +
    + +

    In above example, runOnFunction for DominatorTree is called by pass manager +before returning a reference to the desired pass.

    If your pass is capable of updating analyses if they exist (e.g., @@ -1153,7 +1193,7 @@ it is active. For example:

    -

    Now that we understand the basics of how passes are defined, how the are +

    Now that we understand the basics of how passes are defined, how they are used, and how they are required from other passes, it's time to get a little bit fancier. All of the pass relationships that we have seen so far are very simple: one pass depends on one other specific pass to be run before it can run. @@ -1275,8 +1315,9 @@ no problem.

    Here we show how the default implementation is specified (using the extra argument to the RegisterAnalysisGroup template). There must be exactly one default implementation available at all times for an Analysis Group to be -used. Here we declare that the BasicAliasAnalysis +used. Only default implementation can derive from ImmutablePass. +Here we declare that the + BasicAliasAnalysis pass is the default implementation for the interface.

    @@ -1341,7 +1382,8 @@ the LLVM program representation for a single function at a time, instead of traversing the entire program. It reduces the memory consumption of compiler, because, for example, only one DominatorSet -needs to be calculated at a time. This also makes it possible some interesting enhancements in the future.

    @@ -1379,8 +1421,8 @@ Module Pass Manager Module Verifier -- Dominator Set Construction -- Module Verifier - Bytecode Writer ---Bytecode Writer + Bitcode Writer +--Bitcode Writer

    This output shows us when passes are constructed and when the analysis @@ -1420,8 +1462,8 @@ Module Pass Manager Module Verifier -- Dominator Set Construction -- Module Verifier - Bytecode Writer ---Bytecode Writer + Bitcode Writer +--Bitcode Writer Hello: __main Hello: puts Hello: main @@ -1460,8 +1502,8 @@ Module Pass Manager Module Verifier -- Dominator Set Construction -- Module Verifier - Bytecode Writer ---Bytecode Writer + Bitcode Writer +--Bitcode Writer Hello: __main Hello: puts Hello: main @@ -1766,29 +1808,6 @@ Despite that, we have kept the LLVM passes SMP ready, and you should too.

    - -
    -ModulePasses requiring FunctionPasses -
    - -
    - -

    Currently it is illegal for a ModulePass -to require a FunctionPass. This is because -there is only one instance of the FunctionPass object ever created, thus nowhere -to store information for all of the functions in the program at the same time. -Although this has come up a couple of times before, this has always been worked -around by factoring one big complicated pass into a global and an -interprocedural part, both of which are distinct. In the future, it would be -nice to have this though.

    - -

    Note that it is no problem for a FunctionPass to require the results of a ModulePass, only the other way around.

    - -
    -