blob: 9d50f35f5414213a8079d316f50b396ab44a01d0 [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
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000023//************************* Internal Functions *****************************/
24
Chris Lattnere96e2632003-09-01 20:24:06 +000025static inline void
Chris Lattner55291ea2002-10-28 01:41:47 +000026DeleteInstruction(MachineBasicBlock& mvec,
27 MachineBasicBlock::iterator& BBI,
Chris Lattnere96e2632003-09-01 20:24:06 +000028 const TargetMachine& target) {
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000029 // Check if this instruction is in a delay slot of its predecessor.
Misha Brukmanf96eb642003-05-23 19:20:57 +000030 if (BBI != mvec.begin()) {
Chris Lattner3501fea2003-01-14 22:00:31 +000031 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000032 MachineInstr* predMI = *(BBI-1);
Misha Brukmanf96eb642003-05-23 19:20:57 +000033 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.
37 //
38 assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
39 (*BBI)->replace(mii.getNOPOpCode(), 0);
40 return;
41 }
42 }
Vikram S. Adve7e5167a2002-09-27 14:27:37 +000043
44 // The instruction is not in a delay slot, so we can simply erase it.
45 mvec.erase(BBI);
46 BBI = mvec.end();
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000047}
48
49//******************* Individual Peephole Optimizations ********************/
50
Chris Lattnera89d8f72003-09-01 20:38:03 +000051//----------------------------------------------------------------------------
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//----------------------------------------------------------------------------
60
61static 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());
Vikram S. Adved2486522003-10-21 12:29:45 +000066 } else if (MI->getOpCode() == V9::ADDr || MI->getOpCode() == V9::ORr ||
67 MI->getOpCode() == V9::ADDi || MI->getOpCode() == V9::ORi) {
Chris Lattnera89d8f72003-09-01 20:38:03 +000068 unsigned srcWithDestReg;
69
70 for (srcWithDestReg = 0; srcWithDestReg < 2; ++srcWithDestReg)
71 if (MI->getOperand(srcWithDestReg).hasAllocatedReg() &&
72 MI->getOperand(srcWithDestReg).getAllocatedRegNum()
73 == MI->getOperand(2).getAllocatedRegNum())
74 break;
75
76 if (srcWithDestReg == 2)
77 return false;
78 else {
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()) ||
85
86 /* or operand otherOp == 0 */
87 (MI->getOperand(otherOp).getType()
88 == MachineOperand::MO_SignExtendedImmed &&
89 MI->getOperand(otherOp).getImmedValue() == 0));
90 }
91 }
92 else
93 return false;
94}
95
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000096inline bool
Chris Lattner55291ea2002-10-28 01:41:47 +000097RemoveUselessCopies(MachineBasicBlock& mvec,
98 MachineBasicBlock::iterator& BBI,
Chris Lattnere96e2632003-09-01 20:24:06 +000099 const TargetMachine& target) {
Chris Lattnera89d8f72003-09-01 20:38:03 +0000100 if (IsUselessCopy(target, *BBI)) {
Misha Brukmanf96eb642003-05-23 19:20:57 +0000101 DeleteInstruction(mvec, BBI, target);
102 return true;
103 }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000104 return false;
105}
106
107
108//************************ Class Implementations **************************/
109
110class PeepholeOpts: public BasicBlockPass {
111 const TargetMachine &target;
Chris Lattner55291ea2002-10-28 01:41:47 +0000112 bool visit(MachineBasicBlock& mvec,
113 MachineBasicBlock::iterator BBI) const;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000114public:
115 PeepholeOpts(const TargetMachine &T): target(T) { }
116 bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
Chris Lattnera8e40f52003-08-14 14:43:24 +0000117 virtual const char *getPassName() const { return "Peephole Optimization"; }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000118};
119
Chris Lattnere96e2632003-09-01 20:24:06 +0000120// 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
123//
124bool PeepholeOpts::visit(MachineBasicBlock& mvec,
125 MachineBasicBlock::iterator BBI) const {
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000126 /* Remove redundant copy instructions */
Chris Lattnere96e2632003-09-01 20:24:06 +0000127 return RemoveUselessCopies(mvec, BBI, target);
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000128}
129
130
Chris Lattnere96e2632003-09-01 20:24:06 +0000131bool PeepholeOpts::runOnBasicBlock(BasicBlock &BB) {
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000132 // Get the machine instructions for this BB
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000133 // 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;
Chris Lattnere96e2632003-09-01 20:24:06 +0000137 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000138 if (I->getBasicBlock() == &BB)
139 MBB = I;
Chris Lattnere96e2632003-09-01 20:24:06 +0000140
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000141 assert(MBB && "MachineBasicBlock object not found for specified block!");
142 MachineBasicBlock &mvec = *MBB;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000143
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.
147 //
Chris Lattner55291ea2002-10-28 01:41:47 +0000148 for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
Chris Lattnere96e2632003-09-01 20:24:06 +0000149 RE=mvec.rend(); RI != RE; ) {
Misha Brukmanf96eb642003-05-23 19:20:57 +0000150 MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
151 ++RI; // pre-increment to delete MI or after it
152 visit(mvec, BBI);
153 }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000154
155 return true;
156}
157
158
159//===----------------------------------------------------------------------===//
160// createPeepholeOptsPass - Public entrypoint for peephole optimization
161// and this file as a whole...
162//
Chris Lattnere96e2632003-09-01 20:24:06 +0000163FunctionPass* createPeepholeOptsPass(TargetMachine &T) {
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000164 return new PeepholeOpts(T);
165}