[llvm-commits] [llvm] r171871 - /llvm/trunk/lib/Transforms/Vectorize/LoopVectorize.cpp

Nadav Rotem nrotem at apple.com
Tue Jan 8 09:37:45 PST 2013


Author: nadav
Date: Tue Jan  8 11:37:45 2013
New Revision: 171871

URL: http://llvm.org/viewvc/llvm-project?rev=171871&view=rev
Log:
Code cleanup: refactor the switch statements in the generation of reduction variables into an IR builder call.

Modified:
    llvm/trunk/lib/Transforms/Vectorize/LoopVectorize.cpp

Modified: llvm/trunk/lib/Transforms/Vectorize/LoopVectorize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Vectorize/LoopVectorize.cpp?rev=171871&r1=171870&r2=171871&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Vectorize/LoopVectorize.cpp (original)
+++ llvm/trunk/lib/Transforms/Vectorize/LoopVectorize.cpp Tue Jan  8 11:37:45 2013
@@ -1263,6 +1263,29 @@
   return false;
 }
 
+/// This function translates the reduction kind to an LLVM binary operator.
+static Instruction::BinaryOps
+getReductionBinOp(LoopVectorizationLegality::ReductionKind Kind) {
+  switch (Kind) {
+    case LoopVectorizationLegality::RK_IntegerAdd:
+      return Instruction::Add;
+    case LoopVectorizationLegality::RK_IntegerMult:
+      return Instruction::Mul;
+    case LoopVectorizationLegality::RK_IntegerOr:
+      return Instruction::Or;
+    case LoopVectorizationLegality::RK_IntegerAnd:
+      return Instruction::And;
+    case LoopVectorizationLegality::RK_IntegerXor:
+      return Instruction::Xor;
+    case LoopVectorizationLegality::RK_FloatMult:
+      return Instruction::FMul;
+    case LoopVectorizationLegality::RK_FloatAdd:
+      return Instruction::FAdd;
+    default:
+      llvm_unreachable("Unknown reduction operation");
+  }
+}
+
 void
 InnerLoopVectorizer::vectorizeLoop(LoopVectorizationLegality *Legal) {
   //===------------------------------------------------===//
@@ -1376,40 +1399,10 @@
     // Reduce all of the unrolled parts into a single vector.
     Value *ReducedPartRdx = RdxParts[0];
     for (unsigned part = 1; part < UF; ++part) {
-      switch (RdxDesc.Kind) {
-      case LoopVectorizationLegality::RK_IntegerAdd:
-        ReducedPartRdx = 
-          Builder.CreateAdd(RdxParts[part], ReducedPartRdx, "add.rdx");
-        break;
-      case LoopVectorizationLegality::RK_IntegerMult:
-        ReducedPartRdx =
-          Builder.CreateMul(RdxParts[part], ReducedPartRdx, "mul.rdx");
-        break;
-      case LoopVectorizationLegality::RK_IntegerOr:
-        ReducedPartRdx =
-          Builder.CreateOr(RdxParts[part], ReducedPartRdx, "or.rdx");
-        break;
-      case LoopVectorizationLegality::RK_IntegerAnd:
-        ReducedPartRdx =
-          Builder.CreateAnd(RdxParts[part], ReducedPartRdx, "and.rdx");
-        break;
-      case LoopVectorizationLegality::RK_IntegerXor:
-        ReducedPartRdx =
-          Builder.CreateXor(RdxParts[part], ReducedPartRdx, "xor.rdx");
-        break;
-      case LoopVectorizationLegality::RK_FloatMult:
-        ReducedPartRdx =
-          Builder.CreateFMul(RdxParts[part], ReducedPartRdx, "fmul.rdx");
-        break;
-      case LoopVectorizationLegality::RK_FloatAdd:
-        ReducedPartRdx =
-          Builder.CreateFAdd(RdxParts[part], ReducedPartRdx, "fadd.rdx");
-        break;
-      default:
-        llvm_unreachable("Unknown reduction operation");
-      }
+      Instruction::BinaryOps Op = getReductionBinOp(RdxDesc.Kind);
+      ReducedPartRdx = Builder.CreateBinOp(Op, RdxParts[part], ReducedPartRdx,
+                                           "bin.rdx");
     }
-    
 
     // VF is a power of 2 so we can emit the reduction using log2(VF) shuffles
     // and vector ops, reducing the set of values being computed by half each
@@ -1433,32 +1426,8 @@
                                     ConstantVector::get(ShuffleMask),
                                     "rdx.shuf");
 
-      // Emit the operation on the shuffled value.
-      switch (RdxDesc.Kind) {
-      case LoopVectorizationLegality::RK_IntegerAdd:
-        TmpVec = Builder.CreateAdd(TmpVec, Shuf, "add.rdx");
-        break;
-      case LoopVectorizationLegality::RK_IntegerMult:
-        TmpVec = Builder.CreateMul(TmpVec, Shuf, "mul.rdx");
-        break;
-      case LoopVectorizationLegality::RK_IntegerOr:
-        TmpVec = Builder.CreateOr(TmpVec, Shuf, "or.rdx");
-        break;
-      case LoopVectorizationLegality::RK_IntegerAnd:
-        TmpVec = Builder.CreateAnd(TmpVec, Shuf, "and.rdx");
-        break;
-      case LoopVectorizationLegality::RK_IntegerXor:
-        TmpVec = Builder.CreateXor(TmpVec, Shuf, "xor.rdx");
-        break;
-      case LoopVectorizationLegality::RK_FloatMult:
-        TmpVec = Builder.CreateFMul(TmpVec, Shuf, "fmul.rdx");
-        break;
-      case LoopVectorizationLegality::RK_FloatAdd:
-        TmpVec = Builder.CreateFAdd(TmpVec, Shuf, "fadd.rdx");
-        break;
-      default:
-        llvm_unreachable("Unknown reduction operation");
-      }
+      Instruction::BinaryOps Op = getReductionBinOp(RdxDesc.Kind);
+      TmpVec = Builder.CreateBinOp(Op, TmpVec, Shuf, "bin.rdx");
     }
 
     // The result is in the first element of the vector.





More information about the llvm-commits mailing list