Eliminate duplicate or unneccesary #include's
[oota-llvm.git] / lib / Analysis / IPA / FindUnsafePointerTypes.cpp
1 //===- FindUnsafePointerTypes.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/Module.h"
24 #include "llvm/Support/InstIterator.h"
25 #include "Support/CommandLine.h"
26
27 AnalysisID FindUnsafePointerTypes::ID(AnalysisID::create<FindUnsafePointerTypes>());
28
29 // Provide a command line option to turn on printing of which instructions cause
30 // a type to become invalid
31 //
32 static cl::Flag 
33 PrintFailures("printunsafeptrinst", "Print Unsafe Pointer Access Instructions",
34               cl::Hidden, false);
35
36 static inline bool isSafeInstruction(const Instruction *I) {
37   switch (I->getOpcode()) {
38   case Instruction::Alloca:
39   case Instruction::Malloc:
40   case Instruction::Free:
41   case Instruction::Load:
42   case Instruction::Store:
43   case Instruction::GetElementPtr:
44   case Instruction::Call:
45   case Instruction::Invoke:
46   case Instruction::PHINode:
47     return true;
48   }
49   return false;
50 }
51
52
53 bool FindUnsafePointerTypes::run(Module *Mod) {
54   for (Module::iterator MI = Mod->begin(), ME = Mod->end();
55        MI != ME; ++MI) {
56     const Function *M = *MI;  // We don't need/want write access
57     for (const_inst_iterator I = inst_begin(M), E = inst_end(M); I != E; ++I) {
58       const Instruction *Inst = *I;
59       const Type *ITy = Inst->getType();
60       if (ITy->isPointerType() && !UnsafeTypes.count((PointerType*)ITy))
61         if (!isSafeInstruction(Inst)) {
62           UnsafeTypes.insert((PointerType*)ITy);
63
64           if (PrintFailures) {
65             CachedWriter CW(M->getParent(), std::cerr);
66             CW << "FindUnsafePointerTypes: Type '" << ITy
67                << "' marked unsafe in '" << M->getName() << "' by:\n" << Inst;
68           }
69         }
70     }
71   }
72
73   return false;
74 }
75
76
77 // printResults - Loop over the results of the analysis, printing out unsafe
78 // types.
79 //
80 void FindUnsafePointerTypes::printResults(const Module *M,
81                                           std::ostream &o) const {
82   if (UnsafeTypes.empty()) {
83     o << "SafePointerAccess Analysis: No unsafe types found!\n";
84     return;
85   }
86
87   CachedWriter CW(M, o);
88
89   CW << "SafePointerAccess Analysis: Found these unsafe types:\n";
90   unsigned Counter = 1;
91   for (std::set<PointerType*>::const_iterator I = getUnsafeTypes().begin(), 
92          E = getUnsafeTypes().end(); I != E; ++I, ++Counter) {
93     
94     CW << " #" << Counter << ". " << (Value*)*I << "\n";
95   }
96 }