blob: 245fa482b74ae2c01b99919b55b462e03c827371 [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 Lattner28eca8b2002-10-09 23:12:59 +000010#include "llvm/Constant.h"
Chris Lattnerd1e693f2002-09-08 18:59:35 +000011#include "Support/LeakDetector.h"
Chris Lattner00950542001-06-06 20:29:01 +000012#include <algorithm>
Chris Lattner3e009942002-06-30 16:25:25 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014//===----------------------------------------------------------------------===//
15// Value Class
16//===----------------------------------------------------------------------===//
17
Chris Lattner82d18aa2001-12-13 00:41:27 +000018static inline const Type *checkType(const Type *Ty) {
19 assert(Ty && "Value defined with a null type: Error!");
20 return Ty;
21}
22
Chris Lattnerfe8041a2002-07-24 22:08:53 +000023Value::Value(const Type *ty, ValueTy vty, const std::string &name)
Chris Lattner82d18aa2001-12-13 00:41:27 +000024 : Name(name), Ty(checkType(ty), this) {
Chris Lattner00950542001-06-06 20:29:01 +000025 VTy = vty;
26}
27
28Value::~Value() {
29#ifndef NDEBUG // Only in -g mode...
Chris Lattneree976f32001-06-11 15:04:40 +000030 // 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 //
Chris Lattner00950542001-06-06 20:29:01 +000036 if (Uses.begin() != Uses.end()) {
Chris Lattnere7eaf172002-04-12 18:19:45 +000037 std::cerr << "While deleting: " << Ty << "%" << Name << "\n";
Chris Lattnerfe8041a2002-07-24 22:08:53 +000038 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";
Chris Lattner00950542001-06-06 20:29:01 +000041 }
42#endif
Chris Lattnera89ab2c2003-06-24 22:20:19 +000043 assert(Uses.begin() == Uses.end() &&"Uses remain when a value is destroyed!");
Chris Lattnerd1e693f2002-09-08 18:59:35 +000044
45 // There should be no uses of this object anymore, remove it.
46 LeakDetector::removeGarbageObject(this);
Chris Lattner00950542001-06-06 20:29:01 +000047}
48
Chris Lattner28eca8b2002-10-09 23:12:59 +000049
50
51
52void 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() &&
Chris Lattner150dcb92002-03-21 05:38:15 +000056 "replaceAllUses of value with new value of different type!");
Chris Lattner00950542001-06-06 20:29:01 +000057 while (!Uses.empty()) {
Chris Lattner46cbff62001-09-14 16:56:32 +000058 User *Use = Uses.back();
Chris Lattner28eca8b2002-10-09 23:12:59 +000059 // 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 }
Chris Lattner00950542001-06-06 20:29:01 +000066 }
67}
68
Chris Lattner2bc065b2003-08-29 05:09:37 +000069// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
70// except that it doesn't have all of the asserts. The asserts fail because we
71// are half-way done resolving types, which causes some types to exist as two
72// different Type*'s at the same time. This is a sledgehammer to work around
73// this problem.
74//
75void Value::uncheckedReplaceAllUsesWith(Value *New) {
76 while (!Uses.empty()) {
77 User *Use = Uses.back();
78 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
79 // constant!
80 if (Constant *C = dyn_cast<Constant>(Use)) {
Chris Lattner287d1112003-08-29 05:37:22 +000081 C->replaceUsesOfWithOnConstant(this, New, true);
Chris Lattner2bc065b2003-08-29 05:09:37 +000082 } else {
83 Use->replaceUsesOfWith(this, New);
84 }
85 }
86}
87
88
Chris Lattner36bd82a2001-09-07 16:57:07 +000089// refineAbstractType - This function is implemented because we use
90// potentially abstract types, and these types may be resolved to more
91// concrete types after we are constructed. For the value class, we simply
92// change Ty to point to the right type. :)
93//
94void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Chris Lattnerfe8041a2002-07-24 22:08:53 +000095 assert(Ty.get() == OldTy && "Can't refine anything but my type!");
Chris Lattner02d429d2002-01-25 03:45:27 +000096 if (OldTy == NewTy && !OldTy->isAbstract())
97 Ty.removeUserFromConcrete();
Chris Lattner36bd82a2001-09-07 16:57:07 +000098 Ty = NewTy;
99}
100
Chris Lattnerd1c657e2002-10-08 23:46:55 +0000101void Value::killUse(User *U) {
102 if (U == 0) return;
103 unsigned i;
Chris Lattner00950542001-06-06 20:29:01 +0000104
Chris Lattnerd1c657e2002-10-08 23:46:55 +0000105 // Scan backwards through the uses list looking for the user. We do this
106 // because vectors like to be accessed on the end. This is incredibly
107 // important from a performance perspective.
108 for (i = Uses.size()-1; Uses[i] != U; --i)
109 /* empty */;
110
111 assert(i < Uses.size() && "Use not in uses list!!");
Chris Lattner07793882003-02-13 19:46:22 +0000112 Uses[i] = Uses.back();
113 Uses.pop_back();
Chris Lattner00950542001-06-06 20:29:01 +0000114}
115
Chris Lattner00950542001-06-06 20:29:01 +0000116//===----------------------------------------------------------------------===//
117// User Class
118//===----------------------------------------------------------------------===//
119
Chris Lattner697954c2002-01-20 22:54:45 +0000120User::User(const Type *Ty, ValueTy vty, const std::string &name)
Chris Lattner00950542001-06-06 20:29:01 +0000121 : Value(Ty, vty, name) {
122}
123
124// replaceUsesOfWith - Replaces all references to the "From" definition with
125// references to the "To" definition.
126//
127void User::replaceUsesOfWith(Value *From, Value *To) {
128 if (From == To) return; // Duh what?
129
Chris Lattner28eca8b2002-10-09 23:12:59 +0000130 assert(!isa<Constant>(this) &&
131 "Cannot call User::replaceUsesofWith on a constant!");
132
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000133 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
134 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner00950542001-06-06 20:29:01 +0000135 // The side effects of this setOperand call include linking to
136 // "To", adding "this" to the uses list of To, and
137 // most importantly, removing "this" from the use list of "From".
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000138 setOperand(i, To); // Fix it now...
Chris Lattner00950542001-06-06 20:29:01 +0000139 }
Chris Lattner00950542001-06-06 20:29:01 +0000140}