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"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
STATISTIC(NumITs, "Number of IT blocks inserted");
|
|
|
|
|
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
struct Thumb2ITBlockPass : public MachineFunctionPass {
|
2009-07-10 01:54:42 +00:00
|
|
|
static char ID;
|
|
|
|
Thumb2ITBlockPass() : MachineFunctionPass(&ID) {}
|
|
|
|
|
2009-07-11 07:26:20 +00:00
|
|
|
const Thumb2InstrInfo *TII;
|
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:
|
|
|
|
bool InsertITBlocks(MachineBasicBlock &MBB);
|
|
|
|
};
|
|
|
|
char Thumb2ITBlockPass::ID = 0;
|
|
|
|
}
|
|
|
|
|
2009-09-28 09:14:39 +00:00
|
|
|
static ARMCC::CondCodes getPredicate(const MachineInstr *MI, unsigned &PredReg){
|
2009-07-11 07:26:20 +00:00
|
|
|
unsigned Opc = MI->getOpcode();
|
|
|
|
if (Opc == ARM::tBcc || Opc == ARM::t2Bcc)
|
|
|
|
return ARMCC::AL;
|
2009-09-28 09:14:39 +00:00
|
|
|
return llvm::getInstrPredicate(MI, PredReg);
|
2009-07-11 07:26:20 +00:00
|
|
|
}
|
|
|
|
|
2009-07-10 01:54:42 +00:00
|
|
|
bool Thumb2ITBlockPass::InsertITBlocks(MachineBasicBlock &MBB) {
|
|
|
|
bool Modified = false;
|
|
|
|
|
|
|
|
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;
|
|
|
|
ARMCC::CondCodes CC = getPredicate(MI, PredReg);
|
|
|
|
|
2009-07-10 01:54:42 +00:00
|
|
|
if (CC == ARMCC::AL) {
|
|
|
|
++MBBI;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert an IT instruction.
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII->get(ARM::t2IT))
|
|
|
|
.addImm(CC);
|
|
|
|
++MBBI;
|
|
|
|
|
2009-08-15 07:59:10 +00:00
|
|
|
// Finalize IT mask.
|
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.
|
|
|
|
while (MBBI != E && Pos &&
|
|
|
|
(!MI->getDesc().isBranch() && !MI->getDesc().isReturn())) {
|
2009-09-28 20:47:15 +00:00
|
|
|
MachineInstr *NMI = &*MBBI;
|
2009-10-15 22:25:32 +00:00
|
|
|
MI = NMI;
|
2009-09-28 20:47:15 +00:00
|
|
|
DebugLoc ndl = NMI->getDebugLoc();
|
|
|
|
unsigned NPredReg = 0;
|
|
|
|
ARMCC::CondCodes NCC = getPredicate(NMI, NPredReg);
|
2010-03-16 16:36:54 +00:00
|
|
|
if (NCC == CC || NCC == OCC)
|
|
|
|
Mask |= (NCC & 1) << Pos;
|
|
|
|
else
|
2009-07-10 01:54:42 +00:00
|
|
|
break;
|
2009-08-15 07:59:10 +00:00
|
|
|
--Pos;
|
2009-07-10 01:54:42 +00:00
|
|
|
++MBBI;
|
|
|
|
}
|
2009-08-15 07:59:10 +00:00
|
|
|
Mask |= (1 << Pos);
|
2010-03-16 16:36:54 +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);
|
|
|
|
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());
|
2009-07-10 01:54:42 +00:00
|
|
|
|
|
|
|
if (!AFI->isThumbFunction())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool Modified = false;
|
|
|
|
for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
|
|
|
|
++MFI) {
|
|
|
|
MachineBasicBlock &MBB = *MFI;
|
|
|
|
Modified |= InsertITBlocks(MBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
FunctionPass *llvm::createThumb2ITBlockPass() {
|
|
|
|
return new Thumb2ITBlockPass();
|
|
|
|
}
|