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/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"
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"; }
121 // getAnalysisUsage - this pass preserves the CFG
122 void getAnalysisUsage(AnalysisUsage &AU) const {
123 AU.setPreservesCFG();
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
131 bool PeepholeOpts::visit(MachineBasicBlock& mvec,
132 MachineBasicBlock::iterator BBI) const {
133 // Remove redundant copy instructions
134 return RemoveUselessCopies(mvec, BBI, target);
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)
148 assert(MBB && "MachineBasicBlock object not found for specified block!");
149 MachineBasicBlock &mvec = *MBB;
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.
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
165 /// createPeepholeOptsPass - Public entry point for peephole optimization
167 FunctionPass* createPeepholeOptsPass(const TargetMachine &TM) {
168 return new PeepholeOpts(TM);
171 } // End llvm namespace