Removed dead code/const_casts/lists
authorUmar Arshad <umar@arrayfire.com>
Tue, 9 Aug 2016 18:05:03 +0000 (14:05 -0400)
committerDavid Neto <dneto@google.com>
Tue, 9 Aug 2016 22:09:38 +0000 (18:09 -0400)
source/val/Instruction.h
source/val/ValidationState.cpp
source/val/ValidationState.h
source/validate_id.cpp

index 45a93b9..51a94f0 100644 (file)
@@ -73,16 +73,11 @@ class Instruction {
   }
 
   /// The word used to define the Instruction
-  uint32_t words(size_t index) const { return words_[index]; }
+  uint32_t word(size_t index) const { return words_[index]; }
 
   /// The words used to define the Instruction
   const std::vector<uint32_t>& words() const { return words_; }
 
-  /// The operand of the Instruction at \p index
-  const spv_parsed_operand_t& operands(size_t index) const {
-    return operands_[index];
-  }
-
   /// The operands of the Instruction
   const std::vector<spv_parsed_operand_t>& operands() const {
     return operands_;
index 79c5241..adb4753 100644 (file)
@@ -32,7 +32,7 @@
 #include "val/Construct.h"
 #include "val/Function.h"
 
-using std::list;
+using std::deque;
 using std::make_pair;
 using std::pair;
 using std::string;
@@ -268,8 +268,14 @@ const Instruction* ValidationState_t::FindDef(uint32_t id) const {
 }
 
 Instruction* ValidationState_t::FindDef(uint32_t id) {
-  return const_cast<Instruction*>(
-      const_cast<const ValidationState_t*>(this)->FindDef(id));
+  if (all_definitions_.count(id) == 0) {
+    return nullptr;
+  } else {
+    /// We are in a const function, so we cannot use defs.operator[]().
+    /// Luckily we know the key exists, so defs_.at() won't throw an
+    /// exception.
+    return all_definitions_.at(id);
+  }
 }
 
 // Increments the instruction count. Used for diagnostic
@@ -299,7 +305,7 @@ DiagnosticStream ValidationState_t::diag(spv_result_t error_code) const {
       error_code);
 }
 
-list<Function>& ValidationState_t::functions() { return module_functions_; }
+deque<Function>& ValidationState_t::functions() { return module_functions_; }
 
 Function& ValidationState_t::current_function() {
   assert(in_function_body());
index b2395ec..a8b77a3 100644 (file)
@@ -27,7 +27,7 @@
 #ifndef LIBSPIRV_VAL_VALIDATIONSTATE_H_
 #define LIBSPIRV_VAL_VALIDATIONSTATE_H_
 
-#include <list>
+#include <deque>
 #include <string>
 #include <unordered_map>
 #include <unordered_set>
@@ -88,7 +88,7 @@ class ValidationState_t {
   /// defined
   size_t unresolved_forward_id_count() const;
 
-  /// Returns a list of unresolved forward ids.
+  /// Returns a vector of unresolved forward ids.
   std::vector<uint32_t> UnresolvedForwardIds() const;
 
   /// Returns true if the id has been defined
@@ -109,7 +109,7 @@ class ValidationState_t {
   libspirv::DiagnosticStream diag(spv_result_t error_code) const;
 
   /// Returns the function states
-  std::list<Function>& functions();
+  std::deque<Function>& functions();
 
   /// Returns the function states
   Function& current_function();
@@ -170,8 +170,8 @@ class ValidationState_t {
   /// nullptr
   Instruction* FindDef(uint32_t id);
 
-  /// Returns a vector of instructions in the order they appear in the binary
-  const std::list<Instruction>& ordered_instructions() {
+  /// Returns a deque of instructions in the order they appear in the binary
+  const std::deque<Instruction>& ordered_instructions() {
     return ordered_instructions_;
   }
 
@@ -197,14 +197,14 @@ class ValidationState_t {
   ModuleLayoutSection current_layout_section_;
 
   /// A list of functions in the module
-  std::list<Function> module_functions_;
+  std::deque<Function> module_functions_;
 
   /// Mask of the capabilities available in the module
   spv_capability_mask_t
       module_capabilities_;  /// Module's declared capabilities.
 
   /// List of all instructions in the order they appear in the binary
-  std::list<Instruction> ordered_instructions_;
+  std::deque<Instruction> ordered_instructions_;
 
   /// Instructions that can be referenced by Ids
   std::unordered_map<uint32_t, Instruction*> all_definitions_;
index 4489569..0a50c27 100644 (file)
@@ -2386,19 +2386,10 @@ spv_result_t UpdateIdUse(ValidationState_t& _) {
   for (const auto& inst : _.ordered_instructions()) {
     for (auto& operand : inst.operands()) {
       const spv_operand_type_t& type = operand.type;
-      const uint32_t operand_id = inst.words()[operand.offset];
-
-      switch (type) {
-        case SPV_OPERAND_TYPE_VARIABLE_ID:
-        case SPV_OPERAND_TYPE_ID:
-        case SPV_OPERAND_TYPE_TYPE_ID:
-        case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
-        case SPV_OPERAND_TYPE_SCOPE_ID:
-          if (auto def = _.FindDef(operand_id))
-            def->RegisterUse(&inst, operand.offset);
-          break;
-        default:
-          break;
+      const uint32_t operand_id = inst.word(operand.offset);
+      if (spvIsIdType(type) && type != SPV_OPERAND_TYPE_RESULT_ID) {
+        if (auto def = _.FindDef(operand_id))
+          def->RegisterUse(&inst, operand.offset);
       }
     }
   }