[LLVMdev] "Refactoring" Basic Blocks into a new function

Chris Lattner sabre at nondot.org
Tue Apr 19 21:03:50 PDT 2005


On Tue, 19 Apr 2005, Evan Jones wrote:
> I have two very specific questions about LLVM, but first let me give you the 
> general overview of what I am trying to achieve. I have some section of a 
> function that I want to replace with a function call to a brand new function. 
> For example, I want to take the following function:
> I think I have a pretty good idea of how to do this (BasicBlock::split really 
> helps!), but I have a few specific questions:

ok

> 1. To mark the section of code to be cut out, I'm using "magic" function 
> calls (begin() and end()). In order to locate these calls, I am currently 
> iterating over the basic blocks in a function using the iterator. Is it 
> possible that I could get the blocks "out of order" with respect to the 
> control flow? For example, if I have this code:
>
> blockA
> if ( blockB ) {
> 	blockC
> } else {
> 	blockD
> }
> blockE
>
> I expect to get the blocks in alphabetical order. I can see that it is 
> possible that LLVM could give them to me in an arbitrary order. If so, it 
> will complicate how I need to locate the basic blocks that compose "code B" 
> in my example above.

The order of BasicBlock's in an LLVM Function match the order that they 
are printed.  If you iterate over them, you are guaranteed to get this 
order.  Note that if you're looking at the output of the C front-end, they 
could be permuted arbitrarily w.r.t. the source order, due to various 
optimizations.  That said, if you have begin/end markers in the same basic 
block, they should stay within the same basic block most of the time 
(enough for it to not matter).

> 2. I am going to need to pass dependencies for the "code B" block into the 
> new function. I'm hoping that it work if I just create the arguments with the 
> same name and type as the dependencies, then use "setParent" to transfer the

This is trickier.  In particular, if you have code like this:

   ...
   a = ...
   ---cut---
   X = add a, 1
   ---cut---
   C = add X, b

Then you need to arrange to pass the value of "a" into the new function 
and get the value of "x" back out of it.  This is exactly the sort of 
details that ExtractCodeRegion takes care of for you. :)

> By the way, I have to say that I am *very* impressed with LLVM's 
> documentation and organization. I have been able to get an amazing amount of 
> functionality working *very* quickly. This is a really useful tool for 
> manipulating code.

Great! :)

-Chris

-- 
http://nondot.org/sabre/
http://llvm.cs.uiuc.edu/




More information about the llvm-dev mailing list