1 //===-- PeepholeOpts.cpp --------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Support for performing several peephole opts in one or a few passes over the
11 // machine code of a method.
13 //===----------------------------------------------------------------------===//
15 #include "SparcInternals.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20 #include "llvm/BasicBlock.h"
21 #include "llvm/Pass.h"
25 //************************* Internal Functions *****************************/
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.
40 assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
41 (*BBI)->replace(mii.getNOPOpCode(), 0);
46 // The instruction is not in a delay slot, so we can simply erase it.
51 //******************* Individual Peephole Optimizations ********************/
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 //----------------------------------------------------------------------------
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;
72 for (srcWithDestReg = 0; srcWithDestReg < 2; ++srcWithDestReg)
73 if (MI->getOperand(srcWithDestReg).hasAllocatedReg() &&
74 MI->getOperand(srcWithDestReg).getAllocatedRegNum()
75 == MI->getOperand(2).getAllocatedRegNum())
78 if (srcWithDestReg == 2)
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()) ||
88 /* or operand otherOp == 0 */
89 (MI->getOperand(otherOp).getType()
90 == MachineOperand::MO_SignExtendedImmed &&
91 MI->getOperand(otherOp).getImmedValue() == 0));
99 RemoveUselessCopies(MachineBasicBlock& mvec,
100 MachineBasicBlock::iterator& BBI,
101 const TargetMachine& target) {
102 if (IsUselessCopy(target, *BBI)) {
103 DeleteInstruction(mvec, BBI, target);
110 //************************ Class Implementations **************************/
112 class PeepholeOpts: public BasicBlockPass {
113 const TargetMachine ⌖
114 bool visit(MachineBasicBlock& mvec,
115 MachineBasicBlock::iterator BBI) const;
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"; }
122 // Apply a list of peephole optimizations to this machine instruction
123 // within its local context. They are allowed to delete MI or any
124 // instruction before MI, but not
126 bool PeepholeOpts::visit(MachineBasicBlock& mvec,
127 MachineBasicBlock::iterator BBI) const {
128 /* Remove redundant copy instructions */
129 return RemoveUselessCopies(mvec, BBI, target);
133 bool PeepholeOpts::runOnBasicBlock(BasicBlock &BB) {
134 // Get the machine instructions for this BB
135 // FIXME: MachineBasicBlock::get() is deprecated, hence inlining the function
136 const Function *F = BB.getParent();
137 MachineFunction &MF = MachineFunction::get(F);
138 MachineBasicBlock *MBB = NULL;
139 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
140 if (I->getBasicBlock() == &BB)
143 assert(MBB && "MachineBasicBlock object not found for specified block!");
144 MachineBasicBlock &mvec = *MBB;
146 // Iterate over all machine instructions in the BB
147 // Use a reverse iterator to allow deletion of MI or any instruction after it.
148 // Insertions or deletions *before* MI are not safe.
150 for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
151 RE=mvec.rend(); RI != RE; ) {
152 MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
153 ++RI; // pre-increment to delete MI or after it
161 //===----------------------------------------------------------------------===//
162 // createPeepholeOptsPass - Public entrypoint for peephole optimization
163 // and this file as a whole...
165 FunctionPass* createPeepholeOptsPass(const TargetMachine &TM) {
166 return new PeepholeOpts(TM);
169 } // End llvm namespace