1 //===-- Scalar.cpp --------------------------------------------------------===//
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 implements common infrastructure for libLLVMScalarOpts.a, which
10 // implements several scalar transformations over the LLVM intermediate
11 // representation, including the C bindings for that library.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Transforms/Scalar.h"
16 #include "llvm-c/Initialization.h"
17 #include "llvm-c/Transforms/Scalar.h"
18 #include "llvm/Analysis/BasicAliasAnalysis.h"
19 #include "llvm/Analysis/Passes.h"
20 #include "llvm/Analysis/ScopedNoAliasAA.h"
21 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/LegacyPassManager.h"
24 #include "llvm/IR/Verifier.h"
25 #include "llvm/InitializePasses.h"
26 #include "llvm/Transforms/Scalar/GVN.h"
27 #include "llvm/Transforms/Scalar/Scalarizer.h"
28 #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
29 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
30 
31 using namespace llvm;
32 
33 /// initializeScalarOptsPasses - Initialize all passes linked into the
34 /// ScalarOpts library.
35 void llvm::initializeScalarOpts(PassRegistry &Registry) {
36   initializeADCELegacyPassPass(Registry);
37   initializeAnnotationRemarksLegacyPass(Registry);
38   initializeBDCELegacyPassPass(Registry);
39   initializeAlignmentFromAssumptionsPass(Registry);
40   initializeCallSiteSplittingLegacyPassPass(Registry);
41   initializeConstantHoistingLegacyPassPass(Registry);
42   initializeConstraintEliminationPass(Registry);
43   initializeCorrelatedValuePropagationPass(Registry);
44   initializeDCELegacyPassPass(Registry);
45   initializeDivRemPairsLegacyPassPass(Registry);
46   initializeScalarizerLegacyPassPass(Registry);
47   initializeDSELegacyPassPass(Registry);
48   initializeGuardWideningLegacyPassPass(Registry);
49   initializeLoopGuardWideningLegacyPassPass(Registry);
50   initializeGVNLegacyPassPass(Registry);
51   initializeNewGVNLegacyPassPass(Registry);
52   initializeEarlyCSELegacyPassPass(Registry);
53   initializeEarlyCSEMemSSALegacyPassPass(Registry);
54   initializeMakeGuardsExplicitLegacyPassPass(Registry);
55   initializeGVNHoistLegacyPassPass(Registry);
56   initializeGVNSinkLegacyPassPass(Registry);
57   initializeFlattenCFGLegacyPassPass(Registry);
58   initializeIRCELegacyPassPass(Registry);
59   initializeIndVarSimplifyLegacyPassPass(Registry);
60   initializeInferAddressSpacesPass(Registry);
61   initializeInstSimplifyLegacyPassPass(Registry);
62   initializeJumpThreadingPass(Registry);
63   initializeDFAJumpThreadingLegacyPassPass(Registry);
64   initializeLegacyLICMPassPass(Registry);
65   initializeLegacyLoopSinkPassPass(Registry);
66   initializeLoopFuseLegacyPass(Registry);
67   initializeLoopDataPrefetchLegacyPassPass(Registry);
68   initializeLoopDeletionLegacyPassPass(Registry);
69   initializeLoopAccessLegacyAnalysisPass(Registry);
70   initializeLoopInstSimplifyLegacyPassPass(Registry);
71   initializeLoopInterchangeLegacyPassPass(Registry);
72   initializeLoopFlattenLegacyPassPass(Registry);
73   initializeLoopPredicationLegacyPassPass(Registry);
74   initializeLoopRotateLegacyPassPass(Registry);
75   initializeLoopStrengthReducePass(Registry);
76   initializeLoopRerollLegacyPassPass(Registry);
77   initializeLoopUnrollPass(Registry);
78   initializeLoopUnrollAndJamPass(Registry);
79   initializeLoopUnswitchPass(Registry);
80   initializeWarnMissedTransformationsLegacyPass(Registry);
81   initializeLoopVersioningLICMLegacyPassPass(Registry);
82   initializeLoopIdiomRecognizeLegacyPassPass(Registry);
83   initializeLowerAtomicLegacyPassPass(Registry);
84   initializeLowerConstantIntrinsicsPass(Registry);
85   initializeLowerExpectIntrinsicPass(Registry);
86   initializeLowerGuardIntrinsicLegacyPassPass(Registry);
87   initializeLowerMatrixIntrinsicsLegacyPassPass(Registry);
88   initializeLowerMatrixIntrinsicsMinimalLegacyPassPass(Registry);
89   initializeLowerWidenableConditionLegacyPassPass(Registry);
90   initializeMemCpyOptLegacyPassPass(Registry);
91   initializeMergeICmpsLegacyPassPass(Registry);
92   initializeMergedLoadStoreMotionLegacyPassPass(Registry);
93   initializeNaryReassociateLegacyPassPass(Registry);
94   initializePartiallyInlineLibCallsLegacyPassPass(Registry);
95   initializeReassociateLegacyPassPass(Registry);
96   initializeRedundantDbgInstEliminationPass(Registry);
97   initializeRegToMemLegacyPass(Registry);
98   initializeRewriteStatepointsForGCLegacyPassPass(Registry);
99   initializeScalarizeMaskedMemIntrinLegacyPassPass(Registry);
100   initializeSCCPLegacyPassPass(Registry);
101   initializeSROALegacyPassPass(Registry);
102   initializeCFGSimplifyPassPass(Registry);
103   initializeStructurizeCFGLegacyPassPass(Registry);
104   initializeSimpleLoopUnswitchLegacyPassPass(Registry);
105   initializeSinkingLegacyPassPass(Registry);
106   initializeTailCallElimPass(Registry);
107   initializeSeparateConstOffsetFromGEPLegacyPassPass(Registry);
108   initializeSpeculativeExecutionLegacyPassPass(Registry);
109   initializeStraightLineStrengthReduceLegacyPassPass(Registry);
110   initializePlaceBackedgeSafepointsImplPass(Registry);
111   initializePlaceSafepointsPass(Registry);
112   initializeFloat2IntLegacyPassPass(Registry);
113   initializeLoopDistributeLegacyPass(Registry);
114   initializeLoopLoadEliminationPass(Registry);
115   initializeLoopSimplifyCFGLegacyPassPass(Registry);
116   initializeLoopVersioningLegacyPassPass(Registry);
117   initializeEntryExitInstrumenterPass(Registry);
118   initializePostInlineEntryExitInstrumenterPass(Registry);
119 }
120 
121 void LLVMAddLoopSimplifyCFGPass(LLVMPassManagerRef PM) {
122   unwrap(PM)->add(createLoopSimplifyCFGPass());
123 }
124 
125 void LLVMInitializeScalarOpts(LLVMPassRegistryRef R) {
126   initializeScalarOpts(*unwrap(R));
127 }
128 
129 void LLVMAddAggressiveDCEPass(LLVMPassManagerRef PM) {
130   unwrap(PM)->add(createAggressiveDCEPass());
131 }
132 
133 void LLVMAddDCEPass(LLVMPassManagerRef PM) {
134   unwrap(PM)->add(createDeadCodeEliminationPass());
135 }
136 
137 void LLVMAddBitTrackingDCEPass(LLVMPassManagerRef PM) {
138   unwrap(PM)->add(createBitTrackingDCEPass());
139 }
140 
141 void LLVMAddAlignmentFromAssumptionsPass(LLVMPassManagerRef PM) {
142   unwrap(PM)->add(createAlignmentFromAssumptionsPass());
143 }
144 
145 void LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM) {
146   unwrap(PM)->add(createCFGSimplificationPass());
147 }
148 
149 void LLVMAddDeadStoreEliminationPass(LLVMPassManagerRef PM) {
150   unwrap(PM)->add(createDeadStoreEliminationPass());
151 }
152 
153 void LLVMAddScalarizerPass(LLVMPassManagerRef PM) {
154   unwrap(PM)->add(createScalarizerPass());
155 }
156 
157 void LLVMAddGVNPass(LLVMPassManagerRef PM) {
158   unwrap(PM)->add(createGVNPass());
159 }
160 
161 void LLVMAddNewGVNPass(LLVMPassManagerRef PM) {
162   unwrap(PM)->add(createNewGVNPass());
163 }
164 
165 void LLVMAddMergedLoadStoreMotionPass(LLVMPassManagerRef PM) {
166   unwrap(PM)->add(createMergedLoadStoreMotionPass());
167 }
168 
169 void LLVMAddIndVarSimplifyPass(LLVMPassManagerRef PM) {
170   unwrap(PM)->add(createIndVarSimplifyPass());
171 }
172 
173 void LLVMAddInstructionSimplifyPass(LLVMPassManagerRef PM) {
174   unwrap(PM)->add(createInstSimplifyLegacyPass());
175 }
176 
177 void LLVMAddJumpThreadingPass(LLVMPassManagerRef PM) {
178   unwrap(PM)->add(createJumpThreadingPass());
179 }
180 
181 void LLVMAddLoopSinkPass(LLVMPassManagerRef PM) {
182   unwrap(PM)->add(createLoopSinkPass());
183 }
184 
185 void LLVMAddLICMPass(LLVMPassManagerRef PM) {
186   unwrap(PM)->add(createLICMPass());
187 }
188 
189 void LLVMAddLoopDeletionPass(LLVMPassManagerRef PM) {
190   unwrap(PM)->add(createLoopDeletionPass());
191 }
192 
193 void LLVMAddLoopFlattenPass(LLVMPassManagerRef PM) {
194   unwrap(PM)->add(createLoopFlattenPass());
195 }
196 
197 void LLVMAddLoopIdiomPass(LLVMPassManagerRef PM) {
198   unwrap(PM)->add(createLoopIdiomPass());
199 }
200 
201 void LLVMAddLoopRotatePass(LLVMPassManagerRef PM) {
202   unwrap(PM)->add(createLoopRotatePass());
203 }
204 
205 void LLVMAddLoopRerollPass(LLVMPassManagerRef PM) {
206   unwrap(PM)->add(createLoopRerollPass());
207 }
208 
209 void LLVMAddLoopUnrollPass(LLVMPassManagerRef PM) {
210   unwrap(PM)->add(createLoopUnrollPass());
211 }
212 
213 void LLVMAddLoopUnrollAndJamPass(LLVMPassManagerRef PM) {
214   unwrap(PM)->add(createLoopUnrollAndJamPass());
215 }
216 
217 void LLVMAddLoopUnswitchPass(LLVMPassManagerRef PM) {
218   unwrap(PM)->add(createLoopUnswitchPass());
219 }
220 
221 void LLVMAddLowerAtomicPass(LLVMPassManagerRef PM) {
222   unwrap(PM)->add(createLowerAtomicPass());
223 }
224 
225 void LLVMAddMemCpyOptPass(LLVMPassManagerRef PM) {
226   unwrap(PM)->add(createMemCpyOptPass());
227 }
228 
229 void LLVMAddPartiallyInlineLibCallsPass(LLVMPassManagerRef PM) {
230   unwrap(PM)->add(createPartiallyInlineLibCallsPass());
231 }
232 
233 void LLVMAddReassociatePass(LLVMPassManagerRef PM) {
234   unwrap(PM)->add(createReassociatePass());
235 }
236 
237 void LLVMAddSCCPPass(LLVMPassManagerRef PM) {
238   unwrap(PM)->add(createSCCPPass());
239 }
240 
241 void LLVMAddScalarReplAggregatesPass(LLVMPassManagerRef PM) {
242   unwrap(PM)->add(createSROAPass());
243 }
244 
245 void LLVMAddScalarReplAggregatesPassSSA(LLVMPassManagerRef PM) {
246   unwrap(PM)->add(createSROAPass());
247 }
248 
249 void LLVMAddScalarReplAggregatesPassWithThreshold(LLVMPassManagerRef PM,
250                                                   int Threshold) {
251   unwrap(PM)->add(createSROAPass());
252 }
253 
254 void LLVMAddSimplifyLibCallsPass(LLVMPassManagerRef PM) {
255   // NOTE: The simplify-libcalls pass has been removed.
256 }
257 
258 void LLVMAddTailCallEliminationPass(LLVMPassManagerRef PM) {
259   unwrap(PM)->add(createTailCallEliminationPass());
260 }
261 
262 void LLVMAddDemoteMemoryToRegisterPass(LLVMPassManagerRef PM) {
263   unwrap(PM)->add(createDemoteRegisterToMemoryPass());
264 }
265 
266 void LLVMAddVerifierPass(LLVMPassManagerRef PM) {
267   unwrap(PM)->add(createVerifierPass());
268 }
269 
270 void LLVMAddCorrelatedValuePropagationPass(LLVMPassManagerRef PM) {
271   unwrap(PM)->add(createCorrelatedValuePropagationPass());
272 }
273 
274 void LLVMAddEarlyCSEPass(LLVMPassManagerRef PM) {
275   unwrap(PM)->add(createEarlyCSEPass(false/*=UseMemorySSA*/));
276 }
277 
278 void LLVMAddEarlyCSEMemSSAPass(LLVMPassManagerRef PM) {
279   unwrap(PM)->add(createEarlyCSEPass(true/*=UseMemorySSA*/));
280 }
281 
282 void LLVMAddGVNHoistLegacyPass(LLVMPassManagerRef PM) {
283   unwrap(PM)->add(createGVNHoistPass());
284 }
285 
286 void LLVMAddTypeBasedAliasAnalysisPass(LLVMPassManagerRef PM) {
287   unwrap(PM)->add(createTypeBasedAAWrapperPass());
288 }
289 
290 void LLVMAddScopedNoAliasAAPass(LLVMPassManagerRef PM) {
291   unwrap(PM)->add(createScopedNoAliasAAWrapperPass());
292 }
293 
294 void LLVMAddBasicAliasAnalysisPass(LLVMPassManagerRef PM) {
295   unwrap(PM)->add(createBasicAAWrapperPass());
296 }
297 
298 void LLVMAddLowerConstantIntrinsicsPass(LLVMPassManagerRef PM) {
299   unwrap(PM)->add(createLowerConstantIntrinsicsPass());
300 }
301 
302 void LLVMAddLowerExpectIntrinsicPass(LLVMPassManagerRef PM) {
303   unwrap(PM)->add(createLowerExpectIntrinsicPass());
304 }
305 
306 void LLVMAddUnifyFunctionExitNodesPass(LLVMPassManagerRef PM) {
307   unwrap(PM)->add(createUnifyFunctionExitNodesPass());
308 }
309