Restore 'nice name' to pass
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9PeepholeOpts.cpp
1 //===-- PeepholeOpts.cpp --------------------------------------------------===//
2 // 
3 // Support for performing several peephole opts in one or a few passes over the
4 // machine code of a method.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/CodeGen/PeepholeOpts.h"
9 #include "llvm/CodeGen/MachineFunction.h"
10 #include "llvm/CodeGen/MachineInstr.h"
11 #include "llvm/Target/TargetMachine.h"
12 #include "llvm/Target/TargetInstrInfo.h"
13 #include "llvm/Target/TargetOptInfo.h"
14 #include "llvm/BasicBlock.h"
15 #include "llvm/Pass.h"
16
17 //************************* Internal Functions *****************************/
18
19 inline void
20 DeleteInstruction(MachineBasicBlock& mvec,
21                   MachineBasicBlock::iterator& BBI,
22                   const TargetMachine& target)
23 {
24   // Check if this instruction is in a delay slot of its predecessor.
25   if (BBI != mvec.begin()) {
26       const TargetInstrInfo& mii = target.getInstrInfo();
27       MachineInstr* predMI = *(BBI-1);
28       if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode())) {
29         // This instruction is in a delay slot of its predecessor, so
30         // replace it with a nop. By replacing in place, we save having
31         // to update the I-I maps.
32         // 
33         assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
34         (*BBI)->replace(mii.getNOPOpCode(), 0);
35         return;
36       }
37   }
38   
39   // The instruction is not in a delay slot, so we can simply erase it.
40   mvec.erase(BBI);
41   BBI = mvec.end();
42 }
43
44 //******************* Individual Peephole Optimizations ********************/
45
46
47 inline bool
48 RemoveUselessCopies(MachineBasicBlock& mvec,
49                     MachineBasicBlock::iterator& BBI,
50                     const TargetMachine& target)
51 {
52   if (target.getOptInfo().IsUselessCopy(*BBI)) {
53     DeleteInstruction(mvec, BBI, target);
54     return true;
55   }
56   return false;
57 }
58
59
60 //************************ Class Implementations **************************/
61
62 class PeepholeOpts: public BasicBlockPass {
63   const TargetMachine ⌖
64   bool visit(MachineBasicBlock& mvec,
65              MachineBasicBlock::iterator BBI) const;
66 public:
67   PeepholeOpts(const TargetMachine &T): target(T) { }
68   bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
69   virtual const char *getPassName() const { return "Peephole Optimization"; }
70 };
71
72 /* Apply a list of peephole optimizations to this machine instruction
73  * within its local context.  They are allowed to delete MI or any
74  * instruction before MI, but not 
75  */
76 bool
77 PeepholeOpts::visit(MachineBasicBlock& mvec,
78                     MachineBasicBlock::iterator BBI) const
79 {
80   bool changed = false;
81
82   /* Remove redundant copy instructions */
83   changed |= RemoveUselessCopies(mvec, BBI, target);
84   if (BBI == mvec.end())                // nothing more to do!
85     return changed;
86
87   return changed;
88 }
89
90
91 bool
92 PeepholeOpts::runOnBasicBlock(BasicBlock &BB)
93 {
94   // Get the machine instructions for this BB
95   // FIXME: MachineBasicBlock::get() is deprecated, hence inlining the function
96   const Function *F = BB.getParent();
97   MachineFunction &MF = MachineFunction::get(F);
98   MachineBasicBlock *MBB = NULL;
99   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
100     if (I->getBasicBlock() == &BB)
101       MBB = I;
102   }
103   assert(MBB && "MachineBasicBlock object not found for specified block!");
104   MachineBasicBlock &mvec = *MBB;
105
106   // Iterate over all machine instructions in the BB
107   // Use a reverse iterator to allow deletion of MI or any instruction after it.
108   // Insertions or deletions *before* MI are not safe.
109   // 
110   for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
111          RE=mvec.rend(); RI != RE; )
112   {
113     MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
114     ++RI;             // pre-increment to delete MI or after it
115     visit(mvec, BBI);
116   }
117
118   return true;
119 }
120
121
122 //===----------------------------------------------------------------------===//
123 // createPeepholeOptsPass - Public entrypoint for peephole optimization
124 // and this file as a whole...
125 //
126 FunctionPass*
127 createPeepholeOptsPass(TargetMachine &T)
128 {
129   return new PeepholeOpts(T);
130 }