blob: 7445f9f51ce21f6b8d3df93650640b24a928398f [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"
16#include "llvm/IR/Instructions.h"
17#include "llvm/IR/IRBuilder.h"
18#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};
35}
36
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}
45}
46
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);
73 } else if (isa<Constant>(U)) {
74 llvm_unreachable("User of a constant expression was a constant!");
75 return false;
76 } else {
77 llvm_unreachable("Unknown user type of a constant expression!");
78 return false;
79 }
80 }
81
82 for (Instruction *I : WorkList) {
83 // Create the instruction equivalent of the constant expression.
84 Instruction *NewI = CE->getAsInstruction();
85
86 if (PHINode *PHI = dyn_cast<PHINode>(I)) {
87 // If PHINode uses CE, put new instruction of CE at end of the incoming
88 // block.
89 for (unsigned i = 0; i < PHI->getNumIncomingValues(); i++) {
90 if (PHI->getIncomingValue(i) == CE) {
91 NewI->insertBefore(PHI->getIncomingBlock(i)->getTerminator());
92 }
93 }
94 } else {
95 // Insert it just before the instruction that will use it.
96 NewI->insertBefore(I);
97 }
98
99 // Walk the operands of the instruction to find where the constant
100 // expression was used.
101 for (unsigned i = 0; i < I->getNumOperands(); i++) {
102 if (CE == I->getOperand(i)) {
103 I->setOperand(i, NewI);
104 break;
105 }
106 }
107 }
108
109 CE->destroyConstant();
110
111 return true;
112}