Be a bit more efficient when processing the active and inactive
[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 "llvm/GlobalValue.h"
19 #include "Support/LeakDetector.h"
20 #include <algorithm>
21 #include <iostream>
22 using namespace llvm;
23
24 //===----------------------------------------------------------------------===//
25 //                                Value Class
26 //===----------------------------------------------------------------------===//
27
28 static inline const Type *checkType(const Type *Ty) {
29   assert(Ty && "Value defined with a null type: Error!");
30   return Ty;
31 }
32
33 Value::Value(const Type *ty, unsigned scid, const std::string &name)
34   : SubclassID(scid), Ty(checkType(ty)), Name(name) {
35   if (!isa<Constant>(this) && !isa<BasicBlock>(this))
36     assert((Ty->isFirstClassType() || Ty == Type::VoidTy || 
37            isa<OpaqueType>(ty)) &&
38            "Cannot create non-first-class values except for constants!");
39   if (ty == Type::VoidTy)
40     assert(name.empty() && "Cannot have named void values!");
41 }
42
43 Value::~Value() {
44 #ifndef NDEBUG      // Only in -g mode...
45   // Check to make sure that there are no uses of this value that are still
46   // around when the value is destroyed.  If there are, then we have a dangling
47   // reference and something is wrong.  This code is here to print out what is
48   // still being referenced.  The value in question should be printed as 
49   // a <badref>
50   //
51   if (Uses.begin() != Uses.end()) {
52     std::cerr << "While deleting: " << *Ty << "%" << Name << "\n";
53     for (use_const_iterator I = Uses.begin(), E = Uses.end(); I != E; ++I)
54       std::cerr << "Use still stuck around after Def is destroyed:"
55                 << **I << "\n";
56   }
57 #endif
58   assert(Uses.begin() == Uses.end() &&"Uses remain when a value is destroyed!");
59
60   // There should be no uses of this object anymore, remove it.
61   LeakDetector::removeGarbageObject(this);
62 }
63
64
65 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
66 // except that it doesn't have all of the asserts.  The asserts fail because we
67 // are half-way done resolving types, which causes some types to exist as two
68 // different Type*'s at the same time.  This is a sledgehammer to work around
69 // this problem.
70 //
71 void Value::uncheckedReplaceAllUsesWith(Value *New) {
72   while (!Uses.empty()) {
73     Use &U = Uses.back();
74     // Must handle Constants specially, we cannot call replaceUsesOfWith on a
75     // constant!
76     if (Constant *C = dyn_cast<Constant>(U.getUser())) {
77       if (!isa<GlobalValue>(C))
78         C->replaceUsesOfWithOnConstant(this, New, true);
79       else 
80         U.set(New);
81     } else {
82       U.set(New);
83     }
84   }
85 }
86
87 void Value::replaceAllUsesWith(Value *New) {
88   assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
89   assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
90   assert(New->getType() == getType() &&
91          "replaceAllUses of value with new value of different type!");
92
93   uncheckedReplaceAllUsesWith(New);
94 }
95
96 //===----------------------------------------------------------------------===//
97 //                                 User Class
98 //===----------------------------------------------------------------------===//
99
100 // replaceUsesOfWith - Replaces all references to the "From" definition with
101 // references to the "To" definition.
102 //
103 void User::replaceUsesOfWith(Value *From, Value *To) {
104   if (From == To) return;   // Duh what?
105
106   assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
107          "Cannot call User::replaceUsesofWith on a constant!");
108
109   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
110     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
111       // The side effects of this setOperand call include linking to
112       // "To", adding "this" to the uses list of To, and
113       // most importantly, removing "this" from the use list of "From".
114       setOperand(i, To); // Fix it now...
115     }
116 }
117