#include "ARM.h"
#include "ARMCallLowering.h"
#include "ARMLegalizerInfo.h"
#include "ARMRegisterBankInfo.h"
#include "ARMFrameLowering.h"
#include "ARMInstrInfo.h"
#include "ARMSubtarget.h"
#include "ARMTargetMachine.h"
#include "MCTargetDesc/ARMMCTargetDesc.h"
#include "Thumb1FrameLowering.h"
#include "Thumb1InstrInfo.h"
#include "Thumb2InstrInfo.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Triple.h"
#include "llvm/ADT/Twine.h"
#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCTargetOptions.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ARMTargetParser.h"
#include "llvm/Support/TargetParser.h"
#include "llvm/Target/TargetOptions.h"
using namespace llvm;
#define DEBUG_TYPE "arm-subtarget"
#define GET_SUBTARGETINFO_TARGET_DESC
#define GET_SUBTARGETINFO_CTOR
#include "ARMGenSubtargetInfo.inc"
static cl::opt<bool>
UseFusedMulOps("arm-use-mulops",
cl::init(true), cl::Hidden);
enum ITMode {
DefaultIT,
RestrictedIT
};
static cl::opt<ITMode>
IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT),
cl::values(clEnumValN(DefaultIT, "arm-default-it",
"Generate any type of IT block"),
clEnumValN(RestrictedIT, "arm-restrict-it",
"Disallow complex IT blocks")));
static cl::opt<bool>
ForceFastISel("arm-force-fast-isel",
cl::init(false), cl::Hidden);
static cl::opt<bool> EnableSubRegLiveness("arm-enable-subreg-liveness",
cl::init(false), cl::Hidden);
ARMSubtarget &ARMSubtarget::initializeSubtargetDependencies(StringRef CPU,
StringRef FS) {
initializeEnvironment();
initSubtargetFeatures(CPU, FS);
return *this;
}
ARMFrameLowering *ARMSubtarget::initializeFrameLowering(StringRef CPU,
StringRef FS) {
ARMSubtarget &STI = initializeSubtargetDependencies(CPU, FS);
if (STI.isThumb1Only())
return (ARMFrameLowering *)new Thumb1FrameLowering(STI);
return new ARMFrameLowering(STI);
}
ARMSubtarget::ARMSubtarget(const Triple &TT, const std::string &CPU,
const std::string &FS,
const ARMBaseTargetMachine &TM, bool IsLittle,
bool MinSize)
: ARMGenSubtargetInfo(TT, CPU, CPU, FS),
UseMulOps(UseFusedMulOps), CPUString(CPU), OptMinSize(MinSize),
IsLittle(IsLittle), TargetTriple(TT), Options(TM.Options), TM(TM),
FrameLowering(initializeFrameLowering(CPU, FS)),
InstrInfo(isThumb1Only()
? (ARMBaseInstrInfo *)new Thumb1InstrInfo(*this)
: !isThumb()
? (ARMBaseInstrInfo *)new ARMInstrInfo(*this)
: (ARMBaseInstrInfo *)new Thumb2InstrInfo(*this)),
TLInfo(TM, *this) {
CallLoweringInfo.reset(new ARMCallLowering(*getTargetLowering()));
Legalizer.reset(new ARMLegalizerInfo(*this));
auto *RBI = new ARMRegisterBankInfo(*getRegisterInfo());
InstSelector.reset(createARMInstructionSelector(
*static_cast<const ARMBaseTargetMachine *>(&TM), *this, *RBI));
RegBankInfo.reset(RBI);
}
const CallLowering *ARMSubtarget::getCallLowering() const {
return CallLoweringInfo.get();
}
InstructionSelector *ARMSubtarget::getInstructionSelector() const {
return InstSelector.get();
}
const LegalizerInfo *ARMSubtarget::getLegalizerInfo() const {
return Legalizer.get();
}
const RegisterBankInfo *ARMSubtarget::getRegBankInfo() const {
return RegBankInfo.get();
}
bool ARMSubtarget::isXRaySupported() const {
return hasV6Ops() && hasARMOps() && !isTargetWindows();
}
void ARMSubtarget::initializeEnvironment() {
UseSjLjEH = (isTargetDarwin() && !isTargetWatchABI() &&
Options.ExceptionModel == ExceptionHandling::None) ||
Options.ExceptionModel == ExceptionHandling::SjLj;
assert((!TM.getMCAsmInfo() ||
(TM.getMCAsmInfo()->getExceptionHandlingType() ==
ExceptionHandling::SjLj) == UseSjLjEH) &&
"inconsistent sjlj choice between CodeGen and MC");
}
void ARMSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
if (CPUString.empty()) {
CPUString = "generic";
if (isTargetDarwin()) {
StringRef ArchName = TargetTriple.getArchName();
ARM::ArchKind AK = ARM::parseArch(ArchName);
if (AK == ARM::ArchKind::ARMV7S)
CPUString = "swift";
else if (AK == ARM::ArchKind::ARMV7K)
CPUString = "cortex-a7";
}
}
std::string ArchFS = ARM_MC::ParseARMTriple(TargetTriple, CPUString);
if (!FS.empty()) {
if (!ArchFS.empty())
ArchFS = (Twine(ArchFS) + "," + FS).str();
else
ArchFS = std::string(FS);
}
ParseSubtargetFeatures(CPUString, CPUString, ArchFS);
assert(hasV6T2Ops() || !hasThumb2());
if (genExecuteOnly()) {
NoMovt = false;
assert(hasV8MBaselineOps() && "Cannot generate execute-only code for this target");
}
SchedModel = getSchedModelForCPU(CPUString);
InstrItins = getInstrItineraryForCPU(CPUString);
if (isTargetWindows())
NoARM = true;
if (isAAPCS_ABI())
stackAlignment = Align(8);
if (isTargetNaCl() || isAAPCS16_ABI())
stackAlignment = Align(16);
SupportsTailCall = !isThumb1Only() || hasV8MBaselineOps();
if (isTargetMachO() && isTargetIOS() && getTargetTriple().isOSVersionLT(5, 0))
SupportsTailCall = false;
switch (IT) {
case DefaultIT:
RestrictIT = false;
break;
case RestrictedIT:
RestrictIT = true;
break;
}
const FeatureBitset &Bits = getFeatureBits();
if ((Bits[ARM::ProcA5] || Bits[ARM::ProcA8]) && (Options.UnsafeFPMath || isTargetDarwin()))
HasNEONForFP = true;
if (isRWPI())
ReserveR9 = true;
if (MVEVectorCostFactor == 0)
MVEVectorCostFactor = 2;
switch (ARMProcFamily) {
case Others:
case CortexA5:
break;
case CortexA7:
LdStMultipleTiming = DoubleIssue;
break;
case CortexA8:
LdStMultipleTiming = DoubleIssue;
break;
case CortexA9:
LdStMultipleTiming = DoubleIssueCheckUnalignedAccess;
PreISelOperandLatencyAdjustment = 1;
break;
case CortexA12:
break;
case CortexA15:
MaxInterleaveFactor = 2;
PreISelOperandLatencyAdjustment = 1;
PartialUpdateClearance = 12;
break;
case CortexA17:
case CortexA32:
case CortexA35:
case CortexA53:
case CortexA55:
case CortexA57:
case CortexA72:
case CortexA73:
case CortexA75:
case CortexA76:
case CortexA77:
case CortexA78:
case CortexA78C:
case CortexA710:
case CortexR4:
case CortexR4F:
case CortexR5:
case CortexR7:
case CortexM3:
case CortexM7:
case CortexR52:
case CortexX1:
case CortexX1C:
break;
case Exynos:
LdStMultipleTiming = SingleIssuePlusExtras;
MaxInterleaveFactor = 4;
if (!isThumb())
PrefLoopLogAlignment = 3;
break;
case Kryo:
break;
case Krait:
PreISelOperandLatencyAdjustment = 1;
break;
case NeoverseN1:
case NeoverseN2:
case NeoverseV1:
break;
case Swift:
MaxInterleaveFactor = 2;
LdStMultipleTiming = SingleIssuePlusExtras;
PreISelOperandLatencyAdjustment = 1;
PartialUpdateClearance = 12;
break;
}
}
bool ARMSubtarget::isTargetHardFloat() const { return TM.isTargetHardFloat(); }
bool ARMSubtarget::isAPCS_ABI() const {
assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_APCS;
}
bool ARMSubtarget::isAAPCS_ABI() const {
assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS ||
TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16;
}
bool ARMSubtarget::isAAPCS16_ABI() const {
assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16;
}
bool ARMSubtarget::isROPI() const {
return TM.getRelocationModel() == Reloc::ROPI ||
TM.getRelocationModel() == Reloc::ROPI_RWPI;
}
bool ARMSubtarget::isRWPI() const {
return TM.getRelocationModel() == Reloc::RWPI ||
TM.getRelocationModel() == Reloc::ROPI_RWPI;
}
bool ARMSubtarget::isGVIndirectSymbol(const GlobalValue *GV) const {
if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
return true;
if (isTargetMachO() && TM.isPositionIndependent() &&
(GV->isDeclarationForLinker() || GV->hasCommonLinkage()))
return true;
return false;
}
bool ARMSubtarget::isGVInGOT(const GlobalValue *GV) const {
return isTargetELF() && TM.isPositionIndependent() &&
!TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
}
unsigned ARMSubtarget::getMispredictionPenalty() const {
return SchedModel.MispredictPenalty;
}
bool ARMSubtarget::enableMachineScheduler() const {
if (isMClass() && hasMinSize())
return false;
return useMachineScheduler();
}
bool ARMSubtarget::enableSubRegLiveness() const {
if (EnableSubRegLiveness.getNumOccurrences())
return EnableSubRegLiveness;
return hasMVEIntegerOps();
}
bool ARMSubtarget::enableMachinePipeliner() const {
return getSchedModel().hasInstrSchedModel() && useMachinePipeliner();
}
bool ARMSubtarget::useDFAforSMS() const { return false; }
bool ARMSubtarget::enablePostRAScheduler() const {
if (enableMachineScheduler())
return false;
if (disablePostRAScheduler())
return false;
return !isThumb1Only();
}
bool ARMSubtarget::enablePostRAMachineScheduler() const {
if (!enableMachineScheduler())
return false;
if (disablePostRAScheduler())
return false;
return !isThumb1Only();
}
bool ARMSubtarget::useStride4VFPs() const {
return isTargetWatchABI() ||
(useWideStrideVFP() && !OptMinSize);
}
bool ARMSubtarget::useMovt() const {
return !NoMovt && hasV8MBaselineOps() &&
(isTargetWindows() || !OptMinSize || genExecuteOnly());
}
bool ARMSubtarget::useFastISel() const {
if (ForceFastISel)
return true;
if (!hasV6Ops())
return false;
return TM.Options.EnableFastISel &&
((isTargetMachO() && !isThumb1Only()) ||
(isTargetLinux() && !isThumb()) || (isTargetNaCl() && !isThumb()));
}
unsigned ARMSubtarget::getGPRAllocationOrder(const MachineFunction &MF) const {
if (isThumb1Only())
return 2;
if (isThumb2() && MF.getFunction().hasMinSize())
return 3;
return 1;
}
bool ARMSubtarget::ignoreCSRForAllocationOrder(const MachineFunction &MF,
unsigned PhysReg) const {
return isThumb2() && MF.getFunction().hasMinSize() &&
ARM::GPRRegClass.contains(PhysReg);
}
bool ARMSubtarget::splitFramePointerPush(const MachineFunction &MF) const {
const Function &F = MF.getFunction();
if (!MF.getTarget().getMCAsmInfo()->usesWindowsCFI() ||
!F.needsUnwindTableEntry())
return false;
const MachineFrameInfo &MFI = MF.getFrameInfo();
return MFI.hasVarSizedObjects() || getRegisterInfo()->hasStackRealignment(MF);
}