folding_rules.cpp 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695
  1. // Copyright (c) 2018 Google LLC
  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/folding_rules.h"
  15. #include <climits>
  16. #include <limits>
  17. #include <memory>
  18. #include <utility>
  19. #include "ir_builder.h"
  20. #include "source/latest_version_glsl_std_450_header.h"
  21. #include "source/opt/ir_context.h"
  22. namespace spvtools {
  23. namespace opt {
  24. namespace {
  25. const uint32_t kExtractCompositeIdInIdx = 0;
  26. const uint32_t kInsertObjectIdInIdx = 0;
  27. const uint32_t kInsertCompositeIdInIdx = 1;
  28. const uint32_t kExtInstSetIdInIdx = 0;
  29. const uint32_t kExtInstInstructionInIdx = 1;
  30. const uint32_t kFMixXIdInIdx = 2;
  31. const uint32_t kFMixYIdInIdx = 3;
  32. const uint32_t kFMixAIdInIdx = 4;
  33. const uint32_t kStoreObjectInIdx = 1;
  34. // Some image instructions may contain an "image operands" argument.
  35. // Returns the operand index for the "image operands".
  36. // Returns -1 if the instruction does not have image operands.
  37. int32_t ImageOperandsMaskInOperandIndex(Instruction* inst) {
  38. const auto opcode = inst->opcode();
  39. switch (opcode) {
  40. case SpvOpImageSampleImplicitLod:
  41. case SpvOpImageSampleExplicitLod:
  42. case SpvOpImageSampleProjImplicitLod:
  43. case SpvOpImageSampleProjExplicitLod:
  44. case SpvOpImageFetch:
  45. case SpvOpImageRead:
  46. case SpvOpImageSparseSampleImplicitLod:
  47. case SpvOpImageSparseSampleExplicitLod:
  48. case SpvOpImageSparseSampleProjImplicitLod:
  49. case SpvOpImageSparseSampleProjExplicitLod:
  50. case SpvOpImageSparseFetch:
  51. case SpvOpImageSparseRead:
  52. return inst->NumOperands() > 4 ? 2 : -1;
  53. case SpvOpImageSampleDrefImplicitLod:
  54. case SpvOpImageSampleDrefExplicitLod:
  55. case SpvOpImageSampleProjDrefImplicitLod:
  56. case SpvOpImageSampleProjDrefExplicitLod:
  57. case SpvOpImageGather:
  58. case SpvOpImageDrefGather:
  59. case SpvOpImageSparseSampleDrefImplicitLod:
  60. case SpvOpImageSparseSampleDrefExplicitLod:
  61. case SpvOpImageSparseSampleProjDrefImplicitLod:
  62. case SpvOpImageSparseSampleProjDrefExplicitLod:
  63. case SpvOpImageSparseGather:
  64. case SpvOpImageSparseDrefGather:
  65. return inst->NumOperands() > 5 ? 3 : -1;
  66. case SpvOpImageWrite:
  67. return inst->NumOperands() > 3 ? 3 : -1;
  68. default:
  69. return -1;
  70. }
  71. }
  72. // Returns the element width of |type|.
  73. uint32_t ElementWidth(const analysis::Type* type) {
  74. if (const analysis::Vector* vec_type = type->AsVector()) {
  75. return ElementWidth(vec_type->element_type());
  76. } else if (const analysis::Float* float_type = type->AsFloat()) {
  77. return float_type->width();
  78. } else {
  79. assert(type->AsInteger());
  80. return type->AsInteger()->width();
  81. }
  82. }
  83. // Returns true if |type| is Float or a vector of Float.
  84. bool HasFloatingPoint(const analysis::Type* type) {
  85. if (type->AsFloat()) {
  86. return true;
  87. } else if (const analysis::Vector* vec_type = type->AsVector()) {
  88. return vec_type->element_type()->AsFloat() != nullptr;
  89. }
  90. return false;
  91. }
  92. // Returns false if |val| is NaN, infinite or subnormal.
  93. template <typename T>
  94. bool IsValidResult(T val) {
  95. int classified = std::fpclassify(val);
  96. switch (classified) {
  97. case FP_NAN:
  98. case FP_INFINITE:
  99. case FP_SUBNORMAL:
  100. return false;
  101. default:
  102. return true;
  103. }
  104. }
  105. const analysis::Constant* ConstInput(
  106. const std::vector<const analysis::Constant*>& constants) {
  107. return constants[0] ? constants[0] : constants[1];
  108. }
  109. Instruction* NonConstInput(IRContext* context, const analysis::Constant* c,
  110. Instruction* inst) {
  111. uint32_t in_op = c ? 1u : 0u;
  112. return context->get_def_use_mgr()->GetDef(
  113. inst->GetSingleWordInOperand(in_op));
  114. }
  115. std::vector<uint32_t> ExtractInts(uint64_t val) {
  116. std::vector<uint32_t> words;
  117. words.push_back(static_cast<uint32_t>(val));
  118. words.push_back(static_cast<uint32_t>(val >> 32));
  119. return words;
  120. }
  121. std::vector<uint32_t> GetWordsFromScalarIntConstant(
  122. const analysis::IntConstant* c) {
  123. assert(c != nullptr);
  124. uint32_t width = c->type()->AsInteger()->width();
  125. assert(width == 32 || width == 64);
  126. if (width == 64) {
  127. uint64_t uval = static_cast<uint64_t>(c->GetU64());
  128. return ExtractInts(uval);
  129. }
  130. return {c->GetU32()};
  131. }
  132. std::vector<uint32_t> GetWordsFromScalarFloatConstant(
  133. const analysis::FloatConstant* c) {
  134. assert(c != nullptr);
  135. uint32_t width = c->type()->AsFloat()->width();
  136. assert(width == 32 || width == 64);
  137. if (width == 64) {
  138. utils::FloatProxy<double> result(c->GetDouble());
  139. return result.GetWords();
  140. }
  141. utils::FloatProxy<float> result(c->GetFloat());
  142. return result.GetWords();
  143. }
  144. std::vector<uint32_t> GetWordsFromNumericScalarOrVectorConstant(
  145. analysis::ConstantManager* const_mgr, const analysis::Constant* c) {
  146. if (const auto* float_constant = c->AsFloatConstant()) {
  147. return GetWordsFromScalarFloatConstant(float_constant);
  148. } else if (const auto* int_constant = c->AsIntConstant()) {
  149. return GetWordsFromScalarIntConstant(int_constant);
  150. } else if (const auto* vec_constant = c->AsVectorConstant()) {
  151. std::vector<uint32_t> words;
  152. for (const auto* comp : vec_constant->GetComponents()) {
  153. auto comp_in_words =
  154. GetWordsFromNumericScalarOrVectorConstant(const_mgr, comp);
  155. words.insert(words.end(), comp_in_words.begin(), comp_in_words.end());
  156. }
  157. return words;
  158. }
  159. return {};
  160. }
  161. const analysis::Constant* ConvertWordsToNumericScalarOrVectorConstant(
  162. analysis::ConstantManager* const_mgr, const std::vector<uint32_t>& words,
  163. const analysis::Type* type) {
  164. if (type->AsInteger() || type->AsFloat())
  165. return const_mgr->GetConstant(type, words);
  166. if (const auto* vec_type = type->AsVector())
  167. return const_mgr->GetNumericVectorConstantWithWords(vec_type, words);
  168. return nullptr;
  169. }
  170. // Returns the negation of |c|. |c| must be a 32 or 64 bit floating point
  171. // constant.
  172. uint32_t NegateFloatingPointConstant(analysis::ConstantManager* const_mgr,
  173. const analysis::Constant* c) {
  174. assert(c);
  175. assert(c->type()->AsFloat());
  176. uint32_t width = c->type()->AsFloat()->width();
  177. assert(width == 32 || width == 64);
  178. std::vector<uint32_t> words;
  179. if (width == 64) {
  180. utils::FloatProxy<double> result(c->GetDouble() * -1.0);
  181. words = result.GetWords();
  182. } else {
  183. utils::FloatProxy<float> result(c->GetFloat() * -1.0f);
  184. words = result.GetWords();
  185. }
  186. const analysis::Constant* negated_const =
  187. const_mgr->GetConstant(c->type(), std::move(words));
  188. return const_mgr->GetDefiningInstruction(negated_const)->result_id();
  189. }
  190. // Negates the integer constant |c|. Returns the id of the defining instruction.
  191. uint32_t NegateIntegerConstant(analysis::ConstantManager* const_mgr,
  192. const analysis::Constant* c) {
  193. assert(c);
  194. assert(c->type()->AsInteger());
  195. uint32_t width = c->type()->AsInteger()->width();
  196. assert(width == 32 || width == 64);
  197. std::vector<uint32_t> words;
  198. if (width == 64) {
  199. uint64_t uval = static_cast<uint64_t>(0 - c->GetU64());
  200. words = ExtractInts(uval);
  201. } else {
  202. words.push_back(static_cast<uint32_t>(0 - c->GetU32()));
  203. }
  204. const analysis::Constant* negated_const =
  205. const_mgr->GetConstant(c->type(), std::move(words));
  206. return const_mgr->GetDefiningInstruction(negated_const)->result_id();
  207. }
  208. // Negates the vector constant |c|. Returns the id of the defining instruction.
  209. uint32_t NegateVectorConstant(analysis::ConstantManager* const_mgr,
  210. const analysis::Constant* c) {
  211. assert(const_mgr && c);
  212. assert(c->type()->AsVector());
  213. if (c->AsNullConstant()) {
  214. // 0.0 vs -0.0 shouldn't matter.
  215. return const_mgr->GetDefiningInstruction(c)->result_id();
  216. } else {
  217. const analysis::Type* component_type =
  218. c->AsVectorConstant()->component_type();
  219. std::vector<uint32_t> words;
  220. for (auto& comp : c->AsVectorConstant()->GetComponents()) {
  221. if (component_type->AsFloat()) {
  222. words.push_back(NegateFloatingPointConstant(const_mgr, comp));
  223. } else {
  224. assert(component_type->AsInteger());
  225. words.push_back(NegateIntegerConstant(const_mgr, comp));
  226. }
  227. }
  228. const analysis::Constant* negated_const =
  229. const_mgr->GetConstant(c->type(), std::move(words));
  230. return const_mgr->GetDefiningInstruction(negated_const)->result_id();
  231. }
  232. }
  233. // Negates |c|. Returns the id of the defining instruction.
  234. uint32_t NegateConstant(analysis::ConstantManager* const_mgr,
  235. const analysis::Constant* c) {
  236. if (c->type()->AsVector()) {
  237. return NegateVectorConstant(const_mgr, c);
  238. } else if (c->type()->AsFloat()) {
  239. return NegateFloatingPointConstant(const_mgr, c);
  240. } else {
  241. assert(c->type()->AsInteger());
  242. return NegateIntegerConstant(const_mgr, c);
  243. }
  244. }
  245. // Takes the reciprocal of |c|. |c|'s type must be Float or a vector of Float.
  246. // Returns 0 if the reciprocal is NaN, infinite or subnormal.
  247. uint32_t Reciprocal(analysis::ConstantManager* const_mgr,
  248. const analysis::Constant* c) {
  249. assert(const_mgr && c);
  250. assert(c->type()->AsFloat());
  251. uint32_t width = c->type()->AsFloat()->width();
  252. assert(width == 32 || width == 64);
  253. std::vector<uint32_t> words;
  254. if (width == 64) {
  255. spvtools::utils::FloatProxy<double> result(1.0 / c->GetDouble());
  256. if (!IsValidResult(result.getAsFloat())) return 0;
  257. words = result.GetWords();
  258. } else {
  259. spvtools::utils::FloatProxy<float> result(1.0f / c->GetFloat());
  260. if (!IsValidResult(result.getAsFloat())) return 0;
  261. words = result.GetWords();
  262. }
  263. const analysis::Constant* negated_const =
  264. const_mgr->GetConstant(c->type(), std::move(words));
  265. return const_mgr->GetDefiningInstruction(negated_const)->result_id();
  266. }
  267. // Replaces fdiv where second operand is constant with fmul.
  268. FoldingRule ReciprocalFDiv() {
  269. return [](IRContext* context, Instruction* inst,
  270. const std::vector<const analysis::Constant*>& constants) {
  271. assert(inst->opcode() == SpvOpFDiv);
  272. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  273. const analysis::Type* type =
  274. context->get_type_mgr()->GetType(inst->type_id());
  275. if (!inst->IsFloatingPointFoldingAllowed()) return false;
  276. uint32_t width = ElementWidth(type);
  277. if (width != 32 && width != 64) return false;
  278. if (constants[1] != nullptr) {
  279. uint32_t id = 0;
  280. if (const analysis::VectorConstant* vector_const =
  281. constants[1]->AsVectorConstant()) {
  282. std::vector<uint32_t> neg_ids;
  283. for (auto& comp : vector_const->GetComponents()) {
  284. id = Reciprocal(const_mgr, comp);
  285. if (id == 0) return false;
  286. neg_ids.push_back(id);
  287. }
  288. const analysis::Constant* negated_const =
  289. const_mgr->GetConstant(constants[1]->type(), std::move(neg_ids));
  290. id = const_mgr->GetDefiningInstruction(negated_const)->result_id();
  291. } else if (constants[1]->AsFloatConstant()) {
  292. id = Reciprocal(const_mgr, constants[1]);
  293. if (id == 0) return false;
  294. } else {
  295. // Don't fold a null constant.
  296. return false;
  297. }
  298. inst->SetOpcode(SpvOpFMul);
  299. inst->SetInOperands(
  300. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(0u)}},
  301. {SPV_OPERAND_TYPE_ID, {id}}});
  302. return true;
  303. }
  304. return false;
  305. };
  306. }
  307. // Elides consecutive negate instructions.
  308. FoldingRule MergeNegateArithmetic() {
  309. return [](IRContext* context, Instruction* inst,
  310. const std::vector<const analysis::Constant*>& constants) {
  311. assert(inst->opcode() == SpvOpFNegate || inst->opcode() == SpvOpSNegate);
  312. (void)constants;
  313. const analysis::Type* type =
  314. context->get_type_mgr()->GetType(inst->type_id());
  315. if (HasFloatingPoint(type) && !inst->IsFloatingPointFoldingAllowed())
  316. return false;
  317. Instruction* op_inst =
  318. context->get_def_use_mgr()->GetDef(inst->GetSingleWordInOperand(0u));
  319. if (HasFloatingPoint(type) && !op_inst->IsFloatingPointFoldingAllowed())
  320. return false;
  321. if (op_inst->opcode() == inst->opcode()) {
  322. // Elide negates.
  323. inst->SetOpcode(SpvOpCopyObject);
  324. inst->SetInOperands(
  325. {{SPV_OPERAND_TYPE_ID, {op_inst->GetSingleWordInOperand(0u)}}});
  326. return true;
  327. }
  328. return false;
  329. };
  330. }
  331. // Merges negate into a mul or div operation if that operation contains a
  332. // constant operand.
  333. // Cases:
  334. // -(x * 2) = x * -2
  335. // -(2 * x) = x * -2
  336. // -(x / 2) = x / -2
  337. // -(2 / x) = -2 / x
  338. FoldingRule MergeNegateMulDivArithmetic() {
  339. return [](IRContext* context, Instruction* inst,
  340. const std::vector<const analysis::Constant*>& constants) {
  341. assert(inst->opcode() == SpvOpFNegate || inst->opcode() == SpvOpSNegate);
  342. (void)constants;
  343. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  344. const analysis::Type* type =
  345. context->get_type_mgr()->GetType(inst->type_id());
  346. if (HasFloatingPoint(type) && !inst->IsFloatingPointFoldingAllowed())
  347. return false;
  348. Instruction* op_inst =
  349. context->get_def_use_mgr()->GetDef(inst->GetSingleWordInOperand(0u));
  350. if (HasFloatingPoint(type) && !op_inst->IsFloatingPointFoldingAllowed())
  351. return false;
  352. uint32_t width = ElementWidth(type);
  353. if (width != 32 && width != 64) return false;
  354. SpvOp opcode = op_inst->opcode();
  355. if (opcode == SpvOpFMul || opcode == SpvOpFDiv || opcode == SpvOpIMul ||
  356. opcode == SpvOpSDiv || opcode == SpvOpUDiv) {
  357. std::vector<const analysis::Constant*> op_constants =
  358. const_mgr->GetOperandConstants(op_inst);
  359. // Merge negate into mul or div if one operand is constant.
  360. if (op_constants[0] || op_constants[1]) {
  361. bool zero_is_variable = op_constants[0] == nullptr;
  362. const analysis::Constant* c = ConstInput(op_constants);
  363. uint32_t neg_id = NegateConstant(const_mgr, c);
  364. uint32_t non_const_id = zero_is_variable
  365. ? op_inst->GetSingleWordInOperand(0u)
  366. : op_inst->GetSingleWordInOperand(1u);
  367. // Change this instruction to a mul/div.
  368. inst->SetOpcode(op_inst->opcode());
  369. if (opcode == SpvOpFDiv || opcode == SpvOpUDiv || opcode == SpvOpSDiv) {
  370. uint32_t op0 = zero_is_variable ? non_const_id : neg_id;
  371. uint32_t op1 = zero_is_variable ? neg_id : non_const_id;
  372. inst->SetInOperands(
  373. {{SPV_OPERAND_TYPE_ID, {op0}}, {SPV_OPERAND_TYPE_ID, {op1}}});
  374. } else {
  375. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {non_const_id}},
  376. {SPV_OPERAND_TYPE_ID, {neg_id}}});
  377. }
  378. return true;
  379. }
  380. }
  381. return false;
  382. };
  383. }
  384. // Merges negate into a add or sub operation if that operation contains a
  385. // constant operand.
  386. // Cases:
  387. // -(x + 2) = -2 - x
  388. // -(2 + x) = -2 - x
  389. // -(x - 2) = 2 - x
  390. // -(2 - x) = x - 2
  391. FoldingRule MergeNegateAddSubArithmetic() {
  392. return [](IRContext* context, Instruction* inst,
  393. const std::vector<const analysis::Constant*>& constants) {
  394. assert(inst->opcode() == SpvOpFNegate || inst->opcode() == SpvOpSNegate);
  395. (void)constants;
  396. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  397. const analysis::Type* type =
  398. context->get_type_mgr()->GetType(inst->type_id());
  399. if (HasFloatingPoint(type) && !inst->IsFloatingPointFoldingAllowed())
  400. return false;
  401. Instruction* op_inst =
  402. context->get_def_use_mgr()->GetDef(inst->GetSingleWordInOperand(0u));
  403. if (HasFloatingPoint(type) && !op_inst->IsFloatingPointFoldingAllowed())
  404. return false;
  405. uint32_t width = ElementWidth(type);
  406. if (width != 32 && width != 64) return false;
  407. if (op_inst->opcode() == SpvOpFAdd || op_inst->opcode() == SpvOpFSub ||
  408. op_inst->opcode() == SpvOpIAdd || op_inst->opcode() == SpvOpISub) {
  409. std::vector<const analysis::Constant*> op_constants =
  410. const_mgr->GetOperandConstants(op_inst);
  411. if (op_constants[0] || op_constants[1]) {
  412. bool zero_is_variable = op_constants[0] == nullptr;
  413. bool is_add = (op_inst->opcode() == SpvOpFAdd) ||
  414. (op_inst->opcode() == SpvOpIAdd);
  415. bool swap_operands = !is_add || zero_is_variable;
  416. bool negate_const = is_add;
  417. const analysis::Constant* c = ConstInput(op_constants);
  418. uint32_t const_id = 0;
  419. if (negate_const) {
  420. const_id = NegateConstant(const_mgr, c);
  421. } else {
  422. const_id = zero_is_variable ? op_inst->GetSingleWordInOperand(1u)
  423. : op_inst->GetSingleWordInOperand(0u);
  424. }
  425. // Swap operands if necessary and make the instruction a subtraction.
  426. uint32_t op0 =
  427. zero_is_variable ? op_inst->GetSingleWordInOperand(0u) : const_id;
  428. uint32_t op1 =
  429. zero_is_variable ? const_id : op_inst->GetSingleWordInOperand(1u);
  430. if (swap_operands) std::swap(op0, op1);
  431. inst->SetOpcode(HasFloatingPoint(type) ? SpvOpFSub : SpvOpISub);
  432. inst->SetInOperands(
  433. {{SPV_OPERAND_TYPE_ID, {op0}}, {SPV_OPERAND_TYPE_ID, {op1}}});
  434. return true;
  435. }
  436. }
  437. return false;
  438. };
  439. }
  440. // Returns true if |c| has a zero element.
  441. bool HasZero(const analysis::Constant* c) {
  442. if (c->AsNullConstant()) {
  443. return true;
  444. }
  445. if (const analysis::VectorConstant* vec_const = c->AsVectorConstant()) {
  446. for (auto& comp : vec_const->GetComponents())
  447. if (HasZero(comp)) return true;
  448. } else {
  449. assert(c->AsScalarConstant());
  450. return c->AsScalarConstant()->IsZero();
  451. }
  452. return false;
  453. }
  454. // Performs |input1| |opcode| |input2| and returns the merged constant result
  455. // id. Returns 0 if the result is not a valid value. The input types must be
  456. // Float.
  457. uint32_t PerformFloatingPointOperation(analysis::ConstantManager* const_mgr,
  458. SpvOp opcode,
  459. const analysis::Constant* input1,
  460. const analysis::Constant* input2) {
  461. const analysis::Type* type = input1->type();
  462. assert(type->AsFloat());
  463. uint32_t width = type->AsFloat()->width();
  464. assert(width == 32 || width == 64);
  465. std::vector<uint32_t> words;
  466. #define FOLD_OP(op) \
  467. if (width == 64) { \
  468. utils::FloatProxy<double> val = \
  469. input1->GetDouble() op input2->GetDouble(); \
  470. double dval = val.getAsFloat(); \
  471. if (!IsValidResult(dval)) return 0; \
  472. words = val.GetWords(); \
  473. } else { \
  474. utils::FloatProxy<float> val = input1->GetFloat() op input2->GetFloat(); \
  475. float fval = val.getAsFloat(); \
  476. if (!IsValidResult(fval)) return 0; \
  477. words = val.GetWords(); \
  478. } \
  479. static_assert(true, "require extra semicolon")
  480. switch (opcode) {
  481. case SpvOpFMul:
  482. FOLD_OP(*);
  483. break;
  484. case SpvOpFDiv:
  485. if (HasZero(input2)) return 0;
  486. FOLD_OP(/);
  487. break;
  488. case SpvOpFAdd:
  489. FOLD_OP(+);
  490. break;
  491. case SpvOpFSub:
  492. FOLD_OP(-);
  493. break;
  494. default:
  495. assert(false && "Unexpected operation");
  496. break;
  497. }
  498. #undef FOLD_OP
  499. const analysis::Constant* merged_const = const_mgr->GetConstant(type, words);
  500. return const_mgr->GetDefiningInstruction(merged_const)->result_id();
  501. }
  502. // Performs |input1| |opcode| |input2| and returns the merged constant result
  503. // id. Returns 0 if the result is not a valid value. The input types must be
  504. // Integers.
  505. uint32_t PerformIntegerOperation(analysis::ConstantManager* const_mgr,
  506. SpvOp opcode, const analysis::Constant* input1,
  507. const analysis::Constant* input2) {
  508. assert(input1->type()->AsInteger());
  509. const analysis::Integer* type = input1->type()->AsInteger();
  510. uint32_t width = type->AsInteger()->width();
  511. assert(width == 32 || width == 64);
  512. std::vector<uint32_t> words;
  513. // Regardless of the sign of the constant, folding is performed on an unsigned
  514. // interpretation of the constant data. This avoids signed integer overflow
  515. // while folding, and works because sign is irrelevant for the IAdd, ISub and
  516. // IMul instructions.
  517. #define FOLD_OP(op) \
  518. if (width == 64) { \
  519. uint64_t val = input1->GetU64() op input2->GetU64(); \
  520. words = ExtractInts(val); \
  521. } else { \
  522. uint32_t val = input1->GetU32() op input2->GetU32(); \
  523. words.push_back(val); \
  524. } \
  525. static_assert(true, "require extra semicolon")
  526. switch (opcode) {
  527. case SpvOpIMul:
  528. FOLD_OP(*);
  529. break;
  530. case SpvOpSDiv:
  531. case SpvOpUDiv:
  532. assert(false && "Should not merge integer division");
  533. break;
  534. case SpvOpIAdd:
  535. FOLD_OP(+);
  536. break;
  537. case SpvOpISub:
  538. FOLD_OP(-);
  539. break;
  540. default:
  541. assert(false && "Unexpected operation");
  542. break;
  543. }
  544. #undef FOLD_OP
  545. const analysis::Constant* merged_const = const_mgr->GetConstant(type, words);
  546. return const_mgr->GetDefiningInstruction(merged_const)->result_id();
  547. }
  548. // Performs |input1| |opcode| |input2| and returns the merged constant result
  549. // id. Returns 0 if the result is not a valid value. The input types must be
  550. // Integers, Floats or Vectors of such.
  551. uint32_t PerformOperation(analysis::ConstantManager* const_mgr, SpvOp opcode,
  552. const analysis::Constant* input1,
  553. const analysis::Constant* input2) {
  554. assert(input1 && input2);
  555. const analysis::Type* type = input1->type();
  556. std::vector<uint32_t> words;
  557. if (const analysis::Vector* vector_type = type->AsVector()) {
  558. const analysis::Type* ele_type = vector_type->element_type();
  559. for (uint32_t i = 0; i != vector_type->element_count(); ++i) {
  560. uint32_t id = 0;
  561. const analysis::Constant* input1_comp = nullptr;
  562. if (const analysis::VectorConstant* input1_vector =
  563. input1->AsVectorConstant()) {
  564. input1_comp = input1_vector->GetComponents()[i];
  565. } else {
  566. assert(input1->AsNullConstant());
  567. input1_comp = const_mgr->GetConstant(ele_type, {});
  568. }
  569. const analysis::Constant* input2_comp = nullptr;
  570. if (const analysis::VectorConstant* input2_vector =
  571. input2->AsVectorConstant()) {
  572. input2_comp = input2_vector->GetComponents()[i];
  573. } else {
  574. assert(input2->AsNullConstant());
  575. input2_comp = const_mgr->GetConstant(ele_type, {});
  576. }
  577. if (ele_type->AsFloat()) {
  578. id = PerformFloatingPointOperation(const_mgr, opcode, input1_comp,
  579. input2_comp);
  580. } else {
  581. assert(ele_type->AsInteger());
  582. id = PerformIntegerOperation(const_mgr, opcode, input1_comp,
  583. input2_comp);
  584. }
  585. if (id == 0) return 0;
  586. words.push_back(id);
  587. }
  588. const analysis::Constant* merged_const =
  589. const_mgr->GetConstant(type, words);
  590. return const_mgr->GetDefiningInstruction(merged_const)->result_id();
  591. } else if (type->AsFloat()) {
  592. return PerformFloatingPointOperation(const_mgr, opcode, input1, input2);
  593. } else {
  594. assert(type->AsInteger());
  595. return PerformIntegerOperation(const_mgr, opcode, input1, input2);
  596. }
  597. }
  598. // Merges consecutive multiplies where each contains one constant operand.
  599. // Cases:
  600. // 2 * (x * 2) = x * 4
  601. // 2 * (2 * x) = x * 4
  602. // (x * 2) * 2 = x * 4
  603. // (2 * x) * 2 = x * 4
  604. FoldingRule MergeMulMulArithmetic() {
  605. return [](IRContext* context, Instruction* inst,
  606. const std::vector<const analysis::Constant*>& constants) {
  607. assert(inst->opcode() == SpvOpFMul || inst->opcode() == SpvOpIMul);
  608. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  609. const analysis::Type* type =
  610. context->get_type_mgr()->GetType(inst->type_id());
  611. if (HasFloatingPoint(type) && !inst->IsFloatingPointFoldingAllowed())
  612. return false;
  613. uint32_t width = ElementWidth(type);
  614. if (width != 32 && width != 64) return false;
  615. // Determine the constant input and the variable input in |inst|.
  616. const analysis::Constant* const_input1 = ConstInput(constants);
  617. if (!const_input1) return false;
  618. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  619. if (HasFloatingPoint(type) && !other_inst->IsFloatingPointFoldingAllowed())
  620. return false;
  621. if (other_inst->opcode() == inst->opcode()) {
  622. std::vector<const analysis::Constant*> other_constants =
  623. const_mgr->GetOperandConstants(other_inst);
  624. const analysis::Constant* const_input2 = ConstInput(other_constants);
  625. if (!const_input2) return false;
  626. bool other_first_is_variable = other_constants[0] == nullptr;
  627. uint32_t merged_id = PerformOperation(const_mgr, inst->opcode(),
  628. const_input1, const_input2);
  629. if (merged_id == 0) return false;
  630. uint32_t non_const_id = other_first_is_variable
  631. ? other_inst->GetSingleWordInOperand(0u)
  632. : other_inst->GetSingleWordInOperand(1u);
  633. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {non_const_id}},
  634. {SPV_OPERAND_TYPE_ID, {merged_id}}});
  635. return true;
  636. }
  637. return false;
  638. };
  639. }
  640. // Merges divides into subsequent multiplies if each instruction contains one
  641. // constant operand. Does not support integer operations.
  642. // Cases:
  643. // 2 * (x / 2) = x * 1
  644. // 2 * (2 / x) = 4 / x
  645. // (x / 2) * 2 = x * 1
  646. // (2 / x) * 2 = 4 / x
  647. // (y / x) * x = y
  648. // x * (y / x) = y
  649. FoldingRule MergeMulDivArithmetic() {
  650. return [](IRContext* context, Instruction* inst,
  651. const std::vector<const analysis::Constant*>& constants) {
  652. assert(inst->opcode() == SpvOpFMul);
  653. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  654. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  655. const analysis::Type* type =
  656. context->get_type_mgr()->GetType(inst->type_id());
  657. if (!inst->IsFloatingPointFoldingAllowed()) return false;
  658. uint32_t width = ElementWidth(type);
  659. if (width != 32 && width != 64) return false;
  660. for (uint32_t i = 0; i < 2; i++) {
  661. uint32_t op_id = inst->GetSingleWordInOperand(i);
  662. Instruction* op_inst = def_use_mgr->GetDef(op_id);
  663. if (op_inst->opcode() == SpvOpFDiv) {
  664. if (op_inst->GetSingleWordInOperand(1) ==
  665. inst->GetSingleWordInOperand(1 - i)) {
  666. inst->SetOpcode(SpvOpCopyObject);
  667. inst->SetInOperands(
  668. {{SPV_OPERAND_TYPE_ID, {op_inst->GetSingleWordInOperand(0)}}});
  669. return true;
  670. }
  671. }
  672. }
  673. const analysis::Constant* const_input1 = ConstInput(constants);
  674. if (!const_input1) return false;
  675. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  676. if (!other_inst->IsFloatingPointFoldingAllowed()) return false;
  677. if (other_inst->opcode() == SpvOpFDiv) {
  678. std::vector<const analysis::Constant*> other_constants =
  679. const_mgr->GetOperandConstants(other_inst);
  680. const analysis::Constant* const_input2 = ConstInput(other_constants);
  681. if (!const_input2 || HasZero(const_input2)) return false;
  682. bool other_first_is_variable = other_constants[0] == nullptr;
  683. // If the variable value is the second operand of the divide, multiply
  684. // the constants together. Otherwise divide the constants.
  685. uint32_t merged_id = PerformOperation(
  686. const_mgr,
  687. other_first_is_variable ? other_inst->opcode() : inst->opcode(),
  688. const_input1, const_input2);
  689. if (merged_id == 0) return false;
  690. uint32_t non_const_id = other_first_is_variable
  691. ? other_inst->GetSingleWordInOperand(0u)
  692. : other_inst->GetSingleWordInOperand(1u);
  693. // If the variable value is on the second operand of the div, then this
  694. // operation is a div. Otherwise it should be a multiply.
  695. inst->SetOpcode(other_first_is_variable ? inst->opcode()
  696. : other_inst->opcode());
  697. if (other_first_is_variable) {
  698. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {non_const_id}},
  699. {SPV_OPERAND_TYPE_ID, {merged_id}}});
  700. } else {
  701. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {merged_id}},
  702. {SPV_OPERAND_TYPE_ID, {non_const_id}}});
  703. }
  704. return true;
  705. }
  706. return false;
  707. };
  708. }
  709. // Merges multiply of constant and negation.
  710. // Cases:
  711. // (-x) * 2 = x * -2
  712. // 2 * (-x) = x * -2
  713. FoldingRule MergeMulNegateArithmetic() {
  714. return [](IRContext* context, Instruction* inst,
  715. const std::vector<const analysis::Constant*>& constants) {
  716. assert(inst->opcode() == SpvOpFMul || inst->opcode() == SpvOpIMul);
  717. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  718. const analysis::Type* type =
  719. context->get_type_mgr()->GetType(inst->type_id());
  720. bool uses_float = HasFloatingPoint(type);
  721. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  722. uint32_t width = ElementWidth(type);
  723. if (width != 32 && width != 64) return false;
  724. const analysis::Constant* const_input1 = ConstInput(constants);
  725. if (!const_input1) return false;
  726. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  727. if (uses_float && !other_inst->IsFloatingPointFoldingAllowed())
  728. return false;
  729. if (other_inst->opcode() == SpvOpFNegate ||
  730. other_inst->opcode() == SpvOpSNegate) {
  731. uint32_t neg_id = NegateConstant(const_mgr, const_input1);
  732. inst->SetInOperands(
  733. {{SPV_OPERAND_TYPE_ID, {other_inst->GetSingleWordInOperand(0u)}},
  734. {SPV_OPERAND_TYPE_ID, {neg_id}}});
  735. return true;
  736. }
  737. return false;
  738. };
  739. }
  740. // Merges consecutive divides if each instruction contains one constant operand.
  741. // Does not support integer division.
  742. // Cases:
  743. // 2 / (x / 2) = 4 / x
  744. // 4 / (2 / x) = 2 * x
  745. // (4 / x) / 2 = 2 / x
  746. // (x / 2) / 2 = x / 4
  747. FoldingRule MergeDivDivArithmetic() {
  748. return [](IRContext* context, Instruction* inst,
  749. const std::vector<const analysis::Constant*>& constants) {
  750. assert(inst->opcode() == SpvOpFDiv);
  751. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  752. const analysis::Type* type =
  753. context->get_type_mgr()->GetType(inst->type_id());
  754. if (!inst->IsFloatingPointFoldingAllowed()) return false;
  755. uint32_t width = ElementWidth(type);
  756. if (width != 32 && width != 64) return false;
  757. const analysis::Constant* const_input1 = ConstInput(constants);
  758. if (!const_input1 || HasZero(const_input1)) return false;
  759. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  760. if (!other_inst->IsFloatingPointFoldingAllowed()) return false;
  761. bool first_is_variable = constants[0] == nullptr;
  762. if (other_inst->opcode() == inst->opcode()) {
  763. std::vector<const analysis::Constant*> other_constants =
  764. const_mgr->GetOperandConstants(other_inst);
  765. const analysis::Constant* const_input2 = ConstInput(other_constants);
  766. if (!const_input2 || HasZero(const_input2)) return false;
  767. bool other_first_is_variable = other_constants[0] == nullptr;
  768. SpvOp merge_op = inst->opcode();
  769. if (other_first_is_variable) {
  770. // Constants magnify.
  771. merge_op = SpvOpFMul;
  772. }
  773. // This is an x / (*) case. Swap the inputs. Doesn't harm multiply
  774. // because it is commutative.
  775. if (first_is_variable) std::swap(const_input1, const_input2);
  776. uint32_t merged_id =
  777. PerformOperation(const_mgr, merge_op, const_input1, const_input2);
  778. if (merged_id == 0) return false;
  779. uint32_t non_const_id = other_first_is_variable
  780. ? other_inst->GetSingleWordInOperand(0u)
  781. : other_inst->GetSingleWordInOperand(1u);
  782. SpvOp op = inst->opcode();
  783. if (!first_is_variable && !other_first_is_variable) {
  784. // Effectively div of 1/x, so change to multiply.
  785. op = SpvOpFMul;
  786. }
  787. uint32_t op1 = merged_id;
  788. uint32_t op2 = non_const_id;
  789. if (first_is_variable && other_first_is_variable) std::swap(op1, op2);
  790. inst->SetOpcode(op);
  791. inst->SetInOperands(
  792. {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}});
  793. return true;
  794. }
  795. return false;
  796. };
  797. }
  798. // Fold multiplies succeeded by divides where each instruction contains a
  799. // constant operand. Does not support integer divide.
  800. // Cases:
  801. // 4 / (x * 2) = 2 / x
  802. // 4 / (2 * x) = 2 / x
  803. // (x * 4) / 2 = x * 2
  804. // (4 * x) / 2 = x * 2
  805. // (x * y) / x = y
  806. // (y * x) / x = y
  807. FoldingRule MergeDivMulArithmetic() {
  808. return [](IRContext* context, Instruction* inst,
  809. const std::vector<const analysis::Constant*>& constants) {
  810. assert(inst->opcode() == SpvOpFDiv);
  811. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  812. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  813. const analysis::Type* type =
  814. context->get_type_mgr()->GetType(inst->type_id());
  815. if (!inst->IsFloatingPointFoldingAllowed()) return false;
  816. uint32_t width = ElementWidth(type);
  817. if (width != 32 && width != 64) return false;
  818. uint32_t op_id = inst->GetSingleWordInOperand(0);
  819. Instruction* op_inst = def_use_mgr->GetDef(op_id);
  820. if (op_inst->opcode() == SpvOpFMul) {
  821. for (uint32_t i = 0; i < 2; i++) {
  822. if (op_inst->GetSingleWordInOperand(i) ==
  823. inst->GetSingleWordInOperand(1)) {
  824. inst->SetOpcode(SpvOpCopyObject);
  825. inst->SetInOperands({{SPV_OPERAND_TYPE_ID,
  826. {op_inst->GetSingleWordInOperand(1 - i)}}});
  827. return true;
  828. }
  829. }
  830. }
  831. const analysis::Constant* const_input1 = ConstInput(constants);
  832. if (!const_input1 || HasZero(const_input1)) return false;
  833. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  834. if (!other_inst->IsFloatingPointFoldingAllowed()) return false;
  835. bool first_is_variable = constants[0] == nullptr;
  836. if (other_inst->opcode() == SpvOpFMul) {
  837. std::vector<const analysis::Constant*> other_constants =
  838. const_mgr->GetOperandConstants(other_inst);
  839. const analysis::Constant* const_input2 = ConstInput(other_constants);
  840. if (!const_input2) return false;
  841. bool other_first_is_variable = other_constants[0] == nullptr;
  842. // This is an x / (*) case. Swap the inputs.
  843. if (first_is_variable) std::swap(const_input1, const_input2);
  844. uint32_t merged_id = PerformOperation(const_mgr, inst->opcode(),
  845. const_input1, const_input2);
  846. if (merged_id == 0) return false;
  847. uint32_t non_const_id = other_first_is_variable
  848. ? other_inst->GetSingleWordInOperand(0u)
  849. : other_inst->GetSingleWordInOperand(1u);
  850. uint32_t op1 = merged_id;
  851. uint32_t op2 = non_const_id;
  852. if (first_is_variable) std::swap(op1, op2);
  853. // Convert to multiply
  854. if (first_is_variable) inst->SetOpcode(other_inst->opcode());
  855. inst->SetInOperands(
  856. {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}});
  857. return true;
  858. }
  859. return false;
  860. };
  861. }
  862. // Fold divides of a constant and a negation.
  863. // Cases:
  864. // (-x) / 2 = x / -2
  865. // 2 / (-x) = -2 / x
  866. FoldingRule MergeDivNegateArithmetic() {
  867. return [](IRContext* context, Instruction* inst,
  868. const std::vector<const analysis::Constant*>& constants) {
  869. assert(inst->opcode() == SpvOpFDiv);
  870. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  871. if (!inst->IsFloatingPointFoldingAllowed()) return false;
  872. const analysis::Constant* const_input1 = ConstInput(constants);
  873. if (!const_input1) return false;
  874. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  875. if (!other_inst->IsFloatingPointFoldingAllowed()) return false;
  876. bool first_is_variable = constants[0] == nullptr;
  877. if (other_inst->opcode() == SpvOpFNegate) {
  878. uint32_t neg_id = NegateConstant(const_mgr, const_input1);
  879. if (first_is_variable) {
  880. inst->SetInOperands(
  881. {{SPV_OPERAND_TYPE_ID, {other_inst->GetSingleWordInOperand(0u)}},
  882. {SPV_OPERAND_TYPE_ID, {neg_id}}});
  883. } else {
  884. inst->SetInOperands(
  885. {{SPV_OPERAND_TYPE_ID, {neg_id}},
  886. {SPV_OPERAND_TYPE_ID, {other_inst->GetSingleWordInOperand(0u)}}});
  887. }
  888. return true;
  889. }
  890. return false;
  891. };
  892. }
  893. // Folds addition of a constant and a negation.
  894. // Cases:
  895. // (-x) + 2 = 2 - x
  896. // 2 + (-x) = 2 - x
  897. FoldingRule MergeAddNegateArithmetic() {
  898. return [](IRContext* context, Instruction* inst,
  899. const std::vector<const analysis::Constant*>& constants) {
  900. assert(inst->opcode() == SpvOpFAdd || inst->opcode() == SpvOpIAdd);
  901. const analysis::Type* type =
  902. context->get_type_mgr()->GetType(inst->type_id());
  903. bool uses_float = HasFloatingPoint(type);
  904. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  905. const analysis::Constant* const_input1 = ConstInput(constants);
  906. if (!const_input1) return false;
  907. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  908. if (uses_float && !other_inst->IsFloatingPointFoldingAllowed())
  909. return false;
  910. if (other_inst->opcode() == SpvOpSNegate ||
  911. other_inst->opcode() == SpvOpFNegate) {
  912. inst->SetOpcode(HasFloatingPoint(type) ? SpvOpFSub : SpvOpISub);
  913. uint32_t const_id = constants[0] ? inst->GetSingleWordInOperand(0u)
  914. : inst->GetSingleWordInOperand(1u);
  915. inst->SetInOperands(
  916. {{SPV_OPERAND_TYPE_ID, {const_id}},
  917. {SPV_OPERAND_TYPE_ID, {other_inst->GetSingleWordInOperand(0u)}}});
  918. return true;
  919. }
  920. return false;
  921. };
  922. }
  923. // Folds subtraction of a constant and a negation.
  924. // Cases:
  925. // (-x) - 2 = -2 - x
  926. // 2 - (-x) = x + 2
  927. FoldingRule MergeSubNegateArithmetic() {
  928. return [](IRContext* context, Instruction* inst,
  929. const std::vector<const analysis::Constant*>& constants) {
  930. assert(inst->opcode() == SpvOpFSub || inst->opcode() == SpvOpISub);
  931. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  932. const analysis::Type* type =
  933. context->get_type_mgr()->GetType(inst->type_id());
  934. bool uses_float = HasFloatingPoint(type);
  935. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  936. uint32_t width = ElementWidth(type);
  937. if (width != 32 && width != 64) return false;
  938. const analysis::Constant* const_input1 = ConstInput(constants);
  939. if (!const_input1) return false;
  940. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  941. if (uses_float && !other_inst->IsFloatingPointFoldingAllowed())
  942. return false;
  943. if (other_inst->opcode() == SpvOpSNegate ||
  944. other_inst->opcode() == SpvOpFNegate) {
  945. uint32_t op1 = 0;
  946. uint32_t op2 = 0;
  947. SpvOp opcode = inst->opcode();
  948. if (constants[0] != nullptr) {
  949. op1 = other_inst->GetSingleWordInOperand(0u);
  950. op2 = inst->GetSingleWordInOperand(0u);
  951. opcode = HasFloatingPoint(type) ? SpvOpFAdd : SpvOpIAdd;
  952. } else {
  953. op1 = NegateConstant(const_mgr, const_input1);
  954. op2 = other_inst->GetSingleWordInOperand(0u);
  955. }
  956. inst->SetOpcode(opcode);
  957. inst->SetInOperands(
  958. {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}});
  959. return true;
  960. }
  961. return false;
  962. };
  963. }
  964. // Folds addition of an addition where each operation has a constant operand.
  965. // Cases:
  966. // (x + 2) + 2 = x + 4
  967. // (2 + x) + 2 = x + 4
  968. // 2 + (x + 2) = x + 4
  969. // 2 + (2 + x) = x + 4
  970. FoldingRule MergeAddAddArithmetic() {
  971. return [](IRContext* context, Instruction* inst,
  972. const std::vector<const analysis::Constant*>& constants) {
  973. assert(inst->opcode() == SpvOpFAdd || inst->opcode() == SpvOpIAdd);
  974. const analysis::Type* type =
  975. context->get_type_mgr()->GetType(inst->type_id());
  976. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  977. bool uses_float = HasFloatingPoint(type);
  978. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  979. uint32_t width = ElementWidth(type);
  980. if (width != 32 && width != 64) return false;
  981. const analysis::Constant* const_input1 = ConstInput(constants);
  982. if (!const_input1) return false;
  983. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  984. if (uses_float && !other_inst->IsFloatingPointFoldingAllowed())
  985. return false;
  986. if (other_inst->opcode() == SpvOpFAdd ||
  987. other_inst->opcode() == SpvOpIAdd) {
  988. std::vector<const analysis::Constant*> other_constants =
  989. const_mgr->GetOperandConstants(other_inst);
  990. const analysis::Constant* const_input2 = ConstInput(other_constants);
  991. if (!const_input2) return false;
  992. Instruction* non_const_input =
  993. NonConstInput(context, other_constants[0], other_inst);
  994. uint32_t merged_id = PerformOperation(const_mgr, inst->opcode(),
  995. const_input1, const_input2);
  996. if (merged_id == 0) return false;
  997. inst->SetInOperands(
  998. {{SPV_OPERAND_TYPE_ID, {non_const_input->result_id()}},
  999. {SPV_OPERAND_TYPE_ID, {merged_id}}});
  1000. return true;
  1001. }
  1002. return false;
  1003. };
  1004. }
  1005. // Folds addition of a subtraction where each operation has a constant operand.
  1006. // Cases:
  1007. // (x - 2) + 2 = x + 0
  1008. // (2 - x) + 2 = 4 - x
  1009. // 2 + (x - 2) = x + 0
  1010. // 2 + (2 - x) = 4 - x
  1011. FoldingRule MergeAddSubArithmetic() {
  1012. return [](IRContext* context, Instruction* inst,
  1013. const std::vector<const analysis::Constant*>& constants) {
  1014. assert(inst->opcode() == SpvOpFAdd || inst->opcode() == SpvOpIAdd);
  1015. const analysis::Type* type =
  1016. context->get_type_mgr()->GetType(inst->type_id());
  1017. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1018. bool uses_float = HasFloatingPoint(type);
  1019. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  1020. uint32_t width = ElementWidth(type);
  1021. if (width != 32 && width != 64) return false;
  1022. const analysis::Constant* const_input1 = ConstInput(constants);
  1023. if (!const_input1) return false;
  1024. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  1025. if (uses_float && !other_inst->IsFloatingPointFoldingAllowed())
  1026. return false;
  1027. if (other_inst->opcode() == SpvOpFSub ||
  1028. other_inst->opcode() == SpvOpISub) {
  1029. std::vector<const analysis::Constant*> other_constants =
  1030. const_mgr->GetOperandConstants(other_inst);
  1031. const analysis::Constant* const_input2 = ConstInput(other_constants);
  1032. if (!const_input2) return false;
  1033. bool first_is_variable = other_constants[0] == nullptr;
  1034. SpvOp op = inst->opcode();
  1035. uint32_t op1 = 0;
  1036. uint32_t op2 = 0;
  1037. if (first_is_variable) {
  1038. // Subtract constants. Non-constant operand is first.
  1039. op1 = other_inst->GetSingleWordInOperand(0u);
  1040. op2 = PerformOperation(const_mgr, other_inst->opcode(), const_input1,
  1041. const_input2);
  1042. } else {
  1043. // Add constants. Constant operand is first. Change the opcode.
  1044. op1 = PerformOperation(const_mgr, inst->opcode(), const_input1,
  1045. const_input2);
  1046. op2 = other_inst->GetSingleWordInOperand(1u);
  1047. op = other_inst->opcode();
  1048. }
  1049. if (op1 == 0 || op2 == 0) return false;
  1050. inst->SetOpcode(op);
  1051. inst->SetInOperands(
  1052. {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}});
  1053. return true;
  1054. }
  1055. return false;
  1056. };
  1057. }
  1058. // Folds subtraction of an addition where each operand has a constant operand.
  1059. // Cases:
  1060. // (x + 2) - 2 = x + 0
  1061. // (2 + x) - 2 = x + 0
  1062. // 2 - (x + 2) = 0 - x
  1063. // 2 - (2 + x) = 0 - x
  1064. FoldingRule MergeSubAddArithmetic() {
  1065. return [](IRContext* context, Instruction* inst,
  1066. const std::vector<const analysis::Constant*>& constants) {
  1067. assert(inst->opcode() == SpvOpFSub || inst->opcode() == SpvOpISub);
  1068. const analysis::Type* type =
  1069. context->get_type_mgr()->GetType(inst->type_id());
  1070. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1071. bool uses_float = HasFloatingPoint(type);
  1072. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  1073. uint32_t width = ElementWidth(type);
  1074. if (width != 32 && width != 64) return false;
  1075. const analysis::Constant* const_input1 = ConstInput(constants);
  1076. if (!const_input1) return false;
  1077. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  1078. if (uses_float && !other_inst->IsFloatingPointFoldingAllowed())
  1079. return false;
  1080. if (other_inst->opcode() == SpvOpFAdd ||
  1081. other_inst->opcode() == SpvOpIAdd) {
  1082. std::vector<const analysis::Constant*> other_constants =
  1083. const_mgr->GetOperandConstants(other_inst);
  1084. const analysis::Constant* const_input2 = ConstInput(other_constants);
  1085. if (!const_input2) return false;
  1086. Instruction* non_const_input =
  1087. NonConstInput(context, other_constants[0], other_inst);
  1088. // If the first operand of the sub is not a constant, swap the constants
  1089. // so the subtraction has the correct operands.
  1090. if (constants[0] == nullptr) std::swap(const_input1, const_input2);
  1091. // Subtract the constants.
  1092. uint32_t merged_id = PerformOperation(const_mgr, inst->opcode(),
  1093. const_input1, const_input2);
  1094. SpvOp op = inst->opcode();
  1095. uint32_t op1 = 0;
  1096. uint32_t op2 = 0;
  1097. if (constants[0] == nullptr) {
  1098. // Non-constant operand is first. Change the opcode.
  1099. op1 = non_const_input->result_id();
  1100. op2 = merged_id;
  1101. op = other_inst->opcode();
  1102. } else {
  1103. // Constant operand is first.
  1104. op1 = merged_id;
  1105. op2 = non_const_input->result_id();
  1106. }
  1107. if (op1 == 0 || op2 == 0) return false;
  1108. inst->SetOpcode(op);
  1109. inst->SetInOperands(
  1110. {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}});
  1111. return true;
  1112. }
  1113. return false;
  1114. };
  1115. }
  1116. // Folds subtraction of a subtraction where each operand has a constant operand.
  1117. // Cases:
  1118. // (x - 2) - 2 = x - 4
  1119. // (2 - x) - 2 = 0 - x
  1120. // 2 - (x - 2) = 4 - x
  1121. // 2 - (2 - x) = x + 0
  1122. FoldingRule MergeSubSubArithmetic() {
  1123. return [](IRContext* context, Instruction* inst,
  1124. const std::vector<const analysis::Constant*>& constants) {
  1125. assert(inst->opcode() == SpvOpFSub || inst->opcode() == SpvOpISub);
  1126. const analysis::Type* type =
  1127. context->get_type_mgr()->GetType(inst->type_id());
  1128. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1129. bool uses_float = HasFloatingPoint(type);
  1130. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  1131. uint32_t width = ElementWidth(type);
  1132. if (width != 32 && width != 64) return false;
  1133. const analysis::Constant* const_input1 = ConstInput(constants);
  1134. if (!const_input1) return false;
  1135. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  1136. if (uses_float && !other_inst->IsFloatingPointFoldingAllowed())
  1137. return false;
  1138. if (other_inst->opcode() == SpvOpFSub ||
  1139. other_inst->opcode() == SpvOpISub) {
  1140. std::vector<const analysis::Constant*> other_constants =
  1141. const_mgr->GetOperandConstants(other_inst);
  1142. const analysis::Constant* const_input2 = ConstInput(other_constants);
  1143. if (!const_input2) return false;
  1144. Instruction* non_const_input =
  1145. NonConstInput(context, other_constants[0], other_inst);
  1146. // Merge the constants.
  1147. uint32_t merged_id = 0;
  1148. SpvOp merge_op = inst->opcode();
  1149. if (other_constants[0] == nullptr) {
  1150. merge_op = uses_float ? SpvOpFAdd : SpvOpIAdd;
  1151. } else if (constants[0] == nullptr) {
  1152. std::swap(const_input1, const_input2);
  1153. }
  1154. merged_id =
  1155. PerformOperation(const_mgr, merge_op, const_input1, const_input2);
  1156. if (merged_id == 0) return false;
  1157. SpvOp op = inst->opcode();
  1158. if (constants[0] != nullptr && other_constants[0] != nullptr) {
  1159. // Change the operation.
  1160. op = uses_float ? SpvOpFAdd : SpvOpIAdd;
  1161. }
  1162. uint32_t op1 = 0;
  1163. uint32_t op2 = 0;
  1164. if ((constants[0] == nullptr) ^ (other_constants[0] == nullptr)) {
  1165. op1 = merged_id;
  1166. op2 = non_const_input->result_id();
  1167. } else {
  1168. op1 = non_const_input->result_id();
  1169. op2 = merged_id;
  1170. }
  1171. inst->SetOpcode(op);
  1172. inst->SetInOperands(
  1173. {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}});
  1174. return true;
  1175. }
  1176. return false;
  1177. };
  1178. }
  1179. // Helper function for MergeGenericAddSubArithmetic. If |addend| and
  1180. // subtrahend of |sub| is the same, merge to copy of minuend of |sub|.
  1181. bool MergeGenericAddendSub(uint32_t addend, uint32_t sub, Instruction* inst) {
  1182. IRContext* context = inst->context();
  1183. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1184. Instruction* sub_inst = def_use_mgr->GetDef(sub);
  1185. if (sub_inst->opcode() != SpvOpFSub && sub_inst->opcode() != SpvOpISub)
  1186. return false;
  1187. if (sub_inst->opcode() == SpvOpFSub &&
  1188. !sub_inst->IsFloatingPointFoldingAllowed())
  1189. return false;
  1190. if (addend != sub_inst->GetSingleWordInOperand(1)) return false;
  1191. inst->SetOpcode(SpvOpCopyObject);
  1192. inst->SetInOperands(
  1193. {{SPV_OPERAND_TYPE_ID, {sub_inst->GetSingleWordInOperand(0)}}});
  1194. context->UpdateDefUse(inst);
  1195. return true;
  1196. }
  1197. // Folds addition of a subtraction where the subtrahend is equal to the
  1198. // other addend. Return a copy of the minuend. Accepts generic (const and
  1199. // non-const) operands.
  1200. // Cases:
  1201. // (a - b) + b = a
  1202. // b + (a - b) = a
  1203. FoldingRule MergeGenericAddSubArithmetic() {
  1204. return [](IRContext* context, Instruction* inst,
  1205. const std::vector<const analysis::Constant*>&) {
  1206. assert(inst->opcode() == SpvOpFAdd || inst->opcode() == SpvOpIAdd);
  1207. const analysis::Type* type =
  1208. context->get_type_mgr()->GetType(inst->type_id());
  1209. bool uses_float = HasFloatingPoint(type);
  1210. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  1211. uint32_t width = ElementWidth(type);
  1212. if (width != 32 && width != 64) return false;
  1213. uint32_t add_op0 = inst->GetSingleWordInOperand(0);
  1214. uint32_t add_op1 = inst->GetSingleWordInOperand(1);
  1215. if (MergeGenericAddendSub(add_op0, add_op1, inst)) return true;
  1216. return MergeGenericAddendSub(add_op1, add_op0, inst);
  1217. };
  1218. }
  1219. // Helper function for FactorAddMuls. If |factor0_0| is the same as |factor1_0|,
  1220. // generate |factor0_0| * (|factor0_1| + |factor1_1|).
  1221. bool FactorAddMulsOpnds(uint32_t factor0_0, uint32_t factor0_1,
  1222. uint32_t factor1_0, uint32_t factor1_1,
  1223. Instruction* inst) {
  1224. IRContext* context = inst->context();
  1225. if (factor0_0 != factor1_0) return false;
  1226. InstructionBuilder ir_builder(
  1227. context, inst,
  1228. IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
  1229. Instruction* new_add_inst = ir_builder.AddBinaryOp(
  1230. inst->type_id(), inst->opcode(), factor0_1, factor1_1);
  1231. inst->SetOpcode(inst->opcode() == SpvOpFAdd ? SpvOpFMul : SpvOpIMul);
  1232. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {factor0_0}},
  1233. {SPV_OPERAND_TYPE_ID, {new_add_inst->result_id()}}});
  1234. context->UpdateDefUse(inst);
  1235. return true;
  1236. }
  1237. // Perform the following factoring identity, handling all operand order
  1238. // combinations: (a * b) + (a * c) = a * (b + c)
  1239. FoldingRule FactorAddMuls() {
  1240. return [](IRContext* context, Instruction* inst,
  1241. const std::vector<const analysis::Constant*>&) {
  1242. assert(inst->opcode() == SpvOpFAdd || inst->opcode() == SpvOpIAdd);
  1243. const analysis::Type* type =
  1244. context->get_type_mgr()->GetType(inst->type_id());
  1245. bool uses_float = HasFloatingPoint(type);
  1246. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  1247. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1248. uint32_t add_op0 = inst->GetSingleWordInOperand(0);
  1249. Instruction* add_op0_inst = def_use_mgr->GetDef(add_op0);
  1250. if (add_op0_inst->opcode() != SpvOpFMul &&
  1251. add_op0_inst->opcode() != SpvOpIMul)
  1252. return false;
  1253. uint32_t add_op1 = inst->GetSingleWordInOperand(1);
  1254. Instruction* add_op1_inst = def_use_mgr->GetDef(add_op1);
  1255. if (add_op1_inst->opcode() != SpvOpFMul &&
  1256. add_op1_inst->opcode() != SpvOpIMul)
  1257. return false;
  1258. // Only perform this optimization if both of the muls only have one use.
  1259. // Otherwise this is a deoptimization in size and performance.
  1260. if (def_use_mgr->NumUses(add_op0_inst) > 1) return false;
  1261. if (def_use_mgr->NumUses(add_op1_inst) > 1) return false;
  1262. if (add_op0_inst->opcode() == SpvOpFMul &&
  1263. (!add_op0_inst->IsFloatingPointFoldingAllowed() ||
  1264. !add_op1_inst->IsFloatingPointFoldingAllowed()))
  1265. return false;
  1266. for (int i = 0; i < 2; i++) {
  1267. for (int j = 0; j < 2; j++) {
  1268. // Check if operand i in add_op0_inst matches operand j in add_op1_inst.
  1269. if (FactorAddMulsOpnds(add_op0_inst->GetSingleWordInOperand(i),
  1270. add_op0_inst->GetSingleWordInOperand(1 - i),
  1271. add_op1_inst->GetSingleWordInOperand(j),
  1272. add_op1_inst->GetSingleWordInOperand(1 - j),
  1273. inst))
  1274. return true;
  1275. }
  1276. }
  1277. return false;
  1278. };
  1279. }
  1280. // Replaces |inst| inplace with an FMA instruction |(x*y)+a|.
  1281. void ReplaceWithFma(Instruction* inst, uint32_t x, uint32_t y, uint32_t a) {
  1282. uint32_t ext =
  1283. inst->context()->get_feature_mgr()->GetExtInstImportId_GLSLstd450();
  1284. if (ext == 0) {
  1285. inst->context()->AddExtInstImport("GLSL.std.450");
  1286. ext = inst->context()->get_feature_mgr()->GetExtInstImportId_GLSLstd450();
  1287. assert(ext != 0 &&
  1288. "Could not add the GLSL.std.450 extended instruction set");
  1289. }
  1290. std::vector<Operand> operands;
  1291. operands.push_back({SPV_OPERAND_TYPE_ID, {ext}});
  1292. operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {GLSLstd450Fma}});
  1293. operands.push_back({SPV_OPERAND_TYPE_ID, {x}});
  1294. operands.push_back({SPV_OPERAND_TYPE_ID, {y}});
  1295. operands.push_back({SPV_OPERAND_TYPE_ID, {a}});
  1296. inst->SetOpcode(SpvOpExtInst);
  1297. inst->SetInOperands(std::move(operands));
  1298. }
  1299. // Folds a multiple and add into an Fma.
  1300. //
  1301. // Cases:
  1302. // (x * y) + a = Fma x y a
  1303. // a + (x * y) = Fma x y a
  1304. bool MergeMulAddArithmetic(IRContext* context, Instruction* inst,
  1305. const std::vector<const analysis::Constant*>&) {
  1306. assert(inst->opcode() == SpvOpFAdd);
  1307. if (!inst->IsFloatingPointFoldingAllowed()) {
  1308. return false;
  1309. }
  1310. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1311. for (int i = 0; i < 2; i++) {
  1312. uint32_t op_id = inst->GetSingleWordInOperand(i);
  1313. Instruction* op_inst = def_use_mgr->GetDef(op_id);
  1314. if (op_inst->opcode() != SpvOpFMul) {
  1315. continue;
  1316. }
  1317. if (!op_inst->IsFloatingPointFoldingAllowed()) {
  1318. continue;
  1319. }
  1320. uint32_t x = op_inst->GetSingleWordInOperand(0);
  1321. uint32_t y = op_inst->GetSingleWordInOperand(1);
  1322. uint32_t a = inst->GetSingleWordInOperand((i + 1) % 2);
  1323. ReplaceWithFma(inst, x, y, a);
  1324. return true;
  1325. }
  1326. return false;
  1327. }
  1328. FoldingRule IntMultipleBy1() {
  1329. return [](IRContext*, Instruction* inst,
  1330. const std::vector<const analysis::Constant*>& constants) {
  1331. assert(inst->opcode() == SpvOpIMul && "Wrong opcode. Should be OpIMul.");
  1332. for (uint32_t i = 0; i < 2; i++) {
  1333. if (constants[i] == nullptr) {
  1334. continue;
  1335. }
  1336. const analysis::IntConstant* int_constant = constants[i]->AsIntConstant();
  1337. if (int_constant) {
  1338. uint32_t width = ElementWidth(int_constant->type());
  1339. if (width != 32 && width != 64) return false;
  1340. bool is_one = (width == 32) ? int_constant->GetU32BitValue() == 1u
  1341. : int_constant->GetU64BitValue() == 1ull;
  1342. if (is_one) {
  1343. inst->SetOpcode(SpvOpCopyObject);
  1344. inst->SetInOperands(
  1345. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(1 - i)}}});
  1346. return true;
  1347. }
  1348. }
  1349. }
  1350. return false;
  1351. };
  1352. }
  1353. // Returns the number of elements that the |index|th in operand in |inst|
  1354. // contributes to the result of |inst|. |inst| must be an
  1355. // OpCompositeConstructInstruction.
  1356. uint32_t GetNumOfElementsContributedByOperand(IRContext* context,
  1357. const Instruction* inst,
  1358. uint32_t index) {
  1359. assert(inst->opcode() == SpvOpCompositeConstruct);
  1360. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1361. analysis::TypeManager* type_mgr = context->get_type_mgr();
  1362. analysis::Vector* result_type =
  1363. type_mgr->GetType(inst->type_id())->AsVector();
  1364. if (result_type == nullptr) {
  1365. // If the result of the OpCompositeConstruct is not a vector then every
  1366. // operands corresponds to a single element in the result.
  1367. return 1;
  1368. }
  1369. // If the result type is a vector then the operands are either scalars or
  1370. // vectors. If it is a scalar, then it corresponds to a single element. If it
  1371. // is a vector, then each element in the vector will be an element in the
  1372. // result.
  1373. uint32_t id = inst->GetSingleWordInOperand(index);
  1374. Instruction* def = def_use_mgr->GetDef(id);
  1375. analysis::Vector* type = type_mgr->GetType(def->type_id())->AsVector();
  1376. if (type == nullptr) {
  1377. return 1;
  1378. }
  1379. return type->element_count();
  1380. }
  1381. // Returns the in-operands for an OpCompositeExtract instruction that are needed
  1382. // to extract the |result_index|th element in the result of |inst| without using
  1383. // the result of |inst|. Returns the empty vector if |result_index| is
  1384. // out-of-bounds. |inst| must be an |OpCompositeConstruct| instruction.
  1385. std::vector<Operand> GetExtractOperandsForElementOfCompositeConstruct(
  1386. IRContext* context, const Instruction* inst, uint32_t result_index) {
  1387. assert(inst->opcode() == SpvOpCompositeConstruct);
  1388. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1389. analysis::TypeManager* type_mgr = context->get_type_mgr();
  1390. analysis::Type* result_type = type_mgr->GetType(inst->type_id());
  1391. if (result_type->AsVector() == nullptr) {
  1392. uint32_t id = inst->GetSingleWordInOperand(result_index);
  1393. return {Operand(SPV_OPERAND_TYPE_ID, {id})};
  1394. }
  1395. // If the result type is a vector, then vector operands are concatenated.
  1396. uint32_t total_element_count = 0;
  1397. for (uint32_t idx = 0; idx < inst->NumInOperands(); ++idx) {
  1398. uint32_t element_count =
  1399. GetNumOfElementsContributedByOperand(context, inst, idx);
  1400. total_element_count += element_count;
  1401. if (result_index < total_element_count) {
  1402. std::vector<Operand> operands;
  1403. uint32_t id = inst->GetSingleWordInOperand(idx);
  1404. Instruction* operand_def = def_use_mgr->GetDef(id);
  1405. analysis::Type* operand_type = type_mgr->GetType(operand_def->type_id());
  1406. operands.push_back({SPV_OPERAND_TYPE_ID, {id}});
  1407. if (operand_type->AsVector()) {
  1408. uint32_t start_index_of_id = total_element_count - element_count;
  1409. uint32_t index_into_id = result_index - start_index_of_id;
  1410. operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {index_into_id}});
  1411. }
  1412. return operands;
  1413. }
  1414. }
  1415. return {};
  1416. }
  1417. bool CompositeConstructFeedingExtract(
  1418. IRContext* context, Instruction* inst,
  1419. const std::vector<const analysis::Constant*>&) {
  1420. // If the input to an OpCompositeExtract is an OpCompositeConstruct,
  1421. // then we can simply use the appropriate element in the construction.
  1422. assert(inst->opcode() == SpvOpCompositeExtract &&
  1423. "Wrong opcode. Should be OpCompositeExtract.");
  1424. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1425. // If there are no index operands, then this rule cannot do anything.
  1426. if (inst->NumInOperands() <= 1) {
  1427. return false;
  1428. }
  1429. uint32_t cid = inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1430. Instruction* cinst = def_use_mgr->GetDef(cid);
  1431. if (cinst->opcode() != SpvOpCompositeConstruct) {
  1432. return false;
  1433. }
  1434. uint32_t index_into_result = inst->GetSingleWordInOperand(1);
  1435. std::vector<Operand> operands =
  1436. GetExtractOperandsForElementOfCompositeConstruct(context, cinst,
  1437. index_into_result);
  1438. if (operands.empty()) {
  1439. return false;
  1440. }
  1441. // Add the remaining indices for extraction.
  1442. for (uint32_t i = 2; i < inst->NumInOperands(); ++i) {
  1443. operands.push_back(
  1444. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {inst->GetSingleWordInOperand(i)}});
  1445. }
  1446. if (operands.size() == 1) {
  1447. // If there were no extra indices, then we have the final object. No need
  1448. // to extract any more.
  1449. inst->SetOpcode(SpvOpCopyObject);
  1450. }
  1451. inst->SetInOperands(std::move(operands));
  1452. return true;
  1453. }
  1454. // If the OpCompositeConstruct is simply putting back together elements that
  1455. // where extracted from the same source, we can simply reuse the source.
  1456. //
  1457. // This is a common code pattern because of the way that scalar replacement
  1458. // works.
  1459. bool CompositeExtractFeedingConstruct(
  1460. IRContext* context, Instruction* inst,
  1461. const std::vector<const analysis::Constant*>&) {
  1462. assert(inst->opcode() == SpvOpCompositeConstruct &&
  1463. "Wrong opcode. Should be OpCompositeConstruct.");
  1464. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1465. uint32_t original_id = 0;
  1466. if (inst->NumInOperands() == 0) {
  1467. // The struct being constructed has no members.
  1468. return false;
  1469. }
  1470. // Check each element to make sure they are:
  1471. // - extractions
  1472. // - extracting the same position they are inserting
  1473. // - all extract from the same id.
  1474. for (uint32_t i = 0; i < inst->NumInOperands(); ++i) {
  1475. const uint32_t element_id = inst->GetSingleWordInOperand(i);
  1476. Instruction* element_inst = def_use_mgr->GetDef(element_id);
  1477. if (element_inst->opcode() != SpvOpCompositeExtract) {
  1478. return false;
  1479. }
  1480. if (element_inst->NumInOperands() != 2) {
  1481. return false;
  1482. }
  1483. if (element_inst->GetSingleWordInOperand(1) != i) {
  1484. return false;
  1485. }
  1486. if (i == 0) {
  1487. original_id =
  1488. element_inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1489. } else if (original_id !=
  1490. element_inst->GetSingleWordInOperand(kExtractCompositeIdInIdx)) {
  1491. return false;
  1492. }
  1493. }
  1494. // The last check it to see that the object being extracted from is the
  1495. // correct type.
  1496. Instruction* original_inst = def_use_mgr->GetDef(original_id);
  1497. if (original_inst->type_id() != inst->type_id()) {
  1498. return false;
  1499. }
  1500. // Simplify by using the original object.
  1501. inst->SetOpcode(SpvOpCopyObject);
  1502. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {original_id}}});
  1503. return true;
  1504. }
  1505. FoldingRule InsertFeedingExtract() {
  1506. return [](IRContext* context, Instruction* inst,
  1507. const std::vector<const analysis::Constant*>&) {
  1508. assert(inst->opcode() == SpvOpCompositeExtract &&
  1509. "Wrong opcode. Should be OpCompositeExtract.");
  1510. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1511. uint32_t cid = inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1512. Instruction* cinst = def_use_mgr->GetDef(cid);
  1513. if (cinst->opcode() != SpvOpCompositeInsert) {
  1514. return false;
  1515. }
  1516. // Find the first position where the list of insert and extract indicies
  1517. // differ, if at all.
  1518. uint32_t i;
  1519. for (i = 1; i < inst->NumInOperands(); ++i) {
  1520. if (i + 1 >= cinst->NumInOperands()) {
  1521. break;
  1522. }
  1523. if (inst->GetSingleWordInOperand(i) !=
  1524. cinst->GetSingleWordInOperand(i + 1)) {
  1525. break;
  1526. }
  1527. }
  1528. // We are extracting the element that was inserted.
  1529. if (i == inst->NumInOperands() && i + 1 == cinst->NumInOperands()) {
  1530. inst->SetOpcode(SpvOpCopyObject);
  1531. inst->SetInOperands(
  1532. {{SPV_OPERAND_TYPE_ID,
  1533. {cinst->GetSingleWordInOperand(kInsertObjectIdInIdx)}}});
  1534. return true;
  1535. }
  1536. // Extracting the value that was inserted along with values for the base
  1537. // composite. Cannot do anything.
  1538. if (i == inst->NumInOperands()) {
  1539. return false;
  1540. }
  1541. // Extracting an element of the value that was inserted. Extract from
  1542. // that value directly.
  1543. if (i + 1 == cinst->NumInOperands()) {
  1544. std::vector<Operand> operands;
  1545. operands.push_back(
  1546. {SPV_OPERAND_TYPE_ID,
  1547. {cinst->GetSingleWordInOperand(kInsertObjectIdInIdx)}});
  1548. for (; i < inst->NumInOperands(); ++i) {
  1549. operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER,
  1550. {inst->GetSingleWordInOperand(i)}});
  1551. }
  1552. inst->SetInOperands(std::move(operands));
  1553. return true;
  1554. }
  1555. // Extracting a value that is disjoint from the element being inserted.
  1556. // Rewrite the extract to use the composite input to the insert.
  1557. std::vector<Operand> operands;
  1558. operands.push_back(
  1559. {SPV_OPERAND_TYPE_ID,
  1560. {cinst->GetSingleWordInOperand(kInsertCompositeIdInIdx)}});
  1561. for (i = 1; i < inst->NumInOperands(); ++i) {
  1562. operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER,
  1563. {inst->GetSingleWordInOperand(i)}});
  1564. }
  1565. inst->SetInOperands(std::move(operands));
  1566. return true;
  1567. };
  1568. }
  1569. // When a VectorShuffle is feeding an Extract, we can extract from one of the
  1570. // operands of the VectorShuffle. We just need to adjust the index in the
  1571. // extract instruction.
  1572. FoldingRule VectorShuffleFeedingExtract() {
  1573. return [](IRContext* context, Instruction* inst,
  1574. const std::vector<const analysis::Constant*>&) {
  1575. assert(inst->opcode() == SpvOpCompositeExtract &&
  1576. "Wrong opcode. Should be OpCompositeExtract.");
  1577. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1578. analysis::TypeManager* type_mgr = context->get_type_mgr();
  1579. uint32_t cid = inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1580. Instruction* cinst = def_use_mgr->GetDef(cid);
  1581. if (cinst->opcode() != SpvOpVectorShuffle) {
  1582. return false;
  1583. }
  1584. // Find the size of the first vector operand of the VectorShuffle
  1585. Instruction* first_input =
  1586. def_use_mgr->GetDef(cinst->GetSingleWordInOperand(0));
  1587. analysis::Type* first_input_type =
  1588. type_mgr->GetType(first_input->type_id());
  1589. assert(first_input_type->AsVector() &&
  1590. "Input to vector shuffle should be vectors.");
  1591. uint32_t first_input_size = first_input_type->AsVector()->element_count();
  1592. // Get index of the element the vector shuffle is placing in the position
  1593. // being extracted.
  1594. uint32_t new_index =
  1595. cinst->GetSingleWordInOperand(2 + inst->GetSingleWordInOperand(1));
  1596. // Extracting an undefined value so fold this extract into an undef.
  1597. const uint32_t undef_literal_value = 0xffffffff;
  1598. if (new_index == undef_literal_value) {
  1599. inst->SetOpcode(SpvOpUndef);
  1600. inst->SetInOperands({});
  1601. return true;
  1602. }
  1603. // Get the id of the of the vector the elemtent comes from, and update the
  1604. // index if needed.
  1605. uint32_t new_vector = 0;
  1606. if (new_index < first_input_size) {
  1607. new_vector = cinst->GetSingleWordInOperand(0);
  1608. } else {
  1609. new_vector = cinst->GetSingleWordInOperand(1);
  1610. new_index -= first_input_size;
  1611. }
  1612. // Update the extract instruction.
  1613. inst->SetInOperand(kExtractCompositeIdInIdx, {new_vector});
  1614. inst->SetInOperand(1, {new_index});
  1615. return true;
  1616. };
  1617. }
  1618. // When an FMix with is feeding an Extract that extracts an element whose
  1619. // corresponding |a| in the FMix is 0 or 1, we can extract from one of the
  1620. // operands of the FMix.
  1621. FoldingRule FMixFeedingExtract() {
  1622. return [](IRContext* context, Instruction* inst,
  1623. const std::vector<const analysis::Constant*>&) {
  1624. assert(inst->opcode() == SpvOpCompositeExtract &&
  1625. "Wrong opcode. Should be OpCompositeExtract.");
  1626. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1627. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1628. uint32_t composite_id =
  1629. inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1630. Instruction* composite_inst = def_use_mgr->GetDef(composite_id);
  1631. if (composite_inst->opcode() != SpvOpExtInst) {
  1632. return false;
  1633. }
  1634. uint32_t inst_set_id =
  1635. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450();
  1636. if (composite_inst->GetSingleWordInOperand(kExtInstSetIdInIdx) !=
  1637. inst_set_id ||
  1638. composite_inst->GetSingleWordInOperand(kExtInstInstructionInIdx) !=
  1639. GLSLstd450FMix) {
  1640. return false;
  1641. }
  1642. // Get the |a| for the FMix instruction.
  1643. uint32_t a_id = composite_inst->GetSingleWordInOperand(kFMixAIdInIdx);
  1644. std::unique_ptr<Instruction> a(inst->Clone(context));
  1645. a->SetInOperand(kExtractCompositeIdInIdx, {a_id});
  1646. context->get_instruction_folder().FoldInstruction(a.get());
  1647. if (a->opcode() != SpvOpCopyObject) {
  1648. return false;
  1649. }
  1650. const analysis::Constant* a_const =
  1651. const_mgr->FindDeclaredConstant(a->GetSingleWordInOperand(0));
  1652. if (!a_const) {
  1653. return false;
  1654. }
  1655. bool use_x = false;
  1656. assert(a_const->type()->AsFloat());
  1657. double element_value = a_const->GetValueAsDouble();
  1658. if (element_value == 0.0) {
  1659. use_x = true;
  1660. } else if (element_value == 1.0) {
  1661. use_x = false;
  1662. } else {
  1663. return false;
  1664. }
  1665. // Get the id of the of the vector the element comes from.
  1666. uint32_t new_vector = 0;
  1667. if (use_x) {
  1668. new_vector = composite_inst->GetSingleWordInOperand(kFMixXIdInIdx);
  1669. } else {
  1670. new_vector = composite_inst->GetSingleWordInOperand(kFMixYIdInIdx);
  1671. }
  1672. // Update the extract instruction.
  1673. inst->SetInOperand(kExtractCompositeIdInIdx, {new_vector});
  1674. return true;
  1675. };
  1676. }
  1677. FoldingRule RedundantPhi() {
  1678. // An OpPhi instruction where all values are the same or the result of the phi
  1679. // itself, can be replaced by the value itself.
  1680. return [](IRContext*, Instruction* inst,
  1681. const std::vector<const analysis::Constant*>&) {
  1682. assert(inst->opcode() == SpvOpPhi && "Wrong opcode. Should be OpPhi.");
  1683. uint32_t incoming_value = 0;
  1684. for (uint32_t i = 0; i < inst->NumInOperands(); i += 2) {
  1685. uint32_t op_id = inst->GetSingleWordInOperand(i);
  1686. if (op_id == inst->result_id()) {
  1687. continue;
  1688. }
  1689. if (incoming_value == 0) {
  1690. incoming_value = op_id;
  1691. } else if (op_id != incoming_value) {
  1692. // Found two possible value. Can't simplify.
  1693. return false;
  1694. }
  1695. }
  1696. if (incoming_value == 0) {
  1697. // Code looks invalid. Don't do anything.
  1698. return false;
  1699. }
  1700. // We have a single incoming value. Simplify using that value.
  1701. inst->SetOpcode(SpvOpCopyObject);
  1702. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {incoming_value}}});
  1703. return true;
  1704. };
  1705. }
  1706. FoldingRule BitCastScalarOrVector() {
  1707. return [](IRContext* context, Instruction* inst,
  1708. const std::vector<const analysis::Constant*>& constants) {
  1709. assert(inst->opcode() == SpvOpBitcast && constants.size() == 1);
  1710. if (constants[0] == nullptr) return false;
  1711. const analysis::Type* type =
  1712. context->get_type_mgr()->GetType(inst->type_id());
  1713. if (HasFloatingPoint(type) && !inst->IsFloatingPointFoldingAllowed())
  1714. return false;
  1715. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1716. std::vector<uint32_t> words =
  1717. GetWordsFromNumericScalarOrVectorConstant(const_mgr, constants[0]);
  1718. if (words.size() == 0) return false;
  1719. const analysis::Constant* bitcasted_constant =
  1720. ConvertWordsToNumericScalarOrVectorConstant(const_mgr, words, type);
  1721. if (!bitcasted_constant) return false;
  1722. auto new_feeder_id =
  1723. const_mgr->GetDefiningInstruction(bitcasted_constant, inst->type_id())
  1724. ->result_id();
  1725. inst->SetOpcode(SpvOpCopyObject);
  1726. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {new_feeder_id}}});
  1727. return true;
  1728. };
  1729. }
  1730. FoldingRule RedundantSelect() {
  1731. // An OpSelect instruction where both values are the same or the condition is
  1732. // constant can be replaced by one of the values
  1733. return [](IRContext*, Instruction* inst,
  1734. const std::vector<const analysis::Constant*>& constants) {
  1735. assert(inst->opcode() == SpvOpSelect &&
  1736. "Wrong opcode. Should be OpSelect.");
  1737. assert(inst->NumInOperands() == 3);
  1738. assert(constants.size() == 3);
  1739. uint32_t true_id = inst->GetSingleWordInOperand(1);
  1740. uint32_t false_id = inst->GetSingleWordInOperand(2);
  1741. if (true_id == false_id) {
  1742. // Both results are the same, condition doesn't matter
  1743. inst->SetOpcode(SpvOpCopyObject);
  1744. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {true_id}}});
  1745. return true;
  1746. } else if (constants[0]) {
  1747. const analysis::Type* type = constants[0]->type();
  1748. if (type->AsBool()) {
  1749. // Scalar constant value, select the corresponding value.
  1750. inst->SetOpcode(SpvOpCopyObject);
  1751. if (constants[0]->AsNullConstant() ||
  1752. !constants[0]->AsBoolConstant()->value()) {
  1753. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {false_id}}});
  1754. } else {
  1755. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {true_id}}});
  1756. }
  1757. return true;
  1758. } else {
  1759. assert(type->AsVector());
  1760. if (constants[0]->AsNullConstant()) {
  1761. // All values come from false id.
  1762. inst->SetOpcode(SpvOpCopyObject);
  1763. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {false_id}}});
  1764. return true;
  1765. } else {
  1766. // Convert to a vector shuffle.
  1767. std::vector<Operand> ops;
  1768. ops.push_back({SPV_OPERAND_TYPE_ID, {true_id}});
  1769. ops.push_back({SPV_OPERAND_TYPE_ID, {false_id}});
  1770. const analysis::VectorConstant* vector_const =
  1771. constants[0]->AsVectorConstant();
  1772. uint32_t size =
  1773. static_cast<uint32_t>(vector_const->GetComponents().size());
  1774. for (uint32_t i = 0; i != size; ++i) {
  1775. const analysis::Constant* component =
  1776. vector_const->GetComponents()[i];
  1777. if (component->AsNullConstant() ||
  1778. !component->AsBoolConstant()->value()) {
  1779. // Selecting from the false vector which is the second input
  1780. // vector to the shuffle. Offset the index by |size|.
  1781. ops.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {i + size}});
  1782. } else {
  1783. // Selecting from true vector which is the first input vector to
  1784. // the shuffle.
  1785. ops.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {i}});
  1786. }
  1787. }
  1788. inst->SetOpcode(SpvOpVectorShuffle);
  1789. inst->SetInOperands(std::move(ops));
  1790. return true;
  1791. }
  1792. }
  1793. }
  1794. return false;
  1795. };
  1796. }
  1797. enum class FloatConstantKind { Unknown, Zero, One };
  1798. FloatConstantKind getFloatConstantKind(const analysis::Constant* constant) {
  1799. if (constant == nullptr) {
  1800. return FloatConstantKind::Unknown;
  1801. }
  1802. assert(HasFloatingPoint(constant->type()) && "Unexpected constant type");
  1803. if (constant->AsNullConstant()) {
  1804. return FloatConstantKind::Zero;
  1805. } else if (const analysis::VectorConstant* vc =
  1806. constant->AsVectorConstant()) {
  1807. const std::vector<const analysis::Constant*>& components =
  1808. vc->GetComponents();
  1809. assert(!components.empty());
  1810. FloatConstantKind kind = getFloatConstantKind(components[0]);
  1811. for (size_t i = 1; i < components.size(); ++i) {
  1812. if (getFloatConstantKind(components[i]) != kind) {
  1813. return FloatConstantKind::Unknown;
  1814. }
  1815. }
  1816. return kind;
  1817. } else if (const analysis::FloatConstant* fc = constant->AsFloatConstant()) {
  1818. if (fc->IsZero()) return FloatConstantKind::Zero;
  1819. uint32_t width = fc->type()->AsFloat()->width();
  1820. if (width != 32 && width != 64) return FloatConstantKind::Unknown;
  1821. double value = (width == 64) ? fc->GetDoubleValue() : fc->GetFloatValue();
  1822. if (value == 0.0) {
  1823. return FloatConstantKind::Zero;
  1824. } else if (value == 1.0) {
  1825. return FloatConstantKind::One;
  1826. } else {
  1827. return FloatConstantKind::Unknown;
  1828. }
  1829. } else {
  1830. return FloatConstantKind::Unknown;
  1831. }
  1832. }
  1833. FoldingRule RedundantFAdd() {
  1834. return [](IRContext*, Instruction* inst,
  1835. const std::vector<const analysis::Constant*>& constants) {
  1836. assert(inst->opcode() == SpvOpFAdd && "Wrong opcode. Should be OpFAdd.");
  1837. assert(constants.size() == 2);
  1838. if (!inst->IsFloatingPointFoldingAllowed()) {
  1839. return false;
  1840. }
  1841. FloatConstantKind kind0 = getFloatConstantKind(constants[0]);
  1842. FloatConstantKind kind1 = getFloatConstantKind(constants[1]);
  1843. if (kind0 == FloatConstantKind::Zero || kind1 == FloatConstantKind::Zero) {
  1844. inst->SetOpcode(SpvOpCopyObject);
  1845. inst->SetInOperands({{SPV_OPERAND_TYPE_ID,
  1846. {inst->GetSingleWordInOperand(
  1847. kind0 == FloatConstantKind::Zero ? 1 : 0)}}});
  1848. return true;
  1849. }
  1850. return false;
  1851. };
  1852. }
  1853. FoldingRule RedundantFSub() {
  1854. return [](IRContext*, Instruction* inst,
  1855. const std::vector<const analysis::Constant*>& constants) {
  1856. assert(inst->opcode() == SpvOpFSub && "Wrong opcode. Should be OpFSub.");
  1857. assert(constants.size() == 2);
  1858. if (!inst->IsFloatingPointFoldingAllowed()) {
  1859. return false;
  1860. }
  1861. FloatConstantKind kind0 = getFloatConstantKind(constants[0]);
  1862. FloatConstantKind kind1 = getFloatConstantKind(constants[1]);
  1863. if (kind0 == FloatConstantKind::Zero) {
  1864. inst->SetOpcode(SpvOpFNegate);
  1865. inst->SetInOperands(
  1866. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(1)}}});
  1867. return true;
  1868. }
  1869. if (kind1 == FloatConstantKind::Zero) {
  1870. inst->SetOpcode(SpvOpCopyObject);
  1871. inst->SetInOperands(
  1872. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(0)}}});
  1873. return true;
  1874. }
  1875. return false;
  1876. };
  1877. }
  1878. FoldingRule RedundantFMul() {
  1879. return [](IRContext*, Instruction* inst,
  1880. const std::vector<const analysis::Constant*>& constants) {
  1881. assert(inst->opcode() == SpvOpFMul && "Wrong opcode. Should be OpFMul.");
  1882. assert(constants.size() == 2);
  1883. if (!inst->IsFloatingPointFoldingAllowed()) {
  1884. return false;
  1885. }
  1886. FloatConstantKind kind0 = getFloatConstantKind(constants[0]);
  1887. FloatConstantKind kind1 = getFloatConstantKind(constants[1]);
  1888. if (kind0 == FloatConstantKind::Zero || kind1 == FloatConstantKind::Zero) {
  1889. inst->SetOpcode(SpvOpCopyObject);
  1890. inst->SetInOperands({{SPV_OPERAND_TYPE_ID,
  1891. {inst->GetSingleWordInOperand(
  1892. kind0 == FloatConstantKind::Zero ? 0 : 1)}}});
  1893. return true;
  1894. }
  1895. if (kind0 == FloatConstantKind::One || kind1 == FloatConstantKind::One) {
  1896. inst->SetOpcode(SpvOpCopyObject);
  1897. inst->SetInOperands({{SPV_OPERAND_TYPE_ID,
  1898. {inst->GetSingleWordInOperand(
  1899. kind0 == FloatConstantKind::One ? 1 : 0)}}});
  1900. return true;
  1901. }
  1902. return false;
  1903. };
  1904. }
  1905. FoldingRule RedundantFDiv() {
  1906. return [](IRContext*, Instruction* inst,
  1907. const std::vector<const analysis::Constant*>& constants) {
  1908. assert(inst->opcode() == SpvOpFDiv && "Wrong opcode. Should be OpFDiv.");
  1909. assert(constants.size() == 2);
  1910. if (!inst->IsFloatingPointFoldingAllowed()) {
  1911. return false;
  1912. }
  1913. FloatConstantKind kind0 = getFloatConstantKind(constants[0]);
  1914. FloatConstantKind kind1 = getFloatConstantKind(constants[1]);
  1915. if (kind0 == FloatConstantKind::Zero) {
  1916. inst->SetOpcode(SpvOpCopyObject);
  1917. inst->SetInOperands(
  1918. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(0)}}});
  1919. return true;
  1920. }
  1921. if (kind1 == FloatConstantKind::One) {
  1922. inst->SetOpcode(SpvOpCopyObject);
  1923. inst->SetInOperands(
  1924. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(0)}}});
  1925. return true;
  1926. }
  1927. return false;
  1928. };
  1929. }
  1930. FoldingRule RedundantFMix() {
  1931. return [](IRContext* context, Instruction* inst,
  1932. const std::vector<const analysis::Constant*>& constants) {
  1933. assert(inst->opcode() == SpvOpExtInst &&
  1934. "Wrong opcode. Should be OpExtInst.");
  1935. if (!inst->IsFloatingPointFoldingAllowed()) {
  1936. return false;
  1937. }
  1938. uint32_t instSetId =
  1939. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450();
  1940. if (inst->GetSingleWordInOperand(kExtInstSetIdInIdx) == instSetId &&
  1941. inst->GetSingleWordInOperand(kExtInstInstructionInIdx) ==
  1942. GLSLstd450FMix) {
  1943. assert(constants.size() == 5);
  1944. FloatConstantKind kind4 = getFloatConstantKind(constants[4]);
  1945. if (kind4 == FloatConstantKind::Zero || kind4 == FloatConstantKind::One) {
  1946. inst->SetOpcode(SpvOpCopyObject);
  1947. inst->SetInOperands(
  1948. {{SPV_OPERAND_TYPE_ID,
  1949. {inst->GetSingleWordInOperand(kind4 == FloatConstantKind::Zero
  1950. ? kFMixXIdInIdx
  1951. : kFMixYIdInIdx)}}});
  1952. return true;
  1953. }
  1954. }
  1955. return false;
  1956. };
  1957. }
  1958. // This rule handles addition of zero for integers.
  1959. FoldingRule RedundantIAdd() {
  1960. return [](IRContext* context, Instruction* inst,
  1961. const std::vector<const analysis::Constant*>& constants) {
  1962. assert(inst->opcode() == SpvOpIAdd && "Wrong opcode. Should be OpIAdd.");
  1963. uint32_t operand = std::numeric_limits<uint32_t>::max();
  1964. const analysis::Type* operand_type = nullptr;
  1965. if (constants[0] && constants[0]->IsZero()) {
  1966. operand = inst->GetSingleWordInOperand(1);
  1967. operand_type = constants[0]->type();
  1968. } else if (constants[1] && constants[1]->IsZero()) {
  1969. operand = inst->GetSingleWordInOperand(0);
  1970. operand_type = constants[1]->type();
  1971. }
  1972. if (operand != std::numeric_limits<uint32_t>::max()) {
  1973. const analysis::Type* inst_type =
  1974. context->get_type_mgr()->GetType(inst->type_id());
  1975. if (inst_type->IsSame(operand_type)) {
  1976. inst->SetOpcode(SpvOpCopyObject);
  1977. } else {
  1978. inst->SetOpcode(SpvOpBitcast);
  1979. }
  1980. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {operand}}});
  1981. return true;
  1982. }
  1983. return false;
  1984. };
  1985. }
  1986. // This rule look for a dot with a constant vector containing a single 1 and
  1987. // the rest 0s. This is the same as doing an extract.
  1988. FoldingRule DotProductDoingExtract() {
  1989. return [](IRContext* context, Instruction* inst,
  1990. const std::vector<const analysis::Constant*>& constants) {
  1991. assert(inst->opcode() == SpvOpDot && "Wrong opcode. Should be OpDot.");
  1992. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1993. if (!inst->IsFloatingPointFoldingAllowed()) {
  1994. return false;
  1995. }
  1996. for (int i = 0; i < 2; ++i) {
  1997. if (!constants[i]) {
  1998. continue;
  1999. }
  2000. const analysis::Vector* vector_type = constants[i]->type()->AsVector();
  2001. assert(vector_type && "Inputs to OpDot must be vectors.");
  2002. const analysis::Float* element_type =
  2003. vector_type->element_type()->AsFloat();
  2004. assert(element_type && "Inputs to OpDot must be vectors of floats.");
  2005. uint32_t element_width = element_type->width();
  2006. if (element_width != 32 && element_width != 64) {
  2007. return false;
  2008. }
  2009. std::vector<const analysis::Constant*> components;
  2010. components = constants[i]->GetVectorComponents(const_mgr);
  2011. const uint32_t kNotFound = std::numeric_limits<uint32_t>::max();
  2012. uint32_t component_with_one = kNotFound;
  2013. bool all_others_zero = true;
  2014. for (uint32_t j = 0; j < components.size(); ++j) {
  2015. const analysis::Constant* element = components[j];
  2016. double value =
  2017. (element_width == 32 ? element->GetFloat() : element->GetDouble());
  2018. if (value == 0.0) {
  2019. continue;
  2020. } else if (value == 1.0) {
  2021. if (component_with_one == kNotFound) {
  2022. component_with_one = j;
  2023. } else {
  2024. component_with_one = kNotFound;
  2025. break;
  2026. }
  2027. } else {
  2028. all_others_zero = false;
  2029. break;
  2030. }
  2031. }
  2032. if (!all_others_zero || component_with_one == kNotFound) {
  2033. continue;
  2034. }
  2035. std::vector<Operand> operands;
  2036. operands.push_back(
  2037. {SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(1u - i)}});
  2038. operands.push_back(
  2039. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {component_with_one}});
  2040. inst->SetOpcode(SpvOpCompositeExtract);
  2041. inst->SetInOperands(std::move(operands));
  2042. return true;
  2043. }
  2044. return false;
  2045. };
  2046. }
  2047. // If we are storing an undef, then we can remove the store.
  2048. //
  2049. // TODO: We can do something similar for OpImageWrite, but checking for volatile
  2050. // is complicated. Waiting to see if it is needed.
  2051. FoldingRule StoringUndef() {
  2052. return [](IRContext* context, Instruction* inst,
  2053. const std::vector<const analysis::Constant*>&) {
  2054. assert(inst->opcode() == SpvOpStore && "Wrong opcode. Should be OpStore.");
  2055. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  2056. // If this is a volatile store, the store cannot be removed.
  2057. if (inst->NumInOperands() == 3) {
  2058. if (inst->GetSingleWordInOperand(2) & SpvMemoryAccessVolatileMask) {
  2059. return false;
  2060. }
  2061. }
  2062. uint32_t object_id = inst->GetSingleWordInOperand(kStoreObjectInIdx);
  2063. Instruction* object_inst = def_use_mgr->GetDef(object_id);
  2064. if (object_inst->opcode() == SpvOpUndef) {
  2065. inst->ToNop();
  2066. return true;
  2067. }
  2068. return false;
  2069. };
  2070. }
  2071. FoldingRule VectorShuffleFeedingShuffle() {
  2072. return [](IRContext* context, Instruction* inst,
  2073. const std::vector<const analysis::Constant*>&) {
  2074. assert(inst->opcode() == SpvOpVectorShuffle &&
  2075. "Wrong opcode. Should be OpVectorShuffle.");
  2076. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  2077. analysis::TypeManager* type_mgr = context->get_type_mgr();
  2078. Instruction* feeding_shuffle_inst =
  2079. def_use_mgr->GetDef(inst->GetSingleWordInOperand(0));
  2080. analysis::Vector* op0_type =
  2081. type_mgr->GetType(feeding_shuffle_inst->type_id())->AsVector();
  2082. uint32_t op0_length = op0_type->element_count();
  2083. bool feeder_is_op0 = true;
  2084. if (feeding_shuffle_inst->opcode() != SpvOpVectorShuffle) {
  2085. feeding_shuffle_inst =
  2086. def_use_mgr->GetDef(inst->GetSingleWordInOperand(1));
  2087. feeder_is_op0 = false;
  2088. }
  2089. if (feeding_shuffle_inst->opcode() != SpvOpVectorShuffle) {
  2090. return false;
  2091. }
  2092. Instruction* feeder2 =
  2093. def_use_mgr->GetDef(feeding_shuffle_inst->GetSingleWordInOperand(0));
  2094. analysis::Vector* feeder_op0_type =
  2095. type_mgr->GetType(feeder2->type_id())->AsVector();
  2096. uint32_t feeder_op0_length = feeder_op0_type->element_count();
  2097. uint32_t new_feeder_id = 0;
  2098. std::vector<Operand> new_operands;
  2099. new_operands.resize(
  2100. 2, {SPV_OPERAND_TYPE_ID, {0}}); // Place holders for vector operands.
  2101. const uint32_t undef_literal = 0xffffffff;
  2102. for (uint32_t op = 2; op < inst->NumInOperands(); ++op) {
  2103. uint32_t component_index = inst->GetSingleWordInOperand(op);
  2104. // Do not interpret the undefined value literal as coming from operand 1.
  2105. if (component_index != undef_literal &&
  2106. feeder_is_op0 == (component_index < op0_length)) {
  2107. // This component comes from the feeding_shuffle_inst. Update
  2108. // |component_index| to be the index into the operand of the feeder.
  2109. // Adjust component_index to get the index into the operands of the
  2110. // feeding_shuffle_inst.
  2111. if (component_index >= op0_length) {
  2112. component_index -= op0_length;
  2113. }
  2114. component_index =
  2115. feeding_shuffle_inst->GetSingleWordInOperand(component_index + 2);
  2116. // Check if we are using a component from the first or second operand of
  2117. // the feeding instruction.
  2118. if (component_index < feeder_op0_length) {
  2119. if (new_feeder_id == 0) {
  2120. // First time through, save the id of the operand the element comes
  2121. // from.
  2122. new_feeder_id = feeding_shuffle_inst->GetSingleWordInOperand(0);
  2123. } else if (new_feeder_id !=
  2124. feeding_shuffle_inst->GetSingleWordInOperand(0)) {
  2125. // We need both elements of the feeding_shuffle_inst, so we cannot
  2126. // fold.
  2127. return false;
  2128. }
  2129. } else if (component_index != undef_literal) {
  2130. if (new_feeder_id == 0) {
  2131. // First time through, save the id of the operand the element comes
  2132. // from.
  2133. new_feeder_id = feeding_shuffle_inst->GetSingleWordInOperand(1);
  2134. } else if (new_feeder_id !=
  2135. feeding_shuffle_inst->GetSingleWordInOperand(1)) {
  2136. // We need both elements of the feeding_shuffle_inst, so we cannot
  2137. // fold.
  2138. return false;
  2139. }
  2140. component_index -= feeder_op0_length;
  2141. }
  2142. if (!feeder_is_op0 && component_index != undef_literal) {
  2143. component_index += op0_length;
  2144. }
  2145. }
  2146. new_operands.push_back(
  2147. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {component_index}});
  2148. }
  2149. if (new_feeder_id == 0) {
  2150. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  2151. const analysis::Type* type =
  2152. type_mgr->GetType(feeding_shuffle_inst->type_id());
  2153. const analysis::Constant* null_const = const_mgr->GetConstant(type, {});
  2154. new_feeder_id =
  2155. const_mgr->GetDefiningInstruction(null_const, 0)->result_id();
  2156. }
  2157. if (feeder_is_op0) {
  2158. // If the size of the first vector operand changed then the indices
  2159. // referring to the second operand need to be adjusted.
  2160. Instruction* new_feeder_inst = def_use_mgr->GetDef(new_feeder_id);
  2161. analysis::Type* new_feeder_type =
  2162. type_mgr->GetType(new_feeder_inst->type_id());
  2163. uint32_t new_op0_size = new_feeder_type->AsVector()->element_count();
  2164. int32_t adjustment = op0_length - new_op0_size;
  2165. if (adjustment != 0) {
  2166. for (uint32_t i = 2; i < new_operands.size(); i++) {
  2167. if (inst->GetSingleWordInOperand(i) >= op0_length) {
  2168. new_operands[i].words[0] -= adjustment;
  2169. }
  2170. }
  2171. }
  2172. new_operands[0].words[0] = new_feeder_id;
  2173. new_operands[1] = inst->GetInOperand(1);
  2174. } else {
  2175. new_operands[1].words[0] = new_feeder_id;
  2176. new_operands[0] = inst->GetInOperand(0);
  2177. }
  2178. inst->SetInOperands(std::move(new_operands));
  2179. return true;
  2180. };
  2181. }
  2182. // Removes duplicate ids from the interface list of an OpEntryPoint
  2183. // instruction.
  2184. FoldingRule RemoveRedundantOperands() {
  2185. return [](IRContext*, Instruction* inst,
  2186. const std::vector<const analysis::Constant*>&) {
  2187. assert(inst->opcode() == SpvOpEntryPoint &&
  2188. "Wrong opcode. Should be OpEntryPoint.");
  2189. bool has_redundant_operand = false;
  2190. std::unordered_set<uint32_t> seen_operands;
  2191. std::vector<Operand> new_operands;
  2192. new_operands.emplace_back(inst->GetOperand(0));
  2193. new_operands.emplace_back(inst->GetOperand(1));
  2194. new_operands.emplace_back(inst->GetOperand(2));
  2195. for (uint32_t i = 3; i < inst->NumOperands(); ++i) {
  2196. if (seen_operands.insert(inst->GetSingleWordOperand(i)).second) {
  2197. new_operands.emplace_back(inst->GetOperand(i));
  2198. } else {
  2199. has_redundant_operand = true;
  2200. }
  2201. }
  2202. if (!has_redundant_operand) {
  2203. return false;
  2204. }
  2205. inst->SetInOperands(std::move(new_operands));
  2206. return true;
  2207. };
  2208. }
  2209. // If an image instruction's operand is a constant, updates the image operand
  2210. // flag from Offset to ConstOffset.
  2211. FoldingRule UpdateImageOperands() {
  2212. return [](IRContext*, Instruction* inst,
  2213. const std::vector<const analysis::Constant*>& constants) {
  2214. const auto opcode = inst->opcode();
  2215. (void)opcode;
  2216. assert((opcode == SpvOpImageSampleImplicitLod ||
  2217. opcode == SpvOpImageSampleExplicitLod ||
  2218. opcode == SpvOpImageSampleDrefImplicitLod ||
  2219. opcode == SpvOpImageSampleDrefExplicitLod ||
  2220. opcode == SpvOpImageSampleProjImplicitLod ||
  2221. opcode == SpvOpImageSampleProjExplicitLod ||
  2222. opcode == SpvOpImageSampleProjDrefImplicitLod ||
  2223. opcode == SpvOpImageSampleProjDrefExplicitLod ||
  2224. opcode == SpvOpImageFetch || opcode == SpvOpImageGather ||
  2225. opcode == SpvOpImageDrefGather || opcode == SpvOpImageRead ||
  2226. opcode == SpvOpImageWrite ||
  2227. opcode == SpvOpImageSparseSampleImplicitLod ||
  2228. opcode == SpvOpImageSparseSampleExplicitLod ||
  2229. opcode == SpvOpImageSparseSampleDrefImplicitLod ||
  2230. opcode == SpvOpImageSparseSampleDrefExplicitLod ||
  2231. opcode == SpvOpImageSparseSampleProjImplicitLod ||
  2232. opcode == SpvOpImageSparseSampleProjExplicitLod ||
  2233. opcode == SpvOpImageSparseSampleProjDrefImplicitLod ||
  2234. opcode == SpvOpImageSparseSampleProjDrefExplicitLod ||
  2235. opcode == SpvOpImageSparseFetch ||
  2236. opcode == SpvOpImageSparseGather ||
  2237. opcode == SpvOpImageSparseDrefGather ||
  2238. opcode == SpvOpImageSparseRead) &&
  2239. "Wrong opcode. Should be an image instruction.");
  2240. int32_t operand_index = ImageOperandsMaskInOperandIndex(inst);
  2241. if (operand_index >= 0) {
  2242. auto image_operands = inst->GetSingleWordInOperand(operand_index);
  2243. if (image_operands & SpvImageOperandsOffsetMask) {
  2244. uint32_t offset_operand_index = operand_index + 1;
  2245. if (image_operands & SpvImageOperandsBiasMask) offset_operand_index++;
  2246. if (image_operands & SpvImageOperandsLodMask) offset_operand_index++;
  2247. if (image_operands & SpvImageOperandsGradMask)
  2248. offset_operand_index += 2;
  2249. assert(((image_operands & SpvImageOperandsConstOffsetMask) == 0) &&
  2250. "Offset and ConstOffset may not be used together");
  2251. if (offset_operand_index < inst->NumOperands()) {
  2252. if (constants[offset_operand_index]) {
  2253. image_operands = image_operands | SpvImageOperandsConstOffsetMask;
  2254. image_operands = image_operands & ~SpvImageOperandsOffsetMask;
  2255. inst->SetInOperand(operand_index, {image_operands});
  2256. return true;
  2257. }
  2258. }
  2259. }
  2260. }
  2261. return false;
  2262. };
  2263. }
  2264. } // namespace
  2265. void FoldingRules::AddFoldingRules() {
  2266. // Add all folding rules to the list for the opcodes to which they apply.
  2267. // Note that the order in which rules are added to the list matters. If a rule
  2268. // applies to the instruction, the rest of the rules will not be attempted.
  2269. // Take that into consideration.
  2270. rules_[SpvOpBitcast].push_back(BitCastScalarOrVector());
  2271. rules_[SpvOpCompositeConstruct].push_back(CompositeExtractFeedingConstruct);
  2272. rules_[SpvOpCompositeExtract].push_back(InsertFeedingExtract());
  2273. rules_[SpvOpCompositeExtract].push_back(CompositeConstructFeedingExtract);
  2274. rules_[SpvOpCompositeExtract].push_back(VectorShuffleFeedingExtract());
  2275. rules_[SpvOpCompositeExtract].push_back(FMixFeedingExtract());
  2276. rules_[SpvOpDot].push_back(DotProductDoingExtract());
  2277. rules_[SpvOpEntryPoint].push_back(RemoveRedundantOperands());
  2278. rules_[SpvOpFAdd].push_back(RedundantFAdd());
  2279. rules_[SpvOpFAdd].push_back(MergeAddNegateArithmetic());
  2280. rules_[SpvOpFAdd].push_back(MergeAddAddArithmetic());
  2281. rules_[SpvOpFAdd].push_back(MergeAddSubArithmetic());
  2282. rules_[SpvOpFAdd].push_back(MergeGenericAddSubArithmetic());
  2283. rules_[SpvOpFAdd].push_back(FactorAddMuls());
  2284. rules_[SpvOpFAdd].push_back(MergeMulAddArithmetic);
  2285. rules_[SpvOpFDiv].push_back(RedundantFDiv());
  2286. rules_[SpvOpFDiv].push_back(ReciprocalFDiv());
  2287. rules_[SpvOpFDiv].push_back(MergeDivDivArithmetic());
  2288. rules_[SpvOpFDiv].push_back(MergeDivMulArithmetic());
  2289. rules_[SpvOpFDiv].push_back(MergeDivNegateArithmetic());
  2290. rules_[SpvOpFMul].push_back(RedundantFMul());
  2291. rules_[SpvOpFMul].push_back(MergeMulMulArithmetic());
  2292. rules_[SpvOpFMul].push_back(MergeMulDivArithmetic());
  2293. rules_[SpvOpFMul].push_back(MergeMulNegateArithmetic());
  2294. rules_[SpvOpFNegate].push_back(MergeNegateArithmetic());
  2295. rules_[SpvOpFNegate].push_back(MergeNegateAddSubArithmetic());
  2296. rules_[SpvOpFNegate].push_back(MergeNegateMulDivArithmetic());
  2297. rules_[SpvOpFSub].push_back(RedundantFSub());
  2298. rules_[SpvOpFSub].push_back(MergeSubNegateArithmetic());
  2299. rules_[SpvOpFSub].push_back(MergeSubAddArithmetic());
  2300. rules_[SpvOpFSub].push_back(MergeSubSubArithmetic());
  2301. rules_[SpvOpIAdd].push_back(RedundantIAdd());
  2302. rules_[SpvOpIAdd].push_back(MergeAddNegateArithmetic());
  2303. rules_[SpvOpIAdd].push_back(MergeAddAddArithmetic());
  2304. rules_[SpvOpIAdd].push_back(MergeAddSubArithmetic());
  2305. rules_[SpvOpIAdd].push_back(MergeGenericAddSubArithmetic());
  2306. rules_[SpvOpIAdd].push_back(FactorAddMuls());
  2307. rules_[SpvOpIMul].push_back(IntMultipleBy1());
  2308. rules_[SpvOpIMul].push_back(MergeMulMulArithmetic());
  2309. rules_[SpvOpIMul].push_back(MergeMulNegateArithmetic());
  2310. rules_[SpvOpISub].push_back(MergeSubNegateArithmetic());
  2311. rules_[SpvOpISub].push_back(MergeSubAddArithmetic());
  2312. rules_[SpvOpISub].push_back(MergeSubSubArithmetic());
  2313. rules_[SpvOpPhi].push_back(RedundantPhi());
  2314. rules_[SpvOpSNegate].push_back(MergeNegateArithmetic());
  2315. rules_[SpvOpSNegate].push_back(MergeNegateMulDivArithmetic());
  2316. rules_[SpvOpSNegate].push_back(MergeNegateAddSubArithmetic());
  2317. rules_[SpvOpSelect].push_back(RedundantSelect());
  2318. rules_[SpvOpStore].push_back(StoringUndef());
  2319. rules_[SpvOpVectorShuffle].push_back(VectorShuffleFeedingShuffle());
  2320. rules_[SpvOpImageSampleImplicitLod].push_back(UpdateImageOperands());
  2321. rules_[SpvOpImageSampleExplicitLod].push_back(UpdateImageOperands());
  2322. rules_[SpvOpImageSampleDrefImplicitLod].push_back(UpdateImageOperands());
  2323. rules_[SpvOpImageSampleDrefExplicitLod].push_back(UpdateImageOperands());
  2324. rules_[SpvOpImageSampleProjImplicitLod].push_back(UpdateImageOperands());
  2325. rules_[SpvOpImageSampleProjExplicitLod].push_back(UpdateImageOperands());
  2326. rules_[SpvOpImageSampleProjDrefImplicitLod].push_back(UpdateImageOperands());
  2327. rules_[SpvOpImageSampleProjDrefExplicitLod].push_back(UpdateImageOperands());
  2328. rules_[SpvOpImageFetch].push_back(UpdateImageOperands());
  2329. rules_[SpvOpImageGather].push_back(UpdateImageOperands());
  2330. rules_[SpvOpImageDrefGather].push_back(UpdateImageOperands());
  2331. rules_[SpvOpImageRead].push_back(UpdateImageOperands());
  2332. rules_[SpvOpImageWrite].push_back(UpdateImageOperands());
  2333. rules_[SpvOpImageSparseSampleImplicitLod].push_back(UpdateImageOperands());
  2334. rules_[SpvOpImageSparseSampleExplicitLod].push_back(UpdateImageOperands());
  2335. rules_[SpvOpImageSparseSampleDrefImplicitLod].push_back(
  2336. UpdateImageOperands());
  2337. rules_[SpvOpImageSparseSampleDrefExplicitLod].push_back(
  2338. UpdateImageOperands());
  2339. rules_[SpvOpImageSparseSampleProjImplicitLod].push_back(
  2340. UpdateImageOperands());
  2341. rules_[SpvOpImageSparseSampleProjExplicitLod].push_back(
  2342. UpdateImageOperands());
  2343. rules_[SpvOpImageSparseSampleProjDrefImplicitLod].push_back(
  2344. UpdateImageOperands());
  2345. rules_[SpvOpImageSparseSampleProjDrefExplicitLod].push_back(
  2346. UpdateImageOperands());
  2347. rules_[SpvOpImageSparseFetch].push_back(UpdateImageOperands());
  2348. rules_[SpvOpImageSparseGather].push_back(UpdateImageOperands());
  2349. rules_[SpvOpImageSparseDrefGather].push_back(UpdateImageOperands());
  2350. rules_[SpvOpImageSparseRead].push_back(UpdateImageOperands());
  2351. FeatureManager* feature_manager = context_->get_feature_mgr();
  2352. // Add rules for GLSLstd450
  2353. uint32_t ext_inst_glslstd450_id =
  2354. feature_manager->GetExtInstImportId_GLSLstd450();
  2355. if (ext_inst_glslstd450_id != 0) {
  2356. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMix}].push_back(
  2357. RedundantFMix());
  2358. }
  2359. }
  2360. } // namespace opt
  2361. } // namespace spvtools