2016-07-27 21:37:04 +00:00
|
|
|
// Copyright (c) 2016 Google Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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
|
2016-07-27 21:37:04 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-07-27 21:37:04 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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.
|
2016-07-27 21:37:04 +00:00
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef SOURCE_OPT_DEF_USE_MANAGER_H_
|
|
|
|
#define SOURCE_OPT_DEF_USE_MANAGER_H_
|
2016-07-27 21:37:04 +00:00
|
|
|
|
|
|
|
#include <list>
|
2017-11-14 19:11:50 +00:00
|
|
|
#include <set>
|
2016-07-27 21:37:04 +00:00
|
|
|
#include <unordered_map>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <utility>
|
2016-07-27 21:37:04 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/instruction.h"
|
|
|
|
#include "source/opt/module.h"
|
2016-09-21 21:16:31 +00:00
|
|
|
#include "spirv-tools/libspirv.hpp"
|
2016-07-27 21:37:04 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
namespace analysis {
|
|
|
|
|
2016-08-15 17:41:47 +00:00
|
|
|
// Class for representing a use of id. Note that:
|
|
|
|
// * Result type id is a use.
|
2016-07-27 21:37:04 +00:00
|
|
|
// * Ids referenced in OpSectionMerge & OpLoopMerge are considered as use.
|
|
|
|
// * Ids referenced in OpPhi's in operands are considered as use.
|
|
|
|
struct Use {
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* inst; // Instruction using the id.
|
2016-07-27 21:37:04 +00:00
|
|
|
uint32_t operand_index; // logical operand index of the id use. This can be
|
|
|
|
// the index of result type id.
|
|
|
|
};
|
|
|
|
|
2017-11-09 16:24:41 +00:00
|
|
|
inline bool operator==(const Use& lhs, const Use& rhs) {
|
|
|
|
return lhs.inst == rhs.inst && lhs.operand_index == rhs.operand_index;
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:16:41 +00:00
|
|
|
inline bool operator!=(const Use& lhs, const Use& rhs) { return !(lhs == rhs); }
|
2017-11-09 16:24:41 +00:00
|
|
|
|
|
|
|
inline bool operator<(const Use& lhs, const Use& rhs) {
|
2017-11-27 15:16:41 +00:00
|
|
|
if (lhs.inst < rhs.inst) return true;
|
|
|
|
if (lhs.inst > rhs.inst) return false;
|
2017-11-09 16:24:41 +00:00
|
|
|
return lhs.operand_index < rhs.operand_index;
|
|
|
|
}
|
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
// Definition and user pair.
|
|
|
|
//
|
|
|
|
// The first element of the pair is the definition.
|
|
|
|
// The second element of the pair is the user.
|
|
|
|
//
|
|
|
|
// Definition should never be null. User can be null, however, such an entry
|
|
|
|
// should be used only for searching (e.g. all users of a particular definition)
|
|
|
|
// and never stored in a container.
|
2018-07-12 19:14:43 +00:00
|
|
|
using UserEntry = std::pair<Instruction*, Instruction*>;
|
2017-11-14 19:11:50 +00:00
|
|
|
|
|
|
|
// Orders UserEntry for use in associative containers (i.e. less than ordering).
|
|
|
|
//
|
|
|
|
// The definition of an UserEntry is treated as the major key and the users as
|
|
|
|
// the minor key so that all the users of a particular definition are
|
|
|
|
// consecutive in a container.
|
|
|
|
//
|
|
|
|
// A null user always compares less than a real user. This is done to provide
|
|
|
|
// easy values to search for the beginning of the users of a particular
|
|
|
|
// definition (i.e. using {def, nullptr}).
|
|
|
|
struct UserEntryLess {
|
|
|
|
bool operator()(const UserEntry& lhs, const UserEntry& rhs) const {
|
|
|
|
// If lhs.first and rhs.first are both null, fall through to checking the
|
|
|
|
// second entries.
|
2017-11-27 15:16:41 +00:00
|
|
|
if (!lhs.first && rhs.first) return true;
|
|
|
|
if (lhs.first && !rhs.first) return false;
|
2017-11-14 19:11:50 +00:00
|
|
|
|
2017-12-03 20:27:21 +00:00
|
|
|
// If neither definition is null, then compare unique ids.
|
2017-11-14 19:11:50 +00:00
|
|
|
if (lhs.first && rhs.first) {
|
2017-11-27 15:16:41 +00:00
|
|
|
if (lhs.first->unique_id() < rhs.first->unique_id()) return true;
|
|
|
|
if (rhs.first->unique_id() < lhs.first->unique_id()) return false;
|
2017-11-14 19:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return false on equality.
|
2017-11-27 15:16:41 +00:00
|
|
|
if (!lhs.second && !rhs.second) return false;
|
|
|
|
if (!lhs.second) return true;
|
|
|
|
if (!rhs.second) return false;
|
2017-11-14 19:11:50 +00:00
|
|
|
|
|
|
|
// If neither user is null then compare unique ids.
|
|
|
|
return lhs.second->unique_id() < rhs.second->unique_id();
|
|
|
|
}
|
|
|
|
};
|
2016-07-27 21:37:04 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
// A class for analyzing and managing defs and uses in an Module.
|
2016-07-27 21:37:04 +00:00
|
|
|
class DefUseManager {
|
|
|
|
public:
|
2018-07-12 19:14:43 +00:00
|
|
|
using IdToDefMap = std::unordered_map<uint32_t, Instruction*>;
|
2017-11-14 19:11:50 +00:00
|
|
|
using IdToUsersMap = std::set<UserEntry, UserEntryLess>;
|
2016-07-27 21:37:04 +00:00
|
|
|
|
2016-08-20 03:17:44 +00:00
|
|
|
// Constructs a def-use manager from the given |module|. All internal messages
|
|
|
|
// will be communicated to the outside via the given message |consumer|. This
|
|
|
|
// instance only keeps a reference to the |consumer|, so the |consumer| should
|
|
|
|
// outlive this instance.
|
2018-07-12 19:14:43 +00:00
|
|
|
DefUseManager(Module* module) { AnalyzeDefUse(module); }
|
2016-08-20 03:17:44 +00:00
|
|
|
|
2016-08-15 17:41:47 +00:00
|
|
|
DefUseManager(const DefUseManager&) = delete;
|
|
|
|
DefUseManager(DefUseManager&&) = delete;
|
|
|
|
DefUseManager& operator=(const DefUseManager&) = delete;
|
|
|
|
DefUseManager& operator=(DefUseManager&&) = delete;
|
2016-07-27 21:37:04 +00:00
|
|
|
|
2017-06-16 21:37:31 +00:00
|
|
|
// Analyzes the defs in the given |inst|.
|
2018-07-12 19:14:43 +00:00
|
|
|
void AnalyzeInstDef(Instruction* inst);
|
2017-06-16 21:37:31 +00:00
|
|
|
|
|
|
|
// Analyzes the uses in the given |inst|.
|
2017-11-14 19:11:50 +00:00
|
|
|
//
|
|
|
|
// All operands of |inst| must be analyzed as defs.
|
2018-07-12 19:14:43 +00:00
|
|
|
void AnalyzeInstUse(Instruction* inst);
|
2017-06-16 21:37:31 +00:00
|
|
|
|
2016-08-12 14:05:58 +00:00
|
|
|
// Analyzes the defs and uses in the given |inst|.
|
2018-07-12 19:14:43 +00:00
|
|
|
void AnalyzeInstDefUse(Instruction* inst);
|
2016-08-12 14:05:58 +00:00
|
|
|
|
2016-07-27 21:37:04 +00:00
|
|
|
// Returns the def instruction for the given |id|. If there is no instruction
|
|
|
|
// defining |id|, returns nullptr.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* GetDef(uint32_t id);
|
|
|
|
const Instruction* GetDef(uint32_t id) const;
|
2017-11-14 19:11:50 +00:00
|
|
|
|
|
|
|
// Runs the given function |f| on each unique user instruction of |def| (or
|
|
|
|
// |id|).
|
|
|
|
//
|
|
|
|
// If one instruction uses |def| in multiple operands, that instruction will
|
|
|
|
// only be visited once.
|
|
|
|
//
|
|
|
|
// |def| (or |id|) must be registered as a definition.
|
2018-07-12 19:14:43 +00:00
|
|
|
void ForEachUser(const Instruction* def,
|
|
|
|
const std::function<void(Instruction*)>& f) const;
|
2017-11-14 19:11:50 +00:00
|
|
|
void ForEachUser(uint32_t id,
|
2018-07-12 19:14:43 +00:00
|
|
|
const std::function<void(Instruction*)>& f) const;
|
2017-11-14 19:11:50 +00:00
|
|
|
|
2018-01-12 20:05:53 +00:00
|
|
|
// Runs the given function |f| on each unique user instruction of |def| (or
|
|
|
|
// |id|). If |f| returns false, iteration is terminated and this function
|
|
|
|
// returns false.
|
|
|
|
//
|
|
|
|
// If one instruction uses |def| in multiple operands, that instruction will
|
|
|
|
// be only be visited once.
|
|
|
|
//
|
|
|
|
// |def| (or |id|) must be registered as a definition.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool WhileEachUser(const Instruction* def,
|
|
|
|
const std::function<bool(Instruction*)>& f) const;
|
2018-01-12 20:05:53 +00:00
|
|
|
bool WhileEachUser(uint32_t id,
|
2018-07-12 19:14:43 +00:00
|
|
|
const std::function<bool(Instruction*)>& f) const;
|
2018-01-12 20:05:53 +00:00
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
// Runs the given function |f| on each unique use of |def| (or
|
|
|
|
// |id|).
|
|
|
|
//
|
|
|
|
// If one instruction uses |def| in multiple operands, each operand will be
|
|
|
|
// visited separately.
|
|
|
|
//
|
|
|
|
// |def| (or |id|) must be registered as a definition.
|
2018-07-12 19:14:43 +00:00
|
|
|
void ForEachUse(
|
|
|
|
const Instruction* def,
|
|
|
|
const std::function<void(Instruction*, uint32_t operand_index)>& f) const;
|
|
|
|
void ForEachUse(
|
|
|
|
uint32_t id,
|
|
|
|
const std::function<void(Instruction*, uint32_t operand_index)>& f) const;
|
2017-11-14 19:11:50 +00:00
|
|
|
|
2018-01-12 20:05:53 +00:00
|
|
|
// Runs the given function |f| on each unique use of |def| (or
|
|
|
|
// |id|). If |f| returns false, iteration is terminated and this function
|
|
|
|
// returns false.
|
|
|
|
//
|
|
|
|
// If one instruction uses |def| in multiple operands, each operand will be
|
|
|
|
// visited separately.
|
|
|
|
//
|
|
|
|
// |def| (or |id|) must be registered as a definition.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool WhileEachUse(
|
|
|
|
const Instruction* def,
|
|
|
|
const std::function<bool(Instruction*, uint32_t operand_index)>& f) const;
|
|
|
|
bool WhileEachUse(
|
|
|
|
uint32_t id,
|
|
|
|
const std::function<bool(Instruction*, uint32_t operand_index)>& f) const;
|
2018-01-12 20:05:53 +00:00
|
|
|
|
2017-11-30 22:03:06 +00:00
|
|
|
// Returns the number of users of |def| (or |id|).
|
2018-07-12 19:14:43 +00:00
|
|
|
uint32_t NumUsers(const Instruction* def) const;
|
2017-11-30 22:03:06 +00:00
|
|
|
uint32_t NumUsers(uint32_t id) const;
|
|
|
|
|
|
|
|
// Returns the number of uses of |def| (or |id|).
|
2018-07-12 19:14:43 +00:00
|
|
|
uint32_t NumUses(const Instruction* def) const;
|
2017-11-30 22:03:06 +00:00
|
|
|
uint32_t NumUses(uint32_t id) const;
|
|
|
|
|
2016-08-29 22:05:24 +00:00
|
|
|
// Returns the annotation instrunctions which are a direct use of the given
|
|
|
|
// |id|. This means when the decorations are applied through decoration
|
|
|
|
// group(s), this function will just return the OpGroupDecorate
|
|
|
|
// instrcution(s) which refer to the given id as an operand. The OpDecorate
|
|
|
|
// instructions which decorate the decoration group will not be returned.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Instruction*> GetAnnotations(uint32_t id) const;
|
2016-07-27 21:37:04 +00:00
|
|
|
|
|
|
|
// Returns the map from ids to their def instructions.
|
|
|
|
const IdToDefMap& id_to_defs() const { return id_to_def_; }
|
2017-11-14 19:11:50 +00:00
|
|
|
// Returns the map from instructions to their users.
|
|
|
|
const IdToUsersMap& id_to_users() const { return id_to_users_; }
|
2016-07-27 21:37:04 +00:00
|
|
|
|
2016-08-16 17:06:03 +00:00
|
|
|
// Clear the internal def-use record of the given instruction |inst|. This
|
|
|
|
// method will update the use information of the operand ids of |inst|. The
|
|
|
|
// record: |inst| uses an |id|, will be removed from the use records of |id|.
|
|
|
|
// If |inst| defines an result id, the use record of this result id will also
|
|
|
|
// be removed. Does nothing if |inst| was not analyzed before.
|
2018-07-12 19:14:43 +00:00
|
|
|
void ClearInst(Instruction* inst);
|
2016-08-12 14:43:26 +00:00
|
|
|
|
2016-08-16 17:06:03 +00:00
|
|
|
// Erases the records that a given instruction uses its operand ids.
|
2018-07-12 19:14:43 +00:00
|
|
|
void EraseUseRecordsOfOperandIds(const Instruction* inst);
|
2016-07-27 21:37:04 +00:00
|
|
|
|
2017-11-27 15:16:41 +00:00
|
|
|
friend bool operator==(const DefUseManager&, const DefUseManager&);
|
|
|
|
friend bool operator!=(const DefUseManager& lhs, const DefUseManager& rhs) {
|
2017-11-09 16:24:41 +00:00
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2018-02-08 15:59:03 +00:00
|
|
|
// If |inst| has not already been analysed, then analyses its defintion and
|
|
|
|
// uses.
|
2018-07-12 19:14:43 +00:00
|
|
|
void UpdateDefUse(Instruction* inst);
|
2018-02-08 15:59:03 +00:00
|
|
|
|
2017-11-02 18:25:48 +00:00
|
|
|
private:
|
|
|
|
using InstToUsedIdsMap =
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_map<const Instruction*, std::vector<uint32_t>>;
|
2017-11-02 18:25:48 +00:00
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
// Returns the first location that {|def|, nullptr} could be inserted into the
|
|
|
|
// users map without violating ordering.
|
2018-07-12 19:14:43 +00:00
|
|
|
IdToUsersMap::const_iterator UsersBegin(const Instruction* def) const;
|
2017-11-14 19:11:50 +00:00
|
|
|
|
|
|
|
// Returns true if |iter| has not reached the end of |def|'s users.
|
|
|
|
//
|
|
|
|
// In the first version |iter| is compared against the end of the map for
|
|
|
|
// validity before other checks. In the second version, |iter| is compared
|
|
|
|
// against |cached_end| for validity before other checks. This allows caching
|
|
|
|
// the map's end which is a performance improvement on some platforms.
|
|
|
|
bool UsersNotEnd(const IdToUsersMap::const_iterator& iter,
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* def) const;
|
2017-11-14 19:11:50 +00:00
|
|
|
bool UsersNotEnd(const IdToUsersMap::const_iterator& iter,
|
|
|
|
const IdToUsersMap::const_iterator& cached_end,
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* def) const;
|
2017-11-14 19:11:50 +00:00
|
|
|
|
2017-11-02 18:25:48 +00:00
|
|
|
// Analyzes the defs and uses in the given |module| and populates data
|
|
|
|
// structures in this class. Does nothing if |module| is nullptr.
|
2018-07-12 19:14:43 +00:00
|
|
|
void AnalyzeDefUse(Module* module);
|
2017-11-02 18:25:48 +00:00
|
|
|
|
2017-11-27 15:16:41 +00:00
|
|
|
IdToDefMap id_to_def_; // Mapping from ids to their definitions
|
|
|
|
IdToUsersMap id_to_users_; // Mapping from ids to their users
|
2017-11-02 18:25:48 +00:00
|
|
|
// Mapping from instructions to the ids used in the instruction.
|
2016-08-16 17:06:03 +00:00
|
|
|
InstToUsedIdsMap inst_to_used_ids_;
|
2016-07-27 21:37:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace analysis
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_OPT_DEF_USE_MANAGER_H_
|