xref: /freebsd/share/mk/bsd.cpu.mk (revision 0869dceb9d2642defe8d34b09d65145cb3f81e09)
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} == "mips"
18MACHINE_CPU = mips
19. elif ${MACHINE_CPUARCH} == "powerpc"
20MACHINE_CPU = aim
21. elif ${MACHINE_CPUARCH} == "riscv"
22MACHINE_CPU = riscv
23. elif ${MACHINE_CPUARCH} == "sparc64"
24MACHINE_CPU = ultrasparc
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. elif ${MACHINE_ARCH} == "sparc64"
79.  if ${CPUTYPE} == "us"
80CPUTYPE = ultrasparc
81.  elif ${CPUTYPE} == "us3"
82CPUTYPE = ultrasparc3
83.  endif
84. endif
85
86###############################################################################
87# Logic to set up correct gcc optimization flag.  This must be included
88# after /etc/make.conf so it can react to the local value of CPUTYPE
89# defined therein.  Consult:
90#	http://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html
91#	http://gcc.gnu.org/onlinedocs/gcc/RS-6000-and-PowerPC-Options.html
92#	http://gcc.gnu.org/onlinedocs/gcc/MIPS-Options.html
93#	http://gcc.gnu.org/onlinedocs/gcc/SPARC-Options.html
94#	http://gcc.gnu.org/onlinedocs/gcc/i386-and-x86_002d64-Options.html
95
96. if ${MACHINE_CPUARCH} == "i386"
97.  if ${CPUTYPE} == "crusoe"
98_CPUCFLAGS = -march=i686 -falign-functions=0 -falign-jumps=0 -falign-loops=0
99.  elif ${CPUTYPE} == "k5"
100_CPUCFLAGS = -march=pentium
101.  elif ${CPUTYPE} == "c7"
102_CPUCFLAGS = -march=c3-2
103.  else
104_CPUCFLAGS = -march=${CPUTYPE}
105.  endif
106. elif ${MACHINE_CPUARCH} == "amd64"
107_CPUCFLAGS = -march=${CPUTYPE}
108. elif ${MACHINE_CPUARCH} == "arm"
109.  if ${CPUTYPE} == "xscale"
110#XXX: gcc doesn't seem to like -mcpu=xscale, and dies while rebuilding itself
111#_CPUCFLAGS = -mcpu=xscale
112_CPUCFLAGS = -march=armv5te -D__XSCALE__
113.  elif ${CPUTYPE:M*soft*} != ""
114_CPUCFLAGS = -mfloat-abi=softfp
115.  elif ${CPUTYPE} == "cortexa"
116_CPUCFLAGS = -march=armv7 -mfpu=vfp
117.  elif ${CPUTYPE:Marmv[4567]*} != ""
118# Handle all the armvX types that FreeBSD runs:
119#	armv4, armv4t, armv5, armv5te, armv6, armv6t2, armv7, armv7-a, armv7ve
120# they require -march=. All the others require -mcpu=.
121_CPUCFLAGS = -march=${CPUTYPE}
122.  else
123# Common values for FreeBSD
124# arm: (any arm v4 or v5 processor you are targeting)
125#	arm920t, arm926ej-s, marvell-pj4, fa526, fa626,
126#	fa606te, fa626te, fa726te
127# armv6: (any arm v7 or v8 processor you are targeting and the arm1176jzf-s)
128# 	arm1176jzf-s, generic-armv7-a, cortex-a5, cortex-a7, cortex-a8,
129#	cortex-a9, cortex-a12, cortex-a15, cortex-a17, cortex-a53, cortex-a57,
130#	cortex-a72, exynos-m1
131_CPUCFLAGS = -mcpu=${CPUTYPE}
132. endif
133. elif ${MACHINE_ARCH} == "powerpc"
134.  if ${CPUTYPE} == "e500"
135_CPUCFLAGS = -Wa,-me500 -msoft-float
136.  else
137_CPUCFLAGS = -mcpu=${CPUTYPE} -mno-powerpc64
138.  endif
139. elif ${MACHINE_ARCH} == "powerpc64"
140_CPUCFLAGS = -mcpu=${CPUTYPE}
141. elif ${MACHINE_CPUARCH} == "mips"
142# mips[1234], mips32, mips64, and all later releases need to have mips
143# preserved (releases later than r2 require external toolchain)
144.  if ${CPUTYPE:Mmips32*} != "" || ${CPUTYPE:Mmips64*} != "" || \
145	${CPUTYPE:Mmips[1234]} != ""
146_CPUCFLAGS = -march=${CPUTYPE}
147. else
148# Default -march to the CPUTYPE passed in, with mips stripped off so we
149# accept either mips4kc or 4kc, mostly for historical reasons
150# Typical values for cores:
151#	4kc, 24kc, 34kc, 74kc, 1004kc, octeon, octeon+, octeon2, octeon3,
152#	sb1, xlp, xlr
153_CPUCFLAGS = -march=${CPUTYPE:S/^mips//}
154. endif
155. elif ${MACHINE_ARCH} == "sparc64"
156.  if ${CPUTYPE} == "v9"
157_CPUCFLAGS = -mcpu=v9
158.  elif ${CPUTYPE} == "ultrasparc"
159_CPUCFLAGS = -mcpu=ultrasparc
160.  elif ${CPUTYPE} == "ultrasparc3"
161_CPUCFLAGS = -mcpu=ultrasparc3
162.  endif
163. elif ${MACHINE_CPUARCH} == "aarch64"
164_CPUCFLAGS = -mcpu=${CPUTYPE}
165. endif
166
167# Set up the list of CPU features based on the CPU type.  This is an
168# unordered list to make it easy for client makefiles to test for the
169# presence of a CPU feature.
170
171########## i386
172. if ${MACHINE_CPUARCH} == "i386"
173.  if ${CPUTYPE} == "znver1"
174MACHINE_CPU = avx2 avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
175.  elif ${CPUTYPE} == "bdver4"
176MACHINE_CPU = xop avx2 avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
177.  elif ${CPUTYPE} == "bdver3" || ${CPUTYPE} == "bdver2" || \
178    ${CPUTYPE} == "bdver1"
179MACHINE_CPU = xop avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
180.  elif ${CPUTYPE} == "btver2"
181MACHINE_CPU = avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
182.  elif ${CPUTYPE} == "btver1"
183MACHINE_CPU = ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
184.  elif ${CPUTYPE} == "amdfam10"
185MACHINE_CPU = athlon-xp athlon k7 3dnow sse4a sse3 sse2 sse mmx k6 k5 i586
186.  elif ${CPUTYPE} == "opteron-sse3" || ${CPUTYPE} == "athlon64-sse3"
187MACHINE_CPU = athlon-xp athlon k7 3dnow sse3 sse2 sse mmx k6 k5 i586
188.  elif ${CPUTYPE} == "opteron" || ${CPUTYPE} == "athlon64" || \
189    ${CPUTYPE} == "athlon-fx"
190MACHINE_CPU = athlon-xp athlon k7 3dnow sse2 sse mmx k6 k5 i586
191.  elif ${CPUTYPE} == "athlon-mp" || ${CPUTYPE} == "athlon-xp" || \
192    ${CPUTYPE} == "athlon-4"
193MACHINE_CPU = athlon-xp athlon k7 3dnow sse mmx k6 k5 i586
194.  elif ${CPUTYPE} == "athlon" || ${CPUTYPE} == "athlon-tbird"
195MACHINE_CPU = athlon k7 3dnow mmx k6 k5 i586
196.  elif ${CPUTYPE} == "k6-3" || ${CPUTYPE} == "k6-2" || ${CPUTYPE} == "geode"
197MACHINE_CPU = 3dnow mmx k6 k5 i586
198.  elif ${CPUTYPE} == "k6"
199MACHINE_CPU = mmx k6 k5 i586
200.  elif ${CPUTYPE} == "k5"
201MACHINE_CPU = k5 i586
202.  elif ${CPUTYPE} == "cascadelake" || ${CPUTYPE} == "icelake-server" || \
203    ${CPUTYPE} == "icelake-client" || ${CPUTYPE} == "cannonlake" || \
204    ${CPUTYPE} == "knm" || ${CPUTYPE} == "skylake-avx512" || \
205    ${CPUTYPE} == "knl"
206MACHINE_CPU = avx512 avx2 avx sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586
207.  elif ${CPUTYPE} == "skylake" || ${CPUTYPE} == "broadwell" || \
208    ${CPUTYPE} == "haswell"
209MACHINE_CPU = avx2 avx sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586
210.  elif ${CPUTYPE} == "ivybridge" || ${CPUTYPE} == "sandybridge"
211MACHINE_CPU = avx sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586
212.  elif ${CPUTYPE} == "tremont" || ${CPUTYPE} == "goldmont-plus" || \
213    ${CPUTYPE} == "goldmont" || ${CPUTYPE} == "westmere" || \
214    ${CPUTYPE} == "nehalem" || ${CPUTYPE} == "silvermont"
215MACHINE_CPU = sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586
216.  elif ${CPUTYPE} == "penryn"
217MACHINE_CPU = sse41 ssse3 sse3 sse2 sse i686 mmx i586
218.  elif ${CPUTYPE} == "core2" || ${CPUTYPE} == "bonnell"
219MACHINE_CPU = ssse3 sse3 sse2 sse i686 mmx i586
220.  elif ${CPUTYPE} == "yonah" || ${CPUTYPE} == "prescott"
221MACHINE_CPU = sse3 sse2 sse i686 mmx i586
222.  elif ${CPUTYPE} == "pentium4" || ${CPUTYPE} == "pentium4m" || \
223    ${CPUTYPE} == "pentium-m"
224MACHINE_CPU = sse2 sse i686 mmx i586
225.  elif ${CPUTYPE} == "pentium3" || ${CPUTYPE} == "pentium3m"
226MACHINE_CPU = sse i686 mmx i586
227.  elif ${CPUTYPE} == "pentium2"
228MACHINE_CPU = i686 mmx i586
229.  elif ${CPUTYPE} == "pentiumpro"
230MACHINE_CPU = i686 i586
231.  elif ${CPUTYPE} == "pentium-mmx"
232MACHINE_CPU = mmx i586
233.  elif ${CPUTYPE} == "pentium"
234MACHINE_CPU = i586
235.  elif ${CPUTYPE} == "c7"
236MACHINE_CPU = sse3 sse2 sse i686 mmx i586
237.  elif ${CPUTYPE} == "c3-2"
238MACHINE_CPU = sse i686 mmx i586
239.  elif ${CPUTYPE} == "c3"
240MACHINE_CPU = 3dnow mmx i586
241.  elif ${CPUTYPE} == "winchip2"
242MACHINE_CPU = 3dnow mmx
243.  elif ${CPUTYPE} == "winchip-c6"
244MACHINE_CPU = mmx
245.  endif
246MACHINE_CPU += i486
247########## amd64
248. elif ${MACHINE_CPUARCH} == "amd64"
249.  if ${CPUTYPE} == "znver1"
250MACHINE_CPU = avx2 avx sse42 sse41 ssse3 sse4a sse3
251.  elif ${CPUTYPE} == "bdver4"
252MACHINE_CPU = xop avx2 avx sse42 sse41 ssse3 sse4a sse3
253.  elif ${CPUTYPE} == "bdver3" || ${CPUTYPE} == "bdver2" || \
254    ${CPUTYPE} == "bdver1"
255MACHINE_CPU = xop avx sse42 sse41 ssse3 sse4a sse3
256.  elif ${CPUTYPE} == "btver2"
257MACHINE_CPU = avx sse42 sse41 ssse3 sse4a sse3
258.  elif ${CPUTYPE} == "btver1"
259MACHINE_CPU = ssse3 sse4a sse3
260.  elif ${CPUTYPE} == "amdfam10"
261MACHINE_CPU = k8 3dnow sse4a sse3
262.  elif ${CPUTYPE} == "opteron-sse3" || ${CPUTYPE} == "athlon64-sse3" || \
263    ${CPUTYPE} == "k8-sse3"
264MACHINE_CPU = k8 3dnow sse3
265.  elif ${CPUTYPE} == "opteron" || ${CPUTYPE} == "athlon64" || \
266    ${CPUTYPE} == "athlon-fx" || ${CPUTYPE} == "k8"
267MACHINE_CPU = k8 3dnow
268.  elif ${CPUTYPE} == "cascadelake" || ${CPUTYPE} == "icelake-server" || \
269    ${CPUTYPE} == "icelake-client" || ${CPUTYPE} == "cannonlake" || \
270    ${CPUTYPE} == "knm" || ${CPUTYPE} == "skylake-avx512" || \
271    ${CPUTYPE} == "knl"
272MACHINE_CPU = avx512 avx2 avx sse42 sse41 ssse3 sse3
273.  elif ${CPUTYPE} == "skylake" || ${CPUTYPE} == "broadwell" || \
274    ${CPUTYPE} == "haswell"
275MACHINE_CPU = avx2 avx sse42 sse41 ssse3 sse3
276.  elif ${CPUTYPE} == "ivybridge" || ${CPUTYPE} == "sandybridge"
277MACHINE_CPU = avx sse42 sse41 ssse3 sse3
278.  elif ${CPUTYPE} == "tremont" || ${CPUTYPE} == "goldmont-plus" || \
279    ${CPUTYPE} == "goldmont" || ${CPUTYPE} == "westmere" || \
280    ${CPUTYPE} == "nehalem" || ${CPUTYPE} == "silvermont"
281MACHINE_CPU = sse42 sse41 ssse3 sse3
282.  elif ${CPUTYPE} == "penryn"
283MACHINE_CPU = sse41 ssse3 sse3
284.  elif ${CPUTYPE} == "core2" || ${CPUTYPE} == "bonnell"
285MACHINE_CPU = ssse3 sse3
286.  elif ${CPUTYPE} == "nocona"
287MACHINE_CPU = sse3
288.  endif
289MACHINE_CPU += amd64 sse2 sse mmx
290########## Mips
291. elif ${MACHINE_CPUARCH} == "mips"
292MACHINE_CPU = mips
293########## powerpc
294. elif ${MACHINE_ARCH} == "powerpc"
295.  if ${CPUTYPE} == "e500"
296MACHINE_CPU = booke softfp
297.  endif
298########## riscv
299. elif ${MACHINE_CPUARCH} == "riscv"
300MACHINE_CPU = riscv
301########## sparc64
302. elif ${MACHINE_ARCH} == "sparc64"
303.  if ${CPUTYPE} == "v9"
304MACHINE_CPU = v9
305.  elif ${CPUTYPE} == "ultrasparc"
306MACHINE_CPU = v9 ultrasparc
307.  elif ${CPUTYPE} == "ultrasparc3"
308MACHINE_CPU = v9 ultrasparc ultrasparc3
309.  endif
310. endif
311.endif
312
313.if ${MACHINE_CPUARCH} == "mips"
314CFLAGS += -G0
315AFLAGS+= -${MIPS_ENDIAN} -mabi=${MIPS_ABI}
316CFLAGS+= -${MIPS_ENDIAN} -mabi=${MIPS_ABI}
317LDFLAGS+= -${MIPS_ENDIAN} -mabi=${MIPS_ABI}
318. if ${MACHINE_ARCH:Mmips*el*} != ""
319MIPS_ENDIAN=	EL
320. else
321MIPS_ENDIAN=	EB
322. endif
323. if ${MACHINE_ARCH:Mmips64*} != ""
324MIPS_ABI?=	64
325. elif ${MACHINE_ARCH:Mmipsn32*} != ""
326MIPS_ABI?=	n32
327. else
328MIPS_ABI?=	32
329. endif
330. if ${MACHINE_ARCH:Mmips*hf}
331CFLAGS += -mhard-float
332. else
333CFLAGS += -msoft-float
334. endif
335.endif
336
337########## arm
338.if ${MACHINE_CPUARCH} == "arm"
339MACHINE_CPU += arm
340. if ${MACHINE_ARCH:Marmv6*} != ""
341MACHINE_CPU += armv6
342. endif
343. if ${MACHINE_ARCH:Marmv7*} != ""
344MACHINE_CPU += armv7
345. endif
346# armv6 and armv7 are a hybrid. It can use the softfp ABI, but doesn't emulate
347# floating point in the general case, so don't define softfp for it at this
348# time. arm is pure softfp, so define it for them.
349. if ${MACHINE_ARCH:Marmv[67]*} == ""
350MACHINE_CPU += softfp
351. endif
352# Normally armv6 and armv7 are hard float ABI from FreeBSD 11 onwards. However
353# when CPUTYPE has 'soft' in it, we use the soft-float ABI to allow building of
354# soft-float ABI libraries. In this case, we have to add the -mfloat-abi=softfp
355# to force that.
356.if ${MACHINE_ARCH:Marmv[67]*} && defined(CPUTYPE) && ${CPUTYPE:M*soft*} != ""
357# Needs to be CFLAGS not _CPUCFLAGS because it's needed for the ABI
358# not a nice optimization.
359CFLAGS += -mfloat-abi=softfp
360.endif
361.endif
362
363.if ${MACHINE_ARCH} == "powerpc" || ${MACHINE_ARCH} == "powerpcspe"
364LDFLAGS+= -Wl,--secure-plt
365.endif
366
367.if ${MACHINE_ARCH} == "powerpcspe"
368CFLAGS += -mcpu=8548 -mspe
369CFLAGS.gcc+= -mabi=spe -mfloat-gprs=double -Wa,-me500
370.endif
371
372.if ${MACHINE_CPUARCH} == "riscv"
373.if ${MACHINE_ARCH:Mriscv*sf}
374CFLAGS += -march=rv64imac -mabi=lp64
375.else
376CFLAGS += -march=rv64imafdc -mabi=lp64d
377.endif
378
379.if ${LINKER_FEATURES:U:Mriscv-relaxations} == ""
380CFLAGS += -mno-relax
381.endif
382.endif
383
384# NB: COPTFLAGS is handled in /usr/src/sys/conf/kern.pre.mk
385
386.if !defined(NO_CPU_CFLAGS)
387CFLAGS += ${_CPUCFLAGS}
388.endif
389
390#
391# Prohibit the compiler from emitting SIMD instructions.
392# These flags are added to CFLAGS in areas where the extra context-switch
393# cost outweighs the advantages of SIMD instructions.
394#
395# gcc:
396# Setting -mno-mmx implies -mno-3dnow
397# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
398#
399# clang:
400# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
401# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and
402# -mno-sse42
403# (-mfpmath= is not supported)
404#
405.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
406CFLAGS_NO_SIMD.clang= -mno-avx -mno-avx2
407CFLAGS_NO_SIMD= -mno-mmx -mno-sse
408.endif
409CFLAGS_NO_SIMD += ${CFLAGS_NO_SIMD.${COMPILER_TYPE}}
410
411# Add in any architecture-specific CFLAGS.
412# These come from make.conf or the command line or the environment.
413CFLAGS += ${CFLAGS.${MACHINE_ARCH}}
414CXXFLAGS += ${CXXFLAGS.${MACHINE_ARCH}}
415