Add support for tracking whether a module is 64/32 bit and big/little endian
[oota-llvm.git] / lib / VMCore / Value.cpp
1 //===-- Value.cpp - Implement the Value class -----------------------------===//
2 //
3 // This file implements the Value and User classes. 
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/InstrTypes.h"
8 #include "llvm/SymbolTable.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/Constant.h"
11 #include "Support/LeakDetector.h"
12 #include <algorithm>
13
14 //===----------------------------------------------------------------------===//
15 //                                Value Class
16 //===----------------------------------------------------------------------===//
17
18 static inline const Type *checkType(const Type *Ty) {
19   assert(Ty && "Value defined with a null type: Error!");
20   return Ty;
21 }
22
23 Value::Value(const Type *ty, ValueTy vty, const std::string &name)
24   : Name(name), Ty(checkType(ty), this) {
25   VTy = vty;
26 }
27
28 Value::~Value() {
29 #ifndef NDEBUG      // Only in -g mode...
30   // Check to make sure that there are no uses of this value that are still
31   // around when the value is destroyed.  If there are, then we have a dangling
32   // reference and something is wrong.  This code is here to print out what is
33   // still being referenced.  The value in question should be printed as 
34   // a <badref>
35   //
36   if (Uses.begin() != Uses.end()) {
37     std::cerr << "While deleting: " << Ty << "%" << Name << "\n";
38     for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
39       std::cerr << "Use still stuck around after Def is destroyed:"
40                 << **I << "\n";
41   }
42 #endif
43   assert(Uses.begin() == Uses.end());
44
45   // There should be no uses of this object anymore, remove it.
46   LeakDetector::removeGarbageObject(this);
47 }
48
49
50
51
52 void Value::replaceAllUsesWith(Value *New) {
53   assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
54   assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
55   assert(New->getType() == getType() &&
56          "replaceAllUses of value with new value of different type!");
57   while (!Uses.empty()) {
58     User *Use = Uses.back();
59     // Must handle Constants specially, we cannot call replaceUsesOfWith on a
60     // constant!
61     if (Constant *C = dyn_cast<Constant>(Use)) {
62       C->replaceUsesOfWithOnConstant(this, New);
63     } else {
64       Use->replaceUsesOfWith(this, New);
65     }
66   }
67 }
68
69 // refineAbstractType - This function is implemented because we use
70 // potentially abstract types, and these types may be resolved to more
71 // concrete types after we are constructed.  For the value class, we simply
72 // change Ty to point to the right type.  :)
73 //
74 void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
75   assert(Ty.get() == OldTy && "Can't refine anything but my type!");
76   if (OldTy == NewTy && !OldTy->isAbstract())
77     Ty.removeUserFromConcrete();
78   Ty = NewTy;
79 }
80
81 void Value::killUse(User *U) {
82   if (U == 0) return;
83   unsigned i;
84
85   // Scan backwards through the uses list looking for the user.  We do this
86   // because vectors like to be accessed on the end.  This is incredibly
87   // important from a performance perspective.
88   for (i = Uses.size()-1; Uses[i] != U; --i)
89     /* empty */;
90
91   assert(i < Uses.size() && "Use not in uses list!!");
92   Uses[i] = Uses.back();
93   Uses.pop_back();
94 }
95
96 //===----------------------------------------------------------------------===//
97 //                                 User Class
98 //===----------------------------------------------------------------------===//
99
100 User::User(const Type *Ty, ValueTy vty, const std::string &name) 
101   : Value(Ty, vty, name) {
102 }
103
104 // replaceUsesOfWith - Replaces all references to the "From" definition with
105 // references to the "To" definition.
106 //
107 void User::replaceUsesOfWith(Value *From, Value *To) {
108   if (From == To) return;   // Duh what?
109
110   assert(!isa<Constant>(this) &&
111          "Cannot call User::replaceUsesofWith on a constant!");
112
113   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
114     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
115       // The side effects of this setOperand call include linking to
116       // "To", adding "this" to the uses list of To, and
117       // most importantly, removing "this" from the use list of "From".
118       setOperand(i, To); // Fix it now...
119     }
120 }