Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion llvm/include/llvm/Transforms/Scalar/JumpTableToSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ namespace llvm {

class Function;

struct JumpTableToSwitchPass : PassInfoMixin<JumpTableToSwitchPass> {
class JumpTableToSwitchPass : public PassInfoMixin<JumpTableToSwitchPass> {
// Necessary until we switch to GUIDs as metadata, after which we can drop it.
const bool InLTO;

public:
explicit JumpTableToSwitchPass(bool InLTO = false) : InLTO(InLTO) {}
/// Run the pass over the function.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
};
Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/Passes/PassBuilderPipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,9 @@ PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,

// Jump table to switch conversion.
if (EnableJumpTableToSwitch)
FPM.addPass(JumpTableToSwitchPass());
FPM.addPass(JumpTableToSwitchPass(
/*InLTO=*/Phase == ThinOrFullLTOPhase::ThinLTOPostLink ||
Phase == ThinOrFullLTOPhase::FullLTOPostLink));

FPM.addPass(
SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true)));
Expand Down
22 changes: 8 additions & 14 deletions llvm/lib/Transforms/Scalar/JumpTableToSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,12 @@ PreservedAnalyses JumpTableToSwitchPass::run(Function &F,
PostDominatorTree *PDT = AM.getCachedResult<PostDominatorTreeAnalysis>(F);
DomTreeUpdater DTU(DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy);
bool Changed = false;
InstrProfSymtab Symtab;
if (auto E = Symtab.create(*F.getParent()))
F.getContext().emitError(
"Could not create indirect call table, likely corrupted IR" +
toString(std::move(E)));
DenseMap<const Function *, GlobalValue::GUID> FToGuid;
for (const auto &[G, FPtr] : Symtab.getIDToNameMap())
FToGuid.insert({FPtr, G});
auto FuncToGuid = [&](const Function &Fct) {
if (Fct.getMetadata(AssignGUIDPass::GUIDMetadataName))
return AssignGUIDPass::getGUID(Fct);

return Function::getGUIDAssumingExternalLinkage(getIRPGOFuncName(F, InLTO));
};

for (BasicBlock &BB : make_early_inc_range(F)) {
BasicBlock *CurrentBB = &BB;
Expand All @@ -230,12 +228,8 @@ PreservedAnalyses JumpTableToSwitchPass::run(Function &F,
std::optional<JumpTableTy> JumpTable = parseJumpTable(GEP, PtrTy);
if (!JumpTable)
continue;
SplittedOutTail = expandToSwitch(
Call, *JumpTable, DTU, ORE, [&](const Function &Fct) {
if (Fct.getMetadata(AssignGUIDPass::GUIDMetadataName))
return AssignGUIDPass::getGUID(Fct);
return FToGuid.lookup_or(&Fct, 0U);
});
SplittedOutTail =
expandToSwitch(Call, *JumpTable, DTU, ORE, FuncToGuid);
Changed = true;
break;
}
Expand Down