1 //===-- SparcV9PeepholeOpts.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 "SparcV9Internals.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 #include "llvm/ADT/STLExtras.h"
26 //************************* Internal Functions *****************************/
29 DeleteInstruction(MachineBasicBlock& mvec,
30 MachineBasicBlock::iterator& BBI,
31 const TargetMachine& target) {
32 // Check if this instruction is in a delay slot of its predecessor.
33 if (BBI != mvec.begin()) {
34 const TargetInstrInfo& mii = *target.getInstrInfo();
35 MachineBasicBlock::iterator predMI = prior(BBI);
36 if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpcode())) {
37 // This instruction is in a delay slot of its predecessor, so
38 // replace it with a nop. By replacing in place, we save having
39 // to update the I-I maps.
41 assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
42 BBI->replace(V9::NOP, 0);
47 // The instruction is not in a delay slot, so we can simply erase it.
52 //******************* Individual Peephole Optimizations ********************/
54 //----------------------------------------------------------------------------
55 // Function: IsUselessCopy
56 // Decide whether a machine instruction is a redundant copy:
57 // -- ADD with g0 and result and operand are identical, or
58 // -- OR with g0 and result and operand are identical, or
59 // -- FMOVS or FMOVD and result and operand are identical.
60 // Other cases are possible but very rare that they would be useless copies,
61 // so it's not worth analyzing them.
62 //----------------------------------------------------------------------------
64 static bool IsUselessCopy(const TargetMachine &target, const MachineInstr* MI) {
65 if (MI->getOpcode() == V9::FMOVS || MI->getOpcode() == V9::FMOVD) {
66 return (// both operands are allocated to the same register
67 MI->getOperand(0).getReg() == MI->getOperand(1).getReg());
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).getReg()
75 == MI->getOperand(2).getReg())
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).getReg() ==
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 for (MachineBasicBlock::iterator I = mvec.begin(), E = mvec.end(); I != E; )
157 /// createPeepholeOptsPass - Public entry point for peephole optimization
159 FunctionPass* createPeepholeOptsPass(const TargetMachine &TM) {
160 return new PeepholeOpts(TM);
163 } // End llvm namespace