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