2016-09-16 134 views
0

我正在分析使用的指令。鑑於我發現 有興趣的說明,我想回溯所有使用的操作和 變量。例如在LLVM IR中給出以下循環。LLVM中的回溯操作和操作數

%i.01 = phi i32 [ 0, %0 ], [ %4, %2 ] 
%3 = icmp eq i32 %i.01, 0 
%bar.foobar.i = select i1 %3, void()* @bar, void()* @foobar 
tail call void %bar.foobar.i() #2 
%4 = add nuw nsw i32 %i.01, 1 
%exitcond = icmp eq i32 %4, 10 
br i1 %exitcond, label %1, label %2 

我很感興趣的例子在最後一個分支的所有相關數據。在使用涉及變量%4和常數10ICompInst之前計算條件一條指令。在使用加法之前一步計算%4。等等等等......

鑑於我找到了感興趣的說明,在這種情況下BranchInst,我想開始我的回溯。

if(BranchInst* BI = dyn_cast<BranchInst>(&I)) { 
    if(BI->isConditional()) { 
    backtrack(BI, &I); 
    } 
} 

爲了實現這樣的算法,我有兩個問題。有分支指令,我怎樣才能獲得啓動回溯的條件? 其次,我怎樣才能確定條件是否在先前的指令中使用?要了解更容易,我有以下的僞代碼:

void backtrack(BranchInst* BI, Instruction* I) { 
    Value* condition = BI->getCondition(); 
    Instruction* prevInst = I; 

    do { 
    prevInst = prev(prevInst); 
    if(prevInst->prevInst(condition)) { 
     backTrackOperand(prevInst->getOperand(0), prevInst); 
     backTrackOperand(prevInst->getOperand(1), prevInst); 
     break; 
    } 
    } while(1); 
} 

void backTrackOperand(Value* operand, Instruction* I) { 
    Instruction* prevInst = I; 

    do { 
    prevInst = prev(prevInst); 
    if(prevInst->usesOperand(operand)) { 
     ... 
    } 
    } 
} 

所以我怎麼可能實現的功能getCondition()computesCondition()usesOperand()

回答

0
  • getCondition()是getOperand(0)。
  • 我沒有在你的代碼中看到computesCondition()?
  • usesOperand()類似於llvm::find_if(prevInst->operands(), [&] (Uses &U) { return isa<Instruction>(&U) && cast<Instruction>(&U) == I; });。但是請注意,從條件或任何值,你可以很容易地找到所有的用途。