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