LLVM API Documentation

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/Constant.h"
00015 #include "llvm/DerivedTypes.h"
00016 #include "llvm/InstrTypes.h"
00017 #include "llvm/Module.h"
00018 #include "llvm/SymbolTable.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), SubclassData(0), Ty(checkType(ty)),
00035     UseList(0), Name(name) {
00036   if (!isa<Constant>(this) && !isa<BasicBlock>(this))
00037     assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
00038            isa<OpaqueType>(ty)) &&
00039            "Cannot create non-first-class values except for constants!");
00040   if (ty == Type::VoidTy)
00041     assert(name.empty() && "Cannot have named void values!");
00042 }
00043 
00044 Value::~Value() {
00045 #ifndef NDEBUG      // Only in -g mode...
00046   // Check to make sure that there are no uses of this value that are still
00047   // around when the value is destroyed.  If there are, then we have a dangling
00048   // reference and something is wrong.  This code is here to print out what is
00049   // still being referenced.  The value in question should be printed as
00050   // a <badref>
00051   //
00052   if (use_begin() != use_end()) {
00053     std::cerr << "While deleting: " << *Ty << " %" << Name << "\n";
00054     for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
00055       std::cerr << "Use still stuck around after Def is destroyed:"
00056                 << **I << "\n";
00057   }
00058 #endif
00059   assert(use_begin() == use_end() && "Uses remain when a value is destroyed!");
00060 
00061   // There should be no uses of this object anymore, remove it.
00062   LeakDetector::removeGarbageObject(this);
00063 }
00064 
00065 /// hasNUses - Return true if this Value has exactly N users.
00066 ///
00067 bool Value::hasNUses(unsigned N) const {
00068   use_const_iterator UI = use_begin(), E = use_end();
00069 
00070   for (; N; --N, ++UI)
00071     if (UI == E) return false;  // Too few.
00072   return UI == E;
00073 }
00074 
00075 /// hasNUsesOrMore - Return true if this value has N users or more.  This is
00076 /// logically equivalent to getNumUses() >= N.
00077 ///
00078 bool Value::hasNUsesOrMore(unsigned N) const {
00079   use_const_iterator UI = use_begin(), E = use_end();
00080 
00081   for (; N; --N, ++UI)
00082     if (UI == E) return false;  // Too few.
00083 
00084   return true;
00085 }
00086 
00087 
00088 /// getNumUses - This method computes the number of uses of this Value.  This
00089 /// is a linear time operation.  Use hasOneUse or hasNUses to check for specific
00090 /// values.
00091 unsigned Value::getNumUses() const {
00092   return (unsigned)std::distance(use_begin(), use_end());
00093 }
00094 
00095 
00096 void Value::setName(const std::string &name) {
00097   if (Name == name) return;   // Name is already set.
00098 
00099   // Get the symbol table to update for this object.
00100   SymbolTable *ST = 0;
00101   if (Instruction *I = dyn_cast<Instruction>(this)) {
00102     if (BasicBlock *P = I->getParent())
00103       if (Function *PP = P->getParent())
00104         ST = &PP->getSymbolTable();
00105   } else if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
00106     if (Function *P = BB->getParent()) ST = &P->getSymbolTable();
00107   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
00108     if (Module *P = GV->getParent()) ST = &P->getSymbolTable();
00109   } else if (Argument *A = dyn_cast<Argument>(this)) {
00110     if (Function *P = A->getParent()) ST = &P->getSymbolTable();
00111   } else {
00112     assert(isa<Constant>(this) && "Unknown value type!");
00113     return;  // no name is setable for this.
00114   }
00115 
00116   if (!ST)  // No symbol table to update?  Just do the change.
00117     Name = name;
00118   else if (hasName()) {
00119     if (!name.empty()) {    // Replacing name.
00120       ST->changeName(this, name);
00121     } else {                // Transitioning from hasName -> noname.
00122       ST->remove(this);
00123       Name.clear();
00124     }
00125   } else {                  // Transitioning from noname -> hasName.
00126     Name = name;
00127     ST->insert(this);
00128   }
00129 }
00130 
00131 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
00132 // except that it doesn't have all of the asserts.  The asserts fail because we
00133 // are half-way done resolving types, which causes some types to exist as two
00134 // different Type*'s at the same time.  This is a sledgehammer to work around
00135 // this problem.
00136 //
00137 void Value::uncheckedReplaceAllUsesWith(Value *New) {
00138   while (!use_empty()) {
00139     Use &U = *UseList;
00140     // Must handle Constants specially, we cannot call replaceUsesOfWith on a
00141     // constant!
00142     if (Constant *C = dyn_cast<Constant>(U.getUser())) {
00143       if (!isa<GlobalValue>(C))
00144         C->replaceUsesOfWithOnConstant(this, New, &U);
00145       else
00146         U.set(New);
00147     } else {
00148       U.set(New);
00149     }
00150   }
00151 }
00152 
00153 void Value::replaceAllUsesWith(Value *New) {
00154   assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
00155   assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
00156   assert(New->getType() == getType() &&
00157          "replaceAllUses of value with new value of different type!");
00158 
00159   uncheckedReplaceAllUsesWith(New);
00160 }
00161 
00162 //===----------------------------------------------------------------------===//
00163 //                                 User Class
00164 //===----------------------------------------------------------------------===//
00165 
00166 // replaceUsesOfWith - Replaces all references to the "From" definition with
00167 // references to the "To" definition.
00168 //
00169 void User::replaceUsesOfWith(Value *From, Value *To) {
00170   if (From == To) return;   // Duh what?
00171 
00172   assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
00173          "Cannot call User::replaceUsesofWith on a constant!");
00174 
00175   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
00176     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
00177       // The side effects of this setOperand call include linking to
00178       // "To", adding "this" to the uses list of To, and
00179       // most importantly, removing "this" from the use list of "From".
00180       setOperand(i, To); // Fix it now...
00181     }
00182 }
00183