[llvm] r249686 - Treat Mul just like Add and Subtract

James Molloy via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 8 05:39:59 PDT 2015


Author: jamesm
Date: Thu Oct  8 07:39:59 2015
New Revision: 249686

URL: http://llvm.org/viewvc/llvm-project?rev=249686&view=rev
Log:
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.

Added:
    llvm/trunk/test/Analysis/DemandedBits/
    llvm/trunk/test/Analysis/DemandedBits/basic.ll
Modified:
    llvm/trunk/include/llvm/Analysis/DemandedBits.h
    llvm/trunk/lib/Analysis/DemandedBits.cpp

Modified: llvm/trunk/include/llvm/Analysis/DemandedBits.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/DemandedBits.h?rev=249686&r1=249685&r2=249686&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/DemandedBits.h (original)
+++ llvm/trunk/include/llvm/Analysis/DemandedBits.h Thu Oct  8 07:39:59 2015
@@ -41,7 +41,8 @@ struct DemandedBits : public FunctionPas
 
   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);
 

Modified: llvm/trunk/lib/Analysis/DemandedBits.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/DemandedBits.cpp?rev=249686&r1=249685&r2=249686&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/DemandedBits.cpp (original)
+++ llvm/trunk/lib/Analysis/DemandedBits.cpp Thu Oct  8 07:39:59 2015
@@ -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::determineLiveOperandB
     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(Ins
     !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();
 }

Added: llvm/trunk/test/Analysis/DemandedBits/basic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/DemandedBits/basic.ll?rev=249686&view=auto
==============================================================================
--- llvm/trunk/test/Analysis/DemandedBits/basic.ll (added)
+++ llvm/trunk/test/Analysis/DemandedBits/basic.ll Thu Oct  8 07:39:59 2015
@@ -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
+}




More information about the llvm-commits mailing list