Add another construtor for opt::ir::Instruction
authorqining <qining@google.com>
Fri, 12 Aug 2016 21:26:35 +0000 (17:26 -0400)
committerqining <qining@google.com>
Sat, 13 Aug 2016 15:17:11 +0000 (11:17 -0400)
source/opt/instruction.cpp
source/opt/instruction.h

index 65fb529..03031b4 100644 (file)
@@ -27,6 +27,7 @@
 #include "instruction.h"
 
 #include <cassert>
+#include <initializer_list>
 
 #include "reflect.h"
 
@@ -50,6 +51,20 @@ Instruction::Instruction(const spv_parsed_instruction_t& inst,
   }
 }
 
+Instruction::Instruction(SpvOp op, uint32_t ty_id, uint32_t res_id,
+                         const std::vector<Operand>& in_operands)
+    : opcode_(op), type_id_(ty_id), result_id_(res_id), operands_() {
+  if (type_id_ != 0) {
+    operands_.emplace_back(spv_operand_type_t::SPV_OPERAND_TYPE_TYPE_ID,
+                           std::initializer_list<uint32_t>{type_id_});
+  }
+  if (result_id_ != 0) {
+    operands_.emplace_back(spv_operand_type_t::SPV_OPERAND_TYPE_RESULT_ID,
+                           std::initializer_list<uint32_t>{result_id_});
+  }
+  operands_.insert(operands_.end(), in_operands.begin(), in_operands.end());
+}
+
 Instruction::Instruction(Instruction&& that)
     : opcode_(that.opcode_),
       type_id_(that.type_id_),
index 6c53f08..73ac828 100644 (file)
@@ -62,6 +62,9 @@ struct Operand {
   Operand(spv_operand_type_t t, std::vector<uint32_t>&& w)
       : type(t), words(std::move(w)) {}
 
+  Operand(spv_operand_type_t t, const std::vector<uint32_t>& w)
+      : type(t), words(w) {}
+
   spv_operand_type_t type;      // Type of this logical operand.
   std::vector<uint32_t> words;  // Binary segments of this logical operand.
 
@@ -85,6 +88,11 @@ class Instruction {
   Instruction(const spv_parsed_instruction_t& inst,
               std::vector<Instruction>&& dbg_line = {});
 
+  // Creates an instruction with the given opcode |op|, type id: |ty_id|,
+  // result id: |res_id| and input operands: |in_operands|.
+  Instruction(SpvOp op, uint32_t ty_id, uint32_t res_id,
+              const std::vector<Operand>& in_operands);
+
   Instruction(const Instruction&) = default;
   Instruction& operator=(const Instruction&) = default;