2001-06-06 22:29:01 +02:00
|
|
|
//===-- Value.cpp - Implement the Value class -----------------------------===//
|
2003-10-20 21:43:21 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
2002-04-28 06:52:28 +02:00
|
|
|
// This file implements the Value and User classes.
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/InstrTypes.h"
|
|
|
|
#include "llvm/SymbolTable.h"
|
2002-01-25 04:45:27 +01:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-10-10 01:12:59 +02:00
|
|
|
#include "llvm/Constant.h"
|
2004-07-18 02:01:50 +02:00
|
|
|
#include "llvm/GlobalValue.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/LeakDetector.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include <algorithm>
|
2004-07-04 13:55:37 +02:00
|
|
|
#include <iostream>
|
2003-11-21 21:23:48 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2001-06-06 22:29:01 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Value Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2001-12-13 01:41:27 +01:00
|
|
|
static inline const Type *checkType(const Type *Ty) {
|
|
|
|
assert(Ty && "Value defined with a null type: Error!");
|
|
|
|
return Ty;
|
|
|
|
}
|
|
|
|
|
2004-06-26 22:33:39 +02:00
|
|
|
Value::Value(const Type *ty, unsigned scid, const std::string &name)
|
|
|
|
: SubclassID(scid), Ty(checkType(ty)), Name(name) {
|
2004-07-06 19:44:17 +02:00
|
|
|
if (!isa<Constant>(this) && !isa<BasicBlock>(this))
|
2004-07-07 20:07:46 +02:00
|
|
|
assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
|
|
|
|
isa<OpaqueType>(ty)) &&
|
2004-07-06 19:44:17 +02:00
|
|
|
"Cannot create non-first-class values except for constants!");
|
2004-07-25 08:07:15 +02:00
|
|
|
if (ty == Type::VoidTy)
|
2004-07-25 08:16:52 +02:00
|
|
|
assert(name.empty() && "Cannot have named void values!");
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Value::~Value() {
|
|
|
|
#ifndef NDEBUG // Only in -g mode...
|
2001-06-11 17:04:40 +02:00
|
|
|
// Check to make sure that there are no uses of this value that are still
|
|
|
|
// around when the value is destroyed. If there are, then we have a dangling
|
|
|
|
// reference and something is wrong. This code is here to print out what is
|
|
|
|
// still being referenced. The value in question should be printed as
|
|
|
|
// a <badref>
|
|
|
|
//
|
2001-06-06 22:29:01 +02:00
|
|
|
if (Uses.begin() != Uses.end()) {
|
2004-10-16 01:08:50 +02:00
|
|
|
std::cerr << "While deleting: " << *Ty << " %" << Name << "\n";
|
2003-10-16 18:53:07 +02:00
|
|
|
for (use_const_iterator I = Uses.begin(), E = Uses.end(); I != E; ++I)
|
2002-07-25 00:08:53 +02:00
|
|
|
std::cerr << "Use still stuck around after Def is destroyed:"
|
|
|
|
<< **I << "\n";
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
#endif
|
2003-06-25 00:20:19 +02:00
|
|
|
assert(Uses.begin() == Uses.end() &&"Uses remain when a value is destroyed!");
|
2002-09-08 20:59:35 +02:00
|
|
|
|
|
|
|
// There should be no uses of this object anymore, remove it.
|
|
|
|
LeakDetector::removeGarbageObject(this);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2002-10-10 01:12:59 +02:00
|
|
|
|
2003-08-29 07:09:37 +02:00
|
|
|
// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
|
|
|
|
// except that it doesn't have all of the asserts. The asserts fail because we
|
|
|
|
// are half-way done resolving types, which causes some types to exist as two
|
|
|
|
// different Type*'s at the same time. This is a sledgehammer to work around
|
|
|
|
// this problem.
|
|
|
|
//
|
|
|
|
void Value::uncheckedReplaceAllUsesWith(Value *New) {
|
|
|
|
while (!Uses.empty()) {
|
2003-10-16 18:53:07 +02:00
|
|
|
Use &U = Uses.back();
|
2003-08-29 07:09:37 +02:00
|
|
|
// Must handle Constants specially, we cannot call replaceUsesOfWith on a
|
|
|
|
// constant!
|
2003-10-16 18:53:07 +02:00
|
|
|
if (Constant *C = dyn_cast<Constant>(U.getUser())) {
|
2004-07-18 02:01:50 +02:00
|
|
|
if (!isa<GlobalValue>(C))
|
|
|
|
C->replaceUsesOfWithOnConstant(this, New, true);
|
|
|
|
else
|
|
|
|
U.set(New);
|
2003-08-29 07:09:37 +02:00
|
|
|
} else {
|
2003-10-16 18:53:07 +02:00
|
|
|
U.set(New);
|
2003-08-29 07:09:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-16 18:53:07 +02:00
|
|
|
void Value::replaceAllUsesWith(Value *New) {
|
|
|
|
assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
|
|
|
|
assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
|
|
|
|
assert(New->getType() == getType() &&
|
|
|
|
"replaceAllUses of value with new value of different type!");
|
2003-08-29 07:09:37 +02:00
|
|
|
|
2003-10-16 18:53:07 +02:00
|
|
|
uncheckedReplaceAllUsesWith(New);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// User Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// replaceUsesOfWith - Replaces all references to the "From" definition with
|
|
|
|
// references to the "To" definition.
|
|
|
|
//
|
|
|
|
void User::replaceUsesOfWith(Value *From, Value *To) {
|
|
|
|
if (From == To) return; // Duh what?
|
|
|
|
|
2004-07-18 02:01:50 +02:00
|
|
|
assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
|
2002-10-10 01:12:59 +02:00
|
|
|
"Cannot call User::replaceUsesofWith on a constant!");
|
|
|
|
|
2001-07-07 10:36:50 +02:00
|
|
|
for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
|
|
|
|
if (getOperand(i) == From) { // Is This operand is pointing to oldval?
|
2001-06-06 22:29:01 +02:00
|
|
|
// The side effects of this setOperand call include linking to
|
|
|
|
// "To", adding "this" to the uses list of To, and
|
|
|
|
// most importantly, removing "this" from the use list of "From".
|
2001-07-07 10:36:50 +02:00
|
|
|
setOperand(i, To); // Fix it now...
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
}
|
2004-07-18 02:01:50 +02:00
|
|
|
|