{"id":2976,"date":"2013-08-29T13:13:12","date_gmt":"2013-08-29T04:13:12","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=2976"},"modified":"2013-08-29T13:13:12","modified_gmt":"2013-08-29T04:13:12","slug":"5-2-5-ifthenelse%e3%81%ae%e3%81%9f%e3%82%81%e3%81%ae%e3%82%b3%e3%83%bc%e3%83%89%e7%94%9f%e6%88%90","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/2976","title":{"rendered":"5.2.5. if\/then\/else\u306e\u305f\u3081\u306e\u30b3\u30fc\u30c9\u751f\u6210"},"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\u7b2c5\u7ae0 \u4e07\u83ef\u93e1: \u8a00\u8a9e\u306e\u62e1\u5f35: \u5236\u5fa1\u30d5\u30ed\u30fc<br \/>\n<a href=\"http:\/\/llvm.org\/docs\/tutorial\/LangImpl05.html#code-generation-for-if-then-else\" target=\"_blank\">\u7b2c2.5\u7bc0 if\/then\/else\u306e\u305f\u3081\u306e\u30b3\u30fc\u30c9\u751f\u6210<\/a><\/p>\n<p>if\/then\/else\u306e\u305f\u3081\u306e\u30b3\u30fc\u30c9\u3092\u751f\u6210\u3059\u308b\u305f\u3081\u306b\u3001IfExprAST\u306eCodegen\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5\u3059\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Value *IfExprAST::Codegen() {\r\n  Value *CondV = Cond-&gt;Codegen();\r\n  if (CondV == 0) return 0;\r\n\r\n  \/\/ \u6761\u4ef6\u30920.0\u3068\u6bd4\u8f03\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u771f\u507d\u5024\u306b\u5909\u63db\u3059\u308b\u3002\r\n  CondV = Builder.CreateFCmpONE(CondV,\r\n                              ConstantFP::get(getGlobalContext(), APFloat(0.0)),\r\n                                &quot;ifcond&quot;);<\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u306f\u5b9f\u306b\u7d20\u76f4\u3067\u3001\u305d\u3057\u3066\u4ee5\u524d\u898b\u305f\u3082\u306e\u3068\u4f3c\u3066\u3044\u308b\u3002<br \/>\n\u6761\u4ef6\u306e\u305f\u3081\u306e\u5f0f\u3092\u751f\u6210\u3057\u3001\u305d\u3057\u3066\u305d\u306e\u5024\u3092\u30bc\u30ed\u3068\u6bd4\u8f03\u3057\u30011bit\u306e\u5024\u3068\u3057\u3066\u771f\u507d\u5024\u3092\u5f97\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Function *TheFunction = Builder.GetInsertBlock()-&gt;getParent();\r\n\r\n\/\/ then\u3068else\u306e\u5834\u5408\u306e\u305f\u3081\u306e\u30d6\u30ed\u30c3\u30af\u3092\u751f\u6210\u3059\u308b\u3002\r\n\/\/ then\u30d6\u30ed\u30c3\u30af\u3092\u95a2\u6570\u306e\u6700\u5f8c\u306b\u633f\u5165\u3059\u308b\u3002\r\nBasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), &quot;then&quot;, TheFunction);\r\nBasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), &quot;else&quot;);\r\nBasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), &quot;ifcont&quot;);\r\n\r\nBuilder.CreateCondBr(CondV, ThenBB, ElseBB);<\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u306f\u3001\u524d\u7bc0\u306e\u4f8b\u306b\u304a\u3051\u308b\u30d6\u30ed\u30c3\u30af\u3068\u76f4\u63a5\u5bfe\u5fdc\u3057\u3066\u3044\u3066\u3001if\/then\/else\u306b\u95a2\u9023\u3057\u305f\u57fa\u672c\u30d6\u30ed\u30c3\u30af\u3092\u751f\u6210\u3059\u308b\u3082\u306e\u3067\u3042\u308b\u3002<br \/>\n\u6700\u521d\u306e\u884c\u3067\u3001\u73fe\u5728\u69cb\u7bc9\u4e2d\u306e\u95a2\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3059\u308b\u3002<br \/>\n\u73fe\u5728\u306e\u57fa\u672c\u30d6\u30ed\u30c3\u30af\u306b\u3064\u3044\u3066Builder\u306b\u5c0b\u306d\u3001\u305d\u3057\u3066\u305d\u306e\u89aa\u306e\u30d6\u30ed\u30c3\u30af\u306b\u3064\u3044\u3066\u5c0b\u306d\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u305d\u308c\u3092\u5f97\u308b\u3002\uff08\u305d\u306e\u95a2\u6570\u306f\u3059\u3067\u306b\u57cb\u3081\u3053\u307e\u308c\u3066\u3044\u308b\u306e\u3067\u3053\u3046\u3057\u3066\u53d6\u5f97\u3059\u308b\u3002\uff09<\/p>\n<p>\u305d\u308c\u304c\u7d42\u308f\u308c\u3070\u3001\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u306f3\u3064\u306e\u30d6\u30ed\u30c3\u30af\u3092\u751f\u6210\u3059\u308b\u3002<br \/>\nthen\u30d6\u30ed\u30c3\u30af\u306e\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u306bTheFunction\u304c\u6e21\u3055\u308c\u3066\u3044\u308b\u3068\u3044\u3046\u70b9\u306b\u6ce8\u610f\u3002<br \/>\n\u3053\u308c\u306b\u3088\u3063\u3066\u3001\u65b0\u3057\u3044\u30d6\u30ed\u30c3\u30af\u304c\u6307\u5b9a\u3055\u308c\u305f\u95a2\u6570\u306e\u672b\u5c3e\u306b\u81ea\u52d5\u7684\u306b\u633f\u5165\u3055\u308c\u308b\u3002<br \/>\n\u4ed6\u306e2\u3064\u306e\u30d6\u30ed\u30c3\u30af\u3082\u4f5c\u6210\u3055\u308c\u308b\u304c\u3001\u307e\u3060\u95a2\u6570\u306e\u4e2d\u3078\u306f\u633f\u5165\u3057\u306a\u3044\u3002<\/p>\n<p>\u30d6\u30ed\u30c3\u30af\u304c\u751f\u6210\u3055\u308c\u305f\u3089\u3001\u3069\u3061\u3089\u306e\u30d6\u30ed\u30c3\u30af\u3092\u9078\u3076\u304b\u3092\u6c7a\u3081\u308b\u305f\u3081\u306e\u6761\u4ef6\u5206\u5c90\u3092\u751f\u6210\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u65b0\u3057\u3044\u30d6\u30ed\u30c3\u30af\u306e\u751f\u6210\u306f\u3001IRBuilder\u306b\u5bfe\u3057\u3066\u6697\u9ed9\u7684\u306b\u3082\u5f71\u97ff\u3092\u4e0e\u3048\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3002<br \/>\n\u306a\u306e\u3067\u3001\u305d\u308c\u306f\u307e\u3060\u3001\u6761\u4ef6\u304c\u5165\u3063\u3066\u308b\u30d6\u30ed\u30c3\u30af\u306b\u633f\u5165\u3057\u3066\u3044\u308b\u3002\uff08so it is still inserting into the block that the condition went into.\uff09<br \/>\nthen\u30d6\u30ed\u30c3\u30af\u3084else\u30d6\u30ed\u30c3\u30af\u3078\u306e\u679d\u5206\u304b\u308c\u306e\u751f\u6210\u306f\u3057\u3066\u3044\u308b\u304c\u3001&#8221;else&#8221;\u30d6\u30ed\u30c3\u30af\u306f\u307e\u3060\u95a2\u6570\u306b\u633f\u5165\u3055\u308c\u3066\u3044\u306a\u3044\u3053\u3068\u306b\u3082\u6ce8\u610f\u3002<br \/>\n\u3053\u308c\u3067\u3044\u3044\u306e\u3067\u3042\u308b\u3002<br \/>\n\u3053\u308c\u306fLLVM\u304c\u524d\u65b9\u53c2\u7167\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u6a19\u6e96\u7684\u306a\u65b9\u6cd5\u3067\u3042\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ then\u5024\u3092\u767a\u884c\u3059\u308b\u3002\r\nBuilder.SetInsertPoint(ThenBB);\r\n\r\nValue *ThenV = Then-&gt;Codegen();\r\nif (ThenV == 0) return 0;\r\n\r\nBuilder.CreateBr(MergeBB);\r\n\/\/ then\u306eCodegen\u306f\u3001\u73fe\u5728\u306e\u30d6\u30ed\u30c3\u30af\u3092\u5909\u66f4\u3057\u3001phi\u306e\u305f\u3081\u306bThenBB\u3092\u66f4\u65b0\u3057\u3046\u308b\u3002\r\nThenBB = Builder.GetInsertBlock();<\/pre>\n<p>\u6761\u4ef6\u5206\u5c90\u304c\u633f\u5165\u3055\u308c\u305f\u3042\u3068\u3001Builder\u306bthen\u30d6\u30ed\u30c3\u30af\u3078\u633f\u5165\u3055\u305b\u308b\u3088\u3046\u306b\u3059\u308b\u3002<br \/>\n\u53b3\u5bc6\u306b\u8a00\u3048\u3070\u3001\u3053\u306e\u547c\u51fa\u306f\u6307\u5b9a\u3055\u308c\u305f\u30d6\u30ed\u30c3\u30af\u306e\u6700\u5f8c\u306b\u633f\u5165\u70b9\u3092\u79fb\u52d5\u3059\u308b\u3002<br \/>\n\u3057\u304b\u3057\u306a\u304c\u3089\u3001then\u30d6\u30ed\u30c3\u30af\u306f\u7a7a\u306a\u306e\u3067\u3001\u305d\u306e\u30d6\u30ed\u30c3\u30af\u306e\u6700\u521d\u304b\u3089\u633f\u5165\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n<p>\u633f\u5165\u70b9\u304c\u30bb\u30c3\u30c8\u3055\u308c\u305f\u3089\u3001AST\u304b\u3089then\u306eCodegen\u3092\u518d\u5e30\u7684\u306b\u5b9f\u884c\u3059\u308b\u3002<br \/>\nthen\u30d6\u30ed\u30c3\u30af\u3092\u4ed5\u4e0a\u3052\u308b\u305f\u3081\u306b\u3001\u7121\u6761\u4ef6\u306e\u5206\u5c90\uff08br\u547d\u4ee4\uff09\u3092merge\u30d6\u30ed\u30c3\u30af\u306b\u751f\u6210\u3059\u308b\u3002<br \/>\nLLVM IR\u306e\u9762\u767d\u3044\uff08\u305d\u3057\u3066\u3068\u3066\u3082\u91cd\u8981\u306a\uff09\u6027\u8cea\u3068\u3057\u3066\u3001\u8fd4\u308a\uff08return\uff09\u3084\u679d\u5206\u304b\u308c\uff08branch\uff09\u306e\u3088\u3046\u306a\u5236\u5fa1\u30d5\u30ed\u30fc\u547d\u4ee4\u3067\u3001\u57fa\u672c\u30d6\u30ed\u30c3\u30af\u3092\u5fc5\u305a\u7d42\u7aef\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u3044\u3046\u70b9\u304c\u3042\u308b\u3002<br \/>\n\u3053\u308c\u306f\u3001\u30d5\u30a9\u30fc\u30eb\u30b9\u30eb\u30fc\u3092\u542b\u3080\u5168\u3066\u306e\u5236\u5fa1\u30d5\u30ed\u30fc\u306f\u3001LLVM IR\u306b\u304a\u3044\u3066\u306f\u660e\u78ba\u3067\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u3002<br \/>\n\u3053\u306e\u30eb\u30fc\u30eb\u306b\u9055\u53cd\u3057\u305f\u5834\u5408\u3001\u30d9\u30ea\u30d5\u30a1\u30a4\u30a2\u306f\u30a8\u30e9\u30fc\u3092\u767a\u884c\u3059\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: \u3053\u3053\u3067\u3044\u3046\u30d5\u30a9\u30fc\u30eb\u30b9\u30eb\u30fc\u3068\u306f\u3001LLVM IR\u306b\u304a\u3044\u3066\u3001\u3042\u308b\u95a2\u6570\u5185\u306e\u3042\u308b\u30d6\u30ed\u30c3\u30af\u304b\u3089\u5358\u306b\u3059\u3050\u5f8c\u306e\u30d6\u30ed\u30c3\u30af\u306b\u79fb\u308b\u3001\u3068\u3044\u3044\u3046\u610f\u5473\u3060\u3068\u601d\u3046\u3002<br \/>\n\u3053\u306e\u5834\u5408\u3082\u3001\u660e\u78ba\u306bbr\u547d\u4ee4\u3092\u304a\u3044\u3066\u6b21\u306e\u30d6\u30ed\u30c3\u30af\u3092\u6307\u5b9a\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<\/em><\/p>\n<p>\u6700\u5f8c\u306e\u884c\u306f\u304b\u306a\u308a\u5206\u304b\u308a\u3065\u3089\u3044\u304c\u3001\u3068\u3066\u3082\u91cd\u8981\u3067\u3042\u308b\u3002<br \/>\nmerge\u30d6\u30ed\u30c3\u30af\u306b\u304a\u3044\u3066phi\u30ce\u30fc\u30c9\u3092\u751f\u6210\u3059\u308b\u3068\u304d\u3001phi\u3092\u3069\u3046\u50cd\u304b\u305b\u308b\u304b\u3092\u793a\u3059\u305f\u3081\u306e\u30d6\u30ed\u30c3\u30af\u3068\u5024\u306e\u30da\u30a2\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3068\u3044\u3046\u3053\u3068\u304c\u57fa\u672c\u7684\u306a\u554f\u984c\u3067\u3042\u308b\u3002<br \/>\n\u91cd\u8981\u306a\u306e\u306f\u3001phi\u30ce\u30fc\u30c9\u306f\u3001CFG\uff08Control flow graph\uff09\u306b\u304a\u3051\u308b\u30d6\u30ed\u30c3\u30af\u306e\u5404\u5148\u884c\u547d\u4ee4\u306e\u305f\u3081\u306e\u30a8\u30f3\u30c8\u30ea\u3092\u6301\u3064\u3068\u3044\u3046\u3053\u3068\u3092\u4e88\u671f\u3057\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3042\u308b\u3002<br \/>\n\u3068\u3053\u308d\u3067\u30015\u884c\u524d\u3067\u633f\u5165\u70b9\u3092ThenBB\u3078\u30bb\u30c3\u30c8\u3057\u305f\u3068\u304d\u306b\u3001\u73fe\u5728\u306e\u30d6\u30ed\u30c3\u30af\u5f97\u305f\u304b\uff1f<br \/>\nthen\u5f0f\u306f\u5b9f\u969b\u306b\u3001Builder\u304c\u305d\u306e\u4e2d\u306b\u767a\u884c\u3057\u305f\u81ea\u8eab\u306e\u30d6\u30ed\u30c3\u30af\u3092\u5909\u66f4\u3057\u3046\u308b\u3068\u3044\u3046\u3053\u3068\u304c\u554f\u984c\u3067\u3042\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u5165\u308c\u5b50\u306b\u306a\u3063\u305fif\/then\/else\u5f0f\u3092\u542b\u3080\u304b\u3082\u3057\u308c\u306a\u3044\u3002<br \/>\nCodegen\u3092\u518d\u5e30\u7684\u306b\u547c\u3076\u3053\u3068\u3067\u3001\u73fe\u5728\u306e\u30d6\u30ed\u30c3\u30af\u306e\u610f\u5411\u3092\u4efb\u610f\u306b\u5909\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3001phi\u30ce\u30fc\u30c9\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u30b3\u30fc\u30c9\u306e\u6700\u65b0\u306e\u5024\u304c\u5fc5\u8981\u3067\u3042\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: \u6b63\u76f4\u3053\u3053\u306e\u6587\u7ae0\u306e\u8a33\u306f\u5168\u304f\u3057\u3063\u304f\u308a\u6765\u3066\u306a\u3044\u306e\u3067\u3001\u539f\u6587\u3092\u307e\u3068\u3081\u3066\u8f09\u305b\u3066\u304a\u304f\u3002<br \/>\nThe final line here is quite subtle, but is very important. The basic issue is that when we create the Phi node in the merge block, we need to set up the block\/value pairs that indicate how the Phi will work. Importantly, the Phi node expects to have an entry for each predecessor of the block in the CFG. Why then, are we getting the current block when we just set it to ThenBB 5 lines above? The problem is that the \u201cThen\u201d expression may actually itself change the block that the Builder is emitting into if, for example, it contains a nested \u201cif\/then\/else\u201d expression. Because calling Codegen recursively could arbitrarily change the notion of the current block, we are required to get an up-to-date value for code that will set up the Phi node.<\/em><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ else\u30d6\u30ed\u30c3\u30af\u3092\u767a\u884c\u3059\u308b\u3002\r\nTheFunction-&gt;getBasicBlockList().push_back(ElseBB);\r\nBuilder.SetInsertPoint(ElseBB);\r\n\r\nValue *ElseV = Else-&gt;Codegen();\r\nif (ElseV == 0) return 0;\r\n\r\nBuilder.CreateBr(MergeBB);\r\n\/\/ else\u306eCodegen\u306f\u3001\u73fe\u5728\u306e\u30d6\u30ed\u30c3\u30af\u3092\u5909\u66f4\u3057\u3001phi\u306e\u305f\u3081\u306bElseBB\u3092\u66f4\u65b0\u3057\u3046\u308b\u3002.\r\nElseBB = Builder.GetInsertBlock();<\/pre>\n<p>else\u30d6\u30ed\u30c3\u30af\u306e\u305f\u3081\u306e\u30b3\u30fc\u30c9\u751f\u6210\u306f\u3001\u57fa\u672c\u7684\u306bthen\u306e\u5834\u5408\u3068\u540c\u3058\u3067\u3042\u308b\u3002<br \/>\n\u552f\u4e00\u306e\u91cd\u8981\u306a\u9055\u3044\u306f\u3001\u6700\u521d\u306e\u884c\u3067\u3001else\u30d6\u30ed\u30c3\u30af\u3092\u95a2\u6570\u3078\u8ffd\u52a0\u3057\u3066\u3044\u308b\u3002<br \/>\n\u5c11\u3057\u524d\u306b\u3001else\u30d6\u30ed\u30c3\u30af\u306f\u4f5c\u6210\u3055\u308c\u305f\u304c\u3001\u95a2\u6570\u3078\u8ffd\u52a0\u3055\u308c\u3066\u3044\u306a\u304b\u3063\u305f\u3053\u3068\u3092\u601d\u3044\u51fa\u305d\u3046\u3002<br \/>\n\u3053\u308c\u3067then\u30d6\u30ed\u30c3\u30af\u3068else\u30d6\u30ed\u30c3\u30af\u306f\u767a\u884c\u3055\u308c\u305f\u3002<br \/>\n\u5206\u5c90\u3092\u5408\u6d41\u3055\u305b\u308b\u30b3\u30fc\u30c9\uff08merge code\uff09\u306b\u3088\u3063\u3066\u3053\u308c\u3092\u4ed5\u4e0a\u3052\u3088\u3046\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">  \/\/ merge\u30d6\u30ed\u30c3\u30af\u306e\u767a\u884c\u3002\r\n  TheFunction-&gt;getBasicBlockList().push_back(MergeBB);\r\n  Builder.SetInsertPoint(MergeBB);\r\n  PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,\r\n                                  &quot;iftmp&quot;);\r\n\r\n  PN-&gt;addIncoming(ThenV, ThenBB);\r\n  PN-&gt;addIncoming(ElseV, ElseBB);\r\n  return PN;\r\n}<\/pre>\n<p>\u6700\u521d\u306e2\u884c\u306f\u3001\u4eca\u3068\u306a\u3063\u3066\u306f\u898b\u6163\u308c\u305f\u3060\u308d\u3046\u3002<br \/>\n\u6700\u521d\u306e\u884c\u306f\u3001\u95a2\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3078merge\u30d6\u30ed\u30c3\u30af\uff08\u4e0a\u306eelse\u30d6\u30ed\u30c3\u30af\u3068\u540c\u3058\u3088\u3046\u306b\u5b99\u3076\u3089\u308a\u3093\u306b\u306a\u3063\u3066\u3044\u305f\uff09\u3092\u8ffd\u52a0\u3057\u3066\u3044\u308b\u3002<br \/>\n\u6b21\u306e\u884c\u3067\u3001\u65b0\u3057\u304f\u751f\u6210\u3055\u308c\u305f\u30b3\u30fc\u30c9\u304cmerge\u30d6\u30ed\u30c3\u30af\u306b\u633f\u5165\u3055\u308c\u308b\u3088\u3046\u633f\u5165\u70b9\u3092\u5909\u66f4\u3057\u3066\u3044\u308b\u3002<br \/>\n\u3053\u308c\u304c\u5b8c\u4e86\u3057\u305f\u3089\u3001phi\u30ce\u30fc\u30c9\u3092\u751f\u6210\u3057\u3001phi\u30ce\u30fc\u30c9\u306e\u305f\u3081\u306e\u30d6\u30ed\u30c3\u30af\u3068\u5024\u306e\u30da\u30a2\u3092\u6e96\u5099\u3059\u308b\u3002<\/p>\n<p>\u6700\u5f8c\u306b\u3001\u3053\u306eCodegen\u95a2\u6570\u306f\u3001phi\u30ce\u30fc\u30c9\u3092if\/then\/else\u5f0f\u306b\u3088\u3063\u3066\u8a08\u7b97\u3055\u308c\u305f\u5024\u3068\u3057\u3066\u8fd4\u3059\u3002<br \/>\n\u524d\u7bc0\u306e\u4f8b\u3067\u306f\u3001\u3053\u306e\u623b\u308a\u5024\u306f\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u95a2\u6570\u306e\u30b3\u30fc\u30c9\u306e\u4e2d\u306b\u9001\u308a\u8fbc\u307e\u308c\u3001\u305d\u306e\u95a2\u6570\u306freturn\u547d\u4ee4\u3092\u751f\u6210\u3059\u308b\u3060\u308d\u3046\u3002<br \/>\n<em>\u8a33\u6ce8: \u524d\u7bc0\u306e\u4f8b\u3068\u3044\u3046\u306e\u306f\u591a\u5206\u30b3\u30ec\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">extern foo();\r\nextern bar();\r\ndef baz(x) if x then foo() else bar();<\/pre>\n<p><\/em><\/p>\n<p>\u3053\u308c\u3067\u3001\u4e07\u83ef\u93e1\u306b\u304a\u3044\u3066\u6761\u4ef6\u4ed8\u304d\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u80fd\u529b\u3092\u5f97\u305f\u3002<br \/>\n\u3053\u306e\u62e1\u5f35\u306b\u3088\u3063\u3066\u3001\u4e07\u83ef\u93e1\u306f\u3001\u3044\u308d\u3093\u306a\u7a2e\u985e\u306e\u6570\u5b66\u7684\u8a08\u7b97\u3092\u304a\u3053\u306a\u3048\u308b\u6975\u3081\u3066\u5b8c\u5168\u306a\u8a00\u8a9e\u3068\u306a\u3063\u305f\u3002<br \/>\n\u6b21\u306f\u3001\u975e\u95a2\u6570\u578b\u8a00\u8a9e\u3067\u304a\u306a\u3058\u307f\u306e\u4fbf\u5229\u306a\u5f0f\u3092\u8ffd\u52a0\u3059\u308b\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\u7b2c5\u7ae0 \u4e07\u83ef\u93e1: \u8a00\u8a9e\u306e\u62e1\u5f35: \u5236\u5fa1\u30d5\u30ed\u30fc<br \/>\n<a href=\"http:\/\/llvm.org\/docs\/tutorial\/LangImpl05.html#code-generation-for-if-then-else\" target=\"_blank\">\u7b2c2.5\u7bc0 if\/then\/else\u306e\u305f\u3081\u306e\u30b3\u30fc\u30c9\u751f\u6210<\/a><\/p>\n<p>if\/then\/else\u306e\u305f\u3081\u306e\u30b3\u30fc\u30c9\u3092\u751f\u6210\u3059\u308b\u305f\u3081\u306b\u3001IfExprAST\u306eCodegen\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5\u3059\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Value *IfExprAST::Codegen() {\r\n  Value *CondV = Cond-&gt;Codegen();\r\n  if (CondV == 0) return 0;\r\n\r\n  \/\/ \u6761\u4ef6\u30920.0\u3068\u6bd4\u8f03\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u771f\u507d\u5024\u306b\u5909\u63db\u3059\u308b\u3002\r\n  CondV = Builder.CreateFCmpONE(CondV,\r\n                              ConstantFP::get(getGlobalContext(), APFloat(0.0)),\r\n                                &quot;ifcond&quot;);<\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u306f\u5b9f\u306b\u7d20\u76f4\u3067\u3001\u305d\u3057\u3066\u4ee5\u524d\u898b\u305f\u3082\u306e\u3068\u4f3c\u3066\u3044\u308b\u3002<br \/>\n\u6761\u4ef6\u306e\u305f\u3081\u306e\u5f0f\u3092\u751f\u6210\u3057\u3001\u305d\u3057\u3066\u305d\u306e\u5024\u3092\u30bc\u30ed\u3068\u6bd4\u8f03\u3057\u30011bit\u306e\u5024\u3068\u3057\u3066\u771f\u507d\u5024\u3092\u5f97\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Function *TheFunction = Builder.GetInsertBlock()-&gt;getParent();\r\n\r\n\/\/ then\u3068else\u306e\u5834\u5408\u306e\u305f\u3081\u306e\u30d6\u30ed\u30c3\u30af\u3092\u751f\u6210\u3059\u308b\u3002\r\n\/\/ then\u30d6\u30ed\u30c3\u30af\u3092\u95a2\u6570\u306e\u6700\u5f8c\u306b\u633f\u5165\u3059\u308b\u3002\r\nBasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), &quot;then&quot;, TheFunction);\r\nBasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), &quot;else&quot;);\r\nBasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), &quot;ifcont&quot;);\r\n\r\nBuilder.CreateCondBr(CondV, ThenBB, ElseBB);<\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u306f\u3001\u524d\u7bc0\u306e\u4f8b\u306b\u304a\u3051\u308b\u30d6\u30ed\u30c3\u30af\u3068\u76f4\u63a5\u5bfe\u5fdc\u3057\u3066\u3044\u3066\u3001if\/then\/else\u306b\u95a2\u9023\u3057\u305f\u57fa\u672c\u30d6\u30ed\u30c3\u30af\u3092\u751f\u6210\u3059\u308b\u3082\u306e\u3067\u3042\u308b\u3002<br \/>\n\u6700\u521d\u306e\u884c\u3067\u3001\u73fe\u5728\u69cb\u7bc9\u4e2d\u306e\u95a2\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3059\u308b\u3002<br \/>\n\u73fe\u5728\u306e\u57fa\u672c\u30d6\u30ed\u30c3\u30af\u306b\u3064\u3044\u3066Builder\u306b\u5c0b\u306d\u3001\u305d\u3057\u3066\u305d\u306e\u89aa\u306e\u30d6\u30ed\u30c3\u30af\u306b\u3064\u3044\u3066\u5c0b\u306d\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u305d\u308c\u3092\u5f97\u308b\u3002\uff08\u305d\u306e\u95a2\u6570\u306f\u3059\u3067\u306b\u57cb\u3081\u3053\u307e\u308c\u3066\u3044\u308b\u306e\u3067\u3053\u3046\u3057\u3066\u53d6\u5f97\u3059\u308b\u3002\uff09<\/p>\n<p>\u305d\u308c\u304c\u7d42\u308f\u308c\u3070\u3001\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u306f3\u3064\u306e\u30d6\u30ed\u30c3\u30af\u3092\u751f\u6210\u3059\u308b\u3002<br \/>\nthen\u30d6\u30ed\u30c3\u30af\u306e\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u306bTheFunction\u304c\u6e21\u3055\u308c\u3066\u3044\u308b\u3068\u3044\u3046\u70b9\u306b\u6ce8\u610f\u3002<br \/>\n\u3053\u308c\u306b\u3088\u3063\u3066\u3001\u65b0\u3057\u3044\u30d6\u30ed\u30c3\u30af\u304c\u6307\u5b9a\u3055\u308c\u305f\u95a2\u6570\u306e\u672b\u5c3e\u306b\u81ea\u52d5\u7684\u306b\u633f\u5165\u3055\u308c\u308b\u3002<br \/>\n\u4ed6\u306e2\u3064\u306e\u30d6\u30ed\u30c3\u30af\u3082\u4f5c\u6210\u3055\u308c\u308b\u304c\u3001\u307e\u3060\u95a2\u6570\u306e\u4e2d\u3078\u306f\u633f\u5165\u3057\u306a\u3044\u3002<\/p>\n<p>\u30d6\u30ed\u30c3\u30af\u304c\u751f\u6210\u3055\u308c\u305f\u3089\u3001\u3069\u3061\u3089\u306e\u30d6\u30ed\u30c3\u30af\u3092\u9078\u3076\u304b\u3092\u6c7a\u3081\u308b\u305f\u3081\u306e\u6761\u4ef6\u5206\u5c90\u3092\u751f\u6210\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u65b0\u3057\u3044\u30d6\u30ed\u30c3\u30af\u306e\u751f\u6210\u306f\u3001IRBuilder\u306b\u5bfe\u3057\u3066\u6697\u9ed9\u7684\u306b\u3082\u5f71\u97ff\u3092\u4e0e\u3048\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3002<br \/>\n\u306a\u306e\u3067\u3001\u305d\u308c\u306f\u307e\u3060\u3001\u6761\u4ef6\u304c\u5165\u3063\u3066\u308b\u30d6\u30ed\u30c3\u30af\u306b\u633f\u5165\u3057\u3066\u3044\u308b\u3002\uff08so it is still inserting into the block that the condition went into.\uff09<br \/>\nthen\u30d6\u30ed\u30c3\u30af\u3084else\u30d6\u30ed\u30c3\u30af\u3078\u306e\u679d\u5206\u304b\u308c\u306e\u751f\u6210\u306f\u3057\u3066\u3044\u308b\u304c\u3001&#8221;else&#8221;\u30d6\u30ed\u30c3\u30af\u306f\u307e\u3060\u95a2\u6570\u306b\u633f\u5165\u3055\u308c\u3066\u3044\u306a\u3044\u3053\u3068\u306b\u3082\u6ce8\u610f\u3002<br \/>\n\u3053\u308c\u3067\u3044\u3044\u306e\u3067\u3042\u308b\u3002<br \/>\n\u3053\u308c\u306fLLVM\u304c\u524d\u65b9\u53c2\u7167\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u6a19\u6e96\u7684\u306a\u65b9\u6cd5\u3067\u3042\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ then\u5024\u3092\u767a\u884c\u3059\u308b\u3002\r\nBuilder.SetInsertPoint(ThenBB);\r\n\r\nValue *ThenV = Then-&gt;Codegen();\r\nif (ThenV == 0) return 0;\r\n\r\nBuilder.CreateBr(MergeBB);\r\n\/\/ then\u306eCodegen\u306f\u3001\u73fe\u5728\u306e\u30d6\u30ed\u30c3\u30af\u3092\u5909\u66f4\u3057\u3001phi\u306e\u305f\u3081\u306bThenBB\u3092\u66f4\u65b0\u3057\u3046\u308b\u3002\r\nThenBB = Builder.GetInsertBlock();<\/pre>\n<p>\u6761\u4ef6\u5206\u5c90\u304c\u633f\u5165\u3055\u308c\u305f\u3042\u3068\u3001Builder\u306bthen\u30d6\u30ed\u30c3\u30af\u3078\u633f\u5165\u3055\u305b\u308b\u3088\u3046\u306b\u3059\u308b\u3002<br \/>\n\u53b3\u5bc6\u306b\u8a00\u3048\u3070\u3001\u3053\u306e\u547c\u51fa\u306f\u6307\u5b9a\u3055\u308c\u305f\u30d6\u30ed\u30c3\u30af\u306e\u6700\u5f8c\u306b\u633f\u5165\u70b9\u3092\u79fb\u52d5\u3059\u308b\u3002<br \/>\n\u3057\u304b\u3057\u306a\u304c\u3089\u3001then\u30d6\u30ed\u30c3\u30af\u306f\u7a7a\u306a\u306e\u3067\u3001\u305d\u306e\u30d6\u30ed\u30c3\u30af\u306e\u6700\u521d\u304b\u3089\u633f\u5165\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n<p>\u633f\u5165\u70b9\u304c\u30bb\u30c3\u30c8\u3055\u308c\u305f\u3089\u3001AST\u304b\u3089then\u306eCodegen\u3092\u518d\u5e30\u7684\u306b\u5b9f\u884c\u3059\u308b\u3002<br \/>\nthen\u30d6\u30ed\u30c3\u30af\u3092\u4ed5\u4e0a\u3052\u308b\u305f\u3081\u306b\u3001\u7121\u6761\u4ef6\u306e\u5206\u5c90\uff08br\u547d\u4ee4\uff09\u3092merge\u30d6\u30ed\u30c3\u30af\u306b\u751f\u6210\u3059\u308b\u3002<br \/>\nLLVM IR\u306e\u9762\u767d\u3044\uff08\u305d\u3057\u3066\u3068\u3066\u3082\u91cd\u8981\u306a\uff09\u6027\u8cea\u3068\u3057\u3066\u3001\u8fd4\u308a\uff08return\uff09\u3084\u679d\u5206\u304b\u308c\uff08branch\uff09\u306e\u3088\u3046\u306a\u5236\u5fa1\u30d5\u30ed\u30fc\u547d\u4ee4\u3067\u3001\u57fa\u672c\u30d6\u30ed\u30c3\u30af\u3092\u5fc5\u305a\u7d42\u7aef\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u3044\u3046\u70b9\u304c\u3042\u308b\u3002<br \/>\n\u3053\u308c\u306f\u3001\u30d5\u30a9\u30fc\u30eb\u30b9\u30eb\u30fc\u3092\u542b\u3080\u5168\u3066\u306e\u5236\u5fa1\u30d5\u30ed\u30fc\u306f\u3001LLVM IR\u306b\u304a\u3044\u3066\u306f\u660e\u78ba\u3067\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u3002<br \/>\n\u3053\u306e\u30eb\u30fc\u30eb\u306b\u9055\u53cd\u3057\u305f\u5834\u5408\u3001\u30d9\u30ea\u30d5\u30a1\u30a4\u30a2\u306f\u30a8\u30e9\u30fc\u3092\u767a\u884c\u3059\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: \u3053\u3053\u3067\u3044\u3046\u30d5\u30a9\u30fc\u30eb\u30b9\u30eb\u30fc\u3068\u306f\u3001LLVM IR\u306b\u304a\u3044\u3066\u3001\u3042\u308b\u95a2\u6570\u5185\u306e\u3042\u308b\u30d6\u30ed\u30c3\u30af\u304b\u3089\u5358\u306b\u3059\u3050\u5f8c\u306e\u30d6\u30ed\u30c3\u30af\u306b\u79fb\u308b\u3001\u3068\u3044\u3044\u3046\u610f\u5473\u3060\u3068\u601d\u3046\u3002<br \/>\n\u3053\u306e\u5834\u5408\u3082\u3001\u660e\u78ba\u306bbr\u547d\u4ee4\u3092\u304a\u3044\u3066\u6b21\u306e\u30d6\u30ed\u30c3\u30af\u3092\u6307\u5b9a\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<\/em><\/p>\n<p>\u6700\u5f8c\u306e\u884c\u306f\u304b\u306a\u308a\u5206\u304b\u308a\u3065\u3089\u3044\u304c\u3001\u3068\u3066\u3082\u91cd\u8981\u3067\u3042\u308b\u3002<br \/>\nmerge\u30d6\u30ed\u30c3\u30af\u306b\u304a\u3044\u3066phi\u30ce\u30fc\u30c9\u3092\u751f\u6210\u3059\u308b\u3068\u304d\u3001phi\u3092\u3069\u3046\u50cd\u304b\u305b\u308b\u304b\u3092\u793a\u3059\u305f\u3081\u306e\u30d6\u30ed\u30c3\u30af\u3068\u5024\u306e\u30da\u30a2\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3068\u3044\u3046\u3053\u3068\u304c\u57fa\u672c\u7684\u306a\u554f\u984c\u3067\u3042\u308b\u3002<br \/>\n\u91cd\u8981\u306a\u306e\u306f\u3001phi\u30ce\u30fc\u30c9\u306f\u3001CFG\uff08Control flow graph\uff09\u306b\u304a\u3051\u308b\u30d6\u30ed\u30c3\u30af\u306e\u5404\u5148\u884c\u547d\u4ee4\u306e\u305f\u3081\u306e\u30a8\u30f3\u30c8\u30ea\u3092\u6301\u3064\u3068\u3044\u3046\u3053\u3068\u3092\u4e88\u671f\u3057\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3042\u308b\u3002<br \/>\n\u3068\u3053\u308d\u3067\u30015\u884c\u524d\u3067\u633f\u5165\u70b9\u3092ThenBB\u3078\u30bb\u30c3\u30c8\u3057\u305f\u3068\u304d\u306b\u3001\u73fe\u5728\u306e\u30d6\u30ed\u30c3\u30af\u5f97\u305f\u304b\uff1f<br \/>\nthen\u5f0f\u306f\u5b9f\u969b\u306b\u3001Builder\u304c\u305d\u306e\u4e2d\u306b\u767a\u884c\u3057\u305f\u81ea\u8eab\u306e\u30d6\u30ed\u30c3\u30af\u3092\u5909\u66f4\u3057\u3046\u308b\u3068\u3044\u3046\u3053\u3068\u304c\u554f\u984c\u3067\u3042\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u5165\u308c\u5b50\u306b\u306a\u3063\u305fif\/then\/else\u5f0f\u3092\u542b\u3080\u304b\u3082\u3057\u308c\u306a\u3044\u3002<br \/>\nCodegen\u3092\u518d\u5e30\u7684\u306b\u547c\u3076\u3053\u3068\u3067\u3001\u73fe\u5728\u306e\u30d6\u30ed\u30c3\u30af\u306e\u610f\u5411\u3092\u4efb\u610f\u306b\u5909\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3001phi\u30ce\u30fc\u30c9\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u30b3\u30fc\u30c9\u306e\u6700\u65b0\u306e\u5024\u304c\u5fc5\u8981\u3067\u3042\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: \u6b63\u76f4\u3053\u3053\u306e\u6587\u7ae0\u306e\u8a33\u306f\u5168\u304f\u3057\u3063\u304f\u308a\u6765\u3066\u306a\u3044\u306e\u3067\u3001\u539f\u6587\u3092\u307e\u3068\u3081\u3066\u8f09\u305b\u3066\u304a\u304f\u3002<br \/>\nThe final line here is quite subtle, but is very important. The basic issue is that when we create the Phi node in the merge block, we need to set up the block\/value pairs that indicate how the Phi will work. Importantly, the Phi node expects to have an entry for each predecessor of the block in the CFG. Why then, are we getting the current block when we just set it to ThenBB 5 lines above? The problem is that the \u201cThen\u201d expression may actually itself change the block that the Builder is emitting into if, for example, it contains a nested \u201cif\/then\/else\u201d expression. Because calling Codegen recursively could arbitrarily change the notion of the current block, we are required to get an up-to-date value for code that will set up the Phi node.<\/em><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ else\u30d6\u30ed\u30c3\u30af\u3092\u767a\u884c\u3059\u308b\u3002\r\nTheFunction-&gt;getBasicBlockList().push_back(ElseBB);\r\nBuilder.SetInsertPoint(ElseBB);\r\n\r\nValue *ElseV = Else-&gt;Codegen();\r\nif (ElseV == 0) return 0;\r\n\r\nBuilder.CreateBr(MergeBB);\r\n\/\/ else\u306eCodegen\u306f\u3001\u73fe\u5728\u306e\u30d6\u30ed\u30c3\u30af\u3092\u5909\u66f4\u3057\u3001phi\u306e\u305f\u3081\u306bElseBB\u3092\u66f4\u65b0\u3057\u3046\u308b\u3002.\r\nElseBB = Builder.GetInsertBlock();<\/pre>\n<p>else\u30d6\u30ed\u30c3\u30af\u306e\u305f\u3081\u306e\u30b3\u30fc\u30c9\u751f\u6210\u306f\u3001\u57fa\u672c\u7684\u306bthen\u306e\u5834\u5408\u3068\u540c\u3058\u3067\u3042\u308b\u3002<br \/>\n\u552f\u4e00\u306e\u91cd\u8981\u306a\u9055\u3044\u306f\u3001\u6700\u521d\u306e\u884c\u3067\u3001else\u30d6\u30ed\u30c3\u30af\u3092\u95a2\u6570\u3078\u8ffd\u52a0\u3057\u3066\u3044\u308b\u3002<br \/>\n\u5c11\u3057\u524d\u306b\u3001else\u30d6\u30ed\u30c3\u30af\u306f\u4f5c\u6210\u3055\u308c\u305f\u304c\u3001\u95a2\u6570\u3078\u8ffd\u52a0\u3055\u308c\u3066\u3044\u306a\u304b\u3063\u305f\u3053\u3068\u3092\u601d\u3044\u51fa\u305d\u3046\u3002<br \/>\n\u3053\u308c\u3067then\u30d6\u30ed\u30c3\u30af\u3068else\u30d6\u30ed\u30c3\u30af\u306f\u767a\u884c\u3055\u308c\u305f\u3002<br \/>\n\u5206\u5c90\u3092\u5408\u6d41\u3055\u305b\u308b\u30b3\u30fc\u30c9\uff08merge code\uff09\u306b\u3088\u3063\u3066\u3053\u308c\u3092\u4ed5\u4e0a\u3052\u3088\u3046\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">  \/\/ merge\u30d6\u30ed\u30c3\u30af\u306e\u767a\u884c\u3002\r\n  TheFunction-&gt;getBasicBlockList().push_back(MergeBB);\r\n  Builder.SetInsertPoint(MergeBB);\r\n  PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,\r\n                                  &quot;iftmp&quot;);\r\n\r\n  PN-&gt;addIncoming(ThenV, ThenBB);\r\n  PN-&gt;addIncoming(ElseV, ElseBB);\r\n  return PN;\r\n}<\/pre>\n<p>\u6700\u521d\u306e2\u884c\u306f\u3001\u4eca\u3068\u306a\u3063\u3066\u306f\u898b\u6163\u308c\u305f\u3060\u308d\u3046\u3002<br \/>\n\u6700\u521d\u306e\u884c\u306f\u3001\u95a2\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3078merge\u30d6\u30ed\u30c3\u30af\uff08\u4e0a\u306eelse\u30d6\u30ed\u30c3\u30af\u3068\u540c\u3058\u3088\u3046\u306b\u5b99\u3076\u3089\u308a\u3093\u306b\u306a\u3063\u3066\u3044\u305f\uff09\u3092\u8ffd\u52a0\u3057\u3066\u3044\u308b\u3002<br \/>\n\u6b21\u306e\u884c\u3067\u3001\u65b0\u3057\u304f\u751f\u6210\u3055\u308c\u305f\u30b3\u30fc\u30c9\u304cmerge\u30d6\u30ed\u30c3\u30af\u306b\u633f\u5165\u3055\u308c\u308b\u3088\u3046\u633f\u5165\u70b9\u3092\u5909\u66f4\u3057\u3066\u3044\u308b\u3002<br \/>\n\u3053\u308c\u304c\u5b8c\u4e86\u3057\u305f\u3089\u3001phi\u30ce\u30fc\u30c9\u3092\u751f\u6210\u3057\u3001phi\u30ce\u30fc\u30c9\u306e\u305f\u3081\u306e\u30d6\u30ed\u30c3\u30af\u3068\u5024\u306e\u30da\u30a2\u3092\u6e96\u5099\u3059\u308b\u3002<\/p>\n<p>\u6700\u5f8c\u306b\u3001\u3053\u306eCodegen\u95a2\u6570\u306f\u3001phi\u30ce\u30fc\u30c9\u3092if\/then\/else\u5f0f\u306b\u3088\u3063\u3066\u8a08\u7b97\u3055\u308c\u305f\u5024\u3068\u3057\u3066\u8fd4\u3059\u3002<br \/>\n\u524d\u7bc0\u306e\u4f8b\u3067\u306f\u3001\u3053\u306e\u623b\u308a\u5024\u306f\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u95a2\u6570\u306e\u30b3\u30fc\u30c9\u306e\u4e2d\u306b\u9001\u308a\u8fbc\u307e\u308c\u3001\u305d\u306e\u95a2\u6570\u306freturn\u547d\u4ee4\u3092\u751f\u6210\u3059\u308b\u3060\u308d\u3046\u3002<br \/>\n<em>\u8a33\u6ce8: \u524d\u7bc0\u306e\u4f8b\u3068\u3044\u3046\u306e\u306f\u591a\u5206\u30b3\u30ec\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">extern foo();\r\nextern bar();\r\ndef baz(x) if x then foo() else bar();<\/pre>\n<p><\/em><\/p>\n<p>\u3053\u308c\u3067\u3001\u4e07\u83ef\u93e1\u306b\u304a\u3044\u3066\u6761\u4ef6\u4ed8\u304d\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u80fd\u529b\u3092\u5f97\u305f\u3002<br \/>\n\u3053\u306e\u62e1\u5f35\u306b\u3088\u3063\u3066\u3001\u4e07\u83ef\u93e1\u306f\u3001\u3044\u308d\u3093\u306a\u7a2e\u985e\u306e\u6570\u5b66\u7684\u8a08\u7b97\u3092\u304a\u3053\u306a\u3048\u308b\u6975\u3081\u3066\u5b8c\u5168\u306a\u8a00\u8a9e\u3068\u306a\u3063\u305f\u3002<br \/>\n\u6b21\u306f\u3001\u975e\u95a2\u6570\u578b\u8a00\u8a9e\u3067\u304a\u306a\u3058\u307f\u306e\u4fbf\u5229\u306a\u5f0f\u3092\u8ffd\u52a0\u3059\u308b\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-2976","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\/2976","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=2976"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/2976\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=2976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=2976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=2976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}