xref: /freebsd/share/mk/bsd.cpu.mk (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1
2# Set default CPU compile flags and baseline CPUTYPE for each arch.  The
3# compile flags must support the minimum CPU type for each architecture but
4# may tune support for more advanced processors.
5
6.if !defined(CPUTYPE) || empty(CPUTYPE)
7_CPUCFLAGS =
8. if ${MACHINE_CPUARCH} == "aarch64"
9MACHINE_CPU = arm64
10. elif ${MACHINE_CPUARCH} == "amd64"
11MACHINE_CPU = amd64 sse2 sse mmx
12. elif ${MACHINE_CPUARCH} == "arm"
13MACHINE_CPU = arm
14. elif ${MACHINE_CPUARCH} == "i386"
15MACHINE_CPU = i486
16. elif ${MACHINE_ARCH} == "powerpc"
17MACHINE_CPU = aim
18. elif ${MACHINE_ARCH} == "powerpc64"
19MACHINE_CPU = aim altivec
20. elif ${MACHINE_ARCH} == "powerpc64le"
21MACHINE_CPU = aim altivec vsx vsx2
22. elif ${MACHINE_CPUARCH} == "riscv"
23MACHINE_CPU = riscv
24. endif
25.else
26
27# Handle aliases (not documented in make.conf to avoid user confusion
28# between e.g. i586 and pentium)
29
30. if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
31.  if ${CPUTYPE} == "barcelona"
32CPUTYPE = amdfam10
33.  elif ${CPUTYPE} == "skx"
34CPUTYPE = skylake-avx512
35.  elif ${CPUTYPE} == "core-avx2"
36CPUTYPE = haswell
37.  elif ${CPUTYPE} == "core-avx-i"
38CPUTYPE = ivybridge
39.  elif ${CPUTYPE} == "corei7-avx"
40CPUTYPE = sandybridge
41.  elif ${CPUTYPE} == "corei7"
42CPUTYPE = nehalem
43.  elif ${CPUTYPE} == "slm"
44CPUTYPE = silvermont
45.  elif ${CPUTYPE} == "atom"
46CPUTYPE = bonnell
47.  elif ${CPUTYPE} == "core"
48CPUTYPE = prescott
49.  endif
50.  if ${MACHINE_CPUARCH} == "amd64"
51.   if ${CPUTYPE} == "prescott"
52CPUTYPE = nocona
53.   endif
54.  else
55.   if ${CPUTYPE} == "k7"
56CPUTYPE = athlon
57.   elif ${CPUTYPE} == "p4"
58CPUTYPE = pentium4
59.   elif ${CPUTYPE} == "p4m"
60CPUTYPE = pentium4m
61.   elif ${CPUTYPE} == "p3"
62CPUTYPE = pentium3
63.   elif ${CPUTYPE} == "p3m"
64CPUTYPE = pentium3m
65.   elif ${CPUTYPE} == "p-m"
66CPUTYPE = pentium-m
67.   elif ${CPUTYPE} == "p2"
68CPUTYPE = pentium2
69.   elif ${CPUTYPE} == "i686"
70CPUTYPE = pentiumpro
71.   elif ${CPUTYPE} == "i586/mmx"
72CPUTYPE = pentium-mmx
73.   elif ${CPUTYPE} == "i586"
74CPUTYPE = pentium
75.   endif
76.  endif
77. endif
78
79###############################################################################
80# Logic to set up correct gcc optimization flag.  This must be included
81# after /etc/make.conf so it can react to the local value of CPUTYPE
82# defined therein.  Consult:
83#	http://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html
84#	http://gcc.gnu.org/onlinedocs/gcc/RS-6000-and-PowerPC-Options.html
85#	http://gcc.gnu.org/onlinedocs/gcc/SPARC-Options.html
86#	http://gcc.gnu.org/onlinedocs/gcc/i386-and-x86_002d64-Options.html
87
88. if ${MACHINE_CPUARCH} == "i386"
89.  if ${CPUTYPE} == "crusoe"
90_CPUCFLAGS = -march=i686 -falign-functions=0 -falign-jumps=0 -falign-loops=0
91.  elif ${CPUTYPE} == "k5"
92_CPUCFLAGS = -march=pentium
93.  elif ${CPUTYPE} == "c7"
94_CPUCFLAGS = -march=c3-2
95.  else
96_CPUCFLAGS = -march=${CPUTYPE}
97.  endif
98. elif ${MACHINE_CPUARCH} == "amd64"
99_CPUCFLAGS = -march=${CPUTYPE}
100. elif ${MACHINE_CPUARCH} == "arm"
101.  if ${CPUTYPE} == "xscale"
102#XXX: gcc doesn't seem to like -mcpu=xscale, and dies while rebuilding itself
103#_CPUCFLAGS = -mcpu=xscale
104_CPUCFLAGS = -march=armv5te -D__XSCALE__
105.  elif ${CPUTYPE:M*soft*} != ""
106_CPUCFLAGS = -mfloat-abi=softfp
107.  elif ${CPUTYPE} == "cortexa"
108_CPUCFLAGS = -march=armv7 -mfpu=vfp
109.  elif ${CPUTYPE:Marmv[67]*} != ""
110# Handle all the armvX types that FreeBSD runs:
111#	armv7, armv7-a, armv7ve
112# they require -march=. All the others require -mcpu=.
113_CPUCFLAGS = -march=${CPUTYPE}
114.  else
115# Common values for FreeBSD
116# armv7: generic-armv7-a, cortex-a5, cortex-a7, cortex-a8, cortex-a9,
117#       cortex-a12, cortex-a15, cortex-a17
118#       cortex-a53, cortex-a57, cortex-a72,
119#       exynos-m1
120_CPUCFLAGS = -mcpu=${CPUTYPE}
121. endif
122. elif ${MACHINE_ARCH} == "powerpc"
123.  if ${CPUTYPE} == "e500"
124_CPUCFLAGS = -Wa,-me500 -msoft-float
125.  else
126_CPUCFLAGS = -mcpu=${CPUTYPE}
127.  endif
128. elif ${MACHINE_ARCH:Mpowerpc64*} != ""
129_CPUCFLAGS = -mcpu=${CPUTYPE}
130. elif ${MACHINE_CPUARCH} == "aarch64"
131.  if ${CPUTYPE:Marmv*} != ""
132# Use -march when the CPU type is an architecture value, e.g. armv8.1-a
133_CPUCFLAGS = -march=${CPUTYPE}
134.  else
135# Otherwise assume we have a CPU type
136_CPUCFLAGS = -mcpu=${CPUTYPE}
137.  endif
138. endif
139
140# Set up the list of CPU features based on the CPU type.  This is an
141# unordered list to make it easy for client makefiles to test for the
142# presence of a CPU feature.
143
144########## i386
145. if ${MACHINE_CPUARCH} == "i386"
146.  if ${CPUTYPE} == "znver4"
147MACHINE_CPU = avx512 avx2 avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586 f16c
148.  elif ${CPUTYPE} == "znver3" || ${CPUTYPE} == "znver2" || \
149    ${CPUTYPE} == "znver1"
150MACHINE_CPU = avx2 avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586 f16c
151.  elif ${CPUTYPE} == "bdver4"
152MACHINE_CPU = xop avx2 avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586 f16c
153.  elif ${CPUTYPE} == "bdver3" || ${CPUTYPE} == "bdver2"
154MACHINE_CPU = xop avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586 f16c
155.  elif ${CPUTYPE} == "bdver1"
156MACHINE_CPU = xop avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
157.  elif ${CPUTYPE} == "btver2"
158MACHINE_CPU = avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586 f16c
159.  elif ${CPUTYPE} == "btver1"
160MACHINE_CPU = ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
161.  elif ${CPUTYPE} == "amdfam10"
162MACHINE_CPU = athlon-xp athlon k7 3dnow sse4a sse3 sse2 sse mmx k6 k5 i586
163.  elif ${CPUTYPE} == "opteron-sse3" || ${CPUTYPE} == "athlon64-sse3"
164MACHINE_CPU = athlon-xp athlon k7 3dnow sse3 sse2 sse mmx k6 k5 i586
165.  elif ${CPUTYPE} == "opteron" || ${CPUTYPE} == "athlon64" || \
166    ${CPUTYPE} == "athlon-fx"
167MACHINE_CPU = athlon-xp athlon k7 3dnow sse2 sse mmx k6 k5 i586
168.  elif ${CPUTYPE} == "athlon-mp" || ${CPUTYPE} == "athlon-xp" || \
169    ${CPUTYPE} == "athlon-4"
170MACHINE_CPU = athlon-xp athlon k7 3dnow sse mmx k6 k5 i586
171.  elif ${CPUTYPE} == "athlon" || ${CPUTYPE} == "athlon-tbird"
172MACHINE_CPU = athlon k7 3dnow mmx k6 k5 i586
173.  elif ${CPUTYPE} == "k6-3" || ${CPUTYPE} == "k6-2" || ${CPUTYPE} == "geode"
174MACHINE_CPU = 3dnow mmx k6 k5 i586
175.  elif ${CPUTYPE} == "k6"
176MACHINE_CPU = mmx k6 k5 i586
177.  elif ${CPUTYPE} == "k5"
178MACHINE_CPU = k5 i586
179.  elif ${CPUTYPE} == "sapphirerapids" || ${CPUTYPE} == "tigerlake" || \
180    ${CPUTYPE} == "cooperlake" || ${CPUTYPE} == "cascadelake" || \
181    ${CPUTYPE} == "icelake-server" || ${CPUTYPE} == "icelake-client" || \
182    ${CPUTYPE} == "cannonlake" || ${CPUTYPE} == "knm" || \
183    ${CPUTYPE} == "skylake-avx512" || ${CPUTYPE} == "knl" || \
184    ${CPUTYPE} == "x86-64-v4"
185MACHINE_CPU = avx512 avx2 avx sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586 f16c
186.  elif ${CPUTYPE} == "alderlake" || ${CPUTYPE} == "skylake" || \
187    ${CPUTYPE} == "broadwell" || ${CPUTYPE} == "haswell" || \
188    ${CPUTYPE} == "x86-64-v3"
189MACHINE_CPU = avx2 avx sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586 f16c
190.  elif ${CPUTYPE} == "ivybridge"
191MACHINE_CPU = avx sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586 f16c
192.  elif ${CPUTYPE} == "sandybridge"
193MACHINE_CPU = avx sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586
194.  elif ${CPUTYPE} == "tremont" || ${CPUTYPE} == "goldmont-plus" || \
195    ${CPUTYPE} == "goldmont" || ${CPUTYPE} == "westmere" || \
196    ${CPUTYPE} == "nehalem" || ${CPUTYPE} == "silvermont" || \
197    ${CPUTYPE} == "x86-64-v2"
198MACHINE_CPU = sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586
199.  elif ${CPUTYPE} == "penryn"
200MACHINE_CPU = sse41 ssse3 sse3 sse2 sse i686 mmx i586
201.  elif ${CPUTYPE} == "core2" || ${CPUTYPE} == "bonnell"
202MACHINE_CPU = ssse3 sse3 sse2 sse i686 mmx i586
203.  elif ${CPUTYPE} == "yonah" || ${CPUTYPE} == "prescott"
204MACHINE_CPU = sse3 sse2 sse i686 mmx i586
205.  elif ${CPUTYPE} == "pentium4" || ${CPUTYPE} == "pentium4m" || \
206    ${CPUTYPE} == "pentium-m" || ${CPUTYPE} == "x86-64"
207MACHINE_CPU = sse2 sse i686 mmx i586
208.  elif ${CPUTYPE} == "pentium3" || ${CPUTYPE} == "pentium3m"
209MACHINE_CPU = sse i686 mmx i586
210.  elif ${CPUTYPE} == "pentium2"
211MACHINE_CPU = i686 mmx i586
212.  elif ${CPUTYPE} == "pentiumpro"
213MACHINE_CPU = i686 i586
214.  elif ${CPUTYPE} == "pentium-mmx"
215MACHINE_CPU = mmx i586
216.  elif ${CPUTYPE} == "pentium"
217MACHINE_CPU = i586
218.  elif ${CPUTYPE} == "c7"
219MACHINE_CPU = sse3 sse2 sse i686 mmx i586
220.  elif ${CPUTYPE} == "c3-2"
221MACHINE_CPU = sse i686 mmx i586
222.  elif ${CPUTYPE} == "c3"
223MACHINE_CPU = 3dnow mmx i586
224.  elif ${CPUTYPE} == "winchip2"
225MACHINE_CPU = 3dnow mmx
226.  elif ${CPUTYPE} == "winchip-c6"
227MACHINE_CPU = mmx
228.  endif
229MACHINE_CPU += i486
230########## amd64
231. elif ${MACHINE_CPUARCH} == "amd64"
232.  if ${CPUTYPE} == "znver4"
233MACHINE_CPU = avx512 avx2 avx sse42 sse41 ssse3 sse4a sse3 f16c
234.  elif ${CPUTYPE} == "znver3" || ${CPUTYPE} == "znver2" || \
235    ${CPUTYPE} == "znver1"
236MACHINE_CPU = avx2 avx sse42 sse41 ssse3 sse4a sse3 f16c
237.  elif ${CPUTYPE} == "bdver4"
238MACHINE_CPU = xop avx2 avx sse42 sse41 ssse3 sse4a sse3 f16c
239.  elif ${CPUTYPE} == "bdver3" || ${CPUTYPE} == "bdver2"
240MACHINE_CPU = xop avx sse42 sse41 ssse3 sse4a sse3 f16c
241.  elif ${CPUTYPE} == "bdver1"
242MACHINE_CPU = xop avx sse42 sse41 ssse3 sse4a sse3
243.  elif ${CPUTYPE} == "btver2"
244MACHINE_CPU = avx sse42 sse41 ssse3 sse4a sse3 f16c
245.  elif ${CPUTYPE} == "btver1"
246MACHINE_CPU = ssse3 sse4a sse3
247.  elif ${CPUTYPE} == "amdfam10"
248MACHINE_CPU = k8 3dnow sse4a sse3
249.  elif ${CPUTYPE} == "opteron-sse3" || ${CPUTYPE} == "athlon64-sse3" || \
250    ${CPUTYPE} == "k8-sse3"
251MACHINE_CPU = k8 3dnow sse3
252.  elif ${CPUTYPE} == "opteron" || ${CPUTYPE} == "athlon64" || \
253    ${CPUTYPE} == "athlon-fx" || ${CPUTYPE} == "k8"
254MACHINE_CPU = k8 3dnow
255.  elif ${CPUTYPE} == "sapphirerapids" || ${CPUTYPE} == "tigerlake" || \
256    ${CPUTYPE} == "cooperlake" || ${CPUTYPE} == "cascadelake" || \
257    ${CPUTYPE} == "icelake-server" || ${CPUTYPE} == "icelake-client" || \
258    ${CPUTYPE} == "cannonlake" || ${CPUTYPE} == "knm" || \
259    ${CPUTYPE} == "skylake-avx512" || ${CPUTYPE} == "knl" || \
260    ${CPUTYPE} == "x86-64-v4"
261MACHINE_CPU = avx512 avx2 avx sse42 sse41 ssse3 sse3 f16c
262.  elif ${CPUTYPE} == "alderlake" || ${CPUTYPE} == "skylake" || \
263    ${CPUTYPE} == "broadwell" || ${CPUTYPE} == "haswell" || \
264    ${CPUTYPE} == "x86-64-v3"
265MACHINE_CPU = avx2 avx sse42 sse41 ssse3 sse3 f16c
266.  elif ${CPUTYPE} == "ivybridge"
267MACHINE_CPU = avx sse42 sse41 ssse3 sse3 f16c
268.  elif ${CPUTYPE} == "sandybridge"
269MACHINE_CPU = avx sse42 sse41 ssse3 sse3
270.  elif ${CPUTYPE} == "tremont" || ${CPUTYPE} == "goldmont-plus" || \
271    ${CPUTYPE} == "goldmont" || ${CPUTYPE} == "westmere" || \
272    ${CPUTYPE} == "nehalem" || ${CPUTYPE} == "silvermont" || \
273    ${CPUTYPE} == "x86-64-v2"
274MACHINE_CPU = sse42 sse41 ssse3 sse3
275.  elif ${CPUTYPE} == "penryn"
276MACHINE_CPU = sse41 ssse3 sse3
277.  elif ${CPUTYPE} == "core2" || ${CPUTYPE} == "bonnell"
278MACHINE_CPU = ssse3 sse3
279.  elif ${CPUTYPE} == "nocona"
280MACHINE_CPU = sse3
281.  endif
282MACHINE_CPU += amd64 sse2 sse mmx
283########## powerpc
284. elif ${MACHINE_ARCH} == "powerpc"
285.  if ${CPUTYPE} == "e500"
286MACHINE_CPU = booke softfp
287.  elif ${CPUTYPE} == "g4"
288MACHINE_CPU = aim altivec
289.  else
290MACHINE_CPU= aim
291.  endif
292. elif ${MACHINE_ARCH} == "powerpc64"
293.  if ${CPUTYPE} == "e5500"
294MACHINE_CPU = booke
295.  elif ${CPUTYPE} == power7
296MACHINE_CPU = altivec vsx
297.  elif ${CPUTYPE} == power8
298MACHINE_CPU = altivec vsx vsx2
299.  elif ${CPUTYPE} == power9
300MACHINE_CPU = altivec vsx vsx2 vsx3
301.  else
302MACHINE_CPU = aim altivec
303.  endif
304. elif ${MACHINE_ARCH} == "powerpc64le"
305MACHINE_CPU = aim altivec vsx vsx2
306.  if ${CPUTYPE} == power9
307MACHINE_CPU += vsx3
308.  endif
309########## riscv
310. elif ${MACHINE_CPUARCH} == "riscv"
311MACHINE_CPU = riscv
312. endif
313.endif
314
315########## arm
316.if ${MACHINE_CPUARCH} == "arm"
317MACHINE_CPU += arm
318. if ${MACHINE_ARCH:Marmv7*} != ""
319MACHINE_CPU += armv7
320. endif
321# Normally armv7 is hard float ABI from FreeBSD 11 onwards. However
322# when CPUTYPE has 'soft' in it, we use the soft-float ABI to allow building of
323# soft-float ABI libraries. In this case, we have to add the -mfloat-abi=softfp
324# to force that.
325. if defined(CPUTYPE) && ${CPUTYPE:M*soft*} != ""
326# Needs to be CFLAGS not _CPUCFLAGS because it's needed for the ABI
327# not a nice optimization. Please note: softfp ABI uses hardware floating
328# instructions, but passes arguments to function calls in integer regsiters.
329# -mfloat-abi=soft is full software floating point, but is not currently
330# supported. softfp support in FreeBSD may disappear in FreeBSD 13.0 since
331# it was a transition tool from FreeBSD 10 to 11 and is a bit of an odd duck.
332CFLAGS += -mfloat-abi=softfp
333. endif
334.endif
335
336.if ${MACHINE_ARCH} == "powerpc" || ${MACHINE_ARCH} == "powerpcspe"
337LDFLAGS.bfd+= -Wl,--secure-plt
338.endif
339
340.if ${MACHINE_ARCH} == "powerpcspe"
341CFLAGS += -mcpu=8548 -mspe
342CFLAGS.gcc+= -mabi=spe -mfloat-gprs=double -Wa,-me500
343.endif
344
345.if ${MACHINE_CPUARCH} == "riscv"
346CFLAGS += -march=rv64imafdc -mabi=lp64d
347.endif
348
349# NB: COPTFLAGS is handled in /usr/src/sys/conf/kern.pre.mk
350
351.if !defined(NO_CPU_CFLAGS)
352CFLAGS += ${_CPUCFLAGS}
353.endif
354
355#
356# Prohibit the compiler from emitting SIMD instructions.
357# These flags are added to CFLAGS in areas where the extra context-switch
358# cost outweighs the advantages of SIMD instructions.
359#
360# gcc:
361# Setting -mno-mmx implies -mno-3dnow
362# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
363#
364# clang:
365# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
366# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and
367# -mno-sse42
368# (-mfpmath= is not supported)
369#
370.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
371CFLAGS_NO_SIMD.clang= -mno-avx -mno-avx2
372CFLAGS_NO_SIMD= -mno-mmx -mno-sse
373.endif
374CFLAGS_NO_SIMD += ${CFLAGS_NO_SIMD.${COMPILER_TYPE}}
375
376# Add in any architecture-specific CFLAGS.
377# These come from make.conf or the command line or the environment.
378CFLAGS += ${CFLAGS.${MACHINE_ARCH}}
379CXXFLAGS += ${CXXFLAGS.${MACHINE_ARCH}}
380
381#
382# MACHINE_ABI is a list of properties about the ABI used for MACHINE_ARCH.
383# The following properties are indicated with one of the follow values:
384#
385# Byte order:			big-endian, little-endian
386# Floating point ABI:		soft-float, hard-float
387# Size of long (size_t, etc):	long32, long64
388# Pointer type:			ptr32, ptr64
389# Size of time_t:		time32, time64
390#
391.if (${MACHINE} == "arm" && (defined(CPUTYPE) && ${CPUTYPE:M*soft*})) || \
392    (${MACHINE_ARCH} == "powerpc" && (defined(CPUTYPE) && ${CPUTYPE} == "e500"))
393MACHINE_ABI+=	soft-float
394.else
395MACHINE_ABI+=	hard-float
396.endif
397# Currently all 64-bit architectures include 64 in their name (see arch(7)).
398.if ${MACHINE_ARCH:M*64*}
399MACHINE_ABI+=  long64
400.else
401MACHINE_ABI+=  long32
402.endif
403.if ${MACHINE_ABI:Mlong64}
404MACHINE_ABI+=  ptr64
405.else
406MACHINE_ABI+=  ptr32
407.endif
408.if ${MACHINE_ARCH} == "i386"
409MACHINE_ABI+=  time32
410.else
411MACHINE_ABI+=  time64
412.endif
413.if ${MACHINE_ARCH:Mpowerpc*} && !${MACHINE_ARCH:M*le}
414MACHINE_ABI+=	big-endian
415.else
416MACHINE_ABI+=	little-endian
417.endif
418