1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//===----------------------------------------------------------------------===//
10
11#include "RISCVExpandPseudoBase.h"
12#include "RISCVInstrInfo.h"
13#include "RISCVSubtarget.h"
14
15using namespace llvm;
16
17#ifndef NDEBUG
18static unsigned getFuncSizeFromInsts(const MachineFunction &MF,
19 const RISCVInstrInfo *TII) {
20 unsigned Size = 0;
21 for (auto &MBB : MF)
22 for (auto &MI : MBB)
23 Size += TII->getInstSizeInBytes(MI);
24 return Size;
25}
26#endif
27
28bool RISCVExpandPseudoImplBase::run(MachineFunction &MF) {
29 STI = &MF.getSubtarget<RISCVSubtarget>();
30 TII = STI->getInstrInfo();
31
32#ifndef NDEBUG
33 const unsigned OldSize = getFuncSizeFromInsts(MF, TII);
34#endif
35
36 bool Modified = false;
37 for (auto &MBB : MF)
38 Modified |= expandMBB(MBB);
39
40#ifndef NDEBUG
41 const unsigned NewSize = getFuncSizeFromInsts(MF, TII);
42 assert(OldSize >= NewSize &&
43 "Expanding Pseudos should not increase function size estimate");
44#endif
45 return Modified;
46}
47
48bool RISCVExpandPseudoImplBase::expandMBB(MachineBasicBlock &MBB) const {
49 bool Modified = false;
50
51 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
52 while (MBBI != E) {
53 MachineBasicBlock::iterator NMBBI = std::next(x: MBBI);
54 Modified |= expandMI(MBB, MBBI, NextMBBI&: NMBBI);
55 MBBI = NMBBI;
56 }
57
58 return Modified;
59}
60