Skip to content

Commit 9b0e1c2

Browse files
authored
[NewPM][CodeGen] Port finalize-isel to new pass manager (#94214)
It should preserve more analysis results, but it happens immediately after instruction selection.
1 parent cac5d0e commit 9b0e1c2

16 files changed

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

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/CodeGen/DwarfEHPrepare.h"
3030
#include "llvm/CodeGen/ExpandMemCmp.h"
3131
#include "llvm/CodeGen/ExpandReductions.h"
32+
#include "llvm/CodeGen/FinalizeISel.h"
3233
#include "llvm/CodeGen/GCMetadata.h"
3334
#include "llvm/CodeGen/GlobalMerge.h"
3435
#include "llvm/CodeGen/IndirectBrExpand.h"

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PI
124124
#define MACHINE_FUNCTION_PASS(NAME, CREATE_PASS)
125125
#endif
126126
MACHINE_FUNCTION_PASS("dead-mi-elimination", DeadMachineInstructionElimPass())
127+
MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass())
127128
MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass())
128129
MACHINE_FUNCTION_PASS("print", PrintMIRPass())
129130
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
@@ -169,7 +170,6 @@ DUMMY_MACHINE_FUNCTION_PASS("early-ifcvt", EarlyIfConverterPass)
169170
DUMMY_MACHINE_FUNCTION_PASS("early-machinelicm", EarlyMachineLICMPass)
170171
DUMMY_MACHINE_FUNCTION_PASS("early-tailduplication", EarlyTailDuplicatePass)
171172
DUMMY_MACHINE_FUNCTION_PASS("fentry-insert", FEntryInserterPass)
172-
DUMMY_MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass)
173173
DUMMY_MACHINE_FUNCTION_PASS("fixup-statepoint-caller-saved", FixupStatepointCallerSavedPass)
174174
DUMMY_MACHINE_FUNCTION_PASS("fs-profile-loader", MIRProfileLoaderNewPass)
175175
DUMMY_MACHINE_FUNCTION_PASS("funclet-layout", FuncletLayoutPass)

llvm/lib/CodeGen/FinalizeISel.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17+
#include "llvm/CodeGen/FinalizeISel.h"
1718
#include "llvm/CodeGen/MachineFrameInfo.h"
1819
#include "llvm/CodeGen/MachineFunction.h"
1920
#include "llvm/CodeGen/MachineFunctionPass.h"
@@ -40,13 +41,9 @@ namespace {
4041
};
4142
} // end anonymous namespace
4243

43-
char FinalizeISel::ID = 0;
44-
char &llvm::FinalizeISelID = FinalizeISel::ID;
45-
INITIALIZE_PASS(FinalizeISel, DEBUG_TYPE,
46-
"Finalize ISel and expand pseudo-instructions", false, false)
47-
48-
bool FinalizeISel::runOnMachineFunction(MachineFunction &MF) {
44+
static std::pair<bool, bool> runImpl(MachineFunction &MF) {
4945
bool Changed = false;
46+
bool PreserveCFG = true;
5047
const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
5148
const TargetLowering *TLI = MF.getSubtarget().getTargetLowering();
5249

@@ -68,6 +65,7 @@ bool FinalizeISel::runOnMachineFunction(MachineFunction &MF) {
6865
MachineBasicBlock *NewMBB = TLI->EmitInstrWithCustomInserter(MI, MBB);
6966
// The expansion may involve new basic blocks.
7067
if (NewMBB != MBB) {
68+
PreserveCFG = false;
7169
MBB = NewMBB;
7270
I = NewMBB->getIterator();
7371
MBBI = NewMBB->begin();
@@ -79,5 +77,25 @@ bool FinalizeISel::runOnMachineFunction(MachineFunction &MF) {
7977

8078
TLI->finalizeLowering(MF);
8179

82-
return Changed;
80+
return {Changed, PreserveCFG};
81+
}
82+
83+
char FinalizeISel::ID = 0;
84+
char &llvm::FinalizeISelID = FinalizeISel::ID;
85+
INITIALIZE_PASS(FinalizeISel, DEBUG_TYPE,
86+
"Finalize ISel and expand pseudo-instructions", false, false)
87+
88+
bool FinalizeISel::runOnMachineFunction(MachineFunction &MF) {
89+
return runImpl(MF).first;
90+
}
91+
92+
PreservedAnalyses FinalizeISelPass::run(MachineFunction &MF,
93+
MachineFunctionAnalysisManager &) {
94+
auto [Changed, PreserveCFG] = runImpl(MF);
95+
if (!Changed)
96+
return PreservedAnalyses::all();
97+
auto PA = getMachineFunctionPassPreservedAnalyses();
98+
if (PreserveCFG)
99+
PA.preserveSet<CFGAnalyses>();
100+
return PA;
83101
}

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/ExpandLargeDivRem.h"
8383
#include "llvm/CodeGen/ExpandLargeFpConvert.h"
8484
#include "llvm/CodeGen/ExpandMemCmp.h"
85+
#include "llvm/CodeGen/FinalizeISel.h"
8586
#include "llvm/CodeGen/GCMetadata.h"
8687
#include "llvm/CodeGen/GlobalMerge.h"
8788
#include "llvm/CodeGen/HardwareLoops.h"

llvm/test/CodeGen/AMDGPU/add_sub_u64_pseudos.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
22
# RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -run-pass=finalize-isel -o - %s | FileCheck -check-prefix=GFX11 %s
33
# RUN: llc -mtriple=amdgcn -mcpu=gfx1200 -run-pass=finalize-isel -o - %s | FileCheck -check-prefix=GFX12 %s
4+
# RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -passes=finalize-isel -o - %s | FileCheck -check-prefix=GFX11 %s
5+
# RUN: llc -mtriple=amdgcn -mcpu=gfx1200 -passes=finalize-isel -o - %s | FileCheck -check-prefix=GFX12 %s
46

57
---
68
name: reg_ops

llvm/test/CodeGen/AMDGPU/llvm.amdgcn.wave.reduce.umax.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 UTC_ARGS: --version 3
22
# RUN: llc -mtriple=amdgcn -run-pass=finalize-isel -verify-machineinstrs %s -o - | FileCheck -check-prefix=GCN %s
3+
# RUN: llc -mtriple=amdgcn -passes=finalize-isel -verify-machineinstrs %s -o - | FileCheck -check-prefix=GCN %s
34

45
---
56
name: uniform_value

llvm/test/CodeGen/AMDGPU/llvm.amdgcn.wave.reduce.umin.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 UTC_ARGS: --version 3
22
# RUN: llc -mtriple=amdgcn -run-pass=finalize-isel -verify-machineinstrs %s -o - | FileCheck -check-prefix=GCN %s
3+
# RUN: llc -mtriple=amdgcn -passes=finalize-isel -verify-machineinstrs %s -o - | FileCheck -check-prefix=GCN %s
34

45
---
56
name: uniform_value

llvm/test/CodeGen/Mips/call-site-info-output.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1+
;; TODO: Add -enable-new-pm
12
;; Test mips32:
23
; RUN: llc -mtriple=mips-linux-gnu -emit-call-site-info %s -stop-before=finalize-isel -o -| \
34
; RUN: llc -mtriple=mips-linux-gnu -emit-call-site-info -x='mir' -run-pass=finalize-isel -o -| FileCheck %s
5+
; RUN: llc -mtriple=mips-linux-gnu -emit-call-site-info %s -stop-before=finalize-isel -o -| \
6+
; RUN: llc -mtriple=mips-linux-gnu -emit-call-site-info -x='mir' -passes=finalize-isel -o -| FileCheck %s
47
;; Test mips64:
58
; RUN: llc -mtriple=mips64-linux-gnu -emit-call-site-info %s -stop-before=finalize-isel -o -| \
69
; RUN: llc -mtriple=mips64-linux-gnu -emit-call-site-info -x='mir' -run-pass=finalize-isel -o -| FileCheck %s --check-prefix=CHECK64
10+
; RUN: llc -mtriple=mips64-linux-gnu -emit-call-site-info %s -stop-before=finalize-isel -o -| \
11+
; RUN: llc -mtriple=mips64-linux-gnu -emit-call-site-info -x='mir' -passes=finalize-isel -o -| FileCheck %s --check-prefix=CHECK64
712
;; Test mipsel:
813
; RUN: llc -mtriple=mipsel-linux-gnu -emit-call-site-info %s -stop-before=finalize-isel -o -| \
914
; RUN: llc -mtriple=mipsel-linux-gnu -emit-call-site-info -x='mir' -run-pass=finalize-isel -o -| FileCheck %s
15+
; RUN: llc -mtriple=mipsel-linux-gnu -emit-call-site-info %s -stop-before=finalize-isel -o -| \
16+
; RUN: llc -mtriple=mipsel-linux-gnu -emit-call-site-info -x='mir' -passes=finalize-isel -o -| FileCheck %s
1017
;; Test mips64el:
1118
; RUN: llc -mtriple=mips64el-linux-gnu -emit-call-site-info %s -stop-before=finalize-isel -o -| \
1219
; RUN: llc -mtriple=mips64el-linux-gnu -emit-call-site-info -x='mir' -run-pass=finalize-isel -o -| FileCheck %s --check-prefix=CHECK64
20+
; RUN: llc -mtriple=mips64el-linux-gnu -emit-call-site-info %s -stop-before=finalize-isel -o -| \
21+
; RUN: llc -mtriple=mips64el-linux-gnu -emit-call-site-info -x='mir' -passes=finalize-isel -o -| FileCheck %s --check-prefix=CHECK64
1322

1423
;; Test call site info MIR parser and printer. Parser assertions and machine
1524
;; verifier will check the rest.

llvm/test/CodeGen/RISCV/rvv/tail-agnostic-impdef-copy.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 %s -mtriple=riscv64 -mattr=v -riscv-v-vector-bits-min=128 -run-pass=finalize-isel -o - | FileCheck %s
3+
# RUN: llc %s -mtriple=riscv64 -mattr=v -riscv-v-vector-bits-min=128 -passes=finalize-isel -o - | FileCheck %s
34

45
# This test makes sure we peak through the COPY instruction between the
56
# IMPLICIT_DEF and PseudoVLE64_V_M8_MASK in order to select the tail agnostic

llvm/test/CodeGen/RISCV/select-optimize-multiple.mir

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
# RUN: | FileCheck -check-prefix=RV32I %s
44
# RUN: llc -mtriple=riscv64 -run-pass=finalize-isel -simplify-mir -o - %s \
55
# RUN: | FileCheck -check-prefix=RV64I %s
6+
# RUN: llc -mtriple=riscv32 -passes=finalize-isel -simplify-mir -o - %s \
7+
# RUN: | FileCheck -check-prefix=RV32I %s
8+
# RUN: llc -mtriple=riscv64 -passes=finalize-isel -simplify-mir -o - %s \
9+
# RUN: | FileCheck -check-prefix=RV64I %s
610

711
# Provide dummy definitions of functions and just enough metadata to create a
812
# DBG_VALUE.

llvm/test/CodeGen/SystemZ/debuginstr-02.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#
44
# RUN: llc %s -mtriple=s390x-linux-gnu -mcpu=z13 -run-pass=finalize-isel \
55
# RUN: -o - 2>&1 | FileCheck %s
6+
# RUN: llc %s -mtriple=s390x-linux-gnu -mcpu=z13 -passes=finalize-isel \
7+
# RUN: -o - 2>&1 | FileCheck %s
68
#
79
# CHECK-LABEL: bb.1 (%ir-block.0):
810
# CHECK-NEXT: %5:fp32bit = PHI %1, %bb.0, %2, %bb.2

llvm/test/CodeGen/SystemZ/multiselect-02.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# RUN: llc -mtriple=s390x-linux-gnu -mcpu=z10 -run-pass=finalize-isel -o - %s \
22
# RUN: | FileCheck %s
3+
# RUN: llc -mtriple=s390x-linux-gnu -mcpu=z10 -passes=finalize-isel -o - %s \
4+
# RUN: | FileCheck %s
35
#
46
# Test that an instruction (ZEXT128) that uses custom insertion gets treated
57
# correctly also when it lies between two Select instructions that could

llvm/test/CodeGen/Thumb2/mve-tp-loop.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=thumbv8.1m.main-none-eabi -mattr=+mve -simplify-mir --verify-machineinstrs -run-pass=finalize-isel %s -o - | FileCheck %s
3+
# RUN: llc -mtriple=thumbv8.1m.main-none-eabi -mattr=+mve -simplify-mir -passes=finalize-isel %s -o - | FileCheck %s
34
--- |
45
target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
56
target triple = "arm-arm-none-eabi"

llvm/test/CodeGen/X86/call-site-info-output.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
; CHECK-NEXT: arg: 1, reg: '$esi'
1212
; CHECK-NEXT: arg: 2, reg: '$edx'
1313
; RUN: llc -emit-call-site-info %t.mir -run-pass=finalize-isel -o -| FileCheck %s --check-prefix=PARSER
14+
; RUN: llc -emit-call-site-info %t.mir -passes=finalize-isel -o -| FileCheck %s --check-prefix=PARSER
1415
; Verify that we are able to parse output mir and that we are getting the same result.
1516
; PARSER: name: fn2
1617
; PARSER: callSites:

llvm/test/CodeGen/X86/sjlj-shadow-stack-liveness.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -mtriple=x86_64-- -run-pass=finalize-isel -verify-machineinstrs -o - %s | FileCheck %s
2+
# RUN: llc -mtriple=x86_64-- -passes=finalize-isel -o - %s | FileCheck %s
23
# Check that we're not copying the kill flags with the operands from the pseudo
34
# instruction.
45
--- |

0 commit comments

Comments
 (0)