C++ 如何在LLVM中获取GetElementInst、AllocaInst或LoadInst的左值?
我想知道如何在LLVM中获取C++ 如何在LLVM中获取GetElementInst、AllocaInst或LoadInst的左值?,c++,llvm,llvm-ir,C++,Llvm,Llvm Ir,我想知道如何在LLVM中获取GetElementInst或AllocaInst的左值 调查结果如下: %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 = getelementp
GetElementInst
或AllocaInst
的左值
调查结果如下:
%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
我需要分析红外光谱。我想知道是否有任何方法可以获得GetElementInst
、AllocaInst
或LoadIns
的左值,因为我必须分析所有寄存器值之间的关系
希望你的帮助
详细说明 事实上,我想跟踪所有对象的存储计数和加载计数 为此,我遍历所有指令以获取加载和存储信息。但在LLVM IR中,前3条指令只是表示
%class.M*%c
的加载操作
简而言之,对于一个GetElementPtrInst
,比如%b16=getelementptr inbounds%class.B*%b15,i32 0,i32 1
,我想得出结论,%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);
}
}
}
}
}
virtualbool runOnFunction(函数&F){
//错误()打印;
键入->打印(errs());
S.flush();
strcpy(OpType,typestring.c_str());
createCallForParameterLine(op,1,OpName,OpType,OpV);
}
}
否则如果(自动*op=dyn_转换(&I)){
errs()getType()->getTypeID()==14*/){
strcpy(OpName,OpV->getName().str().c_str());
//获取操作数类型
auto*type=OpV->getType();
字符串类型字符串;
原始字符串(类型字符串);
类型->打印;
S.flush();
strcpy(OpType,typestring.c_str());
createCallForParameterLine(op,2,OpName,OpType,OpV);
}
}
}
}
}
[编辑以更好地匹配OP的问题]
只需对指令的底层用户调用User
,就可以拥有任何指令的操作数
在特定的指令中,例如LoadInst
或GetElementPtrInst
,您还有许多其他方法,例如getPointerOperand
这些方法在llvm代码库的指令.h中声明
无论如何,这将返回一个值
,您可以在处理过程中使用该值
例如,关于:
%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
}
}
}
std::映射结果;
用于(自动和BB:F)
{
用于(自动和输入:BB)
{
开关(I.getOpcode()){
案例说明::GetElementPtr:
llvm::GetElementPtrInst*gep=llvm::dyn_cast(&I);
insert(std::pair(&I,gep->getPointerOperand());
打破
//…处理其他案件
}
}
}
然后,您可以处理依赖关系映射以适当地显示名称
希望这能有所帮助。[编辑以更好地匹配OP的问题] 只需对指令的底层用户调用
User
,就可以拥有任何指令的操作数
在特定的指令中,例如LoadInst
或GetElementPtrInst
,您还有许多其他方法,例如getPointerOperand
这些方法在llvm代码库的指令.h中声明
无论如何,这将返回一个值
,您可以在处理过程中使用该值
例如,关于:
%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
}
}
}
std::映射结果;
用于(自动和BB:F)
{
用于(自动和输入:BB)
{
开关(I.getOpcode()){
案例说明::GetElementPtr:
llvm::GetElementPtrInst*gep=llvm::dyn_cast(&I);
insert(std::pair(&I,gep->getPointerOperand());
打破
//…处理其他案件
}
}
}
然后,您可以处理依赖关系映射以适当地显示名称
希望这能有所帮助。抱歉,我对“如果您需要%b16,它只是您正在处理的值”感到困惑.事实上,我想得到%b15和%b16之间的关系。也就是说,我想根据这条指令同时得到%b15和%b16。那么有没有办法得到这些值呢?%b16是指令本身,%b15是它的第一个操作数。或者我不明白你所说的“值”是什么意思在我看来,%b15和%b16是寄存器值,我可以使用
Value::getName()获取它们的名称
。我想找到它们之间的关系。例如,在:%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
我感到困惑。请您分享您已经拥有的代码好吗?我已经更改了我的问题描述,很抱歉操作错误。很抱歉,我对“如果您需要%b16,它只是您正在处理的值”感到困惑.事实上,我想得到%b15和%b16之间的关系。也就是说,我想根据这条指令同时得到%b15和%b16。那么有没有办法得到这些值呢?%b16是指令本身,%b15是它的第一个操作数。或者我不明白你所说的“值”是什么意思在我看来,%b15和%b16是寄存器值,我可以使用Value::getName()
获取它们的名称