[llvm] b3cd44f - Use SETNE directly rather than SUB/SETNE 0 for stack guard check

James Clarke via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 18 05:21:54 PST 2020


Author: James Clarke
Date: 2020-02-18T13:21:26Z
New Revision: b3cd44f80b8d6de76d41f1c78241d782c77cd193

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

LOG: Use SETNE directly rather than SUB/SETNE 0 for stack guard check

Summary:
Backends should fold the subtraction into the comparison, but not all
seem to. Moreover, on targets where pointers are not integers, such as
CHERI, an integer subtraction is not appropriate. Instead we should just
compare the two pointers directly, as this should work everywhere and
potentially generate more efficient code.

Reviewers: bogner, lebedev.ri, efriedma, t.p.northover, uweigand, sunfish

Reviewed By: lebedev.ri

Subscribers: dschuff, sbc100, arichardson, jgravelle-google, hiraditya, aheejin, llvm-commits

Tags: #llvm

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

Added: 
    

Modified: 
    llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
    llvm/test/CodeGen/SystemZ/stack-guard.ll
    llvm/test/CodeGen/Thumb/stack_guard_remat.ll
    llvm/test/CodeGen/WebAssembly/stack-protector.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index f1ca734217a7..e3c2139ef347 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -2596,17 +2596,13 @@ void SelectionDAGBuilder::visitSPDescriptorParent(StackProtectorDescriptor &SPD,
                         MachineMemOperand::MOVolatile);
   }
 
-  // Perform the comparison via a subtract/getsetcc.
-  EVT VT = Guard.getValueType();
-  SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, Guard, GuardVal);
-
+  // Perform the comparison via a getsetcc.
   SDValue Cmp = DAG.getSetCC(dl, TLI.getSetCCResultType(DAG.getDataLayout(),
                                                         *DAG.getContext(),
-                                                        Sub.getValueType()),
-                             Sub, DAG.getConstant(0, dl, VT), ISD::SETNE);
+                                                        Guard.getValueType()),
+                             Guard, GuardVal, ISD::SETNE);
 
-  // If the sub is not 0, then we know the guard/stackslot do not equal, so
-  // branch to failure MBB.
+  // If the guard/stackslot do not equal, branch to failure MBB.
   SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
                                MVT::Other, GuardVal.getOperand(0),
                                Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));

diff  --git a/llvm/test/CodeGen/SystemZ/stack-guard.ll b/llvm/test/CodeGen/SystemZ/stack-guard.ll
index 2908cbe92bbb..df74f15a8d47 100644
--- a/llvm/test/CodeGen/SystemZ/stack-guard.ll
+++ b/llvm/test/CodeGen/SystemZ/stack-guard.ll
@@ -11,7 +11,7 @@
 ; CHECK: sllg [[REG2]], [[REG2]], 32
 ; CHECK: ear [[REG2]], %a1
 ; CHECK: lg [[REG2]], 40([[REG2]])
-; CHECK: sg [[REG2]], {{[0-9]*}}(%r15)
+; CHECK: cg [[REG2]], {{[0-9]*}}(%r15)
 
 define i32 @test_stack_guard() #0 {
 entry:

diff  --git a/llvm/test/CodeGen/Thumb/stack_guard_remat.ll b/llvm/test/CodeGen/Thumb/stack_guard_remat.ll
index dcf85d262017..675888496a05 100644
--- a/llvm/test/CodeGen/Thumb/stack_guard_remat.ll
+++ b/llvm/test/CodeGen/Thumb/stack_guard_remat.ll
@@ -11,7 +11,7 @@
 ;PIC-NEXT:   add [[ORIGINAL_GUARD]], pc
 ;PIC-NEXT:   ldr [[ORIGINAL_GUARD]], {{\[}}[[ORIGINAL_GUARD]]{{\]}}
 ;PIC-NEXT:   ldr [[ORIGINAL_GUARD]], {{\[}}[[ORIGINAL_GUARD]]{{\]}}
-;PIC-NEXT:   subs {{r[0-9]+}}, [[ORIGINAL_GUARD]], [[SAVED_GUARD]]
+;PIC-NEXT:   cmp [[ORIGINAL_GUARD]], [[SAVED_GUARD]]
 
 ;PIC:      [[GUARD_STACK_OFFSET]]:
 ;PIC-NEXT:   .long 1028
@@ -26,7 +26,7 @@
 ;NO-PIC-NOT: LPC
 ;NO-PIC-NEXT:           ldr [[ORIGINAL_GUARD]], {{\[}}[[ORIGINAL_GUARD]]{{\]}}
 ;DYNAMIC-NO-PIC-NEXT:   ldr [[ORIGINAL_GUARD]], {{\[}}[[ORIGINAL_GUARD]]{{\]}}
-;NO-PIC-NEXT:           subs {{r[0-9]+}}, [[ORIGINAL_GUARD]], [[SAVED_GUARD]]
+;NO-PIC-NEXT:           cmp [[ORIGINAL_GUARD]], [[SAVED_GUARD]]
 
 ;STATIC:      [[GUARD_STACK_OFFSET]]:
 ;STATIC-NEXT:   .long 1028

diff  --git a/llvm/test/CodeGen/WebAssembly/stack-protector.ll b/llvm/test/CodeGen/WebAssembly/stack-protector.ll
index ef0bf94fcecc..2b6521b43770 100644
--- a/llvm/test/CodeGen/WebAssembly/stack-protector.ll
+++ b/llvm/test/CodeGen/WebAssembly/stack-protector.ll
@@ -1,7 +1,7 @@
 ; RUN: llc -verify-machineinstrs -mtriple=wasm32-unknown-unknown < %s | FileCheck -check-prefix=WASM32 %s
 
 ; WASM32: i32.load        28
-; WASM32-NEXT: i32.sub
+; WASM32-NEXT: i32.ne
 ; WASM32-NEXT: br_if           0
 
 ; WASM32: __stack_chk_fail


        


More information about the llvm-commits mailing list