{"id":253,"date":"2020-04-24T01:57:31","date_gmt":"2020-04-23T17:57:31","guid":{"rendered":"https:\/\/syaro.hotococoa.moe\/wordpress\/?p=253"},"modified":"2020-09-26T13:32:26","modified_gmt":"2020-09-26T05:32:26","slug":"llvm%e5%be%aa%e7%8e%af%e6%9c%af%e8%af%ad%ef%bc%88%e5%92%8c%e8%a7%84%e8%8c%83%e5%bd%a2%e5%bc%8f%ef%bc%89","status":"publish","type":"post","link":"https:\/\/syaro.hotococoa.moe\/wordpress\/?p=253","title":{"rendered":"LLVM\u5faa\u73af\u672f\u8bed\uff08\u548c\u89c4\u8303\u5f62\u5f0f\uff09"},"content":{"rendered":"\n<p>\u539f\u6587\u94fe\u63a5\uff1a<a rel=\"noreferrer noopener\" href=\"https:\/\/llvm.org\/docs\/LoopTerminology.html\" target=\"_blank\">LLVM Loop Terminology (and Canonical Forms) \u2014 LLVM 10 documentation<\/a>\uff0c\u672c\u6587\u7ffb\u8bd1\u65f6\u7248\u672c\uff1a<a href=\"https:\/\/archive.is\/W68H8\">https:\/\/archive.is\/W68H8<\/a>\uff0c\u57fa\u4e8e<a href=\"https:\/\/creativecommons.org\/licenses\/by-nc-sa\/4.0\/deed.zh-Hans\">CC BY-NC-SA 4.0<\/a>\u534f\u8bae\u53d1\u5e03<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ecb\u7ecd<\/h2>\n\n\n\n<p>\u5faa\u73af\u5728\u4efb\u4f55\u4f18\u5316\u5668\u4e2d\u90fd\u662f\u4e00\u4e2a\u6838\u5fc3\u6982\u5ff5\u3002\u672c\u9875\u9762\u9610\u660e\u4e86LLVM\u4ee3\u7801\u4e2d\u7528\u4e8e\u63cf\u8ff0\u5faa\u73af\u7ed3\u6784\u7684\u4e00\u4e9b\u5e38\u7528\u672f\u8bed\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u4ece\u57fa\u7840\u5f00\u59cb\u3002\u5728LLVM\u4e2d\uff0c\u5faa\u73af\u662f\u5728<a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Control-flow_graph\" target=\"_blank\">\u63a7\u5236\u6d41\u56fe<\/a>\uff08CFG\uff09\u4e2d\u5f62\u6210<a rel=\"noreferrer noopener\" href=\"https:\/\/zh.wikipedia.org\/zh-cn\/%E5%BC%BA%E8%BF%9E%E9%80%9A%E5%88%86%E9%87%8F\" target=\"_blank\">\u5f3a\u8fde\u63a5\u5206\u91cf<\/a>\uff08SCC\uff09\u7684\u6700\u5927\u57fa\u672c\u5757\u96c6\uff0c\u5176\u4e2d\u5b58\u5728\u4e13\u7528\u7684\u5165\u53e3\/\u5934\u5757\uff0c\u8be5\u5165\u53e3\/\u5934\u5757\u63a7\u5236\u7740\u5faa\u73af\u4e2d\u7684\u6240\u6709\u5176\u4ed6\u5757\u3002\u56e0\u6b64\uff0c\u65e0\u9700\u79bb\u5f00\u5faa\u73af\uff0c\u5c31\u53ef\u4ee5\u4ece\u5934\u5757\u5230\u8fbe\u5faa\u73af\u4e2d\u7684\u6bcf\u4e2a\u5757\uff0c\u5e76\u4ece\u5faa\u73af\u4e2d\u7684\u6bcf\u4e2a\u5757\u5230\u8fbe\u5934\u5757\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\u672c\u5b9a\u4e49\u6709\u4e00\u4e9b\u91cd\u8981\u7684\u9690\u542b\u4fe1\u606f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5e76\u975e\u6240\u6709\u5f3a\u8fde\u63a5\u5206\u91cf\u90fd\u662f\u5faa\u73af\u3002\u5b58\u5728\u4e0d\u7b26\u5408\u63a7\u5236\u8981\u6c42\u7684\u5f3a\u8fde\u63a5\u5206\u91cf\uff0c\u56e0\u6b64\u4e0d\u5c06\u5176\u89c6\u4e3a\u5faa\u73af\u3002<\/li><li>\u5faa\u73af\u53ef\u4ee5\u5305\u542b\u975e\u5faa\u73af\u5f3a\u8fde\u63a5\u5206\u91cf\uff0c\u975e\u5faa\u73af\u5f3a\u8fde\u63a5\u5206\u91cf\u53ef\u4ee5\u5305\u542b\u5faa\u73af\u3002\u5faa\u73af\u4e5f\u53ef\u4ee5\u5305\u542b\u5b50\u5faa\u73af\u3002<\/li><li>\u5934\u5757\u4e0e\u4e00\u4e2a\u5faa\u73af\u552f\u4e00\u5173\u8054\u3002\u8be5\u5faa\u73af\u4e2d\u53ef\u4ee5\u6709\u591a\u4e2a\u5f3a\u8fde\u63a5\u5206\u91cf\uff0c\u4f46\u662f\u7531\u5b83\u4eec\u7684\u5e76\u96c6\u5f62\u6210\u7684\u5f3a\u8fde\u63a5\u5206\u91cf\u5fc5\u987b\u59cb\u7ec8\u662f\u552f\u4e00\u7684\u3002<\/li><li>\u5bf9\u4e8e\u672c\u5b9a\u4e49\u4e2d\u7684\u201c\u63a7\u5236\u201d\u7684\u4f7f\u7528\uff0c\u6240\u6709\u7684\u5faa\u73af\u90fd\u662f\u81ea\u51fd\u6570\u7684\u5165\u53e3\u9759\u6001\u53ef\u8fbe\u7684\u3002\uff08\u539f\u6587\uff1aGiven the use of dominance in the definition, all loops are statically reachable from the entry of the function.\u8bf7\u6c42\u66f4\u597d\u7ffb\u8bd1\uff09<\/li><li>\u6bcf\u4e2a\u5faa\u73af\u90fd\u5fc5\u987b\u6709\u4e00\u4e2a\u5934\u5757\uff0c\u4ee5\u53ca\u5faa\u73af\u5916\u7684\u4e00\u4e9b\u524d\u9a71\u3002\u5faa\u73af\u5141\u8bb8\u662f\u9759\u6001\u5730\u65e0\u9650\u5faa\u73af\u7684\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u4efb\u4f55\u9000\u51fa\u8fb9\u7f18\u3002<\/li><li>\u4efb\u4f55\u4e24\u4e2a\u5faa\u73af\u8981\u4e48\u5b8c\u5168\u4e0d\u76f8\u4ea4\uff08\u6ca1\u6709\u76f8\u4ea4\u7684\u5757\uff09\uff0c\u8981\u4e48\u4e00\u4e2a\u5faa\u73af\u5fc5\u987b\u662f\u53e6\u4e00\u4e2a\u5faa\u73af\u7684\u5b50\u5faa\u73af\u3002<\/li><li>\u51fd\u6570\u4e2d\u7684\u5faa\u73af\u5f62\u6210\u68ee\u6797\u3002\u8fd9\u4e00\u4e8b\u5b9e\u7684\u4e00\u4e2a\u6697\u793a\u662f\uff0c\u5faa\u73af\u8981\u4e48\u6ca1\u6709\u4eb2\u4ee3\uff0c\u8981\u4e48\u4ec5\u6709\u4e00\u4e2a\u4eb2\u4ee3\u3002<\/li><\/ul>\n\n\n\n<p>\u5faa\u73af\u53ef\u4ee5\u6709\u4efb\u610f\u6570\u91cf\u7684\u9000\u51fa\uff0c\u65e0\u8bba\u662f\u663e\u5f0f\u9000\u51fa\uff08\u901a\u8fc7\u63a7\u5236\u6d41\uff09\u8fd8\u662f\u9690\u5f0f\u9000\u51fa\uff08\u901a\u8fc7\u5f15\u53d1\u5c06\u63a7\u5236\u4ece\u5305\u542b\u7684\u51fd\u6570\u4e2d\u79fb\u51fa\u7684\u8c03\u7528\uff09\u3002\u5bf9\u51fa\u53e3\u5757\uff08\u5206\u652f\u4e4b\u5916\u7684\u5faa\u73af\u5757\uff09\u7684\u5f62\u5f0f\u6216\u7ed3\u6784\u6ca1\u6709\u7279\u6b8a\u8981\u6c42\u3002\u4ed6\u4eec\u53ef\u80fd\u6709\u591a\u4e2a\u524d\u9a71\uff0cphi\u7b49\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5173\u952e\u672f\u8bed<\/h2>\n\n\n\n<p><strong>\u5934\u5757\uff08Header Block\uff09<\/strong>&nbsp;&#8211; \u63a7\u5236\u5faa\u73af\u4e2d\u5305\u542b\u7684\u6240\u6709\u5176\u4ed6\u5757\u7684\u57fa\u672c\u5757\u3002\u56e0\u6b64\uff0c\u5982\u679c\u5faa\u73af\u6267\u884c\u4e86\uff0c\u5b83\u5c06\u662f\u7b2c\u4e00\u4e2a\u88ab\u6267\u884c\u7684\u3002\u6ce8\u610f\u4e00\u4e2a\u5757\u53ef\u4ee5\u540c\u65f6\u662f\u4e24\u4e2a\u5355\u72ec\u7684\u5faa\u73af\u7684\u5934\u5757\uff0c\u4f46\u524d\u63d0\u662f\u4e00\u4e2a\u662f\u53e6\u4e00\u4e2a\u7684\u5b50\u5faa\u73af\u3002<\/p>\n\n\n\n<p><strong>\u9000\u51fa\u5757\uff08Exiting Block\uff09<\/strong>&nbsp;&#8211; \u7ed9\u5b9a\u5faa\u73af\u4e2d\u5305\u542b\u7684\u4e00\u79cd\u57fa\u672c\u5757\uff0c\u8be5\u57fa\u672c\u5757\u5728\u5faa\u73af\u5916\u81f3\u5c11\u6709\u4e00\u4e2a\u540e\u7ee7\uff0c\u4ee5\u53ca\u5728\u5faa\u73af\u5185\u81f3\u5c11\u6709\u4e00\u4e2a\u540e\u7ee7\u3002\uff08\u540e\u8005\u662f\u7531\u4e8e\u8be5\u5757\u5305\u542b\u5728\u4f5c\u4e3a\u5faa\u73af\u4e00\u90e8\u5206\u7684\u5f3a\u8fde\u63a5\u5206\u91cf\u5185\u7684\u7ed3\u679c\u3002\uff09\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u5177\u6709\u4e00\u4e2a\u540e\u7ee7\uff0c\u5373\u51fa\u53e3\u5757\u3002<\/p>\n\n\n\n<p><strong>\u51fa\u53e3\u5757\uff08Exit Block\uff09<\/strong>&nbsp;&#8211; \u76f8\u5173\u5faa\u73af\u5916\u90e8\u7684\u4e00\u79cd\u57fa\u672c\u5757\uff0c\u5728\u5faa\u73af\u5185\u90e8\u5177\u6709\u524d\u9a71\u3002\u5373\u5b83\u7684\u524d\u9a71\u662f\u9000\u51fa\u5757\u3002<\/p>\n\n\n\n<p><strong>\u95e9\u9501\u5757\uff08Latch Block\uff09<\/strong>&nbsp;&#8211; \u5faa\u73af\u4e2d\u7684\u4e00\u79cd\u57fa\u672c\u5757\uff0c\u5176\u540e\u7ee7\u5305\u62ec\u5faa\u73af\u7684\u5934\u5757\u3002\u56e0\u6b64\uff0c\u95e9\u9501\u662f\u8fd4\u56de\u8fb9\u7684\u6765\u6e90\u3002\u5faa\u73af\u53ef\u4ee5\u5177\u6709\u591a\u4e2a\u95e9\u9501\u5757\u3002\u95e9\u9501\u5757\u53ef\u4ee5\u662f\u6761\u4ef6\u7684\uff0c\u4e5f\u53ef\u4ee5\u662f\u65e0\u6761\u4ef6\u7684\u3002<\/p>\n\n\n\n<p><strong>\u8fd4\u56de\u8fb9\uff08Backedge(s)\uff09<\/strong>&nbsp;&#8211; \u63a7\u5236\u6d41\u56fe\u4e2d\u4ece\u95e9\u9501\u5757\u5230\u5934\u5757\u7684\u8fb9\u3002\u6ce8\u610f\uff0c\u53ef\u4ee5\u6709\u591a\u4e2a\u8fd9\u6837\u7684\u8fb9\uff0c\u751a\u81f3\u53ef\u4ee5\u6709\u591a\u4e2a\u8fd9\u6837\u7684\u4ece\u95e9\u9501\u5757\u79bb\u5f00\u7684\u8fb9\u3002<\/p>\n\n\n\n<p><strong>\u5faa\u73af\u524d\u9a71\uff08Loop Predecessor\uff09<\/strong>&nbsp;&#8211; \u5faa\u73af\u5934\u7684\u524d\u9a71\u5757\u4e0d\u5305\u542b\u5728\u5faa\u73af\u672c\u8eab\u4e2d\u3002\u8fd9\u4e9b\u662f\u901a\u8fc7\u6267\u884c\u53ef\u4ee5\u8fdb\u5165\u5faa\u73af\u7684\u552f\u4e00\u5757\u3002\u5f53\u4ee5\u5355\u6570\u5f62\u5f0f\u4f7f\u7528\u65f6\uff0c\u610f\u5473\u7740\u53ea\u6709\u552f\u4e00\u4e00\u4e2a\u8fd9\u6837\u7684\u5757\u3002<\/p>\n\n\n\n<p><strong>Preheader\u5757\uff08Preheader Block\uff09<\/strong>\u00a0&#8211; Preheader\u662f\u4e00\u4e2a\uff08\u5355\u4e00\uff09\u5faa\u73af\u7684\u524d\u9a71\uff0c\u5176\u4ee5\u65e0\u6761\u4ef6\u5730\u5c06\u63a7\u5236\u8f6c\u79fb\u5230\u5faa\u73af\u5934\u7ed3\u675f\u3002\u8bf7\u6ce8\u610f\uff0c\u5e76\u975e\u6240\u6709\u5faa\u73af\u90fd\u5177\u6709\u6b64\u7c7b\u5757\u3002<\/p>\n\n\n\n<p><strong><strong>\u8fd4\u56de\u8fb9<\/strong>\u88ab\u4f7f\u7528\u6570\uff08Backedge Taken Count\uff09<\/strong>&nbsp;&#8211; \u8fd4\u56de\u8fb9\u5c06\u8981\u5728\u67d0\u4e9b\u5173\u6ce8\u7684\u4e8b\u4ef6\u53d1\u751f\u4e4b\u524d\u6267\u884c\u7684\u6b21\u6570\u3002\u901a\u5e38\u5728\u4e0d\u9650\u5b9a\u4e8b\u4ef6\u7684\u60c5\u51b5\u4e0b\u7528\u4f5c\u67d0\u4e9b\u9000\u51fa\u5757\u5206\u652f\u5230\u67d0\u4e2a\u9000\u51fa\u5757\u7684\u7b80\u5199\u3002\u5b83\u53ef\u80fd\u4e3a\u96f6\uff0c\u6216\u8005\u5e76\u975e\u9759\u6001\u53ef\u8ba1\u7b97\u7684\u3002<\/p>\n\n\n\n<p><strong>\u8fed\u4ee3\u6570\uff08Iteration Count\uff09<\/strong>&nbsp;&#8211; \u5faa\u73af\u5934\u5728\u53d1\u751f\u67d0\u4e9b\u5173\u6ce8\u7684\u4e8b\u4ef6\u4e4b\u524d\u6267\u884c\u7684\u6b21\u6570\u3002\u901a\u5e38\u4e0d\u52a0\u9650\u5b9a\u5730\u4f7f\u7528\uff0c\u6307\u7684\u662f\u5faa\u73af\u9000\u51fa\u65f6\u7684\u8fed\u4ee3\u6570\u3002\u5c06\u59cb\u7ec8\u6bd4\u6240\u91c7\u7528\u7684\u8fd4\u56de\u8fb9\u88ab\u4f7f\u7528\u6570\u5927\u4e00\u3002<em>\u8b66\u544a<\/em>\uff1a\u6b64\u53e5\u5728<em>\u6574\u6570\u57df<\/em>\u4e2d\u4e3a\u771f\uff1b\u5982\u679c\u8981\u5904\u7406\u5b9a\u5bbd\u6574\u6570\uff08\u4f8b\u5982LLVM\u503c\u6216<a rel=\"noreferrer noopener\" href=\"https:\/\/llvm.org\/doxygen\/classllvm_1_1SCEV.html\" target=\"_blank\">SCEV<\/a>\uff09\uff0c\u5219\u5728\u5c06\u4e00\u4e2a\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u65f6\u9700\u8981\u8c28\u614e\u3002<\/p>\n\n\n\n<p>\u8981\u6ce8\u610f\uff0c\u540c\u4e00\u57fa\u672c\u5757\u53ef\u4ee5\u5728\u540c\u4e00\u5faa\u73af\u4e2d\u6216\u540c\u65f6\u5728\u4e0d\u540c\u5faa\u73af\u4e2d\u626e\u6f14\u591a\u4e2a\u89d2\u8272\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u5757\u53ef\u4ee5\u4e00\u6b21\u6210\u4e3a\u4e24\u4e2a\u5d4c\u5957\u5faa\u73af\u7684\u5934\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u4ec5\u662f\u4e00\u4e2a\u5185\u90e8\u5faa\u73af\u7684\u9000\u51fa\u5757\u548c\u4e00\u4e2a\u540c\u7ea7\u5faa\u73af\u7684\u51fa\u53e3\u5757\u3002\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>while<\/strong> (..) {<br>  <strong>for<\/strong> (..) {}<br>  <strong>do<\/strong> {<br>    <strong>do<\/strong> {<br>      <em>\/\/ &lt;-- \u9700\u5173\u6ce8\u7684\u5757<\/em><br>      <strong>if<\/strong> (exit) <strong>break<\/strong>;<br>    } <strong>while<\/strong> (..);<br>  } <strong>while<\/strong> (..)<br>}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">LoopInfo<\/h2>\n\n\n\n<p>LoopInfo\u662f\u83b7\u53d6\u6709\u5173\u5faa\u73af\u4fe1\u606f\u7684\u6838\u5fc3\u5206\u6790\u3002\u4e0a\u9762\u7ed9\u51fa\u7684\u5b9a\u4e49\u5f88\u5c11\u6709\u5bf9\u4e8e\u6210\u529f\u4f7f\u7528\u6b64\u63a5\u53e3\u5f88\u91cd\u8981\u7684\u5173\u952e\u542b\u4e49\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>LoopInfo\u4e0d\u5305\u542b\u6709\u5173\u975e\u5faa\u73af\u5468\u671f\u7684\u4fe1\u606f\u3002\u56e0\u6b64\u5b83\u4e0d\u9002\u7528\u4e8e\u9700\u8981\u5b8c\u6574\u5468\u671f\u68c0\u6d4b\u4ee5\u786e\u4fdd\u6b63\u786e\u6027\u7684\u4efb\u4f55\u7b97\u6cd5\u3002<\/li><li>LoopInfo\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7528\u4e8e\u679a\u4e3e\u6240\u6709\u9876\u7ea7\u5faa\u73af\u63a5\u53e3\uff08\u4f8b\u5982\uff0c\u672a\u5305\u542b\u5728\u4efb\u4f55\u5176\u4ed6\u5faa\u73af\u4e2d\u7684\u90a3\u4e9b\u5faa\u73af\uff09\u3002\u4ece\u90a3\u91cc\u53ef\u4ee5\u904d\u5386\u4ee5\u8be5\u9876\u7ea7\u5faa\u73af\u4e3a\u6839\u7684\u5b50\u5faa\u73af\u6811\u3002<\/li><li>\u5728\u4f18\u5316\u8fc7\u7a0b\u4e2d\u53d8\u5f97\u65e0\u6cd5\u9759\u6001\u8bbf\u95ee\u7684\u5faa\u73af\u5fc5\u987b\u4eceLoopInfo\u4e2d\u5220\u9664\u3002\u5982\u679c\u7531\u4e8e\u67d0\u79cd\u539f\u56e0\u65e0\u6cd5\u5b8c\u6210\u6b64\u64cd\u4f5c\uff0c\u5219\u4f18\u5316<em>\u9700\u8981<\/em>\u4fdd\u7559\u5faa\u73af\u7684\u9759\u6001\u53ef\u8fbe\u6027\u3002<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5faa\u73af\u7b80\u5316\u5f62\u5f0f\uff08Loop Simplify Form\uff09<\/h2>\n\n\n\n<p>\u5faa\u73af\u7b80\u5316\u5f62\u5f0f\u662f\u4e00\u79cd\u89c4\u8303\u7684\u5f62\u5f0f\uff0c\u5b83\u4f7f\u591a\u9879\u5206\u6790\u548c\u8f6c\u6362\u66f4\u7b80\u5355\uff0c\u66f4\u6709\u6548\u3002\u5b83\u7531LoopSimplify\uff08<a href=\"https:\/\/llvm.org\/docs\/Passes.html#passes-loop-simplify\">-loop-simplify<\/a>\uff09pass\u786e\u4fdd\uff0c\u5e76\u4e14\u5728\u8c03\u5ea6LoopPass\u65f6\u7531\u4f20\u9012\u7ba1\u7406\u5668\u81ea\u52a8\u6dfb\u52a0\u3002\u6b64\u8fc7\u7a0b\u5728<a href=\"https:\/\/llvm.org\/doxygen\/LoopSimplify_8h_source.html\">LoopSimplify.h<\/a>\u4e2d\u5b9e\u73b0\u3002\u6210\u529f\u540e\uff0c\u5faa\u73af\u5c06\u5177\u6709\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4e00\u4e2aPreheader<\/li><li>\u5355\u4e2a\u7684\u8fd4\u56de\u8fb9\uff08\u8fd9\u610f\u5473\u7740\u53ea\u6709\u4e00\u4e2a\u95e9\u9501\uff09\u3002<\/li><li>\u4e13\u7528\u51fa\u53e3\u3002\u5373\u8be5\u5faa\u73af\u7684\u4efb\u4f55\u51fa\u53e3\u5757\u90fd\u6ca1\u6709\u4f4d\u4e8e\u5faa\u73af\u5916\u90e8\u7684\u524d\u9a71\u3002\u8fd9\u610f\u5473\u7740\u6240\u6709\u51fa\u53e3\u5757\u90fd\u7531\u5faa\u73af\u5934\u63a7\u5236\u3002<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5faa\u73af\u5c01\u95edSSA (Loop Closed SSA\uff0cLCSSA)<\/h2>\n\n\n\n<p>\u5982\u679c\u7a0b\u5e8f\u4e3aSSA\u5f62\u5f0f\uff0c\u5e76\u4e14\u8be5\u7a0b\u5e8f\u5728\u5faa\u73af\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u503c\u4ec5\u5728\u6b64\u5faa\u73af\u5185\u4f7f\u7528\uff0c\u5219\u8be5\u7a0b\u5e8f\u4e3a\u201c\u5faa\u73af\u5c01\u95edSSA\u5f62\u5f0f\u201d\u3002\u7528LLVM IR\u7f16\u5199\u7684\u7a0b\u5e8f\u59cb\u7ec8\u91c7\u7528SSA\u5f62\u5f0f\uff0c\u4f46\u4e0d\u4e00\u5b9a\u91c7\u7528LCSSA\u3002\u8981\u5b9e\u73b0\u540e\u8005\uff0c\u9700\u5728\u5faa\u73af\u672b\u5c3e\u4e3a\u5b58\u6d3b\u5230\u8de8\u8d8a\u5faa\u73af\u8fb9\u754c\u7684\u6240\u6709\u503c\u63d2\u5165\u4e00\u4e2aPHI\u8282\u70b9<sup class=\"modern-footnotes-footnote \" data-mfn=\"1\" data-mfn-post-scope=\"00000000000006280000000000000000_253\"><a href=\"javascript:void(0)\"  role=\"button\" aria-pressed=\"false\" aria-describedby=\"mfn-content-00000000000006280000000000000000_253-1\">1<\/a><\/sup><span id=\"mfn-content-00000000000006280000000000000000_253-1\" role=\"tooltip\" class=\"modern-footnotes-footnote__note\" tabindex=\"0\" data-mfn=\"1\">\u8981\u63d2\u5165\u8fd9\u4e9b\u5faa\u73af\u5c01\u95ed\u7684PHI\u8282\u70b9\uff0c\u5fc5\u987b\uff08\u91cd\u65b0\uff09\u8ba1\u7b97\u63a7\u5236\u8fb9\u754c\uff08\u5982\u679c\u8be5\u5faa\u73af\u5177\u6709\u591a\u4e2a\u51fa\u53e3\uff09\u3002<\/span>\u3002\u7279\u522b\u662f\uff0c\u8bf7\u8003\u8651\u4ee5\u4e0b\u5faa\u73af\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">c = ...;<br><strong>for<\/strong> (...) {<br>  <strong>if<\/strong> (c)<br>    X1 = ...<br>  <strong>else<\/strong><br>    X2 = ...<br>  X3 = phi(X1, X2);  <em>\/\/ \u5b9a\u4e49\u4e86X3<\/em><br>}<br><br>... = X3 + 4;  <em>\/\/ \u4f7f\u7528\u4e86X3\uff0c\u5373\u5b58\u6d3b\u5230\u4e86\u5faa\u73af\u5916\u90e8<\/em><\/pre>\n\n\n\n<p>\u5728\u5185\u90e8\u5faa\u73af\u4e2d\uff0cX3\u5728\u5faa\u73af\u5185\u5b9a\u4e49\uff0c\u4f46\u5728\u5faa\u73af\u5916\u90e8\u4f7f\u7528\u3002\u5728\u5faa\u73af\u5c01\u95edSSA\u5f62\u5f0f\u4e2d\uff0c\u8fd9\u5c06\u8868\u793a\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">c = ...;<br><strong>for<\/strong> (...) {<br>  <strong>if<\/strong> (c)<br>    X1 = ...<br>  <strong>else<\/strong><br>    X2 = ...<br>  X3 = phi(X1, X2);<br>}<br>X4 = phi(X3);<br><br>... = X4 + 4;<\/pre>\n\n\n\n<p>\u8fd9\u4ecd\u7136\u662f\u6709\u6548\u7684LLVM\u4ee3\u7801\uff1b\u989d\u5916\u7684phi\u8282\u70b9\u7eaf\u7cb9\u662f\u5197\u4f59\u7684\uff0c\u4f46\u662f\u6240\u6709LoopPass\u90fd\u9700\u8981\u4fdd\u7559\u5b83\u4eec\u3002\u8be5\u683c\u5f0f\u7531LCSSA\uff08<a href=\"https:\/\/llvm.org\/docs\/Passes.html#passes-lcssa\">-lcssa<\/a>\uff09Pass\u786e\u4fdd\uff0c\u5e76\u4e14\u5728\u8c03\u5ea6LoopPass\u65f6\u7531LoopPassManager\u81ea\u52a8\u6dfb\u52a0\u3002\u5faa\u73af\u4f18\u5316\u5b8c\u6210\u540e\uff0c\u8fd9\u4e9b\u591a\u4f59\u7684phi\u8282\u70b9\u5c06\u88ab<a href=\"https:\/\/llvm.org\/docs\/Passes.html#passes-instcombine\">-instcombine<\/a>\u5220\u200b\u200b\u9664\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u8f6c\u6362\u7684\u4e3b\u8981\u597d\u5904\u662f\u5b83\u4f7f\u8bb8\u591a\u5176\u4ed6\u7684\u5faa\u73af\u4f18\u5316\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u4e00\u4e2a\u7b80\u5355\u7684\u89c2\u5bdf\u7ed3\u679c\u662f\uff0c\u5982\u679c\u6709\u4eba\u9700\u8981\u67e5\u770b\u6240\u6709\u5916\u90e8\u4f7f\u7528\u8005\uff0c\u4ed6\u4eec\u53ef\u4ee5\u904d\u5386\u51fa\u53e3\u5757\u4e2d\u7684\u6240\u6709\uff08\u5faa\u73af\u5c01\u95ed\u7684\uff09PHI\u8282\u70b9\uff08\u66ff\u4ee3\u65b9\u6cd5\u662f\u626b\u63cf\u5faa\u73af\u4e2d\u6240\u6709\u6307\u4ee4\u7684def-use\u94fe<sup class=\"modern-footnotes-footnote \" data-mfn=\"2\" data-mfn-post-scope=\"00000000000006280000000000000000_253\"><a href=\"javascript:void(0)\"  role=\"button\" aria-pressed=\"false\" aria-describedby=\"mfn-content-00000000000006280000000000000000_253-2\">2<\/a><\/sup><span id=\"mfn-content-00000000000006280000000000000000_253-2\" role=\"tooltip\" class=\"modern-footnotes-footnote__note\" tabindex=\"0\" data-mfn=\"2\">SSA\u7684\u4e00\u4e2a\u7279\u6027\u662f\u6bcf\u4e2a\u5b9a\u4e49\u90fd\u6709\u4e00\u4e2adef-use\u94fe\uff0c\u8be5\u94fe\u662f\u8be5\u5b9a\u4e49\u7684\u6240\u6709\u4f7f\u7528\u7684\u5217\u8868\u3002LLVM\u901a\u8fc7\u5728\u5185\u90e8\u6570\u636e\u7ed3\u6784\u4e2d\u4fdd\u7559\u503c\u7684\u6240\u6709\u4f7f\u7528\u7684\u5217\u8868\u6765\u5b9e\u73b0\u6b64\u5c5e\u6027\u3002<\/span>\uff09\u3002<\/p>\n\n\n\n<p>\u7136\u540e\uff0c\u8003\u8651<a href=\"https:\/\/llvm.org\/docs\/Passes.html#passes-loop-unswitch\">-loop-unswitch<\/a>\u6389\u4e0a\u9762\u7684\u5faa\u73af\u3002\u56e0\u4e3a\u5b83\u91c7\u7528LCSSA\u5f62\u5f0f\uff0c\u6240\u4ee5\u6211\u4eec\u77e5\u9053\uff0c\u5728\u5faa\u73af\u5185\u90e8\u5b9a\u4e49\u7684\u4efb\u4f55\u503c\u90fd\u5c06\u4ec5\u5728\u5faa\u73af\u5185\u90e8\u6216\u5728\u5faa\u73af\u5c01\u95edPHI\u8282\u70b9\u7684\u5faa\u73af\u4e2d\u4f7f\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u552f\u4e00\u7684\u95ed\u73afPHI\u8282\u70b9\u662fX4\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u590d\u5236\u5faa\u73af\u5e76\u76f8\u5e94\u5730\u66f4\u6539X4\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">c = ...;<br>if (c) {<br>  for (...) {<br>    if (true)<br>      X1 = ...<br>    else<br>      X2 = ...<br>    X3 = phi(X1, X2);<br>  }<br>} else {<br>  for (...) {<br>    if (false)<br>      X1' = ...<br>    else<br>      X2' = ...<br>    X3' = phi(X1', X2');<br>  }<br>}<br>X4 = phi(X3, X3')<\/pre>\n\n\n\n<p>\u73b0\u5728\uff0c\u5bf9X4\u7684\u6240\u6709\u4f7f\u7528\u90fd\u5c06\u83b7\u5f97\u66f4\u65b0\u540e\u7684\u503c\uff08\u901a\u5e38\uff0c\u5982\u679c\u5faa\u73af\u91c7\u7528LCSSA\u5f62\u5f0f\uff0c\u5219\u5728\u4efb\u4f55\u5faa\u73af\u8f6c\u6362\u4e2d\uff0c\u6211\u4eec\u53ea\u9700\u66f4\u65b0\u5faa\u73af\u5c01\u95edPHI\u8282\u70b9\u5373\u53ef\u4f7f\u66f4\u6539\u751f\u6548\uff09\u3002\u5982\u679c\u6211\u4eec\u6ca1\u6709\u5faa\u73af\u5c01\u95edSSA\u5f62\u5f0f\uff0c\u5219\u610f\u5473\u7740X3\u53ef\u80fd\u5728\u5faa\u73af\u5916\u90e8\u4f7f\u7528\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5c06\u4e0d\u5f97\u4e0d\u5f15\u5165X4\uff08\u5b83\u662f\u65b0\u7684X3\uff09\uff0c\u5e76\u7528X4\u66ff\u4ee3X3\u7684\u6240\u6709\u4f7f\u7528\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5e94\u8be5\u6ce8\u610f\uff0c\u56e0\u4e3aLLVM\u4e3a\u6bcf\u4e2aValue\u4fdd\u7559\u4e86\u4e00\u4e2adef-use\u94fe\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u9700\u8981\u6267\u884c\u6570\u636e\u6d41\u5206\u6790\u6765\u67e5\u627e\u548c\u66ff\u6362\u6240\u6709\u4f7f\u7528\uff08\u751a\u81f3\u8fd8\u6709\u4e00\u4e2a\u5b9e\u7528\u51fd\u6570\uff0creplaceAllUsesWith()\uff0c\u5b83\u901a\u8fc7\u8fed\u4ee3def-use\u94fe\u6765\u6267\u884c\u6b64\u8f6c\u6362\uff09\u3002<\/p>\n\n\n\n<p>\u53e6\u4e00\u4e2a\u91cd\u8981\u7684\u4f18\u70b9\u662f\uff0c\u5f52\u7eb3\u53d8\u91cf\uff08<a href=\"https:\/\/en.wikipedia.org\/wiki\/Induction_variable\">induction variable<\/a>\uff09\u7684\u6240\u6709\u4f7f\u7528\u7684\u884c\u4e3a\u90fd\u662f\u76f8\u540c\u7684\u3002\u5426\u5219\uff0c\u9700\u8981\u533a\u5206\u5728\u53d8\u91cf\u88ab\u5b9a\u4e49\u7684\u5faa\u73af\u4e4b\u5916\u4f7f\u7528\u53d8\u91cf\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>for<\/strong> (i = 0; i &lt; 100; i++) {<br>  <strong>for<\/strong> (j = 0; j &lt; 100; j++) {<br>    k = i + j;<br>    use(k);    <em>\/\/ \u4f7f\u75281<\/em><br>  }<br>  use(k);      <em>\/\/ \u4f7f\u75282<\/em><br>}<\/pre>\n\n\n\n<p>\u4ece\u5177\u6709\u5e38\u89c4SSA\u5f62\u5f0f\u7684\u5916\u90e8\u5faa\u73af\u770b\uff0c\u7b2c\u4e00\u6b21\u4f7f\u7528k\u884c\u4e3a\u4e0d\u6b63\u786e\uff0c\u800c\u7b2c\u4e8c\u6b21\u4f7f\u7528\u662f\u4e00\u4e2a\u57fa\u6570\u4e3a100\u3001\u6b65\u6570\u4e3a1\u7684\u5f52\u7eb3\u53d8\u91cf\uff08\u8bd1\u6ce8\uff1a\u5373i + 100\uff0c100\u662fj\u8fed\u4ee3\u5b8c\u6210\u540e\u7684\u503c\uff09\u3002\u5c3d\u7ba1\u5728\u5b9e\u8df5\u4e2d\u548cLLVM\u4e0a\u4e0b\u6587\u4e2d\uff0c\u6b64\u7c7b\u60c5\u51b5SCEV\u5b9e\u9645\u4e0a\u662f\u53ef\u4ee5\u5904\u7406\u7684\u3002\u6807\u91cf\u6f14\u5316\uff08<a href=\"https:\/\/llvm.org\/docs\/Passes.html#passes-scalar-evolution\">scalar-evolution<\/a>\uff09\u53c8\u79f0SCEV\uff0c\u662f\u4e00\u79cd\uff08\u5206\u6790\uff09Pass\uff0c\u5b83\u5bf9\u5faa\u73af\u4e2d\u6807\u91cf\u8868\u8fbe\u5f0f\u7684\u6f14\u5316\u8fdb\u884c\u5206\u6790\u548c\u5206\u7c7b\u3002<\/p>\n\n\n\n<p>\u901a\u5e38\uff0c\u5728LCSSA\u5f62\u5f0f\u7684\u5faa\u73af\u4e2d\u4f7f\u7528SCEV\u66f4\u5bb9\u6613\u3002\u6839\u636e\u5b9a\u4e49\uff0cSCEV\u53ef\u4ee5\u5206\u6790\u7684\u6807\u91cf\uff08\u5faa\u73af\u53ef\u53d8\u7684\uff09\u8868\u8fbe\u5f0f\u7684\u6f14\u5316\u662f\u76f8\u5bf9\u4e8e\u5faa\u73af\u7684\u3002LLVM\u4e2d\u7684\u8868\u8fbe\u5f0f\u7531<a rel=\"noreferrer noopener\" href=\"https:\/\/llvm.org\/doxygen\/classllvm_1_1Instruction.html\" target=\"_blank\">llvm::Instruction<\/a>\u8868\u793a\u3002\u5982\u679c\u8868\u8fbe\u5f0f\u4f4d\u4e8e\u4e24\u4e2a\uff08\u6216\u591a\u4e2a\uff09\u5faa\u73af\u5185\uff08\u4ec5\u5728\u5faa\u73af\u5d4c\u5957\u7684\u60c5\u51b5\u4e0b\u624d\u53ef\u80fd\u53d1\u751f\uff0c\u5982\u4e0a\u4f8b\u6240\u793a\uff09\uff0c\u5e76\u4e14\u60f3\u8981\u5206\u6790\u5176\u6f14\u5316\uff08\u4eceSCEV\uff09\uff0c\u5219\u8fd8\u5fc5\u987b\u6307\u5b9a\u76f8\u5bf9\u4e8e\u4f60\u60f3\u8981\u7684\u4ec0\u4e48\u5faa\u73af\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5fc5\u987b\u4f7f\u7528<a href=\"https:\/\/llvm.org\/doxygen\/classllvm_1_1ScalarEvolution.html#a21d6ee82eed29080d911dbb548a8bb68\">getSCEVAtScope<\/a>()\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\uff0c\u5982\u679c\u6240\u6709\u5faa\u73af\u90fd\u662fLCSSA\u5f62\u5f0f\uff0c\u5219\u6bcf\u4e2a\u8868\u8fbe\u5f0f\u5b9e\u9645\u4e0a\u7531\u4e24\u4e2a\u4e0d\u540c\u7684llvm::Instructions\u8868\u793a\u3002\u4e00\u4e2a\u5728\u5faa\u73af\u5185\u90e8\uff0c\u4e00\u4e2a\u5728\u5faa\u73af\u5916\u90e8\uff0c\u5b83\u662f\u5faa\u73af\u5c01\u95ed\u7684PHI\u8282\u70b9\uff0c\u4ee3\u8868\u6700\u540e\u4e00\u6b21\u8fed\u4ee3\u540e\u8868\u8fbe\u5f0f\u7684\u503c\uff08\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u5c06\u6bcf\u4e2a\u5faa\u73af\u53ef\u53d8\u7684\u8868\u8fbe\u5f0f\u5206\u6210\u4e24\u4e2a\u8868\u8fbe\u5f0f\uff0c\u56e0\u6b64\uff0c\u6bcf\u4e2a\u8868\u8fbe\u5f0f\u6700\u591a\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\uff09\u3002\u73b0\u5728\u53ea\u8981\u4f7f\u7528<a href=\"https:\/\/llvm.org\/doxygen\/classllvm_1_1ScalarEvolution.html#a30bd18ac905eacf3601bc6a553a9ff49\">getSCEV<\/a>()\uff0c\u7136\u540e\u901a\u8fc7\u4f20\u9012\u7ed9\u5b83\u7684\u8fd9\u4e24\u4e2allvm::Instructions\u4e2d\u7684\u90a3\u4e2a\u6765\u6d88\u9664\u4e0a\u4e0b\u6587\/\u4f5c\u7528\u57df\/\u76f8\u5bf9\u5faa\u73af\u7684\u6b67\u4e49\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u201c\u66f4\u52a0\u89c4\u8303\u201d\u7684\u5faa\u73af<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">\u65cb\u8f6c\u5faa\u73af\uff08Rotated Loops\uff09<\/h2>\n\n\n\n<p>\u5faa\u73af\u901a\u8fc7LoopRotate\uff08<a href=\"https:\/\/llvm.org\/docs\/Passes.html#passes-loop-rotate\">loop-rotate<\/a>\uff09\u65cb\u8f6c\uff0c\u8be5Pass\u5c06\u5faa\u73af\u8f6c\u6362\u4e3ado \/ while\u6837\u5f0f\u5faa\u73af\uff0c\u5e76\u5728<a href=\"https:\/\/llvm.org\/doxygen\/LoopRotation_8h_source.html\">LoopRotation.h<\/a>\u4e2d\u5b9e\u73b0\u3002\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void test(int n) {<br>  <strong>for<\/strong> (int i = 0; i &lt; n; i += 1)<br>    <em>\/\/ <\/em>\u5faa\u73af\u4f53<br>}<\/pre>\n\n\n\n<p>\u88ab\u8f6c\u6362\u5230\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void test(int n) {<br>  int i = 0;<br>  <strong>do<\/strong> {<br>    <em>\/\/ <\/em>\u5faa\u73af\u4f53<br>    i += 1;<br>  } <strong>while<\/strong> (i &lt; n);<br>}<\/pre>\n\n\n\n<p><strong>\u8b66\u544a<\/strong>\uff1a\u4ec5\u5f53\u7f16\u8bd1\u5668\u53ef\u4ee5\u8bc1\u660e\u5faa\u73af\u4f53\u81f3\u5c11\u6267\u884c\u4e00\u6b21\u65f6\uff0c\u6b64\u8f6c\u6362\u624d\u6709\u6548\u3002\u5426\u5219\uff0c\u5b83\u5fc5\u987b\u63d2\u5165\u4e00\u4e2a\u5c06\u5728\u8fd0\u884c\u65f6\u5bf9\u5176\u8fdb\u884c\u6d4b\u8bd5\u7684\u4fdd\u62a4\u7a0b\u5e8f\u3002\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5c06\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void test(int n) {<br>  int i = 0;<br>  <strong>if<\/strong> (n &gt; 0) {<br>    <strong>do<\/strong> {<br>      <em>\/\/ <\/em>\u5faa\u73af\u4f53<br>      i += 1;<br>    } <strong>while<\/strong> (i &lt; n);<br>  }<br>}<\/pre>\n\n\n\n<p>\u4e86\u89e3LLVM IR\u7ea7\u522b\u7684\u5faa\u73af\u65cb\u8f6c\u6548\u679c\u662f\u5f88\u91cd\u8981\u7684\u3002\u6211\u4eec\u9075\u5faaLLVM IR\u4e2d\u7684\u5148\u524d\u793a\u4f8b\uff0c\u540c\u65f6\u8fd8\u63d0\u4f9b\u4e86\u63a7\u5236\u6d41\u56fe\uff08CFG\uff09\u7684\u56fe\u50cf\u8868\u793a\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<a href=\"https:\/\/llvm.org\/docs\/Passes.html#passes-view-cfg\">view-cfg<\/a> Pass\u83b7\u5f97\u76f8\u540c\u7684\u56fe\u50cf\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u5f00\u5934\u7684<strong>for<\/strong>\u5faa\u73af\u53ef\u4ee5\u88ab\u7ffb\u8bd1\u5230\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">define void @test(i32 %n) {<br>entry:<br>  br label %for.header<br><br>for.header:<br>  %i = phi i32 [ 0, %entry ], [ %i.next, %latch ]<br>  %cond = icmp slt i32 %i, %n<br>  br i1 %cond, label %body, label %exit<br><br>body:<br>  ; Loop body<br>  br label %latch<br><br>latch:<br>  %i.next = add nsw i32 %i, 1<br>  br label %for.header<br><br>exit:<br>  ret void<br>}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"467\" height=\"554\" data-attachment-id=\"258\" data-permalink=\"https:\/\/syaro.hotococoa.moe\/wordpress\/?attachment_id=258\" data-orig-file=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-initial-loop.png?fit=467%2C554&amp;ssl=1\" data-orig-size=\"467,554\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"loop-terminology-initial-loop\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-initial-loop.png?fit=253%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-initial-loop.png?fit=467%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-initial-loop.png?resize=467%2C554&#038;ssl=1\" alt=\"\" class=\"wp-image-258\" srcset=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-initial-loop.png?w=467&amp;ssl=1 467w, https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-initial-loop.png?resize=253%2C300&amp;ssl=1 253w\" sizes=\"auto, (max-width: 467px) 100vw, 467px\" \/><\/figure>\n\n\n\n<p>\u5728\u8bf4\u660eLoopRotate\u5982\u4f55\u5b9e\u9645\u8f6c\u6362\u6b64\u5faa\u73af\u4e4b\u524d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\uff08\u624b\u52a8\uff09\u5c06\u5176\u8f6c\u6362\u4e3ado-while\u98ce\u683c\u7684\u5faa\u73af\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">define void @test(i32 %n) {<br>entry:<br>  br label %body<br><br>body:<br>  %i = phi i32 [ 0, %entry ], [ %i.next, %latch ]<br>  ; Loop body<br>  br label %latch<br><br>latch:<br>  %i.next = add nsw i32 %i, 1<br>  %cond = icmp slt i32 %i.next, %n<br>  br i1 %cond, label %body, label %exit<br><br>exit:<br>  ret void<br>}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"554\" data-attachment-id=\"259\" data-permalink=\"https:\/\/syaro.hotococoa.moe\/wordpress\/?attachment_id=259\" data-orig-file=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-rotated-loop.png?fit=748%2C789&amp;ssl=1\" data-orig-size=\"748,789\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"loop-terminology-rotated-loop\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-rotated-loop.png?fit=284%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-rotated-loop.png?fit=525%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-rotated-loop.png?resize=525%2C554&#038;ssl=1\" alt=\"\" class=\"wp-image-259\" srcset=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-rotated-loop.png?w=748&amp;ssl=1 748w, https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-rotated-loop.png?resize=284%2C300&amp;ssl=1 284w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\n\n\n\n<p>\u6ce8\u610f\u4e24\u4ef6\u4e8b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5c06\u6761\u4ef6\u68c0\u67e5\u79fb\u81f3\u5faa\u73af\u7684\u201c\u5e95\u90e8\u201d\uff0c\u5373\u95e9\u9501\u3002\u8fd9\u662fLoopRotate\u901a\u8fc7\u5c06\u5faa\u73af\u7684\u6807\u5934\u590d\u5236\u5230\u95e9\u9501\u6765\u5b8c\u6210\u7684\u3002<\/li><li>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u63a8\u65ad\u51fa\u5faa\u73af\u80af\u5b9a\u4f1a\u81f3\u5c11\u6267\u884c\u4e00\u6b21\uff0c\u56e0\u6b64\u4e0a\u8ff0\u8f6c\u6362\u65e0\u6548\u3002\u5982\u4e0a\u6240\u8ff0\uff0c\u5fc5\u987b\u63d2\u5165\u9632\u62a4\uff0c\u8fd9\u662fLoopRotate\u8981\u505a\u7684\u3002<\/li><\/ul>\n\n\n\n<p>\u8fd9\u662fLoopRotate\u8f6c\u6362\u6b64\u5faa\u73af\u7684\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">define void @test(i32 %n) {<br>entry:<br>  %guard_cond = icmp slt i32 0, %n<br>  br i1 %guard_cond, label %loop.preheader, label %exit<br><br>loop.preheader:<br>  br label %body<br><br>body:<br>  %i2 = phi i32 [ 0, %loop.preheader ], [ %i.next, %latch ]<br>  br label %latch<br><br>latch:<br>  %i.next = add nsw i32 %i2, 1<br>  %cond = icmp slt i32 %i.next, %n<br>  br i1 %cond, label %body, label %loop.exit<br><br>loop.exit:<br>  br label %exit<br><br>exit:<br>  ret void<br>}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"588\" data-attachment-id=\"260\" data-permalink=\"https:\/\/syaro.hotococoa.moe\/wordpress\/?attachment_id=260\" data-orig-file=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-guarded-loop.png?fit=764%2C855&amp;ssl=1\" data-orig-size=\"764,855\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"loop-terminology-guarded-loop\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-guarded-loop.png?fit=268%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-guarded-loop.png?fit=525%2C588&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-guarded-loop.png?resize=525%2C588&#038;ssl=1\" alt=\"\" class=\"wp-image-260\" srcset=\"https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-guarded-loop.png?w=764&amp;ssl=1 764w, https:\/\/i0.wp.com\/syaro.hotococoa.moe\/wordpress\/wp-content\/uploads\/2020\/04\/loop-terminology-guarded-loop.png?resize=268%2C300&amp;ssl=1 268w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\n\n\n\n<p>\u7ed3\u679c\u6bd4\u6211\u4eec\u9884\u671f\u7684\u8981\u590d\u6742\u4e00\u4e9b\uff0c\u56e0\u4e3aLoopRotate\u786e\u4fdd\u65cb\u8f6c\u540e\u5faa\u73af\u5904\u4e8e\u201c\u5faa\u73af\u7b80\u5316\u5f62\u5f0f\u201d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b83\u63d2\u5165\uff05loop.preheader\u57fa\u672c\u5757\uff0c\u4ee5\u4fbf\u8be5\u5faa\u73af\u5177\u6709\u4e00\u4e2aPreheader\uff0c\u5e76\u5f15\u5165\u4e86\uff05loop.exit\u57fa\u672c\u5757\uff0c\u4ee5\u4fbf\u8be5\u5faa\u73af\u5177\u6709\u4e13\u7528\u51fa\u53e3\uff08\u5426\u5219\uff0c\uff05exit\u5c06\u4ece\uff05latch\u548c\uff05entry\u8df3\u8f6c\u5230\uff0c\u4f46\u5faa\u73af\u4e2d\u4e0d\u5305\u542b\uff05entry\uff09\u3002\u8bf7\u6ce8\u610f\uff0c\u4e3a\u4e86\u6210\u529f\u5e94\u7528LoopRotate\uff0c\u5faa\u73af\u4e5f\u5fc5\u987b\u4e8b\u5148\u91c7\u7528\u201c\u5faa\u73af\u7b80\u5316\u5f62\u5f0f\u201d\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u5f62\u5f0f\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u5b83\u5141\u8bb8\u5c06\u4e0d\u53d8\u7684\u6307\u4ee4\uff08\u7279\u522b\u662f\u52a0\u8f7d\uff09\u63d0\u5347\u5230Preheader\u4e2d\u3002\u8fd9\u4e5f\u53ef\u4ee5\u5728\u975e\u65cb\u8f6c\u5faa\u73af\u4e2d\u5b8c\u6210\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u7f3a\u70b9\u3002\u8ba9\u6211\u4eec\u7528\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\u5b83\u4eec\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>for<\/strong> (int i = 0; i &lt; n; ++i) {<br>  <strong>auto<\/strong> v = *p;<br>  use(v);<br>}<\/pre>\n\n\n\n<p>\u6211\u4eec\u5047\u8bbe\u4ecep\u52a0\u8f7d\u662f\u4e0d\u53d8\u7684\uff0c\u800cuse(v)\u662f\u67d0\u4e9b\u4f7f\u7528v\u7684\u8bed\u53e5\u3002\u5982\u679c\u53ea\u60f3\u6267\u884c\u4e00\u6b21\u52a0\u8f7d\uff0c\u5c31\u53ef\u4ee5\u5c06\u5176\u79fb\u201c\u51fa\u201d\u5faa\u73af\u4e3b\u4f53\uff0c\u7ed3\u679c\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>auto<\/strong> v = *p;<br><strong>for<\/strong> (int i = 0; i &lt; n; ++i) {<br>  use(v);<br>}<\/pre>\n\n\n\n<p>\u4f46\u662f\u73b0\u5728\uff0c\u5728n &lt;= 0\u7684\u60c5\u51b5\u4e0b\uff0c\u521d\u59cb\u5f62\u5f0f\u4e0b\u5faa\u73af\u4f53\u5c06\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c\uff0c\u56e0\u6b64\u52a0\u8f7d\u4e5f\u5c06\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c\u3002\u8fd9\u662f\u4e00\u4e2a\u4e3b\u8981\u51fa\u4e8e\u8bed\u4e49\u539f\u56e0\u7684\u95ee\u9898\u3002\u8003\u8651n &lt;= 0\u4e14\u4ecep\u52a0\u8f7d\u65e0\u6548\u7684\u60c5\u51b5\u3002\u5728\u521d\u59cb\u5f62\u5f0f\u4e2d\u4e0d\u4f1a\u6709\u9519\u8bef\u3002\u4f46\u662f\uff0c\u901a\u8fc7\u8fd9\u79cd\u8f6c\u6362\uff0c\u6211\u4eec\u5c06\u5f15\u5165\u4e00\u4e2a\u52a0\u8f7d\uff0c\u5b9e\u9645\u4e0a\u6253\u7834\u4e86\u521d\u59cb\u7684\u8bed\u4e49\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u907f\u514d\u8fd9\u4e24\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u63d2\u5165\u4e00\u4e2a\u9632\u62a4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>if<\/strong> (n &gt; 0) {  <em>\/\/ <\/em>\u5faa\u73af\u9632\u62a4<br>  <strong>auto<\/strong> v = *p;<br>  <strong>for<\/strong> (int i = 0; i &lt; n; ++i) {<br>    use(v);<br>  }<br>}<\/pre>\n\n\n\n<p>\u8fd9\u5f53\u7136\u66f4\u597d\uff0c\u4f46\u53ef\u4ee5\u7a0d\u4f5c\u6539\u8fdb\u3002\u8bf7\u6ce8\u610f\uff0c\u5bf9n\u662f\u5426\u5927\u4e8e0\u7684\u68c0\u67e5\u6267\u884c\u4e86\u4e24\u6b21\uff08\u5e76\u4e14n\u5728\u8fd9\u4e4b\u95f4\u4e0d\u53d8\uff09\u3002\u4e00\u6b21\u662f\u5f53\u6211\u4eec\u68c0\u67e5\u4fdd\u62a4\u6761\u4ef6\u65f6\uff0c\u53e6\u4e00\u6b21\u5728\u5faa\u73af\u7684\u7b2c\u4e00\u6b21\u6267\u884c\u4e2d\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u505a\u65e0\u6761\u4ef6\u7684\u7b2c\u4e00\u6b21\u6267\u884c\uff0c\u5e76\u5728\u6700\u540e\u63d2\u5165\u5faa\u73af\u6761\u4ef6\u3002\u8fd9\u5b9e\u9645\u4e0a\u610f\u5473\u7740\u5c06\u5faa\u73af\u8f6c\u6362\u4e3ado-while\u5faa\u73af\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>if<\/strong> (0 &lt; n) {<br>  <strong>auto<\/strong> v = *p;<br>  <strong>do<\/strong> {<br>    use(v);<br>    ++i;<br>  } <strong>while<\/strong> (i &lt; n);<br>}<\/pre>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0cLoopRotate\u901a\u5e38\u4e0d\u8fdb\u884c\u6b64\u7c7b\u63d0\u5347\u3002\u76f8\u53cd\uff0c\u5b83\u662f\u5176\u4ed6Pass\u7684\u4e00\u79cd<a href=\"https:\/\/en.wikipedia.org\/wiki\/Enabling_transformation\">\u4f7f\u80fd\u8f6c\u6362<\/a>\uff08Enabling transformation\uff09\uff08\u5982\u5faa\u73af\u4e0d\u53d8\u4ee3\u7801\u79fb\u52a8\uff08<a href=\"https:\/\/llvm.org\/docs\/Passes.html#passes-licm\">-licm<\/a>\uff09\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1aLLVM Loop Terminology (and Canonical Forms) \u2014 LLVM &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/syaro.hotococoa.moe\/wordpress\/?p=253\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cLLVM\u5faa\u73af\u672f\u8bed\uff08\u548c\u89c4\u8303\u5f62\u5f0f\uff09\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[10],"tags":[],"class_list":["post-253","post","type-post","status-publish","format-standard","hentry","category-llvm"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9CzIN-45","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":160,"url":"https:\/\/syaro.hotococoa.moe\/wordpress\/?p=160","url_meta":{"origin":253,"position":0},"title":"Android Studio\u8c03\u8bd5\u65f6\u5361\u5728Loaded module\u7684\u89e3\u51b3\u65b9\u6848","author":"Chino","date":"2018\u5e7411\u670821\u65e5","format":false,"excerpt":"\u4e4b\u524d\u7528Android Studio\u8c03\u8bd5app\u65f6\uff0c\u5728\u542f\u52a8\u8fc7\u7a0b\u4e2d\u603b\u662f\u5361\u5728Loaded module\u63d0\u793a\uff0c\u2026","rel":"","context":"\u5728\u201c\u8e29\u5751\u201d\u4e2d","block_context":{"text":"\u8e29\u5751","link":"https:\/\/syaro.hotococoa.moe\/wordpress\/?cat=6"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":67,"url":"https:\/\/syaro.hotococoa.moe\/wordpress\/?p=67","url_meta":{"origin":253,"position":1},"title":"\u8bb0\u4e00\u4e9b\u81ea\u5df1\u9047\u5230\u7684\u5751","author":"Chino","date":"2018\u5e741\u670826\u65e5","format":false,"excerpt":"\u7c7b\u5185\u5305\u542b\u975e\u9759\u6001\u7684\u53f3\u503c\u5f15\u7528\u6210\u5458\u65f6\u9690\u5f0f\u751f\u6210\u7684\u590d\u5236\u6784\u9020\u51fd\u6570\u662fdelete\u7684\uff0c\u4f46\u662f\u79fb\u52a8\u6784\u9020\u6b63\u5e38\uff0c\u56e0\u6b64\u5305\u542b\u53f3\u503c\u2026","rel":"","context":"\u5728\u201c\u8e29\u5751\u201d\u4e2d","block_context":{"text":"\u8e29\u5751","link":"https:\/\/syaro.hotococoa.moe\/wordpress\/?cat=6"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":134,"url":"https:\/\/syaro.hotococoa.moe\/wordpress\/?p=134","url_meta":{"origin":253,"position":2},"title":"\u4eceC++\u8c08\u591a\u6001","author":"Chino","date":"2018\u5e749\u670814\u65e5","format":false,"excerpt":"\u591a\u6001\uff08Polymorphism\uff09\u6307\u7684\u662f\u4e00\u79cd\u76f8\u540c\u7684\u5f62\u5f0f\uff08\u540d\u79f0\u548c\u64cd\u4f5c\u7b49\uff09\u8868\u73b0\u51fa\u4e0d\u540c\u884c\u4e3a\u7684\u6982\u5ff5\uff0c\u591a\u6001\u7684\u6982\u5ff5\u2026","rel":"","context":"\u5728\u201c\u7f16\u7a0b\u5b66\u4e60\u201d\u4e2d","block_context":{"text":"\u7f16\u7a0b\u5b66\u4e60","link":"https:\/\/syaro.hotococoa.moe\/wordpress\/?cat=5"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/syaro.hotococoa.moe\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/syaro.hotococoa.moe\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/syaro.hotococoa.moe\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/syaro.hotococoa.moe\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/syaro.hotococoa.moe\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=253"}],"version-history":[{"count":11,"href":"https:\/\/syaro.hotococoa.moe\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/253\/revisions"}],"predecessor-version":[{"id":292,"href":"https:\/\/syaro.hotococoa.moe\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/253\/revisions\/292"}],"wp:attachment":[{"href":"https:\/\/syaro.hotococoa.moe\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/syaro.hotococoa.moe\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/syaro.hotococoa.moe\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}