AliasAnalysisSummary.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "AliasAnalysisSummary.h"
  2. #include "llvm/IR/Argument.h"
  3. #include "llvm/IR/InstrTypes.h"
  4. #include "llvm/IR/Type.h"
  5. #include "llvm/Support/Compiler.h"
  6. namespace llvm {
  7. namespace cflaa {
  8. namespace {
  9. const unsigned AttrEscapedIndex = 0;
  10. const unsigned AttrUnknownIndex = 1;
  11. const unsigned AttrGlobalIndex = 2;
  12. const unsigned AttrCallerIndex = 3;
  13. const unsigned AttrFirstArgIndex = 4;
  14. const unsigned AttrLastArgIndex = NumAliasAttrs;
  15. const unsigned AttrMaxNumArgs = AttrLastArgIndex - AttrFirstArgIndex;
  16. // It would be *slightly* prettier if we changed these to AliasAttrs, but it
  17. // seems that both GCC and MSVC emit dynamic initializers for const bitsets.
  18. using AliasAttr = unsigned;
  19. const AliasAttr AttrNone = 0;
  20. const AliasAttr AttrEscaped = 1 << AttrEscapedIndex;
  21. const AliasAttr AttrUnknown = 1 << AttrUnknownIndex;
  22. const AliasAttr AttrGlobal = 1 << AttrGlobalIndex;
  23. const AliasAttr AttrCaller = 1 << AttrCallerIndex;
  24. const AliasAttr ExternalAttrMask = AttrEscaped | AttrUnknown | AttrGlobal;
  25. }
  26. AliasAttrs getAttrNone() { return AttrNone; }
  27. AliasAttrs getAttrUnknown() { return AttrUnknown; }
  28. bool hasUnknownAttr(AliasAttrs Attr) { return Attr.test(AttrUnknownIndex); }
  29. AliasAttrs getAttrCaller() { return AttrCaller; }
  30. bool hasCallerAttr(AliasAttrs Attr) { return Attr.test(AttrCaller); }
  31. bool hasUnknownOrCallerAttr(AliasAttrs Attr) {
  32. return Attr.test(AttrUnknownIndex) || Attr.test(AttrCallerIndex);
  33. }
  34. AliasAttrs getAttrEscaped() { return AttrEscaped; }
  35. bool hasEscapedAttr(AliasAttrs Attr) { return Attr.test(AttrEscapedIndex); }
  36. static AliasAttr argNumberToAttr(unsigned ArgNum) {
  37. if (ArgNum >= AttrMaxNumArgs)
  38. return AttrUnknown;
  39. // N.B. MSVC complains if we use `1U` here, since AliasAttr' ctor takes
  40. // an unsigned long long.
  41. return AliasAttr(1ULL << (ArgNum + AttrFirstArgIndex));
  42. }
  43. AliasAttrs getGlobalOrArgAttrFromValue(const Value &Val) {
  44. if (isa<GlobalValue>(Val))
  45. return AttrGlobal;
  46. if (auto *Arg = dyn_cast<Argument>(&Val))
  47. // Only pointer arguments should have the argument attribute,
  48. // because things can't escape through scalars without us seeing a
  49. // cast, and thus, interaction with them doesn't matter.
  50. if (!Arg->hasNoAliasAttr() && Arg->getType()->isPointerTy())
  51. return argNumberToAttr(Arg->getArgNo());
  52. return AttrNone;
  53. }
  54. bool isGlobalOrArgAttr(AliasAttrs Attr) {
  55. return Attr.reset(AttrEscapedIndex)
  56. .reset(AttrUnknownIndex)
  57. .reset(AttrCallerIndex)
  58. .any();
  59. }
  60. AliasAttrs getExternallyVisibleAttrs(AliasAttrs Attr) {
  61. return Attr & AliasAttrs(ExternalAttrMask);
  62. }
  63. Optional<InstantiatedValue> instantiateInterfaceValue(InterfaceValue IValue,
  64. CallBase &Call) {
  65. auto Index = IValue.Index;
  66. auto *V = (Index == 0) ? &Call : Call.getArgOperand(Index - 1);
  67. if (V->getType()->isPointerTy())
  68. return InstantiatedValue{V, IValue.DerefLevel};
  69. return None;
  70. }
  71. Optional<InstantiatedRelation>
  72. instantiateExternalRelation(ExternalRelation ERelation, CallBase &Call) {
  73. auto From = instantiateInterfaceValue(ERelation.From, Call);
  74. if (!From)
  75. return None;
  76. auto To = instantiateInterfaceValue(ERelation.To, Call);
  77. if (!To)
  78. return None;
  79. return InstantiatedRelation{*From, *To, ERelation.Offset};
  80. }
  81. Optional<InstantiatedAttr> instantiateExternalAttribute(ExternalAttribute EAttr,
  82. CallBase &Call) {
  83. auto Value = instantiateInterfaceValue(EAttr.IValue, Call);
  84. if (!Value)
  85. return None;
  86. return InstantiatedAttr{*Value, EAttr.Attr};
  87. }
  88. }
  89. }