Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit d4b40fa

Browse files
committed
[PM] Port MergedLoadStoreMotion to the new pass manager.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@272606 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent b785f15 commit d4b40fa

File tree

8 files changed

+107
-52
lines changed

8 files changed

+107
-52
lines changed

include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ void initializeMemoryDependenceWrapperPassPass(PassRegistry&);
226226
void initializeMemorySSAWrapperPassPass(PassRegistry&);
227227
void initializeMemorySanitizerPass(PassRegistry&);
228228
void initializeMergeFunctionsPass(PassRegistry&);
229-
void initializeMergedLoadStoreMotionPass(PassRegistry &);
229+
void initializeMergedLoadStoreMotionLegacyPassPass(PassRegistry &);
230230
void initializeMetaRenamerPass(PassRegistry&);
231231
void initializeModuleDebugInfoPrinterPass(PassRegistry&);
232232
void initializeModuleSummaryIndexWrapperPassPass(PassRegistry &);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===- MergedLoadStoreMotion.cpp - merge and hoist/sink load/stores -------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
//! \file
11+
//! \brief This pass performs merges of loads and stores on both sides of a
12+
// diamond (hammock). It hoists the loads and sinks the stores.
13+
//
14+
// The algorithm iteratively hoists two loads to the same address out of a
15+
// diamond (hammock) and merges them into a single load in the header. Similar
16+
// it sinks and merges two stores to the tail block (footer). The algorithm
17+
// iterates over the instructions of one side of the diamond and attempts to
18+
// find a matching load/store on the other side. It hoists / sinks when it
19+
// thinks it safe to do so. This optimization helps with eg. hiding load
20+
// latencies, triggering if-conversion, and reducing static code size.
21+
//
22+
//===----------------------------------------------------------------------===//
23+
24+
#ifndef LLVM_TRANSFORMS_SCALAR_MERGEDLOADSTOREMOTION_H
25+
#define LLVM_TRANSFORMS_SCALAR_MERGEDLOADSTOREMOTION_H
26+
27+
#include "llvm/IR/Module.h"
28+
#include "llvm/IR/PassManager.h"
29+
30+
namespace llvm {
31+
class MergedLoadStoreMotionPass
32+
: public PassInfoMixin<MergedLoadStoreMotionPass> {
33+
public:
34+
PreservedAnalyses run(Function &F, AnalysisManager<Function> &AM);
35+
};
36+
}
37+
38+
#endif // LLVM_TRANSFORMS_SCALAR_MERGEDLOADSTOREMOTION_H

lib/LTO/LTOCodeGenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void LTOCodeGenerator::initializeLTOPasses() {
125125
initializeReversePostOrderFunctionAttrsLegacyPassPass(R);
126126
initializeGlobalsAAWrapperPassPass(R);
127127
initializeLICMPass(R);
128-
initializeMergedLoadStoreMotionPass(R);
128+
initializeMergedLoadStoreMotionLegacyPassPass(R);
129129
initializeGVNLegacyPassPass(R);
130130
initializeMemCpyOptPass(R);
131131
initializeDCELegacyPassPass(R);

lib/Passes/PassBuilder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,14 @@
7272
#include "llvm/Transforms/Scalar/DCE.h"
7373
#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
7474
#include "llvm/Transforms/Scalar/EarlyCSE.h"
75-
#include "llvm/Transforms/Scalar/GuardWidening.h"
7675
#include "llvm/Transforms/Scalar/GVN.h"
76+
#include "llvm/Transforms/Scalar/GuardWidening.h"
7777
#include "llvm/Transforms/Scalar/IndVarSimplify.h"
7878
#include "llvm/Transforms/Scalar/LoopRotation.h"
7979
#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
8080
#include "llvm/Transforms/Scalar/LowerAtomic.h"
8181
#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
82+
#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
8283
#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
8384
#include "llvm/Transforms/Scalar/Reassociate.h"
8485
#include "llvm/Transforms/Scalar/SCCP.h"

lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ FUNCTION_PASS("loweratomic", LowerAtomicPass())
129129
FUNCTION_PASS("lower-expect", LowerExpectIntrinsicPass())
130130
FUNCTION_PASS("guard-widening", GuardWideningPass())
131131
FUNCTION_PASS("gvn", GVN())
132+
FUNCTION_PASS("mldst-motion", MergedLoadStoreMotionPass())
132133
FUNCTION_PASS("partially-inline-libcalls", PartiallyInlineLibCallsPass())
133134
FUNCTION_PASS("lcssa", LCSSAPass())
134135
FUNCTION_PASS("print", PrintFunctionPass(dbgs()))

lib/Transforms/Scalar/MergedLoadStoreMotion.cpp

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
//
7373
//===----------------------------------------------------------------------===//
7474

75+
#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
7576
#include "llvm/ADT/Statistic.h"
7677
#include "llvm/Analysis/AliasAnalysis.h"
7778
#include "llvm/Analysis/CFG.h"
@@ -96,52 +97,12 @@ using namespace llvm;
9697
// MergedLoadStoreMotion Pass
9798
//===----------------------------------------------------------------------===//
9899

99-
namespace {
100-
class MergedLoadStoreMotion : public FunctionPass {
101-
AliasAnalysis *AA;
102-
MemoryDependenceResults *MD;
103-
104-
public:
105-
static char ID; // Pass identification, replacement for typeid
106-
MergedLoadStoreMotion() : FunctionPass(ID), MD(nullptr) {
107-
initializeMergedLoadStoreMotionPass(*PassRegistry::getPassRegistry());
108-
}
109-
110-
bool runOnFunction(Function &F) override;
111-
112-
private:
113-
// This transformation requires dominator postdominator info
114-
void getAnalysisUsage(AnalysisUsage &AU) const override {
115-
AU.setPreservesCFG();
116-
AU.addRequired<AAResultsWrapperPass>();
117-
AU.addPreserved<GlobalsAAWrapperPass>();
118-
AU.addPreserved<MemoryDependenceWrapperPass>();
119-
}
120-
};
121-
122-
char MergedLoadStoreMotion::ID = 0;
123-
} // anonymous namespace
124-
125100
// The mergeLoad/Store algorithms could have Size0 * Size1 complexity,
126101
// where Size0 and Size1 are the #instructions on the two sides of
127102
// the diamond. The constant chosen here is arbitrary. Compiler Time
128103
// Control is enforced by the check Size0 * Size1 < MagicCompileTimeControl.
129104
const int MagicCompileTimeControl = 250;
130105

131-
///
132-
/// \brief createMergedLoadStoreMotionPass - The public interface to this file.
133-
///
134-
FunctionPass *llvm::createMergedLoadStoreMotionPass() {
135-
return new MergedLoadStoreMotion();
136-
}
137-
138-
INITIALIZE_PASS_BEGIN(MergedLoadStoreMotion, "mldst-motion",
139-
"MergedLoadStoreMotion", false, false)
140-
INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
141-
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
142-
INITIALIZE_PASS_END(MergedLoadStoreMotion, "mldst-motion",
143-
"MergedLoadStoreMotion", false, false)
144-
145106
///
146107
/// \brief Remove instruction from parent and update memory dependence analysis.
147108
///
@@ -533,14 +494,8 @@ static bool mergeStores(BasicBlock *T, AliasAnalysis *AA,
533494
///
534495
/// \brief Run the transformation for each function
535496
///
536-
bool MergedLoadStoreMotion::runOnFunction(Function &F) {
537-
if (skipFunction(F))
538-
return false;
539-
540-
auto *MDWP = getAnalysisIfAvailable<MemoryDependenceWrapperPass>();
541-
MD = MDWP ? &MDWP->getMemDep() : nullptr;
542-
AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
543-
497+
static bool runMergedLoadStoreMotion(Function &F, AliasAnalysis *AA,
498+
MemoryDependenceResults *MD) {
544499
bool Changed = false;
545500
DEBUG(dbgs() << "Instruction Merger\n");
546501

@@ -558,3 +513,61 @@ bool MergedLoadStoreMotion::runOnFunction(Function &F) {
558513
}
559514
return Changed;
560515
}
516+
517+
PreservedAnalyses
518+
MergedLoadStoreMotionPass::run(Function &F, AnalysisManager<Function> &AM) {
519+
auto &AA = AM.getResult<AAManager>(F);
520+
auto *MD = AM.getCachedResult<MemoryDependenceAnalysis>(F);
521+
if (!runMergedLoadStoreMotion(F, &AA, MD))
522+
return PreservedAnalyses::all();
523+
return PreservedAnalyses::none();
524+
}
525+
526+
namespace {
527+
class MergedLoadStoreMotionLegacyPass : public FunctionPass {
528+
AliasAnalysis *AA;
529+
MemoryDependenceResults *MD;
530+
531+
public:
532+
static char ID; // Pass identification, replacement for typeid
533+
MergedLoadStoreMotionLegacyPass() : FunctionPass(ID), MD(nullptr) {
534+
initializeMergedLoadStoreMotionLegacyPassPass(
535+
*PassRegistry::getPassRegistry());
536+
}
537+
538+
bool runOnFunction(Function &F) override {
539+
if (skipFunction(F))
540+
return false;
541+
542+
AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
543+
auto *MDWP = getAnalysisIfAvailable<MemoryDependenceWrapperPass>();
544+
MD = MDWP ? &MDWP->getMemDep() : nullptr;
545+
return runMergedLoadStoreMotion(F, AA, MD);
546+
}
547+
548+
private:
549+
// This transformation requires dominator postdominator info
550+
void getAnalysisUsage(AnalysisUsage &AU) const override {
551+
AU.setPreservesCFG();
552+
AU.addRequired<AAResultsWrapperPass>();
553+
AU.addPreserved<GlobalsAAWrapperPass>();
554+
AU.addPreserved<MemoryDependenceWrapperPass>();
555+
}
556+
};
557+
558+
char MergedLoadStoreMotionLegacyPass::ID = 0;
559+
} // anonymous namespace
560+
561+
///
562+
/// \brief createMergedLoadStoreMotionPass - The public interface to this file.
563+
///
564+
FunctionPass *llvm::createMergedLoadStoreMotionPass() {
565+
return new MergedLoadStoreMotionLegacyPass();
566+
}
567+
568+
INITIALIZE_PASS_BEGIN(MergedLoadStoreMotionLegacyPass, "mldst-motion",
569+
"MergedLoadStoreMotion", false, false)
570+
INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
571+
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
572+
INITIALIZE_PASS_END(MergedLoadStoreMotionLegacyPass, "mldst-motion",
573+
"MergedLoadStoreMotion", false, false)

lib/Transforms/Scalar/Scalar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void llvm::initializeScalarOpts(PassRegistry &Registry) {
6565
initializeLowerExpectIntrinsicPass(Registry);
6666
initializeLowerGuardIntrinsicPass(Registry);
6767
initializeMemCpyOptPass(Registry);
68-
initializeMergedLoadStoreMotionPass(Registry);
68+
initializeMergedLoadStoreMotionLegacyPassPass(Registry);
6969
initializeNaryReassociatePass(Registry);
7070
initializePartiallyInlineLibCallsLegacyPassPass(Registry);
7171
initializeReassociateLegacyPassPass(Registry);

test/Transforms/InstMerge/exceptions.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
; RUN: opt -basicaa -memdep -mldst-motion -S < %s | FileCheck %s
2+
; RUN: opt -aa-pipeline=basicaa -passes='require<memdep>',mldst-motion \
3+
; RUN: -S < %s | FileCheck %s
24
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
35
target triple = "x86_64-unknown-linux-gnu"
46

0 commit comments

Comments
 (0)