From 9cfbe7bbfea762d72b60c51c8ab5dadf6b317a9a Mon Sep 17 00:00:00 2001 From: Chen Zheng Date: Tue, 21 Jun 2022 00:26:17 -0400 Subject: [PATCH] [PowerPC][ctrloop] handles calls in preheader before MTCTRloop --- llvm/lib/Target/PowerPC/PPCCTRLoops.cpp | 15 +++++++-- llvm/test/CodeGen/PowerPC/ctrloops32.mir | 52 ++++++++++++++++++++++++++++++++ llvm/test/CodeGen/PowerPC/ctrloops64.mir | 52 ++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Target/PowerPC/PPCCTRLoops.cpp b/llvm/lib/Target/PowerPC/PPCCTRLoops.cpp index ea3bdb9..48167c3 100644 --- a/llvm/lib/Target/PowerPC/PPCCTRLoops.cpp +++ b/llvm/lib/Target/PowerPC/PPCCTRLoops.cpp @@ -108,6 +108,18 @@ bool PPCCTRLoops::runOnMachineFunction(MachineFunction &MF) { } bool PPCCTRLoops::isCTRClobber(MachineInstr *MI, bool CheckReads) const { + if (!CheckReads) { + // If we are only checking for defs, that is we are going to find + // definitions before MTCTRloop, for this case: + // CTR defination inside the callee of a call instruction will not impact + // the defination of MTCTRloop, so we can use definesRegister() for the + // check, no need to check the regmask. + return (MI->definesRegister(PPC::CTR) && + !MI->registerDefIsDead(PPC::CTR)) || + (MI->definesRegister(PPC::CTR8) && + !MI->registerDefIsDead(PPC::CTR8)); + } + if ((MI->modifiesRegister(PPC::CTR) && !MI->registerDefIsDead(PPC::CTR)) || (MI->modifiesRegister(PPC::CTR8) && !MI->registerDefIsDead(PPC::CTR8))) return true; @@ -115,9 +127,6 @@ bool PPCCTRLoops::isCTRClobber(MachineInstr *MI, bool CheckReads) const { if (MI->getDesc().isCall()) return true; - if (!CheckReads) - return false; - // We define the CTR in the loop preheader, so if there is any CTR reader in // the loop, we also can not use CTR loop form. if (MI->readsRegister(PPC::CTR) || MI->readsRegister(PPC::CTR8)) diff --git a/llvm/test/CodeGen/PowerPC/ctrloops32.mir b/llvm/test/CodeGen/PowerPC/ctrloops32.mir index 2153d73..90ee800 100644 --- a/llvm/test/CodeGen/PowerPC/ctrloops32.mir +++ b/llvm/test/CodeGen/PowerPC/ctrloops32.mir @@ -80,6 +80,58 @@ body: | BLR implicit $lr, implicit $rm ... --- +name: test_fail_call_in_preheader_after_mtctr +# CHECK: test_fail_call_in_preheader_after_mtctr +tracksRegLiveness: true +body: | + bb.0.entry: + + %0:gprc = LI 2048 + ; CHECK-NOT: MTCTR + ; CHECK-NOT: BDNZ + ; CHECK: ADDI + ; CHECK: CMPLWI + ; CHECK: BC + MTCTRPseudo killed %0:gprc, implicit-def dead $ctr + BL @test_fail_use_in_loop, csr_aix32, implicit-def dead $lr, implicit $rm + + bb.1: + + %1:crbitrc = DecreaseCTRPseudo 1, implicit-def dead $ctr, implicit $ctr + BC killed %1:crbitrc, %bb.1 + B %bb.2 + + bb.2: + + BLR implicit $lr, implicit $rm +... +--- +name: test_succ_call_in_preheader_before_mtctr +# CHECK: test_succ_call_in_preheader_before_mtctr +tracksRegLiveness: true +body: | + bb.0.entry: + + %0:gprc = LI 2048 + BL @test_fail_use_in_loop, csr_aix32, implicit-def dead $lr, implicit $rm + ; CHECK: MTCTR + ; CHECK: BDNZ + ; CHECK-NOT: ADDI + ; CHECK-NOT: CMPLWI + ; CHECK-NOT: BC + MTCTRPseudo killed %0:gprc, implicit-def dead $ctr + + bb.1: + + %1:crbitrc = DecreaseCTRPseudo 1, implicit-def dead $ctr, implicit $ctr + BC killed %1:crbitrc, %bb.1 + B %bb.2 + + bb.2: + + BLR implicit $lr, implicit $rm +... +--- name: test_fail_call_in_loop # CHECK: test_fail_call_in_loop tracksRegLiveness: true diff --git a/llvm/test/CodeGen/PowerPC/ctrloops64.mir b/llvm/test/CodeGen/PowerPC/ctrloops64.mir index 841faa3..1d4ed84 100644 --- a/llvm/test/CodeGen/PowerPC/ctrloops64.mir +++ b/llvm/test/CodeGen/PowerPC/ctrloops64.mir @@ -82,6 +82,58 @@ body: | BLR8 implicit $lr8, implicit $rm ... --- +name: test_fail_call_in_preheader_after_mtctr +# CHECK: test_fail_call_in_preheader_after_mtctr +tracksRegLiveness: true +body: | + bb.0.entry: + + %0:g8rc = LI8 2048 + ; CHECK-NOT: MTCTR8 + ; CHECK-NOT: BDNZ8 + ; CHECK: ADDI8 + ; CHECK: CMPLDI + ; CHECK: BC + MTCTR8Pseudo killed %0:g8rc, implicit-def dead $ctr8 + BL8 @test_fail_use_in_loop, csr_ppc64, implicit-def dead $lr8, implicit $rm + + bb.1: + + %1:crbitrc = DecreaseCTR8Pseudo 1, implicit-def dead $ctr8, implicit $ctr8 + BC killed %1:crbitrc, %bb.1 + B %bb.2 + + bb.2: + + BLR8 implicit $lr8, implicit $rm +... +--- +name: test_succ_call_in_preheader_before_mtctr +# CHECK: test_succ_call_in_preheader_before_mtctr +tracksRegLiveness: true +body: | + bb.0.entry: + + %0:g8rc = LI8 2048 + BL8 @test_fail_use_in_loop, csr_ppc64, implicit-def dead $lr8, implicit $rm + ; CHECK: MTCTR8 + ; CHECK: BDNZ8 + ; CHECK-NOT: ADDI8 + ; CHECK-NOT: CMPLDI + ; CHECK-NOT: BC + MTCTR8Pseudo killed %0:g8rc, implicit-def dead $ctr8 + + bb.1: + + %1:crbitrc = DecreaseCTR8Pseudo 1, implicit-def dead $ctr8, implicit $ctr8 + BC killed %1:crbitrc, %bb.1 + B %bb.2 + + bb.2: + + BLR8 implicit $lr8, implicit $rm +... +--- name: test_fail_call_in_loop # CHECK: test_fail_call_in_loop tracksRegLiveness: true -- 2.7.4