//===-- Constants.cpp - Implement Constant nodes --------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the Constant* classes...
//
//===----------------------------------------------------------------------===//
#include "llvm/Constants.h"
#include "ConstantFolding.h"
#include "llvm/DerivedTypes.h"
#include "llvm/GlobalValue.h"
#include "llvm/iMemory.h"
#include "llvm/SymbolTable.h"
#include "llvm/Module.h"
#include "Support/StringExtras.h"
#include <algorithm>
#include <iostream>
using namespace llvm;
ConstantBool *ConstantBool::True = new ConstantBool(true);
ConstantBool *ConstantBool::False = new ConstantBool(false);
//===----------------------------------------------------------------------===//
// Constant Class
//===----------------------------------------------------------------------===//
// Specialize setName to take care of symbol table majik
void Constant::setName(const std::string &Name, SymbolTable *ST) {
assert(ST && "Type::setName - Must provide symbol table argument!");
if (Name.size()) ST->insert(Name, this);
}
void Constant::destroyConstantImpl() {
// When a Constant is destroyed, there may be lingering
// references to the constant by other constants in the constant pool. These
// constants are implicitly dependent 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
// Constants) that they are, in fact, invalid now and should be deleted.
//
while (!use_empty()) {
Value *V = use_back();
#ifndef NDEBUG // Only in -g mode...
if (!isa<Constant>(V))
std::cerr << "While deleting: " << *this
<< "\n\nUse still stuck around after Def is destroyed: "
<< *V << "\n\n";
#endif
assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Constant *CV = cast<Constant>(V);
CV->destroyConstant();
// The constant should remove itself from our use list...
assert((use_empty() || use_back() != V) && "Constant not removed!");
}
// Value has no outstanding references it is safe to delete it now...
delete this;
}
// Static constructor to create a '0' constant of arbitrary type...
Constant *Constant::getNullValue(const Type *Ty) {
switch (Ty->getTypeID()) {
case Type::BoolTyID: {
static Constant *NullBool = ConstantBool::get(false);
return NullBool;
}
case Type::SByteTyID: {
static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
return NullSByte;
}
case Type::UByteTyID: {
static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
return NullUByte;
}
case Type::ShortTyID: {
static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
return NullShort;
}
case Type::UShortTyID: {