]> granicus.if.org Git - llvm/blob - unittests/CodeGen/MachineSizeOptsTest.cpp
[InstCombine] Fold uadd.sat(a, b) == 0 and usub.sat(a, b) == 0
[llvm] / unittests / CodeGen / MachineSizeOptsTest.cpp
1 //===- MachineSizeOptsTest.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 "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"
24
25 using namespace llvm;
26
27 namespace {
28
29 std::unique_ptr<LLVMTargetMachine> createTargetMachine() {
30   auto TT(Triple::normalize("x86_64--"));
31   std::string Error;
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)));
36 }
37
38 class MachineSizeOptsTest : public testing::Test {
39  protected:
40   static const char* MIRString;
41   LLVMContext Context;
42   std::unique_ptr<LLVMTargetMachine> TM;
43   std::unique_ptr<MachineModuleInfo> MMI;
44   std::unique_ptr<MIRParser> Parser;
45   std::unique_ptr<Module> M;
46   struct BFIData {
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));
56     }
57     MachineBlockFrequencyInfo *get() { return MBFI.get(); }
58   };
59
60   static void SetUpTestCase() {
61     InitializeAllTargets();
62     InitializeAllTargetMCs();
63   }
64
65   void SetUp() override {
66     TM = createTargetMachine();
67     std::unique_ptr<MemoryBuffer> MBuffer =
68         MemoryBuffer::getMemBuffer(MIRString);
69     Parser = createMIRParser(std::move(MBuffer), Context);
70     if (!Parser)
71       report_fatal_error("null MIRParser");
72     M = Parser->parseIRModule();
73     if (!M)
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");
80   }
81
82   MachineFunction *getMachineFunction(Module *M, StringRef Name) {
83     auto F = M->getFunction(Name);
84     if (!F)
85       report_fatal_error("null Function");
86     auto &MF = MMI->getOrCreateMachineFunction(*F);
87     return &MF;
88   }
89 };
90
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());
100   BFIData BFID_F(*F);
101   BFIData BFID_G(*G);
102   BFIData BFID_H(*H);
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;
109   iter++;
110   MachineBasicBlock *BB2 = *iter;
111   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));
122 }
123
124 const char* MachineSizeOptsTest::MIRString = R"MIR(
125 --- |
126   define i32 @g(i32 %x) !prof !14 {
127     ret i32 0
128   }
129
130   define i32 @h(i32 %x) !prof !15 {
131     ret i32 0
132   }
133
134   define i32 @f(i32 %x) !prof !16 {
135   bb0:
136     %y1 = icmp eq i32 %x, 0
137     br i1 %y1, label %bb1, label %bb2, !prof !17
138
139   bb1:                                              ; preds = %bb0
140     %z1 = call i32 @g(i32 %x)
141     br label %bb3
142
143   bb2:                                              ; preds = %bb0
144     %z2 = call i32 @h(i32 %x)
145     br label %bb3
146
147   bb3:                                              ; preds = %bb2, %bb1
148     %y2 = phi i32 [ 0, %bb1 ], [ 1, %bb2 ]
149     ret i32 %y2
150   }
151
152   !llvm.module.flags = !{!0}
153
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}
172
173 ...
174 ---
175 name:            g
176 body:             |
177   bb.0:
178     %1:gr32 = MOV32r0 implicit-def dead $eflags
179     $eax = COPY %1
180     RET 0, $eax
181
182 ...
183 ---
184 name:            h
185 body:             |
186   bb.0:
187     %1:gr32 = MOV32r0 implicit-def dead $eflags
188     $eax = COPY %1
189     RET 0, $eax
190
191 ...
192 ---
193 name:            f
194 tracksRegLiveness: true
195 body:             |
196   bb.0:
197     successors: %bb.1(0x7ebb907a), %bb.2(0x01446f86)
198     liveins: $edi
199
200     %1:gr32 = COPY $edi
201     TEST32rr %1, %1, implicit-def $eflags
202     JCC_1 %bb.2, 5, implicit $eflags
203     JMP_1 %bb.1
204
205   bb.1:
206     successors: %bb.3(0x80000000)
207
208     ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp
209     $edi = COPY %1
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
212     %5:gr32 = COPY $eax
213     %4:gr32 = MOV32r0 implicit-def dead $eflags
214     JMP_1 %bb.3
215
216   bb.2:
217     successors: %bb.3(0x80000000)
218
219     ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp
220     $edi = COPY %1
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
223     %3:gr32 = COPY $eax
224     %2:gr32 = MOV32ri 1
225
226   bb.3:
227     %0:gr32 = PHI %2, %bb.2, %4, %bb.1
228     $eax = COPY %0
229     RET 0, $eax
230
231 ...
232 )MIR";
233
234 } // anonymous namespace