LLVM  3.7.0
User.cpp
Go to the documentation of this file.
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 
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 void User::allocHungoffUses(unsigned N, bool IsPhi) {
44  assert(HasHungOffUses && "alloc must have hung off uses");
45 
47  "Alignment is insufficient for 'hung-off-uses' pieces");
48  static_assert(AlignOf<Use::UserRef>::Alignment >=
50  "Alignment is insufficient for 'hung-off-uses' pieces");
51 
52  // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
53  // the User.
54  size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
55  if (IsPhi)
56  size += N * sizeof(BasicBlock *);
57  Use *Begin = static_cast<Use*>(::operator new(size));
58  Use *End = Begin + N;
59  (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
60  setOperandList(Use::initTags(Begin, End));
61 }
62 
63 void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
64  assert(HasHungOffUses && "realloc must have hung off uses");
65 
66  unsigned OldNumUses = getNumOperands();
67 
68  // We don't support shrinking the number of uses. We wouldn't have enough
69  // space to copy the old uses in to the new space.
70  assert(NewNumUses > OldNumUses && "realloc must grow num uses");
71 
72  Use *OldOps = getOperandList();
73  allocHungoffUses(NewNumUses, IsPhi);
74  Use *NewOps = getOperandList();
75 
76  // Now copy from the old operands list to the new one.
77  std::copy(OldOps, OldOps + OldNumUses, NewOps);
78 
79  // If this is a Phi, then we need to copy the BB pointers too.
80  if (IsPhi) {
81  auto *OldPtr =
82  reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
83  auto *NewPtr =
84  reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
85  std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
86  }
87  Use::zap(OldOps, OldOps + OldNumUses, true);
88 }
89 
90 //===----------------------------------------------------------------------===//
91 // User operator new Implementations
92 //===----------------------------------------------------------------------===//
93 
94 void *User::operator new(size_t Size, unsigned Us) {
95  assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
96  void *Storage = ::operator new(Size + sizeof(Use) * Us);
97  Use *Start = static_cast<Use*>(Storage);
98  Use *End = Start + Us;
99  User *Obj = reinterpret_cast<User*>(End);
100  Obj->NumUserOperands = Us;
101  Obj->HasHungOffUses = false;
102  Use::initTags(Start, End);
103  return Obj;
104 }
105 
106 void *User::operator new(size_t Size) {
107  // Allocate space for a single Use*
108  void *Storage = ::operator new(Size + sizeof(Use *));
109  Use **HungOffOperandList = static_cast<Use **>(Storage);
110  User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
111  Obj->NumUserOperands = 0;
112  Obj->HasHungOffUses = true;
113  *HungOffOperandList = nullptr;
114  return Obj;
115 }
116 
117 //===----------------------------------------------------------------------===//
118 // User operator delete Implementation
119 //===----------------------------------------------------------------------===//
120 
121 void User::operator delete(void *Usr) {
122  // Hung off uses use a single Use* before the User, while other subclasses
123  // use a Use[] allocated prior to the user.
124  User *Obj = static_cast<User *>(Usr);
125  if (Obj->HasHungOffUses) {
126  Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
127  // drop the hung off uses.
128  Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
129  /* Delete */ true);
130  ::operator delete(HungOffOperandList);
131  } else {
132  Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
133  Use::zap(Storage, Storage + Obj->NumUserOperands,
134  /* Delete */ false);
135  ::operator delete(Storage);
136  }
137 }
138 
139 //===----------------------------------------------------------------------===//
140 // Operator Class
141 //===----------------------------------------------------------------------===//
142 
144  llvm_unreachable("should never destroy an Operator");
145 }
146 
147 } // End llvm namespace
AlignOf - A templated class that contains an enum value representing the alignment of the template ar...
Definition: AlignOf.h:46
Various leaf nodes.
Definition: ISDOpcodes.h:60
unsigned getNumOperands() const
Definition: User.h:138
bool HasHungOffUses
Definition: Value.h:112
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
void growHungoffUses(unsigned N, bool IsPhi=false)
Grow the number of hung off uses.
Definition: User.cpp:63
Use * getOperandList()
Definition: User.h:112
~Operator() override
Definition: User.cpp:143
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:24
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
PointerIntPair - This class implements a pair of a pointer and small integer.
static Use * initTags(Use *Start, Use *Stop)
Initializes the waymarking tags on an array of Uses.
Definition: Use.cpp:57
Value * getOperand(unsigned i) const
Definition: User.h:118
PointerIntPair< User *, 1, unsigned > UserRef
Definition: Use.h:77
unsigned NumUserOperands
Definition: Value.h:108
void setOperand(unsigned i, Value *Val)
Definition: User.h:122
#define N
void size_t size
LLVM Value Representation.
Definition: Value.h:69
void allocHungoffUses(unsigned N, bool IsPhi=false)
Allocate the array of Uses, followed by a pointer (with bottom bit set) to the User.
Definition: User.cpp:43
static void zap(Use *Start, const Use *Stop, bool del=false)
Destroys Use operands when the number of operands of a User changes.
Definition: Use.cpp:87