9

有时我发现 llvm IR 中的标签标识符以逗号“;” ; <label> 6 开头,但是据我所知,逗号用于注释。那么llvm如何检索评论中的标签信息呢?我错过了什么吗?
接下来是一个简单的测试。
C源文件:

#include <stdio.h>

int main()
{
 int a;
 scanf("%d", &a);
 if ( a > 3)
  a *= 2;
 return 0;
}

http://llvm.org/demo/index.cgi生成的 llvm IR 代码(与 clang -c -emit-llvm main.c 相同)如下:

; ModuleID = '/tmp/webcompile/_13654_0.bc'

@.str = private unnamed_addr constant [3 x i8] c"%d\00", align 1

define i32 @main() nounwind uwtable {
  %a = alloca i32, align 4
  %1 = call i32 (i8*, ...)* @__isoc99_scanf(i8* getelementptr inbounds ([3 x i8]* @.str, i64 0, i64 0), i32* %a) nounwind
  %2 = load i32* %a, align 4, !tbaa !0
  %3 = icmp sgt i32 %2, 3
  br i1 %3, label %4, label %6

; <label>:4                                       ; preds = %0
  %5 = shl nsw i32 %2, 1
  store i32 %5, i32* %a, align 4, !tbaa !0
  br label %6

; <label>:6                                       ; preds = %4, %0
  ret i32 0
}
4

3 回答 3

7

In LLVM IR a block does not need an explicit label. Instructions are the same way which leads to the %1, %2, %3. LLVM assigns numbers to unnamed instructions and blocks in increasing order. The br i1 %3... terminates the first block and the last used number label is 3 so the next block gets labelled as 4. That block ends with the next br instruction and the last used number is 5 so the next and final block is labelled with 6. At first it might seem weird that blocks and instructions share the same namespace, but remember that blocks are values too.

于 2012-03-19T16:00:00.653 回答
2

尽管有这样的措辞,%4inlabel %4不是标签,它只是对块的引用。您是对的,这很混乱,请参阅此问题进行讨论。

于 2013-12-19T13:47:26.083 回答
1

您可以尝试在您的 IR 上运行 instnamer 传递,这将为所有内容提供显式名称,因此您无需担心查找隐式名称。

于 2014-06-16T20:40:15.320 回答