1 //===- PHITransAddr.cpp - PHI Translation for Addresses -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the PHITransAddr class.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Analysis/PHITransAddr.h"
14 #include "llvm/Analysis/InstructionSimplify.h"
15 #include "llvm/Analysis/ValueTracking.h"
16 #include "llvm/Config/llvm-config.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Dominators.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
24 static cl::opt
<bool> EnableAddPhiTranslation(
25 "gvn-add-phi-translation", cl::init(false), cl::Hidden
,
26 cl::desc("Enable phi-translation of add instructions"));
28 static bool canPHITrans(Instruction
*Inst
) {
29 if (isa
<PHINode
>(Inst
) || isa
<GetElementPtrInst
>(Inst
) || isa
<CastInst
>(Inst
))
32 if (Inst
->getOpcode() == Instruction::Add
&&
33 isa
<ConstantInt
>(Inst
->getOperand(1)))
39 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
40 LLVM_DUMP_METHOD
void PHITransAddr::dump() const {
42 dbgs() << "PHITransAddr: null\n";
45 dbgs() << "PHITransAddr: " << *Addr
<< "\n";
46 for (unsigned i
= 0, e
= InstInputs
.size(); i
!= e
; ++i
)
47 dbgs() << " Input #" << i
<< " is " << *InstInputs
[i
] << "\n";
51 static bool verifySubExpr(Value
*Expr
,
52 SmallVectorImpl
<Instruction
*> &InstInputs
) {
53 // If this is a non-instruction value, there is nothing to do.
54 Instruction
*I
= dyn_cast
<Instruction
>(Expr
);
57 // If it's an instruction, it is either in Tmp or its operands recursively
59 if (auto Entry
= find(InstInputs
, I
); Entry
!= InstInputs
.end()) {
60 InstInputs
.erase(Entry
);
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";
69 llvm_unreachable("Either something is missing from InstInputs or "
70 "canPHITrans is wrong.");
73 // Validate the operands of the instruction.
74 return all_of(I
->operands(),
75 [&](Value
*Op
) { return verifySubExpr(Op
, InstInputs
); });
78 /// verify - Check internal consistency of this data structure. If the
79 /// structure is valid, it returns true. If invalid, it prints errors and
81 bool PHITransAddr::verify() const {
82 if (!Addr
) return true;
84 SmallVector
<Instruction
*, 8> Tmp(InstInputs
.begin(), InstInputs
.end());
86 if (!verifySubExpr(Addr
, Tmp
))
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.");
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.
103 bool PHITransAddr::isPotentiallyPHITranslatable() const {
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
);
110 static void RemoveInstInputs(Value
*V
,
111 SmallVectorImpl
<Instruction
*> &InstInputs
) {
112 Instruction
*I
= dyn_cast
<Instruction
>(V
);
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
);
121 assert(!isa
<PHINode
>(I
) && "Error, removing something that isn't an input");
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
);
129 Value
*PHITransAddr::translateSubExpr(Value
*V
, BasicBlock
*CurBB
,
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
);
136 // Determine whether 'Inst' is an input to our PHI translatable expression.
137 bool isInput
= is_contained(InstInputs
, Inst
);
139 // Handle inputs instructions if needed.
141 if (Inst
->getParent() != CurBB
) {
142 // If it is an input defined in a different block, then it remains an
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.
150 // In either case, the instruction itself isn't an input any longer.
151 InstInputs
.erase(find(InstInputs
, Inst
));
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
));
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
))
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())
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.
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))
178 // Find an available version of this cast.
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
);
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
)))
199 // Handle getelementptr with at least one PHI translatable operand.
200 if (GetElementPtrInst
*GEP
= dyn_cast
<GetElementPtrInst
>(Inst
)) {
201 SmallVector
<Value
*, 8> GEPOps
;
202 bool AnyChanged
= false;
203 for (Value
*Op
: GEP
->operands()) {
204 Value
*GEPOp
= translateSubExpr(Op
, CurBB
, PredBB
, DT
);
205 if (!GEPOp
) return nullptr;
207 AnyChanged
|= GEPOp
!= Op
;
208 GEPOps
.push_back(GEPOp
);
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
);
221 return addAsInput(V
);
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()))
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();
248 Value
*LHS
= translateSubExpr(Inst
->getOperand(0), CurBB
, PredBB
, DT
);
249 if (!LHS
) return nullptr;
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;
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
);
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
270 RemoveInstInputs(LHS
, InstInputs
);
271 return addAsInput(Res
);
274 // If we didn't modify the add, just return it.
275 if (LHS
== Inst
->getOperand(0) && RHS
== Inst
->getOperand(1))
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
)))
291 // Otherwise, we failed.
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.
298 Value
*PHITransAddr::translateValue(BasicBlock
*CurBB
, BasicBlock
*PredBB
,
299 const DominatorTree
*DT
,
301 assert(DT
|| !MustDominate
);
302 assert(verify() && "Invalid PHITransAddr!");
303 if (DT
&& DT
->isReachableFromEntry(PredBB
))
304 Addr
= translateSubExpr(Addr
, CurBB
, PredBB
, DT
);
307 assert(verify() && "Invalid PHITransAddr!");
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
))
318 /// PHITranslateWithInsertion - PHI translate this value into the specified
319 /// predecessor block, inserting a computation of the value if it is
322 /// All newly created instructions are added to the NewInsts list. This
323 /// returns null on failure.
326 PHITransAddr::translateWithInsertion(BasicBlock
*CurBB
, BasicBlock
*PredBB
,
327 const DominatorTree
&DT
,
328 SmallVectorImpl
<Instruction
*> &NewInsts
) {
329 unsigned NISize
= NewInsts
.size();
331 // Attempt to PHI translate with insertion.
332 Addr
= insertTranslatedSubExpr(Addr
, CurBB
, PredBB
, DT
, NewInsts
);
334 // If successful, return the new value.
335 if (Addr
) return Addr
;
337 // If not, destroy any intermediate instructions inserted.
338 while (NewInsts
.size() != NISize
)
339 NewInsts
.pop_back_val()->eraseFromParent();
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.
348 Value
*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
);
355 Tmp
.translateValue(CurBB
, PredBB
, &DT
, /*MustDominate=*/true))
358 // We don't need to PHI translate values which aren't instructions.
359 auto *Inst
= dyn_cast
<Instruction
>(InVal
);
363 // Handle cast of PHI translatable value.
364 if (CastInst
*Cast
= dyn_cast
<CastInst
>(Inst
)) {
365 Value
*OpVal
= insertTranslatedSubExpr(Cast
->getOperand(0), CurBB
, PredBB
,
367 if (!OpVal
) return nullptr;
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
);
378 // Handle getelementptr with at least one PHI operand.
379 if (GetElementPtrInst
*GEP
= dyn_cast
<GetElementPtrInst
>(Inst
)) {
380 SmallVector
<Value
*, 8> GEPOps
;
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
);
388 GetElementPtrInst
*Result
= GetElementPtrInst::Create(
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
);
397 // Handle add with a constant RHS.
398 if (EnableAddPhiTranslation
&& Inst
->getOpcode() == Instruction::Add
&&
399 isa
<ConstantInt
>(Inst
->getOperand(1))) {
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.
405 // PHI translate the LHS.
406 Value
*OpVal
= insertTranslatedSubExpr(Inst
->getOperand(0), CurBB
, PredBB
,
408 if (OpVal
== nullptr)
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
);