2016-11-11 16:27:37 +08:00
|
|
|
//===- ARMInstructionSelector.cpp ----------------------------*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
/// This file implements the targeting of the InstructionSelector class for ARM.
|
|
|
|
/// \todo This should be generated by TableGen.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ARMRegisterBankInfo.h"
|
|
|
|
#include "ARMSubtarget.h"
|
|
|
|
#include "ARMTargetMachine.h"
|
2017-04-28 17:10:38 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
|
2016-12-16 20:54:46 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2016-11-11 16:27:37 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "arm-isel"
|
|
|
|
|
[globalisel][tablegen] Partially fix compile-time regressions by converting matcher to state-machine(s)
Summary:
Replace the matcher if-statements for each rule with a state-machine. This
significantly reduces compile time, memory allocations, and cumulative memory
allocation when compiling AArch64InstructionSelector.cpp.o after r303259 is
recommitted.
The following patches will expand on this further to fully fix the regressions.
Reviewers: rovka, ab, t.p.northover, qcolombet, aditya_nandakumar
Reviewed By: ab
Subscribers: vitalybuka, aemerson, javed.absar, igorb, llvm-commits, kristof.beyls
Differential Revision: https://reviews.llvm.org/D33758
llvm-svn: 307079
2017-07-04 22:35:06 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h"
|
|
|
|
|
2016-11-11 16:27:37 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#ifndef LLVM_BUILD_GLOBAL_ISEL
|
|
|
|
#error "You shouldn't build this"
|
|
|
|
#endif
|
|
|
|
|
2017-04-28 17:10:38 +08:00
|
|
|
namespace {
|
2017-05-02 17:40:49 +08:00
|
|
|
|
|
|
|
#define GET_GLOBALISEL_PREDICATE_BITSET
|
|
|
|
#include "ARMGenGlobalISel.inc"
|
|
|
|
#undef GET_GLOBALISEL_PREDICATE_BITSET
|
|
|
|
|
2017-04-28 17:10:38 +08:00
|
|
|
class ARMInstructionSelector : public InstructionSelector {
|
|
|
|
public:
|
2017-05-02 17:40:49 +08:00
|
|
|
ARMInstructionSelector(const ARMBaseTargetMachine &TM, const ARMSubtarget &STI,
|
2017-04-28 17:10:38 +08:00
|
|
|
const ARMRegisterBankInfo &RBI);
|
|
|
|
|
|
|
|
bool select(MachineInstr &I) const override;
|
|
|
|
|
|
|
|
private:
|
2017-05-02 17:40:49 +08:00
|
|
|
bool selectImpl(MachineInstr &I) const;
|
|
|
|
|
2017-06-19 17:40:51 +08:00
|
|
|
bool selectICmp(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII,
|
|
|
|
MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
|
|
|
|
const RegisterBankInfo &RBI) const;
|
|
|
|
|
2017-06-27 17:19:51 +08:00
|
|
|
bool selectSelect(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII,
|
|
|
|
MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
|
|
|
|
const RegisterBankInfo &RBI) const;
|
|
|
|
|
2017-04-28 17:10:38 +08:00
|
|
|
const ARMBaseInstrInfo &TII;
|
|
|
|
const ARMBaseRegisterInfo &TRI;
|
2017-05-02 17:40:49 +08:00
|
|
|
const ARMBaseTargetMachine &TM;
|
2017-04-28 17:10:38 +08:00
|
|
|
const ARMRegisterBankInfo &RBI;
|
2017-05-02 17:40:49 +08:00
|
|
|
const ARMSubtarget &STI;
|
|
|
|
|
|
|
|
#define GET_GLOBALISEL_PREDICATES_DECL
|
|
|
|
#include "ARMGenGlobalISel.inc"
|
|
|
|
#undef GET_GLOBALISEL_PREDICATES_DECL
|
|
|
|
|
|
|
|
// We declare the temporaries used by selectImpl() in the class to minimize the
|
|
|
|
// cost of constructing placeholder values.
|
|
|
|
#define GET_GLOBALISEL_TEMPORARIES_DECL
|
|
|
|
#include "ARMGenGlobalISel.inc"
|
|
|
|
#undef GET_GLOBALISEL_TEMPORARIES_DECL
|
2017-04-28 17:10:38 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
InstructionSelector *
|
2017-05-02 17:40:49 +08:00
|
|
|
createARMInstructionSelector(const ARMBaseTargetMachine &TM,
|
|
|
|
const ARMSubtarget &STI,
|
2017-04-28 17:10:38 +08:00
|
|
|
const ARMRegisterBankInfo &RBI) {
|
2017-05-02 17:40:49 +08:00
|
|
|
return new ARMInstructionSelector(TM, STI, RBI);
|
2017-04-28 17:10:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 17:40:49 +08:00
|
|
|
unsigned zero_reg = 0;
|
|
|
|
|
|
|
|
#define GET_GLOBALISEL_IMPL
|
|
|
|
#include "ARMGenGlobalISel.inc"
|
|
|
|
#undef GET_GLOBALISEL_IMPL
|
|
|
|
|
|
|
|
ARMInstructionSelector::ARMInstructionSelector(const ARMBaseTargetMachine &TM,
|
|
|
|
const ARMSubtarget &STI,
|
2016-11-11 16:27:37 +08:00
|
|
|
const ARMRegisterBankInfo &RBI)
|
2016-11-16 00:42:10 +08:00
|
|
|
: InstructionSelector(), TII(*STI.getInstrInfo()),
|
2017-05-02 17:40:49 +08:00
|
|
|
TRI(*STI.getRegisterInfo()), TM(TM), RBI(RBI), STI(STI),
|
|
|
|
#define GET_GLOBALISEL_PREDICATES_INIT
|
|
|
|
#include "ARMGenGlobalISel.inc"
|
|
|
|
#undef GET_GLOBALISEL_PREDICATES_INIT
|
|
|
|
#define GET_GLOBALISEL_TEMPORARIES_INIT
|
|
|
|
#include "ARMGenGlobalISel.inc"
|
|
|
|
#undef GET_GLOBALISEL_TEMPORARIES_INIT
|
|
|
|
{
|
|
|
|
}
|
2016-11-11 16:27:37 +08:00
|
|
|
|
2016-12-16 20:54:46 +08:00
|
|
|
static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
|
|
|
|
MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
|
|
|
|
const RegisterBankInfo &RBI) {
|
|
|
|
unsigned DstReg = I.getOperand(0).getReg();
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(DstReg))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const RegisterBank *RegBank = RBI.getRegBank(DstReg, MRI, TRI);
|
2016-12-16 21:13:03 +08:00
|
|
|
(void)RegBank;
|
2016-12-16 20:54:46 +08:00
|
|
|
assert(RegBank && "Can't get reg bank for virtual register");
|
|
|
|
|
2016-12-19 22:07:50 +08:00
|
|
|
const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
|
2017-02-08 21:23:04 +08:00
|
|
|
assert((RegBank->getID() == ARM::GPRRegBankID ||
|
|
|
|
RegBank->getID() == ARM::FPRRegBankID) &&
|
|
|
|
"Unsupported reg bank");
|
|
|
|
|
2016-12-16 20:54:46 +08:00
|
|
|
const TargetRegisterClass *RC = &ARM::GPRRegClass;
|
|
|
|
|
2017-02-08 21:23:04 +08:00
|
|
|
if (RegBank->getID() == ARM::FPRRegBankID) {
|
2017-02-16 20:19:52 +08:00
|
|
|
if (DstSize == 32)
|
|
|
|
RC = &ARM::SPRRegClass;
|
|
|
|
else if (DstSize == 64)
|
|
|
|
RC = &ARM::DPRRegClass;
|
|
|
|
else
|
|
|
|
llvm_unreachable("Unsupported destination size");
|
2017-02-08 21:23:04 +08:00
|
|
|
}
|
|
|
|
|
2016-12-16 20:54:46 +08:00
|
|
|
// No need to constrain SrcReg. It will get constrained when
|
|
|
|
// we hit another of its uses or its defs.
|
|
|
|
// Copies do not have constraints.
|
|
|
|
if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
|
|
|
|
DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
|
|
|
|
<< " operand\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-07 20:35:05 +08:00
|
|
|
static bool selectMergeValues(MachineInstrBuilder &MIB,
|
|
|
|
const ARMBaseInstrInfo &TII,
|
|
|
|
MachineRegisterInfo &MRI,
|
|
|
|
const TargetRegisterInfo &TRI,
|
|
|
|
const RegisterBankInfo &RBI) {
|
|
|
|
assert(TII.getSubtarget().hasVFP2() && "Can't select merge without VFP");
|
|
|
|
|
|
|
|
// We only support G_MERGE_VALUES as a way to stick together two scalar GPRs
|
2017-02-16 20:19:57 +08:00
|
|
|
// into one DPR.
|
|
|
|
unsigned VReg0 = MIB->getOperand(0).getReg();
|
|
|
|
(void)VReg0;
|
|
|
|
assert(MRI.getType(VReg0).getSizeInBits() == 64 &&
|
|
|
|
RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID &&
|
2017-06-07 20:35:05 +08:00
|
|
|
"Unsupported operand for G_MERGE_VALUES");
|
2017-02-16 20:19:57 +08:00
|
|
|
unsigned VReg1 = MIB->getOperand(1).getReg();
|
|
|
|
(void)VReg1;
|
|
|
|
assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
|
|
|
|
RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
|
2017-06-07 20:35:05 +08:00
|
|
|
"Unsupported operand for G_MERGE_VALUES");
|
|
|
|
unsigned VReg2 = MIB->getOperand(2).getReg();
|
2017-02-16 20:19:57 +08:00
|
|
|
(void)VReg2;
|
|
|
|
assert(MRI.getType(VReg2).getSizeInBits() == 32 &&
|
|
|
|
RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID &&
|
2017-06-07 20:35:05 +08:00
|
|
|
"Unsupported operand for G_MERGE_VALUES");
|
2017-02-16 20:19:57 +08:00
|
|
|
|
|
|
|
MIB->setDesc(TII.get(ARM::VMOVDRR));
|
|
|
|
MIB.add(predOps(ARMCC::AL));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-07 20:35:05 +08:00
|
|
|
static bool selectUnmergeValues(MachineInstrBuilder &MIB,
|
|
|
|
const ARMBaseInstrInfo &TII,
|
|
|
|
MachineRegisterInfo &MRI,
|
|
|
|
const TargetRegisterInfo &TRI,
|
|
|
|
const RegisterBankInfo &RBI) {
|
|
|
|
assert(TII.getSubtarget().hasVFP2() && "Can't select unmerge without VFP");
|
2017-02-16 20:19:57 +08:00
|
|
|
|
2017-06-07 20:35:05 +08:00
|
|
|
// We only support G_UNMERGE_VALUES as a way to break up one DPR into two
|
|
|
|
// GPRs.
|
2017-02-16 20:19:57 +08:00
|
|
|
unsigned VReg0 = MIB->getOperand(0).getReg();
|
|
|
|
(void)VReg0;
|
|
|
|
assert(MRI.getType(VReg0).getSizeInBits() == 32 &&
|
|
|
|
RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID &&
|
2017-06-07 20:35:05 +08:00
|
|
|
"Unsupported operand for G_UNMERGE_VALUES");
|
2017-02-16 20:19:57 +08:00
|
|
|
unsigned VReg1 = MIB->getOperand(1).getReg();
|
|
|
|
(void)VReg1;
|
2017-06-07 20:35:05 +08:00
|
|
|
assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
|
|
|
|
RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
|
|
|
|
"Unsupported operand for G_UNMERGE_VALUES");
|
|
|
|
unsigned VReg2 = MIB->getOperand(2).getReg();
|
|
|
|
(void)VReg2;
|
|
|
|
assert(MRI.getType(VReg2).getSizeInBits() == 64 &&
|
|
|
|
RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::FPRRegBankID &&
|
|
|
|
"Unsupported operand for G_UNMERGE_VALUES");
|
2017-02-16 20:19:57 +08:00
|
|
|
|
2017-06-07 20:35:05 +08:00
|
|
|
MIB->setDesc(TII.get(ARM::VMOVRRD));
|
2017-02-16 20:19:57 +08:00
|
|
|
MIB.add(predOps(ARMCC::AL));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-25 16:10:40 +08:00
|
|
|
/// Select the opcode for simple extensions (that translate to a single SXT/UXT
|
|
|
|
/// instruction). Extension operations more complicated than that should not
|
2017-02-17 21:44:19 +08:00
|
|
|
/// invoke this. Returns the original opcode if it doesn't know how to select a
|
|
|
|
/// better one.
|
2017-01-25 16:10:40 +08:00
|
|
|
static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) {
|
|
|
|
using namespace TargetOpcode;
|
|
|
|
|
2017-02-17 21:44:19 +08:00
|
|
|
if (Size != 8 && Size != 16)
|
|
|
|
return Opc;
|
2017-01-25 16:10:40 +08:00
|
|
|
|
|
|
|
if (Opc == G_SEXT)
|
|
|
|
return Size == 8 ? ARM::SXTB : ARM::SXTH;
|
|
|
|
|
|
|
|
if (Opc == G_ZEXT)
|
|
|
|
return Size == 8 ? ARM::UXTB : ARM::UXTH;
|
|
|
|
|
2017-02-17 21:44:19 +08:00
|
|
|
return Opc;
|
2017-01-25 16:10:40 +08:00
|
|
|
}
|
|
|
|
|
2017-02-24 22:01:27 +08:00
|
|
|
/// Select the opcode for simple loads and stores. For types smaller than 32
|
|
|
|
/// bits, the value will be zero extended. Returns the original opcode if it
|
|
|
|
/// doesn't know how to select a better one.
|
|
|
|
static unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank,
|
|
|
|
unsigned Size) {
|
|
|
|
bool isStore = Opc == TargetOpcode::G_STORE;
|
|
|
|
|
2017-02-16 22:10:50 +08:00
|
|
|
if (RegBank == ARM::GPRRegBankID) {
|
|
|
|
switch (Size) {
|
|
|
|
case 1:
|
|
|
|
case 8:
|
2017-02-24 22:01:27 +08:00
|
|
|
return isStore ? ARM::STRBi12 : ARM::LDRBi12;
|
2017-02-16 22:10:50 +08:00
|
|
|
case 16:
|
2017-02-24 22:01:27 +08:00
|
|
|
return isStore ? ARM::STRH : ARM::LDRH;
|
2017-02-16 22:10:50 +08:00
|
|
|
case 32:
|
2017-02-24 22:01:27 +08:00
|
|
|
return isStore ? ARM::STRi12 : ARM::LDRi12;
|
2017-02-17 21:44:19 +08:00
|
|
|
default:
|
2017-02-24 22:01:27 +08:00
|
|
|
return Opc;
|
2017-02-16 22:10:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 21:44:19 +08:00
|
|
|
if (RegBank == ARM::FPRRegBankID) {
|
|
|
|
switch (Size) {
|
|
|
|
case 32:
|
2017-02-24 22:01:27 +08:00
|
|
|
return isStore ? ARM::VSTRS : ARM::VLDRS;
|
2017-02-17 21:44:19 +08:00
|
|
|
case 64:
|
2017-02-24 22:01:27 +08:00
|
|
|
return isStore ? ARM::VSTRD : ARM::VLDRD;
|
2017-02-17 21:44:19 +08:00
|
|
|
default:
|
2017-02-24 22:01:27 +08:00
|
|
|
return Opc;
|
2017-02-17 21:44:19 +08:00
|
|
|
}
|
2017-01-26 17:20:47 +08:00
|
|
|
}
|
|
|
|
|
2017-02-24 22:01:27 +08:00
|
|
|
return Opc;
|
2017-01-26 17:20:47 +08:00
|
|
|
}
|
|
|
|
|
2017-06-19 17:40:51 +08:00
|
|
|
static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
|
|
|
|
switch (Pred) {
|
|
|
|
// Needs two compares...
|
|
|
|
case CmpInst::FCMP_ONE:
|
|
|
|
case CmpInst::FCMP_UEQ:
|
|
|
|
default:
|
|
|
|
// AL is our "false" for now. The other two need more compares.
|
|
|
|
return ARMCC::AL;
|
|
|
|
case CmpInst::ICMP_EQ:
|
|
|
|
case CmpInst::FCMP_OEQ:
|
|
|
|
return ARMCC::EQ;
|
|
|
|
case CmpInst::ICMP_SGT:
|
|
|
|
case CmpInst::FCMP_OGT:
|
|
|
|
return ARMCC::GT;
|
|
|
|
case CmpInst::ICMP_SGE:
|
|
|
|
case CmpInst::FCMP_OGE:
|
|
|
|
return ARMCC::GE;
|
|
|
|
case CmpInst::ICMP_UGT:
|
|
|
|
case CmpInst::FCMP_UGT:
|
|
|
|
return ARMCC::HI;
|
|
|
|
case CmpInst::FCMP_OLT:
|
|
|
|
return ARMCC::MI;
|
|
|
|
case CmpInst::ICMP_ULE:
|
|
|
|
case CmpInst::FCMP_OLE:
|
|
|
|
return ARMCC::LS;
|
|
|
|
case CmpInst::FCMP_ORD:
|
|
|
|
return ARMCC::VC;
|
|
|
|
case CmpInst::FCMP_UNO:
|
|
|
|
return ARMCC::VS;
|
|
|
|
case CmpInst::FCMP_UGE:
|
|
|
|
return ARMCC::PL;
|
|
|
|
case CmpInst::ICMP_SLT:
|
|
|
|
case CmpInst::FCMP_ULT:
|
|
|
|
return ARMCC::LT;
|
|
|
|
case CmpInst::ICMP_SLE:
|
|
|
|
case CmpInst::FCMP_ULE:
|
|
|
|
return ARMCC::LE;
|
|
|
|
case CmpInst::FCMP_UNE:
|
|
|
|
case CmpInst::ICMP_NE:
|
|
|
|
return ARMCC::NE;
|
|
|
|
case CmpInst::ICMP_UGE:
|
|
|
|
return ARMCC::HS;
|
|
|
|
case CmpInst::ICMP_ULT:
|
|
|
|
return ARMCC::LO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ARMInstructionSelector::selectICmp(MachineInstrBuilder &MIB,
|
|
|
|
const ARMBaseInstrInfo &TII,
|
|
|
|
MachineRegisterInfo &MRI,
|
|
|
|
const TargetRegisterInfo &TRI,
|
|
|
|
const RegisterBankInfo &RBI) const {
|
|
|
|
auto &MBB = *MIB->getParent();
|
|
|
|
auto InsertBefore = std::next(MIB->getIterator());
|
|
|
|
auto &DebugLoc = MIB->getDebugLoc();
|
|
|
|
|
|
|
|
// Move 0 into the result register.
|
|
|
|
auto Mov0I = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::MOVi))
|
|
|
|
.addDef(MRI.createVirtualRegister(&ARM::GPRRegClass))
|
|
|
|
.addImm(0)
|
|
|
|
.add(predOps(ARMCC::AL))
|
|
|
|
.add(condCodeOp());
|
|
|
|
if (!constrainSelectedInstRegOperands(*Mov0I, TII, TRI, RBI))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Perform the comparison.
|
|
|
|
auto LHSReg = MIB->getOperand(2).getReg();
|
|
|
|
auto RHSReg = MIB->getOperand(3).getReg();
|
|
|
|
assert(MRI.getType(LHSReg) == MRI.getType(RHSReg) &&
|
|
|
|
MRI.getType(LHSReg).getSizeInBits() == 32 &&
|
|
|
|
MRI.getType(RHSReg).getSizeInBits() == 32 &&
|
|
|
|
"Unsupported types for comparison operation");
|
|
|
|
auto CmpI = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::CMPrr))
|
|
|
|
.addUse(LHSReg)
|
|
|
|
.addUse(RHSReg)
|
|
|
|
.add(predOps(ARMCC::AL));
|
|
|
|
if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Move 1 into the result register if the flags say so.
|
|
|
|
auto ResReg = MIB->getOperand(0).getReg();
|
|
|
|
auto Cond =
|
|
|
|
static_cast<CmpInst::Predicate>(MIB->getOperand(1).getPredicate());
|
|
|
|
auto ARMCond = getComparePred(Cond);
|
|
|
|
if (ARMCond == ARMCC::AL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto Mov1I = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::MOVCCi))
|
|
|
|
.addDef(ResReg)
|
|
|
|
.addUse(Mov0I->getOperand(0).getReg())
|
|
|
|
.addImm(1)
|
|
|
|
.add(predOps(ARMCond, ARM::CPSR));
|
|
|
|
if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MIB->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-27 17:19:51 +08:00
|
|
|
bool ARMInstructionSelector::selectSelect(MachineInstrBuilder &MIB,
|
|
|
|
const ARMBaseInstrInfo &TII,
|
|
|
|
MachineRegisterInfo &MRI,
|
|
|
|
const TargetRegisterInfo &TRI,
|
|
|
|
const RegisterBankInfo &RBI) const {
|
|
|
|
auto &MBB = *MIB->getParent();
|
|
|
|
auto InsertBefore = std::next(MIB->getIterator());
|
|
|
|
auto &DebugLoc = MIB->getDebugLoc();
|
|
|
|
|
|
|
|
// Compare the condition to 0.
|
|
|
|
auto CondReg = MIB->getOperand(1).getReg();
|
|
|
|
assert(MRI.getType(CondReg).getSizeInBits() == 1 &&
|
|
|
|
RBI.getRegBank(CondReg, MRI, TRI)->getID() == ARM::GPRRegBankID &&
|
|
|
|
"Unsupported types for select operation");
|
|
|
|
auto CmpI = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::CMPri))
|
|
|
|
.addUse(CondReg)
|
|
|
|
.addImm(0)
|
|
|
|
.add(predOps(ARMCC::AL));
|
|
|
|
if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Move a value into the result register based on the result of the
|
|
|
|
// comparison.
|
|
|
|
auto ResReg = MIB->getOperand(0).getReg();
|
|
|
|
auto TrueReg = MIB->getOperand(2).getReg();
|
|
|
|
auto FalseReg = MIB->getOperand(3).getReg();
|
|
|
|
assert(MRI.getType(ResReg) == MRI.getType(TrueReg) &&
|
|
|
|
MRI.getType(TrueReg) == MRI.getType(FalseReg) &&
|
|
|
|
MRI.getType(FalseReg).getSizeInBits() == 32 &&
|
|
|
|
RBI.getRegBank(TrueReg, MRI, TRI)->getID() == ARM::GPRRegBankID &&
|
|
|
|
RBI.getRegBank(FalseReg, MRI, TRI)->getID() == ARM::GPRRegBankID &&
|
|
|
|
"Unsupported types for select operation");
|
|
|
|
auto Mov1I = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::MOVCCr))
|
|
|
|
.addDef(ResReg)
|
|
|
|
.addUse(TrueReg)
|
|
|
|
.addUse(FalseReg)
|
|
|
|
.add(predOps(ARMCC::EQ, ARM::CPSR));
|
|
|
|
if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MIB->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-16 20:54:46 +08:00
|
|
|
bool ARMInstructionSelector::select(MachineInstr &I) const {
|
|
|
|
assert(I.getParent() && "Instruction should be in a basic block!");
|
|
|
|
assert(I.getParent()->getParent() && "Instruction should be in a function!");
|
|
|
|
|
|
|
|
auto &MBB = *I.getParent();
|
|
|
|
auto &MF = *MBB.getParent();
|
|
|
|
auto &MRI = MF.getRegInfo();
|
|
|
|
|
|
|
|
if (!isPreISelGenericOpcode(I.getOpcode())) {
|
|
|
|
if (I.isCopy())
|
|
|
|
return selectCopy(I, TII, MRI, TRI, RBI);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-02 17:40:49 +08:00
|
|
|
if (selectImpl(I))
|
|
|
|
return true;
|
|
|
|
|
2016-12-19 19:26:31 +08:00
|
|
|
MachineInstrBuilder MIB{MF, I};
|
2017-01-25 16:47:40 +08:00
|
|
|
bool isSExt = false;
|
2016-12-19 19:26:31 +08:00
|
|
|
|
|
|
|
using namespace TargetOpcode;
|
|
|
|
switch (I.getOpcode()) {
|
2017-01-25 16:10:40 +08:00
|
|
|
case G_SEXT:
|
2017-01-25 16:47:40 +08:00
|
|
|
isSExt = true;
|
|
|
|
LLVM_FALLTHROUGH;
|
2017-01-25 16:10:40 +08:00
|
|
|
case G_ZEXT: {
|
|
|
|
LLT DstTy = MRI.getType(I.getOperand(0).getReg());
|
|
|
|
// FIXME: Smaller destination sizes coming soon!
|
|
|
|
if (DstTy.getSizeInBits() != 32) {
|
|
|
|
DEBUG(dbgs() << "Unsupported destination size for extension");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
|
|
|
|
unsigned SrcSize = SrcTy.getSizeInBits();
|
|
|
|
switch (SrcSize) {
|
2017-01-25 16:47:40 +08:00
|
|
|
case 1: {
|
|
|
|
// ZExt boils down to & 0x1; for SExt we also subtract that from 0
|
|
|
|
I.setDesc(TII.get(ARM::ANDri));
|
|
|
|
MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp());
|
|
|
|
|
|
|
|
if (isSExt) {
|
|
|
|
unsigned SExtResult = I.getOperand(0).getReg();
|
|
|
|
|
|
|
|
// Use a new virtual register for the result of the AND
|
|
|
|
unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass);
|
|
|
|
I.getOperand(0).setReg(AndResult);
|
|
|
|
|
|
|
|
auto InsertBefore = std::next(I.getIterator());
|
2017-01-25 22:28:19 +08:00
|
|
|
auto SubI =
|
2017-01-25 16:47:40 +08:00
|
|
|
BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri))
|
|
|
|
.addDef(SExtResult)
|
|
|
|
.addUse(AndResult)
|
|
|
|
.addImm(0)
|
|
|
|
.add(predOps(ARMCC::AL))
|
|
|
|
.add(condCodeOp());
|
|
|
|
if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-01-25 16:10:40 +08:00
|
|
|
case 8:
|
|
|
|
case 16: {
|
|
|
|
unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize);
|
2017-02-17 21:44:19 +08:00
|
|
|
if (NewOpc == I.getOpcode())
|
|
|
|
return false;
|
2017-01-25 16:10:40 +08:00
|
|
|
I.setDesc(TII.get(NewOpc));
|
|
|
|
MIB.addImm(0).add(predOps(ARMCC::AL));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
DEBUG(dbgs() << "Unsupported source size for extension");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-05-11 16:28:31 +08:00
|
|
|
case G_ANYEXT:
|
2017-04-21 21:16:50 +08:00
|
|
|
case G_TRUNC: {
|
|
|
|
// The high bits are undefined, so there's nothing special to do, just
|
|
|
|
// treat it as a copy.
|
|
|
|
auto SrcReg = I.getOperand(1).getReg();
|
|
|
|
auto DstReg = I.getOperand(0).getReg();
|
|
|
|
|
|
|
|
const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
|
|
|
|
const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
|
|
|
|
|
|
|
|
if (SrcRegBank.getID() != DstRegBank.getID()) {
|
2017-05-11 16:28:31 +08:00
|
|
|
DEBUG(dbgs() << "G_TRUNC/G_ANYEXT operands on different register banks\n");
|
2017-04-21 21:16:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SrcRegBank.getID() != ARM::GPRRegBankID) {
|
2017-05-11 16:28:31 +08:00
|
|
|
DEBUG(dbgs() << "G_TRUNC/G_ANYEXT on non-GPR not supported yet\n");
|
2017-04-21 21:16:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
I.setDesc(TII.get(COPY));
|
|
|
|
return selectCopy(I, TII, MRI, TRI, RBI);
|
|
|
|
}
|
2017-06-19 17:40:51 +08:00
|
|
|
case G_ICMP:
|
|
|
|
return selectICmp(MIB, TII, MRI, TRI, RBI);
|
2017-06-27 17:19:51 +08:00
|
|
|
case G_SELECT:
|
|
|
|
return selectSelect(MIB, TII, MRI, TRI, RBI);
|
2017-02-28 18:14:38 +08:00
|
|
|
case G_GEP:
|
2016-12-16 20:54:46 +08:00
|
|
|
I.setDesc(TII.get(ARM::ADDrr));
|
2017-01-13 18:18:01 +08:00
|
|
|
MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
|
2016-12-19 19:26:31 +08:00
|
|
|
break;
|
|
|
|
case G_FRAME_INDEX:
|
|
|
|
// Add 0 to the given frame index and hope it will eventually be folded into
|
|
|
|
// the user(s).
|
|
|
|
I.setDesc(TII.get(ARM::ADDri));
|
2017-01-13 18:18:01 +08:00
|
|
|
MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp());
|
2016-12-19 19:26:31 +08:00
|
|
|
break;
|
2017-02-28 21:05:42 +08:00
|
|
|
case G_CONSTANT: {
|
|
|
|
unsigned Reg = I.getOperand(0).getReg();
|
|
|
|
if (MRI.getType(Reg).getSizeInBits() != 32)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
assert(RBI.getRegBank(Reg, MRI, TRI)->getID() == ARM::GPRRegBankID &&
|
|
|
|
"Expected constant to live in a GPR");
|
|
|
|
I.setDesc(TII.get(ARM::MOVi));
|
|
|
|
MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
|
2017-04-24 14:30:56 +08:00
|
|
|
|
|
|
|
auto &Val = I.getOperand(1);
|
|
|
|
if (Val.isCImm()) {
|
|
|
|
if (Val.getCImm()->getBitWidth() > 32)
|
|
|
|
return false;
|
|
|
|
Val.ChangeToImmediate(Val.getCImm()->getZExtValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Val.isImm()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-28 21:05:42 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-02-24 22:01:27 +08:00
|
|
|
case G_STORE:
|
2017-01-26 17:20:47 +08:00
|
|
|
case G_LOAD: {
|
2017-02-20 22:45:58 +08:00
|
|
|
const auto &MemOp = **I.memoperands_begin();
|
|
|
|
if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
|
|
|
|
DEBUG(dbgs() << "Atomic load/store not supported yet\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-16 22:10:50 +08:00
|
|
|
unsigned Reg = I.getOperand(0).getReg();
|
|
|
|
unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID();
|
|
|
|
|
|
|
|
LLT ValTy = MRI.getType(Reg);
|
2017-01-26 17:20:47 +08:00
|
|
|
const auto ValSize = ValTy.getSizeInBits();
|
|
|
|
|
2017-02-16 22:10:50 +08:00
|
|
|
assert((ValSize != 64 || TII.getSubtarget().hasVFP2()) &&
|
2017-02-24 22:01:27 +08:00
|
|
|
"Don't know how to load/store 64-bit value without VFP");
|
2017-02-16 22:10:50 +08:00
|
|
|
|
2017-02-24 22:01:27 +08:00
|
|
|
const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize);
|
|
|
|
if (NewOpc == G_LOAD || NewOpc == G_STORE)
|
2017-02-17 21:44:19 +08:00
|
|
|
return false;
|
|
|
|
|
2017-01-26 17:20:47 +08:00
|
|
|
I.setDesc(TII.get(NewOpc));
|
|
|
|
|
2017-02-24 22:01:27 +08:00
|
|
|
if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH)
|
2017-01-26 17:20:47 +08:00
|
|
|
// LDRH has a funny addressing mode (there's already a FIXME for it).
|
|
|
|
MIB.addReg(0);
|
2017-01-13 17:37:56 +08:00
|
|
|
MIB.addImm(0).add(predOps(ARMCC::AL));
|
2016-12-19 19:26:31 +08:00
|
|
|
break;
|
2017-01-26 17:20:47 +08:00
|
|
|
}
|
2017-06-07 20:35:05 +08:00
|
|
|
case G_MERGE_VALUES: {
|
|
|
|
if (!selectMergeValues(MIB, TII, MRI, TRI, RBI))
|
2017-02-16 20:19:57 +08:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
2017-06-07 20:35:05 +08:00
|
|
|
case G_UNMERGE_VALUES: {
|
|
|
|
if (!selectUnmergeValues(MIB, TII, MRI, TRI, RBI))
|
2017-02-16 20:19:57 +08:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
2016-12-19 19:26:31 +08:00
|
|
|
default:
|
|
|
|
return false;
|
2016-12-16 20:54:46 +08:00
|
|
|
}
|
|
|
|
|
2016-12-19 19:26:31 +08:00
|
|
|
return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
|
2016-11-11 16:27:37 +08:00
|
|
|
}
|