2014-05-24 14:50:23 +02:00
|
|
|
//===-- AArch64AdvSIMDScalar.cpp - Replace dead defs w/ zero reg --===//
|
2014-03-29 11:18:08 +01:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// 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
|
2014-03-29 11:18:08 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// When profitable, replace GPR targeting i64 instructions with their
|
|
|
|
// AdvSIMD scalar equivalents. Generally speaking, "profitable" is defined
|
|
|
|
// as minimizing the number of cross-class register copies.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TODO: Graph based predicate heuristics.
|
|
|
|
// Walking the instruction list linearly will get many, perhaps most, of
|
2014-04-09 16:47:27 +02:00
|
|
|
// the cases, but to do a truly thorough job of this, we need a more
|
2014-03-29 11:18:08 +01:00
|
|
|
// wholistic approach.
|
|
|
|
//
|
|
|
|
// This optimization is very similar in spirit to the register allocator's
|
|
|
|
// spill placement, only here we're determining where to place cross-class
|
|
|
|
// register copies rather than spills. As such, a similar approach is
|
|
|
|
// called for.
|
|
|
|
//
|
|
|
|
// We want to build up a set of graphs of all instructions which are candidates
|
|
|
|
// for transformation along with instructions which generate their inputs and
|
|
|
|
// consume their outputs. For each edge in the graph, we assign a weight
|
|
|
|
// based on whether there is a copy required there (weight zero if not) and
|
|
|
|
// the block frequency of the block containing the defining or using
|
|
|
|
// instruction, whichever is less. Our optimization is then a graph problem
|
|
|
|
// to minimize the total weight of all the graphs, then transform instructions
|
|
|
|
// and add or remove copy instructions as called for to implement the
|
|
|
|
// solution.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-05-24 14:50:23 +02:00
|
|
|
#include "AArch64.h"
|
|
|
|
#include "AArch64InstrInfo.h"
|
|
|
|
#include "AArch64RegisterInfo.h"
|
2014-03-29 11:18:08 +01:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2014-07-25 13:42:14 +02:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2014-03-29 11:18:08 +01:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-05-24 14:50:23 +02:00
|
|
|
#define DEBUG_TYPE "aarch64-simd-scalar"
|
2014-04-22 04:41:26 +02:00
|
|
|
|
2014-03-29 11:18:08 +01:00
|
|
|
// Allow forcing all i64 operations with equivalent SIMD instructions to use
|
|
|
|
// them. For stress-testing the transformation function.
|
|
|
|
static cl::opt<bool>
|
2014-05-24 14:50:23 +02:00
|
|
|
TransformAll("aarch64-simd-scalar-force-all",
|
2014-03-29 11:18:08 +01:00
|
|
|
cl::desc("Force use of AdvSIMD scalar instructions everywhere"),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
|
|
|
STATISTIC(NumScalarInsnsUsed, "Number of scalar instructions used");
|
|
|
|
STATISTIC(NumCopiesDeleted, "Number of cross-class copies deleted");
|
|
|
|
STATISTIC(NumCopiesInserted, "Number of cross-class copies inserted");
|
|
|
|
|
2015-08-05 17:18:58 +02:00
|
|
|
#define AARCH64_ADVSIMD_NAME "AdvSIMD Scalar Operation Optimization"
|
|
|
|
|
2014-03-29 11:18:08 +01:00
|
|
|
namespace {
|
2014-05-24 14:50:23 +02:00
|
|
|
class AArch64AdvSIMDScalar : public MachineFunctionPass {
|
2014-03-29 11:18:08 +01:00
|
|
|
MachineRegisterInfo *MRI;
|
2015-01-30 02:10:18 +01:00
|
|
|
const TargetInstrInfo *TII;
|
2014-03-29 11:18:08 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
// isProfitableToTransform - Predicate function to determine whether an
|
|
|
|
// instruction should be transformed to its equivalent AdvSIMD scalar
|
|
|
|
// instruction. "add Xd, Xn, Xm" ==> "add Dd, Da, Db", for example.
|
2016-07-08 22:29:42 +02:00
|
|
|
bool isProfitableToTransform(const MachineInstr &MI) const;
|
2014-03-29 11:18:08 +01:00
|
|
|
|
2014-04-09 16:47:27 +02:00
|
|
|
// transformInstruction - Perform the transformation of an instruction
|
2014-03-29 11:18:08 +01:00
|
|
|
// to its equivalant AdvSIMD scalar instruction. Update inputs and outputs
|
|
|
|
// to be the correct register class, minimizing cross-class copies.
|
2016-07-08 22:29:42 +02:00
|
|
|
void transformInstruction(MachineInstr &MI);
|
2014-03-29 11:18:08 +01:00
|
|
|
|
|
|
|
// processMachineBasicBlock - Main optimzation loop.
|
|
|
|
bool processMachineBasicBlock(MachineBasicBlock *MBB);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid.
|
2015-08-05 17:18:58 +02:00
|
|
|
explicit AArch64AdvSIMDScalar() : MachineFunctionPass(ID) {
|
|
|
|
initializeAArch64AdvSIMDScalarPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2014-03-29 11:18:08 +01:00
|
|
|
|
2014-04-29 09:58:25 +02:00
|
|
|
bool runOnMachineFunction(MachineFunction &F) override;
|
2014-03-29 11:18:08 +01:00
|
|
|
|
2016-10-01 04:56:57 +02:00
|
|
|
StringRef getPassName() const override { return AARCH64_ADVSIMD_NAME; }
|
2014-03-29 11:18:08 +01:00
|
|
|
|
2014-04-29 09:58:25 +02:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-03-29 11:18:08 +01:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
};
|
2014-05-24 14:50:23 +02:00
|
|
|
char AArch64AdvSIMDScalar::ID = 0;
|
2014-03-29 11:18:08 +01:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2015-08-05 17:18:58 +02:00
|
|
|
INITIALIZE_PASS(AArch64AdvSIMDScalar, "aarch64-simd-scalar",
|
|
|
|
AARCH64_ADVSIMD_NAME, false, false)
|
|
|
|
|
2014-03-29 11:18:08 +01:00
|
|
|
static bool isGPR64(unsigned Reg, unsigned SubReg,
|
|
|
|
const MachineRegisterInfo *MRI) {
|
|
|
|
if (SubReg)
|
|
|
|
return false;
|
2019-08-02 01:27:28 +02:00
|
|
|
if (Register::isVirtualRegister(Reg))
|
2014-05-24 14:50:23 +02:00
|
|
|
return MRI->getRegClass(Reg)->hasSuperClassEq(&AArch64::GPR64RegClass);
|
|
|
|
return AArch64::GPR64RegClass.contains(Reg);
|
2014-03-29 11:18:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isFPR64(unsigned Reg, unsigned SubReg,
|
|
|
|
const MachineRegisterInfo *MRI) {
|
2019-08-02 01:27:28 +02:00
|
|
|
if (Register::isVirtualRegister(Reg))
|
2014-05-24 14:50:23 +02:00
|
|
|
return (MRI->getRegClass(Reg)->hasSuperClassEq(&AArch64::FPR64RegClass) &&
|
2014-03-29 11:18:08 +01:00
|
|
|
SubReg == 0) ||
|
2014-05-24 14:50:23 +02:00
|
|
|
(MRI->getRegClass(Reg)->hasSuperClassEq(&AArch64::FPR128RegClass) &&
|
|
|
|
SubReg == AArch64::dsub);
|
2014-04-28 18:21:50 +02:00
|
|
|
// Physical register references just check the register class directly.
|
2014-05-24 14:50:23 +02:00
|
|
|
return (AArch64::FPR64RegClass.contains(Reg) && SubReg == 0) ||
|
|
|
|
(AArch64::FPR128RegClass.contains(Reg) && SubReg == AArch64::dsub);
|
2014-03-29 11:18:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// getSrcFromCopy - Get the original source register for a GPR64 <--> FPR64
|
2020-08-20 14:36:20 +02:00
|
|
|
// copy instruction. Return nullptr if the instruction is not a copy.
|
2016-04-22 20:09:14 +02:00
|
|
|
static MachineOperand *getSrcFromCopy(MachineInstr *MI,
|
|
|
|
const MachineRegisterInfo *MRI,
|
|
|
|
unsigned &SubReg) {
|
2014-03-29 11:18:08 +01:00
|
|
|
SubReg = 0;
|
|
|
|
// The "FMOV Xd, Dn" instruction is the typical form.
|
2014-05-24 14:50:23 +02:00
|
|
|
if (MI->getOpcode() == AArch64::FMOVDXr ||
|
|
|
|
MI->getOpcode() == AArch64::FMOVXDr)
|
2016-04-22 20:09:14 +02:00
|
|
|
return &MI->getOperand(1);
|
2014-03-29 11:18:08 +01:00
|
|
|
// A lane zero extract "UMOV.d Xd, Vn[0]" is equivalent. We shouldn't see
|
|
|
|
// these at this stage, but it's easy to check for.
|
2014-05-24 14:50:23 +02:00
|
|
|
if (MI->getOpcode() == AArch64::UMOVvi64 && MI->getOperand(2).getImm() == 0) {
|
|
|
|
SubReg = AArch64::dsub;
|
2016-04-22 20:09:14 +02:00
|
|
|
return &MI->getOperand(1);
|
2014-03-29 11:18:08 +01:00
|
|
|
}
|
|
|
|
// Or just a plain COPY instruction. This can be directly to/from FPR64,
|
|
|
|
// or it can be a dsub subreg reference to an FPR128.
|
2014-05-24 14:50:23 +02:00
|
|
|
if (MI->getOpcode() == AArch64::COPY) {
|
2014-03-29 11:18:08 +01:00
|
|
|
if (isFPR64(MI->getOperand(0).getReg(), MI->getOperand(0).getSubReg(),
|
|
|
|
MRI) &&
|
|
|
|
isGPR64(MI->getOperand(1).getReg(), MI->getOperand(1).getSubReg(), MRI))
|
2016-04-22 20:09:14 +02:00
|
|
|
return &MI->getOperand(1);
|
2014-03-29 11:18:08 +01:00
|
|
|
if (isGPR64(MI->getOperand(0).getReg(), MI->getOperand(0).getSubReg(),
|
|
|
|
MRI) &&
|
|
|
|
isFPR64(MI->getOperand(1).getReg(), MI->getOperand(1).getSubReg(),
|
|
|
|
MRI)) {
|
2014-04-28 18:21:50 +02:00
|
|
|
SubReg = MI->getOperand(1).getSubReg();
|
2016-04-22 20:09:14 +02:00
|
|
|
return &MI->getOperand(1);
|
2014-03-29 11:18:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, this is some other kind of instruction.
|
2016-04-22 20:09:14 +02:00
|
|
|
return nullptr;
|
2014-03-29 11:18:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// getTransformOpcode - For any opcode for which there is an AdvSIMD equivalent
|
|
|
|
// that we're considering transforming to, return that AdvSIMD opcode. For all
|
|
|
|
// others, return the original opcode.
|
2015-05-18 22:27:55 +02:00
|
|
|
static unsigned getTransformOpcode(unsigned Opc) {
|
2014-03-29 11:18:08 +01:00
|
|
|
switch (Opc) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
// FIXME: Lots more possibilities.
|
2014-05-24 14:50:23 +02:00
|
|
|
case AArch64::ADDXrr:
|
|
|
|
return AArch64::ADDv1i64;
|
|
|
|
case AArch64::SUBXrr:
|
|
|
|
return AArch64::SUBv1i64;
|
2014-08-04 23:20:25 +02:00
|
|
|
case AArch64::ANDXrr:
|
|
|
|
return AArch64::ANDv8i8;
|
|
|
|
case AArch64::EORXrr:
|
|
|
|
return AArch64::EORv8i8;
|
|
|
|
case AArch64::ORRXrr:
|
|
|
|
return AArch64::ORRv8i8;
|
2014-03-29 11:18:08 +01:00
|
|
|
}
|
|
|
|
// No AdvSIMD equivalent, so just return the original opcode.
|
|
|
|
return Opc;
|
|
|
|
}
|
|
|
|
|
2016-07-08 22:29:42 +02:00
|
|
|
static bool isTransformable(const MachineInstr &MI) {
|
|
|
|
unsigned Opc = MI.getOpcode();
|
2014-03-29 11:18:08 +01:00
|
|
|
return Opc != getTransformOpcode(Opc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// isProfitableToTransform - Predicate function to determine whether an
|
|
|
|
// instruction should be transformed to its equivalent AdvSIMD scalar
|
|
|
|
// instruction. "add Xd, Xn, Xm" ==> "add Dd, Da, Db", for example.
|
2016-07-08 22:29:42 +02:00
|
|
|
bool AArch64AdvSIMDScalar::isProfitableToTransform(
|
|
|
|
const MachineInstr &MI) const {
|
2014-03-29 11:18:08 +01:00
|
|
|
// If this instruction isn't eligible to be transformed (no SIMD equivalent),
|
|
|
|
// early exit since that's the common case.
|
|
|
|
if (!isTransformable(MI))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Count the number of copies we'll need to add and approximate the number
|
|
|
|
// of copies that a transform will enable us to remove.
|
|
|
|
unsigned NumNewCopies = 3;
|
|
|
|
unsigned NumRemovableCopies = 0;
|
|
|
|
|
[aarch64] Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Manual fixups in:
AArch64InstrInfo.cpp - genFusedMultiply() now takes a Register* instead of unsigned*
AArch64LoadStoreOptimizer.cpp - Ternary operator was ambiguous between Register/MCRegister. Settled on Register
Depends on D65919
Reviewers: aemerson
Subscribers: jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision for full review was: https://reviews.llvm.org/D65962
llvm-svn: 368628
2019-08-13 00:40:53 +02:00
|
|
|
Register OrigSrc0 = MI.getOperand(1).getReg();
|
|
|
|
Register OrigSrc1 = MI.getOperand(2).getReg();
|
2016-04-22 20:09:14 +02:00
|
|
|
unsigned SubReg0;
|
|
|
|
unsigned SubReg1;
|
2014-03-29 11:18:08 +01:00
|
|
|
if (!MRI->def_empty(OrigSrc0)) {
|
|
|
|
MachineRegisterInfo::def_instr_iterator Def =
|
|
|
|
MRI->def_instr_begin(OrigSrc0);
|
|
|
|
assert(std::next(Def) == MRI->def_instr_end() && "Multiple def in SSA!");
|
2016-04-22 20:09:14 +02:00
|
|
|
MachineOperand *MOSrc0 = getSrcFromCopy(&*Def, MRI, SubReg0);
|
2014-03-29 11:18:08 +01:00
|
|
|
// If the source was from a copy, we don't need to insert a new copy.
|
2016-04-22 20:09:14 +02:00
|
|
|
if (MOSrc0)
|
2014-03-29 11:18:08 +01:00
|
|
|
--NumNewCopies;
|
|
|
|
// If there are no other users of the original source, we can delete
|
|
|
|
// that instruction.
|
2016-04-22 20:09:14 +02:00
|
|
|
if (MOSrc0 && MRI->hasOneNonDBGUse(OrigSrc0))
|
2014-03-29 11:18:08 +01:00
|
|
|
++NumRemovableCopies;
|
|
|
|
}
|
|
|
|
if (!MRI->def_empty(OrigSrc1)) {
|
|
|
|
MachineRegisterInfo::def_instr_iterator Def =
|
|
|
|
MRI->def_instr_begin(OrigSrc1);
|
|
|
|
assert(std::next(Def) == MRI->def_instr_end() && "Multiple def in SSA!");
|
2016-04-22 20:09:14 +02:00
|
|
|
MachineOperand *MOSrc1 = getSrcFromCopy(&*Def, MRI, SubReg1);
|
|
|
|
if (MOSrc1)
|
2014-03-29 11:18:08 +01:00
|
|
|
--NumNewCopies;
|
|
|
|
// If there are no other users of the original source, we can delete
|
|
|
|
// that instruction.
|
2016-04-22 20:09:14 +02:00
|
|
|
if (MOSrc1 && MRI->hasOneNonDBGUse(OrigSrc1))
|
2014-03-29 11:18:08 +01:00
|
|
|
++NumRemovableCopies;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any of the uses of the original instructions is a cross class copy,
|
|
|
|
// that's a copy that will be removable if we transform. Likewise, if
|
|
|
|
// any of the uses is a transformable instruction, it's likely the tranforms
|
|
|
|
// will chain, enabling us to save a copy there, too. This is an aggressive
|
|
|
|
// heuristic that approximates the graph based cost analysis described above.
|
[aarch64] Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Manual fixups in:
AArch64InstrInfo.cpp - genFusedMultiply() now takes a Register* instead of unsigned*
AArch64LoadStoreOptimizer.cpp - Ternary operator was ambiguous between Register/MCRegister. Settled on Register
Depends on D65919
Reviewers: aemerson
Subscribers: jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision for full review was: https://reviews.llvm.org/D65962
llvm-svn: 368628
2019-08-13 00:40:53 +02:00
|
|
|
Register Dst = MI.getOperand(0).getReg();
|
2014-03-29 11:18:08 +01:00
|
|
|
bool AllUsesAreCopies = true;
|
|
|
|
for (MachineRegisterInfo::use_instr_nodbg_iterator
|
|
|
|
Use = MRI->use_instr_nodbg_begin(Dst),
|
|
|
|
E = MRI->use_instr_nodbg_end();
|
|
|
|
Use != E; ++Use) {
|
|
|
|
unsigned SubReg;
|
2016-07-08 22:29:42 +02:00
|
|
|
if (getSrcFromCopy(&*Use, MRI, SubReg) || isTransformable(*Use))
|
2014-03-29 11:18:08 +01:00
|
|
|
++NumRemovableCopies;
|
|
|
|
// If the use is an INSERT_SUBREG, that's still something that can
|
|
|
|
// directly use the FPR64, so we don't invalidate AllUsesAreCopies. It's
|
|
|
|
// preferable to have it use the FPR64 in most cases, as if the source
|
|
|
|
// vector is an IMPLICIT_DEF, the INSERT_SUBREG just goes away entirely.
|
|
|
|
// Ditto for a lane insert.
|
2014-05-24 14:50:23 +02:00
|
|
|
else if (Use->getOpcode() == AArch64::INSERT_SUBREG ||
|
|
|
|
Use->getOpcode() == AArch64::INSvi64gpr)
|
2014-03-29 11:18:08 +01:00
|
|
|
;
|
|
|
|
else
|
|
|
|
AllUsesAreCopies = false;
|
|
|
|
}
|
|
|
|
// If all of the uses of the original destination register are copies to
|
|
|
|
// FPR64, then we won't end up having a new copy back to GPR64 either.
|
|
|
|
if (AllUsesAreCopies)
|
|
|
|
--NumNewCopies;
|
|
|
|
|
2014-04-09 16:47:27 +02:00
|
|
|
// If a transform will not increase the number of cross-class copies required,
|
2014-03-29 11:18:08 +01:00
|
|
|
// return true.
|
|
|
|
if (NumNewCopies <= NumRemovableCopies)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Finally, even if we otherwise wouldn't transform, check if we're forcing
|
|
|
|
// transformation of everything.
|
|
|
|
return TransformAll;
|
|
|
|
}
|
|
|
|
|
2016-07-08 22:29:42 +02:00
|
|
|
static MachineInstr *insertCopy(const TargetInstrInfo *TII, MachineInstr &MI,
|
2014-03-29 11:18:08 +01:00
|
|
|
unsigned Dst, unsigned Src, bool IsKill) {
|
2016-07-08 22:29:42 +02:00
|
|
|
MachineInstrBuilder MIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
|
|
|
|
TII->get(AArch64::COPY), Dst)
|
|
|
|
.addReg(Src, getKillRegState(IsKill));
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << " adding copy: " << *MIB);
|
2014-03-29 11:18:08 +01:00
|
|
|
++NumCopiesInserted;
|
|
|
|
return MIB;
|
|
|
|
}
|
|
|
|
|
2014-04-09 16:47:27 +02:00
|
|
|
// transformInstruction - Perform the transformation of an instruction
|
2014-03-29 11:18:08 +01:00
|
|
|
// to its equivalant AdvSIMD scalar instruction. Update inputs and outputs
|
|
|
|
// to be the correct register class, minimizing cross-class copies.
|
2016-07-08 22:29:42 +02:00
|
|
|
void AArch64AdvSIMDScalar::transformInstruction(MachineInstr &MI) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Scalar transform: " << MI);
|
2014-03-29 11:18:08 +01:00
|
|
|
|
2016-07-08 22:29:42 +02:00
|
|
|
MachineBasicBlock *MBB = MI.getParent();
|
|
|
|
unsigned OldOpc = MI.getOpcode();
|
2015-05-18 22:27:55 +02:00
|
|
|
unsigned NewOpc = getTransformOpcode(OldOpc);
|
2014-03-29 11:18:08 +01:00
|
|
|
assert(OldOpc != NewOpc && "transform an instruction to itself?!");
|
|
|
|
|
|
|
|
// Check if we need a copy for the source registers.
|
[aarch64] Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Manual fixups in:
AArch64InstrInfo.cpp - genFusedMultiply() now takes a Register* instead of unsigned*
AArch64LoadStoreOptimizer.cpp - Ternary operator was ambiguous between Register/MCRegister. Settled on Register
Depends on D65919
Reviewers: aemerson
Subscribers: jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision for full review was: https://reviews.llvm.org/D65962
llvm-svn: 368628
2019-08-13 00:40:53 +02:00
|
|
|
Register OrigSrc0 = MI.getOperand(1).getReg();
|
|
|
|
Register OrigSrc1 = MI.getOperand(2).getReg();
|
2014-03-29 11:18:08 +01:00
|
|
|
unsigned Src0 = 0, SubReg0;
|
|
|
|
unsigned Src1 = 0, SubReg1;
|
2016-04-22 20:09:14 +02:00
|
|
|
bool KillSrc0 = false, KillSrc1 = false;
|
2014-03-29 11:18:08 +01:00
|
|
|
if (!MRI->def_empty(OrigSrc0)) {
|
|
|
|
MachineRegisterInfo::def_instr_iterator Def =
|
|
|
|
MRI->def_instr_begin(OrigSrc0);
|
|
|
|
assert(std::next(Def) == MRI->def_instr_end() && "Multiple def in SSA!");
|
2016-04-22 20:09:14 +02:00
|
|
|
MachineOperand *MOSrc0 = getSrcFromCopy(&*Def, MRI, SubReg0);
|
2014-03-29 11:18:08 +01:00
|
|
|
// If there are no other users of the original source, we can delete
|
|
|
|
// that instruction.
|
2016-04-22 20:09:14 +02:00
|
|
|
if (MOSrc0) {
|
|
|
|
Src0 = MOSrc0->getReg();
|
|
|
|
KillSrc0 = MOSrc0->isKill();
|
|
|
|
// Src0 is going to be reused, thus, it cannot be killed anymore.
|
|
|
|
MOSrc0->setIsKill(false);
|
|
|
|
if (MRI->hasOneNonDBGUse(OrigSrc0)) {
|
|
|
|
assert(MOSrc0 && "Can't delete copy w/o a valid original source!");
|
|
|
|
Def->eraseFromParent();
|
|
|
|
++NumCopiesDeleted;
|
|
|
|
}
|
2014-03-29 11:18:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!MRI->def_empty(OrigSrc1)) {
|
|
|
|
MachineRegisterInfo::def_instr_iterator Def =
|
|
|
|
MRI->def_instr_begin(OrigSrc1);
|
|
|
|
assert(std::next(Def) == MRI->def_instr_end() && "Multiple def in SSA!");
|
2016-04-22 20:09:14 +02:00
|
|
|
MachineOperand *MOSrc1 = getSrcFromCopy(&*Def, MRI, SubReg1);
|
2014-03-29 11:18:08 +01:00
|
|
|
// If there are no other users of the original source, we can delete
|
|
|
|
// that instruction.
|
2016-04-22 20:09:14 +02:00
|
|
|
if (MOSrc1) {
|
|
|
|
Src1 = MOSrc1->getReg();
|
|
|
|
KillSrc1 = MOSrc1->isKill();
|
|
|
|
// Src0 is going to be reused, thus, it cannot be killed anymore.
|
|
|
|
MOSrc1->setIsKill(false);
|
|
|
|
if (MRI->hasOneNonDBGUse(OrigSrc1)) {
|
|
|
|
assert(MOSrc1 && "Can't delete copy w/o a valid original source!");
|
|
|
|
Def->eraseFromParent();
|
|
|
|
++NumCopiesDeleted;
|
|
|
|
}
|
2014-03-29 11:18:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we weren't able to reference the original source directly, create a
|
|
|
|
// copy.
|
|
|
|
if (!Src0) {
|
|
|
|
SubReg0 = 0;
|
2014-05-24 14:50:23 +02:00
|
|
|
Src0 = MRI->createVirtualRegister(&AArch64::FPR64RegClass);
|
2016-04-22 20:09:14 +02:00
|
|
|
insertCopy(TII, MI, Src0, OrigSrc0, KillSrc0);
|
|
|
|
KillSrc0 = true;
|
2014-03-29 11:18:08 +01:00
|
|
|
}
|
|
|
|
if (!Src1) {
|
|
|
|
SubReg1 = 0;
|
2014-05-24 14:50:23 +02:00
|
|
|
Src1 = MRI->createVirtualRegister(&AArch64::FPR64RegClass);
|
2016-04-22 20:09:14 +02:00
|
|
|
insertCopy(TII, MI, Src1, OrigSrc1, KillSrc1);
|
|
|
|
KillSrc1 = true;
|
2014-03-29 11:18:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a vreg for the destination.
|
|
|
|
// FIXME: No need to do this if the ultimate user expects an FPR64.
|
|
|
|
// Check for that and avoid the copy if possible.
|
[aarch64] Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Manual fixups in:
AArch64InstrInfo.cpp - genFusedMultiply() now takes a Register* instead of unsigned*
AArch64LoadStoreOptimizer.cpp - Ternary operator was ambiguous between Register/MCRegister. Settled on Register
Depends on D65919
Reviewers: aemerson
Subscribers: jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision for full review was: https://reviews.llvm.org/D65962
llvm-svn: 368628
2019-08-13 00:40:53 +02:00
|
|
|
Register Dst = MRI->createVirtualRegister(&AArch64::FPR64RegClass);
|
2014-03-29 11:18:08 +01:00
|
|
|
|
|
|
|
// For now, all of the new instructions have the same simple three-register
|
|
|
|
// form, so no need to special case based on what instruction we're
|
|
|
|
// building.
|
2016-07-08 22:29:42 +02:00
|
|
|
BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(NewOpc), Dst)
|
2016-04-22 20:09:14 +02:00
|
|
|
.addReg(Src0, getKillRegState(KillSrc0), SubReg0)
|
|
|
|
.addReg(Src1, getKillRegState(KillSrc1), SubReg1);
|
2014-03-29 11:18:08 +01:00
|
|
|
|
|
|
|
// Now copy the result back out to a GPR.
|
|
|
|
// FIXME: Try to avoid this if all uses could actually just use the FPR64
|
|
|
|
// directly.
|
2016-07-08 22:29:42 +02:00
|
|
|
insertCopy(TII, MI, MI.getOperand(0).getReg(), Dst, true);
|
2014-03-29 11:18:08 +01:00
|
|
|
|
|
|
|
// Erase the old instruction.
|
2016-07-08 22:29:42 +02:00
|
|
|
MI.eraseFromParent();
|
2014-03-29 11:18:08 +01:00
|
|
|
|
|
|
|
++NumScalarInsnsUsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// processMachineBasicBlock - Main optimzation loop.
|
2014-05-24 14:50:23 +02:00
|
|
|
bool AArch64AdvSIMDScalar::processMachineBasicBlock(MachineBasicBlock *MBB) {
|
2014-03-29 11:18:08 +01:00
|
|
|
bool Changed = false;
|
|
|
|
for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;) {
|
2016-07-08 22:29:42 +02:00
|
|
|
MachineInstr &MI = *I++;
|
2014-03-29 11:18:08 +01:00
|
|
|
if (isProfitableToTransform(MI)) {
|
|
|
|
transformInstruction(MI);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// runOnMachineFunction - Pass entry point from PassManager.
|
2014-05-24 14:50:23 +02:00
|
|
|
bool AArch64AdvSIMDScalar::runOnMachineFunction(MachineFunction &mf) {
|
2014-03-29 11:18:08 +01:00
|
|
|
bool Changed = false;
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "***** AArch64AdvSIMDScalar *****\n");
|
2014-03-29 11:18:08 +01:00
|
|
|
|
2017-12-15 23:22:58 +01:00
|
|
|
if (skipFunction(mf.getFunction()))
|
2016-04-25 23:58:52 +02:00
|
|
|
return false;
|
|
|
|
|
2014-03-29 11:18:08 +01:00
|
|
|
MRI = &mf.getRegInfo();
|
2015-01-30 02:10:18 +01:00
|
|
|
TII = mf.getSubtarget().getInstrInfo();
|
2014-03-29 11:18:08 +01:00
|
|
|
|
|
|
|
// Just check things on a one-block-at-a-time basis.
|
|
|
|
for (MachineFunction::iterator I = mf.begin(), E = mf.end(); I != E; ++I)
|
2015-10-13 22:02:15 +02:00
|
|
|
if (processMachineBasicBlock(&*I))
|
2014-03-29 11:18:08 +01:00
|
|
|
Changed = true;
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2014-05-24 14:50:23 +02:00
|
|
|
// createAArch64AdvSIMDScalar - Factory function used by AArch64TargetMachine
|
2014-03-29 11:18:08 +01:00
|
|
|
// to add the pass to the PassManager.
|
2014-05-24 14:50:23 +02:00
|
|
|
FunctionPass *llvm::createAArch64AdvSIMDScalar() {
|
|
|
|
return new AArch64AdvSIMDScalar();
|
2014-03-29 11:18:08 +01:00
|
|
|
}
|