gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:12:39 32 4
gpt4 key购买 nike

我想知道如何在 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com