【问题标题】:Finding the list of successors of an Instruction in LLVM在 LLVM 中查找指令的后继列表
【发布时间】:2017-01-12 04:01:03
【问题描述】:

我想获取 llvm 中每条指令的后继列表。如果我理解正确,对于除分支 (br) 之外的所有指令,后继指令是下一条指令。但是对于分支指令来说有点棘手。

例如,如果我有以下 C 代码:

int main() {
  int a = 7;
  int b = a * 2;

  int x;
  if (a < 3) {
    x = 10 + b;
  } else {
    x = 20 + a;
  }
  return b;

}

我得到以下字节码:

define i32 @main() #0 {
%1 = mul nsw i32 7, 2
%2 = icmp slt i32 7, 3
br i1 %2, label %3, label %5

; <label>:3:                                      ; preds = %0
%4 = add nsw i32 10, %1
br label %7

; <label>:5:                                      ; preds = %0
%6 = add nsw i32 20, 7
br label %7

; <label>:7:                                      ; preds = %5, %3
ret i32 %1
}

所以说明

br i1 %2,标签 %3,标签 %5

有 2 个继任者:

{%4 = 添加新 i32 10, %1, %6 = 添加 nsw i32 20, 7}

我怎样才能从指令中获得对继任者的访问权?

注意:我真正想做的是消除死代码。我知道 llvm 在实时分析库中有一些方法,例如 IsInstructionTriviallyDead() 。出于练习目的,我不打算使用它们。

【问题讨论】:

    标签: llvm


    【解决方案1】:

    您可以使用BranchInst 上的getSuccessor(unsigned)getNumSuccessors() 方法获取指令可以分支到的基本块。给定BasicBlock *BB,然后您可以通过BB-&gt;front() 访问第一条指令。

    【讨论】:

    • 非常感谢 :) 是的。只是出于好奇,有没有一种方法可以使用迭代器来完成?所以说把所有基本块的指令放在一个向量中并遍历它们?
    猜你喜欢
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多