1 //===- PHITransAddr.cpp - PHI Translation for Addresses -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the PHITransAddr class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/PHITransAddr.h"
15 #include "llvm/Constants.h"
16 #include "llvm/Instructions.h"
17 #include "llvm/Analysis/Dominators.h"
18 #include "llvm/Analysis/InstructionSimplify.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
24 static bool CanPHITrans(Instruction
*Inst
) {
25 if (isa
<PHINode
>(Inst
) ||
26 isa
<GetElementPtrInst
>(Inst
))
29 if (isa
<CastInst
>(Inst
) &&
30 Inst
->isSafeToSpeculativelyExecute())
33 if (Inst
->getOpcode() == Instruction::Add
&&
34 isa
<ConstantInt
>(Inst
->getOperand(1)))
37 // cerr << "MEMDEP: Could not PHI translate: " << *Pointer;
38 // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst))
39 // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0);
43 void PHITransAddr::dump() const {
45 dbgs() << "PHITransAddr: null\n";
48 dbgs() << "PHITransAddr: " << *Addr
<< "\n";
49 for (unsigned i
= 0, e
= InstInputs
.size(); i
!= e
; ++i
)
50 dbgs() << " Input #" << i
<< " is " << *InstInputs
[i
] << "\n";
54 static bool VerifySubExpr(Value
*Expr
,
55 SmallVectorImpl
<Instruction
*> &InstInputs
) {
56 // If this is a non-instruction value, there is nothing to do.
57 Instruction
*I
= dyn_cast
<Instruction
>(Expr
);
58 if (I
== 0) return true;
60 // If it's an instruction, it is either in Tmp or its operands recursively
62 SmallVectorImpl
<Instruction
*>::iterator Entry
=
63 std::find(InstInputs
.begin(), InstInputs
.end(), I
);
64 if (Entry
!= InstInputs
.end()) {
65 InstInputs
.erase(Entry
);
69 // If it isn't in the InstInputs list it is a subexpr incorporated into the
70 // address. Sanity check that it is phi translatable.
71 if (!CanPHITrans(I
)) {
72 errs() << "Non phi translatable instruction found in PHITransAddr:\n";
74 llvm_unreachable("Either something is missing from InstInputs or "
75 "CanPHITrans is wrong.");
79 // Validate the operands of the instruction.
80 for (unsigned i
= 0, e
= I
->getNumOperands(); i
!= e
; ++i
)
81 if (!VerifySubExpr(I
->getOperand(i
), InstInputs
))
87 /// Verify - Check internal consistency of this data structure. If the
88 /// structure is valid, it returns true. If invalid, it prints errors and
90 bool PHITransAddr::Verify() const {
91 if (Addr
== 0) return true;
93 SmallVector
<Instruction
*, 8> Tmp(InstInputs
.begin(), InstInputs
.end());
95 if (!VerifySubExpr(Addr
, Tmp
))
99 errs() << "PHITransAddr contains extra instructions:\n";
100 for (unsigned i
= 0, e
= InstInputs
.size(); i
!= e
; ++i
)
101 errs() << " InstInput #" << i
<< " is " << *InstInputs
[i
] << "\n";
102 llvm_unreachable("This is unexpected.");
111 /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
112 /// if we have some hope of doing it. This should be used as a filter to
113 /// avoid calling PHITranslateValue in hopeless situations.
114 bool PHITransAddr::IsPotentiallyPHITranslatable() const {
115 // If the input value is not an instruction, or if it is not defined in CurBB,
116 // then we don't need to phi translate it.
117 Instruction
*Inst
= dyn_cast
<Instruction
>(Addr
);
118 return Inst
== 0 || CanPHITrans(Inst
);
122 static void RemoveInstInputs(Value
*V
,
123 SmallVectorImpl
<Instruction
*> &InstInputs
) {
124 Instruction
*I
= dyn_cast
<Instruction
>(V
);
127 // If the instruction is in the InstInputs list, remove it.
128 SmallVectorImpl
<Instruction
*>::iterator Entry
=
129 std::find(InstInputs
.begin(), InstInputs
.end(), I
);
130 if (Entry
!= InstInputs
.end()) {
131 InstInputs
.erase(Entry
);
135 assert(!isa
<PHINode
>(I
) && "Error, removing something that isn't an input");
137 // Otherwise, it must have instruction inputs itself. Zap them recursively.
138 for (unsigned i
= 0, e
= I
->getNumOperands(); i
!= e
; ++i
) {
139 if (Instruction
*Op
= dyn_cast
<Instruction
>(I
->getOperand(i
)))
140 RemoveInstInputs(Op
, InstInputs
);
144 Value
*PHITransAddr::PHITranslateSubExpr(Value
*V
, BasicBlock
*CurBB
,
146 const DominatorTree
*DT
) {
147 // If this is a non-instruction value, it can't require PHI translation.
148 Instruction
*Inst
= dyn_cast
<Instruction
>(V
);
149 if (Inst
== 0) return V
;
151 // Determine whether 'Inst' is an input to our PHI translatable expression.
152 bool isInput
= std::count(InstInputs
.begin(), InstInputs
.end(), Inst
);
154 // Handle inputs instructions if needed.
156 if (Inst
->getParent() != CurBB
) {
157 // If it is an input defined in a different block, then it remains an
162 // If 'Inst' is defined in this block and is an input that needs to be phi
163 // translated, we need to incorporate the value into the expression or fail.
165 // In either case, the instruction itself isn't an input any longer.
166 InstInputs
.erase(std::find(InstInputs
.begin(), InstInputs
.end(), Inst
));
168 // If this is a PHI, go ahead and translate it.
169 if (PHINode
*PN
= dyn_cast
<PHINode
>(Inst
))
170 return AddAsInput(PN
->getIncomingValueForBlock(PredBB
));
172 // If this is a non-phi value, and it is analyzable, we can incorporate it
173 // into the expression by making all instruction operands be inputs.
174 if (!CanPHITrans(Inst
))
177 // All instruction operands are now inputs (and of course, they may also be
178 // defined in this block, so they may need to be phi translated themselves.
179 for (unsigned i
= 0, e
= Inst
->getNumOperands(); i
!= e
; ++i
)
180 if (Instruction
*Op
= dyn_cast
<Instruction
>(Inst
->getOperand(i
)))
181 InstInputs
.push_back(Op
);
184 // Ok, it must be an intermediate result (either because it started that way
185 // or because we just incorporated it into the expression). See if its
186 // operands need to be phi translated, and if so, reconstruct it.
188 if (CastInst
*Cast
= dyn_cast
<CastInst
>(Inst
)) {
189 if (!Cast
->isSafeToSpeculativelyExecute()) return 0;
190 Value
*PHIIn
= PHITranslateSubExpr(Cast
->getOperand(0), CurBB
, PredBB
, DT
);
191 if (PHIIn
== 0) return 0;
192 if (PHIIn
== Cast
->getOperand(0))
195 // Find an available version of this cast.
197 // Constants are trivial to find.
198 if (Constant
*C
= dyn_cast
<Constant
>(PHIIn
))
199 return AddAsInput(ConstantExpr::getCast(Cast
->getOpcode(),
200 C
, Cast
->getType()));
202 // Otherwise we have to see if a casted version of the incoming pointer
203 // is available. If so, we can use it, otherwise we have to fail.
204 for (Value::use_iterator UI
= PHIIn
->use_begin(), E
= PHIIn
->use_end();
206 if (CastInst
*CastI
= dyn_cast
<CastInst
>(*UI
))
207 if (CastI
->getOpcode() == Cast
->getOpcode() &&
208 CastI
->getType() == Cast
->getType() &&
209 (!DT
|| DT
->dominates(CastI
->getParent(), PredBB
)))
215 // Handle getelementptr with at least one PHI translatable operand.
216 if (GetElementPtrInst
*GEP
= dyn_cast
<GetElementPtrInst
>(Inst
)) {
217 SmallVector
<Value
*, 8> GEPOps
;
218 bool AnyChanged
= false;
219 for (unsigned i
= 0, e
= GEP
->getNumOperands(); i
!= e
; ++i
) {
220 Value
*GEPOp
= PHITranslateSubExpr(GEP
->getOperand(i
), CurBB
, PredBB
, DT
);
221 if (GEPOp
== 0) return 0;
223 AnyChanged
|= GEPOp
!= GEP
->getOperand(i
);
224 GEPOps
.push_back(GEPOp
);
230 // Simplify the GEP to handle 'gep x, 0' -> x etc.
231 if (Value
*V
= SimplifyGEPInst(&GEPOps
[0], GEPOps
.size(), TD
, DT
)) {
232 for (unsigned i
= 0, e
= GEPOps
.size(); i
!= e
; ++i
)
233 RemoveInstInputs(GEPOps
[i
], InstInputs
);
235 return AddAsInput(V
);
238 // Scan to see if we have this GEP available.
239 Value
*APHIOp
= GEPOps
[0];
240 for (Value::use_iterator UI
= APHIOp
->use_begin(), E
= APHIOp
->use_end();
242 if (GetElementPtrInst
*GEPI
= dyn_cast
<GetElementPtrInst
>(*UI
))
243 if (GEPI
->getType() == GEP
->getType() &&
244 GEPI
->getNumOperands() == GEPOps
.size() &&
245 GEPI
->getParent()->getParent() == CurBB
->getParent() &&
246 (!DT
|| DT
->dominates(GEPI
->getParent(), PredBB
))) {
247 bool Mismatch
= false;
248 for (unsigned i
= 0, e
= GEPOps
.size(); i
!= e
; ++i
)
249 if (GEPI
->getOperand(i
) != GEPOps
[i
]) {
260 // Handle add with a constant RHS.
261 if (Inst
->getOpcode() == Instruction::Add
&&
262 isa
<ConstantInt
>(Inst
->getOperand(1))) {
263 // PHI translate the LHS.
264 Constant
*RHS
= cast
<ConstantInt
>(Inst
->getOperand(1));
265 bool isNSW
= cast
<BinaryOperator
>(Inst
)->hasNoSignedWrap();
266 bool isNUW
= cast
<BinaryOperator
>(Inst
)->hasNoUnsignedWrap();
268 Value
*LHS
= PHITranslateSubExpr(Inst
->getOperand(0), CurBB
, PredBB
, DT
);
269 if (LHS
== 0) return 0;
271 // If the PHI translated LHS is an add of a constant, fold the immediates.
272 if (BinaryOperator
*BOp
= dyn_cast
<BinaryOperator
>(LHS
))
273 if (BOp
->getOpcode() == Instruction::Add
)
274 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(BOp
->getOperand(1))) {
275 LHS
= BOp
->getOperand(0);
276 RHS
= ConstantExpr::getAdd(RHS
, CI
);
277 isNSW
= isNUW
= false;
279 // If the old 'LHS' was an input, add the new 'LHS' as an input.
280 if (std::count(InstInputs
.begin(), InstInputs
.end(), BOp
)) {
281 RemoveInstInputs(BOp
, InstInputs
);
286 // See if the add simplifies away.
287 if (Value
*Res
= SimplifyAddInst(LHS
, RHS
, isNSW
, isNUW
, TD
, DT
)) {
288 // If we simplified the operands, the LHS is no longer an input, but Res
290 RemoveInstInputs(LHS
, InstInputs
);
291 return AddAsInput(Res
);
294 // If we didn't modify the add, just return it.
295 if (LHS
== Inst
->getOperand(0) && RHS
== Inst
->getOperand(1))
298 // Otherwise, see if we have this add available somewhere.
299 for (Value::use_iterator UI
= LHS
->use_begin(), E
= LHS
->use_end();
301 if (BinaryOperator
*BO
= dyn_cast
<BinaryOperator
>(*UI
))
302 if (BO
->getOpcode() == Instruction::Add
&&
303 BO
->getOperand(0) == LHS
&& BO
->getOperand(1) == RHS
&&
304 BO
->getParent()->getParent() == CurBB
->getParent() &&
305 (!DT
|| DT
->dominates(BO
->getParent(), PredBB
)))
312 // Otherwise, we failed.
317 /// PHITranslateValue - PHI translate the current address up the CFG from
318 /// CurBB to Pred, updating our state to reflect any needed changes. If the
319 /// dominator tree DT is non-null, the translated value must dominate
320 /// PredBB. This returns true on failure and sets Addr to null.
321 bool PHITransAddr::PHITranslateValue(BasicBlock
*CurBB
, BasicBlock
*PredBB
,
322 const DominatorTree
*DT
) {
323 assert(Verify() && "Invalid PHITransAddr!");
324 Addr
= PHITranslateSubExpr(Addr
, CurBB
, PredBB
, DT
);
325 assert(Verify() && "Invalid PHITransAddr!");
328 // Make sure the value is live in the predecessor.
329 if (Instruction
*Inst
= dyn_cast_or_null
<Instruction
>(Addr
))
330 if (!DT
->dominates(Inst
->getParent(), PredBB
))
337 /// PHITranslateWithInsertion - PHI translate this value into the specified
338 /// predecessor block, inserting a computation of the value if it is
341 /// All newly created instructions are added to the NewInsts list. This
342 /// returns null on failure.
344 Value
*PHITransAddr::
345 PHITranslateWithInsertion(BasicBlock
*CurBB
, BasicBlock
*PredBB
,
346 const DominatorTree
&DT
,
347 SmallVectorImpl
<Instruction
*> &NewInsts
) {
348 unsigned NISize
= NewInsts
.size();
350 // Attempt to PHI translate with insertion.
351 Addr
= InsertPHITranslatedSubExpr(Addr
, CurBB
, PredBB
, DT
, NewInsts
);
353 // If successful, return the new value.
354 if (Addr
) return Addr
;
356 // If not, destroy any intermediate instructions inserted.
357 while (NewInsts
.size() != NISize
)
358 NewInsts
.pop_back_val()->eraseFromParent();
363 /// InsertPHITranslatedPointer - Insert a computation of the PHI translated
364 /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
365 /// block. All newly created instructions are added to the NewInsts list.
366 /// This returns null on failure.
368 Value
*PHITransAddr::
369 InsertPHITranslatedSubExpr(Value
*InVal
, BasicBlock
*CurBB
,
370 BasicBlock
*PredBB
, const DominatorTree
&DT
,
371 SmallVectorImpl
<Instruction
*> &NewInsts
) {
372 // See if we have a version of this value already available and dominating
373 // PredBB. If so, there is no need to insert a new instance of it.
374 PHITransAddr
Tmp(InVal
, TD
);
375 if (!Tmp
.PHITranslateValue(CurBB
, PredBB
, &DT
))
376 return Tmp
.getAddr();
378 // If we don't have an available version of this value, it must be an
380 Instruction
*Inst
= cast
<Instruction
>(InVal
);
382 // Handle cast of PHI translatable value.
383 if (CastInst
*Cast
= dyn_cast
<CastInst
>(Inst
)) {
384 if (!Cast
->isSafeToSpeculativelyExecute()) return 0;
385 Value
*OpVal
= InsertPHITranslatedSubExpr(Cast
->getOperand(0),
386 CurBB
, PredBB
, DT
, NewInsts
);
387 if (OpVal
== 0) return 0;
389 // Otherwise insert a cast at the end of PredBB.
390 CastInst
*New
= CastInst::Create(Cast
->getOpcode(),
391 OpVal
, InVal
->getType(),
392 InVal
->getName()+".phi.trans.insert",
393 PredBB
->getTerminator());
394 NewInsts
.push_back(New
);
398 // Handle getelementptr with at least one PHI operand.
399 if (GetElementPtrInst
*GEP
= dyn_cast
<GetElementPtrInst
>(Inst
)) {
400 SmallVector
<Value
*, 8> GEPOps
;
401 BasicBlock
*CurBB
= GEP
->getParent();
402 for (unsigned i
= 0, e
= GEP
->getNumOperands(); i
!= e
; ++i
) {
403 Value
*OpVal
= InsertPHITranslatedSubExpr(GEP
->getOperand(i
),
404 CurBB
, PredBB
, DT
, NewInsts
);
405 if (OpVal
== 0) return 0;
406 GEPOps
.push_back(OpVal
);
409 GetElementPtrInst
*Result
=
410 GetElementPtrInst::Create(GEPOps
[0], GEPOps
.begin()+1, GEPOps
.end(),
411 InVal
->getName()+".phi.trans.insert",
412 PredBB
->getTerminator());
413 Result
->setIsInBounds(GEP
->isInBounds());
414 NewInsts
.push_back(Result
);
419 // FIXME: This code works, but it is unclear that we actually want to insert
420 // a big chain of computation in order to make a value available in a block.
421 // This needs to be evaluated carefully to consider its cost trade offs.
423 // Handle add with a constant RHS.
424 if (Inst
->getOpcode() == Instruction::Add
&&
425 isa
<ConstantInt
>(Inst
->getOperand(1))) {
426 // PHI translate the LHS.
427 Value
*OpVal
= InsertPHITranslatedSubExpr(Inst
->getOperand(0),
428 CurBB
, PredBB
, DT
, NewInsts
);
429 if (OpVal
== 0) return 0;
431 BinaryOperator
*Res
= BinaryOperator::CreateAdd(OpVal
, Inst
->getOperand(1),
432 InVal
->getName()+".phi.trans.insert",
433 PredBB
->getTerminator());
434 Res
->setHasNoSignedWrap(cast
<BinaryOperator
>(Inst
)->hasNoSignedWrap());
435 Res
->setHasNoUnsignedWrap(cast
<BinaryOperator
>(Inst
)->hasNoUnsignedWrap());
436 NewInsts
.push_back(Res
);