2012-02-08 5 views
6

私はLLVMで遊んでいます。私は中間コードの定数の値を変更することを考えました。しかし、クラスllvm::ConstantIntのために、私はsetValueの機能が表示されません。どのように私はIRコードの定数の値を変更できますか?設定値:: ConstantInt

答えて

12

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とゼロの経験を持っている)

は、あなたがこのような何かをしたいように。そこのソリューションです(インクリメントとare illustratedをデクリメント)単独の定数を「修正」し

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

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

あなたの解決策はよく見えます。:) – MetallicPriest

+0

私は願って、@Anton Korobeynikovは、私のコードに答えたり、コメントします。また、setOperandは定数そのものを変更することはできません。 – osgx

+0

それは働いた!それを使ったことのない人(あなた)のために華麗です!また、学習して使用するのが簡単なので、LLVMがどのくらいよく書かれているかを示します。 – MetallicPriest