0dbcdf3cb9c18f56dfcfae1e8697b78d254e071e
[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 "SparcInternals.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/BasicBlock.h"
14 #include "llvm/Pass.h"
15
16 //************************* Internal Functions *****************************/
17
18 static inline void
19 DeleteInstruction(MachineBasicBlock& mvec,
20                   MachineBasicBlock::iterator& BBI,
21                   const TargetMachine& target) {
22   // Check if this instruction is in a delay slot of its predecessor.
23   if (BBI != mvec.begin()) {
24       const TargetInstrInfo& mii = target.getInstrInfo();
25       MachineInstr* predMI = *(BBI-1);
26       if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode())) {
27         // This instruction is in a delay slot of its predecessor, so
28         // replace it with a nop. By replacing in place, we save having
29         // to update the I-I maps.
30         // 
31         assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
32         (*BBI)->replace(mii.getNOPOpCode(), 0);
33         return;
34       }
35   }
36   
37   // The instruction is not in a delay slot, so we can simply erase it.
38   mvec.erase(BBI);
39   BBI = mvec.end();
40 }
41
42 //******************* Individual Peephole Optimizations ********************/
43
44 //----------------------------------------------------------------------------
45 // Function: IsUselessCopy
46 // Decide whether a machine instruction is a redundant copy:
47 // -- ADD    with g0 and result and operand are identical, or
48 // -- OR     with g0 and result and operand are identical, or
49 // -- FMOVS or FMOVD and result and operand are identical.
50 // Other cases are possible but very rare that they would be useless copies,
51 // so it's not worth analyzing them.
52 //----------------------------------------------------------------------------
53
54 static bool IsUselessCopy(const TargetMachine &target, const MachineInstr* MI) {
55   if (MI->getOpCode() == V9::FMOVS || MI->getOpCode() == V9::FMOVD) {
56     return (/* both operands are allocated to the same register */
57             MI->getOperand(0).getAllocatedRegNum() == 
58             MI->getOperand(1).getAllocatedRegNum());
59   } else if (MI->getOpCode() == V9::ADDr || MI->getOpCode() == V9::ORr) {
60     unsigned srcWithDestReg;
61     
62     for (srcWithDestReg = 0; srcWithDestReg < 2; ++srcWithDestReg)
63       if (MI->getOperand(srcWithDestReg).hasAllocatedReg() &&
64           MI->getOperand(srcWithDestReg).getAllocatedRegNum()
65           == MI->getOperand(2).getAllocatedRegNum())
66         break;
67     
68     if (srcWithDestReg == 2)
69       return false;
70     else {
71       /* else source and dest are allocated to the same register */
72       unsigned otherOp = 1 - srcWithDestReg;
73       return (/* either operand otherOp is register %g0 */
74               (MI->getOperand(otherOp).hasAllocatedReg() &&
75                MI->getOperand(otherOp).getAllocatedRegNum() ==
76                target.getRegInfo().getZeroRegNum()) ||
77               
78               /* or operand otherOp == 0 */
79               (MI->getOperand(otherOp).getType()
80                == MachineOperand::MO_SignExtendedImmed &&
81                MI->getOperand(otherOp).getImmedValue() == 0));
82     }
83   }
84   else
85     return false;
86 }
87
88 inline bool
89 RemoveUselessCopies(MachineBasicBlock& mvec,
90                     MachineBasicBlock::iterator& BBI,
91                     const TargetMachine& target) {
92   if (IsUselessCopy(target, *BBI)) {
93     DeleteInstruction(mvec, BBI, target);
94     return true;
95   }
96   return false;
97 }
98
99
100 //************************ Class Implementations **************************/
101
102 class PeepholeOpts: public BasicBlockPass {
103   const TargetMachine &target;
104   bool visit(MachineBasicBlock& mvec,
105              MachineBasicBlock::iterator BBI) const;
106 public:
107   PeepholeOpts(const TargetMachine &T): target(T) { }
108   bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
109   virtual const char *getPassName() const { return "Peephole Optimization"; }
110 };
111
112 // Apply a list of peephole optimizations to this machine instruction
113 // within its local context.  They are allowed to delete MI or any
114 // instruction before MI, but not 
115 //
116 bool PeepholeOpts::visit(MachineBasicBlock& mvec,
117                          MachineBasicBlock::iterator BBI) const {
118   /* Remove redundant copy instructions */
119   return RemoveUselessCopies(mvec, BBI, target);
120 }
121
122
123 bool PeepholeOpts::runOnBasicBlock(BasicBlock &BB) {
124   // Get the machine instructions for this BB
125   // FIXME: MachineBasicBlock::get() is deprecated, hence inlining the function
126   const Function *F = BB.getParent();
127   MachineFunction &MF = MachineFunction::get(F);
128   MachineBasicBlock *MBB = NULL;
129   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
130     if (I->getBasicBlock() == &BB)
131       MBB = I;
132
133   assert(MBB && "MachineBasicBlock object not found for specified block!");
134   MachineBasicBlock &mvec = *MBB;
135
136   // Iterate over all machine instructions in the BB
137   // Use a reverse iterator to allow deletion of MI or any instruction after it.
138   // Insertions or deletions *before* MI are not safe.
139   // 
140   for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
141          RE=mvec.rend(); RI != RE; ) {
142     MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
143     ++RI;             // pre-increment to delete MI or after it
144     visit(mvec, BBI);
145   }
146
147   return true;
148 }
149
150
151 //===----------------------------------------------------------------------===//
152 // createPeepholeOptsPass - Public entrypoint for peephole optimization
153 // and this file as a whole...
154 //
155 FunctionPass* createPeepholeOptsPass(TargetMachine &T) {
156   return new PeepholeOpts(T);
157 }