xref: /freebsd/share/mk/bsd.cpu.mk (revision faf139cc5dd3396181c11922bc6685c0c59b7b24)
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} == "armv6"
112_CPUCFLAGS = -march=${CPUTYPE} -DARM_ARCH_6=1
113. elif ${CPUTYPE} == "cortexa"
114_CPUCFLAGS = -march=armv7 -DARM_ARCH_6=1 -mfpu=vfp
115. elif ${CPUTYPE:Marmv[4567]*} != ""
116# Handle all the armvX types that FreeBSD runs:
117#	armv4, armv4t, armv5, armv5te, armv6, armv6t2, armv7, armv7-a, armv7ve
118# they require -march=. All the others require -mcpu=.
119_CPUCFLAGS = -march=${CPUTYPE}
120. else
121# Common values for FreeBSD
122# arm:
123#	arm920t, arm926ej-s, marvell-pj4, fa526, fa626,
124#	fa606te, fa626te, fa726te
125# armv6:
126# 	arm1176jzf-s, generic-armv7-a, cortex-a5, cortex-a7, cortex-a8,
127#	cortex-a9, cortex-a12, cortex-a15, cortex-a17, cortex-a53, cortex-a57,
128#	cortex-a72, exynos-m1
129_CPUCFLAGS = -mcpu=${CPUTYPE}
130. endif
131. elif ${MACHINE_ARCH} == "powerpc"
132.  if ${CPUTYPE} == "e500"
133_CPUCFLAGS = -Wa,-me500 -msoft-float
134.  else
135_CPUCFLAGS = -mcpu=${CPUTYPE} -mno-powerpc64
136.  endif
137. elif ${MACHINE_ARCH} == "powerpc64"
138_CPUCFLAGS = -mcpu=${CPUTYPE}
139. elif ${MACHINE_CPUARCH} == "mips"
140# mips[1234], mips32, mips64, and all later releases need to have mips
141# preserved (releases later than r2 require external toolchain)
142.  if ${CPUTYPE:Mmips32*} != "" || ${CPUTYPE:Mmips64*} != "" || \
143	${CPUTYPE:Mmips[1234]} != ""
144_CPUCFLAGS = -march=${CPUTYPE}
145. else
146# Default -march to the CPUTYPE passed in, with mips stripped off so we
147# accept either mips4kc or 4kc, mostly for historical reasons
148# Typical values for cores:
149#	4kc, 24kc, 34kc, 74kc, 1004kc, octeon, octeon+, octeon2, octeon3,
150#	sb1, xlp, xlr
151_CPUCFLAGS = -march=${CPUTYPE:S/^mips//}
152. endif
153. elif ${MACHINE_CPUARCH} == "riscv"
154_CPUCFLAGS = -msoft-float # -march="RV64I" # RISCVTODO
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} == "bdver4"
174MACHINE_CPU = xop avx2 avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
175.  elif ${CPUTYPE} == "bdver3" || ${CPUTYPE} == "bdver2" || \
176    ${CPUTYPE} == "bdver1"
177MACHINE_CPU = xop avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
178.  elif ${CPUTYPE} == "btver2"
179MACHINE_CPU = avx sse42 sse41 ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
180.  elif ${CPUTYPE} == "btver1"
181MACHINE_CPU = ssse3 sse4a sse3 sse2 sse mmx k6 k5 i586
182.  elif ${CPUTYPE} == "amdfam10"
183MACHINE_CPU = athlon-xp athlon k7 3dnow sse4a sse3 sse2 sse mmx k6 k5 i586
184.  elif ${CPUTYPE} == "opteron-sse3" || ${CPUTYPE} == "athlon64-sse3"
185MACHINE_CPU = athlon-xp athlon k7 3dnow sse3 sse2 sse mmx k6 k5 i586
186.  elif ${CPUTYPE} == "opteron" || ${CPUTYPE} == "athlon64" || \
187    ${CPUTYPE} == "athlon-fx"
188MACHINE_CPU = athlon-xp athlon k7 3dnow sse2 sse mmx k6 k5 i586
189.  elif ${CPUTYPE} == "athlon-mp" || ${CPUTYPE} == "athlon-xp" || \
190    ${CPUTYPE} == "athlon-4"
191MACHINE_CPU = athlon-xp athlon k7 3dnow sse mmx k6 k5 i586
192.  elif ${CPUTYPE} == "athlon" || ${CPUTYPE} == "athlon-tbird"
193MACHINE_CPU = athlon k7 3dnow mmx k6 k5 i586
194.  elif ${CPUTYPE} == "k6-3" || ${CPUTYPE} == "k6-2" || ${CPUTYPE} == "geode"
195MACHINE_CPU = 3dnow mmx k6 k5 i586
196.  elif ${CPUTYPE} == "k6"
197MACHINE_CPU = mmx k6 k5 i586
198.  elif ${CPUTYPE} == "k5"
199MACHINE_CPU = k5 i586
200.  elif ${CPUTYPE} == "skylake" || ${CPUTYPE} == "knl"
201MACHINE_CPU = avx512 avx2 avx sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586
202.  elif ${CPUTYPE} == "broadwell" || ${CPUTYPE} == "haswell"
203MACHINE_CPU = avx2 avx sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586
204.  elif ${CPUTYPE} == "ivybridge" || ${CPUTYPE} == "sandybridge"
205MACHINE_CPU = avx sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586
206.  elif ${CPUTYPE} == "westmere" || ${CPUTYPE} == "nehalem" || \
207    ${CPUTYPE} == "silvermont"
208MACHINE_CPU = sse42 sse41 ssse3 sse3 sse2 sse i686 mmx i586
209.  elif ${CPUTYPE} == "penryn"
210MACHINE_CPU = sse41 ssse3 sse3 sse2 sse i686 mmx i586
211.  elif ${CPUTYPE} == "core2" || ${CPUTYPE} == "bonnell"
212MACHINE_CPU = ssse3 sse3 sse2 sse i686 mmx i586
213.  elif ${CPUTYPE} == "yonah" || ${CPUTYPE} == "prescott"
214MACHINE_CPU = sse3 sse2 sse i686 mmx i586
215.  elif ${CPUTYPE} == "pentium4" || ${CPUTYPE} == "pentium4m" || \
216    ${CPUTYPE} == "pentium-m"
217MACHINE_CPU = sse2 sse i686 mmx i586
218.  elif ${CPUTYPE} == "pentium3" || ${CPUTYPE} == "pentium3m"
219MACHINE_CPU = sse i686 mmx i586
220.  elif ${CPUTYPE} == "pentium2"
221MACHINE_CPU = i686 mmx i586
222.  elif ${CPUTYPE} == "pentiumpro"
223MACHINE_CPU = i686 i586
224.  elif ${CPUTYPE} == "pentium-mmx"
225MACHINE_CPU = mmx i586
226.  elif ${CPUTYPE} == "pentium"
227MACHINE_CPU = i586
228.  elif ${CPUTYPE} == "c7"
229MACHINE_CPU = sse3 sse2 sse i686 mmx i586
230.  elif ${CPUTYPE} == "c3-2"
231MACHINE_CPU = sse i686 mmx i586
232.  elif ${CPUTYPE} == "c3"
233MACHINE_CPU = 3dnow mmx i586
234.  elif ${CPUTYPE} == "winchip2"
235MACHINE_CPU = 3dnow mmx
236.  elif ${CPUTYPE} == "winchip-c6"
237MACHINE_CPU = mmx
238.  endif
239MACHINE_CPU += i486
240########## amd64
241. elif ${MACHINE_CPUARCH} == "amd64"
242.  if ${CPUTYPE} == "bdver4"
243MACHINE_CPU = xop avx2 avx sse42 sse41 ssse3 sse4a sse3
244.  elif ${CPUTYPE} == "bdver3" || ${CPUTYPE} == "bdver2" || \
245    ${CPUTYPE} == "bdver1"
246MACHINE_CPU = xop avx sse42 sse41 ssse3 sse4a sse3
247.  elif ${CPUTYPE} == "btver2"
248MACHINE_CPU = avx sse42 sse41 ssse3 sse4a sse3
249.  elif ${CPUTYPE} == "btver1"
250MACHINE_CPU = ssse3 sse4a sse3
251.  elif ${CPUTYPE} == "amdfam10"
252MACHINE_CPU = k8 3dnow sse4a sse3
253.  elif ${CPUTYPE} == "opteron-sse3" || ${CPUTYPE} == "athlon64-sse3" || \
254    ${CPUTYPE} == "k8-sse3"
255MACHINE_CPU = k8 3dnow sse3
256.  elif ${CPUTYPE} == "opteron" || ${CPUTYPE} == "athlon64" || \
257    ${CPUTYPE} == "athlon-fx" || ${CPUTYPE} == "k8"
258MACHINE_CPU = k8 3dnow
259.  elif ${CPUTYPE} == "skylake" || ${CPUTYPE} == "knl"
260MACHINE_CPU = avx512 avx2 avx sse42 sse41 ssse3 sse3
261.  elif ${CPUTYPE} == "broadwell" || ${CPUTYPE} == "haswell"
262MACHINE_CPU = avx2 avx sse42 sse41 ssse3 sse3
263.  elif ${CPUTYPE} == "ivybridge" || ${CPUTYPE} == "sandybridge"
264MACHINE_CPU = avx sse42 sse41 ssse3 sse3
265.  elif ${CPUTYPE} == "westmere" || ${CPUTYPE} == "nehalem" || \
266    ${CPUTYPE} == "silvermont"
267MACHINE_CPU = sse42 sse41 ssse3 sse3
268.  elif ${CPUTYPE} == "penryn"
269MACHINE_CPU = sse41 ssse3 sse3
270.  elif ${CPUTYPE} == "core2" || ${CPUTYPE} == "bonnell"
271MACHINE_CPU = ssse3 sse3
272.  elif ${CPUTYPE} == "nocona"
273MACHINE_CPU = sse3
274.  endif
275MACHINE_CPU += amd64 sse2 sse mmx
276########## Mips
277. elif ${MACHINE_CPUARCH} == "mips"
278MACHINE_CPU = mips
279########## powerpc
280. elif ${MACHINE_ARCH} == "powerpc"
281.  if ${CPUTYPE} == "e500"
282MACHINE_CPU = booke softfp
283.  endif
284########## riscv
285. elif ${MACHINE_CPUARCH} == "riscv"
286MACHINE_CPU = riscv
287########## sparc64
288. elif ${MACHINE_ARCH} == "sparc64"
289.  if ${CPUTYPE} == "v9"
290MACHINE_CPU = v9
291.  elif ${CPUTYPE} == "ultrasparc"
292MACHINE_CPU = v9 ultrasparc
293.  elif ${CPUTYPE} == "ultrasparc3"
294MACHINE_CPU = v9 ultrasparc ultrasparc3
295.  endif
296. endif
297.endif
298
299.if ${MACHINE_CPUARCH} == "mips"
300CFLAGS += -G0
301.endif
302
303########## arm
304.if ${MACHINE_CPUARCH} == "arm"
305MACHINE_CPU += arm
306. if ${MACHINE_ARCH:Marmv6*} != ""
307MACHINE_CPU += armv6
308. endif
309# armv6 is a hybrid. It uses the softfp ABI, but doesn't emulate
310# floating point in the general case, so don't define softfp for
311# it at this time. arm and armeb are pure softfp, so define it
312# for them.
313. if ${MACHINE_ARCH:Marmv6*} == ""
314MACHINE_CPU += softfp
315. endif
316.if ${MACHINE_ARCH} == "armv6"
317# Needs to be CFLAGS not _CPUCFLAGS because it's needed for the ABI
318# not a nice optimization.
319CFLAGS += -mfloat-abi=softfp
320.endif
321.endif
322
323.if ${MACHINE_CPUARCH} == "riscv"
324CFLAGS += -msoft-float
325.endif
326
327# NB: COPTFLAGS is handled in /usr/src/sys/conf/kern.pre.mk
328
329.if !defined(NO_CPU_CFLAGS)
330CFLAGS += ${_CPUCFLAGS}
331.endif
332
333#
334# Prohibit the compiler from emitting SIMD instructions.
335# These flags are added to CFLAGS in areas where the extra context-switch
336# cost outweighs the advantages of SIMD instructions.
337#
338# gcc:
339# Setting -mno-mmx implies -mno-3dnow
340# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
341#
342# clang:
343# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
344# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and
345# -mno-sse42
346# (-mfpmath= is not supported)
347#
348.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
349CFLAGS_NO_SIMD.clang= -mno-avx
350CFLAGS_NO_SIMD= -mno-mmx -mno-sse
351.endif
352CFLAGS_NO_SIMD += ${CFLAGS_NO_SIMD.${COMPILER_TYPE}}
353
354# Add in any architecture-specific CFLAGS.
355# These come from make.conf or the command line or the environment.
356CFLAGS += ${CFLAGS.${MACHINE_ARCH}}
357CXXFLAGS += ${CXXFLAGS.${MACHINE_ARCH}}
358