Sto giocando con LLVM. Ho pensato di cambiare il valore di una costante nel codice intermedio. Tuttavia, per la classe llvm::ConstantInt, non vedo una funzione setvalue. Qualche idea su come posso modificare il valore di una costante nel codice IR?Valore impostato per llvm :: ConstantInt
6
A
risposta
12
ConstantInt è una fabbrica, non è vero? Classe ha la get method per costruire nuova costante:
/* ... return a ConstantInt for the given value. */
00069 static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
Quindi, penso, non è possibile modificare ConstantInt esistente. Se vuoi modificare IR, dovresti provare a cambiare il puntatore in argomento (cambia l'IR stesso, ma non l'oggetto costante).
Potrebbe essere che desideri qualcosa di simile (ricorda, non ho esperienza con LLVM e sono quasi sicuro che l'esempio non è corretto).
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);
}
Per "modificare" una costante sola c'è una soluzione (incremento e decremento 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 ha importanti commentare nel accattonaggio sulla creazione e non di cancellazione Costanti 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
La soluzione sembra buona, lo proverò :). – MetallicPriest
Spero che @Anton Korobeynikov risponda o commenta il mio codice. Inoltre dovresti sapere, che setOperand non può cambiare qualcosa che è una costante stessa. – osgx
Ha funzionato! Brillante per una persona (tu) che non l'ha mai usata! Verrà anche mostrato quanto bene sia scritto LLVM, poiché è così facile da imparare e usare! – MetallicPriest