Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/Value.h - Definition of the Value class -------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file declares the Value class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_IR_VALUE_H |
| 14 | #define LLVM_IR_VALUE_H |
| 15 | |
| 16 | #include "llvm-c/Types.h" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | #include "llvm/ADT/iterator_range.h" |
| 20 | #include "llvm/IR/Use.h" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 21 | #include "llvm/Support/Alignment.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 22 | #include "llvm/Support/CBindingWrapping.h" |
| 23 | #include "llvm/Support/Casting.h" |
| 24 | #include <cassert> |
| 25 | #include <iterator> |
| 26 | #include <memory> |
| 27 | |
| 28 | namespace llvm { |
| 29 | |
| 30 | class APInt; |
| 31 | class Argument; |
| 32 | class BasicBlock; |
| 33 | class Constant; |
| 34 | class ConstantData; |
| 35 | class ConstantAggregate; |
| 36 | class DataLayout; |
| 37 | class Function; |
| 38 | class GlobalAlias; |
| 39 | class GlobalIFunc; |
| 40 | class GlobalIndirectSymbol; |
| 41 | class GlobalObject; |
| 42 | class GlobalValue; |
| 43 | class GlobalVariable; |
| 44 | class InlineAsm; |
| 45 | class Instruction; |
| 46 | class LLVMContext; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 47 | class MDNode; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 48 | class Module; |
| 49 | class ModuleSlotTracker; |
| 50 | class raw_ostream; |
| 51 | template<typename ValueTy> class StringMapEntry; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 52 | class Twine; |
| 53 | class Type; |
| 54 | class User; |
| 55 | |
| 56 | using ValueName = StringMapEntry<Value *>; |
| 57 | |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | // Value Class |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 62 | /// LLVM Value Representation |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 63 | /// |
| 64 | /// This is a very important LLVM class. It is the base class of all values |
| 65 | /// computed by a program that may be used as operands to other values. Value is |
| 66 | /// the super class of other important classes such as Instruction and Function. |
| 67 | /// All Values have a Type. Type is not a subclass of Value. Some values can |
| 68 | /// have a name and they belong to some Module. Setting the name on the Value |
| 69 | /// automatically updates the module's symbol table. |
| 70 | /// |
| 71 | /// Every value has a "use list" that keeps track of which other Values are |
| 72 | /// using this Value. A Value can also have an arbitrary number of ValueHandle |
| 73 | /// objects that watch it and listen to RAUW and Destroy events. See |
| 74 | /// llvm/IR/ValueHandle.h for details. |
| 75 | class Value { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 76 | Type *VTy; |
| 77 | Use *UseList; |
| 78 | |
| 79 | friend class ValueAsMetadata; // Allow access to IsUsedByMD. |
| 80 | friend class ValueHandleBase; |
| 81 | |
| 82 | const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast) |
| 83 | unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this? |
| 84 | |
| 85 | protected: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 86 | /// Hold subclass data that can be dropped. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 87 | /// |
| 88 | /// This member is similar to SubclassData, however it is for holding |
| 89 | /// information which may be used to aid optimization, but which may be |
| 90 | /// cleared to zero without affecting conservative interpretation. |
| 91 | unsigned char SubclassOptionalData : 7; |
| 92 | |
| 93 | private: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 94 | /// Hold arbitrary subclass data. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 95 | /// |
| 96 | /// This member is defined by this class, but is not used for anything. |
| 97 | /// Subclasses can use it to hold whatever state they find useful. This |
| 98 | /// field is initialized to zero by the ctor. |
| 99 | unsigned short SubclassData; |
| 100 | |
| 101 | protected: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 102 | /// The number of operands in the subclass. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 103 | /// |
| 104 | /// This member is defined by this class, but not used for anything. |
| 105 | /// Subclasses can use it to store their number of operands, if they have |
| 106 | /// any. |
| 107 | /// |
| 108 | /// This is stored here to save space in User on 64-bit hosts. Since most |
| 109 | /// instances of Value have operands, 32-bit hosts aren't significantly |
| 110 | /// affected. |
| 111 | /// |
| 112 | /// Note, this should *NOT* be used directly by any class other than User. |
| 113 | /// User uses this value to find the Use list. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 114 | enum : unsigned { NumUserOperandsBits = 27 }; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 115 | unsigned NumUserOperands : NumUserOperandsBits; |
| 116 | |
| 117 | // Use the same type as the bitfield above so that MSVC will pack them. |
| 118 | unsigned IsUsedByMD : 1; |
| 119 | unsigned HasName : 1; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 120 | unsigned HasMetadata : 1; // Has metadata attached to this? |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 121 | unsigned HasHungOffUses : 1; |
| 122 | unsigned HasDescriptor : 1; |
| 123 | |
| 124 | private: |
| 125 | template <typename UseT> // UseT == 'Use' or 'const Use' |
| 126 | class use_iterator_impl |
| 127 | : public std::iterator<std::forward_iterator_tag, UseT *> { |
| 128 | friend class Value; |
| 129 | |
| 130 | UseT *U; |
| 131 | |
| 132 | explicit use_iterator_impl(UseT *u) : U(u) {} |
| 133 | |
| 134 | public: |
| 135 | use_iterator_impl() : U() {} |
| 136 | |
| 137 | bool operator==(const use_iterator_impl &x) const { return U == x.U; } |
| 138 | bool operator!=(const use_iterator_impl &x) const { return !operator==(x); } |
| 139 | |
| 140 | use_iterator_impl &operator++() { // Preincrement |
| 141 | assert(U && "Cannot increment end iterator!"); |
| 142 | U = U->getNext(); |
| 143 | return *this; |
| 144 | } |
| 145 | |
| 146 | use_iterator_impl operator++(int) { // Postincrement |
| 147 | auto tmp = *this; |
| 148 | ++*this; |
| 149 | return tmp; |
| 150 | } |
| 151 | |
| 152 | UseT &operator*() const { |
| 153 | assert(U && "Cannot dereference end iterator!"); |
| 154 | return *U; |
| 155 | } |
| 156 | |
| 157 | UseT *operator->() const { return &operator*(); } |
| 158 | |
| 159 | operator use_iterator_impl<const UseT>() const { |
| 160 | return use_iterator_impl<const UseT>(U); |
| 161 | } |
| 162 | }; |
| 163 | |
| 164 | template <typename UserTy> // UserTy == 'User' or 'const User' |
| 165 | class user_iterator_impl |
| 166 | : public std::iterator<std::forward_iterator_tag, UserTy *> { |
| 167 | use_iterator_impl<Use> UI; |
| 168 | explicit user_iterator_impl(Use *U) : UI(U) {} |
| 169 | friend class Value; |
| 170 | |
| 171 | public: |
| 172 | user_iterator_impl() = default; |
| 173 | |
| 174 | bool operator==(const user_iterator_impl &x) const { return UI == x.UI; } |
| 175 | bool operator!=(const user_iterator_impl &x) const { return !operator==(x); } |
| 176 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 177 | /// Returns true if this iterator is equal to user_end() on the value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 178 | bool atEnd() const { return *this == user_iterator_impl(); } |
| 179 | |
| 180 | user_iterator_impl &operator++() { // Preincrement |
| 181 | ++UI; |
| 182 | return *this; |
| 183 | } |
| 184 | |
| 185 | user_iterator_impl operator++(int) { // Postincrement |
| 186 | auto tmp = *this; |
| 187 | ++*this; |
| 188 | return tmp; |
| 189 | } |
| 190 | |
| 191 | // Retrieve a pointer to the current User. |
| 192 | UserTy *operator*() const { |
| 193 | return UI->getUser(); |
| 194 | } |
| 195 | |
| 196 | UserTy *operator->() const { return operator*(); } |
| 197 | |
| 198 | operator user_iterator_impl<const UserTy>() const { |
| 199 | return user_iterator_impl<const UserTy>(*UI); |
| 200 | } |
| 201 | |
| 202 | Use &getUse() const { return *UI; } |
| 203 | }; |
| 204 | |
| 205 | protected: |
| 206 | Value(Type *Ty, unsigned scid); |
| 207 | |
| 208 | /// Value's destructor should be virtual by design, but that would require |
| 209 | /// that Value and all of its subclasses have a vtable that effectively |
| 210 | /// duplicates the information in the value ID. As a size optimization, the |
| 211 | /// destructor has been protected, and the caller should manually call |
| 212 | /// deleteValue. |
| 213 | ~Value(); // Use deleteValue() to delete a generic Value. |
| 214 | |
| 215 | public: |
| 216 | Value(const Value &) = delete; |
| 217 | Value &operator=(const Value &) = delete; |
| 218 | |
| 219 | /// Delete a pointer to a generic Value. |
| 220 | void deleteValue(); |
| 221 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 222 | /// Support for debugging, callable in GDB: V->dump() |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 223 | void dump() const; |
| 224 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 225 | /// Implement operator<< on Value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 226 | /// @{ |
| 227 | void print(raw_ostream &O, bool IsForDebug = false) const; |
| 228 | void print(raw_ostream &O, ModuleSlotTracker &MST, |
| 229 | bool IsForDebug = false) const; |
| 230 | /// @} |
| 231 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 232 | /// Print the name of this Value out to the specified raw_ostream. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 233 | /// |
| 234 | /// This is useful when you just want to print 'int %reg126', not the |
| 235 | /// instruction that generated it. If you specify a Module for context, then |
| 236 | /// even constanst get pretty-printed; for example, the type of a null |
| 237 | /// pointer is printed symbolically. |
| 238 | /// @{ |
| 239 | void printAsOperand(raw_ostream &O, bool PrintType = true, |
| 240 | const Module *M = nullptr) const; |
| 241 | void printAsOperand(raw_ostream &O, bool PrintType, |
| 242 | ModuleSlotTracker &MST) const; |
| 243 | /// @} |
| 244 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 245 | /// All values are typed, get the type of this value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 246 | Type *getType() const { return VTy; } |
| 247 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 248 | /// All values hold a context through their type. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 249 | LLVMContext &getContext() const; |
| 250 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 251 | // All values can potentially be named. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 252 | bool hasName() const { return HasName; } |
| 253 | ValueName *getValueName() const; |
| 254 | void setValueName(ValueName *VN); |
| 255 | |
| 256 | private: |
| 257 | void destroyValueName(); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 258 | enum class ReplaceMetadataUses { No, Yes }; |
| 259 | void doRAUW(Value *New, ReplaceMetadataUses); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 260 | void setNameImpl(const Twine &Name); |
| 261 | |
| 262 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 263 | /// Return a constant reference to the value's name. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 264 | /// |
| 265 | /// This guaranteed to return the same reference as long as the value is not |
| 266 | /// modified. If the value has a name, this does a hashtable lookup, so it's |
| 267 | /// not free. |
| 268 | StringRef getName() const; |
| 269 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 270 | /// Change the name of the value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 271 | /// |
| 272 | /// Choose a new unique name if the provided name is taken. |
| 273 | /// |
| 274 | /// \param Name The new name; or "" if the value's name should be removed. |
| 275 | void setName(const Twine &Name); |
| 276 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 277 | /// Transfer the name from V to this value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 278 | /// |
| 279 | /// After taking V's name, sets V's name to empty. |
| 280 | /// |
| 281 | /// \note It is an error to call V->takeName(V). |
| 282 | void takeName(Value *V); |
| 283 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 284 | #ifndef NDEBUG |
| 285 | std::string getNameOrAsOperand() const; |
| 286 | #endif |
| 287 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 288 | /// Change all uses of this to point to a new Value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 289 | /// |
| 290 | /// Go through the uses list for this definition and make each use point to |
| 291 | /// "V" instead of "this". After this completes, 'this's use list is |
| 292 | /// guaranteed to be empty. |
| 293 | void replaceAllUsesWith(Value *V); |
| 294 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 295 | /// Change non-metadata uses of this to point to a new Value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 296 | /// |
| 297 | /// Go through the uses list for this definition and make each use point to |
| 298 | /// "V" instead of "this". This function skips metadata entries in the list. |
| 299 | void replaceNonMetadataUsesWith(Value *V); |
| 300 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 301 | /// Go through the uses list for this definition and make each use point |
| 302 | /// to "V" if the callback ShouldReplace returns true for the given Use. |
| 303 | /// Unlike replaceAllUsesWith() this function does not support basic block |
| 304 | /// values or constant users. |
| 305 | void replaceUsesWithIf(Value *New, |
| 306 | llvm::function_ref<bool(Use &U)> ShouldReplace) { |
| 307 | assert(New && "Value::replaceUsesWithIf(<null>) is invalid!"); |
| 308 | assert(New->getType() == getType() && |
| 309 | "replaceUses of value with new value of different type!"); |
| 310 | |
| 311 | for (use_iterator UI = use_begin(), E = use_end(); UI != E;) { |
| 312 | Use &U = *UI; |
| 313 | ++UI; |
| 314 | if (!ShouldReplace(U)) |
| 315 | continue; |
| 316 | U.set(New); |
| 317 | } |
| 318 | } |
| 319 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 320 | /// replaceUsesOutsideBlock - Go through the uses list for this definition and |
| 321 | /// make each use point to "V" instead of "this" when the use is outside the |
| 322 | /// block. 'This's use list is expected to have at least one element. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 323 | /// Unlike replaceAllUsesWith() this function does not support basic block |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 324 | /// values or constant users. |
| 325 | void replaceUsesOutsideBlock(Value *V, BasicBlock *BB); |
| 326 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 327 | //---------------------------------------------------------------------- |
| 328 | // Methods for handling the chain of uses of this Value. |
| 329 | // |
| 330 | // Materializing a function can introduce new uses, so these methods come in |
| 331 | // two variants: |
| 332 | // The methods that start with materialized_ check the uses that are |
| 333 | // currently known given which functions are materialized. Be very careful |
| 334 | // when using them since you might not get all uses. |
| 335 | // The methods that don't start with materialized_ assert that modules is |
| 336 | // fully materialized. |
| 337 | void assertModuleIsMaterializedImpl() const; |
| 338 | // This indirection exists so we can keep assertModuleIsMaterializedImpl() |
| 339 | // around in release builds of Value.cpp to be linked with other code built |
| 340 | // in debug mode. But this avoids calling it in any of the release built code. |
| 341 | void assertModuleIsMaterialized() const { |
| 342 | #ifndef NDEBUG |
| 343 | assertModuleIsMaterializedImpl(); |
| 344 | #endif |
| 345 | } |
| 346 | |
| 347 | bool use_empty() const { |
| 348 | assertModuleIsMaterialized(); |
| 349 | return UseList == nullptr; |
| 350 | } |
| 351 | |
| 352 | bool materialized_use_empty() const { |
| 353 | return UseList == nullptr; |
| 354 | } |
| 355 | |
| 356 | using use_iterator = use_iterator_impl<Use>; |
| 357 | using const_use_iterator = use_iterator_impl<const Use>; |
| 358 | |
| 359 | use_iterator materialized_use_begin() { return use_iterator(UseList); } |
| 360 | const_use_iterator materialized_use_begin() const { |
| 361 | return const_use_iterator(UseList); |
| 362 | } |
| 363 | use_iterator use_begin() { |
| 364 | assertModuleIsMaterialized(); |
| 365 | return materialized_use_begin(); |
| 366 | } |
| 367 | const_use_iterator use_begin() const { |
| 368 | assertModuleIsMaterialized(); |
| 369 | return materialized_use_begin(); |
| 370 | } |
| 371 | use_iterator use_end() { return use_iterator(); } |
| 372 | const_use_iterator use_end() const { return const_use_iterator(); } |
| 373 | iterator_range<use_iterator> materialized_uses() { |
| 374 | return make_range(materialized_use_begin(), use_end()); |
| 375 | } |
| 376 | iterator_range<const_use_iterator> materialized_uses() const { |
| 377 | return make_range(materialized_use_begin(), use_end()); |
| 378 | } |
| 379 | iterator_range<use_iterator> uses() { |
| 380 | assertModuleIsMaterialized(); |
| 381 | return materialized_uses(); |
| 382 | } |
| 383 | iterator_range<const_use_iterator> uses() const { |
| 384 | assertModuleIsMaterialized(); |
| 385 | return materialized_uses(); |
| 386 | } |
| 387 | |
| 388 | bool user_empty() const { |
| 389 | assertModuleIsMaterialized(); |
| 390 | return UseList == nullptr; |
| 391 | } |
| 392 | |
| 393 | using user_iterator = user_iterator_impl<User>; |
| 394 | using const_user_iterator = user_iterator_impl<const User>; |
| 395 | |
| 396 | user_iterator materialized_user_begin() { return user_iterator(UseList); } |
| 397 | const_user_iterator materialized_user_begin() const { |
| 398 | return const_user_iterator(UseList); |
| 399 | } |
| 400 | user_iterator user_begin() { |
| 401 | assertModuleIsMaterialized(); |
| 402 | return materialized_user_begin(); |
| 403 | } |
| 404 | const_user_iterator user_begin() const { |
| 405 | assertModuleIsMaterialized(); |
| 406 | return materialized_user_begin(); |
| 407 | } |
| 408 | user_iterator user_end() { return user_iterator(); } |
| 409 | const_user_iterator user_end() const { return const_user_iterator(); } |
| 410 | User *user_back() { |
| 411 | assertModuleIsMaterialized(); |
| 412 | return *materialized_user_begin(); |
| 413 | } |
| 414 | const User *user_back() const { |
| 415 | assertModuleIsMaterialized(); |
| 416 | return *materialized_user_begin(); |
| 417 | } |
| 418 | iterator_range<user_iterator> materialized_users() { |
| 419 | return make_range(materialized_user_begin(), user_end()); |
| 420 | } |
| 421 | iterator_range<const_user_iterator> materialized_users() const { |
| 422 | return make_range(materialized_user_begin(), user_end()); |
| 423 | } |
| 424 | iterator_range<user_iterator> users() { |
| 425 | assertModuleIsMaterialized(); |
| 426 | return materialized_users(); |
| 427 | } |
| 428 | iterator_range<const_user_iterator> users() const { |
| 429 | assertModuleIsMaterialized(); |
| 430 | return materialized_users(); |
| 431 | } |
| 432 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 433 | /// Return true if there is exactly one use of this value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 434 | /// |
| 435 | /// This is specialized because it is a common request and does not require |
| 436 | /// traversing the whole use list. |
| 437 | bool hasOneUse() const { |
| 438 | const_use_iterator I = use_begin(), E = use_end(); |
| 439 | if (I == E) return false; |
| 440 | return ++I == E; |
| 441 | } |
| 442 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 443 | /// Return true if this Value has exactly N uses. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 444 | bool hasNUses(unsigned N) const; |
| 445 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 446 | /// Return true if this value has N uses or more. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 447 | /// |
| 448 | /// This is logically equivalent to getNumUses() >= N. |
| 449 | bool hasNUsesOrMore(unsigned N) const; |
| 450 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 451 | /// Return true if there is exactly one user of this value. |
| 452 | /// |
| 453 | /// Note that this is not the same as "has one use". If a value has one use, |
| 454 | /// then there certainly is a single user. But if value has several uses, |
| 455 | /// it is possible that all uses are in a single user, or not. |
| 456 | /// |
| 457 | /// This check is potentially costly, since it requires traversing, |
| 458 | /// in the worst case, the whole use list of a value. |
| 459 | bool hasOneUser() const; |
| 460 | |
| 461 | /// Return true if there is exactly one use of this value that cannot be |
| 462 | /// dropped. |
| 463 | /// |
| 464 | /// This is specialized because it is a common request and does not require |
| 465 | /// traversing the whole use list. |
| 466 | Use *getSingleUndroppableUse(); |
| 467 | |
| 468 | /// Return true if there this value. |
| 469 | /// |
| 470 | /// This is specialized because it is a common request and does not require |
| 471 | /// traversing the whole use list. |
| 472 | bool hasNUndroppableUses(unsigned N) const; |
| 473 | |
| 474 | /// Return true if this value has N uses or more. |
| 475 | /// |
| 476 | /// This is logically equivalent to getNumUses() >= N. |
| 477 | bool hasNUndroppableUsesOrMore(unsigned N) const; |
| 478 | |
| 479 | /// Remove every uses that can safely be removed. |
| 480 | /// |
| 481 | /// This will remove for example uses in llvm.assume. |
| 482 | /// This should be used when performing want to perform a tranformation but |
| 483 | /// some Droppable uses pervent it. |
| 484 | /// This function optionally takes a filter to only remove some droppable |
| 485 | /// uses. |
| 486 | void dropDroppableUses(llvm::function_ref<bool(const Use *)> ShouldDrop = |
| 487 | [](const Use *) { return true; }); |
| 488 | |
| 489 | /// Remove every use of this value in \p User that can safely be removed. |
| 490 | void dropDroppableUsesIn(User &Usr); |
| 491 | |
| 492 | /// Remove the droppable use \p U. |
| 493 | static void dropDroppableUse(Use &U); |
| 494 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 495 | /// Check if this value is used in the specified basic block. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 496 | bool isUsedInBasicBlock(const BasicBlock *BB) const; |
| 497 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 498 | /// This method computes the number of uses of this Value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 499 | /// |
| 500 | /// This is a linear time operation. Use hasOneUse, hasNUses, or |
| 501 | /// hasNUsesOrMore to check for specific values. |
| 502 | unsigned getNumUses() const; |
| 503 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 504 | /// This method should only be used by the Use class. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 505 | void addUse(Use &U) { U.addToList(&UseList); } |
| 506 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 507 | /// Concrete subclass of this. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 508 | /// |
| 509 | /// An enumeration for keeping track of the concrete subclass of Value that |
| 510 | /// is actually instantiated. Values of this enumeration are kept in the |
| 511 | /// Value classes SubclassID field. They are used for concrete type |
| 512 | /// identification. |
| 513 | enum ValueTy { |
| 514 | #define HANDLE_VALUE(Name) Name##Val, |
| 515 | #include "llvm/IR/Value.def" |
| 516 | |
| 517 | // Markers: |
| 518 | #define HANDLE_CONSTANT_MARKER(Marker, Constant) Marker = Constant##Val, |
| 519 | #include "llvm/IR/Value.def" |
| 520 | }; |
| 521 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 522 | /// Return an ID for the concrete type of this object. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 523 | /// |
| 524 | /// This is used to implement the classof checks. This should not be used |
| 525 | /// for any other purpose, as the values may change as LLVM evolves. Also, |
| 526 | /// note that for instructions, the Instruction's opcode is added to |
| 527 | /// InstructionVal. So this means three things: |
| 528 | /// # there is no value with code InstructionVal (no opcode==0). |
| 529 | /// # there are more possible values for the value type than in ValueTy enum. |
| 530 | /// # the InstructionVal enumerator must be the highest valued enumerator in |
| 531 | /// the ValueTy enum. |
| 532 | unsigned getValueID() const { |
| 533 | return SubclassID; |
| 534 | } |
| 535 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 536 | /// Return the raw optional flags value contained in this value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 537 | /// |
| 538 | /// This should only be used when testing two Values for equivalence. |
| 539 | unsigned getRawSubclassOptionalData() const { |
| 540 | return SubclassOptionalData; |
| 541 | } |
| 542 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 543 | /// Clear the optional flags contained in this value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 544 | void clearSubclassOptionalData() { |
| 545 | SubclassOptionalData = 0; |
| 546 | } |
| 547 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 548 | /// Check the optional flags for equality. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 549 | bool hasSameSubclassOptionalData(const Value *V) const { |
| 550 | return SubclassOptionalData == V->SubclassOptionalData; |
| 551 | } |
| 552 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 553 | /// Return true if there is a value handle associated with this value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 554 | bool hasValueHandle() const { return HasValueHandle; } |
| 555 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 556 | /// Return true if there is metadata referencing this value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 557 | bool isUsedByMetadata() const { return IsUsedByMD; } |
| 558 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 559 | protected: |
| 560 | /// Get the current metadata attachments for the given kind, if any. |
| 561 | /// |
| 562 | /// These functions require that the value have at most a single attachment |
| 563 | /// of the given kind, and return \c nullptr if such an attachment is missing. |
| 564 | /// @{ |
| 565 | MDNode *getMetadata(unsigned KindID) const; |
| 566 | MDNode *getMetadata(StringRef Kind) const; |
| 567 | /// @} |
| 568 | |
| 569 | /// Appends all attachments with the given ID to \c MDs in insertion order. |
| 570 | /// If the Value has no attachments with the given ID, or if ID is invalid, |
| 571 | /// leaves MDs unchanged. |
| 572 | /// @{ |
| 573 | void getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const; |
| 574 | void getMetadata(StringRef Kind, SmallVectorImpl<MDNode *> &MDs) const; |
| 575 | /// @} |
| 576 | |
| 577 | /// Appends all metadata attached to this value to \c MDs, sorting by |
| 578 | /// KindID. The first element of each pair returned is the KindID, the second |
| 579 | /// element is the metadata value. Attachments with the same ID appear in |
| 580 | /// insertion order. |
| 581 | void |
| 582 | getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const; |
| 583 | |
| 584 | /// Return true if this value has any metadata attached to it. |
| 585 | bool hasMetadata() const { return (bool)HasMetadata; } |
| 586 | |
| 587 | /// Return true if this value has the given type of metadata attached. |
| 588 | /// @{ |
| 589 | bool hasMetadata(unsigned KindID) const { |
| 590 | return getMetadata(KindID) != nullptr; |
| 591 | } |
| 592 | bool hasMetadata(StringRef Kind) const { |
| 593 | return getMetadata(Kind) != nullptr; |
| 594 | } |
| 595 | /// @} |
| 596 | |
| 597 | /// Set a particular kind of metadata attachment. |
| 598 | /// |
| 599 | /// Sets the given attachment to \c MD, erasing it if \c MD is \c nullptr or |
| 600 | /// replacing it if it already exists. |
| 601 | /// @{ |
| 602 | void setMetadata(unsigned KindID, MDNode *Node); |
| 603 | void setMetadata(StringRef Kind, MDNode *Node); |
| 604 | /// @} |
| 605 | |
| 606 | /// Add a metadata attachment. |
| 607 | /// @{ |
| 608 | void addMetadata(unsigned KindID, MDNode &MD); |
| 609 | void addMetadata(StringRef Kind, MDNode &MD); |
| 610 | /// @} |
| 611 | |
| 612 | /// Erase all metadata attachments with the given kind. |
| 613 | /// |
| 614 | /// \returns true if any metadata was removed. |
| 615 | bool eraseMetadata(unsigned KindID); |
| 616 | |
| 617 | /// Erase all metadata attached to this Value. |
| 618 | void clearMetadata(); |
| 619 | |
| 620 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 621 | /// Return true if this value is a swifterror value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 622 | /// |
| 623 | /// swifterror values can be either a function argument or an alloca with a |
| 624 | /// swifterror attribute. |
| 625 | bool isSwiftError() const; |
| 626 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 627 | /// Strip off pointer casts, all-zero GEPs and address space casts. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 628 | /// |
| 629 | /// Returns the original uncasted value. If this is called on a non-pointer |
| 630 | /// value, it returns 'this'. |
| 631 | const Value *stripPointerCasts() const; |
| 632 | Value *stripPointerCasts() { |
| 633 | return const_cast<Value *>( |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 634 | static_cast<const Value *>(this)->stripPointerCasts()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 635 | } |
| 636 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 637 | /// Strip off pointer casts, all-zero GEPs, address space casts, and aliases. |
| 638 | /// |
| 639 | /// Returns the original uncasted value. If this is called on a non-pointer |
| 640 | /// value, it returns 'this'. |
| 641 | const Value *stripPointerCastsAndAliases() const; |
| 642 | Value *stripPointerCastsAndAliases() { |
| 643 | return const_cast<Value *>( |
| 644 | static_cast<const Value *>(this)->stripPointerCastsAndAliases()); |
| 645 | } |
| 646 | |
| 647 | /// Strip off pointer casts, all-zero GEPs and address space casts |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 648 | /// but ensures the representation of the result stays the same. |
| 649 | /// |
| 650 | /// Returns the original uncasted value with the same representation. If this |
| 651 | /// is called on a non-pointer value, it returns 'this'. |
| 652 | const Value *stripPointerCastsSameRepresentation() const; |
| 653 | Value *stripPointerCastsSameRepresentation() { |
| 654 | return const_cast<Value *>(static_cast<const Value *>(this) |
| 655 | ->stripPointerCastsSameRepresentation()); |
| 656 | } |
| 657 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 658 | /// Strip off pointer casts, all-zero GEPs and invariant group info. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 659 | /// |
| 660 | /// Returns the original uncasted value. If this is called on a non-pointer |
| 661 | /// value, it returns 'this'. This function should be used only in |
| 662 | /// Alias analysis. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 663 | const Value *stripPointerCastsAndInvariantGroups() const; |
| 664 | Value *stripPointerCastsAndInvariantGroups() { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 665 | return const_cast<Value *>(static_cast<const Value *>(this) |
| 666 | ->stripPointerCastsAndInvariantGroups()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 667 | } |
| 668 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 669 | /// Strip off pointer casts and all-constant inbounds GEPs. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 670 | /// |
| 671 | /// Returns the original pointer value. If this is called on a non-pointer |
| 672 | /// value, it returns 'this'. |
| 673 | const Value *stripInBoundsConstantOffsets() const; |
| 674 | Value *stripInBoundsConstantOffsets() { |
| 675 | return const_cast<Value *>( |
| 676 | static_cast<const Value *>(this)->stripInBoundsConstantOffsets()); |
| 677 | } |
| 678 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 679 | /// Accumulate the constant offset this value has compared to a base pointer. |
| 680 | /// Only 'getelementptr' instructions (GEPs) are accumulated but other |
| 681 | /// instructions, e.g., casts, are stripped away as well. |
| 682 | /// The accumulated constant offset is added to \p Offset and the base |
| 683 | /// pointer is returned. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 684 | /// |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 685 | /// The APInt \p Offset has to have a bit-width equal to the IntPtr type for |
| 686 | /// the address space of 'this' pointer value, e.g., use |
| 687 | /// DataLayout::getIndexTypeSizeInBits(Ty). |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 688 | /// |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 689 | /// If \p AllowNonInbounds is true, offsets in GEPs are stripped and |
| 690 | /// accumulated even if the GEP is not "inbounds". |
| 691 | /// |
| 692 | /// If \p ExternalAnalysis is provided it will be used to calculate a offset |
| 693 | /// when a operand of GEP is not constant. |
| 694 | /// For example, for a value \p ExternalAnalysis might try to calculate a |
| 695 | /// lower bound. If \p ExternalAnalysis is successful, it should return true. |
| 696 | /// |
| 697 | /// If this is called on a non-pointer value, it returns 'this' and the |
| 698 | /// \p Offset is not modified. |
| 699 | /// |
| 700 | /// Note that this function will never return a nullptr. It will also never |
| 701 | /// manipulate the \p Offset in a way that would not match the difference |
| 702 | /// between the underlying value and the returned one. Thus, if no constant |
| 703 | /// offset was found, the returned value is the underlying one and \p Offset |
| 704 | /// is unchanged. |
| 705 | const Value *stripAndAccumulateConstantOffsets( |
| 706 | const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, |
| 707 | function_ref<bool(Value &Value, APInt &Offset)> ExternalAnalysis = |
| 708 | nullptr) const; |
| 709 | Value *stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, |
| 710 | bool AllowNonInbounds) { |
| 711 | return const_cast<Value *>( |
| 712 | static_cast<const Value *>(this)->stripAndAccumulateConstantOffsets( |
| 713 | DL, Offset, AllowNonInbounds)); |
| 714 | } |
| 715 | |
| 716 | /// This is a wrapper around stripAndAccumulateConstantOffsets with the |
| 717 | /// in-bounds requirement set to false. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 718 | const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 719 | APInt &Offset) const { |
| 720 | return stripAndAccumulateConstantOffsets(DL, Offset, |
| 721 | /* AllowNonInbounds */ false); |
| 722 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 723 | Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, |
| 724 | APInt &Offset) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 725 | return stripAndAccumulateConstantOffsets(DL, Offset, |
| 726 | /* AllowNonInbounds */ false); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 727 | } |
| 728 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 729 | /// Strip off pointer casts and inbounds GEPs. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 730 | /// |
| 731 | /// Returns the original pointer value. If this is called on a non-pointer |
| 732 | /// value, it returns 'this'. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 733 | const Value *stripInBoundsOffsets(function_ref<void(const Value *)> Func = |
| 734 | [](const Value *) {}) const; |
| 735 | inline Value *stripInBoundsOffsets(function_ref<void(const Value *)> Func = |
| 736 | [](const Value *) {}) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 737 | return const_cast<Value *>( |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 738 | static_cast<const Value *>(this)->stripInBoundsOffsets(Func)); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 739 | } |
| 740 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 741 | /// Returns the number of bytes known to be dereferenceable for the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 742 | /// pointer value. |
| 743 | /// |
| 744 | /// If CanBeNull is set by this function the pointer can either be null or be |
| 745 | /// dereferenceable up to the returned number of bytes. |
| 746 | uint64_t getPointerDereferenceableBytes(const DataLayout &DL, |
| 747 | bool &CanBeNull) const; |
| 748 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 749 | /// Returns an alignment of the pointer value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 750 | /// |
| 751 | /// Returns an alignment which is either specified explicitly, e.g. via |
| 752 | /// align attribute of a function argument, or guaranteed by DataLayout. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 753 | Align getPointerAlignment(const DataLayout &DL) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 754 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 755 | /// Translate PHI node to its predecessor from the given basic block. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 756 | /// |
| 757 | /// If this value is a PHI node with CurBB as its parent, return the value in |
| 758 | /// the PHI node corresponding to PredBB. If not, return ourself. This is |
| 759 | /// useful if you want to know the value something has in a predecessor |
| 760 | /// block. |
| 761 | const Value *DoPHITranslation(const BasicBlock *CurBB, |
| 762 | const BasicBlock *PredBB) const; |
| 763 | Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) { |
| 764 | return const_cast<Value *>( |
| 765 | static_cast<const Value *>(this)->DoPHITranslation(CurBB, PredBB)); |
| 766 | } |
| 767 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 768 | /// The maximum alignment for instructions. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 769 | /// |
| 770 | /// This is the greatest alignment value supported by load, store, and alloca |
| 771 | /// instructions, and global values. |
| 772 | static const unsigned MaxAlignmentExponent = 29; |
| 773 | static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent; |
| 774 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 775 | /// Mutate the type of this Value to be of the specified type. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 776 | /// |
| 777 | /// Note that this is an extremely dangerous operation which can create |
| 778 | /// completely invalid IR very easily. It is strongly recommended that you |
| 779 | /// recreate IR objects with the right types instead of mutating them in |
| 780 | /// place. |
| 781 | void mutateType(Type *Ty) { |
| 782 | VTy = Ty; |
| 783 | } |
| 784 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 785 | /// Sort the use-list. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 786 | /// |
| 787 | /// Sorts the Value's use-list by Cmp using a stable mergesort. Cmp is |
| 788 | /// expected to compare two \a Use references. |
| 789 | template <class Compare> void sortUseList(Compare Cmp); |
| 790 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 791 | /// Reverse the use-list. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 792 | void reverseUseList(); |
| 793 | |
| 794 | private: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 795 | /// Merge two lists together. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 796 | /// |
| 797 | /// Merges \c L and \c R using \c Cmp. To enable stable sorts, always pushes |
| 798 | /// "equal" items from L before items from R. |
| 799 | /// |
| 800 | /// \return the first element in the list. |
| 801 | /// |
| 802 | /// \note Completely ignores \a Use::Prev (doesn't read, doesn't update). |
| 803 | template <class Compare> |
| 804 | static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) { |
| 805 | Use *Merged; |
| 806 | Use **Next = &Merged; |
| 807 | |
| 808 | while (true) { |
| 809 | if (!L) { |
| 810 | *Next = R; |
| 811 | break; |
| 812 | } |
| 813 | if (!R) { |
| 814 | *Next = L; |
| 815 | break; |
| 816 | } |
| 817 | if (Cmp(*R, *L)) { |
| 818 | *Next = R; |
| 819 | Next = &R->Next; |
| 820 | R = R->Next; |
| 821 | } else { |
| 822 | *Next = L; |
| 823 | Next = &L->Next; |
| 824 | L = L->Next; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | return Merged; |
| 829 | } |
| 830 | |
| 831 | protected: |
| 832 | unsigned short getSubclassDataFromValue() const { return SubclassData; } |
| 833 | void setValueSubclassData(unsigned short D) { SubclassData = D; } |
| 834 | }; |
| 835 | |
| 836 | struct ValueDeleter { void operator()(Value *V) { V->deleteValue(); } }; |
| 837 | |
| 838 | /// Use this instead of std::unique_ptr<Value> or std::unique_ptr<Instruction>. |
| 839 | /// Those don't work because Value and Instruction's destructors are protected, |
| 840 | /// aren't virtual, and won't destroy the complete object. |
| 841 | using unique_value = std::unique_ptr<Value, ValueDeleter>; |
| 842 | |
| 843 | inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) { |
| 844 | V.print(OS); |
| 845 | return OS; |
| 846 | } |
| 847 | |
| 848 | void Use::set(Value *V) { |
| 849 | if (Val) removeFromList(); |
| 850 | Val = V; |
| 851 | if (V) V->addUse(*this); |
| 852 | } |
| 853 | |
| 854 | Value *Use::operator=(Value *RHS) { |
| 855 | set(RHS); |
| 856 | return RHS; |
| 857 | } |
| 858 | |
| 859 | const Use &Use::operator=(const Use &RHS) { |
| 860 | set(RHS.Val); |
| 861 | return *this; |
| 862 | } |
| 863 | |
| 864 | template <class Compare> void Value::sortUseList(Compare Cmp) { |
| 865 | if (!UseList || !UseList->Next) |
| 866 | // No need to sort 0 or 1 uses. |
| 867 | return; |
| 868 | |
| 869 | // Note: this function completely ignores Prev pointers until the end when |
| 870 | // they're fixed en masse. |
| 871 | |
| 872 | // Create a binomial vector of sorted lists, visiting uses one at a time and |
| 873 | // merging lists as necessary. |
| 874 | const unsigned MaxSlots = 32; |
| 875 | Use *Slots[MaxSlots]; |
| 876 | |
| 877 | // Collect the first use, turning it into a single-item list. |
| 878 | Use *Next = UseList->Next; |
| 879 | UseList->Next = nullptr; |
| 880 | unsigned NumSlots = 1; |
| 881 | Slots[0] = UseList; |
| 882 | |
| 883 | // Collect all but the last use. |
| 884 | while (Next->Next) { |
| 885 | Use *Current = Next; |
| 886 | Next = Current->Next; |
| 887 | |
| 888 | // Turn Current into a single-item list. |
| 889 | Current->Next = nullptr; |
| 890 | |
| 891 | // Save Current in the first available slot, merging on collisions. |
| 892 | unsigned I; |
| 893 | for (I = 0; I < NumSlots; ++I) { |
| 894 | if (!Slots[I]) |
| 895 | break; |
| 896 | |
| 897 | // Merge two lists, doubling the size of Current and emptying slot I. |
| 898 | // |
| 899 | // Since the uses in Slots[I] originally preceded those in Current, send |
| 900 | // Slots[I] in as the left parameter to maintain a stable sort. |
| 901 | Current = mergeUseLists(Slots[I], Current, Cmp); |
| 902 | Slots[I] = nullptr; |
| 903 | } |
| 904 | // Check if this is a new slot. |
| 905 | if (I == NumSlots) { |
| 906 | ++NumSlots; |
| 907 | assert(NumSlots <= MaxSlots && "Use list bigger than 2^32"); |
| 908 | } |
| 909 | |
| 910 | // Found an open slot. |
| 911 | Slots[I] = Current; |
| 912 | } |
| 913 | |
| 914 | // Merge all the lists together. |
| 915 | assert(Next && "Expected one more Use"); |
| 916 | assert(!Next->Next && "Expected only one Use"); |
| 917 | UseList = Next; |
| 918 | for (unsigned I = 0; I < NumSlots; ++I) |
| 919 | if (Slots[I]) |
| 920 | // Since the uses in Slots[I] originally preceded those in UseList, send |
| 921 | // Slots[I] in as the left parameter to maintain a stable sort. |
| 922 | UseList = mergeUseLists(Slots[I], UseList, Cmp); |
| 923 | |
| 924 | // Fix the Prev pointers. |
| 925 | for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 926 | I->Prev = Prev; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 927 | Prev = &I->Next; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | // isa - Provide some specializations of isa so that we don't have to include |
| 932 | // the subtype header files to test to see if the value is a subclass... |
| 933 | // |
| 934 | template <> struct isa_impl<Constant, Value> { |
| 935 | static inline bool doit(const Value &Val) { |
| 936 | static_assert(Value::ConstantFirstVal == 0, "Val.getValueID() >= Value::ConstantFirstVal"); |
| 937 | return Val.getValueID() <= Value::ConstantLastVal; |
| 938 | } |
| 939 | }; |
| 940 | |
| 941 | template <> struct isa_impl<ConstantData, Value> { |
| 942 | static inline bool doit(const Value &Val) { |
| 943 | return Val.getValueID() >= Value::ConstantDataFirstVal && |
| 944 | Val.getValueID() <= Value::ConstantDataLastVal; |
| 945 | } |
| 946 | }; |
| 947 | |
| 948 | template <> struct isa_impl<ConstantAggregate, Value> { |
| 949 | static inline bool doit(const Value &Val) { |
| 950 | return Val.getValueID() >= Value::ConstantAggregateFirstVal && |
| 951 | Val.getValueID() <= Value::ConstantAggregateLastVal; |
| 952 | } |
| 953 | }; |
| 954 | |
| 955 | template <> struct isa_impl<Argument, Value> { |
| 956 | static inline bool doit (const Value &Val) { |
| 957 | return Val.getValueID() == Value::ArgumentVal; |
| 958 | } |
| 959 | }; |
| 960 | |
| 961 | template <> struct isa_impl<InlineAsm, Value> { |
| 962 | static inline bool doit(const Value &Val) { |
| 963 | return Val.getValueID() == Value::InlineAsmVal; |
| 964 | } |
| 965 | }; |
| 966 | |
| 967 | template <> struct isa_impl<Instruction, Value> { |
| 968 | static inline bool doit(const Value &Val) { |
| 969 | return Val.getValueID() >= Value::InstructionVal; |
| 970 | } |
| 971 | }; |
| 972 | |
| 973 | template <> struct isa_impl<BasicBlock, Value> { |
| 974 | static inline bool doit(const Value &Val) { |
| 975 | return Val.getValueID() == Value::BasicBlockVal; |
| 976 | } |
| 977 | }; |
| 978 | |
| 979 | template <> struct isa_impl<Function, Value> { |
| 980 | static inline bool doit(const Value &Val) { |
| 981 | return Val.getValueID() == Value::FunctionVal; |
| 982 | } |
| 983 | }; |
| 984 | |
| 985 | template <> struct isa_impl<GlobalVariable, Value> { |
| 986 | static inline bool doit(const Value &Val) { |
| 987 | return Val.getValueID() == Value::GlobalVariableVal; |
| 988 | } |
| 989 | }; |
| 990 | |
| 991 | template <> struct isa_impl<GlobalAlias, Value> { |
| 992 | static inline bool doit(const Value &Val) { |
| 993 | return Val.getValueID() == Value::GlobalAliasVal; |
| 994 | } |
| 995 | }; |
| 996 | |
| 997 | template <> struct isa_impl<GlobalIFunc, Value> { |
| 998 | static inline bool doit(const Value &Val) { |
| 999 | return Val.getValueID() == Value::GlobalIFuncVal; |
| 1000 | } |
| 1001 | }; |
| 1002 | |
| 1003 | template <> struct isa_impl<GlobalIndirectSymbol, Value> { |
| 1004 | static inline bool doit(const Value &Val) { |
| 1005 | return isa<GlobalAlias>(Val) || isa<GlobalIFunc>(Val); |
| 1006 | } |
| 1007 | }; |
| 1008 | |
| 1009 | template <> struct isa_impl<GlobalValue, Value> { |
| 1010 | static inline bool doit(const Value &Val) { |
| 1011 | return isa<GlobalObject>(Val) || isa<GlobalIndirectSymbol>(Val); |
| 1012 | } |
| 1013 | }; |
| 1014 | |
| 1015 | template <> struct isa_impl<GlobalObject, Value> { |
| 1016 | static inline bool doit(const Value &Val) { |
| 1017 | return isa<GlobalVariable>(Val) || isa<Function>(Val); |
| 1018 | } |
| 1019 | }; |
| 1020 | |
| 1021 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
| 1022 | DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef) |
| 1023 | |
| 1024 | // Specialized opaque value conversions. |
| 1025 | inline Value **unwrap(LLVMValueRef *Vals) { |
| 1026 | return reinterpret_cast<Value**>(Vals); |
| 1027 | } |
| 1028 | |
| 1029 | template<typename T> |
| 1030 | inline T **unwrap(LLVMValueRef *Vals, unsigned Length) { |
| 1031 | #ifndef NDEBUG |
| 1032 | for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I) |
| 1033 | unwrap<T>(*I); // For side effect of calling assert on invalid usage. |
| 1034 | #endif |
| 1035 | (void)Length; |
| 1036 | return reinterpret_cast<T**>(Vals); |
| 1037 | } |
| 1038 | |
| 1039 | inline LLVMValueRef *wrap(const Value **Vals) { |
| 1040 | return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals)); |
| 1041 | } |
| 1042 | |
| 1043 | } // end namespace llvm |
| 1044 | |
| 1045 | #endif // LLVM_IR_VALUE_H |