Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CGBuilder.h
Line
Count
Source (jump to first uncovered line)
1
//===-- CGBuilder.h - Choose IRBuilder implementation  ----------*- C++ -*-===//
2
//
3
// 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
6
//
7
//===----------------------------------------------------------------------===//
8
9
#ifndef LLVM_CLANG_LIB_CODEGEN_CGBUILDER_H
10
#define LLVM_CLANG_LIB_CODEGEN_CGBUILDER_H
11
12
#include "llvm/IR/DataLayout.h"
13
#include "llvm/IR/IRBuilder.h"
14
#include "Address.h"
15
#include "CodeGenTypeCache.h"
16
17
namespace clang {
18
namespace CodeGen {
19
20
class CodeGenFunction;
21
22
/// This is an IRBuilder insertion helper that forwards to
23
/// CodeGenFunction::InsertHelper, which adds necessary metadata to
24
/// instructions.
25
class CGBuilderInserter : protected llvm::IRBuilderDefaultInserter {
26
public:
27
1.67k
  CGBuilderInserter() = default;
28
243k
  explicit CGBuilderInserter(CodeGenFunction *CGF) : CGF(CGF) {}
29
30
protected:
31
  /// This forwards to CodeGenFunction::InsertHelper.
32
  void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
33
                    llvm::BasicBlock *BB,
34
                    llvm::BasicBlock::iterator InsertPt) const;
35
private:
36
  CodeGenFunction *CGF = nullptr;
37
};
38
39
typedef CGBuilderInserter CGBuilderInserterTy;
40
41
typedef llvm::IRBuilder<llvm::ConstantFolder, CGBuilderInserterTy>
42
    CGBuilderBaseTy;
43
44
class CGBuilderTy : public CGBuilderBaseTy {
45
  /// Storing a reference to the type cache here makes it a lot easier
46
  /// to build natural-feeling, target-specific IR.
47
  const CodeGenTypeCache &TypeCache;
48
public:
49
  CGBuilderTy(const CodeGenTypeCache &TypeCache, llvm::LLVMContext &C)
50
223
    : CGBuilderBaseTy(C), TypeCache(TypeCache) {}
51
  CGBuilderTy(const CodeGenTypeCache &TypeCache,
52
              llvm::LLVMContext &C, const llvm::ConstantFolder &F,
53
              const CGBuilderInserterTy &Inserter)
54
243k
    : CGBuilderBaseTy(C, F, Inserter), TypeCache(TypeCache) {}
55
  CGBuilderTy(const CodeGenTypeCache &TypeCache, llvm::Instruction *I)
56
133
    : CGBuilderBaseTy(I), TypeCache(TypeCache) {}
57
  CGBuilderTy(const CodeGenTypeCache &TypeCache, llvm::BasicBlock *BB)
58
1.32k
    : CGBuilderBaseTy(BB), TypeCache(TypeCache) {}
59
60
57.6k
  llvm::ConstantInt *getSize(CharUnits N) {
61
57.6k
    return llvm::ConstantInt::get(TypeCache.SizeTy, N.getQuantity());
62
57.6k
  }
63
54.1k
  llvm::ConstantInt *getSize(uint64_t N) {
64
54.1k
    return llvm::ConstantInt::get(TypeCache.SizeTy, N);
65
54.1k
  }
66
67
  // Note that we intentionally hide the CreateLoad APIs that don't
68
  // take an alignment.
69
74.4k
  llvm::LoadInst *CreateLoad(Address Addr, const llvm::Twine &Name = "") {
70
74.4k
    return CreateAlignedLoad(Addr.getPointer(),
71
74.4k
                             Addr.getAlignment().getAsAlign(), Name);
72
74.4k
  }
73
102k
  llvm::LoadInst *CreateLoad(Address Addr, const char *Name) {
74
102k
    // This overload is required to prevent string literals from
75
102k
    // ending up in the IsVolatile overload.
76
102k
    return CreateAlignedLoad(Addr.getPointer(),
77
102k
                             Addr.getAlignment().getAsAlign(), Name);
78
102k
  }
79
  llvm::LoadInst *CreateLoad(Address Addr, bool IsVolatile,
80
844k
                             const llvm::Twine &Name = "") {
81
844k
    return CreateAlignedLoad(
82
844k
        Addr.getPointer(), Addr.getAlignment().getAsAlign(), IsVolatile, Name);
83
844k
  }
84
85
  using CGBuilderBaseTy::CreateAlignedLoad;
86
  llvm::LoadInst *CreateAlignedLoad(llvm::Value *Addr, CharUnits Align,
87
19.8k
                                    const llvm::Twine &Name = "") {
88
19.8k
    return CreateAlignedLoad(Addr, Align.getAsAlign(), Name);
89
19.8k
  }
90
  llvm::LoadInst *CreateAlignedLoad(llvm::Value *Addr, CharUnits Align,
91
3.24k
                                    const char *Name) {
92
3.24k
    return CreateAlignedLoad(Addr, Align.getAsAlign(), Name);
93
3.24k
  }
94
  llvm::LoadInst *CreateAlignedLoad(llvm::Type *Ty, llvm::Value *Addr,
95
                                    CharUnits Align,
96
470
                                    const llvm::Twine &Name = "") {
97
470
    assert(Addr->getType()->getPointerElementType() == Ty);
98
470
    return CreateAlignedLoad(Addr, Align.getAsAlign(), Name);
99
470
  }
100
101
  // Note that we intentionally hide the CreateStore APIs that don't
102
  // take an alignment.
103
  llvm::StoreInst *CreateStore(llvm::Value *Val, Address Addr,
104
908k
                               bool IsVolatile = false) {
105
908k
    return CreateAlignedStore(Val, Addr.getPointer(),
106
908k
                              Addr.getAlignment().getAsAlign(), IsVolatile);
107
908k
  }
108
109
  using CGBuilderBaseTy::CreateAlignedStore;
110
  llvm::StoreInst *CreateAlignedStore(llvm::Value *Val, llvm::Value *Addr,
111
5.05k
                                      CharUnits Align, bool IsVolatile = false) {
112
5.05k
    return CreateAlignedStore(Val, Addr, Align.getAsAlign(), IsVolatile);
113
5.05k
  }
114
115
  // FIXME: these "default-aligned" APIs should be removed,
116
  // but I don't feel like fixing all the builtin code right now.
117
  llvm::StoreInst *CreateDefaultAlignedStore(llvm::Value *Val,
118
                                             llvm::Value *Addr,
119
955
                                             bool IsVolatile = false) {
120
955
    return CGBuilderBaseTy::CreateStore(Val, Addr, IsVolatile);
121
955
  }
122
123
  /// Emit a load from an i1 flag variable.
124
  llvm::LoadInst *CreateFlagLoad(llvm::Value *Addr,
125
208
                                 const llvm::Twine &Name = "") {
126
208
    assert(Addr->getType()->getPointerElementType() == getInt1Ty());
127
208
    return CreateAlignedLoad(getInt1Ty(), Addr, CharUnits::One(), Name);
128
208
  }
129
130
  /// Emit a store to an i1 flag variable.
131
214
  llvm::StoreInst *CreateFlagStore(bool Value, llvm::Value *Addr) {
132
214
    assert(Addr->getType()->getPointerElementType() == getInt1Ty());
133
214
    return CreateAlignedStore(getInt1(Value), Addr, CharUnits::One());
134
214
  }
135
136
  using CGBuilderBaseTy::CreateBitCast;
137
  Address CreateBitCast(Address Addr, llvm::Type *Ty,
138
380k
                        const llvm::Twine &Name = "") {
139
380k
    return Address(CreateBitCast(Addr.getPointer(), Ty, Name),
140
380k
                   Addr.getAlignment());
141
380k
  }
142
143
  using CGBuilderBaseTy::CreateAddrSpaceCast;
144
  Address CreateAddrSpaceCast(Address Addr, llvm::Type *Ty,
145
5
                              const llvm::Twine &Name = "") {
146
5
    return Address(CreateAddrSpaceCast(Addr.getPointer(), Ty, Name),
147
5
                   Addr.getAlignment());
148
5
  }
149
150
  /// Cast the element type of the given address to a different type,
151
  /// preserving information like the alignment and address space.
152
  Address CreateElementBitCast(Address Addr, llvm::Type *Ty,
153
325k
                               const llvm::Twine &Name = "") {
154
325k
    auto PtrTy = Ty->getPointerTo(Addr.getAddressSpace());
155
325k
    return CreateBitCast(Addr, PtrTy, Name);
156
325k
  }
157
158
  using CGBuilderBaseTy::CreatePointerBitCastOrAddrSpaceCast;
159
  Address CreatePointerBitCastOrAddrSpaceCast(Address Addr, llvm::Type *Ty,
160
1.53k
                                              const llvm::Twine &Name = "") {
161
1.53k
    llvm::Value *Ptr =
162
1.53k
      CreatePointerBitCastOrAddrSpaceCast(Addr.getPointer(), Ty, Name);
163
1.53k
    return Address(Ptr, Addr.getAlignment());
164
1.53k
  }
165
166
  /// Given
167
  ///   %addr = {T1, T2...}* ...
168
  /// produce
169
  ///   %name = getelementptr inbounds %addr, i32 0, i32 index
170
  ///
171
  /// This API assumes that drilling into a struct like this is always an
172
  /// inbounds operation.
173
  using CGBuilderBaseTy::CreateStructGEP;
174
  Address CreateStructGEP(Address Addr, unsigned Index,
175
202k
                          const llvm::Twine &Name = "") {
176
202k
    llvm::StructType *ElTy = cast<llvm::StructType>(Addr.getElementType());
177
202k
    const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
178
202k
    const llvm::StructLayout *Layout = DL.getStructLayout(ElTy);
179
202k
    auto Offset = CharUnits::fromQuantity(Layout->getElementOffset(Index));
180
202k
181
202k
    return Address(CreateStructGEP(Addr.getElementType(),
182
202k
                                   Addr.getPointer(), Index, Name),
183
202k
                   Addr.getAlignment().alignmentAtOffset(Offset));
184
202k
  }
185
186
  /// Given
187
  ///   %addr = [n x T]* ...
188
  /// produce
189
  ///   %name = getelementptr inbounds %addr, i64 0, i64 index
190
  /// where i64 is actually the target word size.
191
  ///
192
  /// This API assumes that drilling into an array like this is always
193
  /// an inbounds operation.
194
  Address CreateConstArrayGEP(Address Addr, uint64_t Index,
195
53.6k
                              const llvm::Twine &Name = "") {
196
53.6k
    llvm::ArrayType *ElTy = cast<llvm::ArrayType>(Addr.getElementType());
197
53.6k
    const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
198
53.6k
    CharUnits EltSize =
199
53.6k
        CharUnits::fromQuantity(DL.getTypeAllocSize(ElTy->getElementType()));
200
53.6k
201
53.6k
    return Address(
202
53.6k
        CreateInBoundsGEP(Addr.getPointer(),
203
53.6k
                          {getSize(CharUnits::Zero()), getSize(Index)}, Name),
204
53.6k
        Addr.getAlignment().alignmentAtOffset(Index * EltSize));
205
53.6k
  }
206
207
  /// Given
208
  ///   %addr = T* ...
209
  /// produce
210
  ///   %name = getelementptr inbounds %addr, i64 index
211
  /// where i64 is actually the target word size.
212
  Address CreateConstInBoundsGEP(Address Addr, uint64_t Index,
213
331
                                 const llvm::Twine &Name = "") {
214
331
    llvm::Type *ElTy = Addr.getElementType();
215
331
    const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
216
331
    CharUnits EltSize = CharUnits::fromQuantity(DL.getTypeAllocSize(ElTy));
217
331
218
331
    return Address(CreateInBoundsGEP(Addr.getElementType(), Addr.getPointer(),
219
331
                                     getSize(Index), Name),
220
331
                   Addr.getAlignment().alignmentAtOffset(Index * EltSize));
221
331
  }
222
223
  /// Given
224
  ///   %addr = T* ...
225
  /// produce
226
  ///   %name = getelementptr inbounds %addr, i64 index
227
  /// where i64 is actually the target word size.
228
  Address CreateConstGEP(Address Addr, uint64_t Index,
229
124
                         const llvm::Twine &Name = "") {
230
124
    const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
231
124
    CharUnits EltSize =
232
124
        CharUnits::fromQuantity(DL.getTypeAllocSize(Addr.getElementType()));
233
124
234
124
    return Address(CreateGEP(Addr.getElementType(), Addr.getPointer(),
235
124
                             getSize(Index), Name),
236
124
                   Addr.getAlignment().alignmentAtOffset(Index * EltSize));
237
124
  }
238
239
  /// Given a pointer to i8, adjust it by a given constant offset.
240
  Address CreateConstInBoundsByteGEP(Address Addr, CharUnits Offset,
241
3.32k
                                     const llvm::Twine &Name = "") {
242
3.32k
    assert(Addr.getElementType() == TypeCache.Int8Ty);
243
3.32k
    return Address(CreateInBoundsGEP(Addr.getPointer(), getSize(Offset), Name),
244
3.32k
                   Addr.getAlignment().alignmentAtOffset(Offset));
245
3.32k
  }
246
  Address CreateConstByteGEP(Address Addr, CharUnits Offset,
247
555
                             const llvm::Twine &Name = "") {
248
555
    assert(Addr.getElementType() == TypeCache.Int8Ty);
249
555
    return Address(CreateGEP(Addr.getPointer(), getSize(Offset), Name),
250
555
                   Addr.getAlignment().alignmentAtOffset(Offset));
251
555
  }
252
253
  using CGBuilderBaseTy::CreateConstInBoundsGEP2_32;
254
  Address CreateConstInBoundsGEP2_32(Address Addr, unsigned Idx0, unsigned Idx1,
255
187
                                     const llvm::Twine &Name = "") {
256
187
    const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
257
187
258
187
    auto *GEP = cast<llvm::GetElementPtrInst>(CreateConstInBoundsGEP2_32(
259
187
        Addr.getElementType(), Addr.getPointer(), Idx0, Idx1, Name));
260
187
    llvm::APInt Offset(
261
187
        DL.getIndexSizeInBits(Addr.getType()->getPointerAddressSpace()), 0,
262
187
        /*isSigned=*/true);
263
187
    if (!GEP->accumulateConstantOffset(DL, Offset))
264
187
      
llvm_unreachable0
("offset of GEP with constants is always computable");
265
187
    return Address(GEP, Addr.getAlignment().alignmentAtOffset(
266
187
                            CharUnits::fromQuantity(Offset.getSExtValue())));
267
187
  }
268
269
  using CGBuilderBaseTy::CreateMemCpy;
270
  llvm::CallInst *CreateMemCpy(Address Dest, Address Src, llvm::Value *Size,
271
13.6k
                               bool IsVolatile = false) {
272
13.6k
    return CreateMemCpy(Dest.getPointer(), Dest.getAlignment().getAsAlign(),
273
13.6k
                        Src.getPointer(), Src.getAlignment().getAsAlign(), Size,
274
13.6k
                        IsVolatile);
275
13.6k
  }
276
  llvm::CallInst *CreateMemCpy(Address Dest, Address Src, uint64_t Size,
277
311
                               bool IsVolatile = false) {
278
311
    return CreateMemCpy(Dest.getPointer(), Dest.getAlignment().getAsAlign(),
279
311
                        Src.getPointer(), Src.getAlignment().getAsAlign(), Size,
280
311
                        IsVolatile);
281
311
  }
282
283
  using CGBuilderBaseTy::CreateMemCpyInline;
284
4
  llvm::CallInst *CreateMemCpyInline(Address Dest, Address Src, uint64_t Size) {
285
4
    return CreateMemCpyInline(
286
4
        Dest.getPointer(), Dest.getAlignment().getAsAlign(), Src.getPointer(),
287
4
        Src.getAlignment().getAsAlign(), getInt64(Size));
288
4
  }
289
290
  using CGBuilderBaseTy::CreateMemMove;
291
  llvm::CallInst *CreateMemMove(Address Dest, Address Src, llvm::Value *Size,
292
13
                                bool IsVolatile = false) {
293
13
    return CreateMemMove(Dest.getPointer(), Dest.getAlignment().getAsAlign(),
294
13
                         Src.getPointer(), Src.getAlignment().getAsAlign(),
295
13
                         Size, IsVolatile);
296
13
  }
297
298
  using CGBuilderBaseTy::CreateMemSet;
299
  llvm::CallInst *CreateMemSet(Address Dest, llvm::Value *Value,
300
2.36k
                               llvm::Value *Size, bool IsVolatile = false) {
301
2.36k
    return CreateMemSet(Dest.getPointer(), Value, Size,
302
2.36k
                        Dest.getAlignment().getAsAlign(), IsVolatile);
303
2.36k
  }
304
305
  using CGBuilderBaseTy::CreatePreserveStructAccessIndex;
306
  Address CreatePreserveStructAccessIndex(Address Addr,
307
                                          unsigned Index,
308
                                          unsigned FieldIndex,
309
38
                                          llvm::MDNode *DbgInfo) {
310
38
    llvm::StructType *ElTy = cast<llvm::StructType>(Addr.getElementType());
311
38
    const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
312
38
    const llvm::StructLayout *Layout = DL.getStructLayout(ElTy);
313
38
    auto Offset = CharUnits::fromQuantity(Layout->getElementOffset(Index));
314
38
315
38
    return Address(CreatePreserveStructAccessIndex(ElTy, Addr.getPointer(),
316
38
                                                   Index, FieldIndex, DbgInfo),
317
38
                   Addr.getAlignment().alignmentAtOffset(Offset));
318
38
  }
319
};
320
321
}  // end namespace CodeGen
322
}  // end namespace clang
323
324
#endif