Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/llvm/lib/Transforms/Utils/Mem2Reg.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===//
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 is a simple pass wrapper around the PromoteMemToReg function call
10
// exposed by the Utils library.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/Transforms/Utils/Mem2Reg.h"
15
#include "llvm/ADT/Statistic.h"
16
#include "llvm/Analysis/AssumptionCache.h"
17
#include "llvm/IR/BasicBlock.h"
18
#include "llvm/IR/Dominators.h"
19
#include "llvm/IR/Function.h"
20
#include "llvm/IR/Instructions.h"
21
#include "llvm/IR/PassManager.h"
22
#include "llvm/InitializePasses.h"
23
#include "llvm/Pass.h"
24
#include "llvm/Support/Casting.h"
25
#include "llvm/Transforms/Utils.h"
26
#include "llvm/Transforms/Utils/PromoteMemToReg.h"
27
#include <vector>
28
29
using namespace llvm;
30
31
#define DEBUG_TYPE "mem2reg"
32
33
STATISTIC(NumPromoted, "Number of alloca's promoted");
34
35
static bool promoteMemoryToRegister(Function &F, DominatorTree &DT,
36
0
                                    AssumptionCache &AC) {
37
0
  std::vector<AllocaInst *> Allocas;
38
0
  BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
39
0
  bool Changed = false;
40
41
0
  while (true) {
42
0
    Allocas.clear();
43
44
    // Find allocas that are safe to promote, by looking at all instructions in
45
    // the entry node
46
0
    for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
47
0
      if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
48
0
        if (isAllocaPromotable(AI))
49
0
          Allocas.push_back(AI);
50
51
0
    if (Allocas.empty())
52
0
      break;
53
54
0
    PromoteMemToReg(Allocas, DT, &AC);
55
0
    NumPromoted += Allocas.size();
56
0
    Changed = true;
57
0
  }
58
0
  return Changed;
59
0
}
60
61
0
PreservedAnalyses PromotePass::run(Function &F, FunctionAnalysisManager &AM) {
62
0
  auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
63
0
  auto &AC = AM.getResult<AssumptionAnalysis>(F);
64
0
  if (!promoteMemoryToRegister(F, DT, AC))
65
0
    return PreservedAnalyses::all();
66
67
0
  PreservedAnalyses PA;
68
0
  PA.preserveSet<CFGAnalyses>();
69
0
  return PA;
70
0
}
71
72
namespace {
73
74
struct PromoteLegacyPass : public FunctionPass {
75
  // Pass identification, replacement for typeid
76
  static char ID;
77
  bool ForcePass; /// If true, forces pass to execute, instead of skipping.
78
79
0
  PromoteLegacyPass() : FunctionPass(ID), ForcePass(false) {
80
0
    initializePromoteLegacyPassPass(*PassRegistry::getPassRegistry());
81
0
  }
82
0
  PromoteLegacyPass(bool IsForced) : FunctionPass(ID), ForcePass(IsForced) {
83
0
    initializePromoteLegacyPassPass(*PassRegistry::getPassRegistry());
84
0
  }
85
86
  // runOnFunction - To run this pass, first we calculate the alloca
87
  // instructions that are safe for promotion, then we promote each one.
88
0
  bool runOnFunction(Function &F) override {
89
0
    if (!ForcePass && skipFunction(F))
90
0
      return false;
91
92
0
    DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
93
0
    AssumptionCache &AC =
94
0
        getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
95
0
    return promoteMemoryToRegister(F, DT, AC);
96
0
  }
97
98
0
  void getAnalysisUsage(AnalysisUsage &AU) const override {
99
0
    AU.addRequired<AssumptionCacheTracker>();
100
0
    AU.addRequired<DominatorTreeWrapperPass>();
101
0
    AU.setPreservesCFG();
102
0
  }
103
};
104
105
} // end anonymous namespace
106
107
char PromoteLegacyPass::ID = 0;
108
109
0
INITIALIZE_PASS_BEGIN(PromoteLegacyPass, "mem2reg", "Promote Memory to "
110
0
                                                    "Register",
111
0
                      false, false)
112
0
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
113
0
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
114
0
INITIALIZE_PASS_END(PromoteLegacyPass, "mem2reg", "Promote Memory to Register",
115
                    false, false)
116
117
// createPromoteMemoryToRegister - Provide an entry point to create this pass.
118
0
FunctionPass *llvm::createPromoteMemoryToRegisterPass(bool IsForced) {
119
0
  return new PromoteLegacyPass(IsForced);
120
0
}