1 //===--------- 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
28 using namespace llvm;
29
30 #define DEBUG_TYPE "ppc-pre-emit-peephole"
31
32 STATISTIC(NumRRConvertedInPreEmit,
33 "Number of r+r instructions converted to r+i in pre-emit peephole");
34 STATISTIC(NumRemovedInPreEmit,
35 "Number of instructions deleted in pre-emit peephole");
36
37 static cl::opt<bool>
38 RunPreEmitPeephole("ppc-late-peephole", cl::Hidden, cl::init(true),
39 cl::desc("Run pre-emit peephole optimizations."));
40
41 namespace {
42 class PPCPreEmitPeephole : public MachineFunctionPass {
43 public:
44 static char ID;
PPCPreEmitPeephole()45 PPCPreEmitPeephole() : MachineFunctionPass(ID) {
46 initializePPCPreEmitPeepholePass(*PassRegistry::getPassRegistry());
47 }
48
getAnalysisUsage(AnalysisUsage & AU) const49 void getAnalysisUsage(AnalysisUsage &AU) const override {
50 MachineFunctionPass::getAnalysisUsage(AU);
51 }
52
getRequiredProperties() const53 MachineFunctionProperties getRequiredProperties() const override {
54 return MachineFunctionProperties().set(
55 MachineFunctionProperties::Property::NoVRegs);
56 }
57
runOnMachineFunction(MachineFunction & MF)58 bool runOnMachineFunction(MachineFunction &MF) override {
59 if (skipFunction(MF.getFunction()) || !RunPreEmitPeephole)
60 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 LLVM_DEBUG(dbgs() << "Converted instruction to imm form: ");
71 LLVM_DEBUG(MI.dump());
72 if (DefMIToErase) {
73 InstrsToErase.push_back(DefMIToErase);
74 }
75 }
76 }
77 }
78 for (MachineInstr *MI : InstrsToErase) {
79 LLVM_DEBUG(dbgs() << "PPC pre-emit peephole: erasing instruction: ");
80 LLVM_DEBUG(MI->dump());
81 MI->eraseFromParent();
82 NumRemovedInPreEmit++;
83 }
84 return Changed;
85 }
86 };
87 }
88
89 INITIALIZE_PASS(PPCPreEmitPeephole, DEBUG_TYPE, "PowerPC Pre-Emit Peephole",
90 false, false)
91 char PPCPreEmitPeephole::ID = 0;
92
createPPCPreEmitPeepholePass()93 FunctionPass *llvm::createPPCPreEmitPeepholePass() {
94 return new PPCPreEmitPeephole();
95 }
96