Skip to content

Commit b8f191e

Browse files
[FunctionPropertiesAnalysis] Add operand type counts
This patch adds operand type counts to the detailed function properties analysis. This is intended to enable more interesting and detailed comparisons across different languages on specific metrics (like usage of inline assembly or global values). Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D158018
1 parent 07181e2 commit b8f191e

File tree

4 files changed

+147
-52
lines changed

4 files changed

+147
-52
lines changed

llvm/include/llvm/Analysis/FunctionPropertiesAnalysis.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,16 @@ class FunctionPropertiesInfo {
107107
// The number of integer instructions inside the function.
108108
int64_t IntegerInstructionCount = 0;
109109

110-
// The number of integer constant operands inside the function.
111-
int64_t IntegerConstantCount = 0;
112-
113-
// The number of floating point constant operands inside the function.
114-
int64_t FloatingPointConstantCount = 0;
110+
// Operand type couns
111+
int64_t ConstantIntOperandCount = 0;
112+
int64_t ConstantFPOperandCount = 0;
113+
int64_t ConstantOperandCount = 0;
114+
int64_t InstructionOperandCount = 0;
115+
int64_t BasicBlockOperandCount = 0;
116+
int64_t GlobalValueOperandCount = 0;
117+
int64_t InlineAsmOperandCount = 0;
118+
int64_t ArgumentOperandCount = 0;
119+
int64_t UnknownOperandCount = 0;
115120
};
116121

117122
// Analysis pass

llvm/lib/Analysis/FunctionPropertiesAnalysis.cpp

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/ADT/SetVector.h"
1717
#include "llvm/Analysis/LoopInfo.h"
1818
#include "llvm/IR/CFG.h"
19+
#include "llvm/IR/Constants.h"
1920
#include "llvm/IR/Dominators.h"
2021
#include "llvm/IR/Instructions.h"
2122
#include "llvm/Support/CommandLine.h"
@@ -111,16 +112,30 @@ void FunctionPropertiesInfo::updateForBB(const BasicBlock &BB,
111112
else if (I.getType()->isIntegerTy())
112113
IntegerInstructionCount += Direction;
113114

115+
#define COUNT_OPERAND(OPTYPE) \
116+
if (isa<OPTYPE>(Operand)) { \
117+
OPTYPE##OperandCount += Direction; \
118+
continue; \
119+
}
120+
114121
for (unsigned int OperandIndex = 0; OperandIndex < I.getNumOperands();
115122
++OperandIndex) {
116-
if (const Constant *C =
117-
dyn_cast<Constant>(I.getOperand(OperandIndex))) {
118-
if (C->getType()->isIntegerTy())
119-
IntegerConstantCount += Direction;
120-
else if (C->getType()->isFloatTy())
121-
FloatingPointConstantCount += Direction;
122-
}
123+
Value *Operand = I.getOperand(OperandIndex);
124+
COUNT_OPERAND(GlobalValue)
125+
COUNT_OPERAND(ConstantInt)
126+
COUNT_OPERAND(ConstantFP)
127+
COUNT_OPERAND(Constant)
128+
COUNT_OPERAND(Instruction)
129+
COUNT_OPERAND(BasicBlock)
130+
COUNT_OPERAND(InlineAsm)
131+
COUNT_OPERAND(Argument)
132+
133+
// We only get to this point if we haven't matched any of the other
134+
// operand types.
135+
UnknownOperandCount += Direction;
123136
}
137+
138+
#undef CHECK_OPERAND
124139
}
125140
}
126141
}
@@ -160,40 +175,44 @@ FunctionPropertiesInfo FunctionPropertiesInfo::getFunctionPropertiesInfo(
160175
}
161176

162177
void FunctionPropertiesInfo::print(raw_ostream &OS) const {
163-
OS << "BasicBlockCount: " << BasicBlockCount << "\n"
164-
<< "BlocksReachedFromConditionalInstruction: "
165-
<< BlocksReachedFromConditionalInstruction << "\n"
166-
<< "Uses: " << Uses << "\n"
167-
<< "DirectCallsToDefinedFunctions: " << DirectCallsToDefinedFunctions
168-
<< "\n"
169-
<< "LoadInstCount: " << LoadInstCount << "\n"
170-
<< "StoreInstCount: " << StoreInstCount << "\n"
171-
<< "MaxLoopDepth: " << MaxLoopDepth << "\n"
172-
<< "TopLevelLoopCount: " << TopLevelLoopCount << "\n"
173-
<< "TotalInstructionCount: " << TotalInstructionCount << "\n";
178+
#define PRINT_PROPERTY(PROP_NAME) OS << #PROP_NAME ": " << PROP_NAME << "\n";
179+
180+
PRINT_PROPERTY(BasicBlockCount)
181+
PRINT_PROPERTY(BlocksReachedFromConditionalInstruction)
182+
PRINT_PROPERTY(Uses)
183+
PRINT_PROPERTY(DirectCallsToDefinedFunctions)
184+
PRINT_PROPERTY(LoadInstCount)
185+
PRINT_PROPERTY(StoreInstCount)
186+
PRINT_PROPERTY(MaxLoopDepth)
187+
PRINT_PROPERTY(TopLevelLoopCount)
188+
PRINT_PROPERTY(TotalInstructionCount)
189+
174190
if (EnableDetailedFunctionProperties) {
175-
OS << "BasicBlocksWithSingleSuccessor: " << BasicBlocksWithSingleSuccessor
176-
<< "\n"
177-
<< "BasicBlocksWithTwoSuccessors: " << BasicBlocksWithTwoSuccessors
178-
<< "\n"
179-
<< "BasicBlocksWithMoreThanTwoSuccessors: "
180-
<< BasicBlocksWithMoreThanTwoSuccessors << "\n"
181-
<< "BasicBlocksWithSinglePredecessor: "
182-
<< BasicBlocksWithSinglePredecessor << "\n"
183-
<< "BasicBlocksWithTwoPredecessors: " << BasicBlocksWithTwoPredecessors
184-
<< "\n"
185-
<< "BasicBlocksWithMoreThanTwoPredecessors: "
186-
<< BasicBlocksWithMoreThanTwoPredecessors << "\n"
187-
<< "BigBasicBlocks: " << BigBasicBlocks << "\n"
188-
<< "MediumBasicBlocks: " << MediumBasicBlocks << "\n"
189-
<< "SmallBasicBlocks: " << SmallBasicBlocks << "\n"
190-
<< "CastInstructionCount: " << CastInstructionCount << "\n"
191-
<< "FloatingPointInstructionCount: " << FloatingPointInstructionCount
192-
<< "\n"
193-
<< "IntegerInstructionCount: " << IntegerInstructionCount << "\n"
194-
<< "IntegerConstantCount: " << IntegerConstantCount << "\n"
195-
<< "FloatingPointConstantCount: " << FloatingPointConstantCount << "\n";
191+
PRINT_PROPERTY(BasicBlocksWithSingleSuccessor)
192+
PRINT_PROPERTY(BasicBlocksWithTwoSuccessors)
193+
PRINT_PROPERTY(BasicBlocksWithMoreThanTwoSuccessors)
194+
PRINT_PROPERTY(BasicBlocksWithSinglePredecessor)
195+
PRINT_PROPERTY(BasicBlocksWithTwoPredecessors)
196+
PRINT_PROPERTY(BasicBlocksWithMoreThanTwoPredecessors)
197+
PRINT_PROPERTY(BigBasicBlocks)
198+
PRINT_PROPERTY(MediumBasicBlocks)
199+
PRINT_PROPERTY(SmallBasicBlocks)
200+
PRINT_PROPERTY(CastInstructionCount)
201+
PRINT_PROPERTY(FloatingPointInstructionCount)
202+
PRINT_PROPERTY(IntegerInstructionCount)
203+
PRINT_PROPERTY(ConstantIntOperandCount)
204+
PRINT_PROPERTY(ConstantFPOperandCount)
205+
PRINT_PROPERTY(ConstantOperandCount)
206+
PRINT_PROPERTY(InstructionOperandCount)
207+
PRINT_PROPERTY(BasicBlockOperandCount)
208+
PRINT_PROPERTY(GlobalValueOperandCount)
209+
PRINT_PROPERTY(InlineAsmOperandCount)
210+
PRINT_PROPERTY(ArgumentOperandCount)
211+
PRINT_PROPERTY(UnknownOperandCount)
196212
}
213+
214+
#undef PRINT_PROPERTY
215+
197216
OS << "\n";
198217
}
199218

llvm/test/Analysis/FunctionPropertiesAnalysis/matmul.ll

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,15 @@ entry:
4848
; DETAILED-PROPERTIES-DAG: CastInstructionCount: 0
4949
; DETAILED-PROPERTIES-DAG: FloatingPointInstructionCount: 0
5050
; DETAILED-PROPERTIES-DAG: IntegerInstructionCount: 0
51-
; DETAILED-PROPERTIES-DAG: IntegerConstantCount: 14
52-
; DETAILED-PROPERTIES-DAG: FloatingPointConstantCount: 0
51+
; DETAILED-PROPERTIES-DAG: ConstantIntOperandCount: 14
52+
; DETAILED-PROPERTIES-DAG: ConstantFPOperandCount: 0
53+
; DETAILED-PROPERTIES-DAG: ConstantOperandCount: 0
54+
; DETAILED-PROPERTIES-DAG: InstructionOperandCount: 7
55+
; DETAILED-PROPERTIES-DAG: BasicBlockOperandCount: 0
56+
; DETAILED-PROPERTIES-DAG: GlobalValueOperandCount: 1
57+
; DETAILED-PROPERTIES-DAG: InlineAsmOperandCount: 0
58+
; DETAILED-PROPERTIES-DAG: ArgumentOperandCount: 0
59+
; DETAILED-PROPERTIES-DAG: UnknownOperandCount: 0
5360

5461
define void @multiply([2 x i32]* %mat1, [2 x i32]* %mat2, [2 x i32]* %res) {
5562
; CHECK-DAG: Printing analysis results of CFA for function 'multiply':
@@ -185,5 +192,13 @@ for.end26: ; preds = %for.cond
185192
; DETAILED-PROPERTIES-DAG: CastInstructionCount: 8
186193
; DETAILED-PROPERTIES-DAG: FloatingPointInstructionCount: 0
187194
; DETAILED-PROPERTIES-DAG: IntegerInstructionCount: 33
188-
; DETAILED-PROPERTIES-DAG: IntegerConstantCount: 20
189-
; DETAILED-PROPERTIES-DAG: FloatingPointConstantCount: 0
195+
; DETAILED-PROPERTIES-DAG: ConstantIntOperandCount: 20
196+
; DETAILED-PROPERTIES-DAG: ConstantFPOperandCount: 0
197+
; DETAILED-PROPERTIES-DAG: ConstantOperandCount: 0
198+
; DETAILED-PROPERTIES-DAG: InstructionOperandCount: 73
199+
; DETAILED-PROPERTIES-DAG: BasicBlockOperandCount: 15
200+
; DETAILED-PROPERTIES-DAG: GlobalValueOperandCount: 0
201+
; DETAILED-PROPERTIES-DAG: InlineAsmOperandCount: 0
202+
; DETAILED-PROPERTIES-DAG: ArgumentOperandCount: 3
203+
; DETAILED-PROPERTIES-DAG: UnknownOperandCount: 0
204+

llvm/unittests/Analysis/FunctionPropertiesAnalysisTest.cpp

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,15 @@ define internal i32 @top() {
137137
EXPECT_EQ(DetailedBranchesFeatures.CastInstructionCount, 0);
138138
EXPECT_EQ(DetailedBranchesFeatures.FloatingPointInstructionCount, 0);
139139
EXPECT_EQ(DetailedBranchesFeatures.IntegerInstructionCount, 4);
140-
EXPECT_EQ(DetailedBranchesFeatures.IntegerConstantCount, 1);
141-
EXPECT_EQ(DetailedBranchesFeatures.FloatingPointConstantCount, 0);
140+
EXPECT_EQ(DetailedBranchesFeatures.ConstantIntOperandCount, 1);
141+
EXPECT_EQ(DetailedBranchesFeatures.ConstantFPOperandCount, 0);
142+
EXPECT_EQ(DetailedBranchesFeatures.ConstantOperandCount, 0);
143+
EXPECT_EQ(DetailedBranchesFeatures.InstructionOperandCount, 4);
144+
EXPECT_EQ(DetailedBranchesFeatures.BasicBlockOperandCount, 4);
145+
EXPECT_EQ(DetailedBranchesFeatures.GlobalValueOperandCount, 2);
146+
EXPECT_EQ(DetailedBranchesFeatures.InlineAsmOperandCount, 0);
147+
EXPECT_EQ(DetailedBranchesFeatures.ArgumentOperandCount, 3);
148+
EXPECT_EQ(DetailedBranchesFeatures.UnknownOperandCount, 0);
142149
EnableDetailedFunctionProperties.setValue(false);
143150
}
144151

@@ -170,8 +177,15 @@ define i64 @f1() {
170177
EXPECT_EQ(DetailedF1Properties.CastInstructionCount, 0);
171178
EXPECT_EQ(DetailedF1Properties.FloatingPointInstructionCount, 0);
172179
EXPECT_EQ(DetailedF1Properties.IntegerInstructionCount, 0);
173-
EXPECT_EQ(DetailedF1Properties.IntegerConstantCount, 3);
174-
EXPECT_EQ(DetailedF1Properties.FloatingPointConstantCount, 0);
180+
EXPECT_EQ(DetailedF1Properties.ConstantIntOperandCount, 3);
181+
EXPECT_EQ(DetailedF1Properties.ConstantFPOperandCount, 0);
182+
EXPECT_EQ(DetailedF1Properties.ConstantOperandCount, 0);
183+
EXPECT_EQ(DetailedF1Properties.InstructionOperandCount, 0);
184+
EXPECT_EQ(DetailedF1Properties.BasicBlockOperandCount, 2);
185+
EXPECT_EQ(DetailedF1Properties.GlobalValueOperandCount, 0);
186+
EXPECT_EQ(DetailedF1Properties.InlineAsmOperandCount, 0);
187+
EXPECT_EQ(DetailedF1Properties.ArgumentOperandCount, 0);
188+
EXPECT_EQ(DetailedF1Properties.UnknownOperandCount, 0);
175189
EnableDetailedFunctionProperties.setValue(false);
176190
}
177191

@@ -796,4 +810,46 @@ declare void @f3()
796810
EXPECT_TRUE(FPU.finishAndTest(FAM));
797811
EXPECT_EQ(FPI, ExpectedFinal);
798812
}
813+
814+
TEST_F(FunctionPropertiesAnalysisTest, DetailedOperandCount) {
815+
LLVMContext C;
816+
std::unique_ptr<Module> M = makeLLVMModule(C,
817+
R"IR(
818+
@a = global i64 1
819+
820+
define i64 @f1(i64 %e) {
821+
%b = load i64, i64* @a
822+
%c = add i64 %b, 2
823+
%d = call i64 asm "mov $1,$0", "=r,r" (i64 %c)
824+
%f = add i64 %d, %e
825+
ret i64 %f
826+
}
827+
)IR");
828+
829+
Function *F1 = M->getFunction("f1");
830+
EnableDetailedFunctionProperties.setValue(true);
831+
FunctionPropertiesInfo DetailedF1Properties = buildFPI(*F1);
832+
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithSingleSuccessor, 0);
833+
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithTwoSuccessors, 0);
834+
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithMoreThanTwoSuccessors, 0);
835+
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithSinglePredecessor, 0);
836+
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithTwoPredecessors, 0);
837+
EXPECT_EQ(DetailedF1Properties.BasicBlocksWithMoreThanTwoPredecessors, 0);
838+
EXPECT_EQ(DetailedF1Properties.BigBasicBlocks, 0);
839+
EXPECT_EQ(DetailedF1Properties.MediumBasicBlocks, 0);
840+
EXPECT_EQ(DetailedF1Properties.SmallBasicBlocks, 1);
841+
EXPECT_EQ(DetailedF1Properties.CastInstructionCount, 0);
842+
EXPECT_EQ(DetailedF1Properties.FloatingPointInstructionCount, 0);
843+
EXPECT_EQ(DetailedF1Properties.IntegerInstructionCount, 4);
844+
EXPECT_EQ(DetailedF1Properties.ConstantIntOperandCount, 1);
845+
EXPECT_EQ(DetailedF1Properties.ConstantFPOperandCount, 0);
846+
EXPECT_EQ(DetailedF1Properties.ConstantOperandCount, 0);
847+
EXPECT_EQ(DetailedF1Properties.InstructionOperandCount, 4);
848+
EXPECT_EQ(DetailedF1Properties.BasicBlockOperandCount, 0);
849+
EXPECT_EQ(DetailedF1Properties.GlobalValueOperandCount, 1);
850+
EXPECT_EQ(DetailedF1Properties.InlineAsmOperandCount, 1);
851+
EXPECT_EQ(DetailedF1Properties.ArgumentOperandCount, 1);
852+
EXPECT_EQ(DetailedF1Properties.UnknownOperandCount, 0);
853+
EnableDetailedFunctionProperties.setValue(false);
854+
}
799855
} // end anonymous namespace

0 commit comments

Comments
 (0)