|
59 | 59 | <ul>
|
60 | 60 | <li><a href="#getAnalysisUsage">The <tt>getAnalysisUsage</tt>
|
61 | 61 | method</a></li>
|
62 |
| - <li><a href="#getAnalysis">The <tt>getAnalysis</tt> method</a></li> |
| 62 | + <li><a href="#AU::addRequired">The <tt>AnalysisUsage::addRequired<></tt> and <tt>AnalysisUsage::addRequiredTransitive<></tt> methods</a></li> |
| 63 | + <li><a href="#AU::addPreserved">The <tt>AnalysisUsage::addPreserved<></tt> method</a></li> |
| 64 | + <li><a href="#AU::examples">Example implementations of <tt>getAnalysisUsage</tt></a></li> |
| 65 | + <li><a href="#getAnalysis">The <tt>getAnalysis<></tt> and <tt>getAnalysisToUpdate<></tt> methods</a></li> |
63 | 66 | </ul></li>
|
64 | 67 | <li><a href="#analysisgroup">Implementing Analysis Groups</a>
|
65 | 68 | <ul>
|
|
171 | 174 | <p>This makefile specifies that all of the <tt>.cpp</tt> files in the current
|
172 | 175 | directory are to be compiled and linked together into a
|
173 | 176 | <tt>lib/Debug/libhello.so</tt> shared object that can be dynamically loaded by
|
174 |
| -the <tt>opt</tt> or <tt>analyze</tt> tools.</p> |
175 |
| - |
176 |
| -<p> |
177 |
| -Note that the suffix of the shared library may differ from the example above if |
178 |
| -your system uses a different suffix by default. |
179 |
| -</p> |
| 177 | +the <tt>opt</tt> or <tt>analyze</tt> tools. If your operating system uses a |
| 178 | +suffix other than .so (such as windows of Mac OS/X), the appropriate extension |
| 179 | +will be used.</p> |
180 | 180 |
|
181 | 181 | <p>Now that we have the build scripts set up, we just need to write the code for
|
182 | 182 | the pass itself.</p>
|
|
487 | 487 | </div>
|
488 | 488 |
|
489 | 489 | <!-- _______________________________________________________________________ -->
|
490 |
| -<div class="subsubsection"> |
| 490 | +<div class="doc_subsubsection"> |
491 | 491 | <a name="doInitialization_mod">The <tt>doInitialization(Module &)</tt>
|
492 | 492 | method</a>
|
493 | 493 | </div>
|
|
516 | 516 | </div>
|
517 | 517 |
|
518 | 518 | <!-- _______________________________________________________________________ -->
|
519 |
| -<div class="subsubsection"> |
| 519 | +<div class="doc_subsubsection"> |
520 | 520 | <a name="runOnFunction">The <tt>runOnFunction</tt> method</a>
|
521 | 521 | </div>
|
522 | 522 |
|
|
533 | 533 | </div>
|
534 | 534 |
|
535 | 535 | <!-- _______________________________________________________________________ -->
|
536 |
| -<div class="subsubsection"> |
| 536 | +<div class="doc_subsubsection"> |
537 | 537 | <a name="doFinalization_mod">The <tt>doFinalization(Module
|
538 | 538 | &)</tt> method</a>
|
539 | 539 | </div>
|
|
581 | 581 | </div>
|
582 | 582 |
|
583 | 583 | <!-- _______________________________________________________________________ -->
|
584 |
| -<div class="subsubsection"> |
| 584 | +<div class="doc_subsubsection"> |
585 | 585 | <a name="doInitialization_fn">The <tt>doInitialization(Function
|
586 | 586 | &)</tt> method</a>
|
587 | 587 | </div>
|
|
603 | 603 | </div>
|
604 | 604 |
|
605 | 605 | <!-- _______________________________________________________________________ -->
|
606 |
| -<div class="subsubsection"> |
| 606 | +<div class="doc_subsubsection"> |
607 | 607 | <a name="runOnBasicBlock">The <tt>runOnBasicBlock</tt> method</a>
|
608 | 608 | </div>
|
609 | 609 |
|
|
621 | 621 | </div>
|
622 | 622 |
|
623 | 623 | <!-- _______________________________________________________________________ -->
|
624 |
| -<div class="subsubsection"> |
| 624 | +<div class="doc_subsubsection"> |
625 | 625 | <a name="doFinalization_fn">The <tt>doFinalization(Function &)</tt>
|
626 | 626 | method</a>
|
627 | 627 | </div>
|
|
647 | 647 |
|
648 | 648 | <div class="doc_text">
|
649 | 649 |
|
650 |
| -<p>A <tt>MachineFunctionPass</tt> executes on the machine-dependent |
651 |
| -representation of each LLVM function in the program, |
652 |
| -independent of all of the other functions in the program. |
653 |
| -A <tt>MachineFunctionPass</tt> is also a <tt>FunctionPass</tt>, so all |
| 650 | +<p>A <tt>MachineFunctionPass</tt> is a part of the LLVM code generator that |
| 651 | +executes on the machine-dependent representation of each LLVM function in the |
| 652 | +program. A <tt>MachineFunctionPass</tt> is also a <tt>FunctionPass</tt>, so all |
654 | 653 | the restrictions that apply to a <tt>FunctionPass</tt> also apply to it.
|
655 |
| -<tt>MachineFunctionPass</tt>es also have additional restrictions. In |
656 |
| -particular, <tt>MachineFunctionPass</tt>es are not allowed to do any of |
657 |
| -the following:</p> |
| 654 | +<tt>MachineFunctionPass</tt>es also have additional restrictions. In particular, |
| 655 | +<tt>MachineFunctionPass</tt>es are not allowed to do any of the following:</p> |
658 | 656 |
|
659 | 657 | <ol>
|
660 | 658 | <li>Modify any LLVM Instructions, BasicBlocks or Functions.</li>
|
|
669 | 667 | </div>
|
670 | 668 |
|
671 | 669 | <!-- _______________________________________________________________________ -->
|
672 |
| -<div class="subsubsection"> |
| 670 | +<div class="doc_subsubsection"> |
673 | 671 | <a name="runOnMachineFunction">The <tt>runOnMachineFunction(MachineFunction
|
674 | 672 | &MF)</tt> method</a>
|
675 | 673 | </div>
|
|
718 | 716 |
|
719 | 717 | <li><b><tt>RegisterAnalysis</tt></b> - This template should be used when you are
|
720 | 718 | registering a pass that logically should be available for use in the
|
721 |
| -'<tt>analysis</tt>' utility.</li> |
722 |
| - |
723 |
| -<li><b><tt>RegisterLLC</tt></b> - This template should be used when you are |
724 |
| -registering a pass that logically should be available for use in the |
725 |
| -'<tt>llc</tt>' utility.</li> |
| 719 | +'<tt>analyze</tt>' utility.</li> |
726 | 720 |
|
727 | 721 | <li><b><tt>RegisterPass</tt></b> - This is the generic form of the
|
728 | 722 | <tt>Register*</tt> templates that should be used if you want your pass listed by
|
|
740 | 734 | the pass, which is to be used for the <tt>--help</tt> output of programs, as
|
741 | 735 | well as for debug output generated by the <tt>--debug-pass</tt> option.</p>
|
742 | 736 |
|
743 |
| -<p>If you pass is constructed by its default constructor, you only ever have to |
744 |
| -pass these two arguments. If, on the other hand, you require other information |
745 |
| -(like target specific information), you must pass an additional argument. This |
746 |
| -argument is a pointer to a function used to create the pass. For an example of |
747 |
| -how this works, look at the <a |
748 |
| -href="http://llvm.cs.uiuc.edu/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations.cpp</a> |
749 |
| -file.</p> |
750 |
| - |
751 | 737 | <p>If a pass is registered to be used by the <tt>analyze</tt> utility, you
|
752 | 738 | should implement the virtual <tt>print</tt> method:</p>
|
753 | 739 |
|
|
819 | 805 | should fill in the <tt><a
|
820 | 806 | href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AnalysisUsage.html">AnalysisUsage</a></tt>
|
821 | 807 | object with information about which passes are required and not invalidated. To
|
822 |
| -do this, the following set methods are provided by the <tt><a |
823 |
| -href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AnalysisUsage.html">AnalysisUsage</a></tt> |
824 |
| -class:</p> |
| 808 | +do this, a pass may call any of the following methods on the AnalysisUsage |
| 809 | +object:</p> |
| 810 | +</div> |
825 | 811 |
|
826 |
| -<pre> |
827 |
| - <i>// addRequires - Add the specified pass to the required set for your pass.</i> |
828 |
| - <b>template</b><<b>class</b> PassClass> |
829 |
| - AnalysisUsage &AnalysisUsage::addRequired(); |
830 |
| - |
831 |
| - <i>// addPreserved - Add the specified pass to the set of analyses preserved by |
832 |
| - // this pass</i> |
833 |
| - <b>template</b><<b>class</b> PassClass> |
834 |
| - AnalysisUsage &AnalysisUsage::addPreserved(); |
835 |
| - |
836 |
| - <i>// setPreservesAll - Call this if the pass does not modify its input at all</i> |
837 |
| - <b>void</b> AnalysisUsage::setPreservesAll(); |
838 |
| - |
839 |
| - <i>// setPreservesCFG - This function should be called by the pass, iff they do not: |
840 |
| - // |
841 |
| - // 1. Add or remove basic blocks from the function |
842 |
| - // 2. Modify terminator instructions in any way. |
843 |
| - // |
844 |
| - // This is automatically implied for <a href="#BasicBlockPass">BasicBlockPass</a>'s |
845 |
| - //</i> |
846 |
| - <b>void</b> AnalysisUsage::setPreservesCFG(); |
847 |
| -</pre> |
| 812 | +<!-- _______________________________________________________________________ --> |
| 813 | +<div class="doc_subsubsection"> |
| 814 | + <a name="AU::addRequired">The <tt>AnalysisUsage::addRequired<></tt> and <tt>AnalysisUsage::addRequiredTransitive<></tt> methods</a> |
| 815 | +</div> |
| 816 | + |
| 817 | +<div class="doc_text"> |
| 818 | +<p> |
| 819 | +If you pass requires a previous pass to be executed (an analysis for example), |
| 820 | +it can use one of these methods to arrange for it to be run before your pass. |
| 821 | +LLVM has many different types of analyses and passes that can be required, |
| 822 | +spaning the range from <tt>DominatorSet</tt> to <tt>BreakCriticalEdges</tt>. |
| 823 | +requiring <tt>BreakCriticalEdges</tt>, for example, guarantees that there will |
| 824 | +be no critical edges in the CFG when your pass has been run. |
| 825 | +</p> |
| 826 | + |
| 827 | +<p> |
| 828 | +Some analyses chain to other analyses to do their job. For example, an <a |
| 829 | +href="AliasAnalysis.html">AliasAnalysis</a> implementation is required to <a |
| 830 | +href="AliasAnalysis.html#chaining">chain</a> to other alias analysis passes. In |
| 831 | +cases where analyses chain, the <tt>addRequiredTransitive</tt> method should be |
| 832 | +used instead of the <tt>addRequired</tt> method. This informs the PassManager |
| 833 | +that the transitively required pass should be alive as long as the requiring |
| 834 | +pass is. |
| 835 | +</p> |
| 836 | +</div> |
| 837 | + |
| 838 | +<!-- _______________________________________________________________________ --> |
| 839 | +<div class="doc_subsubsection"> |
| 840 | + <a name="AU::addPreserved">The <tt>AnalysisUsage::addPreserved<></tt> method</a> |
| 841 | +</div> |
| 842 | + |
| 843 | +<div class="doc_text"> |
| 844 | +<p> |
| 845 | +One of the jobs of the PassManager is to optimize how and when analyses are run. |
| 846 | +In particular, it attempts to avoid recomputing data unless it needs to. For |
| 847 | +this reason, passes are allowed to declare that they preserve (i.e., they don't |
| 848 | +invalidate) an existing analysis if it's available. For example, a simple |
| 849 | +constant folding pass would not modify the CFG, so it can't possible effect the |
| 850 | +results of dominator analysis. By default, all passes are assumed to invalidate |
| 851 | +all others. |
| 852 | +</p> |
| 853 | + |
| 854 | +<p> |
| 855 | +The <tt>AnalysisUsage</tt> class provides several methods which are useful in |
| 856 | +certain circumstances that are related to <tt>addPreserved</tt>. In particular, |
| 857 | +the <tt>setPreservesAll</tt> method can be called to indicate that the pass does |
| 858 | +not modify the LLVM program at all (which is true for analyses), and the |
| 859 | +<tt>setPreservesCFG</tt> method can be used by transformations that change |
| 860 | +instructions in the program but do not modify the CFG or terminator instructions |
| 861 | +(note that this property is implicitly set for <a |
| 862 | +href="#BasicBlockPass">BasicBlockPass</a>'s). |
| 863 | +</p> |
| 864 | + |
| 865 | +<p> |
| 866 | +<tt>addPreserved</tt> is particularly useful for transformations like |
| 867 | +<tt>BreakCriticalEdges</tt>. This pass knows how to update a small set of loop |
| 868 | +and dominator related analyses if they exist, so it can preserve them, despite |
| 869 | +the fact that it hacks on the CFG. |
| 870 | +</p> |
| 871 | +</div> |
| 872 | + |
| 873 | +<!-- _______________________________________________________________________ --> |
| 874 | +<div class="doc_subsubsection"> |
| 875 | + <a name="AU::examples">Example implementations of <tt>getAnalysisUsage</tt></a> |
| 876 | +</div> |
848 | 877 |
|
849 |
| -<p>Some examples of how to use these methods are:</p> |
| 878 | +<div class="doc_text"> |
850 | 879 |
|
851 | 880 | <pre>
|
852 | 881 | <i>// This is an example implementation from an analysis, which does not modify
|
|
871 | 900 |
|
872 | 901 | <!-- _______________________________________________________________________ -->
|
873 | 902 | <div class="doc_subsubsection">
|
874 |
| - <a name="getAnalysis">The <tt>getAnalysis<></tt> method</a> |
| 903 | + <a name="getAnalysis">The <tt>getAnalysis<></tt> and <tt>getAnalysisToUpdate<></tt> methods</a> |
875 | 904 | </div>
|
876 | 905 |
|
877 | 906 | <div class="doc_text">
|
878 | 907 |
|
879 |
| -<p>The <tt>Pass::getAnalysis<></tt> method is inherited by your class, |
880 |
| -providing you with access to the passes that you declared that you required with |
881 |
| -the <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method. It takes |
882 |
| -a single template argument that specifies which pass class you want, and returns |
883 |
| -a reference to that pass.</p> |
| 908 | +<p>The <tt>Pass::getAnalysis<></tt> method is automatically inherited by |
| 909 | +your class, providing you with access to the passes that you declared that you |
| 910 | +required with the <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> |
| 911 | +method. It takes a single template argument that specifies which pass class you |
| 912 | +want, and returns a reference to that pass. For example:</p> |
884 | 913 |
|
885 | 914 | <pre>
|
886 |
| - <b>template</b><<b>typename</b> PassClass> |
887 |
| - AnalysisType &getAnalysis(); |
| 915 | + bool LICM::runOnFunction(Function &F) { |
| 916 | + LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 917 | + ... |
| 918 | + } |
888 | 919 | </pre>
|
889 | 920 |
|
890 | 921 | <p>This method call returns a reference to the pass desired. You may get a
|
|
894 | 925 | method can be called by your <tt>run*</tt> method implementation, or by any
|
895 | 926 | other local method invoked by your <tt>run*</tt> method.</p>
|
896 | 927 |
|
| 928 | +<p> |
| 929 | +If your pass is capable of updating analyses if they exist (e.g., |
| 930 | +<tt>BreakCriticalEdges</tt>, as described above), you can use the |
| 931 | +<tt>getAnalysisToUpdate</tt> method, which returns a pointer to the analysis if |
| 932 | +it is active. For example:</p> |
| 933 | + |
| 934 | +<pre> |
| 935 | + ... |
| 936 | + if (DominatorSet *DS = getAnalysisToUpdate<DominatorSet>()) { |
| 937 | + <i>// A DominatorSet is active. This code will update it.</i> |
| 938 | + } |
| 939 | + ... |
| 940 | +</pre> |
| 941 | + |
897 | 942 | </div>
|
898 | 943 |
|
899 | 944 | <!-- *********************************************************************** -->
|
|
924 | 969 | </div>
|
925 | 970 |
|
926 | 971 | <!-- _______________________________________________________________________ -->
|
927 |
| -<div class="subsubsection"> |
| 972 | +<div class="doc_subsubsection"> |
928 | 973 | <a name="agconcepts">Analysis Group Concepts</a>
|
929 | 974 | </div>
|
930 | 975 |
|
|
975 | 1020 | </div>
|
976 | 1021 |
|
977 | 1022 | <!-- _______________________________________________________________________ -->
|
978 |
| -<div class="subsubsection"> |
| 1023 | +<div class="doc_subsubsection"> |
979 | 1024 | <a name="registerag">Using <tt>RegisterAnalysisGroup</tt></a>
|
980 | 1025 | </div>
|
981 | 1026 |
|
|
1345 | 1390 | </div>
|
1346 | 1391 |
|
1347 | 1392 | <!-- _______________________________________________________________________ -->
|
1348 |
| -<div class="subsubsection"> |
| 1393 | +<div class="doc_subsubsection"> |
1349 | 1394 | <a name="SMP">Multithreaded LLVM</a>
|
1350 | 1395 | </div>
|
1351 | 1396 |
|
|
1368 | 1413 | </div>
|
1369 | 1414 |
|
1370 | 1415 | <!-- _______________________________________________________________________ -->
|
1371 |
| -<div class="subsubsection"> |
| 1416 | +<div class="doc_subsubsection"> |
1372 | 1417 | <a name="ModuleSource">A new <tt>ModuleSource</tt> interface</a>
|
1373 | 1418 | </div>
|
1374 | 1419 |
|
|
1395 | 1440 | </div>
|
1396 | 1441 |
|
1397 | 1442 | <!-- _______________________________________________________________________ -->
|
1398 |
| -<div class="subsubsection"> |
| 1443 | +<div class="doc_subsubsection"> |
1399 | 1444 | <a name="PassFunctionPass"><tt>Pass</tt>es requiring <tt>FunctionPass</tt>es</a>
|
1400 | 1445 | </div>
|
1401 | 1446 |
|
|
0 commit comments