[llvm-commits] [PATCH] Instcombine : extractelement(cast) -> cast(extractelement)

Rotem, Nadav nadav.rotem at intel.com
Wed Mar 30 04:06:15 PDT 2011


Hi,

Please review this patch to the InstCombine pass. In this patch I optimize extractelements which uses the value of cast instructions. I attempt to switch the order of the extract and the cast. I noticed a major performance boost on my workloads.

Nadav

Index: ../llvm/test/Transforms/InstCombine/ExtractCast.ll
===================================================================
--- ../llvm/test/Transforms/InstCombine/ExtractCast.ll  (revision 0)
+++ ../llvm/test/Transforms/InstCombine/ExtractCast.ll  (revision 0)
@@ -0,0 +1,27 @@
+; RUN: opt < %s -instcombine -S -o - | FileCheck %s
+
+; CHECK: @a
+define i32 @a(<4 x i64> %I) {
+entry:
+; CHECK-NOT: trunc <4 x i64>
+        %J = trunc <4 x i64> %I to <4 x i32>
+        %K = extractelement <4 x i32> %J, i32 3
+; CHECK: extractelement <4 x i64>
+; CHECK: trunc i64
+; CHECK: ret
+        ret i32 %K
+}
+
+
+; CHECK: @b
+define i32 @b(<4 x float> %I) {
+entry:
+; CHECK-NOT: fptosi <4 x float>
+        %J = fptosi <4 x float> %I to <4 x i32>
+        %K = extractelement <4 x i32> %J, i32 3
+; CHECK: extractelement <4 x float>
+; CHECK: fptosi float
+; CHECK: ret
+        ret i32 %K
+}
+

Index: ../llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
===================================================================
--- ../llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp (revision 128529)
+++ ../llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp (working copy)
@@ -230,8 +230,17 @@
                                           ConstantInt::get(Int32Ty,
                                                            SrcIdx, false));
       }
+    } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
+      // Canonicalize extractelement(cast) -> cast(extractelement)
+      // Ignore bitcasts, because they change the number of vector elements
+      if (CI->hasOneUse() && EI.hasOneUse() &&
+          (CI->getOpcode() != Instruction::BitCast)) {
+        Value *EE = Builder->CreateExtractElement(CI->getOperand(0),
+                                                  EI.getIndexOperand());
+        return CastInst::Create(CI->getOpcode(), cast<Instruction>(EE),
+                                EI.getType());
+      }
     }
-    // FIXME: Canonicalize extractelement(bitcast) -> bitcast(extractelement)
   }
   return 0;
}
---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20110330/c8a1d910/attachment.html>


More information about the llvm-commits mailing list