1 // Copyright (c) 2017 The Khronos Group Inc. 2 // Copyright (c) 2017 Valve Corporation 3 // Copyright (c) 2017 LunarG Inc. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #include "source/opt/inline_exhaustive_pass.h" 18 19 #include <utility> 20 21 namespace spvtools { 22 namespace opt { 23 InlineExhaustive(Function * func)24bool InlineExhaustivePass::InlineExhaustive(Function* func) { 25 bool modified = false; 26 // Using block iterators here because of block erasures and insertions. 27 for (auto bi = func->begin(); bi != func->end(); ++bi) { 28 for (auto ii = bi->begin(); ii != bi->end();) { 29 if (IsInlinableFunctionCall(&*ii)) { 30 // Inline call. 31 std::vector<std::unique_ptr<BasicBlock>> newBlocks; 32 std::vector<std::unique_ptr<Instruction>> newVars; 33 GenInlineCode(&newBlocks, &newVars, ii, bi); 34 // If call block is replaced with more than one block, point 35 // succeeding phis at new last block. 36 if (newBlocks.size() > 1) UpdateSucceedingPhis(newBlocks); 37 // Replace old calling block with new block(s). 38 39 // We need to kill the name and decorations for the call, which 40 // will be deleted. Other instructions in the block will be moved to 41 // newBlocks. We don't need to do anything with those. 42 context()->KillNamesAndDecorates(&*ii); 43 44 bi = bi.Erase(); 45 46 for (auto& bb : newBlocks) { 47 bb->SetParent(func); 48 } 49 bi = bi.InsertBefore(&newBlocks); 50 // Insert new function variables. 51 if (newVars.size() > 0) 52 func->begin()->begin().InsertBefore(std::move(newVars)); 53 // Restart inlining at beginning of calling block. 54 ii = bi->begin(); 55 modified = true; 56 } else { 57 ++ii; 58 } 59 } 60 } 61 return modified; 62 } 63 ProcessImpl()64Pass::Status InlineExhaustivePass::ProcessImpl() { 65 // Attempt exhaustive inlining on each entry point function in module 66 ProcessFunction pfn = [this](Function* fp) { return InlineExhaustive(fp); }; 67 bool modified = context()->ProcessEntryPointCallTree(pfn); 68 return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange; 69 } 70 71 InlineExhaustivePass::InlineExhaustivePass() = default; 72 Process()73Pass::Status InlineExhaustivePass::Process() { 74 InitializeInline(); 75 return ProcessImpl(); 76 } 77 78 } // namespace opt 79 } // namespace spvtools 80