1200581Srdivacky//===- PHITransAddr.h - PHI Translation for Addresses -----------*- C++ -*-===//
2200581Srdivacky//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6200581Srdivacky//
7200581Srdivacky//===----------------------------------------------------------------------===//
8200581Srdivacky//
9200581Srdivacky// This file declares the PHITransAddr class.
10200581Srdivacky//
11200581Srdivacky//===----------------------------------------------------------------------===//
12200581Srdivacky
13200581Srdivacky#ifndef LLVM_ANALYSIS_PHITRANSADDR_H
14200581Srdivacky#define LLVM_ANALYSIS_PHITRANSADDR_H
15200581Srdivacky
16200581Srdivacky#include "llvm/ADT/SmallVector.h"
17249423Sdim#include "llvm/IR/Instruction.h"
18200581Srdivacky
19200581Srdivackynamespace llvm {
20280031Sdim  class AssumptionCache;
21200581Srdivacky  class DominatorTree;
22243830Sdim  class DataLayout;
23234353Sdim  class TargetLibraryInfo;
24234353Sdim
25200581Srdivacky/// PHITransAddr - An address value which tracks and handles phi translation.
26200581Srdivacky/// As we walk "up" the CFG through predecessors, we need to ensure that the
27200581Srdivacky/// address we're tracking is kept up to date.  For example, if we're analyzing
28200581Srdivacky/// an address of "&A[i]" and walk through the definition of 'i' which is a PHI
29200581Srdivacky/// node, we *must* phi translate i to get "&A[j]" or else we will analyze an
30200581Srdivacky/// incorrect pointer in the predecessor block.
31200581Srdivacky///
32200581Srdivacky/// This is designed to be a relatively small object that lives on the stack and
33200581Srdivacky/// is copyable.
34200581Srdivacky///
35200581Srdivackyclass PHITransAddr {
36200581Srdivacky  /// Addr - The actual address we're analyzing.
37200581Srdivacky  Value *Addr;
38234353Sdim
39288943Sdim  /// The DataLayout we are playing with.
40288943Sdim  const DataLayout &DL;
41288943Sdim
42234353Sdim  /// TLI - The target library info if known, otherwise null.
43234353Sdim  const TargetLibraryInfo *TLI;
44280031Sdim
45341825Sdim  /// A cache of \@llvm.assume calls used by SimplifyInstruction.
46280031Sdim  AssumptionCache *AC;
47280031Sdim
48200581Srdivacky  /// InstInputs - The inputs for our symbolic address.
49200581Srdivacky  SmallVector<Instruction*, 4> InstInputs;
50296417Sdim
51200581Srdivackypublic:
52288943Sdim  PHITransAddr(Value *addr, const DataLayout &DL, AssumptionCache *AC)
53280031Sdim      : Addr(addr), DL(DL), TLI(nullptr), AC(AC) {
54200581Srdivacky    // If the address is an instruction, the whole thing is considered an input.
55200581Srdivacky    if (Instruction *I = dyn_cast<Instruction>(Addr))
56200581Srdivacky      InstInputs.push_back(I);
57200581Srdivacky  }
58296417Sdim
59200581Srdivacky  Value *getAddr() const { return Addr; }
60296417Sdim
61200581Srdivacky  /// NeedsPHITranslationFromBlock - Return true if moving from the specified
62200581Srdivacky  /// BasicBlock to its predecessors requires PHI translation.
63200581Srdivacky  bool NeedsPHITranslationFromBlock(BasicBlock *BB) const {
64200581Srdivacky    // We do need translation if one of our input instructions is defined in
65200581Srdivacky    // this block.
66200581Srdivacky    for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
67200581Srdivacky      if (InstInputs[i]->getParent() == BB)
68200581Srdivacky        return true;
69200581Srdivacky    return false;
70200581Srdivacky  }
71296417Sdim
72200581Srdivacky  /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
73200581Srdivacky  /// if we have some hope of doing it.  This should be used as a filter to
74200581Srdivacky  /// avoid calling PHITranslateValue in hopeless situations.
75200581Srdivacky  bool IsPotentiallyPHITranslatable() const;
76296417Sdim
77200581Srdivacky  /// PHITranslateValue - PHI translate the current address up the CFG from
78288943Sdim  /// CurBB to Pred, updating our state to reflect any needed changes.  If
79288943Sdim  /// 'MustDominate' is true, the translated value must dominate
80204642Srdivacky  /// PredBB.  This returns true on failure and sets Addr to null.
81204642Srdivacky  bool PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
82288943Sdim                         const DominatorTree *DT, bool MustDominate);
83288943Sdim
84200581Srdivacky  /// PHITranslateWithInsertion - PHI translate this value into the specified
85200581Srdivacky  /// predecessor block, inserting a computation of the value if it is
86200581Srdivacky  /// unavailable.
87200581Srdivacky  ///
88200581Srdivacky  /// All newly created instructions are added to the NewInsts list.  This
89200581Srdivacky  /// returns null on failure.
90200581Srdivacky  ///
91200581Srdivacky  Value *PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
92200581Srdivacky                                   const DominatorTree &DT,
93296417Sdim                                   SmallVectorImpl<Instruction *> &NewInsts);
94296417Sdim
95200581Srdivacky  void dump() const;
96296417Sdim
97200581Srdivacky  /// Verify - Check internal consistency of this data structure.  If the
98200581Srdivacky  /// structure is valid, it returns true.  If invalid, it prints errors and
99200581Srdivacky  /// returns false.
100200581Srdivacky  bool Verify() const;
101296417Sdim
102200581Srdivackyprivate:
103204642Srdivacky  Value *PHITranslateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB,
104204642Srdivacky                             const DominatorTree *DT);
105296417Sdim
106200581Srdivacky  /// InsertPHITranslatedSubExpr - Insert a computation of the PHI translated
107200581Srdivacky  /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
108200581Srdivacky  /// block.  All newly created instructions are added to the NewInsts list.
109200581Srdivacky  /// This returns null on failure.
110200581Srdivacky  ///
111200581Srdivacky  Value *InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
112200581Srdivacky                                    BasicBlock *PredBB, const DominatorTree &DT,
113296417Sdim                                    SmallVectorImpl<Instruction *> &NewInsts);
114296417Sdim
115200581Srdivacky  /// AddAsInput - If the specified value is an instruction, add it as an input.
116200581Srdivacky  Value *AddAsInput(Value *V) {
117200581Srdivacky    // If V is an instruction, it is now an input.
118200581Srdivacky    if (Instruction *VI = dyn_cast<Instruction>(V))
119200581Srdivacky      InstInputs.push_back(VI);
120200581Srdivacky    return V;
121200581Srdivacky  }
122200581Srdivacky};
123200581Srdivacky
124200581Srdivacky} // end namespace llvm
125200581Srdivacky
126200581Srdivacky#endif
127