xref: /llvm-project/llvm/unittests/Transforms/Utils/ModuleUtilsTest.cpp (revision 88ef76c4efb11389a9c334c19f751c89c29278d1)
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 
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 
27*88ef76c4SVitaly Buka static int getListSize(Module &M, StringRef Name) {
28*88ef76c4SVitaly Buka   auto *List = M.getGlobalVariable(Name);
29*88ef76c4SVitaly Buka   if (!List)
30ed511560SReshabh Sharma     return 0;
31*88ef76c4SVitaly Buka   auto *T = cast<ArrayType>(List->getValueType());
32*88ef76c4SVitaly Buka   return T->getNumElements();
33ed511560SReshabh Sharma }
34ed511560SReshabh Sharma 
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   }
44*88ef76c4SVitaly Buka   EXPECT_EQ(0, getListSize(*M, "llvm.compiler.used"));
45ed511560SReshabh Sharma   appendToCompilerUsed(*M, Globals);
46*88ef76c4SVitaly Buka   EXPECT_EQ(1, getListSize(*M, "llvm.compiler.used"));
47ed511560SReshabh Sharma 
48*88ef76c4SVitaly Buka   EXPECT_EQ(0, getListSize(*M, "llvm.used"));
49ed511560SReshabh Sharma   appendToUsed(*M, Globals);
50*88ef76c4SVitaly Buka   EXPECT_EQ(1, getListSize(*M, "llvm.used"));
51ed511560SReshabh Sharma }
52ed511560SReshabh Sharma 
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   }
62*88ef76c4SVitaly Buka   EXPECT_EQ(0, getListSize(*M, "llvm.compiler.used"));
63ed511560SReshabh Sharma   appendToCompilerUsed(*M, Globals);
64*88ef76c4SVitaly Buka   EXPECT_EQ(1, getListSize(*M, "llvm.compiler.used"));
65ed511560SReshabh Sharma 
66*88ef76c4SVitaly Buka   EXPECT_EQ(0, getListSize(*M, "llvm.used"));
67ed511560SReshabh Sharma   appendToUsed(*M, Globals);
68*88ef76c4SVitaly Buka   EXPECT_EQ(1, getListSize(*M, "llvm.used"));
69ed511560SReshabh Sharma }
70