2002-10-28 01:41:47 +00:00
|
|
|
//===-- PeepholeOpts.cpp --------------------------------------------------===//
|
2002-09-20 00:42:11 +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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
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
|
|
|
|
2003-09-01 20:33:07 +00:00
|
|
|
#include "SparcInternals.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"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2003-01-14 22:00:31 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2002-09-20 00:42:11 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
|
|
|
|
//************************* 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()) {
|
2003-01-14 22:00:31 +00:00
|
|
|
const TargetInstrInfo& mii = target.getInstrInfo();
|
2002-09-20 00:42:11 +00:00
|
|
|
MachineInstr* predMI = *(BBI-1);
|
2003-05-23 19:20:57 +00:00
|
|
|
if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode())) {
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
|
|
|
|
(*BBI)->replace(mii.getNOPOpCode(), 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
if (MI->getOpCode() == V9::FMOVS || MI->getOpCode() == V9::FMOVD) {
|
|
|
|
return (/* both operands are allocated to the same register */
|
|
|
|
MI->getOperand(0).getAllocatedRegNum() ==
|
|
|
|
MI->getOperand(1).getAllocatedRegNum());
|
2003-10-21 12:29:45 +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;
|
|
|
|
|
|
|
|
for (srcWithDestReg = 0; srcWithDestReg < 2; ++srcWithDestReg)
|
|
|
|
if (MI->getOperand(srcWithDestReg).hasAllocatedReg() &&
|
|
|
|
MI->getOperand(srcWithDestReg).getAllocatedRegNum()
|
|
|
|
== MI->getOperand(2).getAllocatedRegNum())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (srcWithDestReg == 2)
|
|
|
|
return false;
|
|
|
|
else {
|
|
|
|
/* else source and dest are allocated to the same register */
|
|
|
|
unsigned otherOp = 1 - srcWithDestReg;
|
|
|
|
return (/* either operand otherOp is register %g0 */
|
|
|
|
(MI->getOperand(otherOp).hasAllocatedReg() &&
|
|
|
|
MI->getOperand(otherOp).getAllocatedRegNum() ==
|
|
|
|
target.getRegInfo().getZeroRegNum()) ||
|
|
|
|
|
|
|
|
/* or operand otherOp == 0 */
|
|
|
|
(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) {
|
2003-09-01 20:38:03 +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:
|
|
|
|
PeepholeOpts(const TargetMachine &T): target(T) { }
|
|
|
|
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"; }
|
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
|
|
|
|
// instruction before MI, but not
|
|
|
|
//
|
|
|
|
bool PeepholeOpts::visit(MachineBasicBlock& mvec,
|
|
|
|
MachineBasicBlock::iterator BBI) const {
|
2002-09-20 00:42:11 +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
|
|
|
|
|
|
|
// Iterate over all machine instructions in the BB
|
|
|
|
// Use a reverse iterator to allow deletion of MI or any instruction after it.
|
|
|
|
// Insertions or deletions *before* MI are not safe.
|
|
|
|
//
|
2002-10-28 01:41:47 +00:00
|
|
|
for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
|
2003-09-01 20:24:06 +00:00
|
|
|
RE=mvec.rend(); RI != RE; ) {
|
2003-05-23 19:20:57 +00:00
|
|
|
MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
|
|
|
|
++RI; // pre-increment to delete MI or after it
|
|
|
|
visit(mvec, BBI);
|
|
|
|
}
|
2002-09-20 00:42:11 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// createPeepholeOptsPass - Public entrypoint for peephole optimization
|
|
|
|
// and this file as a whole...
|
|
|
|
//
|
2003-09-01 20:24:06 +00:00
|
|
|
FunctionPass* createPeepholeOptsPass(TargetMachine &T) {
|
2002-09-20 00:42:11 +00:00
|
|
|
return new PeepholeOpts(T);
|
|
|
|
}
|