1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
//===- PPCRegisterBankInfo.cpp --------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
/// \file
/// This file implements the targeting of the RegisterBankInfo class for
/// PowerPC.
//===----------------------------------------------------------------------===//
#include "PPCRegisterBankInfo.h"
#include "PPCRegisterInfo.h"
#include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h"
#include "llvm/CodeGen/GlobalISel/Utils.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/Support/Debug.h"
#define DEBUG_TYPE "ppc-reg-bank-info"
#define GET_TARGET_REGBANK_IMPL
#include "PPCGenRegisterBank.inc"
// This file will be TableGen'ed at some point.
#include "PPCGenRegisterBankInfo.def"
using namespace llvm;
PPCRegisterBankInfo::PPCRegisterBankInfo(const TargetRegisterInfo &TRI) {}
const RegisterBank &
PPCRegisterBankInfo::getRegBankFromRegClass(const TargetRegisterClass &RC,
LLT Ty) const {
switch (RC.getID()) {
case PPC::G8RCRegClassID:
case PPC::G8RC_NOX0RegClassID:
case PPC::G8RC_and_G8RC_NOX0RegClassID:
case PPC::GPRCRegClassID:
case PPC::GPRC_NOR0RegClassID:
case PPC::GPRC_and_GPRC_NOR0RegClassID:
return getRegBank(PPC::GPRRegBankID);
case PPC::VSFRCRegClassID:
case PPC::SPILLTOVSRRC_and_VSFRCRegClassID:
case PPC::SPILLTOVSRRC_and_VFRCRegClassID:
case PPC::SPILLTOVSRRC_and_F4RCRegClassID:
case PPC::F8RCRegClassID:
case PPC::VFRCRegClassID:
case PPC::VSSRCRegClassID:
case PPC::F4RCRegClassID:
return getRegBank(PPC::FPRRegBankID);
case PPC::VSRCRegClassID:
case PPC::VRRCRegClassID:
case PPC::VRRC_with_sub_64_in_SPILLTOVSRRCRegClassID:
case PPC::VSRC_with_sub_64_in_SPILLTOVSRRCRegClassID:
case PPC::SPILLTOVSRRCRegClassID:
case PPC::VSLRCRegClassID:
case PPC::VSLRC_with_sub_64_in_SPILLTOVSRRCRegClassID:
return getRegBank(PPC::VECRegBankID);
case PPC::CRRCRegClassID:
case PPC::CRBITRCRegClassID:
return getRegBank(PPC::CRRegBankID);
default:
llvm_unreachable("Unexpected register class");
}
}
const RegisterBankInfo::InstructionMapping &
PPCRegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
const unsigned Opc = MI.getOpcode();
// Try the default logic for non-generic instructions that are either copies
// or already have some operands assigned to banks.
if (!isPreISelGenericOpcode(Opc) || Opc == TargetOpcode::G_PHI) {
const RegisterBankInfo::InstructionMapping &Mapping =
getInstrMappingImpl(MI);
if (Mapping.isValid())
return Mapping;
}
const MachineFunction &MF = *MI.getParent()->getParent();
const MachineRegisterInfo &MRI = MF.getRegInfo();
const TargetSubtargetInfo &STI = MF.getSubtarget();
const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
unsigned NumOperands = MI.getNumOperands();
const ValueMapping *OperandsMapping = nullptr;
unsigned Cost = 1;
unsigned MappingID = DefaultMappingID;
switch (Opc) {
// Arithmetic ops.
case TargetOpcode::G_ADD:
case TargetOpcode::G_SUB:
// Bitwise ops.
case TargetOpcode::G_AND:
case TargetOpcode::G_OR:
case TargetOpcode::G_XOR:
// Extension ops.
case TargetOpcode::G_SEXT:
case TargetOpcode::G_ZEXT:
case TargetOpcode::G_ANYEXT: {
assert(NumOperands <= 3 &&
"This code is for instructions with 3 or less operands");
LLT Ty = MRI.getType(MI.getOperand(0).getReg());
unsigned Size = Ty.getSizeInBits();
switch (Size) {
case 128:
OperandsMapping = getValueMapping(PMI_VEC128);
break;
default:
OperandsMapping = getValueMapping(PMI_GPR64);
break;
}
break;
}
case TargetOpcode::G_FADD:
case TargetOpcode::G_FSUB:
case TargetOpcode::G_FMUL:
case TargetOpcode::G_FDIV: {
Register SrcReg = MI.getOperand(1).getReg();
unsigned Size = getSizeInBits(SrcReg, MRI, TRI);
assert((Size == 32 || Size == 64 || Size == 128) &&
"Unsupported floating point types!\n");
switch (Size) {
case 32:
OperandsMapping = getValueMapping(PMI_FPR32);
break;
case 64:
OperandsMapping = getValueMapping(PMI_FPR64);
break;
case 128:
OperandsMapping = getValueMapping(PMI_VEC128);
break;
}
break;
}
case TargetOpcode::G_FCMP: {
unsigned CmpSize = MRI.getType(MI.getOperand(2).getReg()).getSizeInBits();
OperandsMapping = getOperandsMapping(
{getValueMapping(PMI_CR), nullptr,
getValueMapping(CmpSize == 32 ? PMI_FPR32 : PMI_FPR64),
getValueMapping(CmpSize == 32 ? PMI_FPR32 : PMI_FPR64)});
break;
}
case TargetOpcode::G_CONSTANT:
OperandsMapping = getOperandsMapping({getValueMapping(PMI_GPR64), nullptr});
break;
case TargetOpcode::G_CONSTANT_POOL:
OperandsMapping = getOperandsMapping({getValueMapping(PMI_GPR64), nullptr});
break;
case TargetOpcode::G_FPTOUI:
case TargetOpcode::G_FPTOSI: {
Register SrcReg = MI.getOperand(1).getReg();
unsigned Size = getSizeInBits(SrcReg, MRI, TRI);
OperandsMapping = getOperandsMapping(
{getValueMapping(PMI_GPR64),
getValueMapping(Size == 32 ? PMI_FPR32 : PMI_FPR64)});
break;
}
case TargetOpcode::G_UITOFP:
case TargetOpcode::G_SITOFP: {
Register SrcReg = MI.getOperand(0).getReg();
unsigned Size = getSizeInBits(SrcReg, MRI, TRI);
OperandsMapping =
getOperandsMapping({getValueMapping(Size == 32 ? PMI_FPR32 : PMI_FPR64),
getValueMapping(PMI_GPR64)});
break;
}
case TargetOpcode::G_LOAD: {
unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
// Check if that load feeds fp instructions.
if (any_of(MRI.use_nodbg_instructions(MI.getOperand(0).getReg()),
[&](const MachineInstr &UseMI) {
// If we have at least one direct use in a FP instruction,
// assume this was a floating point load in the IR. If it was
// not, we would have had a bitcast before reaching that
// instruction.
//
// Int->FP conversion operations are also captured in
// onlyDefinesFP().
return onlyUsesFP(UseMI, MRI, TRI);
}))
OperandsMapping = getOperandsMapping(
{getValueMapping(Size == 64 ? PMI_FPR64 : PMI_FPR32),
getValueMapping(PMI_GPR64)});
else
OperandsMapping = getOperandsMapping(
{getValueMapping(Size == 64 ? PMI_GPR64 : PMI_GPR32),
getValueMapping(PMI_GPR64)});
break;
}
case TargetOpcode::G_STORE: {
// Check if the store is fed by fp instructions.
MachineInstr *DefMI = MRI.getVRegDef(MI.getOperand(0).getReg());
unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
if (onlyDefinesFP(*DefMI, MRI, TRI))
OperandsMapping = getOperandsMapping(
{getValueMapping(Size == 64 ? PMI_FPR64 : PMI_FPR32),
getValueMapping(PMI_GPR64)});
else
OperandsMapping = getOperandsMapping(
{getValueMapping(Size == 64 ? PMI_GPR64 : PMI_GPR32),
getValueMapping(PMI_GPR64)});
break;
}
case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS: {
// FIXME: We have to check every operand in this MI and compute value
// mapping accordingly.
SmallVector<const ValueMapping *, 8> OpdsMapping(NumOperands);
OperandsMapping = getOperandsMapping(OpdsMapping);
break;
}
case TargetOpcode::G_BITCAST: {
LLT DstTy = MRI.getType(MI.getOperand(0).getReg());
LLT SrcTy = MRI.getType(MI.getOperand(1).getReg());
unsigned DstSize = DstTy.getSizeInBits();
bool DstIsGPR = !DstTy.isVector();
bool SrcIsGPR = !SrcTy.isVector();
// TODO: Currently, only vector and GPR register banks are handled.
// This needs to be extended to handle floating point register
// banks in the future.
const RegisterBank &DstRB = DstIsGPR ? PPC::GPRRegBank : PPC::VECRegBank;
const RegisterBank &SrcRB = SrcIsGPR ? PPC::GPRRegBank : PPC::VECRegBank;
return getInstructionMapping(
MappingID, Cost, getCopyMapping(DstRB.getID(), SrcRB.getID(), DstSize),
NumOperands);
}
default:
return getInvalidInstructionMapping();
}
return getInstructionMapping(MappingID, Cost, OperandsMapping, NumOperands);
}
/// \returns true if a given intrinsic \p ID only uses and defines FPRs.
static bool isFPIntrinsic(unsigned ID) {
// TODO: Add more intrinsics.
return false;
}
/// FIXME: this is copied from target AArch64. Needs some code refactor here to
/// put this function in class RegisterBankInfo.
bool PPCRegisterBankInfo::hasFPConstraints(const MachineInstr &MI,
const MachineRegisterInfo &MRI,
const TargetRegisterInfo &TRI,
unsigned Depth) const {
unsigned Op = MI.getOpcode();
if (auto *GI = dyn_cast<GIntrinsic>(&MI)) {
if (isFPIntrinsic(GI->getIntrinsicID()))
return true;
}
// Do we have an explicit floating point instruction?
if (isPreISelGenericFloatingPointOpcode(Op))
return true;
// No. Check if we have a copy-like instruction. If we do, then we could
// still be fed by floating point instructions.
if (Op != TargetOpcode::COPY && !MI.isPHI() &&
!isPreISelGenericOptimizationHint(Op))
return false;
// Check if we already know the register bank.
auto *RB = getRegBank(MI.getOperand(0).getReg(), MRI, TRI);
if (RB == &PPC::FPRRegBank)
return true;
if (RB == &PPC::GPRRegBank)
return false;
// We don't know anything.
//
// If we have a phi, we may be able to infer that it will be assigned a FPR
// based off of its inputs.
if (!MI.isPHI() || Depth > MaxFPRSearchDepth)
return false;
return any_of(MI.explicit_uses(), [&](const MachineOperand &Op) {
return Op.isReg() &&
onlyDefinesFP(*MRI.getVRegDef(Op.getReg()), MRI, TRI, Depth + 1);
});
}
/// FIXME: this is copied from target AArch64. Needs some code refactor here to
/// put this function in class RegisterBankInfo.
bool PPCRegisterBankInfo::onlyUsesFP(const MachineInstr &MI,
const MachineRegisterInfo &MRI,
const TargetRegisterInfo &TRI,
unsigned Depth) const {
switch (MI.getOpcode()) {
case TargetOpcode::G_FPTOSI:
case TargetOpcode::G_FPTOUI:
case TargetOpcode::G_FCMP:
case TargetOpcode::G_LROUND:
case TargetOpcode::G_LLROUND:
return true;
default:
break;
}
return hasFPConstraints(MI, MRI, TRI, Depth);
}
/// FIXME: this is copied from target AArch64. Needs some code refactor here to
/// put this function in class RegisterBankInfo.
bool PPCRegisterBankInfo::onlyDefinesFP(const MachineInstr &MI,
const MachineRegisterInfo &MRI,
const TargetRegisterInfo &TRI,
unsigned Depth) const {
switch (MI.getOpcode()) {
case TargetOpcode::G_SITOFP:
case TargetOpcode::G_UITOFP:
return true;
default:
break;
}
return hasFPConstraints(MI, MRI, TRI, Depth);
}
RegisterBankInfo::InstructionMappings
PPCRegisterBankInfo::getInstrAlternativeMappings(const MachineInstr &MI) const {
// TODO Implement.
return RegisterBankInfo::getInstrAlternativeMappings(MI);
}
|