Don't bother passing in default value
[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/MachineBasicBlock.h"
10 #include "llvm/CodeGen/MachineInstr.h"
11 #include "llvm/Target/TargetMachine.h"
12 #include "llvm/Target/MachineInstrInfo.h"
13 #include "llvm/Target/MachineOptInfo.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     {
27       const MachineInstrInfo& mii = target.getInstrInfo();
28       MachineInstr* predMI = *(BBI-1);
29       if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode()))
30         {
31           // This instruction is in a delay slot of its predecessor, so
32           // replace it with a nop. By replacing in place, we save having
33           // to update the I-I maps.
34           // 
35           assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
36           (*BBI)->replace(mii.getNOPOpCode(), 0);
37           return;
38         }
39     }
40   
41   // The instruction is not in a delay slot, so we can simply erase it.
42   mvec.erase(BBI);
43   BBI = mvec.end();
44 }
45
46 //******************* Individual Peephole Optimizations ********************/
47
48
49 inline bool
50 RemoveUselessCopies(MachineBasicBlock& mvec,
51                     MachineBasicBlock::iterator& BBI,
52                     const TargetMachine& target)
53 {
54   if (target.getOptInfo().IsUselessCopy(*BBI))
55     {
56       DeleteInstruction(mvec, BBI, target);
57       return true;
58     }
59   return false;
60 }
61
62
63 //************************ Class Implementations **************************/
64
65 class PeepholeOpts: public BasicBlockPass {
66   const TargetMachine ⌖
67   bool visit(MachineBasicBlock& mvec,
68              MachineBasicBlock::iterator BBI) const;
69 public:
70   PeepholeOpts(const TargetMachine &T): target(T) { }
71   bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
72 };
73
74
75 // Register the pass with llc only, and not opt...
76 static RegisterLLC<PeepholeOpts>
77 X("peephole", "Peephole Optimization", createPeepholeOptsPass);
78
79
80 /* Apply a list of peephole optimizations to this machine instruction
81  * within its local context.  They are allowed to delete MI or any
82  * instruction before MI, but not 
83  */
84 bool
85 PeepholeOpts::visit(MachineBasicBlock& mvec,
86                     MachineBasicBlock::iterator BBI) const
87 {
88   bool changed = false;
89
90   /* Remove redundant copy instructions */
91   changed |= RemoveUselessCopies(mvec, BBI, target);
92   if (BBI == mvec.end())                // nothing more to do!
93     return changed;
94
95   return changed;
96 }
97
98
99 bool
100 PeepholeOpts::runOnBasicBlock(BasicBlock &BB)
101 {
102   // Get the machine instructions for this BB
103   MachineBasicBlock& mvec = MachineBasicBlock::get(&BB);
104
105   // Iterate over all machine instructions in the BB
106   // Use a reverse iterator to allow deletion of MI or any instruction after it.
107   // Insertions or deletions *before* MI are not safe.
108   // 
109   for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
110          RE=mvec.rend(); RI != RE; )
111     {
112       MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
113       ++RI;             // pre-increment to delete MI or after it
114       visit(mvec, BBI);
115     }
116
117   return true;
118 }
119
120
121 //===----------------------------------------------------------------------===//
122 // createPeepholeOptsPass - Public entrypoint for peephole optimization
123 // and this file as a whole...
124 //
125 Pass*
126 createPeepholeOptsPass(TargetMachine &T)
127 {
128   return new PeepholeOpts(T);
129 }
130