blob: ad2b18066843a36c766047df8010a44ca3e866b9 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- Value.cpp - Implement the Value class -----------------------------===//
2//
Chris Lattner67e08db2002-04-28 04:52:28 +00003// This file implements the Value and User classes.
Chris Lattner00950542001-06-06 20:29:01 +00004//
5//===----------------------------------------------------------------------===//
6
Chris Lattner00950542001-06-06 20:29:01 +00007#include "llvm/InstrTypes.h"
8#include "llvm/SymbolTable.h"
Chris Lattner02d429d2002-01-25 03:45:27 +00009#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000010#include <algorithm>
Chris Lattner3e009942002-06-30 16:25:25 +000011
Chris Lattner00950542001-06-06 20:29:01 +000012//===----------------------------------------------------------------------===//
13// Value Class
14//===----------------------------------------------------------------------===//
15
Chris Lattner82d18aa2001-12-13 00:41:27 +000016static inline const Type *checkType(const Type *Ty) {
17 assert(Ty && "Value defined with a null type: Error!");
18 return Ty;
19}
20
Chris Lattnerfe8041a2002-07-24 22:08:53 +000021Value::Value(const Type *ty, ValueTy vty, const std::string &name)
Chris Lattner82d18aa2001-12-13 00:41:27 +000022 : Name(name), Ty(checkType(ty), this) {
Chris Lattner00950542001-06-06 20:29:01 +000023 VTy = vty;
24}
25
26Value::~Value() {
27#ifndef NDEBUG // Only in -g mode...
Chris Lattneree976f32001-06-11 15:04:40 +000028 // Check to make sure that there are no uses of this value that are still
29 // around when the value is destroyed. If there are, then we have a dangling
30 // reference and something is wrong. This code is here to print out what is
31 // still being referenced. The value in question should be printed as
32 // a <badref>
33 //
Chris Lattner00950542001-06-06 20:29:01 +000034 if (Uses.begin() != Uses.end()) {
Chris Lattnere7eaf172002-04-12 18:19:45 +000035 std::cerr << "While deleting: " << Ty << "%" << Name << "\n";
Chris Lattnerfe8041a2002-07-24 22:08:53 +000036 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
37 std::cerr << "Use still stuck around after Def is destroyed:"
38 << **I << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000039 }
40#endif
41 assert(Uses.begin() == Uses.end());
42}
43
44void Value::replaceAllUsesWith(Value *D) {
45 assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
Chris Lattner7b8ec2d2001-06-29 05:25:51 +000046 assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
Chris Lattner150dcb92002-03-21 05:38:15 +000047 assert(D->getType() == getType() &&
48 "replaceAllUses of value with new value of different type!");
Chris Lattner00950542001-06-06 20:29:01 +000049 while (!Uses.empty()) {
Chris Lattner46cbff62001-09-14 16:56:32 +000050 User *Use = Uses.back();
Chris Lattner00950542001-06-06 20:29:01 +000051#ifndef NDEBUG
52 unsigned NumUses = Uses.size();
53#endif
54 Use->replaceUsesOfWith(this, D);
55
56#ifndef NDEBUG // only in -g mode...
Chris Lattnerfe8041a2002-07-24 22:08:53 +000057 if (Uses.size() == NumUses)
58 std::cerr << "Use: " << *Use << "replace with: " << *D;
Chris Lattner00950542001-06-06 20:29:01 +000059#endif
60 assert(Uses.size() != NumUses && "Didn't remove definition!");
61 }
62}
63
Chris Lattner36bd82a2001-09-07 16:57:07 +000064// refineAbstractType - This function is implemented because we use
65// potentially abstract types, and these types may be resolved to more
66// concrete types after we are constructed. For the value class, we simply
67// change Ty to point to the right type. :)
68//
69void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Chris Lattnerfe8041a2002-07-24 22:08:53 +000070 assert(Ty.get() == OldTy && "Can't refine anything but my type!");
Chris Lattner02d429d2002-01-25 03:45:27 +000071 if (OldTy == NewTy && !OldTy->isAbstract())
72 Ty.removeUserFromConcrete();
Chris Lattner36bd82a2001-09-07 16:57:07 +000073 Ty = NewTy;
74}
75
Chris Lattner00950542001-06-06 20:29:01 +000076void Value::killUse(User *i) {
77 if (i == 0) return;
78 use_iterator I = find(Uses.begin(), Uses.end(), i);
79
80 assert(I != Uses.end() && "Use not in uses list!!");
81 Uses.erase(I);
82}
83
84User *Value::use_remove(use_iterator &I) {
85 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
86 User *i = *I;
87 I = Uses.erase(I);
88 return i;
89}
90
Chris Lattner00950542001-06-06 20:29:01 +000091//===----------------------------------------------------------------------===//
92// User Class
93//===----------------------------------------------------------------------===//
94
Chris Lattner697954c2002-01-20 22:54:45 +000095User::User(const Type *Ty, ValueTy vty, const std::string &name)
Chris Lattner00950542001-06-06 20:29:01 +000096 : Value(Ty, vty, name) {
97}
98
99// replaceUsesOfWith - Replaces all references to the "From" definition with
100// references to the "To" definition.
101//
102void User::replaceUsesOfWith(Value *From, Value *To) {
103 if (From == To) return; // Duh what?
104
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000105 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
106 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner00950542001-06-06 20:29:01 +0000107 // The side effects of this setOperand call include linking to
108 // "To", adding "this" to the uses list of To, and
109 // most importantly, removing "this" from the use list of "From".
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000110 setOperand(i, To); // Fix it now...
Chris Lattner00950542001-06-06 20:29:01 +0000111 }
Chris Lattner00950542001-06-06 20:29:01 +0000112}
113
114