Coverage Report

Created: 2017-11-21 16:49

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/Transform/RewriteByReferenceParameters.cpp
Line
Count
Source (jump to first uncovered line)
1
//===------ RewriteByReferenceParameters.cpp --------------------*- C++ -*-===//
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
// This pass introduces separate 'alloca' instructions for read-only
11
// by-reference function parameters to indicate that these paramters are
12
// read-only. After this transformation -mem2reg has more freedom to promote
13
// variables to registers, which allows SCEV to work in more cases.
14
//
15
//===----------------------------------------------------------------------===//
16
17
#include "polly/LinkAllPasses.h"
18
#include "llvm/IR/Instruction.h"
19
#include "llvm/IR/Instructions.h"
20
#include "llvm/IR/PassManager.h"
21
22
#define DEBUG_TYPE "polly-rewrite-byref-params"
23
24
using namespace llvm;
25
26
namespace {
27
28
class RewriteByrefParams : public FunctionPass {
29
private:
30
  RewriteByrefParams(const RewriteByrefParams &) = delete;
31
  const RewriteByrefParams &operator=(const RewriteByrefParams &) = delete;
32
33
public:
34
  static char ID;
35
0
  explicit RewriteByrefParams() : FunctionPass(ID) {}
36
37
0
  virtual void getAnalysisUsage(AnalysisUsage &AU) const override {}
38
39
0
  void tryRewriteInstruction(Instruction &Inst) {
40
0
    BasicBlock *Entry = &Inst.getParent()->getParent()->getEntryBlock();
41
0
42
0
    auto *Call = dyn_cast<CallInst>(&Inst);
43
0
44
0
    if (!Call)
45
0
      return;
46
0
47
0
    llvm::Function *F = Call->getCalledFunction();
48
0
49
0
    if (!F)
50
0
      return;
51
0
52
0
    // We currently match for a very specific function. In case this proves
53
0
    // useful, we can make this code dependent on readonly metadata.
54
0
    if (!F->hasName() || F->getName() != "_gfortran_transfer_integer_write")
55
0
      return;
56
0
57
0
    auto *BitCast = dyn_cast<BitCastInst>(Call->getOperand(1));
58
0
59
0
    if (!BitCast)
60
0
      return;
61
0
62
0
    auto *Alloca = dyn_cast<AllocaInst>(BitCast->getOperand(0));
63
0
64
0
    if (!Alloca)
65
0
      return;
66
0
67
0
    std::string InstName = Alloca->getName();
68
0
69
0
    auto NewAlloca =
70
0
        new AllocaInst(Alloca->getType()->getElementType(), 0,
71
0
                       "polly_byref_alloca_" + InstName, &*Entry->begin());
72
0
73
0
    auto *LoadedVal =
74
0
        new LoadInst(Alloca, "polly_byref_load_" + InstName, &Inst);
75
0
76
0
    new StoreInst(LoadedVal, NewAlloca, &Inst);
77
0
    auto *NewBitCast = new BitCastInst(NewAlloca, BitCast->getType(),
78
0
                                       "polly_byref_cast_" + InstName, &Inst);
79
0
    Call->setOperand(1, NewBitCast);
80
0
  }
81
82
0
  virtual bool runOnFunction(Function &F) override {
83
0
    for (BasicBlock &BB : F)
84
0
      for (Instruction &Inst : BB)
85
0
        tryRewriteInstruction(Inst);
86
0
87
0
    return true;
88
0
  }
89
};
90
91
char RewriteByrefParams::ID;
92
} // anonymous namespace
93
94
0
Pass *polly::createRewriteByrefParamsPass() { return new RewriteByrefParams(); }
95
96
17.2k
INITIALIZE_PASS_BEGIN(RewriteByrefParams, "polly-rewrite-byref-params",
97
17.2k
                      "Polly - Rewrite by reference parameters", false, false)
98
17.2k
INITIALIZE_PASS_END(RewriteByrefParams, "polly-rewrite-byref-params",
99
                    "Polly - Rewrite by reference parameters", false, false)