[llvm-branch-commits] [clang] e8559b2 - endbr

Ilya Leoshkevich via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Mar 2 10:22:25 PST 2023


Author: Ilya Leoshkevich
Date: 2023-02-24T19:09:23+01:00
New Revision: e8559b2e524b9ee0b6f3fa86643a9c05037ebcc4

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

LOG: endbr

Added: 
    llvm/lib/Target/SystemZ/SystemZEndbr.cpp

Modified: 
    clang/lib/Basic/Targets/SystemZ.h
    llvm/lib/Target/SystemZ/CMakeLists.txt
    llvm/lib/Target/SystemZ/SystemZ.h
    llvm/lib/Target/SystemZ/SystemZInstrInfo.td
    llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Basic/Targets/SystemZ.h b/clang/lib/Basic/Targets/SystemZ.h
index 3df4284d4c4f4..4c516a5196bf8 100644
--- a/clang/lib/Basic/Targets/SystemZ.h
+++ b/clang/lib/Basic/Targets/SystemZ.h
@@ -209,6 +209,11 @@ class LLVM_LIBRARY_VISIBILITY SystemZTargetInfo : public TargetInfo {
   int getEHDataRegisterNumber(unsigned RegNo) const override {
     return RegNo < 4 ? 6 + RegNo : -1;
   }
+
+  bool
+  checkCFProtectionBranchSupported(DiagnosticsEngine &Diags) const override {
+    return true;
+  };
 };
 } // namespace targets
 } // namespace clang

diff  --git a/llvm/lib/Target/SystemZ/CMakeLists.txt b/llvm/lib/Target/SystemZ/CMakeLists.txt
index ba845a74949fa..4de38ff391587 100644
--- a/llvm/lib/Target/SystemZ/CMakeLists.txt
+++ b/llvm/lib/Target/SystemZ/CMakeLists.txt
@@ -20,6 +20,7 @@ add_llvm_target(SystemZCodeGen
   SystemZConstantPoolValue.cpp
   SystemZCopyPhysRegs.cpp
   SystemZElimCompare.cpp
+  SystemZEndbr.cpp
   SystemZFrameLowering.cpp
   SystemZHazardRecognizer.cpp
   SystemZISelDAGToDAG.cpp

diff  --git a/llvm/lib/Target/SystemZ/SystemZ.h b/llvm/lib/Target/SystemZ/SystemZ.h
index cdd2850ad8e17..4463e1231b104 100644
--- a/llvm/lib/Target/SystemZ/SystemZ.h
+++ b/llvm/lib/Target/SystemZ/SystemZ.h
@@ -197,6 +197,7 @@ FunctionPass *createSystemZLDCleanupPass(SystemZTargetMachine &TM);
 FunctionPass *createSystemZCopyPhysRegsPass(SystemZTargetMachine &TM);
 FunctionPass *createSystemZPostRewritePass(SystemZTargetMachine &TM);
 FunctionPass *createSystemZTDCPass();
+FunctionPass *createSystemZEndbrPass();
 
 void initializeSystemZCopyPhysRegsPass(PassRegistry &);
 void initializeSystemZDAGToDAGISelPass(PassRegistry &);

diff  --git a/llvm/lib/Target/SystemZ/SystemZEndbr.cpp b/llvm/lib/Target/SystemZ/SystemZEndbr.cpp
new file mode 100644
index 0000000000000..1d6119de4cd36
--- /dev/null
+++ b/llvm/lib/Target/SystemZ/SystemZEndbr.cpp
@@ -0,0 +1,66 @@
+//===---- SystemZEndbr.cpp - ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "SystemZ.h"
+#include "SystemZSubtarget.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachineJumpTableInfo.h"
+#include "llvm/CodeGen/MachineModuleInfo.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "systemz-endbr"
+
+namespace {
+class SystemzEndbrPass : public MachineFunctionPass {
+public:
+  SystemzEndbrPass() : MachineFunctionPass(ID) {}
+
+  StringRef getPassName() const override {
+    return "SystemZ ENDBR";
+  }
+
+  bool runOnMachineFunction(MachineFunction &MF) override;
+
+private:
+  static char ID;
+};
+}
+
+FunctionPass *llvm::createSystemZEndbrPass() {
+  return new SystemzEndbrPass();
+}
+
+char SystemzEndbrPass::ID = 0;
+
+bool SystemzEndbrPass::runOnMachineFunction(MachineFunction &MF) {
+  if (!MF.getMMI().getModule()->getModuleFlag("cf-protection-branch"))
+    return false;
+
+  SmallPtrSet<const MachineBasicBlock *, 8> JumpTableTargets;
+  if (const MachineJumpTableInfo *JTI = MF.getJumpTableInfo())
+    for (const MachineJumpTableEntry &JTE : JTI->getJumpTables())
+      for (const MachineBasicBlock *MBB : JTE.MBBs)
+        JumpTableTargets.insert(MBB);
+
+  const SystemZSubtarget &SubTarget = MF.getSubtarget<SystemZSubtarget>();
+  const SystemZInstrInfo *TII = SubTarget.getInstrInfo();
+  int Count = 0;
+  for (MachineBasicBlock &MBB : MF) {
+    if (&MBB == &MF.front() ||
+        MBB.hasAddressTaken() ||
+        MBB.isEHPad() ||
+        JumpTableTargets.count(&MBB)) {
+      MachineBasicBlock::iterator I = MBB.begin();
+      BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(SystemZ::ENDBR));
+      Count++;
+    }
+  }
+
+  return Count;
+}

diff  --git a/llvm/lib/Target/SystemZ/SystemZInstrInfo.td b/llvm/lib/Target/SystemZ/SystemZInstrInfo.td
index c53cb7cadadb9..69fa5de664c23 100644
--- a/llvm/lib/Target/SystemZ/SystemZInstrInfo.td
+++ b/llvm/lib/Target/SystemZ/SystemZInstrInfo.td
@@ -124,6 +124,12 @@ def JNOP : InstAlias<"jnop\t$RI2", (BRCAsm 0, brtarget16:$RI2), 0>;
 // jgnop on att ; jlnop on hlasm
 def JGNOP : InstAlias<"{jgnop|jlnop}\t$RI2", (BRCLAsm 0, brtarget32:$RI2), 0>;
 
+def ENDBR : InstRILc<0xC04, (outs), (ins), "endbr", []> {
+  let M1 = 0;
+  let RI2 = 0;
+  let hasNoSchedulingInfo = 1; /* ??? */
+}
+
 // Fused compare-and-branch instructions.
 //
 // These instructions do not use or clobber the condition codes.

diff  --git a/llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp b/llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp
index 787c51645de16..2cf3108a68473 100644
--- a/llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp
@@ -302,6 +302,8 @@ void SystemZPassConfig::addPreEmitPass() {
   // after block placement).
   if (getOptLevel() != CodeGenOpt::None)
     addPass(&PostMachineSchedulerID);
+
+  addPass(createSystemZEndbrPass());
 }
 
 TargetPassConfig *SystemZTargetMachine::createPassConfig(PassManagerBase &PM) {


        


More information about the llvm-branch-commits mailing list