diff --git a/Makefile.am b/Makefile.am
index 3ca9fd5e746dcbfa55d85e3632b3466d9ebc21ab..d475bc352597a55ea07054006024969c641b2e59 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -38,7 +38,7 @@ MYFLAGS =
 # Add the source directory and the non-standard paths to the included library headers to CFLAGS
 AM_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/argparse $(HDF5_CPPFLAGS) \
 	$(GSL_INCS) $(FFTW_INCS) $(NUMA_INCS) $(GRACKLE_INCS) $(OPENMP_CFLAGS) \
-	$(CHEALPIX_CFLAGS)
+	$(CHEALPIX_CFLAGS) $(OPT_CFLAGS)
 
 AM_LDFLAGS = $(HDF5_LDFLAGS)
 
diff --git a/argparse/Makefile.am b/argparse/Makefile.am
index 2fa6fb9e9ef4c014697a2c434cd86741cc74d79c..2aa6f135c113a36d192c6a1e2f42b8e78e35cde6 100644
--- a/argparse/Makefile.am
+++ b/argparse/Makefile.am
@@ -22,7 +22,7 @@ AM_SOURCES = argparse.c
 
 # Sources and flags for regular library
 libargparse_la_SOURCES = $(AM_SOURCES)
-libargparse_la_CFLAGS = $(AM_CFLAGS)
+libargparse_la_CFLAGS = $(AM_CFLAGS) $(OPT_CFLAGS)
 libargparse_la_LDFLAGS = $(AM_LDFLAGS)
 
 EXTRA_DIST = LICENSE README.md
diff --git a/configure.ac b/configure.ac
index bc63e5143b691f355efd0b7cf074da8d40432e20..82c754afc2a8dedf0a2207b0db6ab0fcabbaa973 100644
--- a/configure.ac
+++ b/configure.ac
@@ -576,8 +576,9 @@ if test "$enable_opt" = "yes" ; then
 
    # Choose the best flags for this compiler and architecture
    ac_test_CFLAGS="no"
-   AX_CC_MAXOPT
+   AX_CC_MAXOPT_SWIFT
    ac_test_CFLAGS="yes"
+   AC_ARG_VAR([OPT_CFLAGS], [C compiler flags added to the basic CFLAGS for good optimization])
 
    # Choose the best flags for the gravity sub-library on this compiler and architecture
    if test "$ax_cv_c_compiler_vendor" = "intel"; then
@@ -3003,7 +3004,7 @@ AC_MSG_RESULT([
    Compiler             : $CC
     - vendor            : $ax_cv_c_compiler_vendor
     - version           : $ax_cv_c_compiler_version
-    - flags             : $CFLAGS $OPENMP_CFLAGS
+    - flags             : $OPT_CFLAGS $CFLAGS $OPENMP_CFLAGS
    MPI enabled          : $enable_mpi
    HDF5 enabled         : $with_hdf5
     - parallel          : $have_parallel_hdf5
diff --git a/examples/Cooling/CoolingRates/Makefile.am b/examples/Cooling/CoolingRates/Makefile.am
index 604a4691a4983ac206f482c0002b2f5c472ad0bc..0d62607ba2c8c89ef1aad4ac783c1a37bb2083a7 100644
--- a/examples/Cooling/CoolingRates/Makefile.am
+++ b/examples/Cooling/CoolingRates/Makefile.am
@@ -15,7 +15,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 # Add the source directory and the non-standard paths to the included library headers to CFLAGS
-AM_CFLAGS = -I$(top_srcdir)/src -I$(top_builddir)/examples $(HDF5_CPPFLAGS) $(GSL_INCS) $(FFTW_INCS) $(NUMA_INCS) $(OPENMP_CFLAGS) $(CHEALPIX_CFLAGS)
+AM_CFLAGS = -I$(top_srcdir)/src -I$(top_builddir)/examples $(HDF5_CPPFLAGS) $(GSL_INCS) $(FFTW_INCS) $(NUMA_INCS) $(OPENMP_CFLAGS) $(CHEALPIX_CFLAGS) $(OPT_CFLAGS)
 
 AM_LDFLAGS = $(HDF5_LDFLAGS) $(HDF5_LIBS) $(FFTW_LIBS) $(NUMA_LIBS) $(TCMALLOC_LIBS) $(JEMALLOC_LIBS) $(TBBMALLOC_LIBS) $(GRACKLE_LIBS) $(GSL_LIBS) $(PROFILER_LIBS)
 
diff --git a/src/Makefile.am b/src/Makefile.am
index b0ad938f55121ccf5320452aea44c49ac5a2b656..e4912051fa8c78bc67ba03b5009a7ba0836c7af0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -16,7 +16,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 # Add the non-standard paths to the included library headers
-AM_CFLAGS = $(HDF5_CPPFLAGS) $(GSL_INCS) $(FFTW_INCS) $(NUMA_INCS) $(GRACKLE_INCS)  $(SUNDIALS_INCS) $(OPENMP_CFLAGS) $(CHEALPIX_CFLAGS)
+AM_CFLAGS = $(HDF5_CPPFLAGS) $(GSL_INCS) $(FFTW_INCS) $(NUMA_INCS) $(GRACKLE_INCS)  $(SUNDIALS_INCS) $(OPENMP_CFLAGS) $(CHEALPIX_CFLAGS) $(OPT_CFLAGS)
 
 # Assign a "safe" version number
 AM_LDFLAGS = $(HDF5_LDFLAGS) $(FFTW_LIBS)
@@ -528,14 +528,14 @@ version_string.h: version_string.h.in Makefile $(AM_SOURCES) $(include_HEADERS)
 	        -e "s,@GIT_REVISION\@,$${GIT_REVISION}," \
 	        -e "s|@GIT_BRANCH\@|$${GIT_BRANCH}|" \
 	        -e "s|@GIT_DATE\@|$${GIT_DATE}|" \
-	        -e "s|@SWIFT_CFLAGS\@|$(CFLAGS)|" $< > version_string.h; \
+	        -e "s|@SWIFT_CFLAGS\@|$(OPT_CFLAGS) $(CFLAGS)|" $< > version_string.h; \
 	else \
 	    if test ! -f version_string.h; then \
 	        sed -e "s,@PACKAGE_VERSION\@,$(PACKAGE_VERSION)," \
 	            -e "s,@GIT_REVISION\@,unknown," \
 		    -e "s,@GIT_BRANCH\@,unknown," \
 		    -e "s,@GIT_DATE\@,unknown," \
-	            -e "s|@SWIFT_CFLAGS\@|$(CFLAGS)|" $< > version_string.h; \
+	            -e "s|@SWIFT_CFLAGS\@|$(OPT_CFLAGS) $(CFLAGS)|" $< > version_string.h; \
 	    fi; \
 	fi
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 53fce13c93487429a517e960e958b08ff6921c77..6f50aec047dca11a5a5a316fd54700178ce0462e 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -15,7 +15,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 # Add the source directory and the non-standard paths to the included library headers to CFLAGS
-AM_CFLAGS = -I$(top_srcdir)/src $(HDF5_CPPFLAGS) $(GSL_INCS) $(FFTW_INCS) $(NUMA_INCS) $(OPENMP_CFLAGS) $(CHEALPIX_CFLAGS)
+AM_CFLAGS = -I$(top_srcdir)/src $(HDF5_CPPFLAGS) $(GSL_INCS) $(FFTW_INCS) $(NUMA_INCS) $(OPENMP_CFLAGS) $(CHEALPIX_CFLAGS) $(OPT_CFLAGS)
 
 AM_LDFLAGS = ../src/.libs/libswiftsim.a $(HDF5_LDFLAGS) $(HDF5_LIBS) $(FFTW_LIBS) $(NUMA_LIBS) $(TCMALLOC_LIBS) $(JEMALLOC_LIBS) $(TBBMALLOC_LIBS) $(GRACKLE_LIBS) $(GSL_LIBS) $(PROFILER_LIBS) $(CHEALPIX_LIBS)