2014-10-12 116 views
2

我定義了int a = 5;在源代碼中,我變換源LLVM IR:llvm pass:如何使用現有變量值插入變量

%a = alloca i32, align 4 
store i32 5, i32* %a, align 4 

我想通過寫一通插入int b = a;。我編譯int a=5; int b=a到LLVM IR中,它首先加載「a」,然後存儲它。我也檢查了doxygen,其中LoadInst是LoadInst (Value *Ptr, const Twine &NameStr, Instruction *InsertBefore)不過,我不知道如何得到「a」的Value

如何獲取變量值?

回答

2

在LLVM IR序列

int a = 5; 
int b = a; 

不使用任何優化,翻譯爲

%a = alloca i32, align 4 
%b = alloca i32, align 4 
store i32 5, i32* %a, align 4 
%0 = load i32* %a, align 4 
store i32 %0, i32* %b, align 4 

這對應於兩個AllocaInst S,2個StoreInst S和LoadInst如下

警告:未經測試/未編譯的僞代碼

ConstantInt* const_int_5 = ConstantInt::get(llvmContext, APInt(32, StringRef("5"), 10)); 

AllocaInst* a_alloc = new AllocaInst(IntegerType::get(llvmContext, 32), "a"); 
AllocaInst* b_alloc = new AllocaInst(IntegerType::get(llvmContext, 32), "b"); 
StoreInst* store_5 = new StoreInst(const_int_5, a_alloc, false); 
LoadInst* load_from_a = new LoadInst(a_alloc, "", false); 
StoreInst* store_b = new StoreInst(load_from_a, b_alloc, false); 

你可能會感到困惑,因爲指令處於LLVM API歸功於精心設計的繼承層次的價值

+0

對不起,我想你誤解了我的意思。我可以用'llc -march'得到類似的答案。我應該如何將IR問題轉換爲IR問題?由於在IR代碼中存在alloca和store inst,我應該怎麼做才能獲得「a_alloca」? – winter333 2014-10-13 01:31:26

+0

我不明白你的句子的英文。你想將「a」變量的名稱改爲「a_alloca」嗎?如果是這樣,請修改'AllocaInst'構造函數的'const Twine&Name ='「'參數 – 2014-10-13 07:21:59