确定阵列索引

Determine array index

本文关键字:索引 阵列      更新时间:2023-10-16

我在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
}

,但我什么都没有作为输出。我在这里做错了什么?

首先,从 ConstantInt实例获取整数的正确方法是通过 getSExtValue()而不是 getValue();如果您还确保可以处理返回的值,最好是:

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

其次,仅通过第三操作数到GEP将其传递到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