blob: d524c354ed35e8394eab2a5fd80a410ffc5807c5 [file] [log] [blame]
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00001//===--------- PPCPreEmitPeephole.cpp - Late peephole optimizations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// A pre-emit peephole for catching opportunities introduced by late passes such
11// as MachineBlockPlacement.
12//
13//===----------------------------------------------------------------------===//
14
15#include "PPC.h"
16#include "PPCInstrInfo.h"
17#include "PPCSubtarget.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/CodeGen/LivePhysRegs.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/Support/CommandLine.h"
25#include "llvm/ADT/Statistic.h"
26#include "llvm/Support/Debug.h"
27
28using namespace llvm;
29
30#define DEBUG_TYPE "ppc-pre-emit-peephole"
31
32STATISTIC(NumRRConvertedInPreEmit,
33 "Number of r+r instructions converted to r+i in pre-emit peephole");
34STATISTIC(NumRemovedInPreEmit,
35 "Number of instructions deleted in pre-emit peephole");
36
37static cl::opt<bool>
Nemanja Ivanovic4e1f5e02017-12-29 12:22:27 +000038RunPreEmitPeephole("ppc-late-peephole", cl::Hidden, cl::init(true),
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +000039 cl::desc("Run pre-emit peephole optimizations."));
40
41namespace {
42 class PPCPreEmitPeephole : public MachineFunctionPass {
43 public:
44 static char ID;
45 PPCPreEmitPeephole() : MachineFunctionPass(ID) {
46 initializePPCPreEmitPeepholePass(*PassRegistry::getPassRegistry());
47 }
48
49 void getAnalysisUsage(AnalysisUsage &AU) const override {
50 MachineFunctionPass::getAnalysisUsage(AU);
51 }
52
53 MachineFunctionProperties getRequiredProperties() const override {
54 return MachineFunctionProperties().set(
55 MachineFunctionProperties::Property::NoVRegs);
56 }
57
58 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Braunf1caa282017-12-15 22:22:58 +000059 if (skipFunction(MF.getFunction()) || !RunPreEmitPeephole)
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +000060 return false;
61 bool Changed = false;
62 const PPCInstrInfo *TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
63 SmallVector<MachineInstr *, 4> InstrsToErase;
64 for (MachineBasicBlock &MBB : MF) {
65 for (MachineInstr &MI : MBB) {
66 MachineInstr *DefMIToErase = nullptr;
67 if (TII->convertToImmediateForm(MI, &DefMIToErase)) {
68 Changed = true;
69 NumRRConvertedInPreEmit++;
70 DEBUG(dbgs() << "Converted instruction to imm form: ");
71 DEBUG(MI.dump());
72 if (DefMIToErase) {
73 InstrsToErase.push_back(DefMIToErase);
74 }
75 }
76 }
77 }
78 for (MachineInstr *MI : InstrsToErase) {
79 DEBUG(dbgs() << "PPC pre-emit peephole: erasing instruction: ");
80 DEBUG(MI->dump());
81 MI->eraseFromParent();
82 NumRemovedInPreEmit++;
83 }
84 return Changed;
85 }
86 };
87}
88
89INITIALIZE_PASS(PPCPreEmitPeephole, DEBUG_TYPE, "PowerPC Pre-Emit Peephole",
90 false, false)
91char PPCPreEmitPeephole::ID = 0;
92
93FunctionPass *llvm::createPPCPreEmitPeepholePass() {
94 return new PPCPreEmitPeephole();
95}