1 //===-- SCCP.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 Interprocedural Sparse Conditional Constant Propagation. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Transforms/IPO/SCCP.h" 14 #include "llvm/ADT/SetVector.h" 15 #include "llvm/Analysis/AssumptionCache.h" 16 #include "llvm/Analysis/BlockFrequencyInfo.h" 17 #include "llvm/Analysis/PostDominators.h" 18 #include "llvm/Analysis/TargetLibraryInfo.h" 19 #include "llvm/Analysis/TargetTransformInfo.h" 20 #include "llvm/Analysis/ValueLattice.h" 21 #include "llvm/Analysis/ValueLatticeUtils.h" 22 #include "llvm/Analysis/ValueTracking.h" 23 #include "llvm/IR/AttributeMask.h" 24 #include "llvm/IR/Constants.h" 25 #include "llvm/IR/DIBuilder.h" 26 #include "llvm/IR/IntrinsicInst.h" 27 #include "llvm/Support/CommandLine.h" 28 #include "llvm/Support/ModRef.h" 29 #include "llvm/Transforms/IPO.h" 30 #include "llvm/Transforms/IPO/FunctionSpecialization.h" 31 #include "llvm/Transforms/Scalar/SCCP.h" 32 #include "llvm/Transforms/Utils/Local.h" 33 #include "llvm/Transforms/Utils/SCCPSolver.h" 34 35 using namespace llvm; 36 37 #define DEBUG_TYPE "sccp" 38 39 STATISTIC(NumInstRemoved, "Number of instructions removed"); 40 STATISTIC(NumArgsElimed ,"Number of arguments constant propagated"); 41 STATISTIC(NumGlobalConst, "Number of globals found to be constant"); 42 STATISTIC(NumDeadBlocks , "Number of basic blocks unreachable"); 43 STATISTIC(NumInstReplaced, 44 "Number of instructions replaced with (simpler) instruction"); 45 46 static cl::opt<unsigned> FuncSpecMaxIters( 47 "funcspec-max-iters", cl::init(10), cl::Hidden, cl::desc( 48 "The maximum number of iterations function specialization is run")); 49 50 static void findReturnsToZap(Function &F, 51 SmallVector<ReturnInst *, 8> &ReturnsToZap, 52 SCCPSolver &Solver) { 53 // We can only do this if we know that nothing else can call the function. 54 if (!Solver.isArgumentTrackedFunction(&F)) 55 return; 56 57 if (Solver.mustPreserveReturn(&F)) { 58 LLVM_DEBUG( 59 dbgs() 60 << "Can't zap returns of the function : " << F.getName() 61 << " due to present musttail or \"clang.arc.attachedcall\" call of " 62 "it\n"); 63 return; 64 } 65 66 assert( 67 all_of(F.users(), 68 [&Solver](User *U) { 69 if (isa<Instruction>(U) && 70 !Solver.isBlockExecutable(cast<Instruction>(U)->getParent())) 71 return true; 72 // Non-callsite uses are not impacted by zapping. Also, constant 73 // uses (like blockaddresses) could stuck around, without being 74 // used in the underlying IR, meaning we do not have lattice 75 // values for them. 76 if (!isa<CallBase>(U)) 77 return true; 78 if (U->getType()->isStructTy()) { 79 return none_of(Solver.getStructLatticeValueFor(U), 80 SCCPSolver::isOverdefined); 81 } 82 83 // We don't consider assume-like intrinsics to be actual address 84 // captures. 85 if (auto *II = dyn_cast<IntrinsicInst>(U)) { 86 if (II->isAssumeLikeIntrinsic()) 87 return true; 88 } 89 90 return !SCCPSolver::isOverdefined(Solver.getLatticeValueFor(U)); 91 }) && 92 "We can only zap functions where all live users have a concrete value"); 93 94 for (BasicBlock &BB : F) { 95 if (CallInst *CI = BB.getTerminatingMustTailCall()) { 96 LLVM_DEBUG(dbgs() << "Can't zap return of the block due to present " 97 << "musttail call : " << *CI << "\n"); 98 (void)CI; 99 return; 100 } 101 102 if (auto *RI = dyn_cast<ReturnInst>(BB.getTerminator())) 103 if (!isa<UndefValue>(RI->getOperand(0))) 104 ReturnsToZap.push_back(RI); 105 } 106 } 107 108 static bool runIPSCCP( 109 Module &M, const DataLayout &DL, FunctionAnalysisManager *FAM, 110 std::function<const TargetLibraryInfo &(Function &)> GetTLI, 111 std::function<TargetTransformInfo &(Function &)> GetTTI, 112 std::function<AssumptionCache &(Function &)> GetAC, 113 std::function<DominatorTree &(Function &)> GetDT, 114 std::function<BlockFrequencyInfo &(Function &)> GetBFI, 115 bool IsFuncSpecEnabled) { 116 SCCPSolver Solver(DL, GetTLI, M.getContext()); 117 FunctionSpecializer Specializer(Solver, M, FAM, GetBFI, GetTLI, GetTTI, 118 GetAC); 119 120 // Loop over all functions, marking arguments to those with their addresses 121 // taken or that are external as overdefined. 122 for (Function &F : M) { 123 if (F.isDeclaration()) 124 continue; 125 126 DominatorTree &DT = GetDT(F); 127 AssumptionCache &AC = GetAC(F); 128 Solver.addPredicateInfo(F, DT, AC); 129 130 // Determine if we can track the function's return values. If so, add the 131 // function to the solver's set of return-tracked functions. 132 if (canTrackReturnsInterprocedurally(&F)) 133 Solver.addTrackedFunction(&F); 134 135 // Determine if we can track the function's arguments. If so, add the 136 // function to the solver's set of argument-tracked functions. 137 if (canTrackArgumentsInterprocedurally(&F)) { 138 Solver.addArgumentTrackedFunction(&F); 139 continue; 140 } 141 142 // Assume the function is called. 143 Solver.markBlockExecutable(&F.front()); 144 145 for (Argument &AI : F.args()) 146 Solver.trackValueOfArgument(&AI); 147 } 148 149 // Determine if we can track any of the module's global variables. If so, add 150 // the global variables we can track to the solver's set of tracked global 151 // variables. 152 for (GlobalVariable &G : M.globals()) { 153 G.removeDeadConstantUsers(); 154 if (canTrackGlobalVariableInterprocedurally(&G)) 155 Solver.trackValueOfGlobalVariable(&G); 156 } 157 158 // Solve for constants. 159 Solver.solveWhileResolvedUndefsIn(M); 160 161 if (IsFuncSpecEnabled) { 162 unsigned Iters = 0; 163 while (Iters++ < FuncSpecMaxIters && Specializer.run()); 164 } 165 166 // Iterate over all of the instructions in the module, replacing them with 167 // constants if we have found them to be of constant values. 168 bool MadeChanges = false; 169 for (Function &F : M) { 170 if (F.isDeclaration()) 171 continue; 172 173 SmallVector<BasicBlock *, 512> BlocksToErase; 174 175 if (Solver.isBlockExecutable(&F.front())) { 176 bool ReplacedPointerArg = false; 177 for (Argument &Arg : F.args()) { 178 if (!Arg.use_empty() && Solver.tryToReplaceWithConstant(&Arg)) { 179 ReplacedPointerArg |= Arg.getType()->isPointerTy(); 180 ++NumArgsElimed; 181 } 182 } 183 184 // If we replaced an argument, we may now also access a global (currently 185 // classified as "other" memory). Update memory attribute to reflect this. 186 if (ReplacedPointerArg) { 187 auto UpdateAttrs = [&](AttributeList AL) { 188 MemoryEffects ME = AL.getMemoryEffects(); 189 if (ME == MemoryEffects::unknown()) 190 return AL; 191 192 ModRefInfo ArgMemMR = ME.getModRef(IRMemLocation::ArgMem); 193 ME |= MemoryEffects(IRMemLocation::ErrnoMem, ArgMemMR); 194 ME |= MemoryEffects(IRMemLocation::Other, ArgMemMR); 195 196 return AL.addFnAttribute( 197 F.getContext(), 198 Attribute::getWithMemoryEffects(F.getContext(), ME)); 199 }; 200 201 F.setAttributes(UpdateAttrs(F.getAttributes())); 202 for (User *U : F.users()) { 203 auto *CB = dyn_cast<CallBase>(U); 204 if (!CB || CB->getCalledFunction() != &F) 205 continue; 206 207 CB->setAttributes(UpdateAttrs(CB->getAttributes())); 208 } 209 } 210 MadeChanges |= ReplacedPointerArg; 211 } 212 213 SmallPtrSet<Value *, 32> InsertedValues; 214 for (BasicBlock &BB : F) { 215 if (!Solver.isBlockExecutable(&BB)) { 216 LLVM_DEBUG(dbgs() << " BasicBlock Dead:" << BB); 217 ++NumDeadBlocks; 218 219 MadeChanges = true; 220 221 if (&BB != &F.front()) 222 BlocksToErase.push_back(&BB); 223 continue; 224 } 225 226 MadeChanges |= Solver.simplifyInstsInBlock( 227 BB, InsertedValues, NumInstRemoved, NumInstReplaced); 228 } 229 230 DominatorTree *DT = FAM->getCachedResult<DominatorTreeAnalysis>(F); 231 PostDominatorTree *PDT = FAM->getCachedResult<PostDominatorTreeAnalysis>(F); 232 DomTreeUpdater DTU(DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy); 233 // Change dead blocks to unreachable. We do it after replacing constants 234 // in all executable blocks, because changeToUnreachable may remove PHI 235 // nodes in executable blocks we found values for. The function's entry 236 // block is not part of BlocksToErase, so we have to handle it separately. 237 for (BasicBlock *BB : BlocksToErase) { 238 NumInstRemoved += changeToUnreachable(&*BB->getFirstNonPHIOrDbg(), 239 /*PreserveLCSSA=*/false, &DTU); 240 } 241 if (!Solver.isBlockExecutable(&F.front())) 242 NumInstRemoved += changeToUnreachable(&*F.front().getFirstNonPHIOrDbg(), 243 /*PreserveLCSSA=*/false, &DTU); 244 245 BasicBlock *NewUnreachableBB = nullptr; 246 for (BasicBlock &BB : F) 247 MadeChanges |= Solver.removeNonFeasibleEdges(&BB, DTU, NewUnreachableBB); 248 249 for (BasicBlock *DeadBB : BlocksToErase) 250 if (!DeadBB->hasAddressTaken()) 251 DTU.deleteBB(DeadBB); 252 253 Solver.removeSSACopies(F); 254 } 255 256 // If we inferred constant or undef return values for a function, we replaced 257 // all call uses with the inferred value. This means we don't need to bother 258 // actually returning anything from the function. Replace all return 259 // instructions with return undef. 260 // 261 // Do this in two stages: first identify the functions we should process, then 262 // actually zap their returns. This is important because we can only do this 263 // if the address of the function isn't taken. In cases where a return is the 264 // last use of a function, the order of processing functions would affect 265 // whether other functions are optimizable. 266 SmallVector<ReturnInst*, 8> ReturnsToZap; 267 268 Solver.inferReturnAttributes(); 269 Solver.inferArgAttributes(); 270 for (const auto &[F, ReturnValue] : Solver.getTrackedRetVals()) { 271 assert(!F->getReturnType()->isVoidTy() && 272 "should not track void functions"); 273 if (SCCPSolver::isConstant(ReturnValue) || ReturnValue.isUnknownOrUndef()) 274 findReturnsToZap(*F, ReturnsToZap, Solver); 275 } 276 277 for (auto *F : Solver.getMRVFunctionsTracked()) { 278 assert(F->getReturnType()->isStructTy() && 279 "The return type should be a struct"); 280 StructType *STy = cast<StructType>(F->getReturnType()); 281 if (Solver.isStructLatticeConstant(F, STy)) 282 findReturnsToZap(*F, ReturnsToZap, Solver); 283 } 284 285 // Zap all returns which we've identified as zap to change. 286 SmallSetVector<Function *, 8> FuncZappedReturn; 287 for (ReturnInst *RI : ReturnsToZap) { 288 Function *F = RI->getParent()->getParent(); 289 RI->setOperand(0, PoisonValue::get(F->getReturnType())); 290 // Record all functions that are zapped. 291 FuncZappedReturn.insert(F); 292 } 293 294 // Remove the returned attribute for zapped functions and the 295 // corresponding call sites. 296 // Also remove any attributes that convert an undef return value into 297 // immediate undefined behavior 298 AttributeMask UBImplyingAttributes = 299 AttributeFuncs::getUBImplyingAttributes(); 300 for (Function *F : FuncZappedReturn) { 301 for (Argument &A : F->args()) 302 F->removeParamAttr(A.getArgNo(), Attribute::Returned); 303 F->removeRetAttrs(UBImplyingAttributes); 304 for (Use &U : F->uses()) { 305 CallBase *CB = dyn_cast<CallBase>(U.getUser()); 306 if (!CB) { 307 assert(isa<Constant>(U.getUser()) && 308 all_of(U.getUser()->users(), [](const User *UserUser) { 309 return cast<IntrinsicInst>(UserUser)->isAssumeLikeIntrinsic(); 310 })); 311 continue; 312 } 313 314 for (Use &Arg : CB->args()) 315 CB->removeParamAttr(CB->getArgOperandNo(&Arg), Attribute::Returned); 316 CB->removeRetAttrs(UBImplyingAttributes); 317 } 318 } 319 320 // If we inferred constant or undef values for globals variables, we can 321 // delete the global and any stores that remain to it. 322 for (const auto &I : make_early_inc_range(Solver.getTrackedGlobals())) { 323 GlobalVariable *GV = I.first; 324 if (SCCPSolver::isOverdefined(I.second)) 325 continue; 326 LLVM_DEBUG(dbgs() << "Found that GV '" << GV->getName() 327 << "' is constant!\n"); 328 for (User *U : make_early_inc_range(GV->users())) { 329 // We can remove LoadInst here, because we already replaced its users 330 // with a constant. 331 assert((isa<StoreInst>(U) || isa<LoadInst>(U)) && 332 "Only Store|Load Instruction can be user of GlobalVariable at " 333 "reaching here."); 334 cast<Instruction>(U)->eraseFromParent(); 335 } 336 337 // Try to create a debug constant expression for the global variable 338 // initializer value. 339 SmallVector<DIGlobalVariableExpression *, 1> GVEs; 340 GV->getDebugInfo(GVEs); 341 if (GVEs.size() == 1) { 342 DIBuilder DIB(M); 343 if (DIExpression *InitExpr = getExpressionForConstant( 344 DIB, *GV->getInitializer(), *GV->getValueType())) 345 GVEs[0]->replaceOperandWith(1, InitExpr); 346 } 347 348 MadeChanges = true; 349 M.eraseGlobalVariable(GV); 350 ++NumGlobalConst; 351 } 352 353 return MadeChanges; 354 } 355 356 PreservedAnalyses IPSCCPPass::run(Module &M, ModuleAnalysisManager &AM) { 357 const DataLayout &DL = M.getDataLayout(); 358 auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 359 auto GetTLI = [&FAM](Function &F) -> const TargetLibraryInfo & { 360 return FAM.getResult<TargetLibraryAnalysis>(F); 361 }; 362 auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & { 363 return FAM.getResult<TargetIRAnalysis>(F); 364 }; 365 auto GetAC = [&FAM](Function &F) -> AssumptionCache & { 366 return FAM.getResult<AssumptionAnalysis>(F); 367 }; 368 auto GetDT = [&FAM](Function &F) -> DominatorTree & { 369 return FAM.getResult<DominatorTreeAnalysis>(F); 370 }; 371 auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & { 372 return FAM.getResult<BlockFrequencyAnalysis>(F); 373 }; 374 375 376 if (!runIPSCCP(M, DL, &FAM, GetTLI, GetTTI, GetAC, GetDT, GetBFI, 377 isFuncSpecEnabled())) 378 return PreservedAnalyses::all(); 379 380 PreservedAnalyses PA; 381 PA.preserve<DominatorTreeAnalysis>(); 382 PA.preserve<PostDominatorTreeAnalysis>(); 383 PA.preserve<FunctionAnalysisManagerModuleProxy>(); 384 return PA; 385 } 386