llvm迭代时使用替换intwithvalue替换指令
我是LLVM的新手,我想使用函数replaceinstwithvalue()
删除一些无用的说明。
让我们看看以下示例:
define dso_local i32 @foo(i32 %0, i32 %1) #0 {
%3 = add nsw i32 %1, 0
%4 = add nsw i32 %0, 0
%5 = add nsw i32 %3, 1
%6 = add nsw i32 %5, 1
%7 = add nsw i32 %6, 0
ret i32 %7
}
有一些无用的说明,例如%3 =添加NSW i32%1,0
,这是因为我通过命令clang -clang -12 -o0 -o0 -xclang生成了字节码-disable -o0 -optnone -emit -llvm -c main.c -o main.bc
。我知道在禁止迭代时删除迭代者,所以我在下面写了一个非常丑陋的代码:
bool Changed = true;
while(Changed){
Changed = false;
for(auto BBIter = F.begin(); BBIter != F.end(); BBIter++){
auto &BB = *BBIter;
for(auto InstIter = BB.begin(); InstIter != BB.end(); InstIter++){
auto &Inst = *InstIter;
auto Res = isAlgebracIdentity(Inst);
auto IsAlgebrac = Res.first;
Value * V = Res.second;
if(IsAlgebrac){
ReplaceInstWithValue(BB.getInstList(), InstIter, V);
BB.print(outs());
Changed = true;
break;
}
}
}
}
这是一种非常蛮力的方法,所以我想知道有什么更好的处理方法吗?
I'm new to LLVM, and I want to use the function ReplaceInstWithValue()
to delete some useless instructions.
Let's see the following example:
define dso_local i32 @foo(i32 %0, i32 %1) #0 {
%3 = add nsw i32 %1, 0
%4 = add nsw i32 %0, 0
%5 = add nsw i32 %3, 1
%6 = add nsw i32 %5, 1
%7 = add nsw i32 %6, 0
ret i32 %7
}
There are some useless instructions such as %3 = add nsw i32 %1, 0
, this is because I generate bytecode by the command clang-12 -O0 -Xclang -disable-O0-optnone -emit-llvm -c main.c -o main.bc
. I know that delete an iterater when iterating it is forbidden, so I write a very ugly code below:
bool Changed = true;
while(Changed){
Changed = false;
for(auto BBIter = F.begin(); BBIter != F.end(); BBIter++){
auto &BB = *BBIter;
for(auto InstIter = BB.begin(); InstIter != BB.end(); InstIter++){
auto &Inst = *InstIter;
auto Res = isAlgebracIdentity(Inst);
auto IsAlgebrac = Res.first;
Value * V = Res.second;
if(IsAlgebrac){
ReplaceInstWithValue(BB.getInstList(), InstIter, V);
BB.print(outs());
Changed = true;
break;
}
}
}
}
This is a very brute-force approach, so I wonder any better way to deal with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在删除“当前”指令之前,您可以增加迭代器。
这样的事情:
You could increase the iterator before removing the "current" instruction.
Something like this: