c/c++语言开发共享设置llvm :: ConstantInt的值

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

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

    /* ... 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, eg: */ 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); } 

    要单独“修改”常量,有一个解决方案(说明增量和减量):

      /// 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在关于创建和不删除常量的请求中有重要的评论https://llvm.org/docs/doxygen/html/Constant_8h_source.html

    需要了解更多c/c++开发分享设置llvm :: ConstantInt的值,也可以关注C/ C++技术分享栏目—计算机技术网(www.ctvol.com)!

     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/c++开发分享设置llvm :: ConstantInt的值相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/980555.html

      (0)
      上一篇 2021年12月14日
      下一篇 2021年12月14日

      精彩推荐