nllvmset.pas 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. {
  2. Copyright (c) 2019 by Jonas Maebe
  3. Generate LLVM bytecode for set/case nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nllvmset;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. nset, ncgset,
  22. symtype,
  23. cgbase;
  24. type
  25. tllvminnode = class(tcginnode)
  26. protected
  27. procedure in_reg_const(uopdef: tdef; opsize: tcgsize); override;
  28. end;
  29. tllvmcasenode = class(tcgcasenode)
  30. protected
  31. procedure genlinearlist(hp: pcaselabel); override;
  32. end;
  33. implementation
  34. uses
  35. globals,
  36. aasmbase, aasmdata,
  37. hlcgobj,
  38. llvminfo;
  39. procedure tllvminnode.in_reg_const(uopdef: tdef; opsize: tcgsize);
  40. var
  41. hl,hlend: TAsmLabel;
  42. begin
  43. if not(llvmflag_no_freeze in llvmversion_properties[current_settings.llvmversion]) then
  44. begin
  45. { if we have the freeze instruction, we can mark the poison value
  46. potentially generated by the shift as "replace with a fixed value
  47. if it's poison, we don't care since we'll mask it anyway" }
  48. inherited;
  49. exit;
  50. end;
  51. { don't perform the bit test if the register's value can be >=
  52. "number of bits in right.location", because that generates a poison
  53. value in LLVM (and even anding it will 0 will keep it a poison value),
  54. and calculations using poison as input result in undefined behaviour }
  55. current_asmdata.getjumplabel(hl);
  56. current_asmdata.getjumplabel(hlend);
  57. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,uopdef,OC_AE,
  58. right.resultdef.packedbitsize,left.location.register,hl);
  59. hlcg.a_bit_test_reg_loc_reg(current_asmdata.CurrAsmList,
  60. uopdef,right.resultdef,uopdef,
  61. left.location.register,right.location,location.register);
  62. hlcg.a_jmp_always(current_asmdata.CurrAsmList,hlend);
  63. hlcg.a_label(current_asmdata.CurrAsmList,hl);
  64. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,uopdef,0,location.register);
  65. hlcg.a_label(current_asmdata.CurrAsmList,hlend);
  66. end;
  67. procedure tllvmcasenode.genlinearlist(hp: pcaselabel);
  68. begin
  69. { genlinearlist constantly updates the case value in the register,
  70. which causes tons of spilling with LLVM due to the need to bring
  71. it back into SSA form. LLVM will recognise and optimise the linear
  72. cmp list just as well (or even better), while the code that FPC
  73. has to generate is much smaller (no spilling) }
  74. genlinearcmplist(hp);
  75. end;
  76. begin
  77. cinnode:=tllvminnode;
  78. ccasenode:=tllvmcasenode;
  79. end.