DemoteRegToStack.cpp 6.8 KB

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