[PATCH] InstCombine: extract instead of shuffle when performing vector/array type punning

JF Bastien jfb at chromium.org
Wed Feb 18 16:41:40 PST 2015


================
Comment at: lib/Transforms/InstCombine/InstCombineVectorOps.cpp:1009-1027
@@ +1008,21 @@
+      VectorType *CastSrcTy = VectorType::get(TgtTy, TgtNumElems);
+      if (!BegIsAligned) {
+        // Shuffle the input so [0,NumElements) contains the output, and
+        // [NumElems,SrcNumElems) is undef.
+        Constant *Undef = ConstantInt::get(Int32Ty, SrcNumElems);
+        SmallVector<Constant *, 16> ShuffleMask(SrcNumElems, Undef);
+        for (unsigned I = 0, E = MaskElems, Idx = BegIdx; I != E; ++Idx, ++I)
+          ShuffleMask[I] = ConstantInt::get(Int32Ty, Idx);
+        LHS = new ShuffleVectorInst(LHS, UndefValue::get(LHS->getType()),
+                                    ConstantVector::get(ShuffleMask),
+                                    SVI.getName() + ".extract", BC);
+        BegIdx = 0;
+      }
+      unsigned SrcElemsPerTgtElem = TgtElemBitWidth / SrcElemBitWidth;
+      assert(SrcElemsPerTgtElem);
+      BegIdx /= SrcElemsPerTgtElem;
+      Instruction *Ext = ExtractElementInst::Create(
+          CastInst::Create(Instruction::BitCast, LHS, CastSrcTy,
+                           SVI.getName() + ".bc", BC),
+          ConstantInt::get(Int32Ty, BegIdx), SVI.getName() + ".extract", BC);
+      // The shufflevector isn't being replace: the bitcast that used it
----------------
chandlerc wrote:
> Please use the InstCombine IR builder throughout. That will ensure these new instructions are visited.
> 
> Also, please create a single extract element instruction and replace all the bitcasts with it.
I'm not sure I did exactly what you wanted: we have to handle the case where multiple bitcasts to different types of the same size are handled. For now I did this by creating one bitcast per Type, but I could do it with a single bitcast+extract followed by a bitcast of the scalar. The later seems suboptimal, and the example is kind of silly anyways so as long as it's correct I guess "meh".

http://reviews.llvm.org/D7734

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/






More information about the llvm-commits mailing list