llvm.org GIT mirror llvm / 0a51771
[PM] Port ConstantMerge to the new pass manager. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268582 91177308-0d34-0410-b5e6-96231b3b80d8 Davide Italiano 3 years ago
8 changed file(s) with 76 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
9595 void initializeCFGViewerPass(PassRegistry&);
9696 void initializeConstantHoistingPass(PassRegistry&);
9797 void initializeCodeGenPreparePass(PassRegistry&);
98 void initializeConstantMergePass(PassRegistry&);
98 void initializeConstantMergeLegacyPassPass(PassRegistry &);
9999 void initializeConstantPropagationPass(PassRegistry&);
100100 void initializeMachineCopyPropagationPass(PassRegistry&);
101101 void initializeCostModelAnalysisPass(PassRegistry&);
0 //===- ConstantMerge.h - Merge duplicate global constants -------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the interface to a pass that merges duplicate global
10 // constants together into a single constant that is shared. This is useful
11 // because some passes (ie TraceValues) insert a lot of string constants into
12 // the program, regardless of whether or not an existing string is available.
13 //
14 // Algorithm: ConstantMerge is designed to build up a map of available constants
15 // and eliminate duplicates when it is initialized.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_TRANSFORMS_IPO_CONSTANTMERGE_H
20 #define LLVM_TRANSFORMS_IPO_CONSTANTMERGE_H
21
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/PassManager.h"
24
25 namespace llvm {
26
27 /// A pass that merges duplicate global constants into a single constant.
28 class ConstantMergePass : public PassInfoMixin {
29 public:
30 PreservedAnalyses run(Module &M);
31 };
32 }
33
34 #endif // LLVM_TRANSFORMS_IPO_CONSTANTMERGE_H
9898 initializeInternalizeLegacyPassPass(R);
9999 initializeIPSCCPPass(R);
100100 initializeGlobalOptLegacyPassPass(R);
101 initializeConstantMergePass(R);
101 initializeConstantMergeLegacyPassPass(R);
102102 initializeDAHPass(R);
103103 initializeInstructionCombiningPassPass(R);
104104 initializeSimpleInlinerPass(R);
4444 #include "llvm/Support/Debug.h"
4545 #include "llvm/Support/Regex.h"
4646 #include "llvm/Target/TargetMachine.h"
47 #include "llvm/Transforms/IPO/ConstantMerge.h"
4748 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
4849 #include "llvm/Transforms/IPO/FunctionAttrs.h"
4950 #include "llvm/Transforms/IPO/GlobalDCE.h"
5657 #include "llvm/Transforms/Scalar/ADCE.h"
5758 #include "llvm/Transforms/Scalar/DCE.h"
5859 #include "llvm/Transforms/Scalar/EarlyCSE.h"
60 #include "llvm/Transforms/Scalar/GVN.h"
5961 #include "llvm/Transforms/Scalar/LoopRotation.h"
6062 #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
6163 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
62 #include "llvm/Transforms/Scalar/GVN.h"
6364 #include "llvm/Transforms/Scalar/Reassociate.h"
6465 #include "llvm/Transforms/Scalar/SROA.h"
6566 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
3434 #ifndef MODULE_PASS
3535 #define MODULE_PASS(NAME, CREATE_PASS)
3636 #endif
37 MODULE_PASS("constmerge", ConstantMergePass())
3738 MODULE_PASS("forceattrs", ForceFunctionAttrsPass())
3839 MODULE_PASS("globaldce", GlobalDCEPass())
3940 MODULE_PASS("globalopt", GlobalOptPass())
1616 //
1717 //===----------------------------------------------------------------------===//
1818
19 #include "llvm/Transforms/IPO.h"
19 #include "llvm/Transforms/IPO/ConstantMerge.h"
2020 #include "llvm/ADT/DenseMap.h"
2121 #include "llvm/ADT/PointerIntPair.h"
2222 #include "llvm/ADT/SmallPtrSet.h"
2727 #include "llvm/IR/Module.h"
2828 #include "llvm/IR/Operator.h"
2929 #include "llvm/Pass.h"
30 #include "llvm/Transforms/IPO.h"
3031 using namespace llvm;
3132
3233 #define DEBUG_TYPE "constmerge"
3334
3435 STATISTIC(NumMerged, "Number of global constants merged");
35
36 namespace {
37 struct ConstantMerge : public ModulePass {
38 static char ID; // Pass identification, replacement for typeid
39 ConstantMerge() : ModulePass(ID) {
40 initializeConstantMergePass(*PassRegistry::getPassRegistry());
41 }
42
43 // For this pass, process all of the globals in the module, eliminating
44 // duplicate constants.
45 bool runOnModule(Module &M) override;
46 };
47 }
48
49 char ConstantMerge::ID = 0;
50 INITIALIZE_PASS(ConstantMerge, "constmerge",
51 "Merge Duplicate Global Constants", false, false)
52
53 ModulePass *llvm::createConstantMergePass() { return new ConstantMerge(); }
54
55
5636
5737 /// Find values that are marked as llvm.used.
5838 static void FindUsedValues(GlobalVariable *LLVMUsed,
8666 return GV->getParent()->getDataLayout().getPreferredAlignment(GV);
8767 }
8868
89 bool ConstantMerge::runOnModule(Module &M) {
90 if (skipModule(M))
91 return false;
92
69 static bool mergeConstants(Module &M) {
9370 // Find all the globals that are marked "used". These cannot be merged.
9471 SmallPtrSet UsedGlobals;
9572 FindUsedValues(M.getGlobalVariable("llvm.used"), UsedGlobals);
213190 Replacements.clear();
214191 }
215192 }
193
194 PreservedAnalyses ConstantMergePass::run(Module &M) {
195 if (!mergeConstants(M))
196 return PreservedAnalyses::all();
197 return PreservedAnalyses::none();
198 }
199
200 namespace {
201 struct ConstantMergeLegacyPass : public ModulePass {
202 static char ID; // Pass identification, replacement for typeid
203 ConstantMergeLegacyPass() : ModulePass(ID) {
204 initializeConstantMergeLegacyPassPass(*PassRegistry::getPassRegistry());
205 }
206
207 // For this pass, process all of the globals in the module, eliminating
208 // duplicate constants.
209 bool runOnModule(Module &M) {
210 if (skipModule(M))
211 return false;
212 return mergeConstants(M);
213 }
214 };
215 }
216
217 char ConstantMergeLegacyPass::ID = 0;
218 INITIALIZE_PASS(ConstantMergeLegacyPass, "constmerge",
219 "Merge Duplicate Global Constants", false, false)
220
221 ModulePass *llvm::createConstantMergePass() {
222 return new ConstantMergeLegacyPass();
223 }
2323
2424 void llvm::initializeIPO(PassRegistry &Registry) {
2525 initializeArgPromotionPass(Registry);
26 initializeConstantMergePass(Registry);
26 initializeConstantMergeLegacyPassPass(Registry);
2727 initializeCrossDSOCFIPass(Registry);
2828 initializeDAEPass(Registry);
2929 initializeDAHPass(Registry);
None ; RUN: opt -constmerge -S < %s | FileCheck %s
0 ; RUN: opt -S < %s -passes=constmerge | FileCheck %s
11 ; Test that in one run var3 is merged into var2 and var1 into var4.
22 ; Test that we merge @var5 and @var6 into one with the higher alignment
33