LLVM 19.0.0git
PHITransAddr.cpp
Go to the documentation of this file.
1//===- PHITransAddr.cpp - PHI Translation for Addresses -------------------===//
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 implements the PHITransAddr class.
10//
11//===----------------------------------------------------------------------===//
12
16#include "llvm/Config/llvm-config.h"
17#include "llvm/IR/Constants.h"
18#include "llvm/IR/Dominators.h"
22using namespace llvm;
23
25 "gvn-add-phi-translation", cl::init(false), cl::Hidden,
26 cl::desc("Enable phi-translation of add instructions"));
27
28static bool canPHITrans(Instruction *Inst) {
29 if (isa<PHINode>(Inst) || isa<GetElementPtrInst>(Inst) || isa<CastInst>(Inst))
30 return true;
31
32 if (Inst->getOpcode() == Instruction::Add &&
33 isa<ConstantInt>(Inst->getOperand(1)))
34 return true;
35
36 return false;
37}
38
39#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
41 if (!Addr) {
42 dbgs() << "PHITransAddr: null\n";
43 return;
44 }
45 dbgs() << "PHITransAddr: " << *Addr << "\n";
46 for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
47 dbgs() << " Input #" << i << " is " << *InstInputs[i] << "\n";
48}
49#endif
50
51static bool verifySubExpr(Value *Expr,
53 // If this is a non-instruction value, there is nothing to do.
54 Instruction *I = dyn_cast<Instruction>(Expr);
55 if (!I) return true;
56
57 // If it's an instruction, it is either in Tmp or its operands recursively
58 // are.
59 if (auto Entry = find(InstInputs, I); Entry != InstInputs.end()) {
60 InstInputs.erase(Entry);
61 return true;
62 }
63
64 // If it isn't in the InstInputs list it is a subexpr incorporated into the
65 // address. Validate that it is phi translatable.
66 if (!canPHITrans(I)) {
67 errs() << "Instruction in PHITransAddr is not phi-translatable:\n";
68 errs() << *I << '\n';
69 llvm_unreachable("Either something is missing from InstInputs or "
70 "canPHITrans is wrong.");
71 }
72
73 // Validate the operands of the instruction.
74 return all_of(I->operands(),
75 [&](Value *Op) { return verifySubExpr(Op, InstInputs); });
76}
77
78/// verify - Check internal consistency of this data structure. If the
79/// structure is valid, it returns true. If invalid, it prints errors and
80/// returns false.
82 if (!Addr) return true;
83
84 SmallVector<Instruction*, 8> Tmp(InstInputs.begin(), InstInputs.end());
85
86 if (!verifySubExpr(Addr, Tmp))
87 return false;
88
89 if (!Tmp.empty()) {
90 errs() << "PHITransAddr contains extra instructions:\n";
91 for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
92 errs() << " InstInput #" << i << " is " << *InstInputs[i] << "\n";
93 llvm_unreachable("This is unexpected.");
94 }
95
96 // a-ok.
97 return true;
98}
99
100/// isPotentiallyPHITranslatable - If this needs PHI translation, return true
101/// if we have some hope of doing it. This should be used as a filter to
102/// avoid calling PHITranslateValue in hopeless situations.
104 // If the input value is not an instruction, or if it is not defined in CurBB,
105 // then we don't need to phi translate it.
106 Instruction *Inst = dyn_cast<Instruction>(Addr);
107 return !Inst || canPHITrans(Inst);
108}
109
110static void RemoveInstInputs(Value *V,
111 SmallVectorImpl<Instruction*> &InstInputs) {
112 Instruction *I = dyn_cast<Instruction>(V);
113 if (!I) return;
114
115 // If the instruction is in the InstInputs list, remove it.
116 if (auto Entry = find(InstInputs, I); Entry != InstInputs.end()) {
117 InstInputs.erase(Entry);
118 return;
119 }
120
121 assert(!isa<PHINode>(I) && "Error, removing something that isn't an input");
122
123 // Otherwise, it must have instruction inputs itself. Zap them recursively.
124 for (Value *Op : I->operands())
125 if (Instruction *OpInst = dyn_cast<Instruction>(Op))
126 RemoveInstInputs(OpInst, InstInputs);
127}
128
129Value *PHITransAddr::translateSubExpr(Value *V, BasicBlock *CurBB,
130 BasicBlock *PredBB,
131 const DominatorTree *DT) {
132 // If this is a non-instruction value, it can't require PHI translation.
133 Instruction *Inst = dyn_cast<Instruction>(V);
134 if (!Inst) return V;
135
136 // Determine whether 'Inst' is an input to our PHI translatable expression.
137 bool isInput = is_contained(InstInputs, Inst);
138
139 // Handle inputs instructions if needed.
140 if (isInput) {
141 if (Inst->getParent() != CurBB) {
142 // If it is an input defined in a different block, then it remains an
143 // input.
144 return Inst;
145 }
146
147 // If 'Inst' is defined in this block and is an input that needs to be phi
148 // translated, we need to incorporate the value into the expression or fail.
149
150 // In either case, the instruction itself isn't an input any longer.
151 InstInputs.erase(find(InstInputs, Inst));
152
153 // If this is a PHI, go ahead and translate it.
154 if (PHINode *PN = dyn_cast<PHINode>(Inst))
155 return addAsInput(PN->getIncomingValueForBlock(PredBB));
156
157 // If this is a non-phi value, and it is analyzable, we can incorporate it
158 // into the expression by making all instruction operands be inputs.
159 if (!canPHITrans(Inst))
160 return nullptr;
161
162 // All instruction operands are now inputs (and of course, they may also be
163 // defined in this block, so they may need to be phi translated themselves.
164 for (Value *Op : Inst->operands())
165 addAsInput(Op);
166 }
167
168 // Ok, it must be an intermediate result (either because it started that way
169 // or because we just incorporated it into the expression). See if its
170 // operands need to be phi translated, and if so, reconstruct it.
171
172 if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
173 Value *PHIIn = translateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT);
174 if (!PHIIn) return nullptr;
175 if (PHIIn == Cast->getOperand(0))
176 return Cast;
177
178 // Find an available version of this cast.
179
180 // Try to simplify cast first.
181 if (Value *V = simplifyCastInst(Cast->getOpcode(), PHIIn, Cast->getType(),
182 {DL, TLI, DT, AC})) {
183 RemoveInstInputs(PHIIn, InstInputs);
184 return addAsInput(V);
185 }
186
187 // Otherwise we have to see if a casted version of the incoming pointer
188 // is available. If so, we can use it, otherwise we have to fail.
189 for (User *U : PHIIn->users()) {
190 if (CastInst *CastI = dyn_cast<CastInst>(U))
191 if (CastI->getOpcode() == Cast->getOpcode() &&
192 CastI->getType() == Cast->getType() &&
193 (!DT || DT->dominates(CastI->getParent(), PredBB)))
194 return CastI;
195 }
196 return nullptr;
197 }
198
199 // Handle getelementptr with at least one PHI translatable operand.
200 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
202 bool AnyChanged = false;
203 for (Value *Op : GEP->operands()) {
204 Value *GEPOp = translateSubExpr(Op, CurBB, PredBB, DT);
205 if (!GEPOp) return nullptr;
206
207 AnyChanged |= GEPOp != Op;
208 GEPOps.push_back(GEPOp);
209 }
210
211 if (!AnyChanged)
212 return GEP;
213
214 // Simplify the GEP to handle 'gep x, 0' -> x etc.
215 if (Value *V = simplifyGEPInst(GEP->getSourceElementType(), GEPOps[0],
216 ArrayRef<Value *>(GEPOps).slice(1),
217 GEP->isInBounds(), {DL, TLI, DT, AC})) {
218 for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
219 RemoveInstInputs(GEPOps[i], InstInputs);
220
221 return addAsInput(V);
222 }
223
224 // Scan to see if we have this GEP available.
225 Value *APHIOp = GEPOps[0];
226 for (User *U : APHIOp->users()) {
227 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U))
228 if (GEPI->getType() == GEP->getType() &&
229 GEPI->getSourceElementType() == GEP->getSourceElementType() &&
230 GEPI->getNumOperands() == GEPOps.size() &&
231 GEPI->getParent()->getParent() == CurBB->getParent() &&
232 (!DT || DT->dominates(GEPI->getParent(), PredBB))) {
233 if (std::equal(GEPOps.begin(), GEPOps.end(), GEPI->op_begin()))
234 return GEPI;
235 }
236 }
237 return nullptr;
238 }
239
240 // Handle add with a constant RHS.
241 if (Inst->getOpcode() == Instruction::Add &&
242 isa<ConstantInt>(Inst->getOperand(1))) {
243 // PHI translate the LHS.
244 Constant *RHS = cast<ConstantInt>(Inst->getOperand(1));
245 bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap();
246 bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap();
247
248 Value *LHS = translateSubExpr(Inst->getOperand(0), CurBB, PredBB, DT);
249 if (!LHS) return nullptr;
250
251 // If the PHI translated LHS is an add of a constant, fold the immediates.
252 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS))
253 if (BOp->getOpcode() == Instruction::Add)
254 if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
255 LHS = BOp->getOperand(0);
256 RHS = ConstantExpr::getAdd(RHS, CI);
257 isNSW = isNUW = false;
258
259 // If the old 'LHS' was an input, add the new 'LHS' as an input.
260 if (is_contained(InstInputs, BOp)) {
261 RemoveInstInputs(BOp, InstInputs);
262 addAsInput(LHS);
263 }
264 }
265
266 // See if the add simplifies away.
267 if (Value *Res = simplifyAddInst(LHS, RHS, isNSW, isNUW, {DL, TLI, DT, AC})) {
268 // If we simplified the operands, the LHS is no longer an input, but Res
269 // is.
270 RemoveInstInputs(LHS, InstInputs);
271 return addAsInput(Res);
272 }
273
274 // If we didn't modify the add, just return it.
275 if (LHS == Inst->getOperand(0) && RHS == Inst->getOperand(1))
276 return Inst;
277
278 // Otherwise, see if we have this add available somewhere.
279 for (User *U : LHS->users()) {
280 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U))
281 if (BO->getOpcode() == Instruction::Add &&
282 BO->getOperand(0) == LHS && BO->getOperand(1) == RHS &&
283 BO->getParent()->getParent() == CurBB->getParent() &&
284 (!DT || DT->dominates(BO->getParent(), PredBB)))
285 return BO;
286 }
287
288 return nullptr;
289 }
290
291 // Otherwise, we failed.
292 return nullptr;
293}
294
295/// PHITranslateValue - PHI translate the current address up the CFG from
296/// CurBB to Pred, updating our state to reflect any needed changes. If
297/// 'MustDominate' is true, the translated value must dominate PredBB.
299 const DominatorTree *DT,
300 bool MustDominate) {
301 assert(DT || !MustDominate);
302 assert(verify() && "Invalid PHITransAddr!");
303 if (DT && DT->isReachableFromEntry(PredBB))
304 Addr = translateSubExpr(Addr, CurBB, PredBB, DT);
305 else
306 Addr = nullptr;
307 assert(verify() && "Invalid PHITransAddr!");
308
309 if (MustDominate)
310 // Make sure the value is live in the predecessor.
311 if (Instruction *Inst = dyn_cast_or_null<Instruction>(Addr))
312 if (!DT->dominates(Inst->getParent(), PredBB))
313 Addr = nullptr;
314
315 return Addr;
316}
317
318/// PHITranslateWithInsertion - PHI translate this value into the specified
319/// predecessor block, inserting a computation of the value if it is
320/// unavailable.
321///
322/// All newly created instructions are added to the NewInsts list. This
323/// returns null on failure.
324///
325Value *
327 const DominatorTree &DT,
329 unsigned NISize = NewInsts.size();
330
331 // Attempt to PHI translate with insertion.
332 Addr = insertTranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts);
333
334 // If successful, return the new value.
335 if (Addr) return Addr;
336
337 // If not, destroy any intermediate instructions inserted.
338 while (NewInsts.size() != NISize)
339 NewInsts.pop_back_val()->eraseFromParent();
340 return nullptr;
341}
342
343/// insertTranslatedSubExpr - Insert a computation of the PHI translated
344/// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
345/// block. All newly created instructions are added to the NewInsts list.
346/// This returns null on failure.
347///
348Value *PHITransAddr::insertTranslatedSubExpr(
349 Value *InVal, BasicBlock *CurBB, BasicBlock *PredBB,
350 const DominatorTree &DT, SmallVectorImpl<Instruction *> &NewInsts) {
351 // See if we have a version of this value already available and dominating
352 // PredBB. If so, there is no need to insert a new instance of it.
353 PHITransAddr Tmp(InVal, DL, AC);
354 if (Value *Addr =
355 Tmp.translateValue(CurBB, PredBB, &DT, /*MustDominate=*/true))
356 return Addr;
357
358 // We don't need to PHI translate values which aren't instructions.
359 auto *Inst = dyn_cast<Instruction>(InVal);
360 if (!Inst)
361 return nullptr;
362
363 // Handle cast of PHI translatable value.
364 if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
365 Value *OpVal = insertTranslatedSubExpr(Cast->getOperand(0), CurBB, PredBB,
366 DT, NewInsts);
367 if (!OpVal) return nullptr;
368
369 // Otherwise insert a cast at the end of PredBB.
370 CastInst *New = CastInst::Create(Cast->getOpcode(), OpVal, InVal->getType(),
371 InVal->getName() + ".phi.trans.insert",
372 PredBB->getTerminator());
373 New->setDebugLoc(Inst->getDebugLoc());
374 NewInsts.push_back(New);
375 return New;
376 }
377
378 // Handle getelementptr with at least one PHI operand.
379 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
381 BasicBlock *CurBB = GEP->getParent();
382 for (Value *Op : GEP->operands()) {
383 Value *OpVal = insertTranslatedSubExpr(Op, CurBB, PredBB, DT, NewInsts);
384 if (!OpVal) return nullptr;
385 GEPOps.push_back(OpVal);
386 }
387
389 GEP->getSourceElementType(), GEPOps[0], ArrayRef(GEPOps).slice(1),
390 InVal->getName() + ".phi.trans.insert", PredBB->getTerminator());
391 Result->setDebugLoc(Inst->getDebugLoc());
392 Result->setIsInBounds(GEP->isInBounds());
393 NewInsts.push_back(Result);
394 return Result;
395 }
396
397 // Handle add with a constant RHS.
398 if (EnableAddPhiTranslation && Inst->getOpcode() == Instruction::Add &&
399 isa<ConstantInt>(Inst->getOperand(1))) {
400
401 // FIXME: This code works, but it is unclear that we actually want to insert
402 // a big chain of computation in order to make a value available in a block.
403 // This needs to be evaluated carefully to consider its cost trade offs.
404
405 // PHI translate the LHS.
406 Value *OpVal = insertTranslatedSubExpr(Inst->getOperand(0), CurBB, PredBB,
407 DT, NewInsts);
408 if (OpVal == nullptr)
409 return nullptr;
410
411 BinaryOperator *Res = BinaryOperator::CreateAdd(OpVal, Inst->getOperand(1),
412 InVal->getName()+".phi.trans.insert",
413 PredBB->getTerminator());
414 Res->setHasNoSignedWrap(cast<BinaryOperator>(Inst)->hasNoSignedWrap());
415 Res->setHasNoUnsignedWrap(cast<BinaryOperator>(Inst)->hasNoUnsignedWrap());
416 NewInsts.push_back(Res);
417 return Res;
418 }
419
420 return nullptr;
421}
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:529
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Hexagon Common GEP
static bool hasNoSignedWrap(BinaryOperator &I)
static bool hasNoUnsignedWrap(BinaryOperator &I)
#define I(x, y, z)
Definition: MD5.cpp:58
static bool isInput(const ArrayRef< StringLiteral > &Prefixes, StringRef Arg)
Definition: OptTable.cpp:151
static void RemoveInstInputs(Value *V, SmallVectorImpl< Instruction * > &InstInputs)
static bool canPHITrans(Instruction *Inst)
static cl::opt< bool > EnableAddPhiTranslation("gvn-add-phi-translation", cl::init(false), cl::Hidden, cl::desc("Enable phi-translation of add instructions"))
static bool verifySubExpr(Value *Expr, SmallVectorImpl< Instruction * > &InstInputs)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition: ArrayRef.h:195
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:214
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:229
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:483
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2537
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
This is an important base class in LLVM.
Definition: Constant.h:41
This class represents an Operation in the Expression.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:321
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:122
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:949
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:975
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag.
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:452
const BasicBlock * getParent() const
Definition: Instruction.h:150
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:250
PHITransAddr - An address value which tracks and handles phi translation.
Definition: PHITransAddr.h:35
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 ...
void dump() const
bool isPotentiallyPHITranslatable() const
isPotentiallyPHITranslatable - If this needs PHI translation, return true if we have some hope of doi...
bool verify() const
verify - Check internal consistency of this data structure.
Value * translateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB, const DominatorTree &DT, SmallVectorImpl< Instruction * > &NewInsts)
translateWithInsertion - PHI translate this value into the specified predecessor block,...
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
iterator erase(const_iterator CI)
Definition: SmallVector.h:750
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
op_range operands()
Definition: User.h:242
Value * getOperand(unsigned i) const
Definition: User.h:169
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
iterator_range< user_iterator > users()
Definition: Value.h:421
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1751
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1731
Value * simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, const SimplifyQuery &Q)
Given operands for a CastInst, fold the result or return null.
Value * simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
DWARFExpression::Operation Op
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1888
Value * simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef< Value * > Indices, bool InBounds, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, fold the result or return null.