blob: cdf25eda70102b52d8cbb014c49d971187003bf9 [file] [log] [blame]
David Neto22f144c2017-06-12 14:26:21 -04001// Copyright 2017 The Clspv Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
David Neto118188e2018-08-24 11:27:54 -040015#include "llvm/IR/Constants.h"
David Neto118188e2018-08-24 11:27:54 -040016#include "llvm/IR/IRBuilder.h"
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040017#include "llvm/IR/Instructions.h"
David Neto118188e2018-08-24 11:27:54 -040018#include "llvm/IR/Module.h"
19#include "llvm/Pass.h"
20#include "llvm/Transforms/Utils/Cloning.h"
David Neto22f144c2017-06-12 14:26:21 -040021
22using namespace llvm;
23
24#define DEBUG_TYPE "UndoGetElementPtrConstantExpr"
25
26namespace {
27struct UndoGetElementPtrConstantExprPass : public ModulePass {
28 static char ID;
29 UndoGetElementPtrConstantExprPass() : ModulePass(ID) {}
30
31 bool runOnModule(Module &M) override;
32
33 bool replaceGetElementPtrConstantExpr(ConstantExpr *CE);
34};
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040035} // namespace
David Neto22f144c2017-06-12 14:26:21 -040036
37char UndoGetElementPtrConstantExprPass::ID = 0;
38static RegisterPass<UndoGetElementPtrConstantExprPass>
39 X("UndoGetElementPtrConstantExpr", "Undo GEP Constant Expr Pass");
40
41namespace clspv {
42ModulePass *createUndoGetElementPtrConstantExprPass() {
43 return new UndoGetElementPtrConstantExprPass();
44}
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040045} // namespace clspv
David Neto22f144c2017-06-12 14:26:21 -040046
47bool UndoGetElementPtrConstantExprPass::runOnModule(Module &M) {
48 bool changed = false;
49
50 for (GlobalVariable &GV : M.globals()) {
51 // Walk the users of the global variable.
52 const SmallVector<User *, 8> GVUsers(GV.user_begin(), GV.user_end());
53 for (User *U : GVUsers) {
54 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
55 if (Instruction::GetElementPtr == CE->getOpcode()) {
56 changed |= replaceGetElementPtrConstantExpr(CE);
57 }
58 }
59 }
60 }
61
62 return changed;
63}
64
65bool UndoGetElementPtrConstantExprPass::replaceGetElementPtrConstantExpr(
66 ConstantExpr *CE) {
67 SmallVector<Instruction *, 8> WorkList;
68
69 // Walk the users of the constant expression.
70 for (User *U : CE->users()) {
71 if (Instruction *I = dyn_cast<Instruction>(U)) {
72 WorkList.push_back(I);
David Neto22f144c2017-06-12 14:26:21 -040073 }
74 }
75
76 for (Instruction *I : WorkList) {
77 // Create the instruction equivalent of the constant expression.
78 Instruction *NewI = CE->getAsInstruction();
79
80 if (PHINode *PHI = dyn_cast<PHINode>(I)) {
81 // If PHINode uses CE, put new instruction of CE at end of the incoming
82 // block.
83 for (unsigned i = 0; i < PHI->getNumIncomingValues(); i++) {
84 if (PHI->getIncomingValue(i) == CE) {
85 NewI->insertBefore(PHI->getIncomingBlock(i)->getTerminator());
86 }
87 }
88 } else {
89 // Insert it just before the instruction that will use it.
90 NewI->insertBefore(I);
91 }
92
93 // Walk the operands of the instruction to find where the constant
94 // expression was used.
95 for (unsigned i = 0; i < I->getNumOperands(); i++) {
96 if (CE == I->getOperand(i)) {
97 I->setOperand(i, NewI);
98 break;
99 }
100 }
101 }
102
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400103 if (CE->user_empty())
104 CE->destroyConstant();
David Neto22f144c2017-06-12 14:26:21 -0400105
106 return true;
107}