1ccae43a2SGeorge Burgess IV //===- MemoryBuiltinsTest.cpp - Tests for utilities in MemoryBuiltins.h ---===//
2ccae43a2SGeorge Burgess IV //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ccae43a2SGeorge Burgess IV //
7ccae43a2SGeorge Burgess IV //===----------------------------------------------------------------------===//
8ccae43a2SGeorge Burgess IV 
9ccae43a2SGeorge Burgess IV #include "llvm/Analysis/MemoryBuiltins.h"
10ccae43a2SGeorge Burgess IV #include "llvm/IR/Attributes.h"
11ccae43a2SGeorge Burgess IV #include "llvm/IR/Constants.h"
12ccae43a2SGeorge Burgess IV #include "llvm/IR/Function.h"
13ccae43a2SGeorge Burgess IV #include "llvm/IR/LLVMContext.h"
14ccae43a2SGeorge Burgess IV #include "llvm/IR/Module.h"
15ccae43a2SGeorge Burgess IV #include "gtest/gtest.h"
16ccae43a2SGeorge Burgess IV 
17ccae43a2SGeorge Burgess IV using namespace llvm;
18ccae43a2SGeorge Burgess IV 
19ccae43a2SGeorge Burgess IV namespace {
20ccae43a2SGeorge Burgess IV // allocsize should not imply that a function is a traditional allocation
21ccae43a2SGeorge Burgess IV // function (e.g. that can be optimized out/...); it just tells us how many
22ccae43a2SGeorge Burgess IV // bytes exist at the pointer handed back by the function.
TEST(AllocSize,AllocationBuiltinsTest)23ccae43a2SGeorge Burgess IV TEST(AllocSize, AllocationBuiltinsTest) {
24ccae43a2SGeorge Burgess IV   LLVMContext Context;
25ccae43a2SGeorge Burgess IV   Module M("", Context);
26ccae43a2SGeorge Burgess IV   IntegerType *ArgTy = Type::getInt32Ty(Context);
27ccae43a2SGeorge Burgess IV 
28ccae43a2SGeorge Burgess IV   Function *AllocSizeFn = Function::Create(
29ccae43a2SGeorge Burgess IV       FunctionType::get(Type::getInt8PtrTy(Context), {ArgTy}, false),
30ccae43a2SGeorge Burgess IV       GlobalValue::ExternalLinkage, "F", &M);
31ccae43a2SGeorge Burgess IV 
32ccae43a2SGeorge Burgess IV   AllocSizeFn->addFnAttr(Attribute::getWithAllocSizeArgs(Context, 1, None));
33ccae43a2SGeorge Burgess IV 
34ccae43a2SGeorge Burgess IV   // 100 is arbitrary.
35ccae43a2SGeorge Burgess IV   std::unique_ptr<CallInst> Caller(
36ccae43a2SGeorge Burgess IV       CallInst::Create(AllocSizeFn, {ConstantInt::get(ArgTy, 100)}));
37ccae43a2SGeorge Burgess IV 
38ccae43a2SGeorge Burgess IV   const TargetLibraryInfo *TLI = nullptr;
39ccae43a2SGeorge Burgess IV   EXPECT_FALSE(isAllocLikeFn(Caller.get(), TLI));
40ccae43a2SGeorge Burgess IV 
41ccae43a2SGeorge Burgess IV   // FIXME: We might be able to treat allocsize functions as general allocation
42*92d55e73SNikita Popov   // functions.
43ccae43a2SGeorge Burgess IV   EXPECT_FALSE(isAllocationFn(Caller.get(), TLI));
44ccae43a2SGeorge Burgess IV }
45ccae43a2SGeorge Burgess IV }
46