[LLVMdev] Simpler way to insert instructions into the program
Chris Lattner
sabre at nondot.org
Tue Sep 10 14:22:01 PDT 2002
When talking to Joel and Brian a while ago, I realized that it is a very
common occurance to create an instruction, then immediately insert it into
a basic block. At the time, this required keeping iterators around
(updating them as neccesary) to determine where to insert the instruction,
and it also required calling getInstList().insert(....) or something
similar after creating the instruction to get the insertion done.
This is all a thing of the past now. :) You can continue to write code
like that, and it will still work, but there is now an easier way. I have
updated the ctors for all of the main LLVM classes to take a pointer to an
LLVM object. If this pointer is specified, the newly constructed object
is automatically inserted into the LLVM graph by the ctor itself, no extra
insert call required.
For example, the Instruction ctors now take an optional pointer to an
instruction to insert before. If this instruction is specified, the newly
created instruction is inserted into the specified instruction's basic
block, right before the instruction.
Because no iterators are involved, this simplifies a lot of code. For example:
http://mail.cs.uiuc.edu/pipermail/llvm-commits/2002-September/000110.html
As a side note, this insertion pattern is no longer needed:
BI = ++BB->getInstList().insert(BI, InstPointer);
It is exactly equivalent to doing:
BB->getInstList().insert(BI, InstPointer);
Historically this was neccesary because inserting the instruction
invalidated the iterator operand. Because this hasn't been the case for
quite a while, we can stop doing this, even in the cases where we still
need to do inserts.
Let me know if you have any questions, _please_ start using this though.
It makes the code so much simpler. :)
-Chris
http://llvm.cs.uiuc.edu/
http://www.nondot.org/~sabre/Projects/
More information about the llvm-dev
mailing list