Method.h no longer includes BasicBlock.h
[oota-llvm.git] / lib / Analysis / IPA / FindUnsafePointerTypes.cpp
1 //===- SafePointerAccess.cpp - Check pointer usage safety -------------------=//
2 //
3 // This file defines a pass that can be used to determine, interprocedurally, 
4 // which pointer types are accessed unsafely in a program.  If there is an
5 // "unsafe" access to a specific pointer type, transformations that depend on
6 // type safety cannot be permitted.
7 //
8 // The result of running this analysis over a program is a set of unsafe pointer
9 // types that cannot be transformed.  Safe pointer types are not tracked.
10 //
11 // Additionally, this analysis exports a hidden command line argument that (when
12 // enabled) prints out the reasons a type was determined to be unsafe.
13 //
14 // Currently, the only allowed operations on pointer types are:
15 //   alloca, malloc, free, getelementptr, load, and store
16 // 
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Analysis/FindUnsafePointerTypes.h"
20 #include "llvm/Assembly/CachedWriter.h"
21 #include "llvm/Type.h"
22 #include "llvm/Instruction.h"
23 #include "llvm/Method.h"
24 #include "llvm/Module.h"
25 #include "llvm/Support/InstIterator.h"
26 #include "Support/CommandLine.h"
27
28 AnalysisID FindUnsafePointerTypes::ID(AnalysisID::create<FindUnsafePointerTypes>());
29
30 // Provide a command line option to turn on printing of which instructions cause
31 // a type to become invalid
32 //
33 static cl::Flag 
34 PrintFailures("printunsafeptrinst", "Print Unsafe Pointer Access Instructions",
35               cl::Hidden, false);
36
37 static inline bool isSafeInstruction(const Instruction *I) {
38   switch (I->getOpcode()) {
39   case Instruction::Alloca:
40   case Instruction::Malloc:
41   case Instruction::Free:
42   case Instruction::Load:
43   case Instruction::Store:
44   case Instruction::GetElementPtr:
45   case Instruction::Call:
46   case Instruction::Invoke:
47   case Instruction::PHINode:
48     return true;
49   }
50   return false;
51 }
52
53
54 // runOnMethod - Inspect the operations that the specified method does on
55 // values of various types.  If they are deemed to be 'unsafe' note that the
56 // type is not safe to transform.
57 //
58 bool FindUnsafePointerTypes::run(Module *Mod) {
59   for (Module::iterator MI = Mod->begin(), ME = Mod->end();
60        MI != ME; ++MI) {
61     const Method *M = *MI;  // We don't need/want write access
62     for (const_inst_iterator I = inst_begin(M), E = inst_end(M); I != E; ++I) {
63       const Instruction *Inst = *I;
64       const Type *ITy = Inst->getType();
65       if (ITy->isPointerType() && !UnsafeTypes.count((PointerType*)ITy))
66         if (!isSafeInstruction(Inst)) {
67           UnsafeTypes.insert((PointerType*)ITy);
68
69           if (PrintFailures) {
70             CachedWriter CW(M->getParent(), std::cerr);
71             CW << "FindUnsafePointerTypes: Type '" << ITy
72                << "' marked unsafe in '" << M->getName() << "' by:\n" << Inst;
73           }
74         }
75     }
76   }
77
78   return false;
79 }
80
81
82 // printResults - Loop over the results of the analysis, printing out unsafe
83 // types.
84 //
85 void FindUnsafePointerTypes::printResults(const Module *M,
86                                           std::ostream &o) const {
87   if (UnsafeTypes.empty()) {
88     o << "SafePointerAccess Analysis: No unsafe types found!\n";
89     return;
90   }
91
92   CachedWriter CW(M, o);
93
94   CW << "SafePointerAccess Analysis: Found these unsafe types:\n";
95   unsigned Counter = 1;
96   for (std::set<PointerType*>::const_iterator I = getUnsafeTypes().begin(), 
97          E = getUnsafeTypes().end(); I != E; ++I, ++Counter) {
98     
99     CW << " #" << Counter << ". " << (Value*)*I << "\n";
100   }
101 }
102
103 // getAnalysisUsageInfo - Of course, we provide ourself...
104 //
105 void FindUnsafePointerTypes::getAnalysisUsageInfo(Pass::AnalysisSet &Required,
106                                                   Pass::AnalysisSet &Destroyed,
107                                                   Pass::AnalysisSet &Provided) {
108   Provided.push_back(FindUnsafePointerTypes::ID);
109 }