blob: da3d87f9d2e7b7c4d623b7630a1767db3e738e33 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Value.cpp - Implement the Value class -----------------------------===//
2//
Chris Lattner30ddf7f2002-04-28 04:52:28 +00003// This file implements the Value and User classes.
Chris Lattner2f7c9632001-06-06 20:29:01 +00004//
5//===----------------------------------------------------------------------===//
6
Chris Lattner2f7c9632001-06-06 20:29:01 +00007#include "llvm/InstrTypes.h"
8#include "llvm/SymbolTable.h"
Chris Lattner67e5c292002-01-25 03:45:27 +00009#include "llvm/DerivedTypes.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000010#include <algorithm>
Chris Lattner33422fe2002-06-30 16:25:25 +000011
Chris Lattner2f7c9632001-06-06 20:29:01 +000012//===----------------------------------------------------------------------===//
13// Value Class
14//===----------------------------------------------------------------------===//
15
Chris Lattner25450e32001-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 Lattner7f74a562002-01-20 22:54:45 +000021Value::Value(const Type *ty, ValueTy vty, const std::string &name = "")
Chris Lattner25450e32001-12-13 00:41:27 +000022 : Name(name), Ty(checkType(ty), this) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000023 VTy = vty;
24}
25
26Value::~Value() {
27#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-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 Lattner2f7c9632001-06-06 20:29:01 +000034 if (Uses.begin() != Uses.end()) {
Chris Lattner6fc8c232002-04-12 18:19:45 +000035 std::cerr << "While deleting: " << Ty << "%" << Name << "\n";
Chris Lattner3d381bb2002-04-07 22:33:13 +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)->dump();
39 std::cerr << "\n";
40 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000041 }
42#endif
43 assert(Uses.begin() == Uses.end());
44}
45
46void Value::replaceAllUsesWith(Value *D) {
47 assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
Chris Lattner2dd58ae2001-06-29 05:25:51 +000048 assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
Chris Lattner25c8f602002-03-21 05:38:15 +000049 assert(D->getType() == getType() &&
50 "replaceAllUses of value with new value of different type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000051 while (!Uses.empty()) {
Chris Lattner8d44b992001-09-14 16:56:32 +000052 User *Use = Uses.back();
Chris Lattner2f7c9632001-06-06 20:29:01 +000053#ifndef NDEBUG
54 unsigned NumUses = Uses.size();
55#endif
56 Use->replaceUsesOfWith(this, D);
57
58#ifndef NDEBUG // only in -g mode...
Chris Lattner3d381bb2002-04-07 22:33:13 +000059 if (Uses.size() == NumUses) {
60 std::cerr << "Use: ";
61 Use->dump();
62 std::cerr << "replace with: ";
63 D->dump();
64 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000065#endif
66 assert(Uses.size() != NumUses && "Didn't remove definition!");
67 }
68}
69
Chris Lattner97d79f12001-09-07 16:57:07 +000070// refineAbstractType - This function is implemented because we use
71// potentially abstract types, and these types may be resolved to more
72// concrete types after we are constructed. For the value class, we simply
73// change Ty to point to the right type. :)
74//
75void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Chris Lattner67e5c292002-01-25 03:45:27 +000076 assert(Ty.get() == OldTy &&"Can't refine anything but my type!");
77 if (OldTy == NewTy && !OldTy->isAbstract())
78 Ty.removeUserFromConcrete();
Chris Lattner97d79f12001-09-07 16:57:07 +000079 Ty = NewTy;
80}
81
Chris Lattner2f7c9632001-06-06 20:29:01 +000082void Value::killUse(User *i) {
83 if (i == 0) return;
84 use_iterator I = find(Uses.begin(), Uses.end(), i);
85
86 assert(I != Uses.end() && "Use not in uses list!!");
87 Uses.erase(I);
88}
89
90User *Value::use_remove(use_iterator &I) {
91 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
92 User *i = *I;
93 I = Uses.erase(I);
94 return i;
95}
96
Chris Lattner2f7c9632001-06-06 20:29:01 +000097//===----------------------------------------------------------------------===//
98// User Class
99//===----------------------------------------------------------------------===//
100
Chris Lattner7f74a562002-01-20 22:54:45 +0000101User::User(const Type *Ty, ValueTy vty, const std::string &name)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000102 : Value(Ty, vty, name) {
103}
104
105// replaceUsesOfWith - Replaces all references to the "From" definition with
106// references to the "To" definition.
107//
108void User::replaceUsesOfWith(Value *From, Value *To) {
109 if (From == To) return; // Duh what?
110
Chris Lattnera073acb2001-07-07 08:36:50 +0000111 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
112 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000113 // The side effects of this setOperand call include linking to
114 // "To", adding "this" to the uses list of To, and
115 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000116 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000117 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000118}
119
120