2016-11-10 17:11:50 +00:00
|
|
|
// Copyright (c) 2017 The Khronos Group Inc.
|
|
|
|
// Copyright (c) 2017 Valve Corporation
|
|
|
|
// Copyright (c) 2017 LunarG Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef SOURCE_OPT_INLINE_PASS_H_
|
|
|
|
#define SOURCE_OPT_INLINE_PASS_H_
|
2016-11-10 17:11:50 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2017-11-08 17:40:02 +00:00
|
|
|
#include <list>
|
2016-11-10 17:11:50 +00:00
|
|
|
#include <memory>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <set>
|
2016-11-10 17:11:50 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/decoration_manager.h"
|
|
|
|
#include "source/opt/module.h"
|
|
|
|
#include "source/opt/pass.h"
|
2016-11-10 17:11:50 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
|
|
|
// See optimizer.hpp for documentation.
|
|
|
|
class InlinePass : public Pass {
|
2018-07-12 19:14:43 +00:00
|
|
|
using cbb_ptr = const BasicBlock*;
|
2017-05-05 02:55:53 +00:00
|
|
|
|
2016-11-10 17:11:50 +00:00
|
|
|
public:
|
2017-08-01 23:20:13 +00:00
|
|
|
virtual ~InlinePass() = default;
|
2016-11-10 17:11:50 +00:00
|
|
|
|
2017-08-01 23:20:13 +00:00
|
|
|
protected:
|
2018-07-12 13:08:45 +00:00
|
|
|
InlinePass();
|
2016-11-10 17:11:50 +00:00
|
|
|
|
2018-12-18 19:34:03 +00:00
|
|
|
// Add pointer to type to module and return resultId. Returns 0 if the type
|
|
|
|
// could not be created.
|
2016-11-10 17:11:50 +00:00
|
|
|
uint32_t AddPointerToType(uint32_t type_id, SpvStorageClass storage_class);
|
|
|
|
|
|
|
|
// Add unconditional branch to labelId to end of block block_ptr.
|
2018-07-12 19:14:43 +00:00
|
|
|
void AddBranch(uint32_t labelId, std::unique_ptr<BasicBlock>* block_ptr);
|
2016-11-10 17:11:50 +00:00
|
|
|
|
2017-05-05 02:55:53 +00:00
|
|
|
// Add conditional branch to end of block |block_ptr|.
|
2017-11-08 17:40:02 +00:00
|
|
|
void AddBranchCond(uint32_t cond_id, uint32_t true_id, uint32_t false_id,
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<BasicBlock>* block_ptr);
|
2017-05-05 02:55:53 +00:00
|
|
|
|
|
|
|
// Add unconditional branch to labelId to end of block block_ptr.
|
|
|
|
void AddLoopMerge(uint32_t merge_id, uint32_t continue_id,
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<BasicBlock>* block_ptr);
|
2017-05-05 02:55:53 +00:00
|
|
|
|
2016-11-10 17:11:50 +00:00
|
|
|
// Add store of valId to ptrId to end of block block_ptr.
|
|
|
|
void AddStore(uint32_t ptrId, uint32_t valId,
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<BasicBlock>* block_ptr);
|
2016-11-10 17:11:50 +00:00
|
|
|
|
|
|
|
// Add load of ptrId into resultId to end of block block_ptr.
|
|
|
|
void AddLoad(uint32_t typeId, uint32_t resultId, uint32_t ptrId,
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<BasicBlock>* block_ptr);
|
2016-11-10 17:11:50 +00:00
|
|
|
|
|
|
|
// Return new label.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<Instruction> NewLabel(uint32_t label_id);
|
2016-11-10 17:11:50 +00:00
|
|
|
|
2017-05-05 02:55:53 +00:00
|
|
|
// Returns the id for the boolean false value. Looks in the module first
|
2018-12-18 19:34:03 +00:00
|
|
|
// and creates it if not found. Remembers it for future calls. Returns 0 if
|
|
|
|
// the value could not be created.
|
2017-05-05 02:55:53 +00:00
|
|
|
uint32_t GetFalseId();
|
|
|
|
|
2016-11-10 17:11:50 +00:00
|
|
|
// Map callee params to caller args
|
2018-07-12 19:14:43 +00:00
|
|
|
void MapParams(Function* calleeFn, BasicBlock::iterator call_inst_itr,
|
2016-11-10 17:11:50 +00:00
|
|
|
std::unordered_map<uint32_t, uint32_t>* callee2caller);
|
|
|
|
|
2018-12-18 19:34:03 +00:00
|
|
|
// Clone and map callee locals. Return true if successful.
|
|
|
|
bool CloneAndMapLocals(Function* calleeFn,
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<std::unique_ptr<Instruction>>* new_vars,
|
|
|
|
std::unordered_map<uint32_t, uint32_t>* callee2caller);
|
2016-11-10 17:11:50 +00:00
|
|
|
|
2018-12-18 19:34:03 +00:00
|
|
|
// Create return variable for callee clone code. The return type of
|
|
|
|
// |calleeFn| must not be void. Returns the id of the return variable if
|
|
|
|
// created. Returns 0 if the return variable could not be created.
|
2018-07-12 19:14:43 +00:00
|
|
|
uint32_t CreateReturnVar(Function* calleeFn,
|
|
|
|
std::vector<std::unique_ptr<Instruction>>* new_vars);
|
2016-11-10 17:11:50 +00:00
|
|
|
|
|
|
|
// Return true if instruction must be in the same block that its result
|
|
|
|
// is used.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool IsSameBlockOp(const Instruction* inst) const;
|
2016-11-10 17:11:50 +00:00
|
|
|
|
|
|
|
// Clone operands which must be in same block as consumer instructions.
|
|
|
|
// Look in preCallSB for instructions that need cloning. Look in
|
|
|
|
// postCallSB for instructions already cloned. Add cloned instruction
|
|
|
|
// to postCallSB.
|
2018-12-18 19:34:03 +00:00
|
|
|
bool CloneSameBlockOps(std::unique_ptr<Instruction>* inst,
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_map<uint32_t, uint32_t>* postCallSB,
|
|
|
|
std::unordered_map<uint32_t, Instruction*>* preCallSB,
|
|
|
|
std::unique_ptr<BasicBlock>* block_ptr);
|
2016-11-10 17:11:50 +00:00
|
|
|
|
|
|
|
// Return in new_blocks the result of inlining the call at call_inst_itr
|
|
|
|
// within its block at call_block_itr. The block at call_block_itr can
|
|
|
|
// just be replaced with the blocks in new_blocks. Any additional branches
|
|
|
|
// are avoided. Debug instructions are cloned along with their callee
|
|
|
|
// instructions. Early returns are replaced by a store to a local return
|
|
|
|
// variable and a branch to a (created) exit block where the local variable
|
|
|
|
// is returned. Formal parameters are trivially mapped to their actual
|
|
|
|
// parameters. Note that the first block in new_blocks retains the label
|
|
|
|
// of the original calling block. Also note that if an exit block is
|
2017-03-31 14:36:58 +00:00
|
|
|
// created, it is the last block of new_blocks.
|
2016-11-10 17:11:50 +00:00
|
|
|
//
|
|
|
|
// Also return in new_vars additional OpVariable instructions required by
|
|
|
|
// and to be inserted into the caller function after the block at
|
|
|
|
// call_block_itr is replaced with new_blocks.
|
2018-12-18 19:34:03 +00:00
|
|
|
//
|
|
|
|
// Returns true if successful.
|
|
|
|
bool GenInlineCode(std::vector<std::unique_ptr<BasicBlock>>* new_blocks,
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<std::unique_ptr<Instruction>>* new_vars,
|
|
|
|
BasicBlock::iterator call_inst_itr,
|
|
|
|
UptrVectorIterator<BasicBlock> call_block_itr);
|
2016-11-10 17:11:50 +00:00
|
|
|
|
2017-05-05 02:55:53 +00:00
|
|
|
// Return true if |inst| is a function call that can be inlined.
|
2019-07-17 18:59:05 +00:00
|
|
|
bool IsInlinableFunctionCall(const Instruction* inst);
|
2017-03-31 14:36:58 +00:00
|
|
|
|
2018-11-08 14:11:20 +00:00
|
|
|
// Return true if |func| does not have a return that is
|
|
|
|
// nested in a structured if, switch or loop.
|
|
|
|
bool HasNoReturnInStructuredConstruct(Function* func);
|
2017-05-05 02:55:53 +00:00
|
|
|
|
|
|
|
// Return true if |func| has no return in a loop. The current analysis
|
|
|
|
// requires structured control flow, so return false if control flow not
|
|
|
|
// structured ie. module is not a shader.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool HasNoReturnInLoop(Function* func);
|
2017-05-05 02:55:53 +00:00
|
|
|
|
|
|
|
// Find all functions with multiple returns and no returns in loops
|
2018-07-12 19:14:43 +00:00
|
|
|
void AnalyzeReturns(Function* func);
|
2017-05-05 02:55:53 +00:00
|
|
|
|
|
|
|
// Return true if |func| is a function that can be inlined.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool IsInlinableFunction(Function* func);
|
2017-04-25 19:57:20 +00:00
|
|
|
|
2017-08-01 23:20:13 +00:00
|
|
|
// Update phis in succeeding blocks to point to new last block
|
|
|
|
void UpdateSucceedingPhis(
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<std::unique_ptr<BasicBlock>>& new_blocks);
|
2016-11-10 17:11:50 +00:00
|
|
|
|
2017-08-15 23:58:28 +00:00
|
|
|
// Initialize state for optimization of |module|
|
2018-07-12 13:08:45 +00:00
|
|
|
void InitializeInline();
|
2016-11-10 17:11:50 +00:00
|
|
|
|
|
|
|
// Map from function's result id to function.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_map<uint32_t, Function*> id2function_;
|
2016-11-10 17:11:50 +00:00
|
|
|
|
2017-10-30 21:42:26 +00:00
|
|
|
// Map from block's label id to block. TODO(dnovillo): This is superfluous wrt
|
2018-07-12 19:14:43 +00:00
|
|
|
// CFG. It has functionality not present in CFG. Consolidate.
|
|
|
|
std::unordered_map<uint32_t, BasicBlock*> id2block_;
|
2016-11-10 17:11:50 +00:00
|
|
|
|
2018-11-08 14:11:20 +00:00
|
|
|
// Set of ids of functions with early return.
|
|
|
|
std::set<uint32_t> early_return_funcs_;
|
2017-05-05 02:55:53 +00:00
|
|
|
|
|
|
|
// Set of ids of functions with no returns in loop
|
|
|
|
std::set<uint32_t> no_return_in_loop_;
|
|
|
|
|
|
|
|
// Set of ids of inlinable functions
|
2017-04-25 19:57:20 +00:00
|
|
|
std::set<uint32_t> inlinable_;
|
|
|
|
|
2017-05-05 02:55:53 +00:00
|
|
|
// result id for OpConstantFalse
|
|
|
|
uint32_t false_id_;
|
2016-11-10 17:11:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_OPT_INLINE_PASS_H_
|