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