Protection against stack-based memory corruption errors using SafeStack
[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 void User::allocHungoffUses(unsigned N, bool IsPhi) {
44   assert(HasHungOffUses && "alloc must have hung off uses");
45   // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
46   // the User.
47   size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
48   if (IsPhi)
49     size += N * sizeof(BasicBlock *);
50   Use *Begin = static_cast<Use*>(::operator new(size));
51   Use *End = Begin + N;
52   (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
53   setOperandList(Use::initTags(Begin, End));
54 }
55
56 void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
57   assert(HasHungOffUses && "realloc must have hung off uses");
58
59   unsigned OldNumUses = getNumOperands();
60
61   // We don't support shrinking the number of uses.  We wouldn't have enough
62   // space to copy the old uses in to the new space.
63   assert(NewNumUses > OldNumUses && "realloc must grow num uses");
64
65   Use *OldOps = getOperandList();
66   allocHungoffUses(NewNumUses, IsPhi);
67   Use *NewOps = getOperandList();
68
69   // Now copy from the old operands list to the new one.
70   std::copy(OldOps, OldOps + OldNumUses, NewOps);
71
72   // If this is a Phi, then we need to copy the BB pointers too.
73   if (IsPhi) {
74     auto *OldPtr =
75         reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
76     auto *NewPtr =
77         reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
78     std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
79   }
80   Use::zap(OldOps, OldOps + OldNumUses, true);
81 }
82
83 //===----------------------------------------------------------------------===//
84 //                         User operator new Implementations
85 //===----------------------------------------------------------------------===//
86
87 void *User::operator new(size_t Size, unsigned Us) {
88   assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
89   void *Storage = ::operator new(Size + sizeof(Use) * Us);
90   Use *Start = static_cast<Use*>(Storage);
91   Use *End = Start + Us;
92   User *Obj = reinterpret_cast<User*>(End);
93   Obj->NumUserOperands = Us;
94   Obj->HasHungOffUses = false;
95   Use::initTags(Start, End);
96   return Obj;
97 }
98
99 void *User::operator new(size_t Size) {
100   // Allocate space for a single Use*
101   void *Storage = ::operator new(Size + sizeof(Use *));
102   Use **HungOffOperandList = static_cast<Use **>(Storage);
103   User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
104   Obj->NumUserOperands = 0;
105   Obj->HasHungOffUses = true;
106   *HungOffOperandList = nullptr;
107   return Obj;
108 }
109
110 //===----------------------------------------------------------------------===//
111 //                         User operator delete Implementation
112 //===----------------------------------------------------------------------===//
113
114 void User::operator delete(void *Usr) {
115   // Hung off uses use a single Use* before the User, while other subclasses
116   // use a Use[] allocated prior to the user.
117   User *Obj = static_cast<User *>(Usr);
118   if (Obj->HasHungOffUses) {
119     Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
120     // drop the hung off uses.
121     Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
122              /* Delete */ true);
123     ::operator delete(HungOffOperandList);
124   } else {
125     Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
126     Use::zap(Storage, Storage + Obj->NumUserOperands,
127              /* Delete */ false);
128     ::operator delete(Storage);
129   }
130 }
131
132 //===----------------------------------------------------------------------===//
133 //                             Operator Class
134 //===----------------------------------------------------------------------===//
135
136 Operator::~Operator() {
137   llvm_unreachable("should never destroy an Operator");
138 }
139
140 } // End llvm namespace