blob: 520f4d43ffd7b888fb429f4408198520a36284b2 [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/Module.h"
18#include "llvm/Pass.h"
19#include "llvm/Support/raw_ostream.h"
20#include "llvm/Transforms/Utils/Cloning.h"
David Neto22f144c2017-06-12 14:26:21 -040021
Diego Novilloa4c44fa2019-04-11 10:56:15 -040022#include "Passes.h"
23
David Neto22f144c2017-06-12 14:26:21 -040024using namespace llvm;
25
26#define DEBUG_TYPE "UndoTranslateSamplerFold"
27
28namespace {
29struct UndoTranslateSamplerFoldPass : public ModulePass {
30 static char ID;
31 UndoTranslateSamplerFoldPass() : ModulePass(ID) {}
32
33 bool runOnModule(Module &M) override;
34};
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040035} // namespace
David Neto22f144c2017-06-12 14:26:21 -040036
37char UndoTranslateSamplerFoldPass::ID = 0;
Diego Novilloa4c44fa2019-04-11 10:56:15 -040038INITIALIZE_PASS(UndoTranslateSamplerFoldPass, "UndoTranslateSamplerFold",
39 "Undo Transplate Sampler Fold Pass", false, false)
David Neto22f144c2017-06-12 14:26:21 -040040
41namespace clspv {
42ModulePass *createUndoTranslateSamplerFoldPass() {
43 return new UndoTranslateSamplerFoldPass();
44}
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040045} // namespace clspv
David Neto22f144c2017-06-12 14:26:21 -040046
47bool UndoTranslateSamplerFoldPass::runOnModule(Module &M) {
48 auto F = M.getFunction("__translate_sampler_initializer");
49
50 if (!F) {
51 return false;
52 }
53
54 SmallVector<CallInst *, 1> BadCalls;
55
56 for (auto U : F->users()) {
57 if (auto CI = dyn_cast<CallInst>(U)) {
58 // Get the single argument to the translate sampler function.
59 auto Arg = CI->getArgOperand(0);
60
61 if (isa<ConstantInt>(Arg)) {
62 continue;
63 } else if (isa<SelectInst>(Arg)) {
64 BadCalls.push_back(CI);
65 } else {
66 Arg->print(errs());
67 llvm_unreachable(
68 "Unhandled argument to __translate_sampler_initializer!");
69 }
70 }
71 }
72
73 if (0 == BadCalls.size()) {
74 return false;
75 }
76
77 for (auto CI : BadCalls) {
78 // Get the single argument to the translate sampler function.
79 auto Arg = CI->getArgOperand(0);
80
81 if (auto Sel = dyn_cast<SelectInst>(Arg)) {
82 auto NewTrue = CallInst::Create(CI->getCalledFunction(),
83 Sel->getTrueValue(), "", CI);
84 auto NewFalse = CallInst::Create(CI->getCalledFunction(),
85 Sel->getFalseValue(), "", CI);
86 auto NewSel =
87 SelectInst::Create(Sel->getCondition(), NewTrue, NewFalse, "", CI);
88
89 CI->replaceAllUsesWith(NewSel);
90 CI->eraseFromParent();
91 Sel->eraseFromParent();
92 }
93 }
94
95 return true;
96}