14 #ifndef LLVM_ANALYSIS_SPARSEPROPAGATION_H 15 #define LLVM_ANALYSIS_SPARSEPROPAGATION_H 21 #define DEBUG_TYPE "sparseprop" 32 template <
class LatticeKey,
class LatticeVal,
47 LatticeVal UndefVal, OverdefinedVal, UntrackedVal;
51 LatticeVal untrackedVal) {
53 OverdefinedVal = overdefinedVal;
54 UntrackedVal = untrackedVal;
71 return getOverdefinedVal();
82 return getOverdefinedVal();
94 virtual void PrintLatticeVal(LatticeVal LV,
raw_ostream &OS);
110 template <
class LatticeKey,
class LatticeVal,
class KeyInfo>
129 using Edge = std::pair<BasicBlock *, BasicBlock *>;
133 std::set<Edge> KnownFeasibleEdges;
138 : LatticeFunc(Lattice) {}
151 auto I = ValueState.
find(Key);
158 LatticeVal getValueState(LatticeKey
Key);
166 bool AggressiveUndef =
false);
172 return BBExecutable.
count(BB);
183 void UpdateState(LatticeKey Key, LatticeVal LV);
192 bool AggressiveUndef);
203 template <
class LatticeKey,
class LatticeVal>
208 else if (V == OverdefinedVal)
210 else if (V == UntrackedVal)
213 OS <<
"unknown lattice value";
216 template <
class LatticeKey,
class LatticeVal>
219 OS <<
"unknown lattice key";
226 template <
class LatticeKey,
class LatticeVal,
class KeyInfo>
229 auto I = ValueState.find(Key);
230 if (
I != ValueState.end())
233 if (LatticeFunc->IsUntrackedValue(Key))
234 return LatticeFunc->getUntrackedVal();
235 LatticeVal LV = LatticeFunc->ComputeLatticeVal(Key);
238 if (LV == LatticeFunc->getUntrackedVal())
240 return ValueState[
Key] = std::move(LV);
243 template <
class LatticeKey,
class LatticeVal,
class KeyInfo>
246 auto I = ValueState.find(Key);
247 if (
I != ValueState.end() &&
I->second == LV)
252 ValueState[
Key] = std::move(LV);
253 if (
Value *V = KeyInfo::getValueFromLatticeKey(Key))
254 ValueWorkList.push_back(V);
257 template <
class LatticeKey,
class LatticeVal,
class KeyInfo>
260 if (!BBExecutable.insert(BB).second)
263 BBWorkList.push_back(BB);
266 template <
class LatticeKey,
class LatticeVal,
class KeyInfo>
269 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
273 <<
" -> " << Dest->
getName() <<
"\n");
275 if (BBExecutable.count(Dest)) {
280 visitPHINode(*cast<PHINode>(
I));
282 MarkBlockExecutable(Dest);
286 template <
class LatticeKey,
class LatticeVal,
class KeyInfo>
293 if (
BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
294 if (BI->isUnconditional()) {
302 getValueState(KeyInfo::getLatticeKeyFromValue(BI->getCondition()));
304 BCValue = getExistingValueState(
305 KeyInfo::getLatticeKeyFromValue(BI->getCondition()));
307 if (BCValue == LatticeFunc->getOverdefinedVal() ||
308 BCValue == LatticeFunc->getUntrackedVal()) {
310 Succs[0] = Succs[1] =
true;
315 if (BCValue == LatticeFunc->getUndefVal())
319 dyn_cast_or_null<Constant>(LatticeFunc->GetValueFromLatticeVal(
320 std::move(BCValue), BI->getCondition()->getType()));
321 if (!C || !isa<ConstantInt>(C)) {
323 Succs[0] = Succs[1] =
true;
341 SCValue = getValueState(KeyInfo::getLatticeKeyFromValue(SI.
getCondition()));
343 SCValue = getExistingValueState(
346 if (SCValue == LatticeFunc->getOverdefinedVal() ||
347 SCValue == LatticeFunc->getUntrackedVal()) {
354 if (SCValue == LatticeFunc->getUndefVal())
357 Constant *
C = dyn_cast_or_null<Constant>(LatticeFunc->GetValueFromLatticeVal(
359 if (!C || !isa<ConstantInt>(C)) {
368 template <
class LatticeKey,
class LatticeVal,
class KeyInfo>
373 getFeasibleSuccessors(*TI, SuccFeasible, AggressiveUndef);
382 template <
class LatticeKey,
class LatticeVal,
class KeyInfo>
386 getFeasibleSuccessors(TI, SuccFeasible,
true);
391 for (
unsigned i = 0,
e = SuccFeasible.
size(); i !=
e; ++i)
396 template <
class LatticeKey,
class LatticeVal,
class KeyInfo>
401 if (LatticeFunc->IsSpecialCasedPHI(&PN)) {
403 LatticeFunc->ComputeInstructionState(PN, ChangedValues, *
this);
404 for (
auto &ChangedValue : ChangedValues)
405 if (ChangedValue.second != LatticeFunc->getUntrackedVal())
406 UpdateState(std::move(ChangedValue.first),
407 std::move(ChangedValue.second));
411 LatticeKey Key = KeyInfo::getLatticeKeyFromValue(&PN);
412 LatticeVal PNIV = getValueState(Key);
413 LatticeVal Overdefined = LatticeFunc->getOverdefinedVal();
416 if (PNIV == Overdefined || PNIV == LatticeFunc->getUntrackedVal())
422 UpdateState(Key, Overdefined);
438 PNIV = LatticeFunc->MergeValues(PNIV, OpVal);
440 if (PNIV == Overdefined)
445 UpdateState(Key, PNIV);
448 template <
class LatticeKey,
class LatticeVal,
class KeyInfo>
452 if (
PHINode *PN = dyn_cast<PHINode>(&I))
453 return visitPHINode(*PN);
458 LatticeFunc->ComputeInstructionState(I, ChangedValues, *
this);
459 for (
auto &ChangedValue : ChangedValues)
460 if (ChangedValue.second != LatticeFunc->getUntrackedVal())
461 UpdateState(ChangedValue.first, ChangedValue.second);
467 template <
class LatticeKey,
class LatticeVal,
class KeyInfo>
470 while (!BBWorkList.empty() || !ValueWorkList.empty()) {
472 while (!ValueWorkList.empty()) {
473 Value *V = ValueWorkList.back();
474 ValueWorkList.pop_back();
482 if (BBExecutable.count(Inst->getParent()))
487 while (!BBWorkList.empty()) {
489 BBWorkList.pop_back();
501 template <
class LatticeKey,
class LatticeVal,
class KeyInfo>
504 if (ValueState.empty())
510 OS <<
"ValueState:\n";
511 for (
auto &
Entry : ValueState) {
512 std::tie(Key, LV) =
Entry;
513 if (LV == LatticeFunc->getUntrackedVal())
516 LatticeFunc->PrintLatticeVal(LV, OS);
518 LatticeFunc->PrintLatticeKey(Key, OS);
526 #endif // LLVM_ANALYSIS_SPARSEPROPAGATION_H
void Print(raw_ostream &OS) const
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
virtual LatticeVal MergeValues(LatticeVal X, LatticeVal Y)
MergeValues - Compute and return the merge of the two specified lattice values.
SparseSolver(AbstractLatticeFunction< LatticeKey, LatticeVal > *Lattice)
This class represents lattice values for constants.
virtual void PrintLatticeVal(LatticeVal LV, raw_ostream &OS)
PrintLatticeVal - Render the given LatticeVal to the specified stream.
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
Value * getCondition() const
bool isTerminator() const
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...
virtual Value * GetValueFromLatticeVal(LatticeVal LV, Type *Ty=nullptr)
GetValueFromLatticeVal - If the given LatticeVal is representable as an LLVM value, return it; otherwise, return nullptr.
iterator begin()
Instruction iterator methods.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
void assign(size_type NumElts, const T &Elt)
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
void Solve()
Solve - Solve for constants and executable blocks.
Type * getType() const
All values are typed, get the type of this value.
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
iterator find(const_arg_type_t< KeyT > Val)
virtual void PrintLatticeKey(LatticeKey Key, raw_ostream &OS)
PrintLatticeKey - Render the given LatticeKey to the specified stream.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
Conditional or Unconditional Branch instruction.
This is an important base class in LLVM.
LatticeVal getUntrackedVal() const
const Instruction & back() const
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
SparseSolver - This class is a general purpose solver for Sparse Conditional Propagation with a progr...
bool isExceptionalTerminator() const
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
AbstractLatticeFunction - This class is implemented by the dataflow instance to specify what the latt...
LatticeVal getExistingValueState(LatticeKey Key) const
getExistingValueState - Return the LatticeVal object corresponding to the given value from the ValueS...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
BlockVerifier::State From
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool isEdgeFeasible(BasicBlock *From, BasicBlock *To, bool AggressiveUndef=false)
isEdgeFeasible - Return true if the control flow edge from the 'From' basic block to the 'To' basic b...
unsigned getNumIncomingValues() const
Return the number of incoming edges.
CaseIt findCaseValue(const ConstantInt *C)
Search all of the case values for the specified constant.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LatticeVal getValueState(LatticeKey Key)
getValueState - Return the LatticeVal object corresponding to the given value from the ValueState map...
iterator_range< user_iterator > users()
InstListType::iterator iterator
Instruction iterators...
unsigned getSuccessorIndex() const
Returns successor index for current case successor.
virtual bool IsSpecialCasedPHI(PHINode *PN)
IsSpecialCasedPHI - Given a PHI node, determine whether this PHI node is one that the we want to hand...
LatticeVal getUndefVal() const
AbstractLatticeFunction(LatticeVal undefVal, LatticeVal overdefinedVal, LatticeVal untrackedVal)
bool isBlockExecutable(BasicBlock *BB) const
isBlockExecutable - Return true if there are any known feasible edges into the basic block...
virtual bool IsUntrackedValue(LatticeKey Key)
IsUntrackedValue - If the specified LatticeKey is obviously uninteresting to the analysis (i...
StringRef getName() const
Return a constant reference to the value's name.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
LatticeVal getOverdefinedVal() const
bool isIndirectTerminator() const
void MarkBlockExecutable(BasicBlock *BB)
MarkBlockExecutable - This method can be used by clients to mark all of the blocks that are known to ...
LLVM Value Representation.
This class implements an extremely fast bulk output stream that can only output to a stream...
virtual LatticeVal ComputeLatticeVal(LatticeKey Key)
ComputeLatticeVal - Compute and return a LatticeVal corresponding to the given LatticeKey.
const BasicBlock * getParent() const
A template for translating between LLVM Values and LatticeKeys.