Coverage Report

Created: 2019-07-24 05:18

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