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)35void llvm::initializeScalarOpts(PassRegistry &Registry) { 36 initializeADCELegacyPassPass(Registry); 37 initializeBDCELegacyPassPass(Registry); 38 initializeAlignmentFromAssumptionsPass(Registry); 39 initializeCallSiteSplittingLegacyPassPass(Registry); 40 initializeConstantHoistingLegacyPassPass(Registry); 41 initializeConstantPropagationPass(Registry); 42 initializeCorrelatedValuePropagationPass(Registry); 43 initializeDCELegacyPassPass(Registry); 44 initializeDeadInstEliminationPass(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 initializeLoopInterchangePass(Registry); 71 initializeLoopPredicationLegacyPassPass(Registry); 72 initializeLoopRotateLegacyPassPass(Registry); 73 initializeLoopStrengthReducePass(Registry); 74 initializeLoopRerollPass(Registry); 75 initializeLoopUnrollPass(Registry); 76 initializeLoopUnrollAndJamPass(Registry); 77 initializeLoopUnswitchPass(Registry); 78 initializeWarnMissedTransformationsLegacyPass(Registry); 79 initializeLoopVersioningLICMPass(Registry); 80 initializeLoopIdiomRecognizeLegacyPassPass(Registry); 81 initializeLowerAtomicLegacyPassPass(Registry); 82 initializeLowerConstantIntrinsicsPass(Registry); 83 initializeLowerExpectIntrinsicPass(Registry); 84 initializeLowerGuardIntrinsicLegacyPassPass(Registry); 85 initializeLowerMatrixIntrinsicsLegacyPassPass(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 initializeRegToMemPass(Registry); 95 initializeRewriteStatepointsForGCLegacyPassPass(Registry); 96 initializeSCCPLegacyPassPass(Registry); 97 initializeSROALegacyPassPass(Registry); 98 initializeCFGSimplifyPassPass(Registry); 99 initializeStructurizeCFGPass(Registry); 100 initializeSimpleLoopUnswitchLegacyPassPass(Registry); 101 initializeSinkingLegacyPassPass(Registry); 102 initializeTailCallElimPass(Registry); 103 initializeSeparateConstOffsetFromGEPPass(Registry); 104 initializeSpeculativeExecutionLegacyPassPass(Registry); 105 initializeStraightLineStrengthReducePass(Registry); 106 initializePlaceBackedgeSafepointsImplPass(Registry); 107 initializePlaceSafepointsPass(Registry); 108 initializeFloat2IntLegacyPassPass(Registry); 109 initializeLoopDistributeLegacyPass(Registry); 110 initializeLoopLoadEliminationPass(Registry); 111 initializeLoopSimplifyCFGLegacyPassPass(Registry); 112 initializeLoopVersioningPassPass(Registry); 113 initializeEntryExitInstrumenterPass(Registry); 114 initializePostInlineEntryExitInstrumenterPass(Registry); 115 } 116 LLVMAddLoopSimplifyCFGPass(LLVMPassManagerRef PM)117void LLVMAddLoopSimplifyCFGPass(LLVMPassManagerRef PM) { 118 unwrap(PM)->add(createLoopSimplifyCFGPass()); 119 } 120 LLVMInitializeScalarOpts(LLVMPassRegistryRef R)121void LLVMInitializeScalarOpts(LLVMPassRegistryRef R) { 122 initializeScalarOpts(*unwrap(R)); 123 } 124 LLVMAddAggressiveDCEPass(LLVMPassManagerRef PM)125void LLVMAddAggressiveDCEPass(LLVMPassManagerRef PM) { 126 unwrap(PM)->add(createAggressiveDCEPass()); 127 } 128 LLVMAddDCEPass(LLVMPassManagerRef PM)129void LLVMAddDCEPass(LLVMPassManagerRef PM) { 130 unwrap(PM)->add(createDeadCodeEliminationPass()); 131 } 132 LLVMAddBitTrackingDCEPass(LLVMPassManagerRef PM)133void LLVMAddBitTrackingDCEPass(LLVMPassManagerRef PM) { 134 unwrap(PM)->add(createBitTrackingDCEPass()); 135 } 136 LLVMAddAlignmentFromAssumptionsPass(LLVMPassManagerRef PM)137void LLVMAddAlignmentFromAssumptionsPass(LLVMPassManagerRef PM) { 138 unwrap(PM)->add(createAlignmentFromAssumptionsPass()); 139 } 140 LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM)141void LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM) { 142 unwrap(PM)->add(createCFGSimplificationPass(1, false, false, true)); 143 } 144 LLVMAddDeadStoreEliminationPass(LLVMPassManagerRef PM)145void LLVMAddDeadStoreEliminationPass(LLVMPassManagerRef PM) { 146 unwrap(PM)->add(createDeadStoreEliminationPass()); 147 } 148 LLVMAddScalarizerPass(LLVMPassManagerRef PM)149void LLVMAddScalarizerPass(LLVMPassManagerRef PM) { 150 unwrap(PM)->add(createScalarizerPass()); 151 } 152 LLVMAddGVNPass(LLVMPassManagerRef PM)153void LLVMAddGVNPass(LLVMPassManagerRef PM) { 154 unwrap(PM)->add(createGVNPass()); 155 } 156 LLVMAddNewGVNPass(LLVMPassManagerRef PM)157void LLVMAddNewGVNPass(LLVMPassManagerRef PM) { 158 unwrap(PM)->add(createNewGVNPass()); 159 } 160 LLVMAddMergedLoadStoreMotionPass(LLVMPassManagerRef PM)161void LLVMAddMergedLoadStoreMotionPass(LLVMPassManagerRef PM) { 162 unwrap(PM)->add(createMergedLoadStoreMotionPass()); 163 } 164 LLVMAddIndVarSimplifyPass(LLVMPassManagerRef PM)165void LLVMAddIndVarSimplifyPass(LLVMPassManagerRef PM) { 166 unwrap(PM)->add(createIndVarSimplifyPass()); 167 } 168 LLVMAddJumpThreadingPass(LLVMPassManagerRef PM)169void LLVMAddJumpThreadingPass(LLVMPassManagerRef PM) { 170 unwrap(PM)->add(createJumpThreadingPass()); 171 } 172 LLVMAddLoopSinkPass(LLVMPassManagerRef PM)173void LLVMAddLoopSinkPass(LLVMPassManagerRef PM) { 174 unwrap(PM)->add(createLoopSinkPass()); 175 } 176 LLVMAddLICMPass(LLVMPassManagerRef PM)177void LLVMAddLICMPass(LLVMPassManagerRef PM) { 178 unwrap(PM)->add(createLICMPass()); 179 } 180 LLVMAddLoopDeletionPass(LLVMPassManagerRef PM)181void LLVMAddLoopDeletionPass(LLVMPassManagerRef PM) { 182 unwrap(PM)->add(createLoopDeletionPass()); 183 } 184 LLVMAddLoopIdiomPass(LLVMPassManagerRef PM)185void LLVMAddLoopIdiomPass(LLVMPassManagerRef PM) { 186 unwrap(PM)->add(createLoopIdiomPass()); 187 } 188 LLVMAddLoopRotatePass(LLVMPassManagerRef PM)189void LLVMAddLoopRotatePass(LLVMPassManagerRef PM) { 190 unwrap(PM)->add(createLoopRotatePass()); 191 } 192 LLVMAddLoopRerollPass(LLVMPassManagerRef PM)193void LLVMAddLoopRerollPass(LLVMPassManagerRef PM) { 194 unwrap(PM)->add(createLoopRerollPass()); 195 } 196 LLVMAddLoopUnrollPass(LLVMPassManagerRef PM)197void LLVMAddLoopUnrollPass(LLVMPassManagerRef PM) { 198 unwrap(PM)->add(createLoopUnrollPass()); 199 } 200 LLVMAddLoopUnrollAndJamPass(LLVMPassManagerRef PM)201void LLVMAddLoopUnrollAndJamPass(LLVMPassManagerRef PM) { 202 unwrap(PM)->add(createLoopUnrollAndJamPass()); 203 } 204 LLVMAddLoopUnswitchPass(LLVMPassManagerRef PM)205void LLVMAddLoopUnswitchPass(LLVMPassManagerRef PM) { 206 unwrap(PM)->add(createLoopUnswitchPass()); 207 } 208 LLVMAddLowerAtomicPass(LLVMPassManagerRef PM)209void LLVMAddLowerAtomicPass(LLVMPassManagerRef PM) { 210 unwrap(PM)->add(createLowerAtomicPass()); 211 } 212 LLVMAddMemCpyOptPass(LLVMPassManagerRef PM)213void LLVMAddMemCpyOptPass(LLVMPassManagerRef PM) { 214 unwrap(PM)->add(createMemCpyOptPass()); 215 } 216 LLVMAddPartiallyInlineLibCallsPass(LLVMPassManagerRef PM)217void LLVMAddPartiallyInlineLibCallsPass(LLVMPassManagerRef PM) { 218 unwrap(PM)->add(createPartiallyInlineLibCallsPass()); 219 } 220 LLVMAddReassociatePass(LLVMPassManagerRef PM)221void LLVMAddReassociatePass(LLVMPassManagerRef PM) { 222 unwrap(PM)->add(createReassociatePass()); 223 } 224 LLVMAddSCCPPass(LLVMPassManagerRef PM)225void LLVMAddSCCPPass(LLVMPassManagerRef PM) { 226 unwrap(PM)->add(createSCCPPass()); 227 } 228 LLVMAddScalarReplAggregatesPass(LLVMPassManagerRef PM)229void LLVMAddScalarReplAggregatesPass(LLVMPassManagerRef PM) { 230 unwrap(PM)->add(createSROAPass()); 231 } 232 LLVMAddScalarReplAggregatesPassSSA(LLVMPassManagerRef PM)233void LLVMAddScalarReplAggregatesPassSSA(LLVMPassManagerRef PM) { 234 unwrap(PM)->add(createSROAPass()); 235 } 236 LLVMAddScalarReplAggregatesPassWithThreshold(LLVMPassManagerRef PM,int Threshold)237void LLVMAddScalarReplAggregatesPassWithThreshold(LLVMPassManagerRef PM, 238 int Threshold) { 239 unwrap(PM)->add(createSROAPass()); 240 } 241 LLVMAddSimplifyLibCallsPass(LLVMPassManagerRef PM)242void LLVMAddSimplifyLibCallsPass(LLVMPassManagerRef PM) { 243 // NOTE: The simplify-libcalls pass has been removed. 244 } 245 LLVMAddTailCallEliminationPass(LLVMPassManagerRef PM)246void LLVMAddTailCallEliminationPass(LLVMPassManagerRef PM) { 247 unwrap(PM)->add(createTailCallEliminationPass()); 248 } 249 LLVMAddConstantPropagationPass(LLVMPassManagerRef PM)250void LLVMAddConstantPropagationPass(LLVMPassManagerRef PM) { 251 unwrap(PM)->add(createConstantPropagationPass()); 252 } 253 LLVMAddDemoteMemoryToRegisterPass(LLVMPassManagerRef PM)254void LLVMAddDemoteMemoryToRegisterPass(LLVMPassManagerRef PM) { 255 unwrap(PM)->add(createDemoteRegisterToMemoryPass()); 256 } 257 LLVMAddVerifierPass(LLVMPassManagerRef PM)258void LLVMAddVerifierPass(LLVMPassManagerRef PM) { 259 unwrap(PM)->add(createVerifierPass()); 260 } 261 LLVMAddCorrelatedValuePropagationPass(LLVMPassManagerRef PM)262void LLVMAddCorrelatedValuePropagationPass(LLVMPassManagerRef PM) { 263 unwrap(PM)->add(createCorrelatedValuePropagationPass()); 264 } 265 LLVMAddEarlyCSEPass(LLVMPassManagerRef PM)266void LLVMAddEarlyCSEPass(LLVMPassManagerRef PM) { 267 unwrap(PM)->add(createEarlyCSEPass(false/*=UseMemorySSA*/)); 268 } 269 LLVMAddEarlyCSEMemSSAPass(LLVMPassManagerRef PM)270void LLVMAddEarlyCSEMemSSAPass(LLVMPassManagerRef PM) { 271 unwrap(PM)->add(createEarlyCSEPass(true/*=UseMemorySSA*/)); 272 } 273 LLVMAddGVNHoistLegacyPass(LLVMPassManagerRef PM)274void LLVMAddGVNHoistLegacyPass(LLVMPassManagerRef PM) { 275 unwrap(PM)->add(createGVNHoistPass()); 276 } 277 LLVMAddTypeBasedAliasAnalysisPass(LLVMPassManagerRef PM)278void LLVMAddTypeBasedAliasAnalysisPass(LLVMPassManagerRef PM) { 279 unwrap(PM)->add(createTypeBasedAAWrapperPass()); 280 } 281 LLVMAddScopedNoAliasAAPass(LLVMPassManagerRef PM)282void LLVMAddScopedNoAliasAAPass(LLVMPassManagerRef PM) { 283 unwrap(PM)->add(createScopedNoAliasAAWrapperPass()); 284 } 285 LLVMAddBasicAliasAnalysisPass(LLVMPassManagerRef PM)286void LLVMAddBasicAliasAnalysisPass(LLVMPassManagerRef PM) { 287 unwrap(PM)->add(createBasicAAWrapperPass()); 288 } 289 LLVMAddLowerConstantIntrinsicsPass(LLVMPassManagerRef PM)290void LLVMAddLowerConstantIntrinsicsPass(LLVMPassManagerRef PM) { 291 unwrap(PM)->add(createLowerConstantIntrinsicsPass()); 292 } 293 LLVMAddLowerExpectIntrinsicPass(LLVMPassManagerRef PM)294void LLVMAddLowerExpectIntrinsicPass(LLVMPassManagerRef PM) { 295 unwrap(PM)->add(createLowerExpectIntrinsicPass()); 296 } 297 LLVMAddUnifyFunctionExitNodesPass(LLVMPassManagerRef PM)298void LLVMAddUnifyFunctionExitNodesPass(LLVMPassManagerRef PM) { 299 unwrap(PM)->add(createUnifyFunctionExitNodesPass()); 300 } 301