[llvm-commits] [llvm] r76232 - in /llvm/trunk: include/llvm/BinaryOperators.h include/llvm/Operator.h lib/Analysis/ScalarEvolution.cpp lib/Analysis/ValueTracking.cpp lib/Transforms/Scalar/InstructionCombining.cpp

Dan Gohman gohman at apple.com
Fri Jul 17 13:47:04 PDT 2009


Author: djg
Date: Fri Jul 17 15:47:02 2009
New Revision: 76232

URL: http://llvm.org/viewvc/llvm-project?rev=76232&view=rev
Log:
Add a new Operator class, for handling Instructions and ConstantExprs
in a convenient manner, factoring out some common code from
InstructionCombining and ValueTracking. Move the contents of
BinaryOperators.h into Operator.h and use Operator to generalize them
to support ConstantExprs as well as Instructions.

Added:
    llvm/trunk/include/llvm/Operator.h
Removed:
    llvm/trunk/include/llvm/BinaryOperators.h
Modified:
    llvm/trunk/lib/Analysis/ScalarEvolution.cpp
    llvm/trunk/lib/Analysis/ValueTracking.cpp
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Removed: llvm/trunk/include/llvm/BinaryOperators.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/BinaryOperators.h?rev=76231&view=auto

==============================================================================
--- llvm/trunk/include/llvm/BinaryOperators.h (original)
+++ llvm/trunk/include/llvm/BinaryOperators.h (removed)
@@ -1,112 +0,0 @@
-//===-- llvm/BinaryOperators.h - BinaryOperator subclasses ------*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file defines various classes for working with specific BinaryOperators,
-// exposing special properties that individual operations have.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_BINARY_OPTERATORS_H
-#define LLVM_BINARY_OPTERATORS_H
-
-#include "llvm/InstrTypes.h"
-
-namespace llvm {
-
-//===----------------------------------------------------------------------===//
-//                      SpecificBinaryOperator Class
-//===----------------------------------------------------------------------===//
-
-/// SpecificBinaryOperator - This is a base class for utility classes that
-/// provide additional information about binary operator instructions with
-/// specific opcodes.
-///
-class SpecificBinaryOperator : public BinaryOperator {
-private:
-  // Do not implement any of these. The SpecificBinaryOperator class is
-  // intended to be used as a utility, and is never itself instantiated.
-  void *operator new(size_t, unsigned);
-  void *operator new(size_t s);
-  void Create(BinaryOps Op, Value *S1, Value *S2,
-              const std::string &Name = "",
-              Instruction *InsertBefore = 0);
-  void Create(BinaryOps Op, Value *S1, Value *S2,
-              const std::string &Name,
-              BasicBlock *InsertAtEnd);
-  SpecificBinaryOperator();
-  ~SpecificBinaryOperator();
-  void init(BinaryOps iType);
-};
-
-/// OverflowingBinaryOperator - Base class for integer arithmetic operators
-/// which may exhibit overflow - Add, Sub, and Mul.
-///
-class OverflowingBinaryOperator : public SpecificBinaryOperator {
-public:
-  /// hasNoSignedOverflow - Test whether this operation is known to never
-  /// undergo signed overflow.
-  bool hasNoSignedOverflow() const {
-    return SubclassOptionalData & (1 << 0);
-  }
-  void setHasNoSignedOverflow(bool B) {
-    SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
-  }
-
-  /// hasNoUnsignedOverflow - Test whether this operation is known to never
-  /// undergo unsigned overflow.
-  bool hasNoUnsignedOverflow() const {
-    return SubclassOptionalData & (1 << 1);
-  }
-  void setHasNoUnsignedOverflow(bool B) {
-    SubclassOptionalData = (SubclassOptionalData & ~(1 << 1)) | (B << 1);
-  }
-
-  static inline bool classof(const OverflowingBinaryOperator *) { return true; }
-  static inline bool classof(const BinaryOperator *BO) {
-    return BO->getOpcode() == Instruction::Add ||
-           BO->getOpcode() == Instruction::Sub ||
-           BO->getOpcode() == Instruction::Mul;
-  }
-  static inline bool classof(const Instruction *I) {
-    return I->isBinaryOp() && classof(cast<BinaryOperator>(I));
-  }
-  static inline bool classof(const Value *V) {
-    return isa<Instruction>(V) && classof(cast<Instruction>(V));
-  }
-};
-
-/// UDivInst - BinaryOperators with opcode Instruction::UDiv.
-///
-class UDivInst : public SpecificBinaryOperator {
-public:
-  /// isExact - Test whether this division is known to be exact, with
-  /// zero remainder.
-  bool isExact() const {
-    return SubclassOptionalData & (1 << 0);
-  }
-  void setIsExact(bool B) {
-    SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
-  }
-
-  // Methods for support type inquiry through isa, cast, and dyn_cast:
-  static inline bool classof(const UDivInst *) { return true; }
-  static inline bool classof(const BinaryOperator *BO) {
-    return BO->getOpcode() == Instruction::UDiv;
-  }
-  static inline bool classof(const Instruction *I) {
-    return I->isBinaryOp() && classof(cast<BinaryOperator>(I));
-  }
-  static inline bool classof(const Value *V) {
-    return isa<Instruction>(V) && classof(cast<Instruction>(V));
-  }
-};
-
-} // End llvm namespace
-
-#endif

Added: llvm/trunk/include/llvm/Operator.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Operator.h?rev=76232&view=auto

==============================================================================
--- llvm/trunk/include/llvm/Operator.h (added)
+++ llvm/trunk/include/llvm/Operator.h Fri Jul 17 15:47:02 2009
@@ -0,0 +1,132 @@
+//===-- llvm/Operator.h - Operator utility subclass -------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines various classes for working with Instructions and
+// ConstantExprs.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_OPERATOR_H
+#define LLVM_OPERATOR_H
+
+#include "llvm/Instruction.h"
+#include "llvm/Constants.h"
+
+namespace llvm {
+
+/// Operator - This is a utility class that provides an abstraction for the
+/// common functionality between Instructions and ConstantExprs.
+///
+class Operator : public User {
+private:
+  // Do not implement any of these. The Operator class is intended to be used
+  // as a utility, and is never itself instantiated.
+  void *operator new(size_t, unsigned);
+  void *operator new(size_t s);
+  Operator();
+  ~Operator();
+
+public:
+  /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
+  ///
+  unsigned getOpcode() const {
+    if (const Instruction *I = dyn_cast<Instruction>(this))
+      return I->getOpcode();
+    return cast<ConstantExpr>(this)->getOpcode();
+  }
+
+  /// getOpcode - If V is an Instruction or ConstantExpr, return its
+  /// opcode. Otherwise return UserOp1.
+  ///
+  static unsigned getOpcode(const Value *V) {
+    if (const Instruction *I = dyn_cast<Instruction>(V))
+      return I->getOpcode();
+    if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
+      return CE->getOpcode();
+    return Instruction::UserOp1;
+  }
+
+  static inline bool classof(const Operator *) { return true; }
+  static inline bool classof(const Instruction *I) { return true; }
+  static inline bool classof(const ConstantExpr *I) { return true; }
+  static inline bool classof(const Value *V) {
+    return isa<Instruction>(V) || isa<ConstantExpr>(V);
+  }
+};
+
+/// OverflowingBinaryOperator - Utility class for integer arithmetic operators
+/// which may exhibit overflow - Add, Sub, and Mul.
+///
+class OverflowingBinaryOperator : public Operator {
+public:
+  /// hasNoSignedOverflow - Test whether this operation is known to never
+  /// undergo signed overflow.
+  bool hasNoSignedOverflow() const {
+    return SubclassOptionalData & (1 << 0);
+  }
+  void setHasNoSignedOverflow(bool B) {
+    SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
+  }
+
+  /// hasNoUnsignedOverflow - Test whether this operation is known to never
+  /// undergo unsigned overflow.
+  bool hasNoUnsignedOverflow() const {
+    return SubclassOptionalData & (1 << 1);
+  }
+  void setHasNoUnsignedOverflow(bool B) {
+    SubclassOptionalData = (SubclassOptionalData & ~(1 << 1)) | (B << 1);
+  }
+
+  static inline bool classof(const OverflowingBinaryOperator *) { return true; }
+  static inline bool classof(const Instruction *I) {
+    return I->getOpcode() == Instruction::Add ||
+           I->getOpcode() == Instruction::Sub ||
+           I->getOpcode() == Instruction::Mul;
+  }
+  static inline bool classof(const ConstantExpr *CE) {
+    return CE->getOpcode() == Instruction::Add ||
+           CE->getOpcode() == Instruction::Sub ||
+           CE->getOpcode() == Instruction::Mul;
+  }
+  static inline bool classof(const Value *V) {
+    return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
+           (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
+  }
+};
+
+/// UDivOperator - An Operator with opcode Instruction::UDiv.
+///
+class UDivOperator : public Operator {
+public:
+  /// isExact - Test whether this division is known to be exact, with
+  /// zero remainder.
+  bool isExact() const {
+    return SubclassOptionalData & (1 << 0);
+  }
+  void setIsExact(bool B) {
+    SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
+  }
+
+  // Methods for support type inquiry through isa, cast, and dyn_cast:
+  static inline bool classof(const UDivOperator *) { return true; }
+  static inline bool classof(const ConstantExpr *CE) {
+    return CE->getOpcode() == Instruction::UDiv;
+  }
+  static inline bool classof(const Instruction *I) {
+    return I->getOpcode() == Instruction::UDiv;
+  }
+  static inline bool classof(const Value *V) {
+    return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
+           (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
+  }
+};
+
+} // End llvm namespace
+
+#endif

Modified: llvm/trunk/lib/Analysis/ScalarEvolution.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=76232&r1=76231&r2=76232&view=diff

==============================================================================
--- llvm/trunk/lib/Analysis/ScalarEvolution.cpp (original)
+++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp Fri Jul 17 15:47:02 2009
@@ -66,6 +66,7 @@
 #include "llvm/GlobalVariable.h"
 #include "llvm/Instructions.h"
 #include "llvm/LLVMContext.h"
+#include "llvm/Operator.h"
 #include "llvm/Analysis/ConstantFolding.h"
 #include "llvm/Analysis/Dominators.h"
 #include "llvm/Analysis/LoopInfo.h"
@@ -2430,7 +2431,7 @@
 /// createNodeForGEP - Expand GEP instructions into add and multiply
 /// operations. This allows them to be analyzed by regular SCEV code.
 ///
-const SCEV *ScalarEvolution::createNodeForGEP(User *GEP) {
+const SCEV *ScalarEvolution::createNodeForGEP(Operator *GEP) {
 
   const Type *IntPtrTy = TD->getIntPtrType();
   Value *Base = GEP->getOperand(0);
@@ -2779,7 +2780,7 @@
   else
     return getUnknown(V);
 
-  User *U = cast<User>(V);
+  Operator *U = cast<Operator>(V);
   switch (Opcode) {
   case Instruction::Add:
     return getAddExpr(getSCEV(U->getOperand(0)),

Modified: llvm/trunk/lib/Analysis/ValueTracking.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ValueTracking.cpp?rev=76232&r1=76231&r2=76232&view=diff

==============================================================================
--- llvm/trunk/lib/Analysis/ValueTracking.cpp (original)
+++ llvm/trunk/lib/Analysis/ValueTracking.cpp Fri Jul 17 15:47:02 2009
@@ -18,24 +18,13 @@
 #include "llvm/GlobalVariable.h"
 #include "llvm/IntrinsicInst.h"
 #include "llvm/LLVMContext.h"
+#include "llvm/Operator.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/Support/GetElementPtrTypeIterator.h"
 #include "llvm/Support/MathExtras.h"
 #include <cstring>
 using namespace llvm;
 
-/// getOpcode - If this is an Instruction or a ConstantExpr, return the
-/// opcode value. Otherwise return UserOp1.
-static unsigned getOpcode(const Value *V) {
-  if (const Instruction *I = dyn_cast<Instruction>(V))
-    return I->getOpcode();
-  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
-    return CE->getOpcode();
-  // Use UserOp1 to mean there's no opcode.
-  return Instruction::UserOp1;
-}
-
-
 /// 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
@@ -108,11 +97,11 @@
   if (Depth == MaxDepth || Mask == 0)
     return;  // Limit search depth.
 
-  User *I = dyn_cast<User>(V);
+  Operator *I = dyn_cast<Operator>(V);
   if (!I) return;
 
   APInt KnownZero2(KnownZero), KnownOne2(KnownOne);
-  switch (getOpcode(I)) {
+  switch (I->getOpcode()) {
   default: break;
   case Instruction::And: {
     // If either the LHS or the RHS are Zero, the result is zero.
@@ -383,7 +372,7 @@
     // Determine which operand has more trailing zeros, and use that
     // many bits from the other operand.
     if (LHSKnownZeroOut > RHSKnownZeroOut) {
-      if (getOpcode(I) == Instruction::Add) {
+      if (I->getOpcode() == Instruction::Add) {
         APInt Mask = APInt::getLowBitsSet(BitWidth, LHSKnownZeroOut);
         KnownZero |= KnownZero2 & Mask;
         KnownOne  |= KnownOne2 & Mask;
@@ -523,10 +512,10 @@
       for (unsigned i = 0; i != 2; ++i) {
         Value *L = P->getIncomingValue(i);
         Value *R = P->getIncomingValue(!i);
-        User *LU = dyn_cast<User>(L);
+        Operator *LU = dyn_cast<Operator>(L);
         if (!LU)
           continue;
-        unsigned Opcode = getOpcode(LU);
+        unsigned Opcode = LU->getOpcode();
         // Check for operations that have the property that if
         // both their operands have low zero bits, the result
         // will have low zero bits.
@@ -643,8 +632,8 @@
   if (Depth == 6)
     return 1;  // Limit search depth.
   
-  User *U = dyn_cast<User>(V);
-  switch (getOpcode(V)) {
+  Operator *U = dyn_cast<Operator>(V);
+  switch (Operator::getOpcode(V)) {
   default: break;
   case Instruction::SExt:
     Tmp = TyBits-cast<IntegerType>(U->getOperand(0)->getType())->getBitWidth();
@@ -790,7 +779,7 @@
   if (Depth == 6)
     return 1;  // Limit search depth.
 
-  const Instruction *I = dyn_cast<Instruction>(V);
+  const Operator *I = dyn_cast<Operator>(V);
   if (I == 0) return false;
   
   // (add x, 0.0) is guaranteed to return +0.0, not -0.0.

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=76232&r1=76231&r2=76232&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Fri Jul 17 15:47:02 2009
@@ -40,6 +40,7 @@
 #include "llvm/Pass.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/GlobalVariable.h"
+#include "llvm/Operator.h"
 #include "llvm/Analysis/ConstantFolding.h"
 #include "llvm/Analysis/ValueTracking.h"
 #include "llvm/Target/TargetData.h"
@@ -650,17 +651,6 @@
   return false;
 }
 
-/// getOpcode - If this is an Instruction or a ConstantExpr, return the
-/// opcode value. Otherwise return UserOp1.
-static unsigned getOpcode(const Value *V) {
-  if (const Instruction *I = dyn_cast<Instruction>(V))
-    return I->getOpcode();
-  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
-    return CE->getOpcode();
-  // Use UserOp1 to mean there's no opcode.
-  return Instruction::UserOp1;
-}
-
 /// AddOne - Add one to a ConstantInt
 static Constant *AddOne(Constant *C, LLVMContext *Context) {
   return Context->getConstantExprAdd(C, 
@@ -8710,7 +8700,7 @@
 
   // See if the value being truncated is already sign extended.  If so, just
   // eliminate the trunc/sext pair.
-  if (getOpcode(Src) == Instruction::Trunc) {
+  if (Operator::getOpcode(Src) == Instruction::Trunc) {
     Value *Op = cast<User>(Src)->getOperand(0);
     unsigned OpBits   = Op->getType()->getScalarSizeInBits();
     unsigned MidBits  = Src->getType()->getScalarSizeInBits();
@@ -9625,7 +9615,7 @@
   User *U = dyn_cast<User>(V);
   if (!U) return Align;
 
-  switch (getOpcode(U)) {
+  switch (Operator::getOpcode(U)) {
   default: break;
   case Instruction::BitCast:
     return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);





More information about the llvm-commits mailing list