[llvm] r188105 - [stackprotector] Simplify SP Pass so that we emit different fail basic blocks for each fail condition.

Michael Gottesman mgottesman at apple.com
Fri Aug 9 14:26:18 PDT 2013


Author: mgottesman
Date: Fri Aug  9 16:26:18 2013
New Revision: 188105

URL: http://llvm.org/viewvc/llvm-project?rev=188105&view=rev
Log:
[stackprotector] Simplify SP Pass so that we emit different fail basic blocks for each fail condition.

This patch decouples the stack protector pass so that we can support stack
protector implementations that do not use the IR level generated stack protector
fail basic block.

No codesize increase is caused by this change since the MI level tail merge pass
properly merges together the fail condition blocks (see the updated test).

Modified:
    llvm/trunk/lib/CodeGen/StackProtector.cpp
    llvm/trunk/test/CodeGen/X86/stack-protector.ll

Modified: llvm/trunk/lib/CodeGen/StackProtector.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/StackProtector.cpp?rev=188105&r1=188104&r2=188105&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/StackProtector.cpp (original)
+++ llvm/trunk/lib/CodeGen/StackProtector.cpp Fri Aug  9 16:26:18 2013
@@ -312,8 +312,7 @@ static void CreatePrologue(Function *F,
 ///  - The epilogue checks the value stored in the prologue against the original
 ///    value. It calls __stack_chk_fail if they differ.
 bool StackProtector::InsertStackProtectors() {
-  BasicBlock *FailBB = 0;       // The basic block to jump to if check fails.
-  BasicBlock *FailBBDom = 0;    // FailBB's dominator.
+  bool HasPrologue = false;
   AllocaInst *AI = 0;           // Place on stack that stores the stack guard.
   Value *StackGuardVar = 0;  // The stack guard variable.
 
@@ -322,10 +321,9 @@ bool StackProtector::InsertStackProtecto
     ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator());
     if (!RI) continue;
 
-    if (!FailBB) {
+    if (!HasPrologue) {
+      HasPrologue = true;
       CreatePrologue(F, M, RI, TLI, Trip, AI, StackGuardVar);
-      // Create the basic block to jump to when the guard check fails.
-      FailBB = CreateFailBB();
     }
 
     // For each block with a return instruction, convert this:
@@ -350,12 +348,16 @@ bool StackProtector::InsertStackProtecto
     //     call void @__stack_chk_fail()
     //     unreachable
 
+    // Create the fail basic block.
+    BasicBlock *FailBB = CreateFailBB();
+
     // Split the basic block before the return instruction.
     BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return");
 
+    // Update the dominator tree if we need to.
     if (DT && DT->isReachableFromEntry(BB)) {
       DT->addNewBlock(NewBB, BB);
-      FailBBDom = FailBBDom ? DT->findNearestCommonDominator(FailBBDom, BB) :BB;
+      DT->addNewBlock(FailBB, BB);
     }
 
     // Remove default branch instruction to the new BB.
@@ -374,10 +376,8 @@ bool StackProtector::InsertStackProtecto
 
   // Return if we didn't modify any basic blocks. I.e., there are no return
   // statements in the function.
-  if (!FailBB) return false;
-
-  if (DT && FailBBDom)
-    DT->addNewBlock(FailBB, FailBBDom);
+  if (!HasPrologue)
+    return false;
 
   return true;
 }

Modified: llvm/trunk/test/CodeGen/X86/stack-protector.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/stack-protector.ll?rev=188105&r1=188104&r2=188105&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/stack-protector.ll (original)
+++ llvm/trunk/test/CodeGen/X86/stack-protector.ll Fri Aug  9 16:26:18 2013
@@ -2313,18 +2313,22 @@ entry:
 ; LINUX-I386-LABEL: test19d:
 ; LINUX-I386: mov{{l|q}} %gs:
 ; LINUX-I386: calll __stack_chk_fail
+; LINUX-I386-NOT: calll __stack_chk_fail
 
 ; LINUX-X64-LABEL: test19d:
 ; LINUX-X64: mov{{l|q}} %fs:
 ; LINUX-X64: callq __stack_chk_fail
+; LINUX-X64-NOT: callq __stack_chk_fail
 
 ; LINUX-KERNEL-X64-LABEL: test19d:
 ; LINUX-KERNEL-X64: mov{{l|q}} %gs:
 ; LINUX-KERNEL-X64: callq __stack_chk_fail
+; LINUX-KERNEL-X64-NOT: callq ___stack_chk_fail
 
 ; DARWIN-X64-LABEL: test19d:
 ; DARWIN-X64: mov{{l|q}} ___stack_chk_guard
 ; DARWIN-X64: callq ___stack_chk_fail
+; DARWIN-X64-NOT: callq ___stack_chk_fail
   %c = alloca %struct.pair, align 4
   %exn.slot = alloca i8*
   %ehselector.slot = alloca i32





More information about the llvm-commits mailing list