From 6f819bd488d29bec2e60a4fff25d848f46ba36fb Mon Sep 17 00:00:00 2001 From: James Molloy Date: Thu, 8 Oct 2015 12:39:59 +0000 Subject: [PATCH] Treat Mul just like Add and Subtract Like adds and subtracts, muls ripple only to the left so we can use the same logic. While we're here, add a print method to DemandedBits so it can be used with -analyze, which we'll use in the testcase. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@249686 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Analysis/DemandedBits.h | 3 ++- lib/Analysis/DemandedBits.cpp | 12 ++++++++++++ test/Analysis/DemandedBits/basic.ll | 12 ++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/Analysis/DemandedBits/basic.ll diff --git a/include/llvm/Analysis/DemandedBits.h b/include/llvm/Analysis/DemandedBits.h index d7562a5585c..42932bfd349 100644 --- a/include/llvm/Analysis/DemandedBits.h +++ b/include/llvm/Analysis/DemandedBits.h @@ -41,7 +41,8 @@ struct DemandedBits : public FunctionPass { bool runOnFunction(Function& F) override; void getAnalysisUsage(AnalysisUsage& AU) const override; - + void print(raw_ostream &OS, const Module *M) const override; + /// Return the bits demanded from instruction I. APInt getDemandedBits(Instruction *I); diff --git a/lib/Analysis/DemandedBits.cpp b/lib/Analysis/DemandedBits.cpp index 775cbac4c53..6f92ba6289a 100644 --- a/lib/Analysis/DemandedBits.cpp +++ b/lib/Analysis/DemandedBits.cpp @@ -25,6 +25,7 @@ #include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/Analysis/AssumptionCache.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/IR/BasicBlock.h" @@ -131,6 +132,7 @@ void DemandedBits::determineLiveOperandBits( break; case Instruction::Add: case Instruction::Sub: + case Instruction::Mul: // Find the highest live output bit. We don't need any more input // bits than that (adds, and thus subtracts, ripple only to the // left). @@ -368,6 +370,16 @@ bool DemandedBits::isInstructionDead(Instruction *I) { !isAlwaysLive(I); } +void DemandedBits::print(raw_ostream &OS, const Module *M) const { + // This is gross. But the alternative is making all the state mutable + // just because of this one debugging method. + const_cast(this)->performAnalysis(); + for (auto &KV : AliveBits) { + OS << "DemandedBits: 0x" << utohexstr(KV.second.getLimitedValue()) << " for " + << *KV.first << "\n"; + } +} + FunctionPass *llvm::createDemandedBitsPass() { return new DemandedBits(); } diff --git a/test/Analysis/DemandedBits/basic.ll b/test/Analysis/DemandedBits/basic.ll new file mode 100644 index 00000000000..3fd1b321288 --- /dev/null +++ b/test/Analysis/DemandedBits/basic.ll @@ -0,0 +1,12 @@ +; RUN: opt -S -demanded-bits -analyze < %s | FileCheck %s + +; CHECK-LABEL: 'test_mul' +; CHECK-DAG: DemandedBits: 0xFF for %1 = add nsw i32 %a, 5 +; CHECK-DAG: DemandedBits: 0xFF for %3 = trunc i32 %2 to i8 +; CHECK-DAG: DemandedBits: 0xFF for %2 = mul nsw i32 %1, %b +define i8 @test_mul(i32 %a, i32 %b) { + %1 = add nsw i32 %a, 5 + %2 = mul nsw i32 %1, %b + %3 = trunc i32 %2 to i8 + ret i8 %3 +} -- 2.34.1