PHITransAddr.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- PHITransAddr.h - PHI Translation for Addresses -----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the PHITransAddr class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ANALYSIS_PHITRANSADDR_H
  18. #define LLVM_ANALYSIS_PHITRANSADDR_H
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/IR/Instruction.h"
  21. namespace llvm {
  22. class AssumptionCache;
  23. class DominatorTree;
  24. class DataLayout;
  25. class TargetLibraryInfo;
  26. /// PHITransAddr - An address value which tracks and handles phi translation.
  27. /// As we walk "up" the CFG through predecessors, we need to ensure that the
  28. /// address we're tracking is kept up to date. For example, if we're analyzing
  29. /// an address of "&A[i]" and walk through the definition of 'i' which is a PHI
  30. /// node, we *must* phi translate i to get "&A[j]" or else we will analyze an
  31. /// incorrect pointer in the predecessor block.
  32. ///
  33. /// This is designed to be a relatively small object that lives on the stack and
  34. /// is copyable.
  35. ///
  36. class PHITransAddr {
  37. /// Addr - The actual address we're analyzing.
  38. Value *Addr;
  39. /// The DataLayout we are playing with.
  40. const DataLayout &DL;
  41. /// TLI - The target library info if known, otherwise null.
  42. const TargetLibraryInfo *TLI = nullptr;
  43. /// A cache of \@llvm.assume calls used by SimplifyInstruction.
  44. AssumptionCache *AC;
  45. /// InstInputs - The inputs for our symbolic address.
  46. SmallVector<Instruction*, 4> InstInputs;
  47. public:
  48. PHITransAddr(Value *addr, const DataLayout &DL, AssumptionCache *AC)
  49. : Addr(addr), DL(DL), AC(AC) {
  50. // If the address is an instruction, the whole thing is considered an input.
  51. if (Instruction *I = dyn_cast<Instruction>(Addr))
  52. InstInputs.push_back(I);
  53. }
  54. Value *getAddr() const { return Addr; }
  55. /// NeedsPHITranslationFromBlock - Return true if moving from the specified
  56. /// BasicBlock to its predecessors requires PHI translation.
  57. bool NeedsPHITranslationFromBlock(BasicBlock *BB) const {
  58. // We do need translation if one of our input instructions is defined in
  59. // this block.
  60. for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
  61. if (InstInputs[i]->getParent() == BB)
  62. return true;
  63. return false;
  64. }
  65. /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
  66. /// if we have some hope of doing it. This should be used as a filter to
  67. /// avoid calling PHITranslateValue in hopeless situations.
  68. bool IsPotentiallyPHITranslatable() const;
  69. /// PHITranslateValue - PHI translate the current address up the CFG from
  70. /// CurBB to Pred, updating our state to reflect any needed changes. If
  71. /// 'MustDominate' is true, the translated value must dominate
  72. /// PredBB. This returns true on failure and sets Addr to null.
  73. bool PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
  74. const DominatorTree *DT, bool MustDominate);
  75. /// PHITranslateWithInsertion - PHI translate this value into the specified
  76. /// predecessor block, inserting a computation of the value if it is
  77. /// unavailable.
  78. ///
  79. /// All newly created instructions are added to the NewInsts list. This
  80. /// returns null on failure.
  81. ///
  82. Value *PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
  83. const DominatorTree &DT,
  84. SmallVectorImpl<Instruction *> &NewInsts);
  85. void dump() const;
  86. /// Verify - Check internal consistency of this data structure. If the
  87. /// structure is valid, it returns true. If invalid, it prints errors and
  88. /// returns false.
  89. bool Verify() const;
  90. private:
  91. Value *PHITranslateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB,
  92. const DominatorTree *DT);
  93. /// InsertPHITranslatedSubExpr - Insert a computation of the PHI translated
  94. /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
  95. /// block. All newly created instructions are added to the NewInsts list.
  96. /// This returns null on failure.
  97. ///
  98. Value *InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
  99. BasicBlock *PredBB, const DominatorTree &DT,
  100. SmallVectorImpl<Instruction *> &NewInsts);
  101. /// AddAsInput - If the specified value is an instruction, add it as an input.
  102. Value *AddAsInput(Value *V) {
  103. // If V is an instruction, it is now an input.
  104. if (Instruction *VI = dyn_cast<Instruction>(V))
  105. InstInputs.push_back(VI);
  106. return V;
  107. }
  108. };
  109. } // end namespace llvm
  110. #endif
  111. #ifdef __GNUC__
  112. #pragma GCC diagnostic pop
  113. #endif