diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index 1674e0a3a6f..07a82edbd7a 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -11,11 +11,9 @@ #include "llvm/Constant.h" #include "Support/DataTypes.h" - class ArrayType; class StructType; class PointerType; -class ConstantExpr; //===--------------------------------------------------------------------------- // ConstantBool - Boolean Values @@ -340,25 +338,29 @@ public: // Use the appropriate Constant subclass above for known constants. // class ConstantExpr : public Constant { - unsigned iType; // operation type + unsigned iType; // Operation type protected: - ConstantExpr(unsigned opCode, Constant *C, const Type *Ty); - ConstantExpr(unsigned opCode, Constant* C1, Constant* C2, const Type *Ty); - ConstantExpr(unsigned opCode, Constant* C, - const std::vector &IdxList, const Type *Ty); + ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty); + ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2); + ConstantExpr(Constant *C, const std::vector &IdxList, + const Type *DestTy); ~ConstantExpr() {} virtual void destroyConstant(); public: // Static methods to construct a ConstantExpr of different kinds. + + // Unary constant expr - Use with unary operators and casts static ConstantExpr *get(unsigned Opcode, Constant *C, const Type *Ty); - static ConstantExpr *get(unsigned Opcode, - Constant *C1, Constant *C2, const Type *Ty); - static ConstantExpr *get(unsigned Opcode, Constant *C, - const std::vector &IdxList, - const Type *Ty); + + // Binary constant expr - Use with binary operators... + static ConstantExpr *get(unsigned Opcode, Constant *C1, Constant *C2); + + // Getelementptr form... + static ConstantExpr *getGetElementPtr(Constant *C, + const std::vector &IdxList); // isNullValue - Return true if this is the value that would be returned by // getNullValue. @@ -368,10 +370,7 @@ public: unsigned getOpcode() const { return iType; } // getOpcodeName - Return a string representation for an opcode. - static const char *getOpcodeName(unsigned opCode); - const char *getOpcodeName() const { - return getOpcodeName(getOpcode()); - } + const char *getOpcodeName() const; // isConstantExpr - Return true if this is a ConstantExpr virtual bool isConstantExpr() const { return true; } diff --git a/lib/Bytecode/Reader/ConstantReader.cpp b/lib/Bytecode/Reader/ConstantReader.cpp index 6e86997f224..921b875644d 100644 --- a/lib/Bytecode/Reader/ConstantReader.cpp +++ b/lib/Bytecode/Reader/ConstantReader.cpp @@ -186,43 +186,43 @@ bool BytecodeParser::parseConstantValue(const uchar *&Buf, const uchar *EndBuf, unsigned isExprNumArgs; // 0 if not expr; numArgs if is expr if (read_vbr(Buf, EndBuf, isExprNumArgs)) return failure(true); if (isExprNumArgs) { - unsigned opCode; - std::vector argVec; - argVec.reserve(isExprNumArgs); - - if (read_vbr(Buf, EndBuf, opCode)) return failure(true); - + // FIXME: Encoding of constant exprs could be much more compact! + unsigned Opcode; + std::vector ArgVec; + ArgVec.reserve(isExprNumArgs); + if (read_vbr(Buf, EndBuf, Opcode)) return failure(true); + // Read the slot number and types of each of the arguments - for (unsigned i=0; i < isExprNumArgs; ++i) { - unsigned argValSlot, argTypeSlot; - if (read_vbr(Buf, EndBuf, argValSlot)) return failure(true); - if (read_vbr(Buf, EndBuf, argTypeSlot)) return failure(true); - const Type *argTy = getType(argTypeSlot); - if (argTy == 0) return failure(true); + for (unsigned i = 0; i != isExprNumArgs; ++i) { + unsigned ArgValSlot, ArgTypeSlot; + if (read_vbr(Buf, EndBuf, ArgValSlot)) return failure(true); + if (read_vbr(Buf, EndBuf, ArgTypeSlot)) return failure(true); + const Type *ArgTy = getType(ArgTypeSlot); + if (ArgTy == 0) return failure(true); - BCR_TRACE(4, "CE Arg " << i << ": Type: '" << argTy << "' slot: " - << argValSlot << "\n"); + BCR_TRACE(4, "CE Arg " << i << ": Type: '" << ArgTy << "' slot: " + << ArgValSlot << "\n"); // Get the arg value from its slot if it exists, otherwise a placeholder - Value *Val = getValue(argTy, argValSlot, false); + Value *Val = getValue(ArgTy, ArgValSlot, false); Constant *C; if (Val) { if (!(C = dyn_cast(Val))) return failure(true); BCR_TRACE(5, "Constant Found in ValueTable!\n"); } else { // Nope... find or create a forward ref. for it - C = fwdRefs.GetFwdRefToConstant(argTy, argValSlot); + C = fwdRefs.GetFwdRefToConstant(ArgTy, ArgValSlot); } - argVec.push_back(C); + ArgVec.push_back(C); } // Construct a ConstantExpr of the appropriate kind if (isExprNumArgs == 1) { // All one-operand expressions - V = ConstantExpr::get(opCode, argVec[0], Ty); - } else if (opCode == Instruction::GetElementPtr) { // GetElementPtr - std::vector IdxList(argVec.begin()+1, argVec.end()); - V = ConstantExpr::get(opCode, argVec[0], IdxList, Ty); + V = ConstantExpr::get(Opcode, ArgVec[0], Ty); + } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr + std::vector IdxList(ArgVec.begin()+1, ArgVec.end()); + V = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList); } else { // All other 2-operand expressions - V = ConstantExpr::get(opCode, argVec[0], argVec[1], Ty); + V = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]); } return false; } diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index d8f67ff1aad..48e40d0b3d9 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -11,15 +11,10 @@ #include "llvm/Transforms/Utils/Linker.h" #include "llvm/Module.h" -#include "llvm/Function.h" -#include "llvm/BasicBlock.h" -#include "llvm/GlobalVariable.h" #include "llvm/SymbolTable.h" #include "llvm/DerivedTypes.h" #include "llvm/iOther.h" #include "llvm/Constants.h" -#include "llvm/Argument.h" -#include using std::cerr; using std::string; using std::map; @@ -134,7 +129,7 @@ static Value *RemapOperand(const Value *In, map &LocalMap, Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap); Result = ConstantExpr::get(CE->getOpcode(), cast(V1), - cast(V2), CE->getType()); + cast(V2)); } else { // GetElementPtr Expression assert(CE->getOpcode() == Instruction::GetElementPtr); @@ -145,8 +140,7 @@ static Value *RemapOperand(const Value *In, map &LocalMap, Indices.push_back(cast(RemapOperand(CE->getOperand(i), LocalMap, GlobalMap))); - Result = ConstantExpr::get(CE->getOpcode(), cast(Ptr), - Indices, CE->getType()); + Result = ConstantExpr::getGetElementPtr(cast(Ptr), Indices); } } else { diff --git a/lib/Transforms/Utils/Linker.cpp b/lib/Transforms/Utils/Linker.cpp index d8f67ff1aad..48e40d0b3d9 100644 --- a/lib/Transforms/Utils/Linker.cpp +++ b/lib/Transforms/Utils/Linker.cpp @@ -11,15 +11,10 @@ #include "llvm/Transforms/Utils/Linker.h" #include "llvm/Module.h" -#include "llvm/Function.h" -#include "llvm/BasicBlock.h" -#include "llvm/GlobalVariable.h" #include "llvm/SymbolTable.h" #include "llvm/DerivedTypes.h" #include "llvm/iOther.h" #include "llvm/Constants.h" -#include "llvm/Argument.h" -#include using std::cerr; using std::string; using std::map; @@ -134,7 +129,7 @@ static Value *RemapOperand(const Value *In, map &LocalMap, Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap); Result = ConstantExpr::get(CE->getOpcode(), cast(V1), - cast(V2), CE->getType()); + cast(V2)); } else { // GetElementPtr Expression assert(CE->getOpcode() == Instruction::GetElementPtr); @@ -145,8 +140,7 @@ static Value *RemapOperand(const Value *In, map &LocalMap, Indices.push_back(cast(RemapOperand(CE->getOperand(i), LocalMap, GlobalMap))); - Result = ConstantExpr::get(CE->getOpcode(), cast(Ptr), - Indices, CE->getType()); + Result = ConstantExpr::getGetElementPtr(cast(Ptr), Indices); } } else { diff --git a/lib/VMCore/Linker.cpp b/lib/VMCore/Linker.cpp index d8f67ff1aad..48e40d0b3d9 100644 --- a/lib/VMCore/Linker.cpp +++ b/lib/VMCore/Linker.cpp @@ -11,15 +11,10 @@ #include "llvm/Transforms/Utils/Linker.h" #include "llvm/Module.h" -#include "llvm/Function.h" -#include "llvm/BasicBlock.h" -#include "llvm/GlobalVariable.h" #include "llvm/SymbolTable.h" #include "llvm/DerivedTypes.h" #include "llvm/iOther.h" #include "llvm/Constants.h" -#include "llvm/Argument.h" -#include using std::cerr; using std::string; using std::map; @@ -134,7 +129,7 @@ static Value *RemapOperand(const Value *In, map &LocalMap, Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap); Result = ConstantExpr::get(CE->getOpcode(), cast(V1), - cast(V2), CE->getType()); + cast(V2)); } else { // GetElementPtr Expression assert(CE->getOpcode() == Instruction::GetElementPtr); @@ -145,8 +140,7 @@ static Value *RemapOperand(const Value *In, map &LocalMap, Indices.push_back(cast(RemapOperand(CE->getOperand(i), LocalMap, GlobalMap))); - Result = ConstantExpr::get(CE->getOpcode(), cast(Ptr), - Indices, CE->getType()); + Result = ConstantExpr::getGetElementPtr(cast(Ptr), Indices); } } else {