LLVM 23.0.0git
PHITransAddr.h
Go to the documentation of this file.
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
17#include "llvm/IR/Instruction.h"
19
20namespace llvm {
21class AssumptionCache;
22class DominatorTree;
23class DataLayout;
25
26/// Storage of either a normal Value address, or a select condition together
27/// with a pair of addresses for the "true" and "false" variant of a
28/// select-dependent address. If the addresses are not present (both null), V
29/// is a normal address; otherwise V is a select condition and the pair holds
30/// the "true" and "false" addresses.
32public:
33 using SelectAddrs = std::pair<Value *, Value *>;
34
35 SelectAddr(Value *Addr) : V(Addr), Addrs(nullptr, nullptr) {}
36 SelectAddr(Value *Cond, SelectAddrs Addrs) : V(Cond), Addrs(Addrs) {
37 assert(Cond && "Condition must be present");
38 assert(hasSelectAddrs() && "Addrs must be present");
39 }
40
41 bool hasSelectAddrs() const { return Addrs.first && Addrs.second; }
42
43 Value *getAddr() const {
44 assert(!hasSelectAddrs() && "this is a select address");
45 return V;
46 }
47
48 std::pair<Value *, SelectAddrs> getSelectCondAndAddrs() const {
49 assert(hasSelectAddrs() && "this is not a select address");
50 return {V, Addrs};
51 }
52
53private:
54 Value *V;
55 SelectAddrs Addrs;
56};
57
58/// PHITransAddr - An address value which tracks and handles phi translation.
59/// As we walk "up" the CFG through predecessors, we need to ensure that the
60/// address we're tracking is kept up to date. For example, if we're analyzing
61/// an address of "&A[i]" and walk through the definition of 'i' which is a PHI
62/// node, we *must* phi translate i to get "&A[j]" or else we will analyze an
63/// incorrect pointer in the predecessor block.
64///
65/// This is designed to be a relatively small object that lives on the stack and
66/// is copyable.
67///
69 /// Addr - The actual address we're analyzing.
70 Value *Addr;
71
72 /// The DataLayout we are playing with.
73 const DataLayout &DL;
74
75 /// TLI - The target library info if known, otherwise null.
76 const TargetLibraryInfo *TLI = nullptr;
77
78 /// A cache of \@llvm.assume calls used by SimplifyInstruction.
80
81 /// InstInputs - The inputs for our symbolic address.
83
84public:
86 : Addr(Addr), DL(DL), AC(AC) {
87 // If the address is an instruction, the whole thing is considered an input.
88 addAsInput(Addr);
89 }
90
91 Value *getAddr() const { return Addr; }
92
93 /// If the address expression depends on a select instruction (possibly
94 /// through casts or GEPs), return that select's condition. Otherwise return
95 /// nullptr. This is used to drive translation of both sides of a
96 /// select-dependent address (see the \p Cond overload of translateValue).
98
99 /// needsPHITranslationFromBlock - Return true if moving from the specified
100 /// BasicBlock to its predecessors requires PHI translation.
102 // We do need translation if one of our input instructions is defined in
103 // this block.
104 return any_of(InstInputs, [BB](const auto &InstInput) {
105 return InstInput->getParent() == BB;
106 });
107 }
108
109 /// isPotentiallyPHITranslatable - If this needs PHI translation, return true
110 /// if we have some hope of doing it. This should be used as a filter to
111 /// avoid calling PHITranslateValue in hopeless situations.
113
114 /// translateValue - PHI translate the current address up the CFG from
115 /// CurBB to Pred, updating our state to reflect any needed changes. If
116 /// 'MustDominate' is true, the translated value must dominate PredBB.
118 const DominatorTree *DT, bool MustDominate);
119
120 /// PHI translate the current address from \p CurBB to \p PredBB, and if the
121 /// resulting address depends on a select instruction with condition \p Cond,
122 /// translate both the "true" and the "false" side. Returns a pair of
123 /// addresses (true, false); either may be null on failure.
125 BasicBlock *PredBB,
126 const DominatorTree *DT,
127 Value *Cond);
128
129 /// translateWithInsertion - PHI translate this value into the specified
130 /// predecessor block, inserting a computation of the value if it is
131 /// unavailable.
132 ///
133 /// All newly created instructions are added to the NewInsts list. This
134 /// returns null on failure.
135 ///
138 const DominatorTree &DT,
140
141 LLVM_ABI void dump() const;
142
143 /// verify - Check internal consistency of this data structure. If the
144 /// structure is valid, it returns true. If invalid, it prints errors and
145 /// returns false.
146 LLVM_ABI bool verify() const;
147
148private:
149 Value *translateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB,
150 const DominatorTree *DT, Value *Cond = nullptr,
151 bool CondVal = false);
152
153 /// insertTranslatedSubExpr - Insert a computation of the PHI translated
154 /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
155 /// block. All newly created instructions are added to the NewInsts list.
156 /// This returns null on failure.
157 ///
158 Value *insertTranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
159 BasicBlock *PredBB, const DominatorTree &DT,
161
162 /// addAsInput - If the specified value is an instruction, add it as an input.
163 Value *addAsInput(Value *V) {
164 // If V is an instruction, it is now an input.
166 InstInputs.push_back(VI);
167 return V;
168 }
169};
170
171} // end namespace llvm
172
173#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:215
const SmallVectorImpl< MachineOperand > & Cond
This file defines the SmallVector class.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:151
LLVM_ABI Value * translateValue(BasicBlock *CurBB, BasicBlock *PredBB, const DominatorTree *DT, bool MustDominate)
translateValue - PHI translate the current address up the CFG from CurBB to Pred, updating our state ...
LLVM_ABI void dump() const
PHITransAddr(Value *Addr, const DataLayout &DL, AssumptionCache *AC)
LLVM_ABI bool isPotentiallyPHITranslatable() const
isPotentiallyPHITranslatable - If this needs PHI translation, return true if we have some hope of doi...
LLVM_ABI Value * getSelectCondition() const
If the address expression depends on a select instruction (possibly through casts or GEPs),...
LLVM_ABI bool verify() const
verify - Check internal consistency of this data structure.
LLVM_ABI Value * translateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB, const DominatorTree &DT, SmallVectorImpl< Instruction * > &NewInsts)
translateWithInsertion - PHI translate this value into the specified predecessor block,...
bool needsPHITranslationFromBlock(BasicBlock *BB) const
needsPHITranslationFromBlock - Return true if moving from the specified BasicBlock to its predecessor...
Value * getAddr() const
bool hasSelectAddrs() const
std::pair< Value *, SelectAddrs > getSelectCondAndAddrs() const
SelectAddr(Value *Cond, SelectAddrs Addrs)
SelectAddr(Value *Addr)
Value * getAddr() const
std::pair< Value *, Value * > SelectAddrs
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Provides information about what library functions are available for the current target.
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746