查找基本块中的值,这些值是在先前的基本块中计算的

时间:2012-03-12 17:24:02

标签: llvm

在基本块中,我想找到指令中使用的所有值,这些值不是在同一个基本块中计算的 例如,

 for.body5: 
  %i.015 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.body ]
  %add1 = add nsw i32 %2, %i.015
  %arrayidx = getelementptr inbounds [100 x i32]* %b, i32 0, i32 %i.015
  store i32 %add1, i32* %arrayidx, align 4, !tbaa !0
  %arrayidx2 = getelementptr inbounds [100 x i32]* %a, i32 0, i32 %i.015
  store i32 %add1, i32* %arrayidx2, align 4, !tbaa !0
  %inc = add nsw i32 %i.015, 1
  %cmp = icmp slt i32 %inc, %3
  br i1 %cmp, label %for.body, label %for.cond3.preheader

在上面的例子中我应该得到,

  %2
  %b
  %a
  %3

在其他基本块中声明和/或分配的内容 请建议我一个方法 提前致谢。

1 个答案:

答案 0 :(得分:2)

您好我没有测试过这个,但我会做这样的事情:

vector<Value*> values;
BasicBlock::iterator it;
User::op_iterator it;

// Iterate over all of the instructions in the Block
for (it=block->begin(); it++; it != block->end()){

    // Iterate over the operands used by an instruction. 'op_begin' Defined in llvm::User class.
    for (operand_it=it->op_begin(); operand_it++; operand_it != it->op_end() ){

        // Could this if else statement be reduced?
        // If this operand is an argument it was not defined in the block.
        if (isa<Argument>(operand_it)){
            values.push_back(operand_it);
        }
        // Otherwize, it could be a constant value or ... 
        else if (!isa<Instruction>(operand_it)){
            continue; 
        }
        // Check if the parent of the instruction is not the block in question.
        else if (((Instruction*)operand_it)->getParent() != block){
            values.push_back(operand_it);
        } 

    }

}