2012-11-01 43 views
2

我有在C代碼以下循環:確定數組索引

int f[10],b[10],k=0; 
for(int i = 0; i < 10; i++) 
{ 
    k = b[i+3]*f[i+2]; //please ignore the out of bound problem 
} 

我想確定數組b具有3步幅和f已在上面的代碼遞增的2因素。

生成LLVM組件(對於含循環塊):

;<label>:12 
%13 = load i32* %i, align 4 
%14 = icmp slt i32 %13, 10 
br i1 %14, label %15, label %30 

;<label>:15       ;preds=%12 
%16 = load i32* %i, align 4 
%17 = add nsw i32 %16,**3** // this is the increment value 
%18 = sext i32 %17 to i64 
**%19 = getelementptr inbounds [10 x i32]* %b, i32 0, i64 % 18** 
%20 = load i32* % 19, align 4 
%21 = load i32* %i, align 4 
%22 = add nsw i32 %21,**2** // this is the increment value 
%23 = sext i32 %22 to i64 
**%24 = getelementptr invounds[10xi32]* %f, i32 0, i64 %23** 
%25 = load i32* %24, align 4 
%26 = mul nsw i32 %20, %25 
store i32 %26, i32* %k, align 4 
br label %27 

;<label>:27 
%28 = load i32* %l, align 4 
%29 = add nsw i32 %28,1 
store i32 %29, i32* %i, align 4 
br label %12 

現在在我的LoopPass,我使用下面的代碼:

Value *f = gep->getOperand(3); 
if(dyn_cast<llvm::ConstantInt>(f)) 
{ 
    errs()<<(dyn_cast<llvm::ConstantInt>(f))->getValue(); 
    // the output should be 3 and 2 respectively 

} 

但我沒有得到任何東西作爲輸出。我在這裏做錯了什麼?

+2

如果使用術語步幅,我本來期望'I + 3'是'i * 3'(同樣,'i + 2' - >'i * 2') – CAFxX

+0

另外,生成的IR看起來沒有任何循環。 – CAFxX

+0

@CAFxX現在我已經包含了整個裝配循環。此外,我已經改變步伐增加因素。 – krammer

回答

1

首先,從ConstantInt實例獲取整數的正確方法是通過getSExtValue()而不是getValue();它是最好的,如果你還需要確保你能處理返回的值,因此:

assert (CI->getBitWidth() <= 32); 
int x = CI->getSExtValue(); 

其次,剛開始是3操作數的GEP不會讓你一個ConstantInt傳遞的值,它會幫你指向sext指令!如果要查找實際常數,則必須一直遍歷圖形,直到找到指令add,然後將該常數標識爲其操作數之一。

最後,看來你正在尋找補償,而不是步驟;但如果你尋找步驟,可以考慮使用標量演化,其中有機制,你可能會發現有用,如:

/// getStepRecurrence - This method constructs and returns the recurrence 
/// indicating how much this expression steps by. If this is a polynomial 
/// of degree N, it returns a chrec of degree N-1. 
/// We cannot determine whether the step recurrence has self-wraparound. 
const SCEV *getStepRecurrence(ScalarEvolution &SE) const 
+0

謝謝。是否有可能檢測到第三個參數的存在,即如果數組是b [i]?我也可以概括如下:如果在getelementprtinst中有第三個參數,並且它是一個數組,則返回,直到找到add指令。然後從那裏提取第二個操作數項? – krammer

+0

還是可以退回兩條指令? (因爲添加後跟sext,後面跟着getElementPtrInstr) – krammer

+0

@krammer關於移回兩條指令 - 你可以嘗試,但我有一種感覺它會是片狀。我認爲最好嘗試和推廣它。你當然可以使用'getNumIndices()','isa <>'等方法來識別這種情況。 – Oak