blob: de9e2744e58db6b59a8cef5d65006fabd8e0c72e [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
alan-baker931d18a2019-12-12 08:21:32 -050022#include "Constants.h"
Diego Novilloa4c44fa2019-04-11 10:56:15 -040023#include "Passes.h"
24
David Neto22f144c2017-06-12 14:26:21 -040025using namespace llvm;
26
27#define DEBUG_TYPE "UndoTranslateSamplerFold"
28
29namespace {
30struct UndoTranslateSamplerFoldPass : public ModulePass {
31 static char ID;
32 UndoTranslateSamplerFoldPass() : ModulePass(ID) {}
33
34 bool runOnModule(Module &M) override;
35};
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040036} // namespace
David Neto22f144c2017-06-12 14:26:21 -040037
38char UndoTranslateSamplerFoldPass::ID = 0;
Diego Novilloa4c44fa2019-04-11 10:56:15 -040039INITIALIZE_PASS(UndoTranslateSamplerFoldPass, "UndoTranslateSamplerFold",
40 "Undo Transplate Sampler Fold Pass", false, false)
David Neto22f144c2017-06-12 14:26:21 -040041
42namespace clspv {
43ModulePass *createUndoTranslateSamplerFoldPass() {
44 return new UndoTranslateSamplerFoldPass();
45}
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040046} // namespace clspv
David Neto22f144c2017-06-12 14:26:21 -040047
48bool UndoTranslateSamplerFoldPass::runOnModule(Module &M) {
alan-baker931d18a2019-12-12 08:21:32 -050049 auto F = M.getFunction(clspv::TranslateSamplerInitializerFunction());
David Neto22f144c2017-06-12 14:26:21 -040050
51 if (!F) {
52 return false;
53 }
54
55 SmallVector<CallInst *, 1> BadCalls;
56
57 for (auto U : F->users()) {
58 if (auto CI = dyn_cast<CallInst>(U)) {
59 // Get the single argument to the translate sampler function.
60 auto Arg = CI->getArgOperand(0);
61
62 if (isa<ConstantInt>(Arg)) {
63 continue;
64 } else if (isa<SelectInst>(Arg)) {
65 BadCalls.push_back(CI);
66 } else {
67 Arg->print(errs());
alan-baker931d18a2019-12-12 08:21:32 -050068 std::string msg = "Unhandled argument to ";
69 msg += clspv::TranslateSamplerInitializerFunction();
70 llvm_unreachable(msg.c_str());
David Neto22f144c2017-06-12 14:26:21 -040071 }
72 }
73 }
74
75 if (0 == BadCalls.size()) {
76 return false;
77 }
78
79 for (auto CI : BadCalls) {
80 // Get the single argument to the translate sampler function.
81 auto Arg = CI->getArgOperand(0);
82
83 if (auto Sel = dyn_cast<SelectInst>(Arg)) {
84 auto NewTrue = CallInst::Create(CI->getCalledFunction(),
85 Sel->getTrueValue(), "", CI);
86 auto NewFalse = CallInst::Create(CI->getCalledFunction(),
87 Sel->getFalseValue(), "", CI);
88 auto NewSel =
89 SelectInst::Create(Sel->getCondition(), NewTrue, NewFalse, "", CI);
90
91 CI->replaceAllUsesWith(NewSel);
92 CI->eraseFromParent();
93 Sel->eraseFromParent();
94 }
95 }
96
97 return true;
98}