C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 设置llvm :: ConstantInt的值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在玩LLVm.我想过在中间代码中改变常量的值.但是,对于类 llvm::ConstantInt,我没有看到SETVALue函数.任何想法如何修改IR代码中常量的值?

解决方法

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

大佬总结

以上是大佬教程为你收集整理的c – 设置llvm :: ConstantInt的值全部内容,希望文章能够帮你解决c – 设置llvm :: ConstantInt的值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。