fold.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. // Copyright (c) 2017 Google Inc.
  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. #include "source/opt/fold.h"
  15. #include <cassert>
  16. #include <cstdint>
  17. #include <vector>
  18. #include "source/opt/const_folding_rules.h"
  19. #include "source/opt/def_use_manager.h"
  20. #include "source/opt/folding_rules.h"
  21. #include "source/opt/ir_builder.h"
  22. #include "source/opt/ir_context.h"
  23. namespace spvtools {
  24. namespace opt {
  25. namespace {
  26. #ifndef INT32_MIN
  27. #define INT32_MIN (-2147483648)
  28. #endif
  29. #ifndef INT32_MAX
  30. #define INT32_MAX 2147483647
  31. #endif
  32. #ifndef UINT32_MAX
  33. #define UINT32_MAX 0xffffffff /* 4294967295U */
  34. #endif
  35. } // namespace
  36. uint32_t InstructionFolder::UnaryOperate(SpvOp opcode, uint32_t operand) const {
  37. switch (opcode) {
  38. // Arthimetics
  39. case SpvOp::SpvOpSNegate: {
  40. int32_t s_operand = static_cast<int32_t>(operand);
  41. if (s_operand == std::numeric_limits<int32_t>::min()) {
  42. return s_operand;
  43. }
  44. return -s_operand;
  45. }
  46. case SpvOp::SpvOpNot:
  47. return ~operand;
  48. case SpvOp::SpvOpLogicalNot:
  49. return !static_cast<bool>(operand);
  50. case SpvOp::SpvOpUConvert:
  51. return operand;
  52. case SpvOp::SpvOpSConvert:
  53. return operand;
  54. default:
  55. assert(false &&
  56. "Unsupported unary operation for OpSpecConstantOp instruction");
  57. return 0u;
  58. }
  59. }
  60. uint32_t InstructionFolder::BinaryOperate(SpvOp opcode, uint32_t a,
  61. uint32_t b) const {
  62. switch (opcode) {
  63. // Arthimetics
  64. case SpvOp::SpvOpIAdd:
  65. return a + b;
  66. case SpvOp::SpvOpISub:
  67. return a - b;
  68. case SpvOp::SpvOpIMul:
  69. return a * b;
  70. case SpvOp::SpvOpUDiv:
  71. if (b != 0) {
  72. return a / b;
  73. } else {
  74. // Dividing by 0 is undefined, so we will just pick 0.
  75. return 0;
  76. }
  77. case SpvOp::SpvOpSDiv:
  78. if (b != 0u) {
  79. return (static_cast<int32_t>(a)) / (static_cast<int32_t>(b));
  80. } else {
  81. // Dividing by 0 is undefined, so we will just pick 0.
  82. return 0;
  83. }
  84. case SpvOp::SpvOpSRem: {
  85. // The sign of non-zero result comes from the first operand: a. This is
  86. // guaranteed by C++11 rules for integer division operator. The division
  87. // result is rounded toward zero, so the result of '%' has the sign of
  88. // the first operand.
  89. if (b != 0u) {
  90. return static_cast<int32_t>(a) % static_cast<int32_t>(b);
  91. } else {
  92. // Remainder when dividing with 0 is undefined, so we will just pick 0.
  93. return 0;
  94. }
  95. }
  96. case SpvOp::SpvOpSMod: {
  97. // The sign of non-zero result comes from the second operand: b
  98. if (b != 0u) {
  99. int32_t rem = BinaryOperate(SpvOp::SpvOpSRem, a, b);
  100. int32_t b_prim = static_cast<int32_t>(b);
  101. return (rem + b_prim) % b_prim;
  102. } else {
  103. // Mod with 0 is undefined, so we will just pick 0.
  104. return 0;
  105. }
  106. }
  107. case SpvOp::SpvOpUMod:
  108. if (b != 0u) {
  109. return (a % b);
  110. } else {
  111. // Mod with 0 is undefined, so we will just pick 0.
  112. return 0;
  113. }
  114. // Shifting
  115. case SpvOp::SpvOpShiftRightLogical:
  116. if (b >= 32) {
  117. // This is undefined behaviour when |b| > 32. Choose 0 for consistency.
  118. // When |b| == 32, doing the shift in C++ in undefined, but the result
  119. // will be 0, so just return that value.
  120. return 0;
  121. }
  122. return a >> b;
  123. case SpvOp::SpvOpShiftRightArithmetic:
  124. if (b > 32) {
  125. // This is undefined behaviour. Choose 0 for consistency.
  126. return 0;
  127. }
  128. if (b == 32) {
  129. // Doing the shift in C++ is undefined, but the result is defined in the
  130. // spir-v spec. Find that value another way.
  131. if (static_cast<int32_t>(a) >= 0) {
  132. return 0;
  133. } else {
  134. return static_cast<uint32_t>(-1);
  135. }
  136. }
  137. return (static_cast<int32_t>(a)) >> b;
  138. case SpvOp::SpvOpShiftLeftLogical:
  139. if (b >= 32) {
  140. // This is undefined behaviour when |b| > 32. Choose 0 for consistency.
  141. // When |b| == 32, doing the shift in C++ in undefined, but the result
  142. // will be 0, so just return that value.
  143. return 0;
  144. }
  145. return a << b;
  146. // Bitwise operations
  147. case SpvOp::SpvOpBitwiseOr:
  148. return a | b;
  149. case SpvOp::SpvOpBitwiseAnd:
  150. return a & b;
  151. case SpvOp::SpvOpBitwiseXor:
  152. return a ^ b;
  153. // Logical
  154. case SpvOp::SpvOpLogicalEqual:
  155. return (static_cast<bool>(a)) == (static_cast<bool>(b));
  156. case SpvOp::SpvOpLogicalNotEqual:
  157. return (static_cast<bool>(a)) != (static_cast<bool>(b));
  158. case SpvOp::SpvOpLogicalOr:
  159. return (static_cast<bool>(a)) || (static_cast<bool>(b));
  160. case SpvOp::SpvOpLogicalAnd:
  161. return (static_cast<bool>(a)) && (static_cast<bool>(b));
  162. // Comparison
  163. case SpvOp::SpvOpIEqual:
  164. return a == b;
  165. case SpvOp::SpvOpINotEqual:
  166. return a != b;
  167. case SpvOp::SpvOpULessThan:
  168. return a < b;
  169. case SpvOp::SpvOpSLessThan:
  170. return (static_cast<int32_t>(a)) < (static_cast<int32_t>(b));
  171. case SpvOp::SpvOpUGreaterThan:
  172. return a > b;
  173. case SpvOp::SpvOpSGreaterThan:
  174. return (static_cast<int32_t>(a)) > (static_cast<int32_t>(b));
  175. case SpvOp::SpvOpULessThanEqual:
  176. return a <= b;
  177. case SpvOp::SpvOpSLessThanEqual:
  178. return (static_cast<int32_t>(a)) <= (static_cast<int32_t>(b));
  179. case SpvOp::SpvOpUGreaterThanEqual:
  180. return a >= b;
  181. case SpvOp::SpvOpSGreaterThanEqual:
  182. return (static_cast<int32_t>(a)) >= (static_cast<int32_t>(b));
  183. default:
  184. assert(false &&
  185. "Unsupported binary operation for OpSpecConstantOp instruction");
  186. return 0u;
  187. }
  188. }
  189. uint32_t InstructionFolder::TernaryOperate(SpvOp opcode, uint32_t a, uint32_t b,
  190. uint32_t c) const {
  191. switch (opcode) {
  192. case SpvOp::SpvOpSelect:
  193. return (static_cast<bool>(a)) ? b : c;
  194. default:
  195. assert(false &&
  196. "Unsupported ternary operation for OpSpecConstantOp instruction");
  197. return 0u;
  198. }
  199. }
  200. uint32_t InstructionFolder::OperateWords(
  201. SpvOp opcode, const std::vector<uint32_t>& operand_words) const {
  202. switch (operand_words.size()) {
  203. case 1:
  204. return UnaryOperate(opcode, operand_words.front());
  205. case 2:
  206. return BinaryOperate(opcode, operand_words.front(), operand_words.back());
  207. case 3:
  208. return TernaryOperate(opcode, operand_words[0], operand_words[1],
  209. operand_words[2]);
  210. default:
  211. assert(false && "Invalid number of operands");
  212. return 0;
  213. }
  214. }
  215. bool InstructionFolder::FoldInstructionInternal(Instruction* inst) const {
  216. auto identity_map = [](uint32_t id) { return id; };
  217. Instruction* folded_inst = FoldInstructionToConstant(inst, identity_map);
  218. if (folded_inst != nullptr) {
  219. inst->SetOpcode(SpvOpCopyObject);
  220. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {folded_inst->result_id()}}});
  221. return true;
  222. }
  223. analysis::ConstantManager* const_manager = context_->get_constant_mgr();
  224. std::vector<const analysis::Constant*> constants =
  225. const_manager->GetOperandConstants(inst);
  226. for (const FoldingRule& rule :
  227. GetFoldingRules().GetRulesForInstruction(inst)) {
  228. if (rule(context_, inst, constants)) {
  229. return true;
  230. }
  231. }
  232. return false;
  233. }
  234. // Returns the result of performing an operation on scalar constant operands.
  235. // This function extracts the operand values as 32 bit words and returns the
  236. // result in 32 bit word. Scalar constants with longer than 32-bit width are
  237. // not accepted in this function.
  238. uint32_t InstructionFolder::FoldScalars(
  239. SpvOp opcode,
  240. const std::vector<const analysis::Constant*>& operands) const {
  241. assert(IsFoldableOpcode(opcode) &&
  242. "Unhandled instruction opcode in FoldScalars");
  243. std::vector<uint32_t> operand_values_in_raw_words;
  244. for (const auto& operand : operands) {
  245. if (const analysis::ScalarConstant* scalar = operand->AsScalarConstant()) {
  246. const auto& scalar_words = scalar->words();
  247. assert(scalar_words.size() == 1 &&
  248. "Scalar constants with longer than 32-bit width are not allowed "
  249. "in FoldScalars()");
  250. operand_values_in_raw_words.push_back(scalar_words.front());
  251. } else if (operand->AsNullConstant()) {
  252. operand_values_in_raw_words.push_back(0u);
  253. } else {
  254. assert(false &&
  255. "FoldScalars() only accepts ScalarConst or NullConst type of "
  256. "constant");
  257. }
  258. }
  259. return OperateWords(opcode, operand_values_in_raw_words);
  260. }
  261. bool InstructionFolder::FoldBinaryIntegerOpToConstant(
  262. Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
  263. uint32_t* result) const {
  264. SpvOp opcode = inst->opcode();
  265. analysis::ConstantManager* const_manger = context_->get_constant_mgr();
  266. uint32_t ids[2];
  267. const analysis::IntConstant* constants[2];
  268. for (uint32_t i = 0; i < 2; i++) {
  269. const Operand* operand = &inst->GetInOperand(i);
  270. if (operand->type != SPV_OPERAND_TYPE_ID) {
  271. return false;
  272. }
  273. ids[i] = id_map(operand->words[0]);
  274. const analysis::Constant* constant =
  275. const_manger->FindDeclaredConstant(ids[i]);
  276. constants[i] = (constant != nullptr ? constant->AsIntConstant() : nullptr);
  277. }
  278. switch (opcode) {
  279. // Arthimetics
  280. case SpvOp::SpvOpIMul:
  281. for (uint32_t i = 0; i < 2; i++) {
  282. if (constants[i] != nullptr && constants[i]->IsZero()) {
  283. *result = 0;
  284. return true;
  285. }
  286. }
  287. break;
  288. case SpvOp::SpvOpUDiv:
  289. case SpvOp::SpvOpSDiv:
  290. case SpvOp::SpvOpSRem:
  291. case SpvOp::SpvOpSMod:
  292. case SpvOp::SpvOpUMod:
  293. // This changes undefined behaviour (ie divide by 0) into a 0.
  294. for (uint32_t i = 0; i < 2; i++) {
  295. if (constants[i] != nullptr && constants[i]->IsZero()) {
  296. *result = 0;
  297. return true;
  298. }
  299. }
  300. break;
  301. // Shifting
  302. case SpvOp::SpvOpShiftRightLogical:
  303. case SpvOp::SpvOpShiftLeftLogical:
  304. if (constants[1] != nullptr) {
  305. // When shifting by a value larger than the size of the result, the
  306. // result is undefined. We are setting the undefined behaviour to a
  307. // result of 0. If the shift amount is the same as the size of the
  308. // result, then the result is defined, and it 0.
  309. uint32_t shift_amount = constants[1]->GetU32BitValue();
  310. if (shift_amount >= 32) {
  311. *result = 0;
  312. return true;
  313. }
  314. }
  315. break;
  316. // Bitwise operations
  317. case SpvOp::SpvOpBitwiseOr:
  318. for (uint32_t i = 0; i < 2; i++) {
  319. if (constants[i] != nullptr) {
  320. // TODO: Change the mask against a value based on the bit width of the
  321. // instruction result type. This way we can handle say 16-bit values
  322. // as well.
  323. uint32_t mask = constants[i]->GetU32BitValue();
  324. if (mask == 0xFFFFFFFF) {
  325. *result = 0xFFFFFFFF;
  326. return true;
  327. }
  328. }
  329. }
  330. break;
  331. case SpvOp::SpvOpBitwiseAnd:
  332. for (uint32_t i = 0; i < 2; i++) {
  333. if (constants[i] != nullptr) {
  334. if (constants[i]->IsZero()) {
  335. *result = 0;
  336. return true;
  337. }
  338. }
  339. }
  340. break;
  341. // Comparison
  342. case SpvOp::SpvOpULessThan:
  343. if (constants[0] != nullptr &&
  344. constants[0]->GetU32BitValue() == UINT32_MAX) {
  345. *result = false;
  346. return true;
  347. }
  348. if (constants[1] != nullptr && constants[1]->GetU32BitValue() == 0) {
  349. *result = false;
  350. return true;
  351. }
  352. break;
  353. case SpvOp::SpvOpSLessThan:
  354. if (constants[0] != nullptr &&
  355. constants[0]->GetS32BitValue() == INT32_MAX) {
  356. *result = false;
  357. return true;
  358. }
  359. if (constants[1] != nullptr &&
  360. constants[1]->GetS32BitValue() == INT32_MIN) {
  361. *result = false;
  362. return true;
  363. }
  364. break;
  365. case SpvOp::SpvOpUGreaterThan:
  366. if (constants[0] != nullptr && constants[0]->IsZero()) {
  367. *result = false;
  368. return true;
  369. }
  370. if (constants[1] != nullptr &&
  371. constants[1]->GetU32BitValue() == UINT32_MAX) {
  372. *result = false;
  373. return true;
  374. }
  375. break;
  376. case SpvOp::SpvOpSGreaterThan:
  377. if (constants[0] != nullptr &&
  378. constants[0]->GetS32BitValue() == INT32_MIN) {
  379. *result = false;
  380. return true;
  381. }
  382. if (constants[1] != nullptr &&
  383. constants[1]->GetS32BitValue() == INT32_MAX) {
  384. *result = false;
  385. return true;
  386. }
  387. break;
  388. case SpvOp::SpvOpULessThanEqual:
  389. if (constants[0] != nullptr && constants[0]->IsZero()) {
  390. *result = true;
  391. return true;
  392. }
  393. if (constants[1] != nullptr &&
  394. constants[1]->GetU32BitValue() == UINT32_MAX) {
  395. *result = true;
  396. return true;
  397. }
  398. break;
  399. case SpvOp::SpvOpSLessThanEqual:
  400. if (constants[0] != nullptr &&
  401. constants[0]->GetS32BitValue() == INT32_MIN) {
  402. *result = true;
  403. return true;
  404. }
  405. if (constants[1] != nullptr &&
  406. constants[1]->GetS32BitValue() == INT32_MAX) {
  407. *result = true;
  408. return true;
  409. }
  410. break;
  411. case SpvOp::SpvOpUGreaterThanEqual:
  412. if (constants[0] != nullptr &&
  413. constants[0]->GetU32BitValue() == UINT32_MAX) {
  414. *result = true;
  415. return true;
  416. }
  417. if (constants[1] != nullptr && constants[1]->GetU32BitValue() == 0) {
  418. *result = true;
  419. return true;
  420. }
  421. break;
  422. case SpvOp::SpvOpSGreaterThanEqual:
  423. if (constants[0] != nullptr &&
  424. constants[0]->GetS32BitValue() == INT32_MAX) {
  425. *result = true;
  426. return true;
  427. }
  428. if (constants[1] != nullptr &&
  429. constants[1]->GetS32BitValue() == INT32_MIN) {
  430. *result = true;
  431. return true;
  432. }
  433. break;
  434. default:
  435. break;
  436. }
  437. return false;
  438. }
  439. bool InstructionFolder::FoldBinaryBooleanOpToConstant(
  440. Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
  441. uint32_t* result) const {
  442. SpvOp opcode = inst->opcode();
  443. analysis::ConstantManager* const_manger = context_->get_constant_mgr();
  444. uint32_t ids[2];
  445. const analysis::BoolConstant* constants[2];
  446. for (uint32_t i = 0; i < 2; i++) {
  447. const Operand* operand = &inst->GetInOperand(i);
  448. if (operand->type != SPV_OPERAND_TYPE_ID) {
  449. return false;
  450. }
  451. ids[i] = id_map(operand->words[0]);
  452. const analysis::Constant* constant =
  453. const_manger->FindDeclaredConstant(ids[i]);
  454. constants[i] = (constant != nullptr ? constant->AsBoolConstant() : nullptr);
  455. }
  456. switch (opcode) {
  457. // Logical
  458. case SpvOp::SpvOpLogicalOr:
  459. for (uint32_t i = 0; i < 2; i++) {
  460. if (constants[i] != nullptr) {
  461. if (constants[i]->value()) {
  462. *result = true;
  463. return true;
  464. }
  465. }
  466. }
  467. break;
  468. case SpvOp::SpvOpLogicalAnd:
  469. for (uint32_t i = 0; i < 2; i++) {
  470. if (constants[i] != nullptr) {
  471. if (!constants[i]->value()) {
  472. *result = false;
  473. return true;
  474. }
  475. }
  476. }
  477. break;
  478. default:
  479. break;
  480. }
  481. return false;
  482. }
  483. bool InstructionFolder::FoldIntegerOpToConstant(
  484. Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
  485. uint32_t* result) const {
  486. assert(IsFoldableOpcode(inst->opcode()) &&
  487. "Unhandled instruction opcode in FoldScalars");
  488. switch (inst->NumInOperands()) {
  489. case 2:
  490. return FoldBinaryIntegerOpToConstant(inst, id_map, result) ||
  491. FoldBinaryBooleanOpToConstant(inst, id_map, result);
  492. default:
  493. return false;
  494. }
  495. }
  496. std::vector<uint32_t> InstructionFolder::FoldVectors(
  497. SpvOp opcode, uint32_t num_dims,
  498. const std::vector<const analysis::Constant*>& operands) const {
  499. assert(IsFoldableOpcode(opcode) &&
  500. "Unhandled instruction opcode in FoldVectors");
  501. std::vector<uint32_t> result;
  502. for (uint32_t d = 0; d < num_dims; d++) {
  503. std::vector<uint32_t> operand_values_for_one_dimension;
  504. for (const auto& operand : operands) {
  505. if (const analysis::VectorConstant* vector_operand =
  506. operand->AsVectorConstant()) {
  507. // Extract the raw value of the scalar component constants
  508. // in 32-bit words here. The reason of not using FoldScalars() here
  509. // is that we do not create temporary null constants as components
  510. // when the vector operand is a NullConstant because Constant creation
  511. // may need extra checks for the validity and that is not manageed in
  512. // here.
  513. if (const analysis::ScalarConstant* scalar_component =
  514. vector_operand->GetComponents().at(d)->AsScalarConstant()) {
  515. const auto& scalar_words = scalar_component->words();
  516. assert(
  517. scalar_words.size() == 1 &&
  518. "Vector components with longer than 32-bit width are not allowed "
  519. "in FoldVectors()");
  520. operand_values_for_one_dimension.push_back(scalar_words.front());
  521. } else if (operand->AsNullConstant()) {
  522. operand_values_for_one_dimension.push_back(0u);
  523. } else {
  524. assert(false &&
  525. "VectorConst should only has ScalarConst or NullConst as "
  526. "components");
  527. }
  528. } else if (operand->AsNullConstant()) {
  529. operand_values_for_one_dimension.push_back(0u);
  530. } else {
  531. assert(false &&
  532. "FoldVectors() only accepts VectorConst or NullConst type of "
  533. "constant");
  534. }
  535. }
  536. result.push_back(OperateWords(opcode, operand_values_for_one_dimension));
  537. }
  538. return result;
  539. }
  540. bool InstructionFolder::IsFoldableOpcode(SpvOp opcode) const {
  541. // NOTE: Extend to more opcodes as new cases are handled in the folder
  542. // functions.
  543. switch (opcode) {
  544. case SpvOp::SpvOpBitwiseAnd:
  545. case SpvOp::SpvOpBitwiseOr:
  546. case SpvOp::SpvOpBitwiseXor:
  547. case SpvOp::SpvOpIAdd:
  548. case SpvOp::SpvOpIEqual:
  549. case SpvOp::SpvOpIMul:
  550. case SpvOp::SpvOpINotEqual:
  551. case SpvOp::SpvOpISub:
  552. case SpvOp::SpvOpLogicalAnd:
  553. case SpvOp::SpvOpLogicalEqual:
  554. case SpvOp::SpvOpLogicalNot:
  555. case SpvOp::SpvOpLogicalNotEqual:
  556. case SpvOp::SpvOpLogicalOr:
  557. case SpvOp::SpvOpNot:
  558. case SpvOp::SpvOpSDiv:
  559. case SpvOp::SpvOpSelect:
  560. case SpvOp::SpvOpSGreaterThan:
  561. case SpvOp::SpvOpSGreaterThanEqual:
  562. case SpvOp::SpvOpShiftLeftLogical:
  563. case SpvOp::SpvOpShiftRightArithmetic:
  564. case SpvOp::SpvOpShiftRightLogical:
  565. case SpvOp::SpvOpSLessThan:
  566. case SpvOp::SpvOpSLessThanEqual:
  567. case SpvOp::SpvOpSMod:
  568. case SpvOp::SpvOpSNegate:
  569. case SpvOp::SpvOpSRem:
  570. case SpvOp::SpvOpSConvert:
  571. case SpvOp::SpvOpUConvert:
  572. case SpvOp::SpvOpUDiv:
  573. case SpvOp::SpvOpUGreaterThan:
  574. case SpvOp::SpvOpUGreaterThanEqual:
  575. case SpvOp::SpvOpULessThan:
  576. case SpvOp::SpvOpULessThanEqual:
  577. case SpvOp::SpvOpUMod:
  578. return true;
  579. default:
  580. return false;
  581. }
  582. }
  583. bool InstructionFolder::IsFoldableConstant(
  584. const analysis::Constant* cst) const {
  585. // Currently supported constants are 32-bit values or null constants.
  586. if (const analysis::ScalarConstant* scalar = cst->AsScalarConstant())
  587. return scalar->words().size() == 1;
  588. else
  589. return cst->AsNullConstant() != nullptr;
  590. }
  591. Instruction* InstructionFolder::FoldInstructionToConstant(
  592. Instruction* inst, std::function<uint32_t(uint32_t)> id_map) const {
  593. analysis::ConstantManager* const_mgr = context_->get_constant_mgr();
  594. if (!inst->IsFoldableByFoldScalar() &&
  595. !GetConstantFoldingRules().HasFoldingRule(inst)) {
  596. return nullptr;
  597. }
  598. // Collect the values of the constant parameters.
  599. std::vector<const analysis::Constant*> constants;
  600. bool missing_constants = false;
  601. inst->ForEachInId([&constants, &missing_constants, const_mgr,
  602. &id_map](uint32_t* op_id) {
  603. uint32_t id = id_map(*op_id);
  604. const analysis::Constant* const_op = const_mgr->FindDeclaredConstant(id);
  605. if (!const_op) {
  606. constants.push_back(nullptr);
  607. missing_constants = true;
  608. } else {
  609. constants.push_back(const_op);
  610. }
  611. });
  612. const analysis::Constant* folded_const = nullptr;
  613. for (auto rule : GetConstantFoldingRules().GetRulesForInstruction(inst)) {
  614. folded_const = rule(context_, inst, constants);
  615. if (folded_const != nullptr) {
  616. Instruction* const_inst =
  617. const_mgr->GetDefiningInstruction(folded_const, inst->type_id());
  618. if (const_inst == nullptr) {
  619. return nullptr;
  620. }
  621. assert(const_inst->type_id() == inst->type_id());
  622. // May be a new instruction that needs to be analysed.
  623. context_->UpdateDefUse(const_inst);
  624. return const_inst;
  625. }
  626. }
  627. uint32_t result_val = 0;
  628. bool successful = false;
  629. // If all parameters are constant, fold the instruction to a constant.
  630. if (!missing_constants && inst->IsFoldableByFoldScalar()) {
  631. result_val = FoldScalars(inst->opcode(), constants);
  632. successful = true;
  633. }
  634. if (!successful && inst->IsFoldableByFoldScalar()) {
  635. successful = FoldIntegerOpToConstant(inst, id_map, &result_val);
  636. }
  637. if (successful) {
  638. const analysis::Constant* result_const =
  639. const_mgr->GetConstant(const_mgr->GetType(inst), {result_val});
  640. Instruction* folded_inst =
  641. const_mgr->GetDefiningInstruction(result_const, inst->type_id());
  642. return folded_inst;
  643. }
  644. return nullptr;
  645. }
  646. bool InstructionFolder::IsFoldableType(Instruction* type_inst) const {
  647. // Support 32-bit integers.
  648. if (type_inst->opcode() == SpvOpTypeInt) {
  649. return type_inst->GetSingleWordInOperand(0) == 32;
  650. }
  651. // Support booleans.
  652. if (type_inst->opcode() == SpvOpTypeBool) {
  653. return true;
  654. }
  655. // Nothing else yet.
  656. return false;
  657. }
  658. bool InstructionFolder::FoldInstruction(Instruction* inst) const {
  659. bool modified = false;
  660. Instruction* folded_inst(inst);
  661. while (folded_inst->opcode() != SpvOpCopyObject &&
  662. FoldInstructionInternal(&*folded_inst)) {
  663. modified = true;
  664. }
  665. return modified;
  666. }
  667. } // namespace opt
  668. } // namespace spvtools