• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
initializeScalarOpts(PassRegistry & Registry)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   initializeFlattenCFGPassPass(Registry);
58   initializeIRCELegacyPassPass(Registry);
59   initializeIndVarSimplifyLegacyPassPass(Registry);
60   initializeInferAddressSpacesPass(Registry);
61   initializeInstSimplifyLegacyPassPass(Registry);
62   initializeJumpThreadingPass(Registry);
63   initializeLegacyLICMPassPass(Registry);
64   initializeLegacyLoopSinkPassPass(Registry);
65   initializeLoopFuseLegacyPass(Registry);
66   initializeLoopDataPrefetchLegacyPassPass(Registry);
67   initializeLoopDeletionLegacyPassPass(Registry);
68   initializeLoopAccessLegacyAnalysisPass(Registry);
69   initializeLoopInstSimplifyLegacyPassPass(Registry);
70   initializeLoopInterchangeLegacyPassPass(Registry);
71   initializeLoopFlattenLegacyPassPass(Registry);
72   initializeLoopPredicationLegacyPassPass(Registry);
73   initializeLoopRotateLegacyPassPass(Registry);
74   initializeLoopStrengthReducePass(Registry);
75   initializeLoopRerollLegacyPassPass(Registry);
76   initializeLoopUnrollPass(Registry);
77   initializeLoopUnrollAndJamPass(Registry);
78   initializeLoopUnswitchPass(Registry);
79   initializeWarnMissedTransformationsLegacyPass(Registry);
80   initializeLoopVersioningLICMLegacyPassPass(Registry);
81   initializeLoopIdiomRecognizeLegacyPassPass(Registry);
82   initializeLowerAtomicLegacyPassPass(Registry);
83   initializeLowerConstantIntrinsicsPass(Registry);
84   initializeLowerExpectIntrinsicPass(Registry);
85   initializeLowerGuardIntrinsicLegacyPassPass(Registry);
86   initializeLowerMatrixIntrinsicsLegacyPassPass(Registry);
87   initializeLowerMatrixIntrinsicsMinimalLegacyPassPass(Registry);
88   initializeLowerWidenableConditionLegacyPassPass(Registry);
89   initializeMemCpyOptLegacyPassPass(Registry);
90   initializeMergeICmpsLegacyPassPass(Registry);
91   initializeMergedLoadStoreMotionLegacyPassPass(Registry);
92   initializeNaryReassociateLegacyPassPass(Registry);
93   initializePartiallyInlineLibCallsLegacyPassPass(Registry);
94   initializeReassociateLegacyPassPass(Registry);
95   initializeRedundantDbgInstEliminationPass(Registry);
96   initializeRegToMemLegacyPass(Registry);
97   initializeRewriteStatepointsForGCLegacyPassPass(Registry);
98   initializeScalarizeMaskedMemIntrinLegacyPassPass(Registry);
99   initializeSCCPLegacyPassPass(Registry);
100   initializeSROALegacyPassPass(Registry);
101   initializeCFGSimplifyPassPass(Registry);
102   initializeStructurizeCFGLegacyPassPass(Registry);
103   initializeSimpleLoopUnswitchLegacyPassPass(Registry);
104   initializeSinkingLegacyPassPass(Registry);
105   initializeTailCallElimPass(Registry);
106   initializeSeparateConstOffsetFromGEPLegacyPassPass(Registry);
107   initializeSpeculativeExecutionLegacyPassPass(Registry);
108   initializeStraightLineStrengthReduceLegacyPassPass(Registry);
109   initializePlaceBackedgeSafepointsImplPass(Registry);
110   initializePlaceSafepointsPass(Registry);
111   initializeFloat2IntLegacyPassPass(Registry);
112   initializeLoopDistributeLegacyPass(Registry);
113   initializeLoopLoadEliminationPass(Registry);
114   initializeLoopSimplifyCFGLegacyPassPass(Registry);
115   initializeLoopVersioningLegacyPassPass(Registry);
116   initializeEntryExitInstrumenterPass(Registry);
117   initializePostInlineEntryExitInstrumenterPass(Registry);
118 }
119 
LLVMAddLoopSimplifyCFGPass(LLVMPassManagerRef PM)120 void LLVMAddLoopSimplifyCFGPass(LLVMPassManagerRef PM) {
121   unwrap(PM)->add(createLoopSimplifyCFGPass());
122 }
123 
LLVMInitializeScalarOpts(LLVMPassRegistryRef R)124 void LLVMInitializeScalarOpts(LLVMPassRegistryRef R) {
125   initializeScalarOpts(*unwrap(R));
126 }
127 
LLVMAddAggressiveDCEPass(LLVMPassManagerRef PM)128 void LLVMAddAggressiveDCEPass(LLVMPassManagerRef PM) {
129   unwrap(PM)->add(createAggressiveDCEPass());
130 }
131 
LLVMAddDCEPass(LLVMPassManagerRef PM)132 void LLVMAddDCEPass(LLVMPassManagerRef PM) {
133   unwrap(PM)->add(createDeadCodeEliminationPass());
134 }
135 
LLVMAddBitTrackingDCEPass(LLVMPassManagerRef PM)136 void LLVMAddBitTrackingDCEPass(LLVMPassManagerRef PM) {
137   unwrap(PM)->add(createBitTrackingDCEPass());
138 }
139 
LLVMAddAlignmentFromAssumptionsPass(LLVMPassManagerRef PM)140 void LLVMAddAlignmentFromAssumptionsPass(LLVMPassManagerRef PM) {
141   unwrap(PM)->add(createAlignmentFromAssumptionsPass());
142 }
143 
LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM)144 void LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM) {
145   unwrap(PM)->add(createCFGSimplificationPass());
146 }
147 
LLVMAddDeadStoreEliminationPass(LLVMPassManagerRef PM)148 void LLVMAddDeadStoreEliminationPass(LLVMPassManagerRef PM) {
149   unwrap(PM)->add(createDeadStoreEliminationPass());
150 }
151 
LLVMAddScalarizerPass(LLVMPassManagerRef PM)152 void LLVMAddScalarizerPass(LLVMPassManagerRef PM) {
153   unwrap(PM)->add(createScalarizerPass());
154 }
155 
LLVMAddGVNPass(LLVMPassManagerRef PM)156 void LLVMAddGVNPass(LLVMPassManagerRef PM) {
157   unwrap(PM)->add(createGVNPass());
158 }
159 
LLVMAddNewGVNPass(LLVMPassManagerRef PM)160 void LLVMAddNewGVNPass(LLVMPassManagerRef PM) {
161   unwrap(PM)->add(createNewGVNPass());
162 }
163 
LLVMAddMergedLoadStoreMotionPass(LLVMPassManagerRef PM)164 void LLVMAddMergedLoadStoreMotionPass(LLVMPassManagerRef PM) {
165   unwrap(PM)->add(createMergedLoadStoreMotionPass());
166 }
167 
LLVMAddIndVarSimplifyPass(LLVMPassManagerRef PM)168 void LLVMAddIndVarSimplifyPass(LLVMPassManagerRef PM) {
169   unwrap(PM)->add(createIndVarSimplifyPass());
170 }
171 
LLVMAddInstructionSimplifyPass(LLVMPassManagerRef PM)172 void LLVMAddInstructionSimplifyPass(LLVMPassManagerRef PM) {
173   unwrap(PM)->add(createInstSimplifyLegacyPass());
174 }
175 
LLVMAddJumpThreadingPass(LLVMPassManagerRef PM)176 void LLVMAddJumpThreadingPass(LLVMPassManagerRef PM) {
177   unwrap(PM)->add(createJumpThreadingPass());
178 }
179 
LLVMAddLoopSinkPass(LLVMPassManagerRef PM)180 void LLVMAddLoopSinkPass(LLVMPassManagerRef PM) {
181   unwrap(PM)->add(createLoopSinkPass());
182 }
183 
LLVMAddLICMPass(LLVMPassManagerRef PM)184 void LLVMAddLICMPass(LLVMPassManagerRef PM) {
185   unwrap(PM)->add(createLICMPass());
186 }
187 
LLVMAddLoopDeletionPass(LLVMPassManagerRef PM)188 void LLVMAddLoopDeletionPass(LLVMPassManagerRef PM) {
189   unwrap(PM)->add(createLoopDeletionPass());
190 }
191 
LLVMAddLoopFlattenPass(LLVMPassManagerRef PM)192 void LLVMAddLoopFlattenPass(LLVMPassManagerRef PM) {
193   unwrap(PM)->add(createLoopFlattenPass());
194 }
195 
LLVMAddLoopIdiomPass(LLVMPassManagerRef PM)196 void LLVMAddLoopIdiomPass(LLVMPassManagerRef PM) {
197   unwrap(PM)->add(createLoopIdiomPass());
198 }
199 
LLVMAddLoopRotatePass(LLVMPassManagerRef PM)200 void LLVMAddLoopRotatePass(LLVMPassManagerRef PM) {
201   unwrap(PM)->add(createLoopRotatePass());
202 }
203 
LLVMAddLoopRerollPass(LLVMPassManagerRef PM)204 void LLVMAddLoopRerollPass(LLVMPassManagerRef PM) {
205   unwrap(PM)->add(createLoopRerollPass());
206 }
207 
LLVMAddLoopUnrollPass(LLVMPassManagerRef PM)208 void LLVMAddLoopUnrollPass(LLVMPassManagerRef PM) {
209   unwrap(PM)->add(createLoopUnrollPass());
210 }
211 
LLVMAddLoopUnrollAndJamPass(LLVMPassManagerRef PM)212 void LLVMAddLoopUnrollAndJamPass(LLVMPassManagerRef PM) {
213   unwrap(PM)->add(createLoopUnrollAndJamPass());
214 }
215 
LLVMAddLoopUnswitchPass(LLVMPassManagerRef PM)216 void LLVMAddLoopUnswitchPass(LLVMPassManagerRef PM) {
217   unwrap(PM)->add(createLoopUnswitchPass());
218 }
219 
LLVMAddLowerAtomicPass(LLVMPassManagerRef PM)220 void LLVMAddLowerAtomicPass(LLVMPassManagerRef PM) {
221   unwrap(PM)->add(createLowerAtomicPass());
222 }
223 
LLVMAddMemCpyOptPass(LLVMPassManagerRef PM)224 void LLVMAddMemCpyOptPass(LLVMPassManagerRef PM) {
225   unwrap(PM)->add(createMemCpyOptPass());
226 }
227 
LLVMAddPartiallyInlineLibCallsPass(LLVMPassManagerRef PM)228 void LLVMAddPartiallyInlineLibCallsPass(LLVMPassManagerRef PM) {
229   unwrap(PM)->add(createPartiallyInlineLibCallsPass());
230 }
231 
LLVMAddReassociatePass(LLVMPassManagerRef PM)232 void LLVMAddReassociatePass(LLVMPassManagerRef PM) {
233   unwrap(PM)->add(createReassociatePass());
234 }
235 
LLVMAddSCCPPass(LLVMPassManagerRef PM)236 void LLVMAddSCCPPass(LLVMPassManagerRef PM) {
237   unwrap(PM)->add(createSCCPPass());
238 }
239 
LLVMAddScalarReplAggregatesPass(LLVMPassManagerRef PM)240 void LLVMAddScalarReplAggregatesPass(LLVMPassManagerRef PM) {
241   unwrap(PM)->add(createSROAPass());
242 }
243 
LLVMAddScalarReplAggregatesPassSSA(LLVMPassManagerRef PM)244 void LLVMAddScalarReplAggregatesPassSSA(LLVMPassManagerRef PM) {
245   unwrap(PM)->add(createSROAPass());
246 }
247 
LLVMAddScalarReplAggregatesPassWithThreshold(LLVMPassManagerRef PM,int Threshold)248 void LLVMAddScalarReplAggregatesPassWithThreshold(LLVMPassManagerRef PM,
249                                                   int Threshold) {
250   unwrap(PM)->add(createSROAPass());
251 }
252 
LLVMAddSimplifyLibCallsPass(LLVMPassManagerRef PM)253 void LLVMAddSimplifyLibCallsPass(LLVMPassManagerRef PM) {
254   // NOTE: The simplify-libcalls pass has been removed.
255 }
256 
LLVMAddTailCallEliminationPass(LLVMPassManagerRef PM)257 void LLVMAddTailCallEliminationPass(LLVMPassManagerRef PM) {
258   unwrap(PM)->add(createTailCallEliminationPass());
259 }
260 
LLVMAddDemoteMemoryToRegisterPass(LLVMPassManagerRef PM)261 void LLVMAddDemoteMemoryToRegisterPass(LLVMPassManagerRef PM) {
262   unwrap(PM)->add(createDemoteRegisterToMemoryPass());
263 }
264 
LLVMAddVerifierPass(LLVMPassManagerRef PM)265 void LLVMAddVerifierPass(LLVMPassManagerRef PM) {
266   unwrap(PM)->add(createVerifierPass());
267 }
268 
LLVMAddCorrelatedValuePropagationPass(LLVMPassManagerRef PM)269 void LLVMAddCorrelatedValuePropagationPass(LLVMPassManagerRef PM) {
270   unwrap(PM)->add(createCorrelatedValuePropagationPass());
271 }
272 
LLVMAddEarlyCSEPass(LLVMPassManagerRef PM)273 void LLVMAddEarlyCSEPass(LLVMPassManagerRef PM) {
274   unwrap(PM)->add(createEarlyCSEPass(false/*=UseMemorySSA*/));
275 }
276 
LLVMAddEarlyCSEMemSSAPass(LLVMPassManagerRef PM)277 void LLVMAddEarlyCSEMemSSAPass(LLVMPassManagerRef PM) {
278   unwrap(PM)->add(createEarlyCSEPass(true/*=UseMemorySSA*/));
279 }
280 
LLVMAddGVNHoistLegacyPass(LLVMPassManagerRef PM)281 void LLVMAddGVNHoistLegacyPass(LLVMPassManagerRef PM) {
282   unwrap(PM)->add(createGVNHoistPass());
283 }
284 
LLVMAddTypeBasedAliasAnalysisPass(LLVMPassManagerRef PM)285 void LLVMAddTypeBasedAliasAnalysisPass(LLVMPassManagerRef PM) {
286   unwrap(PM)->add(createTypeBasedAAWrapperPass());
287 }
288 
LLVMAddScopedNoAliasAAPass(LLVMPassManagerRef PM)289 void LLVMAddScopedNoAliasAAPass(LLVMPassManagerRef PM) {
290   unwrap(PM)->add(createScopedNoAliasAAWrapperPass());
291 }
292 
LLVMAddBasicAliasAnalysisPass(LLVMPassManagerRef PM)293 void LLVMAddBasicAliasAnalysisPass(LLVMPassManagerRef PM) {
294   unwrap(PM)->add(createBasicAAWrapperPass());
295 }
296 
LLVMAddLowerConstantIntrinsicsPass(LLVMPassManagerRef PM)297 void LLVMAddLowerConstantIntrinsicsPass(LLVMPassManagerRef PM) {
298   unwrap(PM)->add(createLowerConstantIntrinsicsPass());
299 }
300 
LLVMAddLowerExpectIntrinsicPass(LLVMPassManagerRef PM)301 void LLVMAddLowerExpectIntrinsicPass(LLVMPassManagerRef PM) {
302   unwrap(PM)->add(createLowerExpectIntrinsicPass());
303 }
304 
LLVMAddUnifyFunctionExitNodesPass(LLVMPassManagerRef PM)305 void LLVMAddUnifyFunctionExitNodesPass(LLVMPassManagerRef PM) {
306   unwrap(PM)->add(createUnifyFunctionExitNodesPass());
307 }
308