[PATCH] InstCombine: extract instead of shuffle when performing vector/array type punning
Chandler Carruth
chandlerc at gmail.com
Wed Feb 18 14:52:25 PST 2015
================
Comment at: lib/Transforms/InstCombine/InstCombineVectorOps.cpp:862
@@ +861,3 @@
+// +--+--+--+--+
+static bool ShuffleIsExtractingFromLHS(ShuffleVectorInst &SVI,
+ SmallVector<int, 16> &Mask) {
----------------
should be 'isShuffle...'
================
Comment at: lib/Transforms/InstCombine/InstCombineVectorOps.cpp:963-965
@@ +962,5 @@
+ //
+ // If the shuffle is extracting contiguous range of values from the input
+ // vector then each use which is a bitcast of the extracted size can be
+ // replaced. This will work if the vector types are compatible, and the begin
+ // index is aligned to a value in the casted vector type. If the begin index
----------------
The first sentence here doesn't read cleanly. It's important to emphasize that we only do this when the extracted sub-vector is bitcast to an non-vector type that we could extract.
================
Comment at: lib/Transforms/InstCombine/InstCombineVectorOps.cpp:973-980
@@ +972,10 @@
+ //
+ // Example of <16 x i8>, target type i32:
+ // Index range [4,8): v-----------v Will work.
+ // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
+ // <16 x i8>: | | | | | | | | | | | | | | | | |
+ // <4 x i32>: | | | | |
+ // +-----------+-----------+-----------+-----------+
+ // Index range [6,10): ^-----------^ Needs an extra shuffle.
+ // Target type i40: ^--------------^ Won't work, bail.
+ if (ShuffleIsExtractingFromLHS(SVI, Mask)) {
----------------
Fabulous ascii art! Thanks!
================
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
----------------
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.
http://reviews.llvm.org/D7734
EMAIL PREFERENCES
http://reviews.llvm.org/settings/panel/emailpreferences/
More information about the llvm-commits
mailing list