blob: 0218e5798fad37e351d81bdb8371357e6cdb42b5 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Value.cpp - Implement the Value class -----------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Misha Brukmanb1c93172005-04-21 23:48:37 +000010// This file implements the Value and User classes.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2c6abea2002-10-09 23:12:59 +000014#include "llvm/Constant.h"
Chris Lattnercdb9bfc2005-03-05 19:51:50 +000015#include "llvm/DerivedTypes.h"
16#include "llvm/InstrTypes.h"
17#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000018#include "llvm/ValueSymbolTable.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000019#include "llvm/Support/Debug.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/Support/LeakDetector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000021#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattner2f7c9632001-06-06 20:29:01 +000024//===----------------------------------------------------------------------===//
25// Value Class
26//===----------------------------------------------------------------------===//
27
Chris Lattner25450e32001-12-13 00:41:27 +000028static inline const Type *checkType(const Type *Ty) {
29 assert(Ty && "Value defined with a null type: Error!");
30 return Ty;
31}
32
Chris Lattnerd0b0b452004-06-26 20:33:39 +000033Value::Value(const Type *ty, unsigned scid, const std::string &name)
Chris Lattnera29c92f2005-02-05 01:37:58 +000034 : SubclassID(scid), SubclassData(0), Ty(checkType(ty)),
35 UseList(0), Name(name) {
Chris Lattnerbea72472004-07-06 17:44:17 +000036 if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Misha Brukmanb1c93172005-04-21 23:48:37 +000037 assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
Chris Lattner9df9afd2004-07-07 18:07:46 +000038 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000039 "Cannot create non-first-class values except for constants!");
Alkis Evlogimenos2f130282004-07-25 06:07:15 +000040 if (ty == Type::VoidTy)
Alkis Evlogimenos74614b02004-07-25 06:16:52 +000041 assert(name.empty() && "Cannot have named void values!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000042}
43
44Value::~Value() {
45#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000046 // Check to make sure that there are no uses of this value that are still
47 // around when the value is destroyed. If there are, then we have a dangling
48 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +000049 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +000050 // a <badref>
51 //
Chris Lattner4947e672005-02-01 01:24:21 +000052 if (use_begin() != use_end()) {
Bill Wendling6a462f12006-11-17 08:03:48 +000053 DOUT << "While deleting: " << *Ty << " %" << Name << "\n";
Chris Lattner4947e672005-02-01 01:24:21 +000054 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
Bill Wendling6a462f12006-11-17 08:03:48 +000055 DOUT << "Use still stuck around after Def is destroyed:"
56 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000057 }
58#endif
Chris Lattner4947e672005-02-01 01:24:21 +000059 assert(use_begin() == use_end() && "Uses remain when a value is destroyed!");
Chris Lattner184b2982002-09-08 18:59:35 +000060
61 // There should be no uses of this object anymore, remove it.
62 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000063}
64
Chris Lattner4947e672005-02-01 01:24:21 +000065/// hasNUses - Return true if this Value has exactly N users.
66///
67bool Value::hasNUses(unsigned N) const {
68 use_const_iterator UI = use_begin(), E = use_end();
69
70 for (; N; --N, ++UI)
71 if (UI == E) return false; // Too few.
72 return UI == E;
73}
74
Chris Lattnerd36552f2005-02-23 16:51:11 +000075/// hasNUsesOrMore - Return true if this value has N users or more. This is
76/// logically equivalent to getNumUses() >= N.
77///
78bool Value::hasNUsesOrMore(unsigned N) const {
79 use_const_iterator UI = use_begin(), E = use_end();
80
81 for (; N; --N, ++UI)
82 if (UI == E) return false; // Too few.
83
84 return true;
85}
86
87
Chris Lattner4947e672005-02-01 01:24:21 +000088/// getNumUses - This method computes the number of uses of this Value. This
89/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
90/// values.
91unsigned Value::getNumUses() const {
92 return (unsigned)std::distance(use_begin(), use_end());
93}
94
Chris Lattner2c6abea2002-10-09 23:12:59 +000095
Chris Lattnercdb9bfc2005-03-05 19:51:50 +000096void Value::setName(const std::string &name) {
97 if (Name == name) return; // Name is already set.
98
Chris Lattnerffb37782005-03-06 02:14:28 +000099 // Get the symbol table to update for this object.
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000100 ValueSymbolTable *ST = 0;
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000101 if (Instruction *I = dyn_cast<Instruction>(this)) {
102 if (BasicBlock *P = I->getParent())
103 if (Function *PP = P->getParent())
Reid Spencer32af9e82007-01-06 07:24:44 +0000104 ST = &PP->getValueSymbolTable();
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000105 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000106 if (Function *P = BB->getParent())
107 ST = &P->getValueSymbolTable();
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000108 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000109 if (Module *P = GV->getParent())
110 ST = &P->getValueSymbolTable();
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000111 } else if (Argument *A = dyn_cast<Argument>(this)) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000112 if (Function *P = A->getParent())
113 ST = &P->getValueSymbolTable();
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000114 } else {
115 assert(isa<Constant>(this) && "Unknown value type!");
116 return; // no name is setable for this.
117 }
118
Chris Lattnerffb37782005-03-06 02:14:28 +0000119 if (!ST) // No symbol table to update? Just do the change.
120 Name = name;
121 else if (hasName()) {
122 if (!name.empty()) { // Replacing name.
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000123 ST->rename(this, name);
Chris Lattnerffb37782005-03-06 02:14:28 +0000124 } else { // Transitioning from hasName -> noname.
125 ST->remove(this);
126 Name.clear();
127 }
128 } else { // Transitioning from noname -> hasName.
129 Name = name;
130 ST->insert(this);
131 }
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000132}
133
Chris Lattner9f158122003-08-29 05:09:37 +0000134// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
135// except that it doesn't have all of the asserts. The asserts fail because we
136// are half-way done resolving types, which causes some types to exist as two
137// different Type*'s at the same time. This is a sledgehammer to work around
138// this problem.
139//
140void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner4947e672005-02-01 01:24:21 +0000141 while (!use_empty()) {
142 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000143 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
144 // constant!
Chris Lattner079edeb2003-10-16 16:53:07 +0000145 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Reid Spencerbbddbf32004-07-18 00:01:50 +0000146 if (!isa<GlobalValue>(C))
Chris Lattner7a1450d2005-10-04 18:13:04 +0000147 C->replaceUsesOfWithOnConstant(this, New, &U);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000148 else
Reid Spencerbbddbf32004-07-18 00:01:50 +0000149 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000150 } else {
Chris Lattner079edeb2003-10-16 16:53:07 +0000151 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000152 }
153 }
154}
155
Chris Lattner079edeb2003-10-16 16:53:07 +0000156void Value::replaceAllUsesWith(Value *New) {
157 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
158 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
159 assert(New->getType() == getType() &&
160 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000161
Chris Lattner079edeb2003-10-16 16:53:07 +0000162 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000163}
164
Chris Lattner2f7c9632001-06-06 20:29:01 +0000165//===----------------------------------------------------------------------===//
166// User Class
167//===----------------------------------------------------------------------===//
168
Chris Lattner2f7c9632001-06-06 20:29:01 +0000169// replaceUsesOfWith - Replaces all references to the "From" definition with
170// references to the "To" definition.
171//
172void User::replaceUsesOfWith(Value *From, Value *To) {
173 if (From == To) return; // Duh what?
174
Reid Spencerbbddbf32004-07-18 00:01:50 +0000175 assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000176 "Cannot call User::replaceUsesofWith on a constant!");
177
Chris Lattnera073acb2001-07-07 08:36:50 +0000178 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
179 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000180 // The side effects of this setOperand call include linking to
181 // "To", adding "this" to the uses list of To, and
182 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000183 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000184 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000185}
Reid Spencerbbddbf32004-07-18 00:01:50 +0000186