我在玩LLVM。我考虑在中间代码中更改常量的值。但是,对于类llvm::ConstantInt,我没有看到setvalue函数。你知道如何在IR代码中修改常量的值吗?
发布于 2012-02-08 16:27:01
ConstantInt是一家工厂,不是吗?类具有构造新常量的get method:
/* ... return a ConstantInt for the given value. */
00069 static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
所以,我认为,你不能修改现有的ConstantInt。如果要修改IR,应尝试更改指向参数的指针(更改IR本身,但不更改常量对象)。
也许您想要这样的东西(请记住,我没有使用LLVM的经验;我几乎可以肯定示例是不正确的)。
Instruction *I = /* your argument */;
/* check that instruction is of needed format, e.g: */
if (I->getOpcode() == Instruction::Add) {
/* read the first operand of instruction */
Value *oldvalue = I->getOperand(0);
/* construct new constant; here 0x1234 is used as value */
Value *newvalue = ConstantInt::get(oldValue->getType(), 0x1234);
/* replace operand with new value */
I->setOperand(0, newvalue);
}
要单独“修改”一个常量,有一个解决方案(递增和递减are illustrated):
/// AddOne - Add one to a ConstantInt.
static Constant *AddOne(Constant *C) {
return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
}
/// SubOne - Subtract one from a ConstantInt.
static Constant *SubOne(ConstantInt *C) {
return ConstantInt::get(C->getContext(), C->getValue()-1);
}
PS,Constant.h在关于创建和不删除常量http://llvm.org/docs/doxygen/html/Constant_8h_source.html的请求中有重要的评论
00035 /// Note that Constants are immutable (once created they never change)
00036 /// and are fully shared by structural equivalence. This means that two
00037 /// structurally equivalent constants will always have the same address.
00038 /// Constants are created on demand as needed and never deleted: thus clients
00039 /// don't have to worry about the lifetime of the objects.
00040 /// @brief LLVM Constant Representation
https://stackoverflow.com/questions/9196009
复制相似问题