BitSets.rst 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. =======
  2. Bitsets
  3. =======
  4. This is a mechanism that allows IR modules to co-operatively build pointer
  5. sets corresponding to addresses within a given set of globals. One example
  6. of a use case for this is to allow a C++ program to efficiently verify (at
  7. each call site) that a vtable pointer is in the set of valid vtable pointers
  8. for the type of the class or its derived classes.
  9. To use the mechanism, a client creates a global metadata node named
  10. ``llvm.bitsets``. Each element is a metadata node with three elements:
  11. the first is a metadata string containing an identifier for the bitset,
  12. the second is a global variable and the third is a byte offset into the
  13. global variable.
  14. This will cause a link-time optimization pass to generate bitsets from the
  15. memory addresses referenced from the elements of the bitset metadata. The pass
  16. will lay out the referenced globals consecutively, so their definitions must
  17. be available at LTO time. The `GlobalLayoutBuilder`_ class is responsible for
  18. laying out the globals efficiently to minimize the sizes of the underlying
  19. bitsets. An intrinsic, :ref:`llvm.bitset.test <bitset.test>`, generates code
  20. to test whether a given pointer is a member of a bitset.
  21. :Example:
  22. ::
  23. target datalayout = "e-p:32:32"
  24. @a = internal global i32 0
  25. @b = internal global i32 0
  26. @c = internal global i32 0
  27. @d = internal global [2 x i32] [i32 0, i32 0]
  28. !llvm.bitsets = !{!0, !1, !2, !3, !4}
  29. !0 = !{!"bitset1", i32* @a, i32 0}
  30. !1 = !{!"bitset1", i32* @b, i32 0}
  31. !2 = !{!"bitset2", i32* @b, i32 0}
  32. !3 = !{!"bitset2", i32* @c, i32 0}
  33. !4 = !{!"bitset2", i32* @d, i32 4}
  34. declare i1 @llvm.bitset.test(i8* %ptr, metadata %bitset) nounwind readnone
  35. define i1 @foo(i32* %p) {
  36. %pi8 = bitcast i32* %p to i8*
  37. %x = call i1 @llvm.bitset.test(i8* %pi8, metadata !"bitset1")
  38. ret i1 %x
  39. }
  40. define i1 @bar(i32* %p) {
  41. %pi8 = bitcast i32* %p to i8*
  42. %x = call i1 @llvm.bitset.test(i8* %pi8, metadata !"bitset2")
  43. ret i1 %x
  44. }
  45. define void @main() {
  46. %a1 = call i1 @foo(i32* @a) ; returns 1
  47. %b1 = call i1 @foo(i32* @b) ; returns 1
  48. %c1 = call i1 @foo(i32* @c) ; returns 0
  49. %a2 = call i1 @bar(i32* @a) ; returns 0
  50. %b2 = call i1 @bar(i32* @b) ; returns 1
  51. %c2 = call i1 @bar(i32* @c) ; returns 1
  52. %d02 = call i1 @bar(i32* getelementptr ([2 x i32]* @d, i32 0, i32 0)) ; returns 0
  53. %d12 = call i1 @bar(i32* getelementptr ([2 x i32]* @d, i32 0, i32 1)) ; returns 1
  54. ret void
  55. }
  56. .. _GlobalLayoutBuilder: http://llvm.org/klaus/llvm/blob/master/include/llvm/Transforms/IPO/LowerBitSets.h