2014-02-15 77 views
4

我有這樣的字節代碼片段:LLVM找不到getelementptr指令

define void @setGlobal(i32 %a) #0 { 
entry: 
    %a.addr = alloca i32, align 4 
    store i32 %a, i32* %a.addr, align 4 
    %0 = load i32* %a.addr, align 4 
    store i32 %0, i32* @Global, align 4 
    %1 = load i32* %a.addr, align 4 
    store i32 %1, i32* getelementptr inbounds ([5 x i32]* @GlobalVec, i32 0, i64 0), align 4 
    store i32 2, i32* getelementptr inbounds ([5 x i32]* @GlobalVec, i32 0, i64 2), align 4 
    ret void 
} 

我使用這個代碼,以找到「存儲123-132%1 getelementptr,123-132 * getelementptr界外球([5×123-132] * @GlobalVec,123-132 0,I64 0),排列4" :

for (Module::iterator F = p_Module.begin(), endF = p_Module.end(); F != endF; ++F) { 

     for (Function::iterator BB = F->begin(), endBB = F->end(); BB != endBB; ++BB) { 

      for (BasicBlock::iterator I = BB->begin(), endI = BB->end(); I 
        != endI; ++I) { 
       if (StoreInst* SI = dyn_cast<StoreInst>(I)) { 
        if (Instruction *gep = dyn_cast<Instruction>(SI->getOperand(1))) 
        { 
         if (gep->getOpcode() == Instruction::GetElementPtr) 
         { 
          //do something 
         } 

        } 

       } 

      } 
     } 
} 

此代碼不能找到getelementptr。我究竟做錯了什麼?

回答

3

在你的位碼片段中沒有getelementptr指令,這就是爲什麼你找不到它們。

兩個案例,看起來像一個getelementptr指令實際上constant expressions - 的警示信號是,他們出現另一個指令(store),這是不是你可以用常規指令做的一部分。

所以,如果你想搜索該表達式,你需要尋找type GetElementPtrConstantExpr,而不是GetElementPtrInst