Jay Foad | bdbe342 | 2011-01-16 08:10:57 +0000 | [diff] [blame] | 1 | //===-- User.cpp - Implement the User class -------------------------------===// |
| 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 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 10 | #include "llvm/IR/User.h" |
| 11 | #include "llvm/IR/Constant.h" |
| 12 | #include "llvm/IR/GlobalValue.h" |
| 13 | #include "llvm/IR/Operator.h" |
Jay Foad | bdbe342 | 2011-01-16 08:10:57 +0000 | [diff] [blame] | 14 | |
| 15 | namespace llvm { |
| 16 | |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | // User Class |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
David Blaikie | 0becc96 | 2011-12-01 08:00:17 +0000 | [diff] [blame] | 21 | void User::anchor() {} |
| 22 | |
Jay Foad | bdbe342 | 2011-01-16 08:10:57 +0000 | [diff] [blame] | 23 | // replaceUsesOfWith - Replaces all references to the "From" definition with |
| 24 | // references to the "To" definition. |
| 25 | // |
| 26 | void User::replaceUsesOfWith(Value *From, Value *To) { |
| 27 | if (From == To) return; // Duh what? |
| 28 | |
| 29 | assert((!isa<Constant>(this) || isa<GlobalValue>(this)) && |
| 30 | "Cannot call User::replaceUsesOfWith on a constant!"); |
| 31 | |
| 32 | for (unsigned i = 0, E = getNumOperands(); i != E; ++i) |
| 33 | if (getOperand(i) == From) { // Is This operand is pointing to oldval? |
| 34 | // The side effects of this setOperand call include linking to |
| 35 | // "To", adding "this" to the uses list of To, and |
| 36 | // most importantly, removing "this" from the use list of "From". |
| 37 | setOperand(i, To); // Fix it now... |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | // User allocHungoffUses Implementation |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
| 45 | Use *User::allocHungoffUses(unsigned N) const { |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 46 | // Allocate the array of Uses, followed by a pointer (with bottom bit set) to |
| 47 | // the User. |
| 48 | size_t size = N * sizeof(Use) + sizeof(Use::UserRef); |
| 49 | Use *Begin = static_cast<Use*>(::operator new(size)); |
Jay Foad | bdbe342 | 2011-01-16 08:10:57 +0000 | [diff] [blame] | 50 | Use *End = Begin + N; |
Jay Foad | 691c05b | 2011-06-20 14:12:33 +0000 | [diff] [blame] | 51 | (void) new(End) Use::UserRef(const_cast<User*>(this), 1); |
Jay Foad | bdbe342 | 2011-01-16 08:10:57 +0000 | [diff] [blame] | 52 | return Use::initTags(Begin, End); |
| 53 | } |
| 54 | |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | // User operator new Implementations |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | |
| 59 | void *User::operator new(size_t s, unsigned Us) { |
| 60 | void *Storage = ::operator new(s + sizeof(Use) * Us); |
| 61 | Use *Start = static_cast<Use*>(Storage); |
| 62 | Use *End = Start + Us; |
| 63 | User *Obj = reinterpret_cast<User*>(End); |
| 64 | Obj->OperandList = Start; |
| 65 | Obj->NumOperands = Us; |
| 66 | Use::initTags(Start, End); |
| 67 | return Obj; |
| 68 | } |
| 69 | |
| 70 | //===----------------------------------------------------------------------===// |
| 71 | // User operator delete Implementation |
| 72 | //===----------------------------------------------------------------------===// |
| 73 | |
| 74 | void User::operator delete(void *Usr) { |
| 75 | User *Start = static_cast<User*>(Usr); |
| 76 | Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands; |
Jay Foad | adede03 | 2011-01-26 21:56:10 +0000 | [diff] [blame] | 77 | // If there were hung-off uses, they will have been freed already and |
| 78 | // NumOperands reset to 0, so here we just free the User itself. |
| 79 | ::operator delete(Storage); |
Jay Foad | bdbe342 | 2011-01-16 08:10:57 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Richard Smith | 30f57da | 2012-10-24 00:30:41 +0000 | [diff] [blame] | 82 | //===----------------------------------------------------------------------===// |
| 83 | // Operator Class |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | |
| 86 | Operator::~Operator() { |
| 87 | llvm_unreachable("should never destroy an Operator"); |
| 88 | } |
| 89 | |
Jay Foad | bdbe342 | 2011-01-16 08:10:57 +0000 | [diff] [blame] | 90 | } // End llvm namespace |