Fix a subtle bug with cleanups:  when activating
a previously-inactive cleanup, not only do we need a
flag variable, but we should also force the cleanup to
query the flag variable.  However, we only need to do
this when we're activating in a context that's
conditionally executed;  otherwise, we may safely
assume that the cleanup is dominated by the activation
point.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@144271 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGCleanup.cpp b/lib/CodeGen/CGCleanup.cpp
index b2d0786..9e079c6 100644
--- a/lib/CodeGen/CGCleanup.cpp
+++ b/lib/CodeGen/CGCleanup.cpp
@@ -1003,27 +1003,32 @@
                                         ForActivation_t Kind) {
   EHCleanupScope &Scope = cast<EHCleanupScope>(*CGF.EHStack.find(C));
 
-  // We always need the flag if we're activating the cleanup, because
-  // we have to assume that the current location doesn't necessarily
-  // dominate all future uses of the cleanup.
-  bool NeedFlag = (Kind == ForActivation);
+  // We always need the flag if we're activating the cleanup in a
+  // conditional context, because we have to assume that the current
+  // location doesn't necessarily dominate the cleanup's code.
+  bool isActivatedInConditional =
+    (Kind == ForActivation && CGF.isInConditionalBranch());
+
+  bool needFlag = false;
 
   // Calculate whether the cleanup was used:
 
   //   - as a normal cleanup
-  if (Scope.isNormalCleanup() && IsUsedAsNormalCleanup(CGF.EHStack, C)) {
+  if (Scope.isNormalCleanup() &&
+      (isActivatedInConditional || IsUsedAsNormalCleanup(CGF.EHStack, C))) {
     Scope.setTestFlagInNormalCleanup();
-    NeedFlag = true;
+    needFlag = true;
   }
 
   //  - as an EH cleanup
-  if (Scope.isEHCleanup() && IsUsedAsEHCleanup(CGF.EHStack, C)) {
+  if (Scope.isEHCleanup() &&
+      (isActivatedInConditional || IsUsedAsEHCleanup(CGF.EHStack, C))) {
     Scope.setTestFlagInEHCleanup();
-    NeedFlag = true;
+    needFlag = true;
   }
 
   // If it hasn't yet been used as either, we're done.
-  if (!NeedFlag) return;
+  if (!needFlag) return;
 
   llvm::AllocaInst *Var = Scope.getActiveFlag();
   if (!Var) {