FAQ.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. ================================
  2. Frequently Asked Questions (FAQ)
  3. ================================
  4. .. contents::
  5. :local:
  6. License
  7. =======
  8. Can I modify LLVM source code and redistribute the modified source?
  9. -------------------------------------------------------------------
  10. Yes. The modified source distribution must retain the copyright notice and
  11. follow the conditions listed in the `LLVM license
  12. <http://llvm.org/svn/llvm-project/llvm/trunk/LICENSE.TXT>`_.
  13. Can I modify the LLVM source code and redistribute binaries or other tools based on it, without redistributing the source?
  14. --------------------------------------------------------------------------------------------------------------------------
  15. Yes. This is why we distribute LLVM under a less restrictive license than GPL,
  16. as explained in the first question above.
  17. Source Code
  18. ===========
  19. In what language is LLVM written?
  20. ---------------------------------
  21. All of the LLVM tools and libraries are written in C++ with extensive use of
  22. the STL.
  23. How portable is the LLVM source code?
  24. -------------------------------------
  25. The LLVM source code should be portable to most modern Unix-like operating
  26. systems. LLVM has also excellent support on Windows systems.
  27. Most of the code is written in standard C++ with operating system
  28. services abstracted to a support library. The tools required to build and
  29. test LLVM have been ported to a plethora of platforms.
  30. What API do I use to store a value to one of the virtual registers in LLVM IR's SSA representation?
  31. ---------------------------------------------------------------------------------------------------
  32. In short: you can't. It's actually kind of a silly question once you grok
  33. what's going on. Basically, in code like:
  34. .. code-block:: llvm
  35. %result = add i32 %foo, %bar
  36. , ``%result`` is just a name given to the ``Value`` of the ``add``
  37. instruction. In other words, ``%result`` *is* the add instruction. The
  38. "assignment" doesn't explicitly "store" anything to any "virtual register";
  39. the "``=``" is more like the mathematical sense of equality.
  40. Longer explanation: In order to generate a textual representation of the
  41. IR, some kind of name has to be given to each instruction so that other
  42. instructions can textually reference it. However, the isomorphic in-memory
  43. representation that you manipulate from C++ has no such restriction since
  44. instructions can simply keep pointers to any other ``Value``'s that they
  45. reference. In fact, the names of dummy numbered temporaries like ``%1`` are
  46. not explicitly represented in the in-memory representation at all (see
  47. ``Value::getName()``).
  48. Source Languages
  49. ================
  50. What source languages are supported?
  51. ------------------------------------
  52. LLVM currently has full support for C and C++ source languages through
  53. `Clang <http://clang.llvm.org/>`_. Many other language frontends have
  54. been written using LLVM, and an incomplete list is available at
  55. `projects with LLVM <http://llvm.org/ProjectsWithLLVM/>`_.
  56. I'd like to write a self-hosting LLVM compiler. How should I interface with the LLVM middle-end optimizers and back-end code generators?
  57. ----------------------------------------------------------------------------------------------------------------------------------------
  58. Your compiler front-end will communicate with LLVM by creating a module in the
  59. LLVM intermediate representation (IR) format. Assuming you want to write your
  60. language's compiler in the language itself (rather than C++), there are 3
  61. major ways to tackle generating LLVM IR from a front-end:
  62. 1. **Call into the LLVM libraries code using your language's FFI (foreign
  63. function interface).**
  64. * *for:* best tracks changes to the LLVM IR, .ll syntax, and .bc format
  65. * *for:* enables running LLVM optimization passes without a emit/parse
  66. overhead
  67. * *for:* adapts well to a JIT context
  68. * *against:* lots of ugly glue code to write
  69. 2. **Emit LLVM assembly from your compiler's native language.**
  70. * *for:* very straightforward to get started
  71. * *against:* the .ll parser is slower than the bitcode reader when
  72. interfacing to the middle end
  73. * *against:* it may be harder to track changes to the IR
  74. 3. **Emit LLVM bitcode from your compiler's native language.**
  75. * *for:* can use the more-efficient bitcode reader when interfacing to the
  76. middle end
  77. * *against:* you'll have to re-engineer the LLVM IR object model and bitcode
  78. writer in your language
  79. * *against:* it may be harder to track changes to the IR
  80. If you go with the first option, the C bindings in include/llvm-c should help
  81. a lot, since most languages have strong support for interfacing with C. The
  82. most common hurdle with calling C from managed code is interfacing with the
  83. garbage collector. The C interface was designed to require very little memory
  84. management, and so is straightforward in this regard.
  85. What support is there for a higher level source language constructs for building a compiler?
  86. --------------------------------------------------------------------------------------------
  87. Currently, there isn't much. LLVM supports an intermediate representation
  88. which is useful for code representation but will not support the high level
  89. (abstract syntax tree) representation needed by most compilers. There are no
  90. facilities for lexical nor semantic analysis.
  91. I don't understand the ``GetElementPtr`` instruction. Help!
  92. -----------------------------------------------------------
  93. See `The Often Misunderstood GEP Instruction <GetElementPtr.html>`_.
  94. Using the C and C++ Front Ends
  95. ==============================
  96. Can I compile C or C++ code to platform-independent LLVM bitcode?
  97. -----------------------------------------------------------------
  98. No. C and C++ are inherently platform-dependent languages. The most obvious
  99. example of this is the preprocessor. A very common way that C code is made
  100. portable is by using the preprocessor to include platform-specific code. In
  101. practice, information about other platforms is lost after preprocessing, so
  102. the result is inherently dependent on the platform that the preprocessing was
  103. targeting.
  104. Another example is ``sizeof``. It's common for ``sizeof(long)`` to vary
  105. between platforms. In most C front-ends, ``sizeof`` is expanded to a
  106. constant immediately, thus hard-wiring a platform-specific detail.
  107. Also, since many platforms define their ABIs in terms of C, and since LLVM is
  108. lower-level than C, front-ends currently must emit platform-specific IR in
  109. order to have the result conform to the platform ABI.
  110. Questions about code generated by the demo page
  111. ===============================================
  112. What is this ``llvm.global_ctors`` and ``_GLOBAL__I_a...`` stuff that happens when I ``#include <iostream>``?
  113. -------------------------------------------------------------------------------------------------------------
  114. If you ``#include`` the ``<iostream>`` header into a C++ translation unit,
  115. the file will probably use the ``std::cin``/``std::cout``/... global objects.
  116. However, C++ does not guarantee an order of initialization between static
  117. objects in different translation units, so if a static ctor/dtor in your .cpp
  118. file used ``std::cout``, for example, the object would not necessarily be
  119. automatically initialized before your use.
  120. To make ``std::cout`` and friends work correctly in these scenarios, the STL
  121. that we use declares a static object that gets created in every translation
  122. unit that includes ``<iostream>``. This object has a static constructor
  123. and destructor that initializes and destroys the global iostream objects
  124. before they could possibly be used in the file. The code that you see in the
  125. ``.ll`` file corresponds to the constructor and destructor registration code.
  126. If you would like to make it easier to *understand* the LLVM code generated
  127. by the compiler in the demo page, consider using ``printf()`` instead of
  128. ``iostream``\s to print values.
  129. Where did all of my code go??
  130. -----------------------------
  131. If you are using the LLVM demo page, you may often wonder what happened to
  132. all of the code that you typed in. Remember that the demo script is running
  133. the code through the LLVM optimizers, so if your code doesn't actually do
  134. anything useful, it might all be deleted.
  135. To prevent this, make sure that the code is actually needed. For example, if
  136. you are computing some expression, return the value from the function instead
  137. of leaving it in a local variable. If you really want to constrain the
  138. optimizer, you can read from and assign to ``volatile`` global variables.
  139. What is this "``undef``" thing that shows up in my code?
  140. --------------------------------------------------------
  141. ``undef`` is the LLVM way of representing a value that is not defined. You
  142. can get these if you do not initialize a variable before you use it. For
  143. example, the C function:
  144. .. code-block:: c
  145. int X() { int i; return i; }
  146. Is compiled to "``ret i32 undef``" because "``i``" never has a value specified
  147. for it.
  148. Why does instcombine + simplifycfg turn a call to a function with a mismatched calling convention into "unreachable"? Why not make the verifier reject it?
  149. ----------------------------------------------------------------------------------------------------------------------------------------------------------
  150. This is a common problem run into by authors of front-ends that are using
  151. custom calling conventions: you need to make sure to set the right calling
  152. convention on both the function and on each call to the function. For
  153. example, this code:
  154. .. code-block:: llvm
  155. define fastcc void @foo() {
  156. ret void
  157. }
  158. define void @bar() {
  159. call void @foo()
  160. ret void
  161. }
  162. Is optimized to:
  163. .. code-block:: llvm
  164. define fastcc void @foo() {
  165. ret void
  166. }
  167. define void @bar() {
  168. unreachable
  169. }
  170. ... with "``opt -instcombine -simplifycfg``". This often bites people because
  171. "all their code disappears". Setting the calling convention on the caller and
  172. callee is required for indirect calls to work, so people often ask why not
  173. make the verifier reject this sort of thing.
  174. The answer is that this code has undefined behavior, but it is not illegal.
  175. If we made it illegal, then every transformation that could potentially create
  176. this would have to ensure that it doesn't, and there is valid code that can
  177. create this sort of construct (in dead code). The sorts of things that can
  178. cause this to happen are fairly contrived, but we still need to accept them.
  179. Here's an example:
  180. .. code-block:: llvm
  181. define fastcc void @foo() {
  182. ret void
  183. }
  184. define internal void @bar(void()* %FP, i1 %cond) {
  185. br i1 %cond, label %T, label %F
  186. T:
  187. call void %FP()
  188. ret void
  189. F:
  190. call fastcc void %FP()
  191. ret void
  192. }
  193. define void @test() {
  194. %X = or i1 false, false
  195. call void @bar(void()* @foo, i1 %X)
  196. ret void
  197. }
  198. In this example, "test" always passes ``@foo``/``false`` into ``bar``, which
  199. ensures that it is dynamically called with the right calling conv (thus, the
  200. code is perfectly well defined). If you run this through the inliner, you
  201. get this (the explicit "or" is there so that the inliner doesn't dead code
  202. eliminate a bunch of stuff):
  203. .. code-block:: llvm
  204. define fastcc void @foo() {
  205. ret void
  206. }
  207. define void @test() {
  208. %X = or i1 false, false
  209. br i1 %X, label %T.i, label %F.i
  210. T.i:
  211. call void @foo()
  212. br label %bar.exit
  213. F.i:
  214. call fastcc void @foo()
  215. br label %bar.exit
  216. bar.exit:
  217. ret void
  218. }
  219. Here you can see that the inlining pass made an undefined call to ``@foo``
  220. with the wrong calling convention. We really don't want to make the inliner
  221. have to know about this sort of thing, so it needs to be valid code. In this
  222. case, dead code elimination can trivially remove the undefined code. However,
  223. if ``%X`` was an input argument to ``@test``, the inliner would produce this:
  224. .. code-block:: llvm
  225. define fastcc void @foo() {
  226. ret void
  227. }
  228. define void @test(i1 %X) {
  229. br i1 %X, label %T.i, label %F.i
  230. T.i:
  231. call void @foo()
  232. br label %bar.exit
  233. F.i:
  234. call fastcc void @foo()
  235. br label %bar.exit
  236. bar.exit:
  237. ret void
  238. }
  239. The interesting thing about this is that ``%X`` *must* be false for the
  240. code to be well-defined, but no amount of dead code elimination will be able
  241. to delete the broken call as unreachable. However, since
  242. ``instcombine``/``simplifycfg`` turns the undefined call into unreachable, we
  243. end up with a branch on a condition that goes to unreachable: a branch to
  244. unreachable can never happen, so "``-inline -instcombine -simplifycfg``" is
  245. able to produce:
  246. .. code-block:: llvm
  247. define fastcc void @foo() {
  248. ret void
  249. }
  250. define void @test(i1 %X) {
  251. F.i:
  252. call fastcc void @foo()
  253. ret void
  254. }