Quellcode durchsuchen

Minor refactoring to reduce size of dxilconv.dll (#3240)

Move llvm::findScalarElement and LoopSimplifyID into separate files
Remove unused code from DxbcConverter

Reduces dxilconv.dll size from 1,225kB to 1,019kB (-17%).
Measured on x86 release build with compiler optimization for size.
Helena Kotas vor 4 Jahren
Ursprung
Commit
2ac188b4ce

+ 34 - 0
include/llvm/Transforms/Utils/LoopSimplify.h

@@ -0,0 +1,34 @@
+//===- LoopSimplify.h - Loop Canonicalization Pass ----------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// See LoopSimplify.cpp for description of the pass.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Transforms/Scalar.h"
+using namespace llvm;
+
+struct LoopSimplify : public FunctionPass {
+  static char ID; // Pass identification, replacement for typeid
+  LoopSimplify();
+
+  // AA - If we have an alias analysis object to update, this is it, otherwise
+  // this is null.
+  AliasAnalysis* AA;
+  DominatorTree* DT;
+  LoopInfo* LI;
+  ScalarEvolution* SE;
+  AssumptionCache* AC;
+
+  bool runOnFunction(llvm::Function& F) override;
+  void getAnalysisUsage(llvm::AnalysisUsage& AU) const override;
+
+  /// verifyAnalysis() - Verify LoopSimplifyForm's guarantees.
+  void verifyAnalysis() const override;
+};

+ 1 - 0
lib/Analysis/CMakeLists.txt

@@ -70,6 +70,7 @@ add_llvm_library(LLVMAnalysis
   ScopedNoAliasAA.cpp
   ValueTracking.cpp
   VectorUtils.cpp
+  VectorUtils2.cpp
 
   ADDITIONAL_HEADER_DIRS
   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Analysis

+ 0 - 53
lib/Analysis/VectorUtils.cpp

@@ -357,56 +357,3 @@ llvm::Value *llvm::getStrideFromPointer(llvm::Value *Ptr, ScalarEvolution *SE,
 
   return Stride;
 }
-
-/// \brief Given a vector and an element number, see if the scalar value is
-/// already around as a register, for example if it were inserted then extracted
-/// from the vector.
-llvm::Value *llvm::findScalarElement(llvm::Value *V, unsigned EltNo) {
-  assert(V->getType()->isVectorTy() && "Not looking at a vector?");
-  VectorType *VTy = cast<VectorType>(V->getType());
-  unsigned Width = VTy->getNumElements();
-  if (EltNo >= Width)  // Out of range access.
-    return UndefValue::get(VTy->getElementType());
-
-  if (Constant *C = dyn_cast<Constant>(V))
-    return C->getAggregateElement(EltNo);
-
-  if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
-    // If this is an insert to a variable element, we don't know what it is.
-    if (!isa<ConstantInt>(III->getOperand(2)))
-      return nullptr;
-    unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
-
-    // If this is an insert to the element we are looking for, return the
-    // inserted value.
-    if (EltNo == IIElt)
-      return III->getOperand(1);
-
-    // Otherwise, the insertelement doesn't modify the value, recurse on its
-    // vector input.
-    return findScalarElement(III->getOperand(0), EltNo);
-  }
-
-  if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
-    unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements();
-    int InEl = SVI->getMaskValue(EltNo);
-    if (InEl < 0)
-      return UndefValue::get(VTy->getElementType());
-    if (InEl < (int)LHSWidth)
-      return findScalarElement(SVI->getOperand(0), InEl);
-    return findScalarElement(SVI->getOperand(1), InEl - LHSWidth);
-  }
-
-  // Extract a value from a vector add operation with a constant zero.
-  Value *Val = nullptr; Constant *Con = nullptr;
-  if (match(V,
-            llvm::PatternMatch::m_Add(llvm::PatternMatch::m_Value(Val),
-                                      llvm::PatternMatch::m_Constant(Con)))) {
-    if (Constant *Elt = Con->getAggregateElement(EltNo))
-      if (Elt->isNullValue())
-        return findScalarElement(Val, EltNo);
-  }
-
-  // Otherwise, we don't know.
-  return nullptr;
-}

+ 71 - 0
lib/Analysis/VectorUtils2.cpp

@@ -0,0 +1,71 @@
+//===----------- VectorUtils2.cpp - findScalarElement function -----------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines vectorizer utility function findScalarElement.
+// Splitting this function from VectorUtils.cpp into a separate file 
+// makes dxilconv.dll 121kB smaller (x86 release, compiler optimization for size).
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Analysis/VectorUtils.h"
+#include "llvm/IR/PatternMatch.h"
+#include "llvm/IR/Value.h"
+
+/// \brief Given a vector and an element number, see if the scalar value is
+/// already around as a register, for example if it were inserted then extracted
+/// from the vector.
+llvm::Value *llvm::findScalarElement(llvm::Value *V, unsigned EltNo) {
+  assert(V->getType()->isVectorTy() && "Not looking at a vector?");
+  VectorType *VTy = cast<VectorType>(V->getType());
+  unsigned Width = VTy->getNumElements();
+  if (EltNo >= Width)  // Out of range access.
+    return UndefValue::get(VTy->getElementType());
+
+  if (Constant *C = dyn_cast<Constant>(V))
+    return C->getAggregateElement(EltNo);
+
+  if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
+    // If this is an insert to a variable element, we don't know what it is.
+    if (!isa<ConstantInt>(III->getOperand(2)))
+      return nullptr;
+    unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
+
+    // If this is an insert to the element we are looking for, return the
+    // inserted value.
+    if (EltNo == IIElt)
+      return III->getOperand(1);
+
+    // Otherwise, the insertelement doesn't modify the value, recurse on its
+    // vector input.
+    return findScalarElement(III->getOperand(0), EltNo);
+  }
+
+  if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
+    unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements();
+    int InEl = SVI->getMaskValue(EltNo);
+    if (InEl < 0)
+      return UndefValue::get(VTy->getElementType());
+    if (InEl < (int)LHSWidth)
+      return findScalarElement(SVI->getOperand(0), InEl);
+    return findScalarElement(SVI->getOperand(1), InEl - LHSWidth);
+  }
+
+  // Extract a value from a vector add operation with a constant zero.
+  Value *Val = nullptr; Constant *Con = nullptr;
+  if (match(V,
+            llvm::PatternMatch::m_Add(llvm::PatternMatch::m_Value(Val),
+                                      llvm::PatternMatch::m_Constant(Con)))) {
+    if (Constant *Elt = Con->getAggregateElement(EltNo))
+      if (Elt->isNullValue())
+        return findScalarElement(Val, EltNo);
+  }
+
+  // Otherwise, we don't know.
+  return nullptr;
+}

+ 1 - 0
lib/Transforms/Utils/CMakeLists.txt

@@ -19,6 +19,7 @@ add_llvm_library(LLVMTransformUtils
   LCSSA.cpp
   Local.cpp
   LoopSimplify.cpp
+  LoopSimplifyId.cpp
   LoopUnroll.cpp
   LoopUnrollRuntime.cpp
   LoopUtils.cpp

+ 21 - 40
lib/Transforms/Utils/LoopSimplify.cpp

@@ -64,6 +64,7 @@
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Transforms/Utils/LoopUtils.h"
+#include "llvm/Transforms/Utils/LoopSimplify.h"
 using namespace llvm;
 
 #define DEBUG_TYPE "loop-simplify"
@@ -742,45 +743,6 @@ bool llvm::simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, Pass *PP,
   return Changed;
 }
 
-namespace {
-  struct LoopSimplify : public FunctionPass {
-    static char ID; // Pass identification, replacement for typeid
-    LoopSimplify() : FunctionPass(ID) {
-      initializeLoopSimplifyPass(*PassRegistry::getPassRegistry());
-    }
-
-    // AA - If we have an alias analysis object to update, this is it, otherwise
-    // this is null.
-    AliasAnalysis *AA;
-    DominatorTree *DT;
-    LoopInfo *LI;
-    ScalarEvolution *SE;
-    AssumptionCache *AC;
-
-    bool runOnFunction(Function &F) override;
-
-    void getAnalysisUsage(AnalysisUsage &AU) const override {
-      AU.addRequired<AssumptionCacheTracker>();
-
-      // We need loop information to identify the loops...
-      AU.addRequired<DominatorTreeWrapperPass>();
-      AU.addPreserved<DominatorTreeWrapperPass>();
-
-      AU.addRequired<LoopInfoWrapperPass>();
-      AU.addPreserved<LoopInfoWrapperPass>();
-
-      AU.addPreserved<AliasAnalysis>();
-      AU.addPreserved<ScalarEvolution>();
-      AU.addPreserved<DependenceAnalysis>();
-      AU.addPreservedID(BreakCriticalEdgesID);  // No critical edges added.
-    }
-
-    /// verifyAnalysis() - Verify LoopSimplifyForm's guarantees.
-    void verifyAnalysis() const override;
-  };
-}
-
-char LoopSimplify::ID = 0;
 INITIALIZE_PASS_BEGIN(LoopSimplify, "loop-simplify",
                 "Canonicalize natural loops", false, false)
 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
@@ -790,9 +752,28 @@ INITIALIZE_PASS_END(LoopSimplify, "loop-simplify",
                 "Canonicalize natural loops", false, false)
 
 // Publicly exposed interface to pass...
-char &llvm::LoopSimplifyID = LoopSimplify::ID;
 Pass *llvm::createLoopSimplifyPass() { return new LoopSimplify(); }
 
+LoopSimplify::LoopSimplify() : FunctionPass(ID) {
+  initializeLoopSimplifyPass(*PassRegistry::getPassRegistry());
+}
+
+void LoopSimplify::getAnalysisUsage(AnalysisUsage& AU) const {
+  AU.addRequired<AssumptionCacheTracker>();
+
+  // We need loop information to identify the loops...
+  AU.addRequired<DominatorTreeWrapperPass>();
+  AU.addPreserved<DominatorTreeWrapperPass>();
+
+  AU.addRequired<LoopInfoWrapperPass>();
+  AU.addPreserved<LoopInfoWrapperPass>();
+
+  AU.addPreserved<AliasAnalysis>();
+  AU.addPreserved<ScalarEvolution>();
+  AU.addPreserved<DependenceAnalysis>();
+  AU.addPreservedID(BreakCriticalEdgesID);  // No critical edges added.
+}
+
 /// runOnFunction - Run down all loops in the CFG (recursively, but we could do
 /// it in any convenient order) inserting preheaders...
 ///

+ 24 - 0
lib/Transforms/Utils/LoopSimplifyId.cpp

@@ -0,0 +1,24 @@
+//===- LoopSimplifyId.cpp - ID for the Loop Canonicalization Pass ----------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+
+#include "llvm/Pass.h"
+#include "llvm/Analysis/AliasAnalysis.h"
+#include "llvm/Analysis/LoopInfo.h"
+#include "llvm/Analysis/ScalarEvolution.h"
+#include "llvm/Transforms/Utils/LoopSimplify.h"
+
+using namespace llvm;
+
+char LoopSimplify::ID = 0;
+
+// Publicly exposed interface to pass...
+// This is in a separate file instead of LoopSimplify.cpp which brings in many dependencies
+// unnecessary increasing the size of dxilconv.dll.
+char &llvm::LoopSimplifyID = LoopSimplify::ID;

+ 0 - 7
projects/dxilconv/lib/DxbcConverter/DxbcConverter.cpp

@@ -7119,13 +7119,6 @@ void DxbcConverter::Optimize() {
 #endif
 }
 
-void DxbcConverter::AddOptimizationPasses(PassManagerBase &PassManager, unsigned OptLevel) {
-  PassManagerBuilder Builder;
-  Builder.OptLevel = OptLevel;
-  Builder.SizeLevel = 0;
-  Builder.populateModulePassManager(PassManager);
-}
-
 void DxbcConverter::CreateBranchIfNeeded(BasicBlock *pBB, BasicBlock *pTargetBB) {
   bool bNeedBranch = true;
   if (!pBB->empty()) {

+ 0 - 1
projects/dxilconv/lib/DxbcConverter/DxbcConverterImpl.h

@@ -602,7 +602,6 @@ protected:
   Value *CreateHandle(DxilResourceBase::Class Class, unsigned RangeID, Value *pIndex, bool bNonUniformIndex);
 
   void Optimize();
-  void AddOptimizationPasses(PassManagerBase &PassManager, unsigned OptLevel);
 
   void CheckDxbcString(const char *pStr, const void *pMaxPtrInclusive);
 

+ 0 - 1
projects/dxilconv/tools/dxilconv/CMakeLists.txt

@@ -14,7 +14,6 @@ target_link_libraries(dxilconv PRIVATE
   LLVMDxilContainer
   LLVMDxilRootSignature
   LLVMHLSL  
-  LLVMipo
   LLVMMSSupport
   LLVMScalarOpts
 )