Mercurial > hg > CbC > CbC_llvm
comparison docs/ProgrammersManual.rst @ 147:c2174574ed3a
LLVM 10
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 14 Aug 2019 16:55:33 +0900 |
parents | 3a76565eade5 |
children |
comparison
equal
deleted
inserted
replaced
134:3a76565eade5 | 147:c2174574ed3a |
---|---|
161 ``instanceof`` operator, can be abused. In particular, you should not use big | 161 ``instanceof`` operator, can be abused. In particular, you should not use big |
162 chained ``if/then/else`` blocks to check for lots of different variants of | 162 chained ``if/then/else`` blocks to check for lots of different variants of |
163 classes. If you find yourself wanting to do this, it is much cleaner and more | 163 classes. If you find yourself wanting to do this, it is much cleaner and more |
164 efficient to use the ``InstVisitor`` class to dispatch over the instruction | 164 efficient to use the ``InstVisitor`` class to dispatch over the instruction |
165 type directly. | 165 type directly. |
166 | |
167 ``isa_and_nonnull<>``: | |
168 The ``isa_and_nonnull<>`` operator works just like the ``isa<>`` operator, | |
169 except that it allows for a null pointer as an argument (which it then | |
170 returns false). This can sometimes be useful, allowing you to combine several | |
171 null checks into one. | |
166 | 172 |
167 ``cast_or_null<>``: | 173 ``cast_or_null<>``: |
168 The ``cast_or_null<>`` operator works just like the ``cast<>`` operator, | 174 The ``cast_or_null<>`` operator works just like the ``cast<>`` operator, |
169 except that it allows for a null pointer as an argument (which it then | 175 except that it allows for a null pointer as an argument (which it then |
170 propagates). This can sometimes be useful, allowing you to combine several | 176 propagates). This can sometimes be useful, allowing you to combine several |
654 Many kinds of errors have no recovery strategy, the only action that can be | 660 Many kinds of errors have no recovery strategy, the only action that can be |
655 taken is to report them to the user so that the user can attempt to fix the | 661 taken is to report them to the user so that the user can attempt to fix the |
656 environment. In this case representing the error as a string makes perfect | 662 environment. In this case representing the error as a string makes perfect |
657 sense. LLVM provides the ``StringError`` class for this purpose. It takes two | 663 sense. LLVM provides the ``StringError`` class for this purpose. It takes two |
658 arguments: A string error message, and an equivalent ``std::error_code`` for | 664 arguments: A string error message, and an equivalent ``std::error_code`` for |
659 interoperability: | 665 interoperability. It also provides a ``createStringError`` function to simplify |
660 | 666 common usage of this class: |
661 .. code-block:: c++ | 667 |
662 | 668 .. code-block:: c++ |
663 make_error<StringError>("Bad executable", | 669 |
664 make_error_code(errc::executable_format_error")); | 670 // These two lines of code are equivalent: |
671 make_error<StringError>("Bad executable", errc::executable_format_error); | |
672 createStringError(errc::executable_format_error, "Bad executable"); | |
665 | 673 |
666 If you're certain that the error you're building will never need to be converted | 674 If you're certain that the error you're building will never need to be converted |
667 to a ``std::error_code`` you can use the ``inconvertibleErrorCode()`` function: | 675 to a ``std::error_code`` you can use the ``inconvertibleErrorCode()`` function: |
668 | 676 |
669 .. code-block:: c++ | 677 .. code-block:: c++ |
670 | 678 |
671 make_error<StringError>("Bad executable", inconvertibleErrorCode()); | 679 createStringError(inconvertibleErrorCode(), "Bad executable"); |
672 | 680 |
673 This should be done only after careful consideration. If any attempt is made to | 681 This should be done only after careful consideration. If any attempt is made to |
674 convert this error to a ``std::error_code`` it will trigger immediate program | 682 convert this error to a ``std::error_code`` it will trigger immediate program |
675 termination. Unless you are certain that your errors will not need | 683 termination. Unless you are certain that your errors will not need |
676 interoperability you should look for an existing ``std::error_code`` that you | 684 interoperability you should look for an existing ``std::error_code`` that you |
677 can convert to, and even (as painful as it is) consider introducing a new one as | 685 can convert to, and even (as painful as it is) consider introducing a new one as |
678 a stopgap measure. | 686 a stopgap measure. |
687 | |
688 ``createStringError`` can take ``printf`` style format specifiers to provide a | |
689 formatted message: | |
690 | |
691 .. code-block:: c++ | |
692 | |
693 createStringError(errc::executable_format_error, | |
694 "Bad executable: %s", FileName); | |
679 | 695 |
680 Interoperability with std::error_code and ErrorOr | 696 Interoperability with std::error_code and ErrorOr |
681 """"""""""""""""""""""""""""""""""""""""""""""""" | 697 """"""""""""""""""""""""""""""""""""""""""""""""" |
682 | 698 |
683 Many existing LLVM APIs use ``std::error_code`` and its partner ``ErrorOr<T>`` | 699 Many existing LLVM APIs use ``std::error_code`` and its partner ``ErrorOr<T>`` |
803 ... | 819 ... |
804 } | 820 } |
805 | 821 |
806 Like the ExitOnError utility, cantFail simplifies control flow. Their treatment | 822 Like the ExitOnError utility, cantFail simplifies control flow. Their treatment |
807 of error cases is very different however: Where ExitOnError is guaranteed to | 823 of error cases is very different however: Where ExitOnError is guaranteed to |
808 terminate the program on an error input, cantFile simply asserts that the result | 824 terminate the program on an error input, cantFail simply asserts that the result |
809 is success. In debug builds this will result in an assertion failure if an error | 825 is success. In debug builds this will result in an assertion failure if an error |
810 is encountered. In release builds the behavior of cantFail for failure values is | 826 is encountered. In release builds the behavior of cantFail for failure values is |
811 undefined. As such, care must be taken in the use of cantFail: clients must be | 827 undefined. As such, care must be taken in the use of cantFail: clients must be |
812 certain that a cantFail wrapped call really can not fail with the given | 828 certain that a cantFail wrapped call really can not fail with the given |
813 arguments. | 829 arguments. |
923 Building fallible iterators and iterator ranges | 939 Building fallible iterators and iterator ranges |
924 """"""""""""""""""""""""""""""""""""""""""""""" | 940 """"""""""""""""""""""""""""""""""""""""""""""" |
925 | 941 |
926 The archive walking examples above retrieve archive members by index, however | 942 The archive walking examples above retrieve archive members by index, however |
927 this requires considerable boiler-plate for iteration and error checking. We can | 943 this requires considerable boiler-plate for iteration and error checking. We can |
928 clean this up by using ``Error`` with the "fallible iterator" pattern. The usual | 944 clean this up by using the "fallible iterator" pattern, which supports the |
929 C++ iterator patterns do not allow for failure on increment, but we can | 945 following natural iteration idiom for fallible containers like Archive: |
930 incorporate support for it by having iterators hold an Error reference through | |
931 which they can report failure. In this pattern, if an increment operation fails | |
932 the failure is recorded via the Error reference and the iterator value is set to | |
933 the end of the range in order to terminate the loop. This ensures that the | |
934 dereference operation is safe anywhere that an ordinary iterator dereference | |
935 would be safe (i.e. when the iterator is not equal to end). Where this pattern | |
936 is followed (as in the ``llvm::object::Archive`` class) the result is much | |
937 cleaner iteration idiom: | |
938 | 946 |
939 .. code-block:: c++ | 947 .. code-block:: c++ |
940 | 948 |
941 Error Err; | 949 Error Err; |
942 for (auto &Child : Ar->children(Err)) { | 950 for (auto &Child : Ar->children(Err)) { |
943 // Use Child - we only enter the loop when it's valid | 951 // Use Child - only enter the loop when it's valid |
952 | |
953 // Allow early exit from the loop body, since we know that Err is success | |
954 // when we're inside the loop. | |
955 if (BailOutOn(Child)) | |
956 return; | |
957 | |
944 ... | 958 ... |
945 } | 959 } |
946 // Check Err after the loop to ensure it didn't break due to an error. | 960 // Check Err after the loop to ensure it didn't break due to an error. |
947 if (Err) | 961 if (Err) |
948 return Err; | 962 return Err; |
963 | |
964 To enable this idiom, iterators over fallible containers are written in a | |
965 natural style, with their ``++`` and ``--`` operators replaced with fallible | |
966 ``Error inc()`` and ``Error dec()`` functions. E.g.: | |
967 | |
968 .. code-block:: c++ | |
969 | |
970 class FallibleChildIterator { | |
971 public: | |
972 FallibleChildIterator(Archive &A, unsigned ChildIdx); | |
973 Archive::Child &operator*(); | |
974 friend bool operator==(const ArchiveIterator &LHS, | |
975 const ArchiveIterator &RHS); | |
976 | |
977 // operator++/operator-- replaced with fallible increment / decrement: | |
978 Error inc() { | |
979 if (!A.childValid(ChildIdx + 1)) | |
980 return make_error<BadArchiveMember>(...); | |
981 ++ChildIdx; | |
982 return Error::success(); | |
983 } | |
984 | |
985 Error dec() { ... } | |
986 }; | |
987 | |
988 Instances of this kind of fallible iterator interface are then wrapped with the | |
989 fallible_iterator utility which provides ``operator++`` and ``operator--``, | |
990 returning any errors via a reference passed in to the wrapper at construction | |
991 time. The fallible_iterator wrapper takes care of (a) jumping to the end of the | |
992 range on error, and (b) marking the error as checked whenever an iterator is | |
993 compared to ``end`` and found to be inequal (in particular: this marks the | |
994 error as checked throughout the body of a range-based for loop), enabling early | |
995 exit from the loop without redundant error checking. | |
996 | |
997 Instances of the fallible iterator interface (e.g. FallibleChildIterator above) | |
998 are wrapped using the ``make_fallible_itr`` and ``make_fallible_end`` | |
999 functions. E.g.: | |
1000 | |
1001 .. code-block:: c++ | |
1002 | |
1003 class Archive { | |
1004 public: | |
1005 using child_iterator = fallible_iterator<FallibleChildIterator>; | |
1006 | |
1007 child_iterator child_begin(Error &Err) { | |
1008 return make_fallible_itr(FallibleChildIterator(*this, 0), Err); | |
1009 } | |
1010 | |
1011 child_iterator child_end() { | |
1012 return make_fallible_end(FallibleChildIterator(*this, size())); | |
1013 } | |
1014 | |
1015 iterator_range<child_iterator> children(Error &Err) { | |
1016 return make_range(child_begin(Err), child_end()); | |
1017 } | |
1018 }; | |
1019 | |
1020 Using the fallible_iterator utility allows for both natural construction of | |
1021 fallible iterators (using failing ``inc`` and ``dec`` operations) and | |
1022 relatively natural use of c++ iterator/loop idioms. | |
949 | 1023 |
950 .. _function_apis: | 1024 .. _function_apis: |
951 | 1025 |
952 More information on Error and its related utilities can be found in the | 1026 More information on Error and its related utilities can be found in the |
953 Error.h header file. | 1027 Error.h header file. |
1018 using ``std::function``. ``function_ref`` is small enough that it should always | 1092 using ``std::function``. ``function_ref`` is small enough that it should always |
1019 be passed by value. | 1093 be passed by value. |
1020 | 1094 |
1021 .. _DEBUG: | 1095 .. _DEBUG: |
1022 | 1096 |
1023 The ``DEBUG()`` macro and ``-debug`` option | 1097 The ``LLVM_DEBUG()`` macro and ``-debug`` option |
1024 ------------------------------------------- | 1098 ------------------------------------------------ |
1025 | 1099 |
1026 Often when working on your pass you will put a bunch of debugging printouts and | 1100 Often when working on your pass you will put a bunch of debugging printouts and |
1027 other code into your pass. After you get it working, you want to remove it, but | 1101 other code into your pass. After you get it working, you want to remove it, but |
1028 you may need it again in the future (to work out new bugs that you run across). | 1102 you may need it again in the future (to work out new bugs that you run across). |
1029 | 1103 |
1031 you don't want them to always be noisy. A standard compromise is to comment | 1105 you don't want them to always be noisy. A standard compromise is to comment |
1032 them out, allowing you to enable them if you need them in the future. | 1106 them out, allowing you to enable them if you need them in the future. |
1033 | 1107 |
1034 The ``llvm/Support/Debug.h`` (`doxygen | 1108 The ``llvm/Support/Debug.h`` (`doxygen |
1035 <http://llvm.org/doxygen/Debug_8h_source.html>`__) file provides a macro named | 1109 <http://llvm.org/doxygen/Debug_8h_source.html>`__) file provides a macro named |
1036 ``DEBUG()`` that is a much nicer solution to this problem. Basically, you can | 1110 ``LLVM_DEBUG()`` that is a much nicer solution to this problem. Basically, you can |
1037 put arbitrary code into the argument of the ``DEBUG`` macro, and it is only | 1111 put arbitrary code into the argument of the ``LLVM_DEBUG`` macro, and it is only |
1038 executed if '``opt``' (or any other tool) is run with the '``-debug``' command | 1112 executed if '``opt``' (or any other tool) is run with the '``-debug``' command |
1039 line argument: | 1113 line argument: |
1040 | 1114 |
1041 .. code-block:: c++ | 1115 .. code-block:: c++ |
1042 | 1116 |
1043 DEBUG(dbgs() << "I am here!\n"); | 1117 LLVM_DEBUG(dbgs() << "I am here!\n"); |
1044 | 1118 |
1045 Then you can run your pass like this: | 1119 Then you can run your pass like this: |
1046 | 1120 |
1047 .. code-block:: none | 1121 .. code-block:: none |
1048 | 1122 |
1049 $ opt < a.bc > /dev/null -mypass | 1123 $ opt < a.bc > /dev/null -mypass |
1050 <no output> | 1124 <no output> |
1051 $ opt < a.bc > /dev/null -mypass -debug | 1125 $ opt < a.bc > /dev/null -mypass -debug |
1052 I am here! | 1126 I am here! |
1053 | 1127 |
1054 Using the ``DEBUG()`` macro instead of a home-brewed solution allows you to not | 1128 Using the ``LLVM_DEBUG()`` macro instead of a home-brewed solution allows you to not |
1055 have to create "yet another" command line option for the debug output for your | 1129 have to create "yet another" command line option for the debug output for your |
1056 pass. Note that ``DEBUG()`` macros are disabled for non-asserts builds, so they | 1130 pass. Note that ``LLVM_DEBUG()`` macros are disabled for non-asserts builds, so they |
1057 do not cause a performance impact at all (for the same reason, they should also | 1131 do not cause a performance impact at all (for the same reason, they should also |
1058 not contain side-effects!). | 1132 not contain side-effects!). |
1059 | 1133 |
1060 One additional nice thing about the ``DEBUG()`` macro is that you can enable or | 1134 One additional nice thing about the ``LLVM_DEBUG()`` macro is that you can enable or |
1061 disable it directly in gdb. Just use "``set DebugFlag=0``" or "``set | 1135 disable it directly in gdb. Just use "``set DebugFlag=0``" or "``set |
1062 DebugFlag=1``" from the gdb if the program is running. If the program hasn't | 1136 DebugFlag=1``" from the gdb if the program is running. If the program hasn't |
1063 been started yet, you can always just run it with ``-debug``. | 1137 been started yet, you can always just run it with ``-debug``. |
1064 | 1138 |
1065 .. _DEBUG_TYPE: | 1139 .. _DEBUG_TYPE: |
1074 follows: | 1148 follows: |
1075 | 1149 |
1076 .. code-block:: c++ | 1150 .. code-block:: c++ |
1077 | 1151 |
1078 #define DEBUG_TYPE "foo" | 1152 #define DEBUG_TYPE "foo" |
1079 DEBUG(dbgs() << "'foo' debug type\n"); | 1153 LLVM_DEBUG(dbgs() << "'foo' debug type\n"); |
1080 #undef DEBUG_TYPE | 1154 #undef DEBUG_TYPE |
1081 #define DEBUG_TYPE "bar" | 1155 #define DEBUG_TYPE "bar" |
1082 DEBUG(dbgs() << "'bar' debug type\n"); | 1156 LLVM_DEBUG(dbgs() << "'bar' debug type\n"); |
1083 #undef DEBUG_TYPE | 1157 #undef DEBUG_TYPE |
1084 | 1158 |
1085 Then you can run your pass like this: | 1159 Then you can run your pass like this: |
1086 | 1160 |
1087 .. code-block:: none | 1161 .. code-block:: none |
1296 DAG.viewGraph()`` to pop up a window. Alternatively, you can sprinkle calls to | 1370 DAG.viewGraph()`` to pop up a window. Alternatively, you can sprinkle calls to |
1297 these functions in your code in places you want to debug. | 1371 these functions in your code in places you want to debug. |
1298 | 1372 |
1299 Getting this to work requires a small amount of setup. On Unix systems | 1373 Getting this to work requires a small amount of setup. On Unix systems |
1300 with X11, install the `graphviz <http://www.graphviz.org>`_ toolkit, and make | 1374 with X11, install the `graphviz <http://www.graphviz.org>`_ toolkit, and make |
1301 sure 'dot' and 'gv' are in your path. If you are running on Mac OS X, download | 1375 sure 'dot' and 'gv' are in your path. If you are running on macOS, download |
1302 and install the Mac OS X `Graphviz program | 1376 and install the macOS `Graphviz program |
1303 <http://www.pixelglow.com/graphviz/>`_ and add | 1377 <http://www.pixelglow.com/graphviz/>`_ and add |
1304 ``/Applications/Graphviz.app/Contents/MacOS/`` (or wherever you install it) to | 1378 ``/Applications/Graphviz.app/Contents/MacOS/`` (or wherever you install it) to |
1305 your path. The programs need not be present when configuring, building or | 1379 your path. The programs need not be present when configuring, building or |
1306 running LLVM and can simply be installed when needed during an active debug | 1380 running LLVM and can simply be installed when needed during an active debug |
1307 session. | 1381 session. |
1433 ``vector<Type>``: it supports efficient iteration, lays out elements in memory | 1507 ``vector<Type>``: it supports efficient iteration, lays out elements in memory |
1434 order (so you can do pointer arithmetic between elements), supports efficient | 1508 order (so you can do pointer arithmetic between elements), supports efficient |
1435 push_back/pop_back operations, supports efficient random access to its elements, | 1509 push_back/pop_back operations, supports efficient random access to its elements, |
1436 etc. | 1510 etc. |
1437 | 1511 |
1438 The advantage of SmallVector is that it allocates space for some number of | 1512 The main advantage of SmallVector is that it allocates space for some number of |
1439 elements (N) **in the object itself**. Because of this, if the SmallVector is | 1513 elements (N) **in the object itself**. Because of this, if the SmallVector is |
1440 dynamically smaller than N, no malloc is performed. This can be a big win in | 1514 dynamically smaller than N, no malloc is performed. This can be a big win in |
1441 cases where the malloc/free call is far more expensive than the code that | 1515 cases where the malloc/free call is far more expensive than the code that |
1442 fiddles around with the elements. | 1516 fiddles around with the elements. |
1443 | 1517 |
1448 SmallVectors are most useful when on the stack. | 1522 SmallVectors are most useful when on the stack. |
1449 | 1523 |
1450 SmallVector also provides a nice portable and efficient replacement for | 1524 SmallVector also provides a nice portable and efficient replacement for |
1451 ``alloca``. | 1525 ``alloca``. |
1452 | 1526 |
1527 SmallVector has grown a few other minor advantages over std::vector, causing | |
1528 ``SmallVector<Type, 0>`` to be preferred over ``std::vector<Type>``. | |
1529 | |
1530 #. std::vector is exception-safe, and some implementations have pessimizations | |
1531 that copy elements when SmallVector would move them. | |
1532 | |
1533 #. SmallVector understands ``llvm::is_trivially_copyable<Type>`` and uses realloc aggressively. | |
1534 | |
1535 #. Many LLVM APIs take a SmallVectorImpl as an out parameter (see the note | |
1536 below). | |
1537 | |
1538 #. SmallVector with N equal to 0 is smaller than std::vector on 64-bit | |
1539 platforms, since it uses ``unsigned`` (instead of ``void*``) for its size | |
1540 and capacity. | |
1541 | |
1453 .. note:: | 1542 .. note:: |
1454 | 1543 |
1455 Prefer to use ``SmallVectorImpl<T>`` as a parameter type. | 1544 Prefer to use ``SmallVectorImpl<T>`` as a parameter type. |
1456 | 1545 |
1457 In APIs that don't care about the "small size" (most?), prefer to use | 1546 In APIs that don't care about the "small size" (most?), prefer to use |
1480 .. _dss_vector: | 1569 .. _dss_vector: |
1481 | 1570 |
1482 <vector> | 1571 <vector> |
1483 ^^^^^^^^ | 1572 ^^^^^^^^ |
1484 | 1573 |
1485 ``std::vector`` is well loved and respected. It is useful when SmallVector | 1574 ``std::vector<T>`` is well loved and respected. However, ``SmallVector<T, 0>`` |
1486 isn't: when the size of the vector is often large (thus the small optimization | 1575 is often a better option due to the advantages listed above. std::vector is |
1487 will rarely be a benefit) or if you will be allocating many instances of the | 1576 still useful when you need to store more than ``UINT32_MAX`` elements or when |
1488 vector itself (which would waste space for elements that aren't in the | 1577 interfacing with code that expects vectors :). |
1489 container). vector is also useful when interfacing with code that expects | |
1490 vectors :). | |
1491 | 1578 |
1492 One worthwhile note about std::vector: avoid code like this: | 1579 One worthwhile note about std::vector: avoid code like this: |
1493 | 1580 |
1494 .. code-block:: c++ | 1581 .. code-block:: c++ |
1495 | 1582 |
1830 | 1917 |
1831 A sorted 'vector' | 1918 A sorted 'vector' |
1832 ^^^^^^^^^^^^^^^^^ | 1919 ^^^^^^^^^^^^^^^^^ |
1833 | 1920 |
1834 If you intend to insert a lot of elements, then do a lot of queries, a great | 1921 If you intend to insert a lot of elements, then do a lot of queries, a great |
1835 approach is to use a vector (or other sequential container) with | 1922 approach is to use an std::vector (or other sequential container) with |
1836 std::sort+std::unique to remove duplicates. This approach works really well if | 1923 std::sort+std::unique to remove duplicates. This approach works really well if |
1837 your usage pattern has these two distinct phases (insert then query), and can be | 1924 your usage pattern has these two distinct phases (insert then query), and can be |
1838 coupled with a good choice of :ref:`sequential container <ds_sequential>`. | 1925 coupled with a good choice of :ref:`sequential container <ds_sequential>`. |
1839 | 1926 |
1840 This combination provides the several nice properties: the result data is | 1927 This combination provides the several nice properties: the result data is |
1857 representation that guarantees efficient access (for most types, it falls back | 1944 representation that guarantees efficient access (for most types, it falls back |
1858 to :ref:`std::set <dss_set>`, but for pointers it uses something far better, | 1945 to :ref:`std::set <dss_set>`, but for pointers it uses something far better, |
1859 :ref:`SmallPtrSet <dss_smallptrset>`. | 1946 :ref:`SmallPtrSet <dss_smallptrset>`. |
1860 | 1947 |
1861 The magic of this class is that it handles small sets extremely efficiently, but | 1948 The magic of this class is that it handles small sets extremely efficiently, but |
1862 gracefully handles extremely large sets without loss of efficiency. The | 1949 gracefully handles extremely large sets without loss of efficiency. |
1863 drawback is that the interface is quite small: it supports insertion, queries | |
1864 and erasing, but does not support iteration. | |
1865 | 1950 |
1866 .. _dss_smallptrset: | 1951 .. _dss_smallptrset: |
1867 | 1952 |
1868 llvm/ADT/SmallPtrSet.h | 1953 llvm/ADT/SmallPtrSet.h |
1869 ^^^^^^^^^^^^^^^^^^^^^^ | 1954 ^^^^^^^^^^^^^^^^^^^^^^ |
1870 | 1955 |
1871 ``SmallPtrSet`` has all the advantages of ``SmallSet`` (and a ``SmallSet`` of | 1956 ``SmallPtrSet`` has all the advantages of ``SmallSet`` (and a ``SmallSet`` of |
1872 pointers is transparently implemented with a ``SmallPtrSet``), but also supports | 1957 pointers is transparently implemented with a ``SmallPtrSet``). If more than N |
1873 iterators. If more than N insertions are performed, a single quadratically | 1958 insertions are performed, a single quadratically probed hash table is allocated |
1874 probed hash table is allocated and grows as needed, providing extremely | 1959 and grows as needed, providing extremely efficient access (constant time |
1875 efficient access (constant time insertion/deleting/queries with low constant | 1960 insertion/deleting/queries with low constant factors) and is very stingy with |
1876 factors) and is very stingy with malloc traffic. | 1961 malloc traffic. |
1877 | 1962 |
1878 Note that, unlike :ref:`std::set <dss_set>`, the iterators of ``SmallPtrSet`` | 1963 Note that, unlike :ref:`std::set <dss_set>`, the iterators of ``SmallPtrSet`` |
1879 are invalidated whenever an insertion occurs. Also, the values visited by the | 1964 are invalidated whenever an insertion occurs. Also, the values visited by the |
1880 iterators are not visited in sorted order. | 1965 iterators are not visited in sorted order. |
1881 | 1966 |
2578 Iterating over def-use & use-def chains | 2663 Iterating over def-use & use-def chains |
2579 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | 2664 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
2580 | 2665 |
2581 Frequently, we might have an instance of the ``Value`` class (`doxygen | 2666 Frequently, we might have an instance of the ``Value`` class (`doxygen |
2582 <http://llvm.org/doxygen/classllvm_1_1Value.html>`__) and we want to determine | 2667 <http://llvm.org/doxygen/classllvm_1_1Value.html>`__) and we want to determine |
2583 which ``User`` s use the ``Value``. The list of all ``User``\ s of a particular | 2668 which ``User``\ s use the ``Value``. The list of all ``User``\ s of a particular |
2584 ``Value`` is called a *def-use* chain. For example, let's say we have a | 2669 ``Value`` is called a *def-use* chain. For example, let's say we have a |
2585 ``Function*`` named ``F`` to a particular function ``foo``. Finding all of the | 2670 ``Function*`` named ``F`` to a particular function ``foo``. Finding all of the |
2586 instructions that *use* ``foo`` is as simple as iterating over the *def-use* | 2671 instructions that *use* ``foo`` is as simple as iterating over the *def-use* |
2587 chain of ``F``: | 2672 chain of ``F``: |
2588 | 2673 |
2892 GlobalVariable *GV = .. ; | 2977 GlobalVariable *GV = .. ; |
2893 | 2978 |
2894 GV->eraseFromParent(); | 2979 GV->eraseFromParent(); |
2895 | 2980 |
2896 | 2981 |
2897 .. _create_types: | |
2898 | |
2899 How to Create Types | |
2900 ------------------- | |
2901 | |
2902 In generating IR, you may need some complex types. If you know these types | |
2903 statically, you can use ``TypeBuilder<...>::get()``, defined in | |
2904 ``llvm/Support/TypeBuilder.h``, to retrieve them. ``TypeBuilder`` has two forms | |
2905 depending on whether you're building types for cross-compilation or native | |
2906 library use. ``TypeBuilder<T, true>`` requires that ``T`` be independent of the | |
2907 host environment, meaning that it's built out of types from the ``llvm::types`` | |
2908 (`doxygen <http://llvm.org/doxygen/namespacellvm_1_1types.html>`__) namespace | |
2909 and pointers, functions, arrays, etc. built of those. ``TypeBuilder<T, false>`` | |
2910 additionally allows native C types whose size may depend on the host compiler. | |
2911 For example, | |
2912 | |
2913 .. code-block:: c++ | |
2914 | |
2915 FunctionType *ft = TypeBuilder<types::i<8>(types::i<32>*), true>::get(); | |
2916 | |
2917 is easier to read and write than the equivalent | |
2918 | |
2919 .. code-block:: c++ | |
2920 | |
2921 std::vector<const Type*> params; | |
2922 params.push_back(PointerType::getUnqual(Type::Int32Ty)); | |
2923 FunctionType *ft = FunctionType::get(Type::Int8Ty, params, false); | |
2924 | |
2925 See the `class comment | |
2926 <http://llvm.org/doxygen/TypeBuilder_8h_source.html#l00001>`_ for more details. | |
2927 | |
2928 .. _threading: | 2982 .. _threading: |
2929 | 2983 |
2930 Threads and LLVM | 2984 Threads and LLVM |
2931 ================ | 2985 ================ |
2932 | 2986 |
2982 Conceptually, ``LLVMContext`` provides isolation. Every LLVM entity | 3036 Conceptually, ``LLVMContext`` provides isolation. Every LLVM entity |
2983 (``Module``\ s, ``Value``\ s, ``Type``\ s, ``Constant``\ s, etc.) in LLVM's | 3037 (``Module``\ s, ``Value``\ s, ``Type``\ s, ``Constant``\ s, etc.) in LLVM's |
2984 in-memory IR belongs to an ``LLVMContext``. Entities in different contexts | 3038 in-memory IR belongs to an ``LLVMContext``. Entities in different contexts |
2985 *cannot* interact with each other: ``Module``\ s in different contexts cannot be | 3039 *cannot* interact with each other: ``Module``\ s in different contexts cannot be |
2986 linked together, ``Function``\ s cannot be added to ``Module``\ s in different | 3040 linked together, ``Function``\ s cannot be added to ``Module``\ s in different |
2987 contexts, etc. What this means is that is is safe to compile on multiple | 3041 contexts, etc. What this means is that is safe to compile on multiple |
2988 threads simultaneously, as long as no two threads operate on entities within the | 3042 threads simultaneously, as long as no two threads operate on entities within the |
2989 same context. | 3043 same context. |
2990 | 3044 |
2991 In practice, very few places in the API require the explicit specification of a | 3045 In practice, very few places in the API require the explicit specification of a |
2992 ``LLVMContext``, other than the ``Type`` creation/lookup APIs. Because every | 3046 ``LLVMContext``, other than the ``Type`` creation/lookup APIs. Because every |
3331 libraries built with `LLVM_ENABLE_ABI_BREAKING_CHECKS` are not ABI | 3385 libraries built with `LLVM_ENABLE_ABI_BREAKING_CHECKS` are not ABI |
3332 compatible LLVM libraries built without it defined. By default, | 3386 compatible LLVM libraries built without it defined. By default, |
3333 turning on assertions also turns on `LLVM_ENABLE_ABI_BREAKING_CHECKS` | 3387 turning on assertions also turns on `LLVM_ENABLE_ABI_BREAKING_CHECKS` |
3334 so a default +Asserts build is not ABI compatible with a | 3388 so a default +Asserts build is not ABI compatible with a |
3335 default -Asserts build. Clients that want ABI compatibility | 3389 default -Asserts build. Clients that want ABI compatibility |
3336 between +Asserts and -Asserts builds should use the CMake or autoconf | 3390 between +Asserts and -Asserts builds should use the CMake build system |
3337 build systems to set `LLVM_ENABLE_ABI_BREAKING_CHECKS` independently | 3391 to set `LLVM_ENABLE_ABI_BREAKING_CHECKS` independently |
3338 of `LLVM_ENABLE_ASSERTIONS`. | 3392 of `LLVM_ENABLE_ASSERTIONS`. |
3339 | 3393 |
3340 .. _coreclasses: | 3394 .. _coreclasses: |
3341 | 3395 |
3342 The Core LLVM Class Hierarchy Reference | 3396 The Core LLVM Class Hierarchy Reference |
3509 * ``Function *getFunction(StringRef Name) const`` | 3563 * ``Function *getFunction(StringRef Name) const`` |
3510 | 3564 |
3511 Look up the specified function in the ``Module`` SymbolTable_. If it does not | 3565 Look up the specified function in the ``Module`` SymbolTable_. If it does not |
3512 exist, return ``null``. | 3566 exist, return ``null``. |
3513 | 3567 |
3514 * ``Function *getOrInsertFunction(const std::string &Name, const FunctionType | 3568 * ``FunctionCallee getOrInsertFunction(const std::string &Name, |
3515 *T)`` | 3569 const FunctionType *T)`` |
3516 | 3570 |
3517 Look up the specified function in the ``Module`` SymbolTable_. If it does not | 3571 Look up the specified function in the ``Module`` SymbolTable_. If |
3518 exist, add an external declaration for the function and return it. | 3572 it does not exist, add an external declaration for the function and |
3573 return it. Note that the function signature already present may not | |
3574 match the requested signature. Thus, in order to enable the common | |
3575 usage of passing the result directly to EmitCall, the return type is | |
3576 a struct of ``{FunctionType *T, Constant *FunctionPtr}``, rather | |
3577 than simply the ``Function*`` with potentially an unexpected | |
3578 signature. | |
3519 | 3579 |
3520 * ``std::string getTypeName(const Type *Ty)`` | 3580 * ``std::string getTypeName(const Type *Ty)`` |
3521 | 3581 |
3522 If there is at least one entry in the SymbolTable_ for the specified Type_, | 3582 If there is at least one entry in the SymbolTable_ for the specified Type_, |
3523 return it. Otherwise return the empty string. | 3583 return it. Otherwise return the empty string. |
3719 | 3779 |
3720 .. _CmpInst: | 3780 .. _CmpInst: |
3721 | 3781 |
3722 * ``CmpInst`` | 3782 * ``CmpInst`` |
3723 | 3783 |
3724 This subclass respresents the two comparison instructions, | 3784 This subclass represents the two comparison instructions, |
3725 `ICmpInst <LangRef.html#i_icmp>`_ (integer opreands), and | 3785 `ICmpInst <LangRef.html#i_icmp>`_ (integer opreands), and |
3726 `FCmpInst <LangRef.html#i_fcmp>`_ (floating point operands). | 3786 `FCmpInst <LangRef.html#i_fcmp>`_ (floating point operands). |
3727 | |
3728 .. _TerminatorInst: | |
3729 | |
3730 * ``TerminatorInst`` | |
3731 | |
3732 This subclass is the parent of all terminator instructions (those which can | |
3733 terminate a block). | |
3734 | 3787 |
3735 .. _m_Instruction: | 3788 .. _m_Instruction: |
3736 | 3789 |
3737 Important Public Members of the ``Instruction`` class | 3790 Important Public Members of the ``Instruction`` class |
3738 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | 3791 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
4055 | 4108 |
4056 This class represents a single entry single exit section of the code, commonly | 4109 This class represents a single entry single exit section of the code, commonly |
4057 known as a basic block by the compiler community. The ``BasicBlock`` class | 4110 known as a basic block by the compiler community. The ``BasicBlock`` class |
4058 maintains a list of Instruction_\ s, which form the body of the block. Matching | 4111 maintains a list of Instruction_\ s, which form the body of the block. Matching |
4059 the language definition, the last element of this list of instructions is always | 4112 the language definition, the last element of this list of instructions is always |
4060 a terminator instruction (a subclass of the TerminatorInst_ class). | 4113 a terminator instruction. |
4061 | 4114 |
4062 In addition to tracking the list of instructions that make up the block, the | 4115 In addition to tracking the list of instructions that make up the block, the |
4063 ``BasicBlock`` class also keeps track of the :ref:`Function <c_Function>` that | 4116 ``BasicBlock`` class also keeps track of the :ref:`Function <c_Function>` that |
4064 it is embedded into. | 4117 it is embedded into. |
4065 | 4118 |
4106 * ``Function *getParent()`` | 4159 * ``Function *getParent()`` |
4107 | 4160 |
4108 Returns a pointer to :ref:`Function <c_Function>` the block is embedded into, | 4161 Returns a pointer to :ref:`Function <c_Function>` the block is embedded into, |
4109 or a null pointer if it is homeless. | 4162 or a null pointer if it is homeless. |
4110 | 4163 |
4111 * ``TerminatorInst *getTerminator()`` | 4164 * ``Instruction *getTerminator()`` |
4112 | 4165 |
4113 Returns a pointer to the terminator instruction that appears at the end of the | 4166 Returns a pointer to the terminator instruction that appears at the end of the |
4114 ``BasicBlock``. If there is no terminator instruction, or if the last | 4167 ``BasicBlock``. If there is no terminator instruction, or if the last |
4115 instruction in the block is not a terminator, then a null pointer is returned. | 4168 instruction in the block is not a terminator, then a null pointer is returned. |
4116 | 4169 |