{"id":3026,"date":"2013-09-08T04:34:15","date_gmt":"2013-09-07T19:34:15","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=3026"},"modified":"2013-09-08T04:34:15","modified_gmt":"2013-09-07T19:34:15","slug":"7-5-%e5%a4%89%e6%9b%b4%e5%8f%af%e8%83%bd%e3%81%ab%e3%81%99%e3%82%8b%e3%81%9f%e3%82%81%e3%81%ae%e8%aa%bf%e6%95%b4","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/3026","title":{"rendered":"7.5. \u5909\u66f4\u53ef\u80fd\u306b\u3059\u308b\u305f\u3081\u306e\u8abf\u6574"},"content":{"rendered":"<p><a href=\"https:\/\/peta.okechan.net\/blog\/archives\/2836\" title=\"LLVM\u306b\u3088\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u5b9f\u88c5\">LLVM\u306b\u3088\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u5b9f\u88c5\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u65e5\u672c\u8a9e\u8a33<\/a><br \/>\n\u7b2c7\u7ae0 \u4e07\u83ef\u93e1: \u8a00\u8a9e\u306e\u62e1\u5f35: \u5909\u66f4\u53ef\u80fd\u306a\u5909\u6570\uff08Mutable Variables\uff09<br \/>\n<a href=\"http:\/\/llvm.org\/docs\/tutorial\/LangImpl07.html#adjusting-existing-variables-for-mutation\" target=\"_blank\">\u7b2c5\u7bc0 \u5909\u66f4\u53ef\u80fd\u306b\u3059\u308b\u305f\u3081\u306e\u8abf\u6574<\/a><\/p>\n<p>\u4e07\u83ef\u93e1\u306e\u30b7\u30f3\u30dc\u30eb\u30c6\u30fc\u30d6\u30eb\u306f\u3001NamedValues\u30de\u30c3\u30d7\u306b\u3088\u3063\u3066\u30b3\u30fc\u30c9\u751f\u6210\u6642\u306b\u4f7f\u308f\u308c\u3066\u3044\u308b\u3002<br \/>\n\u73fe\u72b6\u3067\u306f\u3053\u306e\u30de\u30c3\u30d7\u306f\u3001\u540d\u524d\u304c\u4ed8\u3044\u305f\u500d\u7cbe\u5ea6\u306e\u5909\u6570\u3092\u4fdd\u6301\u3059\u308bLLVM Value*\u3092\u8ffd\u8de1\u3057\u7d9a\u3051\u308b\u3002<br \/>\n\u5909\u6570\u306e\u5909\u66f4\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u305f\u3081\u306b\u3001\u5909\u6570\u306e\u30e1\u30e2\u30ea\u306e\u4f4d\u7f6e\u3092NamedValues\u3067\u4fdd\u6301\u3059\u308b\u3088\u3046\u3001\u5c11\u3057\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u3053\u306e\u5909\u66f4\u306f\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3002<br \/>\n\u30b3\u30fc\u30c9\u306e\u69cb\u9020\u306f\u5909\u308f\u308b\u304c\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u632f\u308b\u821e\u3044\u306f\u5909\u308f\u3089\u306a\u3044\u3002<br \/>\n\u3053\u308c\u4ee5\u964d\u52a0\u3048\u3066\u3044\u304f\u5909\u66f4\u306f\u3001\u5168\u3066\u4e07\u83ef\u93e1\u306e\u30b3\u30fc\u30c9\u751f\u6210\u5668\u306e\u306a\u304b\u3067\u5206\u96e2\u3055\u308c\u3066\u3044\u308b\u3002<\/p>\n<p>\u3053\u306e\u6642\u70b9\u3067\u306f\u3001\u4e07\u83ef\u93e1\u306f2\u7a2e\u985e\u306e\u5909\u6570\u306e\u307f\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u308b\u3002<br \/>\n\u95a2\u6570\u306e\u5f15\u6570\u3068\u3001for\u30eb\u30fc\u30d7\u306e\u305f\u3081\u306e\u5e30\u7d0d\u5909\u6570\u3067\u3042\u308b\u3002<br \/>\n\u4e00\u8cab\u6027\u306e\u305f\u3081\u3001\u30e6\u30fc\u30b6\u5b9a\u7fa9\u5909\u6570\u306b\u52a0\u3048\u3066\u3001\u305d\u308c\u3089\u306e\u5909\u6570\u306e\u5909\u66f4\u3082\u53ef\u80fd\u306b\u3057\u3066\u3044\u304f\u3002<br \/>\n\u3064\u307e\u308a\u3001\u305d\u308c\u3089\u4e21\u65b9\u3068\u3082\u3001\u30e1\u30e2\u30ea\u306e\u4f4d\u7f6e\u304c\u5fc5\u8981\u3067\u3042\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3042\u308b\u3002<\/p>\n<p>\u4e07\u83ef\u93e1\u306e\u6539\u826f\u3092\u59cb\u3081\u308b\u306b\u3042\u305f\u3063\u3066\u3001NamedValues\u30de\u30c3\u30d7\u3092\u3001Value*\u3058\u3083\u306a\u304f\u3066AllocaInst*\u3092\u6307\u3059\u3088\u3046\u5909\u66f4\u3059\u308b\u3002<br \/>\n\u3053\u3046\u3057\u3066\u304a\u3051\u3070\u3001\u3042\u3068\u306fC++\u306e\u30b3\u30f3\u30d1\u30a4\u30e9\u304c\u30b3\u30fc\u30c9\u306e\u5909\u66f4\u3059\u3079\u304d\u7b87\u6240\u3092\u6559\u3048\u3066\u304f\u308c\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: \u30b3\u30f3\u30d1\u30a4\u30eb\u30a8\u30e9\u30fc\u306e\u5185\u5bb9\u3092\u898b\u308c\u3070\u5909\u66f4\u3059\u3079\u304d\u7b87\u6240\u304c\u5206\u304b\u308b\u3002<\/em><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">static std::map&lt;std::string, AllocaInst*&gt; NamedValues;<\/pre>\n<p>\u305d\u3057\u305f\u3089\u3001alloca\u547d\u4ee4\u3092\u751f\u6210\u3059\u308b\u3088\u3046\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u78ba\u5b9f\u306balloca\u304c\u95a2\u6570\u306eentry\u30d6\u30ed\u30c3\u30af\u306b\u751f\u6210\u3055\u308c\u308b\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306e\u30d8\u30eb\u30d1\u95a2\u6570\u3092\u4f7f\u3046\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/\/ CreateEntryBlockAlloca - alloca\u547d\u4ee4\u3092\u95a2\u6570\u306eentry\u30d6\u30ed\u30c3\u30af\u306b\u751f\u6210\u3059\u308b\u3002\r\n\/\/\/ \u3053\u308c\u306f\u5909\u66f4\u53ef\u80fd\u306a\u5909\u6570\u306a\u3069\u306e\u305f\u3081\u306b\u4f7f\u308f\u308c\u308b\u3002\r\nstatic AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,\r\n                                          const std::string &amp;VarName) {\r\n  IRBuilder&lt;&gt; TmpB(&amp;TheFunction-&gt;getEntryBlock(),\r\n                 TheFunction-&gt;getEntryBlock().begin());\r\n  return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,\r\n                           VarName.c_str());\r\n}<\/pre>\n<p>\u3053\u306e\u5c11\u3005\u5909\u308f\u3063\u305f\u30b3\u30fc\u30c9\u306f\u3001entry\u30d6\u30ed\u30c3\u30af\u306e\u6700\u521d\u306e\u547d\u4ee4\uff08.begin()\uff09\u3092\u6307\u3059IRBuilder\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f\u6210\u3059\u308b\u3002<br \/>\n\u305d\u3057\u305f\u3089\u3001\u6307\u5b9a\u3055\u308c\u305f\u540d\u524d\u3067alloca\u3092\u751f\u6210\u3057\u3001\u305d\u308c\u3092\u8fd4\u3059\u3002<br \/>\n\u4e07\u83ef\u93e1\u306b\u304a\u3051\u308b\u5024\u306f\u5168\u3066double\u306a\u306e\u3067\u3001\u5909\u6570\u306e\u578b\u3092\u6e21\u3059\u5fc5\u8981\u306f\u306a\u3044\u3002<\/p>\n<p>\u3053\u308c\u3092\u8e0f\u307e\u3048\u3001\u6211\u3005\u304c\u6700\u521d\u306b\u52a0\u3048\u305f\u3044\u6a5f\u80fd\u4e0a\u306e\u5909\u66f4\u70b9\u306f\u3001\u5909\u6570\u306e\u53c2\u7167\u3067\u3042\u308b\u3002<br \/>\n\u6211\u3005\u306e\u8a08\u753b\u3067\u306f\u3001\u5909\u6570\u306f\u30b9\u30bf\u30c3\u30af\u4e0a\u306b\u4fdd\u6301\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u5909\u6570\u306e\u53c2\u7167\u306b\u3064\u3044\u3066\u306e\u30b3\u30fc\u30c9\u751f\u6210\u3068\u3057\u3066\u3001\u5b9f\u969b\u306b\u30b9\u30bf\u30c3\u30af\u30b9\u30ed\u30c3\u30c8\u304b\u3089\u30ed\u30fc\u30c9\u3059\u308b\u547d\u4ee4\u306e\u751f\u6210\u304c\u5fc5\u8981\u3068\u306a\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Value *VariableExprAST::Codegen() {\r\n  \/\/ \u95a2\u6570\u306e\u4e2d\u3067\u3053\u306e\u5909\u6570\u3092\u63a2\u3059\u3002\r\n  Value *V = NamedValues&#x5B;Name];\r\n  if (V == 0) return ErrorV(&quot;Unknown variable name&quot;);\r\n\r\n  \/\/ \u5024\u3092\u30ed\u30fc\u30c9\u3059\u308b\u3002\r\n  return Builder.CreateLoad(V, Name.c_str());\r\n}<\/pre>\n<p>\u898b\u3066\u306e\u901a\u308a\u3001\u5b9f\u306b\u7c21\u5358\u306a\u30b3\u30fc\u30c9\u3067\u3042\u308b\u3002<br \/>\n\u305d\u3057\u305f\u3089\u6b21\u306f\u3001alloca\u3092\u4f7f\u3046\u3088\u3046\u3001\u5909\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u90e8\u5206\u3092\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u305d\u308c\u306b\u3064\u3044\u3066\u306f\u307e\u305a\u3001ForExprAST::Codegen\u304b\u3089\u59cb\u3081\u308b\u3002\uff08&#8221;\u5168\u30b3\u30fc\u30c9&#8221;\u306e\u7bc0\u3067\u3001\u7701\u7565\u3055\u308c\u3066\u306a\u3044\u30b3\u30fc\u30c9\u3092\u898b\u308b\u3079\u3057\u3002\uff09<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Function *TheFunction = Builder.GetInsertBlock()-&gt;getParent();\r\n\r\n\/\/ \u5909\u6570\u306e\u305f\u3081\u306ealloca\u3092entry\u30d6\u30ed\u30c3\u30af\u306b\u751f\u6210\u3059\u308b\u3002\r\nAllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);\r\n\r\n\/\/ \u30b9\u30b3\u30fc\u30d7\u4e2d\u306e\u5909\u6570\u7121\u3057\u3067\u3001\u307e\u305aStart\u306e\u30b3\u30fc\u30c9\u3092\u751f\u6210\u3059\u308b\u3002\r\nValue *StartVal = Start-&gt;Codegen();\r\nif (StartVal == 0) return 0;\r\n\r\n\/\/ alloca\u306e\u4e2d\u306b\u5024\u3092\u683c\u7d0d\u3059\u308b\u3002\r\nBuilder.CreateStore(StartVal, Alloca);\r\n...\r\n\r\n\/\/ \u7d42\u4e86\u6761\u4ef6\u3092\u8a08\u7b97\u3059\u308b\u3002\r\nValue *EndCond = End-&gt;Codegen();\r\nif (EndCond == 0) return EndCond;\r\n\r\n\/\/ alloca\u3092\u518d\u8aad\u307f\u8fbc\u307f\u3057\u3001\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3057\u3001\u30bb\u30c3\u30c8\u3059\u308b\u3002\r\n\/\/ \u3053\u308c\u306f\u3001\u30eb\u30fc\u30d7\u306e\u672c\u6587\u304c\u5909\u6570\u3092\u5909\u5316\u3055\u305b\u308b\u5834\u5408\u3092\u51e6\u7406\u3059\u308b\u3002\r\nValue *CurVar = Builder.CreateLoad(Alloca);\r\nValue *NextVar = Builder.CreateFAdd(CurVar, StepVal, &quot;nextvar&quot;);\r\nBuilder.CreateStore(NextVar, Alloca);\r\n...<\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u306f\u3001\u5909\u66f4\u53ef\u80fd\u306a\u5909\u6570\u3092\u53ef\u80fd\u306b\u3059\u308b\u4ee5\u524d\u306e\u30b3\u30fc\u30c9\u3068\u540c\u4e00\u3068\u3044\u3063\u3066\u3082\u3044\u3044\u304f\u3089\u3044\u4f3c\u3066\u3044\u308b\u3002<br \/>\n\u5927\u304d\u306a\u9055\u3044\u306f\u3001\u3082\u3046phi\u30ce\u30fc\u30c9\u3092\u69cb\u7bc9\u3059\u308b\u5fc5\u8981\u304c\u306a\u304f\u3001\u305d\u3057\u3066\u5909\u6570\u3078\u306e\u30a2\u30af\u30bb\u30b9\u306b\u5fc5\u8981\u306b\u5fdc\u3058\u3066load\/store\u3092\u4f7f\u3046\u3068\u3044\u3046\u70b9\u3060\u3051\u3067\u3042\u308b\u3002<\/p>\n<p>\u5f15\u6570\u306e\u5909\u66f4\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u305f\u3081\u306b\u3001\u305d\u308c\u3089\u306b\u95a2\u3057\u3066\u3082alloca\u3092\u751f\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u305d\u306e\u305f\u3081\u306e\u30b3\u30fc\u30c9\u3082\u307e\u305f\u3001\u5b9f\u306b\u30b7\u30f3\u30d7\u30eb\u3067\u3042\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/\/ CreateArgumentAllocas - \u5404\u5f15\u6570\u306e\u305f\u3081\u306ealloca\u3092\u751f\u6210\u3057\u3001\u305d\u3057\u3066\u305d\u308c\u3092\u30b7\u30f3\u30dc\u30eb\u30c6\u30fc\u30d6\u30eb\u306b\u767b\u9332\u3059\u308b\u3002\r\n\/\/\/ \u3053\u308c\u306b\u3088\u3063\u3066\u3001\u5f15\u6570\u3078\u306e\u53c2\u7167\u304c\u53ef\u80fd\u3068\u306a\u308b\u3002\r\nvoid PrototypeAST::CreateArgumentAllocas(Function *F) {\r\n  Function::arg_iterator AI = F-&gt;arg_begin();\r\n  for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) {\r\n    \/\/ \u3053\u306e\u5909\u6570\u306e\u305f\u3081\u306ealloca\u3092\u751f\u6210\u3059\u308b\u3002\r\n    AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args&#x5B;Idx]);\r\n\r\n    \/\/ alloca\u306e\u4e2d\u306b\u521d\u671f\u5024\u3092\u4fdd\u5b58\u3059\u308b\u3002\r\n    Builder.CreateStore(AI, Alloca);\r\n\r\n    \/\/ \u5f15\u6570\u3092\u30b7\u30f3\u30dc\u30eb\u30c6\u30fc\u30d6\u30eb\u306b\u8ffd\u52a0\u3059\u308b\u3002\r\n    NamedValues&#x5B;Args&#x5B;Idx]] = Alloca;\r\n  }\r\n}<\/pre>\n<p>\u5404\u5f15\u6570\u306b\u3064\u3044\u3066\u3001alloca\u3092\u4f5c\u6210\u3057\u3001\u95a2\u6570\u3078\u5165\u529b\u3055\u308c\u305f\u5024\u3092alloca\u306e\u4e2d\u306b\u4fdd\u5b58\u3057\u3001\u5f15\u6570\u306e\u30e1\u30e2\u30ea\u306e\u4f4d\u7f6e\u3068\u3057\u3066alloca\u3092\u767b\u9332\u3059\u308b\u3002<br \/>\nFunctionAST::Codegen\u304c\u95a2\u6570\u306eentry\u30d6\u30ed\u30c3\u30af\u3092\u6e96\u5099\u3057\u305f\u76f4\u5f8c\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306fFunctionAST::Codegen\u306b\u3088\u3063\u3066\u8d77\u52d5\u3055\u308c\u308b\u3002<\/p>\n<p>\u3042\u3068\u6b8b\u3063\u305f\u306e\u306f\u3001mem2reg\u30d1\u30b9\u306e\u8ffd\u52a0\u3067\u3042\u308b\u3002<br \/>\n\u3053\u308c\u306b\u3088\u3063\u3066\u3001\u52b9\u7387\u7684\u306a\u30b3\u30fc\u30c9\u306e\u751f\u6210\u6a5f\u80fd\u3092\u4eca\u4e00\u5ea6\u5f97\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: phi\u30ce\u30fc\u30c9\u3092\u4f7f\u308f\u305a\u3001alloca\/load\/store\u3067\u5909\u6570\u3092\u64cd\u4f5c\u3059\u308b\u3088\u3046\u306b\u3059\u308b\u3068\u52b9\u7387\u304c\u60aa\u304f\u306a\u308b\u3002&#8221;\u4eca\u4e00\u5ea6\u5f97\u308b\u201d\u3068\u3044\u3046\u306e\u306f\u3001mem2reg\u30d1\u30b9\u3067\u305d\u306e\u52b9\u7387\u3092\u53d6\u308a\u623b\u3059\u3068\u3044\u3046\u610f\u5473\u3002<\/em><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ \u30aa\u30d7\u30c6\u30a3\u30de\u30a4\u30b6\u306e\u30d1\u30a4\u30d7\u30e9\u30a4\u30f3\u3092\u6e96\u5099\u3059\u308b\u3002\r\n\/\/ \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u3069\u3046\u3084\u3063\u3066\u914d\u7f6e\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u306e\u60c5\u5831\u3092\u307e\u305a\u767b\u9332\u3059\u308b\u3002\r\nOurFPM.add(new DataLayout(*TheExecutionEngine-&gt;getDataLayout()));\r\n\/\/ alloca\u3092\u30ec\u30b8\u30b9\u30bf\u5316\u3059\u308b\u3002\r\nOurFPM.add(createPromoteMemoryToRegisterPass());\r\n\/\/ \u7c21\u5358\u306a\u30d4\u30fc\u30d7\u30db\u30fc\u30eb\u6700\u9069\u5316\uff08\u8997\u304d\u7a74\u6700\u9069\u5316\uff09\u3068\u30d3\u30c3\u30c8\u64cd\u4f5c\u6700\u9069\u5316\u3092\u884c\u308f\u305b\u308b\u3002\r\nOurFPM.add(createInstructionCombiningPass());\r\n\/\/ \u5f0f\u306e\u518d\u7d50\u5408\u3002\r\nOurFPM.add(createReassociatePass());<\/pre>\n<p>mem2reg\u6700\u9069\u5316\u306e\u5b9f\u884c\u524d\u3068\u5f8c\u3067\u3001\u30b3\u30fc\u30c9\u304c\u3069\u306e\u3088\u3046\u306b\u306a\u308b\u304b\u3092\u898b\u308b\u306e\u306f\u9762\u767d\u3044\u3002<br \/>\n\u4ee5\u4e0b\u306f\u3001\u518d\u5e30\u7684\u306afib\u95a2\u6570\u306e\u30d3\u30d5\u30a9\u30fc\u30fb\u30a2\u30d5\u30bf\u30fc\u306e\u4f8b\u3067\u3042\u308b\u3002<br \/>\n\u307e\u305a\u6700\u9069\u5316\u524d\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">define double @fib(double %x) {\r\nentry:\r\n  %x1 = alloca double\r\n  store double %x, double* %x1\r\n  %x2 = load double* %x1\r\n  %cmptmp = fcmp ult double %x2, 3.000000e+00\r\n  %booltmp = uitofp i1 %cmptmp to double\r\n  %ifcond = fcmp one double %booltmp, 0.000000e+00\r\n  br i1 %ifcond, label %then, label %else\r\n\r\nthen:       ; preds = %entry\r\n  br label %ifcont\r\n\r\nelse:       ; preds = %entry\r\n  %x3 = load double* %x1\r\n  %subtmp = fsub double %x3, 1.000000e+00\r\n  %calltmp = call double @fib(double %subtmp)\r\n  %x4 = load double* %x1\r\n  %subtmp5 = fsub double %x4, 2.000000e+00\r\n  %calltmp6 = call double @fib(double %subtmp5)\r\n  %addtmp = fadd double %calltmp, %calltmp6\r\n  br label %ifcont\r\n\r\nifcont:     ; preds = %else, %then\r\n  %iftmp = phi double &#x5B; 1.000000e+00, %then ], &#x5B; %addtmp, %else ]\r\n  ret double %iftmp\r\n}<\/pre>\n<p>\u3053\u3053\u3067\u306f\u5909\u6570\u306f\u3072\u3068\u3064\u3060\u3051\u3067\u3042\u308b\uff08\u5f15\u6570x\uff09\u304c\u3001\u6211\u3005\u304c\u975e\u5e38\u306b\u5358\u7d14\u306a\u30b3\u30fc\u30c9\u751f\u6210\u6226\u7565\u3092\u4f7f\u3063\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u308b\u3002<br \/>\nentry\u30d6\u30ed\u30c3\u30af\u3067\u3001alloca\u306f\u751f\u6210\u3055\u308c\u3001\u305d\u3053\u306b\u521d\u671f\u5024\u304c\u4fdd\u5b58\u3055\u308c\u308b\u3002<br \/>\n\u5909\u6570\u3078\u306e\u53c2\u7167\u3067\u306f\u3001\u6bce\u56de\u30b9\u30bf\u30c3\u30af\u304b\u3089\u518d\u8aad\u307f\u8fbc\u307f\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\nif\/then\/else\u5f0f\u3092\u4fee\u6b63\u3057\u3066\u306a\u3044\u306e\u3067\u3001\u4f9d\u7136\u3068\u3057\u3066phi\u30ce\u30fc\u30c9\u304c\u633f\u5165\u3055\u308c\u3066\u3044\u308b\u70b9\u306b\u3082\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3042\u308b\u3002<br \/>\n\u3053\u3053\u3067\u3082alloca\u3092\u4f7f\u3046\u3088\u3046\u306b\u3059\u308b\u4e8b\u3082\u3067\u304d\u308b\u304c\u3001phi\u30ce\u30fc\u30c9\u3092\u751f\u6210\u3059\u308b\u307b\u3046\u304c\u7c21\u5358\u306a\u306e\u3067\u305d\u306e\u307e\u307e\u306b\u3057\u3066\u3042\u308b\u3002<\/p>\n<p>\u3053\u308c\u304cmem2reg\u30d1\u30b9\u306b\u3088\u308b\u6700\u9069\u5316\u5f8c\u3067\u3042\u308b\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">define double @fib(double %x) {\r\nentry:\r\n  %cmptmp = fcmp ult double %x, 3.000000e+00\r\n  %booltmp = uitofp i1 %cmptmp to double\r\n  %ifcond = fcmp one double %booltmp, 0.000000e+00\r\n  br i1 %ifcond, label %then, label %else\r\n\r\nthen:\r\n  br label %ifcont\r\n\r\nelse:\r\n  %subtmp = fsub double %x, 1.000000e+00\r\n  %calltmp = call double @fib(double %subtmp)\r\n  %subtmp5 = fsub double %x, 2.000000e+00\r\n  %calltmp6 = call double @fib(double %subtmp5)\r\n  %addtmp = fadd double %calltmp, %calltmp6\r\n  br label %ifcont\r\n\r\nifcont:     ; preds = %else, %then\r\n  %iftmp = phi double &#x5B; 1.000000e+00, %then ], &#x5B; %addtmp, %else ]\r\n  ret double %iftmp\r\n}<\/pre>\n<p>\u3053\u308c\u306f\u5909\u6570\u306e\u518d\u5b9a\u7fa9\u3092\u542b\u307e\u306a\u3044\u306e\u3067\u3001mem2reg\u306b\u3068\u3063\u3066\u53d6\u308b\u306b\u8db3\u3089\u306a\u3044\u306a\u30b1\u30fc\u30b9\u3067\u3042\u308b\u3002<br \/>\n\u975e\u52b9\u7387\u306a\u547d\u4ee4\u306e\u633f\u5165\u3092\u3057\u3066\u3082\u5927\u4e08\u592b\u3060\u3001\u3068\u3044\u3046\u4e8b\u3092\u793a\u3059\u305f\u3081\u306b\u3053\u306e\u30b3\u30fc\u30c9\u3092\u793a\u3057\u3066\u3044\u308b\u3002<\/p>\n<p>\u6b8b\u308a\u306e\u6700\u9069\u5316\u3092\u5b9f\u884c\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">define double @fib(double %x) {\r\nentry:\r\n  %cmptmp = fcmp ult double %x, 3.000000e+00\r\n  %booltmp = uitofp i1 %cmptmp to double\r\n  %ifcond = fcmp ueq double %booltmp, 0.000000e+00\r\n  br i1 %ifcond, label %else, label %ifcont\r\n\r\nelse:\r\n  %subtmp = fsub double %x, 1.000000e+00\r\n  %calltmp = call double @fib(double %subtmp)\r\n  %subtmp5 = fsub double %x, 2.000000e+00\r\n  %calltmp6 = call double @fib(double %subtmp5)\r\n  %addtmp = fadd double %calltmp, %calltmp6\r\n  ret double %addtmp\r\n\r\nifcont:\r\n  ret double 1.000000e+00\r\n}<\/pre>\n<p>simplifycfg\u30d1\u30b9\u306b\u3088\u3063\u3066\u3001ret\u547d\u4ee4\u304celse\u30d6\u30ed\u30c3\u30af\u306e\u6700\u5f8c\u306b\u3082\u8ffd\u52a0\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\n\u3053\u308c\u306b\u3088\u3063\u3066\u3001\u3044\u304f\u3089\u304b\u306e\u679d\u5206\u304b\u308c\u3084phi\u30ce\u30fc\u30c9\u304c\u524a\u6e1b\u53ef\u80fd\u3068\u306a\u308b\u3002<\/p>\n<p>\u4ee5\u4e0a\u3067\u3001\u30b7\u30f3\u30dc\u30eb\u30c6\u30fc\u30d6\u30eb\u306e\u5168\u3066\u306e\u53c2\u7167\u306f\u3001\u30b9\u30bf\u30c3\u30af\u5909\u6570\u3092\u4f7f\u3046\u3088\u3046\u306b\u306a\u3063\u305f\u3002<br \/>\n\u6b21\u306f\u3001\u4ee3\u5165\u6f14\u7b97\u5b50\uff08assignment operator\uff09\u3092\u8ffd\u52a0\u3057\u3088\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p><a href=\"https:\/\/peta.okechan.net\/blog\/archives\/2836\" title=\"LLVM\u306b\u3088\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u5b9f\u88c5\">LLVM\u306b\u3088\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u5b9f\u88c5\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u65e5\u672c\u8a9e\u8a33<\/a><br \/>\n\u7b2c7\u7ae0 \u4e07\u83ef\u93e1: \u8a00\u8a9e\u306e\u62e1\u5f35: \u5909\u66f4\u53ef\u80fd\u306a\u5909\u6570\uff08Mutable Variables\uff09<br \/>\n<a href=\"http:\/\/llvm.org\/docs\/tutorial\/LangImpl07.html#adjusting-existing-variables-for-mutation\" target=\"_blank\">\u7b2c5\u7bc0 \u5909\u66f4\u53ef\u80fd\u306b\u3059\u308b\u305f\u3081\u306e\u8abf\u6574<\/a><\/p>\n<p>\u4e07\u83ef\u93e1\u306e\u30b7\u30f3\u30dc\u30eb\u30c6\u30fc\u30d6\u30eb\u306f\u3001NamedValues\u30de\u30c3\u30d7\u306b\u3088\u3063\u3066\u30b3\u30fc\u30c9\u751f\u6210\u6642\u306b\u4f7f\u308f\u308c\u3066\u3044\u308b\u3002<br \/>\n\u73fe\u72b6\u3067\u306f\u3053\u306e\u30de\u30c3\u30d7\u306f\u3001\u540d\u524d\u304c\u4ed8\u3044\u305f\u500d\u7cbe\u5ea6\u306e\u5909\u6570\u3092\u4fdd\u6301\u3059\u308bLLVM Value*\u3092\u8ffd\u8de1\u3057\u7d9a\u3051\u308b\u3002<br \/>\n\u5909\u6570\u306e\u5909\u66f4\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u305f\u3081\u306b\u3001\u5909\u6570\u306e\u30e1\u30e2\u30ea\u306e\u4f4d\u7f6e\u3092NamedValues\u3067\u4fdd\u6301\u3059\u308b\u3088\u3046\u3001\u5c11\u3057\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u3053\u306e\u5909\u66f4\u306f\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3002<br \/>\n\u30b3\u30fc\u30c9\u306e\u69cb\u9020\u306f\u5909\u308f\u308b\u304c\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u632f\u308b\u821e\u3044\u306f\u5909\u308f\u3089\u306a\u3044\u3002<br \/>\n\u3053\u308c\u4ee5\u964d\u52a0\u3048\u3066\u3044\u304f\u5909\u66f4\u306f\u3001\u5168\u3066\u4e07\u83ef\u93e1\u306e\u30b3\u30fc\u30c9\u751f\u6210\u5668\u306e\u306a\u304b\u3067\u5206\u96e2\u3055\u308c\u3066\u3044\u308b\u3002<\/p>\n<p>\u3053\u306e\u6642\u70b9\u3067\u306f\u3001\u4e07\u83ef\u93e1\u306f2\u7a2e\u985e\u306e\u5909\u6570\u306e\u307f\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u308b\u3002<br \/>\n\u95a2\u6570\u306e\u5f15\u6570\u3068\u3001for\u30eb\u30fc\u30d7\u306e\u305f\u3081\u306e\u5e30\u7d0d\u5909\u6570\u3067\u3042\u308b\u3002<br \/>\n\u4e00\u8cab\u6027\u306e\u305f\u3081\u3001\u30e6\u30fc\u30b6\u5b9a\u7fa9\u5909\u6570\u306b\u52a0\u3048\u3066\u3001\u305d\u308c\u3089\u306e\u5909\u6570\u306e\u5909\u66f4\u3082\u53ef\u80fd\u306b\u3057\u3066\u3044\u304f\u3002<br \/>\n\u3064\u307e\u308a\u3001\u305d\u308c\u3089\u4e21\u65b9\u3068\u3082\u3001\u30e1\u30e2\u30ea\u306e\u4f4d\u7f6e\u304c\u5fc5\u8981\u3067\u3042\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3042\u308b\u3002<\/p>\n<p>\u4e07\u83ef\u93e1\u306e\u6539\u826f\u3092\u59cb\u3081\u308b\u306b\u3042\u305f\u3063\u3066\u3001NamedValues\u30de\u30c3\u30d7\u3092\u3001Value*\u3058\u3083\u306a\u304f\u3066AllocaInst*\u3092\u6307\u3059\u3088\u3046\u5909\u66f4\u3059\u308b\u3002<br \/>\n\u3053\u3046\u3057\u3066\u304a\u3051\u3070\u3001\u3042\u3068\u306fC++\u306e\u30b3\u30f3\u30d1\u30a4\u30e9\u304c\u30b3\u30fc\u30c9\u306e\u5909\u66f4\u3059\u3079\u304d\u7b87\u6240\u3092\u6559\u3048\u3066\u304f\u308c\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: \u30b3\u30f3\u30d1\u30a4\u30eb\u30a8\u30e9\u30fc\u306e\u5185\u5bb9\u3092\u898b\u308c\u3070\u5909\u66f4\u3059\u3079\u304d\u7b87\u6240\u304c\u5206\u304b\u308b\u3002<\/em><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">static std::map&lt;std::string, AllocaInst*&gt; NamedValues;<\/pre>\n<p>\u305d\u3057\u305f\u3089\u3001alloca\u547d\u4ee4\u3092\u751f\u6210\u3059\u308b\u3088\u3046\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u78ba\u5b9f\u306balloca\u304c\u95a2\u6570\u306eentry\u30d6\u30ed\u30c3\u30af\u306b\u751f\u6210\u3055\u308c\u308b\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306e\u30d8\u30eb\u30d1\u95a2\u6570\u3092\u4f7f\u3046\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/\/ CreateEntryBlockAlloca - alloca\u547d\u4ee4\u3092\u95a2\u6570\u306eentry\u30d6\u30ed\u30c3\u30af\u306b\u751f\u6210\u3059\u308b\u3002\r\n\/\/\/ \u3053\u308c\u306f\u5909\u66f4\u53ef\u80fd\u306a\u5909\u6570\u306a\u3069\u306e\u305f\u3081\u306b\u4f7f\u308f\u308c\u308b\u3002\r\nstatic AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,\r\n                                          const std::string &amp;VarName) {\r\n  IRBuilder&lt;&gt; TmpB(&amp;TheFunction-&gt;getEntryBlock(),\r\n                 TheFunction-&gt;getEntryBlock().begin());\r\n  return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,\r\n                           VarName.c_str());\r\n}<\/pre>\n<p>\u3053\u306e\u5c11\u3005\u5909\u308f\u3063\u305f\u30b3\u30fc\u30c9\u306f\u3001entry\u30d6\u30ed\u30c3\u30af\u306e\u6700\u521d\u306e\u547d\u4ee4\uff08.begin()\uff09\u3092\u6307\u3059IRBuilder\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f\u6210\u3059\u308b\u3002<br \/>\n\u305d\u3057\u305f\u3089\u3001\u6307\u5b9a\u3055\u308c\u305f\u540d\u524d\u3067alloca\u3092\u751f\u6210\u3057\u3001\u305d\u308c\u3092\u8fd4\u3059\u3002<br \/>\n\u4e07\u83ef\u93e1\u306b\u304a\u3051\u308b\u5024\u306f\u5168\u3066double\u306a\u306e\u3067\u3001\u5909\u6570\u306e\u578b\u3092\u6e21\u3059\u5fc5\u8981\u306f\u306a\u3044\u3002<\/p>\n<p>\u3053\u308c\u3092\u8e0f\u307e\u3048\u3001\u6211\u3005\u304c\u6700\u521d\u306b\u52a0\u3048\u305f\u3044\u6a5f\u80fd\u4e0a\u306e\u5909\u66f4\u70b9\u306f\u3001\u5909\u6570\u306e\u53c2\u7167\u3067\u3042\u308b\u3002<br \/>\n\u6211\u3005\u306e\u8a08\u753b\u3067\u306f\u3001\u5909\u6570\u306f\u30b9\u30bf\u30c3\u30af\u4e0a\u306b\u4fdd\u6301\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u5909\u6570\u306e\u53c2\u7167\u306b\u3064\u3044\u3066\u306e\u30b3\u30fc\u30c9\u751f\u6210\u3068\u3057\u3066\u3001\u5b9f\u969b\u306b\u30b9\u30bf\u30c3\u30af\u30b9\u30ed\u30c3\u30c8\u304b\u3089\u30ed\u30fc\u30c9\u3059\u308b\u547d\u4ee4\u306e\u751f\u6210\u304c\u5fc5\u8981\u3068\u306a\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Value *VariableExprAST::Codegen() {\r\n  \/\/ \u95a2\u6570\u306e\u4e2d\u3067\u3053\u306e\u5909\u6570\u3092\u63a2\u3059\u3002\r\n  Value *V = NamedValues&#x5B;Name];\r\n  if (V == 0) return ErrorV(&quot;Unknown variable name&quot;);\r\n\r\n  \/\/ \u5024\u3092\u30ed\u30fc\u30c9\u3059\u308b\u3002\r\n  return Builder.CreateLoad(V, Name.c_str());\r\n}<\/pre>\n<p>\u898b\u3066\u306e\u901a\u308a\u3001\u5b9f\u306b\u7c21\u5358\u306a\u30b3\u30fc\u30c9\u3067\u3042\u308b\u3002<br \/>\n\u305d\u3057\u305f\u3089\u6b21\u306f\u3001alloca\u3092\u4f7f\u3046\u3088\u3046\u3001\u5909\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u90e8\u5206\u3092\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u305d\u308c\u306b\u3064\u3044\u3066\u306f\u307e\u305a\u3001ForExprAST::Codegen\u304b\u3089\u59cb\u3081\u308b\u3002\uff08&#8221;\u5168\u30b3\u30fc\u30c9&#8221;\u306e\u7bc0\u3067\u3001\u7701\u7565\u3055\u308c\u3066\u306a\u3044\u30b3\u30fc\u30c9\u3092\u898b\u308b\u3079\u3057\u3002\uff09<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Function *TheFunction = Builder.GetInsertBlock()-&gt;getParent();\r\n\r\n\/\/ \u5909\u6570\u306e\u305f\u3081\u306ealloca\u3092entry\u30d6\u30ed\u30c3\u30af\u306b\u751f\u6210\u3059\u308b\u3002\r\nAllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);\r\n\r\n\/\/ \u30b9\u30b3\u30fc\u30d7\u4e2d\u306e\u5909\u6570\u7121\u3057\u3067\u3001\u307e\u305aStart\u306e\u30b3\u30fc\u30c9\u3092\u751f\u6210\u3059\u308b\u3002\r\nValue *StartVal = Start-&gt;Codegen();\r\nif (StartVal == 0) return 0;\r\n\r\n\/\/ alloca\u306e\u4e2d\u306b\u5024\u3092\u683c\u7d0d\u3059\u308b\u3002\r\nBuilder.CreateStore(StartVal, Alloca);\r\n...\r\n\r\n\/\/ \u7d42\u4e86\u6761\u4ef6\u3092\u8a08\u7b97\u3059\u308b\u3002\r\nValue *EndCond = End-&gt;Codegen();\r\nif (EndCond == 0) return EndCond;\r\n\r\n\/\/ alloca\u3092\u518d\u8aad\u307f\u8fbc\u307f\u3057\u3001\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3057\u3001\u30bb\u30c3\u30c8\u3059\u308b\u3002\r\n\/\/ \u3053\u308c\u306f\u3001\u30eb\u30fc\u30d7\u306e\u672c\u6587\u304c\u5909\u6570\u3092\u5909\u5316\u3055\u305b\u308b\u5834\u5408\u3092\u51e6\u7406\u3059\u308b\u3002\r\nValue *CurVar = Builder.CreateLoad(Alloca);\r\nValue *NextVar = Builder.CreateFAdd(CurVar, StepVal, &quot;nextvar&quot;);\r\nBuilder.CreateStore(NextVar, Alloca);\r\n...<\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u306f\u3001\u5909\u66f4\u53ef\u80fd\u306a\u5909\u6570\u3092\u53ef\u80fd\u306b\u3059\u308b\u4ee5\u524d\u306e\u30b3\u30fc\u30c9\u3068\u540c\u4e00\u3068\u3044\u3063\u3066\u3082\u3044\u3044\u304f\u3089\u3044\u4f3c\u3066\u3044\u308b\u3002<br \/>\n\u5927\u304d\u306a\u9055\u3044\u306f\u3001\u3082\u3046phi\u30ce\u30fc\u30c9\u3092\u69cb\u7bc9\u3059\u308b\u5fc5\u8981\u304c\u306a\u304f\u3001\u305d\u3057\u3066\u5909\u6570\u3078\u306e\u30a2\u30af\u30bb\u30b9\u306b\u5fc5\u8981\u306b\u5fdc\u3058\u3066load\/store\u3092\u4f7f\u3046\u3068\u3044\u3046\u70b9\u3060\u3051\u3067\u3042\u308b\u3002<\/p>\n<p>\u5f15\u6570\u306e\u5909\u66f4\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u305f\u3081\u306b\u3001\u305d\u308c\u3089\u306b\u95a2\u3057\u3066\u3082alloca\u3092\u751f\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u305d\u306e\u305f\u3081\u306e\u30b3\u30fc\u30c9\u3082\u307e\u305f\u3001\u5b9f\u306b\u30b7\u30f3\u30d7\u30eb\u3067\u3042\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/\/ CreateArgumentAllocas - \u5404\u5f15\u6570\u306e\u305f\u3081\u306ealloca\u3092\u751f\u6210\u3057\u3001\u305d\u3057\u3066\u305d\u308c\u3092\u30b7\u30f3\u30dc\u30eb\u30c6\u30fc\u30d6\u30eb\u306b\u767b\u9332\u3059\u308b\u3002\r\n\/\/\/ \u3053\u308c\u306b\u3088\u3063\u3066\u3001\u5f15\u6570\u3078\u306e\u53c2\u7167\u304c\u53ef\u80fd\u3068\u306a\u308b\u3002\r\nvoid PrototypeAST::CreateArgumentAllocas(Function *F) {\r\n  Function::arg_iterator AI = F-&gt;arg_begin();\r\n  for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) {\r\n    \/\/ \u3053\u306e\u5909\u6570\u306e\u305f\u3081\u306ealloca\u3092\u751f\u6210\u3059\u308b\u3002\r\n    AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args&#x5B;Idx]);\r\n\r\n    \/\/ alloca\u306e\u4e2d\u306b\u521d\u671f\u5024\u3092\u4fdd\u5b58\u3059\u308b\u3002\r\n    Builder.CreateStore(AI, Alloca);\r\n\r\n    \/\/ \u5f15\u6570\u3092\u30b7\u30f3\u30dc\u30eb\u30c6\u30fc\u30d6\u30eb\u306b\u8ffd\u52a0\u3059\u308b\u3002\r\n    NamedValues&#x5B;Args&#x5B;Idx]] = Alloca;\r\n  }\r\n}<\/pre>\n<p>\u5404\u5f15\u6570\u306b\u3064\u3044\u3066\u3001alloca\u3092\u4f5c\u6210\u3057\u3001\u95a2\u6570\u3078\u5165\u529b\u3055\u308c\u305f\u5024\u3092alloca\u306e\u4e2d\u306b\u4fdd\u5b58\u3057\u3001\u5f15\u6570\u306e\u30e1\u30e2\u30ea\u306e\u4f4d\u7f6e\u3068\u3057\u3066alloca\u3092\u767b\u9332\u3059\u308b\u3002<br \/>\nFunctionAST::Codegen\u304c\u95a2\u6570\u306eentry\u30d6\u30ed\u30c3\u30af\u3092\u6e96\u5099\u3057\u305f\u76f4\u5f8c\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306fFunctionAST::Codegen\u306b\u3088\u3063\u3066\u8d77\u52d5\u3055\u308c\u308b\u3002<\/p>\n<p>\u3042\u3068\u6b8b\u3063\u305f\u306e\u306f\u3001mem2reg\u30d1\u30b9\u306e\u8ffd\u52a0\u3067\u3042\u308b\u3002<br \/>\n\u3053\u308c\u306b\u3088\u3063\u3066\u3001\u52b9\u7387\u7684\u306a\u30b3\u30fc\u30c9\u306e\u751f\u6210\u6a5f\u80fd\u3092\u4eca\u4e00\u5ea6\u5f97\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: phi\u30ce\u30fc\u30c9\u3092\u4f7f\u308f\u305a\u3001alloca\/load\/store\u3067\u5909\u6570\u3092\u64cd\u4f5c\u3059\u308b\u3088\u3046\u306b\u3059\u308b\u3068\u52b9\u7387\u304c\u60aa\u304f\u306a\u308b\u3002&#8221;\u4eca\u4e00\u5ea6\u5f97\u308b\u201d\u3068\u3044\u3046\u306e\u306f\u3001mem2reg\u30d1\u30b9\u3067\u305d\u306e\u52b9\u7387\u3092\u53d6\u308a\u623b\u3059\u3068\u3044\u3046\u610f\u5473\u3002<\/em><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ \u30aa\u30d7\u30c6\u30a3\u30de\u30a4\u30b6\u306e\u30d1\u30a4\u30d7\u30e9\u30a4\u30f3\u3092\u6e96\u5099\u3059\u308b\u3002\r\n\/\/ \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u3069\u3046\u3084\u3063\u3066\u914d\u7f6e\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u306e\u60c5\u5831\u3092\u307e\u305a\u767b\u9332\u3059\u308b\u3002\r\nOurFPM.add(new DataLayout(*TheExecutionEngine-&gt;getDataLayout()));\r\n\/\/ alloca\u3092\u30ec\u30b8\u30b9\u30bf\u5316\u3059\u308b\u3002\r\nOurFPM.add(createPromoteMemoryToRegisterPass());\r\n\/\/ \u7c21\u5358\u306a\u30d4\u30fc\u30d7\u30db\u30fc\u30eb\u6700\u9069\u5316\uff08\u8997\u304d\u7a74\u6700\u9069\u5316\uff09\u3068\u30d3\u30c3\u30c8\u64cd\u4f5c\u6700\u9069\u5316\u3092\u884c\u308f\u305b\u308b\u3002\r\nOurFPM.add(createInstructionCombiningPass());\r\n\/\/ \u5f0f\u306e\u518d\u7d50\u5408\u3002\r\nOurFPM.add(createReassociatePass());<\/pre>\n<p>mem2reg\u6700\u9069\u5316\u306e\u5b9f\u884c\u524d\u3068\u5f8c\u3067\u3001\u30b3\u30fc\u30c9\u304c\u3069\u306e\u3088\u3046\u306b\u306a\u308b\u304b\u3092\u898b\u308b\u306e\u306f\u9762\u767d\u3044\u3002<br \/>\n\u4ee5\u4e0b\u306f\u3001\u518d\u5e30\u7684\u306afib\u95a2\u6570\u306e\u30d3\u30d5\u30a9\u30fc\u30fb\u30a2\u30d5\u30bf\u30fc\u306e\u4f8b\u3067\u3042\u308b\u3002<br \/>\n\u307e\u305a\u6700\u9069\u5316\u524d\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">define double @fib(double %x) {\r\nentry:\r\n  %x1 = alloca double\r\n  store double %x, double* %x1\r\n  %x2 = load double* %x1\r\n  %cmptmp = fcmp ult double %x2, 3.000000e+00\r\n  %booltmp = uitofp i1 %cmptmp to double\r\n  %ifcond = fcmp one double %booltmp, 0.000000e+00\r\n  br i1 %ifcond, label %then, label %else\r\n\r\nthen:       ; preds = %entry\r\n  br label %ifcont\r\n\r\nelse:       ; preds = %entry\r\n  %x3 = load double* %x1\r\n  %subtmp = fsub double %x3, 1.000000e+00\r\n  %calltmp = call double @fib(double %subtmp)\r\n  %x4 = load double* %x1\r\n  %subtmp5 = fsub double %x4, 2.000000e+00\r\n  %calltmp6 = call double @fib(double %subtmp5)\r\n  %addtmp = fadd double %calltmp, %calltmp6\r\n  br label %ifcont\r\n\r\nifcont:     ; preds = %else, %then\r\n  %iftmp = phi double &#x5B; 1.000000e+00, %then ], &#x5B; %addtmp, %else ]\r\n  ret double %iftmp\r\n}<\/pre>\n<p>\u3053\u3053\u3067\u306f\u5909\u6570\u306f\u3072\u3068\u3064\u3060\u3051\u3067\u3042\u308b\uff08\u5f15\u6570x\uff09\u304c\u3001\u6211\u3005\u304c\u975e\u5e38\u306b\u5358\u7d14\u306a\u30b3\u30fc\u30c9\u751f\u6210\u6226\u7565\u3092\u4f7f\u3063\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u308b\u3002<br \/>\nentry\u30d6\u30ed\u30c3\u30af\u3067\u3001alloca\u306f\u751f\u6210\u3055\u308c\u3001\u305d\u3053\u306b\u521d\u671f\u5024\u304c\u4fdd\u5b58\u3055\u308c\u308b\u3002<br \/>\n\u5909\u6570\u3078\u306e\u53c2\u7167\u3067\u306f\u3001\u6bce\u56de\u30b9\u30bf\u30c3\u30af\u304b\u3089\u518d\u8aad\u307f\u8fbc\u307f\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\nif\/then\/else\u5f0f\u3092\u4fee\u6b63\u3057\u3066\u306a\u3044\u306e\u3067\u3001\u4f9d\u7136\u3068\u3057\u3066phi\u30ce\u30fc\u30c9\u304c\u633f\u5165\u3055\u308c\u3066\u3044\u308b\u70b9\u306b\u3082\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3042\u308b\u3002<br \/>\n\u3053\u3053\u3067\u3082alloca\u3092\u4f7f\u3046\u3088\u3046\u306b\u3059\u308b\u4e8b\u3082\u3067\u304d\u308b\u304c\u3001phi\u30ce\u30fc\u30c9\u3092\u751f\u6210\u3059\u308b\u307b\u3046\u304c\u7c21\u5358\u306a\u306e\u3067\u305d\u306e\u307e\u307e\u306b\u3057\u3066\u3042\u308b\u3002<\/p>\n<p>\u3053\u308c\u304cmem2reg\u30d1\u30b9\u306b\u3088\u308b\u6700\u9069\u5316\u5f8c\u3067\u3042\u308b\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">define double @fib(double %x) {\r\nentry:\r\n  %cmptmp = fcmp ult double %x, 3.000000e+00\r\n  %booltmp = uitofp i1 %cmptmp to double\r\n  %ifcond = fcmp one double %booltmp, 0.000000e+00\r\n  br i1 %ifcond, label %then, label %else\r\n\r\nthen:\r\n  br label %ifcont\r\n\r\nelse:\r\n  %subtmp = fsub double %x, 1.000000e+00\r\n  %calltmp = call double @fib(double %subtmp)\r\n  %subtmp5 = fsub double %x, 2.000000e+00\r\n  %calltmp6 = call double @fib(double %subtmp5)\r\n  %addtmp = fadd double %calltmp, %calltmp6\r\n  br label %ifcont\r\n\r\nifcont:     ; preds = %else, %then\r\n  %iftmp = phi double &#x5B; 1.000000e+00, %then ], &#x5B; %addtmp, %else ]\r\n  ret double %iftmp\r\n}<\/pre>\n<p>\u3053\u308c\u306f\u5909\u6570\u306e\u518d\u5b9a\u7fa9\u3092\u542b\u307e\u306a\u3044\u306e\u3067\u3001mem2reg\u306b\u3068\u3063\u3066\u53d6\u308b\u306b\u8db3\u3089\u306a\u3044\u306a\u30b1\u30fc\u30b9\u3067\u3042\u308b\u3002<br \/>\n\u975e\u52b9\u7387\u306a\u547d\u4ee4\u306e\u633f\u5165\u3092\u3057\u3066\u3082\u5927\u4e08\u592b\u3060\u3001\u3068\u3044\u3046\u4e8b\u3092\u793a\u3059\u305f\u3081\u306b\u3053\u306e\u30b3\u30fc\u30c9\u3092\u793a\u3057\u3066\u3044\u308b\u3002<\/p>\n<p>\u6b8b\u308a\u306e\u6700\u9069\u5316\u3092\u5b9f\u884c\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">define double @fib(double %x) {\r\nentry:\r\n  %cmptmp = fcmp ult double %x, 3.000000e+00\r\n  %booltmp = uitofp i1 %cmptmp to double\r\n  %ifcond = fcmp ueq double %booltmp, 0.000000e+00\r\n  br i1 %ifcond, label %else, label %ifcont\r\n\r\nelse:\r\n  %subtmp = fsub double %x, 1.000000e+00\r\n  %calltmp = call double @fib(double %subtmp)\r\n  %subtmp5 = fsub double %x, 2.000000e+00\r\n  %calltmp6 = call double @fib(double %subtmp5)\r\n  %addtmp = fadd double %calltmp, %calltmp6\r\n  ret double %addtmp\r\n\r\nifcont:\r\n  ret double 1.000000e+00\r\n}<\/pre>\n<p>simplifycfg\u30d1\u30b9\u306b\u3088\u3063\u3066\u3001ret\u547d\u4ee4\u304celse\u30d6\u30ed\u30c3\u30af\u306e\u6700\u5f8c\u306b\u3082\u8ffd\u52a0\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\n\u3053\u308c\u306b\u3088\u3063\u3066\u3001\u3044\u304f\u3089\u304b\u306e\u679d\u5206\u304b\u308c\u3084phi\u30ce\u30fc\u30c9\u304c\u524a\u6e1b\u53ef\u80fd\u3068\u306a\u308b\u3002<\/p>\n<p>\u4ee5\u4e0a\u3067\u3001\u30b7\u30f3\u30dc\u30eb\u30c6\u30fc\u30d6\u30eb\u306e\u5168\u3066\u306e\u53c2\u7167\u306f\u3001\u30b9\u30bf\u30c3\u30af\u5909\u6570\u3092\u4f7f\u3046\u3088\u3046\u306b\u306a\u3063\u305f\u3002<br \/>\n\u6b21\u306f\u3001\u4ee3\u5165\u6f14\u7b97\u5b50\uff08assignment operator\uff09\u3092\u8ffd\u52a0\u3057\u3088\u3046\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[466,467],"class_list":["post-3026","post","type-post","status-publish","format-standard","hentry","category-tech","tag-kaleidoscope","tag-llvm"],"_links":{"self":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/3026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/comments?post=3026"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/3026\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=3026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=3026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=3026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}