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 13 #ifndef LLVM_ANALYSIS_PHITRANSADDR_H 14 #define LLVM_ANALYSIS_PHITRANSADDR_H 15 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/IR/Instruction.h" 18 #include "llvm/Support/Compiler.h" 19 20 namespace llvm { 21 class AssumptionCache; 22 class DominatorTree; 23 class DataLayout; 24 class TargetLibraryInfo; 25 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 40 /// The DataLayout we are playing with. 41 const DataLayout &DL; 42 43 /// TLI - The target library info if known, otherwise null. 44 const TargetLibraryInfo *TLI = nullptr; 45 46 /// A cache of \@llvm.assume calls used by SimplifyInstruction. 47 AssumptionCache *AC; 48 49 /// InstInputs - The inputs for our symbolic address. 50 SmallVector<Instruction*, 4> InstInputs; 51 52 public: PHITransAddr(Value * Addr,const DataLayout & DL,AssumptionCache * AC)53 PHITransAddr(Value *Addr, const DataLayout &DL, AssumptionCache *AC) 54 : Addr(Addr), DL(DL), AC(AC) { 55 // If the address is an instruction, the whole thing is considered an input. 56 addAsInput(Addr); 57 } 58 getAddr()59 Value *getAddr() const { return Addr; } 60 61 /// needsPHITranslationFromBlock - Return true if moving from the specified 62 /// BasicBlock to its predecessors requires PHI translation. needsPHITranslationFromBlock(BasicBlock * BB)63 bool needsPHITranslationFromBlock(BasicBlock *BB) const { 64 // We do need translation if one of our input instructions is defined in 65 // this block. 66 return any_of(InstInputs, [BB](const auto &InstInput) { 67 return InstInput->getParent() == BB; 68 }); 69 } 70 71 /// isPotentiallyPHITranslatable - If this needs PHI translation, return true 72 /// if we have some hope of doing it. This should be used as a filter to 73 /// avoid calling PHITranslateValue in hopeless situations. 74 LLVM_ABI bool isPotentiallyPHITranslatable() const; 75 76 /// translateValue - PHI translate the current address up the CFG from 77 /// CurBB to Pred, updating our state to reflect any needed changes. If 78 /// 'MustDominate' is true, the translated value must dominate PredBB. 79 LLVM_ABI Value *translateValue(BasicBlock *CurBB, BasicBlock *PredBB, 80 const DominatorTree *DT, bool MustDominate); 81 82 /// translateWithInsertion - PHI translate this value into the specified 83 /// predecessor block, inserting a computation of the value if it is 84 /// unavailable. 85 /// 86 /// All newly created instructions are added to the NewInsts list. This 87 /// returns null on failure. 88 /// 89 LLVM_ABI Value * 90 translateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB, 91 const DominatorTree &DT, 92 SmallVectorImpl<Instruction *> &NewInsts); 93 94 LLVM_ABI void dump() const; 95 96 /// verify - Check internal consistency of this data structure. If the 97 /// structure is valid, it returns true. If invalid, it prints errors and 98 /// returns false. 99 LLVM_ABI bool verify() const; 100 101 private: 102 Value *translateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB, 103 const DominatorTree *DT); 104 105 /// insertTranslatedSubExpr - Insert a computation of the PHI translated 106 /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB 107 /// block. All newly created instructions are added to the NewInsts list. 108 /// This returns null on failure. 109 /// 110 Value *insertTranslatedSubExpr(Value *InVal, BasicBlock *CurBB, 111 BasicBlock *PredBB, const DominatorTree &DT, 112 SmallVectorImpl<Instruction *> &NewInsts); 113 114 /// addAsInput - If the specified value is an instruction, add it as an input. addAsInput(Value * V)115 Value *addAsInput(Value *V) { 116 // If V is an instruction, it is now an input. 117 if (Instruction *VI = dyn_cast<Instruction>(V)) 118 InstInputs.push_back(VI); 119 return V; 120 } 121 }; 122 123 } // end namespace llvm 124 125 #endif 126