[Alignment][NFC] Use Align with TargetLowering::setMinFunctionAlignment
[llvm-core.git] / include / llvm / Analysis / MemoryBuiltins.h
blob6dc90e505b24cd4c89d2209a0c58e5840c76afd7
1 //==- llvm/Analysis/MemoryBuiltins.h - Calls to memory builtins --*- 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 // This family of functions identifies calls to builtin functions that allocate
10 // or free memory.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_MEMORYBUILTINS_H
15 #define LLVM_ANALYSIS_MEMORYBUILTINS_H
17 #include "llvm/ADT/APInt.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/Analysis/TargetFolder.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
22 #include "llvm/IR/CallSite.h"
23 #include "llvm/IR/IRBuilder.h"
24 #include "llvm/IR/InstVisitor.h"
25 #include "llvm/IR/ValueHandle.h"
26 #include <cstdint>
27 #include <utility>
29 namespace llvm {
31 class AllocaInst;
32 class Argument;
33 class CallInst;
34 class ConstantInt;
35 class ConstantPointerNull;
36 class DataLayout;
37 class ExtractElementInst;
38 class ExtractValueInst;
39 class GEPOperator;
40 class GlobalAlias;
41 class GlobalVariable;
42 class Instruction;
43 class IntegerType;
44 class IntrinsicInst;
45 class IntToPtrInst;
46 class LLVMContext;
47 class LoadInst;
48 class PHINode;
49 class PointerType;
50 class SelectInst;
51 class TargetLibraryInfo;
52 class Type;
53 class UndefValue;
54 class Value;
56 /// Tests if a value is a call or invoke to a library function that
57 /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
58 /// like).
59 bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
60 bool LookThroughBitCast = false);
62 /// Tests if a value is a call or invoke to a function that returns a
63 /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
64 bool isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
65 bool LookThroughBitCast = false);
67 /// Tests if a value is a call or invoke to a library function that
68 /// allocates uninitialized memory (such as malloc).
69 bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
70 bool LookThroughBitCast = false);
72 /// Tests if a value is a call or invoke to a library function that
73 /// allocates zero-filled memory (such as calloc).
74 bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
75 bool LookThroughBitCast = false);
77 /// Tests if a value is a call or invoke to a library function that
78 /// allocates memory similar to malloc or calloc.
79 bool isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
80 bool LookThroughBitCast = false);
82 /// Tests if a value is a call or invoke to a library function that
83 /// allocates memory (either malloc, calloc, or strdup like).
84 bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
85 bool LookThroughBitCast = false);
87 /// Tests if a value is a call or invoke to a library function that
88 /// reallocates memory (e.g., realloc).
89 bool isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
90 bool LookThroughBitCast = false);
92 /// Tests if a function is a call or invoke to a library function that
93 /// reallocates memory (e.g., realloc).
94 bool isReallocLikeFn(const Function *F, const TargetLibraryInfo *TLI);
96 /// Tests if a value is a call or invoke to a library function that
97 /// allocates memory and throws if an allocation failed (e.g., new).
98 bool isOpNewLikeFn(const Value *V, const TargetLibraryInfo *TLI,
99 bool LookThroughBitCast = false);
101 //===----------------------------------------------------------------------===//
102 // malloc Call Utility Functions.
105 /// extractMallocCall - Returns the corresponding CallInst if the instruction
106 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
107 /// ignore InvokeInst here.
108 const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI);
109 inline CallInst *extractMallocCall(Value *I, const TargetLibraryInfo *TLI) {
110 return const_cast<CallInst*>(extractMallocCall((const Value*)I, TLI));
113 /// getMallocType - Returns the PointerType resulting from the malloc call.
114 /// The PointerType depends on the number of bitcast uses of the malloc call:
115 /// 0: PointerType is the malloc calls' return type.
116 /// 1: PointerType is the bitcast's result type.
117 /// >1: Unique PointerType cannot be determined, return NULL.
118 PointerType *getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI);
120 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
121 /// The Type depends on the number of bitcast uses of the malloc call:
122 /// 0: PointerType is the malloc calls' return type.
123 /// 1: PointerType is the bitcast's result type.
124 /// >1: Unique PointerType cannot be determined, return NULL.
125 Type *getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI);
127 /// getMallocArraySize - Returns the array size of a malloc call. If the
128 /// argument passed to malloc is a multiple of the size of the malloced type,
129 /// then return that multiple. For non-array mallocs, the multiple is
130 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
131 /// determined.
132 Value *getMallocArraySize(CallInst *CI, const DataLayout &DL,
133 const TargetLibraryInfo *TLI,
134 bool LookThroughSExt = false);
136 //===----------------------------------------------------------------------===//
137 // calloc Call Utility Functions.
140 /// extractCallocCall - Returns the corresponding CallInst if the instruction
141 /// is a calloc call.
142 const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI);
143 inline CallInst *extractCallocCall(Value *I, const TargetLibraryInfo *TLI) {
144 return const_cast<CallInst*>(extractCallocCall((const Value*)I, TLI));
148 //===----------------------------------------------------------------------===//
149 // free Call Utility Functions.
152 /// isLibFreeFunction - Returns true if the function is a builtin free()
153 bool isLibFreeFunction(const Function *F, const LibFunc TLIFn);
155 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
156 const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI);
158 inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) {
159 return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI));
162 //===----------------------------------------------------------------------===//
163 // Utility functions to compute size of objects.
166 /// Various options to control the behavior of getObjectSize.
167 struct ObjectSizeOpts {
168 /// Controls how we handle conditional statements with unknown conditions.
169 enum class Mode : uint8_t {
170 /// Fail to evaluate an unknown condition.
171 Exact,
172 /// Evaluate all branches of an unknown condition. If all evaluations
173 /// succeed, pick the minimum size.
174 Min,
175 /// Same as Min, except we pick the maximum size of all of the branches.
179 /// How we want to evaluate this object's size.
180 Mode EvalMode = Mode::Exact;
181 /// Whether to round the result up to the alignment of allocas, byval
182 /// arguments, and global variables.
183 bool RoundToAlign = false;
184 /// If this is true, null pointers in address space 0 will be treated as
185 /// though they can't be evaluated. Otherwise, null is always considered to
186 /// point to a 0 byte region of memory.
187 bool NullIsUnknownSize = false;
190 /// Compute the size of the object pointed by Ptr. Returns true and the
191 /// object size in Size if successful, and false otherwise. In this context, by
192 /// object we mean the region of memory starting at Ptr to the end of the
193 /// underlying object pointed to by Ptr.
194 bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
195 const TargetLibraryInfo *TLI, ObjectSizeOpts Opts = {});
197 /// Try to turn a call to \@llvm.objectsize into an integer value of the given
198 /// Type. Returns null on failure. If MustSucceed is true, this function will
199 /// not return null, and may return conservative values governed by the second
200 /// argument of the call to objectsize.
201 Value *lowerObjectSizeCall(IntrinsicInst *ObjectSize, const DataLayout &DL,
202 const TargetLibraryInfo *TLI, bool MustSucceed);
206 using SizeOffsetType = std::pair<APInt, APInt>;
208 /// Evaluate the size and offset of an object pointed to by a Value*
209 /// statically. Fails if size or offset are not known at compile time.
210 class ObjectSizeOffsetVisitor
211 : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
212 const DataLayout &DL;
213 const TargetLibraryInfo *TLI;
214 ObjectSizeOpts Options;
215 unsigned IntTyBits;
216 APInt Zero;
217 SmallPtrSet<Instruction *, 8> SeenInsts;
219 APInt align(APInt Size, uint64_t Align);
221 SizeOffsetType unknown() {
222 return std::make_pair(APInt(), APInt());
225 public:
226 ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI,
227 LLVMContext &Context, ObjectSizeOpts Options = {});
229 SizeOffsetType compute(Value *V);
231 static bool knownSize(const SizeOffsetType &SizeOffset) {
232 return SizeOffset.first.getBitWidth() > 1;
235 static bool knownOffset(const SizeOffsetType &SizeOffset) {
236 return SizeOffset.second.getBitWidth() > 1;
239 static bool bothKnown(const SizeOffsetType &SizeOffset) {
240 return knownSize(SizeOffset) && knownOffset(SizeOffset);
243 // These are "private", except they can't actually be made private. Only
244 // compute() should be used by external users.
245 SizeOffsetType visitAllocaInst(AllocaInst &I);
246 SizeOffsetType visitArgument(Argument &A);
247 SizeOffsetType visitCallSite(CallSite CS);
248 SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
249 SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
250 SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
251 SizeOffsetType visitGEPOperator(GEPOperator &GEP);
252 SizeOffsetType visitGlobalAlias(GlobalAlias &GA);
253 SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
254 SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
255 SizeOffsetType visitLoadInst(LoadInst &I);
256 SizeOffsetType visitPHINode(PHINode&);
257 SizeOffsetType visitSelectInst(SelectInst &I);
258 SizeOffsetType visitUndefValue(UndefValue&);
259 SizeOffsetType visitInstruction(Instruction &I);
261 private:
262 bool CheckedZextOrTrunc(APInt &I);
265 using SizeOffsetEvalType = std::pair<Value *, Value *>;
267 /// Evaluate the size and offset of an object pointed to by a Value*.
268 /// May create code to compute the result at run-time.
269 class ObjectSizeOffsetEvaluator
270 : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
271 using BuilderTy = IRBuilder<TargetFolder, IRBuilderCallbackInserter>;
272 using WeakEvalType = std::pair<WeakTrackingVH, WeakTrackingVH>;
273 using CacheMapTy = DenseMap<const Value *, WeakEvalType>;
274 using PtrSetTy = SmallPtrSet<const Value *, 8>;
276 const DataLayout &DL;
277 const TargetLibraryInfo *TLI;
278 LLVMContext &Context;
279 BuilderTy Builder;
280 IntegerType *IntTy;
281 Value *Zero;
282 CacheMapTy CacheMap;
283 PtrSetTy SeenVals;
284 ObjectSizeOpts EvalOpts;
285 SmallPtrSet<Instruction *, 8> InsertedInstructions;
287 SizeOffsetEvalType compute_(Value *V);
289 public:
290 static SizeOffsetEvalType unknown() {
291 return std::make_pair(nullptr, nullptr);
294 ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI,
295 LLVMContext &Context, ObjectSizeOpts EvalOpts = {});
297 SizeOffsetEvalType compute(Value *V);
299 bool knownSize(SizeOffsetEvalType SizeOffset) {
300 return SizeOffset.first;
303 bool knownOffset(SizeOffsetEvalType SizeOffset) {
304 return SizeOffset.second;
307 bool anyKnown(SizeOffsetEvalType SizeOffset) {
308 return knownSize(SizeOffset) || knownOffset(SizeOffset);
311 bool bothKnown(SizeOffsetEvalType SizeOffset) {
312 return knownSize(SizeOffset) && knownOffset(SizeOffset);
315 // The individual instruction visitors should be treated as private.
316 SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
317 SizeOffsetEvalType visitCallSite(CallSite CS);
318 SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I);
319 SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
320 SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
321 SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
322 SizeOffsetEvalType visitLoadInst(LoadInst &I);
323 SizeOffsetEvalType visitPHINode(PHINode &PHI);
324 SizeOffsetEvalType visitSelectInst(SelectInst &I);
325 SizeOffsetEvalType visitInstruction(Instruction &I);
328 } // end namespace llvm
330 #endif // LLVM_ANALYSIS_MEMORYBUILTINS_H