blob: ca2a1a609438a0d92914958194e8bddeda2b80a9 [file] [log] [blame]
Inna Palantff3f07a2019-07-11 16:15:26 -07001//===- MemoryBuiltinsTest.cpp - Tests for utilities in MemoryBuiltins.h ---===//
2//
Chih-Hung Hsieh08600532019-12-19 15:55:38 -08003// 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
Inna Palantff3f07a2019-07-11 16:15:26 -07006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Analysis/MemoryBuiltins.h"
10#include "llvm/IR/Attributes.h"
11#include "llvm/IR/Constants.h"
12#include "llvm/IR/Function.h"
13#include "llvm/IR/LLVMContext.h"
14#include "llvm/IR/Module.h"
15#include "gtest/gtest.h"
16
17using namespace llvm;
18
19namespace {
20// allocsize should not imply that a function is a traditional allocation
21// function (e.g. that can be optimized out/...); it just tells us how many
22// bytes exist at the pointer handed back by the function.
23TEST(AllocSize, AllocationBuiltinsTest) {
24 LLVMContext Context;
25 Module M("", Context);
26 IntegerType *ArgTy = Type::getInt32Ty(Context);
27
28 Function *AllocSizeFn = Function::Create(
Chris Wailesc25c0452024-05-02 11:11:34 -070029 FunctionType::get(PointerType::getUnqual(Context), {ArgTy}, false),
Inna Palantff3f07a2019-07-11 16:15:26 -070030 GlobalValue::ExternalLinkage, "F", &M);
31
Charisee635618d2023-06-01 20:46:00 +000032 AllocSizeFn->addFnAttr(
33 Attribute::getWithAllocSizeArgs(Context, 1, std::nullopt));
Inna Palantff3f07a2019-07-11 16:15:26 -070034
35 // 100 is arbitrary.
36 std::unique_ptr<CallInst> Caller(
37 CallInst::Create(AllocSizeFn, {ConstantInt::get(ArgTy, 100)}));
38
39 const TargetLibraryInfo *TLI = nullptr;
Inna Palantff3f07a2019-07-11 16:15:26 -070040 EXPECT_FALSE(isAllocLikeFn(Caller.get(), TLI));
41
42 // FIXME: We might be able to treat allocsize functions as general allocation
Chris Wailes2805eef2022-04-07 11:22:56 -070043 // functions.
Inna Palantff3f07a2019-07-11 16:15:26 -070044 EXPECT_FALSE(isAllocationFn(Caller.get(), TLI));
45}
46}