2009-11-07 22:00:39 +00:00
|
|
|
//===-- Thumb2ITBlockPass.cpp - Insert Thumb IT blocks ----------*- C++ -*-===//
|
2009-07-10 01:54:42 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "thumb2-it"
|
|
|
|
#include "ARM.h"
|
|
|
|
#include "ARMMachineFunctionInfo.h"
|
2009-07-11 07:26:20 +00:00
|
|
|
#include "Thumb2InstrInfo.h"
|
2009-07-10 01:54:42 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2010-06-09 01:46:50 +00:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2009-07-10 01:54:42 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2010-06-09 01:46:50 +00:00
|
|
|
STATISTIC(NumITs, "Number of IT blocks inserted");
|
|
|
|
STATISTIC(NumMovedInsts, "Number of predicated instructions moved");
|
2009-07-10 01:54:42 +00:00
|
|
|
|
|
|
|
namespace {
|
2010-06-09 01:46:50 +00:00
|
|
|
class Thumb2ITBlockPass : public MachineFunctionPass {
|
|
|
|
bool PreRegAlloc;
|
|
|
|
|
|
|
|
public:
|
2009-07-10 01:54:42 +00:00
|
|
|
static char ID;
|
2010-07-02 21:07:09 +00:00
|
|
|
Thumb2ITBlockPass() : MachineFunctionPass(&ID) {}
|
2009-07-10 01:54:42 +00:00
|
|
|
|
2009-07-11 07:26:20 +00:00
|
|
|
const Thumb2InstrInfo *TII;
|
2010-06-18 23:09:54 +00:00
|
|
|
const TargetRegisterInfo *TRI;
|
2009-07-10 01:54:42 +00:00
|
|
|
ARMFunctionInfo *AFI;
|
|
|
|
|
|
|
|
virtual bool runOnMachineFunction(MachineFunction &Fn);
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Thumb IT blocks insertion pass";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2010-06-18 23:09:54 +00:00
|
|
|
bool MoveCopyOutOfITBlock(MachineInstr *MI,
|
|
|
|
ARMCC::CondCodes CC, ARMCC::CondCodes OCC,
|
|
|
|
SmallSet<unsigned, 4> &Defs,
|
|
|
|
SmallSet<unsigned, 4> &Uses);
|
2010-06-09 01:46:50 +00:00
|
|
|
bool InsertITInstructions(MachineBasicBlock &MBB);
|
2009-07-10 01:54:42 +00:00
|
|
|
};
|
|
|
|
char Thumb2ITBlockPass::ID = 0;
|
|
|
|
}
|
|
|
|
|
2010-06-18 23:09:54 +00:00
|
|
|
/// TrackDefUses - Tracking what registers are being defined and used by
|
|
|
|
/// instructions in the IT block. This also tracks "dependencies", i.e. uses
|
|
|
|
/// in the IT block that are defined before the IT instruction.
|
|
|
|
static void TrackDefUses(MachineInstr *MI,
|
|
|
|
SmallSet<unsigned, 4> &Defs,
|
|
|
|
SmallSet<unsigned, 4> &Uses,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
|
|
|
SmallVector<unsigned, 4> LocalDefs;
|
|
|
|
SmallVector<unsigned, 4> LocalUses;
|
|
|
|
|
2010-06-09 01:46:50 +00:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
2010-06-18 23:09:54 +00:00
|
|
|
if (!Reg || Reg == ARM::ITSTATE || Reg == ARM::SP)
|
2010-06-09 01:46:50 +00:00
|
|
|
continue;
|
2010-06-18 23:09:54 +00:00
|
|
|
if (MO.isUse())
|
|
|
|
LocalUses.push_back(Reg);
|
2010-06-09 01:46:50 +00:00
|
|
|
else
|
2010-06-18 23:09:54 +00:00
|
|
|
LocalDefs.push_back(Reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = LocalUses.size(); i != e; ++i) {
|
|
|
|
unsigned Reg = LocalUses[i];
|
|
|
|
Uses.insert(Reg);
|
|
|
|
for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
|
|
|
|
*Subreg; ++Subreg)
|
|
|
|
Uses.insert(*Subreg);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
|
|
|
|
unsigned Reg = LocalDefs[i];
|
|
|
|
Defs.insert(Reg);
|
|
|
|
for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
|
|
|
|
*Subreg; ++Subreg)
|
|
|
|
Defs.insert(*Subreg);
|
|
|
|
if (Reg == ARM::CPSR)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-16 22:35:32 +00:00
|
|
|
static bool isCopy(MachineInstr *MI) {
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case ARM::MOVr:
|
|
|
|
case ARM::MOVr_TC:
|
|
|
|
case ARM::tMOVr:
|
|
|
|
case ARM::tMOVgpr2tgpr:
|
|
|
|
case ARM::tMOVtgpr2gpr:
|
|
|
|
case ARM::tMOVgpr2gpr:
|
|
|
|
case ARM::t2MOVr:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-18 23:09:54 +00:00
|
|
|
bool
|
|
|
|
Thumb2ITBlockPass::MoveCopyOutOfITBlock(MachineInstr *MI,
|
|
|
|
ARMCC::CondCodes CC, ARMCC::CondCodes OCC,
|
|
|
|
SmallSet<unsigned, 4> &Defs,
|
|
|
|
SmallSet<unsigned, 4> &Uses) {
|
2010-07-16 22:35:32 +00:00
|
|
|
if (!isCopy(MI))
|
|
|
|
return false;
|
|
|
|
// llvm models select's as two-address instructions. That means a copy
|
|
|
|
// is inserted before a t2MOVccr, etc. If the copy is scheduled in
|
|
|
|
// between selects we would end up creating multiple IT blocks.
|
|
|
|
assert(MI->getOperand(0).getSubReg() == 0 &&
|
|
|
|
MI->getOperand(1).getSubReg() == 0 &&
|
|
|
|
"Sub-register indices still around?");
|
|
|
|
|
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
|
|
|
unsigned SrcReg = MI->getOperand(1).getReg();
|
|
|
|
|
|
|
|
// First check if it's safe to move it.
|
|
|
|
if (Uses.count(DstReg) || Defs.count(SrcReg))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Then peek at the next instruction to see if it's predicated on CC or OCC.
|
|
|
|
// If not, then there is nothing to be gained by moving the copy.
|
|
|
|
MachineBasicBlock::iterator I = MI; ++I;
|
|
|
|
MachineBasicBlock::iterator E = MI->getParent()->end();
|
|
|
|
while (I != E && I->isDebugValue())
|
|
|
|
++I;
|
|
|
|
if (I != E) {
|
|
|
|
unsigned NPredReg = 0;
|
|
|
|
ARMCC::CondCodes NCC = llvm::getITInstrPredicate(I, NPredReg);
|
|
|
|
if (NCC == CC || NCC == OCC)
|
|
|
|
return true;
|
2010-06-09 01:46:50 +00:00
|
|
|
}
|
2010-06-18 23:09:54 +00:00
|
|
|
return false;
|
2010-06-09 01:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Thumb2ITBlockPass::InsertITInstructions(MachineBasicBlock &MBB) {
|
2009-07-10 01:54:42 +00:00
|
|
|
bool Modified = false;
|
|
|
|
|
2010-06-09 01:46:50 +00:00
|
|
|
SmallSet<unsigned, 4> Defs;
|
|
|
|
SmallSet<unsigned, 4> Uses;
|
2009-07-10 01:54:42 +00:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
|
|
|
|
while (MBBI != E) {
|
|
|
|
MachineInstr *MI = &*MBBI;
|
2009-09-28 09:14:39 +00:00
|
|
|
DebugLoc dl = MI->getDebugLoc();
|
|
|
|
unsigned PredReg = 0;
|
2010-06-22 01:18:16 +00:00
|
|
|
ARMCC::CondCodes CC = llvm::getITInstrPredicate(MI, PredReg);
|
2009-07-10 01:54:42 +00:00
|
|
|
if (CC == ARMCC::AL) {
|
|
|
|
++MBBI;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-06-09 01:46:50 +00:00
|
|
|
Defs.clear();
|
|
|
|
Uses.clear();
|
2010-06-18 23:09:54 +00:00
|
|
|
TrackDefUses(MI, Defs, Uses, TRI);
|
2010-06-09 01:46:50 +00:00
|
|
|
|
2009-07-10 01:54:42 +00:00
|
|
|
// Insert an IT instruction.
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII->get(ARM::t2IT))
|
|
|
|
.addImm(CC);
|
2010-06-18 23:09:54 +00:00
|
|
|
|
|
|
|
// Add implicit use of ITSTATE to IT block instructions.
|
|
|
|
MI->addOperand(MachineOperand::CreateReg(ARM::ITSTATE, false/*ifDef*/,
|
|
|
|
true/*isImp*/, false/*isKill*/));
|
|
|
|
|
|
|
|
MachineInstr *LastITMI = MI;
|
2010-06-09 01:46:50 +00:00
|
|
|
MachineBasicBlock::iterator InsertPos = MIB;
|
2009-07-10 01:54:42 +00:00
|
|
|
++MBBI;
|
|
|
|
|
2010-06-18 23:09:54 +00:00
|
|
|
// Form IT block.
|
2009-07-10 01:54:42 +00:00
|
|
|
ARMCC::CondCodes OCC = ARMCC::getOppositeCondition(CC);
|
2009-08-15 07:59:10 +00:00
|
|
|
unsigned Mask = 0, Pos = 3;
|
2009-10-15 22:25:32 +00:00
|
|
|
// Branches, including tricky ones like LDM_RET, need to end an IT
|
|
|
|
// block so check the instruction we just put in the block.
|
2010-06-07 21:48:47 +00:00
|
|
|
for (; MBBI != E && Pos &&
|
|
|
|
(!MI->getDesc().isBranch() && !MI->getDesc().isReturn()) ; ++MBBI) {
|
|
|
|
if (MBBI->isDebugValue())
|
|
|
|
continue;
|
2010-06-09 01:46:50 +00:00
|
|
|
|
2009-09-28 20:47:15 +00:00
|
|
|
MachineInstr *NMI = &*MBBI;
|
2009-10-15 22:25:32 +00:00
|
|
|
MI = NMI;
|
2010-06-09 01:46:50 +00:00
|
|
|
|
2009-09-28 20:47:15 +00:00
|
|
|
unsigned NPredReg = 0;
|
2010-06-22 01:18:16 +00:00
|
|
|
ARMCC::CondCodes NCC = llvm::getITInstrPredicate(NMI, NPredReg);
|
2010-06-18 23:09:54 +00:00
|
|
|
if (NCC == CC || NCC == OCC) {
|
2010-03-17 23:14:23 +00:00
|
|
|
Mask |= (NCC & 1) << Pos;
|
2010-06-18 23:09:54 +00:00
|
|
|
// Add implicit use of ITSTATE.
|
|
|
|
NMI->addOperand(MachineOperand::CreateReg(ARM::ITSTATE, false/*ifDef*/,
|
2010-06-28 04:27:01 +00:00
|
|
|
true/*isImp*/, false/*isKill*/));
|
2010-06-18 23:09:54 +00:00
|
|
|
LastITMI = NMI;
|
|
|
|
} else {
|
2010-06-09 01:46:50 +00:00
|
|
|
if (NCC == ARMCC::AL &&
|
2010-06-18 23:09:54 +00:00
|
|
|
MoveCopyOutOfITBlock(NMI, CC, OCC, Defs, Uses)) {
|
|
|
|
--MBBI;
|
|
|
|
MBB.remove(NMI);
|
|
|
|
MBB.insert(InsertPos, NMI);
|
|
|
|
++NumMovedInsts;
|
|
|
|
continue;
|
2010-06-09 01:46:50 +00:00
|
|
|
}
|
2009-07-10 01:54:42 +00:00
|
|
|
break;
|
2010-06-09 01:46:50 +00:00
|
|
|
}
|
2010-06-18 23:09:54 +00:00
|
|
|
TrackDefUses(NMI, Defs, Uses, TRI);
|
2009-08-15 07:59:10 +00:00
|
|
|
--Pos;
|
2009-07-10 01:54:42 +00:00
|
|
|
}
|
2010-06-09 01:46:50 +00:00
|
|
|
|
2010-06-18 23:09:54 +00:00
|
|
|
// Finalize IT mask.
|
2009-08-15 07:59:10 +00:00
|
|
|
Mask |= (1 << Pos);
|
2010-03-17 23:14:23 +00:00
|
|
|
// Tag along (firstcond[0] << 4) with the mask.
|
|
|
|
Mask |= (CC & 1) << 4;
|
2009-07-10 01:54:42 +00:00
|
|
|
MIB.addImm(Mask);
|
2010-06-18 23:09:54 +00:00
|
|
|
|
|
|
|
// Last instruction in IT block kills ITSTATE.
|
|
|
|
LastITMI->findRegisterUseOperand(ARM::ITSTATE)->setIsKill();
|
|
|
|
|
2009-07-10 01:54:42 +00:00
|
|
|
Modified = true;
|
|
|
|
++NumITs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Thumb2ITBlockPass::runOnMachineFunction(MachineFunction &Fn) {
|
|
|
|
const TargetMachine &TM = Fn.getTarget();
|
|
|
|
AFI = Fn.getInfo<ARMFunctionInfo>();
|
2009-07-11 07:26:20 +00:00
|
|
|
TII = static_cast<const Thumb2InstrInfo*>(TM.getInstrInfo());
|
2010-06-18 23:09:54 +00:00
|
|
|
TRI = TM.getRegisterInfo();
|
2009-07-10 01:54:42 +00:00
|
|
|
|
|
|
|
if (!AFI->isThumbFunction())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool Modified = false;
|
2010-06-09 01:46:50 +00:00
|
|
|
for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; ) {
|
2009-07-10 01:54:42 +00:00
|
|
|
MachineBasicBlock &MBB = *MFI;
|
2010-06-09 01:46:50 +00:00
|
|
|
++MFI;
|
2010-07-02 21:07:09 +00:00
|
|
|
Modified |= InsertITInstructions(MBB);
|
2009-07-10 01:54:42 +00:00
|
|
|
}
|
|
|
|
|
2010-07-02 21:07:09 +00:00
|
|
|
if (Modified)
|
2010-06-18 23:09:54 +00:00
|
|
|
AFI->setHasITBlocks(true);
|
|
|
|
|
2009-07-10 01:54:42 +00:00
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
|
2009-08-08 02:54:37 +00:00
|
|
|
/// createThumb2ITBlockPass - Returns an instance of the Thumb2 IT blocks
|
2009-07-10 01:54:42 +00:00
|
|
|
/// insertion pass.
|
2010-07-02 21:07:09 +00:00
|
|
|
FunctionPass *llvm::createThumb2ITBlockPass() {
|
|
|
|
return new Thumb2ITBlockPass();
|
2009-07-10 01:54:42 +00:00
|
|
|
}
|