1 //===-- Value.cpp - Implement the Value class -----------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Value and User classes.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Constant.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/InstrTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/ValueSymbolTable.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/LeakDetector.h"
21 #include "llvm/Constants.h"
22 #include "llvm/InlineAsm.h"
23 #include "llvm/Instructions.h"
24 #include "llvm/IntrinsicInst.h"
25 #include "llvm/InstrTypes.h"
29 //===----------------------------------------------------------------------===//
31 //===----------------------------------------------------------------------===//
33 static inline const Type *checkType(const Type *Ty) {
34 assert(Ty && "Value defined with a null type: Error!");
38 Value::Value(const Type *ty, unsigned scid)
39 : SubclassID(scid), SubclassData(0), Ty(checkType(ty)),
41 if (!isa<Constant>(this) && !isa<BasicBlock>(this))
42 assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
43 isa<OpaqueType>(ty)) &&
44 "Cannot create non-first-class values except for constants!");
52 Argument::destroyThis(cast<Argument>(this));
55 BasicBlock::destroyThis(cast<BasicBlock>(this));
58 Function::destroyThis(cast<Function>(this));
61 GlobalAlias::destroyThis(cast<GlobalAlias>(this));
63 case GlobalVariableVal:
64 GlobalVariable::destroyThis(cast<GlobalVariable>(this));
67 UndefValue::destroyThis(cast<UndefValue>(this));
71 ConstantExpr* CE = dyn_cast<ConstantExpr>(this);
72 if(CE->getOpcode() == Instruction::GetElementPtr)
74 GetElementPtrConstantExpr* GECE =
75 dyn_cast<GetElementPtrConstantExpr>(CE);
76 GetElementPtrConstantExpr::destroyThis(GECE);
78 else if(CE->getOpcode() == Instruction::ExtractElement)
80 ExtractElementConstantExpr* EECE =
81 dyn_cast<ExtractElementConstantExpr>(CE);
82 ExtractElementConstantExpr::destroyThis(EECE);
84 else if(CE->getOpcode() == Instruction::InsertElement)
86 InsertElementConstantExpr* IECE =
87 dyn_cast<InsertElementConstantExpr>(CE);
88 InsertElementConstantExpr::destroyThis(IECE);
90 else if(CE->getOpcode() == Instruction::Select)
92 SelectConstantExpr* SCE = dyn_cast<SelectConstantExpr>(CE);
93 SelectConstantExpr::destroyThis(SCE);
95 else if(CE->getOpcode() == Instruction::ShuffleVector)
97 ShuffleVectorConstantExpr* SVCE =
98 dyn_cast<ShuffleVectorConstantExpr>(CE);
99 ShuffleVectorConstantExpr::destroyThis(SVCE);
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);
108 assert(0 && "Unknown ConstantExpr-inherited class in ~Value.");
111 case ConstantAggregateZeroVal:
112 ConstantAggregateZero::destroyThis(cast<ConstantAggregateZero>(this));
115 ConstantInt::destroyThis(cast<ConstantInt>(this));
118 ConstantFP::destroyThis(cast<ConstantFP>(this));
120 case ConstantArrayVal:
121 ConstantArray::destroyThis(cast<ConstantArray>(this));
123 case ConstantStructVal:
124 ConstantStruct::destroyThis(cast<ConstantStruct>(this));
126 case ConstantVectorVal:
127 ConstantVector::destroyThis(cast<ConstantVector>(this));
129 case ConstantPointerNullVal:
130 ConstantPointerNull::destroyThis(cast<ConstantPointerNull>(this));
133 InlineAsm::destroyThis(cast<InlineAsm>(this));
137 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(this))
138 BinaryOperator::destroyThis(BO);
139 else if (CallInst *CI = dyn_cast<CallInst>(this))
140 CallInst::destroyThis(CI);
141 else if (CmpInst *CI = dyn_cast<CmpInst>(this))
143 if (FCmpInst *FCI = dyn_cast<FCmpInst>(CI))
144 FCmpInst::destroyThis(FCI);
145 else if (ICmpInst *ICI = dyn_cast<ICmpInst>(CI))
146 ICmpInst::destroyThis(ICI);
148 assert(0 && "Unknown CmpInst-inherited class in ~Value.");
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))
166 if (BranchInst* BI = dyn_cast<BranchInst>(TI))
167 BranchInst::destroyThis(BI);
168 else if (InvokeInst* II = dyn_cast<InvokeInst>(TI))
169 InvokeInst::destroyThis(II);
170 else if (ReturnInst* RI = dyn_cast<ReturnInst>(TI))
171 ReturnInst::destroyThis(RI);
172 else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI))
173 SwitchInst::destroyThis(SI);
174 else if (UnreachableInst *UI = dyn_cast<UnreachableInst>(TI))
175 UnreachableInst::destroyThis(UI);
176 else if (UnwindInst *UI = dyn_cast<UnwindInst>(TI))
177 UnwindInst::destroyThis(UI);
179 assert(0 && "Unknown TerminatorInst-inherited class in ~Value.");
180 } else if(UnaryInstruction* UI = dyn_cast<UnaryInstruction>(this)) {
181 if(AllocationInst* AI = dyn_cast<AllocationInst>(UI)) {
182 if(AllocaInst* AI = dyn_cast<AllocaInst>(UI))
183 AllocaInst::destroyThis(AI);
184 else if(MallocInst* MI = dyn_cast<MallocInst>(UI))
185 MallocInst::destroyThis(MI);
187 assert(0 && "Unknown AllocationInst-inherited class in ~Value.");
188 } else if(CastInst* CI = dyn_cast<CastInst>(this)) {
189 if(BitCastInst* BCI = dyn_cast<BitCastInst>(CI))
190 BitCastInst::destroyThis(BCI);
191 else if(FPExtInst* FPEI = dyn_cast<FPExtInst>(CI))
192 FPExtInst::destroyThis(FPEI);
193 else if(FPToSIInst* FPSII = dyn_cast<FPToSIInst>(CI))
194 FPToSIInst::destroyThis(FPSII);
195 else if(FPToUIInst* FPUII = dyn_cast<FPToUIInst>(CI))
196 FPToUIInst::destroyThis(FPUII);
197 else if(FPTruncInst* FPTI = dyn_cast<FPTruncInst>(CI))
198 FPTruncInst::destroyThis(FPTI);
199 else if(IntToPtrInst* I2PI = dyn_cast<IntToPtrInst>(CI))
200 IntToPtrInst::destroyThis(I2PI);
201 else if(PtrToIntInst* P2II = dyn_cast<PtrToIntInst>(CI))
202 PtrToIntInst::destroyThis(P2II);
203 else if(SExtInst* SEI = dyn_cast<SExtInst>(CI))
204 SExtInst::destroyThis(SEI);
205 else if(SIToFPInst* SIFPI = dyn_cast<SIToFPInst>(CI))
206 SIToFPInst::destroyThis(SIFPI);
207 else if(TruncInst* TI = dyn_cast<TruncInst>(CI))
208 TruncInst::destroyThis(TI);
209 else if(UIToFPInst* UIFPI = dyn_cast<UIToFPInst>(CI))
210 UIToFPInst::destroyThis(UIFPI);
211 else if(ZExtInst* ZEI = dyn_cast<ZExtInst>(CI))
212 ZExtInst::destroyThis(ZEI);
214 assert(0 && "Unknown CastInst-inherited class in ~Value.");
216 else if(FreeInst* FI = dyn_cast<FreeInst>(this))
217 FreeInst::destroyThis(FI);
218 else if(LoadInst* LI = dyn_cast<LoadInst>(this))
219 LoadInst::destroyThis(LI);
220 else if(VAArgInst* VAI = dyn_cast<VAArgInst>(this))
221 VAArgInst::destroyThis(VAI);
223 assert(0 && "Unknown UnaryInstruction-inherited class in ~Value.");
225 else if (DummyInst *DI = dyn_cast<DummyInst>(this))
226 DummyInst::destroyThis(DI);
228 assert(0 && "Unknown Instruction-inherited class in ~Value.");
233 void Value::destroyThis(Value*v)
235 #ifndef NDEBUG // Only in -g mode...
236 // Check to make sure that there are no uses of this value that are still
237 // around when the value is destroyed. If there are, then we have a dangling
238 // reference and something is wrong. This code is here to print out what is
239 // still being referenced. The value in question should be printed as
242 if (!v->use_empty()) {
243 DOUT << "While deleting: " << *v->Ty << " %" << v->Name << "\n";
244 for (use_iterator I = v->use_begin(), E = v->use_end(); I != E; ++I)
245 DOUT << "Use still stuck around after Def is destroyed:"
249 assert(v->use_empty() && "Uses remain when a value is destroyed!");
251 // If this value is named, destroy the name. This should not be in a symtab
256 // There should be no uses of this object anymore, remove it.
257 LeakDetector::removeGarbageObject(v);
260 /// hasNUses - Return true if this Value has exactly N users.
262 bool Value::hasNUses(unsigned N) const {
263 use_const_iterator UI = use_begin(), E = use_end();
266 if (UI == E) return false; // Too few.
270 /// hasNUsesOrMore - Return true if this value has N users or more. This is
271 /// logically equivalent to getNumUses() >= N.
273 bool Value::hasNUsesOrMore(unsigned N) const {
274 use_const_iterator UI = use_begin(), E = use_end();
277 if (UI == E) return false; // Too few.
283 /// getNumUses - This method computes the number of uses of this Value. This
284 /// is a linear time operation. Use hasOneUse or hasNUses to check for specific
286 unsigned Value::getNumUses() const {
287 return (unsigned)std::distance(use_begin(), use_end());
290 static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
292 if (Instruction *I = dyn_cast<Instruction>(V)) {
293 if (BasicBlock *P = I->getParent())
294 if (Function *PP = P->getParent())
295 ST = &PP->getValueSymbolTable();
296 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
297 if (Function *P = BB->getParent())
298 ST = &P->getValueSymbolTable();
299 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
300 if (Module *P = GV->getParent())
301 ST = &P->getValueSymbolTable();
302 } else if (Argument *A = dyn_cast<Argument>(V)) {
303 if (Function *P = A->getParent())
304 ST = &P->getValueSymbolTable();
306 assert(isa<Constant>(V) && "Unknown value type!");
307 return true; // no name is setable for this.
312 /// getNameStart - Return a pointer to a null terminated string for this name.
313 /// Note that names can have null characters within the string as well as at
314 /// their end. This always returns a non-null pointer.
315 const char *Value::getNameStart() const {
316 if (Name == 0) return "";
317 return Name->getKeyData();
320 /// getNameLen - Return the length of the string, correctly handling nul
321 /// characters embedded into them.
322 unsigned Value::getNameLen() const {
323 return Name ? Name->getKeyLength() : 0;
327 std::string Value::getNameStr() const {
328 if (Name == 0) return "";
329 return std::string(Name->getKeyData(),
330 Name->getKeyData()+Name->getKeyLength());
333 void Value::setName(const std::string &name) {
334 setName(&name[0], name.size());
337 void Value::setName(const char *Name) {
338 setName(Name, Name ? strlen(Name) : 0);
341 void Value::setName(const char *NameStr, unsigned NameLen) {
342 if (NameLen == 0 && !hasName()) return;
343 assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
345 // Get the symbol table to update for this object.
346 ValueSymbolTable *ST;
347 if (getSymTab(this, ST))
348 return; // Cannot set a name on this value (e.g. constant).
350 if (!ST) { // No symbol table to update? Just do the change.
352 // Free the name for this value.
359 // Name isn't changing?
360 if (NameLen == Name->getKeyLength() &&
361 !memcmp(Name->getKeyData(), NameStr, NameLen))
366 // NOTE: Could optimize for the case the name is shrinking to not deallocate
369 // Create the new name.
370 Name = ValueName::Create(NameStr, NameStr+NameLen);
371 Name->setValue(this);
375 // NOTE: Could optimize for the case the name is shrinking to not deallocate
378 // Name isn't changing?
379 if (NameLen == Name->getKeyLength() &&
380 !memcmp(Name->getKeyData(), NameStr, NameLen))
384 ST->removeValueName(Name);
392 // Name is changing to something new.
393 Name = ST->createValueName(NameStr, NameLen, this);
397 /// takeName - transfer the name from V to this value, setting V's name to
398 /// empty. It is an error to call V->takeName(V).
399 void Value::takeName(Value *V) {
400 ValueSymbolTable *ST = 0;
401 // If this value has a name, drop it.
403 // Get the symtab this is in.
404 if (getSymTab(this, ST)) {
405 // We can't set a name on this value, but we need to clear V's name if
407 if (V->hasName()) V->setName(0, 0);
408 return; // Cannot set a name on this value (e.g. constant).
413 ST->removeValueName(Name);
418 // Now we know that this has no name.
420 // If V has no name either, we're done.
421 if (!V->hasName()) return;
423 // Get this's symtab if we didn't before.
425 if (getSymTab(this, ST)) {
428 return; // Cannot set a name on this value (e.g. constant).
432 // Get V's ST, this should always succed, because V has a name.
433 ValueSymbolTable *VST;
434 bool Failure = getSymTab(V, VST);
435 assert(!Failure && "V has a name, so it should have a ST!");
437 // If these values are both in the same symtab, we can do this very fast.
438 // This works even if both values have no symtab yet.
443 Name->setValue(this);
447 // Otherwise, things are slightly more complex. Remove V's name from VST and
448 // then reinsert it into ST.
451 VST->removeValueName(V->Name);
454 Name->setValue(this);
457 ST->reinsertValue(this);
461 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
462 // except that it doesn't have all of the asserts. The asserts fail because we
463 // are half-way done resolving types, which causes some types to exist as two
464 // different Type*'s at the same time. This is a sledgehammer to work around
467 void Value::uncheckedReplaceAllUsesWith(Value *New) {
468 while (!use_empty()) {
470 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
471 // constant because they are uniqued.
472 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
473 if (!isa<GlobalValue>(C)) {
474 C->replaceUsesOfWithOnConstant(this, New, &U);
483 void Value::replaceAllUsesWith(Value *New) {
484 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
485 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
486 assert(New->getType() == getType() &&
487 "replaceAllUses of value with new value of different type!");
489 uncheckedReplaceAllUsesWith(New);
492 //===----------------------------------------------------------------------===//
494 //===----------------------------------------------------------------------===//
496 // replaceUsesOfWith - Replaces all references to the "From" definition with
497 // references to the "To" definition.
499 void User::replaceUsesOfWith(Value *From, Value *To) {
500 if (From == To) return; // Duh what?
502 assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
503 "Cannot call User::replaceUsesofWith on a constant!");
505 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
506 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
507 // The side effects of this setOperand call include linking to
508 // "To", adding "this" to the uses list of To, and
509 // most importantly, removing "this" from the use list of "From".
510 setOperand(i, To); // Fix it now...