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