annotate llvm/lib/IR/User.cpp @ 220:42394fc6a535

Added tag llvm12 for changeset 0572611fdcc8
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 15 Jun 2021 19:13:43 +0900
parents 0572611fdcc8
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===-- User.cpp - Implement the User class -------------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #include "llvm/IR/User.h"
anatofuz
parents:
diff changeset
10 #include "llvm/IR/Constant.h"
anatofuz
parents:
diff changeset
11 #include "llvm/IR/GlobalValue.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
12 #include "llvm/IR/IntrinsicInst.h"
150
anatofuz
parents:
diff changeset
13
anatofuz
parents:
diff changeset
14 namespace llvm {
anatofuz
parents:
diff changeset
15 class BasicBlock;
anatofuz
parents:
diff changeset
16
anatofuz
parents:
diff changeset
17 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
18 // User Class
anatofuz
parents:
diff changeset
19 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
20
anatofuz
parents:
diff changeset
21 void User::replaceUsesOfWith(Value *From, Value *To) {
anatofuz
parents:
diff changeset
22 if (From == To) return; // Duh what?
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
anatofuz
parents:
diff changeset
25 "Cannot call User::replaceUsesOfWith on a constant!");
anatofuz
parents:
diff changeset
26
anatofuz
parents:
diff changeset
27 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
anatofuz
parents:
diff changeset
28 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
anatofuz
parents:
diff changeset
29 // The side effects of this setOperand call include linking to
anatofuz
parents:
diff changeset
30 // "To", adding "this" to the uses list of To, and
anatofuz
parents:
diff changeset
31 // most importantly, removing "this" from the use list of "From".
anatofuz
parents:
diff changeset
32 setOperand(i, To); // Fix it now...
anatofuz
parents:
diff changeset
33 }
anatofuz
parents:
diff changeset
34 }
anatofuz
parents:
diff changeset
35
anatofuz
parents:
diff changeset
36 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
37 // User allocHungoffUses Implementation
anatofuz
parents:
diff changeset
38 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
39
anatofuz
parents:
diff changeset
40 void User::allocHungoffUses(unsigned N, bool IsPhi) {
anatofuz
parents:
diff changeset
41 assert(HasHungOffUses && "alloc must have hung off uses");
anatofuz
parents:
diff changeset
42
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
43 static_assert(alignof(Use) >= alignof(BasicBlock *),
150
anatofuz
parents:
diff changeset
44 "Alignment is insufficient for 'hung-off-uses' pieces");
anatofuz
parents:
diff changeset
45
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
46 // Allocate the array of Uses
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
47 size_t size = N * sizeof(Use);
150
anatofuz
parents:
diff changeset
48 if (IsPhi)
anatofuz
parents:
diff changeset
49 size += N * sizeof(BasicBlock *);
anatofuz
parents:
diff changeset
50 Use *Begin = static_cast<Use*>(::operator new(size));
anatofuz
parents:
diff changeset
51 Use *End = Begin + N;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
52 setOperandList(Begin);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
53 for (; Begin != End; Begin++)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
54 new (Begin) Use(this);
150
anatofuz
parents:
diff changeset
55 }
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
anatofuz
parents:
diff changeset
58 assert(HasHungOffUses && "realloc must have hung off uses");
anatofuz
parents:
diff changeset
59
anatofuz
parents:
diff changeset
60 unsigned OldNumUses = getNumOperands();
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 // We don't support shrinking the number of uses. We wouldn't have enough
anatofuz
parents:
diff changeset
63 // space to copy the old uses in to the new space.
anatofuz
parents:
diff changeset
64 assert(NewNumUses > OldNumUses && "realloc must grow num uses");
anatofuz
parents:
diff changeset
65
anatofuz
parents:
diff changeset
66 Use *OldOps = getOperandList();
anatofuz
parents:
diff changeset
67 allocHungoffUses(NewNumUses, IsPhi);
anatofuz
parents:
diff changeset
68 Use *NewOps = getOperandList();
anatofuz
parents:
diff changeset
69
anatofuz
parents:
diff changeset
70 // Now copy from the old operands list to the new one.
anatofuz
parents:
diff changeset
71 std::copy(OldOps, OldOps + OldNumUses, NewOps);
anatofuz
parents:
diff changeset
72
anatofuz
parents:
diff changeset
73 // If this is a Phi, then we need to copy the BB pointers too.
anatofuz
parents:
diff changeset
74 if (IsPhi) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
75 auto *OldPtr = reinterpret_cast<char *>(OldOps + OldNumUses);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
76 auto *NewPtr = reinterpret_cast<char *>(NewOps + NewNumUses);
150
anatofuz
parents:
diff changeset
77 std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
anatofuz
parents:
diff changeset
78 }
anatofuz
parents:
diff changeset
79 Use::zap(OldOps, OldOps + OldNumUses, true);
anatofuz
parents:
diff changeset
80 }
anatofuz
parents:
diff changeset
81
anatofuz
parents:
diff changeset
82
anatofuz
parents:
diff changeset
83 // This is a private struct used by `User` to track the co-allocated descriptor
anatofuz
parents:
diff changeset
84 // section.
anatofuz
parents:
diff changeset
85 struct DescriptorInfo {
anatofuz
parents:
diff changeset
86 intptr_t SizeInBytes;
anatofuz
parents:
diff changeset
87 };
anatofuz
parents:
diff changeset
88
anatofuz
parents:
diff changeset
89 ArrayRef<const uint8_t> User::getDescriptor() const {
anatofuz
parents:
diff changeset
90 auto MutableARef = const_cast<User *>(this)->getDescriptor();
anatofuz
parents:
diff changeset
91 return {MutableARef.begin(), MutableARef.end()};
anatofuz
parents:
diff changeset
92 }
anatofuz
parents:
diff changeset
93
anatofuz
parents:
diff changeset
94 MutableArrayRef<uint8_t> User::getDescriptor() {
anatofuz
parents:
diff changeset
95 assert(HasDescriptor && "Don't call otherwise!");
anatofuz
parents:
diff changeset
96 assert(!HasHungOffUses && "Invariant!");
anatofuz
parents:
diff changeset
97
anatofuz
parents:
diff changeset
98 auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
anatofuz
parents:
diff changeset
99 assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 return MutableArrayRef<uint8_t>(
anatofuz
parents:
diff changeset
102 reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
anatofuz
parents:
diff changeset
103 }
anatofuz
parents:
diff changeset
104
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
105 bool User::isDroppable() const {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
106 if (const auto *Intr = dyn_cast<IntrinsicInst>(this))
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
107 return Intr->getIntrinsicID() == Intrinsic::assume;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
108 return false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
109 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
110
150
anatofuz
parents:
diff changeset
111 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
112 // User operator new Implementations
anatofuz
parents:
diff changeset
113 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
114
anatofuz
parents:
diff changeset
115 void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
anatofuz
parents:
diff changeset
116 unsigned DescBytes) {
anatofuz
parents:
diff changeset
117 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
anatofuz
parents:
diff changeset
118
anatofuz
parents:
diff changeset
119 static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
anatofuz
parents:
diff changeset
120
anatofuz
parents:
diff changeset
121 unsigned DescBytesToAllocate =
anatofuz
parents:
diff changeset
122 DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
anatofuz
parents:
diff changeset
123 assert(DescBytesToAllocate % sizeof(void *) == 0 &&
anatofuz
parents:
diff changeset
124 "We need this to satisfy alignment constraints for Uses");
anatofuz
parents:
diff changeset
125
anatofuz
parents:
diff changeset
126 uint8_t *Storage = static_cast<uint8_t *>(
anatofuz
parents:
diff changeset
127 ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
anatofuz
parents:
diff changeset
128 Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
anatofuz
parents:
diff changeset
129 Use *End = Start + Us;
anatofuz
parents:
diff changeset
130 User *Obj = reinterpret_cast<User*>(End);
anatofuz
parents:
diff changeset
131 Obj->NumUserOperands = Us;
anatofuz
parents:
diff changeset
132 Obj->HasHungOffUses = false;
anatofuz
parents:
diff changeset
133 Obj->HasDescriptor = DescBytes != 0;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
134 for (; Start != End; Start++)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
135 new (Start) Use(Obj);
150
anatofuz
parents:
diff changeset
136
anatofuz
parents:
diff changeset
137 if (DescBytes != 0) {
anatofuz
parents:
diff changeset
138 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
anatofuz
parents:
diff changeset
139 DescInfo->SizeInBytes = DescBytes;
anatofuz
parents:
diff changeset
140 }
anatofuz
parents:
diff changeset
141
anatofuz
parents:
diff changeset
142 return Obj;
anatofuz
parents:
diff changeset
143 }
anatofuz
parents:
diff changeset
144
anatofuz
parents:
diff changeset
145 void *User::operator new(size_t Size, unsigned Us) {
anatofuz
parents:
diff changeset
146 return allocateFixedOperandUser(Size, Us, 0);
anatofuz
parents:
diff changeset
147 }
anatofuz
parents:
diff changeset
148
anatofuz
parents:
diff changeset
149 void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
anatofuz
parents:
diff changeset
150 return allocateFixedOperandUser(Size, Us, DescBytes);
anatofuz
parents:
diff changeset
151 }
anatofuz
parents:
diff changeset
152
anatofuz
parents:
diff changeset
153 void *User::operator new(size_t Size) {
anatofuz
parents:
diff changeset
154 // Allocate space for a single Use*
anatofuz
parents:
diff changeset
155 void *Storage = ::operator new(Size + sizeof(Use *));
anatofuz
parents:
diff changeset
156 Use **HungOffOperandList = static_cast<Use **>(Storage);
anatofuz
parents:
diff changeset
157 User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
anatofuz
parents:
diff changeset
158 Obj->NumUserOperands = 0;
anatofuz
parents:
diff changeset
159 Obj->HasHungOffUses = true;
anatofuz
parents:
diff changeset
160 Obj->HasDescriptor = false;
anatofuz
parents:
diff changeset
161 *HungOffOperandList = nullptr;
anatofuz
parents:
diff changeset
162 return Obj;
anatofuz
parents:
diff changeset
163 }
anatofuz
parents:
diff changeset
164
anatofuz
parents:
diff changeset
165 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
166 // User operator delete Implementation
anatofuz
parents:
diff changeset
167 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
168
anatofuz
parents:
diff changeset
169 // Repress memory sanitization, due to use-after-destroy by operator
anatofuz
parents:
diff changeset
170 // delete. Bug report 24578 identifies this issue.
anatofuz
parents:
diff changeset
171 LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE void User::operator delete(void *Usr) {
anatofuz
parents:
diff changeset
172 // Hung off uses use a single Use* before the User, while other subclasses
anatofuz
parents:
diff changeset
173 // use a Use[] allocated prior to the user.
anatofuz
parents:
diff changeset
174 User *Obj = static_cast<User *>(Usr);
anatofuz
parents:
diff changeset
175 if (Obj->HasHungOffUses) {
anatofuz
parents:
diff changeset
176 assert(!Obj->HasDescriptor && "not supported!");
anatofuz
parents:
diff changeset
177
anatofuz
parents:
diff changeset
178 Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
anatofuz
parents:
diff changeset
179 // drop the hung off uses.
anatofuz
parents:
diff changeset
180 Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
anatofuz
parents:
diff changeset
181 /* Delete */ true);
anatofuz
parents:
diff changeset
182 ::operator delete(HungOffOperandList);
anatofuz
parents:
diff changeset
183 } else if (Obj->HasDescriptor) {
anatofuz
parents:
diff changeset
184 Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
anatofuz
parents:
diff changeset
185 Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
anatofuz
parents:
diff changeset
186
anatofuz
parents:
diff changeset
187 auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
anatofuz
parents:
diff changeset
188 uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
anatofuz
parents:
diff changeset
189 ::operator delete(Storage);
anatofuz
parents:
diff changeset
190 } else {
anatofuz
parents:
diff changeset
191 Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
anatofuz
parents:
diff changeset
192 Use::zap(Storage, Storage + Obj->NumUserOperands,
anatofuz
parents:
diff changeset
193 /* Delete */ false);
anatofuz
parents:
diff changeset
194 ::operator delete(Storage);
anatofuz
parents:
diff changeset
195 }
anatofuz
parents:
diff changeset
196 }
anatofuz
parents:
diff changeset
197
anatofuz
parents:
diff changeset
198 } // End llvm namespace