1 //===- MachineSizeOptsTest.cpp --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/CodeGen/MachineSizeOpts.h"
10 #include "llvm/Analysis/ProfileSummaryInfo.h"
11 #include "llvm/Analysis/BlockFrequencyInfo.h"
12 #include "llvm/Analysis/BranchProbabilityInfo.h"
13 #include "llvm/Analysis/LoopInfo.h"
14 #include "llvm/CodeGen/MIRParser/MIRParser.h"
15 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
16 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
17 #include "llvm/CodeGen/MachineDominators.h"
18 #include "llvm/CodeGen/MachineLoopInfo.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/Support/TargetRegistry.h"
21 #include "llvm/Support/TargetSelect.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "gtest/gtest.h"
29 std::unique_ptr<LLVMTargetMachine> createTargetMachine() {
30 auto TT(Triple::normalize("x86_64--"));
32 const Target *TheTarget = TargetRegistry::lookupTarget(TT, Error);
33 return std::unique_ptr<LLVMTargetMachine>(static_cast<LLVMTargetMachine*>(
34 TheTarget->createTargetMachine(TT, "", "", TargetOptions(), None, None,
35 CodeGenOpt::Default)));
38 class MachineSizeOptsTest : public testing::Test {
40 static const char* MIRString;
42 std::unique_ptr<LLVMTargetMachine> TM;
43 std::unique_ptr<MachineModuleInfo> MMI;
44 std::unique_ptr<MIRParser> Parser;
45 std::unique_ptr<Module> M;
47 std::unique_ptr<MachineDominatorTree> MDT;
48 std::unique_ptr<MachineLoopInfo> MLI;
49 std::unique_ptr<MachineBranchProbabilityInfo> MBPI;
50 std::unique_ptr<MachineBlockFrequencyInfo> MBFI;
51 BFIData(MachineFunction &MF) {
52 MDT.reset(new MachineDominatorTree(MF));
53 MLI.reset(new MachineLoopInfo(*MDT));
54 MBPI.reset(new MachineBranchProbabilityInfo());
55 MBFI.reset(new MachineBlockFrequencyInfo(MF, *MBPI, *MLI));
57 MachineBlockFrequencyInfo *get() { return MBFI.get(); }
60 static void SetUpTestCase() {
61 InitializeAllTargets();
62 InitializeAllTargetMCs();
65 void SetUp() override {
66 TM = createTargetMachine();
67 std::unique_ptr<MemoryBuffer> MBuffer =
68 MemoryBuffer::getMemBuffer(MIRString);
69 Parser = createMIRParser(std::move(MBuffer), Context);
71 report_fatal_error("null MIRParser");
72 M = Parser->parseIRModule();
74 report_fatal_error("parseIRModule failed");
75 M->setTargetTriple(TM->getTargetTriple().getTriple());
76 M->setDataLayout(TM->createDataLayout());
77 MMI = std::make_unique<MachineModuleInfo>(TM.get());
78 if (Parser->parseMachineFunctions(*M, *MMI.get()))
79 report_fatal_error("parseMachineFunctions failed");
82 MachineFunction *getMachineFunction(Module *M, StringRef Name) {
83 auto F = M->getFunction(Name);
85 report_fatal_error("null Function");
86 auto &MF = MMI->getOrCreateMachineFunction(*F);
91 TEST_F(MachineSizeOptsTest, Test) {
92 MachineFunction *F = getMachineFunction(M.get(), "f");
93 ASSERT_TRUE(F != nullptr);
94 MachineFunction *G = getMachineFunction(M.get(), "g");
95 ASSERT_TRUE(G != nullptr);
96 MachineFunction *H = getMachineFunction(M.get(), "h");
97 ASSERT_TRUE(H != nullptr);
98 ProfileSummaryInfo PSI = ProfileSummaryInfo(*M.get());
99 ASSERT_TRUE(PSI.hasProfileSummary());
103 MachineBlockFrequencyInfo *MBFI_F = BFID_F.get();
104 MachineBlockFrequencyInfo *MBFI_G = BFID_G.get();
105 MachineBlockFrequencyInfo *MBFI_H = BFID_H.get();
106 MachineBasicBlock &BB0 = F->front();
107 auto iter = BB0.succ_begin();
108 MachineBasicBlock *BB1 = *iter;
110 MachineBasicBlock *BB2 = *iter;
112 ASSERT_TRUE(iter == BB0.succ_end());
113 MachineBasicBlock *BB3 = *BB1->succ_begin();
114 ASSERT_TRUE(BB3 == *BB2->succ_begin());
115 EXPECT_FALSE(shouldOptimizeForSize(F, &PSI, MBFI_F));
116 EXPECT_TRUE(shouldOptimizeForSize(G, &PSI, MBFI_G));
117 EXPECT_FALSE(shouldOptimizeForSize(H, &PSI, MBFI_H));
118 EXPECT_FALSE(shouldOptimizeForSize(&BB0, &PSI, MBFI_F));
119 EXPECT_FALSE(shouldOptimizeForSize(BB1, &PSI, MBFI_F));
120 EXPECT_TRUE(shouldOptimizeForSize(BB2, &PSI, MBFI_F));
121 EXPECT_FALSE(shouldOptimizeForSize(BB3, &PSI, MBFI_F));
124 const char* MachineSizeOptsTest::MIRString = R"MIR(
126 define i32 @g(i32 %x) !prof !14 {
130 define i32 @h(i32 %x) !prof !15 {
134 define i32 @f(i32 %x) !prof !16 {
136 %y1 = icmp eq i32 %x, 0
137 br i1 %y1, label %bb1, label %bb2, !prof !17
140 %z1 = call i32 @g(i32 %x)
144 %z2 = call i32 @h(i32 %x)
147 bb3: ; preds = %bb2, %bb1
148 %y2 = phi i32 [ 0, %bb1 ], [ 1, %bb2 ]
152 !llvm.module.flags = !{!0}
154 !0 = !{i32 1, !"ProfileSummary", !1}
155 !1 = !{!2, !3, !4, !5, !6, !7, !8, !9}
156 !2 = !{!"ProfileFormat", !"InstrProf"}
157 !3 = !{!"TotalCount", i64 10000}
158 !4 = !{!"MaxCount", i64 10}
159 !5 = !{!"MaxInternalCount", i64 1}
160 !6 = !{!"MaxFunctionCount", i64 1000}
161 !7 = !{!"NumCounts", i64 3}
162 !8 = !{!"NumFunctions", i64 3}
163 !9 = !{!"DetailedSummary", !10}
164 !10 = !{!11, !12, !13}
165 !11 = !{i32 10000, i64 1000, i32 1}
166 !12 = !{i32 999000, i64 300, i32 3}
167 !13 = !{i32 999999, i64 5, i32 10}
168 !14 = !{!"function_entry_count", i64 1}
169 !15 = !{!"function_entry_count", i64 100}
170 !16 = !{!"function_entry_count", i64 400}
171 !17 = !{!"branch_weights", i32 100, i32 1}
178 %1:gr32 = MOV32r0 implicit-def dead $eflags
187 %1:gr32 = MOV32r0 implicit-def dead $eflags
194 tracksRegLiveness: true
197 successors: %bb.1(0x7ebb907a), %bb.2(0x01446f86)
201 TEST32rr %1, %1, implicit-def $eflags
202 JCC_1 %bb.2, 5, implicit $eflags
206 successors: %bb.3(0x80000000)
208 ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp
210 CALL64pcrel32 @g, csr_64, implicit $rsp, implicit $ssp, implicit $edi, implicit-def $rsp, implicit-def $ssp, implicit-def $eax
211 ADJCALLSTACKUP64 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp
213 %4:gr32 = MOV32r0 implicit-def dead $eflags
217 successors: %bb.3(0x80000000)
219 ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp
221 CALL64pcrel32 @h, csr_64, implicit $rsp, implicit $ssp, implicit $edi, implicit-def $rsp, implicit-def $ssp, implicit-def $eax
222 ADJCALLSTACKUP64 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp
227 %0:gr32 = PHI %2, %bb.2, %4, %bb.1
234 } // anonymous namespace