How can the total number of arrays be counted in a C program ?
The array declarations in LLVM IR correspond to alloca type of operation. So
int a[10];
corresponds to
%a = alloca [10 x i32], align 4
in LLVM IR.
But I also noticed that
int j = 0;
also corresponds to an alloca instruction
%j = alloca i32, align 4
So how to count the number of alloca instructions that correspond only to arrays ?
EDIT:
for (Function::iterator i = F.begin(), e = F.end(); i != e; ++i)
{
for (BasicBlock::iterator ii =(*i).begin(), ii_e = (*i).end(); ii != ii_e; ++ii)
{
Instruction *n = dyn_cast<Instruction>(&*ii);
for( int num = 0; num < n->getNumOperands(); ++num)
if(isa<ArrayType>(n->getOperand(num)->getType()))
{
// doesn't work
errs()<<"yayayayay Array\n";
}
}
}
AllocaInst
has public methodisArrayAllocation()
. You can use it to count the number of alloca instructions that correspond only to arrays.Open the LLVM demo page and compile following code
to the LLVM C++ API calls.
This is how
a
created:where
ArrayTy_6
is:So, to find out if
alloca
instruction you are looking at is defining array, just doisa<ArrayType>()
on it's first argument.See LLVM docs for more info.