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