comparison docs/WritingAnLLVMPass.rst @ 83:60c9769439b8

LLVM 3.7
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Wed, 18 Feb 2015 14:55:36 +0900
parents 54457678186b
children afa8332a0e37
comparison
equal deleted inserted replaced
78:af83660cff7b 83:60c9769439b8
144 This declares pass identifier used by LLVM to identify pass. This allows LLVM 144 This declares pass identifier used by LLVM to identify pass. This allows LLVM
145 to avoid using expensive C++ runtime information. 145 to avoid using expensive C++ runtime information.
146 146
147 .. code-block:: c++ 147 .. code-block:: c++
148 148
149 virtual bool runOnFunction(Function &F) { 149 bool runOnFunction(Function &F) override {
150 errs() << "Hello: "; 150 errs() << "Hello: ";
151 errs().write_escaped(F.getName()) << "\n"; 151 errs().write_escaped(F.getName()) << "\n";
152 return false; 152 return false;
153 } 153 }
154 }; // end of struct Hello 154 }; // end of struct Hello
192 namespace { 192 namespace {
193 struct Hello : public FunctionPass { 193 struct Hello : public FunctionPass {
194 static char ID; 194 static char ID;
195 Hello() : FunctionPass(ID) {} 195 Hello() : FunctionPass(ID) {}
196 196
197 virtual bool runOnFunction(Function &F) { 197 bool runOnFunction(Function &F) override {
198 errs() << "Hello: "; 198 errs() << "Hello: ";
199 errs().write_escaped(F.getName()) << '\n'; 199 errs().write_escaped(F.getName()) << '\n';
200 return false; 200 return false;
201 } 201 }
202 }; 202 };
432 .. code-block:: c++ 432 .. code-block:: c++
433 433
434 virtual bool doFinalization(CallGraph &CG); 434 virtual bool doFinalization(CallGraph &CG);
435 435
436 The ``doFinalization`` method is an infrequently used method that is called 436 The ``doFinalization`` method is an infrequently used method that is called
437 when the pass framework has finished calling :ref:`runOnFunction 437 when the pass framework has finished calling :ref:`runOnSCC
438 <writing-an-llvm-pass-runOnFunction>` for every function in the program being 438 <writing-an-llvm-pass-runOnSCC>` for every SCC in the program being compiled.
439 compiled.
440 439
441 .. _writing-an-llvm-pass-FunctionPass: 440 .. _writing-an-llvm-pass-FunctionPass:
442 441
443 The ``FunctionPass`` class 442 The ``FunctionPass`` class
444 -------------------------- 443 --------------------------
454 To be explicit, ``FunctionPass`` subclasses are not allowed to: 453 To be explicit, ``FunctionPass`` subclasses are not allowed to:
455 454
456 #. Inspect or modify a ``Function`` other than the one currently being processed. 455 #. Inspect or modify a ``Function`` other than the one currently being processed.
457 #. Add or remove ``Function``\ s from the current ``Module``. 456 #. Add or remove ``Function``\ s from the current ``Module``.
458 #. Add or remove global variables from the current ``Module``. 457 #. Add or remove global variables from the current ``Module``.
459 #. Maintain state across invocations of:ref:`runOnFunction 458 #. Maintain state across invocations of :ref:`runOnFunction
460 <writing-an-llvm-pass-runOnFunction>` (including global data). 459 <writing-an-llvm-pass-runOnFunction>` (including global data).
461 460
462 Implementing a ``FunctionPass`` is usually straightforward (See the :ref:`Hello 461 Implementing a ``FunctionPass`` is usually straightforward (See the :ref:`Hello
463 World <writing-an-llvm-pass-basiccode>` pass for example). 462 World <writing-an-llvm-pass-basiccode>` pass for example).
464 ``FunctionPass``\ es may overload three virtual methods to do their work. All 463 ``FunctionPass``\ es may overload three virtual methods to do their work. All
852 .. code-block:: c++ 851 .. code-block:: c++
853 852
854 // This example modifies the program, but does not modify the CFG 853 // This example modifies the program, but does not modify the CFG
855 void LICM::getAnalysisUsage(AnalysisUsage &AU) const { 854 void LICM::getAnalysisUsage(AnalysisUsage &AU) const {
856 AU.setPreservesCFG(); 855 AU.setPreservesCFG();
857 AU.addRequired<LoopInfo>(); 856 AU.addRequired<LoopInfoWrapperPass>();
858 } 857 }
859 858
860 .. _writing-an-llvm-pass-getAnalysis: 859 .. _writing-an-llvm-pass-getAnalysis:
861 860
862 The ``getAnalysis<>`` and ``getAnalysisIfAvailable<>`` methods 861 The ``getAnalysis<>`` and ``getAnalysisIfAvailable<>`` methods
869 you want, and returns a reference to that pass. For example: 868 you want, and returns a reference to that pass. For example:
870 869
871 .. code-block:: c++ 870 .. code-block:: c++
872 871
873 bool LICM::runOnFunction(Function &F) { 872 bool LICM::runOnFunction(Function &F) {
874 LoopInfo &LI = getAnalysis<LoopInfo>(); 873 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
875 //... 874 //...
876 } 875 }
877 876
878 This method call returns a reference to the pass desired. You may get a 877 This method call returns a reference to the pass desired. You may get a
879 runtime assertion failure if you attempt to get an analysis that you did not 878 runtime assertion failure if you attempt to get an analysis that you did not
1161 <writing-an-llvm-pass-getAnalysisUsage>` method to our pass: 1160 <writing-an-llvm-pass-getAnalysisUsage>` method to our pass:
1162 1161
1163 .. code-block:: c++ 1162 .. code-block:: c++
1164 1163
1165 // We don't modify the program, so we preserve all analyses 1164 // We don't modify the program, so we preserve all analyses
1166 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 1165 void getAnalysisUsage(AnalysisUsage &AU) const override {
1167 AU.setPreservesAll(); 1166 AU.setPreservesAll();
1168 } 1167 }
1169 1168
1170 Now when we run our pass, we get this output: 1169 Now when we run our pass, we get this output:
1171 1170