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