Skip to content

Commit 488d392

Browse files
authored
[CodeGen][NewPM] Port EarlyIfConversion pass to NPM. (#108508)
1 parent e36b22f commit 488d392

18 files changed

+102
-30
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===- llvm/CodeGen/EarlyIfConversion.h -------------------------*- 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_EARLYIFCONVERSION_H
10+
#define LLVM_CODEGEN_EARLYIFCONVERSION_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
16+
class EarlyIfConverterPass : public PassInfoMixin<EarlyIfConverterPass> {
17+
public:
18+
PreservedAnalyses run(MachineFunction &MF,
19+
MachineFunctionAnalysisManager &MFAM);
20+
};
21+
22+
} // namespace llvm
23+
24+
#endif // LLVM_CODEGEN_EARLYIFCONVERSION_H

llvm/include/llvm/CodeGen/Passes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ namespace llvm {
273273

274274
/// EarlyIfConverter - This pass performs if-conversion on SSA form by
275275
/// inserting cmov instructions.
276-
extern char &EarlyIfConverterID;
276+
extern char &EarlyIfConverterLegacyID;
277277

278278
/// EarlyIfPredicator - This pass performs if-conversion on SSA form by
279279
/// predicating if/else block and insert select at the join point.

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void initializeDominatorTreeWrapperPassPass(PassRegistry &);
9898
void initializeDwarfEHPrepareLegacyPassPass(PassRegistry &);
9999
void initializeEarlyCSELegacyPassPass(PassRegistry &);
100100
void initializeEarlyCSEMemSSALegacyPassPass(PassRegistry &);
101-
void initializeEarlyIfConverterPass(PassRegistry &);
101+
void initializeEarlyIfConverterLegacyPass(PassRegistry &);
102102
void initializeEarlyIfPredicatorPass(PassRegistry &);
103103
void initializeEarlyMachineLICMPass(PassRegistry &);
104104
void initializeEarlyTailDuplicatePass(PassRegistry &);

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/CodeGen/CodeGenPrepare.h"
2828
#include "llvm/CodeGen/DeadMachineInstructionElim.h"
2929
#include "llvm/CodeGen/DwarfEHPrepare.h"
30+
#include "llvm/CodeGen/EarlyIfConversion.h"
3031
#include "llvm/CodeGen/ExpandLargeDivRem.h"
3132
#include "llvm/CodeGen/ExpandLargeFpConvert.h"
3233
#include "llvm/CodeGen/ExpandMemCmp.h"

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis())
129129
#define MACHINE_FUNCTION_PASS(NAME, CREATE_PASS)
130130
#endif
131131
MACHINE_FUNCTION_PASS("dead-mi-elimination", DeadMachineInstructionElimPass())
132+
MACHINE_FUNCTION_PASS("early-ifcvt", EarlyIfConverterPass())
132133
MACHINE_FUNCTION_PASS("early-machinelicm", EarlyMachineLICMPass())
133134
MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass())
134135
MACHINE_FUNCTION_PASS("localstackalloc", LocalStackSlotAllocationPass())
@@ -205,7 +206,6 @@ DUMMY_MACHINE_FUNCTION_PASS("cfi-fixup", CFIFixupPass)
205206
DUMMY_MACHINE_FUNCTION_PASS("cfi-instr-inserter", CFIInstrInserterPass)
206207
DUMMY_MACHINE_FUNCTION_PASS("detect-dead-lanes", DetectDeadLanesPass)
207208
DUMMY_MACHINE_FUNCTION_PASS("dot-machine-cfg", MachineCFGPrinter)
208-
DUMMY_MACHINE_FUNCTION_PASS("early-ifcvt", EarlyIfConverterPass)
209209
DUMMY_MACHINE_FUNCTION_PASS("early-tailduplication", EarlyTailDuplicatePass)
210210
DUMMY_MACHINE_FUNCTION_PASS("fentry-insert", FEntryInserterPass)
211211
DUMMY_MACHINE_FUNCTION_PASS("fixup-statepoint-caller-saved", FixupStatepointCallerSavedPass)

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
3535
initializeDebugifyMachineModulePass(Registry);
3636
initializeDetectDeadLanesPass(Registry);
3737
initializeDwarfEHPrepareLegacyPassPass(Registry);
38-
initializeEarlyIfConverterPass(Registry);
38+
initializeEarlyIfConverterLegacyPass(Registry);
3939
initializeEarlyIfPredicatorPass(Registry);
4040
initializeEarlyMachineLICMPass(Registry);
4141
initializeEarlyTailDuplicatePass(Registry);

llvm/lib/CodeGen/EarlyIfConversion.cpp

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//
1616
//===----------------------------------------------------------------------===//
1717

18+
#include "llvm/CodeGen/EarlyIfConversion.h"
1819
#include "llvm/ADT/BitVector.h"
1920
#include "llvm/ADT/PostOrderIterator.h"
2021
#include "llvm/ADT/SmallPtrSet.h"
@@ -760,7 +761,7 @@ void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
760761
//===----------------------------------------------------------------------===//
761762

762763
namespace {
763-
class EarlyIfConverter : public MachineFunctionPass {
764+
class EarlyIfConverter {
764765
const TargetInstrInfo *TII = nullptr;
765766
const TargetRegisterInfo *TRI = nullptr;
766767
MCSchedModel SchedModel;
@@ -772,31 +773,41 @@ class EarlyIfConverter : public MachineFunctionPass {
772773
SSAIfConv IfConv;
773774

774775
public:
775-
static char ID;
776-
EarlyIfConverter() : MachineFunctionPass(ID) {}
777-
void getAnalysisUsage(AnalysisUsage &AU) const override;
778-
bool runOnMachineFunction(MachineFunction &MF) override;
779-
StringRef getPassName() const override { return "Early If-Conversion"; }
776+
EarlyIfConverter(MachineDominatorTree &DT, MachineLoopInfo &LI,
777+
MachineTraceMetrics &MTM)
778+
: DomTree(&DT), Loops(&LI), Traces(&MTM) {}
779+
EarlyIfConverter() = delete;
780+
781+
bool run(MachineFunction &MF);
780782

781783
private:
782784
bool tryConvertIf(MachineBasicBlock *);
783785
void invalidateTraces();
784786
bool shouldConvertIf();
785787
};
788+
789+
class EarlyIfConverterLegacy : public MachineFunctionPass {
790+
public:
791+
static char ID;
792+
EarlyIfConverterLegacy() : MachineFunctionPass(ID) {}
793+
void getAnalysisUsage(AnalysisUsage &AU) const override;
794+
bool runOnMachineFunction(MachineFunction &MF) override;
795+
StringRef getPassName() const override { return "Early If-Conversion"; }
796+
};
786797
} // end anonymous namespace
787798

788-
char EarlyIfConverter::ID = 0;
789-
char &llvm::EarlyIfConverterID = EarlyIfConverter::ID;
799+
char EarlyIfConverterLegacy::ID = 0;
800+
char &llvm::EarlyIfConverterLegacyID = EarlyIfConverterLegacy::ID;
790801

791-
INITIALIZE_PASS_BEGIN(EarlyIfConverter, DEBUG_TYPE,
792-
"Early If Converter", false, false)
802+
INITIALIZE_PASS_BEGIN(EarlyIfConverterLegacy, DEBUG_TYPE, "Early If Converter",
803+
false, false)
793804
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
794805
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
795806
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetricsWrapperPass)
796-
INITIALIZE_PASS_END(EarlyIfConverter, DEBUG_TYPE,
797-
"Early If Converter", false, false)
807+
INITIALIZE_PASS_END(EarlyIfConverterLegacy, DEBUG_TYPE, "Early If Converter",
808+
false, false)
798809

799-
void EarlyIfConverter::getAnalysisUsage(AnalysisUsage &AU) const {
810+
void EarlyIfConverterLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
800811
AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
801812
AU.addRequired<MachineDominatorTreeWrapperPass>();
802813
AU.addPreserved<MachineDominatorTreeWrapperPass>();
@@ -1076,11 +1087,9 @@ bool EarlyIfConverter::tryConvertIf(MachineBasicBlock *MBB) {
10761087
return Changed;
10771088
}
10781089

1079-
bool EarlyIfConverter::runOnMachineFunction(MachineFunction &MF) {
1090+
bool EarlyIfConverter::run(MachineFunction &MF) {
10801091
LLVM_DEBUG(dbgs() << "********** EARLY IF-CONVERSION **********\n"
10811092
<< "********** Function: " << MF.getName() << '\n');
1082-
if (skipFunction(MF.getFunction()))
1083-
return false;
10841093

10851094
// Only run if conversion if the target wants it.
10861095
const TargetSubtargetInfo &STI = MF.getSubtarget();
@@ -1091,9 +1100,6 @@ bool EarlyIfConverter::runOnMachineFunction(MachineFunction &MF) {
10911100
TRI = STI.getRegisterInfo();
10921101
SchedModel = STI.getSchedModel();
10931102
MRI = &MF.getRegInfo();
1094-
DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
1095-
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
1096-
Traces = &getAnalysis<MachineTraceMetricsWrapperPass>().getMTM();
10971103
MinInstr = nullptr;
10981104

10991105
bool Changed = false;
@@ -1110,6 +1116,41 @@ bool EarlyIfConverter::runOnMachineFunction(MachineFunction &MF) {
11101116
return Changed;
11111117
}
11121118

1119+
PreservedAnalyses
1120+
EarlyIfConverterPass::run(MachineFunction &MF,
1121+
MachineFunctionAnalysisManager &MFAM) {
1122+
if (MF.getFunction().hasOptNone())
1123+
return PreservedAnalyses::all();
1124+
1125+
MachineDominatorTree &MDT = MFAM.getResult<MachineDominatorTreeAnalysis>(MF);
1126+
MachineLoopInfo &LI = MFAM.getResult<MachineLoopAnalysis>(MF);
1127+
MachineTraceMetrics &MTM = MFAM.getResult<MachineTraceMetricsAnalysis>(MF);
1128+
1129+
EarlyIfConverter Impl(MDT, LI, MTM);
1130+
bool Changed = Impl.run(MF);
1131+
if (!Changed)
1132+
return PreservedAnalyses::all();
1133+
1134+
auto PA = getMachineFunctionPassPreservedAnalyses();
1135+
PA.preserve<MachineDominatorTreeAnalysis>();
1136+
PA.preserve<MachineLoopAnalysis>();
1137+
PA.preserve<MachineTraceMetricsAnalysis>();
1138+
return PA;
1139+
}
1140+
1141+
bool EarlyIfConverterLegacy::runOnMachineFunction(MachineFunction &MF) {
1142+
if (skipFunction(MF.getFunction()))
1143+
return false;
1144+
1145+
MachineDominatorTree &MDT =
1146+
getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
1147+
MachineLoopInfo &LI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
1148+
MachineTraceMetrics &MTM =
1149+
getAnalysis<MachineTraceMetricsWrapperPass>().getMTM();
1150+
1151+
return EarlyIfConverter(MDT, LI, MTM).run(MF);
1152+
}
1153+
11131154
//===----------------------------------------------------------------------===//
11141155
// EarlyIfPredicator Pass
11151156
//===----------------------------------------------------------------------===//

llvm/lib/CodeGen/TargetPassConfig.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ static IdentifyingPassPtr overridePass(AnalysisID StandardID,
305305
if (StandardID == &DeadMachineInstructionElimID)
306306
return applyDisable(TargetID, DisableMachineDCE);
307307

308-
if (StandardID == &EarlyIfConverterID)
308+
if (StandardID == &EarlyIfConverterLegacyID)
309309
return applyDisable(TargetID, DisableEarlyIfConversion);
310310

311311
if (StandardID == &EarlyMachineLICMID)
@@ -521,7 +521,7 @@ void llvm::registerCodeGenCallback(PassInstrumentationCallbacks &PIC,
521521
DISABLE_PASS(DisableBlockPlacement, MachineBlockPlacementPass)
522522
DISABLE_PASS(DisableBranchFold, BranchFolderPass)
523523
DISABLE_PASS(DisableCopyProp, MachineCopyPropagationPass)
524-
DISABLE_PASS(DisableEarlyIfConversion, EarlyIfConverterPass)
524+
DISABLE_PASS(DisableEarlyIfConversion, EarlyIfConverterLegacyPass)
525525
DISABLE_PASS(DisableEarlyTailDup, EarlyTailDuplicatePass)
526526
DISABLE_PASS(DisableMachineCSE, MachineCSELegacyPass)
527527
DISABLE_PASS(DisableMachineDCE, DeadMachineInstructionElimPass)

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
#include "llvm/CodeGen/CodeGenPrepare.h"
8383
#include "llvm/CodeGen/DeadMachineInstructionElim.h"
8484
#include "llvm/CodeGen/DwarfEHPrepare.h"
85+
#include "llvm/CodeGen/EarlyIfConversion.h"
8586
#include "llvm/CodeGen/ExpandLargeDivRem.h"
8687
#include "llvm/CodeGen/ExpandLargeFpConvert.h"
8788
#include "llvm/CodeGen/ExpandMemCmp.h"

llvm/lib/Target/AArch64/AArch64TargetMachine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ bool AArch64PassConfig::addILPOpts() {
784784
if (EnableCondBrTuning)
785785
addPass(createAArch64CondBrTuning());
786786
if (EnableEarlyIfConversion)
787-
addPass(&EarlyIfConverterID);
787+
addPass(&EarlyIfConverterLegacyID);
788788
if (EnableStPairSuppress)
789789
addPass(createAArch64StorePairSuppressPass());
790790
addPass(createAArch64SIMDInstrOptPass());

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ void GCNPassConfig::addMachineSSAOptimization() {
13351335

13361336
bool GCNPassConfig::addILPOpts() {
13371337
if (EnableEarlyIfConversion)
1338-
addPass(&EarlyIfConverterID);
1338+
addPass(&EarlyIfConverterLegacyID);
13391339

13401340
TargetPassConfig::addILPOpts();
13411341
return false;

llvm/lib/Target/PowerPC/PPCTargetMachine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ bool PPCPassConfig::addPreISel() {
521521
}
522522

523523
bool PPCPassConfig::addILPOpts() {
524-
addPass(&EarlyIfConverterID);
524+
addPass(&EarlyIfConverterLegacyID);
525525

526526
if (EnableMachineCombinerPass)
527527
addPass(&MachineCombinerID);

llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ bool SystemZPassConfig::addInstSelector() {
257257
}
258258

259259
bool SystemZPassConfig::addILPOpts() {
260-
addPass(&EarlyIfConverterID);
260+
addPass(&EarlyIfConverterLegacyID);
261261

262262
if (EnableMachineCombinerPass)
263263
addPass(&MachineCombinerID);

llvm/lib/Target/X86/X86TargetMachine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ bool X86PassConfig::addGlobalInstructionSelect() {
536536
}
537537

538538
bool X86PassConfig::addILPOpts() {
539-
addPass(&EarlyIfConverterID);
539+
addPass(&EarlyIfConverterLegacyID);
540540
if (EnableMachineCombinerPass)
541541
addPass(&MachineCombinerID);
542542
addPass(createX86CmovConverterPass());

llvm/test/CodeGen/AArch64/early-ifcvt-likely-predictable.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
22
# RUN: llc -mtriple=arm64-apple-ios -mcpu=apple-m1 -run-pass=early-ifcvt -o - %s | FileCheck %s
3+
# RUN: llc -mtriple=arm64-apple-ios -mcpu=apple-m1 -passes=early-ifcvt -o - %s | FileCheck %s
34

45
--- |
56
define void @test_cond_is_load_with_invariant_ops() {

llvm/test/CodeGen/AArch64/early-ifcvt-regclass-mismatch.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -mtriple=aarch64-unknown-unknown -run-pass=early-ifcvt -verify-machineinstrs %s -o - | FileCheck %s
2+
# RUN: llc -mtriple=aarch64-unknown-unknown -passes=early-ifcvt -verify-each %s -o - | FileCheck %s
23
--- |
34
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
45
target triple = "arm64-apple-ios13.3.0"

llvm/test/CodeGen/AArch64/early-ifcvt-same-value.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
22
# RUN: llc -mtriple=aarch64-- -run-pass=early-ifcvt -stress-early-ifcvt -verify-machineinstrs %s -o - | FileCheck %s
3+
# RUN: llc -mtriple=aarch64-- -passes=early-ifcvt -stress-early-ifcvt %s -o - | FileCheck %s
34

45
---
56
name: fmov0

llvm/test/CodeGen/PowerPC/early-ifcvt-no-isel.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
22
# RUN: llc -mtriple=powerpc64-ibm-aix -mcpu=pwr7 -simplify-mir -verify-machineinstrs \
33
# RUN: -run-pass=early-ifcvt %s -o - | FileCheck %s
4+
# RUN: llc -mtriple=powerpc64-ibm-aix -mcpu=pwr7 -simplify-mir -verify-each \
5+
# RUN: -passes=early-ifcvt %s -o - | FileCheck %s
46

57
--- |
68
source_filename = "<stdin>"

0 commit comments

Comments
 (0)