LLVM 24.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"
20
21namespace llvm {
22class AssumptionCache;
23class DominatorTree;
24class DataLayout;
26
27/// Storage of either a normal Value address, or a select instruction together
28/// with a pair of addresses for the "true" and "false" variant of a
29/// select-dependent address. If the addresses are not present (both null), V
30/// is a normal address; otherwise V is a select instruction and the pair holds
31/// the "true" and "false" addresses.
33public:
34 using SelectAddrs = std::pair<Value *, Value *>;
35
36 SelectAddr(Value *Addr) : V(Addr), Addrs(nullptr, nullptr) {}
37 SelectAddr(SelectInst *Sel, SelectAddrs Addrs) : V(Sel), Addrs(Addrs) {
38 assert(Sel && "Select must be present");
39 assert(hasSelectAddrs() && "Addrs must be present");
40 }
41
42 bool hasSelectAddrs() const { return Addrs.first && Addrs.second; }
43
44 Value *getAddr() const {
45 assert(!hasSelectAddrs() && "this is a select address");
46 return V;
47 }
48
49 std::pair<SelectInst *, SelectAddrs> getSelectAndAddrs() const {
50 assert(hasSelectAddrs() && "this is not a select address");
51 return {cast<SelectInst>(V), Addrs};
52 }
53
54private:
55 Value *V;
56 SelectAddrs Addrs;
57};
58
59/// PHITransAddr - An address value which tracks and handles phi translation.
60/// As we walk "up" the CFG through predecessors, we need to ensure that the
61/// address we're tracking is kept up to date. For example, if we're analyzing
62/// an address of "&A[i]" and walk through the definition of 'i' which is a PHI
63/// node, we *must* phi translate i to get "&A[j]" or else we will analyze an
64/// incorrect pointer in the predecessor block.
65///
66/// This is designed to be a relatively small object that lives on the stack and
67/// is copyable.
68///
70 /// Addr - The actual address we're analyzing.
71 Value *Addr;
72
73 /// The DataLayout we are playing with.
74 const DataLayout &DL;
75
76 /// TLI - The target library info if known, otherwise null.
77 const TargetLibraryInfo *TLI = nullptr;
78
79 /// A cache of \@llvm.assume calls used by SimplifyInstruction.
81
82 /// InstInputs - The inputs for our symbolic address.
84
85public:
87 : Addr(Addr), DL(DL), AC(AC) {
88 // If the address is an instruction, the whole thing is considered an input.
89 addAsInput(Addr);
90 }
91
92 Value *getAddr() const { return Addr; }
93
94 /// If the address expression depends on a select instruction (possibly
95 /// through casts or GEPs), return that select instruction. Otherwise return
96 /// nullptr. This is used to drive translation of both sides of a
97 /// select-dependent address (see the \p Cond overload of translateValue).
99
100 /// needsPHITranslationFromBlock - Return true if moving from the specified
101 /// BasicBlock to its predecessors requires PHI translation.
103 // We do need translation if one of our input instructions is defined in
104 // this block.
105 return any_of(InstInputs, [BB](const auto &InstInput) {
106 return InstInput->getParent() == BB;
107 });
108 }
109
110 /// isPotentiallyPHITranslatable - If this needs PHI translation, return true
111 /// if we have some hope of doing it. This should be used as a filter to
112 /// avoid calling PHITranslateValue in hopeless situations.
114
115 /// translateValue - PHI translate the current address up the CFG from
116 /// CurBB to Pred, updating our state to reflect any needed changes. If
117 /// 'MustDominate' is true, the translated value must dominate PredBB.
119 const DominatorTree *DT, bool MustDominate);
120
121 /// PHI translate the current address from \p CurBB to \p PredBB, and if the
122 /// resulting address depends on a select instruction with condition \p Cond,
123 /// translate both the "true" and the "false" side. Returns a pair of
124 /// addresses (true, false); either may be null on failure.
126 BasicBlock *PredBB,
127 const DominatorTree *DT,
128 Value *Cond);
129
130 /// translateWithInsertion - PHI translate this value into the specified
131 /// predecessor block, inserting a computation of the value if it is
132 /// unavailable.
133 ///
134 /// All newly created instructions are added to the NewInsts list. This
135 /// returns null on failure.
136 ///
139 const DominatorTree &DT,
141
142 LLVM_ABI void dump() const;
143
144 /// verify - Check internal consistency of this data structure. If the
145 /// structure is valid, it returns true. If invalid, it prints errors and
146 /// returns false.
147 LLVM_ABI bool verify() const;
148
149private:
150 Value *translateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB,
151 const DominatorTree *DT, Value *Cond = nullptr,
152 bool CondVal = false);
153
154 /// insertTranslatedSubExpr - Insert a computation of the PHI translated
155 /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
156 /// block. All newly created instructions are added to the NewInsts list.
157 /// This returns null on failure.
158 ///
159 Value *insertTranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
160 BasicBlock *PredBB, const DominatorTree &DT,
162
163 /// addAsInput - If the specified value is an instruction, add it as an input.
164 Value *addAsInput(Value *V) {
165 // If V is an instruction, it is now an input.
167 InstInputs.push_back(VI);
168 return V;
169 }
170};
171
172} // end namespace llvm
173
174#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:122
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 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,...
LLVM_ABI SelectInst * getSelect() const
If the address expression depends on a select instruction (possibly through casts or GEPs),...
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< SelectInst *, SelectAddrs > getSelectAndAddrs() const
SelectAddr(Value *Addr)
Value * getAddr() const
std::pair< Value *, Value * > SelectAddrs
SelectAddr(SelectInst *Sel, SelectAddrs Addrs)
This class represents the LLVM 'select' instruction.
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:1762
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559