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