Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 1 | //==- BlockCounter.h - ADT for counting block visits -------------*- C++ -*-// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2 | // |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 10 | // This file defines BlockCounter, an abstract data type used to count |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 11 | // the number of times a given block has been visited along a path |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 12 | // analyzed by CoreEngine. |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 16 | #include "clang/GR/PathSensitive/BlockCounter.h" |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ImmutableMap.h" |
| 18 | |
| 19 | using namespace clang; |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 20 | using namespace GR; |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 21 | |
Zhongxing Xu | d9e0c0f | 2010-03-23 05:05:02 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | class CountKey { |
| 25 | const StackFrameContext *CallSite; |
| 26 | unsigned BlockID; |
| 27 | |
| 28 | public: |
| 29 | CountKey(const StackFrameContext *CS, unsigned ID) |
| 30 | : CallSite(CS), BlockID(ID) {} |
| 31 | |
| 32 | bool operator==(const CountKey &RHS) const { |
| 33 | return (CallSite == RHS.CallSite) && (BlockID == RHS.BlockID); |
| 34 | } |
| 35 | |
| 36 | bool operator<(const CountKey &RHS) const { |
| 37 | return (CallSite == RHS.CallSite) ? (BlockID < RHS.BlockID) |
| 38 | : (CallSite < RHS.CallSite); |
| 39 | } |
| 40 | |
| 41 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 42 | ID.AddPointer(CallSite); |
| 43 | ID.AddInteger(BlockID); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | } |
| 48 | |
| 49 | typedef llvm::ImmutableMap<CountKey, unsigned> CountMap; |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 50 | |
| 51 | static inline CountMap GetMap(void* D) { |
| 52 | return CountMap(static_cast<CountMap::TreeTy*>(D)); |
| 53 | } |
| 54 | |
| 55 | static inline CountMap::Factory& GetFactory(void* F) { |
| 56 | return *static_cast<CountMap::Factory*>(F); |
| 57 | } |
| 58 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 59 | unsigned BlockCounter::getNumVisited(const StackFrameContext *CallSite, |
Zhongxing Xu | d9e0c0f | 2010-03-23 05:05:02 +0000 | [diff] [blame] | 60 | unsigned BlockID) const { |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 61 | CountMap M = GetMap(Data); |
Zhongxing Xu | d9e0c0f | 2010-03-23 05:05:02 +0000 | [diff] [blame] | 62 | CountMap::data_type* T = M.lookup(CountKey(CallSite, BlockID)); |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 63 | return T ? *T : 0; |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 66 | BlockCounter::Factory::Factory(llvm::BumpPtrAllocator& Alloc) { |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 67 | F = new CountMap::Factory(Alloc); |
| 68 | } |
| 69 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 70 | BlockCounter::Factory::~Factory() { |
Ted Kremenek | 5617fae | 2008-03-06 08:09:22 +0000 | [diff] [blame] | 71 | delete static_cast<CountMap::Factory*>(F); |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 74 | BlockCounter |
| 75 | BlockCounter::Factory::IncrementCount(BlockCounter BC, |
Zhongxing Xu | d9e0c0f | 2010-03-23 05:05:02 +0000 | [diff] [blame] | 76 | const StackFrameContext *CallSite, |
| 77 | unsigned BlockID) { |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 78 | return BlockCounter(GetFactory(F).add(GetMap(BC.Data), |
Zhongxing Xu | d9e0c0f | 2010-03-23 05:05:02 +0000 | [diff] [blame] | 79 | CountKey(CallSite, BlockID), |
| 80 | BC.getNumVisited(CallSite, BlockID)+1).getRoot()); |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 83 | BlockCounter |
| 84 | BlockCounter::Factory::GetEmptyCounter() { |
| 85 | return BlockCounter(GetFactory(F).getEmptyMap().getRoot()); |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 86 | } |