2004-04-25 07:04:49 +00:00
|
|
|
//===-- SparcV9PeepholeOpts.cpp -------------------------------------------===//
|
2005-04-21 23:30:14 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:30:14 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-04-21 23:30:14 +00:00
|
|
|
//
|
2002-10-28 01:41:47 +00:00
|
|
|
// Support for performing several peephole opts in one or a few passes over the
|
|
|
|
// machine code of a method.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-20 00:42:11 +00:00
|
|
|
|
2004-02-25 18:44:15 +00:00
|
|
|
#include "SparcV9Internals.h"
|
2003-12-17 22:08:20 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/Pass.h"
|
2002-10-28 20:00:31 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2002-09-20 00:42:11 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2003-01-14 22:00:31 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2003-12-17 22:08:20 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2002-09-20 00:42:11 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-09-20 00:42:11 +00:00
|
|
|
//************************* Internal Functions *****************************/
|
|
|
|
|
2003-09-01 20:24:06 +00:00
|
|
|
static inline void
|
2002-10-28 01:41:47 +00:00
|
|
|
DeleteInstruction(MachineBasicBlock& mvec,
|
|
|
|
MachineBasicBlock::iterator& BBI,
|
2003-09-01 20:24:06 +00:00
|
|
|
const TargetMachine& target) {
|
2002-09-20 00:42:11 +00:00
|
|
|
// Check if this instruction is in a delay slot of its predecessor.
|
2003-05-23 19:20:57 +00:00
|
|
|
if (BBI != mvec.begin()) {
|
2004-06-02 05:55:25 +00:00
|
|
|
const TargetInstrInfo& mii = *target.getInstrInfo();
|
2004-02-14 01:18:34 +00:00
|
|
|
MachineBasicBlock::iterator predMI = prior(BBI);
|
2004-02-11 20:47:34 +00:00
|
|
|
if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpcode())) {
|
2003-05-23 19:20:57 +00:00
|
|
|
// This instruction is in a delay slot of its predecessor, so
|
|
|
|
// replace it with a nop. By replacing in place, we save having
|
|
|
|
// to update the I-I maps.
|
2005-04-21 23:30:14 +00:00
|
|
|
//
|
2003-05-23 19:20:57 +00:00
|
|
|
assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
|
2004-07-27 17:43:24 +00:00
|
|
|
BBI->replace(V9::NOP, 0);
|
2003-05-23 19:20:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2002-09-27 14:27:37 +00:00
|
|
|
// The instruction is not in a delay slot, so we can simply erase it.
|
|
|
|
mvec.erase(BBI);
|
|
|
|
BBI = mvec.end();
|
2002-09-20 00:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//******************* Individual Peephole Optimizations ********************/
|
|
|
|
|
2003-09-01 20:38:03 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Function: IsUselessCopy
|
|
|
|
// Decide whether a machine instruction is a redundant copy:
|
|
|
|
// -- ADD with g0 and result and operand are identical, or
|
|
|
|
// -- OR with g0 and result and operand are identical, or
|
|
|
|
// -- FMOVS or FMOVD and result and operand are identical.
|
|
|
|
// Other cases are possible but very rare that they would be useless copies,
|
|
|
|
// so it's not worth analyzing them.
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static bool IsUselessCopy(const TargetMachine &target, const MachineInstr* MI) {
|
2004-02-11 20:47:34 +00:00
|
|
|
if (MI->getOpcode() == V9::FMOVS || MI->getOpcode() == V9::FMOVD) {
|
2003-12-17 22:08:20 +00:00
|
|
|
return (// both operands are allocated to the same register
|
2004-02-13 21:01:20 +00:00
|
|
|
MI->getOperand(0).getReg() == MI->getOperand(1).getReg());
|
2004-02-11 20:47:34 +00:00
|
|
|
} else if (MI->getOpcode() == V9::ADDr || MI->getOpcode() == V9::ORr ||
|
|
|
|
MI->getOpcode() == V9::ADDi || MI->getOpcode() == V9::ORi) {
|
2003-09-01 20:38:03 +00:00
|
|
|
unsigned srcWithDestReg;
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2003-09-01 20:38:03 +00:00
|
|
|
for (srcWithDestReg = 0; srcWithDestReg < 2; ++srcWithDestReg)
|
|
|
|
if (MI->getOperand(srcWithDestReg).hasAllocatedReg() &&
|
2004-02-13 21:01:20 +00:00
|
|
|
MI->getOperand(srcWithDestReg).getReg()
|
|
|
|
== MI->getOperand(2).getReg())
|
2003-09-01 20:38:03 +00:00
|
|
|
break;
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2003-09-01 20:38:03 +00:00
|
|
|
if (srcWithDestReg == 2)
|
|
|
|
return false;
|
|
|
|
else {
|
2003-12-17 22:08:20 +00:00
|
|
|
// else source and dest are allocated to the same register
|
2003-09-01 20:38:03 +00:00
|
|
|
unsigned otherOp = 1 - srcWithDestReg;
|
2003-12-17 22:08:20 +00:00
|
|
|
return (// either operand otherOp is register %g0
|
2003-09-01 20:38:03 +00:00
|
|
|
(MI->getOperand(otherOp).hasAllocatedReg() &&
|
2004-02-13 21:01:20 +00:00
|
|
|
MI->getOperand(otherOp).getReg() ==
|
2004-06-02 05:55:25 +00:00
|
|
|
target.getRegInfo()->getZeroRegNum()) ||
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2003-12-17 22:08:20 +00:00
|
|
|
// or operand otherOp == 0
|
2003-09-01 20:38:03 +00:00
|
|
|
(MI->getOperand(otherOp).getType()
|
|
|
|
== MachineOperand::MO_SignExtendedImmed &&
|
|
|
|
MI->getOperand(otherOp).getImmedValue() == 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-09-20 00:42:11 +00:00
|
|
|
inline bool
|
2002-10-28 01:41:47 +00:00
|
|
|
RemoveUselessCopies(MachineBasicBlock& mvec,
|
|
|
|
MachineBasicBlock::iterator& BBI,
|
2003-09-01 20:24:06 +00:00
|
|
|
const TargetMachine& target) {
|
2004-02-12 02:27:10 +00:00
|
|
|
if (IsUselessCopy(target, BBI)) {
|
2003-05-23 19:20:57 +00:00
|
|
|
DeleteInstruction(mvec, BBI, target);
|
|
|
|
return true;
|
|
|
|
}
|
2002-09-20 00:42:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//************************ Class Implementations **************************/
|
|
|
|
|
|
|
|
class PeepholeOpts: public BasicBlockPass {
|
|
|
|
const TargetMachine ⌖
|
2002-10-28 01:41:47 +00:00
|
|
|
bool visit(MachineBasicBlock& mvec,
|
|
|
|
MachineBasicBlock::iterator BBI) const;
|
2002-09-20 00:42:11 +00:00
|
|
|
public:
|
2003-11-07 17:44:18 +00:00
|
|
|
PeepholeOpts(const TargetMachine &TM): target(TM) { }
|
2002-09-20 00:42:11 +00:00
|
|
|
bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
|
2003-08-14 14:43:24 +00:00
|
|
|
virtual const char *getPassName() const { return "Peephole Optimization"; }
|
2003-12-17 22:08:20 +00:00
|
|
|
|
|
|
|
// getAnalysisUsage - this pass preserves the CFG
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
}
|
2002-09-20 00:42:11 +00:00
|
|
|
};
|
|
|
|
|
2003-09-01 20:24:06 +00:00
|
|
|
// Apply a list of peephole optimizations to this machine instruction
|
|
|
|
// within its local context. They are allowed to delete MI or any
|
2005-04-21 23:30:14 +00:00
|
|
|
// instruction before MI, but not
|
2003-09-01 20:24:06 +00:00
|
|
|
//
|
|
|
|
bool PeepholeOpts::visit(MachineBasicBlock& mvec,
|
|
|
|
MachineBasicBlock::iterator BBI) const {
|
2003-12-17 22:08:20 +00:00
|
|
|
// Remove redundant copy instructions
|
2003-09-01 20:24:06 +00:00
|
|
|
return RemoveUselessCopies(mvec, BBI, target);
|
2002-09-20 00:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-01 20:24:06 +00:00
|
|
|
bool PeepholeOpts::runOnBasicBlock(BasicBlock &BB) {
|
2002-09-20 00:42:11 +00:00
|
|
|
// Get the machine instructions for this BB
|
2002-10-28 20:00:31 +00:00
|
|
|
// FIXME: MachineBasicBlock::get() is deprecated, hence inlining the function
|
|
|
|
const Function *F = BB.getParent();
|
|
|
|
MachineFunction &MF = MachineFunction::get(F);
|
|
|
|
MachineBasicBlock *MBB = NULL;
|
2003-09-01 20:24:06 +00:00
|
|
|
for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
|
2002-10-28 20:00:31 +00:00
|
|
|
if (I->getBasicBlock() == &BB)
|
|
|
|
MBB = I;
|
2003-09-01 20:24:06 +00:00
|
|
|
|
2002-10-28 20:00:31 +00:00
|
|
|
assert(MBB && "MachineBasicBlock object not found for specified block!");
|
|
|
|
MachineBasicBlock &mvec = *MBB;
|
2002-09-20 00:42:11 +00:00
|
|
|
|
2004-02-12 02:27:10 +00:00
|
|
|
for (MachineBasicBlock::iterator I = mvec.begin(), E = mvec.end(); I != E; )
|
|
|
|
visit(mvec, I++);
|
2002-09-20 00:42:11 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-12-17 22:08:20 +00:00
|
|
|
/// createPeepholeOptsPass - Public entry point for peephole optimization
|
|
|
|
///
|
2003-11-07 17:44:18 +00:00
|
|
|
FunctionPass* createPeepholeOptsPass(const TargetMachine &TM) {
|
|
|
|
return new PeepholeOpts(TM);
|
2002-09-20 00:42:11 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|
|
|
|
} // End llvm namespace
|