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