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