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