fold.cpp 20 KB

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