2012-02-08 14 views

Respuesta

12

ConstantInt es una fábrica, ¿no? Clase tiene la get method para construir nueva constante:

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

Así que, creo, no se puede modificar ConstantInt existente. Si desea modificar el IR, intente cambiar el puntero a argumento (cambie el IR, pero no el objeto constante).

Puede ser que desee algo así (recuerde, no tengo experiencia con LLVM, y estoy casi seguro de que el ejemplo es incorrecto).

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

"modificar" una constante solo hay una solución (incremento y 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, tiene Constant.h comentario importante en la mendicidad sobre la creación y no de la eliminación Constantes 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

Su solución se ve bien, lo intentaré :). – MetallicPriest

+0

Espero, @Anton Korobeynikov, responder o comentar mi código. También debes saber que setOperand no puede cambiar algo que es una constante. – osgx

+0

¡Funcionó! ¡Brillante para una persona (usted) que nunca lo usó! También muestra lo bien escrito que es LLVM, ¡ya que es muy fácil de aprender y usar! – MetallicPriest

Cuestiones relacionadas