blob: 522722d701bac5b92c68892a8bad6ec2a1bc7014 [file] [log] [blame]
Jay Foad59809c72011-01-16 08:10:57 +00001//===-- 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 Carruth9fb823b2013-01-02 11:36:10 +000010#include "llvm/IR/User.h"
11#include "llvm/IR/Constant.h"
12#include "llvm/IR/GlobalValue.h"
13#include "llvm/IR/Operator.h"
Jay Foad59809c72011-01-16 08:10:57 +000014
15namespace llvm {
Pete Cooper87b925b2015-06-10 22:38:30 +000016class BasicBlock;
Jay Foad59809c72011-01-16 08:10:57 +000017
18//===----------------------------------------------------------------------===//
19// User Class
20//===----------------------------------------------------------------------===//
21
David Blaikie3a15e142011-12-01 08:00:17 +000022void User::anchor() {}
23
Jay Foad59809c72011-01-16 08:10:57 +000024void 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
Pete Cooper3fc30402015-06-10 22:38:46 +000043void User::allocHungoffUses(unsigned N, bool IsPhi) {
Pete Cooperc91fda32015-06-12 17:48:14 +000044 assert(HasHungOffUses && "alloc must have hung off uses");
James Y Knight8096d342015-06-17 01:21:20 +000045
46 static_assert(AlignOf<Use>::Alignment >= AlignOf<Use::UserRef>::Alignment,
James Y Knightf27e4412015-06-17 13:53:12 +000047 "Alignment is insufficient for 'hung-off-uses' pieces");
48 static_assert(AlignOf<Use::UserRef>::Alignment >=
49 AlignOf<BasicBlock *>::Alignment,
50 "Alignment is insufficient for 'hung-off-uses' pieces");
James Y Knight8096d342015-06-17 01:21:20 +000051
Jay Foad61ea0e42011-06-23 09:09:15 +000052 // 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);
Pete Cooper87b925b2015-06-10 22:38:30 +000055 if (IsPhi)
56 size += N * sizeof(BasicBlock *);
Jay Foad61ea0e42011-06-23 09:09:15 +000057 Use *Begin = static_cast<Use*>(::operator new(size));
Jay Foad59809c72011-01-16 08:10:57 +000058 Use *End = Begin + N;
Jay Foad5c54d752011-06-20 14:12:33 +000059 (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
Pete Cooper74510a42015-06-12 17:48:05 +000060 setOperandList(Use::initTags(Begin, End));
Jay Foad59809c72011-01-16 08:10:57 +000061}
62
Pete Cooper93f9ff52015-06-10 22:38:41 +000063void 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
Pete Cooper74510a42015-06-12 17:48:05 +000072 Use *OldOps = getOperandList();
Pete Cooper93f9ff52015-06-10 22:38:41 +000073 allocHungoffUses(NewNumUses, IsPhi);
Pete Cooper74510a42015-06-12 17:48:05 +000074 Use *NewOps = getOperandList();
Pete Cooper93f9ff52015-06-10 22:38:41 +000075
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
Jay Foad59809c72011-01-16 08:10:57 +000090//===----------------------------------------------------------------------===//
91// User operator new Implementations
92//===----------------------------------------------------------------------===//
93
Pete Cooperc91fda32015-06-12 17:48:14 +000094void *User::operator new(size_t Size, unsigned Us) {
Pete Cooperb4eede22015-06-12 17:48:10 +000095 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
Pete Cooperc91fda32015-06-12 17:48:14 +000096 void *Storage = ::operator new(Size + sizeof(Use) * Us);
Jay Foad59809c72011-01-16 08:10:57 +000097 Use *Start = static_cast<Use*>(Storage);
98 Use *End = Start + Us;
99 User *Obj = reinterpret_cast<User*>(End);
Pete Cooperb4eede22015-06-12 17:48:10 +0000100 Obj->NumUserOperands = Us;
Pete Cooperb676b012015-06-12 17:48:18 +0000101 Obj->HasHungOffUses = false;
Jay Foad59809c72011-01-16 08:10:57 +0000102 Use::initTags(Start, End);
103 return Obj;
104}
105
Pete Cooperc91fda32015-06-12 17:48:14 +0000106void *User::operator new(size_t Size) {
Pete Cooperb676b012015-06-12 17:48:18 +0000107 // 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);
Pete Cooperc91fda32015-06-12 17:48:14 +0000111 Obj->NumUserOperands = 0;
Pete Cooperb676b012015-06-12 17:48:18 +0000112 Obj->HasHungOffUses = true;
113 *HungOffOperandList = nullptr;
Pete Cooperc91fda32015-06-12 17:48:14 +0000114 return Obj;
115}
116
Jay Foad59809c72011-01-16 08:10:57 +0000117//===----------------------------------------------------------------------===//
118// User operator delete Implementation
119//===----------------------------------------------------------------------===//
120
Naomi Musgrave5f79c662015-08-31 16:26:44 +0000121void User::operator delete(void *Usr) {
Pete Cooperb676b012015-06-12 17:48:18 +0000122 // 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 }
Jay Foad59809c72011-01-16 08:10:57 +0000137}
138
Richard Smith1f6f4552012-10-24 00:30:41 +0000139//===----------------------------------------------------------------------===//
140// Operator Class
141//===----------------------------------------------------------------------===//
142
143Operator::~Operator() {
144 llvm_unreachable("should never destroy an Operator");
145}
146
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000147} // End llvm namespace