1 //===- MemCpyOptimizer.h - memcpy optimization ------------------*- C++ -*-===// 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 pass performs various transformations related to eliminating memcpy 10 // calls, or transforming sets of stores into memset's. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_TRANSFORMS_SCALAR_MEMCPYOPTIMIZER_H 15 #define LLVM_TRANSFORMS_SCALAR_MEMCPYOPTIMIZER_H 16 17 #include "llvm/IR/BasicBlock.h" 18 #include "llvm/IR/PassManager.h" 19 20 namespace llvm { 21 22 class AAResults; 23 class AllocaInst; 24 class BatchAAResults; 25 class AssumptionCache; 26 class CallBase; 27 class CallInst; 28 class DominatorTree; 29 class Function; 30 class Instruction; 31 class LoadInst; 32 class MemCpyInst; 33 class MemMoveInst; 34 class MemorySSA; 35 class MemorySSAUpdater; 36 class MemSetInst; 37 class PostDominatorTree; 38 class StoreInst; 39 class TargetLibraryInfo; 40 class TypeSize; 41 class Value; 42 43 class MemCpyOptPass : public PassInfoMixin<MemCpyOptPass> { 44 TargetLibraryInfo *TLI = nullptr; 45 AAResults *AA = nullptr; 46 AssumptionCache *AC = nullptr; 47 DominatorTree *DT = nullptr; 48 PostDominatorTree *PDT = nullptr; 49 MemorySSA *MSSA = nullptr; 50 MemorySSAUpdater *MSSAU = nullptr; 51 52 public: 53 MemCpyOptPass() = default; 54 55 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 56 57 // Glue for the old PM. 58 bool runImpl(Function &F, TargetLibraryInfo *TLI, AAResults *AA, 59 AssumptionCache *AC, DominatorTree *DT, PostDominatorTree *PDT, 60 MemorySSA *MSSA); 61 62 private: 63 // Helper functions 64 bool processStore(StoreInst *SI, BasicBlock::iterator &BBI); 65 bool processStoreOfLoad(StoreInst *SI, LoadInst *LI, const DataLayout &DL, 66 BasicBlock::iterator &BBI); 67 bool processMemSet(MemSetInst *SI, BasicBlock::iterator &BBI); 68 bool processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI); 69 bool processMemMove(MemMoveInst *M); 70 bool performCallSlotOptzn(Instruction *cpyLoad, Instruction *cpyStore, 71 Value *cpyDst, Value *cpySrc, TypeSize cpyLen, 72 Align cpyAlign, BatchAAResults &BAA, 73 std::function<CallInst *()> GetC); 74 bool processMemCpyMemCpyDependence(MemCpyInst *M, MemCpyInst *MDep, 75 BatchAAResults &BAA); 76 bool processMemSetMemCpyDependence(MemCpyInst *MemCpy, MemSetInst *MemSet, 77 BatchAAResults &BAA); 78 bool performMemCpyToMemSetOptzn(MemCpyInst *MemCpy, MemSetInst *MemSet, 79 BatchAAResults &BAA); 80 bool processByValArgument(CallBase &CB, unsigned ArgNo); 81 bool processImmutArgument(CallBase &CB, unsigned ArgNo); 82 Instruction *tryMergingIntoMemset(Instruction *I, Value *StartPtr, 83 Value *ByteVal); 84 bool moveUp(StoreInst *SI, Instruction *P, const LoadInst *LI); 85 bool performStackMoveOptzn(Instruction *Load, Instruction *Store, 86 AllocaInst *DestAlloca, AllocaInst *SrcAlloca, 87 TypeSize Size, BatchAAResults &BAA); 88 89 void eraseInstruction(Instruction *I); 90 bool iterateOnFunction(Function &F); 91 }; 92 93 } // end namespace llvm 94 95 #endif // LLVM_TRANSFORMS_SCALAR_MEMCPYOPTIMIZER_H 96