Skip to content

Commit 0628366

Browse files
tarindujmtrofin
authored andcommitted
Add new function properties to FunctionPropertiesAnalysis
Added LoadInstCount, StoreInstCount, MaxLoopDepth, LoopCount Reviewed By: jdoerfert, mtrofin Differential Revision: https://reviews.llvm.org/D82283
1 parent b9c644e commit 0628366

File tree

4 files changed

+86
-29
lines changed

4 files changed

+86
-29
lines changed

llvm/include/llvm/Analysis/FunctionPropertiesAnalysis.h

+16-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@
1414
#ifndef LLVM_FUNCTIONPROPERTIESANALYSIS_H_
1515
#define LLVM_FUNCTIONPROPERTIESANALYSIS_H_
1616

17+
#include "llvm/Analysis/LoopInfo.h"
1718
#include "llvm/IR/PassManager.h"
1819

1920
namespace llvm {
2021
class Function;
2122

2223
class FunctionPropertiesInfo {
2324
public:
25+
static FunctionPropertiesInfo getFunctionPropertiesInfo(const Function &F,
26+
const LoopInfo &LI);
27+
28+
void print(raw_ostream &OS) const;
29+
2430
/// Number of basic blocks
2531
int64_t BasicBlockCount = 0;
2632

@@ -40,9 +46,17 @@ class FunctionPropertiesInfo {
4046
/// defined in this module.
4147
int64_t DirectCallsToDefinedFunctions = 0;
4248

43-
static FunctionPropertiesInfo getFunctionPropertiesInfo(const Function &F);
49+
// Load Instruction Count
50+
int64_t LoadInstCount = 0;
4451

45-
void print(raw_ostream &OS) const;
52+
// Store Instruction Count
53+
int64_t StoreInstCount = 0;
54+
55+
// Maximum Loop Depth in the Function
56+
int64_t MaxLoopDepth = 0;
57+
58+
// Number of Top Level Loops in the Function
59+
int64_t TopLevelLoopCount = 0;
4660
};
4761

4862
// Analysis pass

llvm/lib/Analysis/FunctionPropertiesAnalysis.cpp

+22-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
using namespace llvm;
1818

1919
FunctionPropertiesInfo
20-
FunctionPropertiesInfo::getFunctionPropertiesInfo(const Function &F) {
20+
FunctionPropertiesInfo::getFunctionPropertiesInfo(const Function &F,
21+
const LoopInfo &LI) {
2122

2223
FunctionPropertiesInfo FPI;
2324

@@ -40,7 +41,20 @@ FunctionPropertiesInfo::getFunctionPropertiesInfo(const Function &F) {
4041
if (Callee && !Callee->isIntrinsic() && !Callee->isDeclaration())
4142
++FPI.DirectCallsToDefinedFunctions;
4243
}
44+
if (I.getOpcode() == Instruction::Load) {
45+
++FPI.LoadInstCount;
46+
} else if (I.getOpcode() == Instruction::Store) {
47+
++FPI.StoreInstCount;
48+
}
4349
}
50+
// Loop Depth of the Basic Block
51+
int64_t LoopDepth;
52+
LoopDepth = LI.getLoopDepth(&BB);
53+
if (FPI.MaxLoopDepth < LoopDepth)
54+
FPI.MaxLoopDepth = LoopDepth;
55+
}
56+
for (Loop *L : LI) {
57+
++FPI.TopLevelLoopCount;
4458
}
4559
return FPI;
4660
}
@@ -51,14 +65,19 @@ void FunctionPropertiesInfo::print(raw_ostream &OS) const {
5165
<< BlocksReachedFromConditionalInstruction << "\n"
5266
<< "Uses: " << Uses << "\n"
5367
<< "DirectCallsToDefinedFunctions: " << DirectCallsToDefinedFunctions
54-
<< "\n\n";
68+
<< "\n"
69+
<< "LoadInstCount: " << LoadInstCount << "\n"
70+
<< "StoreInstCount: " << StoreInstCount << "\n"
71+
<< "MaxLoopDepth: " << MaxLoopDepth << "\n"
72+
<< "TopLevelLoopCount: " << TopLevelLoopCount << "\n\n";
5573
}
5674

5775
AnalysisKey FunctionPropertiesAnalysis::Key;
5876

5977
FunctionPropertiesInfo
6078
FunctionPropertiesAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
61-
return FunctionPropertiesInfo::getFunctionPropertiesInfo(F);
79+
return FunctionPropertiesInfo::getFunctionPropertiesInfo(
80+
F, FAM.getResult<LoopAnalysis>(F));
6281
}
6382

6483
PreservedAnalyses

llvm/test/Analysis/FunctionPropertiesAnalysis/matmul.ll

+9-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ entry:
2121
; CHECK-DAG: BlocksReachedFromConditionalInstruction: 0
2222
; CHECK-DAG: Uses: 1
2323
; CHECK-DAG: DirectCallsToDefinedFunctions: 1
24+
; CHECK-DAG: LoadInstCount: 0
25+
; CHECK-DAG: StoreInstCount: 1
26+
; CHECK-DAG: MaxLoopDepth: 0
27+
; CHECK-DAG: TopLevelLoopCount: 0
2428

2529
define void @multiply([2 x i32]* %mat1, [2 x i32]* %mat2, [2 x i32]* %res) {
2630
; CHECK-DAG: Printing analysis results of CFA for function 'multiply':
@@ -129,4 +133,8 @@ for.end26: ; preds = %for.cond
129133
; CHECK-DAG: BasicBlockCount: 13
130134
; CHECK-DAG: BlocksReachedFromConditionalInstruction: 6
131135
; CHECK-DAG: Uses: 2
132-
; CHECK-DAG: DirectCallsToDefinedFunctions: 0
136+
; CHECK-DAG: DirectCallsToDefinedFunctions: 0
137+
; CHECK-DAG: LoadInstCount: 21
138+
; CHECK-DAG: StoreInstCount: 11
139+
; CHECK-DAG: MaxLoopDepth: 3
140+
; CHECK-DAG: TopLevelLoopCount: 1

llvm/unittests/Analysis/FunctionPropertiesAnalysisTest.cpp

+39-23
Original file line numberDiff line numberDiff line change
@@ -8,70 +8,86 @@
88

99
#include "llvm/Analysis/FunctionPropertiesAnalysis.h"
1010
#include "llvm/AsmParser/Parser.h"
11+
#include "llvm/IR/Dominators.h"
1112
#include "llvm/IR/Instructions.h"
1213
#include "llvm/IR/LLVMContext.h"
1314
#include "llvm/IR/Module.h"
1415
#include "llvm/Support/SourceMgr.h"
1516
#include "gtest/gtest.h"
1617

1718
using namespace llvm;
19+
namespace {
1820

19-
static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
20-
SMDiagnostic Err;
21-
std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
22-
if (!Mod)
23-
Err.print("MLAnalysisTests", errs());
24-
return Mod;
25-
}
21+
class FunctionPropertiesAnalysisTest : public testing::Test {
22+
protected:
23+
std::unique_ptr<DominatorTree> DT;
24+
std::unique_ptr<LoopInfo> LI;
25+
26+
FunctionPropertiesInfo buildFPI(Function &F) {
27+
DT.reset(new DominatorTree(F));
28+
LI.reset(new LoopInfo(*DT));
29+
return FunctionPropertiesInfo::getFunctionPropertiesInfo(F, *LI);
30+
}
2631

27-
TEST(FunctionPropertiesTest, BasicTest) {
32+
std::unique_ptr<Module> makeLLVMModule(LLVMContext &C, const char *IR) {
33+
SMDiagnostic Err;
34+
std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
35+
if (!Mod)
36+
Err.print("MLAnalysisTests", errs());
37+
return Mod;
38+
}
39+
};
40+
41+
TEST_F(FunctionPropertiesAnalysisTest, BasicTest) {
2842
LLVMContext C;
29-
std::unique_ptr<Module> M = parseIR(C,
30-
R"IR(
43+
std::unique_ptr<Module> M = makeLLVMModule(C,
44+
R"IR(
3145
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
3246
target triple = "x86_64-pc-linux-gnu"
33-
3447
declare i32 @f1(i32)
3548
declare i32 @f2(i32)
36-
3749
define i32 @branches(i32) {
3850
%cond = icmp slt i32 %0, 3
3951
br i1 %cond, label %then, label %else
40-
4152
then:
4253
%ret.1 = call i32 @f1(i32 %0)
4354
br label %last.block
44-
4555
else:
4656
%ret.2 = call i32 @f2(i32 %0)
4757
br label %last.block
48-
4958
last.block:
5059
%ret = phi i32 [%ret.1, %then], [%ret.2, %else]
5160
ret i32 %ret
5261
}
53-
5462
define internal i32 @top() {
5563
%1 = call i32 @branches(i32 2)
5664
%2 = call i32 @f1(i32 %1)
5765
ret i32 %2
5866
}
5967
)IR");
6068

61-
FunctionAnalysisManager FAM;
62-
FunctionPropertiesAnalysis FPA;
63-
64-
auto BranchesFeatures = FPA.run(*M->getFunction("branches"), FAM);
69+
Function *BranchesFunction = M->getFunction("branches");
70+
FunctionPropertiesInfo BranchesFeatures = buildFPI(*BranchesFunction);
6571
EXPECT_EQ(BranchesFeatures.BasicBlockCount, 4);
6672
EXPECT_EQ(BranchesFeatures.BlocksReachedFromConditionalInstruction, 2);
67-
EXPECT_EQ(BranchesFeatures.DirectCallsToDefinedFunctions, 0);
6873
// 2 Users: top is one. The other is added because @branches is not internal,
6974
// so it may have external callers.
7075
EXPECT_EQ(BranchesFeatures.Uses, 2);
76+
EXPECT_EQ(BranchesFeatures.DirectCallsToDefinedFunctions, 0);
77+
EXPECT_EQ(BranchesFeatures.LoadInstCount, 0);
78+
EXPECT_EQ(BranchesFeatures.StoreInstCount, 0);
79+
EXPECT_EQ(BranchesFeatures.MaxLoopDepth, 0);
80+
EXPECT_EQ(BranchesFeatures.TopLevelLoopCount, 0);
7181

72-
auto TopFeatures = FPA.run(*M->getFunction("top"), FAM);
82+
Function *TopFunction = M->getFunction("top");
83+
FunctionPropertiesInfo TopFeatures = buildFPI(*TopFunction);
7384
EXPECT_EQ(TopFeatures.BasicBlockCount, 1);
7485
EXPECT_EQ(TopFeatures.BlocksReachedFromConditionalInstruction, 0);
75-
EXPECT_EQ(TopFeatures.DirectCallsToDefinedFunctions, 1);
7686
EXPECT_EQ(TopFeatures.Uses, 0);
87+
EXPECT_EQ(TopFeatures.DirectCallsToDefinedFunctions, 1);
88+
EXPECT_EQ(BranchesFeatures.LoadInstCount, 0);
89+
EXPECT_EQ(BranchesFeatures.StoreInstCount, 0);
90+
EXPECT_EQ(BranchesFeatures.MaxLoopDepth, 0);
91+
EXPECT_EQ(BranchesFeatures.TopLevelLoopCount, 0);
7792
}
93+
} // end anonymous namespace

0 commit comments

Comments
 (0)