blob: 9713a02d95e9d02e9d7c9076ecccfe081e201fce [file] [log] [blame]
Chris Lattner55291ea2002-10-28 01:41:47 +00001//===-- PeepholeOpts.cpp --------------------------------------------------===//
Vikram S. Adve25d80cd2002-09-20 00:42:11 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner55291ea2002-10-28 01:41:47 +000010// Support for performing several peephole opts in one or a few passes over the
11// machine code of a method.
12//
13//===----------------------------------------------------------------------===//
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000014
Chris Lattner67699ff2003-09-01 20:33:07 +000015#include "SparcInternals.h"
Misha Brukmanb7551ef2002-10-28 20:00:31 +000016#include "llvm/CodeGen/MachineFunction.h"
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000017#include "llvm/CodeGen/MachineInstr.h"
18#include "llvm/Target/TargetMachine.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000019#include "llvm/Target/TargetInstrInfo.h"
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000020#include "llvm/BasicBlock.h"
21#include "llvm/Pass.h"
22
Brian Gaeked0fde302003-11-11 22:41:34 +000023namespace llvm {
24
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000025//************************* Internal Functions *****************************/
26
Chris Lattnere96e2632003-09-01 20:24:06 +000027static inline void
Chris Lattner55291ea2002-10-28 01:41:47 +000028DeleteInstruction(MachineBasicBlock& mvec,
29 MachineBasicBlock::iterator& BBI,
Chris Lattnere96e2632003-09-01 20:24:06 +000030 const TargetMachine& target) {
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000031 // Check if this instruction is in a delay slot of its predecessor.
Misha Brukmanf96eb642003-05-23 19:20:57 +000032 if (BBI != mvec.begin()) {
Chris Lattner3501fea2003-01-14 22:00:31 +000033 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000034 MachineInstr* predMI = *(BBI-1);
Misha Brukmanf96eb642003-05-23 19:20:57 +000035 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.
39 //
40 assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
41 (*BBI)->replace(mii.getNOPOpCode(), 0);
42 return;
43 }
44 }
Vikram S. Adve7e5167a2002-09-27 14:27:37 +000045
46 // The instruction is not in a delay slot, so we can simply erase it.
47 mvec.erase(BBI);
48 BBI = mvec.end();
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000049}
50
51//******************* Individual Peephole Optimizations ********************/
52
Chris Lattnera89d8f72003-09-01 20:38:03 +000053//----------------------------------------------------------------------------
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//----------------------------------------------------------------------------
62
63static 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());
Vikram S. Adved2486522003-10-21 12:29:45 +000068 } else if (MI->getOpCode() == V9::ADDr || MI->getOpCode() == V9::ORr ||
69 MI->getOpCode() == V9::ADDi || MI->getOpCode() == V9::ORi) {
Chris Lattnera89d8f72003-09-01 20:38:03 +000070 unsigned srcWithDestReg;
71
72 for (srcWithDestReg = 0; srcWithDestReg < 2; ++srcWithDestReg)
73 if (MI->getOperand(srcWithDestReg).hasAllocatedReg() &&
74 MI->getOperand(srcWithDestReg).getAllocatedRegNum()
75 == MI->getOperand(2).getAllocatedRegNum())
76 break;
77
78 if (srcWithDestReg == 2)
79 return false;
80 else {
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()) ||
87
88 /* or operand otherOp == 0 */
89 (MI->getOperand(otherOp).getType()
90 == MachineOperand::MO_SignExtendedImmed &&
91 MI->getOperand(otherOp).getImmedValue() == 0));
92 }
93 }
94 else
95 return false;
96}
97
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000098inline bool
Chris Lattner55291ea2002-10-28 01:41:47 +000099RemoveUselessCopies(MachineBasicBlock& mvec,
100 MachineBasicBlock::iterator& BBI,
Chris Lattnere96e2632003-09-01 20:24:06 +0000101 const TargetMachine& target) {
Chris Lattnera89d8f72003-09-01 20:38:03 +0000102 if (IsUselessCopy(target, *BBI)) {
Misha Brukmanf96eb642003-05-23 19:20:57 +0000103 DeleteInstruction(mvec, BBI, target);
104 return true;
105 }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000106 return false;
107}
108
109
110//************************ Class Implementations **************************/
111
112class PeepholeOpts: public BasicBlockPass {
113 const TargetMachine &target;
Chris Lattner55291ea2002-10-28 01:41:47 +0000114 bool visit(MachineBasicBlock& mvec,
115 MachineBasicBlock::iterator BBI) const;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000116public:
Misha Brukman941fc9a2003-11-07 17:44:18 +0000117 PeepholeOpts(const TargetMachine &TM): target(TM) { }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000118 bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
Chris Lattnera8e40f52003-08-14 14:43:24 +0000119 virtual const char *getPassName() const { return "Peephole Optimization"; }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000120};
121
Chris Lattnere96e2632003-09-01 20:24:06 +0000122// 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
125//
126bool PeepholeOpts::visit(MachineBasicBlock& mvec,
127 MachineBasicBlock::iterator BBI) const {
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000128 /* Remove redundant copy instructions */
Chris Lattnere96e2632003-09-01 20:24:06 +0000129 return RemoveUselessCopies(mvec, BBI, target);
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000130}
131
132
Chris Lattnere96e2632003-09-01 20:24:06 +0000133bool PeepholeOpts::runOnBasicBlock(BasicBlock &BB) {
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000134 // Get the machine instructions for this BB
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000135 // 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;
Chris Lattnere96e2632003-09-01 20:24:06 +0000139 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000140 if (I->getBasicBlock() == &BB)
141 MBB = I;
Chris Lattnere96e2632003-09-01 20:24:06 +0000142
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000143 assert(MBB && "MachineBasicBlock object not found for specified block!");
144 MachineBasicBlock &mvec = *MBB;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000145
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.
149 //
Chris Lattner55291ea2002-10-28 01:41:47 +0000150 for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
Chris Lattnere96e2632003-09-01 20:24:06 +0000151 RE=mvec.rend(); RI != RE; ) {
Misha Brukmanf96eb642003-05-23 19:20:57 +0000152 MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
153 ++RI; // pre-increment to delete MI or after it
154 visit(mvec, BBI);
155 }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000156
157 return true;
158}
159
160
161//===----------------------------------------------------------------------===//
162// createPeepholeOptsPass - Public entrypoint for peephole optimization
163// and this file as a whole...
164//
Misha Brukman941fc9a2003-11-07 17:44:18 +0000165FunctionPass* createPeepholeOptsPass(const TargetMachine &TM) {
166 return new PeepholeOpts(TM);
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000167}
Brian Gaeked0fde302003-11-11 22:41:34 +0000168
169} // End llvm namespace