2018-01-16 16:15:06 +00:00
|
|
|
// Copyright (c) 2018 Google LLC
|
|
|
|
//
|
|
|
|
// 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_IF_CONVERSION_H_
|
|
|
|
#define SOURCE_OPT_IF_CONVERSION_H_
|
2018-01-16 16:15:06 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/basic_block.h"
|
|
|
|
#include "source/opt/ir_builder.h"
|
|
|
|
#include "source/opt/pass.h"
|
|
|
|
#include "source/opt/types.h"
|
2018-01-16 16:15:06 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
|
|
|
// See optimizer.hpp for documentation.
|
|
|
|
class IfConversion : public Pass {
|
|
|
|
public:
|
|
|
|
const char* name() const override { return "if-conversion"; }
|
2018-07-12 13:08:45 +00:00
|
|
|
Status Process() override;
|
2018-01-16 16:15:06 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
IRContext::Analysis GetPreservedAnalyses() override {
|
|
|
|
return IRContext::kAnalysisDefUse | IRContext::kAnalysisDominatorAnalysis |
|
|
|
|
IRContext::kAnalysisInstrToBlockMapping | IRContext::kAnalysisCFG |
|
2018-12-20 18:00:05 +00:00
|
|
|
IRContext::kAnalysisNameMap | IRContext::kAnalysisConstants |
|
|
|
|
IRContext::kAnalysisTypes;
|
2018-01-16 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Returns true if |id| is a valid type for use with OpSelect. OpSelect only
|
|
|
|
// allows scalars, vectors and pointers as valid inputs.
|
|
|
|
bool CheckType(uint32_t id);
|
|
|
|
|
|
|
|
// Returns the basic block containing |id|.
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* GetBlock(uint32_t id);
|
2018-01-16 16:15:06 +00:00
|
|
|
|
|
|
|
// Returns the basic block for the |predecessor|'th index predecessor of
|
|
|
|
// |phi|.
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* GetIncomingBlock(Instruction* phi, uint32_t predecessor);
|
2018-01-16 16:15:06 +00:00
|
|
|
|
|
|
|
// Returns the instruction defining the |predecessor|'th index of |phi|.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* GetIncomingValue(Instruction* phi, uint32_t predecessor);
|
2018-01-16 16:15:06 +00:00
|
|
|
|
|
|
|
// Returns the id of a OpCompositeConstruct boolean vector. The composite has
|
|
|
|
// the same number of elements as |vec_data_ty| and each member is |cond|.
|
|
|
|
// |where| indicates the location in |block| to insert the composite
|
|
|
|
// construct. If necessary, this function will also construct the necessary
|
|
|
|
// type instructions for the boolean vector.
|
2018-01-30 21:43:55 +00:00
|
|
|
uint32_t SplatCondition(analysis::Vector* vec_data_ty, uint32_t cond,
|
|
|
|
InstructionBuilder* builder);
|
2018-01-16 16:15:06 +00:00
|
|
|
|
|
|
|
// Returns true if none of |phi|'s users are in |block|.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool CheckPhiUsers(Instruction* phi, BasicBlock* block);
|
2018-01-16 16:15:06 +00:00
|
|
|
|
|
|
|
// Returns |false| if |block| is not appropriate to transform. Only
|
|
|
|
// transforms blocks with two predecessors. Neither incoming block can be
|
|
|
|
// dominated by |block|. Both predecessors must share a common dominator that
|
|
|
|
// is terminated by a conditional branch.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool CheckBlock(BasicBlock* block, DominatorAnalysis* dominators,
|
|
|
|
BasicBlock** common);
|
2018-04-26 19:07:48 +00:00
|
|
|
|
|
|
|
// Moves |inst| to |target_block| if it does not already dominate the block.
|
|
|
|
// Any instructions that |inst| depends on are move if necessary. It is
|
|
|
|
// assumed that |inst| can be hoisted to |target_block| as defined by
|
|
|
|
// |CanHoistInstruction|. |dominators| is the dominator analysis for the
|
|
|
|
// function that contains |target_block|.
|
2018-07-12 19:14:43 +00:00
|
|
|
void HoistInstruction(Instruction* inst, BasicBlock* target_block,
|
2018-04-26 19:07:48 +00:00
|
|
|
DominatorAnalysis* dominators);
|
|
|
|
|
|
|
|
// Returns true if it is legal to move |inst| and the instructions it depends
|
|
|
|
// on to |target_block| if they do not already dominate |target_block|.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool CanHoistInstruction(Instruction* inst, BasicBlock* target_block,
|
2018-04-26 19:07:48 +00:00
|
|
|
DominatorAnalysis* dominators);
|
2018-01-16 16:15:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_OPT_IF_CONVERSION_H_
|