fold.cpp 22 KB

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