clang  9.0.0svn
CGBuilder.h
Go to the documentation of this file.
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  CGBuilderInserter() = default;
28  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 
40 
41 typedef llvm::IRBuilder<llvm::ConstantFolder, CGBuilderInserterTy>
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  : CGBuilderBaseTy(C), TypeCache(TypeCache) {}
51  CGBuilderTy(const CodeGenTypeCache &TypeCache,
52  llvm::LLVMContext &C, const llvm::ConstantFolder &F,
53  const CGBuilderInserterTy &Inserter)
54  : CGBuilderBaseTy(C, F, Inserter), TypeCache(TypeCache) {}
55  CGBuilderTy(const CodeGenTypeCache &TypeCache, llvm::Instruction *I)
56  : CGBuilderBaseTy(I), TypeCache(TypeCache) {}
57  CGBuilderTy(const CodeGenTypeCache &TypeCache, llvm::BasicBlock *BB)
58  : CGBuilderBaseTy(BB), TypeCache(TypeCache) {}
59 
60  llvm::ConstantInt *getSize(CharUnits N) {
61  return llvm::ConstantInt::get(TypeCache.SizeTy, N.getQuantity());
62  }
63  llvm::ConstantInt *getSize(uint64_t N) {
64  return llvm::ConstantInt::get(TypeCache.SizeTy, N);
65  }
66 
67  // Note that we intentionally hide the CreateLoad APIs that don't
68  // take an alignment.
69  llvm::LoadInst *CreateLoad(Address Addr, const llvm::Twine &Name = "") {
70  return CreateAlignedLoad(Addr.getPointer(),
71  Addr.getAlignment().getQuantity(),
72  Name);
73  }
74  llvm::LoadInst *CreateLoad(Address Addr, const char *Name) {
75  // This overload is required to prevent string literals from
76  // ending up in the IsVolatile overload.
77  return CreateAlignedLoad(Addr.getPointer(),
78  Addr.getAlignment().getQuantity(),
79  Name);
80  }
81  llvm::LoadInst *CreateLoad(Address Addr, bool IsVolatile,
82  const llvm::Twine &Name = "") {
83  return CreateAlignedLoad(Addr.getPointer(),
84  Addr.getAlignment().getQuantity(),
85  IsVolatile,
86  Name);
87  }
88 
89  using CGBuilderBaseTy::CreateAlignedLoad;
90  llvm::LoadInst *CreateAlignedLoad(llvm::Value *Addr, CharUnits Align,
91  const llvm::Twine &Name = "") {
92  return CreateAlignedLoad(Addr, Align.getQuantity(), Name);
93  }
94  llvm::LoadInst *CreateAlignedLoad(llvm::Value *Addr, CharUnits Align,
95  const char *Name) {
96  return CreateAlignedLoad(Addr, Align.getQuantity(), Name);
97  }
98  llvm::LoadInst *CreateAlignedLoad(llvm::Type *Ty, llvm::Value *Addr,
99  CharUnits Align,
100  const llvm::Twine &Name = "") {
101  assert(Addr->getType()->getPointerElementType() == Ty);
102  return CreateAlignedLoad(Addr, Align.getQuantity(), Name);
103  }
104 
105  // Note that we intentionally hide the CreateStore APIs that don't
106  // take an alignment.
107  llvm::StoreInst *CreateStore(llvm::Value *Val, Address Addr,
108  bool IsVolatile = false) {
109  return CreateAlignedStore(Val, Addr.getPointer(),
110  Addr.getAlignment().getQuantity(), IsVolatile);
111  }
112 
113  using CGBuilderBaseTy::CreateAlignedStore;
114  llvm::StoreInst *CreateAlignedStore(llvm::Value *Val, llvm::Value *Addr,
115  CharUnits Align, bool IsVolatile = false) {
116  return CreateAlignedStore(Val, Addr, Align.getQuantity(), IsVolatile);
117  }
118 
119  // FIXME: these "default-aligned" APIs should be removed,
120  // but I don't feel like fixing all the builtin code right now.
121  llvm::StoreInst *CreateDefaultAlignedStore(llvm::Value *Val,
122  llvm::Value *Addr,
123  bool IsVolatile = false) {
124  return CGBuilderBaseTy::CreateStore(Val, Addr, IsVolatile);
125  }
126 
127  /// Emit a load from an i1 flag variable.
128  llvm::LoadInst *CreateFlagLoad(llvm::Value *Addr,
129  const llvm::Twine &Name = "") {
130  assert(Addr->getType()->getPointerElementType() == getInt1Ty());
131  return CreateAlignedLoad(getInt1Ty(), Addr, CharUnits::One(), Name);
132  }
133 
134  /// Emit a store to an i1 flag variable.
135  llvm::StoreInst *CreateFlagStore(bool Value, llvm::Value *Addr) {
136  assert(Addr->getType()->getPointerElementType() == getInt1Ty());
137  return CreateAlignedStore(getInt1(Value), Addr, CharUnits::One());
138  }
139 
140  using CGBuilderBaseTy::CreateBitCast;
142  const llvm::Twine &Name = "") {
143  return Address(CreateBitCast(Addr.getPointer(), Ty, Name),
144  Addr.getAlignment());
145  }
146 
147  using CGBuilderBaseTy::CreateAddrSpaceCast;
149  const llvm::Twine &Name = "") {
150  return Address(CreateAddrSpaceCast(Addr.getPointer(), Ty, Name),
151  Addr.getAlignment());
152  }
153 
154  /// Cast the element type of the given address to a different type,
155  /// preserving information like the alignment and address space.
157  const llvm::Twine &Name = "") {
158  auto PtrTy = Ty->getPointerTo(Addr.getAddressSpace());
159  return CreateBitCast(Addr, PtrTy, Name);
160  }
161 
162  using CGBuilderBaseTy::CreatePointerBitCastOrAddrSpaceCast;
164  const llvm::Twine &Name = "") {
165  llvm::Value *Ptr =
166  CreatePointerBitCastOrAddrSpaceCast(Addr.getPointer(), Ty, Name);
167  return Address(Ptr, Addr.getAlignment());
168  }
169 
170  using CGBuilderBaseTy::CreateStructGEP;
172  const llvm::Twine &Name = "") {
173  return Address(CreateStructGEP(Addr.getElementType(),
174  Addr.getPointer(), Index, Name),
175  Addr.getAlignment().alignmentAtOffset(Offset));
176  }
177  Address CreateStructGEP(Address Addr, unsigned Index,
178  const llvm::StructLayout *Layout,
179  const llvm::Twine &Name = "") {
180  auto Offset = CharUnits::fromQuantity(Layout->getElementOffset(Index));
181  return CreateStructGEP(Addr, Index, Offset, Name);
182  }
183 
184  /// Given
185  /// %addr = [n x T]* ...
186  /// produce
187  /// %name = getelementptr inbounds %addr, i64 0, i64 index
188  /// where i64 is actually the target word size.
189  ///
190  /// This API assumes that drilling into an array like this is always
191  /// an inbounds operation.
192  ///
193  /// \param EltSize - the size of the type T in bytes
194  Address CreateConstArrayGEP(Address Addr, uint64_t Index, CharUnits EltSize,
195  const llvm::Twine &Name = "") {
196  return Address(CreateInBoundsGEP(Addr.getPointer(),
197  {getSize(CharUnits::Zero()),
198  getSize(Index)},
199  Name),
200  Addr.getAlignment().alignmentAtOffset(Index * EltSize));
201  }
202 
203  /// Given
204  /// %addr = T* ...
205  /// produce
206  /// %name = getelementptr inbounds %addr, i64 index
207  /// where i64 is actually the target word size.
208  ///
209  /// \param EltSize - the size of the type T in bytes
210  Address CreateConstInBoundsGEP(Address Addr, uint64_t Index,
211  CharUnits EltSize,
212  const llvm::Twine &Name = "") {
213  return Address(CreateInBoundsGEP(Addr.getElementType(), Addr.getPointer(),
214  getSize(Index), Name),
215  Addr.getAlignment().alignmentAtOffset(Index * EltSize));
216  }
217 
218  /// Given
219  /// %addr = T* ...
220  /// produce
221  /// %name = getelementptr inbounds %addr, i64 index
222  /// where i64 is actually the target word size.
223  ///
224  /// \param EltSize - the size of the type T in bytes
225  Address CreateConstGEP(Address Addr, uint64_t Index, CharUnits EltSize,
226  const llvm::Twine &Name = "") {
227  return Address(CreateGEP(Addr.getElementType(), Addr.getPointer(),
228  getSize(Index), Name),
229  Addr.getAlignment().alignmentAtOffset(Index * EltSize));
230  }
231 
232  /// Given a pointer to i8, adjust it by a given constant offset.
234  const llvm::Twine &Name = "") {
235  assert(Addr.getElementType() == TypeCache.Int8Ty);
236  return Address(CreateInBoundsGEP(Addr.getPointer(), getSize(Offset), Name),
237  Addr.getAlignment().alignmentAtOffset(Offset));
238  }
240  const llvm::Twine &Name = "") {
241  assert(Addr.getElementType() == TypeCache.Int8Ty);
242  return Address(CreateGEP(Addr.getPointer(), getSize(Offset), Name),
243  Addr.getAlignment().alignmentAtOffset(Offset));
244  }
245 
246  using CGBuilderBaseTy::CreateConstInBoundsGEP2_32;
248  unsigned Idx1, const llvm::DataLayout &DL,
249  const llvm::Twine &Name = "") {
250  auto *GEP = cast<llvm::GetElementPtrInst>(CreateConstInBoundsGEP2_32(
251  Addr.getElementType(), Addr.getPointer(), Idx0, Idx1, Name));
252  llvm::APInt Offset(
253  DL.getIndexSizeInBits(Addr.getType()->getPointerAddressSpace()), 0,
254  /*IsSigned=*/true);
255  if (!GEP->accumulateConstantOffset(DL, Offset))
256  llvm_unreachable("offset of GEP with constants is always computable");
257  return Address(GEP, Addr.getAlignment().alignmentAtOffset(
258  CharUnits::fromQuantity(Offset.getSExtValue())));
259  }
260 
262  const llvm::Twine &Name = "") {
263  assert(Ptr->getType()->getPointerElementType() == TypeCache.Int8Ty);
264  return CreateInBoundsGEP(Ptr, getSize(Offset), Name);
265  }
267  const llvm::Twine &Name = "") {
268  assert(Ptr->getType()->getPointerElementType() == TypeCache.Int8Ty);
269  return CreateGEP(Ptr, getSize(Offset), Name);
270  }
271 
272  using CGBuilderBaseTy::CreateMemCpy;
273  llvm::CallInst *CreateMemCpy(Address Dest, Address Src, llvm::Value *Size,
274  bool IsVolatile = false) {
275  return CreateMemCpy(Dest.getPointer(), Dest.getAlignment().getQuantity(),
276  Src.getPointer(), Src.getAlignment().getQuantity(),
277  Size,IsVolatile);
278  }
279  llvm::CallInst *CreateMemCpy(Address Dest, Address Src, uint64_t Size,
280  bool IsVolatile = false) {
281  return CreateMemCpy(Dest.getPointer(), Dest.getAlignment().getQuantity(),
282  Src.getPointer(), Src.getAlignment().getQuantity(),
283  Size, IsVolatile);
284  }
285 
286  using CGBuilderBaseTy::CreateMemMove;
287  llvm::CallInst *CreateMemMove(Address Dest, Address Src, llvm::Value *Size,
288  bool IsVolatile = false) {
289  return CreateMemMove(Dest.getPointer(), Dest.getAlignment().getQuantity(),
290  Src.getPointer(), Src.getAlignment().getQuantity(),
291  Size, IsVolatile);
292  }
293 
294  using CGBuilderBaseTy::CreateMemSet;
295  llvm::CallInst *CreateMemSet(Address Dest, llvm::Value *Value,
296  llvm::Value *Size, bool IsVolatile = false) {
297  return CreateMemSet(Dest.getPointer(), Value, Size,
298  Dest.getAlignment().getQuantity(), IsVolatile);
299  }
300 };
301 
302 } // end namespace CodeGen
303 } // end namespace clang
304 
305 #endif
CGBuilderTy(const CodeGenTypeCache &TypeCache, llvm::Instruction *I)
Definition: CGBuilder.h:55
llvm::StoreInst * CreateDefaultAlignedStore(llvm::Value *Val, llvm::Value *Addr, bool IsVolatile=false)
Definition: CGBuilder.h:121
Address CreateAddrSpaceCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Definition: CGBuilder.h:148
CGBuilderTy(const CodeGenTypeCache &TypeCache, llvm::BasicBlock *BB)
Definition: CGBuilder.h:57
Address CreateStructGEP(Address Addr, unsigned Index, const llvm::StructLayout *Layout, const llvm::Twine &Name="")
Definition: CGBuilder.h:177
Address CreateConstGEP(Address Addr, uint64_t Index, CharUnits EltSize, const llvm::Twine &Name="")
Given addr = T* ...
Definition: CGBuilder.h:225
llvm::ConstantInt * getSize(CharUnits N)
Definition: CGBuilder.h:60
llvm::ConstantInt * getSize(uint64_t N)
Definition: CGBuilder.h:63
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
CGBuilderTy(const CodeGenTypeCache &TypeCache, llvm::LLVMContext &C, const llvm::ConstantFolder &F, const CGBuilderInserterTy &Inserter)
Definition: CGBuilder.h:51
CGBuilderInserter CGBuilderInserterTy
Definition: CGBuilder.h:39
Address CreateConstInBoundsByteGEP(Address Addr, CharUnits Offset, const llvm::Twine &Name="")
Given a pointer to i8, adjust it by a given constant offset.
Definition: CGBuilder.h:233
llvm::Value * getPointer() const
Definition: Address.h:37
llvm::LoadInst * CreateAlignedLoad(llvm::Value *Addr, CharUnits Align, const char *Name)
Definition: CGBuilder.h:94
unsigned getAddressSpace() const
Return the address space that this address resides in.
Definition: Address.h:56
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
CGBuilderInserter(CodeGenFunction *CGF)
Definition: CGBuilder.h:28
llvm::LoadInst * CreateAlignedLoad(llvm::Type *Ty, llvm::Value *Addr, CharUnits Align, const llvm::Twine &Name="")
Definition: CGBuilder.h:98
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:52
llvm::CallInst * CreateMemMove(Address Dest, Address Src, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:287
Address CreateElementBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Cast the element type of the given address to a different type, preserving information like the align...
Definition: CGBuilder.h:156
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:37
llvm::CallInst * CreateMemCpy(Address Dest, Address Src, uint64_t Size, bool IsVolatile=false)
Definition: CGBuilder.h:279
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:66
llvm::CallInst * CreateMemCpy(Address Dest, Address Src, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:273
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition: CharUnits.h:57
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:178
unsigned Offset
Definition: Format.cpp:1630
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition: Address.h:43
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:62
llvm::CallInst * CreateMemSet(Address Dest, llvm::Value *Value, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:295
llvm::StoreInst * CreateFlagStore(bool Value, llvm::Value *Addr)
Emit a store to an i1 flag variable.
Definition: CGBuilder.h:135
The l-value was considered opaque, so the alignment was determined from a type.
Address CreateBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Definition: CGBuilder.h:141
void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock *BB, llvm::BasicBlock::iterator InsertPt) const
This forwards to CodeGenFunction::InsertHelper.
An aligned address.
Definition: Address.h:24
Address CreateConstInBoundsGEP2_32(Address Addr, unsigned Idx0, unsigned Idx1, const llvm::DataLayout &DL, const llvm::Twine &Name="")
Definition: CGBuilder.h:247
llvm::LoadInst * CreateLoad(Address Addr, const char *Name)
Definition: CGBuilder.h:74
This is an IRBuilder insertion helper that forwards to CodeGenFunction::InsertHelper, which adds necessary metadata to instructions.
Definition: CGBuilder.h:25
CharUnits alignmentAtOffset(CharUnits offset) const
Given that this is a non-zero alignment value, what is the alignment at the given offset...
Definition: CharUnits.h:189
Dataflow Directional Tag Classes.
Address CreateStructGEP(Address Addr, unsigned Index, CharUnits Offset, const llvm::Twine &Name="")
Definition: CGBuilder.h:171
llvm::LoadInst * CreateAlignedLoad(llvm::Value *Addr, CharUnits Align, const llvm::Twine &Name="")
Definition: CGBuilder.h:90
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:69
Address CreateConstInBoundsGEP(Address Addr, uint64_t Index, CharUnits EltSize, const llvm::Twine &Name="")
Given addr = T* ...
Definition: CGBuilder.h:210
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition: CGBuilder.h:107
Address CreateConstArrayGEP(Address Addr, uint64_t Index, CharUnits EltSize, const llvm::Twine &Name="")
Given addr = [n x T]* ...
Definition: CGBuilder.h:194
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:51
llvm::Value * CreateConstInBoundsByteGEP(llvm::Value *Ptr, CharUnits Offset, const llvm::Twine &Name="")
Definition: CGBuilder.h:261
llvm::LoadInst * CreateFlagLoad(llvm::Value *Addr, const llvm::Twine &Name="")
Emit a load from an i1 flag variable.
Definition: CGBuilder.h:128
Address CreateConstByteGEP(Address Addr, CharUnits Offset, const llvm::Twine &Name="")
Definition: CGBuilder.h:239
llvm::LoadInst * CreateLoad(Address Addr, bool IsVolatile, const llvm::Twine &Name="")
Definition: CGBuilder.h:81
llvm::Value * CreateConstByteGEP(llvm::Value *Ptr, CharUnits Offset, const llvm::Twine &Name="")
Definition: CGBuilder.h:266
This structure provides a set of types that are commonly used during IR emission. ...
llvm::StoreInst * CreateAlignedStore(llvm::Value *Val, llvm::Value *Addr, CharUnits Align, bool IsVolatile=false)
Definition: CGBuilder.h:114
Address CreatePointerBitCastOrAddrSpaceCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Definition: CGBuilder.h:163
CGBuilderTy(const CodeGenTypeCache &TypeCache, llvm::LLVMContext &C)
Definition: CGBuilder.h:49
llvm::IRBuilder< llvm::ConstantFolder, CGBuilderInserterTy > CGBuilderBaseTy
Definition: CGBuilder.h:42