2012-07-04 00:09:54 +00:00
|
|
|
//===-- EarlyIfConversion.cpp - If-conversion on SSA form machine code ----===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00: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
|
2012-07-04 00:09:54 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Early if-conversion is for out-of-order CPUs that don't have a lot of
|
|
|
|
// predicable instructions. The goal is to eliminate conditional branches that
|
|
|
|
// may mispredict.
|
|
|
|
//
|
|
|
|
// Instructions from both sides of the branch are executed specutatively, and a
|
|
|
|
// cmov instruction selects the result.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2024-10-16 13:22:57 +05:30
|
|
|
#include "llvm/CodeGen/EarlyIfConversion.h"
|
2012-07-04 00:09:54 +00:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2012-07-10 22:18:23 +00:00
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
2012-07-04 00:09:54 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/SparseSet.h"
|
2012-08-13 21:03:27 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2022-03-15 10:54:19 +01:00
|
|
|
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
|
2012-07-04 00:09:54 +00:00
|
|
|
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
|
2012-07-10 22:18:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
2012-07-04 00:09:54 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2019-08-20 15:54:59 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2012-07-10 22:39:56 +00:00
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2020-08-28 15:22:32 -06:00
|
|
|
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
|
2012-07-04 00:09:54 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2013-01-17 01:06:04 +00:00
|
|
|
#include "llvm/CodeGen/MachineTraceMetrics.h"
|
2017-11-08 01:01:31 +00:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 01:07:10 +00:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 13:15:01 -08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2012-07-04 00:09:54 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:02:50 +00:00
|
|
|
#define DEBUG_TYPE "early-ifcvt"
|
|
|
|
|
2012-07-04 00:09:54 +00:00
|
|
|
// Absolute maximum number of instructions allowed per speculated block.
|
|
|
|
// This bypasses all other heuristics, so it should be set fairly high.
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
BlockInstrLimit("early-ifcvt-limit", cl::init(30), cl::Hidden,
|
|
|
|
cl::desc("Maximum number of instructions per speculated block."));
|
|
|
|
|
|
|
|
// Stress testing mode - disable heuristics.
|
|
|
|
static cl::opt<bool> Stress("stress-early-ifcvt", cl::Hidden,
|
|
|
|
cl::desc("Turn all knobs to 11"));
|
|
|
|
|
2012-08-13 21:03:27 +00:00
|
|
|
STATISTIC(NumDiamondsSeen, "Number of diamonds");
|
|
|
|
STATISTIC(NumDiamondsConv, "Number of diamonds converted");
|
|
|
|
STATISTIC(NumTrianglesSeen, "Number of triangles");
|
|
|
|
STATISTIC(NumTrianglesConv, "Number of triangles converted");
|
|
|
|
|
2012-07-04 00:09:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SSAIfConv
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The SSAIfConv class performs if-conversion on SSA form machine code after
|
2012-07-04 01:09:45 +00:00
|
|
|
// determining if it is possible. The class contains no heuristics; external
|
2012-07-04 00:09:54 +00:00
|
|
|
// code should be used to determine when if-conversion is a good idea.
|
|
|
|
//
|
2012-07-04 01:09:45 +00:00
|
|
|
// SSAIfConv can convert both triangles and diamonds:
|
2012-07-04 00:09:54 +00:00
|
|
|
//
|
|
|
|
// Triangle: Head Diamond: Head
|
2012-07-04 01:09:45 +00:00
|
|
|
// | \ / \_
|
|
|
|
// | \ / |
|
2012-07-04 00:09:54 +00:00
|
|
|
// | [TF]BB FBB TBB
|
|
|
|
// | / \ /
|
|
|
|
// | / \ /
|
|
|
|
// Tail Tail
|
|
|
|
//
|
|
|
|
// Instructions in the conditional blocks TBB and/or FBB are spliced into the
|
2012-07-04 01:09:45 +00:00
|
|
|
// Head block, and phis in the Tail block are converted to select instructions.
|
2012-07-04 00:09:54 +00:00
|
|
|
//
|
|
|
|
namespace {
|
|
|
|
class SSAIfConv {
|
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
MachineRegisterInfo *MRI;
|
|
|
|
|
2012-07-10 22:18:23 +00:00
|
|
|
public:
|
2012-07-04 00:09:54 +00:00
|
|
|
/// The block containing the conditional branch.
|
|
|
|
MachineBasicBlock *Head;
|
|
|
|
|
|
|
|
/// The block containing phis after the if-then-else.
|
|
|
|
MachineBasicBlock *Tail;
|
|
|
|
|
2020-01-21 09:47:35 -06:00
|
|
|
/// The 'true' conditional block as determined by analyzeBranch.
|
2012-07-04 00:09:54 +00:00
|
|
|
MachineBasicBlock *TBB;
|
|
|
|
|
2020-01-21 09:47:35 -06:00
|
|
|
/// The 'false' conditional block as determined by analyzeBranch.
|
2012-07-04 00:09:54 +00:00
|
|
|
MachineBasicBlock *FBB;
|
|
|
|
|
|
|
|
/// isTriangle - When there is no 'else' block, either TBB or FBB will be
|
|
|
|
/// equal to Tail.
|
|
|
|
bool isTriangle() const { return TBB == Tail || FBB == Tail; }
|
|
|
|
|
2012-08-10 20:19:17 +00:00
|
|
|
/// Returns the Tail predecessor for the True side.
|
|
|
|
MachineBasicBlock *getTPred() const { return TBB == Tail ? Head : TBB; }
|
|
|
|
|
|
|
|
/// Returns the Tail predecessor for the False side.
|
|
|
|
MachineBasicBlock *getFPred() const { return FBB == Tail ? Head : FBB; }
|
|
|
|
|
2012-07-04 00:09:54 +00:00
|
|
|
/// Information about each phi in the Tail block.
|
|
|
|
struct PHIInfo {
|
|
|
|
MachineInstr *PHI;
|
2022-01-30 12:32:51 -08:00
|
|
|
unsigned TReg = 0, FReg = 0;
|
2012-07-04 00:09:54 +00:00
|
|
|
// Latencies from Cond+Branch, TReg, and FReg to DstReg.
|
2022-01-30 12:32:51 -08:00
|
|
|
int CondCycles = 0, TCycles = 0, FCycles = 0;
|
2012-07-04 00:09:54 +00:00
|
|
|
|
2022-01-30 12:32:51 -08:00
|
|
|
PHIInfo(MachineInstr *phi) : PHI(phi) {}
|
2012-07-04 00:09:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SmallVector<PHIInfo, 8> PHIs;
|
|
|
|
|
2020-01-21 09:47:35 -06:00
|
|
|
/// The branch condition determined by analyzeBranch.
|
2012-07-10 22:18:23 +00:00
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
|
|
|
|
2023-05-12 19:21:03 +01:00
|
|
|
private:
|
2012-07-04 00:09:54 +00:00
|
|
|
/// Instructions in Head that define values used by the conditional blocks.
|
|
|
|
/// The hoisted instructions must be inserted after these instructions.
|
|
|
|
SmallPtrSet<MachineInstr*, 8> InsertAfter;
|
|
|
|
|
|
|
|
/// Register units clobbered by the conditional blocks.
|
|
|
|
BitVector ClobberedRegUnits;
|
|
|
|
|
|
|
|
// Scratch pad for findInsertionPoint.
|
|
|
|
SparseSet<unsigned> LiveRegUnits;
|
|
|
|
|
|
|
|
/// Insertion point in Head for speculatively executed instructions form TBB
|
|
|
|
/// and FBB.
|
|
|
|
MachineBasicBlock::iterator InsertionPoint;
|
|
|
|
|
2024-10-07 16:03:11 +02:00
|
|
|
/// Return true if all non-terminator instructions in MBB can be safely
|
|
|
|
/// speculated.
|
|
|
|
bool canSpeculateInstrs(MachineBasicBlock *MBB);
|
|
|
|
|
2019-08-20 15:54:59 +00:00
|
|
|
/// Return true if all non-terminator instructions in MBB can be safely
|
|
|
|
/// predicated.
|
|
|
|
bool canPredicateInstrs(MachineBasicBlock *MBB);
|
|
|
|
|
|
|
|
/// Scan through instruction dependencies and update InsertAfter array.
|
|
|
|
/// Return false if any dependency is incompatible with if conversion.
|
|
|
|
bool InstrDependenciesAllowIfConv(MachineInstr *I);
|
|
|
|
|
2024-10-07 16:03:11 +02:00
|
|
|
/// Predicate all instructions of the basic block with current condition
|
|
|
|
/// except for terminators. Reverse the condition if ReversePredicate is set.
|
|
|
|
void PredicateBlock(MachineBasicBlock *MBB, bool ReversePredicate);
|
|
|
|
|
2012-07-04 00:09:54 +00:00
|
|
|
/// Find a valid insertion point in Head.
|
|
|
|
bool findInsertionPoint();
|
|
|
|
|
2012-08-13 20:49:04 +00:00
|
|
|
/// Replace PHI instructions in Tail with selects.
|
|
|
|
void replacePHIInstrs();
|
|
|
|
|
|
|
|
/// Insert selects and rewrite PHI operands to use them.
|
|
|
|
void rewritePHIOperands();
|
|
|
|
|
2012-07-04 00:09:54 +00:00
|
|
|
public:
|
2024-10-08 11:11:23 +02:00
|
|
|
/// init - Initialize per-function data structures.
|
|
|
|
void init(MachineFunction &MF) {
|
2014-08-05 02:39:49 +00:00
|
|
|
TII = MF.getSubtarget().getInstrInfo();
|
|
|
|
TRI = MF.getSubtarget().getRegisterInfo();
|
2012-07-04 00:09:54 +00:00
|
|
|
MRI = &MF.getRegInfo();
|
|
|
|
LiveRegUnits.clear();
|
|
|
|
LiveRegUnits.setUniverse(TRI->getNumRegUnits());
|
|
|
|
ClobberedRegUnits.clear();
|
|
|
|
ClobberedRegUnits.resize(TRI->getNumRegUnits());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// canConvertIf - If the sub-CFG headed by MBB can be if-converted,
|
|
|
|
/// initialize the internal state, and return true.
|
2024-10-07 16:03:11 +02:00
|
|
|
/// If predicate is set try to predicate the block otherwise try to
|
|
|
|
/// speculatively execute it.
|
|
|
|
bool canConvertIf(MachineBasicBlock *MBB, bool Predicate = false);
|
2012-07-04 00:09:54 +00:00
|
|
|
|
|
|
|
/// convertIf - If-convert the last block passed to canConvertIf(), assuming
|
2024-07-31 19:20:49 +02:00
|
|
|
/// it is possible. Add any blocks that are to be erased to RemoveBlocks.
|
2024-10-07 16:03:11 +02:00
|
|
|
void convertIf(SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
|
|
|
|
bool Predicate = false);
|
2012-07-04 00:09:54 +00:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2024-10-07 16:03:11 +02:00
|
|
|
/// canSpeculateInstrs - Returns true if all the instructions in MBB can safely
|
|
|
|
/// be speculated. The terminators are not considered.
|
|
|
|
///
|
|
|
|
/// If instructions use any values that are defined in the head basic block,
|
|
|
|
/// the defining instructions are added to InsertAfter.
|
|
|
|
///
|
|
|
|
/// Any clobbered regunits are added to ClobberedRegUnits.
|
|
|
|
///
|
|
|
|
bool SSAIfConv::canSpeculateInstrs(MachineBasicBlock *MBB) {
|
|
|
|
// Reject any live-in physregs. It's probably CPSR/EFLAGS, and very hard to
|
|
|
|
// get right.
|
|
|
|
if (!MBB->livein_empty()) {
|
|
|
|
LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has live-ins.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned InstrCount = 0;
|
|
|
|
|
|
|
|
// Check all instructions, except the terminators. It is assumed that
|
|
|
|
// terminators never have side effects or define any used register values.
|
|
|
|
for (MachineInstr &MI :
|
|
|
|
llvm::make_range(MBB->begin(), MBB->getFirstTerminator())) {
|
|
|
|
if (MI.isDebugInstr())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (++InstrCount > BlockInstrLimit && !Stress) {
|
|
|
|
LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has more than "
|
|
|
|
<< BlockInstrLimit << " instructions.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There shouldn't normally be any phis in a single-predecessor block.
|
|
|
|
if (MI.isPHI()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Can't hoist: " << MI);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't speculate loads. Note that it may be possible and desirable to
|
|
|
|
// speculate GOT or constant pool loads that are guaranteed not to trap,
|
|
|
|
// but we don't support that for now.
|
|
|
|
if (MI.mayLoad()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Won't speculate load: " << MI);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We never speculate stores, so an AA pointer isn't necessary.
|
|
|
|
bool DontMoveAcrossStore = true;
|
|
|
|
if (!MI.isSafeToMove(DontMoveAcrossStore)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Can't speculate: " << MI);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for any dependencies on Head instructions.
|
|
|
|
if (!InstrDependenciesAllowIfConv(&MI))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:54:59 +00:00
|
|
|
/// Check that there is no dependencies preventing if conversion.
|
|
|
|
///
|
|
|
|
/// If instruction uses any values that are defined in the head basic block,
|
|
|
|
/// the defining instructions are added to InsertAfter.
|
|
|
|
bool SSAIfConv::InstrDependenciesAllowIfConv(MachineInstr *I) {
|
|
|
|
for (const MachineOperand &MO : I->operands()) {
|
|
|
|
if (MO.isRegMask()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Won't speculate regmask: " << *I);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
Register Reg = MO.getReg();
|
2012-07-04 00:09:54 +00:00
|
|
|
|
2019-08-20 15:54:59 +00:00
|
|
|
// Remember clobbered regunits.
|
2023-01-13 14:38:08 -08:00
|
|
|
if (MO.isDef() && Reg.isPhysical())
|
2023-05-21 04:28:33 +03:00
|
|
|
for (MCRegUnit Unit : TRI->regunits(Reg.asMCReg()))
|
|
|
|
ClobberedRegUnits.set(Unit);
|
2019-08-20 15:54:59 +00:00
|
|
|
|
2023-01-13 14:38:08 -08:00
|
|
|
if (!MO.readsReg() || !Reg.isVirtual())
|
2019-08-20 15:54:59 +00:00
|
|
|
continue;
|
|
|
|
MachineInstr *DefMI = MRI->getVRegDef(Reg);
|
|
|
|
if (!DefMI || DefMI->getParent() != Head)
|
|
|
|
continue;
|
|
|
|
if (InsertAfter.insert(DefMI).second)
|
|
|
|
LLVM_DEBUG(dbgs() << printMBBReference(*I->getParent()) << " depends on "
|
|
|
|
<< *DefMI);
|
|
|
|
if (DefMI->isTerminator()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Can't insert instructions below terminator.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// canPredicateInstrs - Returns true if all the instructions in MBB can safely
|
|
|
|
/// be predicates. The terminators are not considered.
|
|
|
|
///
|
|
|
|
/// If instructions use any values that are defined in the head basic block,
|
|
|
|
/// the defining instructions are added to InsertAfter.
|
|
|
|
///
|
|
|
|
/// Any clobbered regunits are added to ClobberedRegUnits.
|
|
|
|
///
|
|
|
|
bool SSAIfConv::canPredicateInstrs(MachineBasicBlock *MBB) {
|
|
|
|
// Reject any live-in physregs. It's probably CPSR/EFLAGS, and very hard to
|
|
|
|
// get right.
|
|
|
|
if (!MBB->livein_empty()) {
|
|
|
|
LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has live-ins.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned InstrCount = 0;
|
|
|
|
|
|
|
|
// Check all instructions, except the terminators. It is assumed that
|
|
|
|
// terminators never have side effects or define any used register values.
|
|
|
|
for (MachineBasicBlock::iterator I = MBB->begin(),
|
|
|
|
E = MBB->getFirstTerminator();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->isDebugInstr())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (++InstrCount > BlockInstrLimit && !Stress) {
|
|
|
|
LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has more than "
|
|
|
|
<< BlockInstrLimit << " instructions.\n");
|
|
|
|
return false;
|
2012-07-04 00:09:54 +00:00
|
|
|
}
|
2019-08-20 15:54:59 +00:00
|
|
|
|
|
|
|
// There shouldn't normally be any phis in a single-predecessor block.
|
|
|
|
if (I->isPHI()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Can't predicate: " << *I);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-10-07 16:03:11 +02:00
|
|
|
// Check that instruction is predicable
|
|
|
|
if (!TII->isPredicable(*I)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Isn't predicable: " << *I);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that instruction is not already predicated.
|
|
|
|
if (TII->isPredicated(*I) && !TII->canPredicatePredicatedInstr(*I)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Is already predicated: " << *I);
|
2019-08-20 15:54:59 +00:00
|
|
|
return false;
|
2024-10-07 16:03:11 +02:00
|
|
|
}
|
2019-08-20 15:54:59 +00:00
|
|
|
|
|
|
|
// Check for any dependencies on Head instructions.
|
|
|
|
if (!InstrDependenciesAllowIfConv(&(*I)))
|
|
|
|
return false;
|
2012-07-04 00:09:54 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-10-07 16:03:11 +02:00
|
|
|
// Apply predicate to all instructions in the machine block.
|
|
|
|
void SSAIfConv::PredicateBlock(MachineBasicBlock *MBB, bool ReversePredicate) {
|
|
|
|
auto Condition = Cond;
|
|
|
|
if (ReversePredicate) {
|
|
|
|
bool CanRevCond = !TII->reverseBranchCondition(Condition);
|
|
|
|
assert(CanRevCond && "Reversed predicate is not supported");
|
|
|
|
(void)CanRevCond;
|
|
|
|
}
|
|
|
|
// Terminators don't need to be predicated as they will be removed.
|
|
|
|
for (MachineBasicBlock::iterator I = MBB->begin(),
|
|
|
|
E = MBB->getFirstTerminator();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->isDebugInstr())
|
|
|
|
continue;
|
|
|
|
TII->PredicateInstruction(*I, Condition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 00:09:54 +00:00
|
|
|
/// Find an insertion point in Head for the speculated instructions. The
|
|
|
|
/// insertion point must be:
|
|
|
|
///
|
|
|
|
/// 1. Before any terminators.
|
|
|
|
/// 2. After any instructions in InsertAfter.
|
|
|
|
/// 3. Not have any clobbered regunits live.
|
|
|
|
///
|
|
|
|
/// This function sets InsertionPoint and returns true when successful, it
|
|
|
|
/// returns false if no valid insertion point could be found.
|
|
|
|
///
|
|
|
|
bool SSAIfConv::findInsertionPoint() {
|
|
|
|
// Keep track of live regunits before the current position.
|
|
|
|
// Only track RegUnits that are also in ClobberedRegUnits.
|
|
|
|
LiveRegUnits.clear();
|
2020-10-05 16:49:29 -07:00
|
|
|
SmallVector<MCRegister, 8> Reads;
|
2012-07-04 00:09:54 +00:00
|
|
|
MachineBasicBlock::iterator FirstTerm = Head->getFirstTerminator();
|
|
|
|
MachineBasicBlock::iterator I = Head->end();
|
|
|
|
MachineBasicBlock::iterator B = Head->begin();
|
|
|
|
while (I != B) {
|
|
|
|
--I;
|
|
|
|
// Some of the conditional code depends in I.
|
2016-02-22 02:53:42 +00:00
|
|
|
if (InsertAfter.count(&*I)) {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Can't insert code after " << *I);
|
2012-07-04 00:09:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update live regunits.
|
2015-05-29 02:56:46 +00:00
|
|
|
for (const MachineOperand &MO : I->operands()) {
|
2012-07-04 00:09:54 +00:00
|
|
|
// We're ignoring regmask operands. That is conservatively correct.
|
2015-05-29 02:56:46 +00:00
|
|
|
if (!MO.isReg())
|
2012-07-04 00:09:54 +00: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 19:22:08 +00:00
|
|
|
Register Reg = MO.getReg();
|
2023-01-13 14:38:08 -08:00
|
|
|
if (!Reg.isPhysical())
|
2012-07-04 00:09:54 +00:00
|
|
|
continue;
|
|
|
|
// I clobbers Reg, so it isn't live before I.
|
2015-05-29 02:56:46 +00:00
|
|
|
if (MO.isDef())
|
2023-05-21 04:28:33 +03:00
|
|
|
for (MCRegUnit Unit : TRI->regunits(Reg.asMCReg()))
|
|
|
|
LiveRegUnits.erase(Unit);
|
2012-07-04 00:09:54 +00:00
|
|
|
// Unless I reads Reg.
|
2015-05-29 02:56:46 +00:00
|
|
|
if (MO.readsReg())
|
2020-10-05 16:49:29 -07:00
|
|
|
Reads.push_back(Reg.asMCReg());
|
2012-07-04 00:09:54 +00:00
|
|
|
}
|
|
|
|
// Anything read by I is live before I.
|
|
|
|
while (!Reads.empty())
|
2023-05-21 04:28:33 +03:00
|
|
|
for (MCRegUnit Unit : TRI->regunits(Reads.pop_back_val()))
|
|
|
|
if (ClobberedRegUnits.test(Unit))
|
|
|
|
LiveRegUnits.insert(Unit);
|
2012-07-04 00:09:54 +00:00
|
|
|
|
|
|
|
// We can't insert before a terminator.
|
|
|
|
if (I != FirstTerm && I->isTerminator())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Some of the clobbered registers are live before I, not a valid insertion
|
|
|
|
// point.
|
|
|
|
if (!LiveRegUnits.empty()) {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG({
|
2012-07-04 00:09:54 +00:00
|
|
|
dbgs() << "Would clobber";
|
2021-02-13 20:41:39 -08:00
|
|
|
for (unsigned LRU : LiveRegUnits)
|
|
|
|
dbgs() << ' ' << printRegUnit(LRU, TRI);
|
2012-07-04 00:09:54 +00:00
|
|
|
dbgs() << " live before " << *I;
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a valid insertion point.
|
|
|
|
InsertionPoint = I;
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Can insert before " << *I);
|
2012-07-04 00:09:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "No legal insertion point found.\n");
|
2012-07-04 00:09:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// canConvertIf - analyze the sub-cfg rooted in MBB, and return true if it is
|
|
|
|
/// a potential candidate for if-conversion. Fill out the internal state.
|
|
|
|
///
|
2024-10-07 16:03:11 +02:00
|
|
|
bool SSAIfConv::canConvertIf(MachineBasicBlock *MBB, bool Predicate) {
|
2012-07-04 00:09:54 +00:00
|
|
|
Head = MBB;
|
2014-04-14 00:51:57 +00:00
|
|
|
TBB = FBB = Tail = nullptr;
|
2012-07-04 00:09:54 +00:00
|
|
|
|
|
|
|
if (Head->succ_size() != 2)
|
|
|
|
return false;
|
|
|
|
MachineBasicBlock *Succ0 = Head->succ_begin()[0];
|
|
|
|
MachineBasicBlock *Succ1 = Head->succ_begin()[1];
|
|
|
|
|
|
|
|
// Canonicalize so Succ0 has MBB as its single predecessor.
|
|
|
|
if (Succ0->pred_size() != 1)
|
|
|
|
std::swap(Succ0, Succ1);
|
|
|
|
|
|
|
|
if (Succ0->pred_size() != 1 || Succ0->succ_size() != 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Tail = Succ0->succ_begin()[0];
|
|
|
|
|
|
|
|
// This is not a triangle.
|
|
|
|
if (Tail != Succ1) {
|
|
|
|
// Check for a diamond. We won't deal with any critical edges.
|
|
|
|
if (Succ1->pred_size() != 1 || Succ1->succ_size() != 1 ||
|
|
|
|
Succ1->succ_begin()[0] != Tail)
|
|
|
|
return false;
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "\nDiamond: " << printMBBReference(*Head) << " -> "
|
|
|
|
<< printMBBReference(*Succ0) << "/"
|
|
|
|
<< printMBBReference(*Succ1) << " -> "
|
|
|
|
<< printMBBReference(*Tail) << '\n');
|
2012-07-04 00:09:54 +00:00
|
|
|
|
|
|
|
// Live-in physregs are tricky to get right when speculating code.
|
|
|
|
if (!Tail->livein_empty()) {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Tail has live-ins.\n");
|
2012-07-04 00:09:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "\nTriangle: " << printMBBReference(*Head) << " -> "
|
|
|
|
<< printMBBReference(*Succ0) << " -> "
|
|
|
|
<< printMBBReference(*Tail) << '\n');
|
2012-07-04 00:09:54 +00:00
|
|
|
}
|
|
|
|
|
2024-10-07 16:03:11 +02:00
|
|
|
// This is a triangle or a diamond.
|
|
|
|
// Skip if we cannot predicate and there are no phis skip as there must be
|
|
|
|
// side effects that can only be handled with predication.
|
|
|
|
if (!Predicate && (Tail->empty() || !Tail->front().isPHI())) {
|
|
|
|
LLVM_DEBUG(dbgs() << "No phis in tail.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-04 00:09:54 +00:00
|
|
|
// The branch we're looking to eliminate must be analyzable.
|
|
|
|
Cond.clear();
|
2016-07-15 14:41:04 +00:00
|
|
|
if (TII->analyzeBranch(*Head, TBB, FBB, Cond)) {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Branch not analyzable.\n");
|
2012-07-04 00:09:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is weird, probably some sort of degenerate CFG.
|
|
|
|
if (!TBB) {
|
2020-01-21 09:47:35 -06:00
|
|
|
LLVM_DEBUG(dbgs() << "analyzeBranch didn't find conditional branch.\n");
|
2012-07-04 00:09:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-15 00:19:46 +00:00
|
|
|
// Make sure the analyzed branch is conditional; one of the successors
|
|
|
|
// could be a landing pad. (Empty landing pads can be generated on Windows.)
|
|
|
|
if (Cond.empty()) {
|
2020-01-21 09:47:35 -06:00
|
|
|
LLVM_DEBUG(dbgs() << "analyzeBranch found an unconditional branch.\n");
|
2019-01-15 00:19:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-21 09:47:35 -06:00
|
|
|
// analyzeBranch doesn't set FBB on a fall-through branch.
|
2012-07-04 00:09:54 +00:00
|
|
|
// Make sure it is always set.
|
|
|
|
FBB = TBB == Succ0 ? Succ1 : Succ0;
|
|
|
|
|
|
|
|
// Any phis in the tail block must be convertible to selects.
|
|
|
|
PHIs.clear();
|
2012-08-10 20:19:17 +00:00
|
|
|
MachineBasicBlock *TPred = getTPred();
|
|
|
|
MachineBasicBlock *FPred = getFPred();
|
2012-07-04 00:09:54 +00:00
|
|
|
for (MachineBasicBlock::iterator I = Tail->begin(), E = Tail->end();
|
|
|
|
I != E && I->isPHI(); ++I) {
|
|
|
|
PHIs.push_back(&*I);
|
|
|
|
PHIInfo &PI = PHIs.back();
|
|
|
|
// Find PHI operands corresponding to TPred and FPred.
|
|
|
|
for (unsigned i = 1; i != PI.PHI->getNumOperands(); i += 2) {
|
|
|
|
if (PI.PHI->getOperand(i+1).getMBB() == TPred)
|
|
|
|
PI.TReg = PI.PHI->getOperand(i).getReg();
|
|
|
|
if (PI.PHI->getOperand(i+1).getMBB() == FPred)
|
|
|
|
PI.FReg = PI.PHI->getOperand(i).getReg();
|
|
|
|
}
|
2019-08-01 23:27:28 +00:00
|
|
|
assert(Register::isVirtualRegister(PI.TReg) && "Bad PHI");
|
|
|
|
assert(Register::isVirtualRegister(PI.FReg) && "Bad PHI");
|
2012-07-04 00:09:54 +00:00
|
|
|
|
|
|
|
// Get target information.
|
2020-01-17 14:34:26 -08:00
|
|
|
if (!TII->canInsertSelect(*Head, Cond, PI.PHI->getOperand(0).getReg(),
|
|
|
|
PI.TReg, PI.FReg, PI.CondCycles, PI.TCycles,
|
|
|
|
PI.FCycles)) {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Can't convert: " << *PI.PHI);
|
2012-07-04 00:09:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the conditional instructions can be speculated.
|
|
|
|
InsertAfter.clear();
|
|
|
|
ClobberedRegUnits.reset();
|
2024-10-07 16:03:11 +02:00
|
|
|
if (Predicate) {
|
|
|
|
if (TBB != Tail && !canPredicateInstrs(TBB))
|
2019-08-20 15:54:59 +00:00
|
|
|
return false;
|
2024-10-07 16:03:11 +02:00
|
|
|
if (FBB != Tail && !canPredicateInstrs(FBB))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (TBB != Tail && !canSpeculateInstrs(TBB))
|
|
|
|
return false;
|
|
|
|
if (FBB != Tail && !canSpeculateInstrs(FBB))
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-04 00:09:54 +00:00
|
|
|
|
|
|
|
// Try to find a valid insertion point for the speculated instructions in the
|
|
|
|
// head basic block.
|
|
|
|
if (!findInsertionPoint())
|
|
|
|
return false;
|
|
|
|
|
2012-08-13 21:03:27 +00:00
|
|
|
if (isTriangle())
|
|
|
|
++NumTrianglesSeen;
|
|
|
|
else
|
|
|
|
++NumDiamondsSeen;
|
2012-07-04 00:09:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-29 08:40:41 -07:00
|
|
|
/// \return true iff the two registers are known to have the same value.
|
|
|
|
static bool hasSameValue(const MachineRegisterInfo &MRI,
|
|
|
|
const TargetInstrInfo *TII, Register TReg,
|
|
|
|
Register FReg) {
|
|
|
|
if (TReg == FReg)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!TReg.isVirtual() || !FReg.isVirtual())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const MachineInstr *TDef = MRI.getUniqueVRegDef(TReg);
|
|
|
|
const MachineInstr *FDef = MRI.getUniqueVRegDef(FReg);
|
|
|
|
if (!TDef || !FDef)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If there are side-effects, all bets are off.
|
|
|
|
if (TDef->hasUnmodeledSideEffects())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If the instruction could modify memory, or there may be some intervening
|
|
|
|
// store between the two, we can't consider them to be equal.
|
2022-06-24 12:09:34 -04:00
|
|
|
if (TDef->mayLoadOrStore() && !TDef->isDereferenceableInvariantLoad())
|
2021-04-29 08:40:41 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// We also can't guarantee that they are the same if, for example, the
|
|
|
|
// instructions are both a copy from a physical reg, because some other
|
|
|
|
// instruction may have modified the value in that reg between the two
|
|
|
|
// defining insts.
|
|
|
|
if (any_of(TDef->uses(), [](const MachineOperand &MO) {
|
|
|
|
return MO.isReg() && MO.getReg().isPhysical();
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check whether the two defining instructions produce the same value(s).
|
|
|
|
if (!TII->produceSameValue(*TDef, *FDef, &MRI))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Further, check that the two defs come from corresponding operands.
|
[CodeGen] Make the parameter TRI required in some functions. (#85968)
Fixes #82659
There are some functions, such as `findRegisterDefOperandIdx` and `findRegisterDefOperand`, that have too many default parameters. As a result, we have encountered some issues due to the lack of TRI parameters, as shown in issue #82411.
Following @RKSimon 's suggestion, this patch refactors 9 functions, including `{reads, kills, defines, modifies}Register`, `registerDefIsDead`, and `findRegister{UseOperandIdx, UseOperand, DefOperandIdx, DefOperand}`, adjusting the order of the TRI parameter and making it required. In addition, all the places that call these functions have also been updated correctly to ensure no additional impact.
After this, the caller of these functions should explicitly know whether to pass the `TargetRegisterInfo` or just a `nullptr`.
2024-04-24 21:24:14 +08:00
|
|
|
int TIdx = TDef->findRegisterDefOperandIdx(TReg, /*TRI=*/nullptr);
|
|
|
|
int FIdx = FDef->findRegisterDefOperandIdx(FReg, /*TRI=*/nullptr);
|
2021-04-29 08:40:41 -07:00
|
|
|
if (TIdx == -1 || FIdx == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return TIdx == FIdx;
|
|
|
|
}
|
|
|
|
|
2012-08-13 20:49:04 +00:00
|
|
|
/// replacePHIInstrs - Completely replace PHI instructions with selects.
|
|
|
|
/// This is possible when the only Tail predecessors are the if-converted
|
|
|
|
/// blocks.
|
|
|
|
void SSAIfConv::replacePHIInstrs() {
|
|
|
|
assert(Tail->pred_size() == 2 && "Cannot replace PHIs");
|
2012-07-04 00:09:54 +00:00
|
|
|
MachineBasicBlock::iterator FirstTerm = Head->getFirstTerminator();
|
|
|
|
assert(FirstTerm != Head->end() && "No terminators");
|
|
|
|
DebugLoc HeadDL = FirstTerm->getDebugLoc();
|
|
|
|
|
|
|
|
// Convert all PHIs to select instructions inserted before FirstTerm.
|
2024-06-26 16:49:00 -07:00
|
|
|
for (PHIInfo &PI : PHIs) {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "If-converting " << *PI.PHI);
|
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 19:22:08 +00:00
|
|
|
Register DstReg = PI.PHI->getOperand(0).getReg();
|
2021-04-29 08:40:41 -07:00
|
|
|
if (hasSameValue(*MRI, TII, PI.TReg, PI.FReg)) {
|
|
|
|
// We do not need the select instruction if both incoming values are
|
|
|
|
// equal, but we do need a COPY.
|
|
|
|
BuildMI(*Head, FirstTerm, HeadDL, TII->get(TargetOpcode::COPY), DstReg)
|
|
|
|
.addReg(PI.TReg);
|
|
|
|
} else {
|
|
|
|
TII->insertSelect(*Head, FirstTerm, HeadDL, DstReg, Cond, PI.TReg,
|
|
|
|
PI.FReg);
|
|
|
|
}
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << " --> " << *std::prev(FirstTerm));
|
2012-07-04 00:09:54 +00:00
|
|
|
PI.PHI->eraseFromParent();
|
2014-04-14 00:51:57 +00:00
|
|
|
PI.PHI = nullptr;
|
2012-07-04 00:09:54 +00:00
|
|
|
}
|
2012-08-13 20:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// rewritePHIOperands - When there are additional Tail predecessors, insert
|
|
|
|
/// select instructions in Head and rewrite PHI operands to use the selects.
|
|
|
|
/// Keep the PHI instructions in Tail to handle the other predecessors.
|
|
|
|
void SSAIfConv::rewritePHIOperands() {
|
|
|
|
MachineBasicBlock::iterator FirstTerm = Head->getFirstTerminator();
|
|
|
|
assert(FirstTerm != Head->end() && "No terminators");
|
|
|
|
DebugLoc HeadDL = FirstTerm->getDebugLoc();
|
|
|
|
|
|
|
|
// Convert all PHIs to select instructions inserted before FirstTerm.
|
2024-06-26 16:49:00 -07:00
|
|
|
for (PHIInfo &PI : PHIs) {
|
2015-06-18 22:34:09 +00:00
|
|
|
unsigned DstReg = 0;
|
2016-01-29 01:39:39 +00:00
|
|
|
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "If-converting " << *PI.PHI);
|
2021-04-29 08:40:41 -07:00
|
|
|
if (hasSameValue(*MRI, TII, PI.TReg, PI.FReg)) {
|
2015-06-18 22:34:09 +00:00
|
|
|
// We do not need the select instruction if both incoming values are
|
|
|
|
// equal.
|
|
|
|
DstReg = PI.TReg;
|
|
|
|
} else {
|
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 19:22:08 +00:00
|
|
|
Register PHIDst = PI.PHI->getOperand(0).getReg();
|
2015-06-18 22:34:09 +00:00
|
|
|
DstReg = MRI->createVirtualRegister(MRI->getRegClass(PHIDst));
|
|
|
|
TII->insertSelect(*Head, FirstTerm, HeadDL,
|
|
|
|
DstReg, Cond, PI.TReg, PI.FReg);
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << " --> " << *std::prev(FirstTerm));
|
2015-06-18 22:34:09 +00:00
|
|
|
}
|
2012-08-13 20:49:04 +00:00
|
|
|
|
|
|
|
// Rewrite PHI operands TPred -> (DstReg, Head), remove FPred.
|
|
|
|
for (unsigned i = PI.PHI->getNumOperands(); i != 1; i -= 2) {
|
|
|
|
MachineBasicBlock *MBB = PI.PHI->getOperand(i-1).getMBB();
|
|
|
|
if (MBB == getTPred()) {
|
|
|
|
PI.PHI->getOperand(i-1).setMBB(Head);
|
|
|
|
PI.PHI->getOperand(i-2).setReg(DstReg);
|
|
|
|
} else if (MBB == getFPred()) {
|
2022-03-16 20:21:25 +08:00
|
|
|
PI.PHI->removeOperand(i-1);
|
|
|
|
PI.PHI->removeOperand(i-2);
|
2012-08-13 20:49:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << " --> " << *PI.PHI);
|
2012-08-13 20:49:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// convertIf - Execute the if conversion after canConvertIf has determined the
|
|
|
|
/// feasibility.
|
|
|
|
///
|
2024-07-31 19:20:49 +02:00
|
|
|
/// Any basic blocks that need to be erased will be added to RemoveBlocks.
|
2012-08-13 20:49:04 +00:00
|
|
|
///
|
2024-10-07 16:03:11 +02:00
|
|
|
void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
|
|
|
|
bool Predicate) {
|
2012-08-13 20:49:04 +00:00
|
|
|
assert(Head && Tail && TBB && FBB && "Call canConvertIf first.");
|
|
|
|
|
2012-08-13 21:03:27 +00:00
|
|
|
// Update statistics.
|
|
|
|
if (isTriangle())
|
|
|
|
++NumTrianglesConv;
|
|
|
|
else
|
|
|
|
++NumDiamondsConv;
|
|
|
|
|
2012-08-13 20:49:04 +00:00
|
|
|
// Move all instructions into Head, except for the terminators.
|
2024-10-07 16:03:11 +02:00
|
|
|
if (TBB != Tail) {
|
|
|
|
if (Predicate)
|
|
|
|
PredicateBlock(TBB, /*ReversePredicate=*/false);
|
|
|
|
Head->splice(InsertionPoint, TBB, TBB->begin(), TBB->getFirstTerminator());
|
|
|
|
}
|
|
|
|
if (FBB != Tail) {
|
|
|
|
if (Predicate)
|
|
|
|
PredicateBlock(FBB, /*ReversePredicate=*/true);
|
|
|
|
Head->splice(InsertionPoint, FBB, FBB->begin(), FBB->getFirstTerminator());
|
2019-08-20 15:54:59 +00:00
|
|
|
}
|
2012-08-13 20:49:04 +00:00
|
|
|
// Are there extra Tail predecessors?
|
|
|
|
bool ExtraPreds = Tail->pred_size() != 2;
|
|
|
|
if (ExtraPreds)
|
|
|
|
rewritePHIOperands();
|
|
|
|
else
|
|
|
|
replacePHIInstrs();
|
2012-07-04 00:09:54 +00:00
|
|
|
|
|
|
|
// Fix up the CFG, temporarily leave Head without any successors.
|
|
|
|
Head->removeSuccessor(TBB);
|
2015-12-13 09:26:17 +00:00
|
|
|
Head->removeSuccessor(FBB, true);
|
2012-07-04 00:09:54 +00:00
|
|
|
if (TBB != Tail)
|
2015-12-13 09:26:17 +00:00
|
|
|
TBB->removeSuccessor(Tail, true);
|
2012-07-04 00:09:54 +00:00
|
|
|
if (FBB != Tail)
|
2015-12-13 09:26:17 +00:00
|
|
|
FBB->removeSuccessor(Tail, true);
|
2012-07-04 00:09:54 +00:00
|
|
|
|
|
|
|
// Fix up Head's terminators.
|
|
|
|
// It should become a single branch or a fallthrough.
|
2012-08-13 20:49:04 +00:00
|
|
|
DebugLoc HeadDL = Head->getFirstTerminator()->getDebugLoc();
|
2016-09-14 20:43:16 +00:00
|
|
|
TII->removeBranch(*Head);
|
2012-07-04 00:09:54 +00:00
|
|
|
|
2024-07-31 19:20:49 +02:00
|
|
|
// Mark the now empty conditional blocks for removal and move them to the end.
|
|
|
|
// It is likely that Head can fall
|
2012-07-04 00:09:54 +00:00
|
|
|
// through to Tail, and we can join the two blocks.
|
2012-07-10 22:18:23 +00:00
|
|
|
if (TBB != Tail) {
|
2024-07-31 19:20:49 +02:00
|
|
|
RemoveBlocks.push_back(TBB);
|
|
|
|
if (TBB != &TBB->getParent()->back())
|
|
|
|
TBB->moveAfter(&TBB->getParent()->back());
|
2012-07-10 22:18:23 +00:00
|
|
|
}
|
|
|
|
if (FBB != Tail) {
|
2024-07-31 19:20:49 +02:00
|
|
|
RemoveBlocks.push_back(FBB);
|
|
|
|
if (FBB != &FBB->getParent()->back())
|
|
|
|
FBB->moveAfter(&FBB->getParent()->back());
|
2012-07-10 22:18:23 +00:00
|
|
|
}
|
2012-07-04 00:09:54 +00:00
|
|
|
|
|
|
|
assert(Head->succ_empty() && "Additional head successors?");
|
2012-08-13 20:49:04 +00:00
|
|
|
if (!ExtraPreds && Head->isLayoutSuccessor(Tail)) {
|
2012-07-04 00:09:54 +00:00
|
|
|
// Splice Tail onto the end of Head.
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Joining tail " << printMBBReference(*Tail)
|
|
|
|
<< " into head " << printMBBReference(*Head) << '\n');
|
2012-07-04 00:09:54 +00:00
|
|
|
Head->splice(Head->end(), Tail,
|
|
|
|
Tail->begin(), Tail->end());
|
|
|
|
Head->transferSuccessorsAndUpdatePHIs(Tail);
|
2024-07-31 19:20:49 +02:00
|
|
|
RemoveBlocks.push_back(Tail);
|
|
|
|
if (Tail != &Tail->getParent()->back())
|
|
|
|
Tail->moveAfter(&Tail->getParent()->back());
|
2012-07-04 00:09:54 +00:00
|
|
|
} else {
|
|
|
|
// We need a branch to Tail, let code placement work it out later.
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Converting to unconditional branch.\n");
|
2012-07-04 00:09:54 +00:00
|
|
|
SmallVector<MachineOperand, 0> EmptyCond;
|
2016-09-14 17:24:15 +00:00
|
|
|
TII->insertBranch(*Head, Tail, nullptr, EmptyCond, HeadDL);
|
2012-07-04 00:09:54 +00:00
|
|
|
Head->addSuccessor(Tail);
|
|
|
|
}
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << *Head);
|
2012-07-04 00:09:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// EarlyIfConverter Pass
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2024-10-16 13:22:57 +05:30
|
|
|
class EarlyIfConverter {
|
2024-10-07 14:31:59 +02:00
|
|
|
const TargetInstrInfo *TII = nullptr;
|
|
|
|
const TargetRegisterInfo *TRI = nullptr;
|
2014-09-02 17:43:54 +00:00
|
|
|
MCSchedModel SchedModel;
|
2023-04-17 16:16:23 +08:00
|
|
|
MachineRegisterInfo *MRI = nullptr;
|
|
|
|
MachineDominatorTree *DomTree = nullptr;
|
|
|
|
MachineLoopInfo *Loops = nullptr;
|
|
|
|
MachineTraceMetrics *Traces = nullptr;
|
|
|
|
MachineTraceMetrics::Ensemble *MinInstr = nullptr;
|
2024-10-08 10:27:22 +02:00
|
|
|
SSAIfConv IfConv;
|
2012-07-04 00:09:54 +00:00
|
|
|
|
|
|
|
public:
|
2024-10-16 13:22:57 +05:30
|
|
|
EarlyIfConverter(MachineDominatorTree &DT, MachineLoopInfo &LI,
|
|
|
|
MachineTraceMetrics &MTM)
|
|
|
|
: DomTree(&DT), Loops(&LI), Traces(&MTM) {}
|
|
|
|
EarlyIfConverter() = delete;
|
|
|
|
|
|
|
|
bool run(MachineFunction &MF);
|
2012-07-04 00:09:54 +00:00
|
|
|
|
|
|
|
private:
|
2024-10-08 10:27:22 +02:00
|
|
|
bool tryConvertIf(MachineBasicBlock *);
|
|
|
|
void invalidateTraces();
|
|
|
|
bool shouldConvertIf();
|
2012-07-04 00:09:54 +00:00
|
|
|
};
|
2024-10-16 13:22:57 +05:30
|
|
|
|
|
|
|
class EarlyIfConverterLegacy : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
EarlyIfConverterLegacy() : MachineFunctionPass(ID) {}
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
StringRef getPassName() const override { return "Early If-Conversion"; }
|
|
|
|
};
|
2012-07-04 00:09:54 +00:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2024-10-16 13:22:57 +05:30
|
|
|
char EarlyIfConverterLegacy::ID = 0;
|
|
|
|
char &llvm::EarlyIfConverterLegacyID = EarlyIfConverterLegacy::ID;
|
2012-07-04 00:09:54 +00:00
|
|
|
|
2024-10-16 13:22:57 +05:30
|
|
|
INITIALIZE_PASS_BEGIN(EarlyIfConverterLegacy, DEBUG_TYPE, "Early If Converter",
|
|
|
|
false, false)
|
2024-06-28 10:59:23 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
|
2024-06-11 21:27:14 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
|
2024-10-16 13:19:55 +05:30
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetricsWrapperPass)
|
2024-10-16 13:22:57 +05:30
|
|
|
INITIALIZE_PASS_END(EarlyIfConverterLegacy, DEBUG_TYPE, "Early If Converter",
|
|
|
|
false, false)
|
2012-07-04 00:09:54 +00:00
|
|
|
|
2024-10-16 13:22:57 +05:30
|
|
|
void EarlyIfConverterLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
|
2024-06-28 10:59:23 +08:00
|
|
|
AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
|
2024-06-11 21:27:14 +08:00
|
|
|
AU.addRequired<MachineDominatorTreeWrapperPass>();
|
|
|
|
AU.addPreserved<MachineDominatorTreeWrapperPass>();
|
2024-07-09 09:11:18 +08:00
|
|
|
AU.addRequired<MachineLoopInfoWrapperPass>();
|
|
|
|
AU.addPreserved<MachineLoopInfoWrapperPass>();
|
2024-10-16 13:19:55 +05:30
|
|
|
AU.addRequired<MachineTraceMetricsWrapperPass>();
|
|
|
|
AU.addPreserved<MachineTraceMetricsWrapperPass>();
|
2012-07-04 00:09:54 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:54:59 +00:00
|
|
|
namespace {
|
2012-07-10 22:18:23 +00:00
|
|
|
/// Update the dominator tree after if-conversion erased some blocks.
|
2019-08-20 15:54:59 +00:00
|
|
|
void updateDomTree(MachineDominatorTree *DomTree, const SSAIfConv &IfConv,
|
|
|
|
ArrayRef<MachineBasicBlock *> Removed) {
|
2012-07-10 22:18:23 +00:00
|
|
|
// convertIf can remove TBB, FBB, and Tail can be merged into Head.
|
|
|
|
// TBB and FBB should not dominate any blocks.
|
|
|
|
// Tail children should be transferred to Head.
|
|
|
|
MachineDomTreeNode *HeadNode = DomTree->getNode(IfConv.Head);
|
2022-07-17 01:33:28 -07:00
|
|
|
for (auto *B : Removed) {
|
2019-08-20 15:54:59 +00:00
|
|
|
MachineDomTreeNode *Node = DomTree->getNode(B);
|
2012-07-10 22:18:23 +00:00
|
|
|
assert(Node != HeadNode && "Cannot erase the head node");
|
|
|
|
while (Node->getNumChildren()) {
|
|
|
|
assert(Node->getBlock() == IfConv.Tail && "Unexpected children");
|
DomTree: Remove getChildren() accessor
Summary:
Avoid exposing details about how children are stored. This will enable
subsequent type-erasure changes.
New methods are introduced to cover common access patterns.
Change-Id: Idb5f4b1b9c84e4cc71ddb39bb52a388682f5674f
Reviewers: arsenm, RKSimon, mehdi_amini, courbet
Subscribers: qcolombet, sdardis, wdng, hiraditya, jrtc27, zzheng, atanasyan, asbirlea, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D83083
2020-05-18 16:28:24 +02:00
|
|
|
DomTree->changeImmediateDominator(Node->back(), HeadNode);
|
2012-07-10 22:18:23 +00:00
|
|
|
}
|
2019-08-20 15:54:59 +00:00
|
|
|
DomTree->eraseNode(B);
|
2012-07-10 22:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-10 22:39:56 +00:00
|
|
|
/// Update LoopInfo after if-conversion.
|
2019-08-20 15:54:59 +00:00
|
|
|
void updateLoops(MachineLoopInfo *Loops,
|
|
|
|
ArrayRef<MachineBasicBlock *> Removed) {
|
2012-07-10 22:39:56 +00:00
|
|
|
// If-conversion doesn't change loop structure, and it doesn't mess with back
|
|
|
|
// edges, so updating LoopInfo is simply removing the dead blocks.
|
2022-07-17 01:33:28 -07:00
|
|
|
for (auto *B : Removed)
|
2019-08-20 15:54:59 +00:00
|
|
|
Loops->removeBlock(B);
|
2012-07-10 22:39:56 +00:00
|
|
|
}
|
2019-08-20 15:54:59 +00:00
|
|
|
} // namespace
|
2012-07-10 22:39:56 +00:00
|
|
|
|
2012-07-26 18:38:11 +00:00
|
|
|
/// Invalidate MachineTraceMetrics before if-conversion.
|
2024-10-08 10:27:22 +02:00
|
|
|
void EarlyIfConverter::invalidateTraces() {
|
2012-07-30 20:57:50 +00:00
|
|
|
Traces->verifyAnalysis();
|
2012-07-26 18:38:11 +00:00
|
|
|
Traces->invalidate(IfConv.Head);
|
|
|
|
Traces->invalidate(IfConv.Tail);
|
|
|
|
Traces->invalidate(IfConv.TBB);
|
|
|
|
Traces->invalidate(IfConv.FBB);
|
2012-07-30 20:57:50 +00:00
|
|
|
Traces->verifyAnalysis();
|
2012-07-26 18:38:11 +00:00
|
|
|
}
|
|
|
|
|
2012-08-10 22:27:31 +00:00
|
|
|
// Adjust cycles with downward saturation.
|
|
|
|
static unsigned adjCycles(unsigned Cyc, int Delta) {
|
|
|
|
if (Delta < 0 && Cyc + Delta > Cyc)
|
|
|
|
return 0;
|
|
|
|
return Cyc + Delta;
|
|
|
|
}
|
|
|
|
|
2020-08-28 15:22:32 -06:00
|
|
|
namespace {
|
|
|
|
/// Helper class to simplify emission of cycle counts into optimization remarks.
|
|
|
|
struct Cycles {
|
|
|
|
const char *Key;
|
|
|
|
unsigned Value;
|
|
|
|
};
|
|
|
|
template <typename Remark> Remark &operator<<(Remark &R, Cycles C) {
|
|
|
|
return R << ore::NV(C.Key, C.Value) << (C.Value == 1 ? " cycle" : " cycles");
|
|
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2012-07-26 18:38:11 +00:00
|
|
|
/// Apply cost model and heuristics to the if-conversion in IfConv.
|
|
|
|
/// Return true if the conversion is a good idea.
|
|
|
|
///
|
2024-10-08 10:27:22 +02:00
|
|
|
bool EarlyIfConverter::shouldConvertIf() {
|
2012-08-08 18:24:23 +00:00
|
|
|
// Stress testing mode disables all cost considerations.
|
|
|
|
if (Stress)
|
|
|
|
return true;
|
|
|
|
|
2023-05-12 19:21:03 +01:00
|
|
|
// Do not try to if-convert if the condition has a high chance of being
|
|
|
|
// predictable.
|
|
|
|
MachineLoop *CurrentLoop = Loops->getLoopFor(IfConv.Head);
|
|
|
|
// If the condition is in a loop, consider it predictable if the condition
|
|
|
|
// itself or all its operands are loop-invariant. E.g. this considers a load
|
|
|
|
// from a loop-invariant address predictable; we were unable to prove that it
|
|
|
|
// doesn't alias any of the memory-writes in the loop, but it is likely to
|
|
|
|
// read to same value multiple times.
|
|
|
|
if (CurrentLoop && any_of(IfConv.Cond, [&](MachineOperand &MO) {
|
|
|
|
if (!MO.isReg() || !MO.isUse())
|
|
|
|
return false;
|
|
|
|
Register Reg = MO.getReg();
|
|
|
|
if (Register::isPhysicalRegister(Reg))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineInstr *Def = MRI->getVRegDef(Reg);
|
|
|
|
return CurrentLoop->isLoopInvariant(*Def) ||
|
|
|
|
all_of(Def->operands(), [&](MachineOperand &Op) {
|
|
|
|
if (Op.isImm())
|
|
|
|
return true;
|
|
|
|
if (!MO.isReg() || !MO.isUse())
|
|
|
|
return false;
|
|
|
|
Register Reg = MO.getReg();
|
|
|
|
if (Register::isPhysicalRegister(Reg))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineInstr *Def = MRI->getVRegDef(Reg);
|
|
|
|
return CurrentLoop->isLoopInvariant(*Def);
|
|
|
|
});
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
|
2012-07-26 18:38:11 +00:00
|
|
|
if (!MinInstr)
|
2023-02-14 13:30:13 +03:00
|
|
|
MinInstr = Traces->getEnsemble(MachineTraceStrategy::TS_MinInstrCount);
|
2012-08-07 18:02:19 +00:00
|
|
|
|
2012-08-10 22:27:31 +00:00
|
|
|
MachineTraceMetrics::Trace TBBTrace = MinInstr->getTrace(IfConv.getTPred());
|
|
|
|
MachineTraceMetrics::Trace FBBTrace = MinInstr->getTrace(IfConv.getFPred());
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "TBB: " << TBBTrace << "FBB: " << FBBTrace);
|
2012-08-10 22:27:31 +00:00
|
|
|
unsigned MinCrit = std::min(TBBTrace.getCriticalPath(),
|
|
|
|
FBBTrace.getCriticalPath());
|
|
|
|
|
|
|
|
// Set a somewhat arbitrary limit on the critical path extension we accept.
|
2014-09-02 17:43:54 +00:00
|
|
|
unsigned CritLimit = SchedModel.MispredictPenalty/2;
|
2012-08-10 22:27:31 +00:00
|
|
|
|
2020-08-28 15:22:32 -06:00
|
|
|
MachineBasicBlock &MBB = *IfConv.Head;
|
|
|
|
MachineOptimizationRemarkEmitter MORE(*MBB.getParent(), nullptr);
|
|
|
|
|
2012-08-10 22:27:31 +00:00
|
|
|
// If-conversion only makes sense when there is unexploited ILP. Compute the
|
|
|
|
// maximum-ILP resource length of the trace after if-conversion. Compare it
|
|
|
|
// to the shortest critical path.
|
|
|
|
SmallVector<const MachineBasicBlock*, 1> ExtraBlocks;
|
|
|
|
if (IfConv.TBB != IfConv.Tail)
|
|
|
|
ExtraBlocks.push_back(IfConv.TBB);
|
|
|
|
unsigned ResLength = FBBTrace.getResourceLength(ExtraBlocks);
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Resource length " << ResLength
|
|
|
|
<< ", minimal critical path " << MinCrit << '\n');
|
2012-08-10 22:27:31 +00:00
|
|
|
if (ResLength > MinCrit + CritLimit) {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Not enough available ILP.\n");
|
2020-08-28 15:22:32 -06:00
|
|
|
MORE.emit([&]() {
|
|
|
|
MachineOptimizationRemarkMissed R(DEBUG_TYPE, "IfConversion",
|
|
|
|
MBB.findDebugLoc(MBB.back()), &MBB);
|
|
|
|
R << "did not if-convert branch: the resulting critical path ("
|
|
|
|
<< Cycles{"ResLength", ResLength}
|
|
|
|
<< ") would extend the shorter leg's critical path ("
|
|
|
|
<< Cycles{"MinCrit", MinCrit} << ") by more than the threshold of "
|
|
|
|
<< Cycles{"CritLimit", CritLimit}
|
|
|
|
<< ", which cannot be hidden by available ILP.";
|
|
|
|
return R;
|
|
|
|
});
|
2012-08-07 18:02:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-08-10 22:27:31 +00:00
|
|
|
|
|
|
|
// Assume that the depth of the first head terminator will also be the depth
|
|
|
|
// of the select instruction inserted, as determined by the flag dependency.
|
|
|
|
// TBB / FBB data dependencies may delay the select even more.
|
|
|
|
MachineTraceMetrics::Trace HeadTrace = MinInstr->getTrace(IfConv.Head);
|
|
|
|
unsigned BranchDepth =
|
2016-02-22 03:33:28 +00:00
|
|
|
HeadTrace.getInstrCycles(*IfConv.Head->getFirstTerminator()).Depth;
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Branch depth: " << BranchDepth << '\n');
|
2012-08-10 22:27:31 +00:00
|
|
|
|
|
|
|
// Look at all the tail phis, and compute the critical path extension caused
|
|
|
|
// by inserting select instructions.
|
|
|
|
MachineTraceMetrics::Trace TailTrace = MinInstr->getTrace(IfConv.Tail);
|
2020-08-28 15:22:32 -06:00
|
|
|
struct CriticalPathInfo {
|
2020-09-03 16:42:05 +01:00
|
|
|
unsigned Extra; // Count of extra cycles that the component adds.
|
|
|
|
unsigned Depth; // Absolute depth of the component in cycles.
|
2020-08-28 15:22:32 -06:00
|
|
|
};
|
|
|
|
CriticalPathInfo Cond{};
|
|
|
|
CriticalPathInfo TBlock{};
|
|
|
|
CriticalPathInfo FBlock{};
|
|
|
|
bool ShouldConvert = true;
|
2024-06-26 16:49:00 -07:00
|
|
|
for (SSAIfConv::PHIInfo &PI : IfConv.PHIs) {
|
2016-02-22 03:33:28 +00:00
|
|
|
unsigned Slack = TailTrace.getInstrSlack(*PI.PHI);
|
|
|
|
unsigned MaxDepth = Slack + TailTrace.getInstrCycles(*PI.PHI).Depth;
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Slack " << Slack << ":\t" << *PI.PHI);
|
2012-08-10 22:27:31 +00:00
|
|
|
|
|
|
|
// The condition is pulled into the critical path.
|
|
|
|
unsigned CondDepth = adjCycles(BranchDepth, PI.CondCycles);
|
|
|
|
if (CondDepth > MaxDepth) {
|
|
|
|
unsigned Extra = CondDepth - MaxDepth;
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Condition adds " << Extra << " cycles.\n");
|
2020-08-28 15:22:32 -06:00
|
|
|
if (Extra > Cond.Extra)
|
|
|
|
Cond = {Extra, CondDepth};
|
2012-08-10 22:27:31 +00:00
|
|
|
if (Extra > CritLimit) {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Exceeds limit of " << CritLimit << '\n');
|
2020-08-28 15:22:32 -06:00
|
|
|
ShouldConvert = false;
|
2012-08-10 22:27:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The TBB value is pulled into the critical path.
|
2016-02-22 03:33:28 +00:00
|
|
|
unsigned TDepth = adjCycles(TBBTrace.getPHIDepth(*PI.PHI), PI.TCycles);
|
2012-08-10 22:27:31 +00:00
|
|
|
if (TDepth > MaxDepth) {
|
|
|
|
unsigned Extra = TDepth - MaxDepth;
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "TBB data adds " << Extra << " cycles.\n");
|
2020-08-28 15:22:32 -06:00
|
|
|
if (Extra > TBlock.Extra)
|
|
|
|
TBlock = {Extra, TDepth};
|
2012-08-10 22:27:31 +00:00
|
|
|
if (Extra > CritLimit) {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Exceeds limit of " << CritLimit << '\n');
|
2020-08-28 15:22:32 -06:00
|
|
|
ShouldConvert = false;
|
2012-08-10 22:27:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The FBB value is pulled into the critical path.
|
2016-02-22 03:33:28 +00:00
|
|
|
unsigned FDepth = adjCycles(FBBTrace.getPHIDepth(*PI.PHI), PI.FCycles);
|
2012-08-10 22:27:31 +00:00
|
|
|
if (FDepth > MaxDepth) {
|
|
|
|
unsigned Extra = FDepth - MaxDepth;
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "FBB data adds " << Extra << " cycles.\n");
|
2020-08-28 15:22:32 -06:00
|
|
|
if (Extra > FBlock.Extra)
|
|
|
|
FBlock = {Extra, FDepth};
|
2012-08-10 22:27:31 +00:00
|
|
|
if (Extra > CritLimit) {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Exceeds limit of " << CritLimit << '\n');
|
2020-08-28 15:22:32 -06:00
|
|
|
ShouldConvert = false;
|
2012-08-10 22:27:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 15:22:32 -06:00
|
|
|
|
|
|
|
// Organize by "short" and "long" legs, since the diagnostics get confusing
|
|
|
|
// when referring to the "true" and "false" sides of the branch, given that
|
|
|
|
// those don't always correlate with what the user wrote in source-terms.
|
|
|
|
const CriticalPathInfo Short = TBlock.Extra > FBlock.Extra ? FBlock : TBlock;
|
|
|
|
const CriticalPathInfo Long = TBlock.Extra > FBlock.Extra ? TBlock : FBlock;
|
|
|
|
|
|
|
|
if (ShouldConvert) {
|
|
|
|
MORE.emit([&]() {
|
|
|
|
MachineOptimizationRemark R(DEBUG_TYPE, "IfConversion",
|
|
|
|
MBB.back().getDebugLoc(), &MBB);
|
|
|
|
R << "performing if-conversion on branch: the condition adds "
|
|
|
|
<< Cycles{"CondCycles", Cond.Extra} << " to the critical path";
|
|
|
|
if (Short.Extra > 0)
|
|
|
|
R << ", and the short leg adds another "
|
|
|
|
<< Cycles{"ShortCycles", Short.Extra};
|
|
|
|
if (Long.Extra > 0)
|
|
|
|
R << ", and the long leg adds another "
|
|
|
|
<< Cycles{"LongCycles", Long.Extra};
|
|
|
|
R << ", each staying under the threshold of "
|
|
|
|
<< Cycles{"CritLimit", CritLimit} << ".";
|
|
|
|
return R;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
MORE.emit([&]() {
|
|
|
|
MachineOptimizationRemarkMissed R(DEBUG_TYPE, "IfConversion",
|
|
|
|
MBB.back().getDebugLoc(), &MBB);
|
|
|
|
R << "did not if-convert branch: the condition would add "
|
|
|
|
<< Cycles{"CondCycles", Cond.Extra} << " to the critical path";
|
|
|
|
if (Cond.Extra > CritLimit)
|
|
|
|
R << " exceeding the limit of " << Cycles{"CritLimit", CritLimit};
|
|
|
|
if (Short.Extra > 0) {
|
|
|
|
R << ", and the short leg would add another "
|
|
|
|
<< Cycles{"ShortCycles", Short.Extra};
|
|
|
|
if (Short.Extra > CritLimit)
|
|
|
|
R << " exceeding the limit of " << Cycles{"CritLimit", CritLimit};
|
|
|
|
}
|
|
|
|
if (Long.Extra > 0) {
|
|
|
|
R << ", and the long leg would add another "
|
|
|
|
<< Cycles{"LongCycles", Long.Extra};
|
|
|
|
if (Long.Extra > CritLimit)
|
|
|
|
R << " exceeding the limit of " << Cycles{"CritLimit", CritLimit};
|
|
|
|
}
|
|
|
|
R << ".";
|
|
|
|
return R;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return ShouldConvert;
|
2012-07-26 18:38:11 +00:00
|
|
|
}
|
|
|
|
|
2012-07-04 00:09:54 +00:00
|
|
|
/// Attempt repeated if-conversion on MBB, return true if successful.
|
|
|
|
///
|
2024-10-08 10:27:22 +02:00
|
|
|
bool EarlyIfConverter::tryConvertIf(MachineBasicBlock *MBB) {
|
2012-07-10 22:18:23 +00:00
|
|
|
bool Changed = false;
|
2024-10-08 10:27:22 +02:00
|
|
|
while (IfConv.canConvertIf(MBB) && shouldConvertIf()) {
|
2012-07-10 22:18:23 +00:00
|
|
|
// If-convert MBB and update analyses.
|
2024-10-08 10:27:22 +02:00
|
|
|
invalidateTraces();
|
2024-07-31 19:20:49 +02:00
|
|
|
SmallVector<MachineBasicBlock *, 4> RemoveBlocks;
|
|
|
|
IfConv.convertIf(RemoveBlocks);
|
2012-07-10 22:18:23 +00:00
|
|
|
Changed = true;
|
2024-07-31 19:20:49 +02:00
|
|
|
updateDomTree(DomTree, IfConv, RemoveBlocks);
|
|
|
|
for (MachineBasicBlock *MBB : RemoveBlocks)
|
|
|
|
MBB->eraseFromParent();
|
|
|
|
updateLoops(Loops, RemoveBlocks);
|
2012-07-10 22:18:23 +00:00
|
|
|
}
|
|
|
|
return Changed;
|
2012-07-04 00:09:54 +00:00
|
|
|
}
|
|
|
|
|
2024-10-16 13:22:57 +05:30
|
|
|
bool EarlyIfConverter::run(MachineFunction &MF) {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "********** EARLY IF-CONVERSION **********\n"
|
|
|
|
<< "********** Function: " << MF.getName() << '\n');
|
2016-05-03 22:32:30 +00:00
|
|
|
|
2014-05-21 23:40:26 +00:00
|
|
|
// Only run if conversion if the target wants it.
|
2015-01-27 07:31:29 +00:00
|
|
|
const TargetSubtargetInfo &STI = MF.getSubtarget();
|
|
|
|
if (!STI.enableEarlyIfConversion())
|
2014-05-22 17:49:33 +00:00
|
|
|
return false;
|
2014-05-21 23:40:26 +00:00
|
|
|
|
2024-10-07 14:31:59 +02:00
|
|
|
TII = STI.getInstrInfo();
|
|
|
|
TRI = STI.getRegisterInfo();
|
2015-01-27 07:31:29 +00:00
|
|
|
SchedModel = STI.getSchedModel();
|
2012-07-04 00:09:54 +00:00
|
|
|
MRI = &MF.getRegInfo();
|
2014-04-14 00:51:57 +00:00
|
|
|
MinInstr = nullptr;
|
2012-07-04 00:09:54 +00:00
|
|
|
|
|
|
|
bool Changed = false;
|
2024-10-08 11:11:23 +02:00
|
|
|
IfConv.init(MF);
|
2012-07-04 00:09:54 +00:00
|
|
|
|
2012-07-10 22:18:23 +00:00
|
|
|
// Visit blocks in dominator tree post-order. The post-order enables nested
|
|
|
|
// if-conversion in a single pass. The tryConvertIf() function may erase
|
|
|
|
// blocks, but only blocks dominated by the head block. This makes it safe to
|
|
|
|
// update the dominator tree while the post-order iterator is still active.
|
2022-07-17 01:33:28 -07:00
|
|
|
for (auto *DomNode : post_order(DomTree))
|
2024-10-08 10:27:22 +02:00
|
|
|
if (tryConvertIf(DomNode->getBlock()))
|
2012-07-04 00:09:54 +00:00
|
|
|
Changed = true;
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
2019-08-20 15:54:59 +00:00
|
|
|
|
2024-10-16 13:22:57 +05:30
|
|
|
PreservedAnalyses
|
|
|
|
EarlyIfConverterPass::run(MachineFunction &MF,
|
|
|
|
MachineFunctionAnalysisManager &MFAM) {
|
|
|
|
MachineDominatorTree &MDT = MFAM.getResult<MachineDominatorTreeAnalysis>(MF);
|
|
|
|
MachineLoopInfo &LI = MFAM.getResult<MachineLoopAnalysis>(MF);
|
|
|
|
MachineTraceMetrics &MTM = MFAM.getResult<MachineTraceMetricsAnalysis>(MF);
|
|
|
|
|
|
|
|
EarlyIfConverter Impl(MDT, LI, MTM);
|
|
|
|
bool Changed = Impl.run(MF);
|
|
|
|
if (!Changed)
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
|
|
|
|
auto PA = getMachineFunctionPassPreservedAnalyses();
|
|
|
|
PA.preserve<MachineDominatorTreeAnalysis>();
|
|
|
|
PA.preserve<MachineLoopAnalysis>();
|
|
|
|
PA.preserve<MachineTraceMetricsAnalysis>();
|
|
|
|
return PA;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EarlyIfConverterLegacy::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
if (skipFunction(MF.getFunction()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineDominatorTree &MDT =
|
|
|
|
getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
|
|
|
|
MachineLoopInfo &LI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
|
|
|
|
MachineTraceMetrics &MTM =
|
|
|
|
getAnalysis<MachineTraceMetricsWrapperPass>().getMTM();
|
|
|
|
|
|
|
|
return EarlyIfConverter(MDT, LI, MTM).run(MF);
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:54:59 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// EarlyIfPredicator Pass
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class EarlyIfPredicator : public MachineFunctionPass {
|
2023-04-17 16:16:23 +08:00
|
|
|
const TargetInstrInfo *TII = nullptr;
|
2024-10-07 14:31:59 +02:00
|
|
|
const TargetRegisterInfo *TRI = nullptr;
|
2019-08-20 15:54:59 +00:00
|
|
|
TargetSchedModel SchedModel;
|
2024-10-07 14:31:59 +02:00
|
|
|
MachineRegisterInfo *MRI = nullptr;
|
2023-04-17 16:16:23 +08:00
|
|
|
MachineDominatorTree *DomTree = nullptr;
|
|
|
|
MachineBranchProbabilityInfo *MBPI = nullptr;
|
|
|
|
MachineLoopInfo *Loops = nullptr;
|
2024-10-08 10:27:22 +02:00
|
|
|
SSAIfConv IfConv;
|
2019-08-20 15:54:59 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
EarlyIfPredicator() : MachineFunctionPass(ID) {}
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
StringRef getPassName() const override { return "Early If-predicator"; }
|
|
|
|
|
|
|
|
protected:
|
2024-10-08 10:27:22 +02:00
|
|
|
bool tryConvertIf(MachineBasicBlock *);
|
|
|
|
bool shouldConvertIf();
|
2019-08-20 15:54:59 +00:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
#undef DEBUG_TYPE
|
|
|
|
#define DEBUG_TYPE "early-if-predicator"
|
|
|
|
|
|
|
|
char EarlyIfPredicator::ID = 0;
|
|
|
|
char &llvm::EarlyIfPredicatorID = EarlyIfPredicator::ID;
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(EarlyIfPredicator, DEBUG_TYPE, "Early If Predicator",
|
|
|
|
false, false)
|
2024-06-11 21:27:14 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
|
2024-06-28 10:59:23 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
|
2019-08-20 15:54:59 +00:00
|
|
|
INITIALIZE_PASS_END(EarlyIfPredicator, DEBUG_TYPE, "Early If Predicator", false,
|
|
|
|
false)
|
|
|
|
|
|
|
|
void EarlyIfPredicator::getAnalysisUsage(AnalysisUsage &AU) const {
|
2024-06-28 10:59:23 +08:00
|
|
|
AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
|
2024-06-11 21:27:14 +08:00
|
|
|
AU.addRequired<MachineDominatorTreeWrapperPass>();
|
|
|
|
AU.addPreserved<MachineDominatorTreeWrapperPass>();
|
2024-07-09 09:11:18 +08:00
|
|
|
AU.addRequired<MachineLoopInfoWrapperPass>();
|
|
|
|
AU.addPreserved<MachineLoopInfoWrapperPass>();
|
2019-08-20 15:54:59 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply the target heuristic to decide if the transformation is profitable.
|
2024-10-08 10:27:22 +02:00
|
|
|
bool EarlyIfPredicator::shouldConvertIf() {
|
2019-12-11 04:46:00 -05:00
|
|
|
auto TrueProbability = MBPI->getEdgeProbability(IfConv.Head, IfConv.TBB);
|
2019-08-20 15:54:59 +00:00
|
|
|
if (IfConv.isTriangle()) {
|
|
|
|
MachineBasicBlock &IfBlock =
|
|
|
|
(IfConv.TBB == IfConv.Tail) ? *IfConv.FBB : *IfConv.TBB;
|
|
|
|
|
|
|
|
unsigned ExtraPredCost = 0;
|
|
|
|
unsigned Cycles = 0;
|
|
|
|
for (MachineInstr &I : IfBlock) {
|
|
|
|
unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
|
|
|
|
if (NumCycles > 1)
|
|
|
|
Cycles += NumCycles - 1;
|
|
|
|
ExtraPredCost += TII->getPredicationCost(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TII->isProfitableToIfCvt(IfBlock, Cycles, ExtraPredCost,
|
2019-12-11 04:46:00 -05:00
|
|
|
TrueProbability);
|
2019-08-20 15:54:59 +00:00
|
|
|
}
|
|
|
|
unsigned TExtra = 0;
|
|
|
|
unsigned FExtra = 0;
|
|
|
|
unsigned TCycle = 0;
|
|
|
|
unsigned FCycle = 0;
|
|
|
|
for (MachineInstr &I : *IfConv.TBB) {
|
|
|
|
unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
|
|
|
|
if (NumCycles > 1)
|
|
|
|
TCycle += NumCycles - 1;
|
|
|
|
TExtra += TII->getPredicationCost(I);
|
|
|
|
}
|
|
|
|
for (MachineInstr &I : *IfConv.FBB) {
|
|
|
|
unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
|
|
|
|
if (NumCycles > 1)
|
|
|
|
FCycle += NumCycles - 1;
|
|
|
|
FExtra += TII->getPredicationCost(I);
|
|
|
|
}
|
|
|
|
return TII->isProfitableToIfCvt(*IfConv.TBB, TCycle, TExtra, *IfConv.FBB,
|
2019-12-11 04:46:00 -05:00
|
|
|
FCycle, FExtra, TrueProbability);
|
2019-08-20 15:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt repeated if-conversion on MBB, return true if successful.
|
|
|
|
///
|
2024-10-08 10:27:22 +02:00
|
|
|
bool EarlyIfPredicator::tryConvertIf(MachineBasicBlock *MBB) {
|
2019-08-20 15:54:59 +00:00
|
|
|
bool Changed = false;
|
2024-10-08 10:27:22 +02:00
|
|
|
while (IfConv.canConvertIf(MBB, /*Predicate*/ true) && shouldConvertIf()) {
|
2019-08-20 15:54:59 +00:00
|
|
|
// If-convert MBB and update analyses.
|
2024-07-31 19:20:49 +02:00
|
|
|
SmallVector<MachineBasicBlock *, 4> RemoveBlocks;
|
2024-10-08 10:27:22 +02:00
|
|
|
IfConv.convertIf(RemoveBlocks, /*Predicate*/ true);
|
2019-08-20 15:54:59 +00:00
|
|
|
Changed = true;
|
2024-07-31 19:20:49 +02:00
|
|
|
updateDomTree(DomTree, IfConv, RemoveBlocks);
|
|
|
|
for (MachineBasicBlock *MBB : RemoveBlocks)
|
|
|
|
MBB->eraseFromParent();
|
|
|
|
updateLoops(Loops, RemoveBlocks);
|
2019-08-20 15:54:59 +00:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EarlyIfPredicator::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
LLVM_DEBUG(dbgs() << "********** EARLY IF-PREDICATOR **********\n"
|
|
|
|
<< "********** Function: " << MF.getName() << '\n');
|
|
|
|
if (skipFunction(MF.getFunction()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const TargetSubtargetInfo &STI = MF.getSubtarget();
|
|
|
|
TII = STI.getInstrInfo();
|
2024-10-07 14:31:59 +02:00
|
|
|
TRI = STI.getRegisterInfo();
|
|
|
|
MRI = &MF.getRegInfo();
|
2019-08-20 15:54:59 +00:00
|
|
|
SchedModel.init(&STI);
|
2024-06-11 21:27:14 +08:00
|
|
|
DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
|
2024-07-09 09:11:18 +08:00
|
|
|
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
|
2024-06-28 10:59:23 +08:00
|
|
|
MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
|
2019-08-20 15:54:59 +00:00
|
|
|
|
|
|
|
bool Changed = false;
|
2024-10-08 11:11:23 +02:00
|
|
|
IfConv.init(MF);
|
2019-08-20 15:54:59 +00:00
|
|
|
|
|
|
|
// Visit blocks in dominator tree post-order. The post-order enables nested
|
|
|
|
// if-conversion in a single pass. The tryConvertIf() function may erase
|
|
|
|
// blocks, but only blocks dominated by the head block. This makes it safe to
|
|
|
|
// update the dominator tree while the post-order iterator is still active.
|
2022-07-17 01:33:28 -07:00
|
|
|
for (auto *DomNode : post_order(DomTree))
|
2024-10-08 10:27:22 +02:00
|
|
|
if (tryConvertIf(DomNode->getBlock()))
|
2019-08-20 15:54:59 +00:00
|
|
|
Changed = true;
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|