Fix code emission for conditional branches.
Patch by Collin Winter!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@70898 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/CaptureTracking.cpp b/lib/Analysis/CaptureTracking.cpp
index ceb9646..773f53d 100644
--- a/lib/Analysis/CaptureTracking.cpp
+++ b/lib/Analysis/CaptureTracking.cpp
@@ -49,11 +49,7 @@
     switch (I->getOpcode()) {
     case Instruction::Call:
     case Instruction::Invoke: {
-      CallSite CS = CallSite::get(I);
-      // Not captured if the callee is readonly and doesn't return a copy
-      // through its return value.
-      if (CS.onlyReadsMemory() && I->getType() == Type::VoidTy)
-        break;
+      CallSite CS(I);
 
       // Not captured if only passed via 'nocapture' arguments.  Note that
       // calling a function pointer does not in itself cause the pointer to
@@ -62,46 +58,69 @@
       // that loading a value from a pointer does not cause the pointer to be
       // captured, even though the loaded value might be the pointer itself
       // (think of self-referential objects).
+      bool MayBeCaptured = false;
       CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
       for (CallSite::arg_iterator A = B; A != E; ++A)
-        if (A->get() == V && !CS.paramHasAttr(A - B + 1, Attribute::NoCapture))
-          // The parameter is not marked 'nocapture' - captured.
-          return true;
-      // Only passed via 'nocapture' arguments, or is the called function - not
-      // captured.
+        if (A->get() == V && !CS.paramHasAttr(A-B+1, Attribute::NoCapture)) {
+          // The parameter is not marked 'nocapture' - handled by generic code
+          // below.
+          MayBeCaptured = true;
+          break;
+        }
+      if (!MayBeCaptured)
+        // Only passed via 'nocapture' arguments, or is the called function -
+        // not captured.
+        continue;
+      if (!CS.doesNotThrow())
+        // Even a readonly function can leak bits by throwing an exception or
+        // not depending on the input value.
+        return true;
+      // Fall through to the generic code.
       break;
     }
     case Instruction::Free:
       // Freeing a pointer does not cause it to be captured.
-      break;
+      continue;
     case Instruction::Load:
       // Loading from a pointer does not cause it to be captured.
-      break;
+      continue;
     case Instruction::Ret:
       if (ReturnCaptures)
         return true;
-      break;
+      continue;
     case Instruction::Store:
       if (V == I->getOperand(0))
         // Stored the pointer - it may be captured.
         return true;
       // Storing to the pointee does not cause the pointer to be captured.
-      break;
-    case Instruction::BitCast:
-    case Instruction::GetElementPtr:
-    case Instruction::PHI:
-    case Instruction::Select:
-      // The original value is not captured via this if the new value isn't.
-      for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end();
-           UI != UE; ++UI) {
-        Use *U = &UI.getUse();
-        if (Visited.insert(U))
-          Worklist.push_back(U);
-      }
-      break;
-    default:
-      // Something else - be conservative and say it is captured.
+      continue;
+    }
+
+    // If it may write to memory and isn't one of the special cases above,
+    // be conservative and assume the pointer is captured.
+    if (I->mayWriteToMemory())
       return true;
+
+    // If the instruction doesn't write memory, it can only capture by
+    // having its own value depend on the input value.
+    const Type* Ty = I->getType();
+    if (Ty == Type::VoidTy)
+      // The value of an instruction can't be a copy if it can't contain any
+      // information.
+      continue;
+    if (!isa<PointerType>(Ty))
+      // At the moment, we don't track non-pointer values, so be conservative
+      // and assume the pointer is captured.
+      // FIXME: Track these too.  This would need to be done very carefully as
+      // it is easy to leak bits via control flow if integer values are allowed.
+      return true;
+
+    // The original value is not captured via this if the new value isn't.
+    for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end();
+         UI != UE; ++UI) {
+      Use *U = &UI.getUse();
+      if (Visited.insert(U))
+        Worklist.push_back(U);
     }
   }
 
diff --git a/lib/CodeGen/AsmPrinter/DwarfWriter.cpp b/lib/CodeGen/AsmPrinter/DwarfWriter.cpp
index 848f45e..8ca8590 100644
--- a/lib/CodeGen/AsmPrinter/DwarfWriter.cpp
+++ b/lib/CodeGen/AsmPrinter/DwarfWriter.cpp
@@ -3262,11 +3262,12 @@
     // Assumes in correct section after the entry point.
     EmitLabel("func_begin", ++SubprogramCount);
 
-    // Emit label for the implicitly defined dbg.stoppoint at the start of
-    // the function.
-    if (!Lines.empty()) {
-      const SrcLineInfo &LineInfo = Lines[0];
-      Asm->printLabel(LineInfo.getLabelID());
+    DebugLoc FDL = MF->getDefaultDebugLoc();
+    if (!FDL.isUnknown()) {
+      DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
+      unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
+                                          DICompileUnit(DLT.CompileUnit));
+      Asm->printLabel(LabelID);
     }
 
     if (TimePassesIsEnabled)
diff --git a/lib/CodeGen/SelectionDAG/FastISel.cpp b/lib/CodeGen/SelectionDAG/FastISel.cpp
index afcda1f..f710051 100644
--- a/lib/CodeGen/SelectionDAG/FastISel.cpp
+++ b/lib/CodeGen/SelectionDAG/FastISel.cpp
@@ -333,11 +333,6 @@
       unsigned Col = SPI->getColumn();
       unsigned Idx = MF.getOrCreateDebugLocID(CU.getGV(), Line, Col);
       setCurDebugLoc(DebugLoc::get(Idx));
-      if (DW && DW->ShouldEmitDwarfDebug()) {
-        unsigned ID = DW->RecordSourceLine(Line, Col, CU);
-        const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
-        BuildMI(MBB, DL, II).addImm(ID);
-      }
     }
     return true;
   }
@@ -402,7 +397,7 @@
                                               CompileUnit.getGV(), Line, 0)));
 
       if (DW && DW->ShouldEmitDwarfDebug()) {
-        unsigned LabelID = DW->RecordSourceLine(Line, 0, CompileUnit);
+        unsigned LabelID = MMI->NextLabelID();
         const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
         BuildMI(MBB, DL, II).addImm(LabelID);
         DebugLocTuple PrevLocTpl = MF.getDebugLocTuple(PrevLoc);
@@ -414,10 +409,9 @@
     } else {
       // Record the source line.
       unsigned Line = Subprogram.getLineNumber();
-      setCurDebugLoc(DebugLoc::get(MF.getOrCreateDebugLocID(
+      MF.setDefaultDebugLoc(DebugLoc::get(MF.getOrCreateDebugLocID(
                                               CompileUnit.getGV(), Line, 0)));
       if (DW && DW->ShouldEmitDwarfDebug()) {
-        DW->RecordSourceLine(Line, 0, CompileUnit);
         // llvm.dbg.func_start also defines beginning of function scope.
         DW->RecordRegionStart(cast<GlobalVariable>(FSI->getSubprogram()));
       }
diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp
index acdb043..fc45bbd 100644
--- a/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp
+++ b/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp
@@ -3980,7 +3980,7 @@
                      MF.getOrCreateDebugLocID(CompileUnit.getGV(), Line, 0)));
 
         if (DW && DW->ShouldEmitDwarfDebug()) {
-          unsigned LabelID = DW->RecordSourceLine(Line, 0, CompileUnit);
+          unsigned LabelID = DAG.getMachineModuleInfo()->NextLabelID();
           DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(),
                                    getRoot(), LabelID));
           DebugLocTuple PrevLocTpl = MF.getDebugLocTuple(PrevLoc);
@@ -3992,10 +3992,9 @@
       } else {
         // Record the source line.
         unsigned Line = Subprogram.getLineNumber();
-        setCurDebugLoc(DebugLoc::get(
+        MF.setDefaultDebugLoc(DebugLoc::get(
                      MF.getOrCreateDebugLocID(CompileUnit.getGV(), Line, 0)));
         if (DW && DW->ShouldEmitDwarfDebug()) {
-          DW->RecordSourceLine(Line, 0, CompileUnit);
           // llvm.dbg.func_start also defines beginning of function scope.
           DW->RecordRegionStart(cast<GlobalVariable>(FSI.getSubprogram()));
         }
diff --git a/lib/Target/CppBackend/CPPBackend.cpp b/lib/Target/CppBackend/CPPBackend.cpp
index 6adb73a..bf9c885 100644
--- a/lib/Target/CppBackend/CPPBackend.cpp
+++ b/lib/Target/CppBackend/CPPBackend.cpp
@@ -1092,9 +1092,9 @@
       const BranchInst* br = cast<BranchInst>(I);
       Out << "BranchInst::Create(" ;
       if (br->getNumOperands() == 3 ) {
-        Out << opNames[0] << ", "
+        Out << opNames[2] << ", "
             << opNames[1] << ", "
-            << opNames[2] << ", ";
+            << opNames[0] << ", ";
 
       } else if (br->getNumOperands() == 1) {
         Out << opNames[0] << ", ";
diff --git a/lib/Target/MSP430/MSP430ISelDAGToDAG.cpp b/lib/Target/MSP430/MSP430ISelDAGToDAG.cpp
index 32e1f7a..bf49ec0 100644
--- a/lib/Target/MSP430/MSP430ISelDAGToDAG.cpp
+++ b/lib/Target/MSP430/MSP430ISelDAGToDAG.cpp
@@ -28,8 +28,6 @@
 #include "llvm/Target/TargetLowering.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Debug.h"
-#include <queue>
-#include <set>
 using namespace llvm;
 
 /// MSP430DAGToDAGISel - MSP430 specific code to select MSP430 machine
diff --git a/lib/Transforms/Scalar/JumpThreading.cpp b/lib/Transforms/Scalar/JumpThreading.cpp
index 1844957..c0ca2df 100644
--- a/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/lib/Transforms/Scalar/JumpThreading.cpp
@@ -120,8 +120,8 @@
           BB != &BB->getParent()->getEntryBlock()) {
         DOUT << "  JT: Deleting dead block '" << BB->getNameStart()
              << "' with terminator: " << *BB->getTerminator();
-        DeleteDeadBlock(BB);
         LoopHeaders.erase(BB);
+        DeleteDeadBlock(BB);
         Changed = true;
       }
     }
@@ -133,7 +133,7 @@
   return EverChanged;
 }
 
-/// FindLoopHeaders - We do not wan jump threading to turn proper loop
+/// FindLoopHeaders - We do not want jump threading to turn proper loop
 /// structures into irreducible loops.  Doing this breaks up the loop nesting
 /// hierarchy and pessimizes later transformations.  To prevent this from
 /// happening, we first have to find the loop headers.  Here we approximate this
diff --git a/lib/VMCore/Value.cpp b/lib/VMCore/Value.cpp
index 35c8ccf..3af161f 100644
--- a/lib/VMCore/Value.cpp
+++ b/lib/VMCore/Value.cpp
@@ -406,8 +406,8 @@
 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
 static ManagedStatic<ValueHandlesTy> ValueHandles;
 
-/// AddToUseList - Add this ValueHandle to the use list for VP, where List is
-/// known to point into the existing use list.
+/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
+/// List is known to point into the existing use list.
 void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
   assert(List && "Handle list is null?");
   
@@ -443,7 +443,7 @@
   ValueHandleBase *&Entry = Handles[VP];
   assert(Entry == 0 && "Value really did already have handles?");
   AddToExistingUseList(&Entry);
-  VP->HasValueHandle = 1;
+  VP->HasValueHandle = true;
   
   // If reallocation didn't happen or if this was the first insertion, don't
   // walk the table.
diff --git a/test/CodeGen/CPP/2009-05-04-CondBr.ll b/test/CodeGen/CPP/2009-05-04-CondBr.ll
new file mode 100644
index 0000000..6c3f984
--- /dev/null
+++ b/test/CodeGen/CPP/2009-05-04-CondBr.ll
@@ -0,0 +1,28 @@
+; RUN: llvm-as < %s | llc -march=cpp -cppgen=program -f -o %t
+; RUN: grep "BranchInst::Create(label_if_then, label_if_end, int1_cmp, label_entry);" %t
+
+define i32 @some_func(i32 %a) nounwind {
+entry:
+	%retval = alloca i32		; <i32*> [#uses=2]
+	%a.addr = alloca i32		; <i32*> [#uses=8]
+	store i32 %a, i32* %a.addr
+	%tmp = load i32* %a.addr		; <i32> [#uses=1]
+	%inc = add i32 %tmp, 1		; <i32> [#uses=1]
+	store i32 %inc, i32* %a.addr
+	%tmp1 = load i32* %a.addr		; <i32> [#uses=1]
+	%cmp = icmp slt i32 %tmp1, 3		; <i1> [#uses=1]
+	br i1 %cmp, label %if.then, label %if.end
+
+if.then:		; preds = %entry
+	store i32 7, i32* %a.addr
+	br label %if.end
+
+if.end:		; preds = %if.then, %entry
+	%tmp2 = load i32* %a.addr		; <i32> [#uses=1]
+	%inc3 = add i32 %tmp2, 1		; <i32> [#uses=1]
+	store i32 %inc3, i32* %a.addr
+	%tmp4 = load i32* %a.addr		; <i32> [#uses=1]
+	store i32 %tmp4, i32* %retval
+	%0 = load i32* %retval		; <i32> [#uses=1]
+	ret i32 %0
+}
diff --git a/test/DebugInfo/2009-01-29-HeaderLocation.ll b/test/DebugInfo/2009-01-29-HeaderLocation.ll
index c59a1c7..a201bd5 100644
--- a/test/DebugInfo/2009-01-29-HeaderLocation.ll
+++ b/test/DebugInfo/2009-01-29-HeaderLocation.ll
@@ -1,4 +1,4 @@
-; RUN: llvm-as < %s | llc | grep "m.h" | count 1
+; RUN: llvm-as < %s | llc | grep "\\"m.h\\"" | count 1
 target triple = "i386-apple-darwin9.6"
 	%llvm.dbg.anchor.type = type { i32, i32 }
 	%llvm.dbg.basictype.type = type { i32, { }*, i8*, { }*, i32, i64, i64, i64, i32, i32 }
diff --git a/test/FrontendC++/2009-05-04-PureConstNounwind.cpp b/test/FrontendC++/2009-05-04-PureConstNounwind.cpp
new file mode 100644
index 0000000..a4b4653
--- /dev/null
+++ b/test/FrontendC++/2009-05-04-PureConstNounwind.cpp
@@ -0,0 +1,8 @@
+// RUN: %llvmgxx -S -emit-llvm %s -o - | grep nounwind | count 4
+int c(void) __attribute__((const));
+int p(void) __attribute__((pure));
+int t(void);
+
+int f(void) {
+	return c() + p() + t();
+}
diff --git a/test/FrontendC/2009-05-04-EnumInreg.c b/test/FrontendC/2009-05-04-EnumInreg.c
new file mode 100644
index 0000000..8a76f5f
--- /dev/null
+++ b/test/FrontendC/2009-05-04-EnumInreg.c
@@ -0,0 +1,17 @@
+// RUN: %llvmgcc -S -m32 -mregparm=3 %s -emit-llvm -o - | grep {inreg %action}
+// XTARGET: x86
+// PR3967
+
+enum kobject_action {
+        KOBJ_ADD,
+        KOBJ_REMOVE,
+        KOBJ_CHANGE,
+        KOBJ_MOVE,
+        KOBJ_ONLINE,
+        KOBJ_OFFLINE,
+        KOBJ_MAX
+};
+
+struct kobject;
+
+int kobject_uevent(struct kobject *kobj, enum kobject_action action) {}
diff --git a/test/Transforms/FunctionAttrs/2008-12-31-NoCapture.ll b/test/Transforms/FunctionAttrs/2008-12-31-NoCapture.ll
index b874c82..aec134a 100644
--- a/test/Transforms/FunctionAttrs/2008-12-31-NoCapture.ll
+++ b/test/Transforms/FunctionAttrs/2008-12-31-NoCapture.ll
@@ -39,6 +39,16 @@
 	ret i1 %val
 }
 
+declare void @throw_if_bit_set(i8*, i8) readonly
+define i1 @c6(i8* %q, i8 %bit) {
+	invoke void @throw_if_bit_set(i8* %q, i8 %bit)
+		to label %ret0 unwind label %ret1
+ret0:
+	ret i1 0
+ret1:
+	ret i1 1
+}
+
 define i32 @nc1(i32* %q, i32* %p, i1 %b) {
 e:
 	br label %l
@@ -63,14 +73,20 @@
 	ret void
 }
 
-declare void @external(i8*) readonly
+declare void @external(i8*) readonly nounwind
 define void @nc4(i8* %p) {
 	call void @external(i8* %p)
 	ret void
 }
 
-define void @nc5(void (i8*)* %f, i8* %p) {
-	call void %f(i8* %p) readonly
-	call void %f(i8* nocapture %p)
+define void @nc5(void (i8*)* %p, i8* %r) {
+	call void %p(i8* %r)
+	call void %p(i8* nocapture %r)
+	ret void
+}
+
+declare i8* @external_identity(i8*) readonly nounwind
+define void @nc6(i8* %p) {
+	call i8* @external_identity(i8* %p)
 	ret void
 }
diff --git a/utils/TableGen/AsmWriterEmitter.cpp b/utils/TableGen/AsmWriterEmitter.cpp
index 2c259e5..3937fd1 100644
--- a/utils/TableGen/AsmWriterEmitter.cpp
+++ b/utils/TableGen/AsmWriterEmitter.cpp
@@ -650,12 +650,12 @@
   O << "\";\n\n";
 
   O << "  if (TAI->doesSupportDebugInformation() &&\n"
-    << "      DW->ShouldEmitDwarfDebug() && OptLevel != CodeGenOpt::None) {\n"
+    << "      DW->ShouldEmitDwarfDebug()) {\n"
     << "    DebugLoc CurDL = MI->getDebugLoc();\n\n"
     << "    if (!CurDL.isUnknown()) {\n"
     << "      static DebugLocTuple PrevDLT(0, ~0U, ~0U);\n"
     << "      DebugLocTuple CurDLT = MF->getDebugLocTuple(CurDL);\n\n"
-    << "      if (PrevDLT.CompileUnit != 0 && PrevDLT != CurDLT)\n"
+    << "      if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT)\n"
     << "        printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,\n"
     << "                               DICompileUnit(CurDLT.CompileUnit)));\n\n"
     << "      PrevDLT = CurDLT;\n"
diff --git a/utils/TableGen/DAGISelEmitter.cpp b/utils/TableGen/DAGISelEmitter.cpp
index e3858cc..39791e2 100644
--- a/utils/TableGen/DAGISelEmitter.cpp
+++ b/utils/TableGen/DAGISelEmitter.cpp
@@ -2102,7 +2102,7 @@
 
   OS << "// Include standard, target-independent definitions and methods used\n"
      << "// by the instruction selector.\n";
-  OS << "#include <llvm/CodeGen/DAGISelHeader.h>\n\n";
+  OS << "#include \"llvm/CodeGen/DAGISelHeader.h\"\n\n";
   
   EmitNodeTransforms(OS);
   EmitPredicateFunctions(OS);