Set value for llvm :: ConstantInt

I play with LLVM. I was thinking about changing the constant value in the intermediate code. However, for the llvm :: ConstantInt class, I do not see the setvalue function. Any idea how I can change the constant value in IR code?

+6
source share
1 answer

ConstantInt is a factory, isn't it? The class has a get method to create a new constant:

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

So, I think you cannot modify the existing ConstantInt. If you want to change the IR, you should try to change the pointer to the argument (change the IR itself, but not a permanent object).

Maybe you want something like this (remember, I have no experience with LLVM, and I'm pretty sure the example is wrong).

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

There is a solution for β€œmodifying” one constant (increment and decrement are shown ):

  /// 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 has an important comment in begging to create and not delete constants 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 
+12
source

Source: https://habr.com/ru/post/907957/


All Articles