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 <cstdint>
20 #include <functional>
21 
22 namespace llvm {
23 
24 class AAResults;
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 MemoryDependenceResults;
35 class MemorySSA;
36 class MemorySSAUpdater;
37 class MemSetInst;
38 class StoreInst;
39 class TargetLibraryInfo;
40 class Value;
41 
42 class MemCpyOptPass : public PassInfoMixin<MemCpyOptPass> {
43   MemoryDependenceResults *MD = nullptr;
44   TargetLibraryInfo *TLI = nullptr;
45   AAResults *AA = nullptr;
46   AssumptionCache *AC = nullptr;
47   DominatorTree *DT = nullptr;
48   MemorySSA *MSSA = nullptr;
49   MemorySSAUpdater *MSSAU = nullptr;
50 
51 public:
52   MemCpyOptPass() = default;
53 
54   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
55 
56   // Glue for the old PM.
57   bool runImpl(Function &F, MemoryDependenceResults *MD, TargetLibraryInfo *TLI,
58                AAResults *AA, AssumptionCache *AC, DominatorTree *DT,
59                MemorySSA *MSSA);
60 
61 private:
62   // Helper functions
63   bool processStore(StoreInst *SI, BasicBlock::iterator &BBI);
64   bool processMemSet(MemSetInst *SI, BasicBlock::iterator &BBI);
65   bool processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI);
66   bool processMemMove(MemMoveInst *M);
67   bool performCallSlotOptzn(Instruction *cpyLoad, Instruction *cpyStore,
68                             Value *cpyDst, Value *cpySrc, uint64_t cpyLen,
69                             Align cpyAlign, CallInst *C);
70   bool processMemCpyMemCpyDependence(MemCpyInst *M, MemCpyInst *MDep);
71   bool processMemSetMemCpyDependence(MemCpyInst *MemCpy, MemSetInst *MemSet);
72   bool performMemCpyToMemSetOptzn(MemCpyInst *MemCpy, MemSetInst *MemSet);
73   bool processByValArgument(CallBase &CB, unsigned ArgNo);
74   Instruction *tryMergingIntoMemset(Instruction *I, Value *StartPtr,
75                                     Value *ByteVal);
76   bool moveUp(StoreInst *SI, Instruction *P, const LoadInst *LI);
77 
78   void eraseInstruction(Instruction *I);
79   bool iterateOnFunction(Function &F);
80 };
81 
82 } // end namespace llvm
83 
84 #endif // LLVM_TRANSFORMS_SCALAR_MEMCPYOPTIMIZER_H
85