Simplify code a bit
[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 static inline void
20 DeleteInstruction(MachineBasicBlock& mvec,
21                   MachineBasicBlock::iterator& BBI,
22                   const TargetMachine& target) {
23   // Check if this instruction is in a delay slot of its predecessor.
24   if (BBI != mvec.begin()) {
25       const TargetInstrInfo& mii = target.getInstrInfo();
26       MachineInstr* predMI = *(BBI-1);
27       if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode())) {
28         // This instruction is in a delay slot of its predecessor, so
29         // replace it with a nop. By replacing in place, we save having
30         // to update the I-I maps.
31         // 
32         assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
33         (*BBI)->replace(mii.getNOPOpCode(), 0);
34         return;
35       }
36   }
37   
38   // The instruction is not in a delay slot, so we can simply erase it.
39   mvec.erase(BBI);
40   BBI = mvec.end();
41 }
42
43 //******************* Individual Peephole Optimizations ********************/
44
45 inline bool
46 RemoveUselessCopies(MachineBasicBlock& mvec,
47                     MachineBasicBlock::iterator& BBI,
48                     const TargetMachine& target) {
49   if (target.getOptInfo().IsUselessCopy(*BBI)) {
50     DeleteInstruction(mvec, BBI, target);
51     return true;
52   }
53   return false;
54 }
55
56
57 //************************ Class Implementations **************************/
58
59 class PeepholeOpts: public BasicBlockPass {
60   const TargetMachine ⌖
61   bool visit(MachineBasicBlock& mvec,
62              MachineBasicBlock::iterator BBI) const;
63 public:
64   PeepholeOpts(const TargetMachine &T): target(T) { }
65   bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
66   virtual const char *getPassName() const { return "Peephole Optimization"; }
67 };
68
69 // Apply a list of peephole optimizations to this machine instruction
70 // within its local context.  They are allowed to delete MI or any
71 // instruction before MI, but not 
72 //
73 bool PeepholeOpts::visit(MachineBasicBlock& mvec,
74                          MachineBasicBlock::iterator BBI) const {
75   /* Remove redundant copy instructions */
76   return RemoveUselessCopies(mvec, BBI, target);
77 }
78
79
80 bool PeepholeOpts::runOnBasicBlock(BasicBlock &BB) {
81   // Get the machine instructions for this BB
82   // FIXME: MachineBasicBlock::get() is deprecated, hence inlining the function
83   const Function *F = BB.getParent();
84   MachineFunction &MF = MachineFunction::get(F);
85   MachineBasicBlock *MBB = NULL;
86   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
87     if (I->getBasicBlock() == &BB)
88       MBB = I;
89
90   assert(MBB && "MachineBasicBlock object not found for specified block!");
91   MachineBasicBlock &mvec = *MBB;
92
93   // Iterate over all machine instructions in the BB
94   // Use a reverse iterator to allow deletion of MI or any instruction after it.
95   // Insertions or deletions *before* MI are not safe.
96   // 
97   for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
98          RE=mvec.rend(); RI != RE; ) {
99     MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
100     ++RI;             // pre-increment to delete MI or after it
101     visit(mvec, BBI);
102   }
103
104   return true;
105 }
106
107
108 //===----------------------------------------------------------------------===//
109 // createPeepholeOptsPass - Public entrypoint for peephole optimization
110 // and this file as a whole...
111 //
112 FunctionPass* createPeepholeOptsPass(TargetMachine &T) {
113   return new PeepholeOpts(T);
114 }