diff --git a/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h b/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h --- a/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h +++ b/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h @@ -670,8 +670,12 @@ addPass(ExpandReductionsPass()); // Convert conditional moves to conditional jumps when profitable. - if (getOptLevel() != CodeGenOpt::None && !Opt.DisableSelectOptimize) - addPass(SelectOptimizePass()); + if (getOptLevel() != CodeGenOpt::None && !Opt.DisableSelectOptimize) { + // Enable only for x86 targets + auto ArchT = TM.getTargetTriple().getArch(); + if (ArchT == Triple::x86_64 || ArchT == Triple::x86) + addPass(SelectOptimizePass()); + } } /// Turn exception handling constructs into something the code generators can diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp --- a/llvm/lib/CodeGen/CodeGenPrepare.cpp +++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -6704,6 +6704,14 @@ if (DisableSelectToBranch) return false; + // Disable for x86 instrumentation-based PGO. + // The CodeGen SelectOptimize pass addresses x86 instr-PGO builds. + if ((PSI->hasInstrumentationProfile() || + PSI->hasCSInstrumentationProfile()) && + (TM->getTargetTriple().getArch() == Triple::x86_64 || + TM->getTargetTriple().getArch() == Triple::x86)) + return false; + // Find all consecutive select instructions that share the same condition. SmallVector ASI; ASI.push_back(SI); diff --git a/llvm/lib/CodeGen/SelectOptimize.cpp b/llvm/lib/CodeGen/SelectOptimize.cpp --- a/llvm/lib/CodeGen/SelectOptimize.cpp +++ b/llvm/lib/CodeGen/SelectOptimize.cpp @@ -167,6 +167,11 @@ FunctionPass *llvm::createSelectOptimizePass() { return new SelectOptimize(); } bool SelectOptimize::runOnFunction(Function &F) { + PSI = &getAnalysis().getPSI(); + // Enable only for instrumentation-based PGO. + if (!PSI->hasInstrumentationProfile() && !PSI->hasCSInstrumentationProfile()) + return false; + TM = &getAnalysis().getTM(); TSI = TM->getSubtargetImpl(F); TLI = TSI->getTargetLowering(); @@ -188,7 +193,6 @@ LI = &getAnalysis().getLoopInfo(); BPI.reset(new BranchProbabilityInfo(F, *LI)); BFI.reset(new BlockFrequencyInfo(F, *BPI, *LI)); - PSI = &getAnalysis().getPSI(); ORE = &getAnalysis().getORE(); TSchedModel.init(TSI); diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp --- a/llvm/lib/CodeGen/TargetPassConfig.cpp +++ b/llvm/lib/CodeGen/TargetPassConfig.cpp @@ -258,7 +258,7 @@ /// Disable the select optimization pass. static cl::opt DisableSelectOptimize( - "disable-select-optimize", cl::init(true), cl::Hidden, + "disable-select-optimize", cl::init(false), cl::Hidden, cl::desc("Disable the select-optimization pass from running")); /// Allow standard passes to be disabled by command line options. This supports @@ -930,8 +930,12 @@ addPass(createExpandReductionsPass()); // Convert conditional moves to conditional jumps when profitable. - if (getOptLevel() != CodeGenOpt::None && !DisableSelectOptimize) - addPass(createSelectOptimizePass()); + if (getOptLevel() != CodeGenOpt::None && !DisableSelectOptimize) { + // Enable only for x86 targets + auto ArchT = TM->getTargetTriple().getArch(); + if (ArchT == Triple::x86_64 || ArchT == Triple::x86) + addPass(createSelectOptimizePass()); + } } /// Turn exception handling constructs into something the code generators can diff --git a/llvm/lib/Target/X86/X86CmovConversion.cpp b/llvm/lib/Target/X86/X86CmovConversion.cpp --- a/llvm/lib/Target/X86/X86CmovConversion.cpp +++ b/llvm/lib/Target/X86/X86CmovConversion.cpp @@ -48,6 +48,7 @@ #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/Statistic.h" +#include "llvm/Analysis/ProfileSummaryInfo.h" #include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/CodeGen/MachineFunction.h" #include "llvm/CodeGen/MachineFunctionPass.h" @@ -154,6 +155,7 @@ void X86CmovConverterPass::getAnalysisUsage(AnalysisUsage &AU) const { MachineFunctionPass::getAnalysisUsage(AU); AU.addRequired(); + AU.addRequired(); } bool X86CmovConverterPass::runOnMachineFunction(MachineFunction &MF) { @@ -162,6 +164,12 @@ if (!EnableCmovConverter) return false; + auto *PSI = &getAnalysis().getPSI(); + // Disable for instrumentation-based PGO. + // The CodeGen SelectOptimize pass addresses instr-PGO builds. + if (PSI->hasInstrumentationProfile() || PSI->hasCSInstrumentationProfile()) + return false; + LLVM_DEBUG(dbgs() << "********** " << getPassName() << " : " << MF.getName() << "**********\n"); @@ -858,6 +866,7 @@ INITIALIZE_PASS_BEGIN(X86CmovConverterPass, DEBUG_TYPE, "X86 cmov Conversion", false, false) INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) +INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) INITIALIZE_PASS_END(X86CmovConverterPass, DEBUG_TYPE, "X86 cmov Conversion", false, false) diff --git a/llvm/test/CodeGen/X86/opt-pipeline.ll b/llvm/test/CodeGen/X86/opt-pipeline.ll --- a/llvm/test/CodeGen/X86/opt-pipeline.ll +++ b/llvm/test/CodeGen/X86/opt-pipeline.ll @@ -60,6 +60,12 @@ ; CHECK-NEXT: Expand vector predication intrinsics ; CHECK-NEXT: Scalarize Masked Memory Intrinsics ; CHECK-NEXT: Expand reduction intrinsics +; CHECK-NEXT: Natural Loop Information +; CHECK-NEXT: Lazy Branch Probability Analysis +; CHECK-NEXT: Lazy Block Frequency Analysis +; CHECK-NEXT: Optimization Remark Emitter +; CHECK-NEXT: Optimize selects +; CHECK-NEXT: Dominator Tree Construction ; CHECK-NEXT: Interleaved Access Pass ; CHECK-NEXT: X86 Partial Reduction ; CHECK-NEXT: Expand indirectbr instructions