PHITransAddr.h 4.8 KB

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