LLVM API Documentation
00001 //===-- Value.cpp - Implement the Value class -----------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the Value and User classes. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Constant.h" 00015 #include "llvm/Constants.h" 00016 #include "llvm/DerivedTypes.h" 00017 #include "llvm/InstrTypes.h" 00018 #include "llvm/Instructions.h" 00019 #include "llvm/Module.h" 00020 #include "llvm/ValueSymbolTable.h" 00021 #include "llvm/Support/Debug.h" 00022 #include "llvm/Support/LeakDetector.h" 00023 #include <algorithm> 00024 using namespace llvm; 00025 00026 //===----------------------------------------------------------------------===// 00027 // Value Class 00028 //===----------------------------------------------------------------------===// 00029 00030 static inline const Type *checkType(const Type *Ty) { 00031 assert(Ty && "Value defined with a null type: Error!"); 00032 return Ty; 00033 } 00034 00035 Value::Value(const Type *ty, unsigned scid) 00036 : SubclassID(scid), SubclassData(0), VTy(checkType(ty)), 00037 UseList(0), Name(0) { 00038 if (isa<CallInst>(this) || isa<InvokeInst>(this)) 00039 assert((VTy->isFirstClassType() || VTy == Type::VoidTy || 00040 isa<OpaqueType>(ty) || VTy->getTypeID() == Type::StructTyID) && 00041 "invalid CallInst type!"); 00042 else if (!isa<Constant>(this) && !isa<BasicBlock>(this)) 00043 assert((VTy->isFirstClassType() || VTy == Type::VoidTy || 00044 isa<OpaqueType>(ty)) && 00045 "Cannot create non-first-class values except for constants!"); 00046 } 00047 00048 Value::~Value() { 00049 #ifndef NDEBUG // Only in -g mode... 00050 // Check to make sure that there are no uses of this value that are still 00051 // around when the value is destroyed. If there are, then we have a dangling 00052 // reference and something is wrong. This code is here to print out what is 00053 // still being referenced. The value in question should be printed as 00054 // a <badref> 00055 // 00056 if (!use_empty()) { 00057 cerr << "While deleting: " << *VTy << " %" << getNameStr() << "\n"; 00058 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I) 00059 cerr << "Use still stuck around after Def is destroyed:" 00060 << **I << "\n"; 00061 } 00062 #endif 00063 assert(use_empty() && "Uses remain when a value is destroyed!"); 00064 00065 // If this value is named, destroy the name. This should not be in a symtab 00066 // at this point. 00067 if (Name) 00068 Name->Destroy(); 00069 00070 // There should be no uses of this object anymore, remove it. 00071 LeakDetector::removeGarbageObject(this); 00072 } 00073 00074 /// hasNUses - Return true if this Value has exactly N users. 00075 /// 00076 bool Value::hasNUses(unsigned N) const { 00077 use_const_iterator UI = use_begin(), E = use_end(); 00078 00079 for (; N; --N, ++UI) 00080 if (UI == E) return false; // Too few. 00081 return UI == E; 00082 } 00083 00084 /// hasNUsesOrMore - Return true if this value has N users or more. This is 00085 /// logically equivalent to getNumUses() >= N. 00086 /// 00087 bool Value::hasNUsesOrMore(unsigned N) const { 00088 use_const_iterator UI = use_begin(), E = use_end(); 00089 00090 for (; N; --N, ++UI) 00091 if (UI == E) return false; // Too few. 00092 00093 return true; 00094 } 00095 00096 /// isUsedInBasicBlock - Return true if this value is used in the specified 00097 /// basic block. 00098 bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { 00099 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) { 00100 const Instruction *User = dyn_cast<Instruction>(*I); 00101 if (User && User->getParent() == BB) 00102 return true; 00103 } 00104 return false; 00105 } 00106 00107 00108 /// getNumUses - This method computes the number of uses of this Value. This 00109 /// is a linear time operation. Use hasOneUse or hasNUses to check for specific 00110 /// values. 00111 unsigned Value::getNumUses() const { 00112 return (unsigned)std::distance(use_begin(), use_end()); 00113 } 00114 00115 static bool getSymTab(Value *V, ValueSymbolTable *&ST) { 00116 ST = 0; 00117 if (Instruction *I = dyn_cast<Instruction>(V)) { 00118 if (BasicBlock *P = I->getParent()) 00119 if (Function *PP = P->getParent()) 00120 ST = &PP->getValueSymbolTable(); 00121 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { 00122 if (Function *P = BB->getParent()) 00123 ST = &P->getValueSymbolTable(); 00124 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 00125 if (Module *P = GV->getParent()) 00126 ST = &P->getValueSymbolTable(); 00127 } else if (Argument *A = dyn_cast<Argument>(V)) { 00128 if (Function *P = A->getParent()) 00129 ST = &P->getValueSymbolTable(); 00130 } else { 00131 assert(isa<Constant>(V) && "Unknown value type!"); 00132 return true; // no name is setable for this. 00133 } 00134 return false; 00135 } 00136 00137 /// getNameStart - Return a pointer to a null terminated string for this name. 00138 /// Note that names can have null characters within the string as well as at 00139 /// their end. This always returns a non-null pointer. 00140 const char *Value::getNameStart() const { 00141 if (Name == 0) return ""; 00142 return Name->getKeyData(); 00143 } 00144 00145 /// getNameLen - Return the length of the string, correctly handling nul 00146 /// characters embedded into them. 00147 unsigned Value::getNameLen() const { 00148 return Name ? Name->getKeyLength() : 0; 00149 } 00150 00151 /// isName - Return true if this value has the name specified by the provided 00152 /// nul terminated string. 00153 bool Value::isName(const char *N) const { 00154 unsigned InLen = strlen(N); 00155 return InLen == getNameLen() && memcmp(getNameStart(), N, InLen) == 0; 00156 } 00157 00158 00159 std::string Value::getNameStr() const { 00160 if (Name == 0) return ""; 00161 return std::string(Name->getKeyData(), 00162 Name->getKeyData()+Name->getKeyLength()); 00163 } 00164 00165 void Value::setName(const std::string &name) { 00166 setName(&name[0], name.size()); 00167 } 00168 00169 void Value::setName(const char *Name) { 00170 setName(Name, Name ? strlen(Name) : 0); 00171 } 00172 00173 void Value::setName(const char *NameStr, unsigned NameLen) { 00174 if (NameLen == 0 && !hasName()) return; 00175 assert(getType() != Type::VoidTy && "Cannot assign a name to void values!"); 00176 00177 // Get the symbol table to update for this object. 00178 ValueSymbolTable *ST; 00179 if (getSymTab(this, ST)) 00180 return; // Cannot set a name on this value (e.g. constant). 00181 00182 if (!ST) { // No symbol table to update? Just do the change. 00183 if (NameLen == 0) { 00184 // Free the name for this value. 00185 Name->Destroy(); 00186 Name = 0; 00187 return; 00188 } 00189 00190 if (Name) { 00191 // Name isn't changing? 00192 if (NameLen == Name->getKeyLength() && 00193 !memcmp(Name->getKeyData(), NameStr, NameLen)) 00194 return; 00195 Name->Destroy(); 00196 } 00197 00198 // NOTE: Could optimize for the case the name is shrinking to not deallocate 00199 // then reallocated. 00200 00201 // Create the new name. 00202 Name = ValueName::Create(NameStr, NameStr+NameLen); 00203 Name->setValue(this); 00204 return; 00205 } 00206 00207 // NOTE: Could optimize for the case the name is shrinking to not deallocate 00208 // then reallocated. 00209 if (hasName()) { 00210 // Name isn't changing? 00211 if (NameLen == Name->getKeyLength() && 00212 !memcmp(Name->getKeyData(), NameStr, NameLen)) 00213 return; 00214 00215 // Remove old name. 00216 ST->removeValueName(Name); 00217 Name->Destroy(); 00218 Name = 0; 00219 00220 if (NameLen == 0) 00221 return; 00222 } 00223 00224 // Name is changing to something new. 00225 Name = ST->createValueName(NameStr, NameLen, this); 00226 } 00227 00228 00229 /// takeName - transfer the name from V to this value, setting V's name to 00230 /// empty. It is an error to call V->takeName(V). 00231 void Value::takeName(Value *V) { 00232 ValueSymbolTable *ST = 0; 00233 // If this value has a name, drop it. 00234 if (hasName()) { 00235 // Get the symtab this is in. 00236 if (getSymTab(this, ST)) { 00237 // We can't set a name on this value, but we need to clear V's name if 00238 // it has one. 00239 if (V->hasName()) V->setName(0, 0); 00240 return; // Cannot set a name on this value (e.g. constant). 00241 } 00242 00243 // Remove old name. 00244 if (ST) 00245 ST->removeValueName(Name); 00246 Name->Destroy(); 00247 Name = 0; 00248 } 00249 00250 // Now we know that this has no name. 00251 00252 // If V has no name either, we're done. 00253 if (!V->hasName()) return; 00254 00255 // Get this's symtab if we didn't before. 00256 if (!ST) { 00257 if (getSymTab(this, ST)) { 00258 // Clear V's name. 00259 V->setName(0, 0); 00260 return; // Cannot set a name on this value (e.g. constant). 00261 } 00262 } 00263 00264 // Get V's ST, this should always succed, because V has a name. 00265 ValueSymbolTable *VST; 00266 bool Failure = getSymTab(V, VST); 00267 assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure; 00268 00269 // If these values are both in the same symtab, we can do this very fast. 00270 // This works even if both values have no symtab yet. 00271 if (ST == VST) { 00272 // Take the name! 00273 Name = V->Name; 00274 V->Name = 0; 00275 Name->setValue(this); 00276 return; 00277 } 00278 00279 // Otherwise, things are slightly more complex. Remove V's name from VST and 00280 // then reinsert it into ST. 00281 00282 if (VST) 00283 VST->removeValueName(V->Name); 00284 Name = V->Name; 00285 V->Name = 0; 00286 Name->setValue(this); 00287 00288 if (ST) 00289 ST->reinsertValue(this); 00290 } 00291 00292 00293 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith, 00294 // except that it doesn't have all of the asserts. The asserts fail because we 00295 // are half-way done resolving types, which causes some types to exist as two 00296 // different Type*'s at the same time. This is a sledgehammer to work around 00297 // this problem. 00298 // 00299 void Value::uncheckedReplaceAllUsesWith(Value *New) { 00300 while (!use_empty()) { 00301 Use &U = *UseList; 00302 // Must handle Constants specially, we cannot call replaceUsesOfWith on a 00303 // constant because they are uniqued. 00304 if (Constant *C = dyn_cast<Constant>(U.getUser())) { 00305 if (!isa<GlobalValue>(C)) { 00306 C->replaceUsesOfWithOnConstant(this, New, &U); 00307 continue; 00308 } 00309 } 00310 00311 U.set(New); 00312 } 00313 } 00314 00315 void Value::replaceAllUsesWith(Value *New) { 00316 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); 00317 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!"); 00318 assert(New->getType() == getType() && 00319 "replaceAllUses of value with new value of different type!"); 00320 00321 uncheckedReplaceAllUsesWith(New); 00322 } 00323 00324 Value *Value::stripPointerCasts() { 00325 if (!isa<PointerType>(getType())) 00326 return this; 00327 Value *V = this; 00328 do { 00329 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 00330 if (CE->getOpcode() == Instruction::GetElementPtr) { 00331 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) 00332 if (!CE->getOperand(i)->isNullValue()) 00333 return V; 00334 V = CE->getOperand(0); 00335 } else if (CE->getOpcode() == Instruction::BitCast) { 00336 V = CE->getOperand(0); 00337 } else { 00338 return V; 00339 } 00340 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) { 00341 if (!GEP->hasAllZeroIndices()) 00342 return V; 00343 V = GEP->getOperand(0); 00344 } else if (BitCastInst *CI = dyn_cast<BitCastInst>(V)) { 00345 V = CI->getOperand(0); 00346 } else { 00347 return V; 00348 } 00349 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!"); 00350 } while (1); 00351 } 00352 00353 Value *Value::getUnderlyingObject() { 00354 if (!isa<PointerType>(getType())) 00355 return this; 00356 Value *V = this; 00357 do { 00358 if (Instruction *I = dyn_cast<Instruction>(V)) { 00359 if (!isa<BitCastInst>(I) && !isa<GetElementPtrInst>(I)) 00360 return V; 00361 V = I->getOperand(0); 00362 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 00363 if (CE->getOpcode() != Instruction::BitCast && 00364 CE->getOpcode() != Instruction::GetElementPtr) 00365 return V; 00366 V = CE->getOperand(0); 00367 } else { 00368 return V; 00369 } 00370 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!"); 00371 } while (1); 00372 } 00373 00374 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent, 00375 /// return the value in the PHI node corresponding to PredBB. If not, return 00376 /// ourself. This is useful if you want to know the value something has in a 00377 /// predecessor block. 00378 Value *Value::DoPHITranslation(const BasicBlock *CurBB, 00379 const BasicBlock *PredBB) { 00380 PHINode *PN = dyn_cast<PHINode>(this); 00381 if (PN && PN->getParent() == CurBB) 00382 return PN->getIncomingValueForBlock(PredBB); 00383 return this; 00384 } 00385 00386 00387 //===----------------------------------------------------------------------===// 00388 // User Class 00389 //===----------------------------------------------------------------------===// 00390 00391 // replaceUsesOfWith - Replaces all references to the "From" definition with 00392 // references to the "To" definition. 00393 // 00394 void User::replaceUsesOfWith(Value *From, Value *To) { 00395 if (From == To) return; // Duh what? 00396 00397 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) && 00398 "Cannot call User::replaceUsesofWith on a constant!"); 00399 00400 for (unsigned i = 0, E = getNumOperands(); i != E; ++i) 00401 if (getOperand(i) == From) { // Is This operand is pointing to oldval? 00402 // The side effects of this setOperand call include linking to 00403 // "To", adding "this" to the uses list of To, and 00404 // most importantly, removing "this" from the use list of "From". 00405 setOperand(i, To); // Fix it now... 00406 } 00407 } 00408 00409 void *User::operator new(size_t s, unsigned Us) { 00410 void *Storage = ::operator new(s + sizeof(Use) * Us); 00411 Use *Start = static_cast<Use*>(Storage); 00412 Use *End = Start + Us; 00413 User *Obj = reinterpret_cast<User*>(End); 00414 Obj->OperandList = Start; 00415 Obj->NumOperands = Us; 00416 Use::initTags(Start, End); 00417 return Obj; 00418 } 00419 00420 void User::operator delete(void *Usr) { 00421 User *Start = static_cast<User*>(Usr); 00422 Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands; 00423 ::operator delete(Storage == Start->OperandList 00424 ? Storage 00425 : Usr); 00426 }