LLVM  6.0.0svn
Evaluator.h
Go to the documentation of this file.
1 //===- Evaluator.h - LLVM IR evaluator --------------------------*- C++ -*-===//
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 // Function evaluator for LLVM IR.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TRANSFORMS_UTILS_EVALUATOR_H
15 #define LLVM_TRANSFORMS_UTILS_EVALUATOR_H
16 
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/IR/BasicBlock.h"
21 #include "llvm/IR/GlobalVariable.h"
22 #include "llvm/IR/Value.h"
23 #include "llvm/Support/Casting.h"
24 #include <cassert>
25 #include <deque>
26 #include <memory>
27 
28 namespace llvm {
29 
30 class DataLayout;
31 class Function;
32 class TargetLibraryInfo;
33 
34 /// This class evaluates LLVM IR, producing the Constant representing each SSA
35 /// instruction. Changes to global variables are stored in a mapping that can
36 /// be iterated over after the evaluation is complete. Once an evaluation call
37 /// fails, the evaluation object should not be reused.
38 class Evaluator {
39 public:
40  Evaluator(const DataLayout &DL, const TargetLibraryInfo *TLI)
41  : DL(DL), TLI(TLI) {
42  ValueStack.emplace_back();
43  }
44 
46  for (auto &Tmp : AllocaTmps)
47  // If there are still users of the alloca, the program is doing something
48  // silly, e.g. storing the address of the alloca somewhere and using it
49  // later. Since this is undefined, we'll just make it be null.
50  if (!Tmp->use_empty())
51  Tmp->replaceAllUsesWith(Constant::getNullValue(Tmp->getType()));
52  }
53 
54  /// Evaluate a call to function F, returning true if successful, false if we
55  /// can't evaluate it. ActualArgs contains the formal arguments for the
56  /// function.
57  bool EvaluateFunction(Function *F, Constant *&RetVal,
58  const SmallVectorImpl<Constant*> &ActualArgs);
59 
60  /// Evaluate all instructions in block BB, returning true if successful, false
61  /// if we can't evaluate it. NewBB returns the next BB that control flows
62  /// into, or null upon return.
63  bool EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB);
64 
66  if (Constant *CV = dyn_cast<Constant>(V)) return CV;
67  Constant *R = ValueStack.back().lookup(V);
68  assert(R && "Reference to an uncomputed value!");
69  return R;
70  }
71 
72  void setVal(Value *V, Constant *C) {
73  ValueStack.back()[V] = C;
74  }
75 
77  return MutatedMemory;
78  }
79 
81  return Invariants;
82  }
83 
84 private:
85  Constant *ComputeLoadResult(Constant *P);
86 
87  /// As we compute SSA register values, we store their contents here. The back
88  /// of the deque contains the current function and the stack contains the
89  /// values in the calling frames.
90  std::deque<DenseMap<Value*, Constant*>> ValueStack;
91 
92  /// This is used to detect recursion. In pathological situations we could hit
93  /// exponential behavior, but at least there is nothing unbounded.
94  SmallVector<Function*, 4> CallStack;
95 
96  /// For each store we execute, we update this map. Loads check this to get
97  /// the most up-to-date value. If evaluation is successful, this state is
98  /// committed to the process.
99  DenseMap<Constant*, Constant*> MutatedMemory;
100 
101  /// To 'execute' an alloca, we create a temporary global variable to represent
102  /// its body. This vector is needed so we can delete the temporary globals
103  /// when we are done.
105 
106  /// These global variables have been marked invariant by the static
107  /// constructor.
109 
110  /// These are constants we have checked and know to be simple enough to live
111  /// in a static initializer of a global.
112  SmallPtrSet<Constant*, 8> SimpleConstants;
113 
114  const DataLayout &DL;
115  const TargetLibraryInfo *TLI;
116 };
117 
118 } // end namespace llvm
119 
120 #endif // LLVM_TRANSFORMS_UTILS_EVALUATOR_H
uint64_t CallInst * C
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
bool EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB)
Evaluate all instructions in block BB, returning true if successful, false if we can&#39;t evaluate it...
Definition: Evaluator.cpp:207
F(f)
bool EvaluateFunction(Function *F, Constant *&RetVal, const SmallVectorImpl< Constant *> &ActualArgs)
Evaluate a call to function F, returning true if successful, false if we can&#39;t evaluate it...
Definition: Evaluator.cpp:554
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:207
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:344
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
#define P(N)
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
This is an important base class in LLVM.
Definition: Constant.h:42
void setVal(Value *V, Constant *C)
Definition: Evaluator.h:72
This class evaluates LLVM IR, producing the Constant representing each SSA instruction.
Definition: Evaluator.h:38
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
Constant * getVal(Value *V)
Definition: Evaluator.h:65
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
Provides information about what library functions are available for the current target.
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:91
const SmallPtrSetImpl< GlobalVariable * > & getInvariants() const
Definition: Evaluator.h:80
const DenseMap< Constant *, Constant * > & getMutatedMemory() const
Definition: Evaluator.h:76
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
Evaluator(const DataLayout &DL, const TargetLibraryInfo *TLI)
Definition: Evaluator.h:40