48#define DEBUG_TYPE "hexmux"
55 "farther of the two predicated uses"));
65 StringRef getPassName()
const override {
66 return "Hexagon generate mux instructions";
69 void getAnalysisUsage(AnalysisUsage &AU)
const override {
73 bool runOnMachineFunction(MachineFunction &MF)
override;
75 MachineFunctionProperties getRequiredProperties()
const override {
76 return MachineFunctionProperties().setNoVRegs();
80 const HexagonInstrInfo *HII =
nullptr;
81 const HexagonRegisterInfo *HRI =
nullptr;
85 unsigned TrueX = std::numeric_limits<unsigned>::max();
86 unsigned FalseX = std::numeric_limits<unsigned>::max();
88 CondsetInfo() =
default;
94 DefUseInfo() =
default;
95 DefUseInfo(
const BitVector &
D,
const BitVector &U) : Defs(
D), Uses(
U) {}
100 unsigned DefR, PredR;
101 MachineOperand *SrcT, *SrcF;
102 MachineInstr *Def1, *Def2;
105 MachineOperand *TOp, MachineOperand *FOp, MachineInstr &D1,
107 : At(It), DefR(DR), PredR(PR), SrcT(TOp), SrcF(FOp), Def1(&D1),
111 using InstrIndexMap = DenseMap<MachineInstr *, unsigned>;
112 using DefUseInfoMap = DenseMap<unsigned, DefUseInfo>;
113 using MuxInfoList = SmallVector<MuxInfo, 4>;
115 bool isRegPair(
unsigned Reg)
const {
116 return Hexagon::DoubleRegsRegClass.contains(
Reg);
119 void getSubRegs(
unsigned Reg, BitVector &SRs)
const;
120 void expandReg(
unsigned Reg, BitVector &Set)
const;
121 void getDefsUses(
const MachineInstr *
MI, BitVector &Defs,
122 BitVector &
Uses)
const;
123 void buildMaps(MachineBasicBlock &
B, InstrIndexMap &I2X,
125 bool isCondTransfer(
unsigned Opc)
const;
126 unsigned getMuxOpcode(
const MachineOperand &Src1,
127 const MachineOperand &Src2)
const;
128 bool genMuxInBlock(MachineBasicBlock &
B);
133char HexagonGenMux::ID = 0;
136 "Hexagon generate mux instructions",
false,
false)
138void HexagonGenMux::getSubRegs(
unsigned Reg,
BitVector &SRs)
const {
143void HexagonGenMux::expandReg(
unsigned Reg,
BitVector &Set)
const {
145 getSubRegs(
Reg, Set);
150void HexagonGenMux::getDefsUses(
const MachineInstr *
MI, BitVector &Defs,
151 BitVector &
Uses)
const {
153 unsigned Opc =
MI->getOpcode();
154 const MCInstrDesc &
D = HII->get(
Opc);
161 for (
const MachineOperand &MO :
MI->operands()) {
162 if (!MO.isReg() || MO.isImplicit())
165 BitVector &
Set = MO.isDef() ? Defs :
Uses;
170void HexagonGenMux::buildMaps(MachineBasicBlock &
B, InstrIndexMap &I2X,
171 DefUseInfoMap &DUM) {
173 unsigned NR = HRI->getNumRegs();
174 BitVector Defs(NR),
Uses(NR);
176 for (MachineInstr &
MI :
B) {
177 I2X.insert(std::make_pair(&
MI, Index));
180 getDefsUses(&
MI, Defs,
Uses);
181 DUM.insert(std::make_pair(Index, DefUseInfo(Defs,
Uses)));
186bool HexagonGenMux::isCondTransfer(
unsigned Opc)
const {
188 case Hexagon::A2_tfrt:
189 case Hexagon::A2_tfrf:
190 case Hexagon::C2_cmoveit:
191 case Hexagon::C2_cmoveif:
197unsigned HexagonGenMux::getMuxOpcode(
const MachineOperand &Src1,
198 const MachineOperand &Src2)
const {
199 bool IsReg1 = Src1.
isReg(), IsReg2 = Src2.
isReg();
201 return IsReg2 ? Hexagon::C2_mux : Hexagon::C2_muxir;
203 return Hexagon::C2_muxri;
208 return Hexagon::C2_muxii;
213bool HexagonGenMux::genMuxInBlock(MachineBasicBlock &
B) {
217 buildMaps(
B, I2X, DUM);
219 using CondsetMap = DenseMap<unsigned, CondsetInfo>;
225 unsigned Opc =
MI.getOpcode();
226 if (!isCondTransfer(
Opc))
231 MachineOperand &PredOp =
MI.getOperand(1);
236 unsigned Idx = I2X.lookup(&
MI);
237 CondsetMap::iterator
F = CM.find(DR);
242 if (
F != CM.end() &&
F->second.PredR != PR) {
247 F = CM.try_emplace(DR).first;
248 F->second.PredR = PR;
250 CondsetInfo &CI =
F->second;
255 if (CI.TrueX == std::numeric_limits<unsigned>::max() ||
256 CI.FalseX == std::numeric_limits<unsigned>::max())
264 unsigned MinX = std::min(CI.TrueX, CI.FalseX);
265 unsigned MaxX = std::max(CI.TrueX, CI.FalseX);
269 bool NearDef =
false;
270 for (
unsigned X = SearchX;
X < MaxX; ++
X) {
271 const DefUseInfo &DU = DUM.lookup(
X);
287 std::advance(It1, MinX);
288 std::advance(It2, MaxX);
289 MachineInstr &Def1 = *It1, &Def2 = *It2;
290 MachineOperand *Src1 = &Def1.
getOperand(2), *Src2 = &Def2.getOperand(2);
293 bool Failure =
false, CanUp =
true, CanDown =
true;
294 for (
unsigned X = MinX+1;
X < MaxX;
X++) {
295 const DefUseInfo &DU = DUM.lookup(
X);
296 if (DU.Defs[PR] || DU.Defs[DR] || DU.Uses[DR]) {
300 if (CanDown && DU.Defs[SR1])
302 if (CanUp && DU.Defs[SR2])
305 if (Failure || (!CanUp && !CanDown))
308 MachineOperand *SrcT = (MinX == CI.TrueX) ? Src1 : Src2;
309 MachineOperand *SrcF = (MinX == CI.FalseX) ? Src1 : Src2;
313 ML.push_back(MuxInfo(At, DR, PR, SrcT, SrcF, Def1, Def2));
316 for (MuxInfo &MX :
ML) {
317 unsigned MxOpc = getMuxOpcode(*MX.SrcT, *MX.SrcF);
323 if (!MX.At->getParent() || !MX.Def1->getParent() || !MX.Def2->getParent())
326 MachineBasicBlock &
B = *MX.At->getParent();
328 auto NewMux =
BuildMI(
B, MX.At,
DL, HII->get(MxOpc), MX.DefR)
340 LiveRegUnits LPR(*HRI);
343 if (
I.isDebugInstr())
349 for (MachineOperand &
Op :
I.operands()) {
350 if (!
Op.isReg() || !
Op.isUse())
352 assert(
Op.getSubReg() == 0 &&
"Should have physical registers only");
353 bool Live = !LPR.available(
Op.getReg());
362bool HexagonGenMux::runOnMachineFunction(MachineFunction &MF) {
365 HII = MF.
getSubtarget<HexagonSubtarget>().getInstrInfo();
366 HRI = MF.
getSubtarget<HexagonSubtarget>().getRegisterInfo();
374 return new HexagonGenMux();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the BitVector class.
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines the DenseMap class.
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"))
Promote Memory to Register
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Remove Loads Into Fake Uses
This file defines the SmallVector class.
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
FunctionPass class - This class is used to implement most global optimizations.
bool isPredicatedTrue(const MachineInstr &MI) const
LLVM_ABI DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI void clearKillInfo()
Clears kill flags on all operands.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
Register getReg() const
getReg - Returns the register number.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
auto reverse(ContainerTy &&C)
FunctionPass * createHexagonGenMux()
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
DWARFExpression::Operation Op