xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/CGBuilder.h (revision 4f5890a0fb086324a657f3cd7ba1abc57274e0db)
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 "Address.h"
13 #include "CodeGenTypeCache.h"
14 #include "llvm/IR/DataLayout.h"
15 #include "llvm/IR/IRBuilder.h"
16 #include "llvm/IR/Type.h"
17 
18 namespace clang {
19 namespace CodeGen {
20 
21 class CodeGenFunction;
22 
23 /// This is an IRBuilder insertion helper that forwards to
24 /// CodeGenFunction::InsertHelper, which adds necessary metadata to
25 /// instructions.
26 class CGBuilderInserter final : public llvm::IRBuilderDefaultInserter {
27 public:
28   CGBuilderInserter() = default;
29   explicit CGBuilderInserter(CodeGenFunction *CGF) : CGF(CGF) {}
30 
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 override;
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     : 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.getElementType(), Addr.getPointer(),
71                              Addr.getAlignment().getAsAlign(), Name);
72   }
73   llvm::LoadInst *CreateLoad(Address Addr, const char *Name) {
74     // This overload is required to prevent string literals from
75     // ending up in the IsVolatile overload.
76     return CreateAlignedLoad(Addr.getElementType(), Addr.getPointer(),
77                              Addr.getAlignment().getAsAlign(), Name);
78   }
79   llvm::LoadInst *CreateLoad(Address Addr, bool IsVolatile,
80                              const llvm::Twine &Name = "") {
81     return CreateAlignedLoad(Addr.getElementType(), Addr.getPointer(),
82                              Addr.getAlignment().getAsAlign(), IsVolatile,
83                              Name);
84   }
85 
86   using CGBuilderBaseTy::CreateAlignedLoad;
87   llvm::LoadInst *CreateAlignedLoad(llvm::Type *Ty, llvm::Value *Addr,
88                                     CharUnits Align,
89                                     const llvm::Twine &Name = "") {
90     assert(llvm::cast<llvm::PointerType>(Addr->getType())
91                ->isOpaqueOrPointeeTypeMatches(Ty));
92     return CreateAlignedLoad(Ty, Addr, Align.getAsAlign(), Name);
93   }
94 
95   // Note that we intentionally hide the CreateStore APIs that don't
96   // take an alignment.
97   llvm::StoreInst *CreateStore(llvm::Value *Val, Address Addr,
98                                bool IsVolatile = false) {
99     return CreateAlignedStore(Val, Addr.getPointer(),
100                               Addr.getAlignment().getAsAlign(), IsVolatile);
101   }
102 
103   using CGBuilderBaseTy::CreateAlignedStore;
104   llvm::StoreInst *CreateAlignedStore(llvm::Value *Val, llvm::Value *Addr,
105                                       CharUnits Align, bool IsVolatile = false) {
106     return CreateAlignedStore(Val, Addr, Align.getAsAlign(), IsVolatile);
107   }
108 
109   // FIXME: these "default-aligned" APIs should be removed,
110   // but I don't feel like fixing all the builtin code right now.
111   llvm::StoreInst *CreateDefaultAlignedStore(llvm::Value *Val,
112                                              llvm::Value *Addr,
113                                              bool IsVolatile = false) {
114     return CGBuilderBaseTy::CreateStore(Val, Addr, IsVolatile);
115   }
116 
117   /// Emit a load from an i1 flag variable.
118   llvm::LoadInst *CreateFlagLoad(llvm::Value *Addr,
119                                  const llvm::Twine &Name = "") {
120     assert(llvm::cast<llvm::PointerType>(Addr->getType())
121                ->isOpaqueOrPointeeTypeMatches(getInt1Ty()));
122     return CreateAlignedLoad(getInt1Ty(), Addr, CharUnits::One(), Name);
123   }
124 
125   /// Emit a store to an i1 flag variable.
126   llvm::StoreInst *CreateFlagStore(bool Value, llvm::Value *Addr) {
127     assert(llvm::cast<llvm::PointerType>(Addr->getType())
128                ->isOpaqueOrPointeeTypeMatches(getInt1Ty()));
129     return CreateAlignedStore(getInt1(Value), Addr, CharUnits::One());
130   }
131 
132   // Temporarily use old signature; clang will be updated to an Address overload
133   // in a subsequent patch.
134   llvm::AtomicCmpXchgInst *
135   CreateAtomicCmpXchg(llvm::Value *Ptr, llvm::Value *Cmp, llvm::Value *New,
136                       llvm::AtomicOrdering SuccessOrdering,
137                       llvm::AtomicOrdering FailureOrdering,
138                       llvm::SyncScope::ID SSID = llvm::SyncScope::System) {
139     return CGBuilderBaseTy::CreateAtomicCmpXchg(
140         Ptr, Cmp, New, llvm::MaybeAlign(), SuccessOrdering, FailureOrdering,
141         SSID);
142   }
143 
144   // Temporarily use old signature; clang will be updated to an Address overload
145   // in a subsequent patch.
146   llvm::AtomicRMWInst *
147   CreateAtomicRMW(llvm::AtomicRMWInst::BinOp Op, llvm::Value *Ptr,
148                   llvm::Value *Val, llvm::AtomicOrdering Ordering,
149                   llvm::SyncScope::ID SSID = llvm::SyncScope::System) {
150     return CGBuilderBaseTy::CreateAtomicRMW(Op, Ptr, Val, llvm::MaybeAlign(),
151                                             Ordering, SSID);
152   }
153 
154   using CGBuilderBaseTy::CreateBitCast;
155   Address CreateBitCast(Address Addr, llvm::Type *Ty,
156                         const llvm::Twine &Name = "") {
157     return Address(CreateBitCast(Addr.getPointer(), Ty, Name),
158                    Addr.getAlignment());
159   }
160 
161   using CGBuilderBaseTy::CreateAddrSpaceCast;
162   Address CreateAddrSpaceCast(Address Addr, llvm::Type *Ty,
163                               const llvm::Twine &Name = "") {
164     return Address(CreateAddrSpaceCast(Addr.getPointer(), Ty, Name),
165                    Addr.getAlignment());
166   }
167 
168   /// Cast the element type of the given address to a different type,
169   /// preserving information like the alignment and address space.
170   Address CreateElementBitCast(Address Addr, llvm::Type *Ty,
171                                const llvm::Twine &Name = "") {
172     auto *PtrTy = Ty->getPointerTo(Addr.getAddressSpace());
173     return Address(CreateBitCast(Addr.getPointer(), PtrTy, Name),
174                    Ty, Addr.getAlignment());
175   }
176 
177   using CGBuilderBaseTy::CreatePointerBitCastOrAddrSpaceCast;
178   Address CreatePointerBitCastOrAddrSpaceCast(Address Addr, llvm::Type *Ty,
179                                               const llvm::Twine &Name = "") {
180     llvm::Value *Ptr =
181       CreatePointerBitCastOrAddrSpaceCast(Addr.getPointer(), Ty, Name);
182     return Address(Ptr, Addr.getAlignment());
183   }
184 
185   /// Given
186   ///   %addr = {T1, T2...}* ...
187   /// produce
188   ///   %name = getelementptr inbounds %addr, i32 0, i32 index
189   ///
190   /// This API assumes that drilling into a struct like this is always an
191   /// inbounds operation.
192   using CGBuilderBaseTy::CreateStructGEP;
193   Address CreateStructGEP(Address Addr, unsigned Index,
194                           const llvm::Twine &Name = "") {
195     llvm::StructType *ElTy = cast<llvm::StructType>(Addr.getElementType());
196     const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
197     const llvm::StructLayout *Layout = DL.getStructLayout(ElTy);
198     auto Offset = CharUnits::fromQuantity(Layout->getElementOffset(Index));
199 
200     return Address(CreateStructGEP(Addr.getElementType(),
201                                    Addr.getPointer(), Index, Name),
202                    ElTy->getElementType(Index),
203                    Addr.getAlignment().alignmentAtOffset(Offset));
204   }
205 
206   /// Given
207   ///   %addr = [n x T]* ...
208   /// produce
209   ///   %name = getelementptr inbounds %addr, i64 0, i64 index
210   /// where i64 is actually the target word size.
211   ///
212   /// This API assumes that drilling into an array like this is always
213   /// an inbounds operation.
214   Address CreateConstArrayGEP(Address Addr, uint64_t Index,
215                               const llvm::Twine &Name = "") {
216     llvm::ArrayType *ElTy = cast<llvm::ArrayType>(Addr.getElementType());
217     const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
218     CharUnits EltSize =
219         CharUnits::fromQuantity(DL.getTypeAllocSize(ElTy->getElementType()));
220 
221     return Address(
222         CreateInBoundsGEP(Addr.getElementType(), Addr.getPointer(),
223                           {getSize(CharUnits::Zero()), getSize(Index)}, Name),
224         ElTy->getElementType(),
225         Addr.getAlignment().alignmentAtOffset(Index * EltSize));
226   }
227 
228   /// Given
229   ///   %addr = T* ...
230   /// produce
231   ///   %name = getelementptr inbounds %addr, i64 index
232   /// where i64 is actually the target word size.
233   Address CreateConstInBoundsGEP(Address Addr, uint64_t Index,
234                                  const llvm::Twine &Name = "") {
235     llvm::Type *ElTy = Addr.getElementType();
236     const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
237     CharUnits EltSize = CharUnits::fromQuantity(DL.getTypeAllocSize(ElTy));
238 
239     return Address(CreateInBoundsGEP(Addr.getElementType(), Addr.getPointer(),
240                                      getSize(Index), Name),
241                    ElTy,
242                    Addr.getAlignment().alignmentAtOffset(Index * EltSize));
243   }
244 
245   /// Given
246   ///   %addr = T* ...
247   /// produce
248   ///   %name = getelementptr inbounds %addr, i64 index
249   /// where i64 is actually the target word size.
250   Address CreateConstGEP(Address Addr, uint64_t Index,
251                          const llvm::Twine &Name = "") {
252     const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
253     CharUnits EltSize =
254         CharUnits::fromQuantity(DL.getTypeAllocSize(Addr.getElementType()));
255 
256     return Address(CreateGEP(Addr.getElementType(), Addr.getPointer(),
257                              getSize(Index), Name),
258                    Addr.getElementType(),
259                    Addr.getAlignment().alignmentAtOffset(Index * EltSize));
260   }
261 
262   /// Create GEP with single dynamic index. The address alignment is reduced
263   /// according to the element size.
264   using CGBuilderBaseTy::CreateGEP;
265   Address CreateGEP(Address Addr, llvm::Value *Index,
266                     const llvm::Twine &Name = "") {
267     const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
268     CharUnits EltSize =
269         CharUnits::fromQuantity(DL.getTypeAllocSize(Addr.getElementType()));
270 
271     return Address(CreateGEP(Addr.getElementType(), Addr.getPointer(), Index,
272                              Name),
273                    Addr.getElementType(),
274                    Addr.getAlignment().alignmentOfArrayElement(EltSize));
275   }
276 
277   /// Given a pointer to i8, adjust it by a given constant offset.
278   Address CreateConstInBoundsByteGEP(Address Addr, CharUnits Offset,
279                                      const llvm::Twine &Name = "") {
280     assert(Addr.getElementType() == TypeCache.Int8Ty);
281     return Address(CreateInBoundsGEP(Addr.getElementType(), Addr.getPointer(),
282                                      getSize(Offset), Name),
283                    Addr.getElementType(),
284                    Addr.getAlignment().alignmentAtOffset(Offset));
285   }
286   Address CreateConstByteGEP(Address Addr, CharUnits Offset,
287                              const llvm::Twine &Name = "") {
288     assert(Addr.getElementType() == TypeCache.Int8Ty);
289     return Address(CreateGEP(Addr.getElementType(), Addr.getPointer(),
290                              getSize(Offset), Name),
291                    Addr.getElementType(),
292                    Addr.getAlignment().alignmentAtOffset(Offset));
293   }
294 
295   using CGBuilderBaseTy::CreateConstInBoundsGEP2_32;
296   Address CreateConstInBoundsGEP2_32(Address Addr, unsigned Idx0, unsigned Idx1,
297                                      const llvm::Twine &Name = "") {
298     const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
299 
300     auto *GEP = cast<llvm::GetElementPtrInst>(CreateConstInBoundsGEP2_32(
301         Addr.getElementType(), Addr.getPointer(), Idx0, Idx1, Name));
302     llvm::APInt Offset(
303         DL.getIndexSizeInBits(Addr.getType()->getPointerAddressSpace()), 0,
304         /*isSigned=*/true);
305     if (!GEP->accumulateConstantOffset(DL, Offset))
306       llvm_unreachable("offset of GEP with constants is always computable");
307     return Address(GEP, GEP->getResultElementType(),
308                    Addr.getAlignment().alignmentAtOffset(
309                        CharUnits::fromQuantity(Offset.getSExtValue())));
310   }
311 
312   using CGBuilderBaseTy::CreateMemCpy;
313   llvm::CallInst *CreateMemCpy(Address Dest, Address Src, llvm::Value *Size,
314                                bool IsVolatile = false) {
315     return CreateMemCpy(Dest.getPointer(), Dest.getAlignment().getAsAlign(),
316                         Src.getPointer(), Src.getAlignment().getAsAlign(), Size,
317                         IsVolatile);
318   }
319   llvm::CallInst *CreateMemCpy(Address Dest, Address Src, uint64_t Size,
320                                bool IsVolatile = false) {
321     return CreateMemCpy(Dest.getPointer(), Dest.getAlignment().getAsAlign(),
322                         Src.getPointer(), Src.getAlignment().getAsAlign(), Size,
323                         IsVolatile);
324   }
325 
326   using CGBuilderBaseTy::CreateMemCpyInline;
327   llvm::CallInst *CreateMemCpyInline(Address Dest, Address Src, uint64_t Size) {
328     return CreateMemCpyInline(
329         Dest.getPointer(), Dest.getAlignment().getAsAlign(), Src.getPointer(),
330         Src.getAlignment().getAsAlign(), getInt64(Size));
331   }
332 
333   using CGBuilderBaseTy::CreateMemMove;
334   llvm::CallInst *CreateMemMove(Address Dest, Address Src, llvm::Value *Size,
335                                 bool IsVolatile = false) {
336     return CreateMemMove(Dest.getPointer(), Dest.getAlignment().getAsAlign(),
337                          Src.getPointer(), Src.getAlignment().getAsAlign(),
338                          Size, IsVolatile);
339   }
340 
341   using CGBuilderBaseTy::CreateMemSet;
342   llvm::CallInst *CreateMemSet(Address Dest, llvm::Value *Value,
343                                llvm::Value *Size, bool IsVolatile = false) {
344     return CreateMemSet(Dest.getPointer(), Value, Size,
345                         Dest.getAlignment().getAsAlign(), IsVolatile);
346   }
347 
348   using CGBuilderBaseTy::CreatePreserveStructAccessIndex;
349   Address CreatePreserveStructAccessIndex(Address Addr,
350                                           unsigned Index,
351                                           unsigned FieldIndex,
352                                           llvm::MDNode *DbgInfo) {
353     llvm::StructType *ElTy = cast<llvm::StructType>(Addr.getElementType());
354     const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
355     const llvm::StructLayout *Layout = DL.getStructLayout(ElTy);
356     auto Offset = CharUnits::fromQuantity(Layout->getElementOffset(Index));
357 
358     return Address(CreatePreserveStructAccessIndex(ElTy, Addr.getPointer(),
359                                                    Index, FieldIndex, DbgInfo),
360                    ElTy->getElementType(Index),
361                    Addr.getAlignment().alignmentAtOffset(Offset));
362   }
363 
364   using CGBuilderBaseTy::CreateLaunderInvariantGroup;
365   Address CreateLaunderInvariantGroup(Address Addr) {
366     return Addr.withPointer(CreateLaunderInvariantGroup(Addr.getPointer()));
367   }
368 };
369 
370 }  // end namespace CodeGen
371 }  // end namespace clang
372 
373 #endif
374