2017-02-16 80 views
1

我有一個簡單的C程序。獲取從寄存器存儲在LLVM中的值IR

int 
1.main(int argc, char **argv) { 
2. unsigned buffer[4] = { 0, 0, 0, 0 }; 
3. return buffer[argc]; 
4. } 

和IR代碼如下

; Function Attrs: norecurse nounwind readnone uwtable 
define i32 @main(i32 %argc, i8** nocapture readnone %argv) #0 !dbg !6 { 
    %buffer = alloca [4 x i32], align 16 
    tail call void @llvm.dbg.value(metadata i32 %argc, i64 0, metadata !14, metadata !21), !dbg !22 
    tail call void @llvm.dbg.value(metadata i8** %argv, i64 0, metadata !15, metadata !21), !dbg !23 
    %1 = bitcast [4 x i32]* %buffer to i8*, !dbg !24 
    call void @llvm.lifetime.start(i64 16, i8* %1) #3, !dbg !24 
    tail call void @llvm.dbg.declare(metadata [4 x i32]* %buffer, metadata !16, metadata !21), !dbg !25 
    call void @llvm.memset.p0i8.i64(i8* %1, i8 0, i64 16, i32 16, i1 false), !dbg !26 
    %2 = sext i32 %argc to i64, !dbg !28 
    %3 = getelementptr inbounds [4 x i32], [4 x i32]* %buffer, i64 0, i64 %2, !dbg !28 
    %4 = load i32, i32* %3, align 4, !dbg !28, !tbaa !29 
    call void @llvm.lifetime.end(i64 16, i8* %1) #3, !dbg !33 
    ret i32 %4, !dbg !34 
} 

我想比較在3線的訪問索引是否是有效的索引。爲了進行比較,我需要提取爲argc存儲的值。下面是一段代碼,我已經寫obain ARGC的VVALUE

auto gep = llvm::dyn_cast<llvm::GetElementPtrInst>(inst); 
auto operand2 = gep->getOperand(2); 
outs() << "operand 2 "<<*operand2<<"\n"; 
auto newOperand =operand2.getOperand(0); 
outs()<<"New operand "<<*newOperand<<"\n"; 

輸出: - !

數2%2 =六重峯123-132%的argc到I64,DBG 28

新操作i32%argc

如何獲取%argc的值?

回答

0

getOperand函數的返回值是Value*對象argc變量(在您的代碼中的變量newOperand)。您可以將該值傳遞給IR中可能要注入的任何新指令(例如CreateICmpEQ),以將argc的值與某個常數值進行比較。