{"action":"created","issue":{"url":"https://api.github.com/repos/kripken/emscripten/issues/3231","labels_url":"https://api.github.com/repos/kripken/emscripten/issues/3231/labels{/name}","comments_url":"https://api.github.com/repos/kripken/emscripten/issues/3231/comments","events_url":"https://api.github.com/repos/kripken/emscripten/issues/3231/events","html_url":"https://github.com/kripken/emscripten/issues/3231","id":60070892,"number":3231,"title":"Interesting code generated for 8 bit stuff","user":{"login":"waywardmonkeys","id":178582,"avatar_url":"https://avatars.githubusercontent.com/u/178582?v=3","gravatar_id":"","url":"https://api.github.com/users/waywardmonkeys","html_url":"https://github.com/waywardmonkeys","followers_url":"https://api.github.com/users/waywardmonkeys/followers","following_url":"https://api.github.com/users/waywardmonkeys/following{/other_user}","gists_url":"https://api.github.com/users/waywardmonkeys/gists{/gist_id}","starred_url":"https://api.github.com/users/waywardmonkeys/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/waywardmonkeys/subscriptions","organizations_url":"https://api.github.com/users/waywardmonkeys/orgs","repos_url":"https://api.github.com/users/waywardmonkeys/repos","events_url":"https://api.github.com/users/waywardmonkeys/events{/privacy}","received_events_url":"https://api.github.com/users/waywardmonkeys/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-03-06T07:59:29Z","updated_at":"2015-03-09T18:01:35Z","closed_at":null,"body":"I noticed that our code base has 375 instances of this construct: ``someVar << 24 >> 24``\r\n\r\nThat seemed interesting to me, so I took a particularly interesting case:\r\n\r\n````javascript\r\n  while (1) {\r\n   $50 = $51;\r\n   $52 = HEAP8[$50 >> 0] | 0;\r\n   if (!($52 << 24 >> 24 == 9 | $52 << 24 >> 24 == 13 | $52 << 24 >> 24 == 10 | $52 << 24 >> 24 == 32)) break;\r\n   $51 = $50 + 1 | 0;\r\n  }\r\n````\r\n\r\nThat comes from this LLVM code:\r\n\r\n````llvm\r\n  %48 = phi i32 [ %52, %.critedge.i3 ], [ %46, %44 ]\r\n  %49 = inttoptr i32 %48 to i8*\r\n  %50 = load i8* %49, align 1, !tbaa !21\r\n  switch i8 %50, label %_ZN9rapidjson14SkipWhitespaceINS_19GenericStringStreamINS_4UTF8IcEEEEEEvRT_.exit4 [\r\n    i8 32, label %.critedge.i3\r\n    i8 10, label %.critedge.i3\r\n    i8 13, label %.critedge.i3\r\n    i8 9, label %.critedge.i3\r\n  ]\r\n````\r\n\r\nWhich comes from this C++:\r\n\r\n````c++\r\n    while (s.Peek() == ' ' || s.Peek() == '\\n' || s.Peek() == '\\r' || s.Peek() == '\\t')\r\n        s.Take();\r\n````\r\n\r\nI don't understand why the ``<< 24 >> 24`` is needed in this case. We're loading from HEAP8, so we already know that it is an 8 bit value.  Also, if that was needed, why does that have to happen 4 times?\r\n\r\nHere are some of the other lines in our generated JS where this happens:\r\n\r\n````js\r\n     if ($30 << 24 >> 24 != $31 << 24 >> 24) break;\r\n   if ($66 << 24 >> 24 != $67 << 24 >> 24) break;\r\n      if ($30 << 24 >> 24 != $31 << 24 >> 24) break;\r\n     if ($66 << 24 >> 24 != $67 << 24 >> 24) break;\r\n     if ($34 << 24 >> 24 != $35 << 24 >> 24) break;\r\n     if ($62 << 24 >> 24 != $63 << 24 >> 24) break;\r\n    if ($29 << 24 >> 24 != $30 << 24 >> 24) break;\r\n    if ($57 << 24 >> 24 != $58 << 24 >> 24) break;\r\n      if ($30 << 24 >> 24 != $31 << 24 >> 24) break;\r\n     if ($66 << 24 >> 24 != $67 << 24 >> 24) break;\r\n   HEAP32[$arg >> 2] = ($38 & 255) << 24 >> 24;\r\n   HEAP32[$arg + 4 >> 2] = ((($38 & 255) << 24 >> 24 | 0) < 0) << 31 >> 31;\r\n        if ((HEAP8[$$014$i$i$i >> 0] | 0) == $69 << 24 >> 24) {\r\n  if (!($16 << 24 >> 24 == 9 | $16 << 24 >> 24 == 13 | $16 << 24 >> 24 == 10 | $16 << 24 >> 24 == 32)) break;\r\n   if (!($34 << 24 >> 24 == 9 | $34 << 24 >> 24 == 13 | $34 << 24 >> 24 == 10 | $34 << 24 >> 24 == 32)) break;\r\n   if (!($52 << 24 >> 24 == 9 | $52 << 24 >> 24 == 13 | $52 << 24 >> 24 == 10 | $52 << 24 >> 24 == 32)) break;\r\n    if ($29 << 24 >> 24 != $30 << 24 >> 24) break;\r\n   if ($14 << 24 >> 24 == 32) {\r\n   if ((($14 << 24 >> 24) + -9 | 0) >>> 0 < 5) {\r\n    if ($26 << 24 >> 24 == 32) $56 = $32; else if ((($26 << 24 >> 24) + -9 | 0) >>> 0 < 5) $56 = $32; else {\r\n     if ($27 << 24 >> 24 != $28 << 24 >> 24) break;\r\n   if ((HEAP8[$s$013 >> 0] | 0) == ($c & 255) << 24 >> 24) {\r\n L6 : do if ((label | 0) == 5) if ($$lcssa) if ((HEAP8[$s$0$lcssa >> 0] | 0) == ($c & 255) << 24 >> 24) {\r\n    if ((HEAP8[$s$13 >> 0] | 0) == ($c & 255) << 24 >> 24) {\r\n if (!($typeCount << 24 >> 24)) {\r\n  $19 = $18 << 24 >> 24 != 0;\r\n  $19 = $18 << 24 >> 24 != 0;\r\n $19 = $18 << 24 >> 24 != 0;\r\n    if ($31 << 24 >> 24 != $32 << 24 >> 24) break;\r\n     if ($35) if ((HEAP8[$35 + 17 >> 0] | 0) == $attributeType << 24 >> 24) break L2;\r\n if ($14 << 24 >> 24 != 0 | ($0 | 0) == 0) {\r\n  $25 = $24 << 24 >> 24 == 0;\r\n $25 = $24 << 24 >> 24 == 0;\r\n  $13 = ($10 << 24 >> 24) + ($codepoint$09 << 4) | 0;\r\n  do if (($10 + -48 << 24 >> 24 & 255) < 10) $codepoint$1 = $13 + -48 | 0; else {\r\n   if (($10 + -65 << 24 >> 24 & 255) < 6) {\r\n   if (($10 + -97 << 24 >> 24 & 255) >= 6) {\r\n   if ($25 << 24 >> 24 != $26 << 24 >> 24) break;\r\n   if ($15 << 24 >> 24 != $16 << 24 >> 24) break;\r\n  if ($0 << 24 >> 24 == 32) {\r\n   if ((($0 << 24 >> 24) + -9 | 0) >>> 0 < 5) {\r\n if (($0 << 24 >> 24 | 0) == 43) {\r\n } else if (($0 << 24 >> 24 | 0) == 45) {\r\n if ((($8 << 24 >> 24) + -48 | 0) >>> 0 < 10) {\r\n   $15 = ($n$03 * 10 | 0) + 48 - ($14 << 24 >> 24) | 0;\r\n   if ((($14 << 24 >> 24) + -48 | 0) >>> 0 >= 10) {\r\n  if ($8 << 24 >> 24 == 2) {\r\n  if (!($8 << 24 >> 24)) {\r\n  } else if ($8 << 24 >> 24 != 1) {\r\n  $$count$0 = ((HEAP8[(HEAP32[$6 >> 2] | 0) + 48 >> 0] | 0) == $role << 24 >> 24 & 1) + $count$01 | 0;\r\n````\r\n"},"comment":{"url":"https://api.github.com/repos/kripken/emscripten/issues/comments/77907970","html_url":"https://github.com/kripken/emscripten/issues/3231#issuecomment-77907970","issue_url":"https://api.github.com/repos/kripken/emscripten/issues/3231","id":77907970,"user":{"login":"kripken","id":173661,"avatar_url":"https://avatars.githubusercontent.com/u/173661?v=3","gravatar_id":"","url":"https://api.github.com/users/kripken","html_url":"https://github.com/kripken","followers_url":"https://api.github.com/users/kripken/followers","following_url":"https://api.github.com/users/kripken/following{/other_user}","gists_url":"https://api.github.com/users/kripken/gists{/gist_id}","starred_url":"https://api.github.com/users/kripken/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kripken/subscriptions","organizations_url":"https://api.github.com/users/kripken/orgs","repos_url":"https://api.github.com/users/kripken/repos","events_url":"https://api.github.com/users/kripken/events{/privacy}","received_events_url":"https://api.github.com/users/kripken/received_events","type":"User","site_admin":false},"created_at":"2015-03-09T18:01:35Z","updated_at":"2015-03-09T18:01:35Z","body":"`x << 24 >> 24` is a sign extend of an 8-bit value to 32-bit. The same with 31 is a sign extend of a boolean, etc.\r\n\r\nCan you perhaps make a standalone C testcase? Would help to figure this out.\r\n\r\nMeanwhile, my first guess is that the switch code needs 32-bit ints, so it sign-extends the input, but not in a way that does it only once and stores to a temp variable. But that seems odd. Another possibility is that this is the result of  the aggressive eliminator, which will happily produce such repeated code, in order to avoid temporary variables."}}