Added LLVM project notice to the top of every C++ source file.
[oota-llvm.git] / lib / Analysis / IPA / FindUnsafePointerTypes.cpp
1 //===- FindUnsafePointerTypes.cpp - Check pointer usage safety ------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a pass that can be used to determine, interprocedurally, 
11 // which pointer types are accessed unsafely in a program.  If there is an
12 // "unsafe" access to a specific pointer type, transformations that depend on
13 // type safety cannot be permitted.
14 //
15 // The result of running this analysis over a program is a set of unsafe pointer
16 // types that cannot be transformed.  Safe pointer types are not tracked.
17 //
18 // Additionally, this analysis exports a hidden command line argument that (when
19 // enabled) prints out the reasons a type was determined to be unsafe.
20 //
21 // Currently, the only allowed operations on pointer types are:
22 //   alloca, malloc, free, getelementptr, load, and store
23 // 
24 //===----------------------------------------------------------------------===//
25
26 #include "llvm/Analysis/FindUnsafePointerTypes.h"
27 #include "llvm/Assembly/CachedWriter.h"
28 #include "llvm/Type.h"
29 #include "llvm/Module.h"
30 #include "llvm/Support/InstIterator.h"
31 #include "Support/CommandLine.h"
32
33 static RegisterAnalysis<FindUnsafePointerTypes>
34 X("unsafepointertypes", "Find Unsafe Pointer Types");
35
36 // Provide a command line option to turn on printing of which instructions cause
37 // a type to become invalid
38 //
39 static cl::opt<bool> 
40 PrintFailures("printunsafeptrinst", cl::Hidden,
41               cl::desc("Print Unsafe Pointer Access Instructions"));
42
43 static inline bool isSafeInstruction(const Instruction *I) {
44   switch (I->getOpcode()) {
45   case Instruction::Alloca:
46   case Instruction::Malloc:
47   case Instruction::Free:
48   case Instruction::Load:
49   case Instruction::Store:
50   case Instruction::GetElementPtr:
51   case Instruction::Call:
52   case Instruction::Invoke:
53   case Instruction::PHI:
54     return true;
55   }
56   return false;
57 }
58
59
60 bool FindUnsafePointerTypes::run(Module &Mod) {
61   for (Module::iterator FI = Mod.begin(), E = Mod.end();
62        FI != E; ++FI) {
63     const Function *F = FI;  // We don't need/want write access
64     for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
65       const Type *ITy = I->getType();
66       if (isa<PointerType>(ITy) && !UnsafeTypes.count((PointerType*)ITy))
67         if (!isSafeInstruction(*I)) {
68           UnsafeTypes.insert((PointerType*)ITy);
69
70           if (PrintFailures) {
71             CachedWriter CW(F->getParent(), std::cerr);
72             CW << "FindUnsafePointerTypes: Type '" << ITy
73                << "' marked unsafe in '" << F->getName() << "' by:\n" << **I;
74           }
75         }
76     }
77   }
78
79   return false;
80 }
81
82
83 // printResults - Loop over the results of the analysis, printing out unsafe
84 // types.
85 //
86 void FindUnsafePointerTypes::print(std::ostream &o, const Module *M) 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 }