folding_rules.cpp 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636
  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. FoldingRule IntMultipleBy1() {
  1281. return [](IRContext*, Instruction* inst,
  1282. const std::vector<const analysis::Constant*>& constants) {
  1283. assert(inst->opcode() == SpvOpIMul && "Wrong opcode. Should be OpIMul.");
  1284. for (uint32_t i = 0; i < 2; i++) {
  1285. if (constants[i] == nullptr) {
  1286. continue;
  1287. }
  1288. const analysis::IntConstant* int_constant = constants[i]->AsIntConstant();
  1289. if (int_constant) {
  1290. uint32_t width = ElementWidth(int_constant->type());
  1291. if (width != 32 && width != 64) return false;
  1292. bool is_one = (width == 32) ? int_constant->GetU32BitValue() == 1u
  1293. : int_constant->GetU64BitValue() == 1ull;
  1294. if (is_one) {
  1295. inst->SetOpcode(SpvOpCopyObject);
  1296. inst->SetInOperands(
  1297. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(1 - i)}}});
  1298. return true;
  1299. }
  1300. }
  1301. }
  1302. return false;
  1303. };
  1304. }
  1305. // Returns the number of elements that the |index|th in operand in |inst|
  1306. // contributes to the result of |inst|. |inst| must be an
  1307. // OpCompositeConstructInstruction.
  1308. uint32_t GetNumOfElementsContributedByOperand(IRContext* context,
  1309. const Instruction* inst,
  1310. uint32_t index) {
  1311. assert(inst->opcode() == SpvOpCompositeConstruct);
  1312. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1313. analysis::TypeManager* type_mgr = context->get_type_mgr();
  1314. analysis::Vector* result_type =
  1315. type_mgr->GetType(inst->type_id())->AsVector();
  1316. if (result_type == nullptr) {
  1317. // If the result of the OpCompositeConstruct is not a vector then every
  1318. // operands corresponds to a single element in the result.
  1319. return 1;
  1320. }
  1321. // If the result type is a vector then the operands are either scalars or
  1322. // vectors. If it is a scalar, then it corresponds to a single element. If it
  1323. // is a vector, then each element in the vector will be an element in the
  1324. // result.
  1325. uint32_t id = inst->GetSingleWordInOperand(index);
  1326. Instruction* def = def_use_mgr->GetDef(id);
  1327. analysis::Vector* type = type_mgr->GetType(def->type_id())->AsVector();
  1328. if (type == nullptr) {
  1329. return 1;
  1330. }
  1331. return type->element_count();
  1332. }
  1333. // Returns the in-operands for an OpCompositeExtract instruction that are needed
  1334. // to extract the |result_index|th element in the result of |inst| without using
  1335. // the result of |inst|. Returns the empty vector if |result_index| is
  1336. // out-of-bounds. |inst| must be an |OpCompositeConstruct| instruction.
  1337. std::vector<Operand> GetExtractOperandsForElementOfCompositeConstruct(
  1338. IRContext* context, const Instruction* inst, uint32_t result_index) {
  1339. assert(inst->opcode() == SpvOpCompositeConstruct);
  1340. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1341. analysis::TypeManager* type_mgr = context->get_type_mgr();
  1342. analysis::Type* result_type = type_mgr->GetType(inst->type_id());
  1343. if (result_type->AsVector() == nullptr) {
  1344. uint32_t id = inst->GetSingleWordInOperand(result_index);
  1345. return {Operand(SPV_OPERAND_TYPE_ID, {id})};
  1346. }
  1347. // If the result type is a vector, then vector operands are concatenated.
  1348. uint32_t total_element_count = 0;
  1349. for (uint32_t idx = 0; idx < inst->NumInOperands(); ++idx) {
  1350. uint32_t element_count =
  1351. GetNumOfElementsContributedByOperand(context, inst, idx);
  1352. total_element_count += element_count;
  1353. if (result_index < total_element_count) {
  1354. std::vector<Operand> operands;
  1355. uint32_t id = inst->GetSingleWordInOperand(idx);
  1356. Instruction* operand_def = def_use_mgr->GetDef(id);
  1357. analysis::Type* operand_type = type_mgr->GetType(operand_def->type_id());
  1358. operands.push_back({SPV_OPERAND_TYPE_ID, {id}});
  1359. if (operand_type->AsVector()) {
  1360. uint32_t start_index_of_id = total_element_count - element_count;
  1361. uint32_t index_into_id = result_index - start_index_of_id;
  1362. operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {index_into_id}});
  1363. }
  1364. return operands;
  1365. }
  1366. }
  1367. return {};
  1368. }
  1369. bool CompositeConstructFeedingExtract(
  1370. IRContext* context, Instruction* inst,
  1371. const std::vector<const analysis::Constant*>&) {
  1372. // If the input to an OpCompositeExtract is an OpCompositeConstruct,
  1373. // then we can simply use the appropriate element in the construction.
  1374. assert(inst->opcode() == SpvOpCompositeExtract &&
  1375. "Wrong opcode. Should be OpCompositeExtract.");
  1376. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1377. // If there are no index operands, then this rule cannot do anything.
  1378. if (inst->NumInOperands() <= 1) {
  1379. return false;
  1380. }
  1381. uint32_t cid = inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1382. Instruction* cinst = def_use_mgr->GetDef(cid);
  1383. if (cinst->opcode() != SpvOpCompositeConstruct) {
  1384. return false;
  1385. }
  1386. uint32_t index_into_result = inst->GetSingleWordInOperand(1);
  1387. std::vector<Operand> operands =
  1388. GetExtractOperandsForElementOfCompositeConstruct(context, cinst,
  1389. index_into_result);
  1390. if (operands.empty()) {
  1391. return false;
  1392. }
  1393. // Add the remaining indices for extraction.
  1394. for (uint32_t i = 2; i < inst->NumInOperands(); ++i) {
  1395. operands.push_back(
  1396. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {inst->GetSingleWordInOperand(i)}});
  1397. }
  1398. if (operands.size() == 1) {
  1399. // If there were no extra indices, then we have the final object. No need
  1400. // to extract any more.
  1401. inst->SetOpcode(SpvOpCopyObject);
  1402. }
  1403. inst->SetInOperands(std::move(operands));
  1404. return true;
  1405. }
  1406. // If the OpCompositeConstruct is simply putting back together elements that
  1407. // where extracted from the same source, we can simply reuse the source.
  1408. //
  1409. // This is a common code pattern because of the way that scalar replacement
  1410. // works.
  1411. bool CompositeExtractFeedingConstruct(
  1412. IRContext* context, Instruction* inst,
  1413. const std::vector<const analysis::Constant*>&) {
  1414. assert(inst->opcode() == SpvOpCompositeConstruct &&
  1415. "Wrong opcode. Should be OpCompositeConstruct.");
  1416. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1417. uint32_t original_id = 0;
  1418. if (inst->NumInOperands() == 0) {
  1419. // The struct being constructed has no members.
  1420. return false;
  1421. }
  1422. // Check each element to make sure they are:
  1423. // - extractions
  1424. // - extracting the same position they are inserting
  1425. // - all extract from the same id.
  1426. for (uint32_t i = 0; i < inst->NumInOperands(); ++i) {
  1427. const uint32_t element_id = inst->GetSingleWordInOperand(i);
  1428. Instruction* element_inst = def_use_mgr->GetDef(element_id);
  1429. if (element_inst->opcode() != SpvOpCompositeExtract) {
  1430. return false;
  1431. }
  1432. if (element_inst->NumInOperands() != 2) {
  1433. return false;
  1434. }
  1435. if (element_inst->GetSingleWordInOperand(1) != i) {
  1436. return false;
  1437. }
  1438. if (i == 0) {
  1439. original_id =
  1440. element_inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1441. } else if (original_id !=
  1442. element_inst->GetSingleWordInOperand(kExtractCompositeIdInIdx)) {
  1443. return false;
  1444. }
  1445. }
  1446. // The last check it to see that the object being extracted from is the
  1447. // correct type.
  1448. Instruction* original_inst = def_use_mgr->GetDef(original_id);
  1449. if (original_inst->type_id() != inst->type_id()) {
  1450. return false;
  1451. }
  1452. // Simplify by using the original object.
  1453. inst->SetOpcode(SpvOpCopyObject);
  1454. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {original_id}}});
  1455. return true;
  1456. }
  1457. FoldingRule InsertFeedingExtract() {
  1458. return [](IRContext* context, Instruction* inst,
  1459. const std::vector<const analysis::Constant*>&) {
  1460. assert(inst->opcode() == SpvOpCompositeExtract &&
  1461. "Wrong opcode. Should be OpCompositeExtract.");
  1462. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1463. uint32_t cid = inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1464. Instruction* cinst = def_use_mgr->GetDef(cid);
  1465. if (cinst->opcode() != SpvOpCompositeInsert) {
  1466. return false;
  1467. }
  1468. // Find the first position where the list of insert and extract indicies
  1469. // differ, if at all.
  1470. uint32_t i;
  1471. for (i = 1; i < inst->NumInOperands(); ++i) {
  1472. if (i + 1 >= cinst->NumInOperands()) {
  1473. break;
  1474. }
  1475. if (inst->GetSingleWordInOperand(i) !=
  1476. cinst->GetSingleWordInOperand(i + 1)) {
  1477. break;
  1478. }
  1479. }
  1480. // We are extracting the element that was inserted.
  1481. if (i == inst->NumInOperands() && i + 1 == cinst->NumInOperands()) {
  1482. inst->SetOpcode(SpvOpCopyObject);
  1483. inst->SetInOperands(
  1484. {{SPV_OPERAND_TYPE_ID,
  1485. {cinst->GetSingleWordInOperand(kInsertObjectIdInIdx)}}});
  1486. return true;
  1487. }
  1488. // Extracting the value that was inserted along with values for the base
  1489. // composite. Cannot do anything.
  1490. if (i == inst->NumInOperands()) {
  1491. return false;
  1492. }
  1493. // Extracting an element of the value that was inserted. Extract from
  1494. // that value directly.
  1495. if (i + 1 == cinst->NumInOperands()) {
  1496. std::vector<Operand> operands;
  1497. operands.push_back(
  1498. {SPV_OPERAND_TYPE_ID,
  1499. {cinst->GetSingleWordInOperand(kInsertObjectIdInIdx)}});
  1500. for (; i < inst->NumInOperands(); ++i) {
  1501. operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER,
  1502. {inst->GetSingleWordInOperand(i)}});
  1503. }
  1504. inst->SetInOperands(std::move(operands));
  1505. return true;
  1506. }
  1507. // Extracting a value that is disjoint from the element being inserted.
  1508. // Rewrite the extract to use the composite input to the insert.
  1509. std::vector<Operand> operands;
  1510. operands.push_back(
  1511. {SPV_OPERAND_TYPE_ID,
  1512. {cinst->GetSingleWordInOperand(kInsertCompositeIdInIdx)}});
  1513. for (i = 1; i < inst->NumInOperands(); ++i) {
  1514. operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER,
  1515. {inst->GetSingleWordInOperand(i)}});
  1516. }
  1517. inst->SetInOperands(std::move(operands));
  1518. return true;
  1519. };
  1520. }
  1521. // When a VectorShuffle is feeding an Extract, we can extract from one of the
  1522. // operands of the VectorShuffle. We just need to adjust the index in the
  1523. // extract instruction.
  1524. FoldingRule VectorShuffleFeedingExtract() {
  1525. return [](IRContext* context, Instruction* inst,
  1526. const std::vector<const analysis::Constant*>&) {
  1527. assert(inst->opcode() == SpvOpCompositeExtract &&
  1528. "Wrong opcode. Should be OpCompositeExtract.");
  1529. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1530. analysis::TypeManager* type_mgr = context->get_type_mgr();
  1531. uint32_t cid = inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1532. Instruction* cinst = def_use_mgr->GetDef(cid);
  1533. if (cinst->opcode() != SpvOpVectorShuffle) {
  1534. return false;
  1535. }
  1536. // Find the size of the first vector operand of the VectorShuffle
  1537. Instruction* first_input =
  1538. def_use_mgr->GetDef(cinst->GetSingleWordInOperand(0));
  1539. analysis::Type* first_input_type =
  1540. type_mgr->GetType(first_input->type_id());
  1541. assert(first_input_type->AsVector() &&
  1542. "Input to vector shuffle should be vectors.");
  1543. uint32_t first_input_size = first_input_type->AsVector()->element_count();
  1544. // Get index of the element the vector shuffle is placing in the position
  1545. // being extracted.
  1546. uint32_t new_index =
  1547. cinst->GetSingleWordInOperand(2 + inst->GetSingleWordInOperand(1));
  1548. // Extracting an undefined value so fold this extract into an undef.
  1549. const uint32_t undef_literal_value = 0xffffffff;
  1550. if (new_index == undef_literal_value) {
  1551. inst->SetOpcode(SpvOpUndef);
  1552. inst->SetInOperands({});
  1553. return true;
  1554. }
  1555. // Get the id of the of the vector the elemtent comes from, and update the
  1556. // index if needed.
  1557. uint32_t new_vector = 0;
  1558. if (new_index < first_input_size) {
  1559. new_vector = cinst->GetSingleWordInOperand(0);
  1560. } else {
  1561. new_vector = cinst->GetSingleWordInOperand(1);
  1562. new_index -= first_input_size;
  1563. }
  1564. // Update the extract instruction.
  1565. inst->SetInOperand(kExtractCompositeIdInIdx, {new_vector});
  1566. inst->SetInOperand(1, {new_index});
  1567. return true;
  1568. };
  1569. }
  1570. // When an FMix with is feeding an Extract that extracts an element whose
  1571. // corresponding |a| in the FMix is 0 or 1, we can extract from one of the
  1572. // operands of the FMix.
  1573. FoldingRule FMixFeedingExtract() {
  1574. return [](IRContext* context, Instruction* inst,
  1575. const std::vector<const analysis::Constant*>&) {
  1576. assert(inst->opcode() == SpvOpCompositeExtract &&
  1577. "Wrong opcode. Should be OpCompositeExtract.");
  1578. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1579. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1580. uint32_t composite_id =
  1581. inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1582. Instruction* composite_inst = def_use_mgr->GetDef(composite_id);
  1583. if (composite_inst->opcode() != SpvOpExtInst) {
  1584. return false;
  1585. }
  1586. uint32_t inst_set_id =
  1587. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450();
  1588. if (composite_inst->GetSingleWordInOperand(kExtInstSetIdInIdx) !=
  1589. inst_set_id ||
  1590. composite_inst->GetSingleWordInOperand(kExtInstInstructionInIdx) !=
  1591. GLSLstd450FMix) {
  1592. return false;
  1593. }
  1594. // Get the |a| for the FMix instruction.
  1595. uint32_t a_id = composite_inst->GetSingleWordInOperand(kFMixAIdInIdx);
  1596. std::unique_ptr<Instruction> a(inst->Clone(context));
  1597. a->SetInOperand(kExtractCompositeIdInIdx, {a_id});
  1598. context->get_instruction_folder().FoldInstruction(a.get());
  1599. if (a->opcode() != SpvOpCopyObject) {
  1600. return false;
  1601. }
  1602. const analysis::Constant* a_const =
  1603. const_mgr->FindDeclaredConstant(a->GetSingleWordInOperand(0));
  1604. if (!a_const) {
  1605. return false;
  1606. }
  1607. bool use_x = false;
  1608. assert(a_const->type()->AsFloat());
  1609. double element_value = a_const->GetValueAsDouble();
  1610. if (element_value == 0.0) {
  1611. use_x = true;
  1612. } else if (element_value == 1.0) {
  1613. use_x = false;
  1614. } else {
  1615. return false;
  1616. }
  1617. // Get the id of the of the vector the element comes from.
  1618. uint32_t new_vector = 0;
  1619. if (use_x) {
  1620. new_vector = composite_inst->GetSingleWordInOperand(kFMixXIdInIdx);
  1621. } else {
  1622. new_vector = composite_inst->GetSingleWordInOperand(kFMixYIdInIdx);
  1623. }
  1624. // Update the extract instruction.
  1625. inst->SetInOperand(kExtractCompositeIdInIdx, {new_vector});
  1626. return true;
  1627. };
  1628. }
  1629. FoldingRule RedundantPhi() {
  1630. // An OpPhi instruction where all values are the same or the result of the phi
  1631. // itself, can be replaced by the value itself.
  1632. return [](IRContext*, Instruction* inst,
  1633. const std::vector<const analysis::Constant*>&) {
  1634. assert(inst->opcode() == SpvOpPhi && "Wrong opcode. Should be OpPhi.");
  1635. uint32_t incoming_value = 0;
  1636. for (uint32_t i = 0; i < inst->NumInOperands(); i += 2) {
  1637. uint32_t op_id = inst->GetSingleWordInOperand(i);
  1638. if (op_id == inst->result_id()) {
  1639. continue;
  1640. }
  1641. if (incoming_value == 0) {
  1642. incoming_value = op_id;
  1643. } else if (op_id != incoming_value) {
  1644. // Found two possible value. Can't simplify.
  1645. return false;
  1646. }
  1647. }
  1648. if (incoming_value == 0) {
  1649. // Code looks invalid. Don't do anything.
  1650. return false;
  1651. }
  1652. // We have a single incoming value. Simplify using that value.
  1653. inst->SetOpcode(SpvOpCopyObject);
  1654. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {incoming_value}}});
  1655. return true;
  1656. };
  1657. }
  1658. FoldingRule BitCastScalarOrVector() {
  1659. return [](IRContext* context, Instruction* inst,
  1660. const std::vector<const analysis::Constant*>& constants) {
  1661. assert(inst->opcode() == SpvOpBitcast && constants.size() == 1);
  1662. if (constants[0] == nullptr) return false;
  1663. const analysis::Type* type =
  1664. context->get_type_mgr()->GetType(inst->type_id());
  1665. if (HasFloatingPoint(type) && !inst->IsFloatingPointFoldingAllowed())
  1666. return false;
  1667. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1668. std::vector<uint32_t> words =
  1669. GetWordsFromNumericScalarOrVectorConstant(const_mgr, constants[0]);
  1670. if (words.size() == 0) return false;
  1671. const analysis::Constant* bitcasted_constant =
  1672. ConvertWordsToNumericScalarOrVectorConstant(const_mgr, words, type);
  1673. if (!bitcasted_constant) return false;
  1674. auto new_feeder_id =
  1675. const_mgr->GetDefiningInstruction(bitcasted_constant, inst->type_id())
  1676. ->result_id();
  1677. inst->SetOpcode(SpvOpCopyObject);
  1678. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {new_feeder_id}}});
  1679. return true;
  1680. };
  1681. }
  1682. FoldingRule RedundantSelect() {
  1683. // An OpSelect instruction where both values are the same or the condition is
  1684. // constant can be replaced by one of the values
  1685. return [](IRContext*, Instruction* inst,
  1686. const std::vector<const analysis::Constant*>& constants) {
  1687. assert(inst->opcode() == SpvOpSelect &&
  1688. "Wrong opcode. Should be OpSelect.");
  1689. assert(inst->NumInOperands() == 3);
  1690. assert(constants.size() == 3);
  1691. uint32_t true_id = inst->GetSingleWordInOperand(1);
  1692. uint32_t false_id = inst->GetSingleWordInOperand(2);
  1693. if (true_id == false_id) {
  1694. // Both results are the same, condition doesn't matter
  1695. inst->SetOpcode(SpvOpCopyObject);
  1696. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {true_id}}});
  1697. return true;
  1698. } else if (constants[0]) {
  1699. const analysis::Type* type = constants[0]->type();
  1700. if (type->AsBool()) {
  1701. // Scalar constant value, select the corresponding value.
  1702. inst->SetOpcode(SpvOpCopyObject);
  1703. if (constants[0]->AsNullConstant() ||
  1704. !constants[0]->AsBoolConstant()->value()) {
  1705. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {false_id}}});
  1706. } else {
  1707. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {true_id}}});
  1708. }
  1709. return true;
  1710. } else {
  1711. assert(type->AsVector());
  1712. if (constants[0]->AsNullConstant()) {
  1713. // All values come from false id.
  1714. inst->SetOpcode(SpvOpCopyObject);
  1715. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {false_id}}});
  1716. return true;
  1717. } else {
  1718. // Convert to a vector shuffle.
  1719. std::vector<Operand> ops;
  1720. ops.push_back({SPV_OPERAND_TYPE_ID, {true_id}});
  1721. ops.push_back({SPV_OPERAND_TYPE_ID, {false_id}});
  1722. const analysis::VectorConstant* vector_const =
  1723. constants[0]->AsVectorConstant();
  1724. uint32_t size =
  1725. static_cast<uint32_t>(vector_const->GetComponents().size());
  1726. for (uint32_t i = 0; i != size; ++i) {
  1727. const analysis::Constant* component =
  1728. vector_const->GetComponents()[i];
  1729. if (component->AsNullConstant() ||
  1730. !component->AsBoolConstant()->value()) {
  1731. // Selecting from the false vector which is the second input
  1732. // vector to the shuffle. Offset the index by |size|.
  1733. ops.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {i + size}});
  1734. } else {
  1735. // Selecting from true vector which is the first input vector to
  1736. // the shuffle.
  1737. ops.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {i}});
  1738. }
  1739. }
  1740. inst->SetOpcode(SpvOpVectorShuffle);
  1741. inst->SetInOperands(std::move(ops));
  1742. return true;
  1743. }
  1744. }
  1745. }
  1746. return false;
  1747. };
  1748. }
  1749. enum class FloatConstantKind { Unknown, Zero, One };
  1750. FloatConstantKind getFloatConstantKind(const analysis::Constant* constant) {
  1751. if (constant == nullptr) {
  1752. return FloatConstantKind::Unknown;
  1753. }
  1754. assert(HasFloatingPoint(constant->type()) && "Unexpected constant type");
  1755. if (constant->AsNullConstant()) {
  1756. return FloatConstantKind::Zero;
  1757. } else if (const analysis::VectorConstant* vc =
  1758. constant->AsVectorConstant()) {
  1759. const std::vector<const analysis::Constant*>& components =
  1760. vc->GetComponents();
  1761. assert(!components.empty());
  1762. FloatConstantKind kind = getFloatConstantKind(components[0]);
  1763. for (size_t i = 1; i < components.size(); ++i) {
  1764. if (getFloatConstantKind(components[i]) != kind) {
  1765. return FloatConstantKind::Unknown;
  1766. }
  1767. }
  1768. return kind;
  1769. } else if (const analysis::FloatConstant* fc = constant->AsFloatConstant()) {
  1770. if (fc->IsZero()) return FloatConstantKind::Zero;
  1771. uint32_t width = fc->type()->AsFloat()->width();
  1772. if (width != 32 && width != 64) return FloatConstantKind::Unknown;
  1773. double value = (width == 64) ? fc->GetDoubleValue() : fc->GetFloatValue();
  1774. if (value == 0.0) {
  1775. return FloatConstantKind::Zero;
  1776. } else if (value == 1.0) {
  1777. return FloatConstantKind::One;
  1778. } else {
  1779. return FloatConstantKind::Unknown;
  1780. }
  1781. } else {
  1782. return FloatConstantKind::Unknown;
  1783. }
  1784. }
  1785. FoldingRule RedundantFAdd() {
  1786. return [](IRContext*, Instruction* inst,
  1787. const std::vector<const analysis::Constant*>& constants) {
  1788. assert(inst->opcode() == SpvOpFAdd && "Wrong opcode. Should be OpFAdd.");
  1789. assert(constants.size() == 2);
  1790. if (!inst->IsFloatingPointFoldingAllowed()) {
  1791. return false;
  1792. }
  1793. FloatConstantKind kind0 = getFloatConstantKind(constants[0]);
  1794. FloatConstantKind kind1 = getFloatConstantKind(constants[1]);
  1795. if (kind0 == FloatConstantKind::Zero || kind1 == FloatConstantKind::Zero) {
  1796. inst->SetOpcode(SpvOpCopyObject);
  1797. inst->SetInOperands({{SPV_OPERAND_TYPE_ID,
  1798. {inst->GetSingleWordInOperand(
  1799. kind0 == FloatConstantKind::Zero ? 1 : 0)}}});
  1800. return true;
  1801. }
  1802. return false;
  1803. };
  1804. }
  1805. FoldingRule RedundantFSub() {
  1806. return [](IRContext*, Instruction* inst,
  1807. const std::vector<const analysis::Constant*>& constants) {
  1808. assert(inst->opcode() == SpvOpFSub && "Wrong opcode. Should be OpFSub.");
  1809. assert(constants.size() == 2);
  1810. if (!inst->IsFloatingPointFoldingAllowed()) {
  1811. return false;
  1812. }
  1813. FloatConstantKind kind0 = getFloatConstantKind(constants[0]);
  1814. FloatConstantKind kind1 = getFloatConstantKind(constants[1]);
  1815. if (kind0 == FloatConstantKind::Zero) {
  1816. inst->SetOpcode(SpvOpFNegate);
  1817. inst->SetInOperands(
  1818. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(1)}}});
  1819. return true;
  1820. }
  1821. if (kind1 == FloatConstantKind::Zero) {
  1822. inst->SetOpcode(SpvOpCopyObject);
  1823. inst->SetInOperands(
  1824. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(0)}}});
  1825. return true;
  1826. }
  1827. return false;
  1828. };
  1829. }
  1830. FoldingRule RedundantFMul() {
  1831. return [](IRContext*, Instruction* inst,
  1832. const std::vector<const analysis::Constant*>& constants) {
  1833. assert(inst->opcode() == SpvOpFMul && "Wrong opcode. Should be OpFMul.");
  1834. assert(constants.size() == 2);
  1835. if (!inst->IsFloatingPointFoldingAllowed()) {
  1836. return false;
  1837. }
  1838. FloatConstantKind kind0 = getFloatConstantKind(constants[0]);
  1839. FloatConstantKind kind1 = getFloatConstantKind(constants[1]);
  1840. if (kind0 == FloatConstantKind::Zero || kind1 == FloatConstantKind::Zero) {
  1841. inst->SetOpcode(SpvOpCopyObject);
  1842. inst->SetInOperands({{SPV_OPERAND_TYPE_ID,
  1843. {inst->GetSingleWordInOperand(
  1844. kind0 == FloatConstantKind::Zero ? 0 : 1)}}});
  1845. return true;
  1846. }
  1847. if (kind0 == FloatConstantKind::One || kind1 == FloatConstantKind::One) {
  1848. inst->SetOpcode(SpvOpCopyObject);
  1849. inst->SetInOperands({{SPV_OPERAND_TYPE_ID,
  1850. {inst->GetSingleWordInOperand(
  1851. kind0 == FloatConstantKind::One ? 1 : 0)}}});
  1852. return true;
  1853. }
  1854. return false;
  1855. };
  1856. }
  1857. FoldingRule RedundantFDiv() {
  1858. return [](IRContext*, Instruction* inst,
  1859. const std::vector<const analysis::Constant*>& constants) {
  1860. assert(inst->opcode() == SpvOpFDiv && "Wrong opcode. Should be OpFDiv.");
  1861. assert(constants.size() == 2);
  1862. if (!inst->IsFloatingPointFoldingAllowed()) {
  1863. return false;
  1864. }
  1865. FloatConstantKind kind0 = getFloatConstantKind(constants[0]);
  1866. FloatConstantKind kind1 = getFloatConstantKind(constants[1]);
  1867. if (kind0 == FloatConstantKind::Zero) {
  1868. inst->SetOpcode(SpvOpCopyObject);
  1869. inst->SetInOperands(
  1870. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(0)}}});
  1871. return true;
  1872. }
  1873. if (kind1 == FloatConstantKind::One) {
  1874. inst->SetOpcode(SpvOpCopyObject);
  1875. inst->SetInOperands(
  1876. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(0)}}});
  1877. return true;
  1878. }
  1879. return false;
  1880. };
  1881. }
  1882. FoldingRule RedundantFMix() {
  1883. return [](IRContext* context, Instruction* inst,
  1884. const std::vector<const analysis::Constant*>& constants) {
  1885. assert(inst->opcode() == SpvOpExtInst &&
  1886. "Wrong opcode. Should be OpExtInst.");
  1887. if (!inst->IsFloatingPointFoldingAllowed()) {
  1888. return false;
  1889. }
  1890. uint32_t instSetId =
  1891. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450();
  1892. if (inst->GetSingleWordInOperand(kExtInstSetIdInIdx) == instSetId &&
  1893. inst->GetSingleWordInOperand(kExtInstInstructionInIdx) ==
  1894. GLSLstd450FMix) {
  1895. assert(constants.size() == 5);
  1896. FloatConstantKind kind4 = getFloatConstantKind(constants[4]);
  1897. if (kind4 == FloatConstantKind::Zero || kind4 == FloatConstantKind::One) {
  1898. inst->SetOpcode(SpvOpCopyObject);
  1899. inst->SetInOperands(
  1900. {{SPV_OPERAND_TYPE_ID,
  1901. {inst->GetSingleWordInOperand(kind4 == FloatConstantKind::Zero
  1902. ? kFMixXIdInIdx
  1903. : kFMixYIdInIdx)}}});
  1904. return true;
  1905. }
  1906. }
  1907. return false;
  1908. };
  1909. }
  1910. // This rule handles addition of zero for integers.
  1911. FoldingRule RedundantIAdd() {
  1912. return [](IRContext* context, Instruction* inst,
  1913. const std::vector<const analysis::Constant*>& constants) {
  1914. assert(inst->opcode() == SpvOpIAdd && "Wrong opcode. Should be OpIAdd.");
  1915. uint32_t operand = std::numeric_limits<uint32_t>::max();
  1916. const analysis::Type* operand_type = nullptr;
  1917. if (constants[0] && constants[0]->IsZero()) {
  1918. operand = inst->GetSingleWordInOperand(1);
  1919. operand_type = constants[0]->type();
  1920. } else if (constants[1] && constants[1]->IsZero()) {
  1921. operand = inst->GetSingleWordInOperand(0);
  1922. operand_type = constants[1]->type();
  1923. }
  1924. if (operand != std::numeric_limits<uint32_t>::max()) {
  1925. const analysis::Type* inst_type =
  1926. context->get_type_mgr()->GetType(inst->type_id());
  1927. if (inst_type->IsSame(operand_type)) {
  1928. inst->SetOpcode(SpvOpCopyObject);
  1929. } else {
  1930. inst->SetOpcode(SpvOpBitcast);
  1931. }
  1932. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {operand}}});
  1933. return true;
  1934. }
  1935. return false;
  1936. };
  1937. }
  1938. // This rule look for a dot with a constant vector containing a single 1 and
  1939. // the rest 0s. This is the same as doing an extract.
  1940. FoldingRule DotProductDoingExtract() {
  1941. return [](IRContext* context, Instruction* inst,
  1942. const std::vector<const analysis::Constant*>& constants) {
  1943. assert(inst->opcode() == SpvOpDot && "Wrong opcode. Should be OpDot.");
  1944. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1945. if (!inst->IsFloatingPointFoldingAllowed()) {
  1946. return false;
  1947. }
  1948. for (int i = 0; i < 2; ++i) {
  1949. if (!constants[i]) {
  1950. continue;
  1951. }
  1952. const analysis::Vector* vector_type = constants[i]->type()->AsVector();
  1953. assert(vector_type && "Inputs to OpDot must be vectors.");
  1954. const analysis::Float* element_type =
  1955. vector_type->element_type()->AsFloat();
  1956. assert(element_type && "Inputs to OpDot must be vectors of floats.");
  1957. uint32_t element_width = element_type->width();
  1958. if (element_width != 32 && element_width != 64) {
  1959. return false;
  1960. }
  1961. std::vector<const analysis::Constant*> components;
  1962. components = constants[i]->GetVectorComponents(const_mgr);
  1963. const uint32_t kNotFound = std::numeric_limits<uint32_t>::max();
  1964. uint32_t component_with_one = kNotFound;
  1965. bool all_others_zero = true;
  1966. for (uint32_t j = 0; j < components.size(); ++j) {
  1967. const analysis::Constant* element = components[j];
  1968. double value =
  1969. (element_width == 32 ? element->GetFloat() : element->GetDouble());
  1970. if (value == 0.0) {
  1971. continue;
  1972. } else if (value == 1.0) {
  1973. if (component_with_one == kNotFound) {
  1974. component_with_one = j;
  1975. } else {
  1976. component_with_one = kNotFound;
  1977. break;
  1978. }
  1979. } else {
  1980. all_others_zero = false;
  1981. break;
  1982. }
  1983. }
  1984. if (!all_others_zero || component_with_one == kNotFound) {
  1985. continue;
  1986. }
  1987. std::vector<Operand> operands;
  1988. operands.push_back(
  1989. {SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(1u - i)}});
  1990. operands.push_back(
  1991. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {component_with_one}});
  1992. inst->SetOpcode(SpvOpCompositeExtract);
  1993. inst->SetInOperands(std::move(operands));
  1994. return true;
  1995. }
  1996. return false;
  1997. };
  1998. }
  1999. // If we are storing an undef, then we can remove the store.
  2000. //
  2001. // TODO: We can do something similar for OpImageWrite, but checking for volatile
  2002. // is complicated. Waiting to see if it is needed.
  2003. FoldingRule StoringUndef() {
  2004. return [](IRContext* context, Instruction* inst,
  2005. const std::vector<const analysis::Constant*>&) {
  2006. assert(inst->opcode() == SpvOpStore && "Wrong opcode. Should be OpStore.");
  2007. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  2008. // If this is a volatile store, the store cannot be removed.
  2009. if (inst->NumInOperands() == 3) {
  2010. if (inst->GetSingleWordInOperand(2) & SpvMemoryAccessVolatileMask) {
  2011. return false;
  2012. }
  2013. }
  2014. uint32_t object_id = inst->GetSingleWordInOperand(kStoreObjectInIdx);
  2015. Instruction* object_inst = def_use_mgr->GetDef(object_id);
  2016. if (object_inst->opcode() == SpvOpUndef) {
  2017. inst->ToNop();
  2018. return true;
  2019. }
  2020. return false;
  2021. };
  2022. }
  2023. FoldingRule VectorShuffleFeedingShuffle() {
  2024. return [](IRContext* context, Instruction* inst,
  2025. const std::vector<const analysis::Constant*>&) {
  2026. assert(inst->opcode() == SpvOpVectorShuffle &&
  2027. "Wrong opcode. Should be OpVectorShuffle.");
  2028. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  2029. analysis::TypeManager* type_mgr = context->get_type_mgr();
  2030. Instruction* feeding_shuffle_inst =
  2031. def_use_mgr->GetDef(inst->GetSingleWordInOperand(0));
  2032. analysis::Vector* op0_type =
  2033. type_mgr->GetType(feeding_shuffle_inst->type_id())->AsVector();
  2034. uint32_t op0_length = op0_type->element_count();
  2035. bool feeder_is_op0 = true;
  2036. if (feeding_shuffle_inst->opcode() != SpvOpVectorShuffle) {
  2037. feeding_shuffle_inst =
  2038. def_use_mgr->GetDef(inst->GetSingleWordInOperand(1));
  2039. feeder_is_op0 = false;
  2040. }
  2041. if (feeding_shuffle_inst->opcode() != SpvOpVectorShuffle) {
  2042. return false;
  2043. }
  2044. Instruction* feeder2 =
  2045. def_use_mgr->GetDef(feeding_shuffle_inst->GetSingleWordInOperand(0));
  2046. analysis::Vector* feeder_op0_type =
  2047. type_mgr->GetType(feeder2->type_id())->AsVector();
  2048. uint32_t feeder_op0_length = feeder_op0_type->element_count();
  2049. uint32_t new_feeder_id = 0;
  2050. std::vector<Operand> new_operands;
  2051. new_operands.resize(
  2052. 2, {SPV_OPERAND_TYPE_ID, {0}}); // Place holders for vector operands.
  2053. const uint32_t undef_literal = 0xffffffff;
  2054. for (uint32_t op = 2; op < inst->NumInOperands(); ++op) {
  2055. uint32_t component_index = inst->GetSingleWordInOperand(op);
  2056. // Do not interpret the undefined value literal as coming from operand 1.
  2057. if (component_index != undef_literal &&
  2058. feeder_is_op0 == (component_index < op0_length)) {
  2059. // This component comes from the feeding_shuffle_inst. Update
  2060. // |component_index| to be the index into the operand of the feeder.
  2061. // Adjust component_index to get the index into the operands of the
  2062. // feeding_shuffle_inst.
  2063. if (component_index >= op0_length) {
  2064. component_index -= op0_length;
  2065. }
  2066. component_index =
  2067. feeding_shuffle_inst->GetSingleWordInOperand(component_index + 2);
  2068. // Check if we are using a component from the first or second operand of
  2069. // the feeding instruction.
  2070. if (component_index < feeder_op0_length) {
  2071. if (new_feeder_id == 0) {
  2072. // First time through, save the id of the operand the element comes
  2073. // from.
  2074. new_feeder_id = feeding_shuffle_inst->GetSingleWordInOperand(0);
  2075. } else if (new_feeder_id !=
  2076. feeding_shuffle_inst->GetSingleWordInOperand(0)) {
  2077. // We need both elements of the feeding_shuffle_inst, so we cannot
  2078. // fold.
  2079. return false;
  2080. }
  2081. } else {
  2082. if (new_feeder_id == 0) {
  2083. // First time through, save the id of the operand the element comes
  2084. // from.
  2085. new_feeder_id = feeding_shuffle_inst->GetSingleWordInOperand(1);
  2086. } else if (new_feeder_id !=
  2087. feeding_shuffle_inst->GetSingleWordInOperand(1)) {
  2088. // We need both elements of the feeding_shuffle_inst, so we cannot
  2089. // fold.
  2090. return false;
  2091. }
  2092. component_index -= feeder_op0_length;
  2093. }
  2094. if (!feeder_is_op0) {
  2095. component_index += op0_length;
  2096. }
  2097. }
  2098. new_operands.push_back(
  2099. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {component_index}});
  2100. }
  2101. if (new_feeder_id == 0) {
  2102. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  2103. const analysis::Type* type =
  2104. type_mgr->GetType(feeding_shuffle_inst->type_id());
  2105. const analysis::Constant* null_const = const_mgr->GetConstant(type, {});
  2106. new_feeder_id =
  2107. const_mgr->GetDefiningInstruction(null_const, 0)->result_id();
  2108. }
  2109. if (feeder_is_op0) {
  2110. // If the size of the first vector operand changed then the indices
  2111. // referring to the second operand need to be adjusted.
  2112. Instruction* new_feeder_inst = def_use_mgr->GetDef(new_feeder_id);
  2113. analysis::Type* new_feeder_type =
  2114. type_mgr->GetType(new_feeder_inst->type_id());
  2115. uint32_t new_op0_size = new_feeder_type->AsVector()->element_count();
  2116. int32_t adjustment = op0_length - new_op0_size;
  2117. if (adjustment != 0) {
  2118. for (uint32_t i = 2; i < new_operands.size(); i++) {
  2119. if (inst->GetSingleWordInOperand(i) >= op0_length) {
  2120. new_operands[i].words[0] -= adjustment;
  2121. }
  2122. }
  2123. }
  2124. new_operands[0].words[0] = new_feeder_id;
  2125. new_operands[1] = inst->GetInOperand(1);
  2126. } else {
  2127. new_operands[1].words[0] = new_feeder_id;
  2128. new_operands[0] = inst->GetInOperand(0);
  2129. }
  2130. inst->SetInOperands(std::move(new_operands));
  2131. return true;
  2132. };
  2133. }
  2134. // Removes duplicate ids from the interface list of an OpEntryPoint
  2135. // instruction.
  2136. FoldingRule RemoveRedundantOperands() {
  2137. return [](IRContext*, Instruction* inst,
  2138. const std::vector<const analysis::Constant*>&) {
  2139. assert(inst->opcode() == SpvOpEntryPoint &&
  2140. "Wrong opcode. Should be OpEntryPoint.");
  2141. bool has_redundant_operand = false;
  2142. std::unordered_set<uint32_t> seen_operands;
  2143. std::vector<Operand> new_operands;
  2144. new_operands.emplace_back(inst->GetOperand(0));
  2145. new_operands.emplace_back(inst->GetOperand(1));
  2146. new_operands.emplace_back(inst->GetOperand(2));
  2147. for (uint32_t i = 3; i < inst->NumOperands(); ++i) {
  2148. if (seen_operands.insert(inst->GetSingleWordOperand(i)).second) {
  2149. new_operands.emplace_back(inst->GetOperand(i));
  2150. } else {
  2151. has_redundant_operand = true;
  2152. }
  2153. }
  2154. if (!has_redundant_operand) {
  2155. return false;
  2156. }
  2157. inst->SetInOperands(std::move(new_operands));
  2158. return true;
  2159. };
  2160. }
  2161. // If an image instruction's operand is a constant, updates the image operand
  2162. // flag from Offset to ConstOffset.
  2163. FoldingRule UpdateImageOperands() {
  2164. return [](IRContext*, Instruction* inst,
  2165. const std::vector<const analysis::Constant*>& constants) {
  2166. const auto opcode = inst->opcode();
  2167. (void)opcode;
  2168. assert((opcode == SpvOpImageSampleImplicitLod ||
  2169. opcode == SpvOpImageSampleExplicitLod ||
  2170. opcode == SpvOpImageSampleDrefImplicitLod ||
  2171. opcode == SpvOpImageSampleDrefExplicitLod ||
  2172. opcode == SpvOpImageSampleProjImplicitLod ||
  2173. opcode == SpvOpImageSampleProjExplicitLod ||
  2174. opcode == SpvOpImageSampleProjDrefImplicitLod ||
  2175. opcode == SpvOpImageSampleProjDrefExplicitLod ||
  2176. opcode == SpvOpImageFetch || opcode == SpvOpImageGather ||
  2177. opcode == SpvOpImageDrefGather || opcode == SpvOpImageRead ||
  2178. opcode == SpvOpImageWrite ||
  2179. opcode == SpvOpImageSparseSampleImplicitLod ||
  2180. opcode == SpvOpImageSparseSampleExplicitLod ||
  2181. opcode == SpvOpImageSparseSampleDrefImplicitLod ||
  2182. opcode == SpvOpImageSparseSampleDrefExplicitLod ||
  2183. opcode == SpvOpImageSparseSampleProjImplicitLod ||
  2184. opcode == SpvOpImageSparseSampleProjExplicitLod ||
  2185. opcode == SpvOpImageSparseSampleProjDrefImplicitLod ||
  2186. opcode == SpvOpImageSparseSampleProjDrefExplicitLod ||
  2187. opcode == SpvOpImageSparseFetch ||
  2188. opcode == SpvOpImageSparseGather ||
  2189. opcode == SpvOpImageSparseDrefGather ||
  2190. opcode == SpvOpImageSparseRead) &&
  2191. "Wrong opcode. Should be an image instruction.");
  2192. int32_t operand_index = ImageOperandsMaskInOperandIndex(inst);
  2193. if (operand_index >= 0) {
  2194. auto image_operands = inst->GetSingleWordInOperand(operand_index);
  2195. if (image_operands & SpvImageOperandsOffsetMask) {
  2196. uint32_t offset_operand_index = operand_index + 1;
  2197. if (image_operands & SpvImageOperandsBiasMask) offset_operand_index++;
  2198. if (image_operands & SpvImageOperandsLodMask) offset_operand_index++;
  2199. if (image_operands & SpvImageOperandsGradMask)
  2200. offset_operand_index += 2;
  2201. assert(((image_operands & SpvImageOperandsConstOffsetMask) == 0) &&
  2202. "Offset and ConstOffset may not be used together");
  2203. if (offset_operand_index < inst->NumOperands()) {
  2204. if (constants[offset_operand_index]) {
  2205. image_operands = image_operands | SpvImageOperandsConstOffsetMask;
  2206. image_operands = image_operands & ~SpvImageOperandsOffsetMask;
  2207. inst->SetInOperand(operand_index, {image_operands});
  2208. return true;
  2209. }
  2210. }
  2211. }
  2212. }
  2213. return false;
  2214. };
  2215. }
  2216. } // namespace
  2217. void FoldingRules::AddFoldingRules() {
  2218. // Add all folding rules to the list for the opcodes to which they apply.
  2219. // Note that the order in which rules are added to the list matters. If a rule
  2220. // applies to the instruction, the rest of the rules will not be attempted.
  2221. // Take that into consideration.
  2222. rules_[SpvOpBitcast].push_back(BitCastScalarOrVector());
  2223. rules_[SpvOpCompositeConstruct].push_back(CompositeExtractFeedingConstruct);
  2224. rules_[SpvOpCompositeExtract].push_back(InsertFeedingExtract());
  2225. rules_[SpvOpCompositeExtract].push_back(CompositeConstructFeedingExtract);
  2226. rules_[SpvOpCompositeExtract].push_back(VectorShuffleFeedingExtract());
  2227. rules_[SpvOpCompositeExtract].push_back(FMixFeedingExtract());
  2228. rules_[SpvOpDot].push_back(DotProductDoingExtract());
  2229. rules_[SpvOpEntryPoint].push_back(RemoveRedundantOperands());
  2230. rules_[SpvOpFAdd].push_back(RedundantFAdd());
  2231. rules_[SpvOpFAdd].push_back(MergeAddNegateArithmetic());
  2232. rules_[SpvOpFAdd].push_back(MergeAddAddArithmetic());
  2233. rules_[SpvOpFAdd].push_back(MergeAddSubArithmetic());
  2234. rules_[SpvOpFAdd].push_back(MergeGenericAddSubArithmetic());
  2235. rules_[SpvOpFAdd].push_back(FactorAddMuls());
  2236. rules_[SpvOpFDiv].push_back(RedundantFDiv());
  2237. rules_[SpvOpFDiv].push_back(ReciprocalFDiv());
  2238. rules_[SpvOpFDiv].push_back(MergeDivDivArithmetic());
  2239. rules_[SpvOpFDiv].push_back(MergeDivMulArithmetic());
  2240. rules_[SpvOpFDiv].push_back(MergeDivNegateArithmetic());
  2241. rules_[SpvOpFMul].push_back(RedundantFMul());
  2242. rules_[SpvOpFMul].push_back(MergeMulMulArithmetic());
  2243. rules_[SpvOpFMul].push_back(MergeMulDivArithmetic());
  2244. rules_[SpvOpFMul].push_back(MergeMulNegateArithmetic());
  2245. rules_[SpvOpFNegate].push_back(MergeNegateArithmetic());
  2246. rules_[SpvOpFNegate].push_back(MergeNegateAddSubArithmetic());
  2247. rules_[SpvOpFNegate].push_back(MergeNegateMulDivArithmetic());
  2248. rules_[SpvOpFSub].push_back(RedundantFSub());
  2249. rules_[SpvOpFSub].push_back(MergeSubNegateArithmetic());
  2250. rules_[SpvOpFSub].push_back(MergeSubAddArithmetic());
  2251. rules_[SpvOpFSub].push_back(MergeSubSubArithmetic());
  2252. rules_[SpvOpIAdd].push_back(RedundantIAdd());
  2253. rules_[SpvOpIAdd].push_back(MergeAddNegateArithmetic());
  2254. rules_[SpvOpIAdd].push_back(MergeAddAddArithmetic());
  2255. rules_[SpvOpIAdd].push_back(MergeAddSubArithmetic());
  2256. rules_[SpvOpIAdd].push_back(MergeGenericAddSubArithmetic());
  2257. rules_[SpvOpIAdd].push_back(FactorAddMuls());
  2258. rules_[SpvOpIMul].push_back(IntMultipleBy1());
  2259. rules_[SpvOpIMul].push_back(MergeMulMulArithmetic());
  2260. rules_[SpvOpIMul].push_back(MergeMulNegateArithmetic());
  2261. rules_[SpvOpISub].push_back(MergeSubNegateArithmetic());
  2262. rules_[SpvOpISub].push_back(MergeSubAddArithmetic());
  2263. rules_[SpvOpISub].push_back(MergeSubSubArithmetic());
  2264. rules_[SpvOpPhi].push_back(RedundantPhi());
  2265. rules_[SpvOpSNegate].push_back(MergeNegateArithmetic());
  2266. rules_[SpvOpSNegate].push_back(MergeNegateMulDivArithmetic());
  2267. rules_[SpvOpSNegate].push_back(MergeNegateAddSubArithmetic());
  2268. rules_[SpvOpSelect].push_back(RedundantSelect());
  2269. rules_[SpvOpStore].push_back(StoringUndef());
  2270. rules_[SpvOpVectorShuffle].push_back(VectorShuffleFeedingShuffle());
  2271. rules_[SpvOpImageSampleImplicitLod].push_back(UpdateImageOperands());
  2272. rules_[SpvOpImageSampleExplicitLod].push_back(UpdateImageOperands());
  2273. rules_[SpvOpImageSampleDrefImplicitLod].push_back(UpdateImageOperands());
  2274. rules_[SpvOpImageSampleDrefExplicitLod].push_back(UpdateImageOperands());
  2275. rules_[SpvOpImageSampleProjImplicitLod].push_back(UpdateImageOperands());
  2276. rules_[SpvOpImageSampleProjExplicitLod].push_back(UpdateImageOperands());
  2277. rules_[SpvOpImageSampleProjDrefImplicitLod].push_back(UpdateImageOperands());
  2278. rules_[SpvOpImageSampleProjDrefExplicitLod].push_back(UpdateImageOperands());
  2279. rules_[SpvOpImageFetch].push_back(UpdateImageOperands());
  2280. rules_[SpvOpImageGather].push_back(UpdateImageOperands());
  2281. rules_[SpvOpImageDrefGather].push_back(UpdateImageOperands());
  2282. rules_[SpvOpImageRead].push_back(UpdateImageOperands());
  2283. rules_[SpvOpImageWrite].push_back(UpdateImageOperands());
  2284. rules_[SpvOpImageSparseSampleImplicitLod].push_back(UpdateImageOperands());
  2285. rules_[SpvOpImageSparseSampleExplicitLod].push_back(UpdateImageOperands());
  2286. rules_[SpvOpImageSparseSampleDrefImplicitLod].push_back(
  2287. UpdateImageOperands());
  2288. rules_[SpvOpImageSparseSampleDrefExplicitLod].push_back(
  2289. UpdateImageOperands());
  2290. rules_[SpvOpImageSparseSampleProjImplicitLod].push_back(
  2291. UpdateImageOperands());
  2292. rules_[SpvOpImageSparseSampleProjExplicitLod].push_back(
  2293. UpdateImageOperands());
  2294. rules_[SpvOpImageSparseSampleProjDrefImplicitLod].push_back(
  2295. UpdateImageOperands());
  2296. rules_[SpvOpImageSparseSampleProjDrefExplicitLod].push_back(
  2297. UpdateImageOperands());
  2298. rules_[SpvOpImageSparseFetch].push_back(UpdateImageOperands());
  2299. rules_[SpvOpImageSparseGather].push_back(UpdateImageOperands());
  2300. rules_[SpvOpImageSparseDrefGather].push_back(UpdateImageOperands());
  2301. rules_[SpvOpImageSparseRead].push_back(UpdateImageOperands());
  2302. FeatureManager* feature_manager = context_->get_feature_mgr();
  2303. // Add rules for GLSLstd450
  2304. uint32_t ext_inst_glslstd450_id =
  2305. feature_manager->GetExtInstImportId_GLSLstd450();
  2306. if (ext_inst_glslstd450_id != 0) {
  2307. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMix}].push_back(
  2308. RedundantFMix());
  2309. }
  2310. }
  2311. } // namespace opt
  2312. } // namespace spvtools