Debian Codegraph @

llvm/unittests/Analysis/FunctionPropertiesAnalysisTest.cpp

raw
1 //===- FunctionPropertiesAnalysisTest.cpp - Function Properties Unit Tests-===//
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/Analysis/FunctionPropertiesAnalysis.h"
10 #include "llvm/Analysis/AliasAnalysis.h"
11 #include "llvm/Analysis/IR2Vec.h"
12 #include "llvm/Analysis/LoopInfo.h"
13 #include "llvm/AsmParser/Parser.h"
14 #include "llvm/IR/Dominators.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/Passes/PassBuilder.h"
20 #include "llvm/Passes/StandardInstrumentations.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include "llvm/Transforms/Utils/Cloning.h"
24 #include "gmock/gmock.h"
25 #include "gtest/gtest.h"
26 #include <cstring>
27
28 using namespace llvm;
29 using namespace testing;
30
31 namespace llvm {
32 LLVM_ABI extern cl::opt<bool> EnableDetailedFunctionProperties;
33 LLVM_ABI extern cl::opt<bool> BigBasicBlockInstructionThreshold;
34 LLVM_ABI extern cl::opt<bool> MediumBasicBlockInstrutionThreshold;
35 LLVM_ABI extern cl::opt<float> ir2vec::OpcWeight;
36 LLVM_ABI extern cl::opt<float> ir2vec::TypeWeight;
37 LLVM_ABI extern cl::opt<float> ir2vec::ArgWeight;
38 } // namespace llvm
39
40 namespace {
41
42 class FunctionPropertiesAnalysisTest : public testing::Test {
43 public:
44 FunctionPropertiesAnalysisTest() {
45 auto VocabVector = ir2vec::Vocabulary::createDummyVocabForTest(1);
46 MAM.registerPass([&] { return IR2VecVocabAnalysis(VocabVector); });
47 IR2VecVocab = ir2vec::Vocabulary(std::move(VocabVector));
48 MAM.registerPass([&] { return PassInstrumentationAnalysis(); });
49 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
50 FAM.registerPass([&] { return DominatorTreeAnalysis(); });
51 FAM.registerPass([&] { return LoopAnalysis(); });
52 FAM.registerPass([&] { return PassInstrumentationAnalysis(); });
53
54 ir2vec::OpcWeight = 1.0;
55 ir2vec::TypeWeight = 1.0;
56 ir2vec::ArgWeight = 1.0;
57 }
58
59 private:
60 float OriginalOpcWeight = ir2vec::OpcWeight;
61 float OriginalTypeWeight = ir2vec::TypeWeight;
62 float OriginalArgWeight = ir2vec::ArgWeight;
63
64 protected:
65 std::unique_ptr<DominatorTree> DT;
66 std::unique_ptr<LoopInfo> LI;
67 FunctionAnalysisManager FAM;
68 ModuleAnalysisManager MAM;
69 ir2vec::Vocabulary IR2VecVocab;
70
71 void TearDown() override {
72 // Restore original IR2Vec weights
73 ir2vec::OpcWeight = OriginalOpcWeight;
74 ir2vec::TypeWeight = OriginalTypeWeight;
75 ir2vec::ArgWeight = OriginalArgWeight;
76 }
77
78 FunctionPropertiesInfo buildFPI(Function &F) {
79 // FunctionPropertiesInfo assumes IR2VecVocabAnalysis has been run to
80 // use IR2Vec.
81 auto VocabResult = MAM.getResult<IR2VecVocabAnalysis>(*F.getParent());
82 (void)VocabResult;
83 return FunctionPropertiesInfo::getFunctionPropertiesInfo(F, FAM);
84 }
85
86 void invalidate(Function &F) {
87 PreservedAnalyses PA = PreservedAnalyses::none();
88 FAM.invalidate(F, PA);
89 }
90
91 std::unique_ptr<Module> makeLLVMModule(LLVMContext &C, const char *IR) {
92 SMDiagnostic Err;
93 std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
94 if (!Mod)
95 Err.print("MLAnalysisTests", errs());
96 return Mod;
97 }
98
99 CallBase *findCall(Function &F, const char *Name = nullptr) {
100 for (auto &BB : F)
101 for (auto &I : BB)
102 if (auto *CB = dyn_cast<CallBase>(&I))
103 if (!Name || CB->getName() == Name)
104 return CB;
105 return nullptr;
106 }
107
108 std::unique_ptr<ir2vec::Embedder> createEmbedder(const Function &F) {
109 auto Emb = ir2vec::Embedder::create(IR2VecKind::Symbolic, F, IR2VecVocab);
110 EXPECT_TRUE(static_cast<bool>(Emb));
111 return Emb;
112 }
113 };
114
115 TEST_F(FunctionPropertiesAnalysisTest, BasicTest) {
116 LLVMContext C;
117 std::unique_ptr<Module> M = makeLLVMModule(C,
118 R"IR(
119 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
120 target triple = "x86_64-pc-linux-gnu"
121 declare i32 @f1(i32)
122 declare i32 @f2(i32)
123 define i32 @branches(i32) {
124 %cond = icmp slt i32 %0, 3
125 br i1 %cond, label %then, label %else
126 then:
127 %ret.1 = call i32 @f1(i32 %0)
128 br label %last.block
129 else:
130 %ret.2 = call i32 @f2(i32 %0)
131 br label %last.block
132 last.block:
133 %ret = phi i32 [%ret.1, %then], [%ret.2, %else]
134 ret i32 %ret
135 }
136 define internal i32 @top() {
137 %1 = call i32 @branches(i32 2)
138 %2 = call i32 @f1(i32 %1)
139 ret i32 %2
140 }
141 )IR");
142
143 Function *BranchesFunction = M->getFunction("branches");
144 FunctionPropertiesInfo BranchesFeatures = buildFPI(*BranchesFunction);
145 EXPECT_EQ(BranchesFeatures.BasicBlockCount, 4);
146 EXPECT_EQ(BranchesFeatures.BlocksReachedFromConditionalInstruction, 2);
147 // 2 Users: top is one. The other is added because @branches is not internal,
148 // so it may have external callers.
149 EXPECT_EQ(BranchesFeatures.Uses, 2);
150 EXPECT_EQ(BranchesFeatures.DirectCallsToDefinedFunctions, 0);
151 EXPECT_EQ(BranchesFeatures.LoadInstCount, 0);
152 EXPECT_EQ(BranchesFeatures.StoreInstCount, 0);
153 EXPECT_EQ(BranchesFeatures.MaxLoopDepth, 0);
154 EXPECT_EQ(BranchesFeatures.TopLevelLoopCount, 0);
155 EXPECT_TRUE(BranchesFeatures.getFunctionEmbedding().approximatelyEquals(
156 createEmbedder(*BranchesFunction)->getFunctionVector()));
157
158 Function *TopFunction = M->getFunction("top");
159 FunctionPropertiesInfo TopFeatures = buildFPI(*TopFunction);
160 EXPECT_EQ(TopFeatures.BasicBlockCount, 1);
161 EXPECT_EQ(TopFeatures.BlocksReachedFromConditionalInstruction, 0);
162 EXPECT_EQ(TopFeatures.Uses, 0);
163 EXPECT_EQ(TopFeatures.DirectCallsToDefinedFunctions, 1);
164 EXPECT_TRUE(TopFeatures.getFunctionEmbedding().approximatelyEquals(
165 createEmbedder(*TopFunction)->getFunctionVector()));
166 EXPECT_EQ(BranchesFeatures.LoadInstCount, 0);
167 EXPECT_EQ(BranchesFeatures.StoreInstCount, 0);
168 EXPECT_EQ(BranchesFeatures.MaxLoopDepth, 0);
169 EXPECT_EQ(BranchesFeatures.TopLevelLoopCount, 0);
170
171 EnableDetailedFunctionProperties.setValue(true);
172 FunctionPropertiesInfo DetailedBranchesFeatures = buildFPI(*BranchesFunction);
173 EXPECT_EQ(DetailedBranchesFeatures.BasicBlocksWithSingleSuccessor, 2);
174 EXPECT_EQ(DetailedBranchesFeatures.BasicBlocksWithTwoSuccessors, 1);
175 EXPECT_EQ(DetailedBranchesFeatures.BasicBlocksWithMoreThanTwoSuccessors, 0);
176 EXPECT_EQ(DetailedBranchesFeatures.BasicBlocksWithSinglePredecessor, 2);
177 EXPECT_EQ(DetailedBranchesFeatures.BasicBlocksWithTwoPredecessors, 1);
178 EXPECT_EQ(DetailedBranchesFeatures.BasicBlocksWithMoreThanTwoPredecessors, 0);
179 EXPECT_EQ(DetailedBranchesFeatures.BigBasicBlocks, 0);
180 EXPECT_EQ(DetailedBranchesFeatures.MediumBasicBlocks, 0);
181 EXPECT_EQ(DetailedBranchesFeatures.SmallBasicBlocks, 4);
182 EXPECT_EQ(DetailedBranchesFeatures.CastInstructionCount, 0);
183 EXPECT_EQ(DetailedBranchesFeatures.FloatingPointInstructionCount, 0);
184 EXPECT_EQ(DetailedBranchesFeatures.IntegerInstructionCount, 4);
185 EXPECT_EQ(DetailedBranchesFeatures.ConstantIntOperandCount, 1);
186 EXPECT_EQ(DetailedBranchesFeatures.ConstantFPOperandCount, 0);
187 EXPECT_EQ(DetailedBranchesFeatures.ConstantOperandCount, 0);
188 EXPECT_EQ(DetailedBranchesFeatures.InstructionOperandCount, 4);
189 EXPECT_EQ(DetailedBranchesFeatures.BasicBlockOperandCount, 4);
190 EXPECT_EQ(DetailedBranchesFeatures.GlobalValueOperandCount, 2);
191 EXPECT_EQ(DetailedBranchesFeatures.InlineAsmOperandCount, 0);
192 EXPECT_EQ(DetailedBranchesFeatures.ArgumentOperandCount, 3);
193 EXPECT_EQ(DetailedBranchesFeatures.UnknownOperandCount, 0);
194 EXPECT_EQ(DetailedBranchesFeatures.CriticalEdgeCount, 0);
195 EXPECT_EQ(DetailedBranchesFeatures.ControlFlowEdgeCount, 4);
196 EXPECT_EQ(DetailedBranchesFeatures.UnconditionalBranchCount, 2);
197 EXPECT_EQ(DetailedBranchesFeatures.IntrinsicCount, 0);
198 EXPECT_EQ(DetailedBranchesFeatures.DirectCallCount, 2);
199 EXPECT_EQ(DetailedBranchesFeatures.IndirectCallCount, 0);
200 EXPECT_EQ(DetailedBranchesFeatures.CallReturnsIntegerCount, 2);
201 EXPECT_EQ(DetailedBranchesFeatures.CallReturnsFloatCount, 0);
202 EXPECT_EQ(DetailedBranchesFeatures.CallReturnsPointerCount, 0);
203 EXPECT_EQ(DetailedBranchesFeatures.CallWithManyArgumentsCount, 0);
204 EXPECT_EQ(DetailedBranchesFeatures.CallWithPointerArgumentCount, 0);
205 EXPECT_TRUE(
206 DetailedBranchesFeatures.getFunctionEmbedding().approximatelyEquals(
207 createEmbedder(*BranchesFunction)->getFunctionVector()));
208 EnableDetailedFunctionProperties.setValue(false);
209 }
210
211 TEST_F(FunctionPropertiesAnalysisTest, DifferentPredecessorSuccessorCounts) {
212 LLVMContext C;
213 std::unique_ptr<Module> M = makeLLVMModule(C,
214 R"IR(
215 define i64 @f1() {
216 br i1 0, label %br1, label %finally
217 br1:
218 ret i64 0
219 finally:
220 ret i64 3
221 }
222 )IR");
223
224 Function *F1 = M->getFunction("f1");
225 EnableDetailedFunctionProperties.setValue(true);
226 FunctionPropertiesInfo DetailedF1Properties = buildFPI(*F1);
227 EXPECT_EQ(DetailedF1Properties.BasicBlocksWithSingleSuccessor, 0);
228 EXPECT_EQ(DetailedF1Properties.BasicBlocksWithTwoSuccessors, 1);
229 EXPECT_EQ(DetailedF1Properties.BasicBlocksWithMoreThanTwoSuccessors, 0);
230 EXPECT_EQ(DetailedF1Properties.BasicBlocksWithSinglePredecessor, 2);
231 EXPECT_EQ(DetailedF1Properties.BasicBlocksWithTwoPredecessors, 0);
232 EXPECT_EQ(DetailedF1Properties.BasicBlocksWithMoreThanTwoPredecessors, 0);
233 EXPECT_EQ(DetailedF1Properties.BigBasicBlocks, 0);
234 EXPECT_EQ(DetailedF1Properties.MediumBasicBlocks, 0);
235 EXPECT_EQ(DetailedF1Properties.SmallBasicBlocks, 3);
236 EXPECT_EQ(DetailedF1Properties.CastInstructionCount, 0);
237 EXPECT_EQ(DetailedF1Properties.FloatingPointInstructionCount, 0);
238 EXPECT_EQ(DetailedF1Properties.IntegerInstructionCount, 0);
239 EXPECT_EQ(DetailedF1Properties.ConstantIntOperandCount, 3);
240 EXPECT_EQ(DetailedF1Properties.ConstantFPOperandCount, 0);
241 EXPECT_EQ(DetailedF1Properties.ConstantOperandCount, 0);
242 EXPECT_EQ(DetailedF1Properties.InstructionOperandCount, 0);
243 EXPECT_EQ(DetailedF1Properties.BasicBlockOperandCount, 2);
244 EXPECT_EQ(DetailedF1Properties.GlobalValueOperandCount, 0);
245 EXPECT_EQ(DetailedF1Properties.InlineAsmOperandCount, 0);
246 EXPECT_EQ(DetailedF1Properties.ArgumentOperandCount, 0);
247 EXPECT_EQ(DetailedF1Properties.UnknownOperandCount, 0);
248 EXPECT_EQ(DetailedF1Properties.CriticalEdgeCount, 0);
249 EXPECT_EQ(DetailedF1Properties.ControlFlowEdgeCount, 2);
250 EXPECT_EQ(DetailedF1Properties.UnconditionalBranchCount, 0);
251 EXPECT_EQ(DetailedF1Properties.IntrinsicCount, 0);
252 EXPECT_EQ(DetailedF1Properties.DirectCallCount, 0);
253 EXPECT_EQ(DetailedF1Properties.IndirectCallCount, 0);
254 EXPECT_EQ(DetailedF1Properties.CallReturnsIntegerCount, 0);
255 EXPECT_EQ(DetailedF1Properties.CallReturnsFloatCount, 0);
256 EXPECT_EQ(DetailedF1Properties.CallReturnsPointerCount, 0);
257 EXPECT_EQ(DetailedF1Properties.CallWithManyArgumentsCount, 0);
258 EXPECT_EQ(DetailedF1Properties.CallWithPointerArgumentCount, 0);
259 EXPECT_TRUE(DetailedF1Properties.getFunctionEmbedding().approximatelyEquals(
260 createEmbedder(*F1)->getFunctionVector()));
261 EnableDetailedFunctionProperties.setValue(false);
262 }
263
264 TEST_F(FunctionPropertiesAnalysisTest, InlineSameBBSimple) {
265 LLVMContext C;
266 std::unique_ptr<Module> M = makeLLVMModule(C,
267 R"IR(
268 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
269 target triple = "x86_64-pc-linux-gnu"
270 define i32 @f1(i32 %a) {
271 %b = call i32 @f2(i32 %a)
272 %c = add i32 %b, 2
273 ret i32 %c
274 }
275
276 define i32 @f2(i32 %a) {
277 %b = add i32 %a, 1
278 ret i32 %b
279 }
280 )IR");
281
282 Function *F1 = M->getFunction("f1");
283 CallBase *CB = findCall(*F1, "b");
284 EXPECT_NE(CB, nullptr);
285
286 FunctionPropertiesInfo ExpectedInitial;
287 ExpectedInitial.BasicBlockCount = 1;
288 ExpectedInitial.TotalInstructionCount = 3;
289 ExpectedInitial.Uses = 1;
290 ExpectedInitial.DirectCallsToDefinedFunctions = 1;
291 ExpectedInitial.setFunctionEmbeddingForTest(
292 createEmbedder(*F1)->getFunctionVector());
293
294 FunctionPropertiesInfo ExpectedFinal = ExpectedInitial;
295 ExpectedFinal.DirectCallsToDefinedFunctions = 0;
296
297 auto FPI = buildFPI(*F1);
298 EXPECT_EQ(FPI, ExpectedInitial);
299
300 FunctionPropertiesUpdater FPU(FPI, *CB);
301 InlineFunctionInfo IFI;
302 auto IR = llvm::InlineFunction(*CB, IFI);
303 EXPECT_TRUE(IR.isSuccess());
304 invalidate(*F1);
305 ExpectedFinal.setFunctionEmbeddingForTest(
306 createEmbedder(*F1)->getFunctionVector());
307
308 EXPECT_TRUE(FPU.finishAndTest(FAM));
309 EXPECT_EQ(FPI, ExpectedFinal);
310 }
311
312 TEST_F(FunctionPropertiesAnalysisTest, InlineSameBBLargerCFG) {
313 LLVMContext C;
314 std::unique_ptr<Module> M = makeLLVMModule(C,
315 R"IR(
316 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
317 target triple = "x86_64-pc-linux-gnu"
318 define i32 @f1(i32 %a) {
319 entry:
320 %i = icmp slt i32 %a, 0
321 br i1 %i, label %if.then, label %if.else
322 if.then:
323 %b = call i32 @f2(i32 %a)
324 %c1 = add i32 %b, 2
325 br label %end
326 if.else:
327 %c2 = add i32 %a, 1
328 br label %end
329 end:
330 %ret = phi i32 [%c1, %if.then],[%c2, %if.else]
331 ret i32 %ret
332 }
333
334 define i32 @f2(i32 %a) {
335 %b = add i32 %a, 1
336 ret i32 %b
337 }
338 )IR");
339
340 Function *F1 = M->getFunction("f1");
341 CallBase *CB = findCall(*F1, "b");
342 EXPECT_NE(CB, nullptr);
343
344 FunctionPropertiesInfo ExpectedInitial;
345 ExpectedInitial.BasicBlockCount = 4;
346 ExpectedInitial.BlocksReachedFromConditionalInstruction = 2;
347 ExpectedInitial.TotalInstructionCount = 9;
348 ExpectedInitial.Uses = 1;
349 ExpectedInitial.DirectCallsToDefinedFunctions = 1;
350 ExpectedInitial.setFunctionEmbeddingForTest(
351 createEmbedder(*F1)->getFunctionVector());
352
353 FunctionPropertiesInfo ExpectedFinal = ExpectedInitial;
354 ExpectedFinal.DirectCallsToDefinedFunctions = 0;
355
356 auto FPI = buildFPI(*F1);
357 EXPECT_EQ(FPI, ExpectedInitial);
358
359 FunctionPropertiesUpdater FPU(FPI, *CB);
360 InlineFunctionInfo IFI;
361 auto IR = llvm::InlineFunction(*CB, IFI);
362 EXPECT_TRUE(IR.isSuccess());
363 invalidate(*F1);
364 EXPECT_TRUE(FPU.finishAndTest(FAM));
365
366 ExpectedFinal.setFunctionEmbeddingForTest(
367 createEmbedder(*F1)->getFunctionVector());
368 EXPECT_EQ(FPI, ExpectedFinal);
369 }
370
371 TEST_F(FunctionPropertiesAnalysisTest, InlineSameBBLoops) {
372 LLVMContext C;
373 std::unique_ptr<Module> M = makeLLVMModule(C,
374 R"IR(
375 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
376 target triple = "x86_64-pc-linux-gnu"
377 define i32 @f1(i32 %a) {
378 entry:
379 %i = icmp slt i32 %a, 0
380 br i1 %i, label %if.then, label %if.else
381 if.then:
382 %b = call i32 @f2(i32 %a)
383 %c1 = add i32 %b, 2
384 br label %end
385 if.else:
386 %c2 = add i32 %a, 1
387 br label %end
388 end:
389 %ret = phi i32 [%c1, %if.then],[%c2, %if.else]
390 ret i32 %ret
391 }
392
393 define i32 @f2(i32 %a) {
394 entry:
395 br label %loop
396 loop:
397 %indvar = phi i32 [%indvar.next, %loop], [0, %entry]
398 %b = add i32 %a, %indvar
399 %indvar.next = add i32 %indvar, 1
400 %cond = icmp slt i32 %indvar.next, %a
401 br i1 %cond, label %loop, label %exit
402 exit:
403 ret i32 %b
404 }
405 )IR");
406
407 Function *F1 = M->getFunction("f1");
408 CallBase *CB = findCall(*F1, "b");
409 EXPECT_NE(CB, nullptr);
410
411 FunctionPropertiesInfo ExpectedInitial;
412 ExpectedInitial.BasicBlockCount = 4;
413 ExpectedInitial.BlocksReachedFromConditionalInstruction = 2;
414 ExpectedInitial.TotalInstructionCount = 9;
415 ExpectedInitial.Uses = 1;
416 ExpectedInitial.DirectCallsToDefinedFunctions = 1;
417 ExpectedInitial.setFunctionEmbeddingForTest(
418 createEmbedder(*F1)->getFunctionVector());
419
420 FunctionPropertiesInfo ExpectedFinal;
421 ExpectedFinal.BasicBlockCount = 6;
422 ExpectedFinal.BlocksReachedFromConditionalInstruction = 4;
423 ExpectedFinal.Uses = 1;
424 ExpectedFinal.MaxLoopDepth = 1;
425 ExpectedFinal.TopLevelLoopCount = 1;
426 ExpectedFinal.TotalInstructionCount = 14;
427
428 auto FPI = buildFPI(*F1);
429 EXPECT_EQ(FPI, ExpectedInitial);
430 FunctionPropertiesUpdater FPU(FPI, *CB);
431 InlineFunctionInfo IFI;
432
433 auto IR = llvm::InlineFunction(*CB, IFI);
434 EXPECT_TRUE(IR.isSuccess());
435 invalidate(*F1);
436 EXPECT_TRUE(FPU.finishAndTest(FAM));
437
438 ExpectedFinal.setFunctionEmbeddingForTest(
439 createEmbedder(*F1)->getFunctionVector());
440 EXPECT_EQ(FPI, ExpectedFinal);
441 }
442
443 TEST_F(FunctionPropertiesAnalysisTest, InvokeSimple) {
444 LLVMContext C;
445 std::unique_ptr<Module> M = makeLLVMModule(C,
446 R"IR(
447 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
448 target triple = "x86_64-pc-linux-gnu"
449 declare void @might_throw()
450
451 define internal void @callee() {
452 entry:
453 call void @might_throw()
454 ret void
455 }
456
457 define i32 @caller() personality i32 (...)* @__gxx_personality_v0 {
458 entry:
459 invoke void @callee()
460 to label %cont unwind label %exc
461
462 cont:
463 ret i32 0
464
465 exc:
466 %exn = landingpad {i8*, i32}
467 cleanup
468 ret i32 1
469 }
470
471 declare i32 @__gxx_personality_v0(...)
472 )IR");
473
474 Function *F1 = M->getFunction("caller");
475 CallBase *CB = findCall(*F1);
476 EXPECT_NE(CB, nullptr);
477
478 auto FPI = buildFPI(*F1);
479 FunctionPropertiesUpdater FPU(FPI, *CB);
480 InlineFunctionInfo IFI;
481 auto IR = llvm::InlineFunction(*CB, IFI);
482 EXPECT_TRUE(IR.isSuccess());
483 invalidate(*F1);
484 EXPECT_TRUE(FPU.finishAndTest(FAM));
485 EXPECT_EQ(static_cast<size_t>(FPI.BasicBlockCount), F1->size());
486 EXPECT_EQ(static_cast<size_t>(FPI.TotalInstructionCount),
487 F1->getInstructionCount());
488 EXPECT_TRUE(FPI.getFunctionEmbedding().approximatelyEquals(
489 createEmbedder(*F1)->getFunctionVector()));
490 }
491
492 TEST_F(FunctionPropertiesAnalysisTest, InvokeUnreachableHandler) {
493 LLVMContext C;
494 std::unique_ptr<Module> M = makeLLVMModule(C,
495 R"IR(
496 declare void @might_throw()
497
498 define internal i32 @callee() personality i32 (...)* @__gxx_personality_v0 {
499 entry:
500 invoke void @might_throw()
501 to label %cont unwind label %exc
502
503 cont:
504 ret i32 0
505
506 exc:
507 %exn = landingpad {i8*, i32}
508 cleanup
509 resume { i8*, i32 } %exn
510 }
511
512 define i32 @caller() personality i32 (...)* @__gxx_personality_v0 {
513 entry:
514 %X = invoke i32 @callee()
515 to label %cont unwind label %Handler
516
517 cont:
518 ret i32 %X
519
520 Handler:
521 %exn = landingpad {i8*, i32}
522 cleanup
523 ret i32 1
524 }
525
526 declare i32 @__gxx_personality_v0(...)
527 )IR");
528
529 Function *F1 = M->getFunction("caller");
530 CallBase *CB = findCall(*F1);
531 EXPECT_NE(CB, nullptr);
532
533 auto FPI = buildFPI(*F1);
534 FunctionPropertiesUpdater FPU(FPI, *CB);
535 InlineFunctionInfo IFI;
536 auto IR = llvm::InlineFunction(*CB, IFI);
537 EXPECT_TRUE(IR.isSuccess());
538 invalidate(*F1);
539 EXPECT_TRUE(FPU.finishAndTest(FAM));
540 EXPECT_EQ(static_cast<size_t>(FPI.BasicBlockCount), F1->size() - 1);
541 EXPECT_EQ(static_cast<size_t>(FPI.TotalInstructionCount),
542 F1->getInstructionCount() - 2);
543 EXPECT_TRUE(FPI.getFunctionEmbedding().approximatelyEquals(
544 createEmbedder(*F1)->getFunctionVector()));
545 EXPECT_EQ(FPI, FunctionPropertiesInfo::getFunctionPropertiesInfo(*F1, FAM));
546 }
547
548 TEST_F(FunctionPropertiesAnalysisTest, Rethrow) {
549 LLVMContext C;
550 std::unique_ptr<Module> M = makeLLVMModule(C,
551 R"IR(
552 declare void @might_throw()
553
554 define internal i32 @callee() personality i32 (...)* @__gxx_personality_v0 {
555 entry:
556 invoke void @might_throw()
557 to label %cont unwind label %exc
558
559 cont:
560 ret i32 0
561
562 exc:
563 %exn = landingpad {i8*, i32}
564 cleanup
565 resume { i8*, i32 } %exn
566 }
567
568 define i32 @caller() personality i32 (...)* @__gxx_personality_v0 {
569 entry:
570 %X = invoke i32 @callee()
571 to label %cont unwind label %Handler
572
573 cont:
574 ret i32 %X
575
576 Handler:
577 %exn = landingpad {i8*, i32}
578 cleanup
579 ret i32 1
580 }
581
582 declare i32 @__gxx_personality_v0(...)
583 )IR");
584
585 Function *F1 = M->getFunction("caller");
586 CallBase *CB = findCall(*F1);
587 EXPECT_NE(CB, nullptr);
588
589 auto FPI = buildFPI(*F1);
590 FunctionPropertiesUpdater FPU(FPI, *CB);
591 InlineFunctionInfo IFI;
592 auto IR = llvm::InlineFunction(*CB, IFI);
593 EXPECT_TRUE(IR.isSuccess());
594 invalidate(*F1);
595 EXPECT_TRUE(FPU.finishAndTest(FAM));
596 EXPECT_EQ(static_cast<size_t>(FPI.BasicBlockCount), F1->size() - 1);
597 EXPECT_EQ(static_cast<size_t>(FPI.TotalInstructionCount),
598 F1->getInstructionCount() - 2);
599 EXPECT_EQ(FPI, FunctionPropertiesInfo::getFunctionPropertiesInfo(*F1, FAM));
600 }
601
602 TEST_F(FunctionPropertiesAnalysisTest, LPadChanges) {
603 LLVMContext C;
604 std::unique_ptr<Module> M = makeLLVMModule(C,
605 R"IR(
606 declare void @external_func()
607
608 @exception_type1 = external global i8
609 @exception_type2 = external global i8
610
611
612 define internal void @inner() personality i8* null {
613 invoke void @external_func()
614 to label %cont unwind label %lpad
615 cont:
616 ret void
617 lpad:
618 %lp = landingpad i32
619 catch i8* @exception_type1
620 resume i32 %lp
621 }
622
623 define void @outer() personality i8* null {
624 invoke void @inner()
625 to label %cont unwind label %lpad
626 cont:
627 ret void
628 lpad:
629 %lp = landingpad i32
630 cleanup
631 catch i8* @exception_type2
632 resume i32 %lp
633 }
634
635 )IR");
636
637 Function *F1 = M->getFunction("outer");
638 CallBase *CB = findCall(*F1);
639 EXPECT_NE(CB, nullptr);
640
641 auto FPI = buildFPI(*F1);
642 FunctionPropertiesUpdater FPU(FPI, *CB);
643 InlineFunctionInfo IFI;
644 auto IR = llvm::InlineFunction(*CB, IFI);
645 EXPECT_TRUE(IR.isSuccess());
646 invalidate(*F1);
647 EXPECT_TRUE(FPU.finishAndTest(FAM));
648 EXPECT_EQ(static_cast<size_t>(FPI.BasicBlockCount), F1->size() - 1);
649 EXPECT_EQ(static_cast<size_t>(FPI.TotalInstructionCount),
650 F1->getInstructionCount() - 2);
651 EXPECT_TRUE(FPI.getFunctionEmbedding().approximatelyEquals(
652 createEmbedder(*F1)->getFunctionVector()));
653 EXPECT_EQ(FPI, FunctionPropertiesInfo::getFunctionPropertiesInfo(*F1, FAM));
654 }
655
656 TEST_F(FunctionPropertiesAnalysisTest, LPadChangesConditional) {
657 LLVMContext C;
658 std::unique_ptr<Module> M = makeLLVMModule(C,
659 R"IR(
660 declare void @external_func()
661
662 @exception_type1 = external global i8
663 @exception_type2 = external global i8
664
665
666 define internal void @inner() personality i8* null {
667 invoke void @external_func()
668 to label %cont unwind label %lpad
669 cont:
670 ret void
671 lpad:
672 %lp = landingpad i32
673 catch i8* @exception_type1
674 resume i32 %lp
675 }
676
677 define void @outer(i32 %a) personality i8* null {
678 entry:
679 %i = icmp slt i32 %a, 0
680 br i1 %i, label %if.then, label %cont
681 if.then:
682 invoke void @inner()
683 to label %cont unwind label %lpad
684 cont:
685 ret void
686 lpad:
687 %lp = landingpad i32
688 cleanup
689 catch i8* @exception_type2
690 resume i32 %lp
691 }
692
693 )IR");
694
695 Function *F1 = M->getFunction("outer");
696 CallBase *CB = findCall(*F1);
697 EXPECT_NE(CB, nullptr);
698
699 auto FPI = buildFPI(*F1);
700 FunctionPropertiesUpdater FPU(FPI, *CB);
701 InlineFunctionInfo IFI;
702 auto IR = llvm::InlineFunction(*CB, IFI);
703 EXPECT_TRUE(IR.isSuccess());
704 invalidate(*F1);
705 EXPECT_TRUE(FPU.finishAndTest(FAM));
706 EXPECT_EQ(static_cast<size_t>(FPI.BasicBlockCount), F1->size() - 1);
707 EXPECT_EQ(static_cast<size_t>(FPI.TotalInstructionCount),
708 F1->getInstructionCount() - 2);
709 EXPECT_TRUE(FPI.getFunctionEmbedding().approximatelyEquals(
710 createEmbedder(*F1)->getFunctionVector()));
711 EXPECT_EQ(FPI, FunctionPropertiesInfo::getFunctionPropertiesInfo(*F1, FAM));
712 }
713
714 TEST_F(FunctionPropertiesAnalysisTest, InlineSameLoopBB) {
715 LLVMContext C;
716 std::unique_ptr<Module> M = makeLLVMModule(C,
717 R"IR(
718 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
719 target triple = "x86_64-pc-linux-gnu"
720
721 declare i32 @a()
722 declare i32 @b()
723
724 define i32 @f1(i32 %a) {
725 entry:
726 br label %loop
727 loop:
728 %i = call i32 @f2(i32 %a)
729 %c = icmp slt i32 %i, %a
730 br i1 %c, label %loop, label %end
731 end:
732 %r = phi i32 [%i, %loop], [%a, %entry]
733 ret i32 %r
734 }
735
736 define i32 @f2(i32 %a) {
737 %cnd = icmp slt i32 %a, 0
738 br i1 %cnd, label %then, label %else
739 then:
740 %r1 = call i32 @a()
741 br label %end
742 else:
743 %r2 = call i32 @b()
744 br label %end
745 end:
746 %r = phi i32 [%r1, %then], [%r2, %else]
747 ret i32 %r
748 }
749 )IR");
750
751 Function *F1 = M->getFunction("f1");
752 CallBase *CB = findCall(*F1);
753 EXPECT_NE(CB, nullptr);
754
755 FunctionPropertiesInfo ExpectedInitial;
756 ExpectedInitial.BasicBlockCount = 3;
757 ExpectedInitial.TotalInstructionCount = 6;
758 ExpectedInitial.BlocksReachedFromConditionalInstruction = 2;
759 ExpectedInitial.Uses = 1;
760 ExpectedInitial.DirectCallsToDefinedFunctions = 1;
761 ExpectedInitial.MaxLoopDepth = 1;
762 ExpectedInitial.TopLevelLoopCount = 1;
763 ExpectedInitial.setFunctionEmbeddingForTest(
764 createEmbedder(*F1)->getFunctionVector());
765
766 FunctionPropertiesInfo ExpectedFinal = ExpectedInitial;
767 ExpectedFinal.BasicBlockCount = 6;
768 ExpectedFinal.DirectCallsToDefinedFunctions = 0;
769 ExpectedFinal.BlocksReachedFromConditionalInstruction = 4;
770 ExpectedFinal.TotalInstructionCount = 12;
771
772 auto FPI = buildFPI(*F1);
773 EXPECT_EQ(FPI, ExpectedInitial);
774
775 FunctionPropertiesUpdater FPU(FPI, *CB);
776 InlineFunctionInfo IFI;
777 auto IR = llvm::InlineFunction(*CB, IFI);
778 EXPECT_TRUE(IR.isSuccess());
779 invalidate(*F1);
780 EXPECT_TRUE(FPU.finishAndTest(FAM));
781
782 ExpectedFinal.setFunctionEmbeddingForTest(
783 createEmbedder(*F1)->getFunctionVector());
784 EXPECT_EQ(FPI, ExpectedFinal);
785 }
786
787 TEST_F(FunctionPropertiesAnalysisTest, Unreachable) {
788 LLVMContext C;
789 std::unique_ptr<Module> M = makeLLVMModule(C,
790 R"IR(
791 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
792 target triple = "x86_64-pc-linux-gnu"
793
794 define i64 @f1(i32 noundef %value) {
795 entry:
796 br i1 true, label %cond.true, label %cond.false
797
798 cond.true: ; preds = %entry
799 %conv2 = sext i32 %value to i64
800 br label %cond.end
801
802 cond.false: ; preds = %entry
803 %call3 = call noundef i64 @f2()
804 br label %extra
805
806 extra:
807 br label %extra2
808
809 extra2:
810 br label %cond.end
811
812 cond.end: ; preds = %extra2, %cond.true
813 %cond = phi i64 [ %conv2, %cond.true ], [ %call3, %extra ]
814 ret i64 %cond
815 }
816
817 define i64 @f2() {
818 entry:
819 tail call void @llvm.trap()
820 unreachable
821 }
822
823 declare void @llvm.trap()
824 )IR");
825
826 Function *F1 = M->getFunction("f1");
827 CallBase *CB = findCall(*F1);
828 EXPECT_NE(CB, nullptr);
829
830 FunctionPropertiesInfo ExpectedInitial;
831 ExpectedInitial.BasicBlockCount = 6;
832 ExpectedInitial.TotalInstructionCount = 9;
833 ExpectedInitial.BlocksReachedFromConditionalInstruction = 2;
834 ExpectedInitial.Uses = 1;
835 ExpectedInitial.DirectCallsToDefinedFunctions = 1;
836 ExpectedInitial.setFunctionEmbeddingForTest(
837 createEmbedder(*F1)->getFunctionVector());
838
839 FunctionPropertiesInfo ExpectedFinal = ExpectedInitial;
840 ExpectedFinal.BasicBlockCount = 4;
841 ExpectedFinal.DirectCallsToDefinedFunctions = 0;
842 ExpectedFinal.TotalInstructionCount = 7;
843
844 auto FPI = buildFPI(*F1);
845 EXPECT_EQ(FPI, ExpectedInitial);
846
847 FunctionPropertiesUpdater FPU(FPI, *CB);
848 InlineFunctionInfo IFI;
849 auto IR = llvm::InlineFunction(*CB, IFI);
850 EXPECT_TRUE(IR.isSuccess());
851 invalidate(*F1);
852 EXPECT_TRUE(FPU.finishAndTest(FAM));
853
854 ExpectedFinal.setFunctionEmbeddingForTest(
855 createEmbedder(*F1)->getFunctionVector());
856 EXPECT_EQ(FPI, ExpectedFinal);
857 }
858
859 TEST_F(FunctionPropertiesAnalysisTest, InvokeSkipLP) {
860 LLVMContext C;
861 std::unique_ptr<Module> M = makeLLVMModule(C,
862 R"IR(
863 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
864 target triple = "x86_64-pc-linux-gnu"
865
866 define i64 @f1(i32 noundef %value) {
867 entry:
868 invoke fastcc void @f2() to label %cont unwind label %lpad
869 cont:
870 ret i64 1
871 lpad:
872 %lp = landingpad i32 cleanup
873 br label %ehcleanup
874 ehcleanup:
875 resume i32 0
876 }
877 define void @f2() {
878 invoke noundef void @f3() to label %exit unwind label %lpad
879 exit:
880 ret void
881 lpad:
882 %lp = landingpad i32 cleanup
883 resume i32 %lp
884 }
885 declare void @f3()
886 )IR");
887
888 // The outcome of inlining will be that lpad becomes unreachable. The landing
889 // pad of the invoke inherited from f2 will land on a new bb which will branch
890 // to a bb containing the body of lpad.
891 Function *F1 = M->getFunction("f1");
892 CallBase *CB = findCall(*F1);
893 EXPECT_NE(CB, nullptr);
894
895 FunctionPropertiesInfo ExpectedInitial;
896 ExpectedInitial.BasicBlockCount = 4;
897 ExpectedInitial.TotalInstructionCount = 5;
898 ExpectedInitial.BlocksReachedFromConditionalInstruction = 0;
899 ExpectedInitial.Uses = 1;
900 ExpectedInitial.DirectCallsToDefinedFunctions = 1;
901 ExpectedInitial.setFunctionEmbeddingForTest(
902 createEmbedder(*F1)->getFunctionVector());
903
904 FunctionPropertiesInfo ExpectedFinal = ExpectedInitial;
905 ExpectedFinal.BasicBlockCount = 6;
906 ExpectedFinal.DirectCallsToDefinedFunctions = 0;
907 ExpectedFinal.TotalInstructionCount = 8;
908
909 auto FPI = buildFPI(*F1);
910 EXPECT_EQ(FPI, ExpectedInitial);
911
912 FunctionPropertiesUpdater FPU(FPI, *CB);
913 InlineFunctionInfo IFI;
914 auto IR = llvm::InlineFunction(*CB, IFI);
915 EXPECT_TRUE(IR.isSuccess());
916 invalidate(*F1);
917 EXPECT_TRUE(FPU.finishAndTest(FAM));
918
919 ExpectedFinal.setFunctionEmbeddingForTest(
920 createEmbedder(*F1)->getFunctionVector());
921 EXPECT_EQ(FPI, ExpectedFinal);
922 }
923
924 TEST_F(FunctionPropertiesAnalysisTest, DetailedOperandCount) {
925 LLVMContext C;
926 std::unique_ptr<Module> M = makeLLVMModule(C,
927 R"IR(
928 @a = global i64 1
929
930 define i64 @f1(i64 %e) {
931 %b = load i64, i64* @a
932 %c = add i64 %b, 2
933 %d = call i64 asm "mov $1,$0", "=r,r" (i64 %c)
934 %f = add i64 %d, %e
935 ret i64 %f
936 }
937 )IR");
938
939 Function *F1 = M->getFunction("f1");
940 EnableDetailedFunctionProperties.setValue(true);
941 FunctionPropertiesInfo DetailedF1Properties = buildFPI(*F1);
942 EXPECT_EQ(DetailedF1Properties.BasicBlocksWithSingleSuccessor, 0);
943 EXPECT_EQ(DetailedF1Properties.BasicBlocksWithTwoSuccessors, 0);
944 EXPECT_EQ(DetailedF1Properties.BasicBlocksWithMoreThanTwoSuccessors, 0);
945 EXPECT_EQ(DetailedF1Properties.BasicBlocksWithSinglePredecessor, 0);
946 EXPECT_EQ(DetailedF1Properties.BasicBlocksWithTwoPredecessors, 0);
947 EXPECT_EQ(DetailedF1Properties.BasicBlocksWithMoreThanTwoPredecessors, 0);
948 EXPECT_EQ(DetailedF1Properties.BigBasicBlocks, 0);
949 EXPECT_EQ(DetailedF1Properties.MediumBasicBlocks, 0);
950 EXPECT_EQ(DetailedF1Properties.SmallBasicBlocks, 1);
951 EXPECT_EQ(DetailedF1Properties.CastInstructionCount, 0);
952 EXPECT_EQ(DetailedF1Properties.FloatingPointInstructionCount, 0);
953 EXPECT_EQ(DetailedF1Properties.IntegerInstructionCount, 4);
954 EXPECT_EQ(DetailedF1Properties.ConstantIntOperandCount, 1);
955 EXPECT_EQ(DetailedF1Properties.ConstantFPOperandCount, 0);
956 EXPECT_EQ(DetailedF1Properties.ConstantOperandCount, 0);
957 EXPECT_EQ(DetailedF1Properties.InstructionOperandCount, 4);
958 EXPECT_EQ(DetailedF1Properties.BasicBlockOperandCount, 0);
959 EXPECT_EQ(DetailedF1Properties.GlobalValueOperandCount, 1);
960 EXPECT_EQ(DetailedF1Properties.InlineAsmOperandCount, 1);
961 EXPECT_EQ(DetailedF1Properties.ArgumentOperandCount, 1);
962 EXPECT_EQ(DetailedF1Properties.UnknownOperandCount, 0);
963 EXPECT_EQ(DetailedF1Properties.CriticalEdgeCount, 0);
964 EXPECT_EQ(DetailedF1Properties.ControlFlowEdgeCount, 0);
965 EXPECT_EQ(DetailedF1Properties.UnconditionalBranchCount, 0);
966 EXPECT_EQ(DetailedF1Properties.IntrinsicCount, 0);
967 EXPECT_EQ(DetailedF1Properties.DirectCallCount, 1);
968 EXPECT_EQ(DetailedF1Properties.IndirectCallCount, 0);
969 EXPECT_EQ(DetailedF1Properties.CallReturnsIntegerCount, 1);
970 EXPECT_EQ(DetailedF1Properties.CallReturnsFloatCount, 0);
971 EXPECT_EQ(DetailedF1Properties.CallReturnsPointerCount, 0);
972 EXPECT_EQ(DetailedF1Properties.CallWithManyArgumentsCount, 0);
973 EXPECT_EQ(DetailedF1Properties.CallWithPointerArgumentCount, 0);
974 EXPECT_TRUE(DetailedF1Properties.getFunctionEmbedding().approximatelyEquals(
975 createEmbedder(*F1)->getFunctionVector()));
976 EnableDetailedFunctionProperties.setValue(false);
977 }
978
979 TEST_F(FunctionPropertiesAnalysisTest, IntrinsicCount) {
980 LLVMContext C;
981 std::unique_ptr<Module> M = makeLLVMModule(C,
982 R"IR(
983 define float @f1(float %a) {
984 %b = call float @llvm.cos.f32(float %a)
985 ret float %b
986 }
987 declare float @llvm.cos.f32(float)
988 )IR");
989
990 Function *F1 = M->getFunction("f1");
991 EnableDetailedFunctionProperties.setValue(true);
992 FunctionPropertiesInfo DetailedF1Properties = buildFPI(*F1);
993 EXPECT_EQ(DetailedF1Properties.IntrinsicCount, 1);
994 EXPECT_EQ(DetailedF1Properties.DirectCallCount, 1);
995 EXPECT_EQ(DetailedF1Properties.IndirectCallCount, 0);
996 EXPECT_EQ(DetailedF1Properties.CallReturnsIntegerCount, 0);
997 EXPECT_EQ(DetailedF1Properties.CallReturnsFloatCount, 1);
998 EXPECT_EQ(DetailedF1Properties.CallReturnsPointerCount, 0);
999 EXPECT_EQ(DetailedF1Properties.CallWithManyArgumentsCount, 0);
1000 EXPECT_EQ(DetailedF1Properties.CallWithPointerArgumentCount, 0);
1001 EXPECT_TRUE(DetailedF1Properties.getFunctionEmbedding().approximatelyEquals(
1002 createEmbedder(*F1)->getFunctionVector()));
1003 EnableDetailedFunctionProperties.setValue(false);
1004 }
1005
1006 TEST_F(FunctionPropertiesAnalysisTest, FunctionCallMetrics) {
1007 LLVMContext C;
1008 std::unique_ptr<Module> M = makeLLVMModule(C,
1009 R"IR(
1010 define i64 @f1(i64 %a) {
1011 %b = call i64 @f2(i64 %a, i64 %a, i64 %a, i64 %a, i64 %a)
1012 %c = call ptr @f3()
1013 call void @f4(ptr %c)
1014 %d = call float @f5()
1015 %e = call i64 %c(i64 %b)
1016 ret i64 %b
1017 }
1018
1019 declare i64 @f2(i64,i64,i64,i64,i64)
1020 declare ptr @f3()
1021 declare void @f4(ptr)
1022 declare float @f5()
1023 )IR");
1024
1025 Function *F1 = M->getFunction("f1");
1026 EnableDetailedFunctionProperties.setValue(true);
1027 FunctionPropertiesInfo DetailedF1Properties = buildFPI(*F1);
1028 EXPECT_EQ(DetailedF1Properties.IntrinsicCount, 0);
1029 EXPECT_EQ(DetailedF1Properties.DirectCallCount, 4);
1030 EXPECT_EQ(DetailedF1Properties.IndirectCallCount, 1);
1031 EXPECT_EQ(DetailedF1Properties.CallReturnsIntegerCount, 2);
1032 EXPECT_EQ(DetailedF1Properties.CallReturnsFloatCount, 1);
1033 EXPECT_EQ(DetailedF1Properties.CallReturnsPointerCount, 1);
1034 EXPECT_EQ(DetailedF1Properties.CallWithManyArgumentsCount, 1);
1035 EXPECT_EQ(DetailedF1Properties.CallWithPointerArgumentCount, 1);
1036 EXPECT_TRUE(DetailedF1Properties.getFunctionEmbedding().approximatelyEquals(
1037 createEmbedder(*F1)->getFunctionVector()));
1038 EnableDetailedFunctionProperties.setValue(false);
1039 }
1040
1041 TEST_F(FunctionPropertiesAnalysisTest, CriticalEdge) {
1042 LLVMContext C;
1043 std::unique_ptr<Module> M = makeLLVMModule(C,
1044 R"IR(
1045 define i64 @f1(i64 %a) {
1046 %b = icmp eq i64 %a, 1
1047 br i1 %b, label %TopBlock1, label %TopBlock2
1048 TopBlock1:
1049 %c = add i64 %a, 1
1050 %e = icmp eq i64 %c, 2
1051 br i1 %e, label %BottomBlock1, label %BottomBlock2
1052 TopBlock2:
1053 %d = add i64 %a, 2
1054 br label %BottomBlock2
1055 BottomBlock1:
1056 ret i64 0
1057 BottomBlock2:
1058 %f = phi i64 [ %c, %TopBlock1 ], [ %d, %TopBlock2 ]
1059 ret i64 %f
1060 }
1061 )IR");
1062
1063 Function *F1 = M->getFunction("f1");
1064 EnableDetailedFunctionProperties.setValue(true);
1065 FunctionPropertiesInfo DetailedF1Properties = buildFPI(*F1);
1066 EXPECT_EQ(DetailedF1Properties.CriticalEdgeCount, 1);
1067 EXPECT_TRUE(DetailedF1Properties.getFunctionEmbedding().approximatelyEquals(
1068 createEmbedder(*F1)->getFunctionVector()));
1069 EnableDetailedFunctionProperties.setValue(false);
1070 }
1071
1072 TEST_F(FunctionPropertiesAnalysisTest, FunctionReturnVectors) {
1073 LLVMContext C;
1074 std::unique_ptr<Module> M = makeLLVMModule(C,
1075 R"IR(
1076 define <4 x i64> @f1(<4 x i64> %a) {
1077 %b = call <4 x i64> @f2()
1078 %c = call <4 x float> @f3()
1079 %d = call <4 x ptr> @f4()
1080 ret <4 x i64> %b
1081 }
1082
1083 declare <4 x i64> @f2()
1084 declare <4 x float> @f3()
1085 declare <4 x ptr> @f4()
1086 )IR");
1087
1088 Function *F1 = M->getFunction("f1");
1089 EnableDetailedFunctionProperties.setValue(true);
1090 FunctionPropertiesInfo DetailedF1Properties = buildFPI(*F1);
1091 EXPECT_EQ(DetailedF1Properties.CallReturnsVectorIntCount, 1);
1092 EXPECT_EQ(DetailedF1Properties.CallReturnsVectorFloatCount, 1);
1093 EXPECT_EQ(DetailedF1Properties.CallReturnsVectorPointerCount, 1);
1094 EXPECT_TRUE(DetailedF1Properties.getFunctionEmbedding().approximatelyEquals(
1095 createEmbedder(*F1)->getFunctionVector()));
1096 EnableDetailedFunctionProperties.setValue(false);
1097 }
1098
1099 TEST_F(FunctionPropertiesAnalysisTest, ReAddEdges) {
1100 LLVMContext C;
1101 std::unique_ptr<Module> M = makeLLVMModule(C, R"IR(
1102 define hidden void @f1(ptr noundef %destatep, i32 noundef %offset, i8 noundef zeroext %byte1) {
1103 entry:
1104 %cmp = icmp eq i8 %byte1, 0
1105 br i1 %cmp, label %if.then, label %if.else
1106
1107 if.then: ; preds = %entry
1108 call fastcc void @f2(ptr noundef %destatep, i32 noundef 37, i32 noundef 600)
1109 %and = and i32 %offset, 3
1110 switch i32 %and, label %default.unreachable [
1111 i32 0, label %sw.bb
1112 i32 1, label %sw.bb1
1113 i32 2, label %sw.bb1
1114 i32 3, label %if.end
1115 ]
1116
1117 sw.bb: ; preds = %if.then
1118 call fastcc void @f2(ptr noundef %destatep, i32 noundef 57, i32 noundef 600)
1119 br label %if.end
1120
1121 sw.bb1: ; preds = %if.then, %if.then
1122 call fastcc void @f2(ptr noundef %destatep, i32 noundef 56, i32 noundef 600) #34
1123 br label %if.end
1124
1125 default.unreachable: ; preds = %if.then
1126 unreachable
1127
1128 if.else: ; preds = %entry
1129 call fastcc void @f2(ptr noundef %destatep, i32 noundef 56, i32 noundef 600)
1130 br label %if.end
1131
1132 if.end: ; preds = %sw.bb, %sw.bb1, %if.then, %if.else
1133 ret void
1134 }
1135
1136 define internal fastcc void @f2(ptr nocapture noundef %destatep, i32 noundef %r_enc, i32 noundef %whack) {
1137 entry:
1138 %enc_prob = getelementptr inbounds nuw i8, ptr %destatep, i32 512
1139 %arrayidx = getelementptr inbounds [67 x i32], ptr %enc_prob, i32 0, i32 %r_enc
1140 %0 = load i32, ptr %arrayidx, align 4
1141 %sub = sub nsw i32 %0, %whack
1142 store i32 %sub, ptr %arrayidx, align 4
1143 ret void
1144 }
1145 )IR");
1146 auto *F1 = M->getFunction("f1");
1147 auto *F2 = M->getFunction("f2");
1148 auto *CB = [&]() -> CallBase * {
1149 for (auto &BB : *F1)
1150 for (auto &I : BB)
1151 if (auto *CB = dyn_cast<CallBase>(&I);
1152 CB && CB->getCalledFunction() && CB->getCalledFunction() == F2)
1153 return CB;
1154 return nullptr;
1155 }();
1156 ASSERT_NE(CB, nullptr);
1157 auto FPI = buildFPI(*F1);
1158 FunctionPropertiesUpdater FPU(FPI, *CB);
1159 InlineFunctionInfo IFI;
1160 auto IR = llvm::InlineFunction(*CB, IFI);
1161 EXPECT_TRUE(IR.isSuccess());
1162 invalidate(*F1);
1163 EXPECT_TRUE(FPU.finishAndTest(FAM));
1164 }
1165
1166 TEST_F(FunctionPropertiesAnalysisTest, InvokeLandingCanStillBeReached) {
1167 LLVMContext C;
1168 // %lpad is reachable from a block not involved in the inlining decision. We
1169 // make sure that's not the entry - otherwise the DT will be recomputed from
1170 // scratch. The idea here is that the edge known to the inliner to potentially
1171 // disappear - %lpad->%ehcleanup -should survive because it is still reachable
1172 // from %middle.
1173 std::unique_ptr<Module> M = makeLLVMModule(C,
1174 R"IR(
1175 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
1176 target triple = "x86_64-pc-linux-gnu"
1177
1178 define i64 @f1(i32 noundef %value) {
1179 entry:
1180 br label %middle
1181 middle:
1182 %c = icmp eq i32 %value, 0
1183 br i1 %c, label %invoke, label %lpad
1184 invoke:
1185 invoke fastcc void @f2() to label %cont unwind label %lpad
1186 cont:
1187 br label %exit
1188 lpad:
1189 %lp = landingpad i32 cleanup
1190 br label %ehcleanup
1191 ehcleanup:
1192 resume i32 0
1193 exit:
1194 ret i64 1
1195 }
1196 define void @f2() {
1197 ret void
1198 }
1199 )IR");
1200
1201 Function *F1 = M->getFunction("f1");
1202 CallBase *CB = findCall(*F1);
1203 EXPECT_NE(CB, nullptr);
1204
1205 auto FPI = buildFPI(*F1);
1206 FunctionPropertiesUpdater FPU(FPI, *CB);
1207 InlineFunctionInfo IFI;
1208 auto IR = llvm::InlineFunction(*CB, IFI);
1209 EXPECT_TRUE(IR.isSuccess());
1210 invalidate(*F1);
1211 EXPECT_TRUE(FPU.finishAndTest(FAM));
1212 }
1213 } // end anonymous namespace