blob: 61ba0fab5ec4b3f958c9258bef9e539cce207f3e [file] [log] [blame]
Brian Gaeke3ca4fcc2004-04-25 07:04:49 +00001//===-- SparcV9PeepholeOpts.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
Brian Gaekee3d68072004-02-25 18:44:15 +000015#include "SparcV9Internals.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"
Alkis Evlogimenosf81af212004-02-14 01:18:34 +000022#include "Support/STLExtras.h"
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000023
Brian Gaeked0fde302003-11-11 22:41:34 +000024namespace llvm {
25
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000026//************************* Internal Functions *****************************/
27
Chris Lattnere96e2632003-09-01 20:24:06 +000028static inline void
Chris Lattner55291ea2002-10-28 01:41:47 +000029DeleteInstruction(MachineBasicBlock& mvec,
30 MachineBasicBlock::iterator& BBI,
Chris Lattnere96e2632003-09-01 20:24:06 +000031 const TargetMachine& target) {
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000032 // Check if this instruction is in a delay slot of its predecessor.
Misha Brukmanf96eb642003-05-23 19:20:57 +000033 if (BBI != mvec.begin()) {
Chris Lattner3501fea2003-01-14 22:00:31 +000034 const TargetInstrInfo& mii = target.getInstrInfo();
Alkis Evlogimenosf81af212004-02-14 01:18:34 +000035 MachineBasicBlock::iterator predMI = prior(BBI);
Brian Gaeke12c1d2c2004-02-11 20:47:34 +000036 if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpcode())) {
Misha Brukmanf96eb642003-05-23 19:20:57 +000037 // 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.
40 //
41 assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000042 BBI->replace(mii.getNOPOpCode(), 0);
Misha Brukmanf96eb642003-05-23 19:20:57 +000043 return;
44 }
45 }
Vikram S. Adve7e5167a2002-09-27 14:27:37 +000046
47 // The instruction is not in a delay slot, so we can simply erase it.
48 mvec.erase(BBI);
49 BBI = mvec.end();
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000050}
51
52//******************* Individual Peephole Optimizations ********************/
53
Chris Lattnera89d8f72003-09-01 20:38:03 +000054//----------------------------------------------------------------------------
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//----------------------------------------------------------------------------
63
64static bool IsUselessCopy(const TargetMachine &target, const MachineInstr* MI) {
Brian Gaeke12c1d2c2004-02-11 20:47:34 +000065 if (MI->getOpcode() == V9::FMOVS || MI->getOpcode() == V9::FMOVD) {
Misha Brukman97f9cf22003-12-17 22:08:20 +000066 return (// both operands are allocated to the same register
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +000067 MI->getOperand(0).getReg() == MI->getOperand(1).getReg());
Brian Gaeke12c1d2c2004-02-11 20:47:34 +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() &&
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +000074 MI->getOperand(srcWithDestReg).getReg()
75 == MI->getOperand(2).getReg())
Chris Lattnera89d8f72003-09-01 20:38:03 +000076 break;
77
78 if (srcWithDestReg == 2)
79 return false;
80 else {
Misha Brukman97f9cf22003-12-17 22:08:20 +000081 // else source and dest are allocated to the same register
Chris Lattnera89d8f72003-09-01 20:38:03 +000082 unsigned otherOp = 1 - srcWithDestReg;
Misha Brukman97f9cf22003-12-17 22:08:20 +000083 return (// either operand otherOp is register %g0
Chris Lattnera89d8f72003-09-01 20:38:03 +000084 (MI->getOperand(otherOp).hasAllocatedReg() &&
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +000085 MI->getOperand(otherOp).getReg() ==
Chris Lattnera89d8f72003-09-01 20:38:03 +000086 target.getRegInfo().getZeroRegNum()) ||
87
Misha Brukman97f9cf22003-12-17 22:08:20 +000088 // or operand otherOp == 0
Chris Lattnera89d8f72003-09-01 20:38:03 +000089 (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) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +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"; }
Misha Brukman97f9cf22003-12-17 22:08:20 +0000120
121 // getAnalysisUsage - this pass preserves the CFG
122 void getAnalysisUsage(AnalysisUsage &AU) const {
123 AU.setPreservesCFG();
124 }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000125};
126
Chris Lattnere96e2632003-09-01 20:24:06 +0000127// 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
130//
131bool PeepholeOpts::visit(MachineBasicBlock& mvec,
132 MachineBasicBlock::iterator BBI) const {
Misha Brukman97f9cf22003-12-17 22:08:20 +0000133 // Remove redundant copy instructions
Chris Lattnere96e2632003-09-01 20:24:06 +0000134 return RemoveUselessCopies(mvec, BBI, target);
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000135}
136
137
Chris Lattnere96e2632003-09-01 20:24:06 +0000138bool PeepholeOpts::runOnBasicBlock(BasicBlock &BB) {
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000139 // Get the machine instructions for this BB
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000140 // 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;
Chris Lattnere96e2632003-09-01 20:24:06 +0000144 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000145 if (I->getBasicBlock() == &BB)
146 MBB = I;
Chris Lattnere96e2632003-09-01 20:24:06 +0000147
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000148 assert(MBB && "MachineBasicBlock object not found for specified block!");
149 MachineBasicBlock &mvec = *MBB;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000150
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000151 for (MachineBasicBlock::iterator I = mvec.begin(), E = mvec.end(); I != E; )
152 visit(mvec, I++);
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000153
154 return true;
155}
156
Misha Brukman97f9cf22003-12-17 22:08:20 +0000157/// createPeepholeOptsPass - Public entry point for peephole optimization
158///
Misha Brukman941fc9a2003-11-07 17:44:18 +0000159FunctionPass* createPeepholeOptsPass(const TargetMachine &TM) {
160 return new PeepholeOpts(TM);
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000161}
Brian Gaeked0fde302003-11-11 22:41:34 +0000162
163} // End llvm namespace