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