@@ -234,7 +261,7 @@ static bool isLoopInvariant(const Value *V, const Loop *L)
return true;
// Otherwise, it must be an instruction...
- return !L->contains(cast<Instruction>(V)->getParent());
+ return !L->contains(cast<Instruction>(V)->getParent());
Note that you should not use an isa<> test followed by a
@@ -265,8 +292,8 @@ Another common example is:
// Loop over all of the phi nodes in a basic block
- BasicBlock::iterator BBI = BB->begin();
- for (; PHINode *PN = dyn_cast<PHINode>(&*BBI); ++BBI)
+ BasicBlock::iterator BBI = BB->begin();
+ for (; PHINode *PN = dyn_cast<PHINode>(BBI); ++BBI)
cerr << *PN;
@@ -318,12 +345,11 @@ Naturally, because of this, you don't want to delete the debug printouts, but
you don't want them to always be noisy. A standard compromise is to comment
them out, allowing you to enable them if you need them in the future.
-The "Support/Statistic.h"
-file provides a macro named DEBUG() that is a much nicer solution to
-this problem. Basically, you can put arbitrary code into the argument of the
-DEBUG macro, and it is only executed if 'opt' is run with the
-'-debug' command line argument:
+The "Support/Debug.h" file
+provides a macro named DEBUG() that is a much nicer solution to this
+problem. Basically, you can put arbitrary code into the argument of the
+DEBUG macro, and it is only executed if 'opt' (or any other
+tool) is run with the '-debug' command line argument:
...
@@ -341,10 +367,69 @@ Then you can run your pass like this:
$
-Using the DEBUG() macro instead of a home brewed solution allows you to
+Using the DEBUG() macro instead of a home-brewed solution allows you to
now have to create "yet another" command line option for the debug output for
-your pass. Note that DEBUG() macros are disabled for optimized
-builds, so they do not cause a performance impact at all.
+your pass. Note that DEBUG() macros are disabled for optimized builds,
+so they do not cause a performance impact at all (for the same reason, they
+should also not contain side-effects!).
+
+One additional nice thing about the DEBUG() macro is that you can
+enable or disable it directly in gdb. Just use "set DebugFlag=0" or
+"set DebugFlag=1" from the gdb if the program is running. If the
+program hasn't been started yet, you can always just run it with
+-debug.
+
+
+
+
+Sometimes you may find yourself in a situation where enabling -debug
+just turns on too much information (such as when working on the code
+generator). If you want to enable debug information with more fine-grained
+control, you define the DEBUG_TYPE macro and the -debug only
+option as follows:
+
+
+ ...
+ DEBUG(std::cerr << "No debug type\n");
+ #undef DEBUG_TYPE
+ #define DEBUG_TYPE "foo"
+ DEBUG(std::cerr << "'foo' debug type\n");
+ #undef DEBUG_TYPE
+ #define DEBUG_TYPE "bar"
+ DEBUG(std::cerr << "'bar' debug type\n");
+ #undef DEBUG_TYPE
+ #define DEBUG_TYPE ""
+ DEBUG(std::cerr << "No debug type (2)\n");
+ ...
+
+
+Then you can run your pass like this:
+
+
+ $ opt < a.bc > /dev/null -mypass
+ <no output>
+ $ opt < a.bc > /dev/null -mypass -debug
+ No debug type
+ 'foo' debug type
+ 'bar' debug type
+ No debug type (2)
+ $ opt < a.bc > /dev/null -mypass -debug-only=foo
+ 'foo' debug type
+ $ opt < a.bc > /dev/null -mypass -debug-only=bar
+ 'bar' debug type
+ $
+
+
+Of course, in practice, you should only set DEBUG_TYPE at the top of a
+file, to specify the debug type for the entire module (if you do this before you
+#include "Support/Debug.h", you don't have to insert the ugly
+#undef's). Also, you should use names more meaningful that "foo" and
+"bar", because there is no system in place to ensure that names do not conflict:
+if two different modules use the same string, they will all be turned on when
+the name is specified. This allows all, say, instruction scheduling, debug
+information to be enabled with -debug-type=InstrSched, even if the
+source lives in multiple files.
@@ -419,7 +504,7 @@ a report that looks like this:
49 cee - Number of setcc instruction eliminated
532 gcse - Number of loads removed
2919 gcse - Number of instructions removed
- 86 indvars - Number of cannonical indvars added
+ 86 indvars - Number of canonical indvars added
87 indvars - Number of aux indvars removed
25 instcombine - Number of dead inst eliminate
434 instcombine - Number of insts combined
@@ -492,7 +577,7 @@ contains:
// func is a pointer to a Function instance
- for(Function::iterator i = func->begin(), e = func->end(); i != e; ++i) {
+ for (Function::iterator i = func->begin(), e = func->end(); i != e; ++i) {
// print out the name of the basic block if it has one, and then the
// number of instructions that it contains
@@ -505,7 +590,7 @@ contains:
Note that i can be used as if it were a pointer for the purposes of
invoking member functions of the Instruction class. This is
because the indirection operator is overloaded for the iterator
-classes. In the above code, the expression i->size() is
+classes. In the above code, the expression i->size() is
exactly equivalent to (*i).size() just like you'd expect.
@@ -520,7 +605,7 @@ that prints out each instruction in a BasicBlock:
// blk is a pointer to a BasicBlock instance
- for(BasicBlock::iterator i = blk->begin(), e = blk->end(); i != e; ++i)
+ for (BasicBlock::iterator i = blk->begin(), e = blk->end(); i != e; ++i)
// the next statement works since operator<<(ostream&,...)
// is overloaded for Instruction&
cerr << *i << "\n";
@@ -557,7 +642,7 @@ stderr (Note: Dereferencing an InstIterator yields an
#include "llvm/Support/InstIterator.h"
...
// Suppose F is a ptr to a function
-for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
+for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
cerr << **i << "\n";
@@ -604,16 +689,6 @@ is semantically equivalent to
Instruction* pinst = i;
-Caveat emptor: The above syntax works only when you're not
-working with dyn_cast. The template definition of dyn_cast isn't implemented to handle this yet, so you'll
-still need the following in order for things to work properly:
-
-
-BasicBlock::iterator bbi = ...;
-BranchInst* b = dyn_cast<BranchInst>(&*bbi);
-
-
It's also possible to turn a class pointer into the corresponding
iterator. Usually, this conversion is quite inexpensive. The
following code snippet illustrates use of the conversion constructors
@@ -625,7 +700,7 @@ over some structure:
void printNextInstruction(Instruction* inst) {
BasicBlock::iterator it(inst);
++it; // after this line, it refers to the instruction after *inst.
- if(it != inst->getParent()->end()) cerr << *it << "\n";
+ if (it != inst->getParent()->end()) cerr << *it << "\n";
}
Of course, this example is strictly pedagogical, because it'd be much
@@ -638,8 +713,8 @@ more complex example
Say that you're writing a FunctionPass and would like to count all the
locations in the entire module (that is, across every
-Function) where a certain function (i.e. some
-Function*) already in scope. As you'll learn later, you may
+Function) where a certain function (i.e., some
+Function*) is already in scope. As you'll learn later, you may
want to use an InstVisitor to accomplish this in a much more
straightforward manner, but this example will allow us to explore how
you'd do it if you didn't have InstVisitor around. In
@@ -650,7 +725,7 @@ initialize callCounter to zero
for each Function f in the Module
for each BasicBlock b in f
for each Instruction i in b
- if(i is a CallInst and calls the given function)
+ if (i is a CallInst and calls the given function)
increment callCounter
@@ -666,14 +741,14 @@ class OurFunctionPass : public FunctionPass {
OurFunctionPass(): callCounter(0) { }
virtual runOnFunction(Function& F) {
- for(Function::iterator b = F.begin(), be = F.end(); b != be; ++b) {
- for(BasicBlock::iterator i = b->begin(); ie = b->end(); i != ie; ++i) {
+ for (Function::iterator b = F.begin(), be = F.end(); b != be; ++b) {
+ for (BasicBlock::iterator i = b->begin(); ie = b->end(); i != ie; ++i) {
if (CallInst* callInst = dyn_cast<CallInst>(&*i)) {
// we know we've encountered a call instruction, so we
// need to determine if it's a call to the
// function pointed to by m_func or not.
- if(callInst->getCalledFunction() == targetFunc)
+ if (callInst->getCalledFunction() == targetFunc)
++callCounter;
}
}
@@ -684,6 +759,31 @@ class OurFunctionPass : public FunctionPass {
};
+
+
+
+
+You may have noticed that the previous example was a bit
+oversimplified in that it did not deal with call sites generated by
+'invoke' instructions. In this, and in other situations, you may find
+that you want to treat CallInsts and InvokeInsts the
+same way, even though their most-specific common base class is
+Instruction, which includes lots of less closely-related
+things. For these cases, LLVM provides a handy wrapper class called CallSite
+. It is essentially a wrapper around an Instruction
+pointer, with some methods that provide functionality common to
+CallInsts and InvokeInsts.
+
+This class is supposed to have "value semantics". So it should be
+passed by value, not by reference; it should not be dynamically
+allocated or deallocated using operator new or operator
+delete. It is efficiently copyable, assignable and constructable,
+with costs equivalents to that of a bare pointer. (You will notice, if
+you look at its definition, that it has only a single data member.)
+
+
@@ -701,8 +801,8 @@ all Users of a particular Value is called a
Function* F = ...;
-for(Value::use_iterator i = F->use_begin(), e = F->use_end(); i != e; ++i) {
- if(Instruction* Inst = dyn_cast<Instruction>(*i)) {
+for (Value::use_iterator i = F->use_begin(), e = F->use_end(); i != e; ++i) {
+ if (Instruction *Inst = dyn_cast<Instruction>(*i)) {
cerr << "F is used in instruction:\n";
cerr << *Inst << "\n";
}
@@ -720,7 +820,7 @@ to iterate over all of the values that a particular instruction uses
Instruction* pi = ...;
-for(User::op_iterator i = pi->op_begin(), e = pi->op_end(); i != e; ++i) {
+for (User::op_iterator i = pi->op_begin(), e = pi->op_end(); i != e; ++i) {
Value* v = *i;
...
}
@@ -803,10 +903,10 @@ that BasicBlock, and a newly-created instruction
we wish to insert before *pi, we do the following:
-BasicBlock* pb = ...;
-Instruction* pi = ...;
-Instruction* newInst = new Instruction(...);
-pb->getInstList().insert(pi, newInst); // inserts newInst before pi in pb
+ BasicBlock *pb = ...;
+ Instruction *pi = ...;
+ Instruction *newInst = new Instruction(...);
+ pb->getInstList().insert(pi, newInst); // inserts newInst before pi in pb
@@ -817,9 +917,9 @@ instruction list: the instruction list of the enclosing basic block.
Thus, we could have accomplished the same thing as the above code
without being given a BasicBlock by doing:
-Instruction* pi = ...;
-Instruction* newInst = new Instruction(...);
-pi->getParent()->getInstList().insert(pi, newInst);
+ Instruction *pi = ...;
+ Instruction *newInst = new Instruction(...);
+ pi->getParent()->getInstList().insert(pi, newInst);
In fact, this sequence of steps occurs so frequently that the
Instruction class and Instruction-derived classes
@@ -866,8 +966,7 @@ For example:
Replacing individual instructions
Including "llvm/Transforms/Utils/BasicBlockUtils.h
-" permits use of two very useful replace functions:
+href="/doxygen/BasicBlockUtils_8h-source.html">llvm/Transforms/Utils/BasicBlockUtils.h" permits use of two very useful replace functions:
ReplaceInstWithValue and ReplaceInstWithInst.
@@ -897,7 +996,7 @@ instruction. The following example illustrates the replacement of one
AllocaInst* instToReplace = ...;
BasicBlock::iterator ii(instToReplace);
ReplaceInstWithInst(instToReplace->getParent()->getInstList(), ii,
- new AllocaInst(Type::IntTy, 0, "ptrToReplacedInt");
+ new AllocaInst(Type::IntTy, 0, "ptrToReplacedInt"));
@@ -1123,9 +1222,9 @@ the Instruction class
Returns the BasicBlock that this
Instruction is embedded into.
-
- bool hasSideEffects()
+
- bool mayWriteToMemory()
-Returns true if the instruction has side effects, i.e. it is a call,
+Returns true if the instruction writes to memory, i.e. it is a call,
free, invoke, or store.
- unsigned getOpcode()
@@ -1395,7 +1494,7 @@ list.
- Function::BasicBlockListType &getBasicBlockList()
Returns the list of BasicBlocks. This is
-neccesary to use when you need to update the list or perform a complex action
+necessary to use when you need to update the list or perform a complex action
that doesn't have a forwarding method.
@@ -1410,12 +1509,12 @@ These are forwarding methods that make it easy to access the contents of a
- Function::ArgumentListType &getArgumentList()
Returns the list of Arguments. This is
-neccesary to use when you need to update the list or perform a complex action
+necessary to use when you need to update the list or perform a complex action
that doesn't have a forwarding method.
-
- BasicBlock &getEntryNode()
+
- BasicBlock &getEntryBlock()
Returns the entry BasicBlock for the
function. Because the entry block for the function is always the first block,
@@ -1428,26 +1527,10 @@ This traverses the Type of the Function
and returns the return type of the function, or the FunctionType of the actual function.
-
-
- bool hasSymbolTable() const
-
-Return true if the Function has a symbol table allocated to it and if
-there is at least one entry in it.
-
- SymbolTable *getSymbolTable()
Return a pointer to the SymbolTable for this
-Function or a null pointer if one has not been allocated (because there
-are no named values in the function).
-
-
- SymbolTable *getSymbolTableSure()
-
-Return a pointer to the SymbolTable for this
-Function or allocate a new SymbolTable if one is not already around. This
-should only be used when adding elements to the SymbolTable, so that empty symbol tables are
-not left laying around.
+Function.
@@ -1542,7 +1625,7 @@ list.
- Module::FunctionListType &getFunctionList()
Returns the list of Functions. This is
-neccesary to use when you need to update the list or perform a complex action
+necessary to use when you need to update the list or perform a complex action
that doesn't have a forwarding method.
@@ -1560,32 +1643,17 @@ list.
- Module::GlobalListType &getGlobalList()
Returns the list of GlobalVariables.
-This is neccesary to use when you need to update the list or perform a complex
+This is necessary to use when you need to update the list or perform a complex
action that doesn't have a forwarding method.
- - bool hasSymbolTable() const
-
-Return true if the Module has a symbol table allocated to it and if
-there is at least one entry in it.
-
- SymbolTable *getSymbolTable()
-Return a pointer to the SymbolTable for this
-Module or a null pointer if one has not been allocated (because there
-are no named values in the function).
-
-
- SymbolTable *getSymbolTableSure()
-
-Return a pointer to the SymbolTable for this
-Module or allocate a new SymbolTable if one is not already around. This
-should only be used when adding elements to the SymbolTable, so that empty symbol tables are
-not left laying around.
+Return a reference to the SymbolTable for
+this Module.
@@ -1663,11 +1731,11 @@ Important Subclasses of Constant
ConstantArray : This represents a constant array.
- - const std::vector: Returns a Vecotr of component constants that makeup this array.
+
- const std::vector<Use> &getValues() const: Returns a Vecotr of component constants that makeup this array.
ConstantStruct : This represents a constant struct.
- - const std::vector: Returns a Vecotr of component constants that makeup this array.
+
- const std::vector<Use> &getValues() const: Returns a Vecotr of component constants that makeup this array.
ConstantPointerRef : This represents a constant pointer value that is initialized to point to a global value, which lies at a constant fixed address.