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