Scalar.cpp 10 KB

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