1 //===- llvm/IR/UseListOrder.h - LLVM Use List Order functions ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file has functions to modify the use-list order and to verify that it
11 // doesn't change after serialization.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_USELISTORDER_H
16 #define LLVM_IR_USELISTORDER_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/SmallVector.h"
28 /// \brief Structure to hold a use-list shuffle vector.
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 {
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;
46 UseListShuffleVector() : Size(0) {}
47 UseListShuffleVector(UseListShuffleVector &&X) {
48 std::memcpy(this, &X, sizeof(UseListShuffleVector));
51 UseListShuffleVector(const UseListShuffleVector &X) {
52 std::memcpy(this, &X, sizeof(UseListShuffleVector));
54 Storage.Ptr = new unsigned[Size];
55 std::memcpy(Storage.Ptr, X.Storage.Ptr, Size * sizeof(*Storage.Ptr));
58 explicit UseListShuffleVector(size_t Size) : Size(Size) {
60 Storage.Ptr = new unsigned[Size];
62 ~UseListShuffleVector() {
67 typedef unsigned *iterator;
68 typedef const unsigned *const_iterator;
70 size_t size() const { return Size; }
71 iterator begin() { return data(); }
72 iterator end() { return begin() + size(); }
73 const_iterator begin() const { return data(); }
74 const_iterator end() const { return begin() + size(); }
75 unsigned &operator[](size_t I) { return data()[I]; }
76 unsigned operator[](size_t I) const { return data()[I]; }
79 /// \brief Structure to hold a use-list order.
83 UseListShuffleVector Shuffle;
85 UseListOrder(const Value *V, const Function *F, size_t ShuffleSize)
86 : V(V), F(F), Shuffle(ShuffleSize) {}
89 typedef std::vector<UseListOrder> UseListOrderStack;
91 /// \brief Whether to preserve use-list ordering.
92 bool shouldPreserveBitcodeUseListOrder();
93 bool shouldPreserveAssemblyUseListOrder();
95 /// \brief Shuffle all use-lists in a module.
97 /// Adds \c SeedOffset to the default seed for the random number generator.
98 void shuffleUseLists(Module &M, unsigned SeedOffset = 0);
100 } // end namespace llvm