1 //===- SROA.h - Scalar Replacement Of Aggregates ----------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 /// This file provides the interface for LLVM's Scalar Replacement of
10 /// Aggregates pass. This pass provides both aggregate splitting and the
11 /// primary SSA formation used in the compiler.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TRANSFORMS_SCALAR_SROA_H
16 #define LLVM_TRANSFORMS_SCALAR_SROA_H
18 #include "llvm/ADT/SetVector.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/IR/PassManager.h"
21 #include "llvm/Support/Compiler.h"
27 class AssumptionCache
;
36 /// A private "module" namespace for types and utilities used by SROA. These
37 /// are implementation details and should not be used by clients.
38 namespace sroa LLVM_LIBRARY_VISIBILITY
{
40 class AllocaSliceRewriter
;
45 } // end namespace sroa
47 /// An optimization pass providing Scalar Replacement of Aggregates.
49 /// This pass takes allocations which can be completely analyzed (that is, they
50 /// don't escape) and tries to turn them into scalar SSA values. There are
51 /// a few steps to this process.
53 /// 1) It takes allocations of aggregates and analyzes the ways in which they
54 /// are used to try to split them into smaller allocations, ideally of
55 /// a single scalar data type. It will split up memcpy and memset accesses
56 /// as necessary and try to isolate individual scalar accesses.
57 /// 2) It will transform accesses into forms which are suitable for SSA value
58 /// promotion. This can be replacing a memset with a scalar store of an
59 /// integer value, or it can involve speculating operations on a PHI or
60 /// select to be a PHI or select of the results.
61 /// 3) Finally, this will try to detect a pattern of accesses which map cleanly
62 /// onto insert and extract operations on a vector value, and convert them to
63 /// this form. By doing so, it will enable promotion of vector aggregates to
64 /// SSA vector values.
65 class SROA
: public PassInfoMixin
<SROA
> {
66 LLVMContext
*C
= nullptr;
67 DominatorTree
*DT
= nullptr;
68 AssumptionCache
*AC
= nullptr;
70 /// Worklist of alloca instructions to simplify.
72 /// Each alloca in the function is added to this. Each new alloca formed gets
73 /// added to it as well to recursively simplify unless that alloca can be
74 /// directly promoted. Finally, each time we rewrite a use of an alloca other
75 /// the one being actively rewritten, we add it back onto the list if not
76 /// already present to ensure it is re-visited.
77 SetVector
<AllocaInst
*, SmallVector
<AllocaInst
*, 16>> Worklist
;
79 /// A collection of instructions to delete.
80 /// We try to batch deletions to simplify code and make things a bit more
82 SetVector
<Instruction
*, SmallVector
<Instruction
*, 8>> DeadInsts
;
84 /// Post-promotion worklist.
86 /// Sometimes we discover an alloca which has a high probability of becoming
87 /// viable for SROA after a round of promotion takes place. In those cases,
88 /// the alloca is enqueued here for re-processing.
90 /// Note that we have to be very careful to clear allocas out of this list in
91 /// the event they are deleted.
92 SetVector
<AllocaInst
*, SmallVector
<AllocaInst
*, 16>> PostPromotionWorklist
;
94 /// A collection of alloca instructions we can directly promote.
95 std::vector
<AllocaInst
*> PromotableAllocas
;
97 /// A worklist of PHIs to speculate prior to promoting allocas.
99 /// All of these PHIs have been checked for the safety of speculation and by
100 /// being speculated will allow promoting allocas currently in the promotable
102 SetVector
<PHINode
*, SmallVector
<PHINode
*, 2>> SpeculatablePHIs
;
104 /// A worklist of select instructions to speculate prior to promoting
107 /// All of these select instructions have been checked for the safety of
108 /// speculation and by being speculated will allow promoting allocas
109 /// currently in the promotable queue.
110 SetVector
<SelectInst
*, SmallVector
<SelectInst
*, 2>> SpeculatableSelects
;
115 /// Run the pass over the function.
116 PreservedAnalyses
run(Function
&F
, FunctionAnalysisManager
&AM
);
119 friend class sroa::AllocaSliceRewriter
;
120 friend class sroa::SROALegacyPass
;
122 /// Helper used by both the public run method and by the legacy pass.
123 PreservedAnalyses
runImpl(Function
&F
, DominatorTree
&RunDT
,
124 AssumptionCache
&RunAC
);
126 bool presplitLoadsAndStores(AllocaInst
&AI
, sroa::AllocaSlices
&AS
);
127 AllocaInst
*rewritePartition(AllocaInst
&AI
, sroa::AllocaSlices
&AS
,
129 bool splitAlloca(AllocaInst
&AI
, sroa::AllocaSlices
&AS
);
130 bool runOnAlloca(AllocaInst
&AI
);
131 void clobberUse(Use
&U
);
132 bool deleteDeadInstructions(SmallPtrSetImpl
<AllocaInst
*> &DeletedAllocas
);
133 bool promoteAllocas(Function
&F
);
136 } // end namespace llvm
138 #endif // LLVM_TRANSFORMS_SCALAR_SROA_H