设置llvm :: ConstantInt的值 [英] Set value for llvm::ConstantInt

查看:892
本文介绍了设置llvm :: ConstantInt的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩LLVM。我想到了在中间代码中改变常量的值。但是,对于类 llvm :: ConstantInt ,我看不到setvalue函数。任何想法如何修改IR代码中的常数值?

I'm playing around with LLVM. I thought about changing value of a constant in the intermediate code. However, for the class llvm::ConstantInt, I don't see a setvalue function. Any idea how can I modify value of a constant in the IR code?

推荐答案

ConstantInt是一个工厂,不是吗?类具有 get方法来构造新常量:

ConstantInt is a factory, isn't it? Class has the get method to construct new constant:

     /* ... return a ConstantInt for the given value. */
00069   static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);

所以,我想,你不能修改现有的ConstantInt。如果你想修改IR,你应该尝试改变指针到参数(改变IR本身,而不是常量对象)。

So, I think, you can't modify existing ConstantInt. If you want to modify IR, you should try to change pointer to argument (change the IR itself, but not the constant object).

可能你想要这样(请记住,我对LLVM没有经验;我几乎可以肯定示例不正确。)

May be you want something like this (please remember, I have zero experience with LLVM; and I'm almost sure example is incorrect).

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);
}



单独修改常数有一个解决方案(递增和递减< a href =http://llvm.org/doxygen/InstCombineAddSub_8cpp_source.html#l00023 =nofollow>图示):

 /// 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

这篇关于设置llvm :: ConstantInt的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆