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