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 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)120void LLVMAddLoopSimplifyCFGPass(LLVMPassManagerRef PM) { 121 unwrap(PM)->add(createLoopSimplifyCFGPass()); 122 } 123 LLVMInitializeScalarOpts(LLVMPassRegistryRef R)124void LLVMInitializeScalarOpts(LLVMPassRegistryRef R) { 125 initializeScalarOpts(*unwrap(R)); 126 } 127 LLVMAddAggressiveDCEPass(LLVMPassManagerRef PM)128void LLVMAddAggressiveDCEPass(LLVMPassManagerRef PM) { 129 unwrap(PM)->add(createAggressiveDCEPass()); 130 } 131 LLVMAddDCEPass(LLVMPassManagerRef PM)132void LLVMAddDCEPass(LLVMPassManagerRef PM) { 133 unwrap(PM)->add(createDeadCodeEliminationPass()); 134 } 135 LLVMAddBitTrackingDCEPass(LLVMPassManagerRef PM)136void LLVMAddBitTrackingDCEPass(LLVMPassManagerRef PM) { 137 unwrap(PM)->add(createBitTrackingDCEPass()); 138 } 139 LLVMAddAlignmentFromAssumptionsPass(LLVMPassManagerRef PM)140void LLVMAddAlignmentFromAssumptionsPass(LLVMPassManagerRef PM) { 141 unwrap(PM)->add(createAlignmentFromAssumptionsPass()); 142 } 143 LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM)144void LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM) { 145 unwrap(PM)->add(createCFGSimplificationPass()); 146 } 147 LLVMAddDeadStoreEliminationPass(LLVMPassManagerRef PM)148void LLVMAddDeadStoreEliminationPass(LLVMPassManagerRef PM) { 149 unwrap(PM)->add(createDeadStoreEliminationPass()); 150 } 151 LLVMAddScalarizerPass(LLVMPassManagerRef PM)152void LLVMAddScalarizerPass(LLVMPassManagerRef PM) { 153 unwrap(PM)->add(createScalarizerPass()); 154 } 155 LLVMAddGVNPass(LLVMPassManagerRef PM)156void LLVMAddGVNPass(LLVMPassManagerRef PM) { 157 unwrap(PM)->add(createGVNPass()); 158 } 159 LLVMAddNewGVNPass(LLVMPassManagerRef PM)160void LLVMAddNewGVNPass(LLVMPassManagerRef PM) { 161 unwrap(PM)->add(createNewGVNPass()); 162 } 163 LLVMAddMergedLoadStoreMotionPass(LLVMPassManagerRef PM)164void LLVMAddMergedLoadStoreMotionPass(LLVMPassManagerRef PM) { 165 unwrap(PM)->add(createMergedLoadStoreMotionPass()); 166 } 167 LLVMAddIndVarSimplifyPass(LLVMPassManagerRef PM)168void LLVMAddIndVarSimplifyPass(LLVMPassManagerRef PM) { 169 unwrap(PM)->add(createIndVarSimplifyPass()); 170 } 171 LLVMAddInstructionSimplifyPass(LLVMPassManagerRef PM)172void LLVMAddInstructionSimplifyPass(LLVMPassManagerRef PM) { 173 unwrap(PM)->add(createInstSimplifyLegacyPass()); 174 } 175 LLVMAddJumpThreadingPass(LLVMPassManagerRef PM)176void LLVMAddJumpThreadingPass(LLVMPassManagerRef PM) { 177 unwrap(PM)->add(createJumpThreadingPass()); 178 } 179 LLVMAddLoopSinkPass(LLVMPassManagerRef PM)180void LLVMAddLoopSinkPass(LLVMPassManagerRef PM) { 181 unwrap(PM)->add(createLoopSinkPass()); 182 } 183 LLVMAddLICMPass(LLVMPassManagerRef PM)184void LLVMAddLICMPass(LLVMPassManagerRef PM) { 185 unwrap(PM)->add(createLICMPass()); 186 } 187 LLVMAddLoopDeletionPass(LLVMPassManagerRef PM)188void LLVMAddLoopDeletionPass(LLVMPassManagerRef PM) { 189 unwrap(PM)->add(createLoopDeletionPass()); 190 } 191 LLVMAddLoopFlattenPass(LLVMPassManagerRef PM)192void LLVMAddLoopFlattenPass(LLVMPassManagerRef PM) { 193 unwrap(PM)->add(createLoopFlattenPass()); 194 } 195 LLVMAddLoopIdiomPass(LLVMPassManagerRef PM)196void LLVMAddLoopIdiomPass(LLVMPassManagerRef PM) { 197 unwrap(PM)->add(createLoopIdiomPass()); 198 } 199 LLVMAddLoopRotatePass(LLVMPassManagerRef PM)200void LLVMAddLoopRotatePass(LLVMPassManagerRef PM) { 201 unwrap(PM)->add(createLoopRotatePass()); 202 } 203 LLVMAddLoopRerollPass(LLVMPassManagerRef PM)204void LLVMAddLoopRerollPass(LLVMPassManagerRef PM) { 205 unwrap(PM)->add(createLoopRerollPass()); 206 } 207 LLVMAddLoopUnrollPass(LLVMPassManagerRef PM)208void LLVMAddLoopUnrollPass(LLVMPassManagerRef PM) { 209 unwrap(PM)->add(createLoopUnrollPass()); 210 } 211 LLVMAddLoopUnrollAndJamPass(LLVMPassManagerRef PM)212void LLVMAddLoopUnrollAndJamPass(LLVMPassManagerRef PM) { 213 unwrap(PM)->add(createLoopUnrollAndJamPass()); 214 } 215 LLVMAddLoopUnswitchPass(LLVMPassManagerRef PM)216void LLVMAddLoopUnswitchPass(LLVMPassManagerRef PM) { 217 unwrap(PM)->add(createLoopUnswitchPass()); 218 } 219 LLVMAddLowerAtomicPass(LLVMPassManagerRef PM)220void LLVMAddLowerAtomicPass(LLVMPassManagerRef PM) { 221 unwrap(PM)->add(createLowerAtomicPass()); 222 } 223 LLVMAddMemCpyOptPass(LLVMPassManagerRef PM)224void LLVMAddMemCpyOptPass(LLVMPassManagerRef PM) { 225 unwrap(PM)->add(createMemCpyOptPass()); 226 } 227 LLVMAddPartiallyInlineLibCallsPass(LLVMPassManagerRef PM)228void LLVMAddPartiallyInlineLibCallsPass(LLVMPassManagerRef PM) { 229 unwrap(PM)->add(createPartiallyInlineLibCallsPass()); 230 } 231 LLVMAddReassociatePass(LLVMPassManagerRef PM)232void LLVMAddReassociatePass(LLVMPassManagerRef PM) { 233 unwrap(PM)->add(createReassociatePass()); 234 } 235 LLVMAddSCCPPass(LLVMPassManagerRef PM)236void LLVMAddSCCPPass(LLVMPassManagerRef PM) { 237 unwrap(PM)->add(createSCCPPass()); 238 } 239 LLVMAddScalarReplAggregatesPass(LLVMPassManagerRef PM)240void LLVMAddScalarReplAggregatesPass(LLVMPassManagerRef PM) { 241 unwrap(PM)->add(createSROAPass()); 242 } 243 LLVMAddScalarReplAggregatesPassSSA(LLVMPassManagerRef PM)244void LLVMAddScalarReplAggregatesPassSSA(LLVMPassManagerRef PM) { 245 unwrap(PM)->add(createSROAPass()); 246 } 247 LLVMAddScalarReplAggregatesPassWithThreshold(LLVMPassManagerRef PM,int Threshold)248void LLVMAddScalarReplAggregatesPassWithThreshold(LLVMPassManagerRef PM, 249 int Threshold) { 250 unwrap(PM)->add(createSROAPass()); 251 } 252 LLVMAddSimplifyLibCallsPass(LLVMPassManagerRef PM)253void LLVMAddSimplifyLibCallsPass(LLVMPassManagerRef PM) { 254 // NOTE: The simplify-libcalls pass has been removed. 255 } 256 LLVMAddTailCallEliminationPass(LLVMPassManagerRef PM)257void LLVMAddTailCallEliminationPass(LLVMPassManagerRef PM) { 258 unwrap(PM)->add(createTailCallEliminationPass()); 259 } 260 LLVMAddDemoteMemoryToRegisterPass(LLVMPassManagerRef PM)261void LLVMAddDemoteMemoryToRegisterPass(LLVMPassManagerRef PM) { 262 unwrap(PM)->add(createDemoteRegisterToMemoryPass()); 263 } 264 LLVMAddVerifierPass(LLVMPassManagerRef PM)265void LLVMAddVerifierPass(LLVMPassManagerRef PM) { 266 unwrap(PM)->add(createVerifierPass()); 267 } 268 LLVMAddCorrelatedValuePropagationPass(LLVMPassManagerRef PM)269void LLVMAddCorrelatedValuePropagationPass(LLVMPassManagerRef PM) { 270 unwrap(PM)->add(createCorrelatedValuePropagationPass()); 271 } 272 LLVMAddEarlyCSEPass(LLVMPassManagerRef PM)273void LLVMAddEarlyCSEPass(LLVMPassManagerRef PM) { 274 unwrap(PM)->add(createEarlyCSEPass(false/*=UseMemorySSA*/)); 275 } 276 LLVMAddEarlyCSEMemSSAPass(LLVMPassManagerRef PM)277void LLVMAddEarlyCSEMemSSAPass(LLVMPassManagerRef PM) { 278 unwrap(PM)->add(createEarlyCSEPass(true/*=UseMemorySSA*/)); 279 } 280 LLVMAddGVNHoistLegacyPass(LLVMPassManagerRef PM)281void LLVMAddGVNHoistLegacyPass(LLVMPassManagerRef PM) { 282 unwrap(PM)->add(createGVNHoistPass()); 283 } 284 LLVMAddTypeBasedAliasAnalysisPass(LLVMPassManagerRef PM)285void LLVMAddTypeBasedAliasAnalysisPass(LLVMPassManagerRef PM) { 286 unwrap(PM)->add(createTypeBasedAAWrapperPass()); 287 } 288 LLVMAddScopedNoAliasAAPass(LLVMPassManagerRef PM)289void LLVMAddScopedNoAliasAAPass(LLVMPassManagerRef PM) { 290 unwrap(PM)->add(createScopedNoAliasAAWrapperPass()); 291 } 292 LLVMAddBasicAliasAnalysisPass(LLVMPassManagerRef PM)293void LLVMAddBasicAliasAnalysisPass(LLVMPassManagerRef PM) { 294 unwrap(PM)->add(createBasicAAWrapperPass()); 295 } 296 LLVMAddLowerConstantIntrinsicsPass(LLVMPassManagerRef PM)297void LLVMAddLowerConstantIntrinsicsPass(LLVMPassManagerRef PM) { 298 unwrap(PM)->add(createLowerConstantIntrinsicsPass()); 299 } 300 LLVMAddLowerExpectIntrinsicPass(LLVMPassManagerRef PM)301void LLVMAddLowerExpectIntrinsicPass(LLVMPassManagerRef PM) { 302 unwrap(PM)->add(createLowerExpectIntrinsicPass()); 303 } 304 LLVMAddUnifyFunctionExitNodesPass(LLVMPassManagerRef PM)305void LLVMAddUnifyFunctionExitNodesPass(LLVMPassManagerRef PM) { 306 unwrap(PM)->add(createUnifyFunctionExitNodesPass()); 307 } 308