PHITransAddr.h 4.8 KB

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