llvm迭代时使用替换intwithvalue替换指令

发布于 2025-01-21 23:18:57 字数 1165 浏览 0 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

污味仙女 2025-01-28 23:18:57

在删除“当前”指令之前,您可以增加迭代器。

这样的事情:

bool Changed = true;
while(Changed) {
  Changed = false;
  for(BasicBlock &BB : F) {

    auto InstIter = BB.begin();
    while(InstIter != BB.end()) {
      Instruction &Inst = *InstIter;
      auto Res = isAlgebracIdentity(Inst);
      bool IsAlgebrac = Res.first;
          
      ++InstIter; // increase the iterator

      if (IsAlgebrac) {
        Value * V = Res.second;
        // erase the instruction before InstIter
        ReplaceInstWithValue(BB.getInstList(), Inst.getIterator(), V);
        Changed = true;
      }
    }
  }
}

You could increase the iterator before removing the "current" instruction.

Something like this:

bool Changed = true;
while(Changed) {
  Changed = false;
  for(BasicBlock &BB : F) {

    auto InstIter = BB.begin();
    while(InstIter != BB.end()) {
      Instruction &Inst = *InstIter;
      auto Res = isAlgebracIdentity(Inst);
      bool IsAlgebrac = Res.first;
          
      ++InstIter; // increase the iterator

      if (IsAlgebrac) {
        Value * V = Res.second;
        // erase the instruction before InstIter
        ReplaceInstWithValue(BB.getInstList(), Inst.getIterator(), V);
        Changed = true;
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文