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