2012-02-08 11 views

Odpowiedz

12

ConstantInt to fabryka, prawda? Klasa ma get method do budowania nowej stałej:

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

Tak, myślę, że nie można zmodyfikować istniejący ConstantInt. Jeśli chcesz zmodyfikować IR, powinieneś spróbować zmienić wskaźnik na argument (zmień sam IR, ale nie stały obiekt).

Może chcesz czegoś takiego (pamiętaj, mam zerowe doświadczenie z LLVM i jestem prawie pewien, że przykład jest niepoprawny).

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

do „modyfikować” stała sam jest rozwiązanie (zwiększania i zmniejszania 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 ma istotne Komentarz w prosząc o tworzeniu i braku deletowaniem Stałe 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

Twoje rozwiązanie wygląda dobrze, spróbuję :). – MetallicPriest

+0

Mam nadzieję, że @Anton Korobeynikov, odpowie lub skomentuje mój kod. Powinieneś także wiedzieć, że setOperand nie może zmienić czegoś, co jest stałą samą w sobie. – osgx

+0

To zadziałało! Wspaniałe dla osoby (ciebie), która nigdy jej nie użyła! Pokazuje również, jak dobrze napisane jest LLVM, ponieważ jest tak łatwy do nauczenia się i używania! – MetallicPriest

Powiązane problemy