Add User::growHungoffUses and use it to grow the hung off uses. NFC.
[oota-llvm.git] / lib / IR / User.cpp
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
10 #include "llvm/IR/User.h"
11 #include "llvm/IR/Constant.h"
12 #include "llvm/IR/GlobalValue.h"
13 #include "llvm/IR/Operator.h"
14
15 namespace llvm {
16 class BasicBlock;
17
18 //===----------------------------------------------------------------------===//
19 //                                 User Class
20 //===----------------------------------------------------------------------===//
21
22 void User::anchor() {}
23
24 void User::replaceUsesOfWith(Value *From, Value *To) {
25   if (From == To) return;   // Duh what?
26
27   assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
28          "Cannot call User::replaceUsesOfWith on a constant!");
29
30   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
31     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
32       // The side effects of this setOperand call include linking to
33       // "To", adding "this" to the uses list of To, and
34       // most importantly, removing "this" from the use list of "From".
35       setOperand(i, To); // Fix it now...
36     }
37 }
38
39 //===----------------------------------------------------------------------===//
40 //                         User allocHungoffUses Implementation
41 //===----------------------------------------------------------------------===//
42
43 Use *User::allocHungoffUses(unsigned N, bool IsPhi) {
44   // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
45   // the User.
46   size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
47   if (IsPhi)
48     size += N * sizeof(BasicBlock *);
49   Use *Begin = static_cast<Use*>(::operator new(size));
50   Use *End = Begin + N;
51   (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
52   Use *Uses = Use::initTags(Begin, End);
53   OperandList = Uses;
54   // Tag this operand list as being a hung off.
55   HasHungOffUses = true;
56   return Uses;
57 }
58
59 void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
60   assert(HasHungOffUses && "realloc must have hung off uses");
61
62   unsigned OldNumUses = getNumOperands();
63
64   // We don't support shrinking the number of uses.  We wouldn't have enough
65   // space to copy the old uses in to the new space.
66   assert(NewNumUses > OldNumUses && "realloc must grow num uses");
67
68   Use *OldOps = OperandList;
69   allocHungoffUses(NewNumUses, IsPhi);
70   Use *NewOps = OperandList;
71
72   // Now copy from the old operands list to the new one.
73   std::copy(OldOps, OldOps + OldNumUses, NewOps);
74
75   // If this is a Phi, then we need to copy the BB pointers too.
76   if (IsPhi) {
77     auto *OldPtr =
78         reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
79     auto *NewPtr =
80         reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
81     std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
82   }
83   Use::zap(OldOps, OldOps + OldNumUses, true);
84 }
85
86 //===----------------------------------------------------------------------===//
87 //                         User operator new Implementations
88 //===----------------------------------------------------------------------===//
89
90 void *User::operator new(size_t s, unsigned Us) {
91   void *Storage = ::operator new(s + sizeof(Use) * Us);
92   Use *Start = static_cast<Use*>(Storage);
93   Use *End = Start + Us;
94   User *Obj = reinterpret_cast<User*>(End);
95   Obj->OperandList = Start;
96   Obj->HasHungOffUses = false;
97   Obj->NumOperands = Us;
98   Use::initTags(Start, End);
99   return Obj;
100 }
101
102 //===----------------------------------------------------------------------===//
103 //                         User operator delete Implementation
104 //===----------------------------------------------------------------------===//
105
106 void User::operator delete(void *Usr) {
107   User *Start = static_cast<User*>(Usr);
108   Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands;
109   // If there were hung-off uses, they will have been freed already and
110   // NumOperands reset to 0, so here we just free the User itself.
111   ::operator delete(Storage);
112 }
113
114 //===----------------------------------------------------------------------===//
115 //                             Operator Class
116 //===----------------------------------------------------------------------===//
117
118 Operator::~Operator() {
119   llvm_unreachable("should never destroy an Operator");
120 }
121
122 } // End llvm namespace