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