From 7ddcd35d6beb99118f0b960329b304f9e9a2bf58 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Mon, 26 Mar 2012 01:44:11 +0000 Subject: [PATCH] Use the new range metadata in computeMaskedBits and add a new optimization to instruction simplify that lets us remove an and when loding a boolean value. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@153423 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/InstructionSimplify.cpp | 15 ++++++++++++++ lib/Analysis/ValueTracking.cpp | 26 +++++++++++++++++++++++++ test/Transforms/InstSimplify/pr12251.ll | 15 ++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 test/Transforms/InstSimplify/pr12251.ll diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index 16e7a726595..28400b08b1c 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/lib/Analysis/InstructionSimplify.cpp @@ -1370,6 +1370,21 @@ static Value *SimplifyAndInst(Value *Op0, Value *Op1, const Query &Q, return Op1; } + unsigned Bitwidth = Op1->getType()->getScalarSizeInBits(); + APInt DemandedMask = APInt::getAllOnesValue(Bitwidth); + APInt KnownZero0 = APInt::getNullValue(Bitwidth); + APInt KnownOne0 = APInt::getNullValue(Bitwidth); + ComputeMaskedBits(Op0, DemandedMask, KnownZero0, KnownOne0); + APInt KnownZero1 = APInt::getNullValue(Bitwidth); + APInt KnownOne1 = APInt::getNullValue(Bitwidth); + ComputeMaskedBits(Op1, DemandedMask, KnownZero1, KnownOne1); + + if ((KnownZero0 | KnownOne1).isAllOnesValue()) + return Op0; + + if ((KnownZero1 | KnownOne0).isAllOnesValue()) + return Op1; + // Try some generic simplifications for associative operations. if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, MaxRecurse)) diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index 01e00caa3b2..17bad941e50 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/lib/Analysis/ValueTracking.cpp @@ -20,8 +20,10 @@ #include "llvm/GlobalAlias.h" #include "llvm/IntrinsicInst.h" #include "llvm/LLVMContext.h" +#include "llvm/Metadata.h" #include "llvm/Operator.h" #include "llvm/Target/TargetData.h" +#include "llvm/Support/ConstantRange.h" #include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/PatternMatch.h" @@ -195,6 +197,26 @@ static void ComputeMaskedBitsMul(Value *Op0, Value *Op1, bool NSW, KnownOne.setBit(BitWidth - 1); } +static void computeMaskedBitsLoad(const MDNode &Ranges, const APInt &Mask, + APInt &KnownZero) { + unsigned BitWidth = Mask.getBitWidth(); + unsigned NumRanges = Ranges.getNumOperands() / 2; + assert(NumRanges >= 1); + + // Use the high end of the ranges to find leading zeros. + unsigned MinLeadingZeros = BitWidth; + for (unsigned i = 0; i < NumRanges; ++i) { + ConstantInt *Lower = cast(Ranges.getOperand(2*i + 0)); + ConstantInt *Upper = cast(Ranges.getOperand(2*i + 1)); + ConstantRange Range(Lower->getValue(), Upper->getValue()); + if (Range.isWrappedSet()) + MinLeadingZeros = 0; // -1 has no zeros + unsigned LeadingZeros = (Upper->getValue() - 1).countLeadingZeros(); + MinLeadingZeros = std::min(LeadingZeros, MinLeadingZeros); + } + + KnownZero = Mask & APInt::getHighBitsSet(BitWidth, MinLeadingZeros); +} /// ComputeMaskedBits - Determine which of the bits specified in Mask are /// known to be either zero or one and return them in the KnownZero/KnownOne /// bit sets. This code only analyzes bits in Mask, in order to short-circuit @@ -315,6 +337,10 @@ void llvm::ComputeMaskedBits(Value *V, const APInt &Mask, APInt KnownZero2(KnownZero), KnownOne2(KnownOne); switch (I->getOpcode()) { default: break; + case Instruction::Load: + if (MDNode *MD = cast(I)->getMetadata(LLVMContext::MD_range)) + computeMaskedBitsLoad(*MD, Mask, KnownZero); + return; case Instruction::And: { // If either the LHS or the RHS are Zero, the result is zero. ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1); diff --git a/test/Transforms/InstSimplify/pr12251.ll b/test/Transforms/InstSimplify/pr12251.ll new file mode 100644 index 00000000000..f180f9f1eba --- /dev/null +++ b/test/Transforms/InstSimplify/pr12251.ll @@ -0,0 +1,15 @@ +; RUN: opt < %s -instsimplify -S | FileCheck %s + +define zeroext i1 @_Z3fooPb(i8* nocapture %x) { +entry: + %a = load i8* %x, align 1, !range !0 + %b = and i8 %a, 1 + %tobool = icmp ne i8 %b, 0 + ret i1 %tobool +} + +; CHECK: %a = load i8* %x, align 1, !range !0 +; CHECK-NEXT: %tobool = icmp ne i8 %a, 0 +; CHECK-NEXT: ret i1 %tobool + +!0 = metadata !{i8 0, i8 2}