compiler.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (C) 2004, 2007 Maciej W. Rozycki
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. */
  8. #ifndef _ASM_COMPILER_H
  9. #define _ASM_COMPILER_H
  10. /*
  11. * With GCC 4.5 onwards we can use __builtin_unreachable to indicate to the
  12. * compiler that a particular code path will never be hit. This allows it to be
  13. * optimised out of the generated binary.
  14. *
  15. * Unfortunately at least GCC 4.6.3 through 7.3.0 inclusive suffer from a bug
  16. * that can lead to instructions from beyond an unreachable statement being
  17. * incorrectly reordered into earlier delay slots if the unreachable statement
  18. * is the only content of a case in a switch statement. This can lead to
  19. * seemingly random behaviour, such as invalid memory accesses from incorrectly
  20. * reordered loads or stores. See this potential GCC fix for details:
  21. *
  22. * https://gcc.gnu.org/ml/gcc-patches/2015-09/msg00360.html
  23. *
  24. * It is unclear whether GCC 8 onwards suffer from the same issue - nothing
  25. * relevant is mentioned in GCC 8 release notes and nothing obviously relevant
  26. * stands out in GCC commit logs, but these newer GCC versions generate very
  27. * different code for the testcase which doesn't exhibit the bug.
  28. *
  29. * GCC also handles stack allocation suboptimally when calling noreturn
  30. * functions or calling __builtin_unreachable():
  31. *
  32. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82365
  33. *
  34. * We work around both of these issues by placing a volatile asm statement,
  35. * which GCC is prevented from reordering past, prior to __builtin_unreachable
  36. * calls.
  37. *
  38. * The .insn statement is required to ensure that any branches to the
  39. * statement, which sadly must be kept due to the asm statement, are known to
  40. * be branches to code and satisfy linker requirements for microMIPS kernels.
  41. */
  42. #undef barrier_before_unreachable
  43. #define barrier_before_unreachable() asm volatile(".insn")
  44. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
  45. #define GCC_IMM_ASM() "n"
  46. #define GCC_REG_ACCUM "$0"
  47. #else
  48. #define GCC_IMM_ASM() "rn"
  49. #define GCC_REG_ACCUM "accum"
  50. #endif
  51. #ifdef CONFIG_CPU_MIPSR6
  52. /* All MIPS R6 toolchains support the ZC constrain */
  53. #define GCC_OFF_SMALL_ASM() "ZC"
  54. #else
  55. #ifndef CONFIG_CPU_MICROMIPS
  56. #define GCC_OFF_SMALL_ASM() "R"
  57. #elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)
  58. #define GCC_OFF_SMALL_ASM() "ZC"
  59. #else
  60. #error "microMIPS compilation unsupported with GCC older than 4.9"
  61. #endif /* CONFIG_CPU_MICROMIPS */
  62. #endif /* CONFIG_CPU_MIPSR6 */
  63. #ifdef CONFIG_CPU_MIPSR6
  64. #define MIPS_ISA_LEVEL "mips64r6"
  65. #define MIPS_ISA_ARCH_LEVEL MIPS_ISA_LEVEL
  66. #define MIPS_ISA_LEVEL_RAW mips64r6
  67. #define MIPS_ISA_ARCH_LEVEL_RAW MIPS_ISA_LEVEL_RAW
  68. #else
  69. /* MIPS64 is a superset of MIPS32 */
  70. #define MIPS_ISA_LEVEL "mips64r2"
  71. #define MIPS_ISA_ARCH_LEVEL "arch=r4000"
  72. #define MIPS_ISA_LEVEL_RAW mips64r2
  73. #define MIPS_ISA_ARCH_LEVEL_RAW MIPS_ISA_LEVEL_RAW
  74. #endif /* CONFIG_CPU_MIPSR6 */
  75. #endif /* _ASM_COMPILER_H */