Allocate stack storage for .block_descriptor and captured self at -O0.
This way the register allocator will not optimize away the debug info
for captured variables.

Fixes rdar://problem/12767564

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@177086 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGBlocks.cpp b/lib/CodeGen/CGBlocks.cpp
index c521bf9..77e29bd 100644
--- a/lib/CodeGen/CGBlocks.cpp
+++ b/lib/CodeGen/CGBlocks.cpp
@@ -1157,10 +1157,24 @@
     // There might not be a capture for 'self', but if there is...
     if (blockInfo.Captures.count(self)) {
       const CGBlockInfo::Capture &capture = blockInfo.getCapture(self);
+
       llvm::Value *selfAddr = Builder.CreateStructGEP(BlockPointer,
                                                       capture.getIndex(),
                                                       "block.captured-self");
-      LocalDeclMap[self] = selfAddr;
+
+      // At -O0 we generate an explicit alloca for self to facilitate debugging.
+      if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
+	llvm::Value *load = Builder.CreateLoad(selfAddr);
+
+	// Allocate a stack slot for it, so we can generate debug info for it
+	llvm::AllocaInst *alloca = CreateTempAlloca(load->getType(),
+                                                   "block.captured-self.addr");
+        unsigned align = getContext().getDeclAlign(self).getQuantity();
+        alloca->setAlignment(align);
+	Builder.CreateAlignedStore(load, alloca, align);
+        LocalDeclMap[self] = alloca;
+      } else
+        LocalDeclMap[self] = selfAddr;
     }
   }
 
@@ -1177,7 +1191,7 @@
       CreateMemTemp(variable->getType(), "block.captured-const");
     alloca->setAlignment(align);
 
-    Builder.CreateStore(capture.getConstant(), alloca, align);
+    Builder.CreateAlignedStore(capture.getConstant(), alloca, align);
 
     LocalDeclMap[variable] = alloca;
   }