diff --git a/llvm/lib/Transforms/Scalar/CMakeLists.txt b/llvm/lib/Transforms/Scalar/CMakeLists.txt index 84a5b02043d01..765059d0c3b20 100644 --- a/llvm/lib/Transforms/Scalar/CMakeLists.txt +++ b/llvm/lib/Transforms/Scalar/CMakeLists.txt @@ -95,6 +95,7 @@ add_llvm_component_library(LLVMScalarOpts Analysis Core InstCombine + ProfileData Support TransformUtils ) diff --git a/llvm/lib/Transforms/Scalar/JumpTableToSwitch.cpp b/llvm/lib/Transforms/Scalar/JumpTableToSwitch.cpp index 8f5ec782d5ad5..9dde131185cf8 100644 --- a/llvm/lib/Transforms/Scalar/JumpTableToSwitch.cpp +++ b/llvm/lib/Transforms/Scalar/JumpTableToSwitch.cpp @@ -22,7 +22,6 @@ #include "llvm/ProfileData/InstrProf.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Error.h" -#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include @@ -181,8 +180,14 @@ expandToSwitch(CallBase *CB, const JumpTableTy &JT, DomTreeUpdater &DTU, if (HadProfile && !ProfcheckDisableMetadataFixes) { // At least one of the targets must've been taken. assert(llvm::any_of(BranchWeights, [](uint64_t V) { return V != 0; })); - setProfMetadata(F.getParent(), Switch, BranchWeights, - *llvm::max_element(BranchWeights)); + // FIXME: this duplicates logic in instrumentation. Note: since there's at + // least a nonzero and these are unsigned values, it follows MaxBW != 0. + uint64_t MaxBW = *llvm::max_element(BranchWeights); + SmallVector ScaledBranchWeights( + llvm::map_range(BranchWeights, [MaxBW](uint64_t V) { + return static_cast(V / MaxBW); + })); + setBranchWeights(*Switch, ScaledBranchWeights, /*IsExpected=*/false); } else setExplicitlyUnknownBranchWeights(*Switch); if (PHI)