klee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Passes.h
Go to the documentation of this file.
1 //===-- Passes.h ------------------------------------------------*- C++ -*-===//
2 //
3 // The KLEE Symbolic Virtual Machine
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef KLEE_PASSES_H
11 #define KLEE_PASSES_H
12 
13 #include "klee/Config/Version.h"
14 
15 #if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3)
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/Module.h"
19 #else
20 #include "llvm/Constants.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Module.h"
23 #endif
24 #include "llvm/Pass.h"
25 #include "llvm/CodeGen/IntrinsicLowering.h"
26 
27 namespace llvm {
28  class Function;
29  class Instruction;
30  class Module;
31 #if LLVM_VERSION_CODE <= LLVM_VERSION(3, 1)
32  class TargetData;
33 #else
34  class DataLayout;
35 #endif
36  class TargetLowering;
37  class Type;
38 }
39 
40 namespace klee {
41 
44 class RaiseAsmPass : public llvm::ModulePass {
45  static char ID;
46 
47  const llvm::TargetLowering *TLI;
48 
49  llvm::Function *getIntrinsic(llvm::Module &M,
50  unsigned IID,
51  LLVM_TYPE_Q llvm::Type **Tys,
52  unsigned NumTys);
53  llvm::Function *getIntrinsic(llvm::Module &M,
54  unsigned IID,
55  LLVM_TYPE_Q llvm::Type *Ty0) {
56  return getIntrinsic(M, IID, &Ty0, 1);
57  }
58 
59  bool runOnInstruction(llvm::Module &M, llvm::Instruction *I);
60 
61 public:
62  RaiseAsmPass() : llvm::ModulePass(ID), TLI(0) {}
63 
64  virtual bool runOnModule(llvm::Module &M);
65 };
66 
67  // This is a module pass because it can add and delete module
68  // variables (via intrinsic lowering).
69 class IntrinsicCleanerPass : public llvm::ModulePass {
70  static char ID;
71 #if LLVM_VERSION_CODE <= LLVM_VERSION(3, 1)
72  const llvm::TargetData &TargetData;
73 #else
74  const llvm::DataLayout &DataLayout;
75 #endif
76  llvm::IntrinsicLowering *IL;
78 
79  bool runOnBasicBlock(llvm::BasicBlock &b, llvm::Module &M);
80 public:
81 #if LLVM_VERSION_CODE <= LLVM_VERSION(3, 1)
82  IntrinsicCleanerPass(const llvm::TargetData &TD,
83 #else
84  IntrinsicCleanerPass(const llvm::DataLayout &TD,
85 #endif
86  bool LI=true)
87  : llvm::ModulePass(ID),
89  TargetData(TD),
90 #else
91  DataLayout(TD),
92 #endif
93  IL(new llvm::IntrinsicLowering(TD)),
94  LowerIntrinsics(LI) {}
95  ~IntrinsicCleanerPass() { delete IL; }
96 
97  virtual bool runOnModule(llvm::Module &M);
98 };
99 
100  // performs two transformations which make interpretation
101  // easier and faster.
102  //
103  // 1) Ensure that all the PHI nodes in a basic block have
104  // the incoming block list in the same order. Thus the
105  // incoming block index only needs to be computed once
106  // for each transfer.
107  //
108  // 2) Ensure that no PHI node result is used as an argument to
109  // a subsequent PHI node in the same basic block. This allows
110  // the transfer to execute the instructions in order instead
111  // of in two passes.
112 class PhiCleanerPass : public llvm::FunctionPass {
113  static char ID;
114 
115 public:
116  PhiCleanerPass() : llvm::FunctionPass(ID) {}
117 
118  virtual bool runOnFunction(llvm::Function &f);
119 };
120 
121 class DivCheckPass : public llvm::ModulePass {
122  static char ID;
123 public:
124  DivCheckPass(): ModulePass(ID) {}
125  virtual bool runOnModule(llvm::Module &M);
126 };
127 
142 class OvershiftCheckPass : public llvm::ModulePass {
143  static char ID;
144 public:
145  OvershiftCheckPass(): ModulePass(ID) {}
146  virtual bool runOnModule(llvm::Module &M);
147 };
148 
152 class LowerSwitchPass : public llvm::FunctionPass {
153 public:
154  static char ID; // Pass identification, replacement for typeid
155  LowerSwitchPass() : FunctionPass(ID) {}
156 
157  virtual bool runOnFunction(llvm::Function &F);
158 
159  struct SwitchCase {
160  llvm ::Constant *value;
161  llvm::BasicBlock *block;
162 
163  SwitchCase() : value(0), block(0) { }
164  SwitchCase(llvm::Constant *v, llvm::BasicBlock *b) :
165  value(v), block(b) { }
166  };
167 
168  typedef std::vector<SwitchCase> CaseVector;
169  typedef std::vector<SwitchCase>::iterator CaseItr;
170 
171 private:
172  void processSwitchInst(llvm::SwitchInst *SI);
173  void switchConvert(CaseItr begin,
174  CaseItr end,
175  llvm::Value *value,
176  llvm::BasicBlock *origBlock,
177  llvm::BasicBlock *defaultBlock);
178 };
179 
180 }
181 
182 #endif
void switchConvert(CaseItr begin, CaseItr end, llvm::Value *value, llvm::BasicBlock *origBlock, llvm::BasicBlock *defaultBlock)
Definition: LowerSwitch.cpp:60
llvm::Function * getIntrinsic(llvm::Module &M, unsigned IID, LLVM_TYPE_Q llvm::Type *Ty0)
Definition: Passes.h:53
SwitchCase(llvm::Constant *v, llvm::BasicBlock *b)
Definition: Passes.h:164
virtual bool runOnModule(llvm::Module &M)
bool runOnBasicBlock(llvm::BasicBlock &b, llvm::Module &M)
static char ID
Definition: Passes.h:122
const llvm::TargetData & TargetData
Definition: Passes.h:72
std::vector< SwitchCase >::iterator CaseItr
Definition: Passes.h:169
llvm::IntrinsicLowering * IL
Definition: Passes.h:76
static char ID
Definition: Passes.h:113
#define LLVM_VERSION_CODE
Definition: Version.h:16
#define LLVM_VERSION(major, minor)
Definition: Version.h:15
virtual bool runOnFunction(llvm::Function &F)
Definition: LowerSwitch.cpp:43
static char ID
Definition: Passes.h:45
#define LLVM_TYPE_Q
Definition: Version.h:21
std::vector< SwitchCase > CaseVector
Definition: Passes.h:168
llvm::BasicBlock * block
Definition: Passes.h:161
virtual bool runOnModule(llvm::Module &M)
Definition: Checks.cpp:55
void processSwitchInst(llvm::SwitchInst *SI)
Definition: LowerSwitch.cpp:97
const llvm::TargetLowering * TLI
Definition: Passes.h:47
static char ID
Definition: Passes.h:154
IntrinsicCleanerPass(const llvm::TargetData &TD, bool LI=true)
Definition: Passes.h:82
llvm::Function * getIntrinsic(llvm::Module &M, unsigned IID, LLVM_TYPE_Q llvm::Type **Tys, unsigned NumTys)
virtual bool runOnModule(llvm::Module &M)
Definition: RaiseAsm.cpp:59
bool runOnInstruction(llvm::Module &M, llvm::Instruction *I)
Definition: RaiseAsm.cpp:48
virtual bool runOnModule(llvm::Module &M)
Definition: Checks.cpp:102
virtual bool runOnFunction(llvm::Function &f)
Definition: PhiCleaner.cpp:18