DemoteRegToStack.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
  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. #include "llvm/ADT/DenseMap.h"
  9. #include "llvm/Analysis/CFG.h"
  10. #include "llvm/Transforms/Utils/Local.h"
  11. #include "llvm/IR/Function.h"
  12. #include "llvm/IR/Instructions.h"
  13. #include "llvm/IR/Type.h"
  14. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  15. using namespace llvm;
  16. /// DemoteRegToStack - This function takes a virtual register computed by an
  17. /// Instruction and replaces it with a slot in the stack frame, allocated via
  18. /// alloca. This allows the CFG to be changed around without fear of
  19. /// invalidating the SSA information for the value. It returns the pointer to
  20. /// the alloca inserted to create a stack slot for I.
  21. AllocaInst *llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
  22. Instruction *AllocaPoint) {
  23. if (I.use_empty()) {
  24. I.eraseFromParent();
  25. return nullptr;
  26. }
  27. Function *F = I.getParent()->getParent();
  28. const DataLayout &DL = F->getParent()->getDataLayout();
  29. // Create a stack slot to hold the value.
  30. AllocaInst *Slot;
  31. if (AllocaPoint) {
  32. Slot = new AllocaInst(I.getType(), DL.getAllocaAddrSpace(), nullptr,
  33. I.getName()+".reg2mem", AllocaPoint);
  34. } else {
  35. Slot = new AllocaInst(I.getType(), DL.getAllocaAddrSpace(), nullptr,
  36. I.getName() + ".reg2mem", &F->getEntryBlock().front());
  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(I.getType(), Slot, I.getName() + ".reload",
  70. VolatileLoads,
  71. PN->getIncomingBlock(i)->getTerminator());
  72. }
  73. PN->setIncomingValue(i, V);
  74. }
  75. } else {
  76. // If this is a normal instruction, just insert a load.
  77. Value *V = new LoadInst(I.getType(), Slot, I.getName() + ".reload",
  78. VolatileLoads, U);
  79. U->replaceUsesOfWith(&I, V);
  80. }
  81. }
  82. // Insert stores of the computed value into the stack slot. We have to be
  83. // careful if I is an invoke instruction, because we can't insert the store
  84. // AFTER the terminator instruction.
  85. BasicBlock::iterator InsertPt;
  86. if (!I.isTerminator()) {
  87. InsertPt = ++I.getIterator();
  88. for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
  89. /* empty */; // Don't insert before PHI nodes or landingpad instrs.
  90. } else {
  91. InvokeInst &II = cast<InvokeInst>(I);
  92. InsertPt = II.getNormalDest()->getFirstInsertionPt();
  93. }
  94. new StoreInst(&I, Slot, &*InsertPt);
  95. return Slot;
  96. }
  97. /// DemotePHIToStack - This function takes a virtual register computed by a PHI
  98. /// node and replaces it with a slot in the stack frame allocated via alloca.
  99. /// The PHI node is deleted. It returns the pointer to the alloca inserted.
  100. AllocaInst *llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
  101. if (P->use_empty()) {
  102. P->eraseFromParent();
  103. return nullptr;
  104. }
  105. const DataLayout &DL = P->getModule()->getDataLayout();
  106. // Create a stack slot to hold the value.
  107. AllocaInst *Slot;
  108. if (AllocaPoint) {
  109. Slot = new AllocaInst(P->getType(), DL.getAllocaAddrSpace(), nullptr,
  110. P->getName()+".reg2mem", AllocaPoint);
  111. } else {
  112. Function *F = P->getParent()->getParent();
  113. Slot = new AllocaInst(P->getType(), DL.getAllocaAddrSpace(), nullptr,
  114. P->getName() + ".reg2mem",
  115. &F->getEntryBlock().front());
  116. }
  117. // Iterate over each operand inserting a store in each predecessor.
  118. for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
  119. if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
  120. assert(II->getParent() != P->getIncomingBlock(i) &&
  121. "Invoke edge not supported yet"); (void)II;
  122. }
  123. new StoreInst(P->getIncomingValue(i), Slot,
  124. P->getIncomingBlock(i)->getTerminator());
  125. }
  126. // Insert a load in place of the PHI and replace all uses.
  127. BasicBlock::iterator InsertPt = P->getIterator();
  128. for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
  129. /* empty */; // Don't insert before PHI nodes or landingpad instrs.
  130. Value *V =
  131. new LoadInst(P->getType(), Slot, P->getName() + ".reload", &*InsertPt);
  132. P->replaceAllUsesWith(V);
  133. // Delete PHI.
  134. P->eraseFromParent();
  135. return Slot;
  136. }