2017-09-13 105 views
1

如何在LLVM傳遞中識別帶註釋的變量?在LLVM傳遞中識別帶註釋的變量

#include <stdio.h> 

int main(){ 
int x __attribute__((annotate("my_var")))= 0; 
int a,b; 
x = x + 1; 
a = 5; 
b = 6; 
x = x + a; 

return x; 
} 

例如,我想鑑定具有註釋的變量(x在這種情況下)的指令並打印出來(X = X + 1;以及x = X + A) 我怎樣才能實現這個?

這是使用LLVM

; ModuleID = 'test.c' 
source_filename = "test.c" 
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" 
target triple = "aarch64" 

@.str = private unnamed_addr constant [7 x i8] c"my_var\00", section "llvm.metadata" 
@.str.1 = private unnamed_addr constant [7 x i8] c"test.c\00", section "llvm.metadata" 

; Function Attrs: noinline nounwind optnone 
define i32 @main() #0 { 
    %1 = alloca i32, align 4 
    %2 = alloca i32, align 4 
    %3 = alloca i32, align 4 
    %4 = alloca i32, align 4 
    store i32 0, i32* %1, align 4 
    %5 = bitcast i32* %2 to i8* 
    call void @llvm.var.annotation(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.s$ 
    store i32 0, i32* %2, align 4 
    %6 = load i32, i32* %2, align 4 
    %7 = add nsw i32 %6, 1 
    store i32 %7, i32* %2, align 4 
    store i32 5, i32* %3, align 4 
    store i32 6, i32* %4, align 4 
    %8 = load i32, i32* %2, align 4 
    %9 = load i32, i32* %3, align 4 
    %10 = add nsw i32 %8, %9 
    store i32 %10, i32* %2, align 4 
    %11 = load i32, i32* %2, align 4 
    ret i32 %11 
} 

; Function Attrs: nounwind 
declare void @llvm.var.annotation(i8*, i8*, i8*, i32) #1 

attributes #0 = { noinline nounwind optnone "correctly-rounded-divide-sqrt-fp-math"="false" $ 
attributes #1 = { nounwind } 

!llvm.module.flags = !{!0} 
!llvm.ident = !{!1} 
!0 = !{i32 1, !"wchar_size", i32 4} 
+0

首先將此代碼編譯爲IR並查看生成的內容。 – arrowd

+0

我做到了。我可以將生成的元數據看作全局字符串。但是,我不清楚如何使用這些信息來識別特定的變量。 – Dragonight

+0

向我們展示生成的代碼,也許我們會得到一條線索。 – arrowd

回答

0

你必須在指令循環,並確定調用llvm.var.annotation

第一個參數生成的.ll文件的指針批註的變量(8-18 *)。

要獲得實際的帶註釋的變量,您需要找到這個指針指向的內容。

就你而言,這是bitcast指令的源操作數。

+0

如何識別對llvm.var.annotation的調用?無法弄清楚如何做到這一點。 – Dragonight