xref: /freebsd/share/mk/bsd.cpu.mk (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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#	armv6, armv6t2, 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# arm: (any arm v4 or v5 processor you are targeting)
117#	arm920t, arm926ej-s, marvell-pj4, fa526, fa626,
118#	fa606te, fa626te, fa726te
119# armv6:
120# 	arm1176jzf-s
121# armv7: generic-armv7-a, cortex-a5, cortex-a7, cortex-a8, cortex-a9,
122#       cortex-a12, cortex-a15, cortex-a17
123#       cortex-a53, cortex-a57, cortex-a72,
124#       exynos-m1
125_CPUCFLAGS = -mcpu=${CPUTYPE}
126. endif
127. elif ${MACHINE_ARCH} == "powerpc"
128.  if ${CPUTYPE} == "e500"
129_CPUCFLAGS = -Wa,-me500 -msoft-float
130.  else
131_CPUCFLAGS = -mcpu=${CPUTYPE}
132.  endif
133. elif ${MACHINE_ARCH:Mpowerpc64*} != ""
134_CPUCFLAGS = -mcpu=${CPUTYPE}
135. elif ${MACHINE_CPUARCH} == "aarch64"
136.  if ${CPUTYPE:Marmv*} != ""
137# Use -march when the CPU type is an architecture value, e.g. armv8.1-a
138_CPUCFLAGS = -march=${CPUTYPE}
139.  else
140# Otherwise assume we have a CPU type
141_CPUCFLAGS = -mcpu=${CPUTYPE}
142.  endif
143. endif
144
145# Set up the list of CPU features based on the CPU type.  This is an
146# unordered list to make it easy for client makefiles to test for the
147# presence of a CPU feature.
148
149########## i386
150. if ${MACHINE_CPUARCH} == "i386"
151.  if ${CPUTYPE} == "znver4"
152MACHINE_CPU = avx512 avx2 avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
153.  elif ${CPUTYPE} == "znver3" || ${CPUTYPE} == "znver2" || \
154    ${CPUTYPE} == "znver1"
155MACHINE_CPU = avx2 avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
156.  elif ${CPUTYPE} == "bdver4"
157MACHINE_CPU = xop avx2 avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
158.  elif ${CPUTYPE} == "bdver3" || ${CPUTYPE} == "bdver2" || \
159    ${CPUTYPE} == "bdver1"
160MACHINE_CPU = xop avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
161.  elif ${CPUTYPE} == "btver2"
162MACHINE_CPU = avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
163.  elif ${CPUTYPE} == "btver1"
164MACHINE_CPU = ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
165.  elif ${CPUTYPE} == "amdfam10"
166MACHINE_CPU = athlon-xp athlon k7 3dnow sse4a sse3 sse2 sse mmx k6 k5 i586
167.  elif ${CPUTYPE} == "opteron-sse3" || ${CPUTYPE} == "athlon64-sse3"
168MACHINE_CPU = athlon-xp athlon k7 3dnow sse3 sse2 sse mmx k6 k5 i586
169.  elif ${CPUTYPE} == "opteron" || ${CPUTYPE} == "athlon64" || \
170    ${CPUTYPE} == "athlon-fx"
171MACHINE_CPU = athlon-xp athlon k7 3dnow sse2 sse mmx k6 k5 i586
172.  elif ${CPUTYPE} == "athlon-mp" || ${CPUTYPE} == "athlon-xp" || \
173    ${CPUTYPE} == "athlon-4"
174MACHINE_CPU = athlon-xp athlon k7 3dnow sse mmx k6 k5 i586
175.  elif ${CPUTYPE} == "athlon" || ${CPUTYPE} == "athlon-tbird"
176MACHINE_CPU = athlon k7 3dnow mmx k6 k5 i586
177.  elif ${CPUTYPE} == "k6-3" || ${CPUTYPE} == "k6-2" || ${CPUTYPE} == "geode"
178MACHINE_CPU = 3dnow mmx k6 k5 i586
179.  elif ${CPUTYPE} == "k6"
180MACHINE_CPU = mmx k6 k5 i586
181.  elif ${CPUTYPE} == "k5"
182MACHINE_CPU = k5 i586
183.  elif ${CPUTYPE} == "sapphirerapids" || ${CPUTYPE} == "tigerlake" || \
184    ${CPUTYPE} == "cooperlake" || ${CPUTYPE} == "cascadelake" || \
185    ${CPUTYPE} == "icelake-server" || ${CPUTYPE} == "icelake-client" || \
186    ${CPUTYPE} == "cannonlake" || ${CPUTYPE} == "knm" || \
187    ${CPUTYPE} == "skylake-avx512" || ${CPUTYPE} == "knl" || \
188    ${CPUTYPE} == "x86-64-v4"
189MACHINE_CPU = avx512 avx2 avx sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586
190.  elif ${CPUTYPE} == "alderlake" || ${CPUTYPE} == "skylake" || \
191    ${CPUTYPE} == "broadwell" || ${CPUTYPE} == "haswell" || \
192    ${CPUTYPE} == "x86-64-v3"
193MACHINE_CPU = avx2 avx sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586
194.  elif ${CPUTYPE} == "ivybridge" || ${CPUTYPE} == "sandybridge"
195MACHINE_CPU = avx sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586
196.  elif ${CPUTYPE} == "tremont" || ${CPUTYPE} == "goldmont-plus" || \
197    ${CPUTYPE} == "goldmont" || ${CPUTYPE} == "westmere" || \
198    ${CPUTYPE} == "nehalem" || ${CPUTYPE} == "silvermont" || \
199    ${CPUTYPE} == "x86-64-v2"
200MACHINE_CPU = sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586
201.  elif ${CPUTYPE} == "penryn"
202MACHINE_CPU = sse41 ssse3 sse3 sse2 sse i686 mmx i586
203.  elif ${CPUTYPE} == "core2" || ${CPUTYPE} == "bonnell"
204MACHINE_CPU = ssse3 sse3 sse2 sse i686 mmx i586
205.  elif ${CPUTYPE} == "yonah" || ${CPUTYPE} == "prescott"
206MACHINE_CPU = sse3 sse2 sse i686 mmx i586
207.  elif ${CPUTYPE} == "pentium4" || ${CPUTYPE} == "pentium4m" || \
208    ${CPUTYPE} == "pentium-m" || ${CPUTYPE} == "x86-64"
209MACHINE_CPU = sse2 sse i686 mmx i586
210.  elif ${CPUTYPE} == "pentium3" || ${CPUTYPE} == "pentium3m"
211MACHINE_CPU = sse i686 mmx i586
212.  elif ${CPUTYPE} == "pentium2"
213MACHINE_CPU = i686 mmx i586
214.  elif ${CPUTYPE} == "pentiumpro"
215MACHINE_CPU = i686 i586
216.  elif ${CPUTYPE} == "pentium-mmx"
217MACHINE_CPU = mmx i586
218.  elif ${CPUTYPE} == "pentium"
219MACHINE_CPU = i586
220.  elif ${CPUTYPE} == "c7"
221MACHINE_CPU = sse3 sse2 sse i686 mmx i586
222.  elif ${CPUTYPE} == "c3-2"
223MACHINE_CPU = sse i686 mmx i586
224.  elif ${CPUTYPE} == "c3"
225MACHINE_CPU = 3dnow mmx i586
226.  elif ${CPUTYPE} == "winchip2"
227MACHINE_CPU = 3dnow mmx
228.  elif ${CPUTYPE} == "winchip-c6"
229MACHINE_CPU = mmx
230.  endif
231MACHINE_CPU += i486
232########## amd64
233. elif ${MACHINE_CPUARCH} == "amd64"
234.  if ${CPUTYPE} == "znver4"
235MACHINE_CPU = avx512 avx2 avx sse42 sse41 ssse3 sse4a sse3
236.  elif ${CPUTYPE} == "znver3" || ${CPUTYPE} == "znver2" || \
237    ${CPUTYPE} == "znver1"
238MACHINE_CPU = avx2 avx sse42 sse41 ssse3 sse4a sse3
239.  elif ${CPUTYPE} == "bdver4"
240MACHINE_CPU = xop avx2 avx sse42 sse41 ssse3 sse4a sse3
241.  elif ${CPUTYPE} == "bdver3" || ${CPUTYPE} == "bdver2" || \
242    ${CPUTYPE} == "bdver1"
243MACHINE_CPU = xop avx sse42 sse41 ssse3 sse4a sse3
244.  elif ${CPUTYPE} == "btver2"
245MACHINE_CPU = avx sse42 sse41 ssse3 sse4a sse3
246.  elif ${CPUTYPE} == "btver1"
247MACHINE_CPU = ssse3 sse4a sse3
248.  elif ${CPUTYPE} == "amdfam10"
249MACHINE_CPU = k8 3dnow sse4a sse3
250.  elif ${CPUTYPE} == "opteron-sse3" || ${CPUTYPE} == "athlon64-sse3" || \
251    ${CPUTYPE} == "k8-sse3"
252MACHINE_CPU = k8 3dnow sse3
253.  elif ${CPUTYPE} == "opteron" || ${CPUTYPE} == "athlon64" || \
254    ${CPUTYPE} == "athlon-fx" || ${CPUTYPE} == "k8"
255MACHINE_CPU = k8 3dnow
256.  elif ${CPUTYPE} == "sapphirerapids" || ${CPUTYPE} == "tigerlake" || \
257    ${CPUTYPE} == "cooperlake" || ${CPUTYPE} == "cascadelake" || \
258    ${CPUTYPE} == "icelake-server" || ${CPUTYPE} == "icelake-client" || \
259    ${CPUTYPE} == "cannonlake" || ${CPUTYPE} == "knm" || \
260    ${CPUTYPE} == "skylake-avx512" || ${CPUTYPE} == "knl" || \
261    ${CPUTYPE} == "x86-64-v4"
262MACHINE_CPU = avx512 avx2 avx sse42 sse41 ssse3 sse3
263.  elif ${CPUTYPE} == "alderlake" || ${CPUTYPE} == "skylake" || \
264    ${CPUTYPE} == "broadwell" || ${CPUTYPE} == "haswell" || \
265    ${CPUTYPE} == "x86-64-v3"
266MACHINE_CPU = avx2 avx sse42 sse41 ssse3 sse3
267.  elif ${CPUTYPE} == "ivybridge" || ${CPUTYPE} == "sandybridge"
268MACHINE_CPU = avx sse42 sse41 ssse3 sse3
269.  elif ${CPUTYPE} == "tremont" || ${CPUTYPE} == "goldmont-plus" || \
270    ${CPUTYPE} == "goldmont" || ${CPUTYPE} == "westmere" || \
271    ${CPUTYPE} == "nehalem" || ${CPUTYPE} == "silvermont" || \
272    ${CPUTYPE} == "x86-64-v2"
273MACHINE_CPU = sse42 sse41 ssse3 sse3
274.  elif ${CPUTYPE} == "penryn"
275MACHINE_CPU = sse41 ssse3 sse3
276.  elif ${CPUTYPE} == "core2" || ${CPUTYPE} == "bonnell"
277MACHINE_CPU = ssse3 sse3
278.  elif ${CPUTYPE} == "nocona"
279MACHINE_CPU = sse3
280.  endif
281MACHINE_CPU += amd64 sse2 sse mmx
282########## powerpc
283. elif ${MACHINE_ARCH} == "powerpc"
284.  if ${CPUTYPE} == "e500"
285MACHINE_CPU = booke softfp
286.  elif ${CPUTYPE} == "g4"
287MACHINE_CPU = aim altivec
288.  else
289MACHINE_CPU= aim
290.  endif
291. elif ${MACHINE_ARCH} == "powerpc64"
292.  if ${CPUTYPE} == "e5500"
293MACHINE_CPU = booke
294.  elif ${CPUTYPE} == power7
295MACHINE_CPU = altivec vsx
296.  elif ${CPUTYPE} == power8
297MACHINE_CPU = altivec vsx vsx2
298.  elif ${CPUTYPE} == power9
299MACHINE_CPU = altivec vsx vsx2 vsx3
300.  else
301MACHINE_CPU = aim altivec
302.  endif
303. elif ${MACHINE_ARCH} == "powerpc64le"
304MACHINE_CPU = aim altivec vsx vsx2
305.  if ${CPUTYPE} == power9
306MACHINE_CPU += vsx3
307.  endif
308########## riscv
309. elif ${MACHINE_CPUARCH} == "riscv"
310MACHINE_CPU = riscv
311. endif
312.endif
313
314########## arm
315.if ${MACHINE_CPUARCH} == "arm"
316MACHINE_CPU += arm
317. if ${MACHINE_ARCH:Marmv6*} != ""
318MACHINE_CPU += armv6
319. endif
320. if ${MACHINE_ARCH:Marmv7*} != ""
321MACHINE_CPU += armv7
322. endif
323# Normally armv6 and armv7 are hard float ABI from FreeBSD 11 onwards. However
324# when CPUTYPE has 'soft' in it, we use the soft-float ABI to allow building of
325# soft-float ABI libraries. In this case, we have to add the -mfloat-abi=softfp
326# to force that.
327. if defined(CPUTYPE) && ${CPUTYPE:M*soft*} != ""
328# Needs to be CFLAGS not _CPUCFLAGS because it's needed for the ABI
329# not a nice optimization. Please note: softfp ABI uses hardware floating
330# instructions, but passes arguments to function calls in integer regsiters.
331# -mfloat-abi=soft is full software floating point, but is not currently
332# supported. softfp support in FreeBSD may disappear in FreeBSD 13.0 since
333# it was a transition tool from FreeBSD 10 to 11 and is a bit of an odd duck.
334CFLAGS += -mfloat-abi=softfp
335. endif
336.endif
337
338.if ${MACHINE_ARCH} == "powerpc" || ${MACHINE_ARCH} == "powerpcspe"
339LDFLAGS.bfd+= -Wl,--secure-plt
340.endif
341
342.if ${MACHINE_ARCH} == "powerpcspe"
343CFLAGS += -mcpu=8548 -mspe
344CFLAGS.gcc+= -mabi=spe -mfloat-gprs=double -Wa,-me500
345.endif
346
347.if ${MACHINE_CPUARCH} == "riscv"
348CFLAGS += -march=rv64imafdc -mabi=lp64d
349.endif
350
351# NB: COPTFLAGS is handled in /usr/src/sys/conf/kern.pre.mk
352
353.if !defined(NO_CPU_CFLAGS)
354CFLAGS += ${_CPUCFLAGS}
355.endif
356
357#
358# Prohibit the compiler from emitting SIMD instructions.
359# These flags are added to CFLAGS in areas where the extra context-switch
360# cost outweighs the advantages of SIMD instructions.
361#
362# gcc:
363# Setting -mno-mmx implies -mno-3dnow
364# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
365#
366# clang:
367# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
368# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and
369# -mno-sse42
370# (-mfpmath= is not supported)
371#
372.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
373CFLAGS_NO_SIMD.clang= -mno-avx -mno-avx2
374CFLAGS_NO_SIMD= -mno-mmx -mno-sse
375.endif
376CFLAGS_NO_SIMD += ${CFLAGS_NO_SIMD.${COMPILER_TYPE}}
377
378# Add in any architecture-specific CFLAGS.
379# These come from make.conf or the command line or the environment.
380CFLAGS += ${CFLAGS.${MACHINE_ARCH}}
381CXXFLAGS += ${CXXFLAGS.${MACHINE_ARCH}}
382
383#
384# MACHINE_ABI is a list of properties about the ABI used for MACHINE_ARCH.
385# The following properties are indicated with one of the follow values:
386#
387# Byte order:			big-endian, little-endian
388# Floating point ABI:		soft-float, hard-float
389# Size of long (size_t, etc):	long32, long64
390# Pointer type:			ptr32, ptr64
391# Size of time_t:		time32, time64
392#
393.if (${MACHINE} == "arm" && (defined(CPUTYPE) && ${CPUTYPE:M*soft*})) || \
394    (${MACHINE_ARCH} == "powerpc" && (defined(CPUTYPE) && ${CPUTYPE} == "e500"))
395MACHINE_ABI+=	soft-float
396.else
397MACHINE_ABI+=	hard-float
398.endif
399# Currently all 64-bit architectures include 64 in their name (see arch(7)).
400.if ${MACHINE_ARCH:M*64*}
401MACHINE_ABI+=  long64
402.else
403MACHINE_ABI+=  long32
404.endif
405.if ${MACHINE_ABI:Mlong64}
406MACHINE_ABI+=  ptr64
407.else
408MACHINE_ABI+=  ptr32
409.endif
410.if ${MACHINE_ARCH} == "i386"
411MACHINE_ABI+=  time32
412.else
413MACHINE_ABI+=  time64
414.endif
415.if ${MACHINE_ARCH:Mpowerpc*} && !${MACHINE_ARCH:M*le}
416MACHINE_ABI+=	big-endian
417.else
418MACHINE_ABI+=	little-endian
419.endif
420