Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Value.cpp - Implement the Value class -----------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10 | // This file implements the Value and User classes. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 14 | #include "llvm/Constant.h" |
Anton Korobeynikov | 82c02b2 | 2008-05-06 22:52:30 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
| 17 | #include "llvm/InstrTypes.h" |
Devang Patel | 1f00b53 | 2008-02-21 01:54:02 +0000 | [diff] [blame] | 18 | #include "llvm/Instructions.h" |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 20 | #include "llvm/ValueSymbolTable.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // Value Class |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Chris Lattner | 25450e3 | 2001-12-13 00:41:27 +0000 | [diff] [blame] | 30 | static inline const Type *checkType(const Type *Ty) { |
| 31 | assert(Ty && "Value defined with a null type: Error!"); |
| 32 | return Ty; |
| 33 | } |
| 34 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 35 | Value::Value(const Type *ty, unsigned scid) |
Chris Lattner | a29c92f | 2005-02-05 01:37:58 +0000 | [diff] [blame] | 36 | : SubclassID(scid), SubclassData(0), Ty(checkType(ty)), |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 37 | UseList(0), Name(0) { |
Devang Patel | ad582fc | 2008-02-21 02:14:01 +0000 | [diff] [blame] | 38 | if (isa<CallInst>(this) || isa<InvokeInst>(this)) |
Devang Patel | 1f00b53 | 2008-02-21 01:54:02 +0000 | [diff] [blame] | 39 | assert((Ty->isFirstClassType() || Ty == Type::VoidTy || |
| 40 | isa<OpaqueType>(ty) || Ty->getTypeID() == Type::StructTyID) && |
| 41 | "invalid CallInst type!"); |
| 42 | else if (!isa<Constant>(this) && !isa<BasicBlock>(this)) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 43 | assert((Ty->isFirstClassType() || Ty == Type::VoidTy || |
Chris Lattner | 9df9afd | 2004-07-07 18:07:46 +0000 | [diff] [blame] | 44 | isa<OpaqueType>(ty)) && |
Chris Lattner | bea7247 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 45 | "Cannot create non-first-class values except for constants!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 48 | Value::~Value() { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 49 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | 874ddad | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 50 | // Check to make sure that there are no uses of this value that are still |
| 51 | // around when the value is destroyed. If there are, then we have a dangling |
| 52 | // reference and something is wrong. This code is here to print out what is |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 53 | // still being referenced. The value in question should be printed as |
Chris Lattner | 874ddad | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 54 | // a <badref> |
| 55 | // |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 56 | if (!use_empty()) { |
Nick Lewycky | 7f6e95a | 2008-03-01 17:20:55 +0000 | [diff] [blame] | 57 | DOUT << "While deleting: " << *Ty << " %" << getNameStr() << "\n"; |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 58 | for (use_iterator I = use_begin(), E = use_end(); I != E; ++I) |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 59 | DOUT << "Use still stuck around after Def is destroyed:" |
| 60 | << **I << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 61 | } |
| 62 | #endif |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 63 | assert(use_empty() && "Uses remain when a value is destroyed!"); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 64 | |
Chris Lattner | af867a3 | 2007-03-20 00:18:10 +0000 | [diff] [blame] | 65 | // If this value is named, destroy the name. This should not be in a symtab |
| 66 | // at this point. |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 67 | if (Name) |
| 68 | Name->Destroy(); |
Chris Lattner | af867a3 | 2007-03-20 00:18:10 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 70 | // There should be no uses of this object anymore, remove it. |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 71 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 74 | /// hasNUses - Return true if this Value has exactly N users. |
| 75 | /// |
| 76 | bool Value::hasNUses(unsigned N) const { |
| 77 | use_const_iterator UI = use_begin(), E = use_end(); |
| 78 | |
| 79 | for (; N; --N, ++UI) |
| 80 | if (UI == E) return false; // Too few. |
| 81 | return UI == E; |
| 82 | } |
| 83 | |
Chris Lattner | d36552f | 2005-02-23 16:51:11 +0000 | [diff] [blame] | 84 | /// hasNUsesOrMore - Return true if this value has N users or more. This is |
| 85 | /// logically equivalent to getNumUses() >= N. |
| 86 | /// |
| 87 | bool Value::hasNUsesOrMore(unsigned N) const { |
| 88 | use_const_iterator UI = use_begin(), E = use_end(); |
| 89 | |
| 90 | for (; N; --N, ++UI) |
| 91 | if (UI == E) return false; // Too few. |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
Evan Cheng | 89553cc | 2008-06-12 21:15:59 +0000 | [diff] [blame] | 96 | /// isUsedInBasicBlock - Return true if this value is used in the specified |
| 97 | /// basic block. |
| 98 | bool Value::isUsedInBasicBlock(BasicBlock *BB) const { |
| 99 | for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) { |
| 100 | const Instruction *User = dyn_cast<Instruction>(*I); |
| 101 | if (User && User->getParent() == BB) |
| 102 | return true; |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
Chris Lattner | d36552f | 2005-02-23 16:51:11 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 108 | /// getNumUses - This method computes the number of uses of this Value. This |
| 109 | /// is a linear time operation. Use hasOneUse or hasNUses to check for specific |
| 110 | /// values. |
| 111 | unsigned Value::getNumUses() const { |
| 112 | return (unsigned)std::distance(use_begin(), use_end()); |
| 113 | } |
| 114 | |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 115 | static bool getSymTab(Value *V, ValueSymbolTable *&ST) { |
Chris Lattner | 569c8ac | 2007-02-11 19:12:18 +0000 | [diff] [blame] | 116 | ST = 0; |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 117 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 118 | if (BasicBlock *P = I->getParent()) |
| 119 | if (Function *PP = P->getParent()) |
| 120 | ST = &PP->getValueSymbolTable(); |
| 121 | } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
| 122 | if (Function *P = BB->getParent()) |
| 123 | ST = &P->getValueSymbolTable(); |
| 124 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 125 | if (Module *P = GV->getParent()) |
| 126 | ST = &P->getValueSymbolTable(); |
| 127 | } else if (Argument *A = dyn_cast<Argument>(V)) { |
| 128 | if (Function *P = A->getParent()) |
| 129 | ST = &P->getValueSymbolTable(); |
| 130 | } else { |
| 131 | assert(isa<Constant>(V) && "Unknown value type!"); |
| 132 | return true; // no name is setable for this. |
| 133 | } |
| 134 | return false; |
| 135 | } |
Chris Lattner | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 5109a88 | 2007-08-10 15:34:35 +0000 | [diff] [blame] | 137 | /// getNameStart - Return a pointer to a null terminated string for this name. |
| 138 | /// Note that names can have null characters within the string as well as at |
| 139 | /// their end. This always returns a non-null pointer. |
| 140 | const char *Value::getNameStart() const { |
| 141 | if (Name == 0) return ""; |
| 142 | return Name->getKeyData(); |
| 143 | } |
| 144 | |
| 145 | /// getNameLen - Return the length of the string, correctly handling nul |
| 146 | /// characters embedded into them. |
| 147 | unsigned Value::getNameLen() const { |
Chris Lattner | 2be9ec5 | 2007-09-28 20:09:40 +0000 | [diff] [blame] | 148 | return Name ? Name->getKeyLength() : 0; |
Chris Lattner | 5109a88 | 2007-08-10 15:34:35 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Chris Lattner | a1d850e | 2008-04-30 03:55:40 +0000 | [diff] [blame] | 151 | /// isName - Return true if this value has the name specified by the provided |
| 152 | /// nul terminated string. |
| 153 | bool Value::isName(const char *N) const { |
| 154 | unsigned InLen = strlen(N); |
Chris Lattner | 78769f0 | 2008-04-30 15:27:09 +0000 | [diff] [blame] | 155 | return InLen == getNameLen() && memcmp(getNameStart(), N, InLen) == 0; |
Chris Lattner | a1d850e | 2008-04-30 03:55:40 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Chris Lattner | 5109a88 | 2007-08-10 15:34:35 +0000 | [diff] [blame] | 158 | |
Chris Lattner | fd27ed9 | 2007-02-15 18:53:54 +0000 | [diff] [blame] | 159 | std::string Value::getNameStr() const { |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 160 | if (Name == 0) return ""; |
| 161 | return std::string(Name->getKeyData(), |
| 162 | Name->getKeyData()+Name->getKeyLength()); |
| 163 | } |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 165 | void Value::setName(const std::string &name) { |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 166 | setName(&name[0], name.size()); |
| 167 | } |
| 168 | |
Chris Lattner | cb9a626 | 2007-02-13 07:53:34 +0000 | [diff] [blame] | 169 | void Value::setName(const char *Name) { |
| 170 | setName(Name, Name ? strlen(Name) : 0); |
| 171 | } |
| 172 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 173 | void Value::setName(const char *NameStr, unsigned NameLen) { |
| 174 | if (NameLen == 0 && !hasName()) return; |
Jeff Cohen | b622c11 | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 175 | assert(getType() != Type::VoidTy && "Cannot assign a name to void values!"); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 176 | |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 177 | // Get the symbol table to update for this object. |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 178 | ValueSymbolTable *ST; |
| 179 | if (getSymTab(this, ST)) |
| 180 | return; // Cannot set a name on this value (e.g. constant). |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 182 | if (!ST) { // No symbol table to update? Just do the change. |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 183 | if (NameLen == 0) { |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 184 | // Free the name for this value. |
| 185 | Name->Destroy(); |
| 186 | Name = 0; |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 187 | return; |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 188 | } |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 189 | |
| 190 | if (Name) { |
| 191 | // Name isn't changing? |
| 192 | if (NameLen == Name->getKeyLength() && |
| 193 | !memcmp(Name->getKeyData(), NameStr, NameLen)) |
| 194 | return; |
| 195 | Name->Destroy(); |
| 196 | } |
| 197 | |
| 198 | // NOTE: Could optimize for the case the name is shrinking to not deallocate |
| 199 | // then reallocated. |
| 200 | |
| 201 | // Create the new name. |
| 202 | Name = ValueName::Create(NameStr, NameStr+NameLen); |
| 203 | Name->setValue(this); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 204 | return; |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 205 | } |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 206 | |
| 207 | // NOTE: Could optimize for the case the name is shrinking to not deallocate |
| 208 | // then reallocated. |
| 209 | if (hasName()) { |
| 210 | // Name isn't changing? |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 211 | if (NameLen == Name->getKeyLength() && |
| 212 | !memcmp(Name->getKeyData(), NameStr, NameLen)) |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 213 | return; |
| 214 | |
| 215 | // Remove old name. |
| 216 | ST->removeValueName(Name); |
| 217 | Name->Destroy(); |
| 218 | Name = 0; |
| 219 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 220 | if (NameLen == 0) |
| 221 | return; |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // Name is changing to something new. |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 225 | Name = ST->createValueName(NameStr, NameLen, this); |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 228 | |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 229 | /// takeName - transfer the name from V to this value, setting V's name to |
| 230 | /// empty. It is an error to call V->takeName(V). |
| 231 | void Value::takeName(Value *V) { |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 232 | 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. |
| 239 | if (V->hasName()) V->setName(0, 0); |
| 240 | return; // Cannot set a name on this value (e.g. constant). |
| 241 | } |
| 242 | |
| 243 | // Remove old name. |
| 244 | if (ST) |
| 245 | ST->removeValueName(Name); |
| 246 | Name->Destroy(); |
| 247 | Name = 0; |
| 248 | } |
| 249 | |
| 250 | // Now we know that this has no name. |
| 251 | |
| 252 | // If V has no name either, we're done. |
| 253 | if (!V->hasName()) return; |
| 254 | |
| 255 | // Get this's symtab if we didn't before. |
| 256 | if (!ST) { |
| 257 | if (getSymTab(this, ST)) { |
| 258 | // Clear V's name. |
| 259 | V->setName(0, 0); |
| 260 | return; // Cannot set a name on this value (e.g. constant). |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // Get V's ST, this should always succed, because V has a name. |
| 265 | ValueSymbolTable *VST; |
| 266 | bool Failure = getSymTab(V, VST); |
Chris Lattner | 106b046 | 2008-06-21 19:47:03 +0000 | [diff] [blame^] | 267 | assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure; |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 268 | |
| 269 | // 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 Lattner | cba18e3 | 2007-02-11 01:04:09 +0000 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 279 | // Otherwise, things are slightly more complex. Remove V's name from VST and |
| 280 | // then reinsert it into ST. |
| 281 | |
| 282 | if (VST) |
| 283 | VST->removeValueName(V->Name); |
| 284 | Name = V->Name; |
| 285 | V->Name = 0; |
| 286 | Name->setValue(this); |
| 287 | |
| 288 | if (ST) |
| 289 | ST->reinsertValue(this); |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 293 | // 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 | // |
| 299 | void Value::uncheckedReplaceAllUsesWith(Value *New) { |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 300 | while (!use_empty()) { |
| 301 | Use &U = *UseList; |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 302 | // Must handle Constants specially, we cannot call replaceUsesOfWith on a |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 303 | // constant because they are uniqued. |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 304 | if (Constant *C = dyn_cast<Constant>(U.getUser())) { |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 305 | if (!isa<GlobalValue>(C)) { |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 306 | C->replaceUsesOfWithOnConstant(this, New, &U); |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 307 | continue; |
| 308 | } |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 309 | } |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 310 | |
| 311 | U.set(New); |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 315 | void Value::replaceAllUsesWith(Value *New) { |
| 316 | assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); |
| 317 | assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!"); |
| 318 | assert(New->getType() == getType() && |
| 319 | "replaceAllUses of value with new value of different type!"); |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 320 | |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 321 | uncheckedReplaceAllUsesWith(New); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Anton Korobeynikov | fc2edad | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 324 | Value *Value::stripPointerCasts() { |
| 325 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) { |
| 326 | if (CE->getOpcode() == Instruction::BitCast) { |
| 327 | if (isa<PointerType>(CE->getOperand(0)->getType())) |
| 328 | return CE->getOperand(0)->stripPointerCasts(); |
| 329 | } else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 330 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 331 | if (!CE->getOperand(i)->isNullValue()) |
| 332 | return this; |
| 333 | return CE->getOperand(0)->stripPointerCasts(); |
| 334 | } |
| 335 | return this; |
| 336 | } |
| 337 | |
| 338 | if (BitCastInst *CI = dyn_cast<BitCastInst>(this)) { |
| 339 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 340 | return CI->getOperand(0)->stripPointerCasts(); |
| 341 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(this)) { |
| 342 | if (GEP->hasAllZeroIndices()) |
| 343 | return GEP->getOperand(0)->stripPointerCasts(); |
| 344 | } |
| 345 | return this; |
| 346 | } |
| 347 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 348 | //===----------------------------------------------------------------------===// |
| 349 | // User Class |
| 350 | //===----------------------------------------------------------------------===// |
| 351 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 352 | // replaceUsesOfWith - Replaces all references to the "From" definition with |
| 353 | // references to the "To" definition. |
| 354 | // |
| 355 | void User::replaceUsesOfWith(Value *From, Value *To) { |
| 356 | if (From == To) return; // Duh what? |
| 357 | |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 358 | assert((!isa<Constant>(this) || isa<GlobalValue>(this)) && |
Chris Lattner | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 359 | "Cannot call User::replaceUsesofWith on a constant!"); |
| 360 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 361 | for (unsigned i = 0, E = getNumOperands(); i != E; ++i) |
| 362 | if (getOperand(i) == From) { // Is This operand is pointing to oldval? |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 363 | // The side effects of this setOperand call include linking to |
| 364 | // "To", adding "this" to the uses list of To, and |
| 365 | // most importantly, removing "this" from the use list of "From". |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 366 | setOperand(i, To); // Fix it now... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 368 | } |
Nate Begeman | 60f9320 | 2008-05-15 01:23:11 +0000 | [diff] [blame] | 369 | |
| 370 | void *User::operator new(size_t s, unsigned Us) { |
| 371 | void *Storage = ::operator new(s + sizeof(Use) * Us); |
| 372 | Use *Start = static_cast<Use*>(Storage); |
| 373 | Use *End = Start + Us; |
| 374 | User *Obj = reinterpret_cast<User*>(End); |
| 375 | Obj->OperandList = Start; |
| 376 | Obj->NumOperands = Us; |
| 377 | Use::initTags(Start, End); |
| 378 | return Obj; |
| 379 | } |
| 380 | |
| 381 | void User::operator delete(void *Usr) { |
| 382 | User *Start = static_cast<User*>(Usr); |
| 383 | Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands; |
| 384 | ::operator delete(Storage == Start->OperandList |
| 385 | ? Storage |
| 386 | : Usr); |
| 387 | } |