transformation_invert_comparison_operator.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (c) 2020 Vasyl Teliman
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef SOURCE_FUZZ_TRANSFORMATION_INVERT_COMPARISON_OPERATOR_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_INVERT_COMPARISON_OPERATOR_H_
  16. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  17. #include "source/fuzz/transformation.h"
  18. #include "source/fuzz/transformation_context.h"
  19. #include "source/opt/ir_context.h"
  20. namespace spvtools {
  21. namespace fuzz {
  22. class TransformationInvertComparisonOperator : public Transformation {
  23. public:
  24. explicit TransformationInvertComparisonOperator(
  25. protobufs::TransformationInvertComparisonOperator message);
  26. TransformationInvertComparisonOperator(uint32_t operator_id,
  27. uint32_t fresh_id);
  28. // - |operator_id| should be a result id of some instruction for which
  29. // IsInversionSupported returns true.
  30. // - |fresh_id| must be a fresh id.
  31. bool IsApplicable(
  32. opt::IRContext* ir_context,
  33. const TransformationContext& transformation_context) const override;
  34. // Inverts the opcode of the instruction with result id |operator_id| (e.g >=
  35. // becomes <) and inserts OpLogicalNot instruction after |operator_id|. Also,
  36. // changes the result id of OpLogicalNot to |operator_id| and the result id of
  37. // the inverted operator to |fresh_id|.
  38. void Apply(opt::IRContext* ir_context,
  39. TransformationContext* transformation_context) const override;
  40. std::unordered_set<uint32_t> GetFreshIds() const override;
  41. protobufs::Transformation ToMessage() const override;
  42. // Returns true if |opcode| is supported by this transformation.
  43. static bool IsInversionSupported(spv::Op opcode);
  44. private:
  45. // Returns an inverted |opcode| (e.g. < becomes >=, == becomes != etc.)
  46. static spv::Op InvertOpcode(spv::Op opcode);
  47. protobufs::TransformationInvertComparisonOperator message_;
  48. };
  49. } // namespace fuzz
  50. } // namespace spvtools
  51. #endif // SOURCE_FUZZ_TRANSFORMATION_INVERT_COMPARISON_OPERATOR_H_