10b57cec5SDimitry Andric //===- HexagonEarlyIfConv.cpp ---------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This implements a Hexagon-specific if-conversion pass that runs on the
100b57cec5SDimitry Andric // SSA form.
110b57cec5SDimitry Andric // In SSA it is not straightforward to represent instructions that condi-
120b57cec5SDimitry Andric // tionally define registers, since a conditionally-defined register may
130b57cec5SDimitry Andric // only be used under the same condition on which the definition was based.
140b57cec5SDimitry Andric // To avoid complications of this nature, this patch will only generate
150b57cec5SDimitry Andric // predicated stores, and speculate other instructions from the "if-conver-
160b57cec5SDimitry Andric // ted" block.
170b57cec5SDimitry Andric // The code will recognize CFG patterns where a block with a conditional
180b57cec5SDimitry Andric // branch "splits" into a "true block" and a "false block". Either of these
190b57cec5SDimitry Andric // could be omitted (in case of a triangle, for example).
200b57cec5SDimitry Andric // If after conversion of the side block(s) the CFG allows it, the resul-
210b57cec5SDimitry Andric // ting blocks may be merged. If the "join" block contained PHI nodes, they
220b57cec5SDimitry Andric // will be replaced with MUX (or MUX-like) instructions to maintain the
230b57cec5SDimitry Andric // semantics of the PHI.
240b57cec5SDimitry Andric //
250b57cec5SDimitry Andric // Example:
260b57cec5SDimitry Andric //
270b57cec5SDimitry Andric // %40 = L2_loadrub_io killed %39, 1
280b57cec5SDimitry Andric // %41 = S2_tstbit_i killed %40, 0
290b57cec5SDimitry Andric // J2_jumpt killed %41, <%bb.5>, implicit dead %pc
300b57cec5SDimitry Andric // J2_jump <%bb.4>, implicit dead %pc
310b57cec5SDimitry Andric // Successors according to CFG: %bb.4(62) %bb.5(62)
320b57cec5SDimitry Andric //
330b57cec5SDimitry Andric // %bb.4: derived from LLVM BB %if.then
340b57cec5SDimitry Andric // Predecessors according to CFG: %bb.3
350b57cec5SDimitry Andric // %11 = A2_addp %6, %10
360b57cec5SDimitry Andric // S2_storerd_io %32, 16, %11
370b57cec5SDimitry Andric // Successors according to CFG: %bb.5
380b57cec5SDimitry Andric //
390b57cec5SDimitry Andric // %bb.5: derived from LLVM BB %if.end
400b57cec5SDimitry Andric // Predecessors according to CFG: %bb.3 %bb.4
410b57cec5SDimitry Andric // %12 = PHI %6, <%bb.3>, %11, <%bb.4>
420b57cec5SDimitry Andric // %13 = A2_addp %7, %12
430b57cec5SDimitry Andric // %42 = C2_cmpeqi %9, 10
440b57cec5SDimitry Andric // J2_jumpf killed %42, <%bb.3>, implicit dead %pc
450b57cec5SDimitry Andric // J2_jump <%bb.6>, implicit dead %pc
460b57cec5SDimitry Andric // Successors according to CFG: %bb.6(4) %bb.3(124)
470b57cec5SDimitry Andric //
480b57cec5SDimitry Andric // would become:
490b57cec5SDimitry Andric //
500b57cec5SDimitry Andric // %40 = L2_loadrub_io killed %39, 1
510b57cec5SDimitry Andric // %41 = S2_tstbit_i killed %40, 0
520b57cec5SDimitry Andric // spec-> %11 = A2_addp %6, %10
530b57cec5SDimitry Andric // pred-> S2_pstorerdf_io %41, %32, 16, %11
540b57cec5SDimitry Andric // %46 = PS_pselect %41, %6, %11
550b57cec5SDimitry Andric // %13 = A2_addp %7, %46
560b57cec5SDimitry Andric // %42 = C2_cmpeqi %9, 10
570b57cec5SDimitry Andric // J2_jumpf killed %42, <%bb.3>, implicit dead %pc
580b57cec5SDimitry Andric // J2_jump <%bb.6>, implicit dead %pc
590b57cec5SDimitry Andric // Successors according to CFG: %bb.6 %bb.3
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric #include "Hexagon.h"
620b57cec5SDimitry Andric #include "HexagonInstrInfo.h"
630b57cec5SDimitry Andric #include "HexagonSubtarget.h"
640b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h"
650b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
660b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
670b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
680b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
690b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
700b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
710b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
720b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
730b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
740b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
750b57cec5SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
760b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
770b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
780b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
790b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
800b57cec5SDimitry Andric #include "llvm/Pass.h"
810b57cec5SDimitry Andric #include "llvm/Support/BranchProbability.h"
820b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
830b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
840b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
850b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
860b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
870b57cec5SDimitry Andric #include <cassert>
880b57cec5SDimitry Andric #include <iterator>
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric #define DEBUG_TYPE "hexagon-eif"
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric using namespace llvm;
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric namespace llvm {
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric FunctionPass *createHexagonEarlyIfConversion();
970b57cec5SDimitry Andric void initializeHexagonEarlyIfConversionPass(PassRegistry& Registry);
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric } // end namespace llvm
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric static cl::opt<bool> EnableHexagonBP("enable-hexagon-br-prob", cl::Hidden,
1020b57cec5SDimitry Andric cl::init(true), cl::desc("Enable branch probability info"));
1030b57cec5SDimitry Andric static cl::opt<unsigned> SizeLimit("eif-limit", cl::init(6), cl::Hidden,
1040b57cec5SDimitry Andric cl::desc("Size limit in Hexagon early if-conversion"));
1050b57cec5SDimitry Andric static cl::opt<bool> SkipExitBranches("eif-no-loop-exit", cl::init(false),
1060b57cec5SDimitry Andric cl::Hidden, cl::desc("Do not convert branches that may exit the loop"));
1070b57cec5SDimitry Andric
1080b57cec5SDimitry Andric namespace {
1090b57cec5SDimitry Andric
1100b57cec5SDimitry Andric struct PrintMB {
PrintMB__anon363a33cb0111::PrintMB1110b57cec5SDimitry Andric PrintMB(const MachineBasicBlock *B) : MB(B) {}
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric const MachineBasicBlock *MB;
1140b57cec5SDimitry Andric };
operator <<(raw_ostream & OS,const PrintMB & P)1150b57cec5SDimitry Andric raw_ostream &operator<< (raw_ostream &OS, const PrintMB &P) {
1160b57cec5SDimitry Andric if (!P.MB)
1170b57cec5SDimitry Andric return OS << "<none>";
1180b57cec5SDimitry Andric return OS << '#' << P.MB->getNumber();
1190b57cec5SDimitry Andric }
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric struct FlowPattern {
1220b57cec5SDimitry Andric FlowPattern() = default;
FlowPattern__anon363a33cb0111::FlowPattern1230b57cec5SDimitry Andric FlowPattern(MachineBasicBlock *B, unsigned PR, MachineBasicBlock *TB,
1240b57cec5SDimitry Andric MachineBasicBlock *FB, MachineBasicBlock *JB)
1250b57cec5SDimitry Andric : SplitB(B), TrueB(TB), FalseB(FB), JoinB(JB), PredR(PR) {}
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric MachineBasicBlock *SplitB = nullptr;
1280b57cec5SDimitry Andric MachineBasicBlock *TrueB = nullptr;
1290b57cec5SDimitry Andric MachineBasicBlock *FalseB = nullptr;
1300b57cec5SDimitry Andric MachineBasicBlock *JoinB = nullptr;
1310b57cec5SDimitry Andric unsigned PredR = 0;
1320b57cec5SDimitry Andric };
1330b57cec5SDimitry Andric
1340b57cec5SDimitry Andric struct PrintFP {
PrintFP__anon363a33cb0111::PrintFP1350b57cec5SDimitry Andric PrintFP(const FlowPattern &P, const TargetRegisterInfo &T)
1360b57cec5SDimitry Andric : FP(P), TRI(T) {}
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric const FlowPattern &FP;
1390b57cec5SDimitry Andric const TargetRegisterInfo &TRI;
1400b57cec5SDimitry Andric friend raw_ostream &operator<< (raw_ostream &OS, const PrintFP &P);
1410b57cec5SDimitry Andric };
1420b57cec5SDimitry Andric raw_ostream &operator<<(raw_ostream &OS,
1430b57cec5SDimitry Andric const PrintFP &P) LLVM_ATTRIBUTE_UNUSED;
operator <<(raw_ostream & OS,const PrintFP & P)1440b57cec5SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const PrintFP &P) {
1450b57cec5SDimitry Andric OS << "{ SplitB:" << PrintMB(P.FP.SplitB)
1460b57cec5SDimitry Andric << ", PredR:" << printReg(P.FP.PredR, &P.TRI)
1470b57cec5SDimitry Andric << ", TrueB:" << PrintMB(P.FP.TrueB)
1480b57cec5SDimitry Andric << ", FalseB:" << PrintMB(P.FP.FalseB)
1490b57cec5SDimitry Andric << ", JoinB:" << PrintMB(P.FP.JoinB) << " }";
1500b57cec5SDimitry Andric return OS;
1510b57cec5SDimitry Andric }
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric class HexagonEarlyIfConversion : public MachineFunctionPass {
1540b57cec5SDimitry Andric public:
1550b57cec5SDimitry Andric static char ID;
1560b57cec5SDimitry Andric
HexagonEarlyIfConversion()1570b57cec5SDimitry Andric HexagonEarlyIfConversion() : MachineFunctionPass(ID) {}
1580b57cec5SDimitry Andric
getPassName() const1590b57cec5SDimitry Andric StringRef getPassName() const override {
1600b57cec5SDimitry Andric return "Hexagon early if conversion";
1610b57cec5SDimitry Andric }
1620b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const1630b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
164*0fca6ea1SDimitry Andric AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
165*0fca6ea1SDimitry Andric AU.addRequired<MachineDominatorTreeWrapperPass>();
166*0fca6ea1SDimitry Andric AU.addPreserved<MachineDominatorTreeWrapperPass>();
167*0fca6ea1SDimitry Andric AU.addRequired<MachineLoopInfoWrapperPass>();
1680b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric
1710b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
1720b57cec5SDimitry Andric
1730b57cec5SDimitry Andric private:
1740b57cec5SDimitry Andric using BlockSetType = DenseSet<MachineBasicBlock *>;
1750b57cec5SDimitry Andric
1760b57cec5SDimitry Andric bool isPreheader(const MachineBasicBlock *B) const;
1770b57cec5SDimitry Andric bool matchFlowPattern(MachineBasicBlock *B, MachineLoop *L,
1780b57cec5SDimitry Andric FlowPattern &FP);
1790b57cec5SDimitry Andric bool visitBlock(MachineBasicBlock *B, MachineLoop *L);
1800b57cec5SDimitry Andric bool visitLoop(MachineLoop *L);
1810b57cec5SDimitry Andric
1820b57cec5SDimitry Andric bool hasEHLabel(const MachineBasicBlock *B) const;
1830b57cec5SDimitry Andric bool hasUncondBranch(const MachineBasicBlock *B) const;
1840b57cec5SDimitry Andric bool isValidCandidate(const MachineBasicBlock *B) const;
1850b57cec5SDimitry Andric bool usesUndefVReg(const MachineInstr *MI) const;
1860b57cec5SDimitry Andric bool isValid(const FlowPattern &FP) const;
1870b57cec5SDimitry Andric unsigned countPredicateDefs(const MachineBasicBlock *B) const;
1880b57cec5SDimitry Andric unsigned computePhiCost(const MachineBasicBlock *B,
1890b57cec5SDimitry Andric const FlowPattern &FP) const;
1900b57cec5SDimitry Andric bool isProfitable(const FlowPattern &FP) const;
1910b57cec5SDimitry Andric bool isPredicableStore(const MachineInstr *MI) const;
1920b57cec5SDimitry Andric bool isSafeToSpeculate(const MachineInstr *MI) const;
1930b57cec5SDimitry Andric bool isPredicate(unsigned R) const;
1940b57cec5SDimitry Andric
1950b57cec5SDimitry Andric unsigned getCondStoreOpcode(unsigned Opc, bool IfTrue) const;
1960b57cec5SDimitry Andric void predicateInstr(MachineBasicBlock *ToB, MachineBasicBlock::iterator At,
1970b57cec5SDimitry Andric MachineInstr *MI, unsigned PredR, bool IfTrue);
1980b57cec5SDimitry Andric void predicateBlockNB(MachineBasicBlock *ToB,
1990b57cec5SDimitry Andric MachineBasicBlock::iterator At, MachineBasicBlock *FromB,
2000b57cec5SDimitry Andric unsigned PredR, bool IfTrue);
2010b57cec5SDimitry Andric
2020b57cec5SDimitry Andric unsigned buildMux(MachineBasicBlock *B, MachineBasicBlock::iterator At,
2030b57cec5SDimitry Andric const TargetRegisterClass *DRC, unsigned PredR, unsigned TR,
2040b57cec5SDimitry Andric unsigned TSR, unsigned FR, unsigned FSR);
2050b57cec5SDimitry Andric void updatePhiNodes(MachineBasicBlock *WhereB, const FlowPattern &FP);
2060b57cec5SDimitry Andric void convert(const FlowPattern &FP);
2070b57cec5SDimitry Andric
2080b57cec5SDimitry Andric void removeBlock(MachineBasicBlock *B);
2090b57cec5SDimitry Andric void eliminatePhis(MachineBasicBlock *B);
2100b57cec5SDimitry Andric void mergeBlocks(MachineBasicBlock *PredB, MachineBasicBlock *SuccB);
2110b57cec5SDimitry Andric void simplifyFlowGraph(const FlowPattern &FP);
2120b57cec5SDimitry Andric
2130b57cec5SDimitry Andric const HexagonInstrInfo *HII = nullptr;
2140b57cec5SDimitry Andric const TargetRegisterInfo *TRI = nullptr;
2150b57cec5SDimitry Andric MachineFunction *MFN = nullptr;
2160b57cec5SDimitry Andric MachineRegisterInfo *MRI = nullptr;
2170b57cec5SDimitry Andric MachineDominatorTree *MDT = nullptr;
2180b57cec5SDimitry Andric MachineLoopInfo *MLI = nullptr;
2190b57cec5SDimitry Andric BlockSetType Deleted;
220480093f4SDimitry Andric const MachineBranchProbabilityInfo *MBPI = nullptr;
2210b57cec5SDimitry Andric };
2220b57cec5SDimitry Andric
2230b57cec5SDimitry Andric } // end anonymous namespace
2240b57cec5SDimitry Andric
2250b57cec5SDimitry Andric char HexagonEarlyIfConversion::ID = 0;
2260b57cec5SDimitry Andric
2270b57cec5SDimitry Andric INITIALIZE_PASS(HexagonEarlyIfConversion, "hexagon-early-if",
2280b57cec5SDimitry Andric "Hexagon early if conversion", false, false)
2290b57cec5SDimitry Andric
isPreheader(const MachineBasicBlock * B) const2300b57cec5SDimitry Andric bool HexagonEarlyIfConversion::isPreheader(const MachineBasicBlock *B) const {
2310b57cec5SDimitry Andric if (B->succ_size() != 1)
2320b57cec5SDimitry Andric return false;
2330b57cec5SDimitry Andric MachineBasicBlock *SB = *B->succ_begin();
2340b57cec5SDimitry Andric MachineLoop *L = MLI->getLoopFor(SB);
2350b57cec5SDimitry Andric return L && SB == L->getHeader() && MDT->dominates(B, SB);
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric
matchFlowPattern(MachineBasicBlock * B,MachineLoop * L,FlowPattern & FP)2380b57cec5SDimitry Andric bool HexagonEarlyIfConversion::matchFlowPattern(MachineBasicBlock *B,
2390b57cec5SDimitry Andric MachineLoop *L, FlowPattern &FP) {
2400b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Checking flow pattern at " << printMBBReference(*B)
2410b57cec5SDimitry Andric << "\n");
2420b57cec5SDimitry Andric
2430b57cec5SDimitry Andric // Interested only in conditional branches, no .new, no new-value, etc.
2440b57cec5SDimitry Andric // Check the terminators directly, it's easier than handling all responses
2450b57cec5SDimitry Andric // from analyzeBranch.
2460b57cec5SDimitry Andric MachineBasicBlock *TB = nullptr, *FB = nullptr;
2470b57cec5SDimitry Andric MachineBasicBlock::const_iterator T1I = B->getFirstTerminator();
2480b57cec5SDimitry Andric if (T1I == B->end())
2490b57cec5SDimitry Andric return false;
2500b57cec5SDimitry Andric unsigned Opc = T1I->getOpcode();
2510b57cec5SDimitry Andric if (Opc != Hexagon::J2_jumpt && Opc != Hexagon::J2_jumpf)
2520b57cec5SDimitry Andric return false;
2538bcb0991SDimitry Andric Register PredR = T1I->getOperand(0).getReg();
2540b57cec5SDimitry Andric
2550b57cec5SDimitry Andric // Get the layout successor, or 0 if B does not have one.
2560b57cec5SDimitry Andric MachineFunction::iterator NextBI = std::next(MachineFunction::iterator(B));
2570b57cec5SDimitry Andric MachineBasicBlock *NextB = (NextBI != MFN->end()) ? &*NextBI : nullptr;
2580b57cec5SDimitry Andric
2590b57cec5SDimitry Andric MachineBasicBlock *T1B = T1I->getOperand(1).getMBB();
2600b57cec5SDimitry Andric MachineBasicBlock::const_iterator T2I = std::next(T1I);
2610b57cec5SDimitry Andric // The second terminator should be an unconditional branch.
2620b57cec5SDimitry Andric assert(T2I == B->end() || T2I->getOpcode() == Hexagon::J2_jump);
2630b57cec5SDimitry Andric MachineBasicBlock *T2B = (T2I == B->end()) ? NextB
2640b57cec5SDimitry Andric : T2I->getOperand(0).getMBB();
2650b57cec5SDimitry Andric if (T1B == T2B) {
2660b57cec5SDimitry Andric // XXX merge if T1B == NextB, or convert branch to unconditional.
2670b57cec5SDimitry Andric // mark as diamond with both sides equal?
2680b57cec5SDimitry Andric return false;
2690b57cec5SDimitry Andric }
2700b57cec5SDimitry Andric
2710b57cec5SDimitry Andric // Record the true/false blocks in such a way that "true" means "if (PredR)",
2720b57cec5SDimitry Andric // and "false" means "if (!PredR)".
2730b57cec5SDimitry Andric if (Opc == Hexagon::J2_jumpt)
2740b57cec5SDimitry Andric TB = T1B, FB = T2B;
2750b57cec5SDimitry Andric else
2760b57cec5SDimitry Andric TB = T2B, FB = T1B;
2770b57cec5SDimitry Andric
2780b57cec5SDimitry Andric if (!MDT->properlyDominates(B, TB) || !MDT->properlyDominates(B, FB))
2790b57cec5SDimitry Andric return false;
2800b57cec5SDimitry Andric
2810b57cec5SDimitry Andric // Detect triangle first. In case of a triangle, one of the blocks TB/FB
2820b57cec5SDimitry Andric // can fall through into the other, in other words, it will be executed
2830b57cec5SDimitry Andric // in both cases. We only want to predicate the block that is executed
2840b57cec5SDimitry Andric // conditionally.
285480093f4SDimitry Andric assert(TB && FB && "Failed to find triangle control flow blocks");
2860b57cec5SDimitry Andric unsigned TNP = TB->pred_size(), FNP = FB->pred_size();
2870b57cec5SDimitry Andric unsigned TNS = TB->succ_size(), FNS = FB->succ_size();
2880b57cec5SDimitry Andric
2890b57cec5SDimitry Andric // A block is predicable if it has one predecessor (it must be B), and
2900b57cec5SDimitry Andric // it has a single successor. In fact, the block has to end either with
2910b57cec5SDimitry Andric // an unconditional branch (which can be predicated), or with a fall-
2920b57cec5SDimitry Andric // through.
2930b57cec5SDimitry Andric // Also, skip blocks that do not belong to the same loop.
2940b57cec5SDimitry Andric bool TOk = (TNP == 1 && TNS == 1 && MLI->getLoopFor(TB) == L);
2950b57cec5SDimitry Andric bool FOk = (FNP == 1 && FNS == 1 && MLI->getLoopFor(FB) == L);
2960b57cec5SDimitry Andric
2970b57cec5SDimitry Andric // If requested (via an option), do not consider branches where the
2980b57cec5SDimitry Andric // true and false targets do not belong to the same loop.
2990b57cec5SDimitry Andric if (SkipExitBranches && MLI->getLoopFor(TB) != MLI->getLoopFor(FB))
3000b57cec5SDimitry Andric return false;
3010b57cec5SDimitry Andric
3020b57cec5SDimitry Andric // If neither is predicable, there is nothing interesting.
3030b57cec5SDimitry Andric if (!TOk && !FOk)
3040b57cec5SDimitry Andric return false;
3050b57cec5SDimitry Andric
3060b57cec5SDimitry Andric MachineBasicBlock *TSB = (TNS > 0) ? *TB->succ_begin() : nullptr;
3070b57cec5SDimitry Andric MachineBasicBlock *FSB = (FNS > 0) ? *FB->succ_begin() : nullptr;
3080b57cec5SDimitry Andric MachineBasicBlock *JB = nullptr;
3090b57cec5SDimitry Andric
3100b57cec5SDimitry Andric if (TOk) {
3110b57cec5SDimitry Andric if (FOk) {
3120b57cec5SDimitry Andric if (TSB == FSB)
3130b57cec5SDimitry Andric JB = TSB;
3140b57cec5SDimitry Andric // Diamond: "if (P) then TB; else FB;".
3150b57cec5SDimitry Andric } else {
3160b57cec5SDimitry Andric // TOk && !FOk
3170b57cec5SDimitry Andric if (TSB == FB)
3180b57cec5SDimitry Andric JB = FB;
3190b57cec5SDimitry Andric FB = nullptr;
3200b57cec5SDimitry Andric }
3210b57cec5SDimitry Andric } else {
3220b57cec5SDimitry Andric // !TOk && FOk (at least one must be true by now).
3230b57cec5SDimitry Andric if (FSB == TB)
3240b57cec5SDimitry Andric JB = TB;
3250b57cec5SDimitry Andric TB = nullptr;
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric // Don't try to predicate loop preheaders.
3280b57cec5SDimitry Andric if ((TB && isPreheader(TB)) || (FB && isPreheader(FB))) {
3290b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "One of blocks " << PrintMB(TB) << ", " << PrintMB(FB)
3300b57cec5SDimitry Andric << " is a loop preheader. Skipping.\n");
3310b57cec5SDimitry Andric return false;
3320b57cec5SDimitry Andric }
3330b57cec5SDimitry Andric
3340b57cec5SDimitry Andric FP = FlowPattern(B, PredR, TB, FB, JB);
3350b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Detected " << PrintFP(FP, *TRI) << "\n");
3360b57cec5SDimitry Andric return true;
3370b57cec5SDimitry Andric }
3380b57cec5SDimitry Andric
3390b57cec5SDimitry Andric // KLUDGE: HexagonInstrInfo::analyzeBranch won't work on a block that
3400b57cec5SDimitry Andric // contains EH_LABEL.
hasEHLabel(const MachineBasicBlock * B) const3410b57cec5SDimitry Andric bool HexagonEarlyIfConversion::hasEHLabel(const MachineBasicBlock *B) const {
3420b57cec5SDimitry Andric for (auto &I : *B)
3430b57cec5SDimitry Andric if (I.isEHLabel())
3440b57cec5SDimitry Andric return true;
3450b57cec5SDimitry Andric return false;
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric
3480b57cec5SDimitry Andric // KLUDGE: HexagonInstrInfo::analyzeBranch may be unable to recognize
3490b57cec5SDimitry Andric // that a block can never fall-through.
hasUncondBranch(const MachineBasicBlock * B) const3500b57cec5SDimitry Andric bool HexagonEarlyIfConversion::hasUncondBranch(const MachineBasicBlock *B)
3510b57cec5SDimitry Andric const {
3520b57cec5SDimitry Andric MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
3530b57cec5SDimitry Andric while (I != E) {
3540b57cec5SDimitry Andric if (I->isBarrier())
3550b57cec5SDimitry Andric return true;
3560b57cec5SDimitry Andric ++I;
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric return false;
3590b57cec5SDimitry Andric }
3600b57cec5SDimitry Andric
isValidCandidate(const MachineBasicBlock * B) const3610b57cec5SDimitry Andric bool HexagonEarlyIfConversion::isValidCandidate(const MachineBasicBlock *B)
3620b57cec5SDimitry Andric const {
3630b57cec5SDimitry Andric if (!B)
3640b57cec5SDimitry Andric return true;
3650b57cec5SDimitry Andric if (B->isEHPad() || B->hasAddressTaken())
3660b57cec5SDimitry Andric return false;
367349cc55cSDimitry Andric if (B->succ_empty())
3680b57cec5SDimitry Andric return false;
3690b57cec5SDimitry Andric
3700b57cec5SDimitry Andric for (auto &MI : *B) {
3710b57cec5SDimitry Andric if (MI.isDebugInstr())
3720b57cec5SDimitry Andric continue;
3730b57cec5SDimitry Andric if (MI.isConditionalBranch())
3740b57cec5SDimitry Andric return false;
3750b57cec5SDimitry Andric unsigned Opc = MI.getOpcode();
3760b57cec5SDimitry Andric bool IsJMP = (Opc == Hexagon::J2_jump);
3770b57cec5SDimitry Andric if (!isPredicableStore(&MI) && !IsJMP && !isSafeToSpeculate(&MI))
3780b57cec5SDimitry Andric return false;
3790b57cec5SDimitry Andric // Look for predicate registers defined by this instruction. It's ok
3800b57cec5SDimitry Andric // to speculate such an instruction, but the predicate register cannot
3810b57cec5SDimitry Andric // be used outside of this block (or else it won't be possible to
3820b57cec5SDimitry Andric // update the use of it after predication). PHI uses will be updated
3830b57cec5SDimitry Andric // to use a result of a MUX, and a MUX cannot be created for predicate
3840b57cec5SDimitry Andric // registers.
3850b57cec5SDimitry Andric for (const MachineOperand &MO : MI.operands()) {
3860b57cec5SDimitry Andric if (!MO.isReg() || !MO.isDef())
3870b57cec5SDimitry Andric continue;
3888bcb0991SDimitry Andric Register R = MO.getReg();
389e8d8bef9SDimitry Andric if (!R.isVirtual())
3900b57cec5SDimitry Andric continue;
3910b57cec5SDimitry Andric if (!isPredicate(R))
3920b57cec5SDimitry Andric continue;
393349cc55cSDimitry Andric for (const MachineOperand &U : MRI->use_operands(R))
394349cc55cSDimitry Andric if (U.getParent()->isPHI())
3950b57cec5SDimitry Andric return false;
3960b57cec5SDimitry Andric }
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric return true;
3990b57cec5SDimitry Andric }
4000b57cec5SDimitry Andric
usesUndefVReg(const MachineInstr * MI) const4010b57cec5SDimitry Andric bool HexagonEarlyIfConversion::usesUndefVReg(const MachineInstr *MI) const {
4020b57cec5SDimitry Andric for (const MachineOperand &MO : MI->operands()) {
4030b57cec5SDimitry Andric if (!MO.isReg() || !MO.isUse())
4040b57cec5SDimitry Andric continue;
4058bcb0991SDimitry Andric Register R = MO.getReg();
406e8d8bef9SDimitry Andric if (!R.isVirtual())
4070b57cec5SDimitry Andric continue;
4080b57cec5SDimitry Andric const MachineInstr *DefI = MRI->getVRegDef(R);
4090b57cec5SDimitry Andric // "Undefined" virtual registers are actually defined via IMPLICIT_DEF.
4100b57cec5SDimitry Andric assert(DefI && "Expecting a reaching def in MRI");
4110b57cec5SDimitry Andric if (DefI->isImplicitDef())
4120b57cec5SDimitry Andric return true;
4130b57cec5SDimitry Andric }
4140b57cec5SDimitry Andric return false;
4150b57cec5SDimitry Andric }
4160b57cec5SDimitry Andric
isValid(const FlowPattern & FP) const4170b57cec5SDimitry Andric bool HexagonEarlyIfConversion::isValid(const FlowPattern &FP) const {
4180b57cec5SDimitry Andric if (hasEHLabel(FP.SplitB)) // KLUDGE: see function definition
4190b57cec5SDimitry Andric return false;
4200b57cec5SDimitry Andric if (FP.TrueB && !isValidCandidate(FP.TrueB))
4210b57cec5SDimitry Andric return false;
4220b57cec5SDimitry Andric if (FP.FalseB && !isValidCandidate(FP.FalseB))
4230b57cec5SDimitry Andric return false;
4240b57cec5SDimitry Andric // Check the PHIs in the join block. If any of them use a register
4250b57cec5SDimitry Andric // that is defined as IMPLICIT_DEF, do not convert this. This can
4260b57cec5SDimitry Andric // legitimately happen if one side of the split never executes, but
4270b57cec5SDimitry Andric // the compiler is unable to prove it. That side may then seem to
4280b57cec5SDimitry Andric // provide an "undef" value to the join block, however it will never
4290b57cec5SDimitry Andric // execute at run-time. If we convert this case, the "undef" will
4300b57cec5SDimitry Andric // be used in a MUX instruction, and that may seem like actually
4310b57cec5SDimitry Andric // using an undefined value to other optimizations. This could lead
4320b57cec5SDimitry Andric // to trouble further down the optimization stream, cause assertions
4330b57cec5SDimitry Andric // to fail, etc.
4340b57cec5SDimitry Andric if (FP.JoinB) {
4350b57cec5SDimitry Andric const MachineBasicBlock &B = *FP.JoinB;
4360b57cec5SDimitry Andric for (auto &MI : B) {
4370b57cec5SDimitry Andric if (!MI.isPHI())
4380b57cec5SDimitry Andric break;
4390b57cec5SDimitry Andric if (usesUndefVReg(&MI))
4400b57cec5SDimitry Andric return false;
4418bcb0991SDimitry Andric Register DefR = MI.getOperand(0).getReg();
4420b57cec5SDimitry Andric if (isPredicate(DefR))
4430b57cec5SDimitry Andric return false;
4440b57cec5SDimitry Andric }
4450b57cec5SDimitry Andric }
4460b57cec5SDimitry Andric return true;
4470b57cec5SDimitry Andric }
4480b57cec5SDimitry Andric
computePhiCost(const MachineBasicBlock * B,const FlowPattern & FP) const4490b57cec5SDimitry Andric unsigned HexagonEarlyIfConversion::computePhiCost(const MachineBasicBlock *B,
4500b57cec5SDimitry Andric const FlowPattern &FP) const {
4510b57cec5SDimitry Andric if (B->pred_size() < 2)
4520b57cec5SDimitry Andric return 0;
4530b57cec5SDimitry Andric
4540b57cec5SDimitry Andric unsigned Cost = 0;
4550b57cec5SDimitry Andric for (const MachineInstr &MI : *B) {
4560b57cec5SDimitry Andric if (!MI.isPHI())
4570b57cec5SDimitry Andric break;
4580b57cec5SDimitry Andric // If both incoming blocks are one of the TrueB/FalseB/SplitB, then
4590b57cec5SDimitry Andric // a MUX may be needed. Otherwise the PHI will need to be updated at
4600b57cec5SDimitry Andric // no extra cost.
4610b57cec5SDimitry Andric // Find the interesting PHI operands for further checks.
4620b57cec5SDimitry Andric SmallVector<unsigned,2> Inc;
4630b57cec5SDimitry Andric for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
4640b57cec5SDimitry Andric const MachineBasicBlock *BB = MI.getOperand(i+1).getMBB();
4650b57cec5SDimitry Andric if (BB == FP.SplitB || BB == FP.TrueB || BB == FP.FalseB)
4660b57cec5SDimitry Andric Inc.push_back(i);
4670b57cec5SDimitry Andric }
4680b57cec5SDimitry Andric assert(Inc.size() <= 2);
4690b57cec5SDimitry Andric if (Inc.size() < 2)
4700b57cec5SDimitry Andric continue;
4710b57cec5SDimitry Andric
4720b57cec5SDimitry Andric const MachineOperand &RA = MI.getOperand(1);
4730b57cec5SDimitry Andric const MachineOperand &RB = MI.getOperand(3);
4740b57cec5SDimitry Andric assert(RA.isReg() && RB.isReg());
4750b57cec5SDimitry Andric // Must have a MUX if the phi uses a subregister.
4760b57cec5SDimitry Andric if (RA.getSubReg() != 0 || RB.getSubReg() != 0) {
4770b57cec5SDimitry Andric Cost++;
4780b57cec5SDimitry Andric continue;
4790b57cec5SDimitry Andric }
4800b57cec5SDimitry Andric const MachineInstr *Def1 = MRI->getVRegDef(RA.getReg());
4810b57cec5SDimitry Andric const MachineInstr *Def3 = MRI->getVRegDef(RB.getReg());
4820b57cec5SDimitry Andric if (!HII->isPredicable(*Def1) || !HII->isPredicable(*Def3))
4830b57cec5SDimitry Andric Cost++;
4840b57cec5SDimitry Andric }
4850b57cec5SDimitry Andric return Cost;
4860b57cec5SDimitry Andric }
4870b57cec5SDimitry Andric
countPredicateDefs(const MachineBasicBlock * B) const4880b57cec5SDimitry Andric unsigned HexagonEarlyIfConversion::countPredicateDefs(
4890b57cec5SDimitry Andric const MachineBasicBlock *B) const {
4900b57cec5SDimitry Andric unsigned PredDefs = 0;
4910b57cec5SDimitry Andric for (auto &MI : *B) {
4920b57cec5SDimitry Andric for (const MachineOperand &MO : MI.operands()) {
4930b57cec5SDimitry Andric if (!MO.isReg() || !MO.isDef())
4940b57cec5SDimitry Andric continue;
4958bcb0991SDimitry Andric Register R = MO.getReg();
496e8d8bef9SDimitry Andric if (!R.isVirtual())
4970b57cec5SDimitry Andric continue;
4980b57cec5SDimitry Andric if (isPredicate(R))
4990b57cec5SDimitry Andric PredDefs++;
5000b57cec5SDimitry Andric }
5010b57cec5SDimitry Andric }
5020b57cec5SDimitry Andric return PredDefs;
5030b57cec5SDimitry Andric }
5040b57cec5SDimitry Andric
isProfitable(const FlowPattern & FP) const5050b57cec5SDimitry Andric bool HexagonEarlyIfConversion::isProfitable(const FlowPattern &FP) const {
5060b57cec5SDimitry Andric BranchProbability JumpProb(1, 10);
5070b57cec5SDimitry Andric BranchProbability Prob(9, 10);
5080b57cec5SDimitry Andric if (MBPI && FP.TrueB && !FP.FalseB &&
5090b57cec5SDimitry Andric (MBPI->getEdgeProbability(FP.SplitB, FP.TrueB) < JumpProb ||
5100b57cec5SDimitry Andric MBPI->getEdgeProbability(FP.SplitB, FP.TrueB) > Prob))
5110b57cec5SDimitry Andric return false;
5120b57cec5SDimitry Andric
5130b57cec5SDimitry Andric if (MBPI && !FP.TrueB && FP.FalseB &&
5140b57cec5SDimitry Andric (MBPI->getEdgeProbability(FP.SplitB, FP.FalseB) < JumpProb ||
5150b57cec5SDimitry Andric MBPI->getEdgeProbability(FP.SplitB, FP.FalseB) > Prob))
5160b57cec5SDimitry Andric return false;
5170b57cec5SDimitry Andric
5180b57cec5SDimitry Andric if (FP.TrueB && FP.FalseB) {
5190b57cec5SDimitry Andric // Do not IfCovert if the branch is one sided.
5200b57cec5SDimitry Andric if (MBPI) {
5210b57cec5SDimitry Andric if (MBPI->getEdgeProbability(FP.SplitB, FP.TrueB) > Prob)
5220b57cec5SDimitry Andric return false;
5230b57cec5SDimitry Andric if (MBPI->getEdgeProbability(FP.SplitB, FP.FalseB) > Prob)
5240b57cec5SDimitry Andric return false;
5250b57cec5SDimitry Andric }
5260b57cec5SDimitry Andric
5270b57cec5SDimitry Andric // If both sides are predicable, convert them if they join, and the
5280b57cec5SDimitry Andric // join block has no other predecessors.
5290b57cec5SDimitry Andric MachineBasicBlock *TSB = *FP.TrueB->succ_begin();
5300b57cec5SDimitry Andric MachineBasicBlock *FSB = *FP.FalseB->succ_begin();
5310b57cec5SDimitry Andric if (TSB != FSB)
5320b57cec5SDimitry Andric return false;
5330b57cec5SDimitry Andric if (TSB->pred_size() != 2)
5340b57cec5SDimitry Andric return false;
5350b57cec5SDimitry Andric }
5360b57cec5SDimitry Andric
5370b57cec5SDimitry Andric // Calculate the total size of the predicated blocks.
5380b57cec5SDimitry Andric // Assume instruction counts without branches to be the approximation of
5390b57cec5SDimitry Andric // the code size. If the predicated blocks are smaller than a packet size,
5400b57cec5SDimitry Andric // approximate the spare room in the packet that could be filled with the
5410b57cec5SDimitry Andric // predicated/speculated instructions.
5420b57cec5SDimitry Andric auto TotalCount = [] (const MachineBasicBlock *B, unsigned &Spare) {
5430b57cec5SDimitry Andric if (!B)
5440b57cec5SDimitry Andric return 0u;
5450b57cec5SDimitry Andric unsigned T = std::count_if(B->begin(), B->getFirstTerminator(),
5460b57cec5SDimitry Andric [](const MachineInstr &MI) {
5470b57cec5SDimitry Andric return !MI.isMetaInstruction();
5480b57cec5SDimitry Andric });
5490b57cec5SDimitry Andric if (T < HEXAGON_PACKET_SIZE)
5500b57cec5SDimitry Andric Spare += HEXAGON_PACKET_SIZE-T;
5510b57cec5SDimitry Andric return T;
5520b57cec5SDimitry Andric };
5530b57cec5SDimitry Andric unsigned Spare = 0;
5540b57cec5SDimitry Andric unsigned TotalIn = TotalCount(FP.TrueB, Spare) + TotalCount(FP.FalseB, Spare);
5550b57cec5SDimitry Andric LLVM_DEBUG(
5560b57cec5SDimitry Andric dbgs() << "Total number of instructions to be predicated/speculated: "
5570b57cec5SDimitry Andric << TotalIn << ", spare room: " << Spare << "\n");
5580b57cec5SDimitry Andric if (TotalIn >= SizeLimit+Spare)
5590b57cec5SDimitry Andric return false;
5600b57cec5SDimitry Andric
5610b57cec5SDimitry Andric // Count the number of PHI nodes that will need to be updated (converted
5620b57cec5SDimitry Andric // to MUX). Those can be later converted to predicated instructions, so
5630b57cec5SDimitry Andric // they aren't always adding extra cost.
5640b57cec5SDimitry Andric // KLUDGE: Also, count the number of predicate register definitions in
5650b57cec5SDimitry Andric // each block. The scheduler may increase the pressure of these and cause
5660b57cec5SDimitry Andric // expensive spills (e.g. bitmnp01).
5670b57cec5SDimitry Andric unsigned TotalPh = 0;
5680b57cec5SDimitry Andric unsigned PredDefs = countPredicateDefs(FP.SplitB);
5690b57cec5SDimitry Andric if (FP.JoinB) {
5700b57cec5SDimitry Andric TotalPh = computePhiCost(FP.JoinB, FP);
5710b57cec5SDimitry Andric PredDefs += countPredicateDefs(FP.JoinB);
5720b57cec5SDimitry Andric } else {
573349cc55cSDimitry Andric if (FP.TrueB && !FP.TrueB->succ_empty()) {
5740b57cec5SDimitry Andric MachineBasicBlock *SB = *FP.TrueB->succ_begin();
5750b57cec5SDimitry Andric TotalPh += computePhiCost(SB, FP);
5760b57cec5SDimitry Andric PredDefs += countPredicateDefs(SB);
5770b57cec5SDimitry Andric }
578349cc55cSDimitry Andric if (FP.FalseB && !FP.FalseB->succ_empty()) {
5790b57cec5SDimitry Andric MachineBasicBlock *SB = *FP.FalseB->succ_begin();
5800b57cec5SDimitry Andric TotalPh += computePhiCost(SB, FP);
5810b57cec5SDimitry Andric PredDefs += countPredicateDefs(SB);
5820b57cec5SDimitry Andric }
5830b57cec5SDimitry Andric }
5840b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Total number of extra muxes from converted phis: "
5850b57cec5SDimitry Andric << TotalPh << "\n");
5860b57cec5SDimitry Andric if (TotalIn+TotalPh >= SizeLimit+Spare)
5870b57cec5SDimitry Andric return false;
5880b57cec5SDimitry Andric
5890b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Total number of predicate registers: " << PredDefs
5900b57cec5SDimitry Andric << "\n");
5910b57cec5SDimitry Andric if (PredDefs > 4)
5920b57cec5SDimitry Andric return false;
5930b57cec5SDimitry Andric
5940b57cec5SDimitry Andric return true;
5950b57cec5SDimitry Andric }
5960b57cec5SDimitry Andric
visitBlock(MachineBasicBlock * B,MachineLoop * L)5970b57cec5SDimitry Andric bool HexagonEarlyIfConversion::visitBlock(MachineBasicBlock *B,
5980b57cec5SDimitry Andric MachineLoop *L) {
5990b57cec5SDimitry Andric bool Changed = false;
6000b57cec5SDimitry Andric
6010b57cec5SDimitry Andric // Visit all dominated blocks from the same loop first, then process B.
6020b57cec5SDimitry Andric MachineDomTreeNode *N = MDT->getNode(B);
6030b57cec5SDimitry Andric
6040b57cec5SDimitry Andric // We will change CFG/DT during this traversal, so take precautions to
6050b57cec5SDimitry Andric // avoid problems related to invalidated iterators. In fact, processing
6060b57cec5SDimitry Andric // a child C of B cannot cause another child to be removed, but it can
6070b57cec5SDimitry Andric // cause a new child to be added (which was a child of C before C itself
6080b57cec5SDimitry Andric // was removed. This new child C, however, would have been processed
6090b57cec5SDimitry Andric // prior to processing B, so there is no need to process it again.
6100b57cec5SDimitry Andric // Simply keep a list of children of B, and traverse that list.
6110b57cec5SDimitry Andric using DTNodeVectType = SmallVector<MachineDomTreeNode *, 4>;
6127a6dacacSDimitry Andric DTNodeVectType Cn(llvm::children<MachineDomTreeNode *>(N));
61304eeddc0SDimitry Andric for (auto &I : Cn) {
61404eeddc0SDimitry Andric MachineBasicBlock *SB = I->getBlock();
6150b57cec5SDimitry Andric if (!Deleted.count(SB))
6160b57cec5SDimitry Andric Changed |= visitBlock(SB, L);
6170b57cec5SDimitry Andric }
6180b57cec5SDimitry Andric // When walking down the dominator tree, we want to traverse through
6190b57cec5SDimitry Andric // blocks from nested (other) loops, because they can dominate blocks
6200b57cec5SDimitry Andric // that are in L. Skip the non-L blocks only after the tree traversal.
6210b57cec5SDimitry Andric if (MLI->getLoopFor(B) != L)
6220b57cec5SDimitry Andric return Changed;
6230b57cec5SDimitry Andric
6240b57cec5SDimitry Andric FlowPattern FP;
6250b57cec5SDimitry Andric if (!matchFlowPattern(B, L, FP))
6260b57cec5SDimitry Andric return Changed;
6270b57cec5SDimitry Andric
6280b57cec5SDimitry Andric if (!isValid(FP)) {
6290b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Conversion is not valid\n");
6300b57cec5SDimitry Andric return Changed;
6310b57cec5SDimitry Andric }
6320b57cec5SDimitry Andric if (!isProfitable(FP)) {
6330b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Conversion is not profitable\n");
6340b57cec5SDimitry Andric return Changed;
6350b57cec5SDimitry Andric }
6360b57cec5SDimitry Andric
6370b57cec5SDimitry Andric convert(FP);
6380b57cec5SDimitry Andric simplifyFlowGraph(FP);
6390b57cec5SDimitry Andric return true;
6400b57cec5SDimitry Andric }
6410b57cec5SDimitry Andric
visitLoop(MachineLoop * L)6420b57cec5SDimitry Andric bool HexagonEarlyIfConversion::visitLoop(MachineLoop *L) {
6430b57cec5SDimitry Andric MachineBasicBlock *HB = L ? L->getHeader() : nullptr;
6440b57cec5SDimitry Andric LLVM_DEBUG((L ? dbgs() << "Visiting loop H:" << PrintMB(HB)
6450b57cec5SDimitry Andric : dbgs() << "Visiting function")
6460b57cec5SDimitry Andric << "\n");
6470b57cec5SDimitry Andric bool Changed = false;
6480b57cec5SDimitry Andric if (L) {
64904eeddc0SDimitry Andric for (MachineLoop *I : *L)
65004eeddc0SDimitry Andric Changed |= visitLoop(I);
6510b57cec5SDimitry Andric }
6520b57cec5SDimitry Andric
6530b57cec5SDimitry Andric MachineBasicBlock *EntryB = GraphTraits<MachineFunction*>::getEntryNode(MFN);
6540b57cec5SDimitry Andric Changed |= visitBlock(L ? HB : EntryB, L);
6550b57cec5SDimitry Andric return Changed;
6560b57cec5SDimitry Andric }
6570b57cec5SDimitry Andric
isPredicableStore(const MachineInstr * MI) const6580b57cec5SDimitry Andric bool HexagonEarlyIfConversion::isPredicableStore(const MachineInstr *MI)
6590b57cec5SDimitry Andric const {
6600b57cec5SDimitry Andric // HexagonInstrInfo::isPredicable will consider these stores are non-
6610b57cec5SDimitry Andric // -predicable if the offset would become constant-extended after
6620b57cec5SDimitry Andric // predication.
6630b57cec5SDimitry Andric unsigned Opc = MI->getOpcode();
6640b57cec5SDimitry Andric switch (Opc) {
6650b57cec5SDimitry Andric case Hexagon::S2_storerb_io:
6660b57cec5SDimitry Andric case Hexagon::S2_storerbnew_io:
6670b57cec5SDimitry Andric case Hexagon::S2_storerh_io:
6680b57cec5SDimitry Andric case Hexagon::S2_storerhnew_io:
6690b57cec5SDimitry Andric case Hexagon::S2_storeri_io:
6700b57cec5SDimitry Andric case Hexagon::S2_storerinew_io:
6710b57cec5SDimitry Andric case Hexagon::S2_storerd_io:
6720b57cec5SDimitry Andric case Hexagon::S4_storeirb_io:
6730b57cec5SDimitry Andric case Hexagon::S4_storeirh_io:
6740b57cec5SDimitry Andric case Hexagon::S4_storeiri_io:
6750b57cec5SDimitry Andric return true;
6760b57cec5SDimitry Andric }
6770b57cec5SDimitry Andric
6780b57cec5SDimitry Andric // TargetInstrInfo::isPredicable takes a non-const pointer.
6790b57cec5SDimitry Andric return MI->mayStore() && HII->isPredicable(const_cast<MachineInstr&>(*MI));
6800b57cec5SDimitry Andric }
6810b57cec5SDimitry Andric
isSafeToSpeculate(const MachineInstr * MI) const6820b57cec5SDimitry Andric bool HexagonEarlyIfConversion::isSafeToSpeculate(const MachineInstr *MI)
6830b57cec5SDimitry Andric const {
684480093f4SDimitry Andric if (MI->mayLoadOrStore())
6850b57cec5SDimitry Andric return false;
6860b57cec5SDimitry Andric if (MI->isCall() || MI->isBarrier() || MI->isBranch())
6870b57cec5SDimitry Andric return false;
6880b57cec5SDimitry Andric if (MI->hasUnmodeledSideEffects())
6890b57cec5SDimitry Andric return false;
6900b57cec5SDimitry Andric if (MI->getOpcode() == TargetOpcode::LIFETIME_END)
6910b57cec5SDimitry Andric return false;
6920b57cec5SDimitry Andric
6930b57cec5SDimitry Andric return true;
6940b57cec5SDimitry Andric }
6950b57cec5SDimitry Andric
isPredicate(unsigned R) const6960b57cec5SDimitry Andric bool HexagonEarlyIfConversion::isPredicate(unsigned R) const {
6970b57cec5SDimitry Andric const TargetRegisterClass *RC = MRI->getRegClass(R);
6980b57cec5SDimitry Andric return RC == &Hexagon::PredRegsRegClass ||
6990b57cec5SDimitry Andric RC == &Hexagon::HvxQRRegClass;
7000b57cec5SDimitry Andric }
7010b57cec5SDimitry Andric
getCondStoreOpcode(unsigned Opc,bool IfTrue) const7020b57cec5SDimitry Andric unsigned HexagonEarlyIfConversion::getCondStoreOpcode(unsigned Opc,
7030b57cec5SDimitry Andric bool IfTrue) const {
7040b57cec5SDimitry Andric return HII->getCondOpcode(Opc, !IfTrue);
7050b57cec5SDimitry Andric }
7060b57cec5SDimitry Andric
predicateInstr(MachineBasicBlock * ToB,MachineBasicBlock::iterator At,MachineInstr * MI,unsigned PredR,bool IfTrue)7070b57cec5SDimitry Andric void HexagonEarlyIfConversion::predicateInstr(MachineBasicBlock *ToB,
7080b57cec5SDimitry Andric MachineBasicBlock::iterator At, MachineInstr *MI,
7090b57cec5SDimitry Andric unsigned PredR, bool IfTrue) {
7100b57cec5SDimitry Andric DebugLoc DL;
7110b57cec5SDimitry Andric if (At != ToB->end())
7120b57cec5SDimitry Andric DL = At->getDebugLoc();
7130b57cec5SDimitry Andric else if (!ToB->empty())
7140b57cec5SDimitry Andric DL = ToB->back().getDebugLoc();
7150b57cec5SDimitry Andric
7160b57cec5SDimitry Andric unsigned Opc = MI->getOpcode();
7170b57cec5SDimitry Andric
7180b57cec5SDimitry Andric if (isPredicableStore(MI)) {
7190b57cec5SDimitry Andric unsigned COpc = getCondStoreOpcode(Opc, IfTrue);
7200b57cec5SDimitry Andric assert(COpc);
7210b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(*ToB, At, DL, HII->get(COpc));
7220b57cec5SDimitry Andric MachineInstr::mop_iterator MOI = MI->operands_begin();
7230b57cec5SDimitry Andric if (HII->isPostIncrement(*MI)) {
7240b57cec5SDimitry Andric MIB.add(*MOI);
7250b57cec5SDimitry Andric ++MOI;
7260b57cec5SDimitry Andric }
7270b57cec5SDimitry Andric MIB.addReg(PredR);
7280b57cec5SDimitry Andric for (const MachineOperand &MO : make_range(MOI, MI->operands_end()))
7290b57cec5SDimitry Andric MIB.add(MO);
7300b57cec5SDimitry Andric
7310b57cec5SDimitry Andric // Set memory references.
7320b57cec5SDimitry Andric MIB.cloneMemRefs(*MI);
7330b57cec5SDimitry Andric
7340b57cec5SDimitry Andric MI->eraseFromParent();
7350b57cec5SDimitry Andric return;
7360b57cec5SDimitry Andric }
7370b57cec5SDimitry Andric
7380b57cec5SDimitry Andric if (Opc == Hexagon::J2_jump) {
7390b57cec5SDimitry Andric MachineBasicBlock *TB = MI->getOperand(0).getMBB();
7400b57cec5SDimitry Andric const MCInstrDesc &D = HII->get(IfTrue ? Hexagon::J2_jumpt
7410b57cec5SDimitry Andric : Hexagon::J2_jumpf);
7420b57cec5SDimitry Andric BuildMI(*ToB, At, DL, D)
7430b57cec5SDimitry Andric .addReg(PredR)
7440b57cec5SDimitry Andric .addMBB(TB);
7450b57cec5SDimitry Andric MI->eraseFromParent();
7460b57cec5SDimitry Andric return;
7470b57cec5SDimitry Andric }
7480b57cec5SDimitry Andric
7490b57cec5SDimitry Andric // Print the offending instruction unconditionally as we are about to
7500b57cec5SDimitry Andric // abort.
7510b57cec5SDimitry Andric dbgs() << *MI;
7520b57cec5SDimitry Andric llvm_unreachable("Unexpected instruction");
7530b57cec5SDimitry Andric }
7540b57cec5SDimitry Andric
7550b57cec5SDimitry Andric // Predicate/speculate non-branch instructions from FromB into block ToB.
7560b57cec5SDimitry Andric // Leave the branches alone, they will be handled later. Btw, at this point
7570b57cec5SDimitry Andric // FromB should have at most one branch, and it should be unconditional.
predicateBlockNB(MachineBasicBlock * ToB,MachineBasicBlock::iterator At,MachineBasicBlock * FromB,unsigned PredR,bool IfTrue)7580b57cec5SDimitry Andric void HexagonEarlyIfConversion::predicateBlockNB(MachineBasicBlock *ToB,
7590b57cec5SDimitry Andric MachineBasicBlock::iterator At, MachineBasicBlock *FromB,
7600b57cec5SDimitry Andric unsigned PredR, bool IfTrue) {
7610b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Predicating block " << PrintMB(FromB) << "\n");
7620b57cec5SDimitry Andric MachineBasicBlock::iterator End = FromB->getFirstTerminator();
7630b57cec5SDimitry Andric MachineBasicBlock::iterator I, NextI;
7640b57cec5SDimitry Andric
7650b57cec5SDimitry Andric for (I = FromB->begin(); I != End; I = NextI) {
7660b57cec5SDimitry Andric assert(!I->isPHI());
7670b57cec5SDimitry Andric NextI = std::next(I);
7680b57cec5SDimitry Andric if (isSafeToSpeculate(&*I))
7690b57cec5SDimitry Andric ToB->splice(At, FromB, I);
7700b57cec5SDimitry Andric else
7710b57cec5SDimitry Andric predicateInstr(ToB, At, &*I, PredR, IfTrue);
7720b57cec5SDimitry Andric }
7730b57cec5SDimitry Andric }
7740b57cec5SDimitry Andric
buildMux(MachineBasicBlock * B,MachineBasicBlock::iterator At,const TargetRegisterClass * DRC,unsigned PredR,unsigned TR,unsigned TSR,unsigned FR,unsigned FSR)7750b57cec5SDimitry Andric unsigned HexagonEarlyIfConversion::buildMux(MachineBasicBlock *B,
7760b57cec5SDimitry Andric MachineBasicBlock::iterator At, const TargetRegisterClass *DRC,
7770b57cec5SDimitry Andric unsigned PredR, unsigned TR, unsigned TSR, unsigned FR, unsigned FSR) {
7780b57cec5SDimitry Andric unsigned Opc = 0;
7790b57cec5SDimitry Andric switch (DRC->getID()) {
7800b57cec5SDimitry Andric case Hexagon::IntRegsRegClassID:
7810b57cec5SDimitry Andric case Hexagon::IntRegsLow8RegClassID:
7820b57cec5SDimitry Andric Opc = Hexagon::C2_mux;
7830b57cec5SDimitry Andric break;
7840b57cec5SDimitry Andric case Hexagon::DoubleRegsRegClassID:
7850b57cec5SDimitry Andric case Hexagon::GeneralDoubleLow8RegsRegClassID:
7860b57cec5SDimitry Andric Opc = Hexagon::PS_pselect;
7870b57cec5SDimitry Andric break;
7880b57cec5SDimitry Andric case Hexagon::HvxVRRegClassID:
7890b57cec5SDimitry Andric Opc = Hexagon::PS_vselect;
7900b57cec5SDimitry Andric break;
7910b57cec5SDimitry Andric case Hexagon::HvxWRRegClassID:
7920b57cec5SDimitry Andric Opc = Hexagon::PS_wselect;
7930b57cec5SDimitry Andric break;
7940b57cec5SDimitry Andric default:
7950b57cec5SDimitry Andric llvm_unreachable("unexpected register type");
7960b57cec5SDimitry Andric }
7970b57cec5SDimitry Andric const MCInstrDesc &D = HII->get(Opc);
7980b57cec5SDimitry Andric
7990b57cec5SDimitry Andric DebugLoc DL = B->findBranchDebugLoc();
8008bcb0991SDimitry Andric Register MuxR = MRI->createVirtualRegister(DRC);
8010b57cec5SDimitry Andric BuildMI(*B, At, DL, D, MuxR)
8020b57cec5SDimitry Andric .addReg(PredR)
8030b57cec5SDimitry Andric .addReg(TR, 0, TSR)
8040b57cec5SDimitry Andric .addReg(FR, 0, FSR);
8050b57cec5SDimitry Andric return MuxR;
8060b57cec5SDimitry Andric }
8070b57cec5SDimitry Andric
updatePhiNodes(MachineBasicBlock * WhereB,const FlowPattern & FP)8080b57cec5SDimitry Andric void HexagonEarlyIfConversion::updatePhiNodes(MachineBasicBlock *WhereB,
8090b57cec5SDimitry Andric const FlowPattern &FP) {
8100b57cec5SDimitry Andric // Visit all PHI nodes in the WhereB block and generate MUX instructions
8110b57cec5SDimitry Andric // in the split block. Update the PHI nodes with the values of the MUX.
8120b57cec5SDimitry Andric auto NonPHI = WhereB->getFirstNonPHI();
8130b57cec5SDimitry Andric for (auto I = WhereB->begin(); I != NonPHI; ++I) {
8140b57cec5SDimitry Andric MachineInstr *PN = &*I;
8150b57cec5SDimitry Andric // Registers and subregisters corresponding to TrueB, FalseB and SplitB.
8160b57cec5SDimitry Andric unsigned TR = 0, TSR = 0, FR = 0, FSR = 0, SR = 0, SSR = 0;
8170b57cec5SDimitry Andric for (int i = PN->getNumOperands()-2; i > 0; i -= 2) {
8180b57cec5SDimitry Andric const MachineOperand &RO = PN->getOperand(i), &BO = PN->getOperand(i+1);
8190b57cec5SDimitry Andric if (BO.getMBB() == FP.SplitB)
8200b57cec5SDimitry Andric SR = RO.getReg(), SSR = RO.getSubReg();
8210b57cec5SDimitry Andric else if (BO.getMBB() == FP.TrueB)
8220b57cec5SDimitry Andric TR = RO.getReg(), TSR = RO.getSubReg();
8230b57cec5SDimitry Andric else if (BO.getMBB() == FP.FalseB)
8240b57cec5SDimitry Andric FR = RO.getReg(), FSR = RO.getSubReg();
8250b57cec5SDimitry Andric else
8260b57cec5SDimitry Andric continue;
82781ad6265SDimitry Andric PN->removeOperand(i+1);
82881ad6265SDimitry Andric PN->removeOperand(i);
8290b57cec5SDimitry Andric }
8300b57cec5SDimitry Andric if (TR == 0)
8310b57cec5SDimitry Andric TR = SR, TSR = SSR;
8320b57cec5SDimitry Andric else if (FR == 0)
8330b57cec5SDimitry Andric FR = SR, FSR = SSR;
8340b57cec5SDimitry Andric
8350b57cec5SDimitry Andric assert(TR || FR);
8360b57cec5SDimitry Andric unsigned MuxR = 0, MuxSR = 0;
8370b57cec5SDimitry Andric
8380b57cec5SDimitry Andric if (TR && FR) {
8398bcb0991SDimitry Andric Register DR = PN->getOperand(0).getReg();
8400b57cec5SDimitry Andric const TargetRegisterClass *RC = MRI->getRegClass(DR);
8410b57cec5SDimitry Andric MuxR = buildMux(FP.SplitB, FP.SplitB->getFirstTerminator(), RC,
8420b57cec5SDimitry Andric FP.PredR, TR, TSR, FR, FSR);
8430b57cec5SDimitry Andric } else if (TR) {
8440b57cec5SDimitry Andric MuxR = TR;
8450b57cec5SDimitry Andric MuxSR = TSR;
8460b57cec5SDimitry Andric } else {
8470b57cec5SDimitry Andric MuxR = FR;
8480b57cec5SDimitry Andric MuxSR = FSR;
8490b57cec5SDimitry Andric }
8500b57cec5SDimitry Andric
8510b57cec5SDimitry Andric PN->addOperand(MachineOperand::CreateReg(MuxR, false, false, false, false,
8520b57cec5SDimitry Andric false, false, MuxSR));
8530b57cec5SDimitry Andric PN->addOperand(MachineOperand::CreateMBB(FP.SplitB));
8540b57cec5SDimitry Andric }
8550b57cec5SDimitry Andric }
8560b57cec5SDimitry Andric
convert(const FlowPattern & FP)8570b57cec5SDimitry Andric void HexagonEarlyIfConversion::convert(const FlowPattern &FP) {
8580b57cec5SDimitry Andric MachineBasicBlock *TSB = nullptr, *FSB = nullptr;
8590b57cec5SDimitry Andric MachineBasicBlock::iterator OldTI = FP.SplitB->getFirstTerminator();
8600b57cec5SDimitry Andric assert(OldTI != FP.SplitB->end());
8610b57cec5SDimitry Andric DebugLoc DL = OldTI->getDebugLoc();
8620b57cec5SDimitry Andric
8630b57cec5SDimitry Andric if (FP.TrueB) {
8640b57cec5SDimitry Andric TSB = *FP.TrueB->succ_begin();
8650b57cec5SDimitry Andric predicateBlockNB(FP.SplitB, OldTI, FP.TrueB, FP.PredR, true);
8660b57cec5SDimitry Andric }
8670b57cec5SDimitry Andric if (FP.FalseB) {
8680b57cec5SDimitry Andric FSB = *FP.FalseB->succ_begin();
8690b57cec5SDimitry Andric MachineBasicBlock::iterator At = FP.SplitB->getFirstTerminator();
8700b57cec5SDimitry Andric predicateBlockNB(FP.SplitB, At, FP.FalseB, FP.PredR, false);
8710b57cec5SDimitry Andric }
8720b57cec5SDimitry Andric
8730b57cec5SDimitry Andric // Regenerate new terminators in the split block and update the successors.
8740b57cec5SDimitry Andric // First, remember any information that may be needed later and remove the
8750b57cec5SDimitry Andric // existing terminators/successors from the split block.
8760b57cec5SDimitry Andric MachineBasicBlock *SSB = nullptr;
8770b57cec5SDimitry Andric FP.SplitB->erase(OldTI, FP.SplitB->end());
878349cc55cSDimitry Andric while (!FP.SplitB->succ_empty()) {
8790b57cec5SDimitry Andric MachineBasicBlock *T = *FP.SplitB->succ_begin();
8800b57cec5SDimitry Andric // It's possible that the split block had a successor that is not a pre-
8810b57cec5SDimitry Andric // dicated block. This could only happen if there was only one block to
8820b57cec5SDimitry Andric // be predicated. Example:
8830b57cec5SDimitry Andric // split_b:
8840b57cec5SDimitry Andric // if (p) jump true_b
8850b57cec5SDimitry Andric // jump unrelated2_b
8860b57cec5SDimitry Andric // unrelated1_b:
8870b57cec5SDimitry Andric // ...
8880b57cec5SDimitry Andric // unrelated2_b: ; can have other predecessors, so it's not "false_b"
8890b57cec5SDimitry Andric // jump other_b
8900b57cec5SDimitry Andric // true_b: ; only reachable from split_b, can be predicated
8910b57cec5SDimitry Andric // ...
8920b57cec5SDimitry Andric //
8930b57cec5SDimitry Andric // Find this successor (SSB) if it exists.
8940b57cec5SDimitry Andric if (T != FP.TrueB && T != FP.FalseB) {
8950b57cec5SDimitry Andric assert(!SSB);
8960b57cec5SDimitry Andric SSB = T;
8970b57cec5SDimitry Andric }
8980b57cec5SDimitry Andric FP.SplitB->removeSuccessor(FP.SplitB->succ_begin());
8990b57cec5SDimitry Andric }
9000b57cec5SDimitry Andric
9010b57cec5SDimitry Andric // Insert new branches and update the successors of the split block. This
9020b57cec5SDimitry Andric // may create unconditional branches to the layout successor, etc., but
9030b57cec5SDimitry Andric // that will be cleaned up later. For now, make sure that correct code is
9040b57cec5SDimitry Andric // generated.
9050b57cec5SDimitry Andric if (FP.JoinB) {
9060b57cec5SDimitry Andric assert(!SSB || SSB == FP.JoinB);
9070b57cec5SDimitry Andric BuildMI(*FP.SplitB, FP.SplitB->end(), DL, HII->get(Hexagon::J2_jump))
9080b57cec5SDimitry Andric .addMBB(FP.JoinB);
9090b57cec5SDimitry Andric FP.SplitB->addSuccessor(FP.JoinB);
9100b57cec5SDimitry Andric } else {
9110b57cec5SDimitry Andric bool HasBranch = false;
9120b57cec5SDimitry Andric if (TSB) {
9130b57cec5SDimitry Andric BuildMI(*FP.SplitB, FP.SplitB->end(), DL, HII->get(Hexagon::J2_jumpt))
9140b57cec5SDimitry Andric .addReg(FP.PredR)
9150b57cec5SDimitry Andric .addMBB(TSB);
9160b57cec5SDimitry Andric FP.SplitB->addSuccessor(TSB);
9170b57cec5SDimitry Andric HasBranch = true;
9180b57cec5SDimitry Andric }
9190b57cec5SDimitry Andric if (FSB) {
9200b57cec5SDimitry Andric const MCInstrDesc &D = HasBranch ? HII->get(Hexagon::J2_jump)
9210b57cec5SDimitry Andric : HII->get(Hexagon::J2_jumpf);
9220b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(*FP.SplitB, FP.SplitB->end(), DL, D);
9230b57cec5SDimitry Andric if (!HasBranch)
9240b57cec5SDimitry Andric MIB.addReg(FP.PredR);
9250b57cec5SDimitry Andric MIB.addMBB(FSB);
9260b57cec5SDimitry Andric FP.SplitB->addSuccessor(FSB);
9270b57cec5SDimitry Andric }
9280b57cec5SDimitry Andric if (SSB) {
9290b57cec5SDimitry Andric // This cannot happen if both TSB and FSB are set. [TF]SB are the
9300b57cec5SDimitry Andric // successor blocks of the TrueB and FalseB (or null of the TrueB
9310b57cec5SDimitry Andric // or FalseB block is null). SSB is the potential successor block
9320b57cec5SDimitry Andric // of the SplitB that is neither TrueB nor FalseB.
9330b57cec5SDimitry Andric BuildMI(*FP.SplitB, FP.SplitB->end(), DL, HII->get(Hexagon::J2_jump))
9340b57cec5SDimitry Andric .addMBB(SSB);
9350b57cec5SDimitry Andric FP.SplitB->addSuccessor(SSB);
9360b57cec5SDimitry Andric }
9370b57cec5SDimitry Andric }
9380b57cec5SDimitry Andric
9390b57cec5SDimitry Andric // What is left to do is to update the PHI nodes that could have entries
9400b57cec5SDimitry Andric // referring to predicated blocks.
9410b57cec5SDimitry Andric if (FP.JoinB) {
9420b57cec5SDimitry Andric updatePhiNodes(FP.JoinB, FP);
9430b57cec5SDimitry Andric } else {
9440b57cec5SDimitry Andric if (TSB)
9450b57cec5SDimitry Andric updatePhiNodes(TSB, FP);
9460b57cec5SDimitry Andric if (FSB)
9470b57cec5SDimitry Andric updatePhiNodes(FSB, FP);
9480b57cec5SDimitry Andric // Nothing to update in SSB, since SSB's predecessors haven't changed.
9490b57cec5SDimitry Andric }
9500b57cec5SDimitry Andric }
9510b57cec5SDimitry Andric
removeBlock(MachineBasicBlock * B)9520b57cec5SDimitry Andric void HexagonEarlyIfConversion::removeBlock(MachineBasicBlock *B) {
9530b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Removing block " << PrintMB(B) << "\n");
9540b57cec5SDimitry Andric
9550b57cec5SDimitry Andric // Transfer the immediate dominator information from B to its descendants.
9560b57cec5SDimitry Andric MachineDomTreeNode *N = MDT->getNode(B);
9570b57cec5SDimitry Andric MachineDomTreeNode *IDN = N->getIDom();
9580b57cec5SDimitry Andric if (IDN) {
9590b57cec5SDimitry Andric MachineBasicBlock *IDB = IDN->getBlock();
9600b57cec5SDimitry Andric
9610b57cec5SDimitry Andric using GTN = GraphTraits<MachineDomTreeNode *>;
9620b57cec5SDimitry Andric using DTNodeVectType = SmallVector<MachineDomTreeNode *, 4>;
9630b57cec5SDimitry Andric
9640b57cec5SDimitry Andric DTNodeVectType Cn(GTN::child_begin(N), GTN::child_end(N));
96504eeddc0SDimitry Andric for (auto &I : Cn) {
96604eeddc0SDimitry Andric MachineBasicBlock *SB = I->getBlock();
9670b57cec5SDimitry Andric MDT->changeImmediateDominator(SB, IDB);
9680b57cec5SDimitry Andric }
9690b57cec5SDimitry Andric }
9700b57cec5SDimitry Andric
971349cc55cSDimitry Andric while (!B->succ_empty())
9720b57cec5SDimitry Andric B->removeSuccessor(B->succ_begin());
9730b57cec5SDimitry Andric
97404eeddc0SDimitry Andric for (MachineBasicBlock *Pred : B->predecessors())
97504eeddc0SDimitry Andric Pred->removeSuccessor(B, true);
9760b57cec5SDimitry Andric
9770b57cec5SDimitry Andric Deleted.insert(B);
9780b57cec5SDimitry Andric MDT->eraseNode(B);
9790b57cec5SDimitry Andric MFN->erase(B->getIterator());
9800b57cec5SDimitry Andric }
9810b57cec5SDimitry Andric
eliminatePhis(MachineBasicBlock * B)9820b57cec5SDimitry Andric void HexagonEarlyIfConversion::eliminatePhis(MachineBasicBlock *B) {
9830b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Removing phi nodes from block " << PrintMB(B) << "\n");
9840b57cec5SDimitry Andric MachineBasicBlock::iterator I, NextI, NonPHI = B->getFirstNonPHI();
9850b57cec5SDimitry Andric for (I = B->begin(); I != NonPHI; I = NextI) {
9860b57cec5SDimitry Andric NextI = std::next(I);
9870b57cec5SDimitry Andric MachineInstr *PN = &*I;
9880b57cec5SDimitry Andric assert(PN->getNumOperands() == 3 && "Invalid phi node");
9890b57cec5SDimitry Andric MachineOperand &UO = PN->getOperand(1);
9908bcb0991SDimitry Andric Register UseR = UO.getReg(), UseSR = UO.getSubReg();
9918bcb0991SDimitry Andric Register DefR = PN->getOperand(0).getReg();
9920b57cec5SDimitry Andric unsigned NewR = UseR;
9930b57cec5SDimitry Andric if (UseSR) {
9940b57cec5SDimitry Andric // MRI.replaceVregUsesWith does not allow to update the subregister,
9950b57cec5SDimitry Andric // so instead of doing the use-iteration here, create a copy into a
9960b57cec5SDimitry Andric // "non-subregistered" register.
9970b57cec5SDimitry Andric const DebugLoc &DL = PN->getDebugLoc();
9980b57cec5SDimitry Andric const TargetRegisterClass *RC = MRI->getRegClass(DefR);
9990b57cec5SDimitry Andric NewR = MRI->createVirtualRegister(RC);
10000b57cec5SDimitry Andric NonPHI = BuildMI(*B, NonPHI, DL, HII->get(TargetOpcode::COPY), NewR)
10010b57cec5SDimitry Andric .addReg(UseR, 0, UseSR);
10020b57cec5SDimitry Andric }
10030b57cec5SDimitry Andric MRI->replaceRegWith(DefR, NewR);
10040b57cec5SDimitry Andric B->erase(I);
10050b57cec5SDimitry Andric }
10060b57cec5SDimitry Andric }
10070b57cec5SDimitry Andric
mergeBlocks(MachineBasicBlock * PredB,MachineBasicBlock * SuccB)10080b57cec5SDimitry Andric void HexagonEarlyIfConversion::mergeBlocks(MachineBasicBlock *PredB,
10090b57cec5SDimitry Andric MachineBasicBlock *SuccB) {
10100b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Merging blocks " << PrintMB(PredB) << " and "
10110b57cec5SDimitry Andric << PrintMB(SuccB) << "\n");
10120b57cec5SDimitry Andric bool TermOk = hasUncondBranch(SuccB);
10130b57cec5SDimitry Andric eliminatePhis(SuccB);
10140b57cec5SDimitry Andric HII->removeBranch(*PredB);
10150b57cec5SDimitry Andric PredB->removeSuccessor(SuccB);
10160b57cec5SDimitry Andric PredB->splice(PredB->end(), SuccB, SuccB->begin(), SuccB->end());
10170b57cec5SDimitry Andric PredB->transferSuccessorsAndUpdatePHIs(SuccB);
10185ffd83dbSDimitry Andric MachineBasicBlock *OldLayoutSuccessor = SuccB->getNextNode();
10190b57cec5SDimitry Andric removeBlock(SuccB);
10200b57cec5SDimitry Andric if (!TermOk)
10215ffd83dbSDimitry Andric PredB->updateTerminator(OldLayoutSuccessor);
10220b57cec5SDimitry Andric }
10230b57cec5SDimitry Andric
simplifyFlowGraph(const FlowPattern & FP)10240b57cec5SDimitry Andric void HexagonEarlyIfConversion::simplifyFlowGraph(const FlowPattern &FP) {
10255ffd83dbSDimitry Andric MachineBasicBlock *OldLayoutSuccessor = FP.SplitB->getNextNode();
10260b57cec5SDimitry Andric if (FP.TrueB)
10270b57cec5SDimitry Andric removeBlock(FP.TrueB);
10280b57cec5SDimitry Andric if (FP.FalseB)
10290b57cec5SDimitry Andric removeBlock(FP.FalseB);
10300b57cec5SDimitry Andric
10315ffd83dbSDimitry Andric FP.SplitB->updateTerminator(OldLayoutSuccessor);
10320b57cec5SDimitry Andric if (FP.SplitB->succ_size() != 1)
10330b57cec5SDimitry Andric return;
10340b57cec5SDimitry Andric
10350b57cec5SDimitry Andric MachineBasicBlock *SB = *FP.SplitB->succ_begin();
10360b57cec5SDimitry Andric if (SB->pred_size() != 1)
10370b57cec5SDimitry Andric return;
10380b57cec5SDimitry Andric
10390b57cec5SDimitry Andric // By now, the split block has only one successor (SB), and SB has only
10400b57cec5SDimitry Andric // one predecessor. We can try to merge them. We will need to update ter-
10410b57cec5SDimitry Andric // minators in FP.Split+SB, and that requires working analyzeBranch, which
10420b57cec5SDimitry Andric // fails on Hexagon for blocks that have EH_LABELs. However, if SB ends
10430b57cec5SDimitry Andric // with an unconditional branch, we won't need to touch the terminators.
10440b57cec5SDimitry Andric if (!hasEHLabel(SB) || hasUncondBranch(SB))
10450b57cec5SDimitry Andric mergeBlocks(FP.SplitB, SB);
10460b57cec5SDimitry Andric }
10470b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & MF)10480b57cec5SDimitry Andric bool HexagonEarlyIfConversion::runOnMachineFunction(MachineFunction &MF) {
10490b57cec5SDimitry Andric if (skipFunction(MF.getFunction()))
10500b57cec5SDimitry Andric return false;
10510b57cec5SDimitry Andric
10520b57cec5SDimitry Andric auto &ST = MF.getSubtarget<HexagonSubtarget>();
10530b57cec5SDimitry Andric HII = ST.getInstrInfo();
10540b57cec5SDimitry Andric TRI = ST.getRegisterInfo();
10550b57cec5SDimitry Andric MFN = &MF;
10560b57cec5SDimitry Andric MRI = &MF.getRegInfo();
1057*0fca6ea1SDimitry Andric MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
1058*0fca6ea1SDimitry Andric MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
1059*0fca6ea1SDimitry Andric MBPI = EnableHexagonBP
1060*0fca6ea1SDimitry Andric ? &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI()
1061*0fca6ea1SDimitry Andric : nullptr;
10620b57cec5SDimitry Andric
10630b57cec5SDimitry Andric Deleted.clear();
10640b57cec5SDimitry Andric bool Changed = false;
10650b57cec5SDimitry Andric
106604eeddc0SDimitry Andric for (MachineLoop *L : *MLI)
106704eeddc0SDimitry Andric Changed |= visitLoop(L);
10680b57cec5SDimitry Andric Changed |= visitLoop(nullptr);
10690b57cec5SDimitry Andric
10700b57cec5SDimitry Andric return Changed;
10710b57cec5SDimitry Andric }
10720b57cec5SDimitry Andric
10730b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
10740b57cec5SDimitry Andric // Public Constructor Functions
10750b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
createHexagonEarlyIfConversion()10760b57cec5SDimitry Andric FunctionPass *llvm::createHexagonEarlyIfConversion() {
10770b57cec5SDimitry Andric return new HexagonEarlyIfConversion();
10780b57cec5SDimitry Andric }
1079