gpt4 book ai didi

pointers - 如何比较 LLVM-IR 中的指针?

转载 作者:行者123 更新时间:2023-12-01 08:23:00 25 4
gpt4 key购买 nike

我想分析 LLVM IR 中的指针值。

LLVM Value Class中所示,

Value is is a very important LLVM class. It is the base class of all values computed by a program that may be used as operands to other values. Value is the super class of other important classes such as Instruction and Function. All Values have a Type. Type is not a subclass of Value. Some values can have a name and they belong to some Module. Setting the name on the Value automatically updates the module's symbol table.

要测试Value是否是指针,有一个函数a->getType()->isPointerTy()。 LLVM 还提供了 LLVM PointerType class ,但是没有直接的 api 来比较指针的值。

所以我想知道如何比较这些指针值,以测试它们是否相等。我知道有 AliasAnalysis,但我对 AliasAnalysis 结果有疑问,所以我想自己验证一下。

最佳答案

快速解决方案是使用IRBuilder::CreatePtrDiff。这将计算两个指针之间的差异,并返回 i64 结果。如果指针相等,则该值为零,否则为非零。

这可能看起来有些过分,因为 CreatePtrDiff 会付出额外的努力来根据元素数量而不是字节数量来计算结果 em>,但额外的除法很可能会被优化掉。

另一种选择是使用 ptrtoint 指令,并具有相当大的结果类型(例如 i64),然后进行整数比较。

来自在线引用:

Value *     CreatePtrDiff (Value *LHS, Value *RHS, const Twine &Name="")
Return the i64 difference between two pointer values, dividing out the size of the pointed-to objects.

关于pointers - 如何比较 LLVM-IR 中的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40733325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com