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