Noting and enforcing that GC intrinsics are valid only within a
authorGordon Henriksen <gordonhenriksen@mac.com>
Tue, 25 Dec 2007 02:31:26 +0000 (02:31 +0000)
committerGordon Henriksen <gordonhenriksen@mac.com>
Tue, 25 Dec 2007 02:31:26 +0000 (02:31 +0000)
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

docs/LangRef.html
lib/VMCore/Verifier.cpp
test/CodeGen/Generic/GC/lower_gcroot.ll
test/CodeGen/Generic/GC/outside.ll [new file with mode: 0644]

index 86f1ee3b51bba0657d23657816d7f6d72e52d846..bec79f3711d73fa997b94348be969f1f84876051 100644 (file)
@@ -3995,8 +3995,9 @@ value address) contains the meta-data to be associated with the root.</p>
 
 <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
 
 <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>
 
 
 </div>
 
@@ -4031,7 +4032,9 @@ null).</p>
 
 <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
 
 <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>
 
 
 </div>
 
@@ -4066,7 +4069,9 @@ null.</p>
 
 <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
 
 <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>
 
 
 </div>
 
index e6495a01089ba85897e26e0f71027f7b9617fdad..df0cb997bbfccb8138c4b77e8dad17278b5edcac 100644 (file)
@@ -1167,37 +1167,45 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
   switch (ID) {
   default:
     break;
   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);
   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))),
     } break;
   case Intrinsic::init_trampoline:
     Assert1(isa<Function>(IntrinsicInst::StripPointerCasts(CI.getOperand(2))),
index 4b0a17478f475e29534c6524d423546472dc8fd6..bd5a2bd14b4e61cf8d559e57aefbd582a39377c1 100644 (file)
@@ -2,7 +2,7 @@
 
        %Env = type i8*
 
 
        %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
        %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 (file)
index 0000000..122bfe4
--- /dev/null
@@ -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
+}