Debian Codegraph @

llvm/unittests/CodeGen/RegAllocScoreTest.cpp

raw
1 //===- MachineInstrTest.cpp -----------------------------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "../lib/CodeGen/RegAllocScore.h"
10 #include "llvm/CodeGen/CodeGenTargetMachineImpl.h"
11 #include "llvm/CodeGen/MachineBasicBlock.h"
12 #include "llvm/CodeGen/MachineFunction.h"
13 #include "llvm/CodeGen/MachineInstr.h"
14 #include "llvm/CodeGen/MachineMemOperand.h"
15 #include "llvm/CodeGen/MachineModuleInfo.h"
16 #include "llvm/CodeGen/TargetFrameLowering.h"
17 #include "llvm/CodeGen/TargetInstrInfo.h"
18 #include "llvm/CodeGen/TargetLowering.h"
19 #include "llvm/CodeGen/TargetSubtargetInfo.h"
20 #include "llvm/IR/DebugInfoMetadata.h"
21 #include "llvm/IR/IRBuilder.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/ModuleSlotTracker.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/TargetRegistry.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/TargetSelect.h"
29 #include "llvm/Target/TargetOptions.h"
30 #include "llvm/TargetParser/Triple.h"
31 #include "gtest/gtest.h"
32
33 using namespace llvm;
34 LLVM_ABI extern cl::opt<double> CopyWeight;
35 LLVM_ABI extern cl::opt<double> LoadWeight;
36 LLVM_ABI extern cl::opt<double> StoreWeight;
37 LLVM_ABI extern cl::opt<double> CheapRematWeight;
38 LLVM_ABI extern cl::opt<double> ExpensiveRematWeight;
39
40 namespace {
41 // Include helper functions to ease the manipulation of MachineFunctions.
42 #include "MFCommon.inc"
43
44 // MachineFunction::CreateMachineInstr doesn't copy the MCInstrDesc, it
45 // takes its address. So we want a bunch of pre-allocated mock MCInstrDescs.
46 #define MOCK_INSTR(MACRO) \
47 MACRO(Copy, TargetOpcode::COPY, 0) \
48 MACRO(Load, 0, 1ULL << MCID::MayLoad) \
49 MACRO(Store, 0, 1ULL << MCID::MayStore) \
50 MACRO(LoadStore, 0, (1ULL << MCID::MayLoad) | (1ULL << MCID::MayStore)) \
51 MACRO(CheapRemat, 0, 1ULL << MCID::CheapAsAMove) \
52 MACRO(ExpensiveRemat, 0, 0) \
53 MACRO(Dbg, TargetOpcode::DBG_LABEL, \
54 (1ULL << MCID::MayLoad) | (1ULL << MCID::MayStore)) \
55 MACRO(InlAsm, TargetOpcode::INLINEASM, \
56 (1ULL << MCID::MayLoad) | (1ULL << MCID::MayStore)) \
57 MACRO(Kill, TargetOpcode::KILL, \
58 (1ULL << MCID::MayLoad) | (1ULL << MCID::MayStore))
59
60 enum MockInstrId {
61 #define MOCK_INSTR_ID(ID, IGNORE, IGNORE2) ID,
62 MOCK_INSTR(MOCK_INSTR_ID)
63 #undef MOCK_INSTR_ID
64 TotalMockInstrs
65 };
66
67 const std::array<MCInstrDesc, MockInstrId::TotalMockInstrs> MockInstrDescs{{
68 #define MOCK_SPEC(IGNORE, OPCODE, FLAGS) \
69 {OPCODE, 0, 0, 0, 0, 0, 0, 0, 0, FLAGS, 0},
70 MOCK_INSTR(MOCK_SPEC)
71 #undef MOCK_SPEC
72 }};
73
74 MachineInstr *createMockCopy(MachineFunction &MF) {
75 return MF.CreateMachineInstr(MockInstrDescs[MockInstrId::Copy], DebugLoc());
76 }
77
78 MachineInstr *createMockLoad(MachineFunction &MF) {
79 return MF.CreateMachineInstr(MockInstrDescs[MockInstrId::Load], DebugLoc());
80 }
81
82 MachineInstr *createMockStore(MachineFunction &MF) {
83 return MF.CreateMachineInstr(MockInstrDescs[MockInstrId::Store], DebugLoc());
84 }
85
86 MachineInstr *createMockLoadStore(MachineFunction &MF) {
87 return MF.CreateMachineInstr(MockInstrDescs[MockInstrId::LoadStore],
88 DebugLoc());
89 }
90
91 MachineInstr *createMockCheapRemat(MachineFunction &MF) {
92 return MF.CreateMachineInstr(MockInstrDescs[MockInstrId::CheapRemat],
93 DebugLoc());
94 }
95
96 MachineInstr *createMockExpensiveRemat(MachineFunction &MF) {
97 return MF.CreateMachineInstr(MockInstrDescs[MockInstrId::ExpensiveRemat],
98 DebugLoc());
99 }
100
101 MachineInstr *createMockDebug(MachineFunction &MF) {
102 return MF.CreateMachineInstr(MockInstrDescs[MockInstrId::Dbg], DebugLoc());
103 }
104
105 MachineInstr *createMockKill(MachineFunction &MF) {
106 return MF.CreateMachineInstr(MockInstrDescs[MockInstrId::Kill], DebugLoc());
107 }
108
109 MachineInstr *createMockInlineAsm(MachineFunction &MF) {
110 return MF.CreateMachineInstr(MockInstrDescs[MockInstrId::InlAsm], DebugLoc());
111 }
112
113 TEST(RegAllocScoreTest, SkipDebugKillInlineAsm) {
114 LLVMContext Ctx;
115 Module Mod("Module", Ctx);
116 auto MF = createMachineFunction(Ctx, Mod);
117
118 auto *MBB = MF->CreateMachineBasicBlock();
119 MF->insert(MF->end(), MBB);
120 auto MBBFreqMock = [&](const MachineBasicBlock &_MBB) -> double {
121 assert(&_MBB == MBB);
122 return 0.5;
123 };
124 auto Next = MBB->end();
125 Next = MBB->insertAfter(Next, createMockInlineAsm(*MF));
126 Next = MBB->insertAfter(Next, createMockDebug(*MF));
127 Next = MBB->insertAfter(Next, createMockKill(*MF));
128 const auto Score = llvm::calculateRegAllocScore(
129 *MF, MBBFreqMock, [](const MachineInstr &) { return false; });
130 ASSERT_EQ(MF->size(), 1U);
131 ASSERT_EQ(Score, RegAllocScore());
132 }
133
134 TEST(RegAllocScoreTest, Counts) {
135 LLVMContext Ctx;
136 Module Mod("Module", Ctx);
137 auto MF = createMachineFunction(Ctx, Mod);
138
139 auto *MBB1 = MF->CreateMachineBasicBlock();
140 auto *MBB2 = MF->CreateMachineBasicBlock();
141 MF->insert(MF->end(), MBB1);
142 MF->insert(MF->end(), MBB2);
143 const double Freq1 = 0.5;
144 const double Freq2 = 10.0;
145 auto MBBFreqMock = [&](const MachineBasicBlock &MBB) -> double {
146 if (&MBB == MBB1)
147 return Freq1;
148 if (&MBB == MBB2)
149 return Freq2;
150 llvm_unreachable("We only created 2 basic blocks");
151 };
152 auto Next = MBB1->end();
153 Next = MBB1->insertAfter(Next, createMockCopy(*MF));
154 Next = MBB1->insertAfter(Next, createMockLoad(*MF));
155 Next = MBB1->insertAfter(Next, createMockLoad(*MF));
156 Next = MBB1->insertAfter(Next, createMockStore(*MF));
157 auto *CheapRemat = createMockCheapRemat(*MF);
158 MBB1->insertAfter(Next, CheapRemat);
159 Next = MBB2->end();
160 Next = MBB2->insertAfter(Next, createMockLoad(*MF));
161 Next = MBB2->insertAfter(Next, createMockStore(*MF));
162 Next = MBB2->insertAfter(Next, createMockLoadStore(*MF));
163 auto *ExpensiveRemat = createMockExpensiveRemat(*MF);
164 MBB2->insertAfter(Next, ExpensiveRemat);
165 auto IsRemat = [&](const MachineInstr &MI) {
166 return &MI == CheapRemat || &MI == ExpensiveRemat;
167 };
168 ASSERT_EQ(MF->size(), 2U);
169 const auto TotalScore =
170 llvm::calculateRegAllocScore(*MF, MBBFreqMock, IsRemat);
171 ASSERT_DOUBLE_EQ(Freq1, TotalScore.copyCounts());
172 ASSERT_DOUBLE_EQ(2.0 * Freq1 + Freq2, TotalScore.loadCounts());
173 ASSERT_DOUBLE_EQ(Freq1 + Freq2, TotalScore.storeCounts());
174 ASSERT_DOUBLE_EQ(Freq2, TotalScore.loadStoreCounts());
175 ASSERT_DOUBLE_EQ(Freq1, TotalScore.cheapRematCounts());
176 ASSERT_DOUBLE_EQ(Freq2, TotalScore.expensiveRematCounts());
177 ASSERT_DOUBLE_EQ(
178 TotalScore.getScore(),
179 TotalScore.copyCounts() * CopyWeight +
180 TotalScore.loadCounts() * LoadWeight +
181 TotalScore.storeCounts() * StoreWeight +
182 TotalScore.loadStoreCounts() * (LoadWeight + StoreWeight) +
183 TotalScore.cheapRematCounts() * CheapRematWeight +
184 TotalScore.expensiveRematCounts() * ExpensiveRematWeight
185
186 );
187 }
188 } // end namespace