Add a new Operator class, for handling Instructions and ConstantExprs
authorDan Gohman <gohman@apple.com>
Fri, 17 Jul 2009 20:47:02 +0000 (20:47 +0000)
committerDan Gohman <gohman@apple.com>
Fri, 17 Jul 2009 20:47:02 +0000 (20:47 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76232 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/BinaryOperators.h [deleted file]
include/llvm/Operator.h [new file with mode: 0644]
lib/Analysis/ScalarEvolution.cpp
lib/Analysis/ValueTracking.cpp
lib/Transforms/Scalar/InstructionCombining.cpp

diff --git a/include/llvm/BinaryOperators.h b/include/llvm/BinaryOperators.h
deleted file mode 100644 (file)
index 4dce9af..0000000
+++ /dev/null
@@ -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
diff --git a/include/llvm/Operator.h b/include/llvm/Operator.h
new file mode 100644 (file)
index 0000000..4da1921
--- /dev/null
@@ -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
index 1b0b37ba0105d2ea88da706b034f94f44a3ef467..6c23f401c5b1b82a1b7d72de5f34c372ae35376b 100644 (file)
@@ -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 @@ const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
 /// 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 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) {
   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)),
index ddf17524497fac53a07860072e8c9769181f3333..4cca313101f8e48d5a3c93445c88b77d3801449a 100644 (file)
 #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 @@ void llvm::ComputeMaskedBits(Value *V, const APInt &Mask,
   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 @@ void llvm::ComputeMaskedBits(Value *V, const APInt &Mask,
     // 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 @@ void llvm::ComputeMaskedBits(Value *V, const APInt &Mask,
       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 @@ unsigned llvm::ComputeNumSignBits(Value *V, TargetData *TD, unsigned Depth) {
   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 @@ bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) {
   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.
index 2b7ca6bbaaa62bf18c2800c8b3722f081532054e..1c25bec304b004fe4e2e6eb12f286c27d489381c 100644 (file)
@@ -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 @@ static User *dyn_castGetElementPtr(Value *V) {
   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 @@ Instruction *InstCombiner::visitSExt(SExtInst &CI) {
 
   // 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 @@ static unsigned EnforceKnownAlignment(Value *V,
   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);