blob: cc9c066f38a83fdae3912b35534894afb4cd06cf [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"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000022#include "llvm/ValueSymbolTable.h"
Daniel Dunbar4975db62009-07-25 04:41:11 +000023#include "llvm/ADT/SmallString.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000024#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000025#include "llvm/Support/ErrorHandling.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/Support/LeakDetector.h"
Chris Lattner90234f32009-03-31 22:11:05 +000027#include "llvm/Support/ManagedStatic.h"
28#include "llvm/Support/ValueHandle.h"
29#include "llvm/ADT/DenseMap.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000030#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000031using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000032
Chris Lattner2f7c9632001-06-06 20:29:01 +000033//===----------------------------------------------------------------------===//
34// Value Class
35//===----------------------------------------------------------------------===//
36
Chris Lattner25450e32001-12-13 00:41:27 +000037static inline const Type *checkType(const Type *Ty) {
38 assert(Ty && "Value defined with a null type: Error!");
39 return Ty;
40}
41
Chris Lattner32ab6432007-02-12 05:18:08 +000042Value::Value(const Type *ty, unsigned scid)
Chris Lattner2f2aa2b2009-12-28 23:41:32 +000043 : SubclassID(scid), HasValueHandle(0),
Benjamin Kramer5ff90ed2009-09-17 14:51:57 +000044 SubclassOptionalData(0), SubclassData(0), VTy(checkType(ty)),
Gabor Greif715b9d22008-09-19 15:13:20 +000045 UseList(0), Name(0) {
Devang Patelad582fc2008-02-21 02:14:01 +000046 if (isa<CallInst>(this) || isa<InvokeInst>(this))
Owen Anderson55f1c092009-08-13 21:58:54 +000047 assert((VTy->isFirstClassType() ||
48 VTy == Type::getVoidTy(ty->getContext()) ||
Evan Chenga05c07e2008-07-24 00:08:56 +000049 isa<OpaqueType>(ty) || VTy->getTypeID() == Type::StructTyID) &&
Devang Patel1f00b532008-02-21 01:54:02 +000050 "invalid CallInst type!");
51 else if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Owen Anderson55f1c092009-08-13 21:58:54 +000052 assert((VTy->isFirstClassType() ||
53 VTy == Type::getVoidTy(ty->getContext()) ||
Chris Lattner9df9afd2004-07-07 18:07:46 +000054 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000055 "Cannot create non-first-class values except for constants!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000056}
57
Gordon Henriksen14a55692007-12-10 02:14:30 +000058Value::~Value() {
Chris Lattner90234f32009-03-31 22:11:05 +000059 // Notify all ValueHandles (if present) that this value is going away.
60 if (HasValueHandle)
61 ValueHandleBase::ValueIsDeleted(this);
Daniel Dunbar6058b512009-09-20 04:03:34 +000062
Chris Lattner2f7c9632001-06-06 20:29:01 +000063#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000064 // Check to make sure that there are no uses of this value that are still
65 // around when the value is destroyed. If there are, then we have a dangling
66 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +000067 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +000068 // a <badref>
69 //
Gordon Henriksen14a55692007-12-10 02:14:30 +000070 if (!use_empty()) {
David Greene3f907a92010-01-05 01:30:09 +000071 dbgs() << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
Gordon Henriksen14a55692007-12-10 02:14:30 +000072 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
David Greene3f907a92010-01-05 01:30:09 +000073 dbgs() << "Use still stuck around after Def is destroyed:"
Bill Wendling6a462f12006-11-17 08:03:48 +000074 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000075 }
76#endif
Gordon Henriksen14a55692007-12-10 02:14:30 +000077 assert(use_empty() && "Uses remain when a value is destroyed!");
Chris Lattner694285c2009-08-04 23:07:12 +000078
79 // If this value is named, destroy the name. This should not be in a symtab
80 // at this point.
81 if (Name)
82 Name->Destroy();
Daniel Dunbar6058b512009-09-20 04:03:34 +000083
Chris Lattner694285c2009-08-04 23:07:12 +000084 // There should be no uses of this object anymore, remove it.
85 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000086}
87
Chris Lattner4947e672005-02-01 01:24:21 +000088/// hasNUses - Return true if this Value has exactly N users.
89///
90bool Value::hasNUses(unsigned N) const {
91 use_const_iterator UI = use_begin(), E = use_end();
92
93 for (; N; --N, ++UI)
94 if (UI == E) return false; // Too few.
95 return UI == E;
96}
97
Chris Lattnerd36552f2005-02-23 16:51:11 +000098/// hasNUsesOrMore - Return true if this value has N users or more. This is
99/// logically equivalent to getNumUses() >= N.
100///
101bool Value::hasNUsesOrMore(unsigned N) const {
102 use_const_iterator UI = use_begin(), E = use_end();
103
104 for (; N; --N, ++UI)
105 if (UI == E) return false; // Too few.
106
107 return true;
108}
109
Evan Cheng89553cc2008-06-12 21:15:59 +0000110/// isUsedInBasicBlock - Return true if this value is used in the specified
111/// basic block.
Bill Wendling0c374212008-09-25 22:42:01 +0000112bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
Evan Cheng89553cc2008-06-12 21:15:59 +0000113 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) {
114 const Instruction *User = dyn_cast<Instruction>(*I);
115 if (User && User->getParent() == BB)
116 return true;
117 }
118 return false;
119}
120
Chris Lattnerd36552f2005-02-23 16:51:11 +0000121
Chris Lattner4947e672005-02-01 01:24:21 +0000122/// getNumUses - This method computes the number of uses of this Value. This
123/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
124/// values.
125unsigned Value::getNumUses() const {
126 return (unsigned)std::distance(use_begin(), use_end());
127}
128
Chris Lattnerb6250822007-02-11 00:37:27 +0000129static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Chris Lattner569c8ac2007-02-11 19:12:18 +0000130 ST = 0;
Chris Lattnerb6250822007-02-11 00:37:27 +0000131 if (Instruction *I = dyn_cast<Instruction>(V)) {
132 if (BasicBlock *P = I->getParent())
133 if (Function *PP = P->getParent())
134 ST = &PP->getValueSymbolTable();
135 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Daniel Dunbar6058b512009-09-20 04:03:34 +0000136 if (Function *P = BB->getParent())
Chris Lattnerb6250822007-02-11 00:37:27 +0000137 ST = &P->getValueSymbolTable();
138 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Daniel Dunbar6058b512009-09-20 04:03:34 +0000139 if (Module *P = GV->getParent())
Chris Lattnerb6250822007-02-11 00:37:27 +0000140 ST = &P->getValueSymbolTable();
141 } else if (Argument *A = dyn_cast<Argument>(V)) {
Daniel Dunbar6058b512009-09-20 04:03:34 +0000142 if (Function *P = A->getParent())
Chris Lattnerb6250822007-02-11 00:37:27 +0000143 ST = &P->getValueSymbolTable();
Devang Patel18dfdc92009-07-29 17:16:17 +0000144 } else if (NamedMDNode *N = dyn_cast<NamedMDNode>(V)) {
145 if (Module *P = N->getParent()) {
146 ST = &P->getValueSymbolTable();
147 }
Devang Patel7428d8a2009-07-22 17:43:22 +0000148 } else if (isa<MDString>(V))
149 return true;
150 else {
Chris Lattnerb6250822007-02-11 00:37:27 +0000151 assert(isa<Constant>(V) && "Unknown value type!");
152 return true; // no name is setable for this.
153 }
154 return false;
155}
Chris Lattner2c6abea2002-10-09 23:12:59 +0000156
Daniel Dunbar32285942009-07-26 00:51:56 +0000157StringRef Value::getName() const {
Daniel Dunbar7cc8f7e2009-07-26 09:22:02 +0000158 // Make sure the empty string is still a C string. For historical reasons,
159 // some clients want to call .data() on the result and expect it to be null
160 // terminated.
161 if (!Name) return StringRef("", 0);
Daniel Dunbar32285942009-07-26 00:51:56 +0000162 return Name->getKey();
Chris Lattner5109a882007-08-10 15:34:35 +0000163}
164
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000165std::string Value::getNameStr() const {
Daniel Dunbar987ec562009-07-26 00:17:14 +0000166 return getName().str();
Chris Lattner32ab6432007-02-12 05:18:08 +0000167}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000168
Daniel Dunbard786b512009-07-26 00:34:27 +0000169void Value::setName(const Twine &NewName) {
Daniel Dunbarcb13b482009-08-19 23:37:23 +0000170 // Fast path for common IRBuilder case of setName("") when there is no name.
171 if (NewName.isTriviallyEmpty() && !hasName())
172 return;
173
Daniel Dunbaracf0b252009-08-19 05:08:06 +0000174 SmallString<256> NameData;
Daniel Dunbard786b512009-07-26 00:34:27 +0000175 NewName.toVector(NameData);
Chris Lattner1a5de582007-02-12 18:52:59 +0000176
Daniel Dunbard786b512009-07-26 00:34:27 +0000177 const char *NameStr = NameData.data();
178 unsigned NameLen = NameData.size();
179
Daniel Dunbara32c9992009-07-26 00:42:33 +0000180 // Name isn't changing?
181 if (getName() == StringRef(NameStr, NameLen))
182 return;
183
Owen Anderson55f1c092009-08-13 21:58:54 +0000184 assert(getType() != Type::getVoidTy(getContext()) &&
185 "Cannot assign a name to void values!");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000186
Chris Lattnerffb37782005-03-06 02:14:28 +0000187 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000188 ValueSymbolTable *ST;
189 if (getSymTab(this, ST))
190 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000191
Chris Lattner32ab6432007-02-12 05:18:08 +0000192 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000193 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000194 // Free the name for this value.
195 Name->Destroy();
196 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000197 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000198 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000199
Daniel Dunbara32c9992009-07-26 00:42:33 +0000200 if (Name)
Chris Lattner1a5de582007-02-12 18:52:59 +0000201 Name->Destroy();
Daniel Dunbar6058b512009-09-20 04:03:34 +0000202
Chris Lattner1a5de582007-02-12 18:52:59 +0000203 // NOTE: Could optimize for the case the name is shrinking to not deallocate
204 // then reallocated.
Daniel Dunbar6058b512009-09-20 04:03:34 +0000205
Chris Lattner1a5de582007-02-12 18:52:59 +0000206 // Create the new name.
207 Name = ValueName::Create(NameStr, NameStr+NameLen);
208 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000209 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000210 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000211
Chris Lattner32ab6432007-02-12 05:18:08 +0000212 // NOTE: Could optimize for the case the name is shrinking to not deallocate
213 // then reallocated.
214 if (hasName()) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000215 // Remove old name.
216 ST->removeValueName(Name);
217 Name->Destroy();
218 Name = 0;
219
Chris Lattner1a5de582007-02-12 18:52:59 +0000220 if (NameLen == 0)
221 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000222 }
223
224 // Name is changing to something new.
Daniel Dunbarf01154c2009-07-23 18:50:53 +0000225 Name = ST->createValueName(StringRef(NameStr, NameLen), this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000226}
227
Chris Lattner1a5de582007-02-12 18:52:59 +0000228
Chris Lattnerb6250822007-02-11 00:37:27 +0000229/// takeName - transfer the name from V to this value, setting V's name to
Daniel Dunbar6058b512009-09-20 04:03:34 +0000230/// empty. It is an error to call V->takeName(V).
Chris Lattnerb6250822007-02-11 00:37:27 +0000231void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000232 ValueSymbolTable *ST = 0;
233 // If this value has a name, drop it.
234 if (hasName()) {
235 // Get the symtab this is in.
236 if (getSymTab(this, ST)) {
237 // We can't set a name on this value, but we need to clear V's name if
238 // it has one.
Daniel Dunbard786b512009-07-26 00:34:27 +0000239 if (V->hasName()) V->setName("");
Chris Lattnercf835ff2007-02-15 20:01:43 +0000240 return; // Cannot set a name on this value (e.g. constant).
241 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000242
Chris Lattnercf835ff2007-02-15 20:01:43 +0000243 // Remove old name.
244 if (ST)
245 ST->removeValueName(Name);
246 Name->Destroy();
247 Name = 0;
Daniel Dunbar6058b512009-09-20 04:03:34 +0000248 }
249
Chris Lattnercf835ff2007-02-15 20:01:43 +0000250 // Now we know that this has no name.
Daniel Dunbar6058b512009-09-20 04:03:34 +0000251
Chris Lattnercf835ff2007-02-15 20:01:43 +0000252 // If V has no name either, we're done.
253 if (!V->hasName()) return;
Daniel Dunbar6058b512009-09-20 04:03:34 +0000254
Chris Lattnercf835ff2007-02-15 20:01:43 +0000255 // Get this's symtab if we didn't before.
256 if (!ST) {
257 if (getSymTab(this, ST)) {
258 // Clear V's name.
Daniel Dunbard786b512009-07-26 00:34:27 +0000259 V->setName("");
Chris Lattnercf835ff2007-02-15 20:01:43 +0000260 return; // Cannot set a name on this value (e.g. constant).
261 }
262 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000263
Chris Lattnercf835ff2007-02-15 20:01:43 +0000264 // Get V's ST, this should always succed, because V has a name.
265 ValueSymbolTable *VST;
266 bool Failure = getSymTab(V, VST);
Chris Lattner106b0462008-06-21 19:47:03 +0000267 assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure;
Daniel Dunbar6058b512009-09-20 04:03:34 +0000268
Chris Lattnercf835ff2007-02-15 20:01:43 +0000269 // If these values are both in the same symtab, we can do this very fast.
270 // This works even if both values have no symtab yet.
271 if (ST == VST) {
272 // Take the name!
273 Name = V->Name;
274 V->Name = 0;
275 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000276 return;
277 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000278
Chris Lattnercf835ff2007-02-15 20:01:43 +0000279 // Otherwise, things are slightly more complex. Remove V's name from VST and
280 // then reinsert it into ST.
Daniel Dunbar6058b512009-09-20 04:03:34 +0000281
Chris Lattnercf835ff2007-02-15 20:01:43 +0000282 if (VST)
283 VST->removeValueName(V->Name);
284 Name = V->Name;
285 V->Name = 0;
286 Name->setValue(this);
Daniel Dunbar6058b512009-09-20 04:03:34 +0000287
Chris Lattnercf835ff2007-02-15 20:01:43 +0000288 if (ST)
289 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000290}
291
292
Chris Lattner9f158122003-08-29 05:09:37 +0000293// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
294// except that it doesn't have all of the asserts. The asserts fail because we
295// are half-way done resolving types, which causes some types to exist as two
296// different Type*'s at the same time. This is a sledgehammer to work around
297// this problem.
298//
299void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner90234f32009-03-31 22:11:05 +0000300 // Notify all ValueHandles (if present) that this value is going away.
301 if (HasValueHandle)
302 ValueHandleBase::ValueIsRAUWd(this, New);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000303
Chris Lattner4947e672005-02-01 01:24:21 +0000304 while (!use_empty()) {
Gabor Greif715b9d22008-09-19 15:13:20 +0000305 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000306 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000307 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000308 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000309 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000310 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000311 continue;
312 }
Chris Lattner9f158122003-08-29 05:09:37 +0000313 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000314
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000315 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000316 }
317}
318
Chris Lattner079edeb2003-10-16 16:53:07 +0000319void Value::replaceAllUsesWith(Value *New) {
320 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
321 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
322 assert(New->getType() == getType() &&
323 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000324
Chris Lattner079edeb2003-10-16 16:53:07 +0000325 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000326}
327
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000328Value *Value::stripPointerCasts() {
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000329 if (!isa<PointerType>(getType()))
330 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000331 Value *V = this;
332 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000333 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Duncan Sandse59fa782008-12-29 21:06:19 +0000334 if (!GEP->hasAllZeroIndices())
335 return V;
Dan Gohmane1019db2009-07-17 23:55:56 +0000336 V = GEP->getPointerOperand();
337 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
338 V = cast<Operator>(V)->getOperand(0);
Dan Gohman4ce5ade2009-08-27 17:55:13 +0000339 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
340 if (GA->mayBeOverridden())
341 return V;
342 V = GA->getAliasee();
Duncan Sandse59fa782008-12-29 21:06:19 +0000343 } else {
344 return V;
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000345 }
Duncan Sandse59fa782008-12-29 21:06:19 +0000346 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
347 } while (1);
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000348}
349
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000350Value *Value::getUnderlyingObject() {
351 if (!isa<PointerType>(getType()))
352 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000353 Value *V = this;
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000354 unsigned MaxLookup = 6;
Duncan Sandse59fa782008-12-29 21:06:19 +0000355 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000356 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Dan Gohmane1019db2009-07-17 23:55:56 +0000357 V = GEP->getPointerOperand();
358 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
359 V = cast<Operator>(V)->getOperand(0);
Dan Gohman4ce5ade2009-08-27 17:55:13 +0000360 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
361 if (GA->mayBeOverridden())
362 return V;
363 V = GA->getAliasee();
Duncan Sandse59fa782008-12-29 21:06:19 +0000364 } else {
365 return V;
366 }
367 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000368 } while (--MaxLookup);
369 return V;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000370}
371
Chris Lattner027d7262008-12-02 18:33:11 +0000372/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
Chris Lattner9c1b5022008-12-02 07:16:45 +0000373/// return the value in the PHI node corresponding to PredBB. If not, return
374/// ourself. This is useful if you want to know the value something has in a
375/// predecessor block.
Daniel Dunbar6058b512009-09-20 04:03:34 +0000376Value *Value::DoPHITranslation(const BasicBlock *CurBB,
Chris Lattner9c1b5022008-12-02 07:16:45 +0000377 const BasicBlock *PredBB) {
378 PHINode *PN = dyn_cast<PHINode>(this);
379 if (PN && PN->getParent() == CurBB)
380 return PN->getIncomingValueForBlock(PredBB);
381 return this;
382}
383
Owen Anderson47db9412009-07-22 00:24:57 +0000384LLVMContext &Value::getContext() const { return VTy->getContext(); }
385
Chris Lattner90234f32009-03-31 22:11:05 +0000386//===----------------------------------------------------------------------===//
387// ValueHandleBase Class
388//===----------------------------------------------------------------------===//
389
Dan Gohman3f025952009-05-04 17:25:21 +0000390/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
391/// List is known to point into the existing use list.
Chris Lattner90234f32009-03-31 22:11:05 +0000392void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
393 assert(List && "Handle list is null?");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000394
Chris Lattner90234f32009-03-31 22:11:05 +0000395 // Splice ourselves into the list.
396 Next = *List;
397 *List = this;
398 setPrevPtr(List);
399 if (Next) {
400 Next->setPrevPtr(&Next);
401 assert(VP == Next->VP && "Added to wrong list?");
402 }
403}
404
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000405void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
406 assert(List && "Must insert after existing node");
407
408 Next = List->Next;
409 setPrevPtr(&List->Next);
410 List->Next = this;
411 if (Next)
412 Next->setPrevPtr(&Next);
413}
414
Chris Lattner90234f32009-03-31 22:11:05 +0000415/// AddToUseList - Add this ValueHandle to the use list for VP.
416void ValueHandleBase::AddToUseList() {
417 assert(VP && "Null pointer doesn't have a use list!");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000418
Owen Andersone8f21852009-08-18 18:28:58 +0000419 LLVMContextImpl *pImpl = VP->getContext().pImpl;
Daniel Dunbar6058b512009-09-20 04:03:34 +0000420
Chris Lattner90234f32009-03-31 22:11:05 +0000421 if (VP->HasValueHandle) {
422 // If this value already has a ValueHandle, then it must be in the
423 // ValueHandles map already.
Owen Andersone8f21852009-08-18 18:28:58 +0000424 ValueHandleBase *&Entry = pImpl->ValueHandles[VP];
Chris Lattner90234f32009-03-31 22:11:05 +0000425 assert(Entry != 0 && "Value doesn't have any handles?");
Owen Anderson4b660a82009-06-17 17:36:57 +0000426 AddToExistingUseList(&Entry);
Owen Anderson4b660a82009-06-17 17:36:57 +0000427 return;
Chris Lattner90234f32009-03-31 22:11:05 +0000428 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000429
Chris Lattner90234f32009-03-31 22:11:05 +0000430 // Ok, it doesn't have any handles yet, so we must insert it into the
431 // DenseMap. However, doing this insertion could cause the DenseMap to
432 // reallocate itself, which would invalidate all of the PrevP pointers that
433 // point into the old table. Handle this by checking for reallocation and
434 // updating the stale pointers only if needed.
Owen Andersone8f21852009-08-18 18:28:58 +0000435 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
Chris Lattner90234f32009-03-31 22:11:05 +0000436 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
Daniel Dunbar6058b512009-09-20 04:03:34 +0000437
Chris Lattner90234f32009-03-31 22:11:05 +0000438 ValueHandleBase *&Entry = Handles[VP];
439 assert(Entry == 0 && "Value really did already have handles?");
440 AddToExistingUseList(&Entry);
Dan Gohman3f025952009-05-04 17:25:21 +0000441 VP->HasValueHandle = true;
Daniel Dunbar6058b512009-09-20 04:03:34 +0000442
Chris Lattner90234f32009-03-31 22:11:05 +0000443 // If reallocation didn't happen or if this was the first insertion, don't
444 // walk the table.
Daniel Dunbar6058b512009-09-20 04:03:34 +0000445 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
Owen Anderson4b660a82009-06-17 17:36:57 +0000446 Handles.size() == 1) {
Chris Lattner90234f32009-03-31 22:11:05 +0000447 return;
Owen Anderson4b660a82009-06-17 17:36:57 +0000448 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000449
Chris Lattner90234f32009-03-31 22:11:05 +0000450 // Okay, reallocation did happen. Fix the Prev Pointers.
Owen Andersone8f21852009-08-18 18:28:58 +0000451 for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
452 E = Handles.end(); I != E; ++I) {
Chris Lattner90234f32009-03-31 22:11:05 +0000453 assert(I->second && I->first == I->second->VP && "List invariant broken!");
454 I->second->setPrevPtr(&I->second);
455 }
456}
457
458/// RemoveFromUseList - Remove this ValueHandle from its current use list.
459void ValueHandleBase::RemoveFromUseList() {
460 assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
461
462 // Unlink this from its use list.
463 ValueHandleBase **PrevPtr = getPrevPtr();
464 assert(*PrevPtr == this && "List invariant broken");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000465
Chris Lattner90234f32009-03-31 22:11:05 +0000466 *PrevPtr = Next;
467 if (Next) {
468 assert(Next->getPrevPtr() == &Next && "List invariant broken");
469 Next->setPrevPtr(PrevPtr);
470 return;
471 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000472
Chris Lattner90234f32009-03-31 22:11:05 +0000473 // If the Next pointer was null, then it is possible that this was the last
474 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
475 // map.
Owen Andersone8f21852009-08-18 18:28:58 +0000476 LLVMContextImpl *pImpl = VP->getContext().pImpl;
477 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
Chris Lattner90234f32009-03-31 22:11:05 +0000478 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
479 Handles.erase(VP);
480 VP->HasValueHandle = false;
481 }
482}
483
484
485void ValueHandleBase::ValueIsDeleted(Value *V) {
486 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
487
488 // Get the linked list base, which is guaranteed to exist since the
489 // HasValueHandle flag is set.
Owen Andersone8f21852009-08-18 18:28:58 +0000490 LLVMContextImpl *pImpl = V->getContext().pImpl;
491 ValueHandleBase *Entry = pImpl->ValueHandles[V];
Chris Lattner90234f32009-03-31 22:11:05 +0000492 assert(Entry && "Value bit set but no entries exist");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000493
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000494 // We use a local ValueHandleBase as an iterator so that
495 // ValueHandles can add and remove themselves from the list without
496 // breaking our iteration. This is not really an AssertingVH; we
497 // just have to give ValueHandleBase some kind.
498 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
499 Iterator.RemoveFromUseList();
500 Iterator.AddToExistingUseListAfter(Entry);
501 assert(Entry->Next == &Iterator && "Loop invariant broken.");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000502
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000503 switch (Entry->getKind()) {
Chris Lattner90234f32009-03-31 22:11:05 +0000504 case Assert:
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000505 break;
Daniel Dunbar70d4fb02009-09-22 02:02:33 +0000506 case Tracking:
507 // Mark that this value has been deleted by setting it to an invalid Value
508 // pointer.
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000509 Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey());
Daniel Dunbar70d4fb02009-09-22 02:02:33 +0000510 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000511 case Weak:
512 // Weak just goes to null, which will unlink it from the list.
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000513 Entry->operator=(0);
Chris Lattner90234f32009-03-31 22:11:05 +0000514 break;
515 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000516 // Forward to the subclass's implementation.
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000517 static_cast<CallbackVH*>(Entry)->deleted();
Dan Gohman745ad442009-05-02 21:10:48 +0000518 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000519 }
520 }
Daniel Dunbar6058b512009-09-20 04:03:34 +0000521
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000522 // All callbacks, weak references, and assertingVHs should be dropped by now.
523 if (V->HasValueHandle) {
524#ifndef NDEBUG // Only in +Asserts mode...
David Greene3f907a92010-01-05 01:30:09 +0000525 dbgs() << "While deleting: " << *V->getType() << " %" << V->getNameStr()
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000526 << "\n";
527 if (pImpl->ValueHandles[V]->getKind() == Assert)
528 llvm_unreachable("An asserting value handle still pointed to this"
529 " value!");
530
531#endif
532 llvm_unreachable("All references to V were not removed?");
533 }
Chris Lattner90234f32009-03-31 22:11:05 +0000534}
535
536
537void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
538 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
539 assert(Old != New && "Changing value into itself!");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000540
Chris Lattner90234f32009-03-31 22:11:05 +0000541 // Get the linked list base, which is guaranteed to exist since the
542 // HasValueHandle flag is set.
Owen Andersone8f21852009-08-18 18:28:58 +0000543 LLVMContextImpl *pImpl = Old->getContext().pImpl;
544 ValueHandleBase *Entry = pImpl->ValueHandles[Old];
545
Chris Lattner90234f32009-03-31 22:11:05 +0000546 assert(Entry && "Value bit set but no entries exist");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000547
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000548 // We use a local ValueHandleBase as an iterator so that
549 // ValueHandles can add and remove themselves from the list without
550 // breaking our iteration. This is not really an AssertingVH; we
551 // just have to give ValueHandleBase some kind.
552 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
553 Iterator.RemoveFromUseList();
554 Iterator.AddToExistingUseListAfter(Entry);
555 assert(Entry->Next == &Iterator && "Loop invariant broken.");
Daniel Dunbar6058b512009-09-20 04:03:34 +0000556
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000557 switch (Entry->getKind()) {
Chris Lattner90234f32009-03-31 22:11:05 +0000558 case Assert:
559 // Asserting handle does not follow RAUW implicitly.
560 break;
Daniel Dunbar70d4fb02009-09-22 02:02:33 +0000561 case Tracking:
562 // Tracking goes to new value like a WeakVH. Note that this may make it
563 // something incompatible with its templated type. We don't want to have a
564 // virtual (or inline) interface to handle this though, so instead we make
Daniel Dunbar86707c92009-09-22 10:30:34 +0000565 // the TrackingVH accessors guarantee that a client never sees this value.
Daniel Dunbar70d4fb02009-09-22 02:02:33 +0000566
567 // FALLTHROUGH
Chris Lattner90234f32009-03-31 22:11:05 +0000568 case Weak:
569 // Weak goes to the new value, which will unlink it from Old's list.
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000570 Entry->operator=(New);
Chris Lattner90234f32009-03-31 22:11:05 +0000571 break;
572 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000573 // Forward to the subclass's implementation.
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000574 static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
Dan Gohman745ad442009-05-02 21:10:48 +0000575 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000576 }
577 }
578}
579
Dan Gohman745ad442009-05-02 21:10:48 +0000580/// ~CallbackVH. Empty, but defined here to avoid emitting the vtable
581/// more than once.
582CallbackVH::~CallbackVH() {}
583
Chris Lattner9c1b5022008-12-02 07:16:45 +0000584
Chris Lattner2f7c9632001-06-06 20:29:01 +0000585//===----------------------------------------------------------------------===//
586// User Class
587//===----------------------------------------------------------------------===//
588
Chris Lattner2f7c9632001-06-06 20:29:01 +0000589// replaceUsesOfWith - Replaces all references to the "From" definition with
590// references to the "To" definition.
591//
592void User::replaceUsesOfWith(Value *From, Value *To) {
593 if (From == To) return; // Duh what?
594
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000595 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
Dan Gohman5211b012009-08-11 15:53:15 +0000596 "Cannot call User::replaceUsesOfWith on a constant!");
Chris Lattner2c6abea2002-10-09 23:12:59 +0000597
Chris Lattnera073acb2001-07-07 08:36:50 +0000598 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
599 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000600 // The side effects of this setOperand call include linking to
601 // "To", adding "this" to the uses list of To, and
602 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000603 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000604 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000605}