小编Pre*_*and的帖子

在LLVM IR中查找正确的数组大小

我需要在GetElementPtr指令中计算数组的大小(以字节为单位)I.我之前一直在使用以下逻辑来推导:

/* Get the bitWidth of the item */
int bitWidth = cast<IntegerType>(I->getOperand(2)->getType())->getBitWidth();

/* Find total number of elements in array */
Type *T = cast<PointerType>(cast<GetElementPtrInst>(I)->getPointerOperandType())->getElementType();
int no_of_elements = cast<ArrayType>(T)->getNumElements();

/* Compute total and return bytes */
return (no_of_elements * bitWidth) / 8
Run Code Online (Sandbox Code Playgroud)

但是有一些棘手的案例,如下面的地方,它打破了.答案是1024字节但我的上述逻辑将给出2048,因为它完全没有意识到i32

%arrayidx932 = getelementptr inbounds [256 x i32], [256 x i32]* @array5, i64 0, i64 %idxprom931, !dbg !168
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我纠正我的逻辑吗?

arrays pointers llvm clang

1
推荐指数
1
解决办法
1217
查看次数

标签 统计

arrays ×1

clang ×1

llvm ×1

pointers ×1