[llvm] [aarch64] XOR the frame pointer with the stack cookie when protecting the stack (PR #161114)
    Pan Tao via llvm-commits 
    llvm-commits at lists.llvm.org
       
    Thu Oct  9 18:43:10 PDT 2025
    
    
  
https://github.com/PanTao2 updated https://github.com/llvm/llvm-project/pull/161114
>From 9c6d4c7ef323e45a8171b63517e05a510aef1f84 Mon Sep 17 00:00:00 2001
From: "Pan, Tao" <tao.pan at intel.com>
Date: Mon, 29 Sep 2025 09:43:17 +0800
Subject: [PATCH 1/2] [aarch64] XOR the frame pointer with the stack cookie
 when protecting the stack
---
 llvm/lib/Target/AArch64/AArch64ISelLowering.cpp     | 12 +++++++++++-
 llvm/lib/Target/AArch64/AArch64ISelLowering.h       |  3 +++
 llvm/lib/Target/AArch64/AArch64SelectionDAGInfo.cpp |  2 +-
 3 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index a4c1e265f0e63..fffc060bbe7cd 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -28818,7 +28818,17 @@ void AArch64TargetLowering::ReplaceNodeResults(
 bool AArch64TargetLowering::useLoadStackGuardNode(const Module &M) const {
   if (Subtarget->isTargetAndroid() || Subtarget->isTargetFuchsia())
     return TargetLowering::useLoadStackGuardNode(M);
-  return true;
+  return false;
+}
+
+bool AArch64TargetLowering::useStackGuardXorFP() const { return true; }
+
+SDValue AArch64TargetLowering::emitStackGuardXorFP(SelectionDAG &DAG,
+                                                   SDValue Val,
+                                                   const SDLoc &DL) const {
+  return DAG.getNode(
+      ISD::XOR, DL, Val.getValueType(), Val,
+      DAG.getRegister(getStackPointerRegisterToSaveRestore(), MVT::i64));
 }
 
 unsigned AArch64TargetLowering::combineRepeatedFPDivisors() const {
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.h b/llvm/lib/Target/AArch64/AArch64ISelLowering.h
index d8072d15853ee..8073b0677a11b 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.h
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.h
@@ -349,6 +349,9 @@ class AArch64TargetLowering : public TargetLowering {
   shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *AI) const override;
 
   bool useLoadStackGuardNode(const Module &M) const override;
+  bool useStackGuardXorFP() const override;
+  SDValue emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val,
+                              const SDLoc &DL) const override;
   TargetLoweringBase::LegalizeTypeAction
   getPreferredVectorAction(MVT VT) const override;
 
diff --git a/llvm/lib/Target/AArch64/AArch64SelectionDAGInfo.cpp b/llvm/lib/Target/AArch64/AArch64SelectionDAGInfo.cpp
index d3b1aa621b61a..163de52386221 100644
--- a/llvm/lib/Target/AArch64/AArch64SelectionDAGInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64SelectionDAGInfo.cpp
@@ -32,7 +32,7 @@ AArch64SelectionDAGInfo::AArch64SelectionDAGInfo()
 
 void AArch64SelectionDAGInfo::verifyTargetNode(const SelectionDAG &DAG,
                                                const SDNode *N) const {
-  SelectionDAGGenTargetInfo::verifyTargetNode(DAG, N);
+  //  SelectionDAGGenTargetInfo::verifyTargetNode(DAG, N);
 
 #ifndef NDEBUG
   // Some additional checks not yet implemented by verifyTargetNode.
>From 3ab9aac28f23b7d3fd6328826ee521e711bd814d Mon Sep 17 00:00:00 2001
From: "Pan, Tao" <tao.pan at intel.com>
Date: Mon, 29 Sep 2025 15:21:25 +0800
Subject: [PATCH 2/2] Change DAG.getRegister to DAG.getCopyFromReg
---
 llvm/lib/Target/AArch64/AArch64ISelLowering.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index fffc060bbe7cd..148d334eb15d7 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -28826,9 +28826,10 @@ bool AArch64TargetLowering::useStackGuardXorFP() const { return true; }
 SDValue AArch64TargetLowering::emitStackGuardXorFP(SelectionDAG &DAG,
                                                    SDValue Val,
                                                    const SDLoc &DL) const {
-  return DAG.getNode(
-      ISD::XOR, DL, Val.getValueType(), Val,
-      DAG.getRegister(getStackPointerRegisterToSaveRestore(), MVT::i64));
+  return DAG.getNode(ISD::XOR, DL, Val.getValueType(), Val,
+                     DAG.getCopyFromReg(DAG.getEntryNode(), DL,
+                                        getStackPointerRegisterToSaveRestore(),
+                                        MVT::i64));
 }
 
 unsigned AArch64TargetLowering::combineRepeatedFPDivisors() const {
    
    
More information about the llvm-commits
mailing list