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