Skip to content

Commit 60eca67

Browse files
authored
[CodeGen] Port ExpandMemCmp to new pass manager (#74050)
1 parent 300a550 commit 60eca67

File tree

17 files changed

+95
-43
lines changed

17 files changed

+95
-43
lines changed

llvm/include/llvm/CodeGen/CodeGenPassBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
2626
#include "llvm/CodeGen/CallBrPrepare.h"
2727
#include "llvm/CodeGen/DwarfEHPrepare.h"
28+
#include "llvm/CodeGen/ExpandMemCmp.h"
2829
#include "llvm/CodeGen/ExpandReductions.h"
2930
#include "llvm/CodeGen/GCMetadata.h"
3031
#include "llvm/CodeGen/IndirectBrExpand.h"
@@ -628,7 +629,7 @@ void CodeGenPassBuilder<Derived>::addIRPasses(AddIRPass &addPass) const {
628629
// target lowering hook.
629630
if (!Opt.DisableMergeICmps)
630631
addPass(MergeICmpsPass());
631-
addPass(ExpandMemCmpPass());
632+
addPass(ExpandMemCmpPass(&TM));
632633
}
633634

634635
// Run GC lowering passes for builtin collectors
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===--- ExpandMemCmp.h - Expand memcmp() to load/stores --------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CODEGEN_EXPANDMEMCMP_H
10+
#define LLVM_CODEGEN_EXPANDMEMCMP_H
11+
12+
#include "llvm/IR/PassManager.h"
13+
14+
namespace llvm {
15+
16+
class TargetMachine;
17+
18+
class ExpandMemCmpPass : public PassInfoMixin<ExpandMemCmpPass> {
19+
const TargetMachine *TM;
20+
21+
public:
22+
explicit ExpandMemCmpPass(const TargetMachine *TM_) : TM(TM_) {}
23+
24+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
25+
};
26+
27+
} // namespace llvm
28+
29+
#endif // LLVM_CODEGEN_EXPANDMEMCMP_H

llvm/include/llvm/CodeGen/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ FUNCTION_PASS("dwarf-eh-prepare", DwarfEHPreparePass, (TM))
4646
FUNCTION_PASS("ee-instrument", EntryExitInstrumenterPass, (false))
4747
FUNCTION_PASS("expand-large-div-rem", ExpandLargeDivRemPass, ())
4848
FUNCTION_PASS("expand-large-fp-convert", ExpandLargeFpConvertPass, ())
49+
FUNCTION_PASS("expand-memcmp", ExpandMemCmpPass, (TM))
4950
FUNCTION_PASS("expand-reductions", ExpandReductionsPass, ())
5051
FUNCTION_PASS("expandvp", ExpandVectorPredicationPass, ())
5152
FUNCTION_PASS("indirectbr-expand", IndirectBrExpandPass, (TM))
@@ -130,7 +131,6 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis,
130131
#endif
131132
DUMMY_FUNCTION_PASS("atomic-expand", AtomicExpandPass, ())
132133
DUMMY_FUNCTION_PASS("codegenprepare", CodeGenPreparePass, ())
133-
DUMMY_FUNCTION_PASS("expandmemcmp", ExpandMemCmpPass, ())
134134
DUMMY_FUNCTION_PASS("gc-lowering", GCLoweringPass, ())
135135
DUMMY_FUNCTION_PASS("shadow-stack-gc-lowering", ShadowStackGCLoweringPass, ())
136136
DUMMY_FUNCTION_PASS("stack-protector", StackProtectorPass, ())

llvm/include/llvm/CodeGen/Passes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ namespace llvm {
520520
FunctionPass *createExpandLargeFpConvertPass();
521521

522522
// This pass expands memcmp() to load/stores.
523-
FunctionPass *createExpandMemCmpPass();
523+
FunctionPass *createExpandMemCmpLegacyPass();
524524

525525
/// Creates Break False Dependencies pass. \see BreakFalseDeps.cpp
526526
FunctionPass *createBreakFalseDeps();

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void initializeEdgeBundlesPass(PassRegistry&);
103103
void initializeEHContGuardCatchretPass(PassRegistry &);
104104
void initializeExpandLargeFpConvertLegacyPassPass(PassRegistry&);
105105
void initializeExpandLargeDivRemLegacyPassPass(PassRegistry&);
106-
void initializeExpandMemCmpPassPass(PassRegistry&);
106+
void initializeExpandMemCmpLegacyPassPass(PassRegistry &);
107107
void initializeExpandPostRAPass(PassRegistry&);
108108
void initializeExpandReductionsPass(PassRegistry&);
109109
void initializeExpandVectorPredicationPass(PassRegistry &);

llvm/include/llvm/LinkAllPasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ namespace {
119119
(void) llvm::createPostDomTree();
120120
(void) llvm::createMergeICmpsLegacyPass();
121121
(void) llvm::createExpandLargeDivRemPass();
122-
(void) llvm::createExpandMemCmpPass();
122+
(void)llvm::createExpandMemCmpLegacyPass();
123123
(void) llvm::createExpandVectorPredicationPass();
124124
std::string buf;
125125
llvm::raw_string_ostream os(buf);

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
4141
initializeEarlyTailDuplicatePass(Registry);
4242
initializeExpandLargeDivRemLegacyPassPass(Registry);
4343
initializeExpandLargeFpConvertLegacyPassPass(Registry);
44-
initializeExpandMemCmpPassPass(Registry);
44+
initializeExpandMemCmpLegacyPassPass(Registry);
4545
initializeExpandPostRAPass(Registry);
4646
initializeFEntryInserterPass(Registry);
4747
initializeFinalizeISelPass(Registry);

llvm/lib/CodeGen/ExpandMemCmp.cpp

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
#include "llvm/CodeGen/ExpandMemCmp.h"
1415
#include "llvm/ADT/Statistic.h"
1516
#include "llvm/Analysis/ConstantFolding.h"
1617
#include "llvm/Analysis/DomTreeUpdater.h"
@@ -38,7 +39,7 @@ namespace llvm {
3839
class TargetLowering;
3940
}
4041

41-
#define DEBUG_TYPE "expandmemcmp"
42+
#define DEBUG_TYPE "expand-memcmp"
4243

4344
STATISTIC(NumMemCmpCalls, "Number of memcmp calls");
4445
STATISTIC(NumMemCmpNotConstant, "Number of memcmp calls without constant size");
@@ -886,12 +887,24 @@ static bool expandMemCmp(CallInst *CI, const TargetTransformInfo *TTI,
886887
return true;
887888
}
888889

889-
class ExpandMemCmpPass : public FunctionPass {
890+
// Returns true if a change was made.
891+
static bool runOnBlock(BasicBlock &BB, const TargetLibraryInfo *TLI,
892+
const TargetTransformInfo *TTI, const TargetLowering *TL,
893+
const DataLayout &DL, ProfileSummaryInfo *PSI,
894+
BlockFrequencyInfo *BFI, DomTreeUpdater *DTU);
895+
896+
static PreservedAnalyses runImpl(Function &F, const TargetLibraryInfo *TLI,
897+
const TargetTransformInfo *TTI,
898+
const TargetLowering *TL,
899+
ProfileSummaryInfo *PSI,
900+
BlockFrequencyInfo *BFI, DominatorTree *DT);
901+
902+
class ExpandMemCmpLegacyPass : public FunctionPass {
890903
public:
891904
static char ID;
892905

893-
ExpandMemCmpPass() : FunctionPass(ID) {
894-
initializeExpandMemCmpPassPass(*PassRegistry::getPassRegistry());
906+
ExpandMemCmpLegacyPass() : FunctionPass(ID) {
907+
initializeExpandMemCmpLegacyPassPass(*PassRegistry::getPassRegistry());
895908
}
896909

897910
bool runOnFunction(Function &F) override {
@@ -928,25 +941,13 @@ class ExpandMemCmpPass : public FunctionPass {
928941
LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU);
929942
FunctionPass::getAnalysisUsage(AU);
930943
}
931-
932-
PreservedAnalyses runImpl(Function &F, const TargetLibraryInfo *TLI,
933-
const TargetTransformInfo *TTI,
934-
const TargetLowering *TL, ProfileSummaryInfo *PSI,
935-
BlockFrequencyInfo *BFI, DominatorTree *DT);
936-
// Returns true if a change was made.
937-
bool runOnBlock(BasicBlock &BB, const TargetLibraryInfo *TLI,
938-
const TargetTransformInfo *TTI, const TargetLowering *TL,
939-
const DataLayout &DL, ProfileSummaryInfo *PSI,
940-
BlockFrequencyInfo *BFI, DomTreeUpdater *DTU);
941944
};
942945

943-
bool ExpandMemCmpPass::runOnBlock(BasicBlock &BB, const TargetLibraryInfo *TLI,
944-
const TargetTransformInfo *TTI,
945-
const TargetLowering *TL,
946-
const DataLayout &DL, ProfileSummaryInfo *PSI,
947-
BlockFrequencyInfo *BFI,
948-
DomTreeUpdater *DTU) {
949-
for (Instruction& I : BB) {
946+
bool runOnBlock(BasicBlock &BB, const TargetLibraryInfo *TLI,
947+
const TargetTransformInfo *TTI, const TargetLowering *TL,
948+
const DataLayout &DL, ProfileSummaryInfo *PSI,
949+
BlockFrequencyInfo *BFI, DomTreeUpdater *DTU) {
950+
for (Instruction &I : BB) {
950951
CallInst *CI = dyn_cast<CallInst>(&I);
951952
if (!CI) {
952953
continue;
@@ -961,8 +962,7 @@ bool ExpandMemCmpPass::runOnBlock(BasicBlock &BB, const TargetLibraryInfo *TLI,
961962
return false;
962963
}
963964

964-
PreservedAnalyses
965-
ExpandMemCmpPass::runImpl(Function &F, const TargetLibraryInfo *TLI,
965+
PreservedAnalyses runImpl(Function &F, const TargetLibraryInfo *TLI,
966966
const TargetTransformInfo *TTI,
967967
const TargetLowering *TL, ProfileSummaryInfo *PSI,
968968
BlockFrequencyInfo *BFI, DominatorTree *DT) {
@@ -994,17 +994,32 @@ ExpandMemCmpPass::runImpl(Function &F, const TargetLibraryInfo *TLI,
994994

995995
} // namespace
996996

997-
char ExpandMemCmpPass::ID = 0;
998-
INITIALIZE_PASS_BEGIN(ExpandMemCmpPass, "expandmemcmp",
997+
PreservedAnalyses ExpandMemCmpPass::run(Function &F,
998+
FunctionAnalysisManager &FAM) {
999+
const auto *TL = TM->getSubtargetImpl(F)->getTargetLowering();
1000+
const auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
1001+
const auto &TTI = FAM.getResult<TargetIRAnalysis>(F);
1002+
auto *PSI = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F)
1003+
.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
1004+
BlockFrequencyInfo *BFI = (PSI && PSI->hasProfileSummary())
1005+
? &FAM.getResult<BlockFrequencyAnalysis>(F)
1006+
: nullptr;
1007+
auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
1008+
1009+
return runImpl(F, &TLI, &TTI, TL, PSI, BFI, DT);
1010+
}
1011+
1012+
char ExpandMemCmpLegacyPass::ID = 0;
1013+
INITIALIZE_PASS_BEGIN(ExpandMemCmpLegacyPass, DEBUG_TYPE,
9991014
"Expand memcmp() to load/stores", false, false)
10001015
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
10011016
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
10021017
INITIALIZE_PASS_DEPENDENCY(LazyBlockFrequencyInfoPass)
10031018
INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
10041019
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1005-
INITIALIZE_PASS_END(ExpandMemCmpPass, "expandmemcmp",
1020+
INITIALIZE_PASS_END(ExpandMemCmpLegacyPass, DEBUG_TYPE,
10061021
"Expand memcmp() to load/stores", false, false)
10071022

1008-
FunctionPass *llvm::createExpandMemCmpPass() {
1009-
return new ExpandMemCmpPass();
1023+
FunctionPass *llvm::createExpandMemCmpLegacyPass() {
1024+
return new ExpandMemCmpLegacyPass();
10101025
}

llvm/lib/CodeGen/TargetPassConfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ void TargetPassConfig::addIRPasses() {
868868
// target lowering hook.
869869
if (!DisableMergeICmps)
870870
addPass(createMergeICmpsLegacyPass());
871-
addPass(createExpandMemCmpPass());
871+
addPass(createExpandMemCmpLegacyPass());
872872
}
873873

874874
// Run GC lowering passes for builtin collectors

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#include "llvm/CodeGen/DwarfEHPrepare.h"
7777
#include "llvm/CodeGen/ExpandLargeDivRem.h"
7878
#include "llvm/CodeGen/ExpandLargeFpConvert.h"
79+
#include "llvm/CodeGen/ExpandMemCmp.h"
7980
#include "llvm/CodeGen/GCMetadata.h"
8081
#include "llvm/CodeGen/HardwareLoops.h"
8182
#include "llvm/CodeGen/IndirectBrExpand.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ FUNCTION_PASS("dse", DSEPass())
308308
FUNCTION_PASS("dwarf-eh-prepare", DwarfEHPreparePass(TM))
309309
FUNCTION_PASS("expand-large-div-rem", ExpandLargeDivRemPass(TM))
310310
FUNCTION_PASS("expand-large-fp-convert", ExpandLargeFpConvertPass(TM))
311+
FUNCTION_PASS("expand-memcmp", ExpandMemCmpPass(TM))
311312
FUNCTION_PASS("fix-irreducible", FixIrreduciblePass())
312313
FUNCTION_PASS("flattencfg", FlattenCFGPass())
313314
FUNCTION_PASS("float2int", Float2IntPass())

llvm/test/CodeGen/PowerPC/memcmp-mergeexpand.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
22
; RUN: llc -verify-machineinstrs -mcpu=pwr8 -mtriple=powerpc64le-unknown-gnu-linux < %s | FileCheck %s -check-prefix=PPC64LE
33

4-
; This tests interaction between MergeICmp and ExpandMemCmp.
4+
; This tests interaction between MergeICmp and expand-memcmp.
55

66
%"struct.std::pair" = type { i32, i32 }
77

llvm/test/Transforms/ExpandMemCmp/AArch64/memcmp.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
2-
; RUN: opt -S -expandmemcmp -memcmp-num-loads-per-block=1 -mtriple=aarch64-unknown-unknown < %s | FileCheck %s
2+
; RUN: opt -S -expand-memcmp -memcmp-num-loads-per-block=1 -mtriple=aarch64-unknown-unknown < %s | FileCheck %s
3+
; RUN: opt -S -passes=expand-memcmp -memcmp-num-loads-per-block=1 -mtriple=aarch64-unknown-unknown < %s | FileCheck %s
34

45
declare i32 @memcmp(ptr nocapture, ptr nocapture, i64)
56

llvm/test/Transforms/ExpandMemCmp/X86/bcmp.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2-
; RUN: opt -S -expandmemcmp -memcmp-num-loads-per-block=1 -mtriple=x86_64-unknown-unknown -data-layout=e-m:o-i64:64-f80:128-n8:16:32:64-S128 < %s | FileCheck %s --check-prefix=X64
2+
; RUN: opt -S -expand-memcmp -memcmp-num-loads-per-block=1 -mtriple=x86_64-unknown-unknown -data-layout=e-m:o-i64:64-f80:128-n8:16:32:64-S128 < %s | FileCheck %s --check-prefix=X64
3+
; RUN: opt -S -passes=expand-memcmp -memcmp-num-loads-per-block=1 -mtriple=x86_64-unknown-unknown -data-layout=e-m:o-i64:64-f80:128-n8:16:32:64-S128 < %s | FileCheck %s --check-prefix=X64
34

45
declare i32 @bcmp(ptr nocapture, ptr nocapture, i64)
56

llvm/test/Transforms/ExpandMemCmp/X86/memcmp-x32.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2-
; RUN: opt -S -expandmemcmp -mtriple=i686-unknown-unknown -data-layout=e-m:o-p:32:32-f64:32:64-f80:128-n8:16:32-S128 < %s | FileCheck %s --check-prefix=X32
2+
; RUN: opt -S -expand-memcmp -mtriple=i686-unknown-unknown -data-layout=e-m:o-p:32:32-f64:32:64-f80:128-n8:16:32-S128 < %s | FileCheck %s --check-prefix=X32
3+
; RUN: opt -S -passes=expand-memcmp -mtriple=i686-unknown-unknown -data-layout=e-m:o-p:32:32-f64:32:64-f80:128-n8:16:32-S128 < %s | FileCheck %s --check-prefix=X32
34

45
declare i32 @memcmp(ptr nocapture, ptr nocapture, i32)
56

llvm/test/Transforms/ExpandMemCmp/X86/memcmp.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2-
; RUN: opt -S -expandmemcmp -memcmp-num-loads-per-block=1 -mtriple=x86_64-unknown-unknown -data-layout=e-m:o-i64:64-f80:128-n8:16:32:64-S128 < %s | FileCheck %s --check-prefix=X64 --check-prefix=X64_1LD
3-
; RUN: opt -S -expandmemcmp -memcmp-num-loads-per-block=2 -mtriple=x86_64-unknown-unknown -data-layout=e-m:o-i64:64-f80:128-n8:16:32:64-S128 < %s | FileCheck %s --check-prefix=X64 --check-prefix=X64_2LD
2+
; RUN: opt -S -expand-memcmp -memcmp-num-loads-per-block=1 -mtriple=x86_64-unknown-unknown -data-layout=e-m:o-i64:64-f80:128-n8:16:32:64-S128 < %s | FileCheck %s --check-prefix=X64 --check-prefix=X64_1LD
3+
; RUN: opt -S -expand-memcmp -memcmp-num-loads-per-block=2 -mtriple=x86_64-unknown-unknown -data-layout=e-m:o-i64:64-f80:128-n8:16:32:64-S128 < %s | FileCheck %s --check-prefix=X64 --check-prefix=X64_2LD
4+
; RUN: opt -S -passes=expand-memcmp -memcmp-num-loads-per-block=1 -mtriple=x86_64-unknown-unknown -data-layout=e-m:o-i64:64-f80:128-n8:16:32:64-S128 < %s | FileCheck %s --check-prefix=X64 --check-prefix=X64_1LD
5+
; RUN: opt -S -passes=expand-memcmp -memcmp-num-loads-per-block=2 -mtriple=x86_64-unknown-unknown -data-layout=e-m:o-i64:64-f80:128-n8:16:32:64-S128 < %s | FileCheck %s --check-prefix=X64 --check-prefix=X64_2LD
46

57
declare i32 @memcmp(ptr nocapture, ptr nocapture, i64)
68

llvm/tools/opt/opt.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ static bool shouldPinPassToLegacyPM(StringRef Pass) {
356356
"expand-reductions",
357357
"indirectbr-expand",
358358
"generic-to-nvvm",
359-
"expandmemcmp",
359+
"expand-memcmp",
360360
"loop-reduce",
361361
"lower-amx-type",
362362
"lower-amx-intrinsics",
@@ -422,7 +422,7 @@ int main(int argc, char **argv) {
422422
// supported.
423423
initializeExpandLargeDivRemLegacyPassPass(Registry);
424424
initializeExpandLargeFpConvertLegacyPassPass(Registry);
425-
initializeExpandMemCmpPassPass(Registry);
425+
initializeExpandMemCmpLegacyPassPass(Registry);
426426
initializeScalarizeMaskedMemIntrinLegacyPassPass(Registry);
427427
initializeSelectOptimizePass(Registry);
428428
initializeCallBrPreparePass(Registry);

0 commit comments

Comments
 (0)