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