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