计数阵列的数量 [英] Count number of arrays

查看:210
本文介绍了计数阵列的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何阵列的总数量在C程序算呢?

在LLVM IR的数组声明对应的的alloca 类型的操作。
因此,

  int类型的[10];

相当于

 %A =的alloca [10×I32],调整4

在LLVM IR。

但是,我也注意到,

  INT J = 0;

也相当于一个指令的alloca

 %J =的alloca I32,对齐4

那么怎么算的是仅对应于数组的alloca的指令数?

编辑:

 的(功能:迭代I = F.begin(),E = F.end(!);我= E ++ I)
  {
      对于(BasicBlock:迭代II =(* I).begin(),ii_e =(* I).END(!); II = ii_e ++二)
      {
           指令* N = dyn_cast<指令>(安培; *二);
           对于(INT NUM = 0; NUM<&N- GT; getNumOperands(); ++ NUM)
            如果(ISA<&数组类型GT;(N-> getOperand(NU​​M) - GT;的getType()))
        {
              //不起作用
          犯错()<<yayayayay阵列\\ n;
        }
       }
   }


解决方案

AllocaInst 的公共方法 isArrayAllocation() 。你可以用它来计算对应只有阵列的alloca指令数。

 的(功能:迭代BB = F.begin(),BE = F.end(); BB = BE;!++ BB)
  对于(BasicBlock:迭代II = BB->开始(),IE = BB->结束(!); II = IE ++ II)
    如果(AllocaInst * AI = dyn_cast&所述; AllocaInst&GT(II))
      如果(AI-> isArrayAllocation())
        犯错()&所述;&下; !数组的ALLOCA发现\\ N的;

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 method isArrayAllocation(). You can use it to count the number of alloca instructions that correspond only to arrays.

for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB)
  for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
    if (AllocaInst *AI = dyn_cast<AllocaInst>(II))
      if (AI->isArrayAllocation())
        errs() << "Alloca of array is found!\n";

这篇关于计数阵列的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆