aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Transforms/Utils/ModuleUtilsTest.cpp
blob: d4094c530706005f657b17701d5c06f6cbff3452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//===- ModuleUtilsTest.cpp - Unit tests for Module utility ----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Utils/ModuleUtils.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"

using namespace llvm;

static std::unique_ptr<Module> parseIR(LLVMContext &C, StringRef IR) {
  SMDiagnostic Err;
  std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
  if (!Mod)
    Err.print("ModuleUtilsTest", errs());
  return Mod;
}

static int getListSize(Module &M, StringRef Name) {
  auto *List = M.getGlobalVariable(Name);
  if (!List)
    return 0;
  auto *T = cast<ArrayType>(List->getValueType());
  return T->getNumElements();
}

TEST(ModuleUtils, AppendToUsedList1) {
  LLVMContext C;

  std::unique_ptr<Module> M = parseIR(
      C, R"(@x = addrspace(4) global [2 x i32] zeroinitializer, align 4)");
  SmallVector<GlobalValue *, 2> Globals;
  for (auto &G : M->globals()) {
    Globals.push_back(&G);
  }
  EXPECT_EQ(0, getListSize(*M, "llvm.compiler.used"));
  appendToCompilerUsed(*M, Globals);
  EXPECT_EQ(1, getListSize(*M, "llvm.compiler.used"));

  EXPECT_EQ(0, getListSize(*M, "llvm.used"));
  appendToUsed(*M, Globals);
  EXPECT_EQ(1, getListSize(*M, "llvm.used"));
}

TEST(ModuleUtils, AppendToUsedList2) {
  LLVMContext C;

  std::unique_ptr<Module> M =
      parseIR(C, R"(@x = global [2 x i32] zeroinitializer, align 4)");
  SmallVector<GlobalValue *, 2> Globals;
  for (auto &G : M->globals()) {
    Globals.push_back(&G);
  }
  EXPECT_EQ(0, getListSize(*M, "llvm.compiler.used"));
  appendToCompilerUsed(*M, Globals);
  EXPECT_EQ(1, getListSize(*M, "llvm.compiler.used"));

  EXPECT_EQ(0, getListSize(*M, "llvm.used"));
  appendToUsed(*M, Globals);
  EXPECT_EQ(1, getListSize(*M, "llvm.used"));
}

using AppendFnType = decltype(&appendToGlobalCtors);
using TransformFnType = decltype(&transformGlobalCtors);
using ParamType = std::tuple<StringRef, AppendFnType, TransformFnType>;
class ModuleUtilsTest : public testing::TestWithParam<ParamType> {
public:
  StringRef arrayName() const { return std::get<0>(GetParam()); }
  AppendFnType appendFn() const { return std::get<AppendFnType>(GetParam()); }
  TransformFnType transformFn() const {
    return std::get<TransformFnType>(GetParam());
  }
};

INSTANTIATE_TEST_SUITE_P(
    ModuleUtilsTestCtors, ModuleUtilsTest,
    ::testing::Values(ParamType{"llvm.global_ctors", &appendToGlobalCtors,
                                &transformGlobalCtors},
                      ParamType{"llvm.global_dtors", &appendToGlobalDtors,
                                &transformGlobalDtors}));

TEST_P(ModuleUtilsTest, AppendToMissingArray) {
  LLVMContext C;

  std::unique_ptr<Module> M = parseIR(C, "");

  EXPECT_EQ(0, getListSize(*M, arrayName()));
  Function *F = cast<Function>(
      M->getOrInsertFunction("ctor", Type::getVoidTy(C)).getCallee());
  appendFn()(*M, F, 11, F);
  ASSERT_EQ(1, getListSize(*M, arrayName()));

  ConstantArray *CA = dyn_cast<ConstantArray>(
      M->getGlobalVariable(arrayName())->getInitializer());
  ASSERT_NE(nullptr, CA);
  ConstantStruct *CS = dyn_cast<ConstantStruct>(CA->getOperand(0));
  ASSERT_NE(nullptr, CS);
  ConstantInt *Pri = dyn_cast<ConstantInt>(CS->getOperand(0));
  ASSERT_NE(nullptr, Pri);
  EXPECT_EQ(11u, Pri->getLimitedValue());
  EXPECT_EQ(F, dyn_cast<Function>(CS->getOperand(1)));
  EXPECT_EQ(F, CS->getOperand(2));
}

TEST_P(ModuleUtilsTest, AppendToArray) {
  LLVMContext C;

  std::unique_ptr<Module> M =
      parseIR(C, (R"(@)" + arrayName() +
                  R"( = appending global [2 x { i32, ptr, ptr }] [
            { i32, ptr, ptr } { i32 65535, ptr  null, ptr null },
            { i32, ptr, ptr } { i32 0, ptr  null, ptr null }]
      )")
                     .str());

  EXPECT_EQ(2, getListSize(*M, arrayName()));
  appendFn()(
      *M,
      cast<Function>(
          M->getOrInsertFunction("ctor", Type::getVoidTy(C)).getCallee()),
      11, nullptr);
  EXPECT_EQ(3, getListSize(*M, arrayName()));
}

TEST_P(ModuleUtilsTest, UpdateArray) {
  LLVMContext C;

  std::unique_ptr<Module> M =
      parseIR(C, (R"(@)" + arrayName() +
                  R"( = appending global [2 x { i32, ptr, ptr }] [
            { i32, ptr, ptr } { i32 65535, ptr  null, ptr null },
            { i32, ptr, ptr } { i32 0, ptr  null, ptr null }]
      )")
                     .str());

  EXPECT_EQ(2, getListSize(*M, arrayName()));
  transformFn()(*M, [](Constant *C) -> Constant * {
    ConstantStruct *CS = dyn_cast<ConstantStruct>(C);
    if (!CS)
      return nullptr;
    StructType *EltTy = cast<StructType>(C->getType());
    Constant *CSVals[3] = {
        ConstantInt::getSigned(CS->getOperand(0)->getType(), 12),
        CS->getOperand(1),
        CS->getOperand(2),
    };
    return ConstantStruct::get(EltTy,
                               ArrayRef(CSVals, EltTy->getNumElements()));
  });
  EXPECT_EQ(1, getListSize(*M, arrayName()));
  ConstantArray *CA = dyn_cast<ConstantArray>(
      M->getGlobalVariable(arrayName())->getInitializer());
  ASSERT_NE(nullptr, CA);
  ConstantStruct *CS = dyn_cast<ConstantStruct>(CA->getOperand(0));
  ASSERT_NE(nullptr, CS);
  ConstantInt *Pri = dyn_cast<ConstantInt>(CS->getOperand(0));
  ASSERT_NE(nullptr, Pri);
  EXPECT_EQ(12u, Pri->getLimitedValue());
}