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 "ARMInstructionSelector.h"
|
|
|
|
#include "ARMRegisterBankInfo.h"
|
|
|
|
#include "ARMSubtarget.h"
|
|
|
|
#include "ARMTargetMachine.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"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#ifndef LLVM_BUILD_GLOBAL_ISEL
|
|
|
|
#error "You shouldn't build this"
|
|
|
|
#endif
|
|
|
|
|
2016-11-16 00:42:10 +08:00
|
|
|
ARMInstructionSelector::ARMInstructionSelector(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()),
|
2016-12-16 20:54:46 +08:00
|
|
|
TRI(*STI.getRegisterInfo()), RBI(RBI) {}
|
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();
|
2016-12-19 22:24:22 +08:00
|
|
|
(void)DstSize;
|
2016-12-19 22:07:50 +08:00
|
|
|
unsigned SrcReg = I.getOperand(1).getReg();
|
|
|
|
const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
|
|
|
|
(void)SrcSize;
|
|
|
|
assert((DstSize == SrcSize ||
|
|
|
|
// Copies are a means to setup initial types, the number of
|
|
|
|
// bits may not exactly match.
|
|
|
|
(TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
|
|
|
|
DstSize <= SrcSize)) &&
|
2016-12-16 21:13:03 +08:00
|
|
|
"Copy with different width?!");
|
2016-12-16 20:54:46 +08:00
|
|
|
|
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-02-16 20:19:52 +08:00
|
|
|
static bool selectFAdd(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII,
|
|
|
|
MachineRegisterInfo &MRI) {
|
|
|
|
assert(TII.getSubtarget().hasVFP2() && "Can't select fp add without vfp");
|
|
|
|
|
|
|
|
LLT Ty = MRI.getType(MIB->getOperand(0).getReg());
|
|
|
|
unsigned ValSize = Ty.getSizeInBits();
|
|
|
|
|
|
|
|
if (ValSize == 32) {
|
|
|
|
if (TII.getSubtarget().useNEONForSinglePrecisionFP())
|
|
|
|
return false;
|
|
|
|
MIB->setDesc(TII.get(ARM::VADDS));
|
|
|
|
} else {
|
|
|
|
assert(ValSize == 64 && "Unsupported size for floating point value");
|
|
|
|
if (TII.getSubtarget().isFPOnlySP())
|
|
|
|
return false;
|
|
|
|
MIB->setDesc(TII.get(ARM::VADDD));
|
|
|
|
}
|
|
|
|
MIB.add(predOps(ARMCC::AL));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-16 20:19:57 +08:00
|
|
|
static bool selectSequence(MachineInstrBuilder &MIB,
|
|
|
|
const ARMBaseInstrInfo &TII,
|
|
|
|
MachineRegisterInfo &MRI,
|
|
|
|
const TargetRegisterInfo &TRI,
|
|
|
|
const RegisterBankInfo &RBI) {
|
|
|
|
assert(TII.getSubtarget().hasVFP2() && "Can't select sequence without VFP");
|
|
|
|
|
|
|
|
// We only support G_SEQUENCE as a way to stick together two scalar GPRs
|
|
|
|
// 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 &&
|
|
|
|
"Unsupported operand for G_SEQUENCE");
|
|
|
|
unsigned VReg1 = MIB->getOperand(1).getReg();
|
|
|
|
(void)VReg1;
|
|
|
|
assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
|
|
|
|
RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
|
|
|
|
"Unsupported operand for G_SEQUENCE");
|
|
|
|
unsigned VReg2 = MIB->getOperand(3).getReg();
|
|
|
|
(void)VReg2;
|
|
|
|
assert(MRI.getType(VReg2).getSizeInBits() == 32 &&
|
|
|
|
RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID &&
|
|
|
|
"Unsupported operand for G_SEQUENCE");
|
|
|
|
|
|
|
|
// Remove the operands corresponding to the offsets.
|
|
|
|
MIB->RemoveOperand(4);
|
|
|
|
MIB->RemoveOperand(2);
|
|
|
|
|
|
|
|
MIB->setDesc(TII.get(ARM::VMOVDRR));
|
|
|
|
MIB.add(predOps(ARMCC::AL));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool selectExtract(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII,
|
|
|
|
MachineRegisterInfo &MRI,
|
|
|
|
const TargetRegisterInfo &TRI,
|
|
|
|
const RegisterBankInfo &RBI) {
|
|
|
|
assert(TII.getSubtarget().hasVFP2() && "Can't select extract without VFP");
|
|
|
|
|
|
|
|
// We only support G_EXTRACT as a way to break up one DPR into two GPRs.
|
|
|
|
unsigned VReg0 = MIB->getOperand(0).getReg();
|
|
|
|
(void)VReg0;
|
|
|
|
assert(MRI.getType(VReg0).getSizeInBits() == 32 &&
|
|
|
|
RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID &&
|
|
|
|
"Unsupported operand for G_SEQUENCE");
|
|
|
|
unsigned VReg1 = MIB->getOperand(1).getReg();
|
|
|
|
(void)VReg1;
|
|
|
|
assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
|
|
|
|
RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
|
|
|
|
"Unsupported operand for G_SEQUENCE");
|
|
|
|
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_SEQUENCE");
|
|
|
|
|
|
|
|
// Remove the operands corresponding to the offsets.
|
|
|
|
MIB->RemoveOperand(4);
|
|
|
|
MIB->RemoveOperand(3);
|
|
|
|
|
|
|
|
MIB->setDesc(TII.get(ARM::VMOVRRD));
|
|
|
|
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
|
|
|
|
/// invoke this.
|
|
|
|
static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) {
|
|
|
|
using namespace TargetOpcode;
|
|
|
|
|
|
|
|
assert((Size == 8 || Size == 16) && "Unsupported size");
|
|
|
|
|
|
|
|
if (Opc == G_SEXT)
|
|
|
|
return Size == 8 ? ARM::SXTB : ARM::SXTH;
|
|
|
|
|
|
|
|
if (Opc == G_ZEXT)
|
|
|
|
return Size == 8 ? ARM::UXTB : ARM::UXTH;
|
|
|
|
|
|
|
|
llvm_unreachable("Unsupported opcode");
|
|
|
|
}
|
|
|
|
|
2017-01-26 17:20:47 +08:00
|
|
|
/// Select the opcode for simple loads. For types smaller than 32 bits, the
|
|
|
|
/// value will be zero extended.
|
2017-02-16 22:10:50 +08:00
|
|
|
static unsigned selectLoadOpCode(unsigned RegBank, unsigned Size) {
|
|
|
|
if (RegBank == ARM::GPRRegBankID) {
|
|
|
|
switch (Size) {
|
|
|
|
case 1:
|
|
|
|
case 8:
|
|
|
|
return ARM::LDRBi12;
|
|
|
|
case 16:
|
|
|
|
return ARM::LDRH;
|
|
|
|
case 32:
|
|
|
|
return ARM::LDRi12;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unsupported size");
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(RegBank == ARM::FPRRegBankID && "Unsupported register bank");
|
2017-01-26 17:20:47 +08:00
|
|
|
switch (Size) {
|
|
|
|
case 32:
|
2017-02-16 22:10:50 +08:00
|
|
|
return ARM::VLDRS;
|
|
|
|
case 64:
|
|
|
|
return ARM::VLDRD;
|
2017-01-26 17:20:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unsupported size");
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
I.setDesc(TII.get(NewOpc));
|
|
|
|
MIB.addImm(0).add(predOps(ARMCC::AL));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
DEBUG(dbgs() << "Unsupported source size for extension");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-12-19 19:26:31 +08:00
|
|
|
case G_ADD:
|
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;
|
2017-02-08 21:23:04 +08:00
|
|
|
case G_FADD:
|
2017-02-16 20:19:52 +08:00
|
|
|
if (!selectFAdd(MIB, TII, MRI))
|
2017-02-08 21:23:04 +08:00
|
|
|
return false;
|
|
|
|
break;
|
2016-12-19 19:26:31 +08:00
|
|
|
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-01-26 17:20:47 +08:00
|
|
|
case G_LOAD: {
|
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
|
|
|
if (ValSize != 64 && ValSize != 32 && ValSize != 16 && ValSize != 8 &&
|
|
|
|
ValSize != 1)
|
2017-01-26 17:20:47 +08:00
|
|
|
return false;
|
|
|
|
|
2017-02-16 22:10:50 +08:00
|
|
|
assert((ValSize != 64 || RegBank == ARM::FPRRegBankID) &&
|
|
|
|
"64-bit values should live in the FPR");
|
|
|
|
assert((ValSize != 64 || TII.getSubtarget().hasVFP2()) &&
|
|
|
|
"Don't know how to load 64-bit value without VFP");
|
|
|
|
|
|
|
|
const auto NewOpc = selectLoadOpCode(RegBank, ValSize);
|
2017-01-26 17:20:47 +08:00
|
|
|
I.setDesc(TII.get(NewOpc));
|
|
|
|
|
|
|
|
if (NewOpc == ARM::LDRH)
|
|
|
|
// 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-02-16 20:19:57 +08:00
|
|
|
case G_SEQUENCE: {
|
|
|
|
if (!selectSequence(MIB, TII, MRI, TRI, RBI))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case G_EXTRACT: {
|
|
|
|
if (!selectExtract(MIB, TII, MRI, TRI, RBI))
|
|
|
|
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
|
|
|
}
|