Noting and enforcing that GC intrinsics are valid only within a
function with GC.

This will catch the error when the inliner inlines a function with
GC into a caller with no GC.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45350 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/docs/LangRef.html b/docs/LangRef.html
index 86f1ee3..bec79f3 100644
--- a/docs/LangRef.html
+++ b/docs/LangRef.html
@@ -3995,8 +3995,9 @@
 
 <p>At runtime, a call to this intrinsics stores a null pointer into the "ptrloc"
 location.  At compile-time, the code generator generates information to allow
-the runtime to find the pointer at GC safe points.
-</p>
+the runtime to find the pointer at GC safe points. The '<tt>llvm.gcroot</tt>'
+intrinsic may only be used in a function which <a href="#gc">specifies a GC
+algorithm</a>.</p>
 
 </div>
 
@@ -4031,7 +4032,9 @@
 
 <p>The '<tt>llvm.gcread</tt>' intrinsic has the same semantics as a load
 instruction, but may be replaced with substantially more complex code by the
-garbage collector runtime, as needed.</p>
+garbage collector runtime, as needed. The '<tt>llvm.gcread</tt>' intrinsic
+may only be used in a function which <a href="#gc">specifies a GC
+algorithm</a>.</p>
 
 </div>
 
@@ -4066,7 +4069,9 @@
 
 <p>The '<tt>llvm.gcwrite</tt>' intrinsic has the same semantics as a store
 instruction, but may be replaced with substantially more complex code by the
-garbage collector runtime, as needed.</p>
+garbage collector runtime, as needed. The '<tt>llvm.gcwrite</tt>' intrinsic
+may only be used in a function which <a href="#gc">specifies a GC
+algorithm</a>.</p>
 
 </div>
 
diff --git a/lib/VMCore/Verifier.cpp b/lib/VMCore/Verifier.cpp
index e6495a0..df0cb99 100644
--- a/lib/VMCore/Verifier.cpp
+++ b/lib/VMCore/Verifier.cpp
@@ -1167,37 +1167,45 @@
   switch (ID) {
   default:
     break;
-  case Intrinsic::gcroot: {
-      Type *PtrTy    = PointerType::getUnqual(Type::Int8Ty),
-           *PtrPtrTy = PointerType::getUnqual(PtrTy);
-      Assert1(CI.getOperand(1)->getType() == PtrPtrTy,
-              "Intrinsic parameter #1 is not i8**.", &CI);
-      Assert1(CI.getOperand(2)->getType() == PtrTy,
-              "Intrinsic parameter #2 is not i8*.", &CI);
-      Assert1(
-            isa<AllocaInst>(IntrinsicInst::StripPointerCasts(CI.getOperand(1))),
-            "llvm.gcroot parameter #1 must be an alloca.",
-              &CI);
-      Assert1(isa<Constant>(CI.getOperand(2)),
-              "llvm.gcroot parameter #2 must be a constant.", &CI);
-    } break;
-  case Intrinsic::gcwrite: {
-      Type *PtrTy    = PointerType::getUnqual(Type::Int8Ty),
-           *PtrPtrTy = PointerType::getUnqual(PtrTy);
-      Assert1(CI.getOperand(1)->getType() == PtrTy,
-              "Intrinsic parameter #1 is not a i8*.", &CI);
-      Assert1(CI.getOperand(2)->getType() == PtrTy,
-              "Intrinsic parameter #2 is not a i8*.", &CI);
-      Assert1(CI.getOperand(3)->getType() == PtrPtrTy,
-              "Intrinsic parameter #3 is not a i8**.", &CI);
-    } break;
+  case Intrinsic::gcroot:
+  case Intrinsic::gcwrite:
   case Intrinsic::gcread: {
       Type *PtrTy    = PointerType::getUnqual(Type::Int8Ty),
            *PtrPtrTy = PointerType::getUnqual(PtrTy);
-      Assert1(CI.getOperand(1)->getType() == PtrTy,
-              "Intrinsic parameter #1 is not a i8*.", &CI);
-      Assert1(CI.getOperand(2)->getType() == PtrPtrTy,
-              "Intrinsic parameter #2 is not a i8**.", &CI);
+      
+      switch (ID) {
+      default:
+        break;
+      case Intrinsic::gcroot:
+        Assert1(CI.getOperand(1)->getType() == PtrPtrTy,
+                "Intrinsic parameter #1 is not i8**.", &CI);
+        Assert1(CI.getOperand(2)->getType() == PtrTy,
+                "Intrinsic parameter #2 is not i8*.", &CI);
+        Assert1(isa<AllocaInst>(
+                  IntrinsicInst::StripPointerCasts(CI.getOperand(1))),
+                "llvm.gcroot parameter #1 must be an alloca.", &CI);
+        Assert1(isa<Constant>(CI.getOperand(2)),
+                "llvm.gcroot parameter #2 must be a constant.", &CI);
+        break;
+      case Intrinsic::gcwrite:
+        Assert1(CI.getOperand(1)->getType() == PtrTy,
+                "Intrinsic parameter #1 is not a i8*.", &CI);
+        Assert1(CI.getOperand(2)->getType() == PtrTy,
+                "Intrinsic parameter #2 is not a i8*.", &CI);
+        Assert1(CI.getOperand(3)->getType() == PtrPtrTy,
+                "Intrinsic parameter #3 is not a i8**.", &CI);
+        break;
+      case Intrinsic::gcread:
+        Assert1(CI.getOperand(1)->getType() == PtrTy,
+                "Intrinsic parameter #1 is not a i8*.", &CI);
+        Assert1(CI.getOperand(2)->getType() == PtrPtrTy,
+                "Intrinsic parameter #2 is not a i8**.", &CI);
+        break;
+      }
+      
+      Assert1(CI.getParent()->getParent()->hasCollector(),
+              "Enclosing function does not specify a collector algorithm.",
+              &CI);
     } break;
   case Intrinsic::init_trampoline:
     Assert1(isa<Function>(IntrinsicInst::StripPointerCasts(CI.getOperand(2))),
diff --git a/test/CodeGen/Generic/GC/lower_gcroot.ll b/test/CodeGen/Generic/GC/lower_gcroot.ll
index 4b0a174..bd5a2bd 100644
--- a/test/CodeGen/Generic/GC/lower_gcroot.ll
+++ b/test/CodeGen/Generic/GC/lower_gcroot.ll
@@ -2,7 +2,7 @@
 
 	%Env = type i8*
 
-define void @.main(%Env) {
+define void @.main(%Env) gc "shadow-stack" {
 	%Root = alloca %Env
 	call void @llvm.gcroot( %Env* %Root, %Env null )
 	unreachable
diff --git a/test/CodeGen/Generic/GC/outside.ll b/test/CodeGen/Generic/GC/outside.ll
new file mode 100644
index 0000000..122bfe4
--- /dev/null
+++ b/test/CodeGen/Generic/GC/outside.ll
@@ -0,0 +1,10 @@
+; RUN: not llvm-as < %s
+
+declare void @llvm.gcroot(i8**, i8*)
+
+define void @f(i8* %x) {
+	%root = alloca i8*
+	call void @llvm.gcroot(i8** %root, i8* null)
+	store i8* %x, i8** %root
+	ret void
+}