77 #define DEBUG_TYPE "x86-cmov-conversion" 79 STATISTIC(NumOfSkippedCmovGroups,
"Number of unsupported CMOV-groups");
80 STATISTIC(NumOfCmovGroupCandidate,
"Number of CMOV-group candidates");
81 STATISTIC(NumOfLoopCandidate,
"Number of CMOV-conversion profitable loops");
82 STATISTIC(NumOfOptimizedCmovGroups,
"Number of optimized CMOV-groups");
87 cl::desc(
"Enable the X86 cmov-to-branch optimization."),
92 cl::desc(
"Minimum gain per loop (in cycles) threshold."),
96 "x86-cmov-converter-force-mem-operand",
97 cl::desc(
"Convert cmovs to branches whenever they have memory operands."),
107 StringRef getPassName()
const override {
return "X86 cmov Conversion"; }
131 CmovGroups &CmovInstGroups,
132 bool IncludeLoads =
false);
141 CmovGroups &CmovInstGroups);
153 void X86CmovConverterPass::getAnalysisUsage(
AnalysisUsage &AU)
const {
167 bool Changed =
false;
171 TII = STI.getInstrInfo();
172 TRI = STI.getRegisterInfo();
173 TSchedModel.init(&STI);
181 CmovGroups AllCmovGroups;
185 if (collectCmovCandidates(Blocks, AllCmovGroups,
true)) {
186 for (
auto &Group : AllCmovGroups) {
195 convertCmovInstsToBranches(Group);
227 for (
int i = 0; i < (int)
Loops.size(); ++i)
229 Loops.push_back(Child);
233 if (!CurrLoop->getSubLoops().empty())
237 CmovGroups CmovInstGroups;
239 if (!collectCmovCandidates(CurrLoop->getBlocks(), CmovInstGroups))
242 if (!checkForProfitableCmovCandidates(CurrLoop->getBlocks(),
247 for (
auto &Group : CmovInstGroups)
248 convertCmovInstsToBranches(Group);
254 bool X86CmovConverterPass::collectCmovCandidates(
278 for (
auto *MBB : Blocks) {
285 bool FoundNonCMOVInst =
false;
287 bool SkipGroup =
false;
289 for (
auto &
I : *MBB) {
291 if (
I.isDebugInstr())
302 FoundNonCMOVInst =
false;
308 if (FoundNonCMOVInst || (CC != FirstCC && CC != FirstOppCC))
315 else if (CC != MemOpCC)
322 MRI->use_nodbg_instructions(
I.defs().begin()->getReg()),
324 return UseI.
getOpcode() == X86::SUBREG_TO_REG;
336 FoundNonCMOVInst =
true;
339 if (
I.definesRegister(X86::EFLAGS)) {
343 CmovInstGroups.push_back(Group);
345 ++NumOfSkippedCmovGroups;
354 CmovInstGroups.push_back(Group);
356 ++NumOfSkippedCmovGroups;
359 NumOfCmovGroupCandidate += CmovInstGroups.size();
360 return !CmovInstGroups.empty();
372 divideCeil(TrueOpDepth * 3 + FalseOpDepth, 4),
373 divideCeil(FalseOpDepth * 3 + TrueOpDepth, 4));
376 bool X86CmovConverterPass::checkForProfitableCmovCandidates(
385 static const unsigned LoopIterations = 2;
387 DepthInfo LoopDepth[LoopIterations] = {{0, 0}, {0, 0}};
388 enum { PhyRegType = 0, VirRegType = 1, RegTypeNum = 2 };
396 DepthMap[
nullptr] = {0, 0};
399 for (
auto &Group : CmovInstGroups)
400 CmovInstructions.
insert(Group.begin(), Group.end());
425 for (
unsigned I = 0;
I < LoopIterations; ++
I) {
427 for (
auto *MBB : Blocks) {
429 RegDefMaps[PhyRegType].
clear();
432 if (
MI.isDebugInstr())
434 unsigned MIDepth = 0;
435 unsigned MIDepthOpt = 0;
436 bool IsCMOV = CmovInstructions.
count(&
MI);
437 for (
auto &MO :
MI.uses()) {
439 if (!MO.isReg() || !MO.isUse())
444 OperandToDefMap[&MO] =
DefMI;
446 MIDepth =
std::max(MIDepth, Info.Depth);
448 MIDepthOpt =
std::max(MIDepthOpt, Info.OptDepth);
454 DepthMap[OperandToDefMap.
lookup(&
MI.getOperand(1))].OptDepth,
455 DepthMap[OperandToDefMap.
lookup(&
MI.getOperand(2))].OptDepth);
458 for (
auto &MO :
MI.operands()) {
459 if (!MO.isReg() || !MO.isDef())
465 unsigned Latency = TSchedModel.computeInstrLatency(&
MI);
466 DepthMap[&
MI] = {MIDepth +=
Latency, MIDepthOpt += Latency};
467 MaxDepth.Depth =
std::max(MaxDepth.Depth, MIDepth);
468 MaxDepth.OptDepth =
std::max(MaxDepth.OptDepth, MIDepthOpt);
473 unsigned Diff[LoopIterations] = {LoopDepth[0].Depth - LoopDepth[0].OptDepth,
474 LoopDepth[1].Depth - LoopDepth[1].OptDepth};
506 bool WorthOptLoop =
false;
507 if (Diff[1] == Diff[0])
508 WorthOptLoop = Diff[0] * 8 >= LoopDepth[0].Depth;
509 else if (Diff[1] > Diff[0])
511 (Diff[1] - Diff[0]) * 2 >= (LoopDepth[1].
Depth - LoopDepth[0].
Depth) &&
512 (Diff[1] * 8 >= LoopDepth[1].Depth);
517 ++NumOfLoopCandidate;
530 unsigned MispredictPenalty = TSchedModel.getMCSchedModel()->MispredictPenalty;
531 CmovGroups TempGroups;
533 for (
auto &Group : TempGroups) {
534 bool WorthOpGroup =
true;
535 for (
auto *
MI : Group) {
539 auto UIs =
MRI->use_instructions(
MI->defs().begin()->getReg());
540 if (UIs.begin() != UIs.end() && ++UIs.begin() == UIs.end()) {
541 unsigned Op = UIs.begin()->getOpcode();
542 if (Op == X86::MOV64rm || Op == X86::MOV32rm) {
543 WorthOpGroup =
false;
549 DepthMap[OperandToDefMap.
lookup(&
MI->getOperand(4))].Depth;
551 DepthMap[OperandToDefMap.
lookup(&
MI->getOperand(1))].Depth,
552 DepthMap[OperandToDefMap.
lookup(&
MI->getOperand(2))].Depth);
553 if (ValCost > CondCost || (CondCost - ValCost) * 4 < MispredictPenalty) {
554 WorthOpGroup =
false;
560 CmovInstGroups.push_back(Group);
563 return !CmovInstGroups.empty();
576 for (
auto I = std::next(ItrMI),
E = BB->
end();
I !=
E; ++
I) {
577 if (
I->readsRegister(X86::EFLAGS))
579 if (
I->definesRegister(X86::EFLAGS))
585 if ((*I)->isLiveIn(X86::EFLAGS))
598 "Last instruction in a CMOV group must be a CMOV instruction");
602 if (
I->isDebugInstr())
608 for (
auto *
MI : DBGInstructions)
612 void X86CmovConverterPass::convertCmovInstsToBranches(
614 assert(!Group.
empty() &&
"No CMOV instructions to convert");
615 ++NumOfOptimizedCmovGroups;
719 auto FRIt = FalseBBRegRewriteTable.
find(FalseReg);
720 if (FRIt == FalseBBRegRewriteTable.
end())
722 FalseReg = FRIt->second;
724 FalseBBRegRewriteTable[MI.getOperand(0).getReg()] = FalseReg;
732 "Can only handle memory-operand cmov instructions with a condition " 733 "opposite to the selected branch direction.");
761 bool Unfolded =
TII->unfoldMemoryOperand(*MBB->
getParent(),
MI, TmpReg,
765 assert(Unfolded &&
"Should never fail to unfold a loading cmov!");
771 "Last new instruction isn't the expected CMOV!");
774 if (&*MIItBegin == &MI)
779 for (
auto *NewMI : NewMIs) {
781 FalseMBB->
insert(FalseInsertionPoint, NewMI);
783 for (
auto &MOp : NewMI->uses()) {
786 auto It = FalseBBRegRewriteTable.
find(MOp.getReg());
787 if (It == FalseBBRegRewriteTable.
end())
790 MOp.setReg(It->second);
796 MOp.setIsKill(
false);
803 FalseBBRegRewriteTable[NewCMOV->getOperand(0).getReg()] = TmpReg;
815 Register DestReg = MIIt->getOperand(0).getReg();
816 Register Op1Reg = MIIt->getOperand(1).getReg();
817 Register Op2Reg = MIIt->getOperand(2).getReg();
825 auto Op1Itr = RegRewriteTable.
find(Op1Reg);
826 if (Op1Itr != RegRewriteTable.
end())
827 Op1Reg = Op1Itr->second.first;
829 auto Op2Itr = RegRewriteTable.
find(Op2Reg);
830 if (Op2Itr != RegRewriteTable.
end())
831 Op2Reg = Op2Itr->second.second;
836 MIB =
BuildMI(*SinkMBB, SinkInsertionPoint, DL,
TII->get(X86::PHI), DestReg)
846 RegRewriteTable[DestReg] = std::make_pair(Op1Reg, Op2Reg);
850 MBB->
erase(MIItBegin, MIItEnd);
860 return new X86CmovConverterPass();
This class represents lattice values for constants.
static void packCmovGroup(MachineInstr *First, MachineInstr *Last)
Given /p First CMOV instruction and /p Last CMOV instruction representing a group of CMOV instruction...
void push_back(const T &Elt)
CondCode getCondFromCMov(const MachineInstr &MI)
Return condition code of a CMov opcode.
void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
iterator insertAfter(iterator I, MachineInstr *MI)
Insert MI into the instruction list after I.
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Provide an instruction scheduling machine model to CodeGen passes.
const HexagonInstrInfo * TII
static unsigned getDepthOfOptCmov(unsigned TrueOpDepth, unsigned FalseOpDepth)
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
static bool checkEFLAGSLive(MachineInstr *MI)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
Analysis containing CSE Info
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
TargetInstrInfo - Interface to description of machine instruction set.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
iterator find(const_arg_type_t< KeyT > Val)
static cl::opt< bool > EnableCmovConverter("x86-cmov-converter", cl::desc("Enable the X86 cmov-to-branch optimization."), cl::init(true), cl::Hidden)
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
MachineInstrBundleIterator< MachineInstr > iterator
unsigned const MachineRegisterInfo * MRI
LLVM Basic Block Representation.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
bool killsRegister(Register Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr kills the specified register.
static cl::opt< bool > ForceMemOperand("x86-cmov-converter-force-mem-operand", cl::desc("Convert cmovs to branches whenever they have memory operands."), cl::init(true), cl::Hidden)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static cl::opt< unsigned > GainCycleThreshold("x86-cmov-converter-threshold", cl::desc("Minimum gain per loop (in cycles) threshold."), cl::init(4), cl::Hidden)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent the analysis usage information of a pass.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
FunctionPass class - This class is used to implement most global optimizations.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
self_iterator getIterator()
succ_iterator succ_begin()
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass * createX86CmovConverterPass()
This pass converts X86 cmov instructions into branch when profitable.
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
Align max(MaybeAlign Lhs, Align Rhs)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
CondCode GetOppositeBranchCondition(CondCode CC)
GetOppositeBranchCondition - Return the inverse of the specified cond, e.g.
MachineInstrBuilder MachineInstrBuilder & DefMI
LLVM_NODISCARD T pop_back_val()
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
TargetSubtargetInfo - Generic base class for all target subtargets.
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
INITIALIZE_PASS_BEGIN(X86CmovConverterPass, DEBUG_TYPE, "X86 cmov Conversion", false, false) INITIALIZE_PASS_END(X86CmovConverterPass
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_NODISCARD bool empty() const
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator)
Returns the integer ceil(Numerator / Denominator).
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void insert(iterator MBBI, MachineBasicBlock *MBB)
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
StringRef - Represent a constant reference to a string, i.e.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Wrapper class representing virtual and physical registers.