aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Analysis/FunctionPropertiesAnalysisTest.cpp
blob: dc6059dcf682745d8ecf6dbd4ceeb83eb94161a9 (plain)
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
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
//===- 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/IR2Vec.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/Compiler.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <cstring>

using namespace llvm;
using namespace testing;

namespace llvm {
LLVM_ABI extern cl::opt<bool> EnableDetailedFunctionProperties;
LLVM_ABI extern cl::opt<bool> BigBasicBlockInstructionThreshold;
LLVM_ABI extern cl::opt<bool> MediumBasicBlockInstrutionThreshold;
LLVM_ABI extern cl::opt<float> ir2vec::OpcWeight;
LLVM_ABI extern cl::opt<float> ir2vec::TypeWeight;
LLVM_ABI extern cl::opt<float> ir2vec::ArgWeight;
} // namespace llvm

namespace {

class FunctionPropertiesAnalysisTest : public testing::Test {
public:
  FunctionPropertiesAnalysisTest() {
    auto VocabVector = ir2vec::Vocabulary::createDummyVocabForTest(1);
    MAM.registerPass([&] { return IR2VecVocabAnalysis(VocabVector); });
    IR2VecVocab = ir2vec::Vocabulary(std::move(VocabVector));
    MAM.registerPass([&] { return PassInstrumentationAnalysis(); });
    FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
    FAM.registerPass([&] { return DominatorTreeAnalysis(); });
    FAM.registerPass([&] { return LoopAnalysis(); });
    FAM.registerPass([&] { return PassInstrumentationAnalysis(); });

    ir2vec::OpcWeight = 1.0;
    ir2vec::TypeWeight = 1.0;
    ir2vec::ArgWeight = 1.0;
  }

private:
  float OriginalOpcWeight = ir2vec::OpcWeight;
  float OriginalTypeWeight = ir2vec::TypeWeight;
  float OriginalArgWeight = ir2vec::ArgWeight;

protected:
  std::unique_ptr<DominatorTree> DT;
  std::unique_ptr<LoopInfo> LI;
  FunctionAnalysisManager FAM;
  ModuleAnalysisManager MAM;
  ir2vec::Vocabulary IR2VecVocab;

  void TearDown() override {
    // Restore original IR2Vec weights
    ir2vec::OpcWeight = OriginalOpcWeight;
    ir2vec::TypeWeight = OriginalTypeWeight;
    ir2vec::ArgWeight = OriginalArgWeight;
  }

  FunctionPropertiesInfo buildFPI(Function &F) {
    // FunctionPropertiesInfo assumes IR2VecVocabAnalysis has been run to
    // use IR2Vec.
    auto VocabResult = MAM.getResult<IR2VecVocabAnalysis>(*F.getParent());
    (void)VocabResult;
    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;
  }

  std::unique_ptr<ir2vec::Embedder> createEmbedder(const Function &F) {
    auto Emb = ir2vec::Embedder::create(IR2VecKind::Symbolic, F, IR2VecVocab);
    EXPECT_TRUE(static_cast<bool>(Emb));
    return Emb;
  }
};

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);
  EXPECT_TRUE(BranchesFeatures.getFunctionEmbedding().approximatelyEquals(
      createEmbedder(*BranchesFunction)->getFunctionVector()));

  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_TRUE(TopFeatures.getFunctionEmbedding().approximatelyEquals(
      createEmbedder(*TopFunction)->getFunctionVector()));
  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);
  EXPECT_TRUE(
      DetailedBranchesFeatures.getFunctionEmbedding().approximatelyEquals(
          createEmbedder(*BranchesFunction)->getFunctionVector()));
  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);
  EXPECT_TRUE(DetailedF1Properties.getFunctionEmbedding().approximatelyEquals(
      createEmbedder(*F1)->getFunctionVector()));
  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;
  ExpectedInitial.setFunctionEmbeddingForTest(
      createEmbedder(*F1)->getFunctionVector());

  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);
  ExpectedFinal.setFunctionEmbeddingForTest(
      createEmbedder(*F1)->getFunctionVector());

  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;
  ExpectedInitial.setFunctionEmbeddingForTest(
      createEmbedder(*F1)->getFunctionVector());

  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));

  ExpectedFinal.setFunctionEmbeddingForTest(
      createEmbedder(*F1)->getFunctionVector());
  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;
  ExpectedInitial.setFunctionEmbeddingForTest(
      createEmbedder(*F1)->getFunctionVector());

  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));

  ExpectedFinal.setFunctionEmbeddingForTest(
      createEmbedder(*F1)->getFunctionVector());
  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());
  EXPECT_TRUE(FPI.getFunctionEmbedding().approximatelyEquals(
      createEmbedder(*F1)->getFunctionVector()));
}

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_TRUE(FPI.getFunctionEmbedding().approximatelyEquals(
      createEmbedder(*F1)->getFunctionVector()));
  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_TRUE(FPI.getFunctionEmbedding().approximatelyEquals(
      createEmbedder(*F1)->getFunctionVector()));
  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_TRUE(FPI.getFunctionEmbedding().approximatelyEquals(
      createEmbedder(*F1)->getFunctionVector()));
  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;
  ExpectedInitial.setFunctionEmbeddingForTest(
      createEmbedder(*F1)->getFunctionVector());

  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));

  ExpectedFinal.setFunctionEmbeddingForTest(
      createEmbedder(*F1)->getFunctionVector());
  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 = %extra2, %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;
  ExpectedInitial.setFunctionEmbeddingForTest(
      createEmbedder(*F1)->getFunctionVector());

  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));

  ExpectedFinal.setFunctionEmbeddingForTest(
      createEmbedder(*F1)->getFunctionVector());
  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;
  ExpectedInitial.setFunctionEmbeddingForTest(
      createEmbedder(*F1)->getFunctionVector());

  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));

  ExpectedFinal.setFunctionEmbeddingForTest(
      createEmbedder(*F1)->getFunctionVector());
  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);
  EXPECT_TRUE(DetailedF1Properties.getFunctionEmbedding().approximatelyEquals(
      createEmbedder(*F1)->getFunctionVector()));
  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);
  EXPECT_TRUE(DetailedF1Properties.getFunctionEmbedding().approximatelyEquals(
      createEmbedder(*F1)->getFunctionVector()));
  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);
  EXPECT_TRUE(DetailedF1Properties.getFunctionEmbedding().approximatelyEquals(
      createEmbedder(*F1)->getFunctionVector()));
  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);
  EXPECT_TRUE(DetailedF1Properties.getFunctionEmbedding().approximatelyEquals(
      createEmbedder(*F1)->getFunctionVector()));
  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);
  EXPECT_TRUE(DetailedF1Properties.getFunctionEmbedding().approximatelyEquals(
      createEmbedder(*F1)->getFunctionVector()));
  EnableDetailedFunctionProperties.setValue(false);
}

TEST_F(FunctionPropertiesAnalysisTest, ReAddEdges) {
  LLVMContext C;
  std::unique_ptr<Module> M = makeLLVMModule(C, R"IR(
define hidden void @f1(ptr noundef %destatep, i32 noundef %offset, i8 noundef zeroext %byte1) {
entry:
  %cmp = icmp eq i8 %byte1, 0
  br i1 %cmp, label %if.then, label %if.else

if.then:                                          ; preds = %entry
  call fastcc void @f2(ptr noundef %destatep, i32 noundef 37, i32 noundef 600)
  %and = and i32 %offset, 3
  switch i32 %and, label %default.unreachable [
    i32 0, label %sw.bb
    i32 1, label %sw.bb1
    i32 2, label %sw.bb1
    i32 3, label %if.end
  ]

sw.bb:                                            ; preds = %if.then
  call fastcc void @f2(ptr noundef %destatep, i32 noundef 57, i32 noundef 600)
  br label %if.end

sw.bb1:                                           ; preds = %if.then, %if.then
  call fastcc void @f2(ptr noundef %destatep, i32 noundef 56, i32 noundef 600) #34
  br label %if.end

default.unreachable:                              ; preds = %if.then
  unreachable

if.else:                                          ; preds = %entry
  call fastcc void @f2(ptr noundef %destatep, i32 noundef 56, i32 noundef 600)
  br label %if.end

if.end:                                           ; preds = %sw.bb, %sw.bb1, %if.then, %if.else
  ret void
}

define internal fastcc void @f2(ptr nocapture noundef %destatep, i32 noundef %r_enc, i32 noundef %whack) {
entry:
  %enc_prob = getelementptr inbounds nuw i8, ptr %destatep, i32 512
  %arrayidx = getelementptr inbounds [67 x i32], ptr %enc_prob, i32 0, i32 %r_enc
  %0 = load i32, ptr %arrayidx, align 4
  %sub = sub nsw i32 %0, %whack
  store i32 %sub, ptr %arrayidx, align 4
  ret void
}
  )IR");
  auto *F1 = M->getFunction("f1");
  auto *F2 = M->getFunction("f2");
  auto *CB = [&]() -> CallBase * {
    for (auto &BB : *F1)
      for (auto &I : BB)
        if (auto *CB = dyn_cast<CallBase>(&I);
            CB && CB->getCalledFunction() && CB->getCalledFunction() == F2)
          return CB;
    return nullptr;
  }();
  ASSERT_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));
}

TEST_F(FunctionPropertiesAnalysisTest, InvokeLandingCanStillBeReached) {
  LLVMContext C;
  // %lpad is reachable from a block not involved in the inlining decision. We
  // make sure that's not the entry - otherwise the DT will be recomputed from
  // scratch. The idea here is that the edge known to the inliner to potentially
  // disappear - %lpad->%ehcleanup -should survive because it is still reachable
  // from %middle.
  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 label %middle
middle:
  %c = icmp eq i32 %value, 0
  br i1 %c, label %invoke, label %lpad
invoke:
  invoke fastcc void @f2() to label %cont unwind label %lpad
cont:
  br label %exit
lpad:
  %lp = landingpad i32 cleanup
  br label %ehcleanup
ehcleanup:
  resume i32 0
exit:
  ret i64 1
}
define void @f2() {
  ret void
}
)IR");

  Function *F1 = M->getFunction("f1");
  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));
}
} // end anonymous namespace