IR: Optimize size of use-list order shuffle vectors
[oota-llvm.git] / include / llvm / IR / UseListOrder.h
1 //===- llvm/IR/UseListOrder.h - LLVM Use List Order functions ---*- C++ -*-===//
2 //
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 //
10 // This file has functions to modify the use-list order and to verify that it
11 // doesn't change after serialization.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_IR_USELISTORDER_H
16 #define LLVM_IR_USELISTORDER_H
17
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include <vector>
21
22 namespace llvm {
23
24 class Module;
25 class Function;
26 class Value;
27
28 /// \brief Structure to hold a use-list shuffle vector.
29 ///
30 /// Stores most use-lists locally, but large use-lists use an extra heap entry.
31 /// Costs two fewer pointers than the equivalent \a SmallVector.
32 class UseListShuffleVector {
33   unsigned Size;
34   union {
35     unsigned *Ptr;
36     unsigned Array[6];
37   } Storage;
38
39   bool isSmall() const { return Size <= 6; }
40   unsigned *data() { return isSmall() ? Storage.Array : Storage.Ptr; }
41   const unsigned *data() const {
42     return isSmall() ? Storage.Array : Storage.Ptr;
43   }
44
45 public:
46   UseListShuffleVector() : Size(0) {}
47   UseListShuffleVector(UseListShuffleVector &&X) {
48     std::memcpy(this, &X, sizeof(UseListShuffleVector));
49     X.Size = 0;
50   }
51   explicit UseListShuffleVector(size_t Size) : Size(Size) {
52     if (!isSmall())
53       Storage.Ptr = new unsigned[Size];
54   }
55   ~UseListShuffleVector() {
56     if (!isSmall())
57       delete Storage.Ptr;
58   }
59
60   typedef unsigned *iterator;
61   typedef const unsigned *const_iterator;
62
63   size_t size() const { return Size; }
64   iterator begin() { return data(); }
65   iterator end() { return begin() + size(); }
66   const_iterator begin() const { return data(); }
67   const_iterator end() const { return begin() + size(); }
68   unsigned &operator[](size_t I) { return data()[I]; }
69   unsigned operator[](size_t I) const { return data()[I]; }
70 };
71
72 /// \brief Structure to hold a use-list order.
73 struct UseListOrder {
74   const Value *V;
75   const Function *F;
76   UseListShuffleVector Shuffle;
77
78   UseListOrder(const Value *V, const Function *F, size_t ShuffleSize)
79       : V(V), F(F), Shuffle(ShuffleSize) {}
80 };
81
82 typedef std::vector<UseListOrder> UseListOrderStack;
83
84 /// \brief Whether to preserve use-list ordering.
85 bool shouldPreserveBitcodeUseListOrder();
86 bool shouldPreserveAssemblyUseListOrder();
87
88 /// \brief Shuffle all use-lists in a module.
89 ///
90 /// Adds \c SeedOffset to the default seed for the random number generator.
91 void shuffleUseLists(Module &M, unsigned SeedOffset = 0);
92
93 } // end namespace llvm
94
95 #endif