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