<div dir="ltr">Hi Amjad, Craig,<br><br>Just curious - is there a reason this ended up as not only a separate pass, but as an entirely separate library? Most passes are grouped into broad libraries like IPO/Scalar/etc. & it seems this one could be in Scalar, where it's entry point/construction is declared anyway?<br><br>I ask because this caught/caused some layering violations by inclnuding the Scalar.h header which is logically part of the Transforms/Scalar (ScalarOpt) library, which the AggressiveInstCombine library doesn't depend on.<br><br>Could we move this pass into the ScalarOpt library?<br><br>- Dave<br><br><div class="gmail_quote"><div dir="ltr">On Wed, Jan 24, 2018 at 4:44 AM Amjad Aboud via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: aaboud<br>
Date: Wed Jan 24 04:42:42 2018<br>
New Revision: 323321<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=323321&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=323321&view=rev</a><br>
Log:<br>
[InstCombine] Introducing Aggressive Instruction Combine pass (-aggressive-instcombine).<br>
<br>
Combine expression patterns to form expressions with fewer, simple instructions.<br>
This pass does not modify the CFG.<br>
<br>
For example, this pass reduce width of expressions post-dominated by TruncInst<br>
into smaller width when applicable.<br>
<br>
It differs from instcombine pass in that it contains pattern optimization that<br>
requires higher complexity than the O(1), thus, it should run fewer times than<br>
instcombine pass.<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D38313" rel="noreferrer" target="_blank">https://reviews.llvm.org/D38313</a><br>
<br>
Added:<br>
    llvm/trunk/include/llvm/Transforms/AggressiveInstCombine/   (with props)<br>
    llvm/trunk/include/llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h<br>
    llvm/trunk/lib/Transforms/AggressiveInstCombine/   (with props)<br>
    llvm/trunk/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp<br>
    llvm/trunk/lib/Transforms/AggressiveInstCombine/AggressiveInstCombineInternal.h<br>
    llvm/trunk/lib/Transforms/AggressiveInstCombine/CMakeLists.txt<br>
    llvm/trunk/lib/Transforms/AggressiveInstCombine/LLVMBuild.txt<br>
    llvm/trunk/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp<br>
    llvm/trunk/test/Transforms/AggressiveInstCombine/   (with props)<br>
    llvm/trunk/test/Transforms/AggressiveInstCombine/trunc_multi_uses.ll<br>
Modified:<br>
    llvm/trunk/docs/Passes.rst<br>
    llvm/trunk/include/llvm/InitializePasses.h<br>
    llvm/trunk/include/llvm/Transforms/Scalar.h<br>
    llvm/trunk/lib/LTO/LLVMBuild.txt<br>
    llvm/trunk/lib/Passes/LLVMBuild.txt<br>
    llvm/trunk/lib/Passes/PassBuilder.cpp<br>
    llvm/trunk/lib/Passes/PassRegistry.def<br>
    llvm/trunk/lib/Transforms/CMakeLists.txt<br>
    llvm/trunk/lib/Transforms/IPO/LLVMBuild.txt<br>
    llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp<br>
    llvm/trunk/lib/Transforms/LLVMBuild.txt<br>
    llvm/trunk/lib/Transforms/Scalar/LLVMBuild.txt<br>
    llvm/trunk/test/Other/new-pm-defaults.ll<br>
    llvm/trunk/test/Other/new-pm-lto-defaults.ll<br>
    llvm/trunk/test/Other/new-pm-thinlto-defaults.ll<br>
    llvm/trunk/tools/opt/CMakeLists.txt<br>
    llvm/trunk/tools/opt/opt.cpp<br>
<br>
Modified: llvm/trunk/docs/Passes.rst<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/Passes.rst?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/Passes.rst?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/docs/Passes.rst (original)<br>
+++ llvm/trunk/docs/Passes.rst Wed Jan 24 04:42:42 2018<br>
@@ -641,6 +641,21 @@ not library calls are simplified is cont<br>
 :ref:`-functionattrs <passes-functionattrs>` pass and LLVM's knowledge of<br>
 library calls on different targets.<br>
<br>
+.. _passes-aggressive-instcombine:<br>
+<br>
+``-aggressive-instcombine``: Combine expression patterns<br>
+--------------------------------------------------------<br>
+<br>
+Combine expression patterns to form expressions with fewer, simple instructions.<br>
+This pass does not modify the CFG.<br>
+<br>
+For example, this pass reduce width of expressions post-dominated by TruncInst<br>
+into smaller width when applicable.<br>
+<br>
+It differs from instcombine pass in that it contains pattern optimization that<br>
+requires higher complexity than the O(1), thus, it should run fewer times than<br>
+instcombine pass.<br>
+<br>
 ``-internalize``: Internalize Global Symbols<br>
 --------------------------------------------<br>
<br>
<br>
Modified: llvm/trunk/include/llvm/InitializePasses.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/InitializePasses.h (original)<br>
+++ llvm/trunk/include/llvm/InitializePasses.h Wed Jan 24 04:42:42 2018<br>
@@ -64,6 +64,7 @@ void initializeADCELegacyPassPass(PassRe<br>
 void initializeAddDiscriminatorsLegacyPassPass(PassRegistry&);<br>
 void initializeAddressSanitizerModulePass(PassRegistry&);<br>
 void initializeAddressSanitizerPass(PassRegistry&);<br>
+void initializeAggressiveInstCombinerLegacyPassPass(PassRegistry&);<br>
 void initializeAliasSetPrinterPass(PassRegistry&);<br>
 void initializeAlignmentFromAssumptionsPass(PassRegistry&);<br>
 void initializeAlwaysInlinerLegacyPassPass(PassRegistry&);<br>
<br>
Propchange: llvm/trunk/include/llvm/Transforms/AggressiveInstCombine/<br>
------------------------------------------------------------------------------<br>
    bugtraq:number = true<br>
<br>
Added: llvm/trunk/include/llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h?rev=323321&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h?rev=323321&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h (added)<br>
+++ llvm/trunk/include/llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h Wed Jan 24 04:42:42 2018<br>
@@ -0,0 +1,34 @@<br>
+//===- AggressiveInstCombine.h - AggressiveInstCombine pass -----*- C++ -*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+/// \file<br>
+///<br>
+/// This file provides the primary interface to the aggressive instcombine pass.<br>
+/// This pass is suitable for use in the new pass manager. For a pass that works<br>
+/// with the legacy pass manager, please look for<br>
+/// \c createAggressiveInstCombinerPass() in Scalar.h.<br>
+///<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_TRANSFORMS_AGGRESSIVE_INSTCOMBINE_INSTCOMBINE_H<br>
+#define LLVM_TRANSFORMS_AGGRESSIVE_INSTCOMBINE_INSTCOMBINE_H<br>
+<br>
+#include "llvm/IR/Function.h"<br>
+#include "llvm/IR/PassManager.h"<br>
+<br>
+namespace llvm {<br>
+<br>
+class AggressiveInstCombinePass<br>
+    : public PassInfoMixin<AggressiveInstCombinePass> {<br>
+<br>
+public:<br>
+  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);<br>
+};<br>
+}<br>
+<br>
+#endif<br>
<br>
Modified: llvm/trunk/include/llvm/Transforms/Scalar.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Scalar.h?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Scalar.h?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/Scalar.h (original)<br>
+++ llvm/trunk/include/llvm/Transforms/Scalar.h Wed Jan 24 04:42:42 2018<br>
@@ -142,6 +142,13 @@ FunctionPass *createInstructionCombining<br>
<br>
 //===----------------------------------------------------------------------===//<br>
 //<br>
+// AggressiveInstCombiner - Combine expression patterns to form expressions with<br>
+// fewer, simple instructions. This pass does not modify the CFG.<br>
+//<br>
+FunctionPass *createAggressiveInstCombinerPass();<br>
+<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
 // LICM - This pass is a loop invariant code motion and memory promotion pass.<br>
 //<br>
 Pass *createLICMPass();<br>
<br>
Modified: llvm/trunk/lib/LTO/LLVMBuild.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LLVMBuild.txt?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LLVMBuild.txt?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/LTO/LLVMBuild.txt (original)<br>
+++ llvm/trunk/lib/LTO/LLVMBuild.txt Wed Jan 24 04:42:42 2018<br>
@@ -20,6 +20,7 @@ type = Library<br>
 name = LTO<br>
 parent = Libraries<br>
 required_libraries =<br>
+ AggressiveInstCombine<br>
  Analysis<br>
  BitReader<br>
  BitWriter<br>
<br>
Modified: llvm/trunk/lib/Passes/LLVMBuild.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Passes/LLVMBuild.txt?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Passes/LLVMBuild.txt?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Passes/LLVMBuild.txt (original)<br>
+++ llvm/trunk/lib/Passes/LLVMBuild.txt Wed Jan 24 04:42:42 2018<br>
@@ -19,4 +19,4 @@<br>
 type = Library<br>
 name = Passes<br>
 parent = Libraries<br>
-required_libraries = Analysis CodeGen Core IPO InstCombine Scalar Support Target TransformUtils Vectorize Instrumentation<br>
+required_libraries = AggressiveInstCombine Analysis CodeGen Core IPO InstCombine Scalar Support Target TransformUtils Vectorize Instrumentation<br>
<br>
Modified: llvm/trunk/lib/Passes/PassBuilder.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Passes/PassBuilder.cpp?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Passes/PassBuilder.cpp?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Passes/PassBuilder.cpp (original)<br>
+++ llvm/trunk/lib/Passes/PassBuilder.cpp Wed Jan 24 04:42:42 2018<br>
@@ -59,6 +59,7 @@<br>
 #include "llvm/Support/Debug.h"<br>
 #include "llvm/Support/Regex.h"<br>
 #include "llvm/Target/TargetMachine.h"<br>
+#include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"<br>
 #include "llvm/Transforms/GCOVProfiler.h"<br>
 #include "llvm/Transforms/IPO/AlwaysInliner.h"<br>
 #include "llvm/Transforms/IPO/ArgumentPromotion.h"<br>
@@ -363,6 +364,8 @@ PassBuilder::buildFunctionSimplification<br>
   FPM.addPass(JumpThreadingPass());<br>
   FPM.addPass(CorrelatedValuePropagationPass());<br>
   FPM.addPass(SimplifyCFGPass());<br>
+  if (Level == O3)<br>
+    FPM.addPass(AggressiveInstCombinePass());<br>
   FPM.addPass(InstCombinePass());<br>
<br>
   if (!isOptimizingForSize(Level))<br>
@@ -1010,6 +1013,8 @@ ModulePassManager PassBuilder::buildLTOD<br>
   // function pointers.  When this happens, we often have to resolve varargs<br>
   // calls, etc, so let instcombine do this.<br>
   FunctionPassManager PeepholeFPM(DebugLogging);<br>
+  if (Level == O3)<br>
+    PeepholeFPM.addPass(AggressiveInstCombinePass());<br>
   PeepholeFPM.addPass(InstCombinePass());<br>
   invokePeepholeEPCallbacks(PeepholeFPM, Level);<br>
<br>
<br>
Modified: llvm/trunk/lib/Passes/PassRegistry.def<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Passes/PassRegistry.def?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Passes/PassRegistry.def?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Passes/PassRegistry.def (original)<br>
+++ llvm/trunk/lib/Passes/PassRegistry.def Wed Jan 24 04:42:42 2018<br>
@@ -139,6 +139,7 @@ FUNCTION_ALIAS_ANALYSIS("type-based-aa",<br>
 FUNCTION_PASS("aa-eval", AAEvaluator())<br>
 FUNCTION_PASS("adce", ADCEPass())<br>
 FUNCTION_PASS("add-discriminators", AddDiscriminatorsPass())<br>
+FUNCTION_PASS("aggressive-instcombine", AggressiveInstCombinePass())<br>
 FUNCTION_PASS("alignment-from-assumptions", AlignmentFromAssumptionsPass())<br>
 FUNCTION_PASS("bdce", BDCEPass())<br>
 FUNCTION_PASS("bounds-checking", BoundsCheckingPass())<br>
<br>
Propchange: llvm/trunk/lib/Transforms/AggressiveInstCombine/<br>
------------------------------------------------------------------------------<br>
    bugtraq:number = true<br>
<br>
Added: llvm/trunk/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp?rev=323321&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp?rev=323321&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp (added)<br>
+++ llvm/trunk/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp Wed Jan 24 04:42:42 2018<br>
@@ -0,0 +1,110 @@<br>
+//===- AggressiveInstCombine.cpp ------------------------------------------===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// This file implements the aggressive expression pattern combiner classes.<br>
+// Currently, it handles expression patterns for:<br>
+//  * Truncate instruction<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"<br>
+#include "AggressiveInstCombineInternal.h"<br>
+#include "llvm/Analysis/AliasAnalysis.h"<br>
+#include "llvm/Analysis/BasicAliasAnalysis.h"<br>
+#include "llvm/Analysis/GlobalsModRef.h"<br>
+#include "llvm/Analysis/TargetLibraryInfo.h"<br>
+#include "llvm/IR/DataLayout.h"<br>
+#include "llvm/Pass.h"<br>
+#include "llvm/Transforms/Scalar.h"<br>
+using namespace llvm;<br>
+<br>
+#define DEBUG_TYPE "aggressive-instcombine"<br>
+<br>
+namespace {<br>
+/// Contains expression pattern combiner logic.<br>
+/// This class provides both the logic to combine expression patterns and<br>
+/// combine them. It differs from InstCombiner class in that each pattern<br>
+/// combiner runs only once as opposed to InstCombine's multi-iteration,<br>
+/// which allows pattern combiner to have higher complexity than the O(1)<br>
+/// required by the instruction combiner.<br>
+class AggressiveInstCombinerLegacyPass : public FunctionPass {<br>
+public:<br>
+  static char ID; // Pass identification, replacement for typeid<br>
+<br>
+  AggressiveInstCombinerLegacyPass() : FunctionPass(ID) {<br>
+    initializeAggressiveInstCombinerLegacyPassPass(<br>
+        *PassRegistry::getPassRegistry());<br>
+  }<br>
+<br>
+  void getAnalysisUsage(AnalysisUsage &AU) const override;<br>
+<br>
+  /// Run all expression pattern optimizations on the given /p F function.<br>
+  ///<br>
+  /// \param F function to optimize.<br>
+  /// \returns true if the IR is changed.<br>
+  bool runOnFunction(Function &F) override;<br>
+};<br>
+} // namespace<br>
+<br>
+void AggressiveInstCombinerLegacyPass::getAnalysisUsage(<br>
+    AnalysisUsage &AU) const {<br>
+  AU.setPreservesCFG();<br>
+  AU.addRequired<TargetLibraryInfoWrapperPass>();<br>
+  AU.addPreserved<AAResultsWrapperPass>();<br>
+  AU.addPreserved<BasicAAWrapperPass>();<br>
+  AU.addPreserved<GlobalsAAWrapperPass>();<br>
+}<br>
+<br>
+bool AggressiveInstCombinerLegacyPass::runOnFunction(Function &F) {<br>
+  auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();<br>
+  auto &DL = F.getParent()->getDataLayout();<br>
+<br>
+  bool MadeIRChange = false;<br>
+<br>
+  // Handle TruncInst patterns<br>
+  TruncInstCombine TIC(TLI, DL);<br>
+  MadeIRChange |= TIC.run(F);<br>
+<br>
+  // TODO: add more patterns to handle...<br>
+<br>
+  return MadeIRChange;<br>
+}<br>
+<br>
+PreservedAnalyses AggressiveInstCombinePass::run(Function &F,<br>
+                                                 FunctionAnalysisManager &AM) {<br>
+  auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);<br>
+  auto &DL = F.getParent()->getDataLayout();<br>
+  bool MadeIRChange = false;<br>
+<br>
+  // Handle TruncInst patterns<br>
+  TruncInstCombine TIC(TLI, DL);<br>
+  MadeIRChange |= TIC.run(F);<br>
+  if (!MadeIRChange)<br>
+    // No changes, all analyses are preserved.<br>
+    return PreservedAnalyses::all();<br>
+<br>
+  // Mark all the analyses that instcombine updates as preserved.<br>
+  PreservedAnalyses PA;<br>
+  PA.preserveSet<CFGAnalyses>();<br>
+  PA.preserve<AAManager>();<br>
+  PA.preserve<GlobalsAA>();<br>
+  return PA;<br>
+}<br>
+<br>
+char AggressiveInstCombinerLegacyPass::ID = 0;<br>
+INITIALIZE_PASS_BEGIN(AggressiveInstCombinerLegacyPass,<br>
+                      "aggressive-instcombine",<br>
+                      "Combine pattern based expressions", false, false)<br>
+INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)<br>
+INITIALIZE_PASS_END(AggressiveInstCombinerLegacyPass, "aggressive-instcombine",<br>
+                    "Combine pattern based expressions", false, false)<br>
+<br>
+FunctionPass *llvm::createAggressiveInstCombinerPass() {<br>
+  return new AggressiveInstCombinerLegacyPass();<br>
+}<br>
<br>
Added: llvm/trunk/lib/Transforms/AggressiveInstCombine/AggressiveInstCombineInternal.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/AggressiveInstCombine/AggressiveInstCombineInternal.h?rev=323321&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/AggressiveInstCombine/AggressiveInstCombineInternal.h?rev=323321&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/AggressiveInstCombine/AggressiveInstCombineInternal.h (added)<br>
+++ llvm/trunk/lib/Transforms/AggressiveInstCombine/AggressiveInstCombineInternal.h Wed Jan 24 04:42:42 2018<br>
@@ -0,0 +1,119 @@<br>
+//===- AggressiveInstCombineInternal.h --------------------------*- C++ -*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// This file implements the instruction pattern combiner classes.<br>
+// Currently, it handles pattern expressions for:<br>
+//  * Truncate instruction<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/ADT/MapVector.h"<br>
+#include "llvm/ADT/STLExtras.h"<br>
+#include "llvm/Analysis/AliasAnalysis.h"<br>
+#include "llvm/Analysis/BasicAliasAnalysis.h"<br>
+#include "llvm/Analysis/ConstantFolding.h"<br>
+#include "llvm/Analysis/GlobalsModRef.h"<br>
+#include "llvm/Analysis/TargetLibraryInfo.h"<br>
+#include "llvm/IR/DataLayout.h"<br>
+#include "llvm/Pass.h"<br>
+#include "llvm/Transforms/Scalar.h"<br>
+using namespace llvm;<br>
+<br>
+//===----------------------------------------------------------------------===//<br>
+// TruncInstCombine - looks for expression dags dominated by trunc instructions<br>
+// and for each eligible dag, it will create a reduced bit-width expression and<br>
+// replace the old expression with this new one and remove the old one.<br>
+// Eligible expression dag is such that:<br>
+//   1. Contains only supported instructions.<br>
+//   2. Supported leaves: ZExtInst, SExtInst, TruncInst and Constant value.<br>
+//   3. Can be evaluated into type with reduced legal bit-width (or Trunc type).<br>
+//   4. All instructions in the dag must not have users outside the dag.<br>
+//      Only exception is for {ZExt, SExt}Inst with operand type equal to the<br>
+//      new reduced type chosen in (3).<br>
+//<br>
+// The motivation for this optimization is that evaluating and expression using<br>
+// smaller bit-width is preferable, especially for vectorization where we can<br>
+// fit more values in one vectorized instruction. In addition, this optimization<br>
+// may decrease the number of cast instructions, but will not increase it.<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+namespace llvm {<br>
+  class DataLayout;<br>
+  class TargetLibraryInfo;<br>
+<br>
+class TruncInstCombine {<br>
+  TargetLibraryInfo &TLI;<br>
+  const DataLayout &DL;<br>
+<br>
+  /// List of all TruncInst instructions to be processed.<br>
+  SmallVector<TruncInst *, 4> Worklist;<br>
+<br>
+  /// Current processed TruncInst instruction.<br>
+  TruncInst *CurrentTruncInst;<br>
+<br>
+  /// Information per each instruction in the expression dag.<br>
+  struct Info {<br>
+    /// Number of LSBs that are needed to generate a valid expression.<br>
+    unsigned ValidBitWidth = 0;<br>
+    /// Minimum number of LSBs needed to generate the ValidBitWidth.<br>
+    unsigned MinBitWidth = 0;<br>
+    /// The reduced value generated to replace the old instruction.<br>
+    Value *NewValue = nullptr;<br>
+  };<br>
+  /// An ordered map representing expression dag post-dominated by current<br>
+  /// processed TruncInst. It maps each instruction in the dag to its Info<br>
+  /// structure. The map is ordered such that each instruction appears before<br>
+  /// all other instructions in the dag that uses it.<br>
+  MapVector<Instruction *, Info> InstInfoMap;<br>
+<br>
+public:<br>
+  TruncInstCombine(TargetLibraryInfo &TLI, const DataLayout &DL)<br>
+      : TLI(TLI), DL(DL), CurrentTruncInst(nullptr) {}<br>
+<br>
+  /// Perform TruncInst pattern optimization on given function.<br>
+  bool run(Function &F);<br>
+<br>
+private:<br>
+  /// Build expression dag dominated by the /p CurrentTruncInst and append it to<br>
+  /// the InstInfoMap container.<br>
+  ///<br>
+  /// \return true only if succeed to generate an eligible sub expression dag.<br>
+  bool buildTruncExpressionDag();<br>
+<br>
+  /// Calculate the minimal allowed bit-width of the chain ending with the<br>
+  /// currently visited truncate's operand.<br>
+  ///<br>
+  /// \return minimum number of bits to which the chain ending with the<br>
+  /// truncate's operand can be shrunk to.<br>
+  unsigned getMinBitWidth();<br>
+<br>
+  /// Build an expression dag dominated by the current processed TruncInst and<br>
+  /// Check if it is eligible to be reduced to a smaller type.<br>
+  ///<br>
+  /// \return the scalar version of the new type to be used for the reduced<br>
+  ///         expression dag, or nullptr if the expression dag is not eligible<br>
+  ///         to be reduced.<br>
+  Type *getBestTruncatedType();<br>
+<br>
+  /// Given a \p V value and a \p SclTy scalar type return the generated reduced<br>
+  /// value of \p V based on the type \p SclTy.<br>
+  ///<br>
+  /// \param V value to be reduced.<br>
+  /// \param SclTy scalar version of new type to reduce to.<br>
+  /// \return the new reduced value.<br>
+  Value *getReducedOperand(Value *V, Type *SclTy);<br>
+<br>
+  /// Create a new expression dag using the reduced /p SclTy type and replace<br>
+  /// the old expression dag with it. Also erase all instructions in the old<br>
+  /// dag, except those that are still needed outside the dag.<br>
+  ///<br>
+  /// \param SclTy scalar version of new type to reduce expression dag into.<br>
+  void ReduceExpressionDag(Type *SclTy);<br>
+};<br>
+} // end namespace llvm.<br>
<br>
Added: llvm/trunk/lib/Transforms/AggressiveInstCombine/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/AggressiveInstCombine/CMakeLists.txt?rev=323321&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/AggressiveInstCombine/CMakeLists.txt?rev=323321&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/AggressiveInstCombine/CMakeLists.txt (added)<br>
+++ llvm/trunk/lib/Transforms/AggressiveInstCombine/CMakeLists.txt Wed Jan 24 04:42:42 2018<br>
@@ -0,0 +1,11 @@<br>
+add_llvm_library(LLVMAggressiveInstCombine<br>
+  AggressiveInstCombine.cpp<br>
+  TruncInstCombine.cpp<br>
+<br>
+  ADDITIONAL_HEADER_DIRS<br>
+  ${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms<br>
+  ${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms/AggressiveInstCombine<br>
+<br>
+  DEPENDS<br>
+  intrinsics_gen<br>
+  )<br>
<br>
Added: llvm/trunk/lib/Transforms/AggressiveInstCombine/LLVMBuild.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/AggressiveInstCombine/LLVMBuild.txt?rev=323321&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/AggressiveInstCombine/LLVMBuild.txt?rev=323321&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/AggressiveInstCombine/LLVMBuild.txt (added)<br>
+++ llvm/trunk/lib/Transforms/AggressiveInstCombine/LLVMBuild.txt Wed Jan 24 04:42:42 2018<br>
@@ -0,0 +1,22 @@<br>
+;===- ./lib/Transforms/AggressiveInstCombine/LLVMBuild.txt -----*- Conf -*--===;<br>
+;<br>
+;                     The LLVM Compiler Infrastructure<br>
+;<br>
+; This file is distributed under the University of Illinois Open Source<br>
+; License. See LICENSE.TXT for details.<br>
+;<br>
+;===------------------------------------------------------------------------===;<br>
+;<br>
+; This is an LLVMBuild description file for the components in this subdirectory.<br>
+;<br>
+; For more information on the LLVMBuild system, please see:<br>
+;<br>
+;   <a href="http://llvm.org/docs/LLVMBuild.html" rel="noreferrer" target="_blank">http://llvm.org/docs/LLVMBuild.html</a><br>
+;<br>
+;===------------------------------------------------------------------------===;<br>
+<br>
+[component_0]<br>
+type = Library<br>
+name = AggressiveInstCombine<br>
+parent = Transforms<br>
+required_libraries = Analysis Core Support TransformUtils<br>
<br>
Added: llvm/trunk/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp?rev=323321&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp?rev=323321&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp (added)<br>
+++ llvm/trunk/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp Wed Jan 24 04:42:42 2018<br>
@@ -0,0 +1,401 @@<br>
+//===- TruncInstCombine.cpp -----------------------------------------------===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// TruncInstCombine - looks for expression dags post-dominated by TruncInst and<br>
+// for each eligible dag, it will create a reduced bit-width expression, replace<br>
+// the old expression with this new one and remove the old expression.<br>
+// Eligible expression dag is such that:<br>
+//   1. Contains only supported instructions.<br>
+//   2. Supported leaves: ZExtInst, SExtInst, TruncInst and Constant value.<br>
+//   3. Can be evaluated into type with reduced legal bit-width.<br>
+//   4. All instructions in the dag must not have users outside the dag.<br>
+//      The only exception is for {ZExt, SExt}Inst with operand type equal to<br>
+//      the new reduced type evaluated in (3).<br>
+//<br>
+// The motivation for this optimization is that evaluating and expression using<br>
+// smaller bit-width is preferable, especially for vectorization where we can<br>
+// fit more values in one vectorized instruction. In addition, this optimization<br>
+// may decrease the number of cast instructions, but will not increase it.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "AggressiveInstCombineInternal.h"<br>
+#include "llvm/ADT/MapVector.h"<br>
+#include "llvm/ADT/STLExtras.h"<br>
+#include "llvm/Analysis/ConstantFolding.h"<br>
+#include "llvm/Analysis/TargetLibraryInfo.h"<br>
+#include "llvm/IR/DataLayout.h"<br>
+#include "llvm/IR/IRBuilder.h"<br>
+using namespace llvm;<br>
+<br>
+#define DEBUG_TYPE "aggressive-instcombine"<br>
+<br>
+/// Given an instruction and a container, it fills all the relevant operands of<br>
+/// that instruction, with respect to the Trunc expression dag optimizaton.<br>
+static void getRelevantOperands(Instruction *I, SmallVectorImpl<Value *> &Ops) {<br>
+  unsigned Opc = I->getOpcode();<br>
+  switch (Opc) {<br>
+  case Instruction::Trunc:<br>
+  case Instruction::ZExt:<br>
+  case Instruction::SExt:<br>
+    // These CastInst are considered leaves of the evaluated expression, thus,<br>
+    // their operands are not relevent.<br>
+    break;<br>
+  case Instruction::Add:<br>
+  case Instruction::Sub:<br>
+  case Instruction::Mul:<br>
+  case Instruction::And:<br>
+  case Instruction::Or:<br>
+  case Instruction::Xor:<br>
+    Ops.push_back(I->getOperand(0));<br>
+    Ops.push_back(I->getOperand(1));<br>
+    break;<br>
+  default:<br>
+    llvm_unreachable("Unreachable!");<br>
+  }<br>
+}<br>
+<br>
+bool TruncInstCombine::buildTruncExpressionDag() {<br>
+  SmallVector<Value *, 8> Worklist;<br>
+  SmallVector<Instruction *, 8> Stack;<br>
+  // Clear old expression dag.<br>
+  InstInfoMap.clear();<br>
+<br>
+  Worklist.push_back(CurrentTruncInst->getOperand(0));<br>
+<br>
+  while (!Worklist.empty()) {<br>
+    Value *Curr = Worklist.back();<br>
+<br>
+    if (isa<Constant>(Curr)) {<br>
+      Worklist.pop_back();<br>
+      continue;<br>
+    }<br>
+<br>
+    auto *I = dyn_cast<Instruction>(Curr);<br>
+    if (!I)<br>
+      return false;<br>
+<br>
+    if (!Stack.empty() && Stack.back() == I) {<br>
+      // Already handled all instruction operands, can remove it from both the<br>
+      // Worklist and the Stack, and add it to the instruction info map.<br>
+      Worklist.pop_back();<br>
+      Stack.pop_back();<br>
+      // Insert I to the Info map.<br>
+      InstInfoMap.insert(std::make_pair(I, Info()));<br>
+      continue;<br>
+    }<br>
+<br>
+    if (InstInfoMap.count(I)) {<br>
+      Worklist.pop_back();<br>
+      continue;<br>
+    }<br>
+<br>
+    // Add the instruction to the stack before start handling its operands.<br>
+    Stack.push_back(I);<br>
+<br>
+    unsigned Opc = I->getOpcode();<br>
+    switch (Opc) {<br>
+    case Instruction::Trunc:<br>
+    case Instruction::ZExt:<br>
+    case Instruction::SExt:<br>
+      // trunc(trunc(x)) -> trunc(x)<br>
+      // trunc(ext(x)) -> ext(x) if the source type is smaller than the new dest<br>
+      // trunc(ext(x)) -> trunc(x) if the source type is larger than the new<br>
+      // dest<br>
+      break;<br>
+    case Instruction::Add:<br>
+    case Instruction::Sub:<br>
+    case Instruction::Mul:<br>
+    case Instruction::And:<br>
+    case Instruction::Or:<br>
+    case Instruction::Xor: {<br>
+      SmallVector<Value *, 2> Operands;<br>
+      getRelevantOperands(I, Operands);<br>
+      for (Value *Operand : Operands)<br>
+        Worklist.push_back(Operand);<br>
+      break;<br>
+    }<br>
+    default:<br>
+      // TODO: Can handle more cases here:<br>
+      // 1. select, shufflevector, extractelement, insertelement<br>
+      // 2. udiv, urem<br>
+      // 3. shl, lshr, ashr<br>
+      // 4. phi node(and loop handling)<br>
+      // ...<br>
+      return false;<br>
+    }<br>
+  }<br>
+  return true;<br>
+}<br>
+<br>
+unsigned TruncInstCombine::getMinBitWidth() {<br>
+  SmallVector<Value *, 8> Worklist;<br>
+  SmallVector<Instruction *, 8> Stack;<br>
+<br>
+  Value *Src = CurrentTruncInst->getOperand(0);<br>
+  Type *DstTy = CurrentTruncInst->getType();<br>
+  unsigned TruncBitWidth = DstTy->getScalarSizeInBits();<br>
+  unsigned OrigBitWidth =<br>
+      CurrentTruncInst->getOperand(0)->getType()->getScalarSizeInBits();<br>
+<br>
+  if (isa<Constant>(Src))<br>
+    return TruncBitWidth;<br>
+<br>
+  Worklist.push_back(Src);<br>
+  InstInfoMap[cast<Instruction>(Src)].ValidBitWidth = TruncBitWidth;<br>
+<br>
+  while (!Worklist.empty()) {<br>
+    Value *Curr = Worklist.back();<br>
+<br>
+    if (isa<Constant>(Curr)) {<br>
+      Worklist.pop_back();<br>
+      continue;<br>
+    }<br>
+<br>
+    // Otherwise, it must be an instruction.<br>
+    auto *I = cast<Instruction>(Curr);<br>
+<br>
+    auto &Info = InstInfoMap[I];<br>
+<br>
+    SmallVector<Value *, 2> Operands;<br>
+    getRelevantOperands(I, Operands);<br>
+<br>
+    if (!Stack.empty() && Stack.back() == I) {<br>
+      // Already handled all instruction operands, can remove it from both, the<br>
+      // Worklist and the Stack, and update MinBitWidth.<br>
+      Worklist.pop_back();<br>
+      Stack.pop_back();<br>
+      for (auto *Operand : Operands)<br>
+        if (auto *IOp = dyn_cast<Instruction>(Operand))<br>
+          Info.MinBitWidth =<br>
+              std::max(Info.MinBitWidth, InstInfoMap[IOp].MinBitWidth);<br>
+      continue;<br>
+    }<br>
+<br>
+    // Add the instruction to the stack before start handling its operands.<br>
+    Stack.push_back(I);<br>
+    unsigned ValidBitWidth = Info.ValidBitWidth;<br>
+<br>
+    // Update minimum bit-width before handling its operands. This is required<br>
+    // when the instruction is part of a loop.<br>
+    Info.MinBitWidth = std::max(Info.MinBitWidth, Info.ValidBitWidth);<br>
+<br>
+    for (auto *Operand : Operands)<br>
+      if (auto *IOp = dyn_cast<Instruction>(Operand)) {<br>
+        // If we already calculated the minimum bit-width for this valid<br>
+        // bit-width, or for a smaller valid bit-width, then just keep the<br>
+        // answer we already calculated.<br>
+        unsigned IOpBitwidth = InstInfoMap.lookup(IOp).ValidBitWidth;<br>
+        if (IOpBitwidth >= ValidBitWidth)<br>
+          continue;<br>
+        InstInfoMap[IOp].ValidBitWidth = std::max(ValidBitWidth, IOpBitwidth);<br>
+        Worklist.push_back(IOp);<br>
+      }<br>
+  }<br>
+  unsigned MinBitWidth = InstInfoMap.lookup(cast<Instruction>(Src)).MinBitWidth;<br>
+  assert(MinBitWidth >= TruncBitWidth);<br>
+<br>
+  if (MinBitWidth > TruncBitWidth) {<br>
+    // In this case reducing expression with vector type might generate a new<br>
+    // vector type, which is not preferable as it might result in generating<br>
+    // sub-optimal code.<br>
+    if (DstTy->isVectorTy())<br>
+      return OrigBitWidth;<br>
+    // Use the smallest integer type in the range [MinBitWidth, OrigBitWidth).<br>
+    Type *Ty = DL.getSmallestLegalIntType(DstTy->getContext(), MinBitWidth);<br>
+    // Update minimum bit-width with the new destination type bit-width if<br>
+    // succeeded to find such, otherwise, with original bit-width.<br>
+    MinBitWidth = Ty ? Ty->getScalarSizeInBits() : OrigBitWidth;<br>
+  } else { // MinBitWidth == TruncBitWidth<br>
+    // In this case the expression can be evaluated with the trunc instruction<br>
+    // destination type, and trunc instruction can be omitted. However, we<br>
+    // should not perform the evaluation if the original type is a legal scalar<br>
+    // type and the target type is illegal.<br>
+    bool FromLegal = MinBitWidth == 1 || DL.isLegalInteger(OrigBitWidth);<br>
+    bool ToLegal = MinBitWidth == 1 || DL.isLegalInteger(MinBitWidth);<br>
+    if (!DstTy->isVectorTy() && FromLegal && !ToLegal)<br>
+      return OrigBitWidth;<br>
+  }<br>
+  return MinBitWidth;<br>
+}<br>
+<br>
+Type *TruncInstCombine::getBestTruncatedType() {<br>
+  if (!buildTruncExpressionDag())<br>
+    return nullptr;<br>
+<br>
+  // We don't want to duplicate instructions, which isn't profitable. Thus, we<br>
+  // can't shrink something that has multiple users, unless all users are<br>
+  // post-dominated by the trunc instruction, i.e., were visited during the<br>
+  // expression evaluation.<br>
+  unsigned DesiredBitWidth = 0;<br>
+  for (auto Itr : InstInfoMap) {<br>
+    Instruction *I = Itr.first;<br>
+    if (I->hasOneUse())<br>
+      continue;<br>
+    bool IsExtInst = (isa<ZExtInst>(I) || isa<SExtInst>(I));<br>
+    for (auto *U : I->users())<br>
+      if (auto *UI = dyn_cast<Instruction>(U))<br>
+        if (UI != CurrentTruncInst && !InstInfoMap.count(UI)) {<br>
+          if (!IsExtInst)<br>
+            return nullptr;<br>
+          // If this is an extension from the dest type, we can eliminate it,<br>
+          // even if it has multiple users. Thus, update the DesiredBitWidth and<br>
+          // validate all extension instructions agrees on same DesiredBitWidth.<br>
+          unsigned ExtInstBitWidth =<br>
+              I->getOperand(0)->getType()->getScalarSizeInBits();<br>
+          if (DesiredBitWidth && DesiredBitWidth != ExtInstBitWidth)<br>
+            return nullptr;<br>
+          DesiredBitWidth = ExtInstBitWidth;<br>
+        }<br>
+  }<br>
+<br>
+  unsigned OrigBitWidth =<br>
+      CurrentTruncInst->getOperand(0)->getType()->getScalarSizeInBits();<br>
+<br>
+  // Calculate minimum allowed bit-width allowed for shrinking the currently<br>
+  // visited truncate's operand.<br>
+  unsigned MinBitWidth = getMinBitWidth();<br>
+<br>
+  // Check that we can shrink to smaller bit-width than original one and that<br>
+  // it is similar to the DesiredBitWidth is such exists.<br>
+  if (MinBitWidth >= OrigBitWidth ||<br>
+      (DesiredBitWidth && DesiredBitWidth != MinBitWidth))<br>
+    return nullptr;<br>
+<br>
+  return IntegerType::get(CurrentTruncInst->getContext(), MinBitWidth);<br>
+}<br>
+<br>
+/// Given a reduced scalar type \p Ty and a \p V value, return a reduced type<br>
+/// for \p V, according to its type, if it vector type, return the vector<br>
+/// version of \p Ty, otherwise return \p Ty.<br>
+static Type *getReducedType(Value *V, Type *Ty) {<br>
+  assert(Ty && !Ty->isVectorTy() && "Expect Scalar Type");<br>
+  if (auto *VTy = dyn_cast<VectorType>(V->getType()))<br>
+    return VectorType::get(Ty, VTy->getNumElements());<br>
+  return Ty;<br>
+}<br>
+<br>
+Value *TruncInstCombine::getReducedOperand(Value *V, Type *SclTy) {<br>
+  Type *Ty = getReducedType(V, SclTy);<br>
+  if (auto *C = dyn_cast<Constant>(V)) {<br>
+    C = ConstantExpr::getIntegerCast(C, Ty, false);<br>
+    // If we got a constantexpr back, try to simplify it with DL info.<br>
+    if (Constant *FoldedC = ConstantFoldConstant(C, DL, &TLI))<br>
+      C = FoldedC;<br>
+    return C;<br>
+  }<br>
+<br>
+  auto *I = cast<Instruction>(V);<br>
+  Info Entry = InstInfoMap.lookup(I);<br>
+  assert(Entry.NewValue);<br>
+  return Entry.NewValue;<br>
+}<br>
+<br>
+void TruncInstCombine::ReduceExpressionDag(Type *SclTy) {<br>
+  for (auto &Itr : InstInfoMap) { // Forward<br>
+    Instruction *I = Itr.first;<br>
+<br>
+    assert(!InstInfoMap.lookup(I).NewValue && "Instruction has been evaluated");<br>
+<br>
+    IRBuilder<> Builder(I);<br>
+    Value *Res = nullptr;<br>
+    unsigned Opc = I->getOpcode();<br>
+    switch (Opc) {<br>
+    case Instruction::Trunc:<br>
+    case Instruction::ZExt:<br>
+    case Instruction::SExt: {<br>
+      Type *Ty = getReducedType(I, SclTy);<br>
+      // If the source type of the cast is the type we're trying for then we can<br>
+      // just return the source.  There's no need to insert it because it is not<br>
+      // new.<br>
+      if (I->getOperand(0)->getType() == Ty) {<br>
+        InstInfoMap[I].NewValue = I->getOperand(0);<br>
+        continue;<br>
+      }<br>
+      // Otherwise, must be the same type of cast, so just reinsert a new one.<br>
+      // This also handles the case of zext(trunc(x)) -> zext(x).<br>
+      Res = Builder.CreateIntCast(I->getOperand(0), Ty,<br>
+                                  Opc == Instruction::SExt);<br>
+<br>
+      // Update Worklist entries with new value if needed.<br>
+      if (auto *NewCI = dyn_cast<TruncInst>(Res)) {<br>
+        auto Entry = find(Worklist, I);<br>
+        if (Entry != Worklist.end())<br>
+          *Entry = NewCI;<br>
+      }<br>
+      break;<br>
+    }<br>
+    case Instruction::Add:<br>
+    case Instruction::Sub:<br>
+    case Instruction::Mul:<br>
+    case Instruction::And:<br>
+    case Instruction::Or:<br>
+    case Instruction::Xor: {<br>
+      Value *LHS = getReducedOperand(I->getOperand(0), SclTy);<br>
+      Value *RHS = getReducedOperand(I->getOperand(1), SclTy);<br>
+      Res = Builder.CreateBinOp((Instruction::BinaryOps)Opc, LHS, RHS);<br>
+      break;<br>
+    }<br>
+    default:<br>
+      llvm_unreachable("Unhandled instruction");<br>
+    }<br>
+<br>
+    InstInfoMap[I].NewValue = Res;<br>
+    cast<Instruction>(Res)->takeName(I);<br>
+  }<br>
+<br>
+  Value *Res = getReducedOperand(CurrentTruncInst->getOperand(0), SclTy);<br>
+  Type *DstTy = CurrentTruncInst->getType();<br>
+  if (Res->getType() != DstTy) {<br>
+    IRBuilder<> Builder(CurrentTruncInst);<br>
+    Res = Builder.CreateIntCast(Res, DstTy, false);<br>
+    cast<Instruction>(Res)->takeName(CurrentTruncInst);<br>
+  }<br>
+  CurrentTruncInst->replaceAllUsesWith(Res);<br>
+<br>
+  // Erase old expression dag, which was replaced by the reduced expression dag.<br>
+  // We iterate backward, which means we visit the instruction before we visit<br>
+  // any of its operands, this way, when we get to the operand, we already<br>
+  // removed the instructions (from the expression dag) that uses it.<br>
+  CurrentTruncInst->eraseFromParent();<br>
+  for (auto I = InstInfoMap.rbegin(), E = InstInfoMap.rend(); I != E; ++I) {<br>
+    // We still need to check that the instruction has no users before we erase<br>
+    // it, because {SExt, ZExt}Inst Instruction might have other users that was<br>
+    // not reduced, in such case, we need to keep that instruction.<br>
+    if (!I->first->getNumUses())<br>
+      I->first->eraseFromParent();<br>
+  }<br>
+}<br>
+<br>
+bool TruncInstCombine::run(Function &F) {<br>
+  bool MadeIRChange = false;<br>
+<br>
+  // Collect all TruncInst in the function into the Worklist for evaluating.<br>
+  for (auto &BB : F)<br>
+    for (auto &I : BB)<br>
+      if (auto *CI = dyn_cast<TruncInst>(&I))<br>
+        Worklist.push_back(CI);<br>
+<br>
+  // Process all TruncInst in the Worklist, for each instruction:<br>
+  //   1. Check if it dominates an eligible expression dag to be reduced.<br>
+  //   2. Create a reduced expression dag and replace the old one with it.<br>
+  while (!Worklist.empty()) {<br>
+    CurrentTruncInst = Worklist.pop_back_val();<br>
+<br>
+    if (Type *NewDstSclTy = getBestTruncatedType()) {<br>
+      DEBUG(dbgs() << "ICE: TruncInstCombine reducing type of expression dag "<br>
+                      "dominated by: "<br>
+                   << CurrentTruncInst << '\n');<br>
+      ReduceExpressionDag(NewDstSclTy);<br>
+      MadeIRChange = true;<br>
+    }<br>
+  }<br>
+<br>
+  return MadeIRChange;<br>
+}<br>
<br>
Modified: llvm/trunk/lib/Transforms/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/CMakeLists.txt?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/CMakeLists.txt?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/CMakeLists.txt (original)<br>
+++ llvm/trunk/lib/Transforms/CMakeLists.txt Wed Jan 24 04:42:42 2018<br>
@@ -1,5 +1,6 @@<br>
 add_subdirectory(Utils)<br>
 add_subdirectory(Instrumentation)<br>
+add_subdirectory(AggressiveInstCombine)<br>
 add_subdirectory(InstCombine)<br>
 add_subdirectory(Scalar)<br>
 add_subdirectory(IPO)<br>
<br>
Modified: llvm/trunk/lib/Transforms/IPO/LLVMBuild.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/LLVMBuild.txt?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/LLVMBuild.txt?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/LLVMBuild.txt (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/LLVMBuild.txt Wed Jan 24 04:42:42 2018<br>
@@ -20,4 +20,4 @@ type = Library<br>
 name = IPO<br>
 parent = Transforms<br>
 library_name = ipo<br>
-required_libraries = Analysis BitReader BitWriter Core InstCombine IRReader Linker Object ProfileData Scalar Support TransformUtils Vectorize Instrumentation<br>
+required_libraries = AggressiveInstCombine Analysis BitReader BitWriter Core InstCombine IRReader Linker Object ProfileData Scalar Support TransformUtils Vectorize Instrumentation<br>
<br>
Modified: llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp Wed Jan 24 04:42:42 2018<br>
@@ -318,6 +318,8 @@ void PassManagerBuilder::addFunctionSimp<br>
   MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals<br>
   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs<br>
   // Combine silly seq's<br>
+  if (OptLevel > 2)<br>
+    MPM.add(createAggressiveInstCombinerPass());<br>
   addInstructionCombiningPass(MPM);<br>
   if (SizeLevel == 0 && !DisableLibCallsShrinkWrap)<br>
     MPM.add(createLibCallsShrinkWrapPass());<br>
@@ -765,6 +767,8 @@ void PassManagerBuilder::addLTOOptimizat<br>
   // simplification opportunities, and both can propagate functions through<br>
   // function pointers.  When this happens, we often have to resolve varargs<br>
   // calls, etc, so let instcombine do this.<br>
+  if (OptLevel > 2)<br>
+    PM.add(createAggressiveInstCombinerPass());<br>
   addInstructionCombiningPass(PM);<br>
   addExtensionsToPM(EP_Peephole, PM);<br>
<br>
<br>
Modified: llvm/trunk/lib/Transforms/LLVMBuild.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/LLVMBuild.txt?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/LLVMBuild.txt?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/LLVMBuild.txt (original)<br>
+++ llvm/trunk/lib/Transforms/LLVMBuild.txt Wed Jan 24 04:42:42 2018<br>
@@ -16,7 +16,7 @@<br>
 ;===------------------------------------------------------------------------===;<br>
<br>
 [common]<br>
-subdirectories = Coroutines IPO InstCombine Instrumentation Scalar Utils Vectorize ObjCARC<br>
+subdirectories = AggressiveInstCombine Coroutines IPO InstCombine Instrumentation Scalar Utils Vectorize ObjCARC<br>
<br>
 [component_0]<br>
 type = Group<br>
<br>
Modified: llvm/trunk/lib/Transforms/Scalar/LLVMBuild.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LLVMBuild.txt?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LLVMBuild.txt?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Scalar/LLVMBuild.txt (original)<br>
+++ llvm/trunk/lib/Transforms/Scalar/LLVMBuild.txt Wed Jan 24 04:42:42 2018<br>
@@ -20,4 +20,4 @@ type = Library<br>
 name = Scalar<br>
 parent = Transforms<br>
 library_name = ScalarOpts<br>
-required_libraries = Analysis Core InstCombine Support TransformUtils<br>
+required_libraries = AggressiveInstCombine Analysis Core InstCombine Support TransformUtils<br>
<br>
Modified: llvm/trunk/test/Other/new-pm-defaults.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Other/new-pm-defaults.ll?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Other/new-pm-defaults.ll?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/Other/new-pm-defaults.ll (original)<br>
+++ llvm/trunk/test/Other/new-pm-defaults.ll Wed Jan 24 04:42:42 2018<br>
@@ -126,6 +126,7 @@<br>
 ; CHECK-O-NEXT: Running analysis: LazyValueAnalysis<br>
 ; CHECK-O-NEXT: Running pass: CorrelatedValuePropagationPass<br>
 ; CHECK-O-NEXT: Running pass: SimplifyCFGPass<br>
+; CHECK-O3-NEXT: AggressiveInstCombinePass<br>
 ; CHECK-O-NEXT: Running pass: InstCombinePass<br>
 ; CHECK-O1-NEXT: Running pass: LibCallsShrinkWrapPass<br>
 ; CHECK-O2-NEXT: Running pass: LibCallsShrinkWrapPass<br>
<br>
Modified: llvm/trunk/test/Other/new-pm-lto-defaults.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Other/new-pm-lto-defaults.ll?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Other/new-pm-lto-defaults.ll?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/Other/new-pm-lto-defaults.ll (original)<br>
+++ llvm/trunk/test/Other/new-pm-lto-defaults.ll Wed Jan 24 04:42:42 2018<br>
@@ -10,7 +10,8 @@<br>
 ; RUN:     | FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK-O2<br>
 ; RUN: opt -disable-verify -debug-pass-manager \<br>
 ; RUN:     -passes='lto<O3>' -S  %s 2>&1 \<br>
-; RUN:     | FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK-O2<br>
+; RUN:     | FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK-O2 \<br>
+; RUN:     --check-prefix=CHECK-O3<br>
 ; RUN: opt -disable-verify -debug-pass-manager \<br>
 ; RUN:     -passes='lto<Os>' -S %s 2>&1 \<br>
 ; RUN:     | FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK-O2<br>
@@ -20,7 +21,7 @@<br>
 ; RUN: opt -disable-verify -debug-pass-manager \<br>
 ; RUN:     -passes='lto<O3>' -S  %s -passes-ep-peephole='no-op-function' 2>&1 \<br>
 ; RUN:     | FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK-O2 \<br>
-; RUN:     --check-prefix=CHECK-EP-Peephole<br>
+; RUN:     --check-prefix=CHECK-O3 --check-prefix=CHECK-EP-Peephole<br>
<br>
 ; CHECK-O: Starting llvm::Module pass manager run.<br>
 ; CHECK-O-NEXT: Running pass: PassManager<{{.*}}Module<br>
@@ -60,6 +61,7 @@<br>
 ; CHECK-O2-NEXT: Running pass: DeadArgumentEliminationPass<br>
 ; CHECK-O2-NEXT: Running pass: ModuleToFunctionPassAdaptor<{{.*}}PassManager{{.*}}><br>
 ; CHECK-O2-NEXT: Starting llvm::Function pass manager run.<br>
+; CHECK-O3-NEXT: Running pass: AggressiveInstCombinePass<br>
 ; CHECK-O2-NEXT: Running pass: InstCombinePass<br>
 ; CHECK-EP-Peephole-NEXT: Running pass: NoOpFunctionPass<br>
 ; CHECK-O2-NEXT: Finished llvm::Function pass manager run.<br>
<br>
Modified: llvm/trunk/test/Other/new-pm-thinlto-defaults.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Other/new-pm-thinlto-defaults.ll?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Other/new-pm-thinlto-defaults.ll?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/Other/new-pm-thinlto-defaults.ll (original)<br>
+++ llvm/trunk/test/Other/new-pm-thinlto-defaults.ll Wed Jan 24 04:42:42 2018<br>
@@ -111,6 +111,7 @@<br>
 ; CHECK-O-NEXT: Running analysis: LazyValueAnalysis<br>
 ; CHECK-O-NEXT: Running pass: CorrelatedValuePropagationPass<br>
 ; CHECK-O-NEXT: Running pass: SimplifyCFGPass<br>
+; CHECK-O3-NEXT: Running pass: AggressiveInstCombinePass<br>
 ; CHECK-O-NEXT: Running pass: InstCombinePass<br>
 ; CHECK-O1-NEXT: Running pass: LibCallsShrinkWrapPass<br>
 ; CHECK-O2-NEXT: Running pass: LibCallsShrinkWrapPass<br>
<br>
Propchange: llvm/trunk/test/Transforms/AggressiveInstCombine/<br>
------------------------------------------------------------------------------<br>
    bugtraq:number = true<br>
<br>
Added: llvm/trunk/test/Transforms/AggressiveInstCombine/trunc_multi_uses.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/AggressiveInstCombine/trunc_multi_uses.ll?rev=323321&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/AggressiveInstCombine/trunc_multi_uses.ll?rev=323321&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/AggressiveInstCombine/trunc_multi_uses.ll (added)<br>
+++ llvm/trunk/test/Transforms/AggressiveInstCombine/trunc_multi_uses.ll Wed Jan 24 04:42:42 2018<br>
@@ -0,0 +1,214 @@<br>
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py<br>
+; RUN: opt < %s -aggressive-instcombine -S | FileCheck %s<br>
+; RUN: opt < %s -passes=aggressive-instcombine -S | FileCheck %s<br>
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"<br>
+<br>
+; Aggressive Instcombine should be able to reduce width of these expressions.<br>
+<br>
+declare i32 @use32(i32)<br>
+declare i32 @use64(i64)<br>
+declare <2 x i32> @use32_vec(<2 x i32>)<br>
+declare <2 x i32> @use64_vec(<2 x i64>)<br>
+<br>
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br>
+;; These tests check cases where expression dag post-dominated by TruncInst<br>
+;; contains instruction, which has more than one usage.<br>
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br>
+<br>
+define void @multi_uses_add(i32 %X) {<br>
+; CHECK-LABEL: @multi_uses_add(<br>
+; CHECK-NEXT:    [[A1:%.*]] = zext i32 [[X:%.*]] to i64<br>
+; CHECK-NEXT:    [[B1:%.*]] = add i32 [[X]], 15<br>
+; CHECK-NEXT:    [[C1:%.*]] = mul i32 [[B1]], [[B1]]<br>
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @use32(i32 [[C1]])<br>
+; CHECK-NEXT:    [[TMP2:%.*]] = call i32 @use64(i64 [[A1]])<br>
+; CHECK-NEXT:    ret void<br>
+;<br>
+  %A1 = zext i32 %X to i64<br>
+  %B1 = add i64 %A1, 15<br>
+  %C1 = mul i64 %B1, %B1<br>
+  %T1 = trunc i64 %C1 to i32<br>
+  call i32 @use32(i32 %T1)<br>
+  ; make sure zext have another use that is not post-dominated by the TruncInst.<br>
+  call i32 @use64(i64 %A1)<br>
+  ret void<br>
+}<br>
+<br>
+define void @multi_uses_or(i32 %X) {<br>
+; CHECK-LABEL: @multi_uses_or(<br>
+; CHECK-NEXT:    [[A1:%.*]] = zext i32 [[X:%.*]] to i64<br>
+; CHECK-NEXT:    [[B1:%.*]] = or i32 [[X]], 15<br>
+; CHECK-NEXT:    [[C1:%.*]] = mul i32 [[B1]], [[B1]]<br>
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @use32(i32 [[C1]])<br>
+; CHECK-NEXT:    [[TMP2:%.*]] = call i32 @use64(i64 [[A1]])<br>
+; CHECK-NEXT:    ret void<br>
+;<br>
+  %A1 = zext i32 %X to i64<br>
+  %B1 = or i64 %A1, 15<br>
+  %C1 = mul i64 %B1, %B1<br>
+  %T1 = trunc i64 %C1 to i32<br>
+  call i32 @use32(i32 %T1)<br>
+  ; make sure zext have another use that is not post-dominated by the TruncInst.<br>
+  call i32 @use64(i64 %A1)<br>
+  ret void<br>
+}<br>
+<br>
+define void @multi_uses_xor(i32 %X) {<br>
+; CHECK-LABEL: @multi_uses_xor(<br>
+; CHECK-NEXT:    [[A1:%.*]] = zext i32 [[X:%.*]] to i64<br>
+; CHECK-NEXT:    [[B1:%.*]] = xor i32 [[X]], 15<br>
+; CHECK-NEXT:    [[C1:%.*]] = mul i32 [[B1]], [[B1]]<br>
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @use32(i32 [[C1]])<br>
+; CHECK-NEXT:    [[TMP2:%.*]] = call i32 @use64(i64 [[A1]])<br>
+; CHECK-NEXT:    ret void<br>
+;<br>
+  %A1 = zext i32 %X to i64<br>
+  %B1 = xor i64 %A1, 15<br>
+  %C1 = mul i64 %B1, %B1<br>
+  %T1 = trunc i64 %C1 to i32<br>
+  call i32 @use32(i32 %T1)<br>
+  ; make sure zext have another use that is not post-dominated by the TruncInst.<br>
+  call i32 @use64(i64 %A1)<br>
+  ret void<br>
+}<br>
+<br>
+define void @multi_uses_and(i32 %X) {<br>
+; CHECK-LABEL: @multi_uses_and(<br>
+; CHECK-NEXT:    [[A1:%.*]] = zext i32 [[X:%.*]] to i64<br>
+; CHECK-NEXT:    [[B1:%.*]] = and i32 [[X]], 15<br>
+; CHECK-NEXT:    [[C1:%.*]] = mul i32 [[B1]], [[B1]]<br>
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @use32(i32 [[C1]])<br>
+; CHECK-NEXT:    [[TMP2:%.*]] = call i32 @use64(i64 [[A1]])<br>
+; CHECK-NEXT:    ret void<br>
+;<br>
+  %A1 = zext i32 %X to i64<br>
+  %B1 = and i64 %A1, 15<br>
+  %C1 = mul i64 %B1, %B1<br>
+  %T1 = trunc i64 %C1 to i32<br>
+  call i32 @use32(i32 %T1)<br>
+  ; make sure zext have another use that is not post-dominated by the TruncInst.<br>
+  call i32 @use64(i64 %A1)<br>
+  ret void<br>
+}<br>
+<br>
+define void @multi_uses_sub(i32 %X, i32 %Y) {<br>
+; CHECK-LABEL: @multi_uses_sub(<br>
+; CHECK-NEXT:    [[A1:%.*]] = zext i32 [[X:%.*]] to i64<br>
+; CHECK-NEXT:    [[A2:%.*]] = zext i32 [[Y:%.*]] to i64<br>
+; CHECK-NEXT:    [[B1:%.*]] = sub i32 [[X]], [[Y]]<br>
+; CHECK-NEXT:    [[C1:%.*]] = mul i32 [[B1]], [[B1]]<br>
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @use32(i32 [[C1]])<br>
+; CHECK-NEXT:    [[TMP2:%.*]] = call i32 @use64(i64 [[A1]])<br>
+; CHECK-NEXT:    [[TMP3:%.*]] = call i32 @use64(i64 [[A2]])<br>
+; CHECK-NEXT:    ret void<br>
+;<br>
+  %A1 = zext i32 %X to i64<br>
+  %A2 = zext i32 %Y to i64<br>
+  %B1 = sub i64 %A1, %A2<br>
+  %C1 = mul i64 %B1, %B1<br>
+  %T1 = trunc i64 %C1 to i32<br>
+  call i32 @use32(i32 %T1)<br>
+  ; make sure zext have another use that is not post-dominated by the TruncInst.<br>
+  call i32 @use64(i64 %A1)<br>
+  call i32 @use64(i64 %A2)<br>
+  ret void<br>
+}<br>
+<br>
+define void @multi_use_vec_add(<2 x i32> %X) {<br>
+; CHECK-LABEL: @multi_use_vec_add(<br>
+; CHECK-NEXT:    [[A1:%.*]] = zext <2 x i32> [[X:%.*]] to <2 x i64><br>
+; CHECK-NEXT:    [[B1:%.*]] = add <2 x i32> [[X]], <i32 15, i32 15><br>
+; CHECK-NEXT:    [[C1:%.*]] = mul <2 x i32> [[B1]], [[B1]]<br>
+; CHECK-NEXT:    [[TMP1:%.*]] = call <2 x i32> @use32_vec(<2 x i32> [[C1]])<br>
+; CHECK-NEXT:    [[TMP2:%.*]] = call <2 x i32> @use64_vec(<2 x i64> [[A1]])<br>
+; CHECK-NEXT:    ret void<br>
+;<br>
+  %A1 = zext <2 x i32> %X to <2 x i64><br>
+  %B1 = add <2 x i64> %A1, <i64 15, i64 15><br>
+  %C1 = mul <2 x i64> %B1, %B1<br>
+  %T1 = trunc <2 x i64> %C1 to <2 x i32><br>
+  call <2 x i32> @use32_vec(<2 x i32> %T1)<br>
+  ; make sure zext have another use that is not post-dominated by the TruncInst.<br>
+  call <2 x i32> @use64_vec(<2 x i64> %A1)<br>
+  ret void<br>
+}<br>
+<br>
+define void @multi_use_vec_or(<2 x i32> %X) {<br>
+; CHECK-LABEL: @multi_use_vec_or(<br>
+; CHECK-NEXT:    [[A1:%.*]] = zext <2 x i32> [[X:%.*]] to <2 x i64><br>
+; CHECK-NEXT:    [[B1:%.*]] = or <2 x i32> [[X]], <i32 15, i32 15><br>
+; CHECK-NEXT:    [[C1:%.*]] = mul <2 x i32> [[B1]], [[B1]]<br>
+; CHECK-NEXT:    [[TMP1:%.*]] = call <2 x i32> @use32_vec(<2 x i32> [[C1]])<br>
+; CHECK-NEXT:    [[TMP2:%.*]] = call <2 x i32> @use64_vec(<2 x i64> [[A1]])<br>
+; CHECK-NEXT:    ret void<br>
+;<br>
+  %A1 = zext <2 x i32> %X to <2 x i64><br>
+  %B1 = or <2 x i64> %A1, <i64 15, i64 15><br>
+  %C1 = mul <2 x i64> %B1, %B1<br>
+  %T1 = trunc <2 x i64> %C1 to <2 x i32><br>
+  call <2 x i32> @use32_vec(<2 x i32> %T1)<br>
+  ; make sure zext have another use that is not post-dominated by the TruncInst.<br>
+  call <2 x i32> @use64_vec(<2 x i64> %A1)<br>
+  ret void<br>
+}<br>
+<br>
+define void @multi_use_vec_xor(<2 x i32> %X) {<br>
+; CHECK-LABEL: @multi_use_vec_xor(<br>
+; CHECK-NEXT:    [[A1:%.*]] = zext <2 x i32> [[X:%.*]] to <2 x i64><br>
+; CHECK-NEXT:    [[B1:%.*]] = xor <2 x i32> [[X]], <i32 15, i32 15><br>
+; CHECK-NEXT:    [[C1:%.*]] = mul <2 x i32> [[B1]], [[B1]]<br>
+; CHECK-NEXT:    [[TMP1:%.*]] = call <2 x i32> @use32_vec(<2 x i32> [[C1]])<br>
+; CHECK-NEXT:    [[TMP2:%.*]] = call <2 x i32> @use64_vec(<2 x i64> [[A1]])<br>
+; CHECK-NEXT:    ret void<br>
+;<br>
+  %A1 = zext <2 x i32> %X to <2 x i64><br>
+  %B1 = xor <2 x i64> %A1, <i64 15, i64 15><br>
+  %C1 = mul <2 x i64> %B1, %B1<br>
+  %T1 = trunc <2 x i64> %C1 to <2 x i32><br>
+  call <2 x i32> @use32_vec(<2 x i32> %T1)<br>
+  ; make sure zext have another use that is not post-dominated by the TruncInst.<br>
+  call <2 x i32> @use64_vec(<2 x i64> %A1)<br>
+  ret void<br>
+}<br>
+<br>
+define void @multi_use_vec_and(<2 x i32> %X) {<br>
+; CHECK-LABEL: @multi_use_vec_and(<br>
+; CHECK-NEXT:    [[A1:%.*]] = zext <2 x i32> [[X:%.*]] to <2 x i64><br>
+; CHECK-NEXT:    [[B1:%.*]] = and <2 x i32> [[X]], <i32 15, i32 15><br>
+; CHECK-NEXT:    [[C1:%.*]] = mul <2 x i32> [[B1]], [[B1]]<br>
+; CHECK-NEXT:    [[TMP1:%.*]] = call <2 x i32> @use32_vec(<2 x i32> [[C1]])<br>
+; CHECK-NEXT:    [[TMP2:%.*]] = call <2 x i32> @use64_vec(<2 x i64> [[A1]])<br>
+; CHECK-NEXT:    ret void<br>
+;<br>
+  %A1 = zext <2 x i32> %X to <2 x i64><br>
+  %B1 = and <2 x i64> %A1, <i64 15, i64 15><br>
+  %C1 = mul <2 x i64> %B1, %B1<br>
+  %T1 = trunc <2 x i64> %C1 to <2 x i32><br>
+  call <2 x i32> @use32_vec(<2 x i32> %T1)<br>
+  ; make sure zext have another use that is not post-dominated by the TruncInst.<br>
+  call <2 x i32> @use64_vec(<2 x i64> %A1)<br>
+  ret void<br>
+}<br>
+<br>
+define void @multi_use_vec_sub(<2 x i32> %X, <2 x i32> %Y) {<br>
+; CHECK-LABEL: @multi_use_vec_sub(<br>
+; CHECK-NEXT:    [[A1:%.*]] = zext <2 x i32> [[X:%.*]] to <2 x i64><br>
+; CHECK-NEXT:    [[A2:%.*]] = zext <2 x i32> [[Y:%.*]] to <2 x i64><br>
+; CHECK-NEXT:    [[B1:%.*]] = sub <2 x i32> [[X]], [[Y]]<br>
+; CHECK-NEXT:    [[C1:%.*]] = mul <2 x i32> [[B1]], [[B1]]<br>
+; CHECK-NEXT:    [[TMP1:%.*]] = call <2 x i32> @use32_vec(<2 x i32> [[C1]])<br>
+; CHECK-NEXT:    [[TMP2:%.*]] = call <2 x i32> @use64_vec(<2 x i64> [[A1]])<br>
+; CHECK-NEXT:    [[TMP3:%.*]] = call <2 x i32> @use64_vec(<2 x i64> [[A2]])<br>
+; CHECK-NEXT:    ret void<br>
+;<br>
+  %A1 = zext <2 x i32> %X to <2 x i64><br>
+  %A2 = zext <2 x i32> %Y to <2 x i64><br>
+  %B1 = sub <2 x i64> %A1, %A2<br>
+  %C1 = mul <2 x i64> %B1, %B1<br>
+  %T1 = trunc <2 x i64> %C1 to <2 x i32><br>
+  call <2 x i32> @use32_vec(<2 x i32> %T1)<br>
+  ; make sure zext have another use that is not post-dominated by the TruncInst.<br>
+  call <2 x i32> @use64_vec(<2 x i64> %A1)<br>
+  call <2 x i32> @use64_vec(<2 x i64> %A2)<br>
+  ret void<br>
+}<br>
<br>
Modified: llvm/trunk/tools/opt/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/CMakeLists.txt?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/CMakeLists.txt?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/tools/opt/CMakeLists.txt (original)<br>
+++ llvm/trunk/tools/opt/CMakeLists.txt Wed Jan 24 04:42:42 2018<br>
@@ -1,5 +1,6 @@<br>
 set(LLVM_LINK_COMPONENTS<br>
   ${LLVM_TARGETS_TO_BUILD}<br>
+  AggressiveInstCombine<br>
   Analysis<br>
   BitWriter<br>
   CodeGen<br>
<br>
Modified: llvm/trunk/tools/opt/opt.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/opt.cpp?rev=323321&r1=323320&r2=323321&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/opt.cpp?rev=323321&r1=323320&r2=323321&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/tools/opt/opt.cpp (original)<br>
+++ llvm/trunk/tools/opt/opt.cpp Wed Jan 24 04:42:42 2018<br>
@@ -395,6 +395,7 @@ int main(int argc, char **argv) {<br>
   initializeAnalysis(Registry);<br>
   initializeTransformUtils(Registry);<br>
   initializeInstCombine(Registry);<br>
+  initializeAggressiveInstCombinerLegacyPassPass(Registry);<br>
   initializeInstrumentation(Registry);<br>
   initializeTarget(Registry);<br>
   // For codegen passes, only passes that do IR to IR transformation are<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div></div>