ValueRelativeOffset.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "Backend.h"
  6. const ValueNumber InvalidValueNumber = 0;
  7. const ValueNumber ZeroValueNumber = 1;
  8. const ValueNumber FirstNewValueNumber = ZeroValueNumber + 1;
  9. ValueRelativeOffset::ValueRelativeOffset()
  10. #if DBG
  11. : baseValue(nullptr)
  12. #endif
  13. {
  14. Assert(!IsValid());
  15. }
  16. ValueRelativeOffset::ValueRelativeOffset(const Value *const baseValue, const bool wasEstablishedExplicitly)
  17. : baseValue(baseValue), offset(0), wasEstablishedExplicitly(wasEstablishedExplicitly)
  18. {
  19. Assert(baseValue);
  20. Assert(IsValid());
  21. }
  22. ValueRelativeOffset::ValueRelativeOffset(const Value *const baseValue, const int offset, const bool wasEstablishedExplicitly)
  23. : baseValue(baseValue), offset(offset), wasEstablishedExplicitly(wasEstablishedExplicitly)
  24. {
  25. Assert(baseValue);
  26. Assert(IsValid());
  27. }
  28. #if DBG
  29. bool ValueRelativeOffset::IsValid() const
  30. {
  31. return !!baseValue;
  32. }
  33. #endif
  34. ValueNumber ValueRelativeOffset::BaseValueNumber() const
  35. {
  36. Assert(IsValid());
  37. return baseValue->GetValueNumber();
  38. }
  39. StackSym *ValueRelativeOffset::BaseSym() const
  40. {
  41. Assert(IsValid());
  42. Sym *const baseSym = baseValue->GetValueInfo()->GetSymStore();
  43. return baseSym && baseSym->IsStackSym() ? baseSym->AsStackSym() : nullptr;
  44. }
  45. int ValueRelativeOffset::Offset() const
  46. {
  47. Assert(IsValid());
  48. return offset;
  49. }
  50. void ValueRelativeOffset::SetOffset(const int offset)
  51. {
  52. Assert(IsValid());
  53. this->offset = offset;
  54. }
  55. bool ValueRelativeOffset::WasEstablishedExplicitly() const
  56. {
  57. Assert(IsValid());
  58. return wasEstablishedExplicitly;
  59. }
  60. void ValueRelativeOffset::SetWasEstablishedExplicitly()
  61. {
  62. Assert(IsValid());
  63. wasEstablishedExplicitly = true;
  64. }
  65. bool ValueRelativeOffset::Add(const int n)
  66. {
  67. Assert(IsValid());
  68. return !Int32Math::Add(offset, n, &offset);
  69. }
  70. template<bool Lower, bool Aggressive>
  71. void ValueRelativeOffset::Merge(const ValueRelativeOffset &other)
  72. {
  73. Assert(IsValid());
  74. Assert(other.IsValid());
  75. Assert(BaseValueNumber() == other.BaseValueNumber());
  76. if(!BaseSym() && other.BaseSym())
  77. baseValue = other.baseValue;
  78. MergeConstantValue<Lower, Aggressive>(other.offset);
  79. if(other.wasEstablishedExplicitly == Aggressive)
  80. wasEstablishedExplicitly = Aggressive;
  81. }
  82. template void ValueRelativeOffset::Merge<false, false>(const ValueRelativeOffset &other);
  83. template void ValueRelativeOffset::Merge<false, true>(const ValueRelativeOffset &other);
  84. template void ValueRelativeOffset::Merge<true, false>(const ValueRelativeOffset &other);
  85. template void ValueRelativeOffset::Merge<true, true>(const ValueRelativeOffset &other);
  86. template<bool Lower, bool Aggressive>
  87. void ValueRelativeOffset::MergeConstantValue(const int constantValue)
  88. {
  89. Assert(IsValid());
  90. // Merge down for a conservative lower bound or aggressive upper bound merge, or merge up for a conservative upper bound or
  91. // aggressive lower bound merge
  92. if(Lower ^ Aggressive ? constantValue < offset : constantValue > offset)
  93. offset = constantValue;
  94. }
  95. template void ValueRelativeOffset::MergeConstantValue<false, false>(const int constantValue);
  96. template void ValueRelativeOffset::MergeConstantValue<false, true>(const int constantValue);
  97. template void ValueRelativeOffset::MergeConstantValue<true, false>(const int constantValue);
  98. template void ValueRelativeOffset::MergeConstantValue<true, true>(const int constantValue);