blob: ce7eca5d27c155823f9e161a36b1a99011ea7f4f [file] [log] [blame]
Chris Lattner210e45a2009-12-04 02:10:16 +00001//===- PHITransAddr.cpp - PHI Translation for Addresses -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the PHITransAddr class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/PHITransAddr.h"
15#include "llvm/Analysis/Dominators.h"
16using namespace llvm;
17
18/// IsPHITranslatable - If this needs PHI translation, return true if we have
19/// some hope of doing it. This should be used as a filter to avoid calling
20/// GetPHITranslatedValue in hopeless situations.
21bool PHITransAddr::IsPHITranslatable() const {
22 return true; // not a good filter.
23}
24
25/// GetPHITranslatedValue - Given a computation that satisfied the
26/// isPHITranslatable predicate, see if we can translate the computation into
27/// the specified predecessor block. If so, return that value, otherwise
28/// return null.
29Value *PHITransAddr::GetPHITranslatedValue(Value *InVal, BasicBlock *CurBB,
30 BasicBlock *Pred,
31 const TargetData *TD) const {
32 // Not a great implementation.
33 return 0;
34}
35
36/// GetAvailablePHITranslatePointer - Return the value computed by
37/// PHITranslatePointer if it dominates PredBB, otherwise return null.
38Value *PHITransAddr::
39GetAvailablePHITranslatedValue(Value *V,
40 BasicBlock *CurBB, BasicBlock *PredBB,
41 const TargetData *TD,
42 const DominatorTree &DT) const {
43 // See if PHI translation succeeds.
44 V = GetPHITranslatedValue(V, CurBB, PredBB, TD);
45 if (V == 0) return 0;
46
47 // Make sure the value is live in the predecessor.
48 if (Instruction *Inst = dyn_cast_or_null<Instruction>(V))
49 if (!DT.dominates(Inst->getParent(), PredBB))
50 return 0;
51 return V;
52}
53
54/// InsertPHITranslatedPointer - Insert a computation of the PHI translated
55/// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
56/// block. All newly created instructions are added to the NewInsts list.
57/// This returns null on failure.
58///
59Value *PHITransAddr::
60InsertPHITranslatedPointer(Value *InVal, BasicBlock *CurBB,
61 BasicBlock *PredBB, const TargetData *TD,
62 const DominatorTree &DT,
63 SmallVectorImpl<Instruction*> &NewInsts) const {
64 // See if we have a version of this value already available and dominating
65 // PredBB. If so, there is no need to insert a new copy.
66 if (Value *Res = GetAvailablePHITranslatedValue(InVal, CurBB, PredBB, TD, DT))
67 return Res;
68
69 // Not a great implementation.
70 return 0;
71}