Pascal - inc -> assembler - add? lazarus / 62-General ---------------------------------------------------------------------- Seenkao · 2021-08-06 18:16 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin  i := 0;  inc(i);        // to assembler: add $0x1, %r14dend; Почему не используется инструкция ассемблера INC? Если это невозможно для данного регистра, почему не используется один из основных регистров? google translate: Why isn't the INC assembler instruction being used? If this is not possible for a given register, why is one of the main registers not being used? ---------------------------------------------------------------------- MathMan · 2021-08-07 08:36 Hello Seenkao, I need to speculate a bit because I am not involved in FPC compiler code generation development, but * not all processor architectures have an 'inc' instruction * on modern x86-64 CPUs 'inc' is not faster/better than 'add' <= except some very rare and tricky corner cases * in FPC you can also do e.g. 'Inc( i, 5 )', which can only be translated to an 'add' instruction anyway In combination the above will lead to a much simpler code generation if asm 'add' is used for Pascal 'inc()' throughout, i guess. Cheers, MathMan ---------------------------------------------------------------------- Kays · 2021-08-07 11:43 Quote from: Seenkao on August 06, 2021, 08:16:07 pm[…] Why isn't the INC assembler instruction being used? […]Potential partial flags stall. Quote from: Seenkao on August 06, 2021, 08:16:07 pm[…] If this is not possible for a given register, why is one of the main registers not being used?That’s how {$optimization regVar} is implemented. It would increase the compiler’s complexity if you wanted to use, for instance, rax for that purpose, yet the gain in performance is minimal/non- existent. Certainly you will need the REX prefix for any instruction using one of the r8–r15 registers, but the underlying operation (here add) performs just as fast, you know. ---------------------------------------------------------------------- Leledumbo · 2021-08-07 12:48 You want inc and all of the consequences written by Kays? Simple, optimize for size with -Os. ---------------------------------------------------------------------- PascalDragon · 2021-08-07 15:31 Quote from: Seenkao on August 06, 2021, 08:16:07 pmWhy isn't the INC assembler instruction being used? If this is not possible for a given register, why is one of the main registers not being used? To quote from the compiler's sources (compiler/x86/cgx86.pas, line 197): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     { modern CPUs prefer add/sub over inc/dec because add/sub break instructions dependencies on flags      because they modify all flags } ---------------------------------------------------------------------- 440bx · 2021-08-07 15:55 The number of subtleties involved in generating and optimizing code are frightening. It's much too easy to make incorrect, or at least questionable assumptions, about the behavior of a series of instructions. ---------------------------------------------------------------------- Seenkao · 2021-08-07 17:38 Благодарю за информацию! Давно не занимался ассемблером... надо обновить знания. Google translate: Thanks for the information! I haven't studied assembler for a long time ... I need to update my knowledge. ---------------------------------------------------------------------- MathMan · 2021-08-09 09:35 Quote from: 440bx on August 07, 2021, 05:55:45 pmThe number of subtleties involved in generating and optimizing code are frightening. It's much too easy to make incorrect, or at least questionable assumptions, about the behavior of a series of instructions. And it only starts with the code generated by the compiler. Each hardware implementation of an architecture (e.g. x86-64 instruction set) has subtle quirks that are not visible from the outside. Since some time there are "superoptimizers" <= programs that take as input a sequence of assembler instructions (generated by hand or out of a compiler), then shuffle the instructions within given limits (maintaining execution results) and finally measure execution speed of the different variants. On modern CPU (e.g. Intel Skylake) one can regularly see speed improvements of ~5%. But even these "superoptimizers" will not achieve optimum, because they are bound by the instructions given in the input. I can show you examples, where I achieved a speed improvement of ~20% by inserting a "useless" AVX2 instruction into a core loop which otherwise only uses general purpose registers. Nobody could explain this to me so far - and yes, I did address this to Intel directly too. Cheers, MathMan ---------------------------------------------------------------------- JuhaManninen · 2021-08-09 14:28 Interesting. I haven't done assembly programming since 1980's but I follow CPU architecture innovations. I learned the details of RISC-V design. Wow, just amazing! A true RISC architecture with optional extensions. Support for modern optimization techniques are thought of and built in the design. This has nothing to do with Pascal, thus possible further discussion should happen in the "Other" section. Still, amazing.