Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt
+++ CMakeLists.txt
@@ -157,6 +157,10 @@
 
 option(LLVM_DEPENDENCY_DEBUGGING "Dependency debugging mode to verify correctly expressed library dependencies (Darwin only)" OFF)
 
+# Some tablegen-erators like to format their output using clang-format when
+# it's available.
+find_program(CLANG_FORMAT_EXECUTABLE clang-format)
+
 # Some features of the LLVM build may be disallowed when dependency debugging is
 # enabled. In particular you cannot use ccache because we want to force compile
 # operations to always happen.
Index: cmake/modules/TableGen.cmake
===================================================================
--- cmake/modules/TableGen.cmake
+++ cmake/modules/TableGen.cmake
@@ -9,6 +9,9 @@
 endif()
 
 function(tablegen project ofn)
+  cmake_parse_arguments(tablegen "FORMAT" "" "OPTIONS" ${ARGN})
+  set(tablegen_OPTIONS ${tablegen_OPTIONS} ${tablegen_UNPARSED_ARGUMENTS})
+
   # Validate calling context.
   if(NOT ${project}_TABLEGEN_EXE)
     message(FATAL_ERROR "${project}_TABLEGEN_EXE not set")
@@ -24,18 +27,30 @@
       ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
   endif()
   if (LLVM_ENABLE_DAGISEL_COV)
-    list(FIND ARGN "-gen-dag-isel" idx)
+    list(FIND tablegen_OPTIONS "-gen-dag-isel" idx)
     if( NOT idx EQUAL -1 )
       list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage")
     endif()
   endif()
 
+  set(tablegen_format_command)
+  if(tablegen_FORMAT)
+    if(CLANG_FORMAT_EXECUTABLE)
+      set(tablegen_format_command COMMAND ${CLANG_FORMAT_EXECUTABLE} -i ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp)
+    else()
+      message(STATUS "Not formatting tablegen output ${ofn} due to lack of clang-format")
+    endif()
+  else()
+  endif()
+
   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
     # Generate tablegen output in a temporary file.
-    COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR}
+    COMMAND ${${project}_TABLEGEN_EXE} ${tablegen_OPTIONS} -I ${CMAKE_CURRENT_SOURCE_DIR}
     ${LLVM_TABLEGEN_FLAGS} 
     ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
     -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
+    # Add a formatting command if we want one.
+    ${tablegen_format_command}
     # The file in LLVM_TARGET_DEFINITIONS may be not in the current
     # directory and local_tds may not contain it, so we must
     # explicitly list it here:
Index: lib/Target/AArch64/CMakeLists.txt
===================================================================
--- lib/Target/AArch64/CMakeLists.txt
+++ lib/Target/AArch64/CMakeLists.txt
@@ -14,8 +14,8 @@
 tablegen(LLVM AArch64GenDisassemblerTables.inc -gen-disassembler)
 tablegen(LLVM AArch64GenSystemOperands.inc -gen-searchable-tables)
 if(LLVM_BUILD_GLOBAL_ISEL)
-  tablegen(LLVM AArch64GenRegisterBank.inc -gen-register-bank)
-  tablegen(LLVM AArch64GenGlobalISel.inc -gen-global-isel)
+  tablegen(LLVM AArch64GenRegisterBank.inc FORMAT OPTIONS -gen-register-bank)
+  tablegen(LLVM AArch64GenGlobalISel.inc FORMAT OPTIONS -gen-global-isel)
 endif()
 
 add_public_tablegen_target(AArch64CommonTableGen)