Lu Zhao wrote:
Hi,
I want to extract all instruction of the form "alloca %struct.S", where
$struct.S is defined as a struct
%struct.S = type { i32, i32 }
I'm using the following loop:
for(inst_iterator i = inst_begin(F), e = inst_end(F); i!= e; i++)
{
AllocaInst* ai;
if( (ai = dyn_cast<AllocaInst>(&*i))){
if(ai->getOperand(0)->getType()->getTypeID() ==
Type::StructTyID)
allocs.push_back(ai);
}
}
I might be misunderstanding something, but the above code finds all alloca instructions that allocate structure types. This is not quite what you want if you're looking specifically for type {i32, i32}.
To search specifically for that type, do the following:
1) Create a "new" structure type representing {i32. i32} using the static StructType::get() method. I think the code would look something like this:
Type * MyStructType = StructType::get (Type::Int32Ty, Type::Int32Ty, 0);
2) Compare the pointer of MyStructType to the pointer for the alloca's allocation type:
if (ai->getOperand(0)->getType() == MyStructType)
The above works because LLVM will only create one instance of each type (i.e. if there's already a {i32, i32}, StructType::get() returns a pointer to that instead of creating a new object). This is what allows the pointer comparison to work.
However, I had the impression that the type comparison can be done by a
pointer comparison. Can anyone please suggest me a better way to do it?
Also, I want to get the size of %struct.S, should I use the following
code?
const TargetData &TD = getAnalysis<TargetData>();
unsigned StructSize =
TD.getABITypeSizeInBits(ai->getOperand(0)->getType());
There are different interpretations of "size" that TargetData supports, but without knowing what you're trying to do, I'd have to guess that this is probably what you want.
-- John T.