#include "ARM.h"
#include "ARMMachineFunctionInfo.h"
#include "ARMSubtarget.h"
#include "MCTargetDesc/ARMBaseInfo.h"
#include "Thumb2InstrInfo.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineInstrBundle.h"
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/MC/MCInstrDesc.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/Support/Debug.h"
#include <cassert>
#include <new>
using namespace llvm;
#define DEBUG_TYPE "arm-mve-vpt"
namespace {
class MVEVPTBlock : public MachineFunctionPass {
public:
static char ID;
const Thumb2InstrInfo *TII;
const TargetRegisterInfo *TRI;
MVEVPTBlock() : MachineFunctionPass(ID) {}
bool runOnMachineFunction(MachineFunction &Fn) override;
MachineFunctionProperties getRequiredProperties() const override {
return MachineFunctionProperties().set(
MachineFunctionProperties::Property::NoVRegs);
}
StringRef getPassName() const override {
return "MVE VPT block insertion pass";
}
private:
bool InsertVPTBlocks(MachineBasicBlock &MBB);
};
char MVEVPTBlock::ID = 0;
}
INITIALIZE_PASS(MVEVPTBlock, DEBUG_TYPE, "ARM MVE VPT block pass", false, false)
static MachineInstr *findVCMPToFoldIntoVPST(MachineBasicBlock::iterator MI,
const TargetRegisterInfo *TRI,
unsigned &NewOpcode) {
MachineBasicBlock::iterator CmpMI = MI;
while (CmpMI != MI->getParent()->begin()) {
--CmpMI;
if (CmpMI->modifiesRegister(ARM::VPR, TRI))
break;
if (CmpMI->readsRegister(ARM::VPR, TRI))
break;
}
if (CmpMI == MI)
return nullptr;
NewOpcode = VCMPOpcodeToVPT(CmpMI->getOpcode());
if (NewOpcode == 0)
return nullptr;
if (registerDefinedBetween(CmpMI->getOperand(1).getReg(), std::next(CmpMI),
MI, TRI))
return nullptr;
if (registerDefinedBetween(CmpMI->getOperand(2).getReg(), std::next(CmpMI),
MI, TRI))
return nullptr;
return &*CmpMI;
}
static bool StepOverPredicatedInstrs(MachineBasicBlock::instr_iterator &Iter,
MachineBasicBlock::instr_iterator EndIter,
unsigned MaxSteps,
unsigned &NumInstrsSteppedOver) {
ARMVCC::VPTCodes NextPred = ARMVCC::None;
Register PredReg;
NumInstrsSteppedOver = 0;
while (Iter != EndIter) {
if (Iter->isDebugInstr()) {
++Iter;
continue;
}
NextPred = getVPTInstrPredicate(*Iter, PredReg);
assert(NextPred != ARMVCC::Else &&
"VPT block pass does not expect Else preds");
if (NextPred == ARMVCC::None || MaxSteps == 0)
break;
--MaxSteps;
++Iter;
++NumInstrsSteppedOver;
};
return NumInstrsSteppedOver != 0 &&
(NextPred == ARMVCC::None || Iter == EndIter);
}
static bool IsVPRDefinedOrKilledByBlock(MachineBasicBlock::iterator Iter,
MachineBasicBlock::iterator End) {
for (; Iter != End; ++Iter)
if (Iter->definesRegister(ARM::VPR) || Iter->killsRegister(ARM::VPR))
return true;
return false;
}
static ARM::PredBlockMask GetInitialBlockMask(unsigned BlockSize) {
switch (BlockSize) {
case 1:
return ARM::PredBlockMask::T;
case 2:
return ARM::PredBlockMask::TT;
case 3:
return ARM::PredBlockMask::TTT;
case 4:
return ARM::PredBlockMask::TTTT;
default:
llvm_unreachable("Invalid BlockSize!");
}
}
static ARM::PredBlockMask
CreateVPTBlock(MachineBasicBlock::instr_iterator &Iter,
MachineBasicBlock::instr_iterator EndIter,
SmallVectorImpl<MachineInstr *> &DeadInstructions) {
MachineBasicBlock::instr_iterator BlockBeg = Iter;
(void)BlockBeg;
assert(getVPTInstrPredicate(*Iter) == ARMVCC::Then &&
"Expected a Predicated Instruction");
LLVM_DEBUG(dbgs() << "VPT block created for: "; Iter->dump());
unsigned BlockSize;
StepOverPredicatedInstrs(Iter, EndIter, 4, BlockSize);
LLVM_DEBUG(for (MachineBasicBlock::instr_iterator AddedInstIter =
std::next(BlockBeg);
AddedInstIter != Iter; ++AddedInstIter) {
if (AddedInstIter->isDebugInstr())
continue;
dbgs() << " adding: ";
AddedInstIter->dump();
});
ARM::PredBlockMask BlockMask = GetInitialBlockMask(BlockSize);
ARMVCC::VPTCodes CurrentPredicate = ARMVCC::Else;
while (BlockSize < 4 && Iter != EndIter &&
Iter->getOpcode() == ARM::MVE_VPNOT) {
unsigned ElseInstCnt = 0;
MachineBasicBlock::instr_iterator VPNOTBlockEndIter = std::next(Iter);
if (!StepOverPredicatedInstrs(VPNOTBlockEndIter, EndIter, (4 - BlockSize),
ElseInstCnt))
break;
if (!IsVPRDefinedOrKilledByBlock(Iter, VPNOTBlockEndIter))
break;
LLVM_DEBUG(dbgs() << " removing VPNOT: "; Iter->dump());
BlockSize += ElseInstCnt;
assert(BlockSize <= 4 && "Block is too large!");
DeadInstructions.push_back(&*Iter);
++Iter;
for (; Iter != VPNOTBlockEndIter; ++Iter) {
if (Iter->isDebugInstr())
continue;
int OpIdx = findFirstVPTPredOperandIdx(*Iter);
assert(OpIdx != -1);
Iter->getOperand(OpIdx).setImm(CurrentPredicate);
BlockMask = expandPredBlockMask(BlockMask, CurrentPredicate);
LLVM_DEBUG(dbgs() << " adding : "; Iter->dump());
}
CurrentPredicate =
(CurrentPredicate == ARMVCC::Then ? ARMVCC::Else : ARMVCC::Then);
}
return BlockMask;
}
bool MVEVPTBlock::InsertVPTBlocks(MachineBasicBlock &Block) {
bool Modified = false;
MachineBasicBlock::instr_iterator MBIter = Block.instr_begin();
MachineBasicBlock::instr_iterator EndIter = Block.instr_end();
SmallVector<MachineInstr *, 4> DeadInstructions;
while (MBIter != EndIter) {
MachineInstr *MI = &*MBIter;
Register PredReg;
DebugLoc DL = MI->getDebugLoc();
ARMVCC::VPTCodes Pred = getVPTInstrPredicate(*MI, PredReg);
assert(Pred != ARMVCC::Else && "VPT block pass does not expect Else preds");
if (Pred == ARMVCC::None) {
++MBIter;
continue;
}
ARM::PredBlockMask BlockMask =
CreateVPTBlock(MBIter, EndIter, DeadInstructions);
MachineInstrBuilder MIBuilder;
unsigned NewOpcode;
LLVM_DEBUG(dbgs() << " final block mask: " << (unsigned)BlockMask << "\n");
if (MachineInstr *VCMP = findVCMPToFoldIntoVPST(MI, TRI, NewOpcode)) {
LLVM_DEBUG(dbgs() << " folding VCMP into VPST: "; VCMP->dump());
MIBuilder = BuildMI(Block, MI, DL, TII->get(NewOpcode));
MIBuilder.addImm((uint64_t)BlockMask);
MIBuilder.add(VCMP->getOperand(1));
MIBuilder.add(VCMP->getOperand(2));
MIBuilder.add(VCMP->getOperand(3));
for (MachineInstr &MII :
make_range(VCMP->getIterator(), MI->getIterator())) {
MII.clearRegisterKills(VCMP->getOperand(1).getReg(), TRI);
MII.clearRegisterKills(VCMP->getOperand(2).getReg(), TRI);
}
VCMP->eraseFromParent();
} else {
MIBuilder = BuildMI(Block, MI, DL, TII->get(ARM::MVE_VPST));
MIBuilder.addImm((uint64_t)BlockMask);
}
for (MachineInstr *DeadMI : DeadInstructions)
DeadMI->eraseFromParent();
DeadInstructions.clear();
finalizeBundle(
Block, MachineBasicBlock::instr_iterator(MIBuilder.getInstr()), MBIter);
Modified = true;
}
return Modified;
}
bool MVEVPTBlock::runOnMachineFunction(MachineFunction &Fn) {
const ARMSubtarget &STI = Fn.getSubtarget<ARMSubtarget>();
if (!STI.isThumb2() || !STI.hasMVEIntegerOps())
return false;
TII = static_cast<const Thumb2InstrInfo *>(STI.getInstrInfo());
TRI = STI.getRegisterInfo();
LLVM_DEBUG(dbgs() << "********** ARM MVE VPT BLOCKS **********\n"
<< "********** Function: " << Fn.getName() << '\n');
bool Modified = false;
for (MachineBasicBlock &MBB : Fn)
Modified |= InsertVPTBlocks(MBB);
LLVM_DEBUG(dbgs() << "**************************************\n");
return Modified;
}
FunctionPass *llvm::createMVEVPTBlockPass() { return new MVEVPTBlock(); }