Skip to content

Commit 586f974

Browse files
francisvmSterling-Augustine
authored andcommitted
[CodeGen] Fix InstructionCount remarks for MI bundles (llvm#107621)
For MI bundles, the instruction count remark doesn't count the instructions inside the bundle.
1 parent a522255 commit 586f974

File tree

2 files changed

+108
-7
lines changed

2 files changed

+108
-7
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,20 @@ static bool needFuncLabels(const MachineFunction &MF, const AsmPrinter &Asm) {
17031703
classifyEHPersonality(MF.getFunction().getPersonalityFn()));
17041704
}
17051705

1706+
// Return the mnemonic of a MachineInstr if available, or the MachineInstr
1707+
// opcode name otherwise.
1708+
static StringRef getMIMnemonic(const MachineInstr &MI, MCStreamer &Streamer) {
1709+
const TargetInstrInfo *TII =
1710+
MI.getParent()->getParent()->getSubtarget().getInstrInfo();
1711+
MCInst MCI;
1712+
MCI.setOpcode(MI.getOpcode());
1713+
if (StringRef Name = Streamer.getMnemonic(MCI); !Name.empty())
1714+
return Name;
1715+
StringRef Name = TII->getName(MI.getOpcode());
1716+
assert(!Name.empty() && "Missing mnemonic and name for opcode");
1717+
return Name;
1718+
}
1719+
17061720
/// EmitFunctionBody - This method emits the body and trailer for a
17071721
/// function.
17081722
void AsmPrinter::emitFunctionBody() {
@@ -1746,7 +1760,6 @@ void AsmPrinter::emitFunctionBody() {
17461760
if (!MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() &&
17471761
!MI.isDebugInstr()) {
17481762
HasAnyRealCode = true;
1749-
++NumInstsInFunction;
17501763
}
17511764

17521765
// If there is a pre-instruction symbol, emit a label for it here.
@@ -1845,12 +1858,25 @@ void AsmPrinter::emitFunctionBody() {
18451858
break;
18461859
default:
18471860
emitInstruction(&MI);
1848-
if (CanDoExtraAnalysis) {
1849-
MCInst MCI;
1850-
MCI.setOpcode(MI.getOpcode());
1851-
auto Name = OutStreamer->getMnemonic(MCI);
1852-
auto I = MnemonicCounts.insert({Name, 0u});
1853-
I.first->second++;
1861+
1862+
auto CountInstruction = [&](const MachineInstr &MI) {
1863+
// Skip Meta instructions inside bundles.
1864+
if (MI.isMetaInstruction())
1865+
return;
1866+
++NumInstsInFunction;
1867+
if (CanDoExtraAnalysis) {
1868+
StringRef Name = getMIMnemonic(MI, *OutStreamer);
1869+
++MnemonicCounts[Name];
1870+
}
1871+
};
1872+
if (!MI.isBundle()) {
1873+
CountInstruction(MI);
1874+
break;
1875+
}
1876+
// Separately count all the instructions in a bundle.
1877+
for (auto It = std::next(MI.getIterator());
1878+
It != MBB.end() && It->isInsideBundle(); ++It) {
1879+
CountInstruction(*It);
18541880
}
18551881
break;
18561882
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# RUN: llc -mtriple=riscv32 -verify-machineinstrs -start-before=riscv-expand-pseudo -simplify-mir -o /dev/null -pass-remarks-analysis=asm-printer %s 2>&1 | FileCheck %s
2+
---
3+
name: instrs
4+
tracksRegLiveness: true
5+
body: |
6+
bb.0:
7+
$x0 = ADDI $x0, 0
8+
$x0 = ADDI $x0, 0
9+
$x0 = ADDI $x0, 0
10+
$x0 = LW $x0, 0
11+
$x0 = LW $x0, 0
12+
$x0 = XORI $x0, 0
13+
; CHECK: addi : 3
14+
; CHECK-NEXT: lw : 2
15+
; CHECK-NEXT: xori : 1
16+
; CHECK: 6 instructions in function
17+
...
18+
---
19+
name: bundles
20+
tracksRegLiveness: true
21+
body: |
22+
bb.0:
23+
$x0 = ADDI $x0, 0
24+
BUNDLE {
25+
$x0 = ADDI $x0, 0
26+
$x0 = ADDI $x0, 0
27+
$x0 = LW $x0, 0
28+
}
29+
$x0 = LW $x0, 0
30+
$x0 = XORI $x0, 0
31+
; CHECK: addi : 3
32+
; CHECK-NEXT: lw : 2
33+
; CHECK-NEXT: xori : 1
34+
; CHECK: 6 instructions in function
35+
...
36+
---
37+
name: metainstrs
38+
tracksRegLiveness: true
39+
body: |
40+
bb.0:
41+
$x0 = ADDI $x0, 0
42+
$x0 = ADDI $x0, 0
43+
$x0 = ADDI $x0, 0
44+
$x0 = IMPLICIT_DEF
45+
$x0 = LW $x0, 0
46+
$x0 = LW $x0, 0
47+
CFI_INSTRUCTION adjust_cfa_offset 4
48+
$x0 = XORI $x0, 0
49+
DBG_VALUE $x0, 0
50+
; CHECK: addi : 3
51+
; CHECK-NEXT: lw : 2
52+
; CHECK-NEXT: xori : 1
53+
; CHECK: 6 instructions in function
54+
...
55+
---
56+
name: metabundles
57+
tracksRegLiveness: true
58+
body: |
59+
bb.0:
60+
$x0 = ADDI $x0, 0
61+
BUNDLE {
62+
CFI_INSTRUCTION adjust_cfa_offset 4
63+
$x0 = ADDI $x0, 0
64+
$x0 = ADDI $x0, 0
65+
DBG_VALUE $x0, 0
66+
$x0 = LW $x0, 0
67+
}
68+
$x0 = LW $x0, 0
69+
$x0 = IMPLICIT_DEF
70+
$x0 = XORI $x0, 0
71+
; CHECK: addi : 3
72+
; CHECK-NEXT: lw : 2
73+
; CHECK-NEXT: xori : 1
74+
; CHECK: 6 instructions in function
75+
...

0 commit comments

Comments
 (0)