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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
|
//===- FunctionPropertiesAnalysisTest.cpp - Function Properties Unit Tests-===//
//
// 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/Analysis/FunctionPropertiesAnalysis.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/StandardInstrumentations.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "gtest/gtest.h"
#include <cstring>
using namespace llvm;
namespace llvm {
extern cl::opt<bool> EnableDetailedFunctionProperties;
extern cl::opt<bool> BigBasicBlockInstructionThreshold;
extern cl::opt<bool> MediumBasicBlockInstrutionThreshold;
} // namespace llvm
namespace {
class FunctionPropertiesAnalysisTest : public testing::Test {
public:
FunctionPropertiesAnalysisTest() {
FAM.registerPass([&] { return DominatorTreeAnalysis(); });
FAM.registerPass([&] { return LoopAnalysis(); });
FAM.registerPass([&] { return PassInstrumentationAnalysis(); });
}
protected:
std::unique_ptr<DominatorTree> DT;
std::unique_ptr<LoopInfo> LI;
FunctionAnalysisManager FAM;
FunctionPropertiesInfo buildFPI(Function &F) {
return FunctionPropertiesInfo::getFunctionPropertiesInfo(F, FAM);
}
void invalidate(Function &F) {
PreservedAnalyses PA = PreservedAnalyses::none();
FAM.invalidate(F, PA);
}
std::unique_ptr<Module> makeLLVMModule(LLVMContext &C, const char *IR) {
SMDiagnostic Err;
std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
if (!Mod)
Err.print("MLAnalysisTests", errs());
return Mod;
}
CallBase* findCall(Function& F, const char* Name = nullptr) {
for (auto &BB : F)
for (auto &I : BB )
if (auto *CB = dyn_cast<CallBase>(&I))
if (!Name || CB->getName() == Name)
return CB;
return nullptr;
}
};
TEST_F(FunctionPropertiesAnalysisTest, BasicTest) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
declare i32 @f1(i32)
declare i32 @f2(i32)
define i32 @branches(i32) {
%cond = icmp slt i32 %0, 3
br i1 %cond, label %then, label %else
then:
%ret.1 = call i32 @f1(i32 %0)
br label %last.block
else:
%ret.2 = call i32 @f2(i32 %0)
br label %last.block
last.block:
%ret = phi i32 [%ret.1, %then], [%ret.2, %else]
ret i32 %ret
}
define internal i32 @top() {
%1 = call i32 @branches(i32 2)
%2 = call i32 @f1(i32 %1)
ret i32 %2
}
)IR");
Function *BranchesFunction = M->getFunction("branches");
FunctionPropertiesInfo BranchesFeatures = buildFPI(*BranchesFunction);
EXPECT_EQ(BranchesFeatures.BasicBlockCount, 4);
EXPECT_EQ(BranchesFeatures.BlocksReachedFromConditionalInstruction, 2);
// 2 Users: top is one. The other is added because @branches is not internal,
// so it may have external callers.
EXPECT_EQ(BranchesFeatures.Uses, 2);
EXPECT_EQ(BranchesFeatures.DirectCallsToDefinedFunctions, 0);
EXPECT_EQ(BranchesFeatures.LoadInstCount, 0);
EXPECT_EQ(BranchesFeatures.StoreInstCount, 0);
EXPECT_EQ(BranchesFeatures.MaxLoopDepth, 0);
EXPECT_EQ(BranchesFeatures.TopLevelLoopCount, 0);
Function *TopFunction = M->getFunction("top");
FunctionPropertiesInfo TopFeatures = buildFPI(*TopFunction);
EXPECT_EQ(TopFeatures.BasicBlockCount, 1);
EXPECT_EQ(TopFeatures.BlocksReachedFromConditionalInstruction, 0);
EXPECT_EQ(TopFeatures.Uses, 0);
EXPECT_EQ(TopFeatures.DirectCallsToDefinedFunctions, 1);
EXPECT_EQ(BranchesFeatures.LoadInstCount, 0);
EXPECT_EQ(BranchesFeatures.StoreInstCount, 0);
EXPECT_EQ(BranchesFeatures.MaxLoopDepth, 0);
EXPECT_EQ(BranchesFeatures.TopLevelLoopCount, 0);
EnableDetailedFunctionProperties.setValue(true);
FunctionPropertiesInfo DetailedBranchesFeatures = buildFPI(*BranchesFunction);
EXPECT_EQ(DetailedBranchesFeatures.BasicBlocksWithSingleSuccessor, 2);
EXPECT_EQ(DetailedBranchesFeatures.BasicBlocksWithTwoSuccessors, 1);
EXPECT_EQ(DetailedBranchesFeatures.BasicBlocksWithMoreThanTwoSuccessors, 0);
EXPECT_EQ(DetailedBranchesFeatures.BasicBlocksWithSinglePredecessor, 2);
EXPECT_EQ(DetailedBranchesFeatures.BasicBlocksWithTwoPredecessors, 1);
EXPECT_EQ(DetailedBranchesFeatures.BasicBlocksWithMoreThanTwoPredecessors, 0);
EXPECT_EQ(DetailedBranchesFeatures.BigBasicBlocks, 0);
EXPECT_EQ(DetailedBranchesFeatures.MediumBasicBlocks, 0);
EXPECT_EQ(DetailedBranchesFeatures.SmallBasicBlocks, 4);
EXPECT_EQ(DetailedBranchesFeatures.CastInstructionCount, 0);
EXPECT_EQ(DetailedBranchesFeatures.FloatingPointInstructionCount, 0);
EXPECT_EQ(DetailedBranchesFeatures.IntegerInstructionCount, 4);
EXPECT_EQ(DetailedBranchesFeatures.ConstantIntOperandCount, 1);
EXPECT_EQ(DetailedBranchesFeatures.ConstantFPOperandCount, 0);
EXPECT_EQ(DetailedBranchesFeatures.ConstantOperandCount, 0);
EXPECT_EQ(DetailedBranchesFeatures.InstructionOperandCount, 4);
EXPECT_EQ(DetailedBranchesFeatures.BasicBlockOperandCount, 4);
EXPECT_EQ(DetailedBranchesFeatures.GlobalValueOperandCount, 2);
EXPECT_EQ(DetailedBranchesFeatures.InlineAsmOperandCount, 0);
EXPECT_EQ(DetailedBranchesFeatures.ArgumentOperandCount, 3);
EXPECT_EQ(DetailedBranchesFeatures.UnknownOperandCount, 0);
EXPECT_EQ(DetailedBranchesFeatures.CriticalEdgeCount, 0);
EXPECT_EQ(DetailedBranchesFeatures.ControlFlowEdgeCount, 4);
EXPECT_EQ(DetailedBranchesFeatures.UnconditionalBranchCount, 2);
EXPECT_EQ(DetailedBranchesFeatures.IntrinsicCount, 0);
EXPECT_EQ(DetailedBranchesFeatures.DirectCallCount, 2);
EXPECT_EQ(DetailedBranchesFeatures.IndirectCallCount, 0);
EXPECT_EQ(DetailedBranchesFeatures.CallReturnsIntegerCount, 2);
EXPECT_EQ(DetailedBranchesFeatures.CallReturnsFloatCount, 0);
EXPECT_EQ(DetailedBranchesFeatures.CallReturnsPointerCount, 0);
EXPECT_EQ(DetailedBranchesFeatures.CallWithManyArgumentsCount, 0);
EXPECT_EQ(DetailedBranchesFeatures.CallWithPointerArgumentCount, 0);
EnableDetailedFunctionProperties.setValue(false);
}
TEST_F(FunctionPropertiesAnalysisTest, DifferentPredecessorSuccessorCounts) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
define i64 @f1() {
br i1 0, label %br1, label %finally
br1:
ret i64 0
finally:
ret i64 3
}
)IR");
Function *F1 = M->getFunction("f1");
EnableDetailedFunctionProperties.setValue(true);
FunctionPropertiesInfo DetailedF1Properties = buildFPI(*F1);
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithSingleSuccessor, 0);
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithTwoSuccessors, 1);
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithMoreThanTwoSuccessors, 0);
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithSinglePredecessor, 2);
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithTwoPredecessors, 0);
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithMoreThanTwoPredecessors, 0);
EXPECT_EQ(DetailedF1Properties.BigBasicBlocks, 0);
EXPECT_EQ(DetailedF1Properties.MediumBasicBlocks, 0);
EXPECT_EQ(DetailedF1Properties.SmallBasicBlocks, 3);
EXPECT_EQ(DetailedF1Properties.CastInstructionCount, 0);
EXPECT_EQ(DetailedF1Properties.FloatingPointInstructionCount, 0);
EXPECT_EQ(DetailedF1Properties.IntegerInstructionCount, 0);
EXPECT_EQ(DetailedF1Properties.ConstantIntOperandCount, 3);
EXPECT_EQ(DetailedF1Properties.ConstantFPOperandCount, 0);
EXPECT_EQ(DetailedF1Properties.ConstantOperandCount, 0);
EXPECT_EQ(DetailedF1Properties.InstructionOperandCount, 0);
EXPECT_EQ(DetailedF1Properties.BasicBlockOperandCount, 2);
EXPECT_EQ(DetailedF1Properties.GlobalValueOperandCount, 0);
EXPECT_EQ(DetailedF1Properties.InlineAsmOperandCount, 0);
EXPECT_EQ(DetailedF1Properties.ArgumentOperandCount, 0);
EXPECT_EQ(DetailedF1Properties.UnknownOperandCount, 0);
EXPECT_EQ(DetailedF1Properties.CriticalEdgeCount, 0);
EXPECT_EQ(DetailedF1Properties.ControlFlowEdgeCount, 2);
EXPECT_EQ(DetailedF1Properties.UnconditionalBranchCount, 0);
EXPECT_EQ(DetailedF1Properties.IntrinsicCount, 0);
EXPECT_EQ(DetailedF1Properties.DirectCallCount, 0);
EXPECT_EQ(DetailedF1Properties.IndirectCallCount, 0);
EXPECT_EQ(DetailedF1Properties.CallReturnsIntegerCount, 0);
EXPECT_EQ(DetailedF1Properties.CallReturnsFloatCount, 0);
EXPECT_EQ(DetailedF1Properties.CallReturnsPointerCount, 0);
EXPECT_EQ(DetailedF1Properties.CallWithManyArgumentsCount, 0);
EXPECT_EQ(DetailedF1Properties.CallWithPointerArgumentCount, 0);
EnableDetailedFunctionProperties.setValue(false);
}
TEST_F(FunctionPropertiesAnalysisTest, InlineSameBBSimple) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
define i32 @f1(i32 %a) {
%b = call i32 @f2(i32 %a)
%c = add i32 %b, 2
ret i32 %c
}
define i32 @f2(i32 %a) {
%b = add i32 %a, 1
ret i32 %b
}
)IR");
Function *F1 = M->getFunction("f1");
CallBase* CB = findCall(*F1, "b");
EXPECT_NE(CB, nullptr);
FunctionPropertiesInfo ExpectedInitial;
ExpectedInitial.BasicBlockCount = 1;
ExpectedInitial.TotalInstructionCount = 3;
ExpectedInitial.Uses = 1;
ExpectedInitial.DirectCallsToDefinedFunctions = 1;
FunctionPropertiesInfo ExpectedFinal = ExpectedInitial;
ExpectedFinal.DirectCallsToDefinedFunctions = 0;
auto FPI = buildFPI(*F1);
EXPECT_EQ(FPI, ExpectedInitial);
FunctionPropertiesUpdater FPU(FPI, *CB);
InlineFunctionInfo IFI;
auto IR = llvm::InlineFunction(*CB, IFI);
EXPECT_TRUE(IR.isSuccess());
invalidate(*F1);
EXPECT_TRUE(FPU.finishAndTest(FAM));
EXPECT_EQ(FPI, ExpectedFinal);
}
TEST_F(FunctionPropertiesAnalysisTest, InlineSameBBLargerCFG) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
define i32 @f1(i32 %a) {
entry:
%i = icmp slt i32 %a, 0
br i1 %i, label %if.then, label %if.else
if.then:
%b = call i32 @f2(i32 %a)
%c1 = add i32 %b, 2
br label %end
if.else:
%c2 = add i32 %a, 1
br label %end
end:
%ret = phi i32 [%c1, %if.then],[%c2, %if.else]
ret i32 %ret
}
define i32 @f2(i32 %a) {
%b = add i32 %a, 1
ret i32 %b
}
)IR");
Function *F1 = M->getFunction("f1");
CallBase* CB = findCall(*F1, "b");
EXPECT_NE(CB, nullptr);
FunctionPropertiesInfo ExpectedInitial;
ExpectedInitial.BasicBlockCount = 4;
ExpectedInitial.BlocksReachedFromConditionalInstruction = 2;
ExpectedInitial.TotalInstructionCount = 9;
ExpectedInitial.Uses = 1;
ExpectedInitial.DirectCallsToDefinedFunctions = 1;
FunctionPropertiesInfo ExpectedFinal = ExpectedInitial;
ExpectedFinal.DirectCallsToDefinedFunctions = 0;
auto FPI = buildFPI(*F1);
EXPECT_EQ(FPI, ExpectedInitial);
FunctionPropertiesUpdater FPU(FPI, *CB);
InlineFunctionInfo IFI;
auto IR = llvm::InlineFunction(*CB, IFI);
EXPECT_TRUE(IR.isSuccess());
invalidate(*F1);
EXPECT_TRUE(FPU.finishAndTest(FAM));
EXPECT_EQ(FPI, ExpectedFinal);
}
TEST_F(FunctionPropertiesAnalysisTest, InlineSameBBLoops) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
define i32 @f1(i32 %a) {
entry:
%i = icmp slt i32 %a, 0
br i1 %i, label %if.then, label %if.else
if.then:
%b = call i32 @f2(i32 %a)
%c1 = add i32 %b, 2
br label %end
if.else:
%c2 = add i32 %a, 1
br label %end
end:
%ret = phi i32 [%c1, %if.then],[%c2, %if.else]
ret i32 %ret
}
define i32 @f2(i32 %a) {
entry:
br label %loop
loop:
%indvar = phi i32 [%indvar.next, %loop], [0, %entry]
%b = add i32 %a, %indvar
%indvar.next = add i32 %indvar, 1
%cond = icmp slt i32 %indvar.next, %a
br i1 %cond, label %loop, label %exit
exit:
ret i32 %b
}
)IR");
Function *F1 = M->getFunction("f1");
CallBase* CB = findCall(*F1, "b");
EXPECT_NE(CB, nullptr);
FunctionPropertiesInfo ExpectedInitial;
ExpectedInitial.BasicBlockCount = 4;
ExpectedInitial.BlocksReachedFromConditionalInstruction = 2;
ExpectedInitial.TotalInstructionCount = 9;
ExpectedInitial.Uses = 1;
ExpectedInitial.DirectCallsToDefinedFunctions = 1;
FunctionPropertiesInfo ExpectedFinal;
ExpectedFinal.BasicBlockCount = 6;
ExpectedFinal.BlocksReachedFromConditionalInstruction = 4;
ExpectedFinal.Uses = 1;
ExpectedFinal.MaxLoopDepth = 1;
ExpectedFinal.TopLevelLoopCount = 1;
ExpectedFinal.TotalInstructionCount = 14;
auto FPI = buildFPI(*F1);
EXPECT_EQ(FPI, ExpectedInitial);
FunctionPropertiesUpdater FPU(FPI, *CB);
InlineFunctionInfo IFI;
auto IR = llvm::InlineFunction(*CB, IFI);
EXPECT_TRUE(IR.isSuccess());
invalidate(*F1);
EXPECT_TRUE(FPU.finishAndTest(FAM));
EXPECT_EQ(FPI, ExpectedFinal);
}
TEST_F(FunctionPropertiesAnalysisTest, InvokeSimple) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
declare void @might_throw()
define internal void @callee() {
entry:
call void @might_throw()
ret void
}
define i32 @caller() personality i32 (...)* @__gxx_personality_v0 {
entry:
invoke void @callee()
to label %cont unwind label %exc
cont:
ret i32 0
exc:
%exn = landingpad {i8*, i32}
cleanup
ret i32 1
}
declare i32 @__gxx_personality_v0(...)
)IR");
Function *F1 = M->getFunction("caller");
CallBase* CB = findCall(*F1);
EXPECT_NE(CB, nullptr);
auto FPI = buildFPI(*F1);
FunctionPropertiesUpdater FPU(FPI, *CB);
InlineFunctionInfo IFI;
auto IR = llvm::InlineFunction(*CB, IFI);
EXPECT_TRUE(IR.isSuccess());
invalidate(*F1);
EXPECT_TRUE(FPU.finishAndTest(FAM));
EXPECT_EQ(static_cast<size_t>(FPI.BasicBlockCount), F1->size());
EXPECT_EQ(static_cast<size_t>(FPI.TotalInstructionCount),
F1->getInstructionCount());
}
TEST_F(FunctionPropertiesAnalysisTest, InvokeUnreachableHandler) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
declare void @might_throw()
define internal i32 @callee() personality i32 (...)* @__gxx_personality_v0 {
entry:
invoke void @might_throw()
to label %cont unwind label %exc
cont:
ret i32 0
exc:
%exn = landingpad {i8*, i32}
cleanup
resume { i8*, i32 } %exn
}
define i32 @caller() personality i32 (...)* @__gxx_personality_v0 {
entry:
%X = invoke i32 @callee()
to label %cont unwind label %Handler
cont:
ret i32 %X
Handler:
%exn = landingpad {i8*, i32}
cleanup
ret i32 1
}
declare i32 @__gxx_personality_v0(...)
)IR");
Function *F1 = M->getFunction("caller");
CallBase* CB = findCall(*F1);
EXPECT_NE(CB, nullptr);
auto FPI = buildFPI(*F1);
FunctionPropertiesUpdater FPU(FPI, *CB);
InlineFunctionInfo IFI;
auto IR = llvm::InlineFunction(*CB, IFI);
EXPECT_TRUE(IR.isSuccess());
invalidate(*F1);
EXPECT_TRUE(FPU.finishAndTest(FAM));
EXPECT_EQ(static_cast<size_t>(FPI.BasicBlockCount), F1->size() - 1);
EXPECT_EQ(static_cast<size_t>(FPI.TotalInstructionCount),
F1->getInstructionCount() - 2);
EXPECT_EQ(FPI, FunctionPropertiesInfo::getFunctionPropertiesInfo(*F1, FAM));
}
TEST_F(FunctionPropertiesAnalysisTest, Rethrow) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
declare void @might_throw()
define internal i32 @callee() personality i32 (...)* @__gxx_personality_v0 {
entry:
invoke void @might_throw()
to label %cont unwind label %exc
cont:
ret i32 0
exc:
%exn = landingpad {i8*, i32}
cleanup
resume { i8*, i32 } %exn
}
define i32 @caller() personality i32 (...)* @__gxx_personality_v0 {
entry:
%X = invoke i32 @callee()
to label %cont unwind label %Handler
cont:
ret i32 %X
Handler:
%exn = landingpad {i8*, i32}
cleanup
ret i32 1
}
declare i32 @__gxx_personality_v0(...)
)IR");
Function *F1 = M->getFunction("caller");
CallBase* CB = findCall(*F1);
EXPECT_NE(CB, nullptr);
auto FPI = buildFPI(*F1);
FunctionPropertiesUpdater FPU(FPI, *CB);
InlineFunctionInfo IFI;
auto IR = llvm::InlineFunction(*CB, IFI);
EXPECT_TRUE(IR.isSuccess());
invalidate(*F1);
EXPECT_TRUE(FPU.finishAndTest(FAM));
EXPECT_EQ(static_cast<size_t>(FPI.BasicBlockCount), F1->size() - 1);
EXPECT_EQ(static_cast<size_t>(FPI.TotalInstructionCount),
F1->getInstructionCount() - 2);
EXPECT_EQ(FPI, FunctionPropertiesInfo::getFunctionPropertiesInfo(*F1, FAM));
}
TEST_F(FunctionPropertiesAnalysisTest, LPadChanges) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
declare void @external_func()
@exception_type1 = external global i8
@exception_type2 = external global i8
define internal void @inner() personality i8* null {
invoke void @external_func()
to label %cont unwind label %lpad
cont:
ret void
lpad:
%lp = landingpad i32
catch i8* @exception_type1
resume i32 %lp
}
define void @outer() personality i8* null {
invoke void @inner()
to label %cont unwind label %lpad
cont:
ret void
lpad:
%lp = landingpad i32
cleanup
catch i8* @exception_type2
resume i32 %lp
}
)IR");
Function *F1 = M->getFunction("outer");
CallBase* CB = findCall(*F1);
EXPECT_NE(CB, nullptr);
auto FPI = buildFPI(*F1);
FunctionPropertiesUpdater FPU(FPI, *CB);
InlineFunctionInfo IFI;
auto IR = llvm::InlineFunction(*CB, IFI);
EXPECT_TRUE(IR.isSuccess());
invalidate(*F1);
EXPECT_TRUE(FPU.finishAndTest(FAM));
EXPECT_EQ(static_cast<size_t>(FPI.BasicBlockCount), F1->size() - 1);
EXPECT_EQ(static_cast<size_t>(FPI.TotalInstructionCount),
F1->getInstructionCount() - 2);
EXPECT_EQ(FPI, FunctionPropertiesInfo::getFunctionPropertiesInfo(*F1, FAM));
}
TEST_F(FunctionPropertiesAnalysisTest, LPadChangesConditional) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
declare void @external_func()
@exception_type1 = external global i8
@exception_type2 = external global i8
define internal void @inner() personality i8* null {
invoke void @external_func()
to label %cont unwind label %lpad
cont:
ret void
lpad:
%lp = landingpad i32
catch i8* @exception_type1
resume i32 %lp
}
define void @outer(i32 %a) personality i8* null {
entry:
%i = icmp slt i32 %a, 0
br i1 %i, label %if.then, label %cont
if.then:
invoke void @inner()
to label %cont unwind label %lpad
cont:
ret void
lpad:
%lp = landingpad i32
cleanup
catch i8* @exception_type2
resume i32 %lp
}
)IR");
Function *F1 = M->getFunction("outer");
CallBase* CB = findCall(*F1);
EXPECT_NE(CB, nullptr);
auto FPI = buildFPI(*F1);
FunctionPropertiesUpdater FPU(FPI, *CB);
InlineFunctionInfo IFI;
auto IR = llvm::InlineFunction(*CB, IFI);
EXPECT_TRUE(IR.isSuccess());
invalidate(*F1);
EXPECT_TRUE(FPU.finishAndTest(FAM));
EXPECT_EQ(static_cast<size_t>(FPI.BasicBlockCount), F1->size() - 1);
EXPECT_EQ(static_cast<size_t>(FPI.TotalInstructionCount),
F1->getInstructionCount() - 2);
EXPECT_EQ(FPI, FunctionPropertiesInfo::getFunctionPropertiesInfo(*F1, FAM));
}
TEST_F(FunctionPropertiesAnalysisTest, InlineSameLoopBB) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
declare i32 @a()
declare i32 @b()
define i32 @f1(i32 %a) {
entry:
br label %loop
loop:
%i = call i32 @f2(i32 %a)
%c = icmp slt i32 %i, %a
br i1 %c, label %loop, label %end
end:
%r = phi i32 [%i, %loop], [%a, %entry]
ret i32 %r
}
define i32 @f2(i32 %a) {
%cnd = icmp slt i32 %a, 0
br i1 %cnd, label %then, label %else
then:
%r1 = call i32 @a()
br label %end
else:
%r2 = call i32 @b()
br label %end
end:
%r = phi i32 [%r1, %then], [%r2, %else]
ret i32 %r
}
)IR");
Function *F1 = M->getFunction("f1");
CallBase *CB = findCall(*F1);
EXPECT_NE(CB, nullptr);
FunctionPropertiesInfo ExpectedInitial;
ExpectedInitial.BasicBlockCount = 3;
ExpectedInitial.TotalInstructionCount = 6;
ExpectedInitial.BlocksReachedFromConditionalInstruction = 2;
ExpectedInitial.Uses = 1;
ExpectedInitial.DirectCallsToDefinedFunctions = 1;
ExpectedInitial.MaxLoopDepth = 1;
ExpectedInitial.TopLevelLoopCount = 1;
FunctionPropertiesInfo ExpectedFinal = ExpectedInitial;
ExpectedFinal.BasicBlockCount = 6;
ExpectedFinal.DirectCallsToDefinedFunctions = 0;
ExpectedFinal.BlocksReachedFromConditionalInstruction = 4;
ExpectedFinal.TotalInstructionCount = 12;
auto FPI = buildFPI(*F1);
EXPECT_EQ(FPI, ExpectedInitial);
FunctionPropertiesUpdater FPU(FPI, *CB);
InlineFunctionInfo IFI;
auto IR = llvm::InlineFunction(*CB, IFI);
EXPECT_TRUE(IR.isSuccess());
invalidate(*F1);
EXPECT_TRUE(FPU.finishAndTest(FAM));
EXPECT_EQ(FPI, ExpectedFinal);
}
TEST_F(FunctionPropertiesAnalysisTest, Unreachable) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
define i64 @f1(i32 noundef %value) {
entry:
br i1 true, label %cond.true, label %cond.false
cond.true: ; preds = %entry
%conv2 = sext i32 %value to i64
br label %cond.end
cond.false: ; preds = %entry
%call3 = call noundef i64 @f2()
br label %extra
extra:
br label %extra2
extra2:
br label %cond.end
cond.end: ; preds = %cond.false, %cond.true
%cond = phi i64 [ %conv2, %cond.true ], [ %call3, %extra ]
ret i64 %cond
}
define i64 @f2() {
entry:
tail call void @llvm.trap()
unreachable
}
declare void @llvm.trap()
)IR");
Function *F1 = M->getFunction("f1");
CallBase *CB = findCall(*F1);
EXPECT_NE(CB, nullptr);
FunctionPropertiesInfo ExpectedInitial;
ExpectedInitial.BasicBlockCount = 6;
ExpectedInitial.TotalInstructionCount = 9;
ExpectedInitial.BlocksReachedFromConditionalInstruction = 2;
ExpectedInitial.Uses = 1;
ExpectedInitial.DirectCallsToDefinedFunctions = 1;
FunctionPropertiesInfo ExpectedFinal = ExpectedInitial;
ExpectedFinal.BasicBlockCount = 4;
ExpectedFinal.DirectCallsToDefinedFunctions = 0;
ExpectedFinal.TotalInstructionCount = 7;
auto FPI = buildFPI(*F1);
EXPECT_EQ(FPI, ExpectedInitial);
FunctionPropertiesUpdater FPU(FPI, *CB);
InlineFunctionInfo IFI;
auto IR = llvm::InlineFunction(*CB, IFI);
EXPECT_TRUE(IR.isSuccess());
invalidate(*F1);
EXPECT_TRUE(FPU.finishAndTest(FAM));
EXPECT_EQ(FPI, ExpectedFinal);
}
TEST_F(FunctionPropertiesAnalysisTest, InvokeSkipLP) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
define i64 @f1(i32 noundef %value) {
entry:
invoke fastcc void @f2() to label %cont unwind label %lpad
cont:
ret i64 1
lpad:
%lp = landingpad i32 cleanup
br label %ehcleanup
ehcleanup:
resume i32 0
}
define void @f2() {
invoke noundef void @f3() to label %exit unwind label %lpad
exit:
ret void
lpad:
%lp = landingpad i32 cleanup
resume i32 %lp
}
declare void @f3()
)IR");
// The outcome of inlining will be that lpad becomes unreachable. The landing
// pad of the invoke inherited from f2 will land on a new bb which will branch
// to a bb containing the body of lpad.
Function *F1 = M->getFunction("f1");
CallBase *CB = findCall(*F1);
EXPECT_NE(CB, nullptr);
FunctionPropertiesInfo ExpectedInitial;
ExpectedInitial.BasicBlockCount = 4;
ExpectedInitial.TotalInstructionCount = 5;
ExpectedInitial.BlocksReachedFromConditionalInstruction = 0;
ExpectedInitial.Uses = 1;
ExpectedInitial.DirectCallsToDefinedFunctions = 1;
FunctionPropertiesInfo ExpectedFinal = ExpectedInitial;
ExpectedFinal.BasicBlockCount = 6;
ExpectedFinal.DirectCallsToDefinedFunctions = 0;
ExpectedFinal.TotalInstructionCount = 8;
auto FPI = buildFPI(*F1);
EXPECT_EQ(FPI, ExpectedInitial);
FunctionPropertiesUpdater FPU(FPI, *CB);
InlineFunctionInfo IFI;
auto IR = llvm::InlineFunction(*CB, IFI);
EXPECT_TRUE(IR.isSuccess());
invalidate(*F1);
EXPECT_TRUE(FPU.finishAndTest(FAM));
EXPECT_EQ(FPI, ExpectedFinal);
}
TEST_F(FunctionPropertiesAnalysisTest, DetailedOperandCount) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
@a = global i64 1
define i64 @f1(i64 %e) {
%b = load i64, i64* @a
%c = add i64 %b, 2
%d = call i64 asm "mov $1,$0", "=r,r" (i64 %c)
%f = add i64 %d, %e
ret i64 %f
}
)IR");
Function *F1 = M->getFunction("f1");
EnableDetailedFunctionProperties.setValue(true);
FunctionPropertiesInfo DetailedF1Properties = buildFPI(*F1);
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithSingleSuccessor, 0);
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithTwoSuccessors, 0);
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithMoreThanTwoSuccessors, 0);
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithSinglePredecessor, 0);
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithTwoPredecessors, 0);
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithMoreThanTwoPredecessors, 0);
EXPECT_EQ(DetailedF1Properties.BigBasicBlocks, 0);
EXPECT_EQ(DetailedF1Properties.MediumBasicBlocks, 0);
EXPECT_EQ(DetailedF1Properties.SmallBasicBlocks, 1);
EXPECT_EQ(DetailedF1Properties.CastInstructionCount, 0);
EXPECT_EQ(DetailedF1Properties.FloatingPointInstructionCount, 0);
EXPECT_EQ(DetailedF1Properties.IntegerInstructionCount, 4);
EXPECT_EQ(DetailedF1Properties.ConstantIntOperandCount, 1);
EXPECT_EQ(DetailedF1Properties.ConstantFPOperandCount, 0);
EXPECT_EQ(DetailedF1Properties.ConstantOperandCount, 0);
EXPECT_EQ(DetailedF1Properties.InstructionOperandCount, 4);
EXPECT_EQ(DetailedF1Properties.BasicBlockOperandCount, 0);
EXPECT_EQ(DetailedF1Properties.GlobalValueOperandCount, 1);
EXPECT_EQ(DetailedF1Properties.InlineAsmOperandCount, 1);
EXPECT_EQ(DetailedF1Properties.ArgumentOperandCount, 1);
EXPECT_EQ(DetailedF1Properties.UnknownOperandCount, 0);
EXPECT_EQ(DetailedF1Properties.CriticalEdgeCount, 0);
EXPECT_EQ(DetailedF1Properties.ControlFlowEdgeCount, 0);
EXPECT_EQ(DetailedF1Properties.UnconditionalBranchCount, 0);
EXPECT_EQ(DetailedF1Properties.IntrinsicCount, 0);
EXPECT_EQ(DetailedF1Properties.DirectCallCount, 1);
EXPECT_EQ(DetailedF1Properties.IndirectCallCount, 0);
EXPECT_EQ(DetailedF1Properties.CallReturnsIntegerCount, 1);
EXPECT_EQ(DetailedF1Properties.CallReturnsFloatCount, 0);
EXPECT_EQ(DetailedF1Properties.CallReturnsPointerCount, 0);
EXPECT_EQ(DetailedF1Properties.CallWithManyArgumentsCount, 0);
EXPECT_EQ(DetailedF1Properties.CallWithPointerArgumentCount, 0);
EnableDetailedFunctionProperties.setValue(false);
}
TEST_F(FunctionPropertiesAnalysisTest, IntrinsicCount) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
define float @f1(float %a) {
%b = call float @llvm.cos.f32(float %a)
ret float %b
}
declare float @llvm.cos.f32(float)
)IR");
Function *F1 = M->getFunction("f1");
EnableDetailedFunctionProperties.setValue(true);
FunctionPropertiesInfo DetailedF1Properties = buildFPI(*F1);
EXPECT_EQ(DetailedF1Properties.IntrinsicCount, 1);
EXPECT_EQ(DetailedF1Properties.DirectCallCount, 1);
EXPECT_EQ(DetailedF1Properties.IndirectCallCount, 0);
EXPECT_EQ(DetailedF1Properties.CallReturnsIntegerCount, 0);
EXPECT_EQ(DetailedF1Properties.CallReturnsFloatCount, 1);
EXPECT_EQ(DetailedF1Properties.CallReturnsPointerCount, 0);
EXPECT_EQ(DetailedF1Properties.CallWithManyArgumentsCount, 0);
EXPECT_EQ(DetailedF1Properties.CallWithPointerArgumentCount, 0);
EnableDetailedFunctionProperties.setValue(false);
}
TEST_F(FunctionPropertiesAnalysisTest, FunctionCallMetrics) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
define i64 @f1(i64 %a) {
%b = call i64 @f2(i64 %a, i64 %a, i64 %a, i64 %a, i64 %a)
%c = call ptr @f3()
call void @f4(ptr %c)
%d = call float @f5()
%e = call i64 %c(i64 %b)
ret i64 %b
}
declare i64 @f2(i64,i64,i64,i64,i64)
declare ptr @f3()
declare void @f4(ptr)
declare float @f5()
)IR");
Function *F1 = M->getFunction("f1");
EnableDetailedFunctionProperties.setValue(true);
FunctionPropertiesInfo DetailedF1Properties = buildFPI(*F1);
EXPECT_EQ(DetailedF1Properties.IntrinsicCount, 0);
EXPECT_EQ(DetailedF1Properties.DirectCallCount, 4);
EXPECT_EQ(DetailedF1Properties.IndirectCallCount, 1);
EXPECT_EQ(DetailedF1Properties.CallReturnsIntegerCount, 2);
EXPECT_EQ(DetailedF1Properties.CallReturnsFloatCount, 1);
EXPECT_EQ(DetailedF1Properties.CallReturnsPointerCount, 1);
EXPECT_EQ(DetailedF1Properties.CallWithManyArgumentsCount, 1);
EXPECT_EQ(DetailedF1Properties.CallWithPointerArgumentCount, 1);
EnableDetailedFunctionProperties.setValue(false);
}
TEST_F(FunctionPropertiesAnalysisTest, CriticalEdge) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
define i64 @f1(i64 %a) {
%b = icmp eq i64 %a, 1
br i1 %b, label %TopBlock1, label %TopBlock2
TopBlock1:
%c = add i64 %a, 1
%e = icmp eq i64 %c, 2
br i1 %e, label %BottomBlock1, label %BottomBlock2
TopBlock2:
%d = add i64 %a, 2
br label %BottomBlock2
BottomBlock1:
ret i64 0
BottomBlock2:
%f = phi i64 [ %c, %TopBlock1 ], [ %d, %TopBlock2 ]
ret i64 %f
}
)IR");
Function *F1 = M->getFunction("f1");
EnableDetailedFunctionProperties.setValue(true);
FunctionPropertiesInfo DetailedF1Properties = buildFPI(*F1);
EXPECT_EQ(DetailedF1Properties.CriticalEdgeCount, 1);
EnableDetailedFunctionProperties.setValue(false);
}
TEST_F(FunctionPropertiesAnalysisTest, FunctionReturnVectors) {
LLVMContext C;
std::unique_ptr<Module> M = makeLLVMModule(C,
R"IR(
define <4 x i64> @f1(<4 x i64> %a) {
%b = call <4 x i64> @f2()
%c = call <4 x float> @f3()
%d = call <4 x ptr> @f4()
ret <4 x i64> %b
}
declare <4 x i64> @f2()
declare <4 x float> @f3()
declare <4 x ptr> @f4()
)IR");
Function *F1 = M->getFunction("f1");
EnableDetailedFunctionProperties.setValue(true);
FunctionPropertiesInfo DetailedF1Properties = buildFPI(*F1);
EXPECT_EQ(DetailedF1Properties.CallReturnsVectorIntCount, 1);
EXPECT_EQ(DetailedF1Properties.CallReturnsVectorFloatCount, 1);
EXPECT_EQ(DetailedF1Properties.CallReturnsVectorPointerCount, 1);
EnableDetailedFunctionProperties.setValue(false);
}
} // end anonymous namespace
|