LLVM  7.0.0
PPCPreEmitPeephole.cpp
Go to the documentation of this file.
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"
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;
45  PPCPreEmitPeephole() : MachineFunctionPass(ID) {
47  }
48 
49  void getAnalysisUsage(AnalysisUsage &AU) const override {
51  }
52 
53  MachineFunctionProperties getRequiredProperties() const override {
56  }
57 
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 
94  return new PPCPreEmitPeephole();
95 }
void push_back(const T &Elt)
Definition: SmallVector.h:218
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
STATISTIC(NumFunctions, "Total number of functions")
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
#define DEBUG_TYPE
void dump() const
Definition: Pass.cpp:130
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:410
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:34
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
Definition: MachineInstr.h:60
void initializePPCPreEmitPeepholePass(PassRegistry &)
FunctionPass * createPPCPreEmitPeepholePass()
IRTranslator LLVM IR MI
static cl::opt< bool > RunPreEmitPeephole("ppc-late-peephole", cl::Hidden, cl::init(true), cl::desc("Run pre-emit peephole optimizations."))
#define LLVM_DEBUG(X)
Definition: Debug.h:119
bool convertToImmediateForm(MachineInstr &MI, MachineInstr **KilledDef=nullptr) const
Properties which a MachineFunction may have at a given point in time.