In the section expaining "dyn_cast" There are following lines of code: if (AllocationInst *AI = dyn_cast<AllocationInst>(Val)) { ... } I cannot understand how you take a operand, a value, and cast it into a Instruction. Can you explain it for me? 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) cerr << *PN; In this case you cast &*BBI. It's not an operand. Can you explain to me a little about what you can cast and what cannot? Besides, maybe this is a stupid question, but what do you mean by "&*BBI"? Thanks, xiaodong
Also sprach xli3 at uiuc.edu: } In the section expaining "dyn_cast" } There are following lines of code: } if (AllocationInst *AI = dyn_cast<AllocationInst>(Val)) { } ... } } } I cannot understand how you take a operand, a value, and cast } it into a Instruction. Can you explain it for me? } } 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) } cerr << *PN; } } In this case you cast &*BBI. It's not an operand. Can you } explain to me a little about what you can cast and what } cannot? } } Besides, maybe this is a stupid question, but what do you mean } by "&*BBI"? } I may be incorrect, but here it goes anyway. Operands in LLVM can point to the actual instruction that produces the result that will be used for the instruction. So the first case is valid since 'Val' would point to that instruction. The second is iterating through a basic block which is a list of instructions. The first few instructions in a basic block are the Phi nodes used in SSA. The BasicBlock::iterator returns an instruction type (right?). The "&*BBI" is taking the iterator "BBI", dereferencing it "*BBI" to get the Instruction reference, and then taking the address of that "&*BBI" to get a pointer to that instruction. -- || Bill? Wendling wendling at isanbard.org
> I may be incorrect, but here it goes anyway.Bill is absolutely right, one comment though...> The second is iterating through a basic block which is a list of > instructions. The first few instructions in a basic block are the Phi > nodes used in SSA. The BasicBlock::iterator returns an instruction type > (right?).BasicBlock::iterator iterators over the contents of the basic block, which are the instructions in the block. This *I (where I is a BasicBlock::iterator) returns a reference to an instruction in the basic block (not really it's type). Thanks for the help Bill! -Chris http://llvm.cs.uiuc.edu/ http://www.nondot.org/~sabre/Projects/
> Can you > explain to me a little about what you can cast and what > cannot?I believe Bill and Chris addressed all your other questions. About the one above: An instance of any of the subclasses of Value can be used as an operand of a templated cast operation. In general, you use those operations to cast from an object of some class to an object of one of its superclasses (e.g., Value to Instruction). If you are not sure whether you can cast to a particular class, check if that class defines the classof() member functions. --Vikram
> In general, you use those operations to cast from > an object of some class to an object of one of its > superclassesThat should say "subclasses", not "superclasses".> (e.g., Value to Instruction). > > If you are not sure whether you can cast to a particular class, check if > that class defines the classof() member functions. > > --Vikram