LLVM  8.0.1
SROA.h
Go to the documentation of this file.
1 //===- SROA.h - Scalar Replacement Of Aggregates ----------------*- 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 /// \file
10 /// This file provides the interface for LLVM's Scalar Replacement of
11 /// Aggregates pass. This pass provides both aggregate splitting and the
12 /// primary SSA formation used in the compiler.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_TRANSFORMS_SCALAR_SROA_H
17 #define LLVM_TRANSFORMS_SCALAR_SROA_H
18 
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/IR/PassManager.h"
22 #include "llvm/Support/Compiler.h"
23 #include <vector>
24 
25 namespace llvm {
26 
27 class AllocaInst;
28 class AssumptionCache;
29 class DominatorTree;
30 class Function;
31 class Instruction;
32 class LLVMContext;
33 class PHINode;
34 class SelectInst;
35 class Use;
36 
37 /// A private "module" namespace for types and utilities used by SROA. These
38 /// are implementation details and should not be used by clients.
39 namespace sroa LLVM_LIBRARY_VISIBILITY {
40 
41 class AllocaSliceRewriter;
42 class AllocaSlices;
43 class Partition;
44 class SROALegacyPass;
45 
46 } // end namespace sroa
47 
48 /// An optimization pass providing Scalar Replacement of Aggregates.
49 ///
50 /// This pass takes allocations which can be completely analyzed (that is, they
51 /// don't escape) and tries to turn them into scalar SSA values. There are
52 /// a few steps to this process.
53 ///
54 /// 1) It takes allocations of aggregates and analyzes the ways in which they
55 /// are used to try to split them into smaller allocations, ideally of
56 /// a single scalar data type. It will split up memcpy and memset accesses
57 /// as necessary and try to isolate individual scalar accesses.
58 /// 2) It will transform accesses into forms which are suitable for SSA value
59 /// promotion. This can be replacing a memset with a scalar store of an
60 /// integer value, or it can involve speculating operations on a PHI or
61 /// select to be a PHI or select of the results.
62 /// 3) Finally, this will try to detect a pattern of accesses which map cleanly
63 /// onto insert and extract operations on a vector value, and convert them to
64 /// this form. By doing so, it will enable promotion of vector aggregates to
65 /// SSA vector values.
66 class SROA : public PassInfoMixin<SROA> {
67  LLVMContext *C = nullptr;
68  DominatorTree *DT = nullptr;
69  AssumptionCache *AC = nullptr;
70 
71  /// Worklist of alloca instructions to simplify.
72  ///
73  /// Each alloca in the function is added to this. Each new alloca formed gets
74  /// added to it as well to recursively simplify unless that alloca can be
75  /// directly promoted. Finally, each time we rewrite a use of an alloca other
76  /// the one being actively rewritten, we add it back onto the list if not
77  /// already present to ensure it is re-visited.
79 
80  /// A collection of instructions to delete.
81  /// We try to batch deletions to simplify code and make things a bit more
82  /// efficient.
84 
85  /// Post-promotion worklist.
86  ///
87  /// Sometimes we discover an alloca which has a high probability of becoming
88  /// viable for SROA after a round of promotion takes place. In those cases,
89  /// the alloca is enqueued here for re-processing.
90  ///
91  /// Note that we have to be very careful to clear allocas out of this list in
92  /// the event they are deleted.
94 
95  /// A collection of alloca instructions we can directly promote.
96  std::vector<AllocaInst *> PromotableAllocas;
97 
98  /// A worklist of PHIs to speculate prior to promoting allocas.
99  ///
100  /// All of these PHIs have been checked for the safety of speculation and by
101  /// being speculated will allow promoting allocas currently in the promotable
102  /// queue.
104 
105  /// A worklist of select instructions to speculate prior to promoting
106  /// allocas.
107  ///
108  /// All of these select instructions have been checked for the safety of
109  /// speculation and by being speculated will allow promoting allocas
110  /// currently in the promotable queue.
112 
113 public:
114  SROA() = default;
115 
116  /// Run the pass over the function.
118 
119 private:
121  friend class sroa::SROALegacyPass;
122 
123  /// Helper used by both the public run method and by the legacy pass.
125  AssumptionCache &RunAC);
126 
127  bool presplitLoadsAndStores(AllocaInst &AI, sroa::AllocaSlices &AS);
128  AllocaInst *rewritePartition(AllocaInst &AI, sroa::AllocaSlices &AS,
129  sroa::Partition &P);
130  bool splitAlloca(AllocaInst &AI, sroa::AllocaSlices &AS);
131  bool runOnAlloca(AllocaInst &AI);
132  void clobberUse(Use &U);
133  bool deleteDeadInstructions(SmallPtrSetImpl<AllocaInst *> &DeletedAllocas);
134  bool promoteAllocas(Function &F);
135 };
136 
137 } // end namespace llvm
138 
139 #endif // LLVM_TRANSFORMS_SCALAR_SROA_H
uint64_t CallInst * C
static bool runImpl(Function &F, TargetLibraryInfo &TLI, DominatorTree &DT)
This is the entry point for all transforms.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Representation of the alloca slices.
Definition: SROA.cpp:239
A cache of @llvm.assume calls within a function.
F(f)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:344
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
A partition of the slices.
Definition: SROA.cpp:363
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:366
A legacy pass for the legacy pass manager that wraps the SROA pass.
Definition: SROA.cpp:4552
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:145
#define P(N)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
sroa
Definition: SROA.cpp:4591
Visitor to rewrite instructions using p particular slice of an alloca to use a new alloca...
Definition: SROA.cpp:2220
#define LLVM_LIBRARY_VISIBILITY
LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked into a shared library...
Definition: Compiler.h:108
A vector that has set insertion semantics.
Definition: SetVector.h:41
An optimization pass providing Scalar Replacement of Aggregates.
Definition: SROA.h:66
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
an instruction to allocate memory on the stack
Definition: Instructions.h:60