2017-07-29 02:56:56 +02:00
|
|
|
//===- HexagonGenMux.cpp --------------------------------------------------===//
|
2015-07-20 23:23:25 +02: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
|
2015-07-20 23:23:25 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// During instruction selection, MUX instructions are generated for
|
|
|
|
// conditional assignments. Since such assignments often present an
|
|
|
|
// opportunity to predicate instructions, HexagonExpandCondsets
|
|
|
|
// expands MUXes into pairs of conditional transfers, and then proceeds
|
|
|
|
// with predication of the producers/consumers of the registers involved.
|
|
|
|
// This happens after exiting from the SSA form, but before the machine
|
|
|
|
// instruction scheduler. After the scheduler and after the register
|
|
|
|
// allocation there can be cases of pairs of conditional transfers
|
|
|
|
// resulting from a MUX where neither of them was further predicated. If
|
|
|
|
// these transfers are now placed far enough from the instruction defining
|
|
|
|
// the predicate register, they cannot use the .new form. In such cases it
|
|
|
|
// is better to collapse them back to a single MUX instruction.
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "hexmux"
|
|
|
|
|
2016-12-16 02:00:40 +01:00
|
|
|
#include "HexagonInstrInfo.h"
|
|
|
|
#include "HexagonRegisterInfo.h"
|
|
|
|
#include "HexagonSubtarget.h"
|
|
|
|
#include "llvm/ADT/BitVector.h"
|
2017-07-29 02:56:56 +02:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2016-12-16 02:00:40 +01:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-06-22 22:43:02 +02:00
|
|
|
#include "llvm/CodeGen/LivePhysRegs.h"
|
2016-12-16 02:00:40 +01:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2015-07-20 23:23:25 +02:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2016-12-16 02:00:40 +01:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2015-07-20 23:23:25 +02:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2016-12-16 02:00:40 +01:00
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
|
|
|
#include "llvm/IR/DebugLoc.h"
|
|
|
|
#include "llvm/MC/MCInstrDesc.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/Pass.h"
|
2018-03-23 19:43:09 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2016-12-16 02:00:40 +01:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include <algorithm>
|
2017-07-29 02:56:56 +02:00
|
|
|
#include <cassert>
|
2016-12-16 02:00:40 +01:00
|
|
|
#include <iterator>
|
2017-06-06 13:49:48 +02:00
|
|
|
#include <limits>
|
2016-12-16 02:00:40 +01:00
|
|
|
#include <utility>
|
2015-07-20 23:23:25 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace llvm {
|
2016-12-16 02:00:40 +01:00
|
|
|
|
2015-07-20 23:23:25 +02:00
|
|
|
FunctionPass *createHexagonGenMux();
|
|
|
|
void initializeHexagonGenMuxPass(PassRegistry& Registry);
|
2016-12-16 02:00:40 +01:00
|
|
|
|
|
|
|
} // end namespace llvm
|
2015-07-20 23:23:25 +02:00
|
|
|
|
2018-03-23 19:43:09 +01:00
|
|
|
// Initialize this to 0 to always prefer generating mux by default.
|
|
|
|
static cl::opt<unsigned> MinPredDist("hexagon-gen-mux-threshold", cl::Hidden,
|
|
|
|
cl::init(0), cl::desc("Minimum distance between predicate definition and "
|
|
|
|
"farther of the two predicated uses"));
|
|
|
|
|
2015-07-20 23:23:25 +02:00
|
|
|
namespace {
|
2016-12-16 02:00:40 +01:00
|
|
|
|
2015-07-20 23:23:25 +02:00
|
|
|
class HexagonGenMux : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
2016-12-16 02:00:40 +01:00
|
|
|
|
2017-06-13 18:07:36 +02:00
|
|
|
HexagonGenMux() : MachineFunctionPass(ID) {}
|
2016-12-16 02:00:40 +01:00
|
|
|
|
2016-10-01 04:56:57 +02:00
|
|
|
StringRef getPassName() const override {
|
2015-07-20 23:23:25 +02:00
|
|
|
return "Hexagon generate mux instructions";
|
|
|
|
}
|
2016-12-16 02:00:40 +01:00
|
|
|
|
2015-07-20 23:23:25 +02:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2016-12-16 02:00:40 +01:00
|
|
|
|
2015-07-20 23:23:25 +02:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2016-12-16 02:00:40 +01:00
|
|
|
|
2016-04-04 19:09:25 +02:00
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
|
|
|
return MachineFunctionProperties().set(
|
2016-08-25 03:27:13 +02:00
|
|
|
MachineFunctionProperties::Property::NoVRegs);
|
2016-04-04 19:09:25 +02:00
|
|
|
}
|
2015-07-20 23:23:25 +02:00
|
|
|
|
|
|
|
private:
|
2017-06-13 18:07:36 +02:00
|
|
|
const HexagonInstrInfo *HII = nullptr;
|
|
|
|
const HexagonRegisterInfo *HRI = nullptr;
|
2015-07-20 23:23:25 +02:00
|
|
|
|
|
|
|
struct CondsetInfo {
|
2016-12-16 02:00:40 +01:00
|
|
|
unsigned PredR = 0;
|
|
|
|
unsigned TrueX = std::numeric_limits<unsigned>::max();
|
|
|
|
unsigned FalseX = std::numeric_limits<unsigned>::max();
|
|
|
|
|
|
|
|
CondsetInfo() = default;
|
2015-07-20 23:23:25 +02:00
|
|
|
};
|
2016-12-16 02:00:40 +01:00
|
|
|
|
2015-07-20 23:23:25 +02:00
|
|
|
struct DefUseInfo {
|
|
|
|
BitVector Defs, Uses;
|
2016-12-16 02:00:40 +01:00
|
|
|
|
|
|
|
DefUseInfo() = default;
|
2015-07-20 23:23:25 +02:00
|
|
|
DefUseInfo(const BitVector &D, const BitVector &U) : Defs(D), Uses(U) {}
|
|
|
|
};
|
2016-12-16 02:00:40 +01:00
|
|
|
|
2015-07-20 23:23:25 +02:00
|
|
|
struct MuxInfo {
|
|
|
|
MachineBasicBlock::iterator At;
|
|
|
|
unsigned DefR, PredR;
|
|
|
|
MachineOperand *SrcT, *SrcF;
|
|
|
|
MachineInstr *Def1, *Def2;
|
2016-12-16 02:00:40 +01:00
|
|
|
|
2015-07-20 23:23:25 +02:00
|
|
|
MuxInfo(MachineBasicBlock::iterator It, unsigned DR, unsigned PR,
|
2016-07-12 03:55:32 +02:00
|
|
|
MachineOperand *TOp, MachineOperand *FOp, MachineInstr &D1,
|
|
|
|
MachineInstr &D2)
|
|
|
|
: At(It), DefR(DR), PredR(PR), SrcT(TOp), SrcF(FOp), Def1(&D1),
|
|
|
|
Def2(&D2) {}
|
2015-07-20 23:23:25 +02:00
|
|
|
};
|
2016-12-16 02:00:40 +01:00
|
|
|
|
2017-07-29 02:56:56 +02:00
|
|
|
using InstrIndexMap = DenseMap<MachineInstr *, unsigned>;
|
|
|
|
using DefUseInfoMap = DenseMap<unsigned, DefUseInfo>;
|
|
|
|
using MuxInfoList = SmallVector<MuxInfo, 4>;
|
2015-07-20 23:23:25 +02:00
|
|
|
|
|
|
|
bool isRegPair(unsigned Reg) const {
|
|
|
|
return Hexagon::DoubleRegsRegClass.contains(Reg);
|
|
|
|
}
|
2016-12-16 02:00:40 +01:00
|
|
|
|
2015-07-20 23:23:25 +02:00
|
|
|
void getSubRegs(unsigned Reg, BitVector &SRs) const;
|
|
|
|
void expandReg(unsigned Reg, BitVector &Set) const;
|
|
|
|
void getDefsUses(const MachineInstr *MI, BitVector &Defs,
|
|
|
|
BitVector &Uses) const;
|
|
|
|
void buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
|
|
|
|
DefUseInfoMap &DUM);
|
|
|
|
bool isCondTransfer(unsigned Opc) const;
|
|
|
|
unsigned getMuxOpcode(const MachineOperand &Src1,
|
|
|
|
const MachineOperand &Src2) const;
|
|
|
|
bool genMuxInBlock(MachineBasicBlock &B);
|
|
|
|
};
|
|
|
|
|
2016-12-16 02:00:40 +01:00
|
|
|
} // end anonymous namespace
|
2015-07-20 23:23:25 +02:00
|
|
|
|
2017-07-29 02:56:56 +02:00
|
|
|
char HexagonGenMux::ID = 0;
|
|
|
|
|
2017-06-13 18:07:36 +02:00
|
|
|
INITIALIZE_PASS(HexagonGenMux, "hexagon-gen-mux",
|
2015-07-20 23:23:25 +02:00
|
|
|
"Hexagon generate mux instructions", false, false)
|
|
|
|
|
|
|
|
void HexagonGenMux::getSubRegs(unsigned Reg, BitVector &SRs) const {
|
|
|
|
for (MCSubRegIterator I(Reg, HRI); I.isValid(); ++I)
|
|
|
|
SRs[*I] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonGenMux::expandReg(unsigned Reg, BitVector &Set) const {
|
|
|
|
if (isRegPair(Reg))
|
|
|
|
getSubRegs(Reg, Set);
|
|
|
|
else
|
|
|
|
Set[Reg] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonGenMux::getDefsUses(const MachineInstr *MI, BitVector &Defs,
|
|
|
|
BitVector &Uses) const {
|
|
|
|
// First, get the implicit defs and uses for this instruction.
|
|
|
|
unsigned Opc = MI->getOpcode();
|
|
|
|
const MCInstrDesc &D = HII->get(Opc);
|
2015-12-05 08:13:35 +01:00
|
|
|
if (const MCPhysReg *R = D.ImplicitDefs)
|
2015-07-20 23:23:25 +02:00
|
|
|
while (*R)
|
|
|
|
expandReg(*R++, Defs);
|
2015-12-05 08:13:35 +01:00
|
|
|
if (const MCPhysReg *R = D.ImplicitUses)
|
2015-07-20 23:23:25 +02:00
|
|
|
while (*R)
|
|
|
|
expandReg(*R++, Uses);
|
|
|
|
|
|
|
|
// Look over all operands, and collect explicit defs and uses.
|
2016-10-24 23:36:43 +02:00
|
|
|
for (const MachineOperand &MO : MI->operands()) {
|
|
|
|
if (!MO.isReg() || MO.isImplicit())
|
2015-07-20 23:23:25 +02:00
|
|
|
continue;
|
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).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, 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: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-15 21:22:08 +02:00
|
|
|
Register R = MO.getReg();
|
2016-10-24 23:36:43 +02:00
|
|
|
BitVector &Set = MO.isDef() ? Defs : Uses;
|
2015-07-20 23:23:25 +02:00
|
|
|
expandReg(R, Set);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonGenMux::buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
|
|
|
|
DefUseInfoMap &DUM) {
|
|
|
|
unsigned Index = 0;
|
|
|
|
unsigned NR = HRI->getNumRegs();
|
|
|
|
BitVector Defs(NR), Uses(NR);
|
|
|
|
|
|
|
|
for (MachineBasicBlock::iterator I = B.begin(), E = B.end(); I != E; ++I) {
|
|
|
|
MachineInstr *MI = &*I;
|
|
|
|
I2X.insert(std::make_pair(MI, Index));
|
|
|
|
Defs.reset();
|
|
|
|
Uses.reset();
|
|
|
|
getDefsUses(MI, Defs, Uses);
|
|
|
|
DUM.insert(std::make_pair(Index, DefUseInfo(Defs, Uses)));
|
|
|
|
Index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HexagonGenMux::isCondTransfer(unsigned Opc) const {
|
|
|
|
switch (Opc) {
|
|
|
|
case Hexagon::A2_tfrt:
|
|
|
|
case Hexagon::A2_tfrf:
|
|
|
|
case Hexagon::C2_cmoveit:
|
|
|
|
case Hexagon::C2_cmoveif:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned HexagonGenMux::getMuxOpcode(const MachineOperand &Src1,
|
|
|
|
const MachineOperand &Src2) const {
|
|
|
|
bool IsReg1 = Src1.isReg(), IsReg2 = Src2.isReg();
|
|
|
|
if (IsReg1)
|
|
|
|
return IsReg2 ? Hexagon::C2_mux : Hexagon::C2_muxir;
|
|
|
|
if (IsReg2)
|
|
|
|
return Hexagon::C2_muxri;
|
|
|
|
|
|
|
|
// Neither is a register. The first source is extendable, but the second
|
|
|
|
// is not (s8).
|
|
|
|
if (Src2.isImm() && isInt<8>(Src2.getImm()))
|
|
|
|
return Hexagon::C2_muxii;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HexagonGenMux::genMuxInBlock(MachineBasicBlock &B) {
|
|
|
|
bool Changed = false;
|
|
|
|
InstrIndexMap I2X;
|
|
|
|
DefUseInfoMap DUM;
|
|
|
|
buildMaps(B, I2X, DUM);
|
|
|
|
|
2017-07-29 02:56:56 +02:00
|
|
|
using CondsetMap = DenseMap<unsigned, CondsetInfo>;
|
|
|
|
|
2015-07-20 23:23:25 +02:00
|
|
|
CondsetMap CM;
|
|
|
|
MuxInfoList ML;
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator NextI, End = B.end();
|
|
|
|
for (MachineBasicBlock::iterator I = B.begin(); I != End; I = NextI) {
|
|
|
|
MachineInstr *MI = &*I;
|
|
|
|
NextI = std::next(I);
|
|
|
|
unsigned Opc = MI->getOpcode();
|
|
|
|
if (!isCondTransfer(Opc))
|
|
|
|
continue;
|
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).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, 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: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-15 21:22:08 +02:00
|
|
|
Register DR = MI->getOperand(0).getReg();
|
2015-07-20 23:23:25 +02:00
|
|
|
if (isRegPair(DR))
|
|
|
|
continue;
|
2017-06-08 22:56:36 +02:00
|
|
|
MachineOperand &PredOp = MI->getOperand(1);
|
|
|
|
if (PredOp.isUndef())
|
|
|
|
continue;
|
2015-07-20 23:23:25 +02:00
|
|
|
|
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).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, 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: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-15 21:22:08 +02:00
|
|
|
Register PR = PredOp.getReg();
|
2015-07-20 23:23:25 +02:00
|
|
|
unsigned Idx = I2X.lookup(MI);
|
|
|
|
CondsetMap::iterator F = CM.find(DR);
|
|
|
|
bool IfTrue = HII->isPredicatedTrue(Opc);
|
|
|
|
|
|
|
|
// If there is no record of a conditional transfer for this register,
|
|
|
|
// or the predicate register differs, create a new record for it.
|
|
|
|
if (F != CM.end() && F->second.PredR != PR) {
|
|
|
|
CM.erase(F);
|
|
|
|
F = CM.end();
|
|
|
|
}
|
|
|
|
if (F == CM.end()) {
|
|
|
|
auto It = CM.insert(std::make_pair(DR, CondsetInfo()));
|
|
|
|
F = It.first;
|
|
|
|
F->second.PredR = PR;
|
|
|
|
}
|
|
|
|
CondsetInfo &CI = F->second;
|
|
|
|
if (IfTrue)
|
|
|
|
CI.TrueX = Idx;
|
|
|
|
else
|
|
|
|
CI.FalseX = Idx;
|
2016-12-16 02:00:40 +01:00
|
|
|
if (CI.TrueX == std::numeric_limits<unsigned>::max() ||
|
|
|
|
CI.FalseX == std::numeric_limits<unsigned>::max())
|
2015-07-20 23:23:25 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// There is now a complete definition of DR, i.e. we have the predicate
|
|
|
|
// register, the definition if-true, and definition if-false.
|
|
|
|
|
2018-03-23 19:43:09 +01:00
|
|
|
// First, check if the definitions are far enough from the definition
|
2015-07-20 23:23:25 +02:00
|
|
|
// of the predicate register.
|
|
|
|
unsigned MinX = std::min(CI.TrueX, CI.FalseX);
|
|
|
|
unsigned MaxX = std::max(CI.TrueX, CI.FalseX);
|
2018-03-23 19:43:09 +01:00
|
|
|
// Specifically, check if the predicate definition is within a prescribed
|
|
|
|
// distance from the farther of the two predicated instructions.
|
|
|
|
unsigned SearchX = (MaxX >= MinPredDist) ? MaxX-MinPredDist : 0;
|
2015-07-20 23:23:25 +02:00
|
|
|
bool NearDef = false;
|
|
|
|
for (unsigned X = SearchX; X < MaxX; ++X) {
|
|
|
|
const DefUseInfo &DU = DUM.lookup(X);
|
|
|
|
if (!DU.Defs[PR])
|
|
|
|
continue;
|
|
|
|
NearDef = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (NearDef)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// The predicate register is not defined in the last few instructions.
|
|
|
|
// Check if the conversion to MUX is possible (either "up", i.e. at the
|
|
|
|
// place of the earlier partial definition, or "down", where the later
|
|
|
|
// definition is located). Examine all defs and uses between these two
|
|
|
|
// definitions.
|
|
|
|
// SR1, SR2 - source registers from the first and the second definition.
|
|
|
|
MachineBasicBlock::iterator It1 = B.begin(), It2 = B.begin();
|
|
|
|
std::advance(It1, MinX);
|
|
|
|
std::advance(It2, MaxX);
|
2016-07-12 03:55:32 +02:00
|
|
|
MachineInstr &Def1 = *It1, &Def2 = *It2;
|
|
|
|
MachineOperand *Src1 = &Def1.getOperand(2), *Src2 = &Def2.getOperand(2);
|
2019-06-24 17:50:29 +02:00
|
|
|
Register SR1 = Src1->isReg() ? Src1->getReg() : Register();
|
|
|
|
Register SR2 = Src2->isReg() ? Src2->getReg() : Register();
|
2015-07-20 23:23:25 +02:00
|
|
|
bool Failure = false, CanUp = true, CanDown = true;
|
|
|
|
for (unsigned X = MinX+1; X < MaxX; X++) {
|
|
|
|
const DefUseInfo &DU = DUM.lookup(X);
|
|
|
|
if (DU.Defs[PR] || DU.Defs[DR] || DU.Uses[DR]) {
|
|
|
|
Failure = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (CanDown && DU.Defs[SR1])
|
|
|
|
CanDown = false;
|
|
|
|
if (CanUp && DU.Defs[SR2])
|
|
|
|
CanUp = false;
|
|
|
|
}
|
|
|
|
if (Failure || (!CanUp && !CanDown))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
MachineOperand *SrcT = (MinX == CI.TrueX) ? Src1 : Src2;
|
|
|
|
MachineOperand *SrcF = (MinX == CI.FalseX) ? Src1 : Src2;
|
|
|
|
// Prefer "down", since this will move the MUX farther away from the
|
|
|
|
// predicate definition.
|
|
|
|
MachineBasicBlock::iterator At = CanDown ? Def2 : Def1;
|
|
|
|
ML.push_back(MuxInfo(At, DR, PR, SrcT, SrcF, Def1, Def2));
|
|
|
|
}
|
|
|
|
|
2017-06-22 22:43:02 +02:00
|
|
|
for (MuxInfo &MX : ML) {
|
2015-07-20 23:23:25 +02:00
|
|
|
unsigned MxOpc = getMuxOpcode(*MX.SrcT, *MX.SrcF);
|
|
|
|
if (!MxOpc)
|
|
|
|
continue;
|
2019-11-14 20:01:16 +01:00
|
|
|
// Basic sanity check: since we are deleting instructions, validate the
|
|
|
|
// iterators. There is a possibility that one of Def1 or Def2 is translated
|
|
|
|
// to "mux" and being considered for other "mux" instructions.
|
|
|
|
if (!MX.At->getParent() || !MX.Def1->getParent() || !MX.Def2->getParent())
|
|
|
|
continue;
|
|
|
|
|
2017-06-22 22:43:02 +02:00
|
|
|
MachineBasicBlock &B = *MX.At->getParent();
|
|
|
|
const DebugLoc &DL = B.findDebugLoc(MX.At);
|
|
|
|
auto NewMux = BuildMI(B, MX.At, DL, HII->get(MxOpc), MX.DefR)
|
|
|
|
.addReg(MX.PredR)
|
|
|
|
.add(*MX.SrcT)
|
|
|
|
.add(*MX.SrcF);
|
|
|
|
NewMux->clearKillInfo();
|
2019-11-14 20:01:16 +01:00
|
|
|
B.remove(MX.Def1);
|
|
|
|
B.remove(MX.Def2);
|
2015-07-20 23:23:25 +02:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2017-06-22 22:43:02 +02:00
|
|
|
// Fix up kill flags.
|
|
|
|
|
|
|
|
LivePhysRegs LPR(*HRI);
|
|
|
|
LPR.addLiveOuts(B);
|
|
|
|
auto IsLive = [&LPR,this] (unsigned Reg) -> bool {
|
|
|
|
for (MCSubRegIterator S(Reg, HRI, true); S.isValid(); ++S)
|
|
|
|
if (LPR.contains(*S))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
for (auto I = B.rbegin(), E = B.rend(); I != E; ++I) {
|
2018-05-09 04:42:00 +02:00
|
|
|
if (I->isDebugInstr())
|
2017-06-22 22:43:02 +02:00
|
|
|
continue;
|
|
|
|
// This isn't 100% accurate, but it's safe.
|
|
|
|
// It won't detect (as a kill) a case like this
|
|
|
|
// r0 = add r0, 1 <-- r0 should be "killed"
|
|
|
|
// ... = r0
|
|
|
|
for (MachineOperand &Op : I->operands()) {
|
|
|
|
if (!Op.isReg() || !Op.isUse())
|
|
|
|
continue;
|
|
|
|
assert(Op.getSubReg() == 0 && "Should have physical registers only");
|
|
|
|
bool Live = IsLive(Op.getReg());
|
|
|
|
Op.setIsKill(!Live);
|
|
|
|
}
|
|
|
|
LPR.stepBackward(*I);
|
|
|
|
}
|
|
|
|
|
2015-07-20 23:23:25 +02:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HexagonGenMux::runOnMachineFunction(MachineFunction &MF) {
|
2017-12-15 23:22:58 +01:00
|
|
|
if (skipFunction(MF.getFunction()))
|
2016-04-26 21:46:28 +02:00
|
|
|
return false;
|
2015-07-20 23:23:25 +02:00
|
|
|
HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
|
|
|
|
HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
|
|
|
|
bool Changed = false;
|
|
|
|
for (auto &I : MF)
|
|
|
|
Changed |= genMuxInBlock(I);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createHexagonGenMux() {
|
|
|
|
return new HexagonGenMux();
|
|
|
|
}
|