bug 122:
authorReid Spencer <rspencer@reidspencer.com>
Sat, 17 Jul 2004 23:47:01 +0000 (23:47 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Sat, 17 Jul 2004 23:47:01 +0000 (23:47 +0000)
- Replace ConstantPointerRef usage with GlobalValue usage
- Minimize redundant isa<GlobalValue> usage
- Correct isa<Constant> for GlobalValue subclass

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

lib/VMCore/AsmWriter.cpp
lib/VMCore/ConstantFold.cpp

index 0f76712a2b57f47e976e30589c76eae304c0eeb0..47b4cc83aee1611ca30ffd05f8e005640a41a3a2 100644 (file)
@@ -381,6 +381,7 @@ std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
   }
 }
 
+/// @brief Internal constant writer. 
 static void WriteConstantInt(std::ostream &Out, const Constant *CV, 
                              bool PrintName,
                              std::map<const Type *, std::string> &TypeTable,
@@ -493,9 +494,6 @@ static void WriteConstantInt(std::ostream &Out, const Constant *CV,
   } else if (isa<ConstantPointerNull>(CV)) {
     Out << "null";
 
-  } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
-    WriteAsOperandInternal(Out, PR->getValue(), true, TypeTable, Machine);
-
   } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
     Out << CE->getOpcodeName() << " (";
     
@@ -527,12 +525,13 @@ static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
                                   std::map<const Type*, std::string> &TypeTable,
                                    SlotMachine *Machine) {
   Out << ' ';
-  if (PrintName && V->hasName()) {
+  if ((PrintName || isa<GlobalValue>(V)) && V->hasName())
     Out << getLLVMName(V->getName());
-  } else {
-    if (const Constant *CV = dyn_cast<Constant>(V)) {
+  else {
+    const Constant *CV = dyn_cast<Constant>(V);
+    if (CV && !isa<GlobalValue>(CV))
       WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
-    else {
+    else {
       int Slot;
       if (Machine) {
         Slot = Machine->getSlot(V);
@@ -764,8 +763,14 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
   Out << (GV->isConstant() ? "constant " : "global ");
   printType(GV->getType()->getElementType());
 
-  if (GV->hasInitializer())
-    writeOperand(GV->getInitializer(), false, false);
+  if (GV->hasInitializer()) {
+    Constant* C = cast<Constant>(GV->getInitializer());
+    assert(C &&  "GlobalVar initializer isn't constant?");
+    if (isa<GlobalValue>(C))
+      writeOperand(GV->getInitializer(), false, true);
+    else
+      writeOperand(GV->getInitializer(), false, false);
+  }
 
   printInfoComment(*GV);
   Out << "\n";
@@ -794,8 +799,9 @@ void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
     SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
 
     for (; VI != VE; ++VI) {
-      const Value *V = VI->second;
-      if (const Constant *CPV = dyn_cast<Constant>(V)) {
+      const Value* V = VI->second;
+      const Constant *CPV = dyn_cast<Constant>(V) ;
+      if (CPV && !isa<GlobalValue>(V)) {
         printConstant(CPV);
       }
     }
@@ -1162,12 +1168,6 @@ void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
 void Constant::print(std::ostream &o) const {
   if (this == 0) { o << "<null> constant value\n"; return; }
 
-  // Handle CPR's special, because they have context information...
-  if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
-    CPR->getValue()->print(o);  // Print as a global value, with context info.
-    return;
-  }
-
   o << ' ' << getType()->getDescription() << ' ';
 
   std::map<const Type *, std::string> TypeTable;
@@ -1347,10 +1347,6 @@ int SlotMachine::getSlot(const Value *V) {
   // Check for uninitialized state and do lazy initialization
   this->initialize();
 
-  // Do not number CPR's at all. They are an abomination
-  if ( const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V) )
-    V = CPR->getValue() ;
-
   // Get the type of the value
   const Type* VTy = V->getType();
 
@@ -1593,8 +1589,8 @@ unsigned SlotMachine::insertValue(const Value *V ) {
   SC_DEBUG("  Inserting value [" << VTy << "] = " << V << " slot=" << 
            DestSlot << " [");
   // G = Global, C = Constant, T = Type, F = Function, o = other
-  SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Constant>(V) ? 'C' : 
-           (isa<Function>(V) ? 'F' : 'o'))));
+  SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' : 
+           (isa<Constant>(V) ? 'C' : 'o'))));
   SC_DEBUG("]\n");
   return DestSlot;
 }
index e42de2a7e3951aaca833322b83cb7ed7c6169b0e..2a96291fe49dd2663b836d6bb9513d6debc10f00 100644 (file)
@@ -486,7 +486,7 @@ ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) {
   static DirectFPRules <ConstantFP  , double        , &Type::DoubleTy> DoubleR;
 
   if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
-      isa<ConstantPointerRef>(V1) || isa<ConstantPointerRef>(V2))
+      isa<GlobalValue>(V1) || isa<GlobalValue>(V2))
     return EmptyR;
 
   switch (V1->getType()->getTypeID()) {
@@ -525,12 +525,12 @@ Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
   if (V->getType() == DestTy) return (Constant*)V;
 
   // Cast of a global address to boolean is always true.
-  if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
+  if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
     if (DestTy == Type::BoolTy)
       // FIXME: When we support 'external weak' references, we have to prevent
       // this transformation from happening.  In the meantime we avoid folding
       // any cast of an external symbol.
-      if (!CPR->getValue()->isExternal())
+      if (!GV->isExternal())
         return ConstantBool::True;
 
   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
@@ -624,15 +624,15 @@ static int IdxCompare(Constant *C1, Constant *C2) {
 
 /// evaluateRelation - This function determines if there is anything we can
 /// decide about the two constants provided.  This doesn't need to handle simple
-/// things like integer comparisons, but should instead handle ConstantExpr's
-/// and ConstantPointerRef's.  If we can determine that the two constants have a
+/// things like integer comparisons, but should instead handle ConstantExprs
+/// and GlobalValuess.  If we can determine that the two constants have a
 /// particular relation to each other, we should return the corresponding SetCC
 /// code, otherwise return Instruction::BinaryOpsEnd.
 ///
 /// To simplify this code we canonicalize the relation so that the first
 /// operand is always the most "complex" of the two.  We consider simple
 /// constants (like ConstantInt) to be the simplest, followed by
-/// ConstantPointerRef's, followed by ConstantExpr's (the most complex).
+/// GlobalValues, followed by ConstantExpr's (the most complex).
 ///
 static Instruction::BinaryOps evaluateRelation(const Constant *V1,
                                                const Constant *V2) {
@@ -640,15 +640,15 @@ static Instruction::BinaryOps evaluateRelation(const Constant *V1,
          "Cannot compare different types of values!");
   if (V1 == V2) return Instruction::SetEQ;
 
-  if (!isa<ConstantExpr>(V1) && !isa<ConstantPointerRef>(V1)) {
+  if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
     // If the first operand is simple, swap operands.
-    assert((isa<ConstantPointerRef>(V2) || isa<ConstantExpr>(V2)) &&
+    assert((isa<GlobalValue>(V2) || isa<ConstantExpr>(V2)) &&
            "Simple cases should have been handled by caller!");
     Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
     if (SwappedRelation != Instruction::BinaryOpsEnd)
       return SetCondInst::getSwappedCondition(SwappedRelation);
 
-  } else if (const ConstantPointerRef *CPR1 = dyn_cast<ConstantPointerRef>(V1)){
+  } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)){
     if (isa<ConstantExpr>(V2)) {  // Swap as necessary.
     Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
     if (SwappedRelation != Instruction::BinaryOpsEnd)
@@ -657,11 +657,11 @@ static Instruction::BinaryOps evaluateRelation(const Constant *V1,
       return Instruction::BinaryOpsEnd;
     }
 
-    // Now we know that the RHS is a ConstantPointerRef or simple constant,
+    // Now we know that the RHS is a GlobalValue or simple constant,
     // which (since the types must match) means that it's a ConstantPointerNull.
-    if (const ConstantPointerRef *CPR2 = dyn_cast<ConstantPointerRef>(V2)) {
-      assert(CPR1->getValue() != CPR2->getValue() &&
-             "CPRs for the same value exist at different addresses??");
+    if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
+      assert(CPR1 != CPR2 &&
+             "GVs for the same value exist at different addresses??");
       // FIXME: If both globals are external weak, they might both be null!
       return Instruction::SetNE;
     } else {
@@ -693,7 +693,7 @@ static Instruction::BinaryOps evaluateRelation(const Constant *V1,
       if (isa<ConstantPointerNull>(V2)) {
         // If we are comparing a GEP to a null pointer, check to see if the base
         // of the GEP equals the null pointer.
-        if (isa<ConstantPointerRef>(CE1Op0)) {
+        if (isa<GlobalValue>(CE1Op0)) {
           // FIXME: this is not true when we have external weak references!
           // No offset can go from a global to a null pointer.
           return Instruction::SetGT;
@@ -708,13 +708,11 @@ static Instruction::BinaryOps evaluateRelation(const Constant *V1,
           return Instruction::SetEQ;
         }
         // Otherwise, we can't really say if the first operand is null or not.
-      } else if (const ConstantPointerRef *CPR2 =
-                                             dyn_cast<ConstantPointerRef>(V2)) {
+      } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
         if (isa<ConstantPointerNull>(CE1Op0)) {
           // FIXME: This is not true with external weak references.
           return Instruction::SetLT;
-        } else if (const ConstantPointerRef *CPR1 =
-                   dyn_cast<ConstantPointerRef>(CE1Op0)) {
+        } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
           if (CPR1 == CPR2) {
             // If this is a getelementptr of the same global, then it must be
             // different.  Because the types must match, the getelementptr could
@@ -741,8 +739,7 @@ static Instruction::BinaryOps evaluateRelation(const Constant *V1,
         case Instruction::GetElementPtr:
           // By far the most common case to handle is when the base pointers are
           // obviously to the same or different globals.
-          if (isa<ConstantPointerRef>(CE1Op0) &&
-              isa<ConstantPointerRef>(CE2Op0)) {
+          if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
             if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
               return Instruction::SetNE;
             // Ok, we know that both getelementptr instructions are based on the
@@ -896,13 +893,13 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
           return const_cast<Constant*>(V1);                       // X & -1 == X
         if (V2->isNullValue()) return const_cast<Constant*>(V2);  // X & 0 == 0
         if (CE1->getOpcode() == Instruction::Cast &&
-            isa<ConstantPointerRef>(CE1->getOperand(0))) {
-          ConstantPointerRef *CPR =cast<ConstantPointerRef>(CE1->getOperand(0));
+            isa<GlobalValue>(CE1->getOperand(0))) {
+          GlobalValue *CPR =cast<GlobalValue>(CE1->getOperand(0));
 
           // Functions are at least 4-byte aligned.  If and'ing the address of a
           // function with a constant < 4, fold it to zero.
           if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
-            if (CI->getRawValue() < 4 && isa<Function>(CPR->getValue()))
+            if (CI->getRawValue() < 4 && isa<Function>(CPR))
               return Constant::getNullValue(CI->getType());
         }
         break;