Reg2Mem.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //===- Reg2Mem.cpp - Convert registers to allocas -------------------------===//
  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 file demotes all registers to memory references. It is intended to be
  10. // the inverse of PromoteMemoryToRegister. By converting to loads, the only
  11. // values live across basic blocks are allocas and loads before phi nodes.
  12. // It is intended that this should make CFG hacking much easier.
  13. // To make later hacking easier, the entry block is split into two, such that
  14. // all introduced allocas and nothing else are in the entry block.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "llvm/Transforms/Scalar/Reg2Mem.h"
  18. #include "llvm/ADT/Statistic.h"
  19. #include "llvm/Analysis/LoopInfo.h"
  20. #include "llvm/IR/BasicBlock.h"
  21. #include "llvm/IR/CFG.h"
  22. #include "llvm/IR/Dominators.h"
  23. #include "llvm/IR/Function.h"
  24. #include "llvm/IR/InstIterator.h"
  25. #include "llvm/IR/Instructions.h"
  26. #include "llvm/IR/LLVMContext.h"
  27. #include "llvm/IR/Module.h"
  28. #include "llvm/IR/PassManager.h"
  29. #include "llvm/InitializePasses.h"
  30. #include "llvm/Pass.h"
  31. #include "llvm/Transforms/Scalar.h"
  32. #include "llvm/Transforms/Utils.h"
  33. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  34. #include "llvm/Transforms/Utils/Local.h"
  35. #include <list>
  36. using namespace llvm;
  37. #define DEBUG_TYPE "reg2mem"
  38. STATISTIC(NumRegsDemoted, "Number of registers demoted");
  39. STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
  40. static bool valueEscapes(const Instruction &Inst) {
  41. const BasicBlock *BB = Inst.getParent();
  42. for (const User *U : Inst.users()) {
  43. const Instruction *UI = cast<Instruction>(U);
  44. if (UI->getParent() != BB || isa<PHINode>(UI))
  45. return true;
  46. }
  47. return false;
  48. }
  49. static bool runPass(Function &F) {
  50. // Insert all new allocas into entry block.
  51. BasicBlock *BBEntry = &F.getEntryBlock();
  52. assert(pred_empty(BBEntry) &&
  53. "Entry block to function must not have predecessors!");
  54. // Find first non-alloca instruction and create insertion point. This is
  55. // safe if block is well-formed: it always have terminator, otherwise
  56. // we'll get and assertion.
  57. BasicBlock::iterator I = BBEntry->begin();
  58. while (isa<AllocaInst>(I)) ++I;
  59. CastInst *AllocaInsertionPoint = new BitCastInst(
  60. Constant::getNullValue(Type::getInt32Ty(F.getContext())),
  61. Type::getInt32Ty(F.getContext()), "reg2mem alloca point", &*I);
  62. // Find the escaped instructions. But don't create stack slots for
  63. // allocas in entry block.
  64. std::list<Instruction*> WorkList;
  65. for (Instruction &I : instructions(F))
  66. if (!(isa<AllocaInst>(I) && I.getParent() == BBEntry) && valueEscapes(I))
  67. WorkList.push_front(&I);
  68. // Demote escaped instructions
  69. NumRegsDemoted += WorkList.size();
  70. for (Instruction *I : WorkList)
  71. DemoteRegToStack(*I, false, AllocaInsertionPoint);
  72. WorkList.clear();
  73. // Find all phi's
  74. for (BasicBlock &BB : F)
  75. for (auto &Phi : BB.phis())
  76. WorkList.push_front(&Phi);
  77. // Demote phi nodes
  78. NumPhisDemoted += WorkList.size();
  79. for (Instruction *I : WorkList)
  80. DemotePHIToStack(cast<PHINode>(I), AllocaInsertionPoint);
  81. return true;
  82. }
  83. PreservedAnalyses RegToMemPass::run(Function &F, FunctionAnalysisManager &AM) {
  84. auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
  85. auto *LI = &AM.getResult<LoopAnalysis>(F);
  86. unsigned N = SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI));
  87. bool Changed = runPass(F);
  88. if (N == 0 && !Changed)
  89. return PreservedAnalyses::all();
  90. PreservedAnalyses PA;
  91. PA.preserve<DominatorTreeAnalysis>();
  92. PA.preserve<LoopAnalysis>();
  93. return PA;
  94. }
  95. namespace {
  96. struct RegToMemLegacy : public FunctionPass {
  97. static char ID; // Pass identification, replacement for typeid
  98. RegToMemLegacy() : FunctionPass(ID) {
  99. initializeRegToMemLegacyPass(*PassRegistry::getPassRegistry());
  100. }
  101. void getAnalysisUsage(AnalysisUsage &AU) const override {
  102. AU.addRequiredID(BreakCriticalEdgesID);
  103. AU.addPreservedID(BreakCriticalEdgesID);
  104. }
  105. bool runOnFunction(Function &F) override {
  106. if (F.isDeclaration() || skipFunction(F))
  107. return false;
  108. return runPass(F);
  109. }
  110. };
  111. } // namespace
  112. char RegToMemLegacy::ID = 0;
  113. INITIALIZE_PASS_BEGIN(RegToMemLegacy, "reg2mem",
  114. "Demote all values to stack slots", false, false)
  115. INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
  116. INITIALIZE_PASS_END(RegToMemLegacy, "reg2mem",
  117. "Demote all values to stack slots", false, false)
  118. // createDemoteRegisterToMemory - Provide an entry point to create this pass.
  119. char &llvm::DemoteRegisterToMemoryID = RegToMemLegacy::ID;
  120. FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
  121. return new RegToMemLegacy();
  122. }