diff --git a/docs/LangRef.html b/docs/LangRef.html
index 86f1ee3b51b..bec79f3711d 100644
--- a/docs/LangRef.html
+++ b/docs/LangRef.html
@@ -3995,8 +3995,9 @@ value address) contains the meta-data to be associated with the root.
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.
-
+the runtime to find the pointer at GC safe points. The 'llvm.gcroot'
+intrinsic may only be used in a function which specifies a GC
+algorithm.
@@ -4031,7 +4032,9 @@ null).
The 'llvm.gcread' 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.
+garbage collector runtime, as needed. The 'llvm.gcread' intrinsic
+may only be used in a function which specifies a GC
+algorithm.
@@ -4066,7 +4069,9 @@ null.
The 'llvm.gcwrite' 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.
+garbage collector runtime, as needed. The 'llvm.gcwrite' intrinsic
+may only be used in a function which specifies a GC
+algorithm.
diff --git a/lib/VMCore/Verifier.cpp b/lib/VMCore/Verifier.cpp
index e6495a01089..df0cb997bbf 100644
--- a/lib/VMCore/Verifier.cpp
+++ b/lib/VMCore/Verifier.cpp
@@ -1167,37 +1167,45 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
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(IntrinsicInst::StripPointerCasts(CI.getOperand(1))),
- "llvm.gcroot parameter #1 must be an alloca.",
- &CI);
- Assert1(isa(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(
+ IntrinsicInst::StripPointerCasts(CI.getOperand(1))),
+ "llvm.gcroot parameter #1 must be an alloca.", &CI);
+ Assert1(isa(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(IntrinsicInst::StripPointerCasts(CI.getOperand(2))),
diff --git a/test/CodeGen/Generic/GC/lower_gcroot.ll b/test/CodeGen/Generic/GC/lower_gcroot.ll
index 4b0a17478f4..bd5a2bd14b4 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 00000000000..122bfe4a47b
--- /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
+}