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