David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1 | // 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 Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 15 | #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 Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 21 | |
alan-baker | 931d18a | 2019-12-12 08:21:32 -0500 | [diff] [blame] | 22 | #include "Constants.h" |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 23 | #include "Passes.h" |
| 24 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | #define DEBUG_TYPE "UndoTranslateSamplerFold" |
| 28 | |
| 29 | namespace { |
| 30 | struct UndoTranslateSamplerFoldPass : public ModulePass { |
| 31 | static char ID; |
| 32 | UndoTranslateSamplerFoldPass() : ModulePass(ID) {} |
| 33 | |
| 34 | bool runOnModule(Module &M) override; |
| 35 | }; |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 36 | } // namespace |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 37 | |
| 38 | char UndoTranslateSamplerFoldPass::ID = 0; |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 39 | INITIALIZE_PASS(UndoTranslateSamplerFoldPass, "UndoTranslateSamplerFold", |
| 40 | "Undo Transplate Sampler Fold Pass", false, false) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 41 | |
| 42 | namespace clspv { |
| 43 | ModulePass *createUndoTranslateSamplerFoldPass() { |
| 44 | return new UndoTranslateSamplerFoldPass(); |
| 45 | } |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 46 | } // namespace clspv |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 47 | |
| 48 | bool UndoTranslateSamplerFoldPass::runOnModule(Module &M) { |
alan-baker | 931d18a | 2019-12-12 08:21:32 -0500 | [diff] [blame] | 49 | auto F = M.getFunction(clspv::TranslateSamplerInitializerFunction()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 50 | |
| 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-baker | 931d18a | 2019-12-12 08:21:32 -0500 | [diff] [blame] | 68 | std::string msg = "Unhandled argument to "; |
| 69 | msg += clspv::TranslateSamplerInitializerFunction(); |
| 70 | llvm_unreachable(msg.c_str()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 71 | } |
| 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 | } |