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);
#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"
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).
!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<DemandedBits*>(this)->performAnalysis();
+ for (auto &KV : AliveBits) {
+ OS << "DemandedBits: 0x" << utohexstr(KV.second.getLimitedValue()) << " for "
+ << *KV.first << "\n";
+ }
+}
+
FunctionPass *llvm::createDemandedBitsPass() {
return new DemandedBits();
}
--- /dev/null
+; 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
+}