From ec0b9c874526dc0ddf5556f9d88e8868df7ef1a1 Mon Sep 17 00:00:00 2001 From: James Molloy Date: Mon, 9 May 2016 14:32:30 +0000 Subject: [PATCH] [VectorUtils] Query number of sign bits to allow more truncations When deciding if a vector calculation can be done in a smaller bitwidth, use sign bit information from ValueTracking to add more information and allow more truncations. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268921 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/VectorUtils.cpp | 18 +++++++--- .../AArch64/loop-vectorization-factors.ll | 36 +++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/Analysis/VectorUtils.cpp b/lib/Analysis/VectorUtils.cpp index 23a0de856bc..2c03f1a05ce 100644 --- a/lib/Analysis/VectorUtils.cpp +++ b/lib/Analysis/VectorUtils.cpp @@ -320,6 +320,9 @@ llvm::computeMinimumValueSizes(ArrayRef Blocks, DemandedBits &DB, SmallPtrSet InstructionSet; MapVector MinBWs; + assert(Blocks.size() > 0 && "Must have at least one block!"); + const DataLayout &DL = Blocks[0]->getModule()->getDataLayout(); + // Determine the roots. We work bottom-up, from truncs or icmps. bool SeenExtFromIllegalType = false; for (auto *BB : Blocks) @@ -363,12 +366,19 @@ llvm::computeMinimumValueSizes(ArrayRef Blocks, DemandedBits &DB, // If we encounter a type that is larger than 64 bits, we can't represent // it so bail out. - if (DB.getDemandedBits(I).getBitWidth() > 64) + APInt NeededBits = DB.getDemandedBits(I); + unsigned BW = NeededBits.getBitWidth(); + if (BW > 64) return MapVector(); - uint64_t V = DB.getDemandedBits(I).getZExtValue(); - DBits[Leader] |= V; - DBits[I] = V; + auto NSB = ComputeNumSignBits(I, DL); + + // Query demanded bits for the bits required by the instruction. Remove + // any bits that are equal to the sign bit, because we can truncate the + // instruction without changing their value. + NeededBits &= APInt::getLowBitsSet(BW, BW - NSB); + DBits[Leader] |= NeededBits.getZExtValue(); + DBits[I] |= NeededBits.getZExtValue(); // Casts, loads and instructions outside of our range terminate a chain // successfully. diff --git a/test/Transforms/LoopVectorize/AArch64/loop-vectorization-factors.ll b/test/Transforms/LoopVectorize/AArch64/loop-vectorization-factors.ll index c7ced757581..729592d6f81 100644 --- a/test/Transforms/LoopVectorize/AArch64/loop-vectorization-factors.ll +++ b/test/Transforms/LoopVectorize/AArch64/loop-vectorization-factors.ll @@ -263,5 +263,41 @@ for.body: ; preds = %entry, %for.body br i1 %exitcond, label %for.cond.cleanup, label %for.body } +; CHECK-LABEL: @add_g +; CHECK: load <16 x i8> +; CHECK: xor <16 x i8> +; CHECK: icmp ult <16 x i8> +; CHECK: select <16 x i1> {{.*}}, <16 x i8> +; CHECK: store <16 x i8> +define void @add_g(i8* noalias nocapture readonly %p, i8* noalias nocapture readonly %q, i8* noalias nocapture +%r, i8 %arg1, i32 %len) #0 { + %1 = icmp sgt i32 %len, 0 + br i1 %1, label %.lr.ph, label %._crit_edge + +.lr.ph: ; preds = %0 + %2 = sext i8 %arg1 to i64 + br label %3 + +._crit_edge: ; preds = %3, %0 + ret void + +;