Implement a more powerful, simpler, pass system. This pass system can figure
[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 "Support/CommandLine.h"
23
24 // Provide a command line option to turn on printing of which instructions cause
25 // a type to become invalid
26 //
27 static cl::Flag 
28 PrintFailures("printunsafeptrinst", "Print Unsafe Pointer Access Instructions",
29               cl::Hidden, false);
30
31 static inline bool isSafeInstruction(const Instruction *I) {
32   switch (I->getOpcode()) {
33   case Instruction::Alloca:
34   case Instruction::Malloc:
35   case Instruction::Free:
36   case Instruction::Load:
37   case Instruction::Store:
38   case Instruction::GetElementPtr:
39   case Instruction::Call:
40   case Instruction::Invoke:
41   case Instruction::PHINode:
42     return true;
43   }
44   return false;
45 }
46
47
48 // runOnMethod - Inspect the operations that the specified method does on
49 // values of various types.  If they are deemed to be 'unsafe' note that the
50 // type is not safe to transform.
51 //
52 bool FindUnsafePointerTypes::runOnMethod(Method *Meth) {
53   const Method *M = Meth;  // We don't need/want write access
54   for (Method::const_inst_iterator I = M->inst_begin(), E = M->inst_end();
55        I != E; ++I) {
56     const Instruction *Inst = *I;
57     const Type *ITy = Inst->getType();
58     if (ITy->isPointerType() && !UnsafeTypes.count((PointerType*)ITy))
59       if (!isSafeInstruction(Inst)) {
60         UnsafeTypes.insert((PointerType*)ITy);
61
62         if (PrintFailures) {
63           CachedWriter CW(M->getParent(), std::cerr);
64           CW << "FindUnsafePointerTypes: Type '" << ITy
65              << "' marked unsafe in '" << Meth->getName() << "' by:\n" << Inst;
66         }
67       }
68   }
69
70   return false;
71 }
72
73
74 // printResults - Loop over the results of the analysis, printing out unsafe
75 // types.
76 //
77 void FindUnsafePointerTypes::printResults(const Module *M, std::ostream &o) {
78   if (UnsafeTypes.empty()) {
79     o << "SafePointerAccess Analysis: No unsafe types found!\n";
80     return;
81   }
82
83   CachedWriter CW(M, o);
84
85   CW << "SafePointerAccess Analysis: Found these unsafe types:\n";
86   unsigned Counter = 1;
87   for (std::set<PointerType*>::const_iterator I = getUnsafeTypes().begin(), 
88          E = getUnsafeTypes().end(); I != E; ++I, ++Counter) {
89     
90     CW << " #" << Counter << ". " << (Value*)*I << "\n";
91   }
92 }