Reg2Mem.cpp 4.5 KB

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