1 //===-- PeepholeOpts.cpp --------------------------------------------------===//
3 // Support for performing several peephole opts in one or a few passes over the
4 // machine code of a method.
6 //===----------------------------------------------------------------------===//
8 #include "SparcInternals.h"
9 #include "llvm/CodeGen/MachineFunction.h"
10 #include "llvm/CodeGen/MachineInstr.h"
11 #include "llvm/Target/TargetMachine.h"
12 #include "llvm/Target/TargetInstrInfo.h"
13 #include "llvm/BasicBlock.h"
14 #include "llvm/Pass.h"
16 //************************* Internal Functions *****************************/
19 DeleteInstruction(MachineBasicBlock& mvec,
20 MachineBasicBlock::iterator& BBI,
21 const TargetMachine& target) {
22 // Check if this instruction is in a delay slot of its predecessor.
23 if (BBI != mvec.begin()) {
24 const TargetInstrInfo& mii = target.getInstrInfo();
25 MachineInstr* predMI = *(BBI-1);
26 if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode())) {
27 // This instruction is in a delay slot of its predecessor, so
28 // replace it with a nop. By replacing in place, we save having
29 // to update the I-I maps.
31 assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
32 (*BBI)->replace(mii.getNOPOpCode(), 0);
37 // The instruction is not in a delay slot, so we can simply erase it.
42 //******************* Individual Peephole Optimizations ********************/
44 //----------------------------------------------------------------------------
45 // Function: IsUselessCopy
46 // Decide whether a machine instruction is a redundant copy:
47 // -- ADD with g0 and result and operand are identical, or
48 // -- OR with g0 and result and operand are identical, or
49 // -- FMOVS or FMOVD and result and operand are identical.
50 // Other cases are possible but very rare that they would be useless copies,
51 // so it's not worth analyzing them.
52 //----------------------------------------------------------------------------
54 static bool IsUselessCopy(const TargetMachine &target, const MachineInstr* MI) {
55 if (MI->getOpCode() == V9::FMOVS || MI->getOpCode() == V9::FMOVD) {
56 return (/* both operands are allocated to the same register */
57 MI->getOperand(0).getAllocatedRegNum() ==
58 MI->getOperand(1).getAllocatedRegNum());
59 } else if (MI->getOpCode() == V9::ADDr || MI->getOpCode() == V9::ORr) {
60 unsigned srcWithDestReg;
62 for (srcWithDestReg = 0; srcWithDestReg < 2; ++srcWithDestReg)
63 if (MI->getOperand(srcWithDestReg).hasAllocatedReg() &&
64 MI->getOperand(srcWithDestReg).getAllocatedRegNum()
65 == MI->getOperand(2).getAllocatedRegNum())
68 if (srcWithDestReg == 2)
71 /* else source and dest are allocated to the same register */
72 unsigned otherOp = 1 - srcWithDestReg;
73 return (/* either operand otherOp is register %g0 */
74 (MI->getOperand(otherOp).hasAllocatedReg() &&
75 MI->getOperand(otherOp).getAllocatedRegNum() ==
76 target.getRegInfo().getZeroRegNum()) ||
78 /* or operand otherOp == 0 */
79 (MI->getOperand(otherOp).getType()
80 == MachineOperand::MO_SignExtendedImmed &&
81 MI->getOperand(otherOp).getImmedValue() == 0));
89 RemoveUselessCopies(MachineBasicBlock& mvec,
90 MachineBasicBlock::iterator& BBI,
91 const TargetMachine& target) {
92 if (IsUselessCopy(target, *BBI)) {
93 DeleteInstruction(mvec, BBI, target);
100 //************************ Class Implementations **************************/
102 class PeepholeOpts: public BasicBlockPass {
103 const TargetMachine ⌖
104 bool visit(MachineBasicBlock& mvec,
105 MachineBasicBlock::iterator BBI) const;
107 PeepholeOpts(const TargetMachine &T): target(T) { }
108 bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
109 virtual const char *getPassName() const { return "Peephole Optimization"; }
112 // Apply a list of peephole optimizations to this machine instruction
113 // within its local context. They are allowed to delete MI or any
114 // instruction before MI, but not
116 bool PeepholeOpts::visit(MachineBasicBlock& mvec,
117 MachineBasicBlock::iterator BBI) const {
118 /* Remove redundant copy instructions */
119 return RemoveUselessCopies(mvec, BBI, target);
123 bool PeepholeOpts::runOnBasicBlock(BasicBlock &BB) {
124 // Get the machine instructions for this BB
125 // FIXME: MachineBasicBlock::get() is deprecated, hence inlining the function
126 const Function *F = BB.getParent();
127 MachineFunction &MF = MachineFunction::get(F);
128 MachineBasicBlock *MBB = NULL;
129 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
130 if (I->getBasicBlock() == &BB)
133 assert(MBB && "MachineBasicBlock object not found for specified block!");
134 MachineBasicBlock &mvec = *MBB;
136 // Iterate over all machine instructions in the BB
137 // Use a reverse iterator to allow deletion of MI or any instruction after it.
138 // Insertions or deletions *before* MI are not safe.
140 for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
141 RE=mvec.rend(); RI != RE; ) {
142 MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
143 ++RI; // pre-increment to delete MI or after it
151 //===----------------------------------------------------------------------===//
152 // createPeepholeOptsPass - Public entrypoint for peephole optimization
153 // and this file as a whole...
155 FunctionPass* createPeepholeOptsPass(TargetMachine &T) {
156 return new PeepholeOpts(T);