1 //===- MemoryBuiltinsTest.cpp - Tests for utilities in MemoryBuiltins.h ---===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Analysis/MemoryBuiltins.h"
11 #include "llvm/IR/Attributes.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "gtest/gtest.h"
17
18 using namespace llvm;
19
20 namespace {
21 // allocsize should not imply that a function is a traditional allocation
22 // function (e.g. that can be optimized out/...); it just tells us how many
23 // bytes exist at the pointer handed back by the function.
TEST(AllocSize,AllocationBuiltinsTest)24 TEST(AllocSize, AllocationBuiltinsTest) {
25 LLVMContext Context;
26 Module M("", Context);
27 IntegerType *ArgTy = Type::getInt32Ty(Context);
28
29 Function *AllocSizeFn = Function::Create(
30 FunctionType::get(Type::getInt8PtrTy(Context), {ArgTy}, false),
31 GlobalValue::ExternalLinkage, "F", &M);
32
33 AllocSizeFn->addFnAttr(Attribute::getWithAllocSizeArgs(Context, 1, None));
34
35 // 100 is arbitrary.
36 std::unique_ptr<CallInst> Caller(
37 CallInst::Create(AllocSizeFn, {ConstantInt::get(ArgTy, 100)}));
38
39 const TargetLibraryInfo *TLI = nullptr;
40 EXPECT_FALSE(isNoAliasFn(Caller.get(), TLI));
41 EXPECT_FALSE(isMallocLikeFn(Caller.get(), TLI));
42 EXPECT_FALSE(isCallocLikeFn(Caller.get(), TLI));
43 EXPECT_FALSE(isAllocLikeFn(Caller.get(), TLI));
44
45 // FIXME: We might be able to treat allocsize functions as general allocation
46 // functions. For the moment, being conservative seems better (and we'd have
47 // to plumb stuff around `isNoAliasFn`).
48 EXPECT_FALSE(isAllocationFn(Caller.get(), TLI));
49 }
50 }
51