blob: d8a5515c72ddc48979d81440b5955760bb35e4e8 [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"
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000016#include "llvm/BasicBlock.h"
17#include "llvm/Pass.h"
Misha Brukman97f9cf22003-12-17 22:08:20 +000018#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineInstr.h"
20#include "llvm/Target/TargetInstrInfo.h"
21#include "llvm/Target/TargetMachine.h"
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000022
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();
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000034 MachineBasicBlock::iterator predMI = BBI; --predMI;
Brian Gaeke12c1d2c2004-02-11 20:47:34 +000035 if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpcode())) {
Misha Brukmanf96eb642003-05-23 19:20:57 +000036 // 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");
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000041 BBI->replace(mii.getNOPOpCode(), 0);
Misha Brukmanf96eb642003-05-23 19:20:57 +000042 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) {
Brian Gaeke12c1d2c2004-02-11 20:47:34 +000064 if (MI->getOpcode() == V9::FMOVS || MI->getOpcode() == V9::FMOVD) {
Misha Brukman97f9cf22003-12-17 22:08:20 +000065 return (// both operands are allocated to the same register
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +000066 MI->getOperand(0).getReg() == MI->getOperand(1).getReg());
Brian Gaeke12c1d2c2004-02-11 20:47:34 +000067 } else if (MI->getOpcode() == V9::ADDr || MI->getOpcode() == V9::ORr ||
68 MI->getOpcode() == V9::ADDi || MI->getOpcode() == V9::ORi) {
Chris Lattnera89d8f72003-09-01 20:38:03 +000069 unsigned srcWithDestReg;
70
71 for (srcWithDestReg = 0; srcWithDestReg < 2; ++srcWithDestReg)
72 if (MI->getOperand(srcWithDestReg).hasAllocatedReg() &&
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +000073 MI->getOperand(srcWithDestReg).getReg()
74 == MI->getOperand(2).getReg())
Chris Lattnera89d8f72003-09-01 20:38:03 +000075 break;
76
77 if (srcWithDestReg == 2)
78 return false;
79 else {
Misha Brukman97f9cf22003-12-17 22:08:20 +000080 // else source and dest are allocated to the same register
Chris Lattnera89d8f72003-09-01 20:38:03 +000081 unsigned otherOp = 1 - srcWithDestReg;
Misha Brukman97f9cf22003-12-17 22:08:20 +000082 return (// either operand otherOp is register %g0
Chris Lattnera89d8f72003-09-01 20:38:03 +000083 (MI->getOperand(otherOp).hasAllocatedReg() &&
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +000084 MI->getOperand(otherOp).getReg() ==
Chris Lattnera89d8f72003-09-01 20:38:03 +000085 target.getRegInfo().getZeroRegNum()) ||
86
Misha Brukman97f9cf22003-12-17 22:08:20 +000087 // or operand otherOp == 0
Chris Lattnera89d8f72003-09-01 20:38:03 +000088 (MI->getOperand(otherOp).getType()
89 == MachineOperand::MO_SignExtendedImmed &&
90 MI->getOperand(otherOp).getImmedValue() == 0));
91 }
92 }
93 else
94 return false;
95}
96
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000097inline bool
Chris Lattner55291ea2002-10-28 01:41:47 +000098RemoveUselessCopies(MachineBasicBlock& mvec,
99 MachineBasicBlock::iterator& BBI,
Chris Lattnere96e2632003-09-01 20:24:06 +0000100 const TargetMachine& target) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000101 if (IsUselessCopy(target, BBI)) {
Misha Brukmanf96eb642003-05-23 19:20:57 +0000102 DeleteInstruction(mvec, BBI, target);
103 return true;
104 }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000105 return false;
106}
107
108
109//************************ Class Implementations **************************/
110
111class PeepholeOpts: public BasicBlockPass {
112 const TargetMachine &target;
Chris Lattner55291ea2002-10-28 01:41:47 +0000113 bool visit(MachineBasicBlock& mvec,
114 MachineBasicBlock::iterator BBI) const;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000115public:
Misha Brukman941fc9a2003-11-07 17:44:18 +0000116 PeepholeOpts(const TargetMachine &TM): target(TM) { }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000117 bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
Chris Lattnera8e40f52003-08-14 14:43:24 +0000118 virtual const char *getPassName() const { return "Peephole Optimization"; }
Misha Brukman97f9cf22003-12-17 22:08:20 +0000119
120 // getAnalysisUsage - this pass preserves the CFG
121 void getAnalysisUsage(AnalysisUsage &AU) const {
122 AU.setPreservesCFG();
123 }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000124};
125
Chris Lattnere96e2632003-09-01 20:24:06 +0000126// Apply a list of peephole optimizations to this machine instruction
127// within its local context. They are allowed to delete MI or any
128// instruction before MI, but not
129//
130bool PeepholeOpts::visit(MachineBasicBlock& mvec,
131 MachineBasicBlock::iterator BBI) const {
Misha Brukman97f9cf22003-12-17 22:08:20 +0000132 // Remove redundant copy instructions
Chris Lattnere96e2632003-09-01 20:24:06 +0000133 return RemoveUselessCopies(mvec, BBI, target);
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000134}
135
136
Chris Lattnere96e2632003-09-01 20:24:06 +0000137bool PeepholeOpts::runOnBasicBlock(BasicBlock &BB) {
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000138 // Get the machine instructions for this BB
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000139 // FIXME: MachineBasicBlock::get() is deprecated, hence inlining the function
140 const Function *F = BB.getParent();
141 MachineFunction &MF = MachineFunction::get(F);
142 MachineBasicBlock *MBB = NULL;
Chris Lattnere96e2632003-09-01 20:24:06 +0000143 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000144 if (I->getBasicBlock() == &BB)
145 MBB = I;
Chris Lattnere96e2632003-09-01 20:24:06 +0000146
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000147 assert(MBB && "MachineBasicBlock object not found for specified block!");
148 MachineBasicBlock &mvec = *MBB;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000149
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000150 for (MachineBasicBlock::iterator I = mvec.begin(), E = mvec.end(); I != E; )
151 visit(mvec, I++);
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000152
153 return true;
154}
155
Misha Brukman97f9cf22003-12-17 22:08:20 +0000156/// createPeepholeOptsPass - Public entry point for peephole optimization
157///
Misha Brukman941fc9a2003-11-07 17:44:18 +0000158FunctionPass* createPeepholeOptsPass(const TargetMachine &TM) {
159 return new PeepholeOpts(TM);
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000160}
Brian Gaeked0fde302003-11-11 22:41:34 +0000161
162} // End llvm namespace