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"
23 //************************* Internal Functions *****************************/
26 DeleteInstruction(MachineBasicBlock& mvec,
27 MachineBasicBlock::iterator& BBI,
28 const TargetMachine& target) {
29 // Check if this instruction is in a delay slot of its predecessor.
30 if (BBI != mvec.begin()) {
31 const TargetInstrInfo& mii = target.getInstrInfo();
32 MachineInstr* predMI = *(BBI-1);
33 if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode())) {
34 // This instruction is in a delay slot of its predecessor, so
35 // replace it with a nop. By replacing in place, we save having
36 // to update the I-I maps.
38 assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
39 (*BBI)->replace(mii.getNOPOpCode(), 0);
44 // The instruction is not in a delay slot, so we can simply erase it.
49 //******************* Individual Peephole Optimizations ********************/
51 //----------------------------------------------------------------------------
52 // Function: IsUselessCopy
53 // Decide whether a machine instruction is a redundant copy:
54 // -- ADD with g0 and result and operand are identical, or
55 // -- OR with g0 and result and operand are identical, or
56 // -- FMOVS or FMOVD and result and operand are identical.
57 // Other cases are possible but very rare that they would be useless copies,
58 // so it's not worth analyzing them.
59 //----------------------------------------------------------------------------
61 static bool IsUselessCopy(const TargetMachine &target, const MachineInstr* MI) {
62 if (MI->getOpCode() == V9::FMOVS || MI->getOpCode() == V9::FMOVD) {
63 return (/* both operands are allocated to the same register */
64 MI->getOperand(0).getAllocatedRegNum() ==
65 MI->getOperand(1).getAllocatedRegNum());
66 } else if (MI->getOpCode() == V9::ADDr || MI->getOpCode() == V9::ORr ||
67 MI->getOpCode() == V9::ADDi || MI->getOpCode() == V9::ORi) {
68 unsigned srcWithDestReg;
70 for (srcWithDestReg = 0; srcWithDestReg < 2; ++srcWithDestReg)
71 if (MI->getOperand(srcWithDestReg).hasAllocatedReg() &&
72 MI->getOperand(srcWithDestReg).getAllocatedRegNum()
73 == MI->getOperand(2).getAllocatedRegNum())
76 if (srcWithDestReg == 2)
79 /* else source and dest are allocated to the same register */
80 unsigned otherOp = 1 - srcWithDestReg;
81 return (/* either operand otherOp is register %g0 */
82 (MI->getOperand(otherOp).hasAllocatedReg() &&
83 MI->getOperand(otherOp).getAllocatedRegNum() ==
84 target.getRegInfo().getZeroRegNum()) ||
86 /* or operand otherOp == 0 */
87 (MI->getOperand(otherOp).getType()
88 == MachineOperand::MO_SignExtendedImmed &&
89 MI->getOperand(otherOp).getImmedValue() == 0));
97 RemoveUselessCopies(MachineBasicBlock& mvec,
98 MachineBasicBlock::iterator& BBI,
99 const TargetMachine& target) {
100 if (IsUselessCopy(target, *BBI)) {
101 DeleteInstruction(mvec, BBI, target);
108 //************************ Class Implementations **************************/
110 class PeepholeOpts: public BasicBlockPass {
111 const TargetMachine ⌖
112 bool visit(MachineBasicBlock& mvec,
113 MachineBasicBlock::iterator BBI) const;
115 PeepholeOpts(const TargetMachine &TM): target(TM) { }
116 bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
117 virtual const char *getPassName() const { return "Peephole Optimization"; }
120 // Apply a list of peephole optimizations to this machine instruction
121 // within its local context. They are allowed to delete MI or any
122 // instruction before MI, but not
124 bool PeepholeOpts::visit(MachineBasicBlock& mvec,
125 MachineBasicBlock::iterator BBI) const {
126 /* Remove redundant copy instructions */
127 return RemoveUselessCopies(mvec, BBI, target);
131 bool PeepholeOpts::runOnBasicBlock(BasicBlock &BB) {
132 // Get the machine instructions for this BB
133 // FIXME: MachineBasicBlock::get() is deprecated, hence inlining the function
134 const Function *F = BB.getParent();
135 MachineFunction &MF = MachineFunction::get(F);
136 MachineBasicBlock *MBB = NULL;
137 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
138 if (I->getBasicBlock() == &BB)
141 assert(MBB && "MachineBasicBlock object not found for specified block!");
142 MachineBasicBlock &mvec = *MBB;
144 // Iterate over all machine instructions in the BB
145 // Use a reverse iterator to allow deletion of MI or any instruction after it.
146 // Insertions or deletions *before* MI are not safe.
148 for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
149 RE=mvec.rend(); RI != RE; ) {
150 MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
151 ++RI; // pre-increment to delete MI or after it
159 //===----------------------------------------------------------------------===//
160 // createPeepholeOptsPass - Public entrypoint for peephole optimization
161 // and this file as a whole...
163 FunctionPass* createPeepholeOptsPass(const TargetMachine &TM) {
164 return new PeepholeOpts(TM);