10b57cec5SDimitry Andric //===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
100b57cec5SDimitry Andric #include "llvm/Analysis/CFG.h"
110b57cec5SDimitry Andric #include "llvm/IR/Function.h"
120b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
130b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
1481ad6265SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
150b57cec5SDimitry Andric using namespace llvm;
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric /// DemoteRegToStack - This function takes a virtual register computed by an
180b57cec5SDimitry Andric /// Instruction and replaces it with a slot in the stack frame, allocated via
190b57cec5SDimitry Andric /// alloca. This allows the CFG to be changed around without fear of
200b57cec5SDimitry Andric /// invalidating the SSA information for the value. It returns the pointer to
210b57cec5SDimitry Andric /// the alloca inserted to create a stack slot for I.
DemoteRegToStack(Instruction & I,bool VolatileLoads,Instruction * AllocaPoint)220b57cec5SDimitry Andric AllocaInst *llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
230b57cec5SDimitry Andric Instruction *AllocaPoint) {
240b57cec5SDimitry Andric if (I.use_empty()) {
250b57cec5SDimitry Andric I.eraseFromParent();
260b57cec5SDimitry Andric return nullptr;
270b57cec5SDimitry Andric }
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric Function *F = I.getParent()->getParent();
300b57cec5SDimitry Andric const DataLayout &DL = F->getParent()->getDataLayout();
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric // Create a stack slot to hold the value.
330b57cec5SDimitry Andric AllocaInst *Slot;
340b57cec5SDimitry Andric if (AllocaPoint) {
350b57cec5SDimitry Andric Slot = new AllocaInst(I.getType(), DL.getAllocaAddrSpace(), nullptr,
360b57cec5SDimitry Andric I.getName()+".reg2mem", AllocaPoint);
370b57cec5SDimitry Andric } else {
380b57cec5SDimitry Andric Slot = new AllocaInst(I.getType(), DL.getAllocaAddrSpace(), nullptr,
390b57cec5SDimitry Andric I.getName() + ".reg2mem", &F->getEntryBlock().front());
400b57cec5SDimitry Andric }
410b57cec5SDimitry Andric
420b57cec5SDimitry Andric // We cannot demote invoke instructions to the stack if their normal edge
430b57cec5SDimitry Andric // is critical. Therefore, split the critical edge and create a basic block
440b57cec5SDimitry Andric // into which the store can be inserted.
450b57cec5SDimitry Andric if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
460b57cec5SDimitry Andric if (!II->getNormalDest()->getSinglePredecessor()) {
470b57cec5SDimitry Andric unsigned SuccNum = GetSuccessorNumber(II->getParent(), II->getNormalDest());
480b57cec5SDimitry Andric assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
490b57cec5SDimitry Andric BasicBlock *BB = SplitCriticalEdge(II, SuccNum);
500b57cec5SDimitry Andric assert(BB && "Unable to split critical edge.");
510b57cec5SDimitry Andric (void)BB;
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric
550b57cec5SDimitry Andric // Change all of the users of the instruction to read from the stack slot.
560b57cec5SDimitry Andric while (!I.use_empty()) {
570b57cec5SDimitry Andric Instruction *U = cast<Instruction>(I.user_back());
580b57cec5SDimitry Andric if (PHINode *PN = dyn_cast<PHINode>(U)) {
590b57cec5SDimitry Andric // If this is a PHI node, we can't insert a load of the value before the
600b57cec5SDimitry Andric // use. Instead insert the load in the predecessor block corresponding
610b57cec5SDimitry Andric // to the incoming value.
620b57cec5SDimitry Andric //
630b57cec5SDimitry Andric // Note that if there are multiple edges from a basic block to this PHI
640b57cec5SDimitry Andric // node that we cannot have multiple loads. The problem is that the
650b57cec5SDimitry Andric // resulting PHI node will have multiple values (from each load) coming in
660b57cec5SDimitry Andric // from the same block, which is illegal SSA form. For this reason, we
670b57cec5SDimitry Andric // keep track of and reuse loads we insert.
680b57cec5SDimitry Andric DenseMap<BasicBlock*, Value*> Loads;
690b57cec5SDimitry Andric for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
700b57cec5SDimitry Andric if (PN->getIncomingValue(i) == &I) {
710b57cec5SDimitry Andric Value *&V = Loads[PN->getIncomingBlock(i)];
720b57cec5SDimitry Andric if (!V) {
730b57cec5SDimitry Andric // Insert the load into the predecessor block
740b57cec5SDimitry Andric V = new LoadInst(I.getType(), Slot, I.getName() + ".reload",
750b57cec5SDimitry Andric VolatileLoads,
760b57cec5SDimitry Andric PN->getIncomingBlock(i)->getTerminator());
77*fe013be4SDimitry Andric Loads[PN->getIncomingBlock(i)] = V;
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric PN->setIncomingValue(i, V);
800b57cec5SDimitry Andric }
810b57cec5SDimitry Andric
820b57cec5SDimitry Andric } else {
830b57cec5SDimitry Andric // If this is a normal instruction, just insert a load.
840b57cec5SDimitry Andric Value *V = new LoadInst(I.getType(), Slot, I.getName() + ".reload",
850b57cec5SDimitry Andric VolatileLoads, U);
860b57cec5SDimitry Andric U->replaceUsesOfWith(&I, V);
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric // Insert stores of the computed value into the stack slot. We have to be
910b57cec5SDimitry Andric // careful if I is an invoke instruction, because we can't insert the store
920b57cec5SDimitry Andric // AFTER the terminator instruction.
930b57cec5SDimitry Andric BasicBlock::iterator InsertPt;
940b57cec5SDimitry Andric if (!I.isTerminator()) {
950b57cec5SDimitry Andric InsertPt = ++I.getIterator();
96bdd1243dSDimitry Andric // Don't insert before PHI nodes or landingpad instrs.
970b57cec5SDimitry Andric for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
98bdd1243dSDimitry Andric if (isa<CatchSwitchInst>(InsertPt))
99bdd1243dSDimitry Andric break;
100bdd1243dSDimitry Andric if (isa<CatchSwitchInst>(InsertPt)) {
101bdd1243dSDimitry Andric for (BasicBlock *Handler : successors(&*InsertPt))
102bdd1243dSDimitry Andric new StoreInst(&I, Slot, &*Handler->getFirstInsertionPt());
103bdd1243dSDimitry Andric return Slot;
104bdd1243dSDimitry Andric }
1050b57cec5SDimitry Andric } else {
1060b57cec5SDimitry Andric InvokeInst &II = cast<InvokeInst>(I);
1070b57cec5SDimitry Andric InsertPt = II.getNormalDest()->getFirstInsertionPt();
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric
1100b57cec5SDimitry Andric new StoreInst(&I, Slot, &*InsertPt);
1110b57cec5SDimitry Andric return Slot;
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric
1140b57cec5SDimitry Andric /// DemotePHIToStack - This function takes a virtual register computed by a PHI
1150b57cec5SDimitry Andric /// node and replaces it with a slot in the stack frame allocated via alloca.
1160b57cec5SDimitry Andric /// The PHI node is deleted. It returns the pointer to the alloca inserted.
DemotePHIToStack(PHINode * P,Instruction * AllocaPoint)1170b57cec5SDimitry Andric AllocaInst *llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
1180b57cec5SDimitry Andric if (P->use_empty()) {
1190b57cec5SDimitry Andric P->eraseFromParent();
1200b57cec5SDimitry Andric return nullptr;
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric
1230b57cec5SDimitry Andric const DataLayout &DL = P->getModule()->getDataLayout();
1240b57cec5SDimitry Andric
1250b57cec5SDimitry Andric // Create a stack slot to hold the value.
1260b57cec5SDimitry Andric AllocaInst *Slot;
1270b57cec5SDimitry Andric if (AllocaPoint) {
1280b57cec5SDimitry Andric Slot = new AllocaInst(P->getType(), DL.getAllocaAddrSpace(), nullptr,
1290b57cec5SDimitry Andric P->getName()+".reg2mem", AllocaPoint);
1300b57cec5SDimitry Andric } else {
1310b57cec5SDimitry Andric Function *F = P->getParent()->getParent();
1320b57cec5SDimitry Andric Slot = new AllocaInst(P->getType(), DL.getAllocaAddrSpace(), nullptr,
1330b57cec5SDimitry Andric P->getName() + ".reg2mem",
1340b57cec5SDimitry Andric &F->getEntryBlock().front());
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric
1370b57cec5SDimitry Andric // Iterate over each operand inserting a store in each predecessor.
1380b57cec5SDimitry Andric for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
1390b57cec5SDimitry Andric if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
1400b57cec5SDimitry Andric assert(II->getParent() != P->getIncomingBlock(i) &&
1410b57cec5SDimitry Andric "Invoke edge not supported yet"); (void)II;
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric new StoreInst(P->getIncomingValue(i), Slot,
1440b57cec5SDimitry Andric P->getIncomingBlock(i)->getTerminator());
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric
1470b57cec5SDimitry Andric // Insert a load in place of the PHI and replace all uses.
1480b57cec5SDimitry Andric BasicBlock::iterator InsertPt = P->getIterator();
149bdd1243dSDimitry Andric // Don't insert before PHI nodes or landingpad instrs.
1500b57cec5SDimitry Andric for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
151bdd1243dSDimitry Andric if (isa<CatchSwitchInst>(InsertPt))
152bdd1243dSDimitry Andric break;
153bdd1243dSDimitry Andric if (isa<CatchSwitchInst>(InsertPt)) {
154bdd1243dSDimitry Andric // We need a separate load before each actual use of the PHI
155bdd1243dSDimitry Andric SmallVector<Instruction *, 4> Users;
156bdd1243dSDimitry Andric for (User *U : P->users()) {
157bdd1243dSDimitry Andric Instruction *User = cast<Instruction>(U);
158bdd1243dSDimitry Andric Users.push_back(User);
159bdd1243dSDimitry Andric }
160bdd1243dSDimitry Andric for (Instruction *User : Users) {
161bdd1243dSDimitry Andric Value *V =
162bdd1243dSDimitry Andric new LoadInst(P->getType(), Slot, P->getName() + ".reload", User);
163bdd1243dSDimitry Andric User->replaceUsesOfWith(P, V);
164bdd1243dSDimitry Andric }
165bdd1243dSDimitry Andric } else {
1660b57cec5SDimitry Andric Value *V =
1670b57cec5SDimitry Andric new LoadInst(P->getType(), Slot, P->getName() + ".reload", &*InsertPt);
1680b57cec5SDimitry Andric P->replaceAllUsesWith(V);
169bdd1243dSDimitry Andric }
1700b57cec5SDimitry Andric // Delete PHI.
1710b57cec5SDimitry Andric P->eraseFromParent();
1720b57cec5SDimitry Andric return Slot;
1730b57cec5SDimitry Andric }
174