blob: 24e3ca19709cb9e946c6066fa61b97dfbc236769 [file] [log] [blame]
John McCall5ad74072017-03-02 20:04:19 +00001//===--- ConstantInitBuilder.cpp - Global initializer builder -------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
John McCall5ad74072017-03-02 20:04:19 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines out-of-line routines for building initializers for
10// global variables, in particular the kind of globals that are implicitly
11// introduced by various language ABIs.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/CodeGen/ConstantInitBuilder.h"
16#include "CodeGenModule.h"
17
18using namespace clang;
19using namespace CodeGen;
20
John McCall262f9622017-03-06 19:04:16 +000021llvm::Type *ConstantInitFuture::getType() const {
22 assert(Data && "dereferencing null future");
23 if (Data.is<llvm::Constant*>()) {
24 return Data.get<llvm::Constant*>()->getType();
25 } else {
26 return Data.get<ConstantInitBuilderBase*>()->Buffer[0]->getType();
27 }
28}
29
30void ConstantInitFuture::abandon() {
31 assert(Data && "abandoning null future");
32 if (auto builder = Data.dyn_cast<ConstantInitBuilderBase*>()) {
33 builder->abandon(0);
34 }
35 Data = nullptr;
36}
37
38void ConstantInitFuture::installInGlobal(llvm::GlobalVariable *GV) {
39 assert(Data && "installing null future");
40 if (Data.is<llvm::Constant*>()) {
41 GV->setInitializer(Data.get<llvm::Constant*>());
42 } else {
43 auto &builder = *Data.get<ConstantInitBuilderBase*>();
44 assert(builder.Buffer.size() == 1);
45 builder.setGlobalInitializer(GV, builder.Buffer[0]);
46 builder.Buffer.clear();
47 Data = nullptr;
48 }
49}
50
51ConstantInitFuture
52ConstantInitBuilderBase::createFuture(llvm::Constant *initializer) {
53 assert(Buffer.empty() && "buffer not current empty");
54 Buffer.push_back(initializer);
55 return ConstantInitFuture(this);
56}
57
58// Only used in this file.
59inline ConstantInitFuture::ConstantInitFuture(ConstantInitBuilderBase *builder)
60 : Data(builder) {
61 assert(!builder->Frozen);
62 assert(builder->Buffer.size() == 1);
63 assert(builder->Buffer[0] != nullptr);
64}
65
John McCall5ad74072017-03-02 20:04:19 +000066llvm::GlobalVariable *
John McCall32e0d182017-03-04 21:26:29 +000067ConstantInitBuilderBase::createGlobal(llvm::Constant *initializer,
68 const llvm::Twine &name,
69 CharUnits alignment,
70 bool constant,
71 llvm::GlobalValue::LinkageTypes linkage,
72 unsigned addressSpace) {
John McCall5ad74072017-03-02 20:04:19 +000073 auto GV = new llvm::GlobalVariable(CGM.getModule(),
74 initializer->getType(),
75 constant,
76 linkage,
77 initializer,
78 name,
79 /*insert before*/ nullptr,
80 llvm::GlobalValue::NotThreadLocal,
81 addressSpace);
Guillaume Chateletc79099e2019-10-03 13:00:29 +000082 GV->setAlignment(alignment.getAsAlign());
John McCall5ad74072017-03-02 20:04:19 +000083 resolveSelfReferences(GV);
84 return GV;
85}
86
John McCall32e0d182017-03-04 21:26:29 +000087void ConstantInitBuilderBase::setGlobalInitializer(llvm::GlobalVariable *GV,
88 llvm::Constant *initializer){
John McCall5ad74072017-03-02 20:04:19 +000089 GV->setInitializer(initializer);
90
91 if (!SelfReferences.empty())
92 resolveSelfReferences(GV);
93}
94
John McCall32e0d182017-03-04 21:26:29 +000095void ConstantInitBuilderBase::resolveSelfReferences(llvm::GlobalVariable *GV) {
John McCall5ad74072017-03-02 20:04:19 +000096 for (auto &entry : SelfReferences) {
97 llvm::Constant *resolvedReference =
98 llvm::ConstantExpr::getInBoundsGetElementPtr(
99 GV->getValueType(), GV, entry.Indices);
John McCall262f9622017-03-06 19:04:16 +0000100 auto dummy = entry.Dummy;
101 dummy->replaceAllUsesWith(resolvedReference);
102 dummy->eraseFromParent();
103 }
104 SelfReferences.clear();
105}
106
107void ConstantInitBuilderBase::abandon(size_t newEnd) {
108 // Remove all the entries we've added.
109 Buffer.erase(Buffer.begin() + newEnd, Buffer.end());
110
111 // If we're abandoning all the way to the beginning, destroy
112 // all the self-references, because we might not get another
113 // opportunity.
114 if (newEnd == 0) {
115 for (auto &entry : SelfReferences) {
116 auto dummy = entry.Dummy;
117 dummy->replaceAllUsesWith(llvm::UndefValue::get(dummy->getType()));
118 dummy->eraseFromParent();
119 }
120 SelfReferences.clear();
John McCall5ad74072017-03-02 20:04:19 +0000121 }
122}
123
John McCall32e0d182017-03-04 21:26:29 +0000124void ConstantAggregateBuilderBase::addSize(CharUnits size) {
John McCall5ad74072017-03-02 20:04:19 +0000125 add(Builder.CGM.getSize(size));
126}
127
128llvm::Constant *
John McCall32e0d182017-03-04 21:26:29 +0000129ConstantAggregateBuilderBase::getRelativeOffset(llvm::IntegerType *offsetType,
130 llvm::Constant *target) {
Leonard Chan71568a92020-06-11 11:17:08 -0700131 return getRelativeOffsetToPosition(offsetType, target,
Arnold Schwaighofer4a8120c2020-06-15 10:28:06 -0700132 Builder.Buffer.size() - Begin);
Leonard Chan71568a92020-06-11 11:17:08 -0700133}
134
135llvm::Constant *ConstantAggregateBuilderBase::getRelativeOffsetToPosition(
136 llvm::IntegerType *offsetType, llvm::Constant *target, size_t position) {
John McCall32e0d182017-03-04 21:26:29 +0000137 // Compute the address of the relative-address slot.
Leonard Chan71568a92020-06-11 11:17:08 -0700138 auto base = getAddrOfPosition(offsetType, position);
John McCall32e0d182017-03-04 21:26:29 +0000139
140 // Subtract.
141 base = llvm::ConstantExpr::getPtrToInt(base, Builder.CGM.IntPtrTy);
142 target = llvm::ConstantExpr::getPtrToInt(target, Builder.CGM.IntPtrTy);
143 llvm::Constant *offset = llvm::ConstantExpr::getSub(target, base);
144
145 // Truncate to the relative-address type if necessary.
146 if (Builder.CGM.IntPtrTy != offsetType) {
147 offset = llvm::ConstantExpr::getTrunc(offset, offsetType);
148 }
149
150 return offset;
151}
152
153llvm::Constant *
Leonard Chan71568a92020-06-11 11:17:08 -0700154ConstantAggregateBuilderBase::getAddrOfPosition(llvm::Type *type,
155 size_t position) {
156 // Make a global variable. We will replace this with a GEP to this
157 // position after installing the initializer.
158 auto dummy = new llvm::GlobalVariable(Builder.CGM.getModule(), type, true,
159 llvm::GlobalVariable::PrivateLinkage,
160 nullptr, "");
161 Builder.SelfReferences.emplace_back(dummy);
162 auto &entry = Builder.SelfReferences.back();
163 (void)getGEPIndicesTo(entry.Indices, position + Begin);
164 return dummy;
165}
166
167llvm::Constant *
John McCall32e0d182017-03-04 21:26:29 +0000168ConstantAggregateBuilderBase::getAddrOfCurrentPosition(llvm::Type *type) {
John McCall5ad74072017-03-02 20:04:19 +0000169 // Make a global variable. We will replace this with a GEP to this
170 // position after installing the initializer.
171 auto dummy =
172 new llvm::GlobalVariable(Builder.CGM.getModule(), type, true,
173 llvm::GlobalVariable::PrivateLinkage,
174 nullptr, "");
175 Builder.SelfReferences.emplace_back(dummy);
176 auto &entry = Builder.SelfReferences.back();
177 (void) getGEPIndicesToCurrentPosition(entry.Indices);
178 return dummy;
179}
180
John McCall32e0d182017-03-04 21:26:29 +0000181void ConstantAggregateBuilderBase::getGEPIndicesTo(
John McCall5ad74072017-03-02 20:04:19 +0000182 llvm::SmallVectorImpl<llvm::Constant*> &indices,
183 size_t position) const {
184 // Recurse on the parent builder if present.
185 if (Parent) {
186 Parent->getGEPIndicesTo(indices, Begin);
187
Fangrui Song6907ce22018-07-30 19:24:48 +0000188 // Otherwise, add an index to drill into the first level of pointer.
John McCall5ad74072017-03-02 20:04:19 +0000189 } else {
190 assert(indices.empty());
191 indices.push_back(llvm::ConstantInt::get(Builder.CGM.Int32Ty, 0));
192 }
193
194 assert(position >= Begin);
195 // We have to use i32 here because struct GEPs demand i32 indices.
196 // It's rather unlikely to matter in practice.
197 indices.push_back(llvm::ConstantInt::get(Builder.CGM.Int32Ty,
198 position - Begin));
199}
200
John McCall262f9622017-03-06 19:04:16 +0000201ConstantAggregateBuilderBase::PlaceholderPosition
202ConstantAggregateBuilderBase::addPlaceholderWithSize(llvm::Type *type) {
203 // Bring the offset up to the last field.
204 CharUnits offset = getNextOffsetFromGlobal();
205
206 // Create the placeholder.
207 auto position = addPlaceholder();
208
209 // Advance the offset past that field.
210 auto &layout = Builder.CGM.getDataLayout();
211 if (!Packed)
212 offset = offset.alignTo(CharUnits::fromQuantity(
213 layout.getABITypeAlignment(type)));
214 offset += CharUnits::fromQuantity(layout.getTypeStoreSize(type));
215
216 CachedOffsetEnd = Builder.Buffer.size();
217 CachedOffsetFromGlobal = offset;
218
219 return position;
220}
221
John McCall32e0d182017-03-04 21:26:29 +0000222CharUnits ConstantAggregateBuilderBase::getOffsetFromGlobalTo(size_t end) const{
223 size_t cacheEnd = CachedOffsetEnd;
224 assert(cacheEnd <= end);
225
226 // Fast path: if the cache is valid, just use it.
227 if (cacheEnd == end) {
228 return CachedOffsetFromGlobal;
229 }
230
231 // If the cached range ends before the index at which the current
232 // aggregate starts, recurse for the parent.
233 CharUnits offset;
234 if (cacheEnd < Begin) {
235 assert(cacheEnd == 0);
236 assert(Parent && "Begin != 0 for root builder");
237 cacheEnd = Begin;
238 offset = Parent->getOffsetFromGlobalTo(Begin);
239 } else {
240 offset = CachedOffsetFromGlobal;
241 }
242
243 // Perform simple layout on the elements in cacheEnd..<end.
244 if (cacheEnd != end) {
245 auto &layout = Builder.CGM.getDataLayout();
246 do {
247 llvm::Constant *element = Builder.Buffer[cacheEnd];
248 assert(element != nullptr &&
249 "cannot compute offset when a placeholder is present");
250 llvm::Type *elementType = element->getType();
John McCall262f9622017-03-06 19:04:16 +0000251 if (!Packed)
252 offset = offset.alignTo(CharUnits::fromQuantity(
253 layout.getABITypeAlignment(elementType)));
John McCall32e0d182017-03-04 21:26:29 +0000254 offset += CharUnits::fromQuantity(layout.getTypeStoreSize(elementType));
255 } while (++cacheEnd != end);
256 }
257
258 // Cache and return.
259 CachedOffsetEnd = cacheEnd;
260 CachedOffsetFromGlobal = offset;
261 return offset;
262}
263
264llvm::Constant *ConstantAggregateBuilderBase::finishArray(llvm::Type *eltTy) {
John McCall5ad74072017-03-02 20:04:19 +0000265 markFinished();
266
267 auto &buffer = getBuffer();
268 assert((Begin < buffer.size() ||
John McCall32e0d182017-03-04 21:26:29 +0000269 (Begin == buffer.size() && eltTy))
John McCall5ad74072017-03-02 20:04:19 +0000270 && "didn't add any array elements without element type");
271 auto elts = llvm::makeArrayRef(buffer).slice(Begin);
John McCall32e0d182017-03-04 21:26:29 +0000272 if (!eltTy) eltTy = elts[0]->getType();
John McCall5ad74072017-03-02 20:04:19 +0000273 auto type = llvm::ArrayType::get(eltTy, elts.size());
274 auto constant = llvm::ConstantArray::get(type, elts);
275 buffer.erase(buffer.begin() + Begin, buffer.end());
276 return constant;
277}
278
John McCall32e0d182017-03-04 21:26:29 +0000279llvm::Constant *
280ConstantAggregateBuilderBase::finishStruct(llvm::StructType *ty) {
John McCall5ad74072017-03-02 20:04:19 +0000281 markFinished();
282
283 auto &buffer = getBuffer();
John McCall5ad74072017-03-02 20:04:19 +0000284 auto elts = llvm::makeArrayRef(buffer).slice(Begin);
285
John McCall262f9622017-03-06 19:04:16 +0000286 if (ty == nullptr && elts.empty())
287 ty = llvm::StructType::get(Builder.CGM.getLLVMContext(), {}, Packed);
288
John McCall5ad74072017-03-02 20:04:19 +0000289 llvm::Constant *constant;
John McCall32e0d182017-03-04 21:26:29 +0000290 if (ty) {
John McCall262f9622017-03-06 19:04:16 +0000291 assert(ty->isPacked() == Packed);
John McCall32e0d182017-03-04 21:26:29 +0000292 constant = llvm::ConstantStruct::get(ty, elts);
John McCall5ad74072017-03-02 20:04:19 +0000293 } else {
John McCall262f9622017-03-06 19:04:16 +0000294 constant = llvm::ConstantStruct::getAnon(elts, Packed);
John McCall5ad74072017-03-02 20:04:19 +0000295 }
296
297 buffer.erase(buffer.begin() + Begin, buffer.end());
298 return constant;
299}