c++ - LLVM中如何获取GetElementInst、AllocaInst或LoadInst的左值?

标签 c++ llvm llvm-ir

我想知道如何在 LLVM 中获取 GetElementInstAllocaInst 的左值。

IR如下:

%b15 = **getelementptr** inbounds %class.M* %c, i32 0, i32 2
%b16 = getelementptr inbounds %class.B* %b15, i32 0, i32 1
%6 = **load** i32* %b16, align 4
%add17 = add nsw i32 %6, 10
%b18 = getelementptr inbounds %class.M* %c, i32 0, i32 2
%b19 = getelementptr inbounds %class.B* %b18, i32 0, i32 1

我需要分析 IR。我想知道是否有任何方法可以获取 GetElementInstAllocaInstLoadIns 的左值,因为我必须分析它们之间的关系寄存器值。

希望得到您的帮助!


详细说明

事实上,我想跟踪所有对象的存储计数和加载计数。

为此,我遍历了所有的Instructions,得到了load&store的信息。但在 LLVM IR 作为跟随者中,前 3 条指令仅表示 %class.M* %c 的加载操作。

简而言之,对于 %b16 = getelementptr inbounds %class.B* %b15, i32 0, i32 1 这样的 GetElementPtrInst,我想得到结论%b16 属于 %b15

%b15 = getelementptr inbounds %class.M* %c, i32 0, i32 2
%b16 = getelementptr inbounds %class.B* %b15, i32 0, i32 1
%6 = load i32* %b16, align 4
%add17 = add nsw i32 %6, 10
%b18 = getelementptr inbounds %class.M* %c, i32 0, i32 2
%b19 = getelementptr inbounds %class.B* %b18, i32 0, i32 1

遍历所有指令的函数:

virtual bool runOnFunction(Function &F) {
    //errs() << "Begin" << "\n";
    errs() << F.getName() << "\n";
    char OpName[256];
    char OpType[256];
    for (auto &BB : F) {
        for (auto &I : BB) {
           /* if (auto *op = dyn_cast<AllocaInst>(&I)) {
                errs() << "allocaInst" << "\n";
                Value *OpV = I.getOperand(1);

                strcpy(OpName, OpV->getName().str().c_str());
                //get operand type
                auto *type = I.getAllocatedType();
                std::string typestring;
                raw_string_ostream S(typestring);
                type->print(S);
                S.flush();
                strcpy(OpType, typestring.c_str());
                createCallForParameterLine(op, 1, OpName, OpType, OpV);
            }
            else*/ if (auto *op = dyn_cast<StoreInst>(&I)) {
                errs() << "storeInst" << "\n";
                Value *OpV = I.getOperand(1);
                if (OpV->hasName() /*&& OpV->getType()->getTypeID() == 14*/) {
                    strcpy(OpName, OpV->getName().str().c_str());
                    //get operand type
                    auto *type = OpV->getType();
                    std::string typestring;
                    raw_string_ostream S(typestring);
                    type->print(S);
                    type->print(errs());
                    S.flush();
                    strcpy(OpType, typestring.c_str());
                    createCallForParameterLine(op, 1, OpName, OpType, OpV);
                }
            }
            else if (auto *op = dyn_cast<LoadInst>(&I)) {
                errs() << "loadInst" << "\n";
                Value *OpV = I.getOperand(0);
                if (OpV->hasName() /*&& OpV->getType()->getTypeID() == 14*/) {
                    strcpy(OpName, OpV->getName().str().c_str());
                    //get operand type
                    auto *type = OpV->getType();
                    std::string typestring;
                    raw_string_ostream S(typestring);
                    type->print(S);
                    S.flush();
                    strcpy(OpType, typestring.c_str());
                    createCallForParameterLine(op, 2, OpName, OpType, OpV);
                }
            }
        }
    }
}

最佳答案

[编辑以更好地匹配 OP 的问题]

您只需在指令的底层 User 上调用 getOperand(0) 即可获得任何指令的操作数。

在特定指令上,例如 LoadInstGetElementPtrInst,您还有许多其他方法,例如 getPointerOperand

这些方法在您的 llvm 代码库的 Instructions.h 中声明。

无论如何,这将返回一个 Value,您可以对其进行处理。

例如,在:

%b16 = getelementptr inbounds %class.B* %b15, i32 0, i32 1

inst->getPointerOperand 将返回 %b15。如果您需要 %b16,它只是您正在处理的值。

这是一个代码示例,试图执行您想要的[未测试]:

std::map<Value*, Value*> result;
for(auto &BB: F)
{
  for(auto &I: BB) 
  {
    switch(I.getOpcode()) {
      case Instruction::GetElementPtr:
        llvm::GetElementPtrInst* gep = llvm::dyn_cast<GetElementPtrInst>(&I);
        result.insert(std::pair<Value*, Value*>(&I, gep->getPointerOperand());
        break;
      //.. TODO other cases
    }
  }
}

然后您可以处理依赖关系图以适本地显示名称。

希望这能有所帮助。

关于c++ - LLVM中如何获取GetElementInst、AllocaInst或LoadInst的左值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39081419/

相关文章:

c++ - 第 3 方库提示 MAXPATHLEN 未在一个项目中声明,但未在其他项目中声明

c++ - 获取 LLVM 模块中的所有值

c++ - 如何为 LLVM IR 生成元数据?

llvm - LLVM 操作数的遍历

c++ - 如何使用 llvm 库

c++ - 通过事件和状态重用 boost msm 问题

c++ - Eigen 3.3.0 与 3.2.10 的性能回归?

java - JNI 访问对象的实例变量

c - 使用 llvm 提示编译器

c++ - 如何使用 LLVM API 获取全局变量的初始化器