Put all LLVM code into the llvm namespace, as per bug 109.
[oota-llvm.git] / lib / VMCore / Value.cpp
1 //===-- Value.cpp - Implement the Value class -----------------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Value and User classes. 
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/InstrTypes.h"
15 #include "llvm/SymbolTable.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Constant.h"
18 #include "Support/LeakDetector.h"
19 #include <algorithm>
20
21 namespace llvm {
22
23 //===----------------------------------------------------------------------===//
24 //                                Value Class
25 //===----------------------------------------------------------------------===//
26
27 static inline const Type *checkType(const Type *Ty) {
28   assert(Ty && "Value defined with a null type: Error!");
29   return Ty;
30 }
31
32 Value::Value(const Type *ty, ValueTy vty, const std::string &name)
33   : Name(name), Ty(checkType(ty)) {
34   VTy = vty;
35 }
36
37 Value::~Value() {
38 #ifndef NDEBUG      // Only in -g mode...
39   // Check to make sure that there are no uses of this value that are still
40   // around when the value is destroyed.  If there are, then we have a dangling
41   // reference and something is wrong.  This code is here to print out what is
42   // still being referenced.  The value in question should be printed as 
43   // a <badref>
44   //
45   if (Uses.begin() != Uses.end()) {
46     std::cerr << "While deleting: " << *Ty << "%" << Name << "\n";
47     for (use_const_iterator I = Uses.begin(), E = Uses.end(); I != E; ++I)
48       std::cerr << "Use still stuck around after Def is destroyed:"
49                 << **I << "\n";
50   }
51 #endif
52   assert(Uses.begin() == Uses.end() &&"Uses remain when a value is destroyed!");
53
54   // There should be no uses of this object anymore, remove it.
55   LeakDetector::removeGarbageObject(this);
56 }
57
58
59 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
60 // except that it doesn't have all of the asserts.  The asserts fail because we
61 // are half-way done resolving types, which causes some types to exist as two
62 // different Type*'s at the same time.  This is a sledgehammer to work around
63 // this problem.
64 //
65 void Value::uncheckedReplaceAllUsesWith(Value *New) {
66   while (!Uses.empty()) {
67     Use &U = Uses.back();
68     // Must handle Constants specially, we cannot call replaceUsesOfWith on a
69     // constant!
70     if (Constant *C = dyn_cast<Constant>(U.getUser())) {
71       C->replaceUsesOfWithOnConstant(this, New, true);
72     } else {
73       U.set(New);
74     }
75   }
76 }
77
78 void Value::replaceAllUsesWith(Value *New) {
79   assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
80   assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
81   assert(New->getType() == getType() &&
82          "replaceAllUses of value with new value of different type!");
83
84   uncheckedReplaceAllUsesWith(New);
85 }
86
87 //===----------------------------------------------------------------------===//
88 //                                 User Class
89 //===----------------------------------------------------------------------===//
90
91 User::User(const Type *Ty, ValueTy vty, const std::string &name) 
92   : Value(Ty, vty, name) {
93 }
94
95 // replaceUsesOfWith - Replaces all references to the "From" definition with
96 // references to the "To" definition.
97 //
98 void User::replaceUsesOfWith(Value *From, Value *To) {
99   if (From == To) return;   // Duh what?
100
101   assert(!isa<Constant>(this) &&
102          "Cannot call User::replaceUsesofWith on a constant!");
103
104   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
105     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
106       // The side effects of this setOperand call include linking to
107       // "To", adding "this" to the uses list of To, and
108       // most importantly, removing "this" from the use list of "From".
109       setOperand(i, To); // Fix it now...
110     }
111 }
112
113 } // End llvm namespace