blob: be53ed2584e98d77a584db622e74f9598bd0d2ac [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"
Gordon Henriksen3e5be662007-12-09 22:46:10 +000021#include "llvm/Constants.h"
22#include "llvm/InlineAsm.h"
23#include "llvm/Instructions.h"
24#include "llvm/IntrinsicInst.h"
25#include "llvm/InstrTypes.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000026#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner2f7c9632001-06-06 20:29:01 +000029//===----------------------------------------------------------------------===//
30// Value Class
31//===----------------------------------------------------------------------===//
32
Chris Lattner25450e32001-12-13 00:41:27 +000033static inline const Type *checkType(const Type *Ty) {
34 assert(Ty && "Value defined with a null type: Error!");
35 return Ty;
36}
37
Chris Lattner32ab6432007-02-12 05:18:08 +000038Value::Value(const Type *ty, unsigned scid)
Chris Lattnera29c92f2005-02-05 01:37:58 +000039 : SubclassID(scid), SubclassData(0), Ty(checkType(ty)),
Chris Lattner32ab6432007-02-12 05:18:08 +000040 UseList(0), Name(0) {
Chris Lattnerbea72472004-07-06 17:44:17 +000041 if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Misha Brukmanb1c93172005-04-21 23:48:37 +000042 assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
Chris Lattner9df9afd2004-07-07 18:07:46 +000043 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000044 "Cannot create non-first-class values except for constants!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000045}
46
Gordon Henriksen3e5be662007-12-09 22:46:10 +000047Value::~Value()
48{
49 switch(SubclassID)
50 {
51 case ArgumentVal:
52 Argument::destroyThis(cast<Argument>(this));
53 break;
54 case BasicBlockVal:
55 BasicBlock::destroyThis(cast<BasicBlock>(this));
56 break;
57 case FunctionVal:
58 Function::destroyThis(cast<Function>(this));
59 break;
60 case GlobalAliasVal:
61 GlobalAlias::destroyThis(cast<GlobalAlias>(this));
62 break;
63 case GlobalVariableVal:
64 GlobalVariable::destroyThis(cast<GlobalVariable>(this));
65 break;
66 case UndefValueVal:
67 UndefValue::destroyThis(cast<UndefValue>(this));
68 break;
69 case ConstantExprVal:
70 {
71 ConstantExpr* CE = dyn_cast<ConstantExpr>(this);
72 if(CE->getOpcode() == Instruction::GetElementPtr)
73 {
74 GetElementPtrConstantExpr* GECE =
75 dyn_cast<GetElementPtrConstantExpr>(CE);
76 GetElementPtrConstantExpr::destroyThis(GECE);
77 }
78 else if(CE->getOpcode() == Instruction::ExtractElement)
79 {
80 ExtractElementConstantExpr* EECE =
81 dyn_cast<ExtractElementConstantExpr>(CE);
82 ExtractElementConstantExpr::destroyThis(EECE);
83 }
84 else if(CE->getOpcode() == Instruction::InsertElement)
85 {
86 InsertElementConstantExpr* IECE =
87 dyn_cast<InsertElementConstantExpr>(CE);
88 InsertElementConstantExpr::destroyThis(IECE);
89 }
90 else if(CE->getOpcode() == Instruction::Select)
91 {
92 SelectConstantExpr* SCE = dyn_cast<SelectConstantExpr>(CE);
93 SelectConstantExpr::destroyThis(SCE);
94 }
95 else if(CE->getOpcode() == Instruction::ShuffleVector)
96 {
97 ShuffleVectorConstantExpr* SVCE =
98 dyn_cast<ShuffleVectorConstantExpr>(CE);
99 ShuffleVectorConstantExpr::destroyThis(SVCE);
100 }
101 else if(BinaryConstantExpr* BCE = dyn_cast<BinaryConstantExpr>(this))
102 BinaryConstantExpr::destroyThis(BCE);
103 else if(UnaryConstantExpr* UCE = dyn_cast<UnaryConstantExpr>(this))
104 UnaryConstantExpr::destroyThis(UCE);
105 else if(CompareConstantExpr* CCE = dyn_cast<CompareConstantExpr>(this))
106 CompareConstantExpr::destroyThis(CCE);
107 else
108 assert(0 && "Unknown ConstantExpr-inherited class in ~Value.");
109 }
110 break;
111 case ConstantAggregateZeroVal:
112 ConstantAggregateZero::destroyThis(cast<ConstantAggregateZero>(this));
113 break;
114 case ConstantIntVal:
115 ConstantInt::destroyThis(cast<ConstantInt>(this));
116 break;
117 case ConstantFPVal:
118 ConstantFP::destroyThis(cast<ConstantFP>(this));
119 break;
120 case ConstantArrayVal:
121 ConstantArray::destroyThis(cast<ConstantArray>(this));
122 break;
123 case ConstantStructVal:
124 ConstantStruct::destroyThis(cast<ConstantStruct>(this));
125 break;
126 case ConstantVectorVal:
127 ConstantVector::destroyThis(cast<ConstantVector>(this));
128 break;
129 case ConstantPointerNullVal:
130 ConstantPointerNull::destroyThis(cast<ConstantPointerNull>(this));
131 break;
132 case InlineAsmVal:
133 InlineAsm::destroyThis(cast<InlineAsm>(this));
134 break;
135
136 default:
137 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(this))
138 BinaryOperator::destroyThis(BO);
139 else if (CallInst *CI = dyn_cast<CallInst>(this))
Chris Lattnerd05f4ba2007-12-10 01:48:29 +0000140 CallInst::destroyThis(CI);
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000141 else if (CmpInst *CI = dyn_cast<CmpInst>(this))
142 {
143 if (FCmpInst *FCI = dyn_cast<FCmpInst>(this))
144 FCmpInst::destroyThis(FCI);
145 else if (ICmpInst *ICI = dyn_cast<ICmpInst>(this))
146 ICmpInst::destroyThis(ICI);
147 else
148 assert(0 && "Unknown CmpInst-inherited class in ~Value.");
149 }
150 else if (ExtractElementInst *EEI = dyn_cast<ExtractElementInst>(this))
151 ExtractElementInst::destroyThis(EEI);
152 else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(this))
153 GetElementPtrInst::destroyThis(GEP);
154 else if (InsertElementInst* IE = dyn_cast<InsertElementInst>(this))
155 InsertElementInst::destroyThis(IE);
156 else if (PHINode *PN = dyn_cast<PHINode>(this))
157 PHINode::destroyThis(PN);
158 else if (SelectInst *SI = dyn_cast<SelectInst>(this))
159 SelectInst::destroyThis(SI);
160 else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(this))
161 ShuffleVectorInst::destroyThis(SVI);
162 else if (StoreInst *SI = dyn_cast<StoreInst>(this))
163 StoreInst::destroyThis(SI);
164 else if (TerminatorInst *TI = dyn_cast<TerminatorInst>(this))
165 {
166 if (BranchInst* BI = dyn_cast<BranchInst>(this))
167 BranchInst::destroyThis(BI);
168 else if (InvokeInst* II = dyn_cast<InvokeInst>(this))
169 InvokeInst::destroyThis(II);
170 else if (ReturnInst* RI = dyn_cast<ReturnInst>(this))
171 ReturnInst::destroyThis(RI);
172 else if (SwitchInst *SI = dyn_cast<SwitchInst>(this))
173 SwitchInst::destroyThis(SI);
174 else if (UnreachableInst *UI = dyn_cast<UnreachableInst>(this))
175 UnreachableInst::destroyThis(UI);
176 else if (UnwindInst *UI = dyn_cast<UnwindInst>(this))
177 UnwindInst::destroyThis(UI);
178 else
179 assert(0 && "Unknown TerminatorInst-inherited class in ~Value.");
180 }
181 else if(UnaryInstruction* UI = dyn_cast<UnaryInstruction>(this))
182 {
183 if(AllocationInst* AI = dyn_cast<AllocationInst>(this))
184 {
185 if(AllocaInst* AI = dyn_cast<AllocaInst>(this))
186 AllocaInst::destroyThis(AI);
187 else if(MallocInst* MI = dyn_cast<MallocInst>(this))
188 MallocInst::destroyThis(MI);
189 else
190 assert(0 && "Unknown AllocationInst-inherited class in ~Value.");
Chris Lattnerd05f4ba2007-12-10 01:48:29 +0000191 } else if(CastInst* CI = dyn_cast<CastInst>(this)) {
192 if(BitCastInst* BCI = dyn_cast<BitCastInst>(CI))
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000193 BitCastInst::destroyThis(BCI);
Chris Lattnerd05f4ba2007-12-10 01:48:29 +0000194 else if(FPExtInst* FPEI = dyn_cast<FPExtInst>(CI))
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000195 FPExtInst::destroyThis(FPEI);
Chris Lattnerd05f4ba2007-12-10 01:48:29 +0000196 else if(FPToSIInst* FPSII = dyn_cast<FPToSIInst>(CI))
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000197 FPToSIInst::destroyThis(FPSII);
Chris Lattnerd05f4ba2007-12-10 01:48:29 +0000198 else if(FPToUIInst* FPUII = dyn_cast<FPToUIInst>(CI))
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000199 FPToUIInst::destroyThis(FPUII);
Chris Lattnerd05f4ba2007-12-10 01:48:29 +0000200 else if(FPTruncInst* FPTI = dyn_cast<FPTruncInst>(CI))
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000201 FPTruncInst::destroyThis(FPTI);
Chris Lattnerd05f4ba2007-12-10 01:48:29 +0000202 else if(IntToPtrInst* I2PI = dyn_cast<IntToPtrInst>(CI))
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000203 IntToPtrInst::destroyThis(I2PI);
Chris Lattnerd05f4ba2007-12-10 01:48:29 +0000204 else if(PtrToIntInst* P2II = dyn_cast<PtrToIntInst>(CI))
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000205 PtrToIntInst::destroyThis(P2II);
Chris Lattnerd05f4ba2007-12-10 01:48:29 +0000206 else if(SExtInst* SEI = dyn_cast<SExtInst>(CI))
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000207 SExtInst::destroyThis(SEI);
Chris Lattnerd05f4ba2007-12-10 01:48:29 +0000208 else if(SIToFPInst* SIFPI = dyn_cast<SIToFPInst>(CI))
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000209 SIToFPInst::destroyThis(SIFPI);
Chris Lattnerd05f4ba2007-12-10 01:48:29 +0000210 else if(TruncInst* TI = dyn_cast<TruncInst>(CI))
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000211 TruncInst::destroyThis(TI);
Chris Lattnerd05f4ba2007-12-10 01:48:29 +0000212 else if(UIToFPInst* UIFPI = dyn_cast<UIToFPInst>(CI))
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000213 UIToFPInst::destroyThis(UIFPI);
Chris Lattnerd05f4ba2007-12-10 01:48:29 +0000214 else if(ZExtInst* ZEI = dyn_cast<ZExtInst>(CI))
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000215 ZExtInst::destroyThis(ZEI);
216 else
217 assert(0 && "Unknown CastInst-inherited class in ~Value.");
218 }
219 else if(FreeInst* FI = dyn_cast<FreeInst>(this))
220 FreeInst::destroyThis(FI);
221 else if(LoadInst* LI = dyn_cast<LoadInst>(this))
222 LoadInst::destroyThis(LI);
223 else if(VAArgInst* VAI = dyn_cast<VAArgInst>(this))
224 VAArgInst::destroyThis(VAI);
225 else
226 assert(0 && "Unknown UnaryInstruction-inherited class in ~Value.");
227 }
228 else if (DummyInst *DI = dyn_cast<DummyInst>(this))
229 DummyInst::destroyThis(DI);
230 else
231 assert(0 && "Unknown Instruction-inherited class in ~Value.");
232 break;
233 }
234}
235
236void Value::destroyThis(Value*v)
237{
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +0000239 // Check to make sure that there are no uses of this value that are still
240 // around when the value is destroyed. If there are, then we have a dangling
241 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +0000242 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +0000243 // a <badref>
244 //
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000245 if (!v->use_empty()) {
246 DOUT << "While deleting: " << *v->Ty << " %" << v->Name << "\n";
247 for (use_iterator I = v->use_begin(), E = v->use_end(); I != E; ++I)
Bill Wendling6a462f12006-11-17 08:03:48 +0000248 DOUT << "Use still stuck around after Def is destroyed:"
249 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000250 }
251#endif
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000252 assert(v->use_empty() && "Uses remain when a value is destroyed!");
Chris Lattner184b2982002-09-08 18:59:35 +0000253
Chris Lattneraf867a32007-03-20 00:18:10 +0000254 // If this value is named, destroy the name. This should not be in a symtab
255 // at this point.
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000256 if (v->Name)
257 v->Name->Destroy();
Chris Lattneraf867a32007-03-20 00:18:10 +0000258
Chris Lattner184b2982002-09-08 18:59:35 +0000259 // There should be no uses of this object anymore, remove it.
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000260 LeakDetector::removeGarbageObject(v);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000261}
262
Chris Lattner4947e672005-02-01 01:24:21 +0000263/// hasNUses - Return true if this Value has exactly N users.
264///
265bool Value::hasNUses(unsigned N) const {
266 use_const_iterator UI = use_begin(), E = use_end();
267
268 for (; N; --N, ++UI)
269 if (UI == E) return false; // Too few.
270 return UI == E;
271}
272
Chris Lattnerd36552f2005-02-23 16:51:11 +0000273/// hasNUsesOrMore - Return true if this value has N users or more. This is
274/// logically equivalent to getNumUses() >= N.
275///
276bool Value::hasNUsesOrMore(unsigned N) const {
277 use_const_iterator UI = use_begin(), E = use_end();
278
279 for (; N; --N, ++UI)
280 if (UI == E) return false; // Too few.
281
282 return true;
283}
284
285
Chris Lattner4947e672005-02-01 01:24:21 +0000286/// getNumUses - This method computes the number of uses of this Value. This
287/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
288/// values.
289unsigned Value::getNumUses() const {
290 return (unsigned)std::distance(use_begin(), use_end());
291}
292
Chris Lattnerb6250822007-02-11 00:37:27 +0000293static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Chris Lattner569c8ac2007-02-11 19:12:18 +0000294 ST = 0;
Chris Lattnerb6250822007-02-11 00:37:27 +0000295 if (Instruction *I = dyn_cast<Instruction>(V)) {
296 if (BasicBlock *P = I->getParent())
297 if (Function *PP = P->getParent())
298 ST = &PP->getValueSymbolTable();
299 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
300 if (Function *P = BB->getParent())
301 ST = &P->getValueSymbolTable();
302 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
303 if (Module *P = GV->getParent())
304 ST = &P->getValueSymbolTable();
305 } else if (Argument *A = dyn_cast<Argument>(V)) {
306 if (Function *P = A->getParent())
307 ST = &P->getValueSymbolTable();
308 } else {
309 assert(isa<Constant>(V) && "Unknown value type!");
310 return true; // no name is setable for this.
311 }
312 return false;
313}
Chris Lattner2c6abea2002-10-09 23:12:59 +0000314
Chris Lattner5109a882007-08-10 15:34:35 +0000315/// getNameStart - Return a pointer to a null terminated string for this name.
316/// Note that names can have null characters within the string as well as at
317/// their end. This always returns a non-null pointer.
318const char *Value::getNameStart() const {
319 if (Name == 0) return "";
320 return Name->getKeyData();
321}
322
323/// getNameLen - Return the length of the string, correctly handling nul
324/// characters embedded into them.
325unsigned Value::getNameLen() const {
Chris Lattner2be9ec52007-09-28 20:09:40 +0000326 return Name ? Name->getKeyLength() : 0;
Chris Lattner5109a882007-08-10 15:34:35 +0000327}
328
329
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000330std::string Value::getNameStr() const {
Chris Lattner32ab6432007-02-12 05:18:08 +0000331 if (Name == 0) return "";
332 return std::string(Name->getKeyData(),
333 Name->getKeyData()+Name->getKeyLength());
334}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000335
Chris Lattner32ab6432007-02-12 05:18:08 +0000336void Value::setName(const std::string &name) {
Chris Lattner1a5de582007-02-12 18:52:59 +0000337 setName(&name[0], name.size());
338}
339
Chris Lattnercb9a6262007-02-13 07:53:34 +0000340void Value::setName(const char *Name) {
341 setName(Name, Name ? strlen(Name) : 0);
342}
343
Chris Lattner1a5de582007-02-12 18:52:59 +0000344void Value::setName(const char *NameStr, unsigned NameLen) {
345 if (NameLen == 0 && !hasName()) return;
Jeff Cohenb622c112007-03-05 00:00:42 +0000346 assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
Chris Lattner32ab6432007-02-12 05:18:08 +0000347
Chris Lattnerffb37782005-03-06 02:14:28 +0000348 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000349 ValueSymbolTable *ST;
350 if (getSymTab(this, ST))
351 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000352
Chris Lattner32ab6432007-02-12 05:18:08 +0000353 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000354 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000355 // Free the name for this value.
356 Name->Destroy();
357 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000358 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000359 }
Chris Lattner1a5de582007-02-12 18:52:59 +0000360
361 if (Name) {
362 // Name isn't changing?
363 if (NameLen == Name->getKeyLength() &&
364 !memcmp(Name->getKeyData(), NameStr, NameLen))
365 return;
366 Name->Destroy();
367 }
368
369 // NOTE: Could optimize for the case the name is shrinking to not deallocate
370 // then reallocated.
371
372 // Create the new name.
373 Name = ValueName::Create(NameStr, NameStr+NameLen);
374 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000375 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000376 }
Chris Lattner32ab6432007-02-12 05:18:08 +0000377
378 // NOTE: Could optimize for the case the name is shrinking to not deallocate
379 // then reallocated.
380 if (hasName()) {
381 // Name isn't changing?
Chris Lattner1a5de582007-02-12 18:52:59 +0000382 if (NameLen == Name->getKeyLength() &&
383 !memcmp(Name->getKeyData(), NameStr, NameLen))
Chris Lattner32ab6432007-02-12 05:18:08 +0000384 return;
385
386 // Remove old name.
387 ST->removeValueName(Name);
388 Name->Destroy();
389 Name = 0;
390
Chris Lattner1a5de582007-02-12 18:52:59 +0000391 if (NameLen == 0)
392 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000393 }
394
395 // Name is changing to something new.
Chris Lattner1a5de582007-02-12 18:52:59 +0000396 Name = ST->createValueName(NameStr, NameLen, this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000397}
398
Chris Lattner1a5de582007-02-12 18:52:59 +0000399
Chris Lattnerb6250822007-02-11 00:37:27 +0000400/// takeName - transfer the name from V to this value, setting V's name to
401/// empty. It is an error to call V->takeName(V).
402void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000403 ValueSymbolTable *ST = 0;
404 // If this value has a name, drop it.
405 if (hasName()) {
406 // Get the symtab this is in.
407 if (getSymTab(this, ST)) {
408 // We can't set a name on this value, but we need to clear V's name if
409 // it has one.
410 if (V->hasName()) V->setName(0, 0);
411 return; // Cannot set a name on this value (e.g. constant).
412 }
413
414 // Remove old name.
415 if (ST)
416 ST->removeValueName(Name);
417 Name->Destroy();
418 Name = 0;
419 }
420
421 // Now we know that this has no name.
422
423 // If V has no name either, we're done.
424 if (!V->hasName()) return;
425
426 // Get this's symtab if we didn't before.
427 if (!ST) {
428 if (getSymTab(this, ST)) {
429 // Clear V's name.
430 V->setName(0, 0);
431 return; // Cannot set a name on this value (e.g. constant).
432 }
433 }
434
435 // Get V's ST, this should always succed, because V has a name.
436 ValueSymbolTable *VST;
437 bool Failure = getSymTab(V, VST);
438 assert(!Failure && "V has a name, so it should have a ST!");
439
440 // If these values are both in the same symtab, we can do this very fast.
441 // This works even if both values have no symtab yet.
442 if (ST == VST) {
443 // Take the name!
444 Name = V->Name;
445 V->Name = 0;
446 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000447 return;
448 }
449
Chris Lattnercf835ff2007-02-15 20:01:43 +0000450 // Otherwise, things are slightly more complex. Remove V's name from VST and
451 // then reinsert it into ST.
452
453 if (VST)
454 VST->removeValueName(V->Name);
455 Name = V->Name;
456 V->Name = 0;
457 Name->setValue(this);
458
459 if (ST)
460 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000461}
462
463
Chris Lattner9f158122003-08-29 05:09:37 +0000464// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
465// except that it doesn't have all of the asserts. The asserts fail because we
466// are half-way done resolving types, which causes some types to exist as two
467// different Type*'s at the same time. This is a sledgehammer to work around
468// this problem.
469//
470void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner4947e672005-02-01 01:24:21 +0000471 while (!use_empty()) {
472 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000473 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000474 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000475 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000476 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000477 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000478 continue;
479 }
Chris Lattner9f158122003-08-29 05:09:37 +0000480 }
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000481
482 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000483 }
484}
485
Chris Lattner079edeb2003-10-16 16:53:07 +0000486void Value::replaceAllUsesWith(Value *New) {
487 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
488 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
489 assert(New->getType() == getType() &&
490 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000491
Chris Lattner079edeb2003-10-16 16:53:07 +0000492 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000493}
494
Chris Lattner2f7c9632001-06-06 20:29:01 +0000495//===----------------------------------------------------------------------===//
496// User Class
497//===----------------------------------------------------------------------===//
498
Chris Lattner2f7c9632001-06-06 20:29:01 +0000499// replaceUsesOfWith - Replaces all references to the "From" definition with
500// references to the "To" definition.
501//
502void User::replaceUsesOfWith(Value *From, Value *To) {
503 if (From == To) return; // Duh what?
504
Reid Spencerbbddbf32004-07-18 00:01:50 +0000505 assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000506 "Cannot call User::replaceUsesofWith on a constant!");
507
Chris Lattnera073acb2001-07-07 08:36:50 +0000508 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
509 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000510 // The side effects of this setOperand call include linking to
511 // "To", adding "this" to the uses list of To, and
512 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000513 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000514 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000515}
Reid Spencerbbddbf32004-07-18 00:01:50 +0000516