Nicolas Capens | 0cf2006 | 2016-09-26 15:02:51 -0400 | [diff] [blame] | 1 | //===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file declares the Value class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_IR_VALUE_H |
| 15 | #define LLVM_IR_VALUE_H |
| 16 | |
| 17 | #include "llvm/ADT/iterator_range.h" |
| 18 | #include "llvm/IR/Use.h" |
| 19 | #include "llvm/Support/CBindingWrapping.h" |
| 20 | #include "llvm/Support/Casting.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
| 24 | class APInt; |
| 25 | class Argument; |
| 26 | class AssemblyAnnotationWriter; |
| 27 | class BasicBlock; |
| 28 | class Constant; |
| 29 | class ConstantData; |
| 30 | class ConstantAggregate; |
| 31 | class DataLayout; |
| 32 | class Function; |
| 33 | class GlobalAlias; |
| 34 | class GlobalIFunc; |
| 35 | class GlobalIndirectSymbol; |
| 36 | class GlobalObject; |
| 37 | class GlobalValue; |
| 38 | class GlobalVariable; |
| 39 | class InlineAsm; |
| 40 | class Instruction; |
| 41 | class LLVMContext; |
| 42 | class Module; |
| 43 | class ModuleSlotTracker; |
| 44 | class StringRef; |
| 45 | class Twine; |
| 46 | class Type; |
| 47 | class ValueHandleBase; |
| 48 | class ValueSymbolTable; |
| 49 | class raw_ostream; |
| 50 | |
| 51 | template<typename ValueTy> class StringMapEntry; |
| 52 | typedef StringMapEntry<Value*> ValueName; |
| 53 | |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | // Value Class |
| 56 | //===----------------------------------------------------------------------===// |
| 57 | |
| 58 | /// \brief LLVM Value Representation |
| 59 | /// |
| 60 | /// This is a very important LLVM class. It is the base class of all values |
| 61 | /// computed by a program that may be used as operands to other values. Value is |
| 62 | /// the super class of other important classes such as Instruction and Function. |
| 63 | /// All Values have a Type. Type is not a subclass of Value. Some values can |
| 64 | /// have a name and they belong to some Module. Setting the name on the Value |
| 65 | /// automatically updates the module's symbol table. |
| 66 | /// |
| 67 | /// Every value has a "use list" that keeps track of which other Values are |
| 68 | /// using this Value. A Value can also have an arbitrary number of ValueHandle |
| 69 | /// objects that watch it and listen to RAUW and Destroy events. See |
| 70 | /// llvm/IR/ValueHandle.h for details. |
| 71 | class Value { |
| 72 | Type *VTy; |
| 73 | Use *UseList; |
| 74 | |
| 75 | friend class ValueAsMetadata; // Allow access to IsUsedByMD. |
| 76 | friend class ValueHandleBase; |
| 77 | |
| 78 | const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast) |
| 79 | unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this? |
| 80 | protected: |
| 81 | /// \brief Hold subclass data that can be dropped. |
| 82 | /// |
| 83 | /// This member is similar to SubclassData, however it is for holding |
| 84 | /// information which may be used to aid optimization, but which may be |
| 85 | /// cleared to zero without affecting conservative interpretation. |
| 86 | unsigned char SubclassOptionalData : 7; |
| 87 | |
| 88 | private: |
| 89 | /// \brief Hold arbitrary subclass data. |
| 90 | /// |
| 91 | /// This member is defined by this class, but is not used for anything. |
| 92 | /// Subclasses can use it to hold whatever state they find useful. This |
| 93 | /// field is initialized to zero by the ctor. |
| 94 | unsigned short SubclassData; |
| 95 | |
| 96 | protected: |
| 97 | /// \brief The number of operands in the subclass. |
| 98 | /// |
| 99 | /// This member is defined by this class, but not used for anything. |
| 100 | /// Subclasses can use it to store their number of operands, if they have |
| 101 | /// any. |
| 102 | /// |
| 103 | /// This is stored here to save space in User on 64-bit hosts. Since most |
| 104 | /// instances of Value have operands, 32-bit hosts aren't significantly |
| 105 | /// affected. |
| 106 | /// |
| 107 | /// Note, this should *NOT* be used directly by any class other than User. |
| 108 | /// User uses this value to find the Use list. |
| 109 | enum : unsigned { NumUserOperandsBits = 28 }; |
| 110 | unsigned NumUserOperands : NumUserOperandsBits; |
| 111 | |
| 112 | // Use the same type as the bitfield above so that MSVC will pack them. |
| 113 | unsigned IsUsedByMD : 1; |
| 114 | unsigned HasName : 1; |
| 115 | unsigned HasHungOffUses : 1; |
| 116 | unsigned HasDescriptor : 1; |
| 117 | |
| 118 | private: |
| 119 | template <typename UseT> // UseT == 'Use' or 'const Use' |
| 120 | class use_iterator_impl |
| 121 | : public std::iterator<std::forward_iterator_tag, UseT *> { |
| 122 | UseT *U; |
| 123 | explicit use_iterator_impl(UseT *u) : U(u) {} |
| 124 | friend class Value; |
| 125 | |
| 126 | public: |
| 127 | use_iterator_impl() : U() {} |
| 128 | |
| 129 | bool operator==(const use_iterator_impl &x) const { return U == x.U; } |
| 130 | bool operator!=(const use_iterator_impl &x) const { return !operator==(x); } |
| 131 | |
| 132 | use_iterator_impl &operator++() { // Preincrement |
| 133 | assert(U && "Cannot increment end iterator!"); |
| 134 | U = U->getNext(); |
| 135 | return *this; |
| 136 | } |
| 137 | use_iterator_impl operator++(int) { // Postincrement |
| 138 | auto tmp = *this; |
| 139 | ++*this; |
| 140 | return tmp; |
| 141 | } |
| 142 | |
| 143 | UseT &operator*() const { |
| 144 | assert(U && "Cannot dereference end iterator!"); |
| 145 | return *U; |
| 146 | } |
| 147 | |
| 148 | UseT *operator->() const { return &operator*(); } |
| 149 | |
| 150 | operator use_iterator_impl<const UseT>() const { |
| 151 | return use_iterator_impl<const UseT>(U); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | template <typename UserTy> // UserTy == 'User' or 'const User' |
| 156 | class user_iterator_impl |
| 157 | : public std::iterator<std::forward_iterator_tag, UserTy *> { |
| 158 | use_iterator_impl<Use> UI; |
| 159 | explicit user_iterator_impl(Use *U) : UI(U) {} |
| 160 | friend class Value; |
| 161 | |
| 162 | public: |
| 163 | user_iterator_impl() {} |
| 164 | |
| 165 | bool operator==(const user_iterator_impl &x) const { return UI == x.UI; } |
| 166 | bool operator!=(const user_iterator_impl &x) const { return !operator==(x); } |
| 167 | |
| 168 | /// \brief Returns true if this iterator is equal to user_end() on the value. |
| 169 | bool atEnd() const { return *this == user_iterator_impl(); } |
| 170 | |
| 171 | user_iterator_impl &operator++() { // Preincrement |
| 172 | ++UI; |
| 173 | return *this; |
| 174 | } |
| 175 | user_iterator_impl operator++(int) { // Postincrement |
| 176 | auto tmp = *this; |
| 177 | ++*this; |
| 178 | return tmp; |
| 179 | } |
| 180 | |
| 181 | // Retrieve a pointer to the current User. |
| 182 | UserTy *operator*() const { |
| 183 | return UI->getUser(); |
| 184 | } |
| 185 | |
| 186 | UserTy *operator->() const { return operator*(); } |
| 187 | |
| 188 | operator user_iterator_impl<const UserTy>() const { |
| 189 | return user_iterator_impl<const UserTy>(*UI); |
| 190 | } |
| 191 | |
| 192 | Use &getUse() const { return *UI; } |
| 193 | }; |
| 194 | |
| 195 | void operator=(const Value &) = delete; |
| 196 | Value(const Value &) = delete; |
| 197 | |
| 198 | protected: |
| 199 | Value(Type *Ty, unsigned scid); |
| 200 | public: |
| 201 | virtual ~Value(); |
| 202 | |
| 203 | /// \brief Support for debugging, callable in GDB: V->dump() |
| 204 | void dump() const; |
| 205 | |
| 206 | /// \brief Implement operator<< on Value. |
| 207 | /// @{ |
| 208 | void print(raw_ostream &O, bool IsForDebug = false) const; |
| 209 | void print(raw_ostream &O, ModuleSlotTracker &MST, |
| 210 | bool IsForDebug = false) const; |
| 211 | /// @} |
| 212 | |
| 213 | /// \brief Print the name of this Value out to the specified raw_ostream. |
| 214 | /// |
| 215 | /// This is useful when you just want to print 'int %reg126', not the |
| 216 | /// instruction that generated it. If you specify a Module for context, then |
| 217 | /// even constanst get pretty-printed; for example, the type of a null |
| 218 | /// pointer is printed symbolically. |
| 219 | /// @{ |
| 220 | void printAsOperand(raw_ostream &O, bool PrintType = true, |
| 221 | const Module *M = nullptr) const; |
| 222 | void printAsOperand(raw_ostream &O, bool PrintType, |
| 223 | ModuleSlotTracker &MST) const; |
| 224 | /// @} |
| 225 | |
| 226 | /// \brief All values are typed, get the type of this value. |
| 227 | Type *getType() const { return VTy; } |
| 228 | |
| 229 | /// \brief All values hold a context through their type. |
| 230 | LLVMContext &getContext() const; |
| 231 | |
| 232 | // \brief All values can potentially be named. |
| 233 | bool hasName() const { return HasName; } |
| 234 | ValueName *getValueName() const; |
| 235 | void setValueName(ValueName *VN); |
| 236 | |
| 237 | private: |
| 238 | void destroyValueName(); |
| 239 | void setNameImpl(const Twine &Name); |
| 240 | |
| 241 | public: |
| 242 | /// \brief Return a constant reference to the value's name. |
| 243 | /// |
| 244 | /// This is cheap and guaranteed to return the same reference as long as the |
| 245 | /// value is not modified. |
| 246 | StringRef getName() const; |
| 247 | |
| 248 | /// \brief Change the name of the value. |
| 249 | /// |
| 250 | /// Choose a new unique name if the provided name is taken. |
| 251 | /// |
| 252 | /// \param Name The new name; or "" if the value's name should be removed. |
| 253 | void setName(const Twine &Name); |
| 254 | |
| 255 | |
| 256 | /// \brief Transfer the name from V to this value. |
| 257 | /// |
| 258 | /// After taking V's name, sets V's name to empty. |
| 259 | /// |
| 260 | /// \note It is an error to call V->takeName(V). |
| 261 | void takeName(Value *V); |
| 262 | |
| 263 | /// \brief Change all uses of this to point to a new Value. |
| 264 | /// |
| 265 | /// Go through the uses list for this definition and make each use point to |
| 266 | /// "V" instead of "this". After this completes, 'this's use list is |
| 267 | /// guaranteed to be empty. |
| 268 | void replaceAllUsesWith(Value *V); |
| 269 | |
| 270 | /// replaceUsesOutsideBlock - Go through the uses list for this definition and |
| 271 | /// make each use point to "V" instead of "this" when the use is outside the |
| 272 | /// block. 'This's use list is expected to have at least one element. |
| 273 | /// Unlike replaceAllUsesWith this function does not support basic block |
| 274 | /// values or constant users. |
| 275 | void replaceUsesOutsideBlock(Value *V, BasicBlock *BB); |
| 276 | |
| 277 | //---------------------------------------------------------------------- |
| 278 | // Methods for handling the chain of uses of this Value. |
| 279 | // |
| 280 | // Materializing a function can introduce new uses, so these methods come in |
| 281 | // two variants: |
| 282 | // The methods that start with materialized_ check the uses that are |
| 283 | // currently known given which functions are materialized. Be very careful |
| 284 | // when using them since you might not get all uses. |
| 285 | // The methods that don't start with materialized_ assert that modules is |
| 286 | // fully materialized. |
| 287 | void assertModuleIsMaterialized() const; |
| 288 | |
| 289 | bool use_empty() const { |
| 290 | assertModuleIsMaterialized(); |
| 291 | return UseList == nullptr; |
| 292 | } |
| 293 | |
| 294 | typedef use_iterator_impl<Use> use_iterator; |
| 295 | typedef use_iterator_impl<const Use> const_use_iterator; |
| 296 | use_iterator materialized_use_begin() { return use_iterator(UseList); } |
| 297 | const_use_iterator materialized_use_begin() const { |
| 298 | return const_use_iterator(UseList); |
| 299 | } |
| 300 | use_iterator use_begin() { |
| 301 | assertModuleIsMaterialized(); |
| 302 | return materialized_use_begin(); |
| 303 | } |
| 304 | const_use_iterator use_begin() const { |
| 305 | assertModuleIsMaterialized(); |
| 306 | return materialized_use_begin(); |
| 307 | } |
| 308 | use_iterator use_end() { return use_iterator(); } |
| 309 | const_use_iterator use_end() const { return const_use_iterator(); } |
| 310 | iterator_range<use_iterator> materialized_uses() { |
| 311 | return make_range(materialized_use_begin(), use_end()); |
| 312 | } |
| 313 | iterator_range<const_use_iterator> materialized_uses() const { |
| 314 | return make_range(materialized_use_begin(), use_end()); |
| 315 | } |
| 316 | iterator_range<use_iterator> uses() { |
| 317 | assertModuleIsMaterialized(); |
| 318 | return materialized_uses(); |
| 319 | } |
| 320 | iterator_range<const_use_iterator> uses() const { |
| 321 | assertModuleIsMaterialized(); |
| 322 | return materialized_uses(); |
| 323 | } |
| 324 | |
| 325 | bool user_empty() const { |
| 326 | assertModuleIsMaterialized(); |
| 327 | return UseList == nullptr; |
| 328 | } |
| 329 | |
| 330 | typedef user_iterator_impl<User> user_iterator; |
| 331 | typedef user_iterator_impl<const User> const_user_iterator; |
| 332 | user_iterator materialized_user_begin() { return user_iterator(UseList); } |
| 333 | const_user_iterator materialized_user_begin() const { |
| 334 | return const_user_iterator(UseList); |
| 335 | } |
| 336 | user_iterator user_begin() { |
| 337 | assertModuleIsMaterialized(); |
| 338 | return materialized_user_begin(); |
| 339 | } |
| 340 | const_user_iterator user_begin() const { |
| 341 | assertModuleIsMaterialized(); |
| 342 | return materialized_user_begin(); |
| 343 | } |
| 344 | user_iterator user_end() { return user_iterator(); } |
| 345 | const_user_iterator user_end() const { return const_user_iterator(); } |
| 346 | User *user_back() { |
| 347 | assertModuleIsMaterialized(); |
| 348 | return *materialized_user_begin(); |
| 349 | } |
| 350 | const User *user_back() const { |
| 351 | assertModuleIsMaterialized(); |
| 352 | return *materialized_user_begin(); |
| 353 | } |
| 354 | iterator_range<user_iterator> materialized_users() { |
| 355 | return make_range(materialized_user_begin(), user_end()); |
| 356 | } |
| 357 | iterator_range<const_user_iterator> materialized_users() const { |
| 358 | return make_range(materialized_user_begin(), user_end()); |
| 359 | } |
| 360 | iterator_range<user_iterator> users() { |
| 361 | assertModuleIsMaterialized(); |
| 362 | return materialized_users(); |
| 363 | } |
| 364 | iterator_range<const_user_iterator> users() const { |
| 365 | assertModuleIsMaterialized(); |
| 366 | return materialized_users(); |
| 367 | } |
| 368 | |
| 369 | /// \brief Return true if there is exactly one user of this value. |
| 370 | /// |
| 371 | /// This is specialized because it is a common request and does not require |
| 372 | /// traversing the whole use list. |
| 373 | bool hasOneUse() const { |
| 374 | const_use_iterator I = use_begin(), E = use_end(); |
| 375 | if (I == E) return false; |
| 376 | return ++I == E; |
| 377 | } |
| 378 | |
| 379 | /// \brief Return true if this Value has exactly N users. |
| 380 | bool hasNUses(unsigned N) const; |
| 381 | |
| 382 | /// \brief Return true if this value has N users or more. |
| 383 | /// |
| 384 | /// This is logically equivalent to getNumUses() >= N. |
| 385 | bool hasNUsesOrMore(unsigned N) const; |
| 386 | |
| 387 | /// \brief Check if this value is used in the specified basic block. |
| 388 | bool isUsedInBasicBlock(const BasicBlock *BB) const; |
| 389 | |
| 390 | /// \brief This method computes the number of uses of this Value. |
| 391 | /// |
| 392 | /// This is a linear time operation. Use hasOneUse, hasNUses, or |
| 393 | /// hasNUsesOrMore to check for specific values. |
| 394 | unsigned getNumUses() const; |
| 395 | |
| 396 | /// \brief This method should only be used by the Use class. |
| 397 | void addUse(Use &U) { U.addToList(&UseList); } |
| 398 | |
| 399 | /// \brief Concrete subclass of this. |
| 400 | /// |
| 401 | /// An enumeration for keeping track of the concrete subclass of Value that |
| 402 | /// is actually instantiated. Values of this enumeration are kept in the |
| 403 | /// Value classes SubclassID field. They are used for concrete type |
| 404 | /// identification. |
| 405 | enum ValueTy { |
| 406 | #define HANDLE_VALUE(Name) Name##Val, |
| 407 | #include "llvm/IR/Value.def" |
| 408 | |
| 409 | // Markers: |
| 410 | #define HANDLE_CONSTANT_MARKER(Marker, Constant) Marker = Constant##Val, |
| 411 | #include "llvm/IR/Value.def" |
| 412 | }; |
| 413 | |
| 414 | /// \brief Return an ID for the concrete type of this object. |
| 415 | /// |
| 416 | /// This is used to implement the classof checks. This should not be used |
| 417 | /// for any other purpose, as the values may change as LLVM evolves. Also, |
| 418 | /// note that for instructions, the Instruction's opcode is added to |
| 419 | /// InstructionVal. So this means three things: |
| 420 | /// # there is no value with code InstructionVal (no opcode==0). |
| 421 | /// # there are more possible values for the value type than in ValueTy enum. |
| 422 | /// # the InstructionVal enumerator must be the highest valued enumerator in |
| 423 | /// the ValueTy enum. |
| 424 | unsigned getValueID() const { |
| 425 | return SubclassID; |
| 426 | } |
| 427 | |
| 428 | /// \brief Return the raw optional flags value contained in this value. |
| 429 | /// |
| 430 | /// This should only be used when testing two Values for equivalence. |
| 431 | unsigned getRawSubclassOptionalData() const { |
| 432 | return SubclassOptionalData; |
| 433 | } |
| 434 | |
| 435 | /// \brief Clear the optional flags contained in this value. |
| 436 | void clearSubclassOptionalData() { |
| 437 | SubclassOptionalData = 0; |
| 438 | } |
| 439 | |
| 440 | /// \brief Check the optional flags for equality. |
| 441 | bool hasSameSubclassOptionalData(const Value *V) const { |
| 442 | return SubclassOptionalData == V->SubclassOptionalData; |
| 443 | } |
| 444 | |
| 445 | /// \brief Return true if there is a value handle associated with this value. |
| 446 | bool hasValueHandle() const { return HasValueHandle; } |
| 447 | |
| 448 | /// \brief Return true if there is metadata referencing this value. |
| 449 | bool isUsedByMetadata() const { return IsUsedByMD; } |
| 450 | |
| 451 | /// \brief Strip off pointer casts, all-zero GEPs, and aliases. |
| 452 | /// |
| 453 | /// Returns the original uncasted value. If this is called on a non-pointer |
| 454 | /// value, it returns 'this'. |
| 455 | Value *stripPointerCasts(); |
| 456 | const Value *stripPointerCasts() const { |
| 457 | return const_cast<Value*>(this)->stripPointerCasts(); |
| 458 | } |
| 459 | |
| 460 | /// \brief Strip off pointer casts and all-zero GEPs. |
| 461 | /// |
| 462 | /// Returns the original uncasted value. If this is called on a non-pointer |
| 463 | /// value, it returns 'this'. |
| 464 | Value *stripPointerCastsNoFollowAliases(); |
| 465 | const Value *stripPointerCastsNoFollowAliases() const { |
| 466 | return const_cast<Value*>(this)->stripPointerCastsNoFollowAliases(); |
| 467 | } |
| 468 | |
| 469 | /// \brief Strip off pointer casts and all-constant inbounds GEPs. |
| 470 | /// |
| 471 | /// Returns the original pointer value. If this is called on a non-pointer |
| 472 | /// value, it returns 'this'. |
| 473 | Value *stripInBoundsConstantOffsets(); |
| 474 | const Value *stripInBoundsConstantOffsets() const { |
| 475 | return const_cast<Value*>(this)->stripInBoundsConstantOffsets(); |
| 476 | } |
| 477 | |
| 478 | /// \brief Accumulate offsets from \a stripInBoundsConstantOffsets(). |
| 479 | /// |
| 480 | /// Stores the resulting constant offset stripped into the APInt provided. |
| 481 | /// The provided APInt will be extended or truncated as needed to be the |
| 482 | /// correct bitwidth for an offset of this pointer type. |
| 483 | /// |
| 484 | /// If this is called on a non-pointer value, it returns 'this'. |
| 485 | Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, |
| 486 | APInt &Offset); |
| 487 | const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, |
| 488 | APInt &Offset) const { |
| 489 | return const_cast<Value *>(this) |
| 490 | ->stripAndAccumulateInBoundsConstantOffsets(DL, Offset); |
| 491 | } |
| 492 | |
| 493 | /// \brief Strip off pointer casts and inbounds GEPs. |
| 494 | /// |
| 495 | /// Returns the original pointer value. If this is called on a non-pointer |
| 496 | /// value, it returns 'this'. |
| 497 | Value *stripInBoundsOffsets(); |
| 498 | const Value *stripInBoundsOffsets() const { |
| 499 | return const_cast<Value*>(this)->stripInBoundsOffsets(); |
| 500 | } |
| 501 | |
| 502 | /// \brief Returns the number of bytes known to be dereferenceable for the |
| 503 | /// pointer value. |
| 504 | /// |
| 505 | /// If CanBeNull is set by this function the pointer can either be null or be |
| 506 | /// dereferenceable up to the returned number of bytes. |
| 507 | unsigned getPointerDereferenceableBytes(const DataLayout &DL, |
| 508 | bool &CanBeNull) const; |
| 509 | |
| 510 | /// \brief Returns an alignment of the pointer value. |
| 511 | /// |
| 512 | /// Returns an alignment which is either specified explicitly, e.g. via |
| 513 | /// align attribute of a function argument, or guaranteed by DataLayout. |
| 514 | unsigned getPointerAlignment(const DataLayout &DL) const; |
| 515 | |
| 516 | /// \brief Translate PHI node to its predecessor from the given basic block. |
| 517 | /// |
| 518 | /// If this value is a PHI node with CurBB as its parent, return the value in |
| 519 | /// the PHI node corresponding to PredBB. If not, return ourself. This is |
| 520 | /// useful if you want to know the value something has in a predecessor |
| 521 | /// block. |
| 522 | Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB); |
| 523 | |
| 524 | const Value *DoPHITranslation(const BasicBlock *CurBB, |
| 525 | const BasicBlock *PredBB) const{ |
| 526 | return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB); |
| 527 | } |
| 528 | |
| 529 | /// \brief The maximum alignment for instructions. |
| 530 | /// |
| 531 | /// This is the greatest alignment value supported by load, store, and alloca |
| 532 | /// instructions, and global values. |
| 533 | static const unsigned MaxAlignmentExponent = 29; |
| 534 | static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent; |
| 535 | |
| 536 | /// \brief Mutate the type of this Value to be of the specified type. |
| 537 | /// |
| 538 | /// Note that this is an extremely dangerous operation which can create |
| 539 | /// completely invalid IR very easily. It is strongly recommended that you |
| 540 | /// recreate IR objects with the right types instead of mutating them in |
| 541 | /// place. |
| 542 | void mutateType(Type *Ty) { |
| 543 | VTy = Ty; |
| 544 | } |
| 545 | |
| 546 | /// \brief Sort the use-list. |
| 547 | /// |
| 548 | /// Sorts the Value's use-list by Cmp using a stable mergesort. Cmp is |
| 549 | /// expected to compare two \a Use references. |
| 550 | template <class Compare> void sortUseList(Compare Cmp); |
| 551 | |
| 552 | /// \brief Reverse the use-list. |
| 553 | void reverseUseList(); |
| 554 | |
| 555 | private: |
| 556 | /// \brief Merge two lists together. |
| 557 | /// |
| 558 | /// Merges \c L and \c R using \c Cmp. To enable stable sorts, always pushes |
| 559 | /// "equal" items from L before items from R. |
| 560 | /// |
| 561 | /// \return the first element in the list. |
| 562 | /// |
| 563 | /// \note Completely ignores \a Use::Prev (doesn't read, doesn't update). |
| 564 | template <class Compare> |
| 565 | static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) { |
| 566 | Use *Merged; |
| 567 | Use **Next = &Merged; |
| 568 | |
| 569 | for (;;) { |
| 570 | if (!L) { |
| 571 | *Next = R; |
| 572 | break; |
| 573 | } |
| 574 | if (!R) { |
| 575 | *Next = L; |
| 576 | break; |
| 577 | } |
| 578 | if (Cmp(*R, *L)) { |
| 579 | *Next = R; |
| 580 | Next = &R->Next; |
| 581 | R = R->Next; |
| 582 | } else { |
| 583 | *Next = L; |
| 584 | Next = &L->Next; |
| 585 | L = L->Next; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | return Merged; |
| 590 | } |
| 591 | |
| 592 | /// \brief Tail-recursive helper for \a mergeUseLists(). |
| 593 | /// |
| 594 | /// \param[out] Next the first element in the list. |
| 595 | template <class Compare> |
| 596 | static void mergeUseListsImpl(Use *L, Use *R, Use **Next, Compare Cmp); |
| 597 | |
| 598 | protected: |
| 599 | unsigned short getSubclassDataFromValue() const { return SubclassData; } |
| 600 | void setValueSubclassData(unsigned short D) { SubclassData = D; } |
| 601 | }; |
| 602 | |
| 603 | inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) { |
| 604 | V.print(OS); |
| 605 | return OS; |
| 606 | } |
| 607 | |
| 608 | void Use::set(Value *V) { |
| 609 | if (Val) removeFromList(); |
| 610 | Val = V; |
| 611 | if (V) V->addUse(*this); |
| 612 | } |
| 613 | |
| 614 | Value *Use::operator=(Value *RHS) { |
| 615 | set(RHS); |
| 616 | return RHS; |
| 617 | } |
| 618 | |
| 619 | const Use &Use::operator=(const Use &RHS) { |
| 620 | set(RHS.Val); |
| 621 | return *this; |
| 622 | } |
| 623 | |
| 624 | template <class Compare> void Value::sortUseList(Compare Cmp) { |
| 625 | if (!UseList || !UseList->Next) |
| 626 | // No need to sort 0 or 1 uses. |
| 627 | return; |
| 628 | |
| 629 | // Note: this function completely ignores Prev pointers until the end when |
| 630 | // they're fixed en masse. |
| 631 | |
| 632 | // Create a binomial vector of sorted lists, visiting uses one at a time and |
| 633 | // merging lists as necessary. |
| 634 | const unsigned MaxSlots = 32; |
| 635 | Use *Slots[MaxSlots]; |
| 636 | |
| 637 | // Collect the first use, turning it into a single-item list. |
| 638 | Use *Next = UseList->Next; |
| 639 | UseList->Next = nullptr; |
| 640 | unsigned NumSlots = 1; |
| 641 | Slots[0] = UseList; |
| 642 | |
| 643 | // Collect all but the last use. |
| 644 | while (Next->Next) { |
| 645 | Use *Current = Next; |
| 646 | Next = Current->Next; |
| 647 | |
| 648 | // Turn Current into a single-item list. |
| 649 | Current->Next = nullptr; |
| 650 | |
| 651 | // Save Current in the first available slot, merging on collisions. |
| 652 | unsigned I; |
| 653 | for (I = 0; I < NumSlots; ++I) { |
| 654 | if (!Slots[I]) |
| 655 | break; |
| 656 | |
| 657 | // Merge two lists, doubling the size of Current and emptying slot I. |
| 658 | // |
| 659 | // Since the uses in Slots[I] originally preceded those in Current, send |
| 660 | // Slots[I] in as the left parameter to maintain a stable sort. |
| 661 | Current = mergeUseLists(Slots[I], Current, Cmp); |
| 662 | Slots[I] = nullptr; |
| 663 | } |
| 664 | // Check if this is a new slot. |
| 665 | if (I == NumSlots) { |
| 666 | ++NumSlots; |
| 667 | assert(NumSlots <= MaxSlots && "Use list bigger than 2^32"); |
| 668 | } |
| 669 | |
| 670 | // Found an open slot. |
| 671 | Slots[I] = Current; |
| 672 | } |
| 673 | |
| 674 | // Merge all the lists together. |
| 675 | assert(Next && "Expected one more Use"); |
| 676 | assert(!Next->Next && "Expected only one Use"); |
| 677 | UseList = Next; |
| 678 | for (unsigned I = 0; I < NumSlots; ++I) |
| 679 | if (Slots[I]) |
| 680 | // Since the uses in Slots[I] originally preceded those in UseList, send |
| 681 | // Slots[I] in as the left parameter to maintain a stable sort. |
| 682 | UseList = mergeUseLists(Slots[I], UseList, Cmp); |
| 683 | |
| 684 | // Fix the Prev pointers. |
| 685 | for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) { |
| 686 | I->setPrev(Prev); |
| 687 | Prev = &I->Next; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | // isa - Provide some specializations of isa so that we don't have to include |
| 692 | // the subtype header files to test to see if the value is a subclass... |
| 693 | // |
| 694 | template <> struct isa_impl<Constant, Value> { |
| 695 | static inline bool doit(const Value &Val) { |
| 696 | return Val.getValueID() >= Value::ConstantFirstVal && |
| 697 | Val.getValueID() <= Value::ConstantLastVal; |
| 698 | } |
| 699 | }; |
| 700 | |
| 701 | template <> struct isa_impl<ConstantData, Value> { |
| 702 | static inline bool doit(const Value &Val) { |
| 703 | return Val.getValueID() >= Value::ConstantDataFirstVal && |
| 704 | Val.getValueID() <= Value::ConstantDataLastVal; |
| 705 | } |
| 706 | }; |
| 707 | |
| 708 | template <> struct isa_impl<ConstantAggregate, Value> { |
| 709 | static inline bool doit(const Value &Val) { |
| 710 | return Val.getValueID() >= Value::ConstantAggregateFirstVal && |
| 711 | Val.getValueID() <= Value::ConstantAggregateLastVal; |
| 712 | } |
| 713 | }; |
| 714 | |
| 715 | template <> struct isa_impl<Argument, Value> { |
| 716 | static inline bool doit (const Value &Val) { |
| 717 | return Val.getValueID() == Value::ArgumentVal; |
| 718 | } |
| 719 | }; |
| 720 | |
| 721 | template <> struct isa_impl<InlineAsm, Value> { |
| 722 | static inline bool doit(const Value &Val) { |
| 723 | return Val.getValueID() == Value::InlineAsmVal; |
| 724 | } |
| 725 | }; |
| 726 | |
| 727 | template <> struct isa_impl<Instruction, Value> { |
| 728 | static inline bool doit(const Value &Val) { |
| 729 | return Val.getValueID() >= Value::InstructionVal; |
| 730 | } |
| 731 | }; |
| 732 | |
| 733 | template <> struct isa_impl<BasicBlock, Value> { |
| 734 | static inline bool doit(const Value &Val) { |
| 735 | return Val.getValueID() == Value::BasicBlockVal; |
| 736 | } |
| 737 | }; |
| 738 | |
| 739 | template <> struct isa_impl<Function, Value> { |
| 740 | static inline bool doit(const Value &Val) { |
| 741 | return Val.getValueID() == Value::FunctionVal; |
| 742 | } |
| 743 | }; |
| 744 | |
| 745 | template <> struct isa_impl<GlobalVariable, Value> { |
| 746 | static inline bool doit(const Value &Val) { |
| 747 | return Val.getValueID() == Value::GlobalVariableVal; |
| 748 | } |
| 749 | }; |
| 750 | |
| 751 | template <> struct isa_impl<GlobalAlias, Value> { |
| 752 | static inline bool doit(const Value &Val) { |
| 753 | return Val.getValueID() == Value::GlobalAliasVal; |
| 754 | } |
| 755 | }; |
| 756 | |
| 757 | template <> struct isa_impl<GlobalIFunc, Value> { |
| 758 | static inline bool doit(const Value &Val) { |
| 759 | return Val.getValueID() == Value::GlobalIFuncVal; |
| 760 | } |
| 761 | }; |
| 762 | |
| 763 | template <> struct isa_impl<GlobalIndirectSymbol, Value> { |
| 764 | static inline bool doit(const Value &Val) { |
| 765 | return isa<GlobalAlias>(Val) || isa<GlobalIFunc>(Val); |
| 766 | } |
| 767 | }; |
| 768 | |
| 769 | template <> struct isa_impl<GlobalValue, Value> { |
| 770 | static inline bool doit(const Value &Val) { |
| 771 | return isa<GlobalObject>(Val) || isa<GlobalIndirectSymbol>(Val); |
| 772 | } |
| 773 | }; |
| 774 | |
| 775 | template <> struct isa_impl<GlobalObject, Value> { |
| 776 | static inline bool doit(const Value &Val) { |
| 777 | return isa<GlobalVariable>(Val) || isa<Function>(Val); |
| 778 | } |
| 779 | }; |
| 780 | |
| 781 | // Value* is only 4-byte aligned. |
| 782 | template<> |
| 783 | class PointerLikeTypeTraits<Value*> { |
| 784 | typedef Value* PT; |
| 785 | public: |
| 786 | static inline void *getAsVoidPointer(PT P) { return P; } |
| 787 | static inline PT getFromVoidPointer(void *P) { |
| 788 | return static_cast<PT>(P); |
| 789 | } |
| 790 | enum { NumLowBitsAvailable = 2 }; |
| 791 | }; |
| 792 | |
| 793 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
| 794 | DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef) |
| 795 | |
| 796 | // Specialized opaque value conversions. |
| 797 | inline Value **unwrap(LLVMValueRef *Vals) { |
| 798 | return reinterpret_cast<Value**>(Vals); |
| 799 | } |
| 800 | |
| 801 | template<typename T> |
| 802 | inline T **unwrap(LLVMValueRef *Vals, unsigned Length) { |
| 803 | #ifndef NDEBUG |
| 804 | for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I) |
| 805 | unwrap<T>(*I); // For side effect of calling assert on invalid usage. |
| 806 | #endif |
| 807 | (void)Length; |
| 808 | return reinterpret_cast<T**>(Vals); |
| 809 | } |
| 810 | |
| 811 | inline LLVMValueRef *wrap(const Value **Vals) { |
| 812 | return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals)); |
| 813 | } |
| 814 | |
| 815 | } // End llvm namespace |
| 816 | |
| 817 | #endif |