blob: dd06441a953df2685c19c9bb92ee5c539a3fdb04 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Value.cpp - Implement the Value class -----------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner30ddf7f2002-04-28 04:52:28 +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"
18#include "llvm/SymbolTable.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/Support/LeakDetector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000020#include <algorithm>
Reid Spencer8baf8e22004-07-04 11:55:37 +000021#include <iostream>
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))
Chris Lattner9df9afd2004-07-07 18:07:46 +000037 assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
38 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
49 // still being referenced. The value in question should be printed as
50 // a <badref>
51 //
Chris Lattner4947e672005-02-01 01:24:21 +000052 if (use_begin() != use_end()) {
Misha Brukman65c23ee2004-10-15 23:08:50 +000053 std::cerr << "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)
Chris Lattnera82ee2d2002-07-24 22:08:53 +000055 std::cerr << "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.
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000100 SymbolTable *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())
104 ST = &PP->getSymbolTable();
105 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
106 if (Function *P = BB->getParent()) ST = &P->getSymbolTable();
107 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
108 if (Module *P = GV->getParent()) ST = &P->getSymbolTable();
109 } else if (Argument *A = dyn_cast<Argument>(this)) {
110 if (Function *P = A->getParent()) ST = &P->getSymbolTable();
111 } else {
112 assert(isa<Constant>(this) && "Unknown value type!");
113 return; // no name is setable for this.
114 }
115
Chris Lattnerffb37782005-03-06 02:14:28 +0000116 if (!ST) // No symbol table to update? Just do the change.
117 Name = name;
118 else if (hasName()) {
119 if (!name.empty()) { // Replacing name.
120 ST->changeName(this, name);
121 } else { // Transitioning from hasName -> noname.
122 ST->remove(this);
123 Name.clear();
124 }
125 } else { // Transitioning from noname -> hasName.
126 Name = name;
127 ST->insert(this);
128 }
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000129}
130
Chris Lattner9f158122003-08-29 05:09:37 +0000131// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
132// except that it doesn't have all of the asserts. The asserts fail because we
133// are half-way done resolving types, which causes some types to exist as two
134// different Type*'s at the same time. This is a sledgehammer to work around
135// this problem.
136//
137void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner4947e672005-02-01 01:24:21 +0000138 while (!use_empty()) {
139 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000140 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
141 // constant!
Chris Lattner079edeb2003-10-16 16:53:07 +0000142 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Reid Spencerbbddbf32004-07-18 00:01:50 +0000143 if (!isa<GlobalValue>(C))
144 C->replaceUsesOfWithOnConstant(this, New, true);
145 else
146 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000147 } else {
Chris Lattner079edeb2003-10-16 16:53:07 +0000148 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000149 }
150 }
151}
152
Chris Lattner079edeb2003-10-16 16:53:07 +0000153void Value::replaceAllUsesWith(Value *New) {
154 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
155 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
156 assert(New->getType() == getType() &&
157 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000158
Chris Lattner079edeb2003-10-16 16:53:07 +0000159 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000160}
161
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162//===----------------------------------------------------------------------===//
163// User Class
164//===----------------------------------------------------------------------===//
165
Chris Lattner2f7c9632001-06-06 20:29:01 +0000166// replaceUsesOfWith - Replaces all references to the "From" definition with
167// references to the "To" definition.
168//
169void User::replaceUsesOfWith(Value *From, Value *To) {
170 if (From == To) return; // Duh what?
171
Reid Spencerbbddbf32004-07-18 00:01:50 +0000172 assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000173 "Cannot call User::replaceUsesofWith on a constant!");
174
Chris Lattnera073acb2001-07-07 08:36:50 +0000175 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
176 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000177 // The side effects of this setOperand call include linking to
178 // "To", adding "this" to the uses list of To, and
179 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000180 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000181 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000182}
Reid Spencerbbddbf32004-07-18 00:01:50 +0000183