Files
revng-revng/lib/EarlyFunctionAnalysis/PromoteGlobalToLocalVars.cpp
Alessandro Di Federico a65ccc3413 Introduce ValueMaterializer
`ValueMaterializer` is a rewrite of what was called `AdvancedValueInfo`
which follows the same principles.

The main benefits over the old version is:

* We materialize the data-flow graph and the CFG of the relevant part of
  root. This makes debugging significantly easier.
* We drop the old MonotoneFramework infrastructure in favor of
  getMaximalFixedPoint.
* We significantly reduce the amount of queries we make to
  AdvancedValueInfo.
2023-06-30 13:39:22 +02:00

53 lines
1.6 KiB
C++

/// \file PromoteGlobalToLocalVars.cpp
/// \brief Promote CSVs in form of global variables to local variables.
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include "llvm/IR/IRBuilder.h"
#include "revng/EarlyFunctionAnalysis/PromoteGlobalToLocalVars.h"
#include "revng/Support/IRHelpers.h"
#include "revng/Support/RegisterClobberer.h"
using namespace llvm;
llvm::PreservedAnalyses
PromoteGlobalToLocalPass::run(llvm::Function &F,
llvm::FunctionAnalysisManager &FAM) {
// Collect the CSVs used by the current function.
std::map<GlobalVariable *, Value *> CSVMap;
for (auto &BB : F) {
for (auto &I : BB) {
Value *Pointer = nullptr;
if (auto *Load = dyn_cast<LoadInst>(&I))
Pointer = skipCasts(Load->getPointerOperand());
else if (auto *Store = dyn_cast<StoreInst>(&I))
Pointer = skipCasts(Store->getPointerOperand());
else
continue;
if (auto *CSV = dyn_cast_or_null<GlobalVariable>(Pointer))
CSVMap.try_emplace(CSV);
}
}
// Create an equivalent local variable, replace all the uses of the CSV.
IRBuilder<> Builder(&F.getEntryBlock().front());
for (const auto &[CSV, _] : CSVMap) {
auto *CSVTy = CSV->getValueType();
auto *Alloca = Builder.CreateAlloca(CSVTy, nullptr, CSV->getName());
replaceAllUsesInFunctionWith(&F, CSV, Alloca);
CSVMap[CSV] = Alloca;
}
// Load all the CSVs and store their value onto the local variables.
for (const auto &[CSV, Alloca] : CSVMap)
Builder.CreateStore(createLoad(Builder, CSV), Alloca);
return PreservedAnalyses::none();
}