2002-04-28 19:55:58 +00:00
|
|
|
//===-- Constants.cpp - Implement Constant nodes -----------------*- C++ -*--=//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2001-12-03 22:26:30 +00:00
|
|
|
// This file implements the Constant* classes...
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define __STDC_LIMIT_MACROS // Get defs for INT64_MAX and friends...
|
2002-04-28 19:55:58 +00:00
|
|
|
#include "llvm/Constants.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-07-14 23:13:17 +00:00
|
|
|
#include "llvm/iMemory.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/SymbolTable.h"
|
2001-10-13 06:57:33 +00:00
|
|
|
#include "llvm/Module.h"
|
2002-04-07 22:49:37 +00:00
|
|
|
#include "llvm/SlotCalculator.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/StringExtras.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
using std::map;
|
|
|
|
using std::pair;
|
|
|
|
using std::make_pair;
|
2002-07-16 00:02:17 +00:00
|
|
|
using std::vector;
|
2002-01-20 22:54:45 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantBool *ConstantBool::True = new ConstantBool(true);
|
|
|
|
ConstantBool *ConstantBool::False = new ConstantBool(false);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-07-20 19:16:02 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-12-03 22:26:30 +00:00
|
|
|
// Constant Class
|
2001-06-06 20:29:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Specialize setName to take care of symbol table majik
|
2002-01-20 22:54:45 +00:00
|
|
|
void Constant::setName(const std::string &Name, SymbolTable *ST) {
|
2001-09-07 16:46:31 +00:00
|
|
|
assert(ST && "Type::setName - Must provide symbol table argument!");
|
|
|
|
|
|
|
|
if (Name.size()) ST->insert(Name, this);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Static constructor to create a '0' constant of arbitrary type...
|
2002-04-27 02:25:43 +00:00
|
|
|
Constant *Constant::getNullValue(const Type *Ty) {
|
2001-06-06 20:29:01 +00:00
|
|
|
switch (Ty->getPrimitiveID()) {
|
2001-12-03 22:26:30 +00:00
|
|
|
case Type::BoolTyID: return ConstantBool::get(false);
|
2001-06-06 20:29:01 +00:00
|
|
|
case Type::SByteTyID:
|
|
|
|
case Type::ShortTyID:
|
|
|
|
case Type::IntTyID:
|
2001-12-03 22:26:30 +00:00
|
|
|
case Type::LongTyID: return ConstantSInt::get(Ty, 0);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
case Type::UByteTyID:
|
|
|
|
case Type::UShortTyID:
|
|
|
|
case Type::UIntTyID:
|
2001-12-03 22:26:30 +00:00
|
|
|
case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
case Type::FloatTyID:
|
2001-12-03 22:26:30 +00:00
|
|
|
case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
|
2001-09-30 20:14:07 +00:00
|
|
|
|
|
|
|
case Type::PointerTyID:
|
2001-12-03 22:26:30 +00:00
|
|
|
return ConstantPointerNull::get(cast<PointerType>(Ty));
|
2001-06-06 20:29:01 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
void Constant::destroyConstantImpl() {
|
|
|
|
// When a Constant is destroyed, there may be lingering
|
2001-10-13 06:57:33 +00:00
|
|
|
// references to the constant by other constants in the constant pool. These
|
|
|
|
// constants are implicitly dependant on the module that is being deleted,
|
|
|
|
// but they don't know that. Because we only find out when the CPV is
|
|
|
|
// deleted, we must now notify all of our users (that should only be
|
2001-12-03 22:26:30 +00:00
|
|
|
// Constants) that they are, in fact, invalid now and should be deleted.
|
2001-10-13 06:57:33 +00:00
|
|
|
//
|
|
|
|
while (!use_empty()) {
|
|
|
|
Value *V = use_back();
|
|
|
|
#ifndef NDEBUG // Only in -g mode...
|
2002-07-18 00:14:50 +00:00
|
|
|
if (!isa<Constant>(V))
|
|
|
|
std::cerr << "While deleting: " << *this
|
|
|
|
<< "\n\nUse still stuck around after Def is destroyed: "
|
|
|
|
<< *V << "\n\n";
|
2001-10-13 06:57:33 +00:00
|
|
|
#endif
|
2002-07-14 23:13:17 +00:00
|
|
|
assert(isa<Constant>(V) && "References remain to Constant being destroyed");
|
2001-12-03 22:26:30 +00:00
|
|
|
Constant *CPV = cast<Constant>(V);
|
2001-10-13 06:57:33 +00:00
|
|
|
CPV->destroyConstant();
|
|
|
|
|
|
|
|
// The constant should remove itself from our use list...
|
2002-07-14 23:13:17 +00:00
|
|
|
assert((use_empty() || use_back() != V) && "Constant not removed!");
|
2001-10-13 06:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value has no outstanding references it is safe to delete it now...
|
|
|
|
delete this;
|
2001-10-01 20:11:19 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-12-03 22:26:30 +00:00
|
|
|
// ConstantXXX Classes
|
2001-06-06 20:29:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Normal Constructors
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantBool::ConstantBool(bool V) : Constant(Type::BoolTy) {
|
2001-06-06 20:29:01 +00:00
|
|
|
Val = V;
|
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : Constant(Ty) {
|
2001-09-07 16:46:31 +00:00
|
|
|
Val.Unsigned = V;
|
|
|
|
}
|
2001-07-20 19:16:02 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
|
2001-07-20 19:16:02 +00:00
|
|
|
assert(isValueValidForType(Ty, V) && "Value too large for type!");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
|
2001-07-20 19:16:02 +00:00
|
|
|
assert(isValueValidForType(Ty, V) && "Value too large for type!");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
|
2001-07-20 19:16:02 +00:00
|
|
|
assert(isValueValidForType(Ty, V) && "Value too large for type!");
|
2001-06-06 20:29:01 +00:00
|
|
|
Val = V;
|
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantArray::ConstantArray(const ArrayType *T,
|
2002-01-20 22:54:45 +00:00
|
|
|
const std::vector<Constant*> &V) : Constant(T) {
|
2001-06-06 20:29:01 +00:00
|
|
|
for (unsigned i = 0; i < V.size(); i++) {
|
|
|
|
assert(V[i]->getType() == T->getElementType());
|
2001-07-07 08:36:50 +00:00
|
|
|
Operands.push_back(Use(V[i], this));
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantStruct::ConstantStruct(const StructType *T,
|
2002-01-20 22:54:45 +00:00
|
|
|
const std::vector<Constant*> &V) : Constant(T) {
|
2001-06-06 20:29:01 +00:00
|
|
|
const StructType::ElementTypes &ETypes = T->getElementTypes();
|
2002-07-14 23:13:17 +00:00
|
|
|
assert(V.size() == ETypes.size() &&
|
|
|
|
"Invalid initializer vector for constant structure");
|
2001-06-06 20:29:01 +00:00
|
|
|
for (unsigned i = 0; i < V.size(); i++) {
|
|
|
|
assert(V[i]->getType() == ETypes[i]);
|
2001-07-07 08:36:50 +00:00
|
|
|
Operands.push_back(Use(V[i], this));
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
|
|
|
|
: ConstantPointer(GV->getType()) {
|
2001-10-03 06:12:09 +00:00
|
|
|
Operands.push_back(Use(GV, this));
|
|
|
|
}
|
|
|
|
|
2002-07-14 23:13:17 +00:00
|
|
|
ConstantExpr::ConstantExpr(unsigned opCode, Constant *C, const Type *Ty)
|
|
|
|
: Constant(Ty), iType(opCode) {
|
|
|
|
Operands.push_back(Use(C, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantExpr::ConstantExpr(unsigned opCode, Constant* C1,
|
|
|
|
Constant* C2, const Type *Ty)
|
|
|
|
: Constant(Ty), iType(opCode) {
|
|
|
|
Operands.push_back(Use(C1, this));
|
|
|
|
Operands.push_back(Use(C2, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantExpr::ConstantExpr(unsigned opCode, Constant* C,
|
2002-07-18 00:14:50 +00:00
|
|
|
const std::vector<Constant*> &IdxList, const Type *Ty)
|
2002-07-14 23:13:17 +00:00
|
|
|
: Constant(Ty), iType(opCode) {
|
|
|
|
Operands.reserve(1+IdxList.size());
|
|
|
|
Operands.push_back(Use(C, this));
|
|
|
|
for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
|
|
|
|
Operands.push_back(Use(IdxList[i], this));
|
|
|
|
}
|
|
|
|
|
2001-10-03 06:12:09 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-10-13 06:57:33 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// classof implementations
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
bool ConstantInt::classof(const Constant *CPV) {
|
2002-07-18 00:14:50 +00:00
|
|
|
return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
|
2001-10-13 06:57:33 +00:00
|
|
|
}
|
2001-12-03 22:26:30 +00:00
|
|
|
bool ConstantSInt::classof(const Constant *CPV) {
|
2002-07-18 00:14:50 +00:00
|
|
|
return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
|
2001-10-13 06:57:33 +00:00
|
|
|
}
|
2001-12-03 22:26:30 +00:00
|
|
|
bool ConstantUInt::classof(const Constant *CPV) {
|
2002-07-18 00:14:50 +00:00
|
|
|
return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
|
2001-10-13 06:57:33 +00:00
|
|
|
}
|
2001-12-03 22:26:30 +00:00
|
|
|
bool ConstantFP::classof(const Constant *CPV) {
|
2001-10-13 06:57:33 +00:00
|
|
|
const Type *Ty = CPV->getType();
|
2002-07-14 23:13:17 +00:00
|
|
|
return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
|
2002-07-18 00:14:50 +00:00
|
|
|
!isa<ConstantExpr>(CPV));
|
2001-10-13 06:57:33 +00:00
|
|
|
}
|
2001-12-03 22:26:30 +00:00
|
|
|
bool ConstantArray::classof(const Constant *CPV) {
|
2002-07-18 00:14:50 +00:00
|
|
|
return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
|
2001-10-13 06:57:33 +00:00
|
|
|
}
|
2001-12-03 22:26:30 +00:00
|
|
|
bool ConstantStruct::classof(const Constant *CPV) {
|
2002-07-18 00:14:50 +00:00
|
|
|
return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
|
2001-10-13 06:57:33 +00:00
|
|
|
}
|
2001-12-03 22:26:30 +00:00
|
|
|
bool ConstantPointer::classof(const Constant *CPV) {
|
2002-07-18 00:14:50 +00:00
|
|
|
return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
|
2001-10-13 06:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-14 23:13:17 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// isValueValidForType implementations
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
|
2001-06-06 20:29:01 +00:00
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
default:
|
|
|
|
return false; // These can't be represented as integers!!!
|
|
|
|
|
|
|
|
// Signed types...
|
|
|
|
case Type::SByteTyID:
|
|
|
|
return (Val <= INT8_MAX && Val >= INT8_MIN);
|
|
|
|
case Type::ShortTyID:
|
|
|
|
return (Val <= INT16_MAX && Val >= INT16_MIN);
|
|
|
|
case Type::IntTyID:
|
|
|
|
return (Val <= INT32_MAX && Val >= INT32_MIN);
|
|
|
|
case Type::LongTyID:
|
|
|
|
return true; // This is the largest type...
|
|
|
|
}
|
|
|
|
assert(0 && "WTF?");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
|
2001-06-06 20:29:01 +00:00
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
default:
|
|
|
|
return false; // These can't be represented as integers!!!
|
|
|
|
|
|
|
|
// Unsigned types...
|
|
|
|
case Type::UByteTyID:
|
|
|
|
return (Val <= UINT8_MAX);
|
|
|
|
case Type::UShortTyID:
|
|
|
|
return (Val <= UINT16_MAX);
|
|
|
|
case Type::UIntTyID:
|
|
|
|
return (Val <= UINT32_MAX);
|
|
|
|
case Type::ULongTyID:
|
|
|
|
return true; // This is the largest type...
|
|
|
|
}
|
|
|
|
assert(0 && "WTF?");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
|
2001-06-06 20:29:01 +00:00
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
default:
|
|
|
|
return false; // These can't be represented as floating point!
|
|
|
|
|
|
|
|
// TODO: Figure out how to test if a double can be cast to a float!
|
|
|
|
case Type::FloatTyID:
|
2001-07-15 00:18:39 +00:00
|
|
|
/*
|
2001-06-06 20:29:01 +00:00
|
|
|
return (Val <= UINT8_MAX);
|
|
|
|
*/
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
return true; // This is the largest type...
|
|
|
|
}
|
|
|
|
};
|
2001-07-20 19:16:02 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-09-07 16:46:31 +00:00
|
|
|
// Factory Function Implementation
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
template<class ValType, class ConstantClass>
|
2001-09-07 16:46:31 +00:00
|
|
|
struct ValueMap {
|
|
|
|
typedef pair<const Type*, ValType> ConstHashKey;
|
2001-12-03 22:26:30 +00:00
|
|
|
map<ConstHashKey, ConstantClass *> Map;
|
2001-09-07 16:46:31 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
inline ConstantClass *get(const Type *Ty, ValType V) {
|
2002-07-24 22:08:53 +00:00
|
|
|
typename map<ConstHashKey,ConstantClass *>::iterator I =
|
2001-09-07 16:46:31 +00:00
|
|
|
Map.find(ConstHashKey(Ty, V));
|
|
|
|
return (I != Map.end()) ? I->second : 0;
|
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
|
2001-09-07 16:46:31 +00:00
|
|
|
Map.insert(make_pair(ConstHashKey(Ty, V), CP));
|
|
|
|
}
|
2001-10-13 06:57:33 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
inline void remove(ConstantClass *CP) {
|
2002-07-24 22:08:53 +00:00
|
|
|
for (typename map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
|
2001-10-13 06:57:33 +00:00
|
|
|
E = Map.end(); I != E;++I)
|
|
|
|
if (I->second == CP) {
|
|
|
|
Map.erase(I);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2001-09-07 16:46:31 +00:00
|
|
|
};
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
//---- ConstantUInt::get() and ConstantSInt::get() implementations...
|
2001-09-07 16:46:31 +00:00
|
|
|
//
|
2001-12-03 22:26:30 +00:00
|
|
|
static ValueMap<uint64_t, ConstantInt> IntConstants;
|
2001-09-07 16:46:31 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
|
|
|
|
ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
|
2001-09-07 16:46:31 +00:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-03 22:26:30 +00:00
|
|
|
IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
|
2001-09-07 16:46:31 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
|
|
|
|
ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
|
2001-09-07 16:46:31 +00:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-03 22:26:30 +00:00
|
|
|
IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
|
2001-09-07 16:46:31 +00:00
|
|
|
return Result;
|
|
|
|
}
|
2001-07-20 19:16:02 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
|
2001-09-07 16:46:31 +00:00
|
|
|
assert(V <= 127 && "Can only be used with very small positive constants!");
|
2001-12-03 22:26:30 +00:00
|
|
|
if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
|
|
|
|
return ConstantUInt::get(Ty, V);
|
2001-09-07 16:46:31 +00:00
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
//---- ConstantFP::get() implementation...
|
2001-09-07 16:46:31 +00:00
|
|
|
//
|
2001-12-03 22:26:30 +00:00
|
|
|
static ValueMap<double, ConstantFP> FPConstants;
|
2001-09-07 16:46:31 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantFP *ConstantFP::get(const Type *Ty, double V) {
|
|
|
|
ConstantFP *Result = FPConstants.get(Ty, V);
|
2001-09-07 16:46:31 +00:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-03 22:26:30 +00:00
|
|
|
FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
|
2001-09-07 16:46:31 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
//---- ConstantArray::get() implementation...
|
2001-09-07 16:46:31 +00:00
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
|
2001-09-07 16:46:31 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantArray *ConstantArray::get(const ArrayType *Ty,
|
2002-01-20 22:54:45 +00:00
|
|
|
const std::vector<Constant*> &V) {
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantArray *Result = ArrayConstants.get(Ty, V);
|
2001-09-07 16:46:31 +00:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-03 22:26:30 +00:00
|
|
|
ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
|
2001-09-07 16:46:31 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
// ConstantArray::get(const string&) - Return an array that is initialized to
|
2001-10-14 23:54:12 +00:00
|
|
|
// contain the specified string. A null terminator is added to the specified
|
|
|
|
// string so that it may be used in a natural way...
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
ConstantArray *ConstantArray::get(const std::string &Str) {
|
|
|
|
std::vector<Constant*> ElementVals;
|
2001-10-14 23:54:12 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < Str.length(); ++i)
|
2001-12-14 16:41:18 +00:00
|
|
|
ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
|
2001-10-14 23:54:12 +00:00
|
|
|
|
|
|
|
// Add a null terminator to the string...
|
2001-12-14 16:41:18 +00:00
|
|
|
ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
|
2001-10-14 23:54:12 +00:00
|
|
|
|
2001-12-14 16:41:18 +00:00
|
|
|
ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
|
2001-12-03 22:26:30 +00:00
|
|
|
return ConstantArray::get(ATy, ElementVals);
|
2001-10-14 23:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-13 06:57:33 +00:00
|
|
|
// destroyConstant - Remove the constant from the constant table...
|
|
|
|
//
|
2001-12-03 22:26:30 +00:00
|
|
|
void ConstantArray::destroyConstant() {
|
2001-10-13 06:57:33 +00:00
|
|
|
ArrayConstants.remove(this);
|
|
|
|
destroyConstantImpl();
|
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
//---- ConstantStruct::get() implementation...
|
2001-09-07 16:46:31 +00:00
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
|
2001-09-07 16:46:31 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantStruct *ConstantStruct::get(const StructType *Ty,
|
2002-01-20 22:54:45 +00:00
|
|
|
const std::vector<Constant*> &V) {
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantStruct *Result = StructConstants.get(Ty, V);
|
2001-09-07 16:46:31 +00:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-03 22:26:30 +00:00
|
|
|
StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
|
2001-09-07 16:46:31 +00:00
|
|
|
return Result;
|
|
|
|
}
|
2001-10-03 15:39:36 +00:00
|
|
|
|
2001-10-13 06:57:33 +00:00
|
|
|
// destroyConstant - Remove the constant from the constant table...
|
|
|
|
//
|
2001-12-03 22:26:30 +00:00
|
|
|
void ConstantStruct::destroyConstant() {
|
2001-10-13 06:57:33 +00:00
|
|
|
StructConstants.remove(this);
|
|
|
|
destroyConstantImpl();
|
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
//---- ConstantPointerNull::get() implementation...
|
2001-10-03 15:39:36 +00:00
|
|
|
//
|
2001-12-03 22:26:30 +00:00
|
|
|
static ValueMap<char, ConstantPointerNull> NullPtrConstants;
|
2001-10-03 15:39:36 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
|
|
|
|
ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
|
2001-10-03 15:39:36 +00:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-03 22:26:30 +00:00
|
|
|
NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
|
2001-10-03 15:39:36 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
//---- ConstantPointerRef::get() implementation...
|
2001-10-03 19:28:15 +00:00
|
|
|
//
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
|
2001-10-13 06:57:33 +00:00
|
|
|
assert(GV->getParent() && "Global Value must be attached to a module!");
|
2002-07-14 23:13:17 +00:00
|
|
|
|
2001-10-13 06:57:33 +00:00
|
|
|
// The Module handles the pointer reference sharing...
|
2001-12-03 22:26:30 +00:00
|
|
|
return GV->getParent()->getConstantPointerRef(GV);
|
2001-10-13 06:57:33 +00:00
|
|
|
}
|
|
|
|
|
2002-07-14 23:13:17 +00:00
|
|
|
//---- ConstantExpr::get() implementations...
|
|
|
|
// Return NULL on invalid expressions.
|
|
|
|
//
|
2002-07-15 18:19:33 +00:00
|
|
|
typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
|
|
|
|
static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
|
|
|
|
|
2002-07-18 00:14:50 +00:00
|
|
|
ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C, const Type *Ty) {
|
2002-07-15 18:19:33 +00:00
|
|
|
|
|
|
|
// Look up the constant in the table first to ensure uniqueness
|
|
|
|
vector<Constant*> argVec(1, C);
|
2002-07-18 00:14:50 +00:00
|
|
|
const ExprMapKeyType &Key = make_pair(Opcode, argVec);
|
|
|
|
ConstantExpr *Result = ExprConstants.get(Ty, Key);
|
|
|
|
if (Result) return Result;
|
2002-07-15 18:19:33 +00:00
|
|
|
|
|
|
|
// Its not in the table so create a new one and put it in the table.
|
|
|
|
// Check the operands for consistency first
|
2002-07-18 00:14:50 +00:00
|
|
|
assert(Opcode == Instruction::Cast ||
|
|
|
|
(Opcode >= Instruction::FirstUnaryOp &&
|
|
|
|
Opcode < Instruction::NumUnaryOps) &&
|
|
|
|
"Invalid opcode in unary ConstantExpr!");
|
|
|
|
|
2002-07-14 23:13:17 +00:00
|
|
|
// type of operand will not match result for Cast operation
|
2002-07-18 00:14:50 +00:00
|
|
|
assert((Opcode == Instruction::Cast || Ty == C->getType()) &&
|
|
|
|
"Type of operand in unary constant expression should match result");
|
2002-07-15 18:19:33 +00:00
|
|
|
|
2002-07-18 00:14:50 +00:00
|
|
|
Result = new ConstantExpr(Opcode, C, Ty);
|
|
|
|
ExprConstants.add(Ty, Key, Result);
|
|
|
|
return Result;
|
2002-07-14 23:13:17 +00:00
|
|
|
}
|
|
|
|
|
2002-07-18 00:14:50 +00:00
|
|
|
ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
|
|
|
|
const Type *Ty) {
|
2002-07-15 18:19:33 +00:00
|
|
|
|
|
|
|
// Look up the constant in the table first to ensure uniqueness
|
|
|
|
vector<Constant*> argVec(1, C1); argVec.push_back(C2);
|
2002-07-18 00:14:50 +00:00
|
|
|
const ExprMapKeyType &Key = make_pair(Opcode, argVec);
|
|
|
|
ConstantExpr *Result = ExprConstants.get(Ty, Key);
|
|
|
|
if (Result) return Result;
|
2002-07-15 18:19:33 +00:00
|
|
|
|
|
|
|
// Its not in the table so create a new one and put it in the table.
|
|
|
|
// Check the operands for consistency first
|
2002-07-18 00:14:50 +00:00
|
|
|
assert((Opcode >= Instruction::FirstBinaryOp &&
|
|
|
|
Opcode < Instruction::NumBinaryOps) &&
|
|
|
|
"Invalid opcode in binary constant expression");
|
|
|
|
|
|
|
|
assert(Ty == C1->getType() && Ty == C2->getType() &&
|
|
|
|
"Operand types in binary constant expression should match result");
|
2002-07-15 18:19:33 +00:00
|
|
|
|
2002-07-18 00:14:50 +00:00
|
|
|
Result = new ConstantExpr(Opcode, C1, C2, Ty);
|
|
|
|
ExprConstants.add(Ty, Key, Result);
|
|
|
|
return Result;
|
2002-07-14 23:13:17 +00:00
|
|
|
}
|
2001-10-13 06:57:33 +00:00
|
|
|
|
2002-07-18 00:14:50 +00:00
|
|
|
ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C,
|
|
|
|
const std::vector<Constant*> &IdxList,
|
|
|
|
const Type *Ty) {
|
2002-07-15 18:19:33 +00:00
|
|
|
|
|
|
|
// Look up the constant in the table first to ensure uniqueness
|
|
|
|
vector<Constant*> argVec(1, C);
|
2002-07-18 00:14:50 +00:00
|
|
|
argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
|
2002-07-15 18:19:33 +00:00
|
|
|
|
2002-07-18 00:14:50 +00:00
|
|
|
const ExprMapKeyType &Key = make_pair(Opcode, argVec);
|
|
|
|
ConstantExpr *Result = ExprConstants.get(Ty, Key);
|
|
|
|
if (Result) return Result;
|
2002-07-15 18:19:33 +00:00
|
|
|
|
|
|
|
// Its not in the table so create a new one and put it in the table.
|
|
|
|
// Check the operands for consistency first
|
2002-07-14 23:13:17 +00:00
|
|
|
// Must be a getElementPtr. Check for valid getElementPtr expression.
|
|
|
|
//
|
2002-07-18 00:14:50 +00:00
|
|
|
assert(Opcode == Instruction::GetElementPtr &&
|
|
|
|
"Operator other than GetElementPtr used with an index list");
|
|
|
|
|
|
|
|
assert(isa<PointerType>(Ty) &&
|
|
|
|
"Non-pointer type for constant GelElementPtr expression");
|
|
|
|
|
|
|
|
std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
|
|
|
|
const Type *fldType = GetElementPtrInst::getIndexedType(C->getType(),
|
|
|
|
ValIdxList, true);
|
|
|
|
assert(fldType && "Invalid index list for constant GelElementPtr expression");
|
|
|
|
|
|
|
|
assert(cast<PointerType>(Ty)->getElementType() == fldType &&
|
|
|
|
"Type for constant GelElementPtr expression doesn't match field type");
|
2002-07-14 23:13:17 +00:00
|
|
|
|
2002-07-18 00:14:50 +00:00
|
|
|
Result = new ConstantExpr(Opcode, C, IdxList, Ty);
|
|
|
|
ExprConstants.add(Ty, Key, Result);
|
|
|
|
return Result;
|
2002-07-15 18:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// destroyConstant - Remove the constant from the constant table...
|
|
|
|
//
|
|
|
|
void ConstantExpr::destroyConstant() {
|
|
|
|
ExprConstants.remove(this);
|
|
|
|
destroyConstantImpl();
|
2002-07-14 23:13:17 +00:00
|
|
|
}
|
|
|
|
|
2002-07-18 00:14:50 +00:00
|
|
|
const char *ConstantExpr::getOpcodeName(unsigned Opcode) {
|
|
|
|
return Instruction::getOpcodeName(Opcode);
|
2002-07-14 23:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---- ConstantPointerRef::mutateReferences() implementation...
|
|
|
|
//
|
2002-07-18 00:14:50 +00:00
|
|
|
unsigned ConstantPointerRef::mutateReferences(Value *OldV, Value *NewV) {
|
2002-07-14 23:13:17 +00:00
|
|
|
assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!");
|
2002-07-18 00:14:50 +00:00
|
|
|
GlobalValue *NewGV = cast<GlobalValue>(NewV);
|
2001-12-03 22:26:30 +00:00
|
|
|
getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
|
2001-10-13 06:57:33 +00:00
|
|
|
Operands[0] = NewGV;
|
2002-07-14 23:13:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---- ConstantPointerExpr::mutateReferences() implementation...
|
|
|
|
//
|
2002-07-18 00:14:50 +00:00
|
|
|
unsigned ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
|
|
|
|
unsigned NumReplaced = 0;
|
|
|
|
Constant *NewC = cast<Constant>(NewV);
|
|
|
|
for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
|
2002-07-14 23:13:17 +00:00
|
|
|
if (Operands[i] == OldV) {
|
2002-07-18 00:14:50 +00:00
|
|
|
++NumReplaced;
|
2002-07-14 23:13:17 +00:00
|
|
|
Operands[i] = NewC;
|
|
|
|
}
|
2002-07-18 00:14:50 +00:00
|
|
|
return NumReplaced;
|
2001-10-03 19:28:15 +00:00
|
|
|
}
|