LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Value.cpp

Go to the documentation of this file.
00001 //===-- Value.cpp - Implement the Value class -----------------------------===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the Value and User classes. 
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/InstrTypes.h"
00015 #include "llvm/SymbolTable.h"
00016 #include "llvm/DerivedTypes.h"
00017 #include "llvm/Constant.h"
00018 #include "llvm/GlobalValue.h"
00019 #include "llvm/Support/LeakDetector.h"
00020 #include <algorithm>
00021 #include <iostream>
00022 using namespace llvm;
00023 
00024 //===----------------------------------------------------------------------===//
00025 //                                Value Class
00026 //===----------------------------------------------------------------------===//
00027 
00028 static inline const Type *checkType(const Type *Ty) {
00029   assert(Ty && "Value defined with a null type: Error!");
00030   return Ty;
00031 }
00032 
00033 Value::Value(const Type *ty, unsigned scid, const std::string &name)
00034   : SubclassID(scid), Ty(checkType(ty)), Name(name) {
00035   if (!isa<Constant>(this) && !isa<BasicBlock>(this))
00036     assert((Ty->isFirstClassType() || Ty == Type::VoidTy || 
00037            isa<OpaqueType>(ty)) &&
00038            "Cannot create non-first-class values except for constants!");
00039   if (ty == Type::VoidTy)
00040     assert(name.empty() && "Cannot have named void values!");
00041 }
00042 
00043 Value::~Value() {
00044 #ifndef NDEBUG      // Only in -g mode...
00045   // Check to make sure that there are no uses of this value that are still
00046   // around when the value is destroyed.  If there are, then we have a dangling
00047   // reference and something is wrong.  This code is here to print out what is
00048   // still being referenced.  The value in question should be printed as 
00049   // a <badref>
00050   //
00051   if (Uses.begin() != Uses.end()) {
00052     std::cerr << "While deleting: " << *Ty << " %" << Name << "\n";
00053     for (use_const_iterator I = Uses.begin(), E = Uses.end(); I != E; ++I)
00054       std::cerr << "Use still stuck around after Def is destroyed:"
00055                 << **I << "\n";
00056   }
00057 #endif
00058   assert(Uses.begin() == Uses.end() &&"Uses remain when a value is destroyed!");
00059 
00060   // There should be no uses of this object anymore, remove it.
00061   LeakDetector::removeGarbageObject(this);
00062 }
00063 
00064 
00065 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
00066 // except that it doesn't have all of the asserts.  The asserts fail because we
00067 // are half-way done resolving types, which causes some types to exist as two
00068 // different Type*'s at the same time.  This is a sledgehammer to work around
00069 // this problem.
00070 //
00071 void Value::uncheckedReplaceAllUsesWith(Value *New) {
00072   while (!Uses.empty()) {
00073     Use &U = Uses.back();
00074     // Must handle Constants specially, we cannot call replaceUsesOfWith on a
00075     // constant!
00076     if (Constant *C = dyn_cast<Constant>(U.getUser())) {
00077       if (!isa<GlobalValue>(C))
00078         C->replaceUsesOfWithOnConstant(this, New, true);
00079       else 
00080         U.set(New);
00081     } else {
00082       U.set(New);
00083     }
00084   }
00085 }
00086 
00087 void Value::replaceAllUsesWith(Value *New) {
00088   assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
00089   assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
00090   assert(New->getType() == getType() &&
00091          "replaceAllUses of value with new value of different type!");
00092 
00093   uncheckedReplaceAllUsesWith(New);
00094 }
00095 
00096 //===----------------------------------------------------------------------===//
00097 //                                 User Class
00098 //===----------------------------------------------------------------------===//
00099 
00100 // replaceUsesOfWith - Replaces all references to the "From" definition with
00101 // references to the "To" definition.
00102 //
00103 void User::replaceUsesOfWith(Value *From, Value *To) {
00104   if (From == To) return;   // Duh what?
00105 
00106   assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
00107          "Cannot call User::replaceUsesofWith on a constant!");
00108 
00109   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
00110     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
00111       // The side effects of this setOperand call include linking to
00112       // "To", adding "this" to the uses list of To, and
00113       // most importantly, removing "this" from the use list of "From".
00114       setOperand(i, To); // Fix it now...
00115     }
00116 }
00117