blob: 35ec9bef9125c7dee12386c03ee61e8a5304b1a8 [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//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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//
Chris Lattner90234f32009-03-31 22:11:05 +000010// This file implements the Value, ValueHandle, and User classes.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Owen Andersone8f21852009-08-18 18:28:58 +000014#include "LLVMContextImpl.h"
Chris Lattner2c6abea2002-10-09 23:12:59 +000015#include "llvm/Constant.h"
Anton Korobeynikov82c02b22008-05-06 22:52:30 +000016#include "llvm/Constants.h"
Chris Lattnercdb9bfc2005-03-05 19:51:50 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/InstrTypes.h"
Devang Patel1f00b532008-02-21 01:54:02 +000019#include "llvm/Instructions.h"
Dan Gohman1d548d82009-07-17 22:25:10 +000020#include "llvm/Operator.h"
Chris Lattnercdb9bfc2005-03-05 19:51:50 +000021#include "llvm/Module.h"
Devang Patela4f43fb2009-07-28 21:49:47 +000022#include "llvm/Metadata.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000023#include "llvm/ValueSymbolTable.h"
Daniel Dunbar4975db62009-07-25 04:41:11 +000024#include "llvm/ADT/SmallString.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000025#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000026#include "llvm/Support/ErrorHandling.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000027#include "llvm/Support/LeakDetector.h"
Chris Lattner90234f32009-03-31 22:11:05 +000028#include "llvm/Support/ManagedStatic.h"
29#include "llvm/Support/ValueHandle.h"
Owen Anderson4b660a82009-06-17 17:36:57 +000030#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000031#include "llvm/System/Threading.h"
Chris Lattner90234f32009-03-31 22:11:05 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000033#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000034using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000035
Chris Lattner2f7c9632001-06-06 20:29:01 +000036//===----------------------------------------------------------------------===//
37// Value Class
38//===----------------------------------------------------------------------===//
39
Chris Lattner25450e32001-12-13 00:41:27 +000040static inline const Type *checkType(const Type *Ty) {
41 assert(Ty && "Value defined with a null type: Error!");
42 return Ty;
43}
44
Chris Lattner32ab6432007-02-12 05:18:08 +000045Value::Value(const Type *ty, unsigned scid)
Benjamin Kramer5ff90ed2009-09-17 14:51:57 +000046 : SubclassID(scid), HasValueHandle(0), HasMetadata(0),
47 SubclassOptionalData(0), SubclassData(0), VTy(checkType(ty)),
Gabor Greif715b9d22008-09-19 15:13:20 +000048 UseList(0), Name(0) {
Devang Patelad582fc2008-02-21 02:14:01 +000049 if (isa<CallInst>(this) || isa<InvokeInst>(this))
Owen Anderson55f1c092009-08-13 21:58:54 +000050 assert((VTy->isFirstClassType() ||
51 VTy == Type::getVoidTy(ty->getContext()) ||
Evan Chenga05c07e2008-07-24 00:08:56 +000052 isa<OpaqueType>(ty) || VTy->getTypeID() == Type::StructTyID) &&
Devang Patel1f00b532008-02-21 01:54:02 +000053 "invalid CallInst type!");
54 else if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Owen Anderson55f1c092009-08-13 21:58:54 +000055 assert((VTy->isFirstClassType() ||
56 VTy == Type::getVoidTy(ty->getContext()) ||
Chris Lattner9df9afd2004-07-07 18:07:46 +000057 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000058 "Cannot create non-first-class values except for constants!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000059}
60
Gordon Henriksen14a55692007-12-10 02:14:30 +000061Value::~Value() {
Devang Pateld5497a4b2009-09-16 18:09:00 +000062 if (HasMetadata) {
63 LLVMContext &Context = getContext();
64 Context.pImpl->TheMetadata.ValueIsDeleted(this);
65 }
Daniel Dunbar6058b512009-09-20 04:03:34 +000066
Chris Lattner90234f32009-03-31 22:11:05 +000067 // Notify all ValueHandles (if present) that this value is going away.
68 if (HasValueHandle)
69 ValueHandleBase::ValueIsDeleted(this);
Daniel Dunbar6058b512009-09-20 04:03:34 +000070
Chris Lattner2f7c9632001-06-06 20:29:01 +000071#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000072 // Check to make sure that there are no uses of this value that are still
73 // around when the value is destroyed. If there are, then we have a dangling
74 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +000075 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +000076 // a <badref>
77 //
Gordon Henriksen14a55692007-12-10 02:14:30 +000078 if (!use_empty()) {
Daniel Dunbarb99eac82009-07-24 10:05:20 +000079 errs() << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
Gordon Henriksen14a55692007-12-10 02:14:30 +000080 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
Daniel Dunbarb99eac82009-07-24 10:05:20 +000081 errs() << "Use still stuck around after Def is destroyed:"
Bill Wendling6a462f12006-11-17 08:03:48 +000082 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000083 }
84#endif
Gordon Henriksen14a55692007-12-10 02:14:30 +000085 assert(use_empty() && "Uses remain when a value is destroyed!");
Chris Lattner694285c2009-08-04 23:07:12 +000086
87 // If this value is named, destroy the name. This should not be in a symtab
88 // at this point.
89 if (Name)
90 Name->Destroy();
Daniel Dunbar6058b512009-09-20 04:03:34 +000091
Chris Lattner694285c2009-08-04 23:07:12 +000092 // There should be no uses of this object anymore, remove it.
93 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000094}
95
Chris Lattner4947e672005-02-01 01:24:21 +000096/// hasNUses - Return true if this Value has exactly N users.
97///
98bool Value::hasNUses(unsigned N) const {
99 use_const_iterator UI = use_begin(), E = use_end();
100
101 for (; N; --N, ++UI)
102 if (UI == E) return false; // Too few.
103 return UI == E;
104}
105
Chris Lattnerd36552f2005-02-23 16:51:11 +0000106/// hasNUsesOrMore - Return true if this value has N users or more. This is
107/// logically equivalent to getNumUses() >= N.
108///
109bool Value::hasNUsesOrMore(unsigned N) const {
110 use_const_iterator UI = use_begin(), E = use_end();
111
112 for (; N; --N, ++UI)
113 if (UI == E) return false; // Too few.
114
115 return true;
116}
117
Evan Cheng89553cc2008-06-12 21:15:59 +0000118/// isUsedInBasicBlock - Return true if this value is used in the specified
119/// basic block.
Bill Wendling0c374212008-09-25 22:42:01 +0000120bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
Evan Cheng89553cc2008-06-12 21:15:59 +0000121 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) {
122 const Instruction *User = dyn_cast<Instruction>(*I);
123 if (User && User->getParent() == BB)
124 return true;
125 }
126 return false;
127}
128
Chris Lattnerd36552f2005-02-23 16:51:11 +0000129
Chris Lattner4947e672005-02-01 01:24:21 +0000130/// getNumUses - This method computes the number of uses of this Value. This
131/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
132/// values.
133unsigned Value::getNumUses() const {
134 return (unsigned)std::distance(use_begin(), use_end());
135}
136
Chris Lattnerb6250822007-02-11 00:37:27 +0000137static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Chris Lattner569c8ac2007-02-11 19:12:18 +0000138 ST = 0;
Chris Lattnerb6250822007-02-11 00:37:27 +0000139 if (Instruction *I = dyn_cast<Instruction>(V)) {
140 if (BasicBlock *P = I->getParent())
141 if (Function *PP = P->getParent())
142 ST = &PP->getValueSymbolTable();
143 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Daniel Dunbar6058b512009-09-20 04:03:34 +0000144 if (Function *P = BB->getParent())
Chris Lattnerb6250822007-02-11 00:37:27 +0000145 ST = &P->getValueSymbolTable();
146 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Daniel Dunbar6058b512009-09-20 04:03:34 +0000147 if (Module *P = GV->getParent())
Chris Lattnerb6250822007-02-11 00:37:27 +0000148 ST = &P->getValueSymbolTable();
149 } else if (Argument *A = dyn_cast<Argument>(V)) {
Daniel Dunbar6058b512009-09-20 04:03:34 +0000150 if (Function *P = A->getParent())
Chris Lattnerb6250822007-02-11 00:37:27 +0000151 ST = &P->getValueSymbolTable();
Devang Patel18dfdc92009-07-29 17:16:17 +0000152 } else if (NamedMDNode *N = dyn_cast<NamedMDNode>(V)) {
153 if (Module *P = N->getParent()) {
154 ST = &P->getValueSymbolTable();
155 }
Devang Patel7428d8a2009-07-22 17:43:22 +0000156 } else if (isa<MDString>(V))
157 return true;
158 else {
Chris Lattnerb6250822007-02-11 00:37:27 +0000159 assert(isa<Constant>(V) && "Unknown value type!");
160 return true; // no name is setable for this.
161 }
162 return false;
163}
Chris Lattner2c6abea2002-10-09 23:12:59 +0000164
Daniel Dunbar32285942009-07-26 00:51:56 +0000165StringRef Value::getName() const {
Daniel Dunbar7cc8f7e2009-07-26 09:22:02 +0000166 // Make sure the empty string is still a C string. For historical reasons,
167 // some clients want to call .data() on the result and expect it to be null
168 // terminated.
169 if (!Name) return StringRef("", 0);
Daniel Dunbar32285942009-07-26 00:51:56 +0000170 return Name->getKey();
Chris Lattner5109a882007-08-10 15:34:35 +0000171}
172
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000173std::string Value::getNameStr() const {
Daniel Dunbar987ec562009-07-26 00:17:14 +0000174 return getName().str();
Chris Lattner32ab6432007-02-12 05:18:08 +0000175}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000176
Daniel Dunbard786b512009-07-26 00:34:27 +0000177void Value::setName(const Twine &NewName) {
Daniel Dunbarcb13b482009-08-19 23:37:23 +0000178 // Fast path for common IRBuilder case of setName("") when there is no name.
179 if (NewName.isTriviallyEmpty() && !hasName())
180 return;
181
Daniel Dunbaracf0b252009-08-19 05:08:06 +0000182 SmallString<256> NameData;
Daniel Dunbard786b512009-07-26 00:34:27 +0000183 NewName.toVector(NameData);
Chris Lattner1a5de582007-02-12 18:52:59 +0000184
Daniel Dunbard786b512009-07-26 00:34:27 +0000185 const char *NameStr = NameData.data();
186 unsigned NameLen = NameData.size();
187
Daniel Dunbara32c9992009-07-26 00:42:33 +0000188 // Name isn't changing?
189 if (getName() == StringRef(NameStr, NameLen))
190 return;
191
Owen Anderson55f1c092009-08-13 21:58:54 +0000192 assert(getType() != Type::getVoidTy(getContext()) &&
193 "Cannot assign a name to void values!");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000194
Chris Lattnerffb37782005-03-06 02:14:28 +0000195 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000196 ValueSymbolTable *ST;
197 if (getSymTab(this, ST))
198 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000199
Chris Lattner32ab6432007-02-12 05:18:08 +0000200 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000201 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000202 // Free the name for this value.
203 Name->Destroy();
204 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000205 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000206 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000207
Daniel Dunbara32c9992009-07-26 00:42:33 +0000208 if (Name)
Chris Lattner1a5de582007-02-12 18:52:59 +0000209 Name->Destroy();
Daniel Dunbar6058b512009-09-20 04:03:34 +0000210
Chris Lattner1a5de582007-02-12 18:52:59 +0000211 // NOTE: Could optimize for the case the name is shrinking to not deallocate
212 // then reallocated.
Daniel Dunbar6058b512009-09-20 04:03:34 +0000213
Chris Lattner1a5de582007-02-12 18:52:59 +0000214 // Create the new name.
215 Name = ValueName::Create(NameStr, NameStr+NameLen);
216 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000217 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000218 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000219
Chris Lattner32ab6432007-02-12 05:18:08 +0000220 // NOTE: Could optimize for the case the name is shrinking to not deallocate
221 // then reallocated.
222 if (hasName()) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000223 // Remove old name.
224 ST->removeValueName(Name);
225 Name->Destroy();
226 Name = 0;
227
Chris Lattner1a5de582007-02-12 18:52:59 +0000228 if (NameLen == 0)
229 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000230 }
231
232 // Name is changing to something new.
Daniel Dunbarf01154c2009-07-23 18:50:53 +0000233 Name = ST->createValueName(StringRef(NameStr, NameLen), this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000234}
235
Chris Lattner1a5de582007-02-12 18:52:59 +0000236
Chris Lattnerb6250822007-02-11 00:37:27 +0000237/// takeName - transfer the name from V to this value, setting V's name to
Daniel Dunbar6058b512009-09-20 04:03:34 +0000238/// empty. It is an error to call V->takeName(V).
Chris Lattnerb6250822007-02-11 00:37:27 +0000239void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000240 ValueSymbolTable *ST = 0;
241 // If this value has a name, drop it.
242 if (hasName()) {
243 // Get the symtab this is in.
244 if (getSymTab(this, ST)) {
245 // We can't set a name on this value, but we need to clear V's name if
246 // it has one.
Daniel Dunbard786b512009-07-26 00:34:27 +0000247 if (V->hasName()) V->setName("");
Chris Lattnercf835ff2007-02-15 20:01:43 +0000248 return; // Cannot set a name on this value (e.g. constant).
249 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000250
Chris Lattnercf835ff2007-02-15 20:01:43 +0000251 // Remove old name.
252 if (ST)
253 ST->removeValueName(Name);
254 Name->Destroy();
255 Name = 0;
Daniel Dunbar6058b512009-09-20 04:03:34 +0000256 }
257
Chris Lattnercf835ff2007-02-15 20:01:43 +0000258 // Now we know that this has no name.
Daniel Dunbar6058b512009-09-20 04:03:34 +0000259
Chris Lattnercf835ff2007-02-15 20:01:43 +0000260 // If V has no name either, we're done.
261 if (!V->hasName()) return;
Daniel Dunbar6058b512009-09-20 04:03:34 +0000262
Chris Lattnercf835ff2007-02-15 20:01:43 +0000263 // Get this's symtab if we didn't before.
264 if (!ST) {
265 if (getSymTab(this, ST)) {
266 // Clear V's name.
Daniel Dunbard786b512009-07-26 00:34:27 +0000267 V->setName("");
Chris Lattnercf835ff2007-02-15 20:01:43 +0000268 return; // Cannot set a name on this value (e.g. constant).
269 }
270 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000271
Chris Lattnercf835ff2007-02-15 20:01:43 +0000272 // Get V's ST, this should always succed, because V has a name.
273 ValueSymbolTable *VST;
274 bool Failure = getSymTab(V, VST);
Chris Lattner106b0462008-06-21 19:47:03 +0000275 assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure;
Daniel Dunbar6058b512009-09-20 04:03:34 +0000276
Chris Lattnercf835ff2007-02-15 20:01:43 +0000277 // If these values are both in the same symtab, we can do this very fast.
278 // This works even if both values have no symtab yet.
279 if (ST == VST) {
280 // Take the name!
281 Name = V->Name;
282 V->Name = 0;
283 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000284 return;
285 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000286
Chris Lattnercf835ff2007-02-15 20:01:43 +0000287 // Otherwise, things are slightly more complex. Remove V's name from VST and
288 // then reinsert it into ST.
Daniel Dunbar6058b512009-09-20 04:03:34 +0000289
Chris Lattnercf835ff2007-02-15 20:01:43 +0000290 if (VST)
291 VST->removeValueName(V->Name);
292 Name = V->Name;
293 V->Name = 0;
294 Name->setValue(this);
Daniel Dunbar6058b512009-09-20 04:03:34 +0000295
Chris Lattnercf835ff2007-02-15 20:01:43 +0000296 if (ST)
297 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000298}
299
300
Chris Lattner9f158122003-08-29 05:09:37 +0000301// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
302// except that it doesn't have all of the asserts. The asserts fail because we
303// are half-way done resolving types, which causes some types to exist as two
304// different Type*'s at the same time. This is a sledgehammer to work around
305// this problem.
306//
307void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner90234f32009-03-31 22:11:05 +0000308 // Notify all ValueHandles (if present) that this value is going away.
309 if (HasValueHandle)
310 ValueHandleBase::ValueIsRAUWd(this, New);
Devang Patele6f26a72009-10-13 17:00:54 +0000311 if (HasMetadata) {
312 LLVMContext &Context = getContext();
313 Context.pImpl->TheMetadata.ValueIsRAUWd(this, New);
314 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000315
Chris Lattner4947e672005-02-01 01:24:21 +0000316 while (!use_empty()) {
Gabor Greif715b9d22008-09-19 15:13:20 +0000317 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000318 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000319 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000320 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000321 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000322 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000323 continue;
324 }
Chris Lattner9f158122003-08-29 05:09:37 +0000325 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000326
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000327 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000328 }
329}
330
Chris Lattner079edeb2003-10-16 16:53:07 +0000331void Value::replaceAllUsesWith(Value *New) {
332 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
333 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
334 assert(New->getType() == getType() &&
335 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000336
Chris Lattner079edeb2003-10-16 16:53:07 +0000337 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000338}
339
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000340Value *Value::stripPointerCasts() {
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000341 if (!isa<PointerType>(getType()))
342 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000343 Value *V = this;
344 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000345 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Duncan Sandse59fa782008-12-29 21:06:19 +0000346 if (!GEP->hasAllZeroIndices())
347 return V;
Dan Gohmane1019db2009-07-17 23:55:56 +0000348 V = GEP->getPointerOperand();
349 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
350 V = cast<Operator>(V)->getOperand(0);
Dan Gohman4ce5ade2009-08-27 17:55:13 +0000351 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
352 if (GA->mayBeOverridden())
353 return V;
354 V = GA->getAliasee();
Duncan Sandse59fa782008-12-29 21:06:19 +0000355 } else {
356 return V;
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000357 }
Duncan Sandse59fa782008-12-29 21:06:19 +0000358 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
359 } while (1);
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000360}
361
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000362Value *Value::getUnderlyingObject() {
363 if (!isa<PointerType>(getType()))
364 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000365 Value *V = this;
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000366 unsigned MaxLookup = 6;
Duncan Sandse59fa782008-12-29 21:06:19 +0000367 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000368 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Dan Gohmane1019db2009-07-17 23:55:56 +0000369 V = GEP->getPointerOperand();
370 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
371 V = cast<Operator>(V)->getOperand(0);
Dan Gohman4ce5ade2009-08-27 17:55:13 +0000372 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
373 if (GA->mayBeOverridden())
374 return V;
375 V = GA->getAliasee();
Duncan Sandse59fa782008-12-29 21:06:19 +0000376 } else {
377 return V;
378 }
379 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000380 } while (--MaxLookup);
381 return V;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000382}
383
Chris Lattner027d7262008-12-02 18:33:11 +0000384/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
Chris Lattner9c1b5022008-12-02 07:16:45 +0000385/// return the value in the PHI node corresponding to PredBB. If not, return
386/// ourself. This is useful if you want to know the value something has in a
387/// predecessor block.
Daniel Dunbar6058b512009-09-20 04:03:34 +0000388Value *Value::DoPHITranslation(const BasicBlock *CurBB,
Chris Lattner9c1b5022008-12-02 07:16:45 +0000389 const BasicBlock *PredBB) {
390 PHINode *PN = dyn_cast<PHINode>(this);
391 if (PN && PN->getParent() == CurBB)
392 return PN->getIncomingValueForBlock(PredBB);
393 return this;
394}
395
Owen Anderson47db9412009-07-22 00:24:57 +0000396LLVMContext &Value::getContext() const { return VTy->getContext(); }
397
Chris Lattner90234f32009-03-31 22:11:05 +0000398//===----------------------------------------------------------------------===//
399// ValueHandleBase Class
400//===----------------------------------------------------------------------===//
401
Dan Gohman3f025952009-05-04 17:25:21 +0000402/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
403/// List is known to point into the existing use list.
Chris Lattner90234f32009-03-31 22:11:05 +0000404void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
405 assert(List && "Handle list is null?");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000406
Chris Lattner90234f32009-03-31 22:11:05 +0000407 // Splice ourselves into the list.
408 Next = *List;
409 *List = this;
410 setPrevPtr(List);
411 if (Next) {
412 Next->setPrevPtr(&Next);
413 assert(VP == Next->VP && "Added to wrong list?");
414 }
415}
416
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000417void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
418 assert(List && "Must insert after existing node");
419
420 Next = List->Next;
421 setPrevPtr(&List->Next);
422 List->Next = this;
423 if (Next)
424 Next->setPrevPtr(&Next);
425}
426
Chris Lattner90234f32009-03-31 22:11:05 +0000427/// AddToUseList - Add this ValueHandle to the use list for VP.
428void ValueHandleBase::AddToUseList() {
429 assert(VP && "Null pointer doesn't have a use list!");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000430
Owen Andersone8f21852009-08-18 18:28:58 +0000431 LLVMContextImpl *pImpl = VP->getContext().pImpl;
Daniel Dunbar6058b512009-09-20 04:03:34 +0000432
Chris Lattner90234f32009-03-31 22:11:05 +0000433 if (VP->HasValueHandle) {
434 // If this value already has a ValueHandle, then it must be in the
435 // ValueHandles map already.
Owen Andersone8f21852009-08-18 18:28:58 +0000436 ValueHandleBase *&Entry = pImpl->ValueHandles[VP];
Chris Lattner90234f32009-03-31 22:11:05 +0000437 assert(Entry != 0 && "Value doesn't have any handles?");
Owen Anderson4b660a82009-06-17 17:36:57 +0000438 AddToExistingUseList(&Entry);
Owen Anderson4b660a82009-06-17 17:36:57 +0000439 return;
Chris Lattner90234f32009-03-31 22:11:05 +0000440 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000441
Chris Lattner90234f32009-03-31 22:11:05 +0000442 // Ok, it doesn't have any handles yet, so we must insert it into the
443 // DenseMap. However, doing this insertion could cause the DenseMap to
444 // reallocate itself, which would invalidate all of the PrevP pointers that
445 // point into the old table. Handle this by checking for reallocation and
446 // updating the stale pointers only if needed.
Owen Andersone8f21852009-08-18 18:28:58 +0000447 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
Chris Lattner90234f32009-03-31 22:11:05 +0000448 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
Daniel Dunbar6058b512009-09-20 04:03:34 +0000449
Chris Lattner90234f32009-03-31 22:11:05 +0000450 ValueHandleBase *&Entry = Handles[VP];
451 assert(Entry == 0 && "Value really did already have handles?");
452 AddToExistingUseList(&Entry);
Dan Gohman3f025952009-05-04 17:25:21 +0000453 VP->HasValueHandle = true;
Daniel Dunbar6058b512009-09-20 04:03:34 +0000454
Chris Lattner90234f32009-03-31 22:11:05 +0000455 // If reallocation didn't happen or if this was the first insertion, don't
456 // walk the table.
Daniel Dunbar6058b512009-09-20 04:03:34 +0000457 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
Owen Anderson4b660a82009-06-17 17:36:57 +0000458 Handles.size() == 1) {
Chris Lattner90234f32009-03-31 22:11:05 +0000459 return;
Owen Anderson4b660a82009-06-17 17:36:57 +0000460 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000461
Chris Lattner90234f32009-03-31 22:11:05 +0000462 // Okay, reallocation did happen. Fix the Prev Pointers.
Owen Andersone8f21852009-08-18 18:28:58 +0000463 for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
464 E = Handles.end(); I != E; ++I) {
Chris Lattner90234f32009-03-31 22:11:05 +0000465 assert(I->second && I->first == I->second->VP && "List invariant broken!");
466 I->second->setPrevPtr(&I->second);
467 }
468}
469
470/// RemoveFromUseList - Remove this ValueHandle from its current use list.
471void ValueHandleBase::RemoveFromUseList() {
472 assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
473
474 // Unlink this from its use list.
475 ValueHandleBase **PrevPtr = getPrevPtr();
476 assert(*PrevPtr == this && "List invariant broken");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000477
Chris Lattner90234f32009-03-31 22:11:05 +0000478 *PrevPtr = Next;
479 if (Next) {
480 assert(Next->getPrevPtr() == &Next && "List invariant broken");
481 Next->setPrevPtr(PrevPtr);
482 return;
483 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000484
Chris Lattner90234f32009-03-31 22:11:05 +0000485 // If the Next pointer was null, then it is possible that this was the last
486 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
487 // map.
Owen Andersone8f21852009-08-18 18:28:58 +0000488 LLVMContextImpl *pImpl = VP->getContext().pImpl;
489 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
Chris Lattner90234f32009-03-31 22:11:05 +0000490 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
491 Handles.erase(VP);
492 VP->HasValueHandle = false;
493 }
494}
495
496
497void ValueHandleBase::ValueIsDeleted(Value *V) {
498 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
499
500 // Get the linked list base, which is guaranteed to exist since the
501 // HasValueHandle flag is set.
Owen Andersone8f21852009-08-18 18:28:58 +0000502 LLVMContextImpl *pImpl = V->getContext().pImpl;
503 ValueHandleBase *Entry = pImpl->ValueHandles[V];
Chris Lattner90234f32009-03-31 22:11:05 +0000504 assert(Entry && "Value bit set but no entries exist");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000505
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000506 // We use a local ValueHandleBase as an iterator so that
507 // ValueHandles can add and remove themselves from the list without
508 // breaking our iteration. This is not really an AssertingVH; we
509 // just have to give ValueHandleBase some kind.
510 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
511 Iterator.RemoveFromUseList();
512 Iterator.AddToExistingUseListAfter(Entry);
513 assert(Entry->Next == &Iterator && "Loop invariant broken.");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000514
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000515 switch (Entry->getKind()) {
Chris Lattner90234f32009-03-31 22:11:05 +0000516 case Assert:
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000517 break;
Daniel Dunbar70d4fb02009-09-22 02:02:33 +0000518 case Tracking:
519 // Mark that this value has been deleted by setting it to an invalid Value
520 // pointer.
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000521 Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey());
Daniel Dunbar70d4fb02009-09-22 02:02:33 +0000522 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000523 case Weak:
524 // Weak just goes to null, which will unlink it from the list.
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000525 Entry->operator=(0);
Chris Lattner90234f32009-03-31 22:11:05 +0000526 break;
527 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000528 // Forward to the subclass's implementation.
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000529 static_cast<CallbackVH*>(Entry)->deleted();
Dan Gohman745ad442009-05-02 21:10:48 +0000530 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000531 }
532 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000533
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000534 // All callbacks, weak references, and assertingVHs should be dropped by now.
535 if (V->HasValueHandle) {
536#ifndef NDEBUG // Only in +Asserts mode...
537 errs() << "While deleting: " << *V->getType() << " %" << V->getNameStr()
538 << "\n";
539 if (pImpl->ValueHandles[V]->getKind() == Assert)
540 llvm_unreachable("An asserting value handle still pointed to this"
541 " value!");
542
543#endif
544 llvm_unreachable("All references to V were not removed?");
545 }
Chris Lattner90234f32009-03-31 22:11:05 +0000546}
547
548
549void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
550 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
551 assert(Old != New && "Changing value into itself!");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000552
Chris Lattner90234f32009-03-31 22:11:05 +0000553 // Get the linked list base, which is guaranteed to exist since the
554 // HasValueHandle flag is set.
Owen Andersone8f21852009-08-18 18:28:58 +0000555 LLVMContextImpl *pImpl = Old->getContext().pImpl;
556 ValueHandleBase *Entry = pImpl->ValueHandles[Old];
557
Chris Lattner90234f32009-03-31 22:11:05 +0000558 assert(Entry && "Value bit set but no entries exist");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000559
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000560 // We use a local ValueHandleBase as an iterator so that
561 // ValueHandles can add and remove themselves from the list without
562 // breaking our iteration. This is not really an AssertingVH; we
563 // just have to give ValueHandleBase some kind.
564 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
565 Iterator.RemoveFromUseList();
566 Iterator.AddToExistingUseListAfter(Entry);
567 assert(Entry->Next == &Iterator && "Loop invariant broken.");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000568
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000569 switch (Entry->getKind()) {
Chris Lattner90234f32009-03-31 22:11:05 +0000570 case Assert:
571 // Asserting handle does not follow RAUW implicitly.
572 break;
Daniel Dunbar70d4fb02009-09-22 02:02:33 +0000573 case Tracking:
574 // Tracking goes to new value like a WeakVH. Note that this may make it
575 // something incompatible with its templated type. We don't want to have a
576 // virtual (or inline) interface to handle this though, so instead we make
Daniel Dunbar86707c92009-09-22 10:30:34 +0000577 // the TrackingVH accessors guarantee that a client never sees this value.
Daniel Dunbar70d4fb02009-09-22 02:02:33 +0000578
579 // FALLTHROUGH
Chris Lattner90234f32009-03-31 22:11:05 +0000580 case Weak:
581 // Weak goes to the new value, which will unlink it from Old's list.
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000582 Entry->operator=(New);
Chris Lattner90234f32009-03-31 22:11:05 +0000583 break;
584 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000585 // Forward to the subclass's implementation.
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000586 static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
Dan Gohman745ad442009-05-02 21:10:48 +0000587 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000588 }
589 }
590}
591
Dan Gohman745ad442009-05-02 21:10:48 +0000592/// ~CallbackVH. Empty, but defined here to avoid emitting the vtable
593/// more than once.
594CallbackVH::~CallbackVH() {}
595
Chris Lattner9c1b5022008-12-02 07:16:45 +0000596
Chris Lattner2f7c9632001-06-06 20:29:01 +0000597//===----------------------------------------------------------------------===//
598// User Class
599//===----------------------------------------------------------------------===//
600
Chris Lattner2f7c9632001-06-06 20:29:01 +0000601// replaceUsesOfWith - Replaces all references to the "From" definition with
602// references to the "To" definition.
603//
604void User::replaceUsesOfWith(Value *From, Value *To) {
605 if (From == To) return; // Duh what?
606
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000607 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
Dan Gohman5211b012009-08-11 15:53:15 +0000608 "Cannot call User::replaceUsesOfWith on a constant!");
Chris Lattner2c6abea2002-10-09 23:12:59 +0000609
Chris Lattnera073acb2001-07-07 08:36:50 +0000610 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
611 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000612 // The side effects of this setOperand call include linking to
613 // "To", adding "this" to the uses list of To, and
614 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000615 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000616 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000617}