DemoteRegToStack.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
  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. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  10. #include "llvm/ADT/DenseMap.h"
  11. #include "llvm/Analysis/CFG.h"
  12. #include "llvm/IR/Function.h"
  13. #include "llvm/IR/Instructions.h"
  14. #include "llvm/IR/Type.h"
  15. #include "llvm/Transforms/Utils/Local.h"
  16. using namespace llvm;
  17. /// DemoteRegToStack - This function takes a virtual register computed by an
  18. /// Instruction and replaces it with a slot in the stack frame, allocated via
  19. /// alloca. This allows the CFG to be changed around without fear of
  20. /// invalidating the SSA information for the value. It returns the pointer to
  21. /// the alloca inserted to create a stack slot for I.
  22. AllocaInst *llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
  23. Instruction *AllocaPoint) {
  24. if (I.use_empty()) {
  25. I.eraseFromParent();
  26. return nullptr;
  27. }
  28. // Create a stack slot to hold the value.
  29. AllocaInst *Slot;
  30. if (AllocaPoint) {
  31. Slot = new AllocaInst(I.getType(), nullptr,
  32. I.getName()+".reg2mem", AllocaPoint);
  33. } else {
  34. Function *F = I.getParent()->getParent();
  35. Slot = new AllocaInst(I.getType(), nullptr, I.getName()+".reg2mem",
  36. F->getEntryBlock().begin());
  37. }
  38. // We cannot demote invoke instructions to the stack if their normal edge
  39. // is critical. Therefore, split the critical edge and create a basic block
  40. // into which the store can be inserted.
  41. if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
  42. if (!II->getNormalDest()->getSinglePredecessor()) {
  43. unsigned SuccNum = GetSuccessorNumber(II->getParent(), II->getNormalDest());
  44. assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
  45. BasicBlock *BB = SplitCriticalEdge(II, SuccNum);
  46. assert(BB && "Unable to split critical edge.");
  47. (void)BB;
  48. }
  49. }
  50. // Change all of the users of the instruction to read from the stack slot.
  51. while (!I.use_empty()) {
  52. Instruction *U = cast<Instruction>(I.user_back());
  53. if (PHINode *PN = dyn_cast<PHINode>(U)) {
  54. // If this is a PHI node, we can't insert a load of the value before the
  55. // use. Instead insert the load in the predecessor block corresponding
  56. // to the incoming value.
  57. //
  58. // Note that if there are multiple edges from a basic block to this PHI
  59. // node that we cannot have multiple loads. The problem is that the
  60. // resulting PHI node will have multiple values (from each load) coming in
  61. // from the same block, which is illegal SSA form. For this reason, we
  62. // keep track of and reuse loads we insert.
  63. DenseMap<BasicBlock*, Value*> Loads;
  64. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
  65. if (PN->getIncomingValue(i) == &I) {
  66. Value *&V = Loads[PN->getIncomingBlock(i)];
  67. if (!V) {
  68. // Insert the load into the predecessor block
  69. V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads,
  70. PN->getIncomingBlock(i)->getTerminator());
  71. }
  72. PN->setIncomingValue(i, V);
  73. }
  74. } else {
  75. // If this is a normal instruction, just insert a load.
  76. Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U);
  77. U->replaceUsesOfWith(&I, V);
  78. }
  79. }
  80. // Insert stores of the computed value into the stack slot. We have to be
  81. // careful if I is an invoke instruction, because we can't insert the store
  82. // AFTER the terminator instruction.
  83. BasicBlock::iterator InsertPt;
  84. if (!isa<TerminatorInst>(I)) {
  85. InsertPt = &I;
  86. ++InsertPt;
  87. for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
  88. /* empty */; // Don't insert before PHI nodes or landingpad instrs.
  89. } else {
  90. InvokeInst &II = cast<InvokeInst>(I);
  91. InsertPt = II.getNormalDest()->getFirstInsertionPt();
  92. }
  93. new StoreInst(&I, Slot, InsertPt);
  94. return Slot;
  95. }
  96. /// DemotePHIToStack - This function takes a virtual register computed by a PHI
  97. /// node and replaces it with a slot in the stack frame allocated via alloca.
  98. /// The PHI node is deleted. It returns the pointer to the alloca inserted.
  99. AllocaInst *llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
  100. if (P->use_empty()) {
  101. P->eraseFromParent();
  102. return nullptr;
  103. }
  104. // Create a stack slot to hold the value.
  105. AllocaInst *Slot;
  106. if (AllocaPoint) {
  107. Slot = new AllocaInst(P->getType(), nullptr,
  108. P->getName()+".reg2mem", AllocaPoint);
  109. } else {
  110. Function *F = P->getParent()->getParent();
  111. Slot = new AllocaInst(P->getType(), nullptr, P->getName()+".reg2mem",
  112. F->getEntryBlock().begin());
  113. }
  114. // Iterate over each operand inserting a store in each predecessor.
  115. for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
  116. if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
  117. assert(II->getParent() != P->getIncomingBlock(i) &&
  118. "Invoke edge not supported yet"); (void)II;
  119. }
  120. new StoreInst(P->getIncomingValue(i), Slot,
  121. P->getIncomingBlock(i)->getTerminator());
  122. }
  123. // Insert a load in place of the PHI and replace all uses.
  124. BasicBlock::iterator InsertPt = P;
  125. for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
  126. /* empty */; // Don't insert before PHI nodes or landingpad instrs.
  127. Value *V = new LoadInst(Slot, P->getName()+".reload", InsertPt);
  128. P->replaceAllUsesWith(V);
  129. // Delete PHI.
  130. P->eraseFromParent();
  131. return Slot;
  132. }