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 | // |
| 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 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" |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 15 | #include "llvm/DerivedTypes.h" |
| 16 | #include "llvm/InstrTypes.h" |
| 17 | #include "llvm/Module.h" |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 18 | #include "llvm/ValueSymbolTable.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/LeakDetector.h" |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 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" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // Value Class |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Chris Lattner | 25450e3 | 2001-12-13 00:41:27 +0000 | [diff] [blame] | 33 | static inline const Type *checkType(const Type *Ty) { |
| 34 | assert(Ty && "Value defined with a null type: Error!"); |
| 35 | return Ty; |
| 36 | } |
| 37 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 38 | Value::Value(const Type *ty, unsigned scid) |
Chris Lattner | a29c92f | 2005-02-05 01:37:58 +0000 | [diff] [blame] | 39 | : SubclassID(scid), SubclassData(0), Ty(checkType(ty)), |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 40 | UseList(0), Name(0) { |
Chris Lattner | bea7247 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 41 | if (!isa<Constant>(this) && !isa<BasicBlock>(this)) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 42 | assert((Ty->isFirstClassType() || Ty == Type::VoidTy || |
Chris Lattner | 9df9afd | 2004-07-07 18:07:46 +0000 | [diff] [blame] | 43 | isa<OpaqueType>(ty)) && |
Chris Lattner | bea7247 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 44 | "Cannot create non-first-class values except for constants!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 47 | Value::~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 Lattner | d05f4ba | 2007-12-10 01:48:29 +0000 | [diff] [blame] | 140 | CallInst::destroyThis(CI); |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 141 | else if (CmpInst *CI = dyn_cast<CmpInst>(this)) |
| 142 | { |
Chris Lattner | 13daba07 | 2007-12-10 01:51:22 +0000 | [diff] [blame] | 143 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 144 | FCmpInst::destroyThis(FCI); |
Chris Lattner | 13daba07 | 2007-12-10 01:51:22 +0000 | [diff] [blame] | 145 | else if (ICmpInst *ICI = dyn_cast<ICmpInst>(CI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 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 | { |
Chris Lattner | 13daba07 | 2007-12-10 01:51:22 +0000 | [diff] [blame] | 166 | if (BranchInst* BI = dyn_cast<BranchInst>(TI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 167 | BranchInst::destroyThis(BI); |
Chris Lattner | 13daba07 | 2007-12-10 01:51:22 +0000 | [diff] [blame] | 168 | else if (InvokeInst* II = dyn_cast<InvokeInst>(TI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 169 | InvokeInst::destroyThis(II); |
Chris Lattner | 13daba07 | 2007-12-10 01:51:22 +0000 | [diff] [blame] | 170 | else if (ReturnInst* RI = dyn_cast<ReturnInst>(TI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 171 | ReturnInst::destroyThis(RI); |
Chris Lattner | 13daba07 | 2007-12-10 01:51:22 +0000 | [diff] [blame] | 172 | else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 173 | SwitchInst::destroyThis(SI); |
Chris Lattner | 13daba07 | 2007-12-10 01:51:22 +0000 | [diff] [blame] | 174 | else if (UnreachableInst *UI = dyn_cast<UnreachableInst>(TI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 175 | UnreachableInst::destroyThis(UI); |
Chris Lattner | 13daba07 | 2007-12-10 01:51:22 +0000 | [diff] [blame] | 176 | else if (UnwindInst *UI = dyn_cast<UnwindInst>(TI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 177 | UnwindInst::destroyThis(UI); |
| 178 | else |
| 179 | assert(0 && "Unknown TerminatorInst-inherited class in ~Value."); |
Chris Lattner | 13daba07 | 2007-12-10 01:51:22 +0000 | [diff] [blame] | 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)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 183 | AllocaInst::destroyThis(AI); |
Chris Lattner | 13daba07 | 2007-12-10 01:51:22 +0000 | [diff] [blame] | 184 | else if(MallocInst* MI = dyn_cast<MallocInst>(UI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 185 | MallocInst::destroyThis(MI); |
| 186 | else |
| 187 | assert(0 && "Unknown AllocationInst-inherited class in ~Value."); |
Chris Lattner | d05f4ba | 2007-12-10 01:48:29 +0000 | [diff] [blame] | 188 | } else if(CastInst* CI = dyn_cast<CastInst>(this)) { |
| 189 | if(BitCastInst* BCI = dyn_cast<BitCastInst>(CI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 190 | BitCastInst::destroyThis(BCI); |
Chris Lattner | d05f4ba | 2007-12-10 01:48:29 +0000 | [diff] [blame] | 191 | else if(FPExtInst* FPEI = dyn_cast<FPExtInst>(CI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 192 | FPExtInst::destroyThis(FPEI); |
Chris Lattner | d05f4ba | 2007-12-10 01:48:29 +0000 | [diff] [blame] | 193 | else if(FPToSIInst* FPSII = dyn_cast<FPToSIInst>(CI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 194 | FPToSIInst::destroyThis(FPSII); |
Chris Lattner | d05f4ba | 2007-12-10 01:48:29 +0000 | [diff] [blame] | 195 | else if(FPToUIInst* FPUII = dyn_cast<FPToUIInst>(CI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 196 | FPToUIInst::destroyThis(FPUII); |
Chris Lattner | d05f4ba | 2007-12-10 01:48:29 +0000 | [diff] [blame] | 197 | else if(FPTruncInst* FPTI = dyn_cast<FPTruncInst>(CI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 198 | FPTruncInst::destroyThis(FPTI); |
Chris Lattner | d05f4ba | 2007-12-10 01:48:29 +0000 | [diff] [blame] | 199 | else if(IntToPtrInst* I2PI = dyn_cast<IntToPtrInst>(CI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 200 | IntToPtrInst::destroyThis(I2PI); |
Chris Lattner | d05f4ba | 2007-12-10 01:48:29 +0000 | [diff] [blame] | 201 | else if(PtrToIntInst* P2II = dyn_cast<PtrToIntInst>(CI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 202 | PtrToIntInst::destroyThis(P2II); |
Chris Lattner | d05f4ba | 2007-12-10 01:48:29 +0000 | [diff] [blame] | 203 | else if(SExtInst* SEI = dyn_cast<SExtInst>(CI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 204 | SExtInst::destroyThis(SEI); |
Chris Lattner | d05f4ba | 2007-12-10 01:48:29 +0000 | [diff] [blame] | 205 | else if(SIToFPInst* SIFPI = dyn_cast<SIToFPInst>(CI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 206 | SIToFPInst::destroyThis(SIFPI); |
Chris Lattner | d05f4ba | 2007-12-10 01:48:29 +0000 | [diff] [blame] | 207 | else if(TruncInst* TI = dyn_cast<TruncInst>(CI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 208 | TruncInst::destroyThis(TI); |
Chris Lattner | d05f4ba | 2007-12-10 01:48:29 +0000 | [diff] [blame] | 209 | else if(UIToFPInst* UIFPI = dyn_cast<UIToFPInst>(CI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 210 | UIToFPInst::destroyThis(UIFPI); |
Chris Lattner | d05f4ba | 2007-12-10 01:48:29 +0000 | [diff] [blame] | 211 | else if(ZExtInst* ZEI = dyn_cast<ZExtInst>(CI)) |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 212 | ZExtInst::destroyThis(ZEI); |
| 213 | else |
| 214 | assert(0 && "Unknown CastInst-inherited class in ~Value."); |
| 215 | } |
| 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); |
| 222 | else |
| 223 | assert(0 && "Unknown UnaryInstruction-inherited class in ~Value."); |
| 224 | } |
| 225 | else if (DummyInst *DI = dyn_cast<DummyInst>(this)) |
| 226 | DummyInst::destroyThis(DI); |
| 227 | else |
| 228 | assert(0 && "Unknown Instruction-inherited class in ~Value."); |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void Value::destroyThis(Value*v) |
| 234 | { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 235 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | 874ddad | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 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 |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 239 | // still being referenced. The value in question should be printed as |
Chris Lattner | 874ddad | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 240 | // a <badref> |
| 241 | // |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 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) |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 245 | DOUT << "Use still stuck around after Def is destroyed:" |
| 246 | << **I << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 247 | } |
| 248 | #endif |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 249 | assert(v->use_empty() && "Uses remain when a value is destroyed!"); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 250 | |
Chris Lattner | af867a3 | 2007-03-20 00:18:10 +0000 | [diff] [blame] | 251 | // If this value is named, destroy the name. This should not be in a symtab |
| 252 | // at this point. |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 253 | if (v->Name) |
| 254 | v->Name->Destroy(); |
Chris Lattner | af867a3 | 2007-03-20 00:18:10 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 256 | // There should be no uses of this object anymore, remove it. |
Gordon Henriksen | 3e5be66 | 2007-12-09 22:46:10 +0000 | [diff] [blame] | 257 | LeakDetector::removeGarbageObject(v); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 260 | /// hasNUses - Return true if this Value has exactly N users. |
| 261 | /// |
| 262 | bool Value::hasNUses(unsigned N) const { |
| 263 | use_const_iterator UI = use_begin(), E = use_end(); |
| 264 | |
| 265 | for (; N; --N, ++UI) |
| 266 | if (UI == E) return false; // Too few. |
| 267 | return UI == E; |
| 268 | } |
| 269 | |
Chris Lattner | d36552f | 2005-02-23 16:51:11 +0000 | [diff] [blame] | 270 | /// hasNUsesOrMore - Return true if this value has N users or more. This is |
| 271 | /// logically equivalent to getNumUses() >= N. |
| 272 | /// |
| 273 | bool Value::hasNUsesOrMore(unsigned N) const { |
| 274 | use_const_iterator UI = use_begin(), E = use_end(); |
| 275 | |
| 276 | for (; N; --N, ++UI) |
| 277 | if (UI == E) return false; // Too few. |
| 278 | |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 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 |
| 285 | /// values. |
| 286 | unsigned Value::getNumUses() const { |
| 287 | return (unsigned)std::distance(use_begin(), use_end()); |
| 288 | } |
| 289 | |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 290 | static bool getSymTab(Value *V, ValueSymbolTable *&ST) { |
Chris Lattner | 569c8ac | 2007-02-11 19:12:18 +0000 | [diff] [blame] | 291 | ST = 0; |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 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(); |
| 305 | } else { |
| 306 | assert(isa<Constant>(V) && "Unknown value type!"); |
| 307 | return true; // no name is setable for this. |
| 308 | } |
| 309 | return false; |
| 310 | } |
Chris Lattner | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 5109a88 | 2007-08-10 15:34:35 +0000 | [diff] [blame] | 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(); |
| 318 | } |
| 319 | |
| 320 | /// getNameLen - Return the length of the string, correctly handling nul |
| 321 | /// characters embedded into them. |
| 322 | unsigned Value::getNameLen() const { |
Chris Lattner | 2be9ec5 | 2007-09-28 20:09:40 +0000 | [diff] [blame] | 323 | return Name ? Name->getKeyLength() : 0; |
Chris Lattner | 5109a88 | 2007-08-10 15:34:35 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | |
Chris Lattner | fd27ed9 | 2007-02-15 18:53:54 +0000 | [diff] [blame] | 327 | std::string Value::getNameStr() const { |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 328 | if (Name == 0) return ""; |
| 329 | return std::string(Name->getKeyData(), |
| 330 | Name->getKeyData()+Name->getKeyLength()); |
| 331 | } |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 332 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 333 | void Value::setName(const std::string &name) { |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 334 | setName(&name[0], name.size()); |
| 335 | } |
| 336 | |
Chris Lattner | cb9a626 | 2007-02-13 07:53:34 +0000 | [diff] [blame] | 337 | void Value::setName(const char *Name) { |
| 338 | setName(Name, Name ? strlen(Name) : 0); |
| 339 | } |
| 340 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 341 | void Value::setName(const char *NameStr, unsigned NameLen) { |
| 342 | if (NameLen == 0 && !hasName()) return; |
Jeff Cohen | b622c11 | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 343 | assert(getType() != Type::VoidTy && "Cannot assign a name to void values!"); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 344 | |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 345 | // Get the symbol table to update for this object. |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 346 | ValueSymbolTable *ST; |
| 347 | if (getSymTab(this, ST)) |
| 348 | return; // Cannot set a name on this value (e.g. constant). |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 350 | if (!ST) { // No symbol table to update? Just do the change. |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 351 | if (NameLen == 0) { |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 352 | // Free the name for this value. |
| 353 | Name->Destroy(); |
| 354 | Name = 0; |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 355 | return; |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 356 | } |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 357 | |
| 358 | if (Name) { |
| 359 | // Name isn't changing? |
| 360 | if (NameLen == Name->getKeyLength() && |
| 361 | !memcmp(Name->getKeyData(), NameStr, NameLen)) |
| 362 | return; |
| 363 | Name->Destroy(); |
| 364 | } |
| 365 | |
| 366 | // NOTE: Could optimize for the case the name is shrinking to not deallocate |
| 367 | // then reallocated. |
| 368 | |
| 369 | // Create the new name. |
| 370 | Name = ValueName::Create(NameStr, NameStr+NameLen); |
| 371 | Name->setValue(this); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 372 | return; |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 373 | } |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 374 | |
| 375 | // NOTE: Could optimize for the case the name is shrinking to not deallocate |
| 376 | // then reallocated. |
| 377 | if (hasName()) { |
| 378 | // Name isn't changing? |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 379 | if (NameLen == Name->getKeyLength() && |
| 380 | !memcmp(Name->getKeyData(), NameStr, NameLen)) |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 381 | return; |
| 382 | |
| 383 | // Remove old name. |
| 384 | ST->removeValueName(Name); |
| 385 | Name->Destroy(); |
| 386 | Name = 0; |
| 387 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 388 | if (NameLen == 0) |
| 389 | return; |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // Name is changing to something new. |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 393 | Name = ST->createValueName(NameStr, NameLen, this); |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 396 | |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 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) { |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 400 | ValueSymbolTable *ST = 0; |
| 401 | // If this value has a name, drop it. |
| 402 | if (hasName()) { |
| 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 |
| 406 | // it has one. |
| 407 | if (V->hasName()) V->setName(0, 0); |
| 408 | return; // Cannot set a name on this value (e.g. constant). |
| 409 | } |
| 410 | |
| 411 | // Remove old name. |
| 412 | if (ST) |
| 413 | ST->removeValueName(Name); |
| 414 | Name->Destroy(); |
| 415 | Name = 0; |
| 416 | } |
| 417 | |
| 418 | // Now we know that this has no name. |
| 419 | |
| 420 | // If V has no name either, we're done. |
| 421 | if (!V->hasName()) return; |
| 422 | |
| 423 | // Get this's symtab if we didn't before. |
| 424 | if (!ST) { |
| 425 | if (getSymTab(this, ST)) { |
| 426 | // Clear V's name. |
| 427 | V->setName(0, 0); |
| 428 | return; // Cannot set a name on this value (e.g. constant). |
| 429 | } |
| 430 | } |
| 431 | |
| 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!"); |
| 436 | |
| 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. |
| 439 | if (ST == VST) { |
| 440 | // Take the name! |
| 441 | Name = V->Name; |
| 442 | V->Name = 0; |
| 443 | Name->setValue(this); |
Chris Lattner | cba18e3 | 2007-02-11 01:04:09 +0000 | [diff] [blame] | 444 | return; |
| 445 | } |
| 446 | |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 447 | // Otherwise, things are slightly more complex. Remove V's name from VST and |
| 448 | // then reinsert it into ST. |
| 449 | |
| 450 | if (VST) |
| 451 | VST->removeValueName(V->Name); |
| 452 | Name = V->Name; |
| 453 | V->Name = 0; |
| 454 | Name->setValue(this); |
| 455 | |
| 456 | if (ST) |
| 457 | ST->reinsertValue(this); |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 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 |
| 465 | // this problem. |
| 466 | // |
| 467 | void Value::uncheckedReplaceAllUsesWith(Value *New) { |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 468 | while (!use_empty()) { |
| 469 | Use &U = *UseList; |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 470 | // Must handle Constants specially, we cannot call replaceUsesOfWith on a |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 471 | // constant because they are uniqued. |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 472 | if (Constant *C = dyn_cast<Constant>(U.getUser())) { |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 473 | if (!isa<GlobalValue>(C)) { |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 474 | C->replaceUsesOfWithOnConstant(this, New, &U); |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 475 | continue; |
| 476 | } |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 477 | } |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 478 | |
| 479 | U.set(New); |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 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!"); |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 488 | |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 489 | uncheckedReplaceAllUsesWith(New); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 492 | //===----------------------------------------------------------------------===// |
| 493 | // User Class |
| 494 | //===----------------------------------------------------------------------===// |
| 495 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 496 | // replaceUsesOfWith - Replaces all references to the "From" definition with |
| 497 | // references to the "To" definition. |
| 498 | // |
| 499 | void User::replaceUsesOfWith(Value *From, Value *To) { |
| 500 | if (From == To) return; // Duh what? |
| 501 | |
Reid Spencer | bbddbf3 | 2004-07-18 00:01:50 +0000 | [diff] [blame] | 502 | assert(!isa<Constant>(this) || isa<GlobalValue>(this) && |
Chris Lattner | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 503 | "Cannot call User::replaceUsesofWith on a constant!"); |
| 504 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 505 | for (unsigned i = 0, E = getNumOperands(); i != E; ++i) |
| 506 | if (getOperand(i) == From) { // Is This operand is pointing to oldval? |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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". |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 510 | setOperand(i, To); // Fix it now... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 511 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 512 | } |
Reid Spencer | bbddbf3 | 2004-07-18 00:01:50 +0000 | [diff] [blame] | 513 | |