确定阵列索引
Determine array index
我在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
相关文章:
- 数组索引的值没有增加
- OpenMP阵列性能较差
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 查找最接近的大于当前数字的数字的索引
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- 在C++中调整向量中的索引
- 当我的阵列太大时出现分段错误
- 重载元组索引运算符-C++
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 访问特定阵列位置/索引时出现分段错误
- 如何将C 枚举类枚举作为std ::阵列索引而没有明确的铸件
- Char阵列中的特定索引的C 拉动签名整数
- C阵列在MIPS组件中索引
- C++动态阵列索引和容量问题
- 用原子索引从静音阵列中锁定互斥品
- 查找有效内部圆形内部阵列的索引
- 负x,y坐标为线性阵列索引
- 确定阵列索引
- 阵列索引(转换为整数)用范围枚举
- 如何交换2D阵列的最大和最小成员索引