From c289d4b13d5cf2f90bf368af4db74eb2b7220a29 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 3 Nov 2007 22:22:30 +0000 Subject: [PATCH] finish the 'Memory in LLVM' section llvm-svn: 43667 --- docs/tutorial/LangImpl7.html | 60 +++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/docs/tutorial/LangImpl7.html b/docs/tutorial/LangImpl7.html index f80c0673fdd..ca0d2f06e03 100644 --- a/docs/tutorial/LangImpl7.html +++ b/docs/tutorial/LangImpl7.html @@ -238,17 +238,63 @@ cond_next: ret i32 %X.01 } + -

The mem2reg pass is guaranteed to work, and +

The mem2reg pass implements the standard "iterated dominator frontier" +algorithm for constructing SSA form and has a number of optimizations that speed +up very common degenerate cases. mem2reg really is the answer for dealing with +mutable variables, and we highly recommend that you depend on it. Note that +mem2reg only works on variables in certain circumstances:

-which cases. -

+
    +
  1. mem2reg is alloca-driven: it looks for allocas and if it can handle them, it +promotes them. It does not apply to global variables or heap allocations.
  2. -

    The final question you may be asking is: should I bother with this nonsense -for my front-end? Wouldn't it be better if I just did SSA construction -directly, avoiding use of the mem2reg optimization pass? +

  3. mem2reg only looks for alloca instructions in the entry block of the +function. Being in the entry block guarantees that the alloca is only executed +once, which makes analysis simpler.
  4. -Proven, well tested, debug info, etc. +
  5. mem2reg only promotes allocas whose uses are direct loads and stores. If +the address of the stack object is passed to a function, or if any funny pointer +arithmetic is involved, the alloca will not be promoted.
  6. + +
  7. mem2reg only works on allocas of scalar values, and only if the array size +of the allocation is 1 (or missing in the .ll file). mem2reg is not capable of +promoting structs or arrays to registers. Note that the "scalarrepl" pass is +more powerful and can promote structs, "unions", and arrays in many cases.
  8. + +
+ +

+All of these properties are easy to satisfy for most imperative languages, and +we'll illustrated this below with Kaleidoscope. The final question you may be +asking is: should I bother with this nonsense for my front-end? Wouldn't it be +better if I just did SSA construction directly, avoiding use of the mem2reg +optimization pass? In short, we strongly recommend that use you this technique +for building SSA form, unless there is an extremely good reason not to. Using +this technique is:

+ + + +

If nothing else, this makes it much easier to get your front-end up and +running, and is very simple to implement. Lets extend Kaleidoscope with mutable +variables now!