blob: 758753559e09b0c33703d8bb09da322be42ef502 [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 Lattner184b2982002-09-08 18:59:35 +000010#include "Support/LeakDetector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000011#include <algorithm>
Chris Lattner33422fe2002-06-30 16:25:25 +000012
Chris Lattner2f7c9632001-06-06 20:29:01 +000013//===----------------------------------------------------------------------===//
14// Value Class
15//===----------------------------------------------------------------------===//
16
Chris Lattner25450e32001-12-13 00:41:27 +000017static inline const Type *checkType(const Type *Ty) {
18 assert(Ty && "Value defined with a null type: Error!");
19 return Ty;
20}
21
Chris Lattnera82ee2d2002-07-24 22:08:53 +000022Value::Value(const Type *ty, ValueTy vty, const std::string &name)
Chris Lattner25450e32001-12-13 00:41:27 +000023 : Name(name), Ty(checkType(ty), this) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000024 VTy = vty;
25}
26
27Value::~Value() {
28#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000029 // Check to make sure that there are no uses of this value that are still
30 // around when the value is destroyed. If there are, then we have a dangling
31 // reference and something is wrong. This code is here to print out what is
32 // still being referenced. The value in question should be printed as
33 // a <badref>
34 //
Chris Lattner2f7c9632001-06-06 20:29:01 +000035 if (Uses.begin() != Uses.end()) {
Chris Lattner6fc8c232002-04-12 18:19:45 +000036 std::cerr << "While deleting: " << Ty << "%" << Name << "\n";
Chris Lattnera82ee2d2002-07-24 22:08:53 +000037 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
38 std::cerr << "Use still stuck around after Def is destroyed:"
39 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000040 }
41#endif
42 assert(Uses.begin() == Uses.end());
Chris Lattner184b2982002-09-08 18:59:35 +000043
44 // There should be no uses of this object anymore, remove it.
45 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000046}
47
48void Value::replaceAllUsesWith(Value *D) {
49 assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
Chris Lattner2dd58ae2001-06-29 05:25:51 +000050 assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
Chris Lattner25c8f602002-03-21 05:38:15 +000051 assert(D->getType() == getType() &&
52 "replaceAllUses of value with new value of different type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000053 while (!Uses.empty()) {
Chris Lattner8d44b992001-09-14 16:56:32 +000054 User *Use = Uses.back();
Chris Lattner2f7c9632001-06-06 20:29:01 +000055#ifndef NDEBUG
56 unsigned NumUses = Uses.size();
57#endif
58 Use->replaceUsesOfWith(this, D);
59
60#ifndef NDEBUG // only in -g mode...
Chris Lattnera82ee2d2002-07-24 22:08:53 +000061 if (Uses.size() == NumUses)
62 std::cerr << "Use: " << *Use << "replace with: " << *D;
Chris Lattner2f7c9632001-06-06 20:29:01 +000063#endif
64 assert(Uses.size() != NumUses && "Didn't remove definition!");
65 }
66}
67
Chris Lattner97d79f12001-09-07 16:57:07 +000068// refineAbstractType - This function is implemented because we use
69// potentially abstract types, and these types may be resolved to more
70// concrete types after we are constructed. For the value class, we simply
71// change Ty to point to the right type. :)
72//
73void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +000074 assert(Ty.get() == OldTy && "Can't refine anything but my type!");
Chris Lattner67e5c292002-01-25 03:45:27 +000075 if (OldTy == NewTy && !OldTy->isAbstract())
76 Ty.removeUserFromConcrete();
Chris Lattner97d79f12001-09-07 16:57:07 +000077 Ty = NewTy;
78}
79
Chris Lattner3ea0c182002-10-08 23:46:55 +000080void Value::killUse(User *U) {
81 if (U == 0) return;
82 unsigned i;
Chris Lattner2f7c9632001-06-06 20:29:01 +000083
Chris Lattner3ea0c182002-10-08 23:46:55 +000084 // Scan backwards through the uses list looking for the user. We do this
85 // because vectors like to be accessed on the end. This is incredibly
86 // important from a performance perspective.
87 for (i = Uses.size()-1; Uses[i] != U; --i)
88 /* empty */;
89
90 assert(i < Uses.size() && "Use not in uses list!!");
91 Uses.erase(Uses.begin()+i);
Chris Lattner2f7c9632001-06-06 20:29:01 +000092}
93
Chris Lattner2f7c9632001-06-06 20:29:01 +000094//===----------------------------------------------------------------------===//
95// User Class
96//===----------------------------------------------------------------------===//
97
Chris Lattner7f74a562002-01-20 22:54:45 +000098User::User(const Type *Ty, ValueTy vty, const std::string &name)
Chris Lattner2f7c9632001-06-06 20:29:01 +000099 : Value(Ty, vty, name) {
100}
101
102// replaceUsesOfWith - Replaces all references to the "From" definition with
103// references to the "To" definition.
104//
105void User::replaceUsesOfWith(Value *From, Value *To) {
106 if (From == To) return; // Duh what?
107
Chris Lattnera073acb2001-07-07 08:36:50 +0000108 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
109 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000110 // The side effects of this setOperand call include linking to
111 // "To", adding "this" to the uses list of To, and
112 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000113 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000114 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000115}
116
117