LLVM  14.0.0git
Recycler.h
Go to the documentation of this file.
1 //==- llvm/Support/Recycler.h - Recycling Allocator --------------*- C++ -*-==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the Recycler class template. See the doxygen comment for
10 // Recycler for more details.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_SUPPORT_RECYCLER_H
15 #define LLVM_SUPPORT_RECYCLER_H
16 
17 #include "llvm/ADT/ilist.h"
18 #include "llvm/Support/Allocator.h"
20 #include <cassert>
21 
22 namespace llvm {
23 
24 /// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for
25 /// printing statistics.
26 ///
27 void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize);
28 
29 /// Recycler - This class manages a linked-list of deallocated nodes
30 /// and facilitates reusing deallocated memory in place of allocating
31 /// new memory.
32 ///
33 template <class T, size_t Size = sizeof(T), size_t Align = alignof(T)>
34 class Recycler {
35  struct FreeNode {
36  FreeNode *Next;
37  };
38 
39  /// List of nodes that have deleted contents and are not in active use.
40  FreeNode *FreeList = nullptr;
41 
42  FreeNode *pop_val() {
43  auto *Val = FreeList;
45  FreeList = FreeList->Next;
47  return Val;
48  }
49 
50  void push(FreeNode *N) {
51  N->Next = FreeList;
52  FreeList = N;
54  }
55 
56 public:
58  // If this fails, either the callee has lost track of some allocation,
59  // or the callee isn't tracking allocations and should just call
60  // clear() before deleting the Recycler.
61  assert(!FreeList && "Non-empty recycler deleted!");
62  }
63 
64  /// clear - Release all the tracked allocations to the allocator. The
65  /// recycler must be free of any tracked allocations before being
66  /// deleted; calling clear is one way to ensure this.
67  template<class AllocatorType>
68  void clear(AllocatorType &Allocator) {
69  while (FreeList) {
70  T *t = reinterpret_cast<T *>(pop_val());
71  Allocator.Deallocate(t);
72  }
73  }
74 
75  /// Special case for BumpPtrAllocator which has an empty Deallocate()
76  /// function.
77  ///
78  /// There is no need to traverse the free list, pulling all the objects into
79  /// cache.
80  void clear(BumpPtrAllocator &) { FreeList = nullptr; }
81 
82  template<class SubClass, class AllocatorType>
83  SubClass *Allocate(AllocatorType &Allocator) {
84  static_assert(alignof(SubClass) <= Align,
85  "Recycler allocation alignment is less than object align!");
86  static_assert(sizeof(SubClass) <= Size,
87  "Recycler allocation size is less than object size!");
88  return FreeList ? reinterpret_cast<SubClass *>(pop_val())
89  : static_cast<SubClass *>(Allocator.Allocate(Size, Align));
90  }
91 
92  template<class AllocatorType>
93  T *Allocate(AllocatorType &Allocator) {
94  return Allocate<T>(Allocator);
95  }
96 
97  template<class SubClass, class AllocatorType>
98  void Deallocate(AllocatorType & /*Allocator*/, SubClass* Element) {
99  push(reinterpret_cast<FreeNode *>(Element));
100  }
101 
102  void PrintStats();
103 };
104 
105 template <class T, size_t Size, size_t Align>
107  size_t S = 0;
108  for (auto *I = FreeList; I; I = I->Next)
109  ++S;
111 }
112 
113 }
114 
115 #endif
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
__asan_poison_memory_region
#define __asan_poison_memory_region(p, size)
Definition: Compiler.h:444
llvm::Recycler::Allocate
T * Allocate(AllocatorType &Allocator)
Definition: Recycler.h:93
ilist.h
ErrorHandling.h
Allocator.h
T
#define T
Definition: Mips16ISelLowering.cpp:341
__asan_unpoison_memory_region
#define __asan_unpoison_memory_region(p, size)
Definition: Compiler.h:445
llvm::Recycler::Deallocate
void Deallocate(AllocatorType &, SubClass *Element)
Definition: Recycler.h:98
llvm::Recycler::clear
void clear(AllocatorType &Allocator)
clear - Release all the tracked allocations to the allocator.
Definition: Recycler.h:68
t
bitcast float %x to i32 %s=and i32 %t, 2147483647 %d=bitcast i32 %s to float ret float %d } declare float @fabsf(float %n) define float @bar(float %x) nounwind { %d=call float @fabsf(float %x) ret float %d } This IR(from PR6194):target datalayout="e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128" target triple="x86_64-apple-darwin10.0.0" %0=type { double, double } %struct.float3=type { float, float, float } define void @test(%0, %struct.float3 *nocapture %res) nounwind noinline ssp { entry:%tmp18=extractvalue %0 %0, 0 t
Definition: README-SSE.txt:788
Align
uint64_t Align
Definition: ELFObjHandler.cpp:83
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::Recycler::PrintStats
void PrintStats()
Definition: Recycler.h:106
llvm::BumpPtrAllocatorImpl
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::Recycler::clear
void clear(BumpPtrAllocator &)
Special case for BumpPtrAllocator which has an empty Deallocate() function.
Definition: Recycler.h:80
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::Recycler
Recycler - This class manages a linked-list of deallocated nodes and facilitates reusing deallocated ...
Definition: Recycler.h:34
llvm::Recycler::~Recycler
~Recycler()
Definition: Recycler.h:57
llvm::PrintRecyclerStats
void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize)
PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for printing statistics.
Definition: Allocator.cpp:31
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::Recycler::Allocate
SubClass * Allocate(AllocatorType &Allocator)
Definition: Recycler.h:83
__msan_allocated_memory
#define __msan_allocated_memory(p, size)
Definition: Compiler.h:432
Allocator
Basic Register Allocator
Definition: RegAllocBasic.cpp:146
N
#define N