[llvm] 52f33f7 - [PowerPC] Enable redundant TOC save removal on AIX

Qiu Chaofan via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 21 23:30:22 PDT 2021


Author: Qiu Chaofan
Date: 2021-03-22T14:29:22+08:00
New Revision: 52f33f795305c1264e9ee03e0899d419a44d12c3

URL: https://github.com/llvm/llvm-project/commit/52f33f795305c1264e9ee03e0899d419a44d12c3
DIFF: https://github.com/llvm/llvm-project/commit/52f33f795305c1264e9ee03e0899d419a44d12c3.diff

LOG: [PowerPC] Enable redundant TOC save removal on AIX

Reviewed By: shchenz

Differential Revision: https://reviews.llvm.org/D97039

Added: 
    

Modified: 
    llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
    llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
    llvm/test/CodeGen/PowerPC/remove-redundant-toc-saves.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp b/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
index bc25b37452b1..867bbd62b294 100644
--- a/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
+++ b/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
@@ -5156,7 +5156,8 @@ bool PPCInstrInfo::isTOCSaveMI(const MachineInstr &MI) const {
   unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
   unsigned StackOffset = MI.getOperand(1).getImm();
   Register StackReg = MI.getOperand(2).getReg();
-  if (StackReg == PPC::X1 && StackOffset == TOCSaveOffset)
+  Register SPReg = Subtarget.isPPC64() ? PPC::X1 : PPC::R1;
+  if (StackReg == SPReg && StackOffset == TOCSaveOffset)
     return true;
 
   return false;

diff  --git a/llvm/lib/Target/PowerPC/PPCMIPeephole.cpp b/llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
index c8b01aaef828..806908f23f2b 100644
--- a/llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
+++ b/llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
@@ -226,28 +226,30 @@ getKnownLeadingZeroCount(MachineInstr *MI, const PPCInstrInfo *TII) {
 void PPCMIPeephole::UpdateTOCSaves(
   std::map<MachineInstr *, bool> &TOCSaves, MachineInstr *MI) {
   assert(TII->isTOCSaveMI(*MI) && "Expecting a TOC save instruction here");
-  assert(MF->getSubtarget<PPCSubtarget>().isELFv2ABI() &&
-         "TOC-save removal only supported on ELFv2");
-  PPCFunctionInfo *FI = MF->getInfo<PPCFunctionInfo>();
-
-  MachineBasicBlock *Entry = &MF->front();
-  uint64_t CurrBlockFreq = MBFI->getBlockFreq(MI->getParent()).getFrequency();
-
-  // If the block in which the TOC save resides is in a block that
-  // post-dominates Entry, or a block that is hotter than entry (keep in mind
-  // that early MachineLICM has already run so the TOC save won't be hoisted)
-  // we can just do the save in the prologue.
-  if (CurrBlockFreq > EntryFreq || MPDT->dominates(MI->getParent(), Entry))
-    FI->setMustSaveTOC(true);
-
-  // If we are saving the TOC in the prologue, all the TOC saves can be removed
-  // from the code.
-  if (FI->mustSaveTOC()) {
-    for (auto &TOCSave : TOCSaves)
-      TOCSave.second = false;
-    // Add new instruction to map.
-    TOCSaves[MI] = false;
-    return;
+  // FIXME: Saving TOC in prologue hasn't been implemented well in AIX ABI part,
+  // here only support it under ELFv2.
+  if (MF->getSubtarget<PPCSubtarget>().isELFv2ABI()) {
+    PPCFunctionInfo *FI = MF->getInfo<PPCFunctionInfo>();
+
+    MachineBasicBlock *Entry = &MF->front();
+    uint64_t CurrBlockFreq = MBFI->getBlockFreq(MI->getParent()).getFrequency();
+
+    // If the block in which the TOC save resides is in a block that
+    // post-dominates Entry, or a block that is hotter than entry (keep in mind
+    // that early MachineLICM has already run so the TOC save won't be hoisted)
+    // we can just do the save in the prologue.
+    if (CurrBlockFreq > EntryFreq || MPDT->dominates(MI->getParent(), Entry))
+      FI->setMustSaveTOC(true);
+
+    // If we are saving the TOC in the prologue, all the TOC saves can be
+    // removed from the code.
+    if (FI->mustSaveTOC()) {
+      for (auto &TOCSave : TOCSaves)
+        TOCSave.second = false;
+      // Add new instruction to map.
+      TOCSaves[MI] = false;
+      return;
+    }
   }
 
   bool Keep = true;
@@ -476,10 +478,12 @@ bool PPCMIPeephole::simplifyCode(void) {
         }
         break;
       }
+      case PPC::STW:
       case PPC::STD: {
         MachineFrameInfo &MFI = MF->getFrameInfo();
         if (MFI.hasVarSizedObjects() ||
-            !MF->getSubtarget<PPCSubtarget>().isELFv2ABI())
+            (!MF->getSubtarget<PPCSubtarget>().isELFv2ABI() &&
+             !MF->getSubtarget<PPCSubtarget>().isAIXABI()))
           break;
         // When encountering a TOC save instruction, call UpdateTOCSaves
         // to add it to the TOCSaves map and mark any existing TOC saves

diff  --git a/llvm/test/CodeGen/PowerPC/remove-redundant-toc-saves.ll b/llvm/test/CodeGen/PowerPC/remove-redundant-toc-saves.ll
index e20b9816f3d4..58c959bc0efa 100644
--- a/llvm/test/CodeGen/PowerPC/remove-redundant-toc-saves.ll
+++ b/llvm/test/CodeGen/PowerPC/remove-redundant-toc-saves.ll
@@ -1,9 +1,20 @@
 ; RUN: llc -verify-machineinstrs -mtriple=powerpc64le-unknown-linux-gnu < %s | FileCheck %s
+; RUN: llc -verify-machineinstrs -mtriple=powerpc64-ibm-aix-xcoff < %s | FileCheck %s --check-prefix=AIX64
+; RUN: llc -verify-machineinstrs -mtriple=powerpc-ibm-aix-xcoff < %s | FileCheck %s --check-prefix=AIX32
+
 define signext i32 @test1(i32 signext %i, i32 (i32)* nocapture %Func, i32 (i32)* nocapture %Func2) {
 entry:
 ; CHECK-LABEL: test1:
 ; CHECK:    std 2, 24(1)
 ; CHECK-NOT:    std 2, 24(1)
+
+; AIX64-LABEL: test1:
+; AIX64: std 2, 40(1)
+; AIX64-NOT: std 2, 40(1)
+
+; AIX32-LABEL: test1:
+; AIX32: stw 2, 20(1)
+; AIX32-NOT: std 2, 20(1)
   %call = tail call signext i32 %Func(i32 signext %i)
   %call1 = tail call signext i32 %Func2(i32 signext %i)
   %add2 = add nsw i32 %call1, %call
@@ -15,6 +26,14 @@ entry:
 ; CHECK-LABEL: test2:
 ; CHECK:    std 2, 24(1)
 ; CHECK-NOT:    std 2, 24(1)
+
+; AIX64-LABEL: test2:
+; AIX64: std 2, 40(1)
+; AIX64-NOT: std 2, 40(1)
+
+; AIX32-LABEL: test2:
+; AIX32: stw 2, 20(1)
+; AIX32-NOT: std 2, 20(1)
   %call = tail call signext i32 %Func(i32 signext %i)
   %tobool = icmp eq i32 %j, 0
   br i1 %tobool, label %if.end, label %if.then
@@ -38,6 +57,12 @@ define signext i32 @test3(i32 signext %i, i32 (i32)* nocapture %Func, i32 (i32)*
 ; CHECK-LABEL: test3:
 ; CHECK:    std 2, 24(1)
 ; CHECK-NOT:    std 2, 24(1)
+
+; AIX64-LABEL: test3:
+; AIX64-COUNT-3: std 2, 40(1)
+
+; AIX32-LABEL: test3:
+; AIX32-COUNT-3: stw 2, 20(1)
 entry:
   %tobool = icmp eq i32 %i, 0
   br i1 %tobool, label %if.else, label %if.then
@@ -62,6 +87,13 @@ define signext i32 @test4(i32 signext %i, i32 (i32)* nocapture %Func, i32 (i32)*
 ; CHECK:    std 2, 24(1)
 ; CHECK-NOT:    std 2, 24(1)
 
+; AIX64-LABEL: test4:
+; AIX64: std 2, 40(1)
+; AIX64-NOT: std 2, 40(1)
+
+; AIX32-LABEL: test4:
+; AIX32: stw 2, 20(1)
+; AIX32-NOT: std 2, 20(1)
 entry:
   %call = tail call signext i32 %Func(i32 signext %i)
   %tobool = icmp eq i32 %i, 0
@@ -87,6 +119,11 @@ entry:
 ; CHECK-LABEL: test5:
 ; CHECK:    std 2, 24(1)
 
+; AIX64-LABEL: test5:
+; AIX64: std 2, 40(1)
+
+; AIX32-LABEL: test5:
+; AIX32: stw 2, 20(1)
   %tobool = icmp eq i32 %i, 0
   br i1 %tobool, label %if.end, label %if.then
 
@@ -108,6 +145,13 @@ entry:
 ; CHECK:    std 2, 24(1)
 ; CHECK:    std 2, 24(1)
 
+; AIX64-LABEL: test6:
+; AIX64: std 2, 40(1)
+; AIX64: std 2, 40(1)
+
+; AIX32-LABEL: test6:
+; AIX32: stw 2, 20(1)
+; AIX32: stw 2, 20(1)
   %conv = sext i32 %i to i64
   %0 = alloca i8, i64 %conv, align 16
   %1 = bitcast i8* %0 to i32*


        


More information about the llvm-commits mailing list