Path: blob/main/contrib/llvm-project/llvm/lib/Target/Hexagon/HexagonGenMux.cpp
35266 views
//===- HexagonGenMux.cpp --------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78// During instruction selection, MUX instructions are generated for9// conditional assignments. Since such assignments often present an10// opportunity to predicate instructions, HexagonExpandCondsets11// expands MUXes into pairs of conditional transfers, and then proceeds12// with predication of the producers/consumers of the registers involved.13// This happens after exiting from the SSA form, but before the machine14// instruction scheduler. After the scheduler and after the register15// allocation there can be cases of pairs of conditional transfers16// resulting from a MUX where neither of them was further predicated. If17// these transfers are now placed far enough from the instruction defining18// the predicate register, they cannot use the .new form. In such cases it19// is better to collapse them back to a single MUX instruction.2021#include "HexagonInstrInfo.h"22#include "HexagonRegisterInfo.h"23#include "HexagonSubtarget.h"24#include "llvm/ADT/BitVector.h"25#include "llvm/ADT/DenseMap.h"26#include "llvm/ADT/SmallVector.h"27#include "llvm/ADT/StringRef.h"28#include "llvm/CodeGen/LiveRegUnits.h"29#include "llvm/CodeGen/MachineBasicBlock.h"30#include "llvm/CodeGen/MachineFunction.h"31#include "llvm/CodeGen/MachineFunctionPass.h"32#include "llvm/CodeGen/MachineInstr.h"33#include "llvm/CodeGen/MachineInstrBuilder.h"34#include "llvm/CodeGen/MachineOperand.h"35#include "llvm/IR/DebugLoc.h"36#include "llvm/MC/MCInstrDesc.h"37#include "llvm/MC/MCRegisterInfo.h"38#include "llvm/Pass.h"39#include "llvm/Support/CommandLine.h"40#include "llvm/Support/MathExtras.h"41#include <algorithm>42#include <cassert>43#include <iterator>44#include <limits>45#include <utility>4647#define DEBUG_TYPE "hexmux"4849using namespace llvm;5051namespace llvm {5253FunctionPass *createHexagonGenMux();54void initializeHexagonGenMuxPass(PassRegistry& Registry);5556} // end namespace llvm5758// Initialize this to 0 to always prefer generating mux by default.59static cl::opt<unsigned> MinPredDist("hexagon-gen-mux-threshold", cl::Hidden,60cl::init(0), cl::desc("Minimum distance between predicate definition and "61"farther of the two predicated uses"));6263namespace {6465class HexagonGenMux : public MachineFunctionPass {66public:67static char ID;6869HexagonGenMux() : MachineFunctionPass(ID) {}7071StringRef getPassName() const override {72return "Hexagon generate mux instructions";73}7475void getAnalysisUsage(AnalysisUsage &AU) const override {76MachineFunctionPass::getAnalysisUsage(AU);77}7879bool runOnMachineFunction(MachineFunction &MF) override;8081MachineFunctionProperties getRequiredProperties() const override {82return MachineFunctionProperties().set(83MachineFunctionProperties::Property::NoVRegs);84}8586private:87const HexagonInstrInfo *HII = nullptr;88const HexagonRegisterInfo *HRI = nullptr;8990struct CondsetInfo {91unsigned PredR = 0;92unsigned TrueX = std::numeric_limits<unsigned>::max();93unsigned FalseX = std::numeric_limits<unsigned>::max();9495CondsetInfo() = default;96};9798struct DefUseInfo {99BitVector Defs, Uses;100101DefUseInfo() = default;102DefUseInfo(const BitVector &D, const BitVector &U) : Defs(D), Uses(U) {}103};104105struct MuxInfo {106MachineBasicBlock::iterator At;107unsigned DefR, PredR;108MachineOperand *SrcT, *SrcF;109MachineInstr *Def1, *Def2;110111MuxInfo(MachineBasicBlock::iterator It, unsigned DR, unsigned PR,112MachineOperand *TOp, MachineOperand *FOp, MachineInstr &D1,113MachineInstr &D2)114: At(It), DefR(DR), PredR(PR), SrcT(TOp), SrcF(FOp), Def1(&D1),115Def2(&D2) {}116};117118using InstrIndexMap = DenseMap<MachineInstr *, unsigned>;119using DefUseInfoMap = DenseMap<unsigned, DefUseInfo>;120using MuxInfoList = SmallVector<MuxInfo, 4>;121122bool isRegPair(unsigned Reg) const {123return Hexagon::DoubleRegsRegClass.contains(Reg);124}125126void getSubRegs(unsigned Reg, BitVector &SRs) const;127void expandReg(unsigned Reg, BitVector &Set) const;128void getDefsUses(const MachineInstr *MI, BitVector &Defs,129BitVector &Uses) const;130void buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,131DefUseInfoMap &DUM);132bool isCondTransfer(unsigned Opc) const;133unsigned getMuxOpcode(const MachineOperand &Src1,134const MachineOperand &Src2) const;135bool genMuxInBlock(MachineBasicBlock &B);136};137138} // end anonymous namespace139140char HexagonGenMux::ID = 0;141142INITIALIZE_PASS(HexagonGenMux, "hexagon-gen-mux",143"Hexagon generate mux instructions", false, false)144145void HexagonGenMux::getSubRegs(unsigned Reg, BitVector &SRs) const {146for (MCPhysReg I : HRI->subregs(Reg))147SRs[I] = true;148}149150void HexagonGenMux::expandReg(unsigned Reg, BitVector &Set) const {151if (isRegPair(Reg))152getSubRegs(Reg, Set);153else154Set[Reg] = true;155}156157void HexagonGenMux::getDefsUses(const MachineInstr *MI, BitVector &Defs,158BitVector &Uses) const {159// First, get the implicit defs and uses for this instruction.160unsigned Opc = MI->getOpcode();161const MCInstrDesc &D = HII->get(Opc);162for (MCPhysReg R : D.implicit_defs())163expandReg(R, Defs);164for (MCPhysReg R : D.implicit_uses())165expandReg(R, Uses);166167// Look over all operands, and collect explicit defs and uses.168for (const MachineOperand &MO : MI->operands()) {169if (!MO.isReg() || MO.isImplicit())170continue;171Register R = MO.getReg();172BitVector &Set = MO.isDef() ? Defs : Uses;173expandReg(R, Set);174}175}176177void HexagonGenMux::buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,178DefUseInfoMap &DUM) {179unsigned Index = 0;180unsigned NR = HRI->getNumRegs();181BitVector Defs(NR), Uses(NR);182183for (MachineInstr &MI : B) {184I2X.insert(std::make_pair(&MI, Index));185Defs.reset();186Uses.reset();187getDefsUses(&MI, Defs, Uses);188DUM.insert(std::make_pair(Index, DefUseInfo(Defs, Uses)));189Index++;190}191}192193bool HexagonGenMux::isCondTransfer(unsigned Opc) const {194switch (Opc) {195case Hexagon::A2_tfrt:196case Hexagon::A2_tfrf:197case Hexagon::C2_cmoveit:198case Hexagon::C2_cmoveif:199return true;200}201return false;202}203204unsigned HexagonGenMux::getMuxOpcode(const MachineOperand &Src1,205const MachineOperand &Src2) const {206bool IsReg1 = Src1.isReg(), IsReg2 = Src2.isReg();207if (IsReg1)208return IsReg2 ? Hexagon::C2_mux : Hexagon::C2_muxir;209if (IsReg2)210return Hexagon::C2_muxri;211212// Neither is a register. The first source is extendable, but the second213// is not (s8).214if (Src2.isImm() && isInt<8>(Src2.getImm()))215return Hexagon::C2_muxii;216217return 0;218}219220bool HexagonGenMux::genMuxInBlock(MachineBasicBlock &B) {221bool Changed = false;222InstrIndexMap I2X;223DefUseInfoMap DUM;224buildMaps(B, I2X, DUM);225226using CondsetMap = DenseMap<unsigned, CondsetInfo>;227228CondsetMap CM;229MuxInfoList ML;230231for (MachineInstr &MI : llvm::make_early_inc_range(B)) {232unsigned Opc = MI.getOpcode();233if (!isCondTransfer(Opc))234continue;235Register DR = MI.getOperand(0).getReg();236if (isRegPair(DR))237continue;238MachineOperand &PredOp = MI.getOperand(1);239if (PredOp.isUndef())240continue;241242Register PR = PredOp.getReg();243unsigned Idx = I2X.lookup(&MI);244CondsetMap::iterator F = CM.find(DR);245bool IfTrue = HII->isPredicatedTrue(Opc);246247// If there is no record of a conditional transfer for this register,248// or the predicate register differs, create a new record for it.249if (F != CM.end() && F->second.PredR != PR) {250CM.erase(F);251F = CM.end();252}253if (F == CM.end()) {254auto It = CM.insert(std::make_pair(DR, CondsetInfo()));255F = It.first;256F->second.PredR = PR;257}258CondsetInfo &CI = F->second;259if (IfTrue)260CI.TrueX = Idx;261else262CI.FalseX = Idx;263if (CI.TrueX == std::numeric_limits<unsigned>::max() ||264CI.FalseX == std::numeric_limits<unsigned>::max())265continue;266267// There is now a complete definition of DR, i.e. we have the predicate268// register, the definition if-true, and definition if-false.269270// First, check if the definitions are far enough from the definition271// of the predicate register.272unsigned MinX = std::min(CI.TrueX, CI.FalseX);273unsigned MaxX = std::max(CI.TrueX, CI.FalseX);274// Specifically, check if the predicate definition is within a prescribed275// distance from the farther of the two predicated instructions.276unsigned SearchX = (MaxX >= MinPredDist) ? MaxX-MinPredDist : 0;277bool NearDef = false;278for (unsigned X = SearchX; X < MaxX; ++X) {279const DefUseInfo &DU = DUM.lookup(X);280if (!DU.Defs[PR])281continue;282NearDef = true;283break;284}285if (NearDef)286continue;287288// The predicate register is not defined in the last few instructions.289// Check if the conversion to MUX is possible (either "up", i.e. at the290// place of the earlier partial definition, or "down", where the later291// definition is located). Examine all defs and uses between these two292// definitions.293// SR1, SR2 - source registers from the first and the second definition.294MachineBasicBlock::iterator It1 = B.begin(), It2 = B.begin();295std::advance(It1, MinX);296std::advance(It2, MaxX);297MachineInstr &Def1 = *It1, &Def2 = *It2;298MachineOperand *Src1 = &Def1.getOperand(2), *Src2 = &Def2.getOperand(2);299Register SR1 = Src1->isReg() ? Src1->getReg() : Register();300Register SR2 = Src2->isReg() ? Src2->getReg() : Register();301bool Failure = false, CanUp = true, CanDown = true;302for (unsigned X = MinX+1; X < MaxX; X++) {303const DefUseInfo &DU = DUM.lookup(X);304if (DU.Defs[PR] || DU.Defs[DR] || DU.Uses[DR]) {305Failure = true;306break;307}308if (CanDown && DU.Defs[SR1])309CanDown = false;310if (CanUp && DU.Defs[SR2])311CanUp = false;312}313if (Failure || (!CanUp && !CanDown))314continue;315316MachineOperand *SrcT = (MinX == CI.TrueX) ? Src1 : Src2;317MachineOperand *SrcF = (MinX == CI.FalseX) ? Src1 : Src2;318// Prefer "down", since this will move the MUX farther away from the319// predicate definition.320MachineBasicBlock::iterator At = CanDown ? Def2 : Def1;321ML.push_back(MuxInfo(At, DR, PR, SrcT, SrcF, Def1, Def2));322}323324for (MuxInfo &MX : ML) {325unsigned MxOpc = getMuxOpcode(*MX.SrcT, *MX.SrcF);326if (!MxOpc)327continue;328// Basic correctness check: since we are deleting instructions, validate the329// iterators. There is a possibility that one of Def1 or Def2 is translated330// to "mux" and being considered for other "mux" instructions.331if (!MX.At->getParent() || !MX.Def1->getParent() || !MX.Def2->getParent())332continue;333334MachineBasicBlock &B = *MX.At->getParent();335const DebugLoc &DL = B.findDebugLoc(MX.At);336auto NewMux = BuildMI(B, MX.At, DL, HII->get(MxOpc), MX.DefR)337.addReg(MX.PredR)338.add(*MX.SrcT)339.add(*MX.SrcF);340NewMux->clearKillInfo();341B.remove(MX.Def1);342B.remove(MX.Def2);343Changed = true;344}345346// Fix up kill flags.347348LiveRegUnits LPR(*HRI);349LPR.addLiveOuts(B);350for (MachineInstr &I : llvm::reverse(B)) {351if (I.isDebugInstr())352continue;353// This isn't 100% accurate, but it's safe.354// It won't detect (as a kill) a case like this355// r0 = add r0, 1 <-- r0 should be "killed"356// ... = r0357for (MachineOperand &Op : I.operands()) {358if (!Op.isReg() || !Op.isUse())359continue;360assert(Op.getSubReg() == 0 && "Should have physical registers only");361bool Live = !LPR.available(Op.getReg());362Op.setIsKill(!Live);363}364LPR.stepBackward(I);365}366367return Changed;368}369370bool HexagonGenMux::runOnMachineFunction(MachineFunction &MF) {371if (skipFunction(MF.getFunction()))372return false;373HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();374HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();375bool Changed = false;376for (auto &I : MF)377Changed |= genMuxInBlock(I);378return Changed;379}380381FunctionPass *llvm::createHexagonGenMux() {382return new HexagonGenMux();383}384385386