LLVM  3.7.0
DemoteRegToStack.cpp
Go to the documentation of this file.
1 //===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "llvm/ADT/DenseMap.h"
12 #include "llvm/Analysis/CFG.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/Type.h"
17 using namespace llvm;
18 
19 /// DemoteRegToStack - This function takes a virtual register computed by an
20 /// Instruction and replaces it with a slot in the stack frame, allocated via
21 /// alloca. This allows the CFG to be changed around without fear of
22 /// invalidating the SSA information for the value. It returns the pointer to
23 /// the alloca inserted to create a stack slot for I.
25  Instruction *AllocaPoint) {
26  if (I.use_empty()) {
27  I.eraseFromParent();
28  return nullptr;
29  }
30 
31  // Create a stack slot to hold the value.
33  if (AllocaPoint) {
34  Slot = new AllocaInst(I.getType(), nullptr,
35  I.getName()+".reg2mem", AllocaPoint);
36  } else {
37  Function *F = I.getParent()->getParent();
38  Slot = new AllocaInst(I.getType(), nullptr, I.getName()+".reg2mem",
39  F->getEntryBlock().begin());
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.
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(Slot, I.getName()+".reload", VolatileLoads,
75  PN->getIncomingBlock(i)->getTerminator());
76  }
77  PN->setIncomingValue(i, V);
78  }
79 
80  } else {
81  // If this is a normal instruction, just insert a load.
82  Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U);
83  U->replaceUsesOfWith(&I, V);
84  }
85  }
86 
87  // Insert stores of the computed value into the stack slot. We have to be
88  // careful if I is an invoke instruction, because we can't insert the store
89  // AFTER the terminator instruction.
90  BasicBlock::iterator InsertPt;
91  if (!isa<TerminatorInst>(I)) {
92  InsertPt = &I;
93  ++InsertPt;
94  for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
95  /* empty */; // Don't insert before PHI nodes or landingpad instrs.
96  } else {
97  InvokeInst &II = cast<InvokeInst>(I);
98  InsertPt = II.getNormalDest()->getFirstInsertionPt();
99  }
100 
101  new StoreInst(&I, Slot, InsertPt);
102  return Slot;
103 }
104 
105 /// DemotePHIToStack - This function takes a virtual register computed by a PHI
106 /// node and replaces it with a slot in the stack frame allocated via alloca.
107 /// The PHI node is deleted. It returns the pointer to the alloca inserted.
109  if (P->use_empty()) {
110  P->eraseFromParent();
111  return nullptr;
112  }
113 
114  // Create a stack slot to hold the value.
115  AllocaInst *Slot;
116  if (AllocaPoint) {
117  Slot = new AllocaInst(P->getType(), nullptr,
118  P->getName()+".reg2mem", AllocaPoint);
119  } else {
120  Function *F = P->getParent()->getParent();
121  Slot = new AllocaInst(P->getType(), nullptr, P->getName()+".reg2mem",
122  F->getEntryBlock().begin());
123  }
124 
125  // Iterate over each operand inserting a store in each predecessor.
126  for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
127  if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
128  assert(II->getParent() != P->getIncomingBlock(i) &&
129  "Invoke edge not supported yet"); (void)II;
130  }
131  new StoreInst(P->getIncomingValue(i), Slot,
133  }
134 
135  // Insert a load in place of the PHI and replace all uses.
136  BasicBlock::iterator InsertPt = P;
137 
138  for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
139  /* empty */; // Don't insert before PHI nodes or landingpad instrs.
140 
141  Value *V = new LoadInst(Slot, P->getName()+".reload", InsertPt);
142  P->replaceAllUsesWith(V);
143 
144  // Delete PHI.
145  P->eraseFromParent();
146  return Slot;
147 }
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
Definition: Instruction.cpp:70
AllocaInst * DemoteRegToStack(Instruction &X, bool VolatileLoads=false, Instruction *AllocaPoint=nullptr)
DemoteRegToStack - This function takes a virtual register computed by an Instruction and replaces it ...
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:111
F(f)
unsigned GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ)
Search for the specified successor of basic block BB and return its position in the terminator instru...
Definition: CFG.cpp:72
LoadInst - an instruction for reading from memory.
Definition: Instructions.h:177
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:231
load Combine Adjacent Loads
StoreInst - an instruction for storing to memory.
Definition: Instructions.h:316
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
BasicBlock * getNormalDest() const
unsigned getNumIncomingValues() const
getNumIncomingValues - Return the number of incoming edges
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:24
BasicBlock * SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
SplitCriticalEdge - If this edge is a critical edge, insert a new node to split the critical edge...
#define P(N)
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
BasicBlock * getIncomingBlock(unsigned i) const
getIncomingBlock - Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
getIncomingValue - Return incoming value number x
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
Instruction * user_back()
user_back - Specialize the methods defined in Value, as we know that an instruction can only be used ...
Definition: Instruction.h:69
const BasicBlock & getEntryBlock() const
Definition: Function.h:442
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
bool use_empty() const
Definition: Value.h:275
AllocaInst * DemotePHIToStack(PHINode *P, Instruction *AllocaPoint=nullptr)
DemotePHIToStack - This function takes a virtual register computed by a phi node and replaces it with...
LLVM Value Representation.
Definition: Value.h:69
InvokeInst - Invoke instruction.
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:194
const BasicBlock * getParent() const
Definition: Instruction.h:72
AllocaInst - an instruction to allocate memory on the stack.
Definition: Instructions.h:76
bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.
Definition: CFG.cpp:87