1ed511560SReshabh Sharma //===- ModuleUtilsTest.cpp - Unit tests for Module utility ----===//
2ed511560SReshabh Sharma //
3ed511560SReshabh Sharma // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ed511560SReshabh Sharma // See https://llvm.org/LICENSE.txt for license information.
5ed511560SReshabh Sharma // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ed511560SReshabh Sharma //
7ed511560SReshabh Sharma //===----------------------------------------------------------------------===//
8ed511560SReshabh Sharma
9ed511560SReshabh Sharma #include "llvm/Transforms/Utils/ModuleUtils.h"
10ed511560SReshabh Sharma #include "llvm/ADT/StringRef.h"
11ed511560SReshabh Sharma #include "llvm/AsmParser/Parser.h"
12ed511560SReshabh Sharma #include "llvm/IR/LLVMContext.h"
13ed511560SReshabh Sharma #include "llvm/IR/Module.h"
14ed511560SReshabh Sharma #include "llvm/Support/SourceMgr.h"
15ed511560SReshabh Sharma #include "gtest/gtest.h"
16ed511560SReshabh Sharma
17ed511560SReshabh Sharma using namespace llvm;
18ed511560SReshabh Sharma
parseIR(LLVMContext & C,const char * IR)19ed511560SReshabh Sharma static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
20ed511560SReshabh Sharma SMDiagnostic Err;
21ed511560SReshabh Sharma std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
22ed511560SReshabh Sharma if (!Mod)
23ed511560SReshabh Sharma Err.print("ModuleUtilsTest", errs());
24ed511560SReshabh Sharma return Mod;
25ed511560SReshabh Sharma }
26ed511560SReshabh Sharma
getUsedListSize(Module & M,StringRef Name)27ed511560SReshabh Sharma static int getUsedListSize(Module &M, StringRef Name) {
28ed511560SReshabh Sharma auto *UsedList = M.getGlobalVariable(Name);
29ed511560SReshabh Sharma if (!UsedList)
30ed511560SReshabh Sharma return 0;
31*4e601325SArthur Eubanks auto *UsedListBaseArrayType = cast<ArrayType>(UsedList->getValueType());
32ed511560SReshabh Sharma return UsedListBaseArrayType->getNumElements();
33ed511560SReshabh Sharma }
34ed511560SReshabh Sharma
TEST(ModuleUtils,AppendToUsedList1)35ed511560SReshabh Sharma TEST(ModuleUtils, AppendToUsedList1) {
36ed511560SReshabh Sharma LLVMContext C;
37ed511560SReshabh Sharma
38ed511560SReshabh Sharma std::unique_ptr<Module> M = parseIR(
39ed511560SReshabh Sharma C, R"(@x = addrspace(4) global [2 x i32] zeroinitializer, align 4)");
40ed511560SReshabh Sharma SmallVector<GlobalValue *, 2> Globals;
41ed511560SReshabh Sharma for (auto &G : M->globals()) {
42ed511560SReshabh Sharma Globals.push_back(&G);
43ed511560SReshabh Sharma }
44ed511560SReshabh Sharma EXPECT_EQ(0, getUsedListSize(*M, "llvm.compiler.used"));
45ed511560SReshabh Sharma appendToCompilerUsed(*M, Globals);
46ed511560SReshabh Sharma EXPECT_EQ(1, getUsedListSize(*M, "llvm.compiler.used"));
47ed511560SReshabh Sharma
48ed511560SReshabh Sharma EXPECT_EQ(0, getUsedListSize(*M, "llvm.used"));
49ed511560SReshabh Sharma appendToUsed(*M, Globals);
50ed511560SReshabh Sharma EXPECT_EQ(1, getUsedListSize(*M, "llvm.used"));
51ed511560SReshabh Sharma }
52ed511560SReshabh Sharma
TEST(ModuleUtils,AppendToUsedList2)53ed511560SReshabh Sharma TEST(ModuleUtils, AppendToUsedList2) {
54ed511560SReshabh Sharma LLVMContext C;
55ed511560SReshabh Sharma
56ed511560SReshabh Sharma std::unique_ptr<Module> M =
57ed511560SReshabh Sharma parseIR(C, R"(@x = global [2 x i32] zeroinitializer, align 4)");
58ed511560SReshabh Sharma SmallVector<GlobalValue *, 2> Globals;
59ed511560SReshabh Sharma for (auto &G : M->globals()) {
60ed511560SReshabh Sharma Globals.push_back(&G);
61ed511560SReshabh Sharma }
62ed511560SReshabh Sharma EXPECT_EQ(0, getUsedListSize(*M, "llvm.compiler.used"));
63ed511560SReshabh Sharma appendToCompilerUsed(*M, Globals);
64ed511560SReshabh Sharma EXPECT_EQ(1, getUsedListSize(*M, "llvm.compiler.used"));
65ed511560SReshabh Sharma
66ed511560SReshabh Sharma EXPECT_EQ(0, getUsedListSize(*M, "llvm.used"));
67ed511560SReshabh Sharma appendToUsed(*M, Globals);
68ed511560SReshabh Sharma EXPECT_EQ(1, getUsedListSize(*M, "llvm.used"));
69ed511560SReshabh Sharma }
70