2009-11-08 06:00:39 +08:00
|
|
|
//===-- Thumb2ITBlockPass.cpp - Insert Thumb IT blocks ----------*- C++ -*-===//
|
2009-07-10 09:54:42 +08: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 15:26:20 +08:00
|
|
|
#include "Thumb2InstrInfo.h"
|
2009-07-10 09:54:42 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2010-06-09 09:46:50 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2009-07-10 09:54:42 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2010-06-09 09:46:50 +08:00
|
|
|
STATISTIC(NumITs, "Number of IT blocks inserted");
|
|
|
|
STATISTIC(NumMovedInsts, "Number of predicated instructions moved");
|
2009-07-10 09:54:42 +08:00
|
|
|
|
|
|
|
namespace {
|
2010-06-09 09:46:50 +08:00
|
|
|
class Thumb2ITBlockPass : public MachineFunctionPass {
|
|
|
|
bool PreRegAlloc;
|
|
|
|
|
|
|
|
public:
|
2009-07-10 09:54:42 +08:00
|
|
|
static char ID;
|
2010-06-09 09:46:50 +08:00
|
|
|
Thumb2ITBlockPass(bool PreRA) :
|
|
|
|
MachineFunctionPass(&ID), PreRegAlloc(PreRA) {}
|
2009-07-10 09:54:42 +08:00
|
|
|
|
2009-07-11 15:26:20 +08:00
|
|
|
const Thumb2InstrInfo *TII;
|
2009-07-10 09:54:42 +08:00
|
|
|
ARMFunctionInfo *AFI;
|
|
|
|
|
|
|
|
virtual bool runOnMachineFunction(MachineFunction &Fn);
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Thumb IT blocks insertion pass";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2010-06-09 09:46:50 +08:00
|
|
|
bool MoveCPSRUseUp(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
MachineBasicBlock::iterator E,
|
|
|
|
unsigned PredReg,
|
|
|
|
ARMCC::CondCodes CC, ARMCC::CondCodes OCC,
|
|
|
|
bool &Done);
|
|
|
|
|
|
|
|
void FindITBlockRanges(MachineBasicBlock &MBB,
|
|
|
|
SmallVector<MachineInstr*,4> &FirstUses,
|
|
|
|
SmallVector<MachineInstr*,4> &LastUses);
|
|
|
|
bool InsertITBlock(MachineInstr *First, MachineInstr *Last);
|
2009-07-10 09:54:42 +08:00
|
|
|
bool InsertITBlocks(MachineBasicBlock &MBB);
|
2010-06-09 09:46:50 +08:00
|
|
|
bool InsertITInstructions(MachineBasicBlock &MBB);
|
2009-07-10 09:54:42 +08:00
|
|
|
};
|
|
|
|
char Thumb2ITBlockPass::ID = 0;
|
|
|
|
}
|
|
|
|
|
2009-09-28 17:14:39 +08:00
|
|
|
static ARMCC::CondCodes getPredicate(const MachineInstr *MI, unsigned &PredReg){
|
2009-07-11 15:26:20 +08:00
|
|
|
unsigned Opc = MI->getOpcode();
|
|
|
|
if (Opc == ARM::tBcc || Opc == ARM::t2Bcc)
|
|
|
|
return ARMCC::AL;
|
2010-06-10 03:26:01 +08:00
|
|
|
return llvm::getInstrPredicate(MI, PredReg);
|
2010-06-09 09:46:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Thumb2ITBlockPass::MoveCPSRUseUp(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
MachineBasicBlock::iterator E,
|
|
|
|
unsigned PredReg,
|
|
|
|
ARMCC::CondCodes CC, ARMCC::CondCodes OCC,
|
|
|
|
bool &Done) {
|
|
|
|
SmallSet<unsigned, 4> Defs, Uses;
|
|
|
|
MachineBasicBlock::iterator I = MBBI;
|
|
|
|
// Look for next CPSR use by scanning up to 4 instructions.
|
|
|
|
for (unsigned i = 0; i < 4; ++i) {
|
|
|
|
MachineInstr *MI = &*I;
|
|
|
|
unsigned MPredReg = 0;
|
|
|
|
ARMCC::CondCodes MCC = getPredicate(MI, MPredReg);
|
|
|
|
if (MCC != ARMCC::AL) {
|
|
|
|
if (MPredReg != PredReg || (MCC != CC && MCC != OCC))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check if the instruction is using any register that's defined
|
|
|
|
// below the previous predicated instruction. Also return false if
|
|
|
|
// it defines any register which is used in between.
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
if (MO.isDef()) {
|
|
|
|
if (Reg == PredReg || Uses.count(Reg))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (Defs.count(Reg))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Done = (I == E);
|
|
|
|
MBB.remove(MI);
|
|
|
|
MBB.insert(MBBI, MI);
|
|
|
|
++NumMovedInsts;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
if (MO.isDef()) {
|
|
|
|
if (Reg == PredReg)
|
|
|
|
return false;
|
|
|
|
Defs.insert(Reg);
|
|
|
|
} else
|
|
|
|
Uses.insert(Reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (I == E)
|
|
|
|
break;
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isCPSRLiveout(MachineBasicBlock &MBB) {
|
|
|
|
for (MachineBasicBlock::succ_iterator I = MBB.succ_begin(),
|
|
|
|
E = MBB.succ_end(); I != E; ++I) {
|
|
|
|
if ((*I)->isLiveIn(ARM::CPSR))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Thumb2ITBlockPass::FindITBlockRanges(MachineBasicBlock &MBB,
|
|
|
|
SmallVector<MachineInstr*,4> &FirstUses,
|
|
|
|
SmallVector<MachineInstr*,4> &LastUses) {
|
|
|
|
bool SeenUse = false;
|
|
|
|
MachineOperand *LastDef = 0;
|
|
|
|
MachineOperand *LastUse = 0;
|
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
|
|
|
|
while (MBBI != E) {
|
|
|
|
MachineInstr *MI = &*MBBI;
|
|
|
|
++MBBI;
|
|
|
|
|
|
|
|
MachineOperand *Def = 0;
|
|
|
|
MachineOperand *Use = 0;
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg() || MO.getReg() != ARM::CPSR)
|
|
|
|
continue;
|
|
|
|
if (MO.isDef()) {
|
|
|
|
assert(Def == 0 && "Multiple defs of CPSR?");
|
|
|
|
Def = &MO;
|
|
|
|
} else {
|
|
|
|
assert(Use == 0 && "Multiple uses of CPSR?");
|
|
|
|
Use = &MO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Use) {
|
|
|
|
LastUse = Use;
|
|
|
|
if (!SeenUse) {
|
|
|
|
FirstUses.push_back(MI);
|
|
|
|
SeenUse = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Def) {
|
|
|
|
if (LastUse) {
|
|
|
|
LastUses.push_back(LastUse->getParent());
|
|
|
|
LastUse = 0;
|
|
|
|
}
|
|
|
|
LastDef = Def;
|
|
|
|
SeenUse = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LastUse) {
|
|
|
|
// Is the last use a kill?
|
|
|
|
if (isCPSRLiveout(MBB))
|
|
|
|
LastUses.push_back(0);
|
|
|
|
else
|
|
|
|
LastUses.push_back(LastUse->getParent());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Thumb2ITBlockPass::InsertITBlock(MachineInstr *First, MachineInstr *Last) {
|
|
|
|
if (First == Last)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool Modified = false;
|
|
|
|
MachineBasicBlock *MBB = First->getParent();
|
|
|
|
MachineBasicBlock::iterator MBBI = First;
|
|
|
|
MachineBasicBlock::iterator E = Last;
|
|
|
|
|
|
|
|
if (First->getDesc().isBranch() || First->getDesc().isReturn())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned PredReg = 0;
|
|
|
|
ARMCC::CondCodes CC = getPredicate(First, PredReg);
|
|
|
|
if (CC == ARMCC::AL)
|
|
|
|
return Modified;
|
|
|
|
|
|
|
|
// Move uses of the CPSR together if possible.
|
|
|
|
ARMCC::CondCodes OCC = ARMCC::getOppositeCondition(CC);
|
|
|
|
|
|
|
|
do {
|
|
|
|
++MBBI;
|
|
|
|
if (MBBI->getDesc().isBranch() || MBBI->getDesc().isReturn())
|
|
|
|
return Modified;
|
|
|
|
MachineInstr *NMI = &*MBBI;
|
|
|
|
unsigned NPredReg = 0;
|
|
|
|
ARMCC::CondCodes NCC = getPredicate(NMI, NPredReg);
|
|
|
|
if (NCC != CC && NCC != OCC) {
|
|
|
|
if (NCC != ARMCC::AL)
|
|
|
|
return Modified;
|
|
|
|
assert(MBBI != E);
|
|
|
|
bool Done = false;
|
|
|
|
if (!MoveCPSRUseUp(*MBB, MBBI, E, PredReg, CC, OCC, Done))
|
|
|
|
return Modified;
|
|
|
|
Modified = true;
|
|
|
|
if (Done)
|
|
|
|
MBBI = E;
|
|
|
|
}
|
|
|
|
} while (MBBI != E);
|
|
|
|
return true;
|
2009-07-11 15:26:20 +08:00
|
|
|
}
|
|
|
|
|
2009-07-10 09:54:42 +08:00
|
|
|
bool Thumb2ITBlockPass::InsertITBlocks(MachineBasicBlock &MBB) {
|
2010-06-09 09:46:50 +08:00
|
|
|
SmallVector<MachineInstr*, 4> FirstUses;
|
|
|
|
SmallVector<MachineInstr*, 4> LastUses;
|
|
|
|
FindITBlockRanges(MBB, FirstUses, LastUses);
|
|
|
|
assert(FirstUses.size() == LastUses.size() && "Incorrect range information!");
|
|
|
|
|
|
|
|
bool Modified = false;
|
|
|
|
for (unsigned i = 0, e = FirstUses.size(); i != e; ++i) {
|
|
|
|
if (LastUses[i] == 0)
|
|
|
|
// Must be the last pair where CPSR is live out of the block.
|
|
|
|
return Modified;
|
|
|
|
Modified |= InsertITBlock(FirstUses[i], LastUses[i]);
|
|
|
|
}
|
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TrackDefUses(MachineInstr *MI, SmallSet<unsigned, 4> &Defs,
|
|
|
|
SmallSet<unsigned, 4> &Uses) {
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
if (MO.isDef())
|
|
|
|
Defs.insert(Reg);
|
|
|
|
else
|
|
|
|
Uses.insert(Reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Thumb2ITBlockPass::InsertITInstructions(MachineBasicBlock &MBB) {
|
2009-07-10 09:54:42 +08:00
|
|
|
bool Modified = false;
|
|
|
|
|
2010-06-09 09:46:50 +08:00
|
|
|
SmallSet<unsigned, 4> Defs;
|
|
|
|
SmallSet<unsigned, 4> Uses;
|
2009-07-10 09:54:42 +08:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
|
|
|
|
while (MBBI != E) {
|
|
|
|
MachineInstr *MI = &*MBBI;
|
2009-09-28 17:14:39 +08:00
|
|
|
DebugLoc dl = MI->getDebugLoc();
|
|
|
|
unsigned PredReg = 0;
|
|
|
|
ARMCC::CondCodes CC = getPredicate(MI, PredReg);
|
2009-07-10 09:54:42 +08:00
|
|
|
if (CC == ARMCC::AL) {
|
|
|
|
++MBBI;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-06-09 09:46:50 +08:00
|
|
|
Defs.clear();
|
|
|
|
Uses.clear();
|
|
|
|
TrackDefUses(MI, Defs, Uses);
|
|
|
|
|
2009-07-10 09:54:42 +08:00
|
|
|
// Insert an IT instruction.
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII->get(ARM::t2IT))
|
|
|
|
.addImm(CC);
|
2010-06-09 09:46:50 +08:00
|
|
|
MachineBasicBlock::iterator InsertPos = MIB;
|
2009-07-10 09:54:42 +08:00
|
|
|
++MBBI;
|
|
|
|
|
2009-08-15 15:59:10 +08:00
|
|
|
// Finalize IT mask.
|
2009-07-10 09:54:42 +08:00
|
|
|
ARMCC::CondCodes OCC = ARMCC::getOppositeCondition(CC);
|
2009-08-15 15:59:10 +08:00
|
|
|
unsigned Mask = 0, Pos = 3;
|
2009-10-16 06:25:32 +08: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-08 05:48:47 +08:00
|
|
|
for (; MBBI != E && Pos &&
|
|
|
|
(!MI->getDesc().isBranch() && !MI->getDesc().isReturn()) ; ++MBBI) {
|
|
|
|
if (MBBI->isDebugValue())
|
|
|
|
continue;
|
2010-06-09 09:46:50 +08:00
|
|
|
|
2009-09-29 04:47:15 +08:00
|
|
|
MachineInstr *NMI = &*MBBI;
|
2009-10-16 06:25:32 +08:00
|
|
|
MI = NMI;
|
2010-06-09 09:46:50 +08:00
|
|
|
|
2009-09-29 04:47:15 +08:00
|
|
|
unsigned NPredReg = 0;
|
|
|
|
ARMCC::CondCodes NCC = getPredicate(NMI, NPredReg);
|
2010-03-18 07:14:23 +08:00
|
|
|
if (NCC == CC || NCC == OCC)
|
|
|
|
Mask |= (NCC & 1) << Pos;
|
2010-06-09 09:46:50 +08:00
|
|
|
else {
|
|
|
|
unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
|
|
|
|
if (NCC == ARMCC::AL &&
|
|
|
|
TII->isMoveInstr(*NMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx)) {
|
|
|
|
assert(SrcSubIdx == 0 && DstSubIdx == 0 &&
|
|
|
|
"Sub-register indices still around?");
|
|
|
|
// 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.
|
|
|
|
if (!Uses.count(DstReg) && !Defs.count(SrcReg)) {
|
|
|
|
--MBBI;
|
|
|
|
MBB.remove(NMI);
|
|
|
|
MBB.insert(InsertPos, NMI);
|
|
|
|
++NumMovedInsts;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2009-07-10 09:54:42 +08:00
|
|
|
break;
|
2010-06-09 09:46:50 +08:00
|
|
|
}
|
|
|
|
TrackDefUses(NMI, Defs, Uses);
|
2009-08-15 15:59:10 +08:00
|
|
|
--Pos;
|
2009-07-10 09:54:42 +08:00
|
|
|
}
|
2010-06-09 09:46:50 +08:00
|
|
|
|
2009-08-15 15:59:10 +08:00
|
|
|
Mask |= (1 << Pos);
|
2010-03-18 07:14:23 +08:00
|
|
|
// Tag along (firstcond[0] << 4) with the mask.
|
|
|
|
Mask |= (CC & 1) << 4;
|
2009-07-10 09:54:42 +08:00
|
|
|
MIB.addImm(Mask);
|
|
|
|
Modified = true;
|
|
|
|
++NumITs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Thumb2ITBlockPass::runOnMachineFunction(MachineFunction &Fn) {
|
|
|
|
const TargetMachine &TM = Fn.getTarget();
|
|
|
|
AFI = Fn.getInfo<ARMFunctionInfo>();
|
2009-07-11 15:26:20 +08:00
|
|
|
TII = static_cast<const Thumb2InstrInfo*>(TM.getInstrInfo());
|
2009-07-10 09:54:42 +08:00
|
|
|
|
|
|
|
if (!AFI->isThumbFunction())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool Modified = false;
|
2010-06-09 09:46:50 +08:00
|
|
|
for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; ) {
|
2009-07-10 09:54:42 +08:00
|
|
|
MachineBasicBlock &MBB = *MFI;
|
2010-06-09 09:46:50 +08:00
|
|
|
++MFI;
|
|
|
|
if (PreRegAlloc)
|
|
|
|
Modified |= InsertITBlocks(MBB);
|
|
|
|
else
|
|
|
|
Modified |= InsertITInstructions(MBB);
|
2009-07-10 09:54:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
|
2009-08-08 10:54:37 +08:00
|
|
|
/// createThumb2ITBlockPass - Returns an instance of the Thumb2 IT blocks
|
2009-07-10 09:54:42 +08:00
|
|
|
/// insertion pass.
|
2010-06-09 09:46:50 +08:00
|
|
|
FunctionPass *llvm::createThumb2ITBlockPass(bool PreAlloc) {
|
|
|
|
return new Thumb2ITBlockPass(PreAlloc);
|
2009-07-10 09:54:42 +08:00
|
|
|
}
|